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