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