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