Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/highmem.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
| 26 | #include <linux/mpage.h> |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 27 | #include <linux/falloc.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 28 | #include <linux/swap.h> |
| 29 | #include <linux/writeback.h> |
| 30 | #include <linux/statfs.h> |
| 31 | #include <linux/compat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include "ctree.h" |
| 34 | #include "disk-io.h" |
| 35 | #include "transaction.h" |
| 36 | #include "btrfs_inode.h" |
| 37 | #include "ioctl.h" |
| 38 | #include "print-tree.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 39 | #include "tree-log.h" |
| 40 | #include "locking.h" |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 41 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | |
| 43 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 44 | /* simple helper to fault in pages and copy. This should go away |
| 45 | * and be replaced with calls into generic code. |
| 46 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 47 | static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 48 | int write_bytes, |
| 49 | struct page **prepared_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 50 | struct iov_iter *i) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 52 | size_t copied = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 53 | int pg = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 55 | int total_copied = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 56 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 57 | while (write_bytes > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 58 | size_t count = min_t(size_t, |
| 59 | PAGE_CACHE_SIZE - offset, write_bytes); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 60 | struct page *page = prepared_pages[pg]; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Copy data from userspace to the current page |
| 63 | * |
| 64 | * Disable pagefault to avoid recursive lock since |
| 65 | * the pages are already locked |
| 66 | */ |
| 67 | pagefault_disable(); |
| 68 | copied = iov_iter_copy_from_user_atomic(page, i, offset, count); |
| 69 | pagefault_enable(); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 70 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | /* Flush processor's dcache for this page */ |
| 72 | flush_dcache_page(page); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 73 | iov_iter_advance(i, copied); |
| 74 | write_bytes -= copied; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 75 | total_copied += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 77 | /* Return to btrfs_file_aio_write to fault page */ |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 78 | if (unlikely(copied == 0)) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 79 | break; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { |
| 83 | offset += copied; |
| 84 | } else { |
| 85 | pg++; |
| 86 | offset = 0; |
| 87 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 88 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 89 | return total_copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 92 | /* |
| 93 | * unlocks pages after btrfs_file_write is done with them |
| 94 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 95 | static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 96 | { |
| 97 | size_t i; |
| 98 | for (i = 0; i < num_pages; i++) { |
| 99 | if (!pages[i]) |
| 100 | break; |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 101 | /* page checked is some magic around finding pages that |
| 102 | * have been modified without going through btrfs_set_page_dirty |
| 103 | * clear it here |
| 104 | */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 105 | ClearPageChecked(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 106 | unlock_page(pages[i]); |
| 107 | mark_page_accessed(pages[i]); |
| 108 | page_cache_release(pages[i]); |
| 109 | } |
| 110 | } |
| 111 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 112 | /* |
| 113 | * after copy_from_user, pages need to be dirtied and we need to make |
| 114 | * sure holes are created between the current EOF and the start of |
| 115 | * any next extents (if required). |
| 116 | * |
| 117 | * this also makes the decision about creating an inline extent vs |
| 118 | * doing real data extents, marking pages dirty and delalloc as required. |
| 119 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 120 | static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 121 | struct btrfs_root *root, |
| 122 | struct file *file, |
| 123 | struct page **pages, |
| 124 | size_t num_pages, |
| 125 | loff_t pos, |
| 126 | size_t write_bytes) |
| 127 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 128 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 129 | int i; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 130 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 131 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 132 | u64 start_pos; |
| 133 | u64 end_of_last_block; |
| 134 | u64 end_pos = pos + write_bytes; |
| 135 | loff_t isize = i_size_read(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 136 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 137 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 138 | num_bytes = (write_bytes + pos - start_pos + |
| 139 | root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 140 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 141 | end_of_last_block = start_pos + num_bytes - 1; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 142 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
| 143 | NULL); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 144 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 145 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | for (i = 0; i < num_pages; i++) { |
| 147 | struct page *p = pages[i]; |
| 148 | SetPageUptodate(p); |
| 149 | ClearPageChecked(p); |
| 150 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 151 | } |
| 152 | if (end_pos > isize) { |
| 153 | i_size_write(inode, end_pos); |
Chris Mason | f597bb1 | 2009-06-27 21:06:22 -0400 | [diff] [blame] | 154 | /* we've only changed i_size in ram, and we haven't updated |
| 155 | * the disk i_size. There is no need to log the inode |
| 156 | * at this time. |
| 157 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 158 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 159 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 162 | /* |
| 163 | * this drops all the extents in the cache that intersect the range |
| 164 | * [start, end]. Existing extents are split as required. |
| 165 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 166 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end, |
| 167 | int skip_pinned) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 168 | { |
| 169 | struct extent_map *em; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 170 | struct extent_map *split = NULL; |
| 171 | struct extent_map *split2 = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 172 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 173 | u64 len = end - start + 1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 174 | int ret; |
| 175 | int testend = 1; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 176 | unsigned long flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 177 | int compressed = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 178 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 179 | WARN_ON(end < start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 180 | if (end == (u64)-1) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 181 | len = (u64)-1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 182 | testend = 0; |
| 183 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 184 | while (1) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 185 | if (!split) |
| 186 | split = alloc_extent_map(GFP_NOFS); |
| 187 | if (!split2) |
| 188 | split2 = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 189 | BUG_ON(!split || !split2); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 190 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 191 | write_lock(&em_tree->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 192 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 193 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 194 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 195 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 196 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 197 | flags = em->flags; |
| 198 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 199 | if (testend && em->start + em->len >= start + len) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 200 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 201 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 202 | break; |
| 203 | } |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 204 | start = em->start + em->len; |
| 205 | if (testend) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 206 | len = start + len - (em->start + em->len); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 207 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 208 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 209 | continue; |
| 210 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | 3ce7e67 | 2008-07-31 15:42:54 -0400 | [diff] [blame] | 212 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 213 | remove_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 214 | |
| 215 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 216 | em->start < start) { |
| 217 | split->start = em->start; |
| 218 | split->len = start - em->start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 219 | split->orig_start = em->orig_start; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 220 | split->block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | |
| 222 | if (compressed) |
| 223 | split->block_len = em->block_len; |
| 224 | else |
| 225 | split->block_len = split->len; |
| 226 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 227 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 228 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 229 | split->compress_type = em->compress_type; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 230 | ret = add_extent_mapping(em_tree, split); |
| 231 | BUG_ON(ret); |
| 232 | free_extent_map(split); |
| 233 | split = split2; |
| 234 | split2 = NULL; |
| 235 | } |
| 236 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 237 | testend && em->start + em->len > start + len) { |
| 238 | u64 diff = start + len - em->start; |
| 239 | |
| 240 | split->start = start + len; |
| 241 | split->len = em->start + em->len - (start + len); |
| 242 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 243 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 244 | split->compress_type = em->compress_type; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 245 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 246 | if (compressed) { |
| 247 | split->block_len = em->block_len; |
| 248 | split->block_start = em->block_start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 249 | split->orig_start = em->orig_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | } else { |
| 251 | split->block_len = split->len; |
| 252 | split->block_start = em->block_start + diff; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 253 | split->orig_start = split->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 255 | |
| 256 | ret = add_extent_mapping(em_tree, split); |
| 257 | BUG_ON(ret); |
| 258 | free_extent_map(split); |
| 259 | split = NULL; |
| 260 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 261 | write_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 262 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 263 | /* once for us */ |
| 264 | free_extent_map(em); |
| 265 | /* once for the tree*/ |
| 266 | free_extent_map(em); |
| 267 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 268 | if (split) |
| 269 | free_extent_map(split); |
| 270 | if (split2) |
| 271 | free_extent_map(split2); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 275 | /* |
| 276 | * this is very complex, but the basic idea is to drop all extents |
| 277 | * in the range start - end. hint_block is filled in with a block number |
| 278 | * that would be a good hint to the block allocator for this file. |
| 279 | * |
| 280 | * If an extent intersects the range but is not entirely inside the range |
| 281 | * it is either truncated or split. Anything entirely inside the range |
| 282 | * is deleted from the tree. |
| 283 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 284 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode, |
| 285 | u64 start, u64 end, u64 *hint_byte, int drop_cache) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 286 | { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 287 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 288 | struct extent_buffer *leaf; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 289 | struct btrfs_file_extent_item *fi; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 290 | struct btrfs_path *path; |
| 291 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 292 | struct btrfs_key new_key; |
| 293 | u64 search_start = start; |
| 294 | u64 disk_bytenr = 0; |
| 295 | u64 num_bytes = 0; |
| 296 | u64 extent_offset = 0; |
| 297 | u64 extent_end = 0; |
| 298 | int del_nr = 0; |
| 299 | int del_slot = 0; |
| 300 | int extent_type; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 301 | int recow; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 302 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 303 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 304 | if (drop_cache) |
| 305 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 306 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 307 | path = btrfs_alloc_path(); |
| 308 | if (!path) |
| 309 | return -ENOMEM; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 310 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 311 | while (1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 312 | recow = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 313 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 314 | search_start, -1); |
| 315 | if (ret < 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 316 | break; |
| 317 | if (ret > 0 && path->slots[0] > 0 && search_start == start) { |
| 318 | leaf = path->nodes[0]; |
| 319 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
| 320 | if (key.objectid == inode->i_ino && |
| 321 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 322 | path->slots[0]--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 323 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 324 | ret = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 325 | next_slot: |
| 326 | leaf = path->nodes[0]; |
| 327 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 328 | BUG_ON(del_nr > 0); |
| 329 | ret = btrfs_next_leaf(root, path); |
| 330 | if (ret < 0) |
| 331 | break; |
| 332 | if (ret > 0) { |
| 333 | ret = 0; |
| 334 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 335 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 336 | leaf = path->nodes[0]; |
| 337 | recow = 1; |
| 338 | } |
| 339 | |
| 340 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 341 | if (key.objectid > inode->i_ino || |
| 342 | key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end) |
| 343 | break; |
| 344 | |
| 345 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 346 | struct btrfs_file_extent_item); |
| 347 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 348 | |
| 349 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 350 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 351 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 352 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 353 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
| 354 | extent_end = key.offset + |
| 355 | btrfs_file_extent_num_bytes(leaf, fi); |
| 356 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 357 | extent_end = key.offset + |
| 358 | btrfs_file_extent_inline_len(leaf, fi); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 359 | } else { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 360 | WARN_ON(1); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 361 | extent_end = search_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 364 | if (extent_end <= search_start) { |
| 365 | path->slots[0]++; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 366 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 369 | search_start = max(key.offset, start); |
| 370 | if (recow) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 371 | btrfs_release_path(root, path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 372 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 373 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 374 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 375 | /* |
| 376 | * | - range to drop - | |
| 377 | * | -------- extent -------- | |
| 378 | */ |
| 379 | if (start > key.offset && end < extent_end) { |
| 380 | BUG_ON(del_nr > 0); |
| 381 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 382 | |
| 383 | memcpy(&new_key, &key, sizeof(new_key)); |
| 384 | new_key.offset = start; |
| 385 | ret = btrfs_duplicate_item(trans, root, path, |
| 386 | &new_key); |
| 387 | if (ret == -EAGAIN) { |
| 388 | btrfs_release_path(root, path); |
| 389 | continue; |
| 390 | } |
| 391 | if (ret < 0) |
| 392 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 393 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 394 | leaf = path->nodes[0]; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 395 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 396 | struct btrfs_file_extent_item); |
| 397 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 398 | start - key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 399 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 400 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 401 | struct btrfs_file_extent_item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 402 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 403 | extent_offset += start - key.offset; |
| 404 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 405 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 406 | extent_end - start); |
| 407 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 408 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 409 | if (disk_bytenr > 0) { |
| 410 | ret = btrfs_inc_extent_ref(trans, root, |
| 411 | disk_bytenr, num_bytes, 0, |
| 412 | root->root_key.objectid, |
| 413 | new_key.objectid, |
| 414 | start - extent_offset); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 415 | BUG_ON(ret); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 416 | *hint_byte = disk_bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 417 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 418 | key.offset = start; |
| 419 | } |
| 420 | /* |
| 421 | * | ---- range to drop ----- | |
| 422 | * | -------- extent -------- | |
| 423 | */ |
| 424 | if (start <= key.offset && end < extent_end) { |
| 425 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 426 | |
| 427 | memcpy(&new_key, &key, sizeof(new_key)); |
| 428 | new_key.offset = end; |
| 429 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 430 | |
| 431 | extent_offset += end - key.offset; |
| 432 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 433 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 434 | extent_end - end); |
| 435 | btrfs_mark_buffer_dirty(leaf); |
| 436 | if (disk_bytenr > 0) { |
| 437 | inode_sub_bytes(inode, end - key.offset); |
| 438 | *hint_byte = disk_bytenr; |
| 439 | } |
| 440 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 443 | search_start = extent_end; |
| 444 | /* |
| 445 | * | ---- range to drop ----- | |
| 446 | * | -------- extent -------- | |
| 447 | */ |
| 448 | if (start > key.offset && end >= extent_end) { |
| 449 | BUG_ON(del_nr > 0); |
| 450 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 451 | |
| 452 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 453 | start - key.offset); |
| 454 | btrfs_mark_buffer_dirty(leaf); |
| 455 | if (disk_bytenr > 0) { |
| 456 | inode_sub_bytes(inode, extent_end - start); |
| 457 | *hint_byte = disk_bytenr; |
| 458 | } |
| 459 | if (end == extent_end) |
| 460 | break; |
| 461 | |
| 462 | path->slots[0]++; |
| 463 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 464 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * | ---- range to drop ----- | |
| 468 | * | ------ extent ------ | |
| 469 | */ |
| 470 | if (start <= key.offset && end >= extent_end) { |
| 471 | if (del_nr == 0) { |
| 472 | del_slot = path->slots[0]; |
| 473 | del_nr = 1; |
| 474 | } else { |
| 475 | BUG_ON(del_slot + del_nr != path->slots[0]); |
| 476 | del_nr++; |
| 477 | } |
| 478 | |
| 479 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 480 | inode_sub_bytes(inode, |
| 481 | extent_end - key.offset); |
| 482 | extent_end = ALIGN(extent_end, |
| 483 | root->sectorsize); |
| 484 | } else if (disk_bytenr > 0) { |
| 485 | ret = btrfs_free_extent(trans, root, |
| 486 | disk_bytenr, num_bytes, 0, |
| 487 | root->root_key.objectid, |
| 488 | key.objectid, key.offset - |
| 489 | extent_offset); |
| 490 | BUG_ON(ret); |
| 491 | inode_sub_bytes(inode, |
| 492 | extent_end - key.offset); |
| 493 | *hint_byte = disk_bytenr; |
| 494 | } |
| 495 | |
| 496 | if (end == extent_end) |
| 497 | break; |
| 498 | |
| 499 | if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) { |
| 500 | path->slots[0]++; |
| 501 | goto next_slot; |
| 502 | } |
| 503 | |
| 504 | ret = btrfs_del_items(trans, root, path, del_slot, |
| 505 | del_nr); |
| 506 | BUG_ON(ret); |
| 507 | |
| 508 | del_nr = 0; |
| 509 | del_slot = 0; |
| 510 | |
| 511 | btrfs_release_path(root, path); |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 516 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 517 | |
| 518 | if (del_nr > 0) { |
| 519 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
| 520 | BUG_ON(ret); |
| 521 | } |
| 522 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 523 | btrfs_free_path(path); |
| 524 | return ret; |
| 525 | } |
| 526 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 527 | static int extent_mergeable(struct extent_buffer *leaf, int slot, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 528 | u64 objectid, u64 bytenr, u64 orig_offset, |
| 529 | u64 *start, u64 *end) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 530 | { |
| 531 | struct btrfs_file_extent_item *fi; |
| 532 | struct btrfs_key key; |
| 533 | u64 extent_end; |
| 534 | |
| 535 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 536 | return 0; |
| 537 | |
| 538 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 539 | if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY) |
| 540 | return 0; |
| 541 | |
| 542 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 543 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG || |
| 544 | btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr || |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 545 | btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset || |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 546 | btrfs_file_extent_compression(leaf, fi) || |
| 547 | btrfs_file_extent_encryption(leaf, fi) || |
| 548 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 549 | return 0; |
| 550 | |
| 551 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 552 | if ((*start && *start != key.offset) || (*end && *end != extent_end)) |
| 553 | return 0; |
| 554 | |
| 555 | *start = key.offset; |
| 556 | *end = extent_end; |
| 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Mark extent in the range start - end as written. |
| 562 | * |
| 563 | * This changes extent type from 'pre-allocated' to 'regular'. If only |
| 564 | * part of extent is marked as written, the extent will be split into |
| 565 | * two or three. |
| 566 | */ |
| 567 | int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 568 | struct inode *inode, u64 start, u64 end) |
| 569 | { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 570 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 571 | struct extent_buffer *leaf; |
| 572 | struct btrfs_path *path; |
| 573 | struct btrfs_file_extent_item *fi; |
| 574 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 575 | struct btrfs_key new_key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 576 | u64 bytenr; |
| 577 | u64 num_bytes; |
| 578 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 579 | u64 orig_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 580 | u64 other_start; |
| 581 | u64 other_end; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 582 | u64 split; |
| 583 | int del_nr = 0; |
| 584 | int del_slot = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 585 | int recow; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 586 | int ret; |
| 587 | |
| 588 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
| 589 | |
| 590 | path = btrfs_alloc_path(); |
| 591 | BUG_ON(!path); |
| 592 | again: |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 593 | recow = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 594 | split = start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 595 | key.objectid = inode->i_ino; |
| 596 | key.type = BTRFS_EXTENT_DATA_KEY; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 597 | key.offset = split; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 598 | |
| 599 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 600 | if (ret > 0 && path->slots[0] > 0) |
| 601 | path->slots[0]--; |
| 602 | |
| 603 | leaf = path->nodes[0]; |
| 604 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 605 | BUG_ON(key.objectid != inode->i_ino || |
| 606 | key.type != BTRFS_EXTENT_DATA_KEY); |
| 607 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 608 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 609 | BUG_ON(btrfs_file_extent_type(leaf, fi) != |
| 610 | BTRFS_FILE_EXTENT_PREALLOC); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 611 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 612 | BUG_ON(key.offset > start || extent_end < end); |
| 613 | |
| 614 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 615 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 616 | orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 617 | memcpy(&new_key, &key, sizeof(new_key)); |
| 618 | |
| 619 | if (start == key.offset && end < extent_end) { |
| 620 | other_start = 0; |
| 621 | other_end = start; |
| 622 | if (extent_mergeable(leaf, path->slots[0] - 1, |
| 623 | inode->i_ino, bytenr, orig_offset, |
| 624 | &other_start, &other_end)) { |
| 625 | new_key.offset = end; |
| 626 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 627 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 628 | struct btrfs_file_extent_item); |
| 629 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 630 | extent_end - end); |
| 631 | btrfs_set_file_extent_offset(leaf, fi, |
| 632 | end - orig_offset); |
| 633 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 634 | struct btrfs_file_extent_item); |
| 635 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 636 | end - other_start); |
| 637 | btrfs_mark_buffer_dirty(leaf); |
| 638 | goto out; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (start > key.offset && end == extent_end) { |
| 643 | other_start = end; |
| 644 | other_end = 0; |
| 645 | if (extent_mergeable(leaf, path->slots[0] + 1, |
| 646 | inode->i_ino, bytenr, orig_offset, |
| 647 | &other_start, &other_end)) { |
| 648 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 649 | struct btrfs_file_extent_item); |
| 650 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 651 | start - key.offset); |
| 652 | path->slots[0]++; |
| 653 | new_key.offset = start; |
| 654 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 655 | |
| 656 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 657 | struct btrfs_file_extent_item); |
| 658 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 659 | other_end - start); |
| 660 | btrfs_set_file_extent_offset(leaf, fi, |
| 661 | start - orig_offset); |
| 662 | btrfs_mark_buffer_dirty(leaf); |
| 663 | goto out; |
| 664 | } |
| 665 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 666 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 667 | while (start > key.offset || end < extent_end) { |
| 668 | if (key.offset == start) |
| 669 | split = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 670 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 671 | new_key.offset = split; |
| 672 | ret = btrfs_duplicate_item(trans, root, path, &new_key); |
| 673 | if (ret == -EAGAIN) { |
| 674 | btrfs_release_path(root, path); |
| 675 | goto again; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 676 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 677 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 678 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 679 | leaf = path->nodes[0]; |
| 680 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 681 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 682 | btrfs_set_file_extent_num_bytes(leaf, fi, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 683 | split - key.offset); |
| 684 | |
| 685 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 686 | struct btrfs_file_extent_item); |
| 687 | |
| 688 | btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); |
| 689 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 690 | extent_end - split); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 691 | btrfs_mark_buffer_dirty(leaf); |
| 692 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 693 | ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, |
| 694 | root->root_key.objectid, |
| 695 | inode->i_ino, orig_offset); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 696 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 697 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 698 | if (split == start) { |
| 699 | key.offset = start; |
| 700 | } else { |
| 701 | BUG_ON(start != key.offset); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 702 | path->slots[0]--; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 703 | extent_end = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 704 | } |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 705 | recow = 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 708 | other_start = end; |
| 709 | other_end = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 710 | if (extent_mergeable(leaf, path->slots[0] + 1, |
| 711 | inode->i_ino, bytenr, orig_offset, |
| 712 | &other_start, &other_end)) { |
| 713 | if (recow) { |
| 714 | btrfs_release_path(root, path); |
| 715 | goto again; |
| 716 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 717 | extent_end = other_end; |
| 718 | del_slot = path->slots[0] + 1; |
| 719 | del_nr++; |
| 720 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 721 | 0, root->root_key.objectid, |
| 722 | inode->i_ino, orig_offset); |
| 723 | BUG_ON(ret); |
| 724 | } |
| 725 | other_start = 0; |
| 726 | other_end = start; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 727 | if (extent_mergeable(leaf, path->slots[0] - 1, |
| 728 | inode->i_ino, bytenr, orig_offset, |
| 729 | &other_start, &other_end)) { |
| 730 | if (recow) { |
| 731 | btrfs_release_path(root, path); |
| 732 | goto again; |
| 733 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 734 | key.offset = other_start; |
| 735 | del_slot = path->slots[0]; |
| 736 | del_nr++; |
| 737 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 738 | 0, root->root_key.objectid, |
| 739 | inode->i_ino, orig_offset); |
| 740 | BUG_ON(ret); |
| 741 | } |
| 742 | if (del_nr == 0) { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 743 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 744 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 745 | btrfs_set_file_extent_type(leaf, fi, |
| 746 | BTRFS_FILE_EXTENT_REG); |
| 747 | btrfs_mark_buffer_dirty(leaf); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 748 | } else { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 749 | fi = btrfs_item_ptr(leaf, del_slot - 1, |
| 750 | struct btrfs_file_extent_item); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 751 | btrfs_set_file_extent_type(leaf, fi, |
| 752 | BTRFS_FILE_EXTENT_REG); |
| 753 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 754 | extent_end - key.offset); |
| 755 | btrfs_mark_buffer_dirty(leaf); |
| 756 | |
| 757 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
| 758 | BUG_ON(ret); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 759 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 760 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 761 | btrfs_free_path(path); |
| 762 | return 0; |
| 763 | } |
| 764 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 765 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 766 | * this gets pages into the page cache and locks them down, it also properly |
| 767 | * waits for data=ordered extents to finish before allowing the pages to be |
| 768 | * modified. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 769 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 770 | static noinline int prepare_pages(struct btrfs_root *root, struct file *file, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 771 | struct page **pages, size_t num_pages, |
| 772 | loff_t pos, unsigned long first_index, |
| 773 | unsigned long last_index, size_t write_bytes) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 774 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 775 | struct extent_state *cached_state = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 776 | int i; |
| 777 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 778 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 779 | int err = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 780 | u64 start_pos; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 781 | u64 last_pos; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 782 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 783 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 784 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 785 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 786 | if (start_pos > inode->i_size) { |
| 787 | err = btrfs_cont_expand(inode, start_pos); |
| 788 | if (err) |
| 789 | return err; |
| 790 | } |
| 791 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 792 | memset(pages, 0, num_pages * sizeof(struct page *)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 793 | again: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 794 | for (i = 0; i < num_pages; i++) { |
| 795 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 796 | if (!pages[i]) { |
Josef Bacik | 7adf5df | 2011-01-25 22:11:54 +0000 | [diff] [blame] | 797 | int c; |
| 798 | for (c = i - 1; c >= 0; c--) { |
| 799 | unlock_page(pages[c]); |
| 800 | page_cache_release(pages[c]); |
| 801 | } |
| 802 | return -ENOMEM; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 803 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 804 | wait_on_page_writeback(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 805 | } |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 806 | if (start_pos < inode->i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 807 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 808 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
| 809 | start_pos, last_pos - 1, 0, &cached_state, |
| 810 | GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 811 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 812 | last_pos - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 813 | if (ordered && |
| 814 | ordered->file_offset + ordered->len > start_pos && |
| 815 | ordered->file_offset < last_pos) { |
| 816 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 817 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 818 | start_pos, last_pos - 1, |
| 819 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 820 | for (i = 0; i < num_pages; i++) { |
| 821 | unlock_page(pages[i]); |
| 822 | page_cache_release(pages[i]); |
| 823 | } |
| 824 | btrfs_wait_ordered_range(inode, start_pos, |
| 825 | last_pos - start_pos); |
| 826 | goto again; |
| 827 | } |
| 828 | if (ordered) |
| 829 | btrfs_put_ordered_extent(ordered); |
| 830 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 831 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 832 | last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 833 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 834 | GFP_NOFS); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 835 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 836 | start_pos, last_pos - 1, &cached_state, |
| 837 | GFP_NOFS); |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 838 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 839 | for (i = 0; i < num_pages; i++) { |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 840 | clear_page_dirty_for_io(pages[i]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 841 | set_page_extent_mapped(pages[i]); |
| 842 | WARN_ON(!PageLocked(pages[i])); |
| 843 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 844 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 845 | } |
| 846 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 847 | static ssize_t btrfs_file_aio_write(struct kiocb *iocb, |
| 848 | const struct iovec *iov, |
| 849 | unsigned long nr_segs, loff_t pos) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 850 | { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 851 | struct file *file = iocb->ki_filp; |
| 852 | struct inode *inode = fdentry(file)->d_inode; |
| 853 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 854 | struct page *pinned[2]; |
| 855 | struct page **pages = NULL; |
| 856 | struct iov_iter i; |
| 857 | loff_t *ppos = &iocb->ki_pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 858 | loff_t start_pos; |
| 859 | ssize_t num_written = 0; |
| 860 | ssize_t err = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 861 | size_t count; |
| 862 | size_t ocount; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 863 | int ret = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 864 | int nrptrs; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 865 | unsigned long first_index; |
| 866 | unsigned long last_index; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 867 | int will_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 868 | int buffered = 0; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 869 | int copied = 0; |
| 870 | int dirty_pages = 0; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 871 | |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 872 | will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) || |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 873 | (file->f_flags & O_DIRECT)); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 874 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 875 | pinned[0] = NULL; |
| 876 | pinned[1] = NULL; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 877 | |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 878 | start_pos = pos; |
| 879 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 880 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 881 | |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 882 | mutex_lock(&inode->i_mutex); |
| 883 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 884 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
| 885 | if (err) |
| 886 | goto out; |
| 887 | count = ocount; |
| 888 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 889 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 890 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 891 | if (err) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 892 | goto out; |
| 893 | |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 894 | if (count == 0) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 895 | goto out; |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 896 | |
| 897 | err = file_remove_suid(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 898 | if (err) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 899 | goto out; |
| 900 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 901 | /* |
| 902 | * If BTRFS flips readonly due to some impossible error |
| 903 | * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), |
| 904 | * although we have opened a file as writable, we have |
| 905 | * to stop this write operation to ensure FS consistency. |
| 906 | */ |
| 907 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { |
| 908 | err = -EROFS; |
| 909 | goto out; |
| 910 | } |
| 911 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 912 | file_update_time(file); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 913 | BTRFS_I(inode)->sequence++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 914 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 915 | if (unlikely(file->f_flags & O_DIRECT)) { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 916 | num_written = generic_file_direct_write(iocb, iov, &nr_segs, |
| 917 | pos, ppos, count, |
| 918 | ocount); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 919 | /* |
| 920 | * the generic O_DIRECT will update in-memory i_size after the |
| 921 | * DIOs are done. But our endio handlers that update the on |
| 922 | * disk i_size never update past the in memory i_size. So we |
| 923 | * need one more update here to catch any additions to the |
| 924 | * file |
| 925 | */ |
| 926 | if (inode->i_size != BTRFS_I(inode)->disk_i_size) { |
| 927 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 928 | mark_inode_dirty(inode); |
| 929 | } |
| 930 | |
| 931 | if (num_written < 0) { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 932 | ret = num_written; |
| 933 | num_written = 0; |
| 934 | goto out; |
| 935 | } else if (num_written == count) { |
| 936 | /* pick up pos changes done by the generic code */ |
| 937 | pos = *ppos; |
| 938 | goto out; |
| 939 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 940 | /* |
| 941 | * We are going to do buffered for the rest of the range, so we |
| 942 | * need to make sure to invalidate the buffered pages when we're |
| 943 | * done. |
| 944 | */ |
| 945 | buffered = 1; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 946 | pos += num_written; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 947 | } |
| 948 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 949 | iov_iter_init(&i, iov, nr_segs, count, num_written); |
| 950 | nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) / |
| 951 | PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / |
| 952 | (sizeof(struct page *))); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 953 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 954 | if (!pages) { |
| 955 | ret = -ENOMEM; |
| 956 | goto out; |
| 957 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 958 | |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 959 | /* generic_write_checks can change our pos */ |
| 960 | start_pos = pos; |
| 961 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 962 | first_index = pos >> PAGE_CACHE_SHIFT; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 963 | last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 964 | |
| 965 | /* |
| 966 | * there are lots of better ways to do this, but this code |
| 967 | * makes sure the first and last page in the file range are |
| 968 | * up to date and ready for cow |
| 969 | */ |
| 970 | if ((pos & (PAGE_CACHE_SIZE - 1))) { |
| 971 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 972 | if (!PageUptodate(pinned[0])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 973 | ret = btrfs_readpage(NULL, pinned[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 974 | BUG_ON(ret); |
| 975 | wait_on_page_locked(pinned[0]); |
| 976 | } else { |
| 977 | unlock_page(pinned[0]); |
| 978 | } |
| 979 | } |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 980 | if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 981 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 982 | if (!PageUptodate(pinned[1])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 983 | ret = btrfs_readpage(NULL, pinned[1]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 984 | BUG_ON(ret); |
| 985 | wait_on_page_locked(pinned[1]); |
| 986 | } else { |
| 987 | unlock_page(pinned[1]); |
| 988 | } |
| 989 | } |
| 990 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 991 | while (iov_iter_count(&i) > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 992 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 993 | size_t write_bytes = min(iov_iter_count(&i), |
| 994 | nrptrs * (size_t)PAGE_CACHE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 995 | offset); |
Yan, Zheng | 3a90983 | 2011-01-18 13:34:40 +0800 | [diff] [blame] | 996 | size_t num_pages = (write_bytes + offset + |
| 997 | PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 998 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 999 | WARN_ON(num_pages > nrptrs); |
yanhai zhu | 9aead43 | 2009-01-05 15:49:11 -0500 | [diff] [blame] | 1000 | memset(pages, 0, sizeof(struct page *) * nrptrs); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1001 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1002 | /* |
| 1003 | * Fault pages before locking them in prepare_pages |
| 1004 | * to avoid recursive lock |
| 1005 | */ |
| 1006 | if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) { |
| 1007 | ret = -EFAULT; |
| 1008 | goto out; |
| 1009 | } |
| 1010 | |
| 1011 | ret = btrfs_delalloc_reserve_space(inode, |
| 1012 | num_pages << PAGE_CACHE_SHIFT); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1013 | if (ret) |
| 1014 | goto out; |
| 1015 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1016 | ret = prepare_pages(root, file, pages, num_pages, |
| 1017 | pos, first_index, last_index, |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1018 | write_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1019 | if (ret) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1020 | btrfs_delalloc_release_space(inode, |
| 1021 | num_pages << PAGE_CACHE_SHIFT); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1022 | goto out; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1023 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1024 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1025 | copied = btrfs_copy_from_user(pos, num_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1026 | write_bytes, pages, &i); |
Yan, Zheng | 3a90983 | 2011-01-18 13:34:40 +0800 | [diff] [blame] | 1027 | dirty_pages = (copied + offset + PAGE_CACHE_SIZE - 1) >> |
| 1028 | PAGE_CACHE_SHIFT; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (num_pages > dirty_pages) { |
| 1031 | if (copied > 0) |
| 1032 | atomic_inc( |
| 1033 | &BTRFS_I(inode)->outstanding_extents); |
| 1034 | btrfs_delalloc_release_space(inode, |
| 1035 | (num_pages - dirty_pages) << |
| 1036 | PAGE_CACHE_SHIFT); |
| 1037 | } |
| 1038 | |
| 1039 | if (copied > 0) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1040 | dirty_and_release_pages(NULL, root, file, pages, |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1041 | dirty_pages, pos, copied); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1042 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1043 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1044 | btrfs_drop_pages(pages, num_pages); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (copied > 0) { |
| 1047 | if (will_write) { |
| 1048 | filemap_fdatawrite_range(inode->i_mapping, pos, |
| 1049 | pos + copied - 1); |
| 1050 | } else { |
| 1051 | balance_dirty_pages_ratelimited_nr( |
| 1052 | inode->i_mapping, |
| 1053 | dirty_pages); |
| 1054 | if (dirty_pages < |
| 1055 | (root->leafsize >> PAGE_CACHE_SHIFT) + 1) |
| 1056 | btrfs_btree_balance_dirty(root, 1); |
| 1057 | btrfs_throttle(root); |
| 1058 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1059 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1060 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1061 | pos += copied; |
| 1062 | num_written += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1063 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1064 | cond_resched(); |
| 1065 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1066 | out: |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1067 | mutex_unlock(&inode->i_mutex); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1068 | if (ret) |
| 1069 | err = ret; |
Chris Mason | 5b92ee7 | 2008-01-03 13:46:11 -0500 | [diff] [blame] | 1070 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1071 | kfree(pages); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1072 | if (pinned[0]) |
| 1073 | page_cache_release(pinned[0]); |
| 1074 | if (pinned[1]) |
| 1075 | page_cache_release(pinned[1]); |
| 1076 | *ppos = pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1077 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1078 | /* |
| 1079 | * we want to make sure fsync finds this change |
| 1080 | * but we haven't joined a transaction running right now. |
| 1081 | * |
| 1082 | * Later on, someone is sure to update the inode and get the |
| 1083 | * real transid recorded. |
| 1084 | * |
| 1085 | * We set last_trans now to the fs_info generation + 1, |
| 1086 | * this will either be one more than the running transaction |
| 1087 | * or the generation used for the next transaction if there isn't |
| 1088 | * one running right now. |
| 1089 | */ |
| 1090 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; |
| 1091 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1092 | if (num_written > 0 && will_write) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1093 | struct btrfs_trans_handle *trans; |
| 1094 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1095 | err = btrfs_wait_ordered_range(inode, start_pos, num_written); |
| 1096 | if (err) |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1097 | num_written = err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1098 | |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 1099 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1100 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1101 | if (IS_ERR(trans)) { |
| 1102 | num_written = PTR_ERR(trans); |
| 1103 | goto done; |
| 1104 | } |
| 1105 | mutex_lock(&inode->i_mutex); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1106 | ret = btrfs_log_dentry_safe(trans, root, |
| 1107 | file->f_dentry); |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1108 | mutex_unlock(&inode->i_mutex); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1109 | if (ret == 0) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1110 | ret = btrfs_sync_log(trans, root); |
| 1111 | if (ret == 0) |
| 1112 | btrfs_end_transaction(trans, root); |
| 1113 | else |
| 1114 | btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1115 | } else if (ret != BTRFS_NO_LOG_SYNC) { |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1116 | btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1117 | } else { |
| 1118 | btrfs_end_transaction(trans, root); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1119 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1120 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1121 | if (file->f_flags & O_DIRECT && buffered) { |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1122 | invalidate_mapping_pages(inode->i_mapping, |
| 1123 | start_pos >> PAGE_CACHE_SHIFT, |
| 1124 | (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT); |
| 1125 | } |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1126 | } |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1127 | done: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1128 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1129 | return num_written ? num_written : err; |
| 1130 | } |
| 1131 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1132 | int btrfs_release_file(struct inode *inode, struct file *filp) |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1133 | { |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1134 | /* |
| 1135 | * ordered_data_close is set by settattr when we are about to truncate |
| 1136 | * a file from a non-zero size to a zero size. This tries to |
| 1137 | * flush down new bytes that may have been written if the |
| 1138 | * application were using truncate to replace a file in place. |
| 1139 | */ |
| 1140 | if (BTRFS_I(inode)->ordered_data_close) { |
| 1141 | BTRFS_I(inode)->ordered_data_close = 0; |
| 1142 | btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode); |
| 1143 | if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 1144 | filemap_flush(inode->i_mapping); |
| 1145 | } |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1146 | if (filp->private_data) |
| 1147 | btrfs_ioctl_trans_end(filp); |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1148 | return 0; |
| 1149 | } |
| 1150 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1151 | /* |
| 1152 | * fsync call for both files and directories. This logs the inode into |
| 1153 | * the tree log instead of forcing full commits whenever possible. |
| 1154 | * |
| 1155 | * It needs to call filemap_fdatawait so that all ordered extent updates are |
| 1156 | * in the metadata btree are up to date for copying to the log. |
| 1157 | * |
| 1158 | * It drops the inode mutex before doing the tree log commit. This is an |
| 1159 | * important optimization for directories because holding the mutex prevents |
| 1160 | * new operations on the dir while we write to disk. |
| 1161 | */ |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1162 | int btrfs_sync_file(struct file *file, int datasync) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1163 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1164 | struct dentry *dentry = file->f_path.dentry; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1165 | struct inode *inode = dentry->d_inode; |
| 1166 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1167 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1168 | struct btrfs_trans_handle *trans; |
| 1169 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1170 | |
| 1171 | /* we wait first, since the writeback may change the inode */ |
| 1172 | root->log_batch++; |
| 1173 | /* the VFS called filemap_fdatawrite for us */ |
| 1174 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 1175 | root->log_batch++; |
| 1176 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1177 | /* |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1178 | * check the transaction that last modified this inode |
| 1179 | * and see if its already been committed |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1180 | */ |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1181 | if (!BTRFS_I(inode)->last_trans) |
| 1182 | goto out; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1183 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1184 | /* |
| 1185 | * if the last transaction that changed this file was before |
| 1186 | * the current transaction, we can bail out now without any |
| 1187 | * syncing |
| 1188 | */ |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1189 | mutex_lock(&root->fs_info->trans_mutex); |
| 1190 | if (BTRFS_I(inode)->last_trans <= |
| 1191 | root->fs_info->last_trans_committed) { |
| 1192 | BTRFS_I(inode)->last_trans = 0; |
| 1193 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1194 | goto out; |
| 1195 | } |
| 1196 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1197 | |
| 1198 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1199 | * ok we haven't committed the transaction yet, lets do a commit |
| 1200 | */ |
Dan Carpenter | 6f902af | 2010-05-29 09:49:07 +0000 | [diff] [blame] | 1201 | if (file->private_data) |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1202 | btrfs_ioctl_trans_end(file); |
| 1203 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1204 | trans = btrfs_start_transaction(root, 0); |
| 1205 | if (IS_ERR(trans)) { |
| 1206 | ret = PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1207 | goto out; |
| 1208 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1209 | |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1210 | ret = btrfs_log_dentry_safe(trans, root, dentry); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1211 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1212 | goto out; |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1213 | |
| 1214 | /* we've logged all the items and now have a consistent |
| 1215 | * version of the file in the log. It is possible that |
| 1216 | * someone will come in and modify the file, but that's |
| 1217 | * fine because the log is consistent on disk, and we |
| 1218 | * have references to all of the file's extents |
| 1219 | * |
| 1220 | * It is possible that someone will come in and log the |
| 1221 | * file again, but that will end up using the synchronization |
| 1222 | * inside btrfs_sync_log to keep things safe. |
| 1223 | */ |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1224 | mutex_unlock(&dentry->d_inode->i_mutex); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1225 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1226 | if (ret != BTRFS_NO_LOG_SYNC) { |
| 1227 | if (ret > 0) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1228 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1229 | } else { |
| 1230 | ret = btrfs_sync_log(trans, root); |
| 1231 | if (ret == 0) |
| 1232 | ret = btrfs_end_transaction(trans, root); |
| 1233 | else |
| 1234 | ret = btrfs_commit_transaction(trans, root); |
| 1235 | } |
| 1236 | } else { |
| 1237 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1238 | } |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1239 | mutex_lock(&dentry->d_inode->i_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1240 | out: |
Roel Kluin | 014e4ac | 2010-01-29 10:42:11 +0000 | [diff] [blame] | 1241 | return ret > 0 ? -EIO : ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1244 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 1245 | .fault = filemap_fault, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1246 | .page_mkwrite = btrfs_page_mkwrite, |
| 1247 | }; |
| 1248 | |
| 1249 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1250 | { |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1251 | struct address_space *mapping = filp->f_mapping; |
| 1252 | |
| 1253 | if (!mapping->a_ops->readpage) |
| 1254 | return -ENOEXEC; |
| 1255 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1256 | file_accessed(filp); |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1257 | vma->vm_ops = &btrfs_file_vm_ops; |
| 1258 | vma->vm_flags |= VM_CAN_NONLINEAR; |
| 1259 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1263 | static long btrfs_fallocate(struct file *file, int mode, |
| 1264 | loff_t offset, loff_t len) |
| 1265 | { |
| 1266 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1267 | struct extent_state *cached_state = NULL; |
| 1268 | u64 cur_offset; |
| 1269 | u64 last_byte; |
| 1270 | u64 alloc_start; |
| 1271 | u64 alloc_end; |
| 1272 | u64 alloc_hint = 0; |
| 1273 | u64 locked_end; |
| 1274 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 1275 | struct extent_map *em; |
| 1276 | int ret; |
| 1277 | |
| 1278 | alloc_start = offset & ~mask; |
| 1279 | alloc_end = (offset + len + mask) & ~mask; |
| 1280 | |
| 1281 | /* We only support the FALLOC_FL_KEEP_SIZE mode */ |
| 1282 | if (mode & ~FALLOC_FL_KEEP_SIZE) |
| 1283 | return -EOPNOTSUPP; |
| 1284 | |
| 1285 | /* |
| 1286 | * wait for ordered IO before we have any locks. We'll loop again |
| 1287 | * below with the locks held. |
| 1288 | */ |
| 1289 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 1290 | |
| 1291 | mutex_lock(&inode->i_mutex); |
| 1292 | ret = inode_newsize_ok(inode, alloc_end); |
| 1293 | if (ret) |
| 1294 | goto out; |
| 1295 | |
| 1296 | if (alloc_start > inode->i_size) { |
| 1297 | ret = btrfs_cont_expand(inode, alloc_start); |
| 1298 | if (ret) |
| 1299 | goto out; |
| 1300 | } |
| 1301 | |
| 1302 | ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); |
| 1303 | if (ret) |
| 1304 | goto out; |
| 1305 | |
| 1306 | locked_end = alloc_end - 1; |
| 1307 | while (1) { |
| 1308 | struct btrfs_ordered_extent *ordered; |
| 1309 | |
| 1310 | /* the extent lock is ordered inside the running |
| 1311 | * transaction |
| 1312 | */ |
| 1313 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
| 1314 | locked_end, 0, &cached_state, GFP_NOFS); |
| 1315 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 1316 | alloc_end - 1); |
| 1317 | if (ordered && |
| 1318 | ordered->file_offset + ordered->len > alloc_start && |
| 1319 | ordered->file_offset < alloc_end) { |
| 1320 | btrfs_put_ordered_extent(ordered); |
| 1321 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1322 | alloc_start, locked_end, |
| 1323 | &cached_state, GFP_NOFS); |
| 1324 | /* |
| 1325 | * we can't wait on the range with the transaction |
| 1326 | * running or with the extent lock held |
| 1327 | */ |
| 1328 | btrfs_wait_ordered_range(inode, alloc_start, |
| 1329 | alloc_end - alloc_start); |
| 1330 | } else { |
| 1331 | if (ordered) |
| 1332 | btrfs_put_ordered_extent(ordered); |
| 1333 | break; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | cur_offset = alloc_start; |
| 1338 | while (1) { |
| 1339 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 1340 | alloc_end - cur_offset, 0); |
| 1341 | BUG_ON(IS_ERR(em) || !em); |
| 1342 | last_byte = min(extent_map_end(em), alloc_end); |
| 1343 | last_byte = (last_byte + mask) & ~mask; |
| 1344 | if (em->block_start == EXTENT_MAP_HOLE || |
| 1345 | (cur_offset >= inode->i_size && |
| 1346 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 1347 | ret = btrfs_prealloc_file_range(inode, mode, cur_offset, |
| 1348 | last_byte - cur_offset, |
| 1349 | 1 << inode->i_blkbits, |
| 1350 | offset + len, |
| 1351 | &alloc_hint); |
| 1352 | if (ret < 0) { |
| 1353 | free_extent_map(em); |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | free_extent_map(em); |
| 1358 | |
| 1359 | cur_offset = last_byte; |
| 1360 | if (cur_offset >= alloc_end) { |
| 1361 | ret = 0; |
| 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 1366 | &cached_state, GFP_NOFS); |
| 1367 | |
| 1368 | btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); |
| 1369 | out: |
| 1370 | mutex_unlock(&inode->i_mutex); |
| 1371 | return ret; |
| 1372 | } |
| 1373 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1374 | const struct file_operations btrfs_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1375 | .llseek = generic_file_llseek, |
| 1376 | .read = do_sync_read, |
Miao Xie | 4a001071 | 2010-06-07 03:38:51 +0000 | [diff] [blame] | 1377 | .write = do_sync_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1378 | .aio_read = generic_file_aio_read, |
Chris Mason | e9906a9 | 2007-12-14 12:56:58 -0500 | [diff] [blame] | 1379 | .splice_read = generic_file_splice_read, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1380 | .aio_write = btrfs_file_aio_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1381 | .mmap = btrfs_file_mmap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1382 | .open = generic_file_open, |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1383 | .release = btrfs_release_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1384 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1385 | .fallocate = btrfs_fallocate, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1386 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1387 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1388 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1389 | #endif |
| 1390 | }; |