]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/bio.h> | |
21 | #include <linux/buffer_head.h> | |
22 | #include <linux/file.h> | |
23 | #include <linux/fs.h> | |
24 | #include <linux/pagemap.h> | |
25 | #include <linux/highmem.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/backing-dev.h> | |
30 | #include <linux/mpage.h> | |
31 | #include <linux/swap.h> | |
32 | #include <linux/writeback.h> | |
33 | #include <linux/compat.h> | |
34 | #include <linux/bit_spinlock.h> | |
35 | #include <linux/xattr.h> | |
36 | #include <linux/posix_acl.h> | |
37 | #include <linux/falloc.h> | |
38 | #include <linux/slab.h> | |
39 | #include <linux/ratelimit.h> | |
40 | #include <linux/mount.h> | |
41 | #include <linux/btrfs.h> | |
42 | #include <linux/blkdev.h> | |
43 | #include <linux/posix_acl_xattr.h> | |
44 | #include <linux/uio.h> | |
45 | #include "ctree.h" | |
46 | #include "disk-io.h" | |
47 | #include "transaction.h" | |
48 | #include "btrfs_inode.h" | |
49 | #include "print-tree.h" | |
50 | #include "ordered-data.h" | |
51 | #include "xattr.h" | |
52 | #include "tree-log.h" | |
53 | #include "volumes.h" | |
54 | #include "compression.h" | |
55 | #include "locking.h" | |
56 | #include "free-space-cache.h" | |
57 | #include "inode-map.h" | |
58 | #include "backref.h" | |
59 | #include "hash.h" | |
60 | #include "props.h" | |
61 | #include "qgroup.h" | |
62 | #include "dedupe.h" | |
63 | ||
64 | struct btrfs_iget_args { | |
65 | struct btrfs_key *location; | |
66 | struct btrfs_root *root; | |
67 | }; | |
68 | ||
69 | struct btrfs_dio_data { | |
70 | u64 outstanding_extents; | |
71 | u64 reserve; | |
72 | u64 unsubmitted_oe_range_start; | |
73 | u64 unsubmitted_oe_range_end; | |
74 | int overwrite; | |
75 | }; | |
76 | ||
77 | static const struct inode_operations btrfs_dir_inode_operations; | |
78 | static const struct inode_operations btrfs_symlink_inode_operations; | |
79 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
80 | static const struct inode_operations btrfs_special_inode_operations; | |
81 | static const struct inode_operations btrfs_file_inode_operations; | |
82 | static const struct address_space_operations btrfs_aops; | |
83 | static const struct address_space_operations btrfs_symlink_aops; | |
84 | static const struct file_operations btrfs_dir_file_operations; | |
85 | static const struct extent_io_ops btrfs_extent_io_ops; | |
86 | ||
87 | static struct kmem_cache *btrfs_inode_cachep; | |
88 | struct kmem_cache *btrfs_trans_handle_cachep; | |
89 | struct kmem_cache *btrfs_path_cachep; | |
90 | struct kmem_cache *btrfs_free_space_cachep; | |
91 | ||
92 | #define S_SHIFT 12 | |
93 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
94 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
95 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
96 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
97 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
98 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
99 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
100 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
101 | }; | |
102 | ||
103 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); | |
104 | static int btrfs_truncate(struct inode *inode); | |
105 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); | |
106 | static noinline int cow_file_range(struct inode *inode, | |
107 | struct page *locked_page, | |
108 | u64 start, u64 end, u64 delalloc_end, | |
109 | int *page_started, unsigned long *nr_written, | |
110 | int unlock, struct btrfs_dedupe_hash *hash); | |
111 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
112 | u64 orig_start, u64 block_start, | |
113 | u64 block_len, u64 orig_block_len, | |
114 | u64 ram_bytes, int compress_type, | |
115 | int type); | |
116 | ||
117 | static void __endio_write_update_ordered(struct inode *inode, | |
118 | const u64 offset, const u64 bytes, | |
119 | const bool uptodate); | |
120 | ||
121 | /* | |
122 | * Cleanup all submitted ordered extents in specified range to handle errors | |
123 | * from the fill_dellaloc() callback. | |
124 | * | |
125 | * NOTE: caller must ensure that when an error happens, it can not call | |
126 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
127 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
128 | * to be released, which we want to happen only when finishing the ordered | |
129 | * extent (btrfs_finish_ordered_io()). Also note that the caller of the | |
130 | * fill_delalloc() callback already does proper cleanup for the first page of | |
131 | * the range, that is, it invokes the callback writepage_end_io_hook() for the | |
132 | * range of the first page. | |
133 | */ | |
134 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, | |
135 | const u64 offset, | |
136 | const u64 bytes) | |
137 | { | |
138 | unsigned long index = offset >> PAGE_SHIFT; | |
139 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
140 | struct page *page; | |
141 | ||
142 | while (index <= end_index) { | |
143 | page = find_get_page(inode->i_mapping, index); | |
144 | index++; | |
145 | if (!page) | |
146 | continue; | |
147 | ClearPagePrivate2(page); | |
148 | put_page(page); | |
149 | } | |
150 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, | |
151 | bytes - PAGE_SIZE, false); | |
152 | } | |
153 | ||
154 | static int btrfs_dirty_inode(struct inode *inode); | |
155 | ||
156 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS | |
157 | void btrfs_test_inode_set_ops(struct inode *inode) | |
158 | { | |
159 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
160 | } | |
161 | #endif | |
162 | ||
163 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, | |
164 | struct inode *inode, struct inode *dir, | |
165 | const struct qstr *qstr) | |
166 | { | |
167 | int err; | |
168 | ||
169 | err = btrfs_init_acl(trans, inode, dir); | |
170 | if (!err) | |
171 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); | |
172 | return err; | |
173 | } | |
174 | ||
175 | /* | |
176 | * this does all the hard work for inserting an inline extent into | |
177 | * the btree. The caller should have done a btrfs_drop_extents so that | |
178 | * no overlapping inline items exist in the btree | |
179 | */ | |
180 | static int insert_inline_extent(struct btrfs_trans_handle *trans, | |
181 | struct btrfs_path *path, int extent_inserted, | |
182 | struct btrfs_root *root, struct inode *inode, | |
183 | u64 start, size_t size, size_t compressed_size, | |
184 | int compress_type, | |
185 | struct page **compressed_pages) | |
186 | { | |
187 | struct extent_buffer *leaf; | |
188 | struct page *page = NULL; | |
189 | char *kaddr; | |
190 | unsigned long ptr; | |
191 | struct btrfs_file_extent_item *ei; | |
192 | int ret; | |
193 | size_t cur_size = size; | |
194 | unsigned long offset; | |
195 | ||
196 | if (compressed_size && compressed_pages) | |
197 | cur_size = compressed_size; | |
198 | ||
199 | inode_add_bytes(inode, size); | |
200 | ||
201 | if (!extent_inserted) { | |
202 | struct btrfs_key key; | |
203 | size_t datasize; | |
204 | ||
205 | key.objectid = btrfs_ino(BTRFS_I(inode)); | |
206 | key.offset = start; | |
207 | key.type = BTRFS_EXTENT_DATA_KEY; | |
208 | ||
209 | datasize = btrfs_file_extent_calc_inline_size(cur_size); | |
210 | path->leave_spinning = 1; | |
211 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
212 | datasize); | |
213 | if (ret) | |
214 | goto fail; | |
215 | } | |
216 | leaf = path->nodes[0]; | |
217 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
218 | struct btrfs_file_extent_item); | |
219 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
220 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
221 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
222 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
223 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
224 | ptr = btrfs_file_extent_inline_start(ei); | |
225 | ||
226 | if (compress_type != BTRFS_COMPRESS_NONE) { | |
227 | struct page *cpage; | |
228 | int i = 0; | |
229 | while (compressed_size > 0) { | |
230 | cpage = compressed_pages[i]; | |
231 | cur_size = min_t(unsigned long, compressed_size, | |
232 | PAGE_SIZE); | |
233 | ||
234 | kaddr = kmap_atomic(cpage); | |
235 | write_extent_buffer(leaf, kaddr, ptr, cur_size); | |
236 | kunmap_atomic(kaddr); | |
237 | ||
238 | i++; | |
239 | ptr += cur_size; | |
240 | compressed_size -= cur_size; | |
241 | } | |
242 | btrfs_set_file_extent_compression(leaf, ei, | |
243 | compress_type); | |
244 | } else { | |
245 | page = find_get_page(inode->i_mapping, | |
246 | start >> PAGE_SHIFT); | |
247 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
248 | kaddr = kmap_atomic(page); | |
249 | offset = start & (PAGE_SIZE - 1); | |
250 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
251 | kunmap_atomic(kaddr); | |
252 | put_page(page); | |
253 | } | |
254 | btrfs_mark_buffer_dirty(leaf); | |
255 | btrfs_release_path(path); | |
256 | ||
257 | /* | |
258 | * we're an inline extent, so nobody can | |
259 | * extend the file past i_size without locking | |
260 | * a page we already have locked. | |
261 | * | |
262 | * We must do any isize and inode updates | |
263 | * before we unlock the pages. Otherwise we | |
264 | * could end up racing with unlink. | |
265 | */ | |
266 | BTRFS_I(inode)->disk_i_size = inode->i_size; | |
267 | ret = btrfs_update_inode(trans, root, inode); | |
268 | ||
269 | fail: | |
270 | return ret; | |
271 | } | |
272 | ||
273 | ||
274 | /* | |
275 | * conditionally insert an inline extent into the file. This | |
276 | * does the checks required to make sure the data is small enough | |
277 | * to fit as an inline extent. | |
278 | */ | |
279 | static noinline int cow_file_range_inline(struct btrfs_root *root, | |
280 | struct inode *inode, u64 start, | |
281 | u64 end, size_t compressed_size, | |
282 | int compress_type, | |
283 | struct page **compressed_pages) | |
284 | { | |
285 | struct btrfs_fs_info *fs_info = root->fs_info; | |
286 | struct btrfs_trans_handle *trans; | |
287 | u64 isize = i_size_read(inode); | |
288 | u64 actual_end = min(end + 1, isize); | |
289 | u64 inline_len = actual_end - start; | |
290 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); | |
291 | u64 data_len = inline_len; | |
292 | int ret; | |
293 | struct btrfs_path *path; | |
294 | int extent_inserted = 0; | |
295 | u32 extent_item_size; | |
296 | ||
297 | if (compressed_size) | |
298 | data_len = compressed_size; | |
299 | ||
300 | if (start > 0 || | |
301 | actual_end > fs_info->sectorsize || | |
302 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
303 | (!compressed_size && | |
304 | (actual_end & (fs_info->sectorsize - 1)) == 0) || | |
305 | end + 1 < isize || | |
306 | data_len > fs_info->max_inline) { | |
307 | return 1; | |
308 | } | |
309 | ||
310 | path = btrfs_alloc_path(); | |
311 | if (!path) | |
312 | return -ENOMEM; | |
313 | ||
314 | trans = btrfs_join_transaction(root); | |
315 | if (IS_ERR(trans)) { | |
316 | btrfs_free_path(path); | |
317 | return PTR_ERR(trans); | |
318 | } | |
319 | trans->block_rsv = &fs_info->delalloc_block_rsv; | |
320 | ||
321 | if (compressed_size && compressed_pages) | |
322 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
323 | compressed_size); | |
324 | else | |
325 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
326 | inline_len); | |
327 | ||
328 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
329 | start, aligned_end, NULL, | |
330 | 1, 1, extent_item_size, &extent_inserted); | |
331 | if (ret) { | |
332 | btrfs_abort_transaction(trans, ret); | |
333 | goto out; | |
334 | } | |
335 | ||
336 | if (isize > actual_end) | |
337 | inline_len = min_t(u64, isize, actual_end); | |
338 | ret = insert_inline_extent(trans, path, extent_inserted, | |
339 | root, inode, start, | |
340 | inline_len, compressed_size, | |
341 | compress_type, compressed_pages); | |
342 | if (ret && ret != -ENOSPC) { | |
343 | btrfs_abort_transaction(trans, ret); | |
344 | goto out; | |
345 | } else if (ret == -ENOSPC) { | |
346 | ret = 1; | |
347 | goto out; | |
348 | } | |
349 | ||
350 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
351 | btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start); | |
352 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); | |
353 | out: | |
354 | /* | |
355 | * Don't forget to free the reserved space, as for inlined extent | |
356 | * it won't count as data extent, free them directly here. | |
357 | * And at reserve time, it's always aligned to page size, so | |
358 | * just free one page here. | |
359 | */ | |
360 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); | |
361 | btrfs_free_path(path); | |
362 | btrfs_end_transaction(trans); | |
363 | return ret; | |
364 | } | |
365 | ||
366 | struct async_extent { | |
367 | u64 start; | |
368 | u64 ram_size; | |
369 | u64 compressed_size; | |
370 | struct page **pages; | |
371 | unsigned long nr_pages; | |
372 | int compress_type; | |
373 | struct list_head list; | |
374 | }; | |
375 | ||
376 | struct async_cow { | |
377 | struct inode *inode; | |
378 | struct btrfs_root *root; | |
379 | struct page *locked_page; | |
380 | u64 start; | |
381 | u64 end; | |
382 | struct list_head extents; | |
383 | struct btrfs_work work; | |
384 | }; | |
385 | ||
386 | static noinline int add_async_extent(struct async_cow *cow, | |
387 | u64 start, u64 ram_size, | |
388 | u64 compressed_size, | |
389 | struct page **pages, | |
390 | unsigned long nr_pages, | |
391 | int compress_type) | |
392 | { | |
393 | struct async_extent *async_extent; | |
394 | ||
395 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
396 | BUG_ON(!async_extent); /* -ENOMEM */ | |
397 | async_extent->start = start; | |
398 | async_extent->ram_size = ram_size; | |
399 | async_extent->compressed_size = compressed_size; | |
400 | async_extent->pages = pages; | |
401 | async_extent->nr_pages = nr_pages; | |
402 | async_extent->compress_type = compress_type; | |
403 | list_add_tail(&async_extent->list, &cow->extents); | |
404 | return 0; | |
405 | } | |
406 | ||
407 | static inline int inode_need_compress(struct inode *inode) | |
408 | { | |
409 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
410 | ||
411 | /* force compress */ | |
412 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) | |
413 | return 1; | |
414 | /* bad compression ratios */ | |
415 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
416 | return 0; | |
417 | if (btrfs_test_opt(fs_info, COMPRESS) || | |
418 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || | |
419 | BTRFS_I(inode)->force_compress) | |
420 | return 1; | |
421 | return 0; | |
422 | } | |
423 | ||
424 | static inline void inode_should_defrag(struct btrfs_inode *inode, | |
425 | u64 start, u64 end, u64 num_bytes, u64 small_write) | |
426 | { | |
427 | /* If this is a small write inside eof, kick off a defrag */ | |
428 | if (num_bytes < small_write && | |
429 | (start > 0 || end + 1 < inode->disk_i_size)) | |
430 | btrfs_add_inode_defrag(NULL, inode); | |
431 | } | |
432 | ||
433 | /* | |
434 | * we create compressed extents in two phases. The first | |
435 | * phase compresses a range of pages that have already been | |
436 | * locked (both pages and state bits are locked). | |
437 | * | |
438 | * This is done inside an ordered work queue, and the compression | |
439 | * is spread across many cpus. The actual IO submission is step | |
440 | * two, and the ordered work queue takes care of making sure that | |
441 | * happens in the same order things were put onto the queue by | |
442 | * writepages and friends. | |
443 | * | |
444 | * If this code finds it can't get good compression, it puts an | |
445 | * entry onto the work queue to write the uncompressed bytes. This | |
446 | * makes sure that both compressed inodes and uncompressed inodes | |
447 | * are written in the same order that the flusher thread sent them | |
448 | * down. | |
449 | */ | |
450 | static noinline void compress_file_range(struct inode *inode, | |
451 | struct page *locked_page, | |
452 | u64 start, u64 end, | |
453 | struct async_cow *async_cow, | |
454 | int *num_added) | |
455 | { | |
456 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
457 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
458 | u64 num_bytes; | |
459 | u64 blocksize = fs_info->sectorsize; | |
460 | u64 actual_end; | |
461 | u64 isize = i_size_read(inode); | |
462 | int ret = 0; | |
463 | struct page **pages = NULL; | |
464 | unsigned long nr_pages; | |
465 | unsigned long total_compressed = 0; | |
466 | unsigned long total_in = 0; | |
467 | int i; | |
468 | int will_compress; | |
469 | int compress_type = fs_info->compress_type; | |
470 | int redirty = 0; | |
471 | ||
472 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, | |
473 | SZ_16K); | |
474 | ||
475 | actual_end = min_t(u64, isize, end + 1); | |
476 | again: | |
477 | will_compress = 0; | |
478 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; | |
479 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); | |
480 | nr_pages = min_t(unsigned long, nr_pages, | |
481 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
482 | ||
483 | /* | |
484 | * we don't want to send crud past the end of i_size through | |
485 | * compression, that's just a waste of CPU time. So, if the | |
486 | * end of the file is before the start of our current | |
487 | * requested range of bytes, we bail out to the uncompressed | |
488 | * cleanup code that can deal with all of this. | |
489 | * | |
490 | * It isn't really the fastest way to fix things, but this is a | |
491 | * very uncommon corner. | |
492 | */ | |
493 | if (actual_end <= start) | |
494 | goto cleanup_and_bail_uncompressed; | |
495 | ||
496 | total_compressed = actual_end - start; | |
497 | ||
498 | /* | |
499 | * skip compression for a small file range(<=blocksize) that | |
500 | * isn't an inline extent, since it doesn't save disk space at all. | |
501 | */ | |
502 | if (total_compressed <= blocksize && | |
503 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
504 | goto cleanup_and_bail_uncompressed; | |
505 | ||
506 | total_compressed = min_t(unsigned long, total_compressed, | |
507 | BTRFS_MAX_UNCOMPRESSED); | |
508 | num_bytes = ALIGN(end - start + 1, blocksize); | |
509 | num_bytes = max(blocksize, num_bytes); | |
510 | total_in = 0; | |
511 | ret = 0; | |
512 | ||
513 | /* | |
514 | * we do compression for mount -o compress and when the | |
515 | * inode has not been flagged as nocompress. This flag can | |
516 | * change at any time if we discover bad compression ratios. | |
517 | */ | |
518 | if (inode_need_compress(inode)) { | |
519 | WARN_ON(pages); | |
520 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); | |
521 | if (!pages) { | |
522 | /* just bail out to the uncompressed code */ | |
523 | goto cont; | |
524 | } | |
525 | ||
526 | if (BTRFS_I(inode)->force_compress) | |
527 | compress_type = BTRFS_I(inode)->force_compress; | |
528 | ||
529 | /* | |
530 | * we need to call clear_page_dirty_for_io on each | |
531 | * page in the range. Otherwise applications with the file | |
532 | * mmap'd can wander in and change the page contents while | |
533 | * we are compressing them. | |
534 | * | |
535 | * If the compression fails for any reason, we set the pages | |
536 | * dirty again later on. | |
537 | */ | |
538 | extent_range_clear_dirty_for_io(inode, start, end); | |
539 | redirty = 1; | |
540 | ret = btrfs_compress_pages(compress_type, | |
541 | inode->i_mapping, start, | |
542 | pages, | |
543 | &nr_pages, | |
544 | &total_in, | |
545 | &total_compressed); | |
546 | ||
547 | if (!ret) { | |
548 | unsigned long offset = total_compressed & | |
549 | (PAGE_SIZE - 1); | |
550 | struct page *page = pages[nr_pages - 1]; | |
551 | char *kaddr; | |
552 | ||
553 | /* zero the tail end of the last page, we might be | |
554 | * sending it down to disk | |
555 | */ | |
556 | if (offset) { | |
557 | kaddr = kmap_atomic(page); | |
558 | memset(kaddr + offset, 0, | |
559 | PAGE_SIZE - offset); | |
560 | kunmap_atomic(kaddr); | |
561 | } | |
562 | will_compress = 1; | |
563 | } | |
564 | } | |
565 | cont: | |
566 | if (start == 0) { | |
567 | /* lets try to make an inline extent */ | |
568 | if (ret || total_in < (actual_end - start)) { | |
569 | /* we didn't compress the entire range, try | |
570 | * to make an uncompressed inline extent. | |
571 | */ | |
572 | ret = cow_file_range_inline(root, inode, start, end, | |
573 | 0, BTRFS_COMPRESS_NONE, NULL); | |
574 | } else { | |
575 | /* try making a compressed inline extent */ | |
576 | ret = cow_file_range_inline(root, inode, start, end, | |
577 | total_compressed, | |
578 | compress_type, pages); | |
579 | } | |
580 | if (ret <= 0) { | |
581 | unsigned long clear_flags = EXTENT_DELALLOC | | |
582 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG; | |
583 | unsigned long page_error_op; | |
584 | ||
585 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; | |
586 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; | |
587 | ||
588 | /* | |
589 | * inline extent creation worked or returned error, | |
590 | * we don't need to create any more async work items. | |
591 | * Unlock and free up our temp pages. | |
592 | */ | |
593 | extent_clear_unlock_delalloc(inode, start, end, end, | |
594 | NULL, clear_flags, | |
595 | PAGE_UNLOCK | | |
596 | PAGE_CLEAR_DIRTY | | |
597 | PAGE_SET_WRITEBACK | | |
598 | page_error_op | | |
599 | PAGE_END_WRITEBACK); | |
600 | if (ret == 0) | |
601 | btrfs_free_reserved_data_space_noquota(inode, | |
602 | start, | |
603 | end - start + 1); | |
604 | goto free_pages_out; | |
605 | } | |
606 | } | |
607 | ||
608 | if (will_compress) { | |
609 | /* | |
610 | * we aren't doing an inline extent round the compressed size | |
611 | * up to a block size boundary so the allocator does sane | |
612 | * things | |
613 | */ | |
614 | total_compressed = ALIGN(total_compressed, blocksize); | |
615 | ||
616 | /* | |
617 | * one last check to make sure the compression is really a | |
618 | * win, compare the page count read with the blocks on disk, | |
619 | * compression must free at least one sector size | |
620 | */ | |
621 | total_in = ALIGN(total_in, PAGE_SIZE); | |
622 | if (total_compressed + blocksize <= total_in) { | |
623 | num_bytes = total_in; | |
624 | *num_added += 1; | |
625 | ||
626 | /* | |
627 | * The async work queues will take care of doing actual | |
628 | * allocation on disk for these compressed pages, and | |
629 | * will submit them to the elevator. | |
630 | */ | |
631 | add_async_extent(async_cow, start, num_bytes, | |
632 | total_compressed, pages, nr_pages, | |
633 | compress_type); | |
634 | ||
635 | if (start + num_bytes < end) { | |
636 | start += num_bytes; | |
637 | pages = NULL; | |
638 | cond_resched(); | |
639 | goto again; | |
640 | } | |
641 | return; | |
642 | } | |
643 | } | |
644 | if (pages) { | |
645 | /* | |
646 | * the compression code ran but failed to make things smaller, | |
647 | * free any pages it allocated and our page pointer array | |
648 | */ | |
649 | for (i = 0; i < nr_pages; i++) { | |
650 | WARN_ON(pages[i]->mapping); | |
651 | put_page(pages[i]); | |
652 | } | |
653 | kfree(pages); | |
654 | pages = NULL; | |
655 | total_compressed = 0; | |
656 | nr_pages = 0; | |
657 | ||
658 | /* flag the file so we don't compress in the future */ | |
659 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && | |
660 | !(BTRFS_I(inode)->force_compress)) { | |
661 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
662 | } | |
663 | } | |
664 | cleanup_and_bail_uncompressed: | |
665 | /* | |
666 | * No compression, but we still need to write the pages in the file | |
667 | * we've been given so far. redirty the locked page if it corresponds | |
668 | * to our extent and set things up for the async work queue to run | |
669 | * cow_file_range to do the normal delalloc dance. | |
670 | */ | |
671 | if (page_offset(locked_page) >= start && | |
672 | page_offset(locked_page) <= end) | |
673 | __set_page_dirty_nobuffers(locked_page); | |
674 | /* unlocked later on in the async handlers */ | |
675 | ||
676 | if (redirty) | |
677 | extent_range_redirty_for_io(inode, start, end); | |
678 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
679 | BTRFS_COMPRESS_NONE); | |
680 | *num_added += 1; | |
681 | ||
682 | return; | |
683 | ||
684 | free_pages_out: | |
685 | for (i = 0; i < nr_pages; i++) { | |
686 | WARN_ON(pages[i]->mapping); | |
687 | put_page(pages[i]); | |
688 | } | |
689 | kfree(pages); | |
690 | } | |
691 | ||
692 | static void free_async_extent_pages(struct async_extent *async_extent) | |
693 | { | |
694 | int i; | |
695 | ||
696 | if (!async_extent->pages) | |
697 | return; | |
698 | ||
699 | for (i = 0; i < async_extent->nr_pages; i++) { | |
700 | WARN_ON(async_extent->pages[i]->mapping); | |
701 | put_page(async_extent->pages[i]); | |
702 | } | |
703 | kfree(async_extent->pages); | |
704 | async_extent->nr_pages = 0; | |
705 | async_extent->pages = NULL; | |
706 | } | |
707 | ||
708 | /* | |
709 | * phase two of compressed writeback. This is the ordered portion | |
710 | * of the code, which only gets called in the order the work was | |
711 | * queued. We walk all the async extents created by compress_file_range | |
712 | * and send them down to the disk. | |
713 | */ | |
714 | static noinline void submit_compressed_extents(struct inode *inode, | |
715 | struct async_cow *async_cow) | |
716 | { | |
717 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
718 | struct async_extent *async_extent; | |
719 | u64 alloc_hint = 0; | |
720 | struct btrfs_key ins; | |
721 | struct extent_map *em; | |
722 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
723 | struct extent_io_tree *io_tree; | |
724 | int ret = 0; | |
725 | ||
726 | again: | |
727 | while (!list_empty(&async_cow->extents)) { | |
728 | async_extent = list_entry(async_cow->extents.next, | |
729 | struct async_extent, list); | |
730 | list_del(&async_extent->list); | |
731 | ||
732 | io_tree = &BTRFS_I(inode)->io_tree; | |
733 | ||
734 | retry: | |
735 | /* did the compression code fall back to uncompressed IO? */ | |
736 | if (!async_extent->pages) { | |
737 | int page_started = 0; | |
738 | unsigned long nr_written = 0; | |
739 | ||
740 | lock_extent(io_tree, async_extent->start, | |
741 | async_extent->start + | |
742 | async_extent->ram_size - 1); | |
743 | ||
744 | /* allocate blocks */ | |
745 | ret = cow_file_range(inode, async_cow->locked_page, | |
746 | async_extent->start, | |
747 | async_extent->start + | |
748 | async_extent->ram_size - 1, | |
749 | async_extent->start + | |
750 | async_extent->ram_size - 1, | |
751 | &page_started, &nr_written, 0, | |
752 | NULL); | |
753 | ||
754 | /* JDM XXX */ | |
755 | ||
756 | /* | |
757 | * if page_started, cow_file_range inserted an | |
758 | * inline extent and took care of all the unlocking | |
759 | * and IO for us. Otherwise, we need to submit | |
760 | * all those pages down to the drive. | |
761 | */ | |
762 | if (!page_started && !ret) | |
763 | extent_write_locked_range(io_tree, | |
764 | inode, async_extent->start, | |
765 | async_extent->start + | |
766 | async_extent->ram_size - 1, | |
767 | btrfs_get_extent, | |
768 | WB_SYNC_ALL); | |
769 | else if (ret) | |
770 | unlock_page(async_cow->locked_page); | |
771 | kfree(async_extent); | |
772 | cond_resched(); | |
773 | continue; | |
774 | } | |
775 | ||
776 | lock_extent(io_tree, async_extent->start, | |
777 | async_extent->start + async_extent->ram_size - 1); | |
778 | ||
779 | ret = btrfs_reserve_extent(root, async_extent->ram_size, | |
780 | async_extent->compressed_size, | |
781 | async_extent->compressed_size, | |
782 | 0, alloc_hint, &ins, 1, 1); | |
783 | if (ret) { | |
784 | free_async_extent_pages(async_extent); | |
785 | ||
786 | if (ret == -ENOSPC) { | |
787 | unlock_extent(io_tree, async_extent->start, | |
788 | async_extent->start + | |
789 | async_extent->ram_size - 1); | |
790 | ||
791 | /* | |
792 | * we need to redirty the pages if we decide to | |
793 | * fallback to uncompressed IO, otherwise we | |
794 | * will not submit these pages down to lower | |
795 | * layers. | |
796 | */ | |
797 | extent_range_redirty_for_io(inode, | |
798 | async_extent->start, | |
799 | async_extent->start + | |
800 | async_extent->ram_size - 1); | |
801 | ||
802 | goto retry; | |
803 | } | |
804 | goto out_free; | |
805 | } | |
806 | /* | |
807 | * here we're doing allocation and writeback of the | |
808 | * compressed pages | |
809 | */ | |
810 | em = create_io_em(inode, async_extent->start, | |
811 | async_extent->ram_size, /* len */ | |
812 | async_extent->start, /* orig_start */ | |
813 | ins.objectid, /* block_start */ | |
814 | ins.offset, /* block_len */ | |
815 | ins.offset, /* orig_block_len */ | |
816 | async_extent->ram_size, /* ram_bytes */ | |
817 | async_extent->compress_type, | |
818 | BTRFS_ORDERED_COMPRESSED); | |
819 | if (IS_ERR(em)) | |
820 | /* ret value is not necessary due to void function */ | |
821 | goto out_free_reserve; | |
822 | free_extent_map(em); | |
823 | ||
824 | ret = btrfs_add_ordered_extent_compress(inode, | |
825 | async_extent->start, | |
826 | ins.objectid, | |
827 | async_extent->ram_size, | |
828 | ins.offset, | |
829 | BTRFS_ORDERED_COMPRESSED, | |
830 | async_extent->compress_type); | |
831 | if (ret) { | |
832 | btrfs_drop_extent_cache(BTRFS_I(inode), | |
833 | async_extent->start, | |
834 | async_extent->start + | |
835 | async_extent->ram_size - 1, 0); | |
836 | goto out_free_reserve; | |
837 | } | |
838 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
839 | ||
840 | /* | |
841 | * clear dirty, set writeback and unlock the pages. | |
842 | */ | |
843 | extent_clear_unlock_delalloc(inode, async_extent->start, | |
844 | async_extent->start + | |
845 | async_extent->ram_size - 1, | |
846 | async_extent->start + | |
847 | async_extent->ram_size - 1, | |
848 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, | |
849 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
850 | PAGE_SET_WRITEBACK); | |
851 | if (btrfs_submit_compressed_write(inode, | |
852 | async_extent->start, | |
853 | async_extent->ram_size, | |
854 | ins.objectid, | |
855 | ins.offset, async_extent->pages, | |
856 | async_extent->nr_pages)) { | |
857 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
858 | struct page *p = async_extent->pages[0]; | |
859 | const u64 start = async_extent->start; | |
860 | const u64 end = start + async_extent->ram_size - 1; | |
861 | ||
862 | p->mapping = inode->i_mapping; | |
863 | tree->ops->writepage_end_io_hook(p, start, end, | |
864 | NULL, 0); | |
865 | p->mapping = NULL; | |
866 | extent_clear_unlock_delalloc(inode, start, end, end, | |
867 | NULL, 0, | |
868 | PAGE_END_WRITEBACK | | |
869 | PAGE_SET_ERROR); | |
870 | free_async_extent_pages(async_extent); | |
871 | } | |
872 | alloc_hint = ins.objectid + ins.offset; | |
873 | kfree(async_extent); | |
874 | cond_resched(); | |
875 | } | |
876 | return; | |
877 | out_free_reserve: | |
878 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
879 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); | |
880 | out_free: | |
881 | extent_clear_unlock_delalloc(inode, async_extent->start, | |
882 | async_extent->start + | |
883 | async_extent->ram_size - 1, | |
884 | async_extent->start + | |
885 | async_extent->ram_size - 1, | |
886 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | | |
887 | EXTENT_DELALLOC_NEW | | |
888 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, | |
889 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
890 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | | |
891 | PAGE_SET_ERROR); | |
892 | free_async_extent_pages(async_extent); | |
893 | kfree(async_extent); | |
894 | goto again; | |
895 | } | |
896 | ||
897 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, | |
898 | u64 num_bytes) | |
899 | { | |
900 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
901 | struct extent_map *em; | |
902 | u64 alloc_hint = 0; | |
903 | ||
904 | read_lock(&em_tree->lock); | |
905 | em = search_extent_mapping(em_tree, start, num_bytes); | |
906 | if (em) { | |
907 | /* | |
908 | * if block start isn't an actual block number then find the | |
909 | * first block in this inode and use that as a hint. If that | |
910 | * block is also bogus then just don't worry about it. | |
911 | */ | |
912 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
913 | free_extent_map(em); | |
914 | em = search_extent_mapping(em_tree, 0, 0); | |
915 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
916 | alloc_hint = em->block_start; | |
917 | if (em) | |
918 | free_extent_map(em); | |
919 | } else { | |
920 | alloc_hint = em->block_start; | |
921 | free_extent_map(em); | |
922 | } | |
923 | } | |
924 | read_unlock(&em_tree->lock); | |
925 | ||
926 | return alloc_hint; | |
927 | } | |
928 | ||
929 | /* | |
930 | * when extent_io.c finds a delayed allocation range in the file, | |
931 | * the call backs end up in this code. The basic idea is to | |
932 | * allocate extents on disk for the range, and create ordered data structs | |
933 | * in ram to track those extents. | |
934 | * | |
935 | * locked_page is the page that writepage had locked already. We use | |
936 | * it to make sure we don't do extra locks or unlocks. | |
937 | * | |
938 | * *page_started is set to one if we unlock locked_page and do everything | |
939 | * required to start IO on it. It may be clean and already done with | |
940 | * IO when we return. | |
941 | */ | |
942 | static noinline int cow_file_range(struct inode *inode, | |
943 | struct page *locked_page, | |
944 | u64 start, u64 end, u64 delalloc_end, | |
945 | int *page_started, unsigned long *nr_written, | |
946 | int unlock, struct btrfs_dedupe_hash *hash) | |
947 | { | |
948 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
949 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
950 | u64 alloc_hint = 0; | |
951 | u64 num_bytes; | |
952 | unsigned long ram_size; | |
953 | u64 disk_num_bytes; | |
954 | u64 cur_alloc_size = 0; | |
955 | u64 blocksize = fs_info->sectorsize; | |
956 | struct btrfs_key ins; | |
957 | struct extent_map *em; | |
958 | unsigned clear_bits; | |
959 | unsigned long page_ops; | |
960 | bool extent_reserved = false; | |
961 | int ret = 0; | |
962 | ||
963 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { | |
964 | WARN_ON_ONCE(1); | |
965 | ret = -EINVAL; | |
966 | goto out_unlock; | |
967 | } | |
968 | ||
969 | num_bytes = ALIGN(end - start + 1, blocksize); | |
970 | num_bytes = max(blocksize, num_bytes); | |
971 | disk_num_bytes = num_bytes; | |
972 | ||
973 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); | |
974 | ||
975 | if (start == 0) { | |
976 | /* lets try to make an inline extent */ | |
977 | ret = cow_file_range_inline(root, inode, start, end, 0, | |
978 | BTRFS_COMPRESS_NONE, NULL); | |
979 | if (ret == 0) { | |
980 | extent_clear_unlock_delalloc(inode, start, end, | |
981 | delalloc_end, NULL, | |
982 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
983 | EXTENT_DELALLOC_NEW | | |
984 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
985 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | | |
986 | PAGE_END_WRITEBACK); | |
987 | btrfs_free_reserved_data_space_noquota(inode, start, | |
988 | end - start + 1); | |
989 | *nr_written = *nr_written + | |
990 | (end - start + PAGE_SIZE) / PAGE_SIZE; | |
991 | *page_started = 1; | |
992 | goto out; | |
993 | } else if (ret < 0) { | |
994 | goto out_unlock; | |
995 | } | |
996 | } | |
997 | ||
998 | BUG_ON(disk_num_bytes > | |
999 | btrfs_super_total_bytes(fs_info->super_copy)); | |
1000 | ||
1001 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); | |
1002 | btrfs_drop_extent_cache(BTRFS_I(inode), start, | |
1003 | start + num_bytes - 1, 0); | |
1004 | ||
1005 | while (disk_num_bytes > 0) { | |
1006 | cur_alloc_size = disk_num_bytes; | |
1007 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, | |
1008 | fs_info->sectorsize, 0, alloc_hint, | |
1009 | &ins, 1, 1); | |
1010 | if (ret < 0) | |
1011 | goto out_unlock; | |
1012 | cur_alloc_size = ins.offset; | |
1013 | extent_reserved = true; | |
1014 | ||
1015 | ram_size = ins.offset; | |
1016 | em = create_io_em(inode, start, ins.offset, /* len */ | |
1017 | start, /* orig_start */ | |
1018 | ins.objectid, /* block_start */ | |
1019 | ins.offset, /* block_len */ | |
1020 | ins.offset, /* orig_block_len */ | |
1021 | ram_size, /* ram_bytes */ | |
1022 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1023 | BTRFS_ORDERED_REGULAR /* type */); | |
1024 | if (IS_ERR(em)) | |
1025 | goto out_reserve; | |
1026 | free_extent_map(em); | |
1027 | ||
1028 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, | |
1029 | ram_size, cur_alloc_size, 0); | |
1030 | if (ret) | |
1031 | goto out_drop_extent_cache; | |
1032 | ||
1033 | if (root->root_key.objectid == | |
1034 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1035 | ret = btrfs_reloc_clone_csums(inode, start, | |
1036 | cur_alloc_size); | |
1037 | /* | |
1038 | * Only drop cache here, and process as normal. | |
1039 | * | |
1040 | * We must not allow extent_clear_unlock_delalloc() | |
1041 | * at out_unlock label to free meta of this ordered | |
1042 | * extent, as its meta should be freed by | |
1043 | * btrfs_finish_ordered_io(). | |
1044 | * | |
1045 | * So we must continue until @start is increased to | |
1046 | * skip current ordered extent. | |
1047 | */ | |
1048 | if (ret) | |
1049 | btrfs_drop_extent_cache(BTRFS_I(inode), start, | |
1050 | start + ram_size - 1, 0); | |
1051 | } | |
1052 | ||
1053 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
1054 | ||
1055 | /* we're not doing compressed IO, don't unlock the first | |
1056 | * page (which the caller expects to stay locked), don't | |
1057 | * clear any dirty bits and don't set any writeback bits | |
1058 | * | |
1059 | * Do set the Private2 bit so we know this page was properly | |
1060 | * setup for writepage | |
1061 | */ | |
1062 | page_ops = unlock ? PAGE_UNLOCK : 0; | |
1063 | page_ops |= PAGE_SET_PRIVATE2; | |
1064 | ||
1065 | extent_clear_unlock_delalloc(inode, start, | |
1066 | start + ram_size - 1, | |
1067 | delalloc_end, locked_page, | |
1068 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
1069 | page_ops); | |
1070 | if (disk_num_bytes < cur_alloc_size) | |
1071 | disk_num_bytes = 0; | |
1072 | else | |
1073 | disk_num_bytes -= cur_alloc_size; | |
1074 | num_bytes -= cur_alloc_size; | |
1075 | alloc_hint = ins.objectid + ins.offset; | |
1076 | start += cur_alloc_size; | |
1077 | extent_reserved = false; | |
1078 | ||
1079 | /* | |
1080 | * btrfs_reloc_clone_csums() error, since start is increased | |
1081 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1082 | * free metadata of current ordered extent, we're OK to exit. | |
1083 | */ | |
1084 | if (ret) | |
1085 | goto out_unlock; | |
1086 | } | |
1087 | out: | |
1088 | return ret; | |
1089 | ||
1090 | out_drop_extent_cache: | |
1091 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); | |
1092 | out_reserve: | |
1093 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
1094 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); | |
1095 | out_unlock: | |
1096 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | | |
1097 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
1098 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | | |
1099 | PAGE_END_WRITEBACK; | |
1100 | /* | |
1101 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1102 | * failed to create the respective ordered extent, then it means that | |
1103 | * when we reserved the extent we decremented the extent's size from | |
1104 | * the data space_info's bytes_may_use counter and incremented the | |
1105 | * space_info's bytes_reserved counter by the same amount. We must make | |
1106 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1107 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1108 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1109 | */ | |
1110 | if (extent_reserved) { | |
1111 | extent_clear_unlock_delalloc(inode, start, | |
1112 | start + cur_alloc_size, | |
1113 | start + cur_alloc_size, | |
1114 | locked_page, | |
1115 | clear_bits, | |
1116 | page_ops); | |
1117 | start += cur_alloc_size; | |
1118 | if (start >= end) | |
1119 | goto out; | |
1120 | } | |
1121 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, | |
1122 | locked_page, | |
1123 | clear_bits | EXTENT_CLEAR_DATA_RESV, | |
1124 | page_ops); | |
1125 | goto out; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * work queue call back to started compression on a file and pages | |
1130 | */ | |
1131 | static noinline void async_cow_start(struct btrfs_work *work) | |
1132 | { | |
1133 | struct async_cow *async_cow; | |
1134 | int num_added = 0; | |
1135 | async_cow = container_of(work, struct async_cow, work); | |
1136 | ||
1137 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1138 | async_cow->start, async_cow->end, async_cow, | |
1139 | &num_added); | |
1140 | if (num_added == 0) { | |
1141 | btrfs_add_delayed_iput(async_cow->inode); | |
1142 | async_cow->inode = NULL; | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * work queue call back to submit previously compressed pages | |
1148 | */ | |
1149 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1150 | { | |
1151 | struct btrfs_fs_info *fs_info; | |
1152 | struct async_cow *async_cow; | |
1153 | struct btrfs_root *root; | |
1154 | unsigned long nr_pages; | |
1155 | ||
1156 | async_cow = container_of(work, struct async_cow, work); | |
1157 | ||
1158 | root = async_cow->root; | |
1159 | fs_info = root->fs_info; | |
1160 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> | |
1161 | PAGE_SHIFT; | |
1162 | ||
1163 | /* | |
1164 | * atomic_sub_return implies a barrier for waitqueue_active | |
1165 | */ | |
1166 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < | |
1167 | 5 * SZ_1M && | |
1168 | waitqueue_active(&fs_info->async_submit_wait)) | |
1169 | wake_up(&fs_info->async_submit_wait); | |
1170 | ||
1171 | if (async_cow->inode) | |
1172 | submit_compressed_extents(async_cow->inode, async_cow); | |
1173 | } | |
1174 | ||
1175 | static noinline void async_cow_free(struct btrfs_work *work) | |
1176 | { | |
1177 | struct async_cow *async_cow; | |
1178 | async_cow = container_of(work, struct async_cow, work); | |
1179 | if (async_cow->inode) | |
1180 | btrfs_add_delayed_iput(async_cow->inode); | |
1181 | kfree(async_cow); | |
1182 | } | |
1183 | ||
1184 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1185 | u64 start, u64 end, int *page_started, | |
1186 | unsigned long *nr_written) | |
1187 | { | |
1188 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1189 | struct async_cow *async_cow; | |
1190 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1191 | unsigned long nr_pages; | |
1192 | u64 cur_end; | |
1193 | ||
1194 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, | |
1195 | 1, 0, NULL, GFP_NOFS); | |
1196 | while (start < end) { | |
1197 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); | |
1198 | BUG_ON(!async_cow); /* -ENOMEM */ | |
1199 | async_cow->inode = igrab(inode); | |
1200 | async_cow->root = root; | |
1201 | async_cow->locked_page = locked_page; | |
1202 | async_cow->start = start; | |
1203 | ||
1204 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && | |
1205 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) | |
1206 | cur_end = end; | |
1207 | else | |
1208 | cur_end = min(end, start + SZ_512K - 1); | |
1209 | ||
1210 | async_cow->end = cur_end; | |
1211 | INIT_LIST_HEAD(&async_cow->extents); | |
1212 | ||
1213 | btrfs_init_work(&async_cow->work, | |
1214 | btrfs_delalloc_helper, | |
1215 | async_cow_start, async_cow_submit, | |
1216 | async_cow_free); | |
1217 | ||
1218 | nr_pages = (cur_end - start + PAGE_SIZE) >> | |
1219 | PAGE_SHIFT; | |
1220 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); | |
1221 | ||
1222 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); | |
1223 | ||
1224 | while (atomic_read(&fs_info->async_submit_draining) && | |
1225 | atomic_read(&fs_info->async_delalloc_pages)) { | |
1226 | wait_event(fs_info->async_submit_wait, | |
1227 | (atomic_read(&fs_info->async_delalloc_pages) == | |
1228 | 0)); | |
1229 | } | |
1230 | ||
1231 | *nr_written += nr_pages; | |
1232 | start = cur_end + 1; | |
1233 | } | |
1234 | *page_started = 1; | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, | |
1239 | u64 bytenr, u64 num_bytes) | |
1240 | { | |
1241 | int ret; | |
1242 | struct btrfs_ordered_sum *sums; | |
1243 | LIST_HEAD(list); | |
1244 | ||
1245 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, | |
1246 | bytenr + num_bytes - 1, &list, 0); | |
1247 | if (ret == 0 && list_empty(&list)) | |
1248 | return 0; | |
1249 | ||
1250 | while (!list_empty(&list)) { | |
1251 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1252 | list_del(&sums->list); | |
1253 | kfree(sums); | |
1254 | } | |
1255 | return 1; | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1260 | * of the extents that exist in the file, and COWs the file as required. | |
1261 | * | |
1262 | * If no cow copies or snapshots exist, we write directly to the existing | |
1263 | * blocks on disk | |
1264 | */ | |
1265 | static noinline int run_delalloc_nocow(struct inode *inode, | |
1266 | struct page *locked_page, | |
1267 | u64 start, u64 end, int *page_started, int force, | |
1268 | unsigned long *nr_written) | |
1269 | { | |
1270 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1271 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1272 | struct extent_buffer *leaf; | |
1273 | struct btrfs_path *path; | |
1274 | struct btrfs_file_extent_item *fi; | |
1275 | struct btrfs_key found_key; | |
1276 | struct extent_map *em; | |
1277 | u64 cow_start; | |
1278 | u64 cur_offset; | |
1279 | u64 extent_end; | |
1280 | u64 extent_offset; | |
1281 | u64 disk_bytenr; | |
1282 | u64 num_bytes; | |
1283 | u64 disk_num_bytes; | |
1284 | u64 ram_bytes; | |
1285 | int extent_type; | |
1286 | int ret, err; | |
1287 | int type; | |
1288 | int nocow; | |
1289 | int check_prev = 1; | |
1290 | bool nolock; | |
1291 | u64 ino = btrfs_ino(BTRFS_I(inode)); | |
1292 | ||
1293 | path = btrfs_alloc_path(); | |
1294 | if (!path) { | |
1295 | extent_clear_unlock_delalloc(inode, start, end, end, | |
1296 | locked_page, | |
1297 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
1298 | EXTENT_DO_ACCOUNTING | | |
1299 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
1300 | PAGE_CLEAR_DIRTY | | |
1301 | PAGE_SET_WRITEBACK | | |
1302 | PAGE_END_WRITEBACK); | |
1303 | return -ENOMEM; | |
1304 | } | |
1305 | ||
1306 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); | |
1307 | ||
1308 | cow_start = (u64)-1; | |
1309 | cur_offset = start; | |
1310 | while (1) { | |
1311 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, | |
1312 | cur_offset, 0); | |
1313 | if (ret < 0) | |
1314 | goto error; | |
1315 | if (ret > 0 && path->slots[0] > 0 && check_prev) { | |
1316 | leaf = path->nodes[0]; | |
1317 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1318 | path->slots[0] - 1); | |
1319 | if (found_key.objectid == ino && | |
1320 | found_key.type == BTRFS_EXTENT_DATA_KEY) | |
1321 | path->slots[0]--; | |
1322 | } | |
1323 | check_prev = 0; | |
1324 | next_slot: | |
1325 | leaf = path->nodes[0]; | |
1326 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1327 | ret = btrfs_next_leaf(root, path); | |
1328 | if (ret < 0) | |
1329 | goto error; | |
1330 | if (ret > 0) | |
1331 | break; | |
1332 | leaf = path->nodes[0]; | |
1333 | } | |
1334 | ||
1335 | nocow = 0; | |
1336 | disk_bytenr = 0; | |
1337 | num_bytes = 0; | |
1338 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1339 | ||
1340 | if (found_key.objectid > ino) | |
1341 | break; | |
1342 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1343 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1344 | path->slots[0]++; | |
1345 | goto next_slot; | |
1346 | } | |
1347 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
1348 | found_key.offset > end) | |
1349 | break; | |
1350 | ||
1351 | if (found_key.offset > cur_offset) { | |
1352 | extent_end = found_key.offset; | |
1353 | extent_type = 0; | |
1354 | goto out_check; | |
1355 | } | |
1356 | ||
1357 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1358 | struct btrfs_file_extent_item); | |
1359 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1360 | ||
1361 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
1362 | if (extent_type == BTRFS_FILE_EXTENT_REG || | |
1363 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
1364 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1365 | extent_offset = btrfs_file_extent_offset(leaf, fi); | |
1366 | extent_end = found_key.offset + | |
1367 | btrfs_file_extent_num_bytes(leaf, fi); | |
1368 | disk_num_bytes = | |
1369 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
1370 | if (extent_end <= start) { | |
1371 | path->slots[0]++; | |
1372 | goto next_slot; | |
1373 | } | |
1374 | if (disk_bytenr == 0) | |
1375 | goto out_check; | |
1376 | if (btrfs_file_extent_compression(leaf, fi) || | |
1377 | btrfs_file_extent_encryption(leaf, fi) || | |
1378 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1379 | goto out_check; | |
1380 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) | |
1381 | goto out_check; | |
1382 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) | |
1383 | goto out_check; | |
1384 | if (btrfs_cross_ref_exist(root, ino, | |
1385 | found_key.offset - | |
1386 | extent_offset, disk_bytenr)) | |
1387 | goto out_check; | |
1388 | disk_bytenr += extent_offset; | |
1389 | disk_bytenr += cur_offset - found_key.offset; | |
1390 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
1391 | /* | |
1392 | * if there are pending snapshots for this root, | |
1393 | * we fall into common COW way. | |
1394 | */ | |
1395 | if (!nolock) { | |
1396 | err = btrfs_start_write_no_snapshoting(root); | |
1397 | if (!err) | |
1398 | goto out_check; | |
1399 | } | |
1400 | /* | |
1401 | * force cow if csum exists in the range. | |
1402 | * this ensure that csum for a given extent are | |
1403 | * either valid or do not exist. | |
1404 | */ | |
1405 | if (csum_exist_in_range(fs_info, disk_bytenr, | |
1406 | num_bytes)) { | |
1407 | if (!nolock) | |
1408 | btrfs_end_write_no_snapshoting(root); | |
1409 | goto out_check; | |
1410 | } | |
1411 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { | |
1412 | if (!nolock) | |
1413 | btrfs_end_write_no_snapshoting(root); | |
1414 | goto out_check; | |
1415 | } | |
1416 | nocow = 1; | |
1417 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1418 | extent_end = found_key.offset + | |
1419 | btrfs_file_extent_inline_len(leaf, | |
1420 | path->slots[0], fi); | |
1421 | extent_end = ALIGN(extent_end, | |
1422 | fs_info->sectorsize); | |
1423 | } else { | |
1424 | BUG_ON(1); | |
1425 | } | |
1426 | out_check: | |
1427 | if (extent_end <= start) { | |
1428 | path->slots[0]++; | |
1429 | if (!nolock && nocow) | |
1430 | btrfs_end_write_no_snapshoting(root); | |
1431 | if (nocow) | |
1432 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1433 | goto next_slot; | |
1434 | } | |
1435 | if (!nocow) { | |
1436 | if (cow_start == (u64)-1) | |
1437 | cow_start = cur_offset; | |
1438 | cur_offset = extent_end; | |
1439 | if (cur_offset > end) | |
1440 | break; | |
1441 | path->slots[0]++; | |
1442 | goto next_slot; | |
1443 | } | |
1444 | ||
1445 | btrfs_release_path(path); | |
1446 | if (cow_start != (u64)-1) { | |
1447 | ret = cow_file_range(inode, locked_page, | |
1448 | cow_start, found_key.offset - 1, | |
1449 | end, page_started, nr_written, 1, | |
1450 | NULL); | |
1451 | if (ret) { | |
1452 | if (!nolock && nocow) | |
1453 | btrfs_end_write_no_snapshoting(root); | |
1454 | if (nocow) | |
1455 | btrfs_dec_nocow_writers(fs_info, | |
1456 | disk_bytenr); | |
1457 | goto error; | |
1458 | } | |
1459 | cow_start = (u64)-1; | |
1460 | } | |
1461 | ||
1462 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
1463 | u64 orig_start = found_key.offset - extent_offset; | |
1464 | ||
1465 | em = create_io_em(inode, cur_offset, num_bytes, | |
1466 | orig_start, | |
1467 | disk_bytenr, /* block_start */ | |
1468 | num_bytes, /* block_len */ | |
1469 | disk_num_bytes, /* orig_block_len */ | |
1470 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1471 | BTRFS_ORDERED_PREALLOC); | |
1472 | if (IS_ERR(em)) { | |
1473 | if (!nolock && nocow) | |
1474 | btrfs_end_write_no_snapshoting(root); | |
1475 | if (nocow) | |
1476 | btrfs_dec_nocow_writers(fs_info, | |
1477 | disk_bytenr); | |
1478 | ret = PTR_ERR(em); | |
1479 | goto error; | |
1480 | } | |
1481 | free_extent_map(em); | |
1482 | } | |
1483 | ||
1484 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
1485 | type = BTRFS_ORDERED_PREALLOC; | |
1486 | } else { | |
1487 | type = BTRFS_ORDERED_NOCOW; | |
1488 | } | |
1489 | ||
1490 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
1491 | num_bytes, num_bytes, type); | |
1492 | if (nocow) | |
1493 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1494 | BUG_ON(ret); /* -ENOMEM */ | |
1495 | ||
1496 | if (root->root_key.objectid == | |
1497 | BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1498 | /* | |
1499 | * Error handled later, as we must prevent | |
1500 | * extent_clear_unlock_delalloc() in error handler | |
1501 | * from freeing metadata of created ordered extent. | |
1502 | */ | |
1503 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1504 | num_bytes); | |
1505 | ||
1506 | extent_clear_unlock_delalloc(inode, cur_offset, | |
1507 | cur_offset + num_bytes - 1, end, | |
1508 | locked_page, EXTENT_LOCKED | | |
1509 | EXTENT_DELALLOC | | |
1510 | EXTENT_CLEAR_DATA_RESV, | |
1511 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1512 | ||
1513 | if (!nolock && nocow) | |
1514 | btrfs_end_write_no_snapshoting(root); | |
1515 | cur_offset = extent_end; | |
1516 | ||
1517 | /* | |
1518 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1519 | * handler, as metadata for created ordered extent will only | |
1520 | * be freed by btrfs_finish_ordered_io(). | |
1521 | */ | |
1522 | if (ret) | |
1523 | goto error; | |
1524 | if (cur_offset > end) | |
1525 | break; | |
1526 | } | |
1527 | btrfs_release_path(path); | |
1528 | ||
1529 | if (cur_offset <= end && cow_start == (u64)-1) { | |
1530 | cow_start = cur_offset; | |
1531 | cur_offset = end; | |
1532 | } | |
1533 | ||
1534 | if (cow_start != (u64)-1) { | |
1535 | ret = cow_file_range(inode, locked_page, cow_start, end, end, | |
1536 | page_started, nr_written, 1, NULL); | |
1537 | if (ret) | |
1538 | goto error; | |
1539 | } | |
1540 | ||
1541 | error: | |
1542 | if (ret && cur_offset < end) | |
1543 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, | |
1544 | locked_page, EXTENT_LOCKED | | |
1545 | EXTENT_DELALLOC | EXTENT_DEFRAG | | |
1546 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1547 | PAGE_CLEAR_DIRTY | | |
1548 | PAGE_SET_WRITEBACK | | |
1549 | PAGE_END_WRITEBACK); | |
1550 | btrfs_free_path(path); | |
1551 | return ret; | |
1552 | } | |
1553 | ||
1554 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) | |
1555 | { | |
1556 | ||
1557 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1558 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1559 | return 0; | |
1560 | ||
1561 | /* | |
1562 | * @defrag_bytes is a hint value, no spinlock held here, | |
1563 | * if is not zero, it means the file is defragging. | |
1564 | * Force cow if given extent needs to be defragged. | |
1565 | */ | |
1566 | if (BTRFS_I(inode)->defrag_bytes && | |
1567 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1568 | EXTENT_DEFRAG, 0, NULL)) | |
1569 | return 1; | |
1570 | ||
1571 | return 0; | |
1572 | } | |
1573 | ||
1574 | /* | |
1575 | * extent_io.c call back to do delayed allocation processing | |
1576 | */ | |
1577 | static int run_delalloc_range(void *private_data, struct page *locked_page, | |
1578 | u64 start, u64 end, int *page_started, | |
1579 | unsigned long *nr_written) | |
1580 | { | |
1581 | struct inode *inode = private_data; | |
1582 | int ret; | |
1583 | int force_cow = need_force_cow(inode, start, end); | |
1584 | ||
1585 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { | |
1586 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
1587 | page_started, 1, nr_written); | |
1588 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { | |
1589 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
1590 | page_started, 0, nr_written); | |
1591 | } else if (!inode_need_compress(inode)) { | |
1592 | ret = cow_file_range(inode, locked_page, start, end, end, | |
1593 | page_started, nr_written, 1, NULL); | |
1594 | } else { | |
1595 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1596 | &BTRFS_I(inode)->runtime_flags); | |
1597 | ret = cow_file_range_async(inode, locked_page, start, end, | |
1598 | page_started, nr_written); | |
1599 | } | |
1600 | if (ret) | |
1601 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); | |
1602 | return ret; | |
1603 | } | |
1604 | ||
1605 | static void btrfs_split_extent_hook(void *private_data, | |
1606 | struct extent_state *orig, u64 split) | |
1607 | { | |
1608 | struct inode *inode = private_data; | |
1609 | u64 size; | |
1610 | ||
1611 | /* not delalloc, ignore it */ | |
1612 | if (!(orig->state & EXTENT_DELALLOC)) | |
1613 | return; | |
1614 | ||
1615 | size = orig->end - orig->start + 1; | |
1616 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1617 | u32 num_extents; | |
1618 | u64 new_size; | |
1619 | ||
1620 | /* | |
1621 | * See the explanation in btrfs_merge_extent_hook, the same | |
1622 | * applies here, just in reverse. | |
1623 | */ | |
1624 | new_size = orig->end - split + 1; | |
1625 | num_extents = count_max_extents(new_size); | |
1626 | new_size = split - orig->start; | |
1627 | num_extents += count_max_extents(new_size); | |
1628 | if (count_max_extents(size) >= num_extents) | |
1629 | return; | |
1630 | } | |
1631 | ||
1632 | spin_lock(&BTRFS_I(inode)->lock); | |
1633 | BTRFS_I(inode)->outstanding_extents++; | |
1634 | spin_unlock(&BTRFS_I(inode)->lock); | |
1635 | } | |
1636 | ||
1637 | /* | |
1638 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1639 | * extents so we can keep track of new extents that are just merged onto old | |
1640 | * extents, such as when we are doing sequential writes, so we can properly | |
1641 | * account for the metadata space we'll need. | |
1642 | */ | |
1643 | static void btrfs_merge_extent_hook(void *private_data, | |
1644 | struct extent_state *new, | |
1645 | struct extent_state *other) | |
1646 | { | |
1647 | struct inode *inode = private_data; | |
1648 | u64 new_size, old_size; | |
1649 | u32 num_extents; | |
1650 | ||
1651 | /* not delalloc, ignore it */ | |
1652 | if (!(other->state & EXTENT_DELALLOC)) | |
1653 | return; | |
1654 | ||
1655 | if (new->start > other->start) | |
1656 | new_size = new->end - other->start + 1; | |
1657 | else | |
1658 | new_size = other->end - new->start + 1; | |
1659 | ||
1660 | /* we're not bigger than the max, unreserve the space and go */ | |
1661 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1662 | spin_lock(&BTRFS_I(inode)->lock); | |
1663 | BTRFS_I(inode)->outstanding_extents--; | |
1664 | spin_unlock(&BTRFS_I(inode)->lock); | |
1665 | return; | |
1666 | } | |
1667 | ||
1668 | /* | |
1669 | * We have to add up either side to figure out how many extents were | |
1670 | * accounted for before we merged into one big extent. If the number of | |
1671 | * extents we accounted for is <= the amount we need for the new range | |
1672 | * then we can return, otherwise drop. Think of it like this | |
1673 | * | |
1674 | * [ 4k][MAX_SIZE] | |
1675 | * | |
1676 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1677 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1678 | * we have 1 so they are == and we can return. But in this case | |
1679 | * | |
1680 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1681 | * | |
1682 | * Each range on their own accounts for 2 extents, but merged together | |
1683 | * they are only 3 extents worth of accounting, so we need to drop in | |
1684 | * this case. | |
1685 | */ | |
1686 | old_size = other->end - other->start + 1; | |
1687 | num_extents = count_max_extents(old_size); | |
1688 | old_size = new->end - new->start + 1; | |
1689 | num_extents += count_max_extents(old_size); | |
1690 | if (count_max_extents(new_size) >= num_extents) | |
1691 | return; | |
1692 | ||
1693 | spin_lock(&BTRFS_I(inode)->lock); | |
1694 | BTRFS_I(inode)->outstanding_extents--; | |
1695 | spin_unlock(&BTRFS_I(inode)->lock); | |
1696 | } | |
1697 | ||
1698 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, | |
1699 | struct inode *inode) | |
1700 | { | |
1701 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1702 | ||
1703 | spin_lock(&root->delalloc_lock); | |
1704 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1705 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1706 | &root->delalloc_inodes); | |
1707 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1708 | &BTRFS_I(inode)->runtime_flags); | |
1709 | root->nr_delalloc_inodes++; | |
1710 | if (root->nr_delalloc_inodes == 1) { | |
1711 | spin_lock(&fs_info->delalloc_root_lock); | |
1712 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1713 | list_add_tail(&root->delalloc_root, | |
1714 | &fs_info->delalloc_roots); | |
1715 | spin_unlock(&fs_info->delalloc_root_lock); | |
1716 | } | |
1717 | } | |
1718 | spin_unlock(&root->delalloc_lock); | |
1719 | } | |
1720 | ||
1721 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1722 | struct btrfs_inode *inode) | |
1723 | { | |
1724 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); | |
1725 | ||
1726 | spin_lock(&root->delalloc_lock); | |
1727 | if (!list_empty(&inode->delalloc_inodes)) { | |
1728 | list_del_init(&inode->delalloc_inodes); | |
1729 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1730 | &inode->runtime_flags); | |
1731 | root->nr_delalloc_inodes--; | |
1732 | if (!root->nr_delalloc_inodes) { | |
1733 | spin_lock(&fs_info->delalloc_root_lock); | |
1734 | BUG_ON(list_empty(&root->delalloc_root)); | |
1735 | list_del_init(&root->delalloc_root); | |
1736 | spin_unlock(&fs_info->delalloc_root_lock); | |
1737 | } | |
1738 | } | |
1739 | spin_unlock(&root->delalloc_lock); | |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1744 | * bytes in this file, and to maintain the list of inodes that | |
1745 | * have pending delalloc work to be done. | |
1746 | */ | |
1747 | static void btrfs_set_bit_hook(void *private_data, | |
1748 | struct extent_state *state, unsigned *bits) | |
1749 | { | |
1750 | struct inode *inode = private_data; | |
1751 | ||
1752 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1753 | ||
1754 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) | |
1755 | WARN_ON(1); | |
1756 | /* | |
1757 | * set_bit and clear bit hooks normally require _irqsave/restore | |
1758 | * but in this case, we are only testing for the DELALLOC | |
1759 | * bit, which is only set or cleared with irqs on | |
1760 | */ | |
1761 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { | |
1762 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1763 | u64 len = state->end + 1 - state->start; | |
1764 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); | |
1765 | ||
1766 | if (*bits & EXTENT_FIRST_DELALLOC) { | |
1767 | *bits &= ~EXTENT_FIRST_DELALLOC; | |
1768 | } else { | |
1769 | spin_lock(&BTRFS_I(inode)->lock); | |
1770 | BTRFS_I(inode)->outstanding_extents++; | |
1771 | spin_unlock(&BTRFS_I(inode)->lock); | |
1772 | } | |
1773 | ||
1774 | /* For sanity tests */ | |
1775 | if (btrfs_is_testing(fs_info)) | |
1776 | return; | |
1777 | ||
1778 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, | |
1779 | fs_info->delalloc_batch); | |
1780 | spin_lock(&BTRFS_I(inode)->lock); | |
1781 | BTRFS_I(inode)->delalloc_bytes += len; | |
1782 | if (*bits & EXTENT_DEFRAG) | |
1783 | BTRFS_I(inode)->defrag_bytes += len; | |
1784 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1785 | &BTRFS_I(inode)->runtime_flags)) | |
1786 | btrfs_add_delalloc_inodes(root, inode); | |
1787 | spin_unlock(&BTRFS_I(inode)->lock); | |
1788 | } | |
1789 | ||
1790 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1791 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1792 | spin_lock(&BTRFS_I(inode)->lock); | |
1793 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1794 | state->start; | |
1795 | spin_unlock(&BTRFS_I(inode)->lock); | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | /* | |
1800 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1801 | */ | |
1802 | static void btrfs_clear_bit_hook(void *private_data, | |
1803 | struct extent_state *state, | |
1804 | unsigned *bits) | |
1805 | { | |
1806 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); | |
1807 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); | |
1808 | u64 len = state->end + 1 - state->start; | |
1809 | u32 num_extents = count_max_extents(len); | |
1810 | ||
1811 | spin_lock(&inode->lock); | |
1812 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1813 | inode->defrag_bytes -= len; | |
1814 | spin_unlock(&inode->lock); | |
1815 | ||
1816 | /* | |
1817 | * set_bit and clear bit hooks normally require _irqsave/restore | |
1818 | * but in this case, we are only testing for the DELALLOC | |
1819 | * bit, which is only set or cleared with irqs on | |
1820 | */ | |
1821 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { | |
1822 | struct btrfs_root *root = inode->root; | |
1823 | bool do_list = !btrfs_is_free_space_inode(inode); | |
1824 | ||
1825 | if (*bits & EXTENT_FIRST_DELALLOC) { | |
1826 | *bits &= ~EXTENT_FIRST_DELALLOC; | |
1827 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { | |
1828 | spin_lock(&inode->lock); | |
1829 | inode->outstanding_extents -= num_extents; | |
1830 | spin_unlock(&inode->lock); | |
1831 | } | |
1832 | ||
1833 | /* | |
1834 | * We don't reserve metadata space for space cache inodes so we | |
1835 | * don't need to call dellalloc_release_metadata if there is an | |
1836 | * error. | |
1837 | */ | |
1838 | if (*bits & EXTENT_CLEAR_META_RESV && | |
1839 | root != fs_info->tree_root) | |
1840 | btrfs_delalloc_release_metadata(inode, len); | |
1841 | ||
1842 | /* For sanity tests. */ | |
1843 | if (btrfs_is_testing(fs_info)) | |
1844 | return; | |
1845 | ||
1846 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && | |
1847 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1848 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
1849 | btrfs_free_reserved_data_space_noquota( | |
1850 | &inode->vfs_inode, | |
1851 | state->start, len); | |
1852 | ||
1853 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, | |
1854 | fs_info->delalloc_batch); | |
1855 | spin_lock(&inode->lock); | |
1856 | inode->delalloc_bytes -= len; | |
1857 | if (do_list && inode->delalloc_bytes == 0 && | |
1858 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1859 | &inode->runtime_flags)) | |
1860 | btrfs_del_delalloc_inode(root, inode); | |
1861 | spin_unlock(&inode->lock); | |
1862 | } | |
1863 | ||
1864 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1865 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1866 | spin_lock(&inode->lock); | |
1867 | ASSERT(inode->new_delalloc_bytes >= len); | |
1868 | inode->new_delalloc_bytes -= len; | |
1869 | spin_unlock(&inode->lock); | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | /* | |
1874 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1875 | * we don't create bios that span stripes or chunks | |
1876 | * | |
1877 | * return 1 if page cannot be merged to bio | |
1878 | * return 0 if page can be merged to bio | |
1879 | * return error otherwise | |
1880 | */ | |
1881 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, | |
1882 | size_t size, struct bio *bio, | |
1883 | unsigned long bio_flags) | |
1884 | { | |
1885 | struct inode *inode = page->mapping->host; | |
1886 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1887 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; | |
1888 | u64 length = 0; | |
1889 | u64 map_length; | |
1890 | int ret; | |
1891 | ||
1892 | if (bio_flags & EXTENT_BIO_COMPRESSED) | |
1893 | return 0; | |
1894 | ||
1895 | length = bio->bi_iter.bi_size; | |
1896 | map_length = length; | |
1897 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, | |
1898 | NULL, 0); | |
1899 | if (ret < 0) | |
1900 | return ret; | |
1901 | if (map_length < length + size) | |
1902 | return 1; | |
1903 | return 0; | |
1904 | } | |
1905 | ||
1906 | /* | |
1907 | * in order to insert checksums into the metadata in large chunks, | |
1908 | * we wait until bio submission time. All the pages in the bio are | |
1909 | * checksummed and sums are attached onto the ordered extent record. | |
1910 | * | |
1911 | * At IO completion time the cums attached on the ordered extent record | |
1912 | * are inserted into the btree | |
1913 | */ | |
1914 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, | |
1915 | int mirror_num, unsigned long bio_flags, | |
1916 | u64 bio_offset) | |
1917 | { | |
1918 | struct inode *inode = private_data; | |
1919 | blk_status_t ret = 0; | |
1920 | ||
1921 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); | |
1922 | BUG_ON(ret); /* -ENOMEM */ | |
1923 | return 0; | |
1924 | } | |
1925 | ||
1926 | /* | |
1927 | * in order to insert checksums into the metadata in large chunks, | |
1928 | * we wait until bio submission time. All the pages in the bio are | |
1929 | * checksummed and sums are attached onto the ordered extent record. | |
1930 | * | |
1931 | * At IO completion time the cums attached on the ordered extent record | |
1932 | * are inserted into the btree | |
1933 | */ | |
1934 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, | |
1935 | int mirror_num, unsigned long bio_flags, | |
1936 | u64 bio_offset) | |
1937 | { | |
1938 | struct inode *inode = private_data; | |
1939 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1940 | blk_status_t ret; | |
1941 | ||
1942 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); | |
1943 | if (ret) { | |
1944 | bio->bi_status = ret; | |
1945 | bio_endio(bio); | |
1946 | } | |
1947 | return ret; | |
1948 | } | |
1949 | ||
1950 | /* | |
1951 | * extent_io.c submission hook. This does the right thing for csum calculation | |
1952 | * on write, or reading the csums from the tree before a read | |
1953 | */ | |
1954 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, | |
1955 | int mirror_num, unsigned long bio_flags, | |
1956 | u64 bio_offset) | |
1957 | { | |
1958 | struct inode *inode = private_data; | |
1959 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1960 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1961 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; | |
1962 | blk_status_t ret = 0; | |
1963 | int skip_sum; | |
1964 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
1965 | ||
1966 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
1967 | ||
1968 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) | |
1969 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; | |
1970 | ||
1971 | if (bio_op(bio) != REQ_OP_WRITE) { | |
1972 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); | |
1973 | if (ret) | |
1974 | goto out; | |
1975 | ||
1976 | if (bio_flags & EXTENT_BIO_COMPRESSED) { | |
1977 | ret = btrfs_submit_compressed_read(inode, bio, | |
1978 | mirror_num, | |
1979 | bio_flags); | |
1980 | goto out; | |
1981 | } else if (!skip_sum) { | |
1982 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); | |
1983 | if (ret) | |
1984 | goto out; | |
1985 | } | |
1986 | goto mapit; | |
1987 | } else if (async && !skip_sum) { | |
1988 | /* csum items have already been cloned */ | |
1989 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1990 | goto mapit; | |
1991 | /* we're doing a write, do the async checksumming */ | |
1992 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, | |
1993 | bio_offset, inode, | |
1994 | __btrfs_submit_bio_start, | |
1995 | __btrfs_submit_bio_done); | |
1996 | goto out; | |
1997 | } else if (!skip_sum) { | |
1998 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); | |
1999 | if (ret) | |
2000 | goto out; | |
2001 | } | |
2002 | ||
2003 | mapit: | |
2004 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); | |
2005 | ||
2006 | out: | |
2007 | if (ret) { | |
2008 | bio->bi_status = ret; | |
2009 | bio_endio(bio); | |
2010 | } | |
2011 | return ret; | |
2012 | } | |
2013 | ||
2014 | /* | |
2015 | * given a list of ordered sums record them in the inode. This happens | |
2016 | * at IO completion time based on sums calculated at bio submission time. | |
2017 | */ | |
2018 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, | |
2019 | struct inode *inode, struct list_head *list) | |
2020 | { | |
2021 | struct btrfs_ordered_sum *sum; | |
2022 | ||
2023 | list_for_each_entry(sum, list, list) { | |
2024 | trans->adding_csums = 1; | |
2025 | btrfs_csum_file_blocks(trans, | |
2026 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
2027 | trans->adding_csums = 0; | |
2028 | } | |
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, | |
2033 | unsigned int extra_bits, | |
2034 | struct extent_state **cached_state, int dedupe) | |
2035 | { | |
2036 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); | |
2037 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, | |
2038 | extra_bits, cached_state); | |
2039 | } | |
2040 | ||
2041 | /* see btrfs_writepage_start_hook for details on why this is required */ | |
2042 | struct btrfs_writepage_fixup { | |
2043 | struct page *page; | |
2044 | struct btrfs_work work; | |
2045 | }; | |
2046 | ||
2047 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) | |
2048 | { | |
2049 | struct btrfs_writepage_fixup *fixup; | |
2050 | struct btrfs_ordered_extent *ordered; | |
2051 | struct extent_state *cached_state = NULL; | |
2052 | struct extent_changeset *data_reserved = NULL; | |
2053 | struct page *page; | |
2054 | struct inode *inode; | |
2055 | u64 page_start; | |
2056 | u64 page_end; | |
2057 | int ret; | |
2058 | ||
2059 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2060 | page = fixup->page; | |
2061 | again: | |
2062 | lock_page(page); | |
2063 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2064 | ClearPageChecked(page); | |
2065 | goto out_page; | |
2066 | } | |
2067 | ||
2068 | inode = page->mapping->host; | |
2069 | page_start = page_offset(page); | |
2070 | page_end = page_offset(page) + PAGE_SIZE - 1; | |
2071 | ||
2072 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, | |
2073 | &cached_state); | |
2074 | ||
2075 | /* already ordered? We're done */ | |
2076 | if (PagePrivate2(page)) | |
2077 | goto out; | |
2078 | ||
2079 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, | |
2080 | PAGE_SIZE); | |
2081 | if (ordered) { | |
2082 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, | |
2083 | page_end, &cached_state, GFP_NOFS); | |
2084 | unlock_page(page); | |
2085 | btrfs_start_ordered_extent(inode, ordered, 1); | |
2086 | btrfs_put_ordered_extent(ordered); | |
2087 | goto again; | |
2088 | } | |
2089 | ||
2090 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, | |
2091 | PAGE_SIZE); | |
2092 | if (ret) { | |
2093 | mapping_set_error(page->mapping, ret); | |
2094 | end_extent_writepage(page, ret, page_start, page_end); | |
2095 | ClearPageChecked(page); | |
2096 | goto out; | |
2097 | } | |
2098 | ||
2099 | btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state, | |
2100 | 0); | |
2101 | ClearPageChecked(page); | |
2102 | set_page_dirty(page); | |
2103 | out: | |
2104 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, | |
2105 | &cached_state, GFP_NOFS); | |
2106 | out_page: | |
2107 | unlock_page(page); | |
2108 | put_page(page); | |
2109 | kfree(fixup); | |
2110 | extent_changeset_free(data_reserved); | |
2111 | } | |
2112 | ||
2113 | /* | |
2114 | * There are a few paths in the higher layers of the kernel that directly | |
2115 | * set the page dirty bit without asking the filesystem if it is a | |
2116 | * good idea. This causes problems because we want to make sure COW | |
2117 | * properly happens and the data=ordered rules are followed. | |
2118 | * | |
2119 | * In our case any range that doesn't have the ORDERED bit set | |
2120 | * hasn't been properly setup for IO. We kick off an async process | |
2121 | * to fix it up. The async helper will wait for ordered extents, set | |
2122 | * the delalloc bit and make it safe to write the page. | |
2123 | */ | |
2124 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) | |
2125 | { | |
2126 | struct inode *inode = page->mapping->host; | |
2127 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2128 | struct btrfs_writepage_fixup *fixup; | |
2129 | ||
2130 | /* this page is properly in the ordered list */ | |
2131 | if (TestClearPagePrivate2(page)) | |
2132 | return 0; | |
2133 | ||
2134 | if (PageChecked(page)) | |
2135 | return -EAGAIN; | |
2136 | ||
2137 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2138 | if (!fixup) | |
2139 | return -EAGAIN; | |
2140 | ||
2141 | SetPageChecked(page); | |
2142 | get_page(page); | |
2143 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, | |
2144 | btrfs_writepage_fixup_worker, NULL, NULL); | |
2145 | fixup->page = page; | |
2146 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); | |
2147 | return -EBUSY; | |
2148 | } | |
2149 | ||
2150 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, | |
2151 | struct inode *inode, u64 file_pos, | |
2152 | u64 disk_bytenr, u64 disk_num_bytes, | |
2153 | u64 num_bytes, u64 ram_bytes, | |
2154 | u8 compression, u8 encryption, | |
2155 | u16 other_encoding, int extent_type) | |
2156 | { | |
2157 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2158 | struct btrfs_file_extent_item *fi; | |
2159 | struct btrfs_path *path; | |
2160 | struct extent_buffer *leaf; | |
2161 | struct btrfs_key ins; | |
2162 | u64 qg_released; | |
2163 | int extent_inserted = 0; | |
2164 | int ret; | |
2165 | ||
2166 | path = btrfs_alloc_path(); | |
2167 | if (!path) | |
2168 | return -ENOMEM; | |
2169 | ||
2170 | /* | |
2171 | * we may be replacing one extent in the tree with another. | |
2172 | * The new extent is pinned in the extent map, and we don't want | |
2173 | * to drop it from the cache until it is completely in the btree. | |
2174 | * | |
2175 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2176 | * the caller is expected to unpin it and allow it to be merged | |
2177 | * with the others. | |
2178 | */ | |
2179 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, | |
2180 | file_pos + num_bytes, NULL, 0, | |
2181 | 1, sizeof(*fi), &extent_inserted); | |
2182 | if (ret) | |
2183 | goto out; | |
2184 | ||
2185 | if (!extent_inserted) { | |
2186 | ins.objectid = btrfs_ino(BTRFS_I(inode)); | |
2187 | ins.offset = file_pos; | |
2188 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2189 | ||
2190 | path->leave_spinning = 1; | |
2191 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2192 | sizeof(*fi)); | |
2193 | if (ret) | |
2194 | goto out; | |
2195 | } | |
2196 | leaf = path->nodes[0]; | |
2197 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2198 | struct btrfs_file_extent_item); | |
2199 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2200 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2201 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2202 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2203 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2204 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2205 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2206 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2207 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2208 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
2209 | ||
2210 | btrfs_mark_buffer_dirty(leaf); | |
2211 | btrfs_release_path(path); | |
2212 | ||
2213 | inode_add_bytes(inode, num_bytes); | |
2214 | ||
2215 | ins.objectid = disk_bytenr; | |
2216 | ins.offset = disk_num_bytes; | |
2217 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
2218 | ||
2219 | /* | |
2220 | * Release the reserved range from inode dirty range map, as it is | |
2221 | * already moved into delayed_ref_head | |
2222 | */ | |
2223 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); | |
2224 | if (ret < 0) | |
2225 | goto out; | |
2226 | qg_released = ret; | |
2227 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, | |
2228 | btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins); | |
2229 | out: | |
2230 | btrfs_free_path(path); | |
2231 | ||
2232 | return ret; | |
2233 | } | |
2234 | ||
2235 | /* snapshot-aware defrag */ | |
2236 | struct sa_defrag_extent_backref { | |
2237 | struct rb_node node; | |
2238 | struct old_sa_defrag_extent *old; | |
2239 | u64 root_id; | |
2240 | u64 inum; | |
2241 | u64 file_pos; | |
2242 | u64 extent_offset; | |
2243 | u64 num_bytes; | |
2244 | u64 generation; | |
2245 | }; | |
2246 | ||
2247 | struct old_sa_defrag_extent { | |
2248 | struct list_head list; | |
2249 | struct new_sa_defrag_extent *new; | |
2250 | ||
2251 | u64 extent_offset; | |
2252 | u64 bytenr; | |
2253 | u64 offset; | |
2254 | u64 len; | |
2255 | int count; | |
2256 | }; | |
2257 | ||
2258 | struct new_sa_defrag_extent { | |
2259 | struct rb_root root; | |
2260 | struct list_head head; | |
2261 | struct btrfs_path *path; | |
2262 | struct inode *inode; | |
2263 | u64 file_pos; | |
2264 | u64 len; | |
2265 | u64 bytenr; | |
2266 | u64 disk_len; | |
2267 | u8 compress_type; | |
2268 | }; | |
2269 | ||
2270 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2271 | struct sa_defrag_extent_backref *b2) | |
2272 | { | |
2273 | if (b1->root_id < b2->root_id) | |
2274 | return -1; | |
2275 | else if (b1->root_id > b2->root_id) | |
2276 | return 1; | |
2277 | ||
2278 | if (b1->inum < b2->inum) | |
2279 | return -1; | |
2280 | else if (b1->inum > b2->inum) | |
2281 | return 1; | |
2282 | ||
2283 | if (b1->file_pos < b2->file_pos) | |
2284 | return -1; | |
2285 | else if (b1->file_pos > b2->file_pos) | |
2286 | return 1; | |
2287 | ||
2288 | /* | |
2289 | * [------------------------------] ===> (a range of space) | |
2290 | * |<--->| |<---->| =============> (fs/file tree A) | |
2291 | * |<---------------------------->| ===> (fs/file tree B) | |
2292 | * | |
2293 | * A range of space can refer to two file extents in one tree while | |
2294 | * refer to only one file extent in another tree. | |
2295 | * | |
2296 | * So we may process a disk offset more than one time(two extents in A) | |
2297 | * and locate at the same extent(one extent in B), then insert two same | |
2298 | * backrefs(both refer to the extent in B). | |
2299 | */ | |
2300 | return 0; | |
2301 | } | |
2302 | ||
2303 | static void backref_insert(struct rb_root *root, | |
2304 | struct sa_defrag_extent_backref *backref) | |
2305 | { | |
2306 | struct rb_node **p = &root->rb_node; | |
2307 | struct rb_node *parent = NULL; | |
2308 | struct sa_defrag_extent_backref *entry; | |
2309 | int ret; | |
2310 | ||
2311 | while (*p) { | |
2312 | parent = *p; | |
2313 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2314 | ||
2315 | ret = backref_comp(backref, entry); | |
2316 | if (ret < 0) | |
2317 | p = &(*p)->rb_left; | |
2318 | else | |
2319 | p = &(*p)->rb_right; | |
2320 | } | |
2321 | ||
2322 | rb_link_node(&backref->node, parent, p); | |
2323 | rb_insert_color(&backref->node, root); | |
2324 | } | |
2325 | ||
2326 | /* | |
2327 | * Note the backref might has changed, and in this case we just return 0. | |
2328 | */ | |
2329 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2330 | void *ctx) | |
2331 | { | |
2332 | struct btrfs_file_extent_item *extent; | |
2333 | struct old_sa_defrag_extent *old = ctx; | |
2334 | struct new_sa_defrag_extent *new = old->new; | |
2335 | struct btrfs_path *path = new->path; | |
2336 | struct btrfs_key key; | |
2337 | struct btrfs_root *root; | |
2338 | struct sa_defrag_extent_backref *backref; | |
2339 | struct extent_buffer *leaf; | |
2340 | struct inode *inode = new->inode; | |
2341 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2342 | int slot; | |
2343 | int ret; | |
2344 | u64 extent_offset; | |
2345 | u64 num_bytes; | |
2346 | ||
2347 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2348 | inum == btrfs_ino(BTRFS_I(inode))) | |
2349 | return 0; | |
2350 | ||
2351 | key.objectid = root_id; | |
2352 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2353 | key.offset = (u64)-1; | |
2354 | ||
2355 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2356 | if (IS_ERR(root)) { | |
2357 | if (PTR_ERR(root) == -ENOENT) | |
2358 | return 0; | |
2359 | WARN_ON(1); | |
2360 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", | |
2361 | inum, offset, root_id); | |
2362 | return PTR_ERR(root); | |
2363 | } | |
2364 | ||
2365 | key.objectid = inum; | |
2366 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2367 | if (offset > (u64)-1 << 32) | |
2368 | key.offset = 0; | |
2369 | else | |
2370 | key.offset = offset; | |
2371 | ||
2372 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2373 | if (WARN_ON(ret < 0)) | |
2374 | return ret; | |
2375 | ret = 0; | |
2376 | ||
2377 | while (1) { | |
2378 | cond_resched(); | |
2379 | ||
2380 | leaf = path->nodes[0]; | |
2381 | slot = path->slots[0]; | |
2382 | ||
2383 | if (slot >= btrfs_header_nritems(leaf)) { | |
2384 | ret = btrfs_next_leaf(root, path); | |
2385 | if (ret < 0) { | |
2386 | goto out; | |
2387 | } else if (ret > 0) { | |
2388 | ret = 0; | |
2389 | goto out; | |
2390 | } | |
2391 | continue; | |
2392 | } | |
2393 | ||
2394 | path->slots[0]++; | |
2395 | ||
2396 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2397 | ||
2398 | if (key.objectid > inum) | |
2399 | goto out; | |
2400 | ||
2401 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2402 | continue; | |
2403 | ||
2404 | extent = btrfs_item_ptr(leaf, slot, | |
2405 | struct btrfs_file_extent_item); | |
2406 | ||
2407 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2408 | continue; | |
2409 | ||
2410 | /* | |
2411 | * 'offset' refers to the exact key.offset, | |
2412 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2413 | * (key.offset - extent_offset). | |
2414 | */ | |
2415 | if (key.offset != offset) | |
2416 | continue; | |
2417 | ||
2418 | extent_offset = btrfs_file_extent_offset(leaf, extent); | |
2419 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); | |
2420 | ||
2421 | if (extent_offset >= old->extent_offset + old->offset + | |
2422 | old->len || extent_offset + num_bytes <= | |
2423 | old->extent_offset + old->offset) | |
2424 | continue; | |
2425 | break; | |
2426 | } | |
2427 | ||
2428 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2429 | if (!backref) { | |
2430 | ret = -ENOENT; | |
2431 | goto out; | |
2432 | } | |
2433 | ||
2434 | backref->root_id = root_id; | |
2435 | backref->inum = inum; | |
2436 | backref->file_pos = offset; | |
2437 | backref->num_bytes = num_bytes; | |
2438 | backref->extent_offset = extent_offset; | |
2439 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2440 | backref->old = old; | |
2441 | backref_insert(&new->root, backref); | |
2442 | old->count++; | |
2443 | out: | |
2444 | btrfs_release_path(path); | |
2445 | WARN_ON(ret); | |
2446 | return ret; | |
2447 | } | |
2448 | ||
2449 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2450 | struct new_sa_defrag_extent *new) | |
2451 | { | |
2452 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); | |
2453 | struct old_sa_defrag_extent *old, *tmp; | |
2454 | int ret; | |
2455 | ||
2456 | new->path = path; | |
2457 | ||
2458 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2459 | ret = iterate_inodes_from_logical(old->bytenr + | |
2460 | old->extent_offset, fs_info, | |
2461 | path, record_one_backref, | |
2462 | old); | |
2463 | if (ret < 0 && ret != -ENOENT) | |
2464 | return false; | |
2465 | ||
2466 | /* no backref to be processed for this extent */ | |
2467 | if (!old->count) { | |
2468 | list_del(&old->list); | |
2469 | kfree(old); | |
2470 | } | |
2471 | } | |
2472 | ||
2473 | if (list_empty(&new->head)) | |
2474 | return false; | |
2475 | ||
2476 | return true; | |
2477 | } | |
2478 | ||
2479 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2480 | struct btrfs_file_extent_item *fi, | |
2481 | struct new_sa_defrag_extent *new) | |
2482 | { | |
2483 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) | |
2484 | return 0; | |
2485 | ||
2486 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2487 | return 0; | |
2488 | ||
2489 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) | |
2490 | return 0; | |
2491 | ||
2492 | if (btrfs_file_extent_encryption(leaf, fi) || | |
2493 | btrfs_file_extent_other_encoding(leaf, fi)) | |
2494 | return 0; | |
2495 | ||
2496 | return 1; | |
2497 | } | |
2498 | ||
2499 | /* | |
2500 | * Note the backref might has changed, and in this case we just return 0. | |
2501 | */ | |
2502 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2503 | struct sa_defrag_extent_backref *prev, | |
2504 | struct sa_defrag_extent_backref *backref) | |
2505 | { | |
2506 | struct btrfs_file_extent_item *extent; | |
2507 | struct btrfs_file_extent_item *item; | |
2508 | struct btrfs_ordered_extent *ordered; | |
2509 | struct btrfs_trans_handle *trans; | |
2510 | struct btrfs_root *root; | |
2511 | struct btrfs_key key; | |
2512 | struct extent_buffer *leaf; | |
2513 | struct old_sa_defrag_extent *old = backref->old; | |
2514 | struct new_sa_defrag_extent *new = old->new; | |
2515 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); | |
2516 | struct inode *inode; | |
2517 | struct extent_state *cached = NULL; | |
2518 | int ret = 0; | |
2519 | u64 start; | |
2520 | u64 len; | |
2521 | u64 lock_start; | |
2522 | u64 lock_end; | |
2523 | bool merge = false; | |
2524 | int index; | |
2525 | ||
2526 | if (prev && prev->root_id == backref->root_id && | |
2527 | prev->inum == backref->inum && | |
2528 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2529 | merge = true; | |
2530 | ||
2531 | /* step 1: get root */ | |
2532 | key.objectid = backref->root_id; | |
2533 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2534 | key.offset = (u64)-1; | |
2535 | ||
2536 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2537 | ||
2538 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2539 | if (IS_ERR(root)) { | |
2540 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2541 | if (PTR_ERR(root) == -ENOENT) | |
2542 | return 0; | |
2543 | return PTR_ERR(root); | |
2544 | } | |
2545 | ||
2546 | if (btrfs_root_readonly(root)) { | |
2547 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2548 | return 0; | |
2549 | } | |
2550 | ||
2551 | /* step 2: get inode */ | |
2552 | key.objectid = backref->inum; | |
2553 | key.type = BTRFS_INODE_ITEM_KEY; | |
2554 | key.offset = 0; | |
2555 | ||
2556 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2557 | if (IS_ERR(inode)) { | |
2558 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2559 | return 0; | |
2560 | } | |
2561 | ||
2562 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2563 | ||
2564 | /* step 3: relink backref */ | |
2565 | lock_start = backref->file_pos; | |
2566 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2567 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2568 | &cached); | |
2569 | ||
2570 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2571 | if (ordered) { | |
2572 | btrfs_put_ordered_extent(ordered); | |
2573 | goto out_unlock; | |
2574 | } | |
2575 | ||
2576 | trans = btrfs_join_transaction(root); | |
2577 | if (IS_ERR(trans)) { | |
2578 | ret = PTR_ERR(trans); | |
2579 | goto out_unlock; | |
2580 | } | |
2581 | ||
2582 | key.objectid = backref->inum; | |
2583 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2584 | key.offset = backref->file_pos; | |
2585 | ||
2586 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2587 | if (ret < 0) { | |
2588 | goto out_free_path; | |
2589 | } else if (ret > 0) { | |
2590 | ret = 0; | |
2591 | goto out_free_path; | |
2592 | } | |
2593 | ||
2594 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2595 | struct btrfs_file_extent_item); | |
2596 | ||
2597 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2598 | backref->generation) | |
2599 | goto out_free_path; | |
2600 | ||
2601 | btrfs_release_path(path); | |
2602 | ||
2603 | start = backref->file_pos; | |
2604 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2605 | start += old->extent_offset + old->offset - | |
2606 | backref->extent_offset; | |
2607 | ||
2608 | len = min(backref->extent_offset + backref->num_bytes, | |
2609 | old->extent_offset + old->offset + old->len); | |
2610 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2611 | ||
2612 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2613 | start + len, 1); | |
2614 | if (ret) | |
2615 | goto out_free_path; | |
2616 | again: | |
2617 | key.objectid = btrfs_ino(BTRFS_I(inode)); | |
2618 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2619 | key.offset = start; | |
2620 | ||
2621 | path->leave_spinning = 1; | |
2622 | if (merge) { | |
2623 | struct btrfs_file_extent_item *fi; | |
2624 | u64 extent_len; | |
2625 | struct btrfs_key found_key; | |
2626 | ||
2627 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2628 | if (ret < 0) | |
2629 | goto out_free_path; | |
2630 | ||
2631 | path->slots[0]--; | |
2632 | leaf = path->nodes[0]; | |
2633 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2634 | ||
2635 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2636 | struct btrfs_file_extent_item); | |
2637 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2638 | ||
2639 | if (extent_len + found_key.offset == start && | |
2640 | relink_is_mergable(leaf, fi, new)) { | |
2641 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
2642 | extent_len + len); | |
2643 | btrfs_mark_buffer_dirty(leaf); | |
2644 | inode_add_bytes(inode, len); | |
2645 | ||
2646 | ret = 1; | |
2647 | goto out_free_path; | |
2648 | } else { | |
2649 | merge = false; | |
2650 | btrfs_release_path(path); | |
2651 | goto again; | |
2652 | } | |
2653 | } | |
2654 | ||
2655 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2656 | sizeof(*extent)); | |
2657 | if (ret) { | |
2658 | btrfs_abort_transaction(trans, ret); | |
2659 | goto out_free_path; | |
2660 | } | |
2661 | ||
2662 | leaf = path->nodes[0]; | |
2663 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2664 | struct btrfs_file_extent_item); | |
2665 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2666 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2667 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2668 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2669 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2670 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2671 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2672 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2673 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2674 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2675 | ||
2676 | btrfs_mark_buffer_dirty(leaf); | |
2677 | inode_add_bytes(inode, len); | |
2678 | btrfs_release_path(path); | |
2679 | ||
2680 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, | |
2681 | new->disk_len, 0, | |
2682 | backref->root_id, backref->inum, | |
2683 | new->file_pos); /* start - extent_offset */ | |
2684 | if (ret) { | |
2685 | btrfs_abort_transaction(trans, ret); | |
2686 | goto out_free_path; | |
2687 | } | |
2688 | ||
2689 | ret = 1; | |
2690 | out_free_path: | |
2691 | btrfs_release_path(path); | |
2692 | path->leave_spinning = 0; | |
2693 | btrfs_end_transaction(trans); | |
2694 | out_unlock: | |
2695 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2696 | &cached, GFP_NOFS); | |
2697 | iput(inode); | |
2698 | return ret; | |
2699 | } | |
2700 | ||
2701 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) | |
2702 | { | |
2703 | struct old_sa_defrag_extent *old, *tmp; | |
2704 | ||
2705 | if (!new) | |
2706 | return; | |
2707 | ||
2708 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2709 | kfree(old); | |
2710 | } | |
2711 | kfree(new); | |
2712 | } | |
2713 | ||
2714 | static void relink_file_extents(struct new_sa_defrag_extent *new) | |
2715 | { | |
2716 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); | |
2717 | struct btrfs_path *path; | |
2718 | struct sa_defrag_extent_backref *backref; | |
2719 | struct sa_defrag_extent_backref *prev = NULL; | |
2720 | struct inode *inode; | |
2721 | struct btrfs_root *root; | |
2722 | struct rb_node *node; | |
2723 | int ret; | |
2724 | ||
2725 | inode = new->inode; | |
2726 | root = BTRFS_I(inode)->root; | |
2727 | ||
2728 | path = btrfs_alloc_path(); | |
2729 | if (!path) | |
2730 | return; | |
2731 | ||
2732 | if (!record_extent_backrefs(path, new)) { | |
2733 | btrfs_free_path(path); | |
2734 | goto out; | |
2735 | } | |
2736 | btrfs_release_path(path); | |
2737 | ||
2738 | while (1) { | |
2739 | node = rb_first(&new->root); | |
2740 | if (!node) | |
2741 | break; | |
2742 | rb_erase(node, &new->root); | |
2743 | ||
2744 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2745 | ||
2746 | ret = relink_extent_backref(path, prev, backref); | |
2747 | WARN_ON(ret < 0); | |
2748 | ||
2749 | kfree(prev); | |
2750 | ||
2751 | if (ret == 1) | |
2752 | prev = backref; | |
2753 | else | |
2754 | prev = NULL; | |
2755 | cond_resched(); | |
2756 | } | |
2757 | kfree(prev); | |
2758 | ||
2759 | btrfs_free_path(path); | |
2760 | out: | |
2761 | free_sa_defrag_extent(new); | |
2762 | ||
2763 | atomic_dec(&fs_info->defrag_running); | |
2764 | wake_up(&fs_info->transaction_wait); | |
2765 | } | |
2766 | ||
2767 | static struct new_sa_defrag_extent * | |
2768 | record_old_file_extents(struct inode *inode, | |
2769 | struct btrfs_ordered_extent *ordered) | |
2770 | { | |
2771 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2772 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2773 | struct btrfs_path *path; | |
2774 | struct btrfs_key key; | |
2775 | struct old_sa_defrag_extent *old; | |
2776 | struct new_sa_defrag_extent *new; | |
2777 | int ret; | |
2778 | ||
2779 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2780 | if (!new) | |
2781 | return NULL; | |
2782 | ||
2783 | new->inode = inode; | |
2784 | new->file_pos = ordered->file_offset; | |
2785 | new->len = ordered->len; | |
2786 | new->bytenr = ordered->start; | |
2787 | new->disk_len = ordered->disk_len; | |
2788 | new->compress_type = ordered->compress_type; | |
2789 | new->root = RB_ROOT; | |
2790 | INIT_LIST_HEAD(&new->head); | |
2791 | ||
2792 | path = btrfs_alloc_path(); | |
2793 | if (!path) | |
2794 | goto out_kfree; | |
2795 | ||
2796 | key.objectid = btrfs_ino(BTRFS_I(inode)); | |
2797 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2798 | key.offset = new->file_pos; | |
2799 | ||
2800 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2801 | if (ret < 0) | |
2802 | goto out_free_path; | |
2803 | if (ret > 0 && path->slots[0] > 0) | |
2804 | path->slots[0]--; | |
2805 | ||
2806 | /* find out all the old extents for the file range */ | |
2807 | while (1) { | |
2808 | struct btrfs_file_extent_item *extent; | |
2809 | struct extent_buffer *l; | |
2810 | int slot; | |
2811 | u64 num_bytes; | |
2812 | u64 offset; | |
2813 | u64 end; | |
2814 | u64 disk_bytenr; | |
2815 | u64 extent_offset; | |
2816 | ||
2817 | l = path->nodes[0]; | |
2818 | slot = path->slots[0]; | |
2819 | ||
2820 | if (slot >= btrfs_header_nritems(l)) { | |
2821 | ret = btrfs_next_leaf(root, path); | |
2822 | if (ret < 0) | |
2823 | goto out_free_path; | |
2824 | else if (ret > 0) | |
2825 | break; | |
2826 | continue; | |
2827 | } | |
2828 | ||
2829 | btrfs_item_key_to_cpu(l, &key, slot); | |
2830 | ||
2831 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) | |
2832 | break; | |
2833 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2834 | break; | |
2835 | if (key.offset >= new->file_pos + new->len) | |
2836 | break; | |
2837 | ||
2838 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2839 | ||
2840 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2841 | if (key.offset + num_bytes < new->file_pos) | |
2842 | goto next; | |
2843 | ||
2844 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2845 | if (!disk_bytenr) | |
2846 | goto next; | |
2847 | ||
2848 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2849 | ||
2850 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2851 | if (!old) | |
2852 | goto out_free_path; | |
2853 | ||
2854 | offset = max(new->file_pos, key.offset); | |
2855 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2856 | ||
2857 | old->bytenr = disk_bytenr; | |
2858 | old->extent_offset = extent_offset; | |
2859 | old->offset = offset - key.offset; | |
2860 | old->len = end - offset; | |
2861 | old->new = new; | |
2862 | old->count = 0; | |
2863 | list_add_tail(&old->list, &new->head); | |
2864 | next: | |
2865 | path->slots[0]++; | |
2866 | cond_resched(); | |
2867 | } | |
2868 | ||
2869 | btrfs_free_path(path); | |
2870 | atomic_inc(&fs_info->defrag_running); | |
2871 | ||
2872 | return new; | |
2873 | ||
2874 | out_free_path: | |
2875 | btrfs_free_path(path); | |
2876 | out_kfree: | |
2877 | free_sa_defrag_extent(new); | |
2878 | return NULL; | |
2879 | } | |
2880 | ||
2881 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, | |
2882 | u64 start, u64 len) | |
2883 | { | |
2884 | struct btrfs_block_group_cache *cache; | |
2885 | ||
2886 | cache = btrfs_lookup_block_group(fs_info, start); | |
2887 | ASSERT(cache); | |
2888 | ||
2889 | spin_lock(&cache->lock); | |
2890 | cache->delalloc_bytes -= len; | |
2891 | spin_unlock(&cache->lock); | |
2892 | ||
2893 | btrfs_put_block_group(cache); | |
2894 | } | |
2895 | ||
2896 | /* as ordered data IO finishes, this gets called so we can finish | |
2897 | * an ordered extent if the range of bytes in the file it covers are | |
2898 | * fully written. | |
2899 | */ | |
2900 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) | |
2901 | { | |
2902 | struct inode *inode = ordered_extent->inode; | |
2903 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2904 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2905 | struct btrfs_trans_handle *trans = NULL; | |
2906 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
2907 | struct extent_state *cached_state = NULL; | |
2908 | struct new_sa_defrag_extent *new = NULL; | |
2909 | int compress_type = 0; | |
2910 | int ret = 0; | |
2911 | u64 logical_len = ordered_extent->len; | |
2912 | bool nolock; | |
2913 | bool truncated = false; | |
2914 | bool range_locked = false; | |
2915 | bool clear_new_delalloc_bytes = false; | |
2916 | ||
2917 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2918 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2919 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2920 | clear_new_delalloc_bytes = true; | |
2921 | ||
2922 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); | |
2923 | ||
2924 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { | |
2925 | ret = -EIO; | |
2926 | goto out; | |
2927 | } | |
2928 | ||
2929 | btrfs_free_io_failure_record(BTRFS_I(inode), | |
2930 | ordered_extent->file_offset, | |
2931 | ordered_extent->file_offset + | |
2932 | ordered_extent->len - 1); | |
2933 | ||
2934 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { | |
2935 | truncated = true; | |
2936 | logical_len = ordered_extent->truncated_len; | |
2937 | /* Truncated the entire extent, don't bother adding */ | |
2938 | if (!logical_len) | |
2939 | goto out; | |
2940 | } | |
2941 | ||
2942 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { | |
2943 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ | |
2944 | ||
2945 | /* | |
2946 | * For mwrite(mmap + memset to write) case, we still reserve | |
2947 | * space for NOCOW range. | |
2948 | * As NOCOW won't cause a new delayed ref, just free the space | |
2949 | */ | |
2950 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, | |
2951 | ordered_extent->len); | |
2952 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); | |
2953 | if (nolock) | |
2954 | trans = btrfs_join_transaction_nolock(root); | |
2955 | else | |
2956 | trans = btrfs_join_transaction(root); | |
2957 | if (IS_ERR(trans)) { | |
2958 | ret = PTR_ERR(trans); | |
2959 | trans = NULL; | |
2960 | goto out; | |
2961 | } | |
2962 | trans->block_rsv = &fs_info->delalloc_block_rsv; | |
2963 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2964 | if (ret) /* -ENOMEM or corruption */ | |
2965 | btrfs_abort_transaction(trans, ret); | |
2966 | goto out; | |
2967 | } | |
2968 | ||
2969 | range_locked = true; | |
2970 | lock_extent_bits(io_tree, ordered_extent->file_offset, | |
2971 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2972 | &cached_state); | |
2973 | ||
2974 | ret = test_range_bit(io_tree, ordered_extent->file_offset, | |
2975 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2976 | EXTENT_DEFRAG, 0, cached_state); | |
2977 | if (ret) { | |
2978 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
2979 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) | |
2980 | /* the inode is shared */ | |
2981 | new = record_old_file_extents(inode, ordered_extent); | |
2982 | ||
2983 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2984 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2985 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2986 | } | |
2987 | ||
2988 | if (nolock) | |
2989 | trans = btrfs_join_transaction_nolock(root); | |
2990 | else | |
2991 | trans = btrfs_join_transaction(root); | |
2992 | if (IS_ERR(trans)) { | |
2993 | ret = PTR_ERR(trans); | |
2994 | trans = NULL; | |
2995 | goto out; | |
2996 | } | |
2997 | ||
2998 | trans->block_rsv = &fs_info->delalloc_block_rsv; | |
2999 | ||
3000 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) | |
3001 | compress_type = ordered_extent->compress_type; | |
3002 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { | |
3003 | BUG_ON(compress_type); | |
3004 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), | |
3005 | ordered_extent->file_offset, | |
3006 | ordered_extent->file_offset + | |
3007 | logical_len); | |
3008 | } else { | |
3009 | BUG_ON(root == fs_info->tree_root); | |
3010 | ret = insert_reserved_file_extent(trans, inode, | |
3011 | ordered_extent->file_offset, | |
3012 | ordered_extent->start, | |
3013 | ordered_extent->disk_len, | |
3014 | logical_len, logical_len, | |
3015 | compress_type, 0, 0, | |
3016 | BTRFS_FILE_EXTENT_REG); | |
3017 | if (!ret) | |
3018 | btrfs_release_delalloc_bytes(fs_info, | |
3019 | ordered_extent->start, | |
3020 | ordered_extent->disk_len); | |
3021 | } | |
3022 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, | |
3023 | ordered_extent->file_offset, ordered_extent->len, | |
3024 | trans->transid); | |
3025 | if (ret < 0) { | |
3026 | btrfs_abort_transaction(trans, ret); | |
3027 | goto out; | |
3028 | } | |
3029 | ||
3030 | add_pending_csums(trans, inode, &ordered_extent->list); | |
3031 | ||
3032 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); | |
3033 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3034 | if (ret) { /* -ENOMEM or corruption */ | |
3035 | btrfs_abort_transaction(trans, ret); | |
3036 | goto out; | |
3037 | } | |
3038 | ret = 0; | |
3039 | out: | |
3040 | if (range_locked || clear_new_delalloc_bytes) { | |
3041 | unsigned int clear_bits = 0; | |
3042 | ||
3043 | if (range_locked) | |
3044 | clear_bits |= EXTENT_LOCKED; | |
3045 | if (clear_new_delalloc_bytes) | |
3046 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3047 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3048 | ordered_extent->file_offset, | |
3049 | ordered_extent->file_offset + | |
3050 | ordered_extent->len - 1, | |
3051 | clear_bits, | |
3052 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
3053 | 0, &cached_state, GFP_NOFS); | |
3054 | } | |
3055 | ||
3056 | if (root != fs_info->tree_root) | |
3057 | btrfs_delalloc_release_metadata(BTRFS_I(inode), | |
3058 | ordered_extent->len); | |
3059 | if (trans) | |
3060 | btrfs_end_transaction(trans); | |
3061 | ||
3062 | if (ret || truncated) { | |
3063 | u64 start, end; | |
3064 | ||
3065 | if (truncated) | |
3066 | start = ordered_extent->file_offset + logical_len; | |
3067 | else | |
3068 | start = ordered_extent->file_offset; | |
3069 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3070 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3071 | ||
3072 | /* Drop the cache for the part of the extent we didn't write. */ | |
3073 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); | |
3074 | ||
3075 | /* | |
3076 | * If the ordered extent had an IOERR or something else went | |
3077 | * wrong we need to return the space for this ordered extent | |
3078 | * back to the allocator. We only free the extent in the | |
3079 | * truncated case if we didn't write out the extent at all. | |
3080 | */ | |
3081 | if ((ret || !logical_len) && | |
3082 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
3083 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) | |
3084 | btrfs_free_reserved_extent(fs_info, | |
3085 | ordered_extent->start, | |
3086 | ordered_extent->disk_len, 1); | |
3087 | } | |
3088 | ||
3089 | ||
3090 | /* | |
3091 | * This needs to be done to make sure anybody waiting knows we are done | |
3092 | * updating everything for this ordered extent. | |
3093 | */ | |
3094 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3095 | ||
3096 | /* for snapshot-aware defrag */ | |
3097 | if (new) { | |
3098 | if (ret) { | |
3099 | free_sa_defrag_extent(new); | |
3100 | atomic_dec(&fs_info->defrag_running); | |
3101 | } else { | |
3102 | relink_file_extents(new); | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | /* once for us */ | |
3107 | btrfs_put_ordered_extent(ordered_extent); | |
3108 | /* once for the tree */ | |
3109 | btrfs_put_ordered_extent(ordered_extent); | |
3110 | ||
3111 | return ret; | |
3112 | } | |
3113 | ||
3114 | static void finish_ordered_fn(struct btrfs_work *work) | |
3115 | { | |
3116 | struct btrfs_ordered_extent *ordered_extent; | |
3117 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3118 | btrfs_finish_ordered_io(ordered_extent); | |
3119 | } | |
3120 | ||
3121 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, | |
3122 | struct extent_state *state, int uptodate) | |
3123 | { | |
3124 | struct inode *inode = page->mapping->host; | |
3125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
3126 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
3127 | struct btrfs_workqueue *wq; | |
3128 | btrfs_work_func_t func; | |
3129 | ||
3130 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); | |
3131 | ||
3132 | ClearPagePrivate2(page); | |
3133 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, | |
3134 | end - start + 1, uptodate)) | |
3135 | return; | |
3136 | ||
3137 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { | |
3138 | wq = fs_info->endio_freespace_worker; | |
3139 | func = btrfs_freespace_write_helper; | |
3140 | } else { | |
3141 | wq = fs_info->endio_write_workers; | |
3142 | func = btrfs_endio_write_helper; | |
3143 | } | |
3144 | ||
3145 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, | |
3146 | NULL); | |
3147 | btrfs_queue_work(wq, &ordered_extent->work); | |
3148 | } | |
3149 | ||
3150 | static int __readpage_endio_check(struct inode *inode, | |
3151 | struct btrfs_io_bio *io_bio, | |
3152 | int icsum, struct page *page, | |
3153 | int pgoff, u64 start, size_t len) | |
3154 | { | |
3155 | char *kaddr; | |
3156 | u32 csum_expected; | |
3157 | u32 csum = ~(u32)0; | |
3158 | ||
3159 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3160 | ||
3161 | kaddr = kmap_atomic(page); | |
3162 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3163 | btrfs_csum_final(csum, (u8 *)&csum); | |
3164 | if (csum != csum_expected) | |
3165 | goto zeroit; | |
3166 | ||
3167 | kunmap_atomic(kaddr); | |
3168 | return 0; | |
3169 | zeroit: | |
3170 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, | |
3171 | io_bio->mirror_num); | |
3172 | memset(kaddr + pgoff, 1, len); | |
3173 | flush_dcache_page(page); | |
3174 | kunmap_atomic(kaddr); | |
3175 | if (csum_expected == 0) | |
3176 | return 0; | |
3177 | return -EIO; | |
3178 | } | |
3179 | ||
3180 | /* | |
3181 | * when reads are done, we need to check csums to verify the data is correct | |
3182 | * if there's a match, we allow the bio to finish. If not, the code in | |
3183 | * extent_io.c will try to find good copies for us. | |
3184 | */ | |
3185 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, | |
3186 | u64 phy_offset, struct page *page, | |
3187 | u64 start, u64 end, int mirror) | |
3188 | { | |
3189 | size_t offset = start - page_offset(page); | |
3190 | struct inode *inode = page->mapping->host; | |
3191 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
3192 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3193 | ||
3194 | if (PageChecked(page)) { | |
3195 | ClearPageChecked(page); | |
3196 | return 0; | |
3197 | } | |
3198 | ||
3199 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
3200 | return 0; | |
3201 | ||
3202 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
3203 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { | |
3204 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); | |
3205 | return 0; | |
3206 | } | |
3207 | ||
3208 | phy_offset >>= inode->i_sb->s_blocksize_bits; | |
3209 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, | |
3210 | start, (size_t)(end - start + 1)); | |
3211 | } | |
3212 | ||
3213 | void btrfs_add_delayed_iput(struct inode *inode) | |
3214 | { | |
3215 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
3216 | struct btrfs_inode *binode = BTRFS_I(inode); | |
3217 | ||
3218 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3219 | return; | |
3220 | ||
3221 | spin_lock(&fs_info->delayed_iput_lock); | |
3222 | if (binode->delayed_iput_count == 0) { | |
3223 | ASSERT(list_empty(&binode->delayed_iput)); | |
3224 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3225 | } else { | |
3226 | binode->delayed_iput_count++; | |
3227 | } | |
3228 | spin_unlock(&fs_info->delayed_iput_lock); | |
3229 | } | |
3230 | ||
3231 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3232 | { | |
3233 | ||
3234 | spin_lock(&fs_info->delayed_iput_lock); | |
3235 | while (!list_empty(&fs_info->delayed_iputs)) { | |
3236 | struct btrfs_inode *inode; | |
3237 | ||
3238 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3239 | struct btrfs_inode, delayed_iput); | |
3240 | if (inode->delayed_iput_count) { | |
3241 | inode->delayed_iput_count--; | |
3242 | list_move_tail(&inode->delayed_iput, | |
3243 | &fs_info->delayed_iputs); | |
3244 | } else { | |
3245 | list_del_init(&inode->delayed_iput); | |
3246 | } | |
3247 | spin_unlock(&fs_info->delayed_iput_lock); | |
3248 | iput(&inode->vfs_inode); | |
3249 | spin_lock(&fs_info->delayed_iput_lock); | |
3250 | } | |
3251 | spin_unlock(&fs_info->delayed_iput_lock); | |
3252 | } | |
3253 | ||
3254 | /* | |
3255 | * This is called in transaction commit time. If there are no orphan | |
3256 | * files in the subvolume, it removes orphan item and frees block_rsv | |
3257 | * structure. | |
3258 | */ | |
3259 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3260 | struct btrfs_root *root) | |
3261 | { | |
3262 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3263 | struct btrfs_block_rsv *block_rsv; | |
3264 | int ret; | |
3265 | ||
3266 | if (atomic_read(&root->orphan_inodes) || | |
3267 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) | |
3268 | return; | |
3269 | ||
3270 | spin_lock(&root->orphan_lock); | |
3271 | if (atomic_read(&root->orphan_inodes)) { | |
3272 | spin_unlock(&root->orphan_lock); | |
3273 | return; | |
3274 | } | |
3275 | ||
3276 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3277 | spin_unlock(&root->orphan_lock); | |
3278 | return; | |
3279 | } | |
3280 | ||
3281 | block_rsv = root->orphan_block_rsv; | |
3282 | root->orphan_block_rsv = NULL; | |
3283 | spin_unlock(&root->orphan_lock); | |
3284 | ||
3285 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && | |
3286 | btrfs_root_refs(&root->root_item) > 0) { | |
3287 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, | |
3288 | root->root_key.objectid); | |
3289 | if (ret) | |
3290 | btrfs_abort_transaction(trans, ret); | |
3291 | else | |
3292 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, | |
3293 | &root->state); | |
3294 | } | |
3295 | ||
3296 | if (block_rsv) { | |
3297 | WARN_ON(block_rsv->size > 0); | |
3298 | btrfs_free_block_rsv(fs_info, block_rsv); | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | /* | |
3303 | * This creates an orphan entry for the given inode in case something goes | |
3304 | * wrong in the middle of an unlink/truncate. | |
3305 | * | |
3306 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3307 | * this function. | |
3308 | */ | |
3309 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, | |
3310 | struct btrfs_inode *inode) | |
3311 | { | |
3312 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); | |
3313 | struct btrfs_root *root = inode->root; | |
3314 | struct btrfs_block_rsv *block_rsv = NULL; | |
3315 | int reserve = 0; | |
3316 | int insert = 0; | |
3317 | int ret; | |
3318 | ||
3319 | if (!root->orphan_block_rsv) { | |
3320 | block_rsv = btrfs_alloc_block_rsv(fs_info, | |
3321 | BTRFS_BLOCK_RSV_TEMP); | |
3322 | if (!block_rsv) | |
3323 | return -ENOMEM; | |
3324 | } | |
3325 | ||
3326 | spin_lock(&root->orphan_lock); | |
3327 | if (!root->orphan_block_rsv) { | |
3328 | root->orphan_block_rsv = block_rsv; | |
3329 | } else if (block_rsv) { | |
3330 | btrfs_free_block_rsv(fs_info, block_rsv); | |
3331 | block_rsv = NULL; | |
3332 | } | |
3333 | ||
3334 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3335 | &inode->runtime_flags)) { | |
3336 | #if 0 | |
3337 | /* | |
3338 | * For proper ENOSPC handling, we should do orphan | |
3339 | * cleanup when mounting. But this introduces backward | |
3340 | * compatibility issue. | |
3341 | */ | |
3342 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3343 | insert = 2; | |
3344 | else | |
3345 | insert = 1; | |
3346 | #endif | |
3347 | insert = 1; | |
3348 | atomic_inc(&root->orphan_inodes); | |
3349 | } | |
3350 | ||
3351 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3352 | &inode->runtime_flags)) | |
3353 | reserve = 1; | |
3354 | spin_unlock(&root->orphan_lock); | |
3355 | ||
3356 | /* grab metadata reservation from transaction handle */ | |
3357 | if (reserve) { | |
3358 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3359 | ASSERT(!ret); | |
3360 | if (ret) { | |
3361 | atomic_dec(&root->orphan_inodes); | |
3362 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3363 | &inode->runtime_flags); | |
3364 | if (insert) | |
3365 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3366 | &inode->runtime_flags); | |
3367 | return ret; | |
3368 | } | |
3369 | } | |
3370 | ||
3371 | /* insert an orphan item to track this unlinked/truncated file */ | |
3372 | if (insert >= 1) { | |
3373 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); | |
3374 | if (ret) { | |
3375 | atomic_dec(&root->orphan_inodes); | |
3376 | if (reserve) { | |
3377 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3378 | &inode->runtime_flags); | |
3379 | btrfs_orphan_release_metadata(inode); | |
3380 | } | |
3381 | if (ret != -EEXIST) { | |
3382 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3383 | &inode->runtime_flags); | |
3384 | btrfs_abort_transaction(trans, ret); | |
3385 | return ret; | |
3386 | } | |
3387 | } | |
3388 | ret = 0; | |
3389 | } | |
3390 | ||
3391 | /* insert an orphan item to track subvolume contains orphan files */ | |
3392 | if (insert >= 2) { | |
3393 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, | |
3394 | root->root_key.objectid); | |
3395 | if (ret && ret != -EEXIST) { | |
3396 | btrfs_abort_transaction(trans, ret); | |
3397 | return ret; | |
3398 | } | |
3399 | } | |
3400 | return 0; | |
3401 | } | |
3402 | ||
3403 | /* | |
3404 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3405 | * item for this particular inode. | |
3406 | */ | |
3407 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, | |
3408 | struct btrfs_inode *inode) | |
3409 | { | |
3410 | struct btrfs_root *root = inode->root; | |
3411 | int delete_item = 0; | |
3412 | int release_rsv = 0; | |
3413 | int ret = 0; | |
3414 | ||
3415 | spin_lock(&root->orphan_lock); | |
3416 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3417 | &inode->runtime_flags)) | |
3418 | delete_item = 1; | |
3419 | ||
3420 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3421 | &inode->runtime_flags)) | |
3422 | release_rsv = 1; | |
3423 | spin_unlock(&root->orphan_lock); | |
3424 | ||
3425 | if (delete_item) { | |
3426 | atomic_dec(&root->orphan_inodes); | |
3427 | if (trans) | |
3428 | ret = btrfs_del_orphan_item(trans, root, | |
3429 | btrfs_ino(inode)); | |
3430 | } | |
3431 | ||
3432 | if (release_rsv) | |
3433 | btrfs_orphan_release_metadata(inode); | |
3434 | ||
3435 | return ret; | |
3436 | } | |
3437 | ||
3438 | /* | |
3439 | * this cleans up any orphans that may be left on the list from the last use | |
3440 | * of this root. | |
3441 | */ | |
3442 | int btrfs_orphan_cleanup(struct btrfs_root *root) | |
3443 | { | |
3444 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3445 | struct btrfs_path *path; | |
3446 | struct extent_buffer *leaf; | |
3447 | struct btrfs_key key, found_key; | |
3448 | struct btrfs_trans_handle *trans; | |
3449 | struct inode *inode; | |
3450 | u64 last_objectid = 0; | |
3451 | int ret = 0, nr_unlink = 0, nr_truncate = 0; | |
3452 | ||
3453 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) | |
3454 | return 0; | |
3455 | ||
3456 | path = btrfs_alloc_path(); | |
3457 | if (!path) { | |
3458 | ret = -ENOMEM; | |
3459 | goto out; | |
3460 | } | |
3461 | path->reada = READA_BACK; | |
3462 | ||
3463 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
3464 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
3465 | key.offset = (u64)-1; | |
3466 | ||
3467 | while (1) { | |
3468 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3469 | if (ret < 0) | |
3470 | goto out; | |
3471 | ||
3472 | /* | |
3473 | * if ret == 0 means we found what we were searching for, which | |
3474 | * is weird, but possible, so only screw with path if we didn't | |
3475 | * find the key and see if we have stuff that matches | |
3476 | */ | |
3477 | if (ret > 0) { | |
3478 | ret = 0; | |
3479 | if (path->slots[0] == 0) | |
3480 | break; | |
3481 | path->slots[0]--; | |
3482 | } | |
3483 | ||
3484 | /* pull out the item */ | |
3485 | leaf = path->nodes[0]; | |
3486 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
3487 | ||
3488 | /* make sure the item matches what we want */ | |
3489 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3490 | break; | |
3491 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) | |
3492 | break; | |
3493 | ||
3494 | /* release the path since we're done with it */ | |
3495 | btrfs_release_path(path); | |
3496 | ||
3497 | /* | |
3498 | * this is where we are basically btrfs_lookup, without the | |
3499 | * crossing root thing. we store the inode number in the | |
3500 | * offset of the orphan item. | |
3501 | */ | |
3502 | ||
3503 | if (found_key.offset == last_objectid) { | |
3504 | btrfs_err(fs_info, | |
3505 | "Error removing orphan entry, stopping orphan cleanup"); | |
3506 | ret = -EINVAL; | |
3507 | goto out; | |
3508 | } | |
3509 | ||
3510 | last_objectid = found_key.offset; | |
3511 | ||
3512 | found_key.objectid = found_key.offset; | |
3513 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3514 | found_key.offset = 0; | |
3515 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); | |
3516 | ret = PTR_ERR_OR_ZERO(inode); | |
3517 | if (ret && ret != -ENOENT) | |
3518 | goto out; | |
3519 | ||
3520 | if (ret == -ENOENT && root == fs_info->tree_root) { | |
3521 | struct btrfs_root *dead_root; | |
3522 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3523 | int is_dead_root = 0; | |
3524 | ||
3525 | /* | |
3526 | * this is an orphan in the tree root. Currently these | |
3527 | * could come from 2 sources: | |
3528 | * a) a snapshot deletion in progress | |
3529 | * b) a free space cache inode | |
3530 | * We need to distinguish those two, as the snapshot | |
3531 | * orphan must not get deleted. | |
3532 | * find_dead_roots already ran before us, so if this | |
3533 | * is a snapshot deletion, we should find the root | |
3534 | * in the dead_roots list | |
3535 | */ | |
3536 | spin_lock(&fs_info->trans_lock); | |
3537 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3538 | root_list) { | |
3539 | if (dead_root->root_key.objectid == | |
3540 | found_key.objectid) { | |
3541 | is_dead_root = 1; | |
3542 | break; | |
3543 | } | |
3544 | } | |
3545 | spin_unlock(&fs_info->trans_lock); | |
3546 | if (is_dead_root) { | |
3547 | /* prevent this orphan from being found again */ | |
3548 | key.offset = found_key.objectid - 1; | |
3549 | continue; | |
3550 | } | |
3551 | } | |
3552 | /* | |
3553 | * Inode is already gone but the orphan item is still there, | |
3554 | * kill the orphan item. | |
3555 | */ | |
3556 | if (ret == -ENOENT) { | |
3557 | trans = btrfs_start_transaction(root, 1); | |
3558 | if (IS_ERR(trans)) { | |
3559 | ret = PTR_ERR(trans); | |
3560 | goto out; | |
3561 | } | |
3562 | btrfs_debug(fs_info, "auto deleting %Lu", | |
3563 | found_key.objectid); | |
3564 | ret = btrfs_del_orphan_item(trans, root, | |
3565 | found_key.objectid); | |
3566 | btrfs_end_transaction(trans); | |
3567 | if (ret) | |
3568 | goto out; | |
3569 | continue; | |
3570 | } | |
3571 | ||
3572 | /* | |
3573 | * add this inode to the orphan list so btrfs_orphan_del does | |
3574 | * the proper thing when we hit it | |
3575 | */ | |
3576 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3577 | &BTRFS_I(inode)->runtime_flags); | |
3578 | atomic_inc(&root->orphan_inodes); | |
3579 | ||
3580 | /* if we have links, this was a truncate, lets do that */ | |
3581 | if (inode->i_nlink) { | |
3582 | if (WARN_ON(!S_ISREG(inode->i_mode))) { | |
3583 | iput(inode); | |
3584 | continue; | |
3585 | } | |
3586 | nr_truncate++; | |
3587 | ||
3588 | /* 1 for the orphan item deletion. */ | |
3589 | trans = btrfs_start_transaction(root, 1); | |
3590 | if (IS_ERR(trans)) { | |
3591 | iput(inode); | |
3592 | ret = PTR_ERR(trans); | |
3593 | goto out; | |
3594 | } | |
3595 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
3596 | btrfs_end_transaction(trans); | |
3597 | if (ret) { | |
3598 | iput(inode); | |
3599 | goto out; | |
3600 | } | |
3601 | ||
3602 | ret = btrfs_truncate(inode); | |
3603 | if (ret) | |
3604 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
3605 | } else { | |
3606 | nr_unlink++; | |
3607 | } | |
3608 | ||
3609 | /* this will do delete_inode and everything for us */ | |
3610 | iput(inode); | |
3611 | if (ret) | |
3612 | goto out; | |
3613 | } | |
3614 | /* release the path since we're done with it */ | |
3615 | btrfs_release_path(path); | |
3616 | ||
3617 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; | |
3618 | ||
3619 | if (root->orphan_block_rsv) | |
3620 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, | |
3621 | (u64)-1); | |
3622 | ||
3623 | if (root->orphan_block_rsv || | |
3624 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
3625 | trans = btrfs_join_transaction(root); | |
3626 | if (!IS_ERR(trans)) | |
3627 | btrfs_end_transaction(trans); | |
3628 | } | |
3629 | ||
3630 | if (nr_unlink) | |
3631 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); | |
3632 | if (nr_truncate) | |
3633 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); | |
3634 | ||
3635 | out: | |
3636 | if (ret) | |
3637 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); | |
3638 | btrfs_free_path(path); | |
3639 | return ret; | |
3640 | } | |
3641 | ||
3642 | /* | |
3643 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3644 | * don't find any xattrs, we know there can't be any acls. | |
3645 | * | |
3646 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3647 | */ | |
3648 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
3649 | int slot, u64 objectid, | |
3650 | int *first_xattr_slot) | |
3651 | { | |
3652 | u32 nritems = btrfs_header_nritems(leaf); | |
3653 | struct btrfs_key found_key; | |
3654 | static u64 xattr_access = 0; | |
3655 | static u64 xattr_default = 0; | |
3656 | int scanned = 0; | |
3657 | ||
3658 | if (!xattr_access) { | |
3659 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, | |
3660 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3661 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3662 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
3663 | } | |
3664 | ||
3665 | slot++; | |
3666 | *first_xattr_slot = -1; | |
3667 | while (slot < nritems) { | |
3668 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3669 | ||
3670 | /* we found a different objectid, there must not be acls */ | |
3671 | if (found_key.objectid != objectid) | |
3672 | return 0; | |
3673 | ||
3674 | /* we found an xattr, assume we've got an acl */ | |
3675 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { | |
3676 | if (*first_xattr_slot == -1) | |
3677 | *first_xattr_slot = slot; | |
3678 | if (found_key.offset == xattr_access || | |
3679 | found_key.offset == xattr_default) | |
3680 | return 1; | |
3681 | } | |
3682 | ||
3683 | /* | |
3684 | * we found a key greater than an xattr key, there can't | |
3685 | * be any acls later on | |
3686 | */ | |
3687 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3688 | return 0; | |
3689 | ||
3690 | slot++; | |
3691 | scanned++; | |
3692 | ||
3693 | /* | |
3694 | * it goes inode, inode backrefs, xattrs, extents, | |
3695 | * so if there are a ton of hard links to an inode there can | |
3696 | * be a lot of backrefs. Don't waste time searching too hard, | |
3697 | * this is just an optimization | |
3698 | */ | |
3699 | if (scanned >= 8) | |
3700 | break; | |
3701 | } | |
3702 | /* we hit the end of the leaf before we found an xattr or | |
3703 | * something larger than an xattr. We have to assume the inode | |
3704 | * has acls | |
3705 | */ | |
3706 | if (*first_xattr_slot == -1) | |
3707 | *first_xattr_slot = slot; | |
3708 | return 1; | |
3709 | } | |
3710 | ||
3711 | /* | |
3712 | * read an inode from the btree into the in-memory inode | |
3713 | */ | |
3714 | static int btrfs_read_locked_inode(struct inode *inode) | |
3715 | { | |
3716 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
3717 | struct btrfs_path *path; | |
3718 | struct extent_buffer *leaf; | |
3719 | struct btrfs_inode_item *inode_item; | |
3720 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3721 | struct btrfs_key location; | |
3722 | unsigned long ptr; | |
3723 | int maybe_acls; | |
3724 | u32 rdev; | |
3725 | int ret; | |
3726 | bool filled = false; | |
3727 | int first_xattr_slot; | |
3728 | ||
3729 | ret = btrfs_fill_inode(inode, &rdev); | |
3730 | if (!ret) | |
3731 | filled = true; | |
3732 | ||
3733 | path = btrfs_alloc_path(); | |
3734 | if (!path) { | |
3735 | ret = -ENOMEM; | |
3736 | goto make_bad; | |
3737 | } | |
3738 | ||
3739 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); | |
3740 | ||
3741 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); | |
3742 | if (ret) { | |
3743 | if (ret > 0) | |
3744 | ret = -ENOENT; | |
3745 | goto make_bad; | |
3746 | } | |
3747 | ||
3748 | leaf = path->nodes[0]; | |
3749 | ||
3750 | if (filled) | |
3751 | goto cache_index; | |
3752 | ||
3753 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
3754 | struct btrfs_inode_item); | |
3755 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); | |
3756 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); | |
3757 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); | |
3758 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
3759 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); | |
3760 | ||
3761 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); | |
3762 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
3763 | ||
3764 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); | |
3765 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
3766 | ||
3767 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); | |
3768 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
3769 | ||
3770 | BTRFS_I(inode)->i_otime.tv_sec = | |
3771 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3772 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3773 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
3774 | ||
3775 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); | |
3776 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); | |
3777 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); | |
3778 | ||
3779 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); | |
3780 | inode->i_generation = BTRFS_I(inode)->generation; | |
3781 | inode->i_rdev = 0; | |
3782 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3783 | ||
3784 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3785 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3786 | ||
3787 | cache_index: | |
3788 | /* | |
3789 | * If we were modified in the current generation and evicted from memory | |
3790 | * and then re-read we need to do a full sync since we don't have any | |
3791 | * idea about which extents were modified before we were evicted from | |
3792 | * cache. | |
3793 | * | |
3794 | * This is required for both inode re-read from disk and delayed inode | |
3795 | * in delayed_nodes_tree. | |
3796 | */ | |
3797 | if (BTRFS_I(inode)->last_trans == fs_info->generation) | |
3798 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3799 | &BTRFS_I(inode)->runtime_flags); | |
3800 | ||
3801 | /* | |
3802 | * We don't persist the id of the transaction where an unlink operation | |
3803 | * against the inode was last made. So here we assume the inode might | |
3804 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3805 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3806 | * between the inode and its parent if the inode is fsync'ed and the log | |
3807 | * replayed. For example, in the scenario: | |
3808 | * | |
3809 | * touch mydir/foo | |
3810 | * ln mydir/foo mydir/bar | |
3811 | * sync | |
3812 | * unlink mydir/bar | |
3813 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3814 | * xfs_io -c fsync mydir/foo | |
3815 | * <power failure> | |
3816 | * mount fs, triggers fsync log replay | |
3817 | * | |
3818 | * We must make sure that when we fsync our inode foo we also log its | |
3819 | * parent inode, otherwise after log replay the parent still has the | |
3820 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3821 | * and doesn't have an inode ref with the name "bar" anymore. | |
3822 | * | |
3823 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
3824 | * but it guarantees correctness at the expense of occasional full | |
3825 | * transaction commits on fsync if our inode is a directory, or if our | |
3826 | * inode is not a directory, logging its parent unnecessarily. | |
3827 | */ | |
3828 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3829 | ||
3830 | path->slots[0]++; | |
3831 | if (inode->i_nlink != 1 || | |
3832 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3833 | goto cache_acl; | |
3834 | ||
3835 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3836 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) | |
3837 | goto cache_acl; | |
3838 | ||
3839 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3840 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3841 | struct btrfs_inode_ref *ref; | |
3842 | ||
3843 | ref = (struct btrfs_inode_ref *)ptr; | |
3844 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3845 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3846 | struct btrfs_inode_extref *extref; | |
3847 | ||
3848 | extref = (struct btrfs_inode_extref *)ptr; | |
3849 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3850 | extref); | |
3851 | } | |
3852 | cache_acl: | |
3853 | /* | |
3854 | * try to precache a NULL acl entry for files that don't have | |
3855 | * any xattrs or acls | |
3856 | */ | |
3857 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], | |
3858 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); | |
3859 | if (first_xattr_slot != -1) { | |
3860 | path->slots[0] = first_xattr_slot; | |
3861 | ret = btrfs_load_inode_props(inode, path); | |
3862 | if (ret) | |
3863 | btrfs_err(fs_info, | |
3864 | "error loading props for ino %llu (root %llu): %d", | |
3865 | btrfs_ino(BTRFS_I(inode)), | |
3866 | root->root_key.objectid, ret); | |
3867 | } | |
3868 | btrfs_free_path(path); | |
3869 | ||
3870 | if (!maybe_acls) | |
3871 | cache_no_acl(inode); | |
3872 | ||
3873 | switch (inode->i_mode & S_IFMT) { | |
3874 | case S_IFREG: | |
3875 | inode->i_mapping->a_ops = &btrfs_aops; | |
3876 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
3877 | inode->i_fop = &btrfs_file_operations; | |
3878 | inode->i_op = &btrfs_file_inode_operations; | |
3879 | break; | |
3880 | case S_IFDIR: | |
3881 | inode->i_fop = &btrfs_dir_file_operations; | |
3882 | inode->i_op = &btrfs_dir_inode_operations; | |
3883 | break; | |
3884 | case S_IFLNK: | |
3885 | inode->i_op = &btrfs_symlink_inode_operations; | |
3886 | inode_nohighmem(inode); | |
3887 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
3888 | break; | |
3889 | default: | |
3890 | inode->i_op = &btrfs_special_inode_operations; | |
3891 | init_special_inode(inode, inode->i_mode, rdev); | |
3892 | break; | |
3893 | } | |
3894 | ||
3895 | btrfs_update_iflags(inode); | |
3896 | return 0; | |
3897 | ||
3898 | make_bad: | |
3899 | btrfs_free_path(path); | |
3900 | make_bad_inode(inode); | |
3901 | return ret; | |
3902 | } | |
3903 | ||
3904 | /* | |
3905 | * given a leaf and an inode, copy the inode fields into the leaf | |
3906 | */ | |
3907 | static void fill_inode_item(struct btrfs_trans_handle *trans, | |
3908 | struct extent_buffer *leaf, | |
3909 | struct btrfs_inode_item *item, | |
3910 | struct inode *inode) | |
3911 | { | |
3912 | struct btrfs_map_token token; | |
3913 | ||
3914 | btrfs_init_map_token(&token); | |
3915 | ||
3916 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); | |
3917 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3918 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3919 | &token); | |
3920 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3921 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
3922 | ||
3923 | btrfs_set_token_timespec_sec(leaf, &item->atime, | |
3924 | inode->i_atime.tv_sec, &token); | |
3925 | btrfs_set_token_timespec_nsec(leaf, &item->atime, | |
3926 | inode->i_atime.tv_nsec, &token); | |
3927 | ||
3928 | btrfs_set_token_timespec_sec(leaf, &item->mtime, | |
3929 | inode->i_mtime.tv_sec, &token); | |
3930 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, | |
3931 | inode->i_mtime.tv_nsec, &token); | |
3932 | ||
3933 | btrfs_set_token_timespec_sec(leaf, &item->ctime, | |
3934 | inode->i_ctime.tv_sec, &token); | |
3935 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, | |
3936 | inode->i_ctime.tv_nsec, &token); | |
3937 | ||
3938 | btrfs_set_token_timespec_sec(leaf, &item->otime, | |
3939 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3940 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3941 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3942 | ||
3943 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), | |
3944 | &token); | |
3945 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3946 | &token); | |
3947 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3948 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3949 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3950 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3951 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
3952 | } | |
3953 | ||
3954 | /* | |
3955 | * copy everything in the in-memory inode into the btree. | |
3956 | */ | |
3957 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, | |
3958 | struct btrfs_root *root, struct inode *inode) | |
3959 | { | |
3960 | struct btrfs_inode_item *inode_item; | |
3961 | struct btrfs_path *path; | |
3962 | struct extent_buffer *leaf; | |
3963 | int ret; | |
3964 | ||
3965 | path = btrfs_alloc_path(); | |
3966 | if (!path) | |
3967 | return -ENOMEM; | |
3968 | ||
3969 | path->leave_spinning = 1; | |
3970 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, | |
3971 | 1); | |
3972 | if (ret) { | |
3973 | if (ret > 0) | |
3974 | ret = -ENOENT; | |
3975 | goto failed; | |
3976 | } | |
3977 | ||
3978 | leaf = path->nodes[0]; | |
3979 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
3980 | struct btrfs_inode_item); | |
3981 | ||
3982 | fill_inode_item(trans, leaf, inode_item, inode); | |
3983 | btrfs_mark_buffer_dirty(leaf); | |
3984 | btrfs_set_inode_last_trans(trans, inode); | |
3985 | ret = 0; | |
3986 | failed: | |
3987 | btrfs_free_path(path); | |
3988 | return ret; | |
3989 | } | |
3990 | ||
3991 | /* | |
3992 | * copy everything in the in-memory inode into the btree. | |
3993 | */ | |
3994 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3995 | struct btrfs_root *root, struct inode *inode) | |
3996 | { | |
3997 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3998 | int ret; | |
3999 | ||
4000 | /* | |
4001 | * If the inode is a free space inode, we can deadlock during commit | |
4002 | * if we put it into the delayed code. | |
4003 | * | |
4004 | * The data relocation inode should also be directly updated | |
4005 | * without delay | |
4006 | */ | |
4007 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) | |
4008 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID | |
4009 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { | |
4010 | btrfs_update_root_times(trans, root); | |
4011 | ||
4012 | ret = btrfs_delayed_update_inode(trans, root, inode); | |
4013 | if (!ret) | |
4014 | btrfs_set_inode_last_trans(trans, inode); | |
4015 | return ret; | |
4016 | } | |
4017 | ||
4018 | return btrfs_update_inode_item(trans, root, inode); | |
4019 | } | |
4020 | ||
4021 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, | |
4022 | struct btrfs_root *root, | |
4023 | struct inode *inode) | |
4024 | { | |
4025 | int ret; | |
4026 | ||
4027 | ret = btrfs_update_inode(trans, root, inode); | |
4028 | if (ret == -ENOSPC) | |
4029 | return btrfs_update_inode_item(trans, root, inode); | |
4030 | return ret; | |
4031 | } | |
4032 | ||
4033 | /* | |
4034 | * unlink helper that gets used here in inode.c and in the tree logging | |
4035 | * recovery code. It remove a link in a directory with a given name, and | |
4036 | * also drops the back refs in the inode to the directory | |
4037 | */ | |
4038 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, | |
4039 | struct btrfs_root *root, | |
4040 | struct btrfs_inode *dir, | |
4041 | struct btrfs_inode *inode, | |
4042 | const char *name, int name_len) | |
4043 | { | |
4044 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4045 | struct btrfs_path *path; | |
4046 | int ret = 0; | |
4047 | struct extent_buffer *leaf; | |
4048 | struct btrfs_dir_item *di; | |
4049 | struct btrfs_key key; | |
4050 | u64 index; | |
4051 | u64 ino = btrfs_ino(inode); | |
4052 | u64 dir_ino = btrfs_ino(dir); | |
4053 | ||
4054 | path = btrfs_alloc_path(); | |
4055 | if (!path) { | |
4056 | ret = -ENOMEM; | |
4057 | goto out; | |
4058 | } | |
4059 | ||
4060 | path->leave_spinning = 1; | |
4061 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, | |
4062 | name, name_len, -1); | |
4063 | if (IS_ERR(di)) { | |
4064 | ret = PTR_ERR(di); | |
4065 | goto err; | |
4066 | } | |
4067 | if (!di) { | |
4068 | ret = -ENOENT; | |
4069 | goto err; | |
4070 | } | |
4071 | leaf = path->nodes[0]; | |
4072 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4073 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
4074 | if (ret) | |
4075 | goto err; | |
4076 | btrfs_release_path(path); | |
4077 | ||
4078 | /* | |
4079 | * If we don't have dir index, we have to get it by looking up | |
4080 | * the inode ref, since we get the inode ref, remove it directly, | |
4081 | * it is unnecessary to do delayed deletion. | |
4082 | * | |
4083 | * But if we have dir index, needn't search inode ref to get it. | |
4084 | * Since the inode ref is close to the inode item, it is better | |
4085 | * that we delay to delete it, and just do this deletion when | |
4086 | * we update the inode item. | |
4087 | */ | |
4088 | if (inode->dir_index) { | |
4089 | ret = btrfs_delayed_delete_inode_ref(inode); | |
4090 | if (!ret) { | |
4091 | index = inode->dir_index; | |
4092 | goto skip_backref; | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, | |
4097 | dir_ino, &index); | |
4098 | if (ret) { | |
4099 | btrfs_info(fs_info, | |
4100 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
4101 | name_len, name, ino, dir_ino); | |
4102 | btrfs_abort_transaction(trans, ret); | |
4103 | goto err; | |
4104 | } | |
4105 | skip_backref: | |
4106 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); | |
4107 | if (ret) { | |
4108 | btrfs_abort_transaction(trans, ret); | |
4109 | goto err; | |
4110 | } | |
4111 | ||
4112 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, | |
4113 | dir_ino); | |
4114 | if (ret != 0 && ret != -ENOENT) { | |
4115 | btrfs_abort_transaction(trans, ret); | |
4116 | goto err; | |
4117 | } | |
4118 | ||
4119 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, | |
4120 | index); | |
4121 | if (ret == -ENOENT) | |
4122 | ret = 0; | |
4123 | else if (ret) | |
4124 | btrfs_abort_transaction(trans, ret); | |
4125 | err: | |
4126 | btrfs_free_path(path); | |
4127 | if (ret) | |
4128 | goto out; | |
4129 | ||
4130 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); | |
4131 | inode_inc_iversion(&inode->vfs_inode); | |
4132 | inode_inc_iversion(&dir->vfs_inode); | |
4133 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4134 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4135 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
4136 | out: | |
4137 | return ret; | |
4138 | } | |
4139 | ||
4140 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, | |
4141 | struct btrfs_root *root, | |
4142 | struct btrfs_inode *dir, struct btrfs_inode *inode, | |
4143 | const char *name, int name_len) | |
4144 | { | |
4145 | int ret; | |
4146 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4147 | if (!ret) { | |
4148 | drop_nlink(&inode->vfs_inode); | |
4149 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
4150 | } | |
4151 | return ret; | |
4152 | } | |
4153 | ||
4154 | /* | |
4155 | * helper to start transaction for unlink and rmdir. | |
4156 | * | |
4157 | * unlink and rmdir are special in btrfs, they do not always free space, so | |
4158 | * if we cannot make our reservations the normal way try and see if there is | |
4159 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4160 | * allow the unlink to occur. | |
4161 | */ | |
4162 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) | |
4163 | { | |
4164 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4165 | ||
4166 | /* | |
4167 | * 1 for the possible orphan item | |
4168 | * 1 for the dir item | |
4169 | * 1 for the dir index | |
4170 | * 1 for the inode ref | |
4171 | * 1 for the inode | |
4172 | */ | |
4173 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); | |
4174 | } | |
4175 | ||
4176 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4177 | { | |
4178 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4179 | struct btrfs_trans_handle *trans; | |
4180 | struct inode *inode = d_inode(dentry); | |
4181 | int ret; | |
4182 | ||
4183 | trans = __unlink_start_trans(dir); | |
4184 | if (IS_ERR(trans)) | |
4185 | return PTR_ERR(trans); | |
4186 | ||
4187 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), | |
4188 | 0); | |
4189 | ||
4190 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), | |
4191 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4192 | dentry->d_name.len); | |
4193 | if (ret) | |
4194 | goto out; | |
4195 | ||
4196 | if (inode->i_nlink == 0) { | |
4197 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
4198 | if (ret) | |
4199 | goto out; | |
4200 | } | |
4201 | ||
4202 | out: | |
4203 | btrfs_end_transaction(trans); | |
4204 | btrfs_btree_balance_dirty(root->fs_info); | |
4205 | return ret; | |
4206 | } | |
4207 | ||
4208 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, | |
4209 | struct btrfs_root *root, | |
4210 | struct inode *dir, u64 objectid, | |
4211 | const char *name, int name_len) | |
4212 | { | |
4213 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4214 | struct btrfs_path *path; | |
4215 | struct extent_buffer *leaf; | |
4216 | struct btrfs_dir_item *di; | |
4217 | struct btrfs_key key; | |
4218 | u64 index; | |
4219 | int ret; | |
4220 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); | |
4221 | ||
4222 | path = btrfs_alloc_path(); | |
4223 | if (!path) | |
4224 | return -ENOMEM; | |
4225 | ||
4226 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, | |
4227 | name, name_len, -1); | |
4228 | if (IS_ERR_OR_NULL(di)) { | |
4229 | if (!di) | |
4230 | ret = -ENOENT; | |
4231 | else | |
4232 | ret = PTR_ERR(di); | |
4233 | goto out; | |
4234 | } | |
4235 | ||
4236 | leaf = path->nodes[0]; | |
4237 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4238 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4239 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
4240 | if (ret) { | |
4241 | btrfs_abort_transaction(trans, ret); | |
4242 | goto out; | |
4243 | } | |
4244 | btrfs_release_path(path); | |
4245 | ||
4246 | ret = btrfs_del_root_ref(trans, fs_info, objectid, | |
4247 | root->root_key.objectid, dir_ino, | |
4248 | &index, name, name_len); | |
4249 | if (ret < 0) { | |
4250 | if (ret != -ENOENT) { | |
4251 | btrfs_abort_transaction(trans, ret); | |
4252 | goto out; | |
4253 | } | |
4254 | di = btrfs_search_dir_index_item(root, path, dir_ino, | |
4255 | name, name_len); | |
4256 | if (IS_ERR_OR_NULL(di)) { | |
4257 | if (!di) | |
4258 | ret = -ENOENT; | |
4259 | else | |
4260 | ret = PTR_ERR(di); | |
4261 | btrfs_abort_transaction(trans, ret); | |
4262 | goto out; | |
4263 | } | |
4264 | ||
4265 | leaf = path->nodes[0]; | |
4266 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4267 | btrfs_release_path(path); | |
4268 | index = key.offset; | |
4269 | } | |
4270 | btrfs_release_path(path); | |
4271 | ||
4272 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); | |
4273 | if (ret) { | |
4274 | btrfs_abort_transaction(trans, ret); | |
4275 | goto out; | |
4276 | } | |
4277 | ||
4278 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); | |
4279 | inode_inc_iversion(dir); | |
4280 | dir->i_mtime = dir->i_ctime = current_time(dir); | |
4281 | ret = btrfs_update_inode_fallback(trans, root, dir); | |
4282 | if (ret) | |
4283 | btrfs_abort_transaction(trans, ret); | |
4284 | out: | |
4285 | btrfs_free_path(path); | |
4286 | return ret; | |
4287 | } | |
4288 | ||
4289 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
4290 | { | |
4291 | struct inode *inode = d_inode(dentry); | |
4292 | int err = 0; | |
4293 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4294 | struct btrfs_trans_handle *trans; | |
4295 | u64 last_unlink_trans; | |
4296 | ||
4297 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
4298 | return -ENOTEMPTY; | |
4299 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) | |
4300 | return -EPERM; | |
4301 | ||
4302 | trans = __unlink_start_trans(dir); | |
4303 | if (IS_ERR(trans)) | |
4304 | return PTR_ERR(trans); | |
4305 | ||
4306 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { | |
4307 | err = btrfs_unlink_subvol(trans, root, dir, | |
4308 | BTRFS_I(inode)->location.objectid, | |
4309 | dentry->d_name.name, | |
4310 | dentry->d_name.len); | |
4311 | goto out; | |
4312 | } | |
4313 | ||
4314 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
4315 | if (err) | |
4316 | goto out; | |
4317 | ||
4318 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; | |
4319 | ||
4320 | /* now the directory is empty */ | |
4321 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), | |
4322 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4323 | dentry->d_name.len); | |
4324 | if (!err) { | |
4325 | btrfs_i_size_write(BTRFS_I(inode), 0); | |
4326 | /* | |
4327 | * Propagate the last_unlink_trans value of the deleted dir to | |
4328 | * its parent directory. This is to prevent an unrecoverable | |
4329 | * log tree in the case we do something like this: | |
4330 | * 1) create dir foo | |
4331 | * 2) create snapshot under dir foo | |
4332 | * 3) delete the snapshot | |
4333 | * 4) rmdir foo | |
4334 | * 5) mkdir foo | |
4335 | * 6) fsync foo or some file inside foo | |
4336 | */ | |
4337 | if (last_unlink_trans >= trans->transid) | |
4338 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4339 | } | |
4340 | out: | |
4341 | btrfs_end_transaction(trans); | |
4342 | btrfs_btree_balance_dirty(root->fs_info); | |
4343 | ||
4344 | return err; | |
4345 | } | |
4346 | ||
4347 | static int truncate_space_check(struct btrfs_trans_handle *trans, | |
4348 | struct btrfs_root *root, | |
4349 | u64 bytes_deleted) | |
4350 | { | |
4351 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4352 | int ret; | |
4353 | ||
4354 | /* | |
4355 | * This is only used to apply pressure to the enospc system, we don't | |
4356 | * intend to use this reservation at all. | |
4357 | */ | |
4358 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); | |
4359 | bytes_deleted *= fs_info->nodesize; | |
4360 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
4361 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
4362 | if (!ret) { | |
4363 | trace_btrfs_space_reservation(fs_info, "transaction", | |
4364 | trans->transid, | |
4365 | bytes_deleted, 1); | |
4366 | trans->bytes_reserved += bytes_deleted; | |
4367 | } | |
4368 | return ret; | |
4369 | ||
4370 | } | |
4371 | ||
4372 | static int truncate_inline_extent(struct inode *inode, | |
4373 | struct btrfs_path *path, | |
4374 | struct btrfs_key *found_key, | |
4375 | const u64 item_end, | |
4376 | const u64 new_size) | |
4377 | { | |
4378 | struct extent_buffer *leaf = path->nodes[0]; | |
4379 | int slot = path->slots[0]; | |
4380 | struct btrfs_file_extent_item *fi; | |
4381 | u32 size = (u32)(new_size - found_key->offset); | |
4382 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4383 | ||
4384 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4385 | ||
4386 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4387 | loff_t offset = new_size; | |
4388 | loff_t page_end = ALIGN(offset, PAGE_SIZE); | |
4389 | ||
4390 | /* | |
4391 | * Zero out the remaining of the last page of our inline extent, | |
4392 | * instead of directly truncating our inline extent here - that | |
4393 | * would be much more complex (decompressing all the data, then | |
4394 | * compressing the truncated data, which might be bigger than | |
4395 | * the size of the inline extent, resize the extent, etc). | |
4396 | * We release the path because to get the page we might need to | |
4397 | * read the extent item from disk (data not in the page cache). | |
4398 | */ | |
4399 | btrfs_release_path(path); | |
4400 | return btrfs_truncate_block(inode, offset, page_end - offset, | |
4401 | 0); | |
4402 | } | |
4403 | ||
4404 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4405 | size = btrfs_file_extent_calc_inline_size(size); | |
4406 | btrfs_truncate_item(root->fs_info, path, size, 1); | |
4407 | ||
4408 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4409 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4410 | ||
4411 | return 0; | |
4412 | } | |
4413 | ||
4414 | /* | |
4415 | * this can truncate away extent items, csum items and directory items. | |
4416 | * It starts at a high offset and removes keys until it can't find | |
4417 | * any higher than new_size | |
4418 | * | |
4419 | * csum items that cross the new i_size are truncated to the new size | |
4420 | * as well. | |
4421 | * | |
4422 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4423 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
4424 | */ | |
4425 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |
4426 | struct btrfs_root *root, | |
4427 | struct inode *inode, | |
4428 | u64 new_size, u32 min_type) | |
4429 | { | |
4430 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4431 | struct btrfs_path *path; | |
4432 | struct extent_buffer *leaf; | |
4433 | struct btrfs_file_extent_item *fi; | |
4434 | struct btrfs_key key; | |
4435 | struct btrfs_key found_key; | |
4436 | u64 extent_start = 0; | |
4437 | u64 extent_num_bytes = 0; | |
4438 | u64 extent_offset = 0; | |
4439 | u64 item_end = 0; | |
4440 | u64 last_size = new_size; | |
4441 | u32 found_type = (u8)-1; | |
4442 | int found_extent; | |
4443 | int del_item; | |
4444 | int pending_del_nr = 0; | |
4445 | int pending_del_slot = 0; | |
4446 | int extent_type = -1; | |
4447 | int ret; | |
4448 | int err = 0; | |
4449 | u64 ino = btrfs_ino(BTRFS_I(inode)); | |
4450 | u64 bytes_deleted = 0; | |
4451 | bool be_nice = 0; | |
4452 | bool should_throttle = 0; | |
4453 | bool should_end = 0; | |
4454 | ||
4455 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
4456 | ||
4457 | /* | |
4458 | * for non-free space inodes and ref cows, we want to back off from | |
4459 | * time to time | |
4460 | */ | |
4461 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && | |
4462 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4463 | be_nice = 1; | |
4464 | ||
4465 | path = btrfs_alloc_path(); | |
4466 | if (!path) | |
4467 | return -ENOMEM; | |
4468 | path->reada = READA_BACK; | |
4469 | ||
4470 | /* | |
4471 | * We want to drop from the next block forward in case this new size is | |
4472 | * not block aligned since we will be keeping the last block of the | |
4473 | * extent just the way it is. | |
4474 | */ | |
4475 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4476 | root == fs_info->tree_root) | |
4477 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, | |
4478 | fs_info->sectorsize), | |
4479 | (u64)-1, 0); | |
4480 | ||
4481 | /* | |
4482 | * This function is also used to drop the items in the log tree before | |
4483 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4484 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4485 | * items. | |
4486 | */ | |
4487 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4488 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); | |
4489 | ||
4490 | key.objectid = ino; | |
4491 | key.offset = (u64)-1; | |
4492 | key.type = (u8)-1; | |
4493 | ||
4494 | search_again: | |
4495 | /* | |
4496 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4497 | * up a huge file in a single leaf. Most of the time that | |
4498 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4499 | */ | |
4500 | if (be_nice && bytes_deleted > SZ_32M) { | |
4501 | if (btrfs_should_end_transaction(trans)) { | |
4502 | err = -EAGAIN; | |
4503 | goto error; | |
4504 | } | |
4505 | } | |
4506 | ||
4507 | ||
4508 | path->leave_spinning = 1; | |
4509 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
4510 | if (ret < 0) { | |
4511 | err = ret; | |
4512 | goto out; | |
4513 | } | |
4514 | ||
4515 | if (ret > 0) { | |
4516 | /* there are no items in the tree for us to truncate, we're | |
4517 | * done | |
4518 | */ | |
4519 | if (path->slots[0] == 0) | |
4520 | goto out; | |
4521 | path->slots[0]--; | |
4522 | } | |
4523 | ||
4524 | while (1) { | |
4525 | fi = NULL; | |
4526 | leaf = path->nodes[0]; | |
4527 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
4528 | found_type = found_key.type; | |
4529 | ||
4530 | if (found_key.objectid != ino) | |
4531 | break; | |
4532 | ||
4533 | if (found_type < min_type) | |
4534 | break; | |
4535 | ||
4536 | item_end = found_key.offset; | |
4537 | if (found_type == BTRFS_EXTENT_DATA_KEY) { | |
4538 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
4539 | struct btrfs_file_extent_item); | |
4540 | extent_type = btrfs_file_extent_type(leaf, fi); | |
4541 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
4542 | item_end += | |
4543 | btrfs_file_extent_num_bytes(leaf, fi); | |
4544 | ||
4545 | trace_btrfs_truncate_show_fi_regular( | |
4546 | BTRFS_I(inode), leaf, fi, | |
4547 | found_key.offset); | |
4548 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
4549 | item_end += btrfs_file_extent_inline_len(leaf, | |
4550 | path->slots[0], fi); | |
4551 | ||
4552 | trace_btrfs_truncate_show_fi_inline( | |
4553 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4554 | found_key.offset); | |
4555 | } | |
4556 | item_end--; | |
4557 | } | |
4558 | if (found_type > min_type) { | |
4559 | del_item = 1; | |
4560 | } else { | |
4561 | if (item_end < new_size) | |
4562 | break; | |
4563 | if (found_key.offset >= new_size) | |
4564 | del_item = 1; | |
4565 | else | |
4566 | del_item = 0; | |
4567 | } | |
4568 | found_extent = 0; | |
4569 | /* FIXME, shrink the extent if the ref count is only 1 */ | |
4570 | if (found_type != BTRFS_EXTENT_DATA_KEY) | |
4571 | goto delete; | |
4572 | ||
4573 | if (del_item) | |
4574 | last_size = found_key.offset; | |
4575 | else | |
4576 | last_size = new_size; | |
4577 | ||
4578 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
4579 | u64 num_dec; | |
4580 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); | |
4581 | if (!del_item) { | |
4582 | u64 orig_num_bytes = | |
4583 | btrfs_file_extent_num_bytes(leaf, fi); | |
4584 | extent_num_bytes = ALIGN(new_size - | |
4585 | found_key.offset, | |
4586 | fs_info->sectorsize); | |
4587 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
4588 | extent_num_bytes); | |
4589 | num_dec = (orig_num_bytes - | |
4590 | extent_num_bytes); | |
4591 | if (test_bit(BTRFS_ROOT_REF_COWS, | |
4592 | &root->state) && | |
4593 | extent_start != 0) | |
4594 | inode_sub_bytes(inode, num_dec); | |
4595 | btrfs_mark_buffer_dirty(leaf); | |
4596 | } else { | |
4597 | extent_num_bytes = | |
4598 | btrfs_file_extent_disk_num_bytes(leaf, | |
4599 | fi); | |
4600 | extent_offset = found_key.offset - | |
4601 | btrfs_file_extent_offset(leaf, fi); | |
4602 | ||
4603 | /* FIXME blocksize != 4096 */ | |
4604 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); | |
4605 | if (extent_start != 0) { | |
4606 | found_extent = 1; | |
4607 | if (test_bit(BTRFS_ROOT_REF_COWS, | |
4608 | &root->state)) | |
4609 | inode_sub_bytes(inode, num_dec); | |
4610 | } | |
4611 | } | |
4612 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
4613 | /* | |
4614 | * we can't truncate inline items that have had | |
4615 | * special encodings | |
4616 | */ | |
4617 | if (!del_item && | |
4618 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
4619 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
4620 | ||
4621 | /* | |
4622 | * Need to release path in order to truncate a | |
4623 | * compressed extent. So delete any accumulated | |
4624 | * extent items so far. | |
4625 | */ | |
4626 | if (btrfs_file_extent_compression(leaf, fi) != | |
4627 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4628 | err = btrfs_del_items(trans, root, path, | |
4629 | pending_del_slot, | |
4630 | pending_del_nr); | |
4631 | if (err) { | |
4632 | btrfs_abort_transaction(trans, | |
4633 | err); | |
4634 | goto error; | |
4635 | } | |
4636 | pending_del_nr = 0; | |
4637 | } | |
4638 | ||
4639 | err = truncate_inline_extent(inode, path, | |
4640 | &found_key, | |
4641 | item_end, | |
4642 | new_size); | |
4643 | if (err) { | |
4644 | btrfs_abort_transaction(trans, err); | |
4645 | goto error; | |
4646 | } | |
4647 | } else if (test_bit(BTRFS_ROOT_REF_COWS, | |
4648 | &root->state)) { | |
4649 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4650 | } | |
4651 | } | |
4652 | delete: | |
4653 | if (del_item) { | |
4654 | if (!pending_del_nr) { | |
4655 | /* no pending yet, add ourselves */ | |
4656 | pending_del_slot = path->slots[0]; | |
4657 | pending_del_nr = 1; | |
4658 | } else if (pending_del_nr && | |
4659 | path->slots[0] + 1 == pending_del_slot) { | |
4660 | /* hop on the pending chunk */ | |
4661 | pending_del_nr++; | |
4662 | pending_del_slot = path->slots[0]; | |
4663 | } else { | |
4664 | BUG(); | |
4665 | } | |
4666 | } else { | |
4667 | break; | |
4668 | } | |
4669 | should_throttle = 0; | |
4670 | ||
4671 | if (found_extent && | |
4672 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4673 | root == fs_info->tree_root)) { | |
4674 | btrfs_set_path_blocking(path); | |
4675 | bytes_deleted += extent_num_bytes; | |
4676 | ret = btrfs_free_extent(trans, fs_info, extent_start, | |
4677 | extent_num_bytes, 0, | |
4678 | btrfs_header_owner(leaf), | |
4679 | ino, extent_offset); | |
4680 | BUG_ON(ret); | |
4681 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) | |
4682 | btrfs_async_run_delayed_refs(fs_info, | |
4683 | trans->delayed_ref_updates * 2, | |
4684 | trans->transid, 0); | |
4685 | if (be_nice) { | |
4686 | if (truncate_space_check(trans, root, | |
4687 | extent_num_bytes)) { | |
4688 | should_end = 1; | |
4689 | } | |
4690 | if (btrfs_should_throttle_delayed_refs(trans, | |
4691 | fs_info)) | |
4692 | should_throttle = 1; | |
4693 | } | |
4694 | } | |
4695 | ||
4696 | if (found_type == BTRFS_INODE_ITEM_KEY) | |
4697 | break; | |
4698 | ||
4699 | if (path->slots[0] == 0 || | |
4700 | path->slots[0] != pending_del_slot || | |
4701 | should_throttle || should_end) { | |
4702 | if (pending_del_nr) { | |
4703 | ret = btrfs_del_items(trans, root, path, | |
4704 | pending_del_slot, | |
4705 | pending_del_nr); | |
4706 | if (ret) { | |
4707 | btrfs_abort_transaction(trans, ret); | |
4708 | goto error; | |
4709 | } | |
4710 | pending_del_nr = 0; | |
4711 | } | |
4712 | btrfs_release_path(path); | |
4713 | if (should_throttle) { | |
4714 | unsigned long updates = trans->delayed_ref_updates; | |
4715 | if (updates) { | |
4716 | trans->delayed_ref_updates = 0; | |
4717 | ret = btrfs_run_delayed_refs(trans, | |
4718 | fs_info, | |
4719 | updates * 2); | |
4720 | if (ret && !err) | |
4721 | err = ret; | |
4722 | } | |
4723 | } | |
4724 | /* | |
4725 | * if we failed to refill our space rsv, bail out | |
4726 | * and let the transaction restart | |
4727 | */ | |
4728 | if (should_end) { | |
4729 | err = -EAGAIN; | |
4730 | goto error; | |
4731 | } | |
4732 | goto search_again; | |
4733 | } else { | |
4734 | path->slots[0]--; | |
4735 | } | |
4736 | } | |
4737 | out: | |
4738 | if (pending_del_nr) { | |
4739 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4740 | pending_del_nr); | |
4741 | if (ret) | |
4742 | btrfs_abort_transaction(trans, ret); | |
4743 | } | |
4744 | error: | |
4745 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | |
4746 | ASSERT(last_size >= new_size); | |
4747 | if (!err && last_size > new_size) | |
4748 | last_size = new_size; | |
4749 | btrfs_ordered_update_i_size(inode, last_size, NULL); | |
4750 | } | |
4751 | ||
4752 | btrfs_free_path(path); | |
4753 | ||
4754 | if (be_nice && bytes_deleted > SZ_32M) { | |
4755 | unsigned long updates = trans->delayed_ref_updates; | |
4756 | if (updates) { | |
4757 | trans->delayed_ref_updates = 0; | |
4758 | ret = btrfs_run_delayed_refs(trans, fs_info, | |
4759 | updates * 2); | |
4760 | if (ret && !err) | |
4761 | err = ret; | |
4762 | } | |
4763 | } | |
4764 | return err; | |
4765 | } | |
4766 | ||
4767 | /* | |
4768 | * btrfs_truncate_block - read, zero a chunk and write a block | |
4769 | * @inode - inode that we're zeroing | |
4770 | * @from - the offset to start zeroing | |
4771 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4772 | * offset | |
4773 | * @front - zero up to the offset instead of from the offset on | |
4774 | * | |
4775 | * This will find the block for the "from" offset and cow the block and zero the | |
4776 | * part we want to zero. This is used with truncate and hole punching. | |
4777 | */ | |
4778 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, | |
4779 | int front) | |
4780 | { | |
4781 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4782 | struct address_space *mapping = inode->i_mapping; | |
4783 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4784 | struct btrfs_ordered_extent *ordered; | |
4785 | struct extent_state *cached_state = NULL; | |
4786 | struct extent_changeset *data_reserved = NULL; | |
4787 | char *kaddr; | |
4788 | u32 blocksize = fs_info->sectorsize; | |
4789 | pgoff_t index = from >> PAGE_SHIFT; | |
4790 | unsigned offset = from & (blocksize - 1); | |
4791 | struct page *page; | |
4792 | gfp_t mask = btrfs_alloc_write_mask(mapping); | |
4793 | int ret = 0; | |
4794 | u64 block_start; | |
4795 | u64 block_end; | |
4796 | ||
4797 | if ((offset & (blocksize - 1)) == 0 && | |
4798 | (!len || ((len & (blocksize - 1)) == 0))) | |
4799 | goto out; | |
4800 | ||
4801 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, | |
4802 | round_down(from, blocksize), blocksize); | |
4803 | if (ret) | |
4804 | goto out; | |
4805 | ||
4806 | again: | |
4807 | page = find_or_create_page(mapping, index, mask); | |
4808 | if (!page) { | |
4809 | btrfs_delalloc_release_space(inode, data_reserved, | |
4810 | round_down(from, blocksize), | |
4811 | blocksize); | |
4812 | ret = -ENOMEM; | |
4813 | goto out; | |
4814 | } | |
4815 | ||
4816 | block_start = round_down(from, blocksize); | |
4817 | block_end = block_start + blocksize - 1; | |
4818 | ||
4819 | if (!PageUptodate(page)) { | |
4820 | ret = btrfs_readpage(NULL, page); | |
4821 | lock_page(page); | |
4822 | if (page->mapping != mapping) { | |
4823 | unlock_page(page); | |
4824 | put_page(page); | |
4825 | goto again; | |
4826 | } | |
4827 | if (!PageUptodate(page)) { | |
4828 | ret = -EIO; | |
4829 | goto out_unlock; | |
4830 | } | |
4831 | } | |
4832 | wait_on_page_writeback(page); | |
4833 | ||
4834 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); | |
4835 | set_page_extent_mapped(page); | |
4836 | ||
4837 | ordered = btrfs_lookup_ordered_extent(inode, block_start); | |
4838 | if (ordered) { | |
4839 | unlock_extent_cached(io_tree, block_start, block_end, | |
4840 | &cached_state, GFP_NOFS); | |
4841 | unlock_page(page); | |
4842 | put_page(page); | |
4843 | btrfs_start_ordered_extent(inode, ordered, 1); | |
4844 | btrfs_put_ordered_extent(ordered); | |
4845 | goto again; | |
4846 | } | |
4847 | ||
4848 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, | |
4849 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
4850 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
4851 | 0, 0, &cached_state, GFP_NOFS); | |
4852 | ||
4853 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, | |
4854 | &cached_state, 0); | |
4855 | if (ret) { | |
4856 | unlock_extent_cached(io_tree, block_start, block_end, | |
4857 | &cached_state, GFP_NOFS); | |
4858 | goto out_unlock; | |
4859 | } | |
4860 | ||
4861 | if (offset != blocksize) { | |
4862 | if (!len) | |
4863 | len = blocksize - offset; | |
4864 | kaddr = kmap(page); | |
4865 | if (front) | |
4866 | memset(kaddr + (block_start - page_offset(page)), | |
4867 | 0, offset); | |
4868 | else | |
4869 | memset(kaddr + (block_start - page_offset(page)) + offset, | |
4870 | 0, len); | |
4871 | flush_dcache_page(page); | |
4872 | kunmap(page); | |
4873 | } | |
4874 | ClearPageChecked(page); | |
4875 | set_page_dirty(page); | |
4876 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, | |
4877 | GFP_NOFS); | |
4878 | ||
4879 | out_unlock: | |
4880 | if (ret) | |
4881 | btrfs_delalloc_release_space(inode, data_reserved, block_start, | |
4882 | blocksize); | |
4883 | unlock_page(page); | |
4884 | put_page(page); | |
4885 | out: | |
4886 | extent_changeset_free(data_reserved); | |
4887 | return ret; | |
4888 | } | |
4889 | ||
4890 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, | |
4891 | u64 offset, u64 len) | |
4892 | { | |
4893 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4894 | struct btrfs_trans_handle *trans; | |
4895 | int ret; | |
4896 | ||
4897 | /* | |
4898 | * Still need to make sure the inode looks like it's been updated so | |
4899 | * that any holes get logged if we fsync. | |
4900 | */ | |
4901 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { | |
4902 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
4903 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4904 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4905 | return 0; | |
4906 | } | |
4907 | ||
4908 | /* | |
4909 | * 1 - for the one we're dropping | |
4910 | * 1 - for the one we're adding | |
4911 | * 1 - for updating the inode. | |
4912 | */ | |
4913 | trans = btrfs_start_transaction(root, 3); | |
4914 | if (IS_ERR(trans)) | |
4915 | return PTR_ERR(trans); | |
4916 | ||
4917 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4918 | if (ret) { | |
4919 | btrfs_abort_transaction(trans, ret); | |
4920 | btrfs_end_transaction(trans); | |
4921 | return ret; | |
4922 | } | |
4923 | ||
4924 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), | |
4925 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
4926 | if (ret) | |
4927 | btrfs_abort_transaction(trans, ret); | |
4928 | else | |
4929 | btrfs_update_inode(trans, root, inode); | |
4930 | btrfs_end_transaction(trans); | |
4931 | return ret; | |
4932 | } | |
4933 | ||
4934 | /* | |
4935 | * This function puts in dummy file extents for the area we're creating a hole | |
4936 | * for. So if we are truncating this file to a larger size we need to insert | |
4937 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4938 | * the range between oldsize and size | |
4939 | */ | |
4940 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) | |
4941 | { | |
4942 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4943 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4944 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4945 | struct extent_map *em = NULL; | |
4946 | struct extent_state *cached_state = NULL; | |
4947 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
4948 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); | |
4949 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
4950 | u64 last_byte; | |
4951 | u64 cur_offset; | |
4952 | u64 hole_size; | |
4953 | int err = 0; | |
4954 | ||
4955 | /* | |
4956 | * If our size started in the middle of a block we need to zero out the | |
4957 | * rest of the block before we expand the i_size, otherwise we could | |
4958 | * expose stale data. | |
4959 | */ | |
4960 | err = btrfs_truncate_block(inode, oldsize, 0, 0); | |
4961 | if (err) | |
4962 | return err; | |
4963 | ||
4964 | if (size <= hole_start) | |
4965 | return 0; | |
4966 | ||
4967 | while (1) { | |
4968 | struct btrfs_ordered_extent *ordered; | |
4969 | ||
4970 | lock_extent_bits(io_tree, hole_start, block_end - 1, | |
4971 | &cached_state); | |
4972 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, | |
4973 | block_end - hole_start); | |
4974 | if (!ordered) | |
4975 | break; | |
4976 | unlock_extent_cached(io_tree, hole_start, block_end - 1, | |
4977 | &cached_state, GFP_NOFS); | |
4978 | btrfs_start_ordered_extent(inode, ordered, 1); | |
4979 | btrfs_put_ordered_extent(ordered); | |
4980 | } | |
4981 | ||
4982 | cur_offset = hole_start; | |
4983 | while (1) { | |
4984 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, | |
4985 | block_end - cur_offset, 0); | |
4986 | if (IS_ERR(em)) { | |
4987 | err = PTR_ERR(em); | |
4988 | em = NULL; | |
4989 | break; | |
4990 | } | |
4991 | last_byte = min(extent_map_end(em), block_end); | |
4992 | last_byte = ALIGN(last_byte, fs_info->sectorsize); | |
4993 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
4994 | struct extent_map *hole_em; | |
4995 | hole_size = last_byte - cur_offset; | |
4996 | ||
4997 | err = maybe_insert_hole(root, inode, cur_offset, | |
4998 | hole_size); | |
4999 | if (err) | |
5000 | break; | |
5001 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, | |
5002 | cur_offset + hole_size - 1, 0); | |
5003 | hole_em = alloc_extent_map(); | |
5004 | if (!hole_em) { | |
5005 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
5006 | &BTRFS_I(inode)->runtime_flags); | |
5007 | goto next; | |
5008 | } | |
5009 | hole_em->start = cur_offset; | |
5010 | hole_em->len = hole_size; | |
5011 | hole_em->orig_start = cur_offset; | |
5012 | ||
5013 | hole_em->block_start = EXTENT_MAP_HOLE; | |
5014 | hole_em->block_len = 0; | |
5015 | hole_em->orig_block_len = 0; | |
5016 | hole_em->ram_bytes = hole_size; | |
5017 | hole_em->bdev = fs_info->fs_devices->latest_bdev; | |
5018 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
5019 | hole_em->generation = fs_info->generation; | |
5020 | ||
5021 | while (1) { | |
5022 | write_lock(&em_tree->lock); | |
5023 | err = add_extent_mapping(em_tree, hole_em, 1); | |
5024 | write_unlock(&em_tree->lock); | |
5025 | if (err != -EEXIST) | |
5026 | break; | |
5027 | btrfs_drop_extent_cache(BTRFS_I(inode), | |
5028 | cur_offset, | |
5029 | cur_offset + | |
5030 | hole_size - 1, 0); | |
5031 | } | |
5032 | free_extent_map(hole_em); | |
5033 | } | |
5034 | next: | |
5035 | free_extent_map(em); | |
5036 | em = NULL; | |
5037 | cur_offset = last_byte; | |
5038 | if (cur_offset >= block_end) | |
5039 | break; | |
5040 | } | |
5041 | free_extent_map(em); | |
5042 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, | |
5043 | GFP_NOFS); | |
5044 | return err; | |
5045 | } | |
5046 | ||
5047 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) | |
5048 | { | |
5049 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5050 | struct btrfs_trans_handle *trans; | |
5051 | loff_t oldsize = i_size_read(inode); | |
5052 | loff_t newsize = attr->ia_size; | |
5053 | int mask = attr->ia_valid; | |
5054 | int ret; | |
5055 | ||
5056 | /* | |
5057 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5058 | * special case where we need to update the times despite not having | |
5059 | * these flags set. For all other operations the VFS set these flags | |
5060 | * explicitly if it wants a timestamp update. | |
5061 | */ | |
5062 | if (newsize != oldsize) { | |
5063 | inode_inc_iversion(inode); | |
5064 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5065 | inode->i_ctime = inode->i_mtime = | |
5066 | current_time(inode); | |
5067 | } | |
5068 | ||
5069 | if (newsize > oldsize) { | |
5070 | /* | |
5071 | * Don't do an expanding truncate while snapshoting is ongoing. | |
5072 | * This is to ensure the snapshot captures a fully consistent | |
5073 | * state of this file - if the snapshot captures this expanding | |
5074 | * truncation, it must capture all writes that happened before | |
5075 | * this truncation. | |
5076 | */ | |
5077 | btrfs_wait_for_snapshot_creation(root); | |
5078 | ret = btrfs_cont_expand(inode, oldsize, newsize); | |
5079 | if (ret) { | |
5080 | btrfs_end_write_no_snapshoting(root); | |
5081 | return ret; | |
5082 | } | |
5083 | ||
5084 | trans = btrfs_start_transaction(root, 1); | |
5085 | if (IS_ERR(trans)) { | |
5086 | btrfs_end_write_no_snapshoting(root); | |
5087 | return PTR_ERR(trans); | |
5088 | } | |
5089 | ||
5090 | i_size_write(inode, newsize); | |
5091 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
5092 | pagecache_isize_extended(inode, oldsize, newsize); | |
5093 | ret = btrfs_update_inode(trans, root, inode); | |
5094 | btrfs_end_write_no_snapshoting(root); | |
5095 | btrfs_end_transaction(trans); | |
5096 | } else { | |
5097 | ||
5098 | /* | |
5099 | * We're truncating a file that used to have good data down to | |
5100 | * zero. Make sure it gets into the ordered flush list so that | |
5101 | * any new writes get down to disk quickly. | |
5102 | */ | |
5103 | if (newsize == 0) | |
5104 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, | |
5105 | &BTRFS_I(inode)->runtime_flags); | |
5106 | ||
5107 | /* | |
5108 | * 1 for the orphan item we're going to add | |
5109 | * 1 for the orphan item deletion. | |
5110 | */ | |
5111 | trans = btrfs_start_transaction(root, 2); | |
5112 | if (IS_ERR(trans)) | |
5113 | return PTR_ERR(trans); | |
5114 | ||
5115 | /* | |
5116 | * We need to do this in case we fail at _any_ point during the | |
5117 | * actual truncate. Once we do the truncate_setsize we could | |
5118 | * invalidate pages which forces any outstanding ordered io to | |
5119 | * be instantly completed which will give us extents that need | |
5120 | * to be truncated. If we fail to get an orphan inode down we | |
5121 | * could have left over extents that were never meant to live, | |
5122 | * so we need to guarantee from this point on that everything | |
5123 | * will be consistent. | |
5124 | */ | |
5125 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
5126 | btrfs_end_transaction(trans); | |
5127 | if (ret) | |
5128 | return ret; | |
5129 | ||
5130 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ | |
5131 | truncate_setsize(inode, newsize); | |
5132 | ||
5133 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
5134 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); | |
5135 | inode_dio_wait(inode); | |
5136 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); | |
5137 | ||
5138 | ret = btrfs_truncate(inode); | |
5139 | if (ret && inode->i_nlink) { | |
5140 | int err; | |
5141 | ||
5142 | /* To get a stable disk_i_size */ | |
5143 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5144 | if (err) { | |
5145 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5146 | return err; | |
5147 | } | |
5148 | ||
5149 | /* | |
5150 | * failed to truncate, disk_i_size is only adjusted down | |
5151 | * as we remove extents, so it should represent the true | |
5152 | * size of the inode, so reset the in memory size and | |
5153 | * delete our orphan entry. | |
5154 | */ | |
5155 | trans = btrfs_join_transaction(root); | |
5156 | if (IS_ERR(trans)) { | |
5157 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5158 | return ret; | |
5159 | } | |
5160 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
5161 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5162 | if (err) | |
5163 | btrfs_abort_transaction(trans, err); | |
5164 | btrfs_end_transaction(trans); | |
5165 | } | |
5166 | } | |
5167 | ||
5168 | return ret; | |
5169 | } | |
5170 | ||
5171 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) | |
5172 | { | |
5173 | struct inode *inode = d_inode(dentry); | |
5174 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5175 | int err; | |
5176 | ||
5177 | if (btrfs_root_readonly(root)) | |
5178 | return -EROFS; | |
5179 | ||
5180 | err = setattr_prepare(dentry, attr); | |
5181 | if (err) | |
5182 | return err; | |
5183 | ||
5184 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { | |
5185 | err = btrfs_setsize(inode, attr); | |
5186 | if (err) | |
5187 | return err; | |
5188 | } | |
5189 | ||
5190 | if (attr->ia_valid) { | |
5191 | setattr_copy(inode, attr); | |
5192 | inode_inc_iversion(inode); | |
5193 | err = btrfs_dirty_inode(inode); | |
5194 | ||
5195 | if (!err && attr->ia_valid & ATTR_MODE) | |
5196 | err = posix_acl_chmod(inode, inode->i_mode); | |
5197 | } | |
5198 | ||
5199 | return err; | |
5200 | } | |
5201 | ||
5202 | /* | |
5203 | * While truncating the inode pages during eviction, we get the VFS calling | |
5204 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5205 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5206 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5207 | * extent_state structures over and over, wasting lots of time. | |
5208 | * | |
5209 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5210 | * those expensive operations on a per page basis and do only the ordered io | |
5211 | * finishing, while we release here the extent_map and extent_state structures, | |
5212 | * without the excessive merging and splitting. | |
5213 | */ | |
5214 | static void evict_inode_truncate_pages(struct inode *inode) | |
5215 | { | |
5216 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5217 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5218 | struct rb_node *node; | |
5219 | ||
5220 | ASSERT(inode->i_state & I_FREEING); | |
5221 | truncate_inode_pages_final(&inode->i_data); | |
5222 | ||
5223 | write_lock(&map_tree->lock); | |
5224 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5225 | struct extent_map *em; | |
5226 | ||
5227 | node = rb_first(&map_tree->map); | |
5228 | em = rb_entry(node, struct extent_map, rb_node); | |
5229 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); | |
5230 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
5231 | remove_extent_mapping(map_tree, em); | |
5232 | free_extent_map(em); | |
5233 | if (need_resched()) { | |
5234 | write_unlock(&map_tree->lock); | |
5235 | cond_resched(); | |
5236 | write_lock(&map_tree->lock); | |
5237 | } | |
5238 | } | |
5239 | write_unlock(&map_tree->lock); | |
5240 | ||
5241 | /* | |
5242 | * Keep looping until we have no more ranges in the io tree. | |
5243 | * We can have ongoing bios started by readpages (called from readahead) | |
5244 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) | |
5245 | * still in progress (unlocked the pages in the bio but did not yet | |
5246 | * unlocked the ranges in the io tree). Therefore this means some | |
5247 | * ranges can still be locked and eviction started because before | |
5248 | * submitting those bios, which are executed by a separate task (work | |
5249 | * queue kthread), inode references (inode->i_count) were not taken | |
5250 | * (which would be dropped in the end io callback of each bio). | |
5251 | * Therefore here we effectively end up waiting for those bios and | |
5252 | * anyone else holding locked ranges without having bumped the inode's | |
5253 | * reference count - if we don't do it, when they access the inode's | |
5254 | * io_tree to unlock a range it may be too late, leading to an | |
5255 | * use-after-free issue. | |
5256 | */ | |
5257 | spin_lock(&io_tree->lock); | |
5258 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5259 | struct extent_state *state; | |
5260 | struct extent_state *cached_state = NULL; | |
5261 | u64 start; | |
5262 | u64 end; | |
5263 | ||
5264 | node = rb_first(&io_tree->state); | |
5265 | state = rb_entry(node, struct extent_state, rb_node); | |
5266 | start = state->start; | |
5267 | end = state->end; | |
5268 | spin_unlock(&io_tree->lock); | |
5269 | ||
5270 | lock_extent_bits(io_tree, start, end, &cached_state); | |
5271 | ||
5272 | /* | |
5273 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5274 | * and its reserved space won't be freed by delayed_ref. | |
5275 | * So we need to free its reserved space here. | |
5276 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5277 | * | |
5278 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5279 | */ | |
5280 | if (state->state & EXTENT_DELALLOC) | |
5281 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); | |
5282 | ||
5283 | clear_extent_bit(io_tree, start, end, | |
5284 | EXTENT_LOCKED | EXTENT_DIRTY | | |
5285 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5286 | EXTENT_DEFRAG, 1, 1, | |
5287 | &cached_state, GFP_NOFS); | |
5288 | ||
5289 | cond_resched(); | |
5290 | spin_lock(&io_tree->lock); | |
5291 | } | |
5292 | spin_unlock(&io_tree->lock); | |
5293 | } | |
5294 | ||
5295 | void btrfs_evict_inode(struct inode *inode) | |
5296 | { | |
5297 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5298 | struct btrfs_trans_handle *trans; | |
5299 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5300 | struct btrfs_block_rsv *rsv, *global_rsv; | |
5301 | int steal_from_global = 0; | |
5302 | u64 min_size; | |
5303 | int ret; | |
5304 | ||
5305 | trace_btrfs_inode_evict(inode); | |
5306 | ||
5307 | if (!root) { | |
5308 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5309 | return; | |
5310 | } | |
5311 | ||
5312 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
5313 | ||
5314 | evict_inode_truncate_pages(inode); | |
5315 | ||
5316 | if (inode->i_nlink && | |
5317 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5318 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5319 | btrfs_is_free_space_inode(BTRFS_I(inode)))) | |
5320 | goto no_delete; | |
5321 | ||
5322 | if (is_bad_inode(inode)) { | |
5323 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5324 | goto no_delete; | |
5325 | } | |
5326 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ | |
5327 | if (!special_file(inode->i_mode)) | |
5328 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5329 | ||
5330 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); | |
5331 | ||
5332 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { | |
5333 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
5334 | &BTRFS_I(inode)->runtime_flags)); | |
5335 | goto no_delete; | |
5336 | } | |
5337 | ||
5338 | if (inode->i_nlink > 0) { | |
5339 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && | |
5340 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
5341 | goto no_delete; | |
5342 | } | |
5343 | ||
5344 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); | |
5345 | if (ret) { | |
5346 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5347 | goto no_delete; | |
5348 | } | |
5349 | ||
5350 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); | |
5351 | if (!rsv) { | |
5352 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5353 | goto no_delete; | |
5354 | } | |
5355 | rsv->size = min_size; | |
5356 | rsv->failfast = 1; | |
5357 | global_rsv = &fs_info->global_block_rsv; | |
5358 | ||
5359 | btrfs_i_size_write(BTRFS_I(inode), 0); | |
5360 | ||
5361 | /* | |
5362 | * This is a bit simpler than btrfs_truncate since we've already | |
5363 | * reserved our space for our orphan item in the unlink, so we just | |
5364 | * need to reserve some slack space in case we add bytes and update | |
5365 | * inode item when doing the truncate. | |
5366 | */ | |
5367 | while (1) { | |
5368 | ret = btrfs_block_rsv_refill(root, rsv, min_size, | |
5369 | BTRFS_RESERVE_FLUSH_LIMIT); | |
5370 | ||
5371 | /* | |
5372 | * Try and steal from the global reserve since we will | |
5373 | * likely not use this space anyway, we want to try as | |
5374 | * hard as possible to get this to work. | |
5375 | */ | |
5376 | if (ret) | |
5377 | steal_from_global++; | |
5378 | else | |
5379 | steal_from_global = 0; | |
5380 | ret = 0; | |
5381 | ||
5382 | /* | |
5383 | * steal_from_global == 0: we reserved stuff, hooray! | |
5384 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5385 | * steal_from_global == 2: we've committed, still not a lot of | |
5386 | * room but maybe we'll have room in the global reserve this | |
5387 | * time. | |
5388 | * steal_from_global == 3: abandon all hope! | |
5389 | */ | |
5390 | if (steal_from_global > 2) { | |
5391 | btrfs_warn(fs_info, | |
5392 | "Could not get space for a delete, will truncate on mount %d", | |
5393 | ret); | |
5394 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5395 | btrfs_free_block_rsv(fs_info, rsv); | |
5396 | goto no_delete; | |
5397 | } | |
5398 | ||
5399 | trans = btrfs_join_transaction(root); | |
5400 | if (IS_ERR(trans)) { | |
5401 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5402 | btrfs_free_block_rsv(fs_info, rsv); | |
5403 | goto no_delete; | |
5404 | } | |
5405 | ||
5406 | /* | |
5407 | * We can't just steal from the global reserve, we need to make | |
5408 | * sure there is room to do it, if not we need to commit and try | |
5409 | * again. | |
5410 | */ | |
5411 | if (steal_from_global) { | |
5412 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) | |
5413 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
5414 | min_size, 0); | |
5415 | else | |
5416 | ret = -ENOSPC; | |
5417 | } | |
5418 | ||
5419 | /* | |
5420 | * Couldn't steal from the global reserve, we have too much | |
5421 | * pending stuff built up, commit the transaction and try it | |
5422 | * again. | |
5423 | */ | |
5424 | if (ret) { | |
5425 | ret = btrfs_commit_transaction(trans); | |
5426 | if (ret) { | |
5427 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5428 | btrfs_free_block_rsv(fs_info, rsv); | |
5429 | goto no_delete; | |
5430 | } | |
5431 | continue; | |
5432 | } else { | |
5433 | steal_from_global = 0; | |
5434 | } | |
5435 | ||
5436 | trans->block_rsv = rsv; | |
5437 | ||
5438 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); | |
5439 | if (ret != -ENOSPC && ret != -EAGAIN) | |
5440 | break; | |
5441 | ||
5442 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5443 | btrfs_end_transaction(trans); | |
5444 | trans = NULL; | |
5445 | btrfs_btree_balance_dirty(fs_info); | |
5446 | } | |
5447 | ||
5448 | btrfs_free_block_rsv(fs_info, rsv); | |
5449 | ||
5450 | /* | |
5451 | * Errors here aren't a big deal, it just means we leave orphan items | |
5452 | * in the tree. They will be cleaned up on the next mount. | |
5453 | */ | |
5454 | if (ret == 0) { | |
5455 | trans->block_rsv = root->orphan_block_rsv; | |
5456 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5457 | } else { | |
5458 | btrfs_orphan_del(NULL, BTRFS_I(inode)); | |
5459 | } | |
5460 | ||
5461 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5462 | if (!(root == fs_info->tree_root || | |
5463 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
5464 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); | |
5465 | ||
5466 | btrfs_end_transaction(trans); | |
5467 | btrfs_btree_balance_dirty(fs_info); | |
5468 | no_delete: | |
5469 | btrfs_remove_delayed_node(BTRFS_I(inode)); | |
5470 | clear_inode(inode); | |
5471 | } | |
5472 | ||
5473 | /* | |
5474 | * this returns the key found in the dir entry in the location pointer. | |
5475 | * If no dir entries were found, location->objectid is 0. | |
5476 | */ | |
5477 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5478 | struct btrfs_key *location) | |
5479 | { | |
5480 | const char *name = dentry->d_name.name; | |
5481 | int namelen = dentry->d_name.len; | |
5482 | struct btrfs_dir_item *di; | |
5483 | struct btrfs_path *path; | |
5484 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
5485 | int ret = 0; | |
5486 | ||
5487 | path = btrfs_alloc_path(); | |
5488 | if (!path) | |
5489 | return -ENOMEM; | |
5490 | ||
5491 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), | |
5492 | name, namelen, 0); | |
5493 | if (IS_ERR(di)) | |
5494 | ret = PTR_ERR(di); | |
5495 | ||
5496 | if (IS_ERR_OR_NULL(di)) | |
5497 | goto out_err; | |
5498 | ||
5499 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); | |
5500 | out: | |
5501 | btrfs_free_path(path); | |
5502 | return ret; | |
5503 | out_err: | |
5504 | location->objectid = 0; | |
5505 | goto out; | |
5506 | } | |
5507 | ||
5508 | /* | |
5509 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5510 | * needs to be changed to reflect the root directory of the tree root. This | |
5511 | * is kind of like crossing a mount point. | |
5512 | */ | |
5513 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, | |
5514 | struct inode *dir, | |
5515 | struct dentry *dentry, | |
5516 | struct btrfs_key *location, | |
5517 | struct btrfs_root **sub_root) | |
5518 | { | |
5519 | struct btrfs_path *path; | |
5520 | struct btrfs_root *new_root; | |
5521 | struct btrfs_root_ref *ref; | |
5522 | struct extent_buffer *leaf; | |
5523 | struct btrfs_key key; | |
5524 | int ret; | |
5525 | int err = 0; | |
5526 | ||
5527 | path = btrfs_alloc_path(); | |
5528 | if (!path) { | |
5529 | err = -ENOMEM; | |
5530 | goto out; | |
5531 | } | |
5532 | ||
5533 | err = -ENOENT; | |
5534 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; | |
5535 | key.type = BTRFS_ROOT_REF_KEY; | |
5536 | key.offset = location->objectid; | |
5537 | ||
5538 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
5539 | if (ret) { | |
5540 | if (ret < 0) | |
5541 | err = ret; | |
5542 | goto out; | |
5543 | } | |
5544 | ||
5545 | leaf = path->nodes[0]; | |
5546 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
5547 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || | |
5548 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) | |
5549 | goto out; | |
5550 | ||
5551 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, | |
5552 | (unsigned long)(ref + 1), | |
5553 | dentry->d_name.len); | |
5554 | if (ret) | |
5555 | goto out; | |
5556 | ||
5557 | btrfs_release_path(path); | |
5558 | ||
5559 | new_root = btrfs_read_fs_root_no_name(fs_info, location); | |
5560 | if (IS_ERR(new_root)) { | |
5561 | err = PTR_ERR(new_root); | |
5562 | goto out; | |
5563 | } | |
5564 | ||
5565 | *sub_root = new_root; | |
5566 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5567 | location->type = BTRFS_INODE_ITEM_KEY; | |
5568 | location->offset = 0; | |
5569 | err = 0; | |
5570 | out: | |
5571 | btrfs_free_path(path); | |
5572 | return err; | |
5573 | } | |
5574 | ||
5575 | static void inode_tree_add(struct inode *inode) | |
5576 | { | |
5577 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5578 | struct btrfs_inode *entry; | |
5579 | struct rb_node **p; | |
5580 | struct rb_node *parent; | |
5581 | struct rb_node *new = &BTRFS_I(inode)->rb_node; | |
5582 | u64 ino = btrfs_ino(BTRFS_I(inode)); | |
5583 | ||
5584 | if (inode_unhashed(inode)) | |
5585 | return; | |
5586 | parent = NULL; | |
5587 | spin_lock(&root->inode_lock); | |
5588 | p = &root->inode_tree.rb_node; | |
5589 | while (*p) { | |
5590 | parent = *p; | |
5591 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5592 | ||
5593 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) | |
5594 | p = &parent->rb_left; | |
5595 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) | |
5596 | p = &parent->rb_right; | |
5597 | else { | |
5598 | WARN_ON(!(entry->vfs_inode.i_state & | |
5599 | (I_WILL_FREE | I_FREEING))); | |
5600 | rb_replace_node(parent, new, &root->inode_tree); | |
5601 | RB_CLEAR_NODE(parent); | |
5602 | spin_unlock(&root->inode_lock); | |
5603 | return; | |
5604 | } | |
5605 | } | |
5606 | rb_link_node(new, parent, p); | |
5607 | rb_insert_color(new, &root->inode_tree); | |
5608 | spin_unlock(&root->inode_lock); | |
5609 | } | |
5610 | ||
5611 | static void inode_tree_del(struct inode *inode) | |
5612 | { | |
5613 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5614 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5615 | int empty = 0; | |
5616 | ||
5617 | spin_lock(&root->inode_lock); | |
5618 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { | |
5619 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); | |
5620 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); | |
5621 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5622 | } | |
5623 | spin_unlock(&root->inode_lock); | |
5624 | ||
5625 | if (empty && btrfs_root_refs(&root->root_item) == 0) { | |
5626 | synchronize_srcu(&fs_info->subvol_srcu); | |
5627 | spin_lock(&root->inode_lock); | |
5628 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5629 | spin_unlock(&root->inode_lock); | |
5630 | if (empty) | |
5631 | btrfs_add_dead_root(root); | |
5632 | } | |
5633 | } | |
5634 | ||
5635 | void btrfs_invalidate_inodes(struct btrfs_root *root) | |
5636 | { | |
5637 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5638 | struct rb_node *node; | |
5639 | struct rb_node *prev; | |
5640 | struct btrfs_inode *entry; | |
5641 | struct inode *inode; | |
5642 | u64 objectid = 0; | |
5643 | ||
5644 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
5645 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
5646 | ||
5647 | spin_lock(&root->inode_lock); | |
5648 | again: | |
5649 | node = root->inode_tree.rb_node; | |
5650 | prev = NULL; | |
5651 | while (node) { | |
5652 | prev = node; | |
5653 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5654 | ||
5655 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) | |
5656 | node = node->rb_left; | |
5657 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) | |
5658 | node = node->rb_right; | |
5659 | else | |
5660 | break; | |
5661 | } | |
5662 | if (!node) { | |
5663 | while (prev) { | |
5664 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
5665 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { | |
5666 | node = prev; | |
5667 | break; | |
5668 | } | |
5669 | prev = rb_next(prev); | |
5670 | } | |
5671 | } | |
5672 | while (node) { | |
5673 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5674 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; | |
5675 | inode = igrab(&entry->vfs_inode); | |
5676 | if (inode) { | |
5677 | spin_unlock(&root->inode_lock); | |
5678 | if (atomic_read(&inode->i_count) > 1) | |
5679 | d_prune_aliases(inode); | |
5680 | /* | |
5681 | * btrfs_drop_inode will have it removed from | |
5682 | * the inode cache when its usage count | |
5683 | * hits zero. | |
5684 | */ | |
5685 | iput(inode); | |
5686 | cond_resched(); | |
5687 | spin_lock(&root->inode_lock); | |
5688 | goto again; | |
5689 | } | |
5690 | ||
5691 | if (cond_resched_lock(&root->inode_lock)) | |
5692 | goto again; | |
5693 | ||
5694 | node = rb_next(node); | |
5695 | } | |
5696 | spin_unlock(&root->inode_lock); | |
5697 | } | |
5698 | ||
5699 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | |
5700 | { | |
5701 | struct btrfs_iget_args *args = p; | |
5702 | inode->i_ino = args->location->objectid; | |
5703 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5704 | sizeof(*args->location)); | |
5705 | BTRFS_I(inode)->root = args->root; | |
5706 | return 0; | |
5707 | } | |
5708 | ||
5709 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5710 | { | |
5711 | struct btrfs_iget_args *args = opaque; | |
5712 | return args->location->objectid == BTRFS_I(inode)->location.objectid && | |
5713 | args->root == BTRFS_I(inode)->root; | |
5714 | } | |
5715 | ||
5716 | static struct inode *btrfs_iget_locked(struct super_block *s, | |
5717 | struct btrfs_key *location, | |
5718 | struct btrfs_root *root) | |
5719 | { | |
5720 | struct inode *inode; | |
5721 | struct btrfs_iget_args args; | |
5722 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); | |
5723 | ||
5724 | args.location = location; | |
5725 | args.root = root; | |
5726 | ||
5727 | inode = iget5_locked(s, hashval, btrfs_find_actor, | |
5728 | btrfs_init_locked_inode, | |
5729 | (void *)&args); | |
5730 | return inode; | |
5731 | } | |
5732 | ||
5733 | /* Get an inode object given its location and corresponding root. | |
5734 | * Returns in *is_new if the inode was read from disk | |
5735 | */ | |
5736 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
5737 | struct btrfs_root *root, int *new) | |
5738 | { | |
5739 | struct inode *inode; | |
5740 | ||
5741 | inode = btrfs_iget_locked(s, location, root); | |
5742 | if (!inode) | |
5743 | return ERR_PTR(-ENOMEM); | |
5744 | ||
5745 | if (inode->i_state & I_NEW) { | |
5746 | int ret; | |
5747 | ||
5748 | ret = btrfs_read_locked_inode(inode); | |
5749 | if (!is_bad_inode(inode)) { | |
5750 | inode_tree_add(inode); | |
5751 | unlock_new_inode(inode); | |
5752 | if (new) | |
5753 | *new = 1; | |
5754 | } else { | |
5755 | unlock_new_inode(inode); | |
5756 | iput(inode); | |
5757 | ASSERT(ret < 0); | |
5758 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
5759 | } | |
5760 | } | |
5761 | ||
5762 | return inode; | |
5763 | } | |
5764 | ||
5765 | static struct inode *new_simple_dir(struct super_block *s, | |
5766 | struct btrfs_key *key, | |
5767 | struct btrfs_root *root) | |
5768 | { | |
5769 | struct inode *inode = new_inode(s); | |
5770 | ||
5771 | if (!inode) | |
5772 | return ERR_PTR(-ENOMEM); | |
5773 | ||
5774 | BTRFS_I(inode)->root = root; | |
5775 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
5776 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); | |
5777 | ||
5778 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
5779 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
5780 | inode->i_opflags &= ~IOP_XATTR; | |
5781 | inode->i_fop = &simple_dir_operations; | |
5782 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
5783 | inode->i_mtime = current_time(inode); | |
5784 | inode->i_atime = inode->i_mtime; | |
5785 | inode->i_ctime = inode->i_mtime; | |
5786 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
5787 | ||
5788 | return inode; | |
5789 | } | |
5790 | ||
5791 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) | |
5792 | { | |
5793 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
5794 | struct inode *inode; | |
5795 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
5796 | struct btrfs_root *sub_root = root; | |
5797 | struct btrfs_key location; | |
5798 | int index; | |
5799 | int ret = 0; | |
5800 | ||
5801 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5802 | return ERR_PTR(-ENAMETOOLONG); | |
5803 | ||
5804 | ret = btrfs_inode_by_name(dir, dentry, &location); | |
5805 | if (ret < 0) | |
5806 | return ERR_PTR(ret); | |
5807 | ||
5808 | if (location.objectid == 0) | |
5809 | return ERR_PTR(-ENOENT); | |
5810 | ||
5811 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
5812 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); | |
5813 | return inode; | |
5814 | } | |
5815 | ||
5816 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5817 | ||
5818 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
5819 | ret = fixup_tree_root_location(fs_info, dir, dentry, | |
5820 | &location, &sub_root); | |
5821 | if (ret < 0) { | |
5822 | if (ret != -ENOENT) | |
5823 | inode = ERR_PTR(ret); | |
5824 | else | |
5825 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5826 | } else { | |
5827 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); | |
5828 | } | |
5829 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
5830 | ||
5831 | if (!IS_ERR(inode) && root != sub_root) { | |
5832 | down_read(&fs_info->cleanup_work_sem); | |
5833 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
5834 | ret = btrfs_orphan_cleanup(sub_root); | |
5835 | up_read(&fs_info->cleanup_work_sem); | |
5836 | if (ret) { | |
5837 | iput(inode); | |
5838 | inode = ERR_PTR(ret); | |
5839 | } | |
5840 | } | |
5841 | ||
5842 | return inode; | |
5843 | } | |
5844 | ||
5845 | static int btrfs_dentry_delete(const struct dentry *dentry) | |
5846 | { | |
5847 | struct btrfs_root *root; | |
5848 | struct inode *inode = d_inode(dentry); | |
5849 | ||
5850 | if (!inode && !IS_ROOT(dentry)) | |
5851 | inode = d_inode(dentry->d_parent); | |
5852 | ||
5853 | if (inode) { | |
5854 | root = BTRFS_I(inode)->root; | |
5855 | if (btrfs_root_refs(&root->root_item) == 0) | |
5856 | return 1; | |
5857 | ||
5858 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5859 | return 1; | |
5860 | } | |
5861 | return 0; | |
5862 | } | |
5863 | ||
5864 | static void btrfs_dentry_release(struct dentry *dentry) | |
5865 | { | |
5866 | kfree(dentry->d_fsdata); | |
5867 | } | |
5868 | ||
5869 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | |
5870 | unsigned int flags) | |
5871 | { | |
5872 | struct inode *inode; | |
5873 | ||
5874 | inode = btrfs_lookup_dentry(dir, dentry); | |
5875 | if (IS_ERR(inode)) { | |
5876 | if (PTR_ERR(inode) == -ENOENT) | |
5877 | inode = NULL; | |
5878 | else | |
5879 | return ERR_CAST(inode); | |
5880 | } | |
5881 | ||
5882 | return d_splice_alias(inode, dentry); | |
5883 | } | |
5884 | ||
5885 | unsigned char btrfs_filetype_table[] = { | |
5886 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
5887 | }; | |
5888 | ||
5889 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) | |
5890 | { | |
5891 | struct inode *inode = file_inode(file); | |
5892 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5893 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5894 | struct btrfs_dir_item *di; | |
5895 | struct btrfs_key key; | |
5896 | struct btrfs_key found_key; | |
5897 | struct btrfs_path *path; | |
5898 | struct list_head ins_list; | |
5899 | struct list_head del_list; | |
5900 | int ret; | |
5901 | struct extent_buffer *leaf; | |
5902 | int slot; | |
5903 | unsigned char d_type; | |
5904 | int over = 0; | |
5905 | char tmp_name[32]; | |
5906 | char *name_ptr; | |
5907 | int name_len; | |
5908 | bool put = false; | |
5909 | struct btrfs_key location; | |
5910 | ||
5911 | if (!dir_emit_dots(file, ctx)) | |
5912 | return 0; | |
5913 | ||
5914 | path = btrfs_alloc_path(); | |
5915 | if (!path) | |
5916 | return -ENOMEM; | |
5917 | ||
5918 | path->reada = READA_FORWARD; | |
5919 | ||
5920 | INIT_LIST_HEAD(&ins_list); | |
5921 | INIT_LIST_HEAD(&del_list); | |
5922 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
5923 | ||
5924 | key.type = BTRFS_DIR_INDEX_KEY; | |
5925 | key.offset = ctx->pos; | |
5926 | key.objectid = btrfs_ino(BTRFS_I(inode)); | |
5927 | ||
5928 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5929 | if (ret < 0) | |
5930 | goto err; | |
5931 | ||
5932 | while (1) { | |
5933 | leaf = path->nodes[0]; | |
5934 | slot = path->slots[0]; | |
5935 | if (slot >= btrfs_header_nritems(leaf)) { | |
5936 | ret = btrfs_next_leaf(root, path); | |
5937 | if (ret < 0) | |
5938 | goto err; | |
5939 | else if (ret > 0) | |
5940 | break; | |
5941 | continue; | |
5942 | } | |
5943 | ||
5944 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
5945 | ||
5946 | if (found_key.objectid != key.objectid) | |
5947 | break; | |
5948 | if (found_key.type != BTRFS_DIR_INDEX_KEY) | |
5949 | break; | |
5950 | if (found_key.offset < ctx->pos) | |
5951 | goto next; | |
5952 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) | |
5953 | goto next; | |
5954 | ||
5955 | ctx->pos = found_key.offset; | |
5956 | ||
5957 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
5958 | if (verify_dir_item(fs_info, leaf, slot, di)) | |
5959 | goto next; | |
5960 | ||
5961 | name_len = btrfs_dir_name_len(leaf, di); | |
5962 | if (name_len <= sizeof(tmp_name)) { | |
5963 | name_ptr = tmp_name; | |
5964 | } else { | |
5965 | name_ptr = kmalloc(name_len, GFP_KERNEL); | |
5966 | if (!name_ptr) { | |
5967 | ret = -ENOMEM; | |
5968 | goto err; | |
5969 | } | |
5970 | } | |
5971 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), | |
5972 | name_len); | |
5973 | ||
5974 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5975 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
5976 | ||
5977 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, | |
5978 | d_type); | |
5979 | ||
5980 | if (name_ptr != tmp_name) | |
5981 | kfree(name_ptr); | |
5982 | ||
5983 | if (over) | |
5984 | goto nopos; | |
5985 | ctx->pos++; | |
5986 | next: | |
5987 | path->slots[0]++; | |
5988 | } | |
5989 | ||
5990 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
5991 | if (ret) | |
5992 | goto nopos; | |
5993 | ||
5994 | /* | |
5995 | * Stop new entries from being returned after we return the last | |
5996 | * entry. | |
5997 | * | |
5998 | * New directory entries are assigned a strictly increasing | |
5999 | * offset. This means that new entries created during readdir | |
6000 | * are *guaranteed* to be seen in the future by that readdir. | |
6001 | * This has broken buggy programs which operate on names as | |
6002 | * they're returned by readdir. Until we re-use freed offsets | |
6003 | * we have this hack to stop new entries from being returned | |
6004 | * under the assumption that they'll never reach this huge | |
6005 | * offset. | |
6006 | * | |
6007 | * This is being careful not to overflow 32bit loff_t unless the | |
6008 | * last entry requires it because doing so has broken 32bit apps | |
6009 | * in the past. | |
6010 | */ | |
6011 | if (ctx->pos >= INT_MAX) | |
6012 | ctx->pos = LLONG_MAX; | |
6013 | else | |
6014 | ctx->pos = INT_MAX; | |
6015 | nopos: | |
6016 | ret = 0; | |
6017 | err: | |
6018 | if (put) | |
6019 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
6020 | btrfs_free_path(path); | |
6021 | return ret; | |
6022 | } | |
6023 | ||
6024 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) | |
6025 | { | |
6026 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6027 | struct btrfs_trans_handle *trans; | |
6028 | int ret = 0; | |
6029 | bool nolock = false; | |
6030 | ||
6031 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) | |
6032 | return 0; | |
6033 | ||
6034 | if (btrfs_fs_closing(root->fs_info) && | |
6035 | btrfs_is_free_space_inode(BTRFS_I(inode))) | |
6036 | nolock = true; | |
6037 | ||
6038 | if (wbc->sync_mode == WB_SYNC_ALL) { | |
6039 | if (nolock) | |
6040 | trans = btrfs_join_transaction_nolock(root); | |
6041 | else | |
6042 | trans = btrfs_join_transaction(root); | |
6043 | if (IS_ERR(trans)) | |
6044 | return PTR_ERR(trans); | |
6045 | ret = btrfs_commit_transaction(trans); | |
6046 | } | |
6047 | return ret; | |
6048 | } | |
6049 | ||
6050 | /* | |
6051 | * This is somewhat expensive, updating the tree every time the | |
6052 | * inode changes. But, it is most likely to find the inode in cache. | |
6053 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6054 | * to keep or drop this code. | |
6055 | */ | |
6056 | static int btrfs_dirty_inode(struct inode *inode) | |
6057 | { | |
6058 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6059 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6060 | struct btrfs_trans_handle *trans; | |
6061 | int ret; | |
6062 | ||
6063 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) | |
6064 | return 0; | |
6065 | ||
6066 | trans = btrfs_join_transaction(root); | |
6067 | if (IS_ERR(trans)) | |
6068 | return PTR_ERR(trans); | |
6069 | ||
6070 | ret = btrfs_update_inode(trans, root, inode); | |
6071 | if (ret && ret == -ENOSPC) { | |
6072 | /* whoops, lets try again with the full transaction */ | |
6073 | btrfs_end_transaction(trans); | |
6074 | trans = btrfs_start_transaction(root, 1); | |
6075 | if (IS_ERR(trans)) | |
6076 | return PTR_ERR(trans); | |
6077 | ||
6078 | ret = btrfs_update_inode(trans, root, inode); | |
6079 | } | |
6080 | btrfs_end_transaction(trans); | |
6081 | if (BTRFS_I(inode)->delayed_node) | |
6082 | btrfs_balance_delayed_items(fs_info); | |
6083 | ||
6084 | return ret; | |
6085 | } | |
6086 | ||
6087 | /* | |
6088 | * This is a copy of file_update_time. We need this so we can return error on | |
6089 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6090 | */ | |
6091 | static int btrfs_update_time(struct inode *inode, struct timespec *now, | |
6092 | int flags) | |
6093 | { | |
6094 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6095 | ||
6096 | if (btrfs_root_readonly(root)) | |
6097 | return -EROFS; | |
6098 | ||
6099 | if (flags & S_VERSION) | |
6100 | inode_inc_iversion(inode); | |
6101 | if (flags & S_CTIME) | |
6102 | inode->i_ctime = *now; | |
6103 | if (flags & S_MTIME) | |
6104 | inode->i_mtime = *now; | |
6105 | if (flags & S_ATIME) | |
6106 | inode->i_atime = *now; | |
6107 | return btrfs_dirty_inode(inode); | |
6108 | } | |
6109 | ||
6110 | /* | |
6111 | * find the highest existing sequence number in a directory | |
6112 | * and then set the in-memory index_cnt variable to reflect | |
6113 | * free sequence numbers | |
6114 | */ | |
6115 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) | |
6116 | { | |
6117 | struct btrfs_root *root = inode->root; | |
6118 | struct btrfs_key key, found_key; | |
6119 | struct btrfs_path *path; | |
6120 | struct extent_buffer *leaf; | |
6121 | int ret; | |
6122 | ||
6123 | key.objectid = btrfs_ino(inode); | |
6124 | key.type = BTRFS_DIR_INDEX_KEY; | |
6125 | key.offset = (u64)-1; | |
6126 | ||
6127 | path = btrfs_alloc_path(); | |
6128 | if (!path) | |
6129 | return -ENOMEM; | |
6130 | ||
6131 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6132 | if (ret < 0) | |
6133 | goto out; | |
6134 | /* FIXME: we should be able to handle this */ | |
6135 | if (ret == 0) | |
6136 | goto out; | |
6137 | ret = 0; | |
6138 | ||
6139 | /* | |
6140 | * MAGIC NUMBER EXPLANATION: | |
6141 | * since we search a directory based on f_pos we have to start at 2 | |
6142 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6143 | * else has to start at 2 | |
6144 | */ | |
6145 | if (path->slots[0] == 0) { | |
6146 | inode->index_cnt = 2; | |
6147 | goto out; | |
6148 | } | |
6149 | ||
6150 | path->slots[0]--; | |
6151 | ||
6152 | leaf = path->nodes[0]; | |
6153 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6154 | ||
6155 | if (found_key.objectid != btrfs_ino(inode) || | |
6156 | found_key.type != BTRFS_DIR_INDEX_KEY) { | |
6157 | inode->index_cnt = 2; | |
6158 | goto out; | |
6159 | } | |
6160 | ||
6161 | inode->index_cnt = found_key.offset + 1; | |
6162 | out: | |
6163 | btrfs_free_path(path); | |
6164 | return ret; | |
6165 | } | |
6166 | ||
6167 | /* | |
6168 | * helper to find a free sequence number in a given directory. This current | |
6169 | * code is very simple, later versions will do smarter things in the btree | |
6170 | */ | |
6171 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) | |
6172 | { | |
6173 | int ret = 0; | |
6174 | ||
6175 | if (dir->index_cnt == (u64)-1) { | |
6176 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
6177 | if (ret) { | |
6178 | ret = btrfs_set_inode_index_count(dir); | |
6179 | if (ret) | |
6180 | return ret; | |
6181 | } | |
6182 | } | |
6183 | ||
6184 | *index = dir->index_cnt; | |
6185 | dir->index_cnt++; | |
6186 | ||
6187 | return ret; | |
6188 | } | |
6189 | ||
6190 | static int btrfs_insert_inode_locked(struct inode *inode) | |
6191 | { | |
6192 | struct btrfs_iget_args args; | |
6193 | args.location = &BTRFS_I(inode)->location; | |
6194 | args.root = BTRFS_I(inode)->root; | |
6195 | ||
6196 | return insert_inode_locked4(inode, | |
6197 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6198 | btrfs_find_actor, &args); | |
6199 | } | |
6200 | ||
6201 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |
6202 | struct btrfs_root *root, | |
6203 | struct inode *dir, | |
6204 | const char *name, int name_len, | |
6205 | u64 ref_objectid, u64 objectid, | |
6206 | umode_t mode, u64 *index) | |
6207 | { | |
6208 | struct btrfs_fs_info *fs_info = root->fs_info; | |
6209 | struct inode *inode; | |
6210 | struct btrfs_inode_item *inode_item; | |
6211 | struct btrfs_key *location; | |
6212 | struct btrfs_path *path; | |
6213 | struct btrfs_inode_ref *ref; | |
6214 | struct btrfs_key key[2]; | |
6215 | u32 sizes[2]; | |
6216 | int nitems = name ? 2 : 1; | |
6217 | unsigned long ptr; | |
6218 | int ret; | |
6219 | ||
6220 | path = btrfs_alloc_path(); | |
6221 | if (!path) | |
6222 | return ERR_PTR(-ENOMEM); | |
6223 | ||
6224 | inode = new_inode(fs_info->sb); | |
6225 | if (!inode) { | |
6226 | btrfs_free_path(path); | |
6227 | return ERR_PTR(-ENOMEM); | |
6228 | } | |
6229 | ||
6230 | /* | |
6231 | * O_TMPFILE, set link count to 0, so that after this point, | |
6232 | * we fill in an inode item with the correct link count. | |
6233 | */ | |
6234 | if (!name) | |
6235 | set_nlink(inode, 0); | |
6236 | ||
6237 | /* | |
6238 | * we have to initialize this early, so we can reclaim the inode | |
6239 | * number if we fail afterwards in this function. | |
6240 | */ | |
6241 | inode->i_ino = objectid; | |
6242 | ||
6243 | if (dir && name) { | |
6244 | trace_btrfs_inode_request(dir); | |
6245 | ||
6246 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); | |
6247 | if (ret) { | |
6248 | btrfs_free_path(path); | |
6249 | iput(inode); | |
6250 | return ERR_PTR(ret); | |
6251 | } | |
6252 | } else if (dir) { | |
6253 | *index = 0; | |
6254 | } | |
6255 | /* | |
6256 | * index_cnt is ignored for everything but a dir, | |
6257 | * btrfs_get_inode_index_count has an explanation for the magic | |
6258 | * number | |
6259 | */ | |
6260 | BTRFS_I(inode)->index_cnt = 2; | |
6261 | BTRFS_I(inode)->dir_index = *index; | |
6262 | BTRFS_I(inode)->root = root; | |
6263 | BTRFS_I(inode)->generation = trans->transid; | |
6264 | inode->i_generation = BTRFS_I(inode)->generation; | |
6265 | ||
6266 | /* | |
6267 | * We could have gotten an inode number from somebody who was fsynced | |
6268 | * and then removed in this same transaction, so let's just set full | |
6269 | * sync since it will be a full sync anyway and this will blow away the | |
6270 | * old info in the log. | |
6271 | */ | |
6272 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6273 | ||
6274 | key[0].objectid = objectid; | |
6275 | key[0].type = BTRFS_INODE_ITEM_KEY; | |
6276 | key[0].offset = 0; | |
6277 | ||
6278 | sizes[0] = sizeof(struct btrfs_inode_item); | |
6279 | ||
6280 | if (name) { | |
6281 | /* | |
6282 | * Start new inodes with an inode_ref. This is slightly more | |
6283 | * efficient for small numbers of hard links since they will | |
6284 | * be packed into one item. Extended refs will kick in if we | |
6285 | * add more hard links than can fit in the ref item. | |
6286 | */ | |
6287 | key[1].objectid = objectid; | |
6288 | key[1].type = BTRFS_INODE_REF_KEY; | |
6289 | key[1].offset = ref_objectid; | |
6290 | ||
6291 | sizes[1] = name_len + sizeof(*ref); | |
6292 | } | |
6293 | ||
6294 | location = &BTRFS_I(inode)->location; | |
6295 | location->objectid = objectid; | |
6296 | location->offset = 0; | |
6297 | location->type = BTRFS_INODE_ITEM_KEY; | |
6298 | ||
6299 | ret = btrfs_insert_inode_locked(inode); | |
6300 | if (ret < 0) | |
6301 | goto fail; | |
6302 | ||
6303 | path->leave_spinning = 1; | |
6304 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); | |
6305 | if (ret != 0) | |
6306 | goto fail_unlock; | |
6307 | ||
6308 | inode_init_owner(inode, dir, mode); | |
6309 | inode_set_bytes(inode, 0); | |
6310 | ||
6311 | inode->i_mtime = current_time(inode); | |
6312 | inode->i_atime = inode->i_mtime; | |
6313 | inode->i_ctime = inode->i_mtime; | |
6314 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6315 | ||
6316 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
6317 | struct btrfs_inode_item); | |
6318 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, | |
6319 | sizeof(*inode_item)); | |
6320 | fill_inode_item(trans, path->nodes[0], inode_item, inode); | |
6321 | ||
6322 | if (name) { | |
6323 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6324 | struct btrfs_inode_ref); | |
6325 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6326 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6327 | ptr = (unsigned long)(ref + 1); | |
6328 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6329 | } | |
6330 | ||
6331 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
6332 | btrfs_free_path(path); | |
6333 | ||
6334 | btrfs_inherit_iflags(inode, dir); | |
6335 | ||
6336 | if (S_ISREG(mode)) { | |
6337 | if (btrfs_test_opt(fs_info, NODATASUM)) | |
6338 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6339 | if (btrfs_test_opt(fs_info, NODATACOW)) | |
6340 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | | |
6341 | BTRFS_INODE_NODATASUM; | |
6342 | } | |
6343 | ||
6344 | inode_tree_add(inode); | |
6345 | ||
6346 | trace_btrfs_inode_new(inode); | |
6347 | btrfs_set_inode_last_trans(trans, inode); | |
6348 | ||
6349 | btrfs_update_root_times(trans, root); | |
6350 | ||
6351 | ret = btrfs_inode_inherit_props(trans, inode, dir); | |
6352 | if (ret) | |
6353 | btrfs_err(fs_info, | |
6354 | "error inheriting props for ino %llu (root %llu): %d", | |
6355 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); | |
6356 | ||
6357 | return inode; | |
6358 | ||
6359 | fail_unlock: | |
6360 | unlock_new_inode(inode); | |
6361 | fail: | |
6362 | if (dir && name) | |
6363 | BTRFS_I(dir)->index_cnt--; | |
6364 | btrfs_free_path(path); | |
6365 | iput(inode); | |
6366 | return ERR_PTR(ret); | |
6367 | } | |
6368 | ||
6369 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6370 | { | |
6371 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6372 | } | |
6373 | ||
6374 | /* | |
6375 | * utility function to add 'inode' into 'parent_inode' with | |
6376 | * a give name and a given sequence number. | |
6377 | * if 'add_backref' is true, also insert a backref from the | |
6378 | * inode to the parent directory. | |
6379 | */ | |
6380 | int btrfs_add_link(struct btrfs_trans_handle *trans, | |
6381 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, | |
6382 | const char *name, int name_len, int add_backref, u64 index) | |
6383 | { | |
6384 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); | |
6385 | int ret = 0; | |
6386 | struct btrfs_key key; | |
6387 | struct btrfs_root *root = parent_inode->root; | |
6388 | u64 ino = btrfs_ino(inode); | |
6389 | u64 parent_ino = btrfs_ino(parent_inode); | |
6390 | ||
6391 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6392 | memcpy(&key, &inode->root->root_key, sizeof(key)); | |
6393 | } else { | |
6394 | key.objectid = ino; | |
6395 | key.type = BTRFS_INODE_ITEM_KEY; | |
6396 | key.offset = 0; | |
6397 | } | |
6398 | ||
6399 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6400 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, | |
6401 | root->root_key.objectid, parent_ino, | |
6402 | index, name, name_len); | |
6403 | } else if (add_backref) { | |
6404 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, | |
6405 | parent_ino, index); | |
6406 | } | |
6407 | ||
6408 | /* Nothing to clean up yet */ | |
6409 | if (ret) | |
6410 | return ret; | |
6411 | ||
6412 | ret = btrfs_insert_dir_item(trans, root, name, name_len, | |
6413 | parent_inode, &key, | |
6414 | btrfs_inode_type(&inode->vfs_inode), index); | |
6415 | if (ret == -EEXIST || ret == -EOVERFLOW) | |
6416 | goto fail_dir_item; | |
6417 | else if (ret) { | |
6418 | btrfs_abort_transaction(trans, ret); | |
6419 | return ret; | |
6420 | } | |
6421 | ||
6422 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + | |
6423 | name_len * 2); | |
6424 | inode_inc_iversion(&parent_inode->vfs_inode); | |
6425 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6426 | current_time(&parent_inode->vfs_inode); | |
6427 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
6428 | if (ret) | |
6429 | btrfs_abort_transaction(trans, ret); | |
6430 | return ret; | |
6431 | ||
6432 | fail_dir_item: | |
6433 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6434 | u64 local_index; | |
6435 | int err; | |
6436 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, | |
6437 | root->root_key.objectid, parent_ino, | |
6438 | &local_index, name, name_len); | |
6439 | ||
6440 | } else if (add_backref) { | |
6441 | u64 local_index; | |
6442 | int err; | |
6443 | ||
6444 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6445 | ino, parent_ino, &local_index); | |
6446 | } | |
6447 | return ret; | |
6448 | } | |
6449 | ||
6450 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
6451 | struct btrfs_inode *dir, struct dentry *dentry, | |
6452 | struct btrfs_inode *inode, int backref, u64 index) | |
6453 | { | |
6454 | int err = btrfs_add_link(trans, dir, inode, | |
6455 | dentry->d_name.name, dentry->d_name.len, | |
6456 | backref, index); | |
6457 | if (err > 0) | |
6458 | err = -EEXIST; | |
6459 | return err; | |
6460 | } | |
6461 | ||
6462 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, | |
6463 | umode_t mode, dev_t rdev) | |
6464 | { | |
6465 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
6466 | struct btrfs_trans_handle *trans; | |
6467 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6468 | struct inode *inode = NULL; | |
6469 | int err; | |
6470 | int drop_inode = 0; | |
6471 | u64 objectid; | |
6472 | u64 index = 0; | |
6473 | ||
6474 | /* | |
6475 | * 2 for inode item and ref | |
6476 | * 2 for dir items | |
6477 | * 1 for xattr if selinux is on | |
6478 | */ | |
6479 | trans = btrfs_start_transaction(root, 5); | |
6480 | if (IS_ERR(trans)) | |
6481 | return PTR_ERR(trans); | |
6482 | ||
6483 | err = btrfs_find_free_ino(root, &objectid); | |
6484 | if (err) | |
6485 | goto out_unlock; | |
6486 | ||
6487 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, | |
6488 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, | |
6489 | mode, &index); | |
6490 | if (IS_ERR(inode)) { | |
6491 | err = PTR_ERR(inode); | |
6492 | goto out_unlock; | |
6493 | } | |
6494 | ||
6495 | /* | |
6496 | * If the active LSM wants to access the inode during | |
6497 | * d_instantiate it needs these. Smack checks to see | |
6498 | * if the filesystem supports xattrs by looking at the | |
6499 | * ops vector. | |
6500 | */ | |
6501 | inode->i_op = &btrfs_special_inode_operations; | |
6502 | init_special_inode(inode, inode->i_mode, rdev); | |
6503 | ||
6504 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6505 | if (err) | |
6506 | goto out_unlock_inode; | |
6507 | ||
6508 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), | |
6509 | 0, index); | |
6510 | if (err) { | |
6511 | goto out_unlock_inode; | |
6512 | } else { | |
6513 | btrfs_update_inode(trans, root, inode); | |
6514 | unlock_new_inode(inode); | |
6515 | d_instantiate(dentry, inode); | |
6516 | } | |
6517 | ||
6518 | out_unlock: | |
6519 | btrfs_end_transaction(trans); | |
6520 | btrfs_balance_delayed_items(fs_info); | |
6521 | btrfs_btree_balance_dirty(fs_info); | |
6522 | if (drop_inode) { | |
6523 | inode_dec_link_count(inode); | |
6524 | iput(inode); | |
6525 | } | |
6526 | return err; | |
6527 | ||
6528 | out_unlock_inode: | |
6529 | drop_inode = 1; | |
6530 | unlock_new_inode(inode); | |
6531 | goto out_unlock; | |
6532 | ||
6533 | } | |
6534 | ||
6535 | static int btrfs_create(struct inode *dir, struct dentry *dentry, | |
6536 | umode_t mode, bool excl) | |
6537 | { | |
6538 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
6539 | struct btrfs_trans_handle *trans; | |
6540 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6541 | struct inode *inode = NULL; | |
6542 | int drop_inode_on_err = 0; | |
6543 | int err; | |
6544 | u64 objectid; | |
6545 | u64 index = 0; | |
6546 | ||
6547 | /* | |
6548 | * 2 for inode item and ref | |
6549 | * 2 for dir items | |
6550 | * 1 for xattr if selinux is on | |
6551 | */ | |
6552 | trans = btrfs_start_transaction(root, 5); | |
6553 | if (IS_ERR(trans)) | |
6554 | return PTR_ERR(trans); | |
6555 | ||
6556 | err = btrfs_find_free_ino(root, &objectid); | |
6557 | if (err) | |
6558 | goto out_unlock; | |
6559 | ||
6560 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, | |
6561 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, | |
6562 | mode, &index); | |
6563 | if (IS_ERR(inode)) { | |
6564 | err = PTR_ERR(inode); | |
6565 | goto out_unlock; | |
6566 | } | |
6567 | drop_inode_on_err = 1; | |
6568 | /* | |
6569 | * If the active LSM wants to access the inode during | |
6570 | * d_instantiate it needs these. Smack checks to see | |
6571 | * if the filesystem supports xattrs by looking at the | |
6572 | * ops vector. | |
6573 | */ | |
6574 | inode->i_fop = &btrfs_file_operations; | |
6575 | inode->i_op = &btrfs_file_inode_operations; | |
6576 | inode->i_mapping->a_ops = &btrfs_aops; | |
6577 | ||
6578 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6579 | if (err) | |
6580 | goto out_unlock_inode; | |
6581 | ||
6582 | err = btrfs_update_inode(trans, root, inode); | |
6583 | if (err) | |
6584 | goto out_unlock_inode; | |
6585 | ||
6586 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), | |
6587 | 0, index); | |
6588 | if (err) | |
6589 | goto out_unlock_inode; | |
6590 | ||
6591 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
6592 | unlock_new_inode(inode); | |
6593 | d_instantiate(dentry, inode); | |
6594 | ||
6595 | out_unlock: | |
6596 | btrfs_end_transaction(trans); | |
6597 | if (err && drop_inode_on_err) { | |
6598 | inode_dec_link_count(inode); | |
6599 | iput(inode); | |
6600 | } | |
6601 | btrfs_balance_delayed_items(fs_info); | |
6602 | btrfs_btree_balance_dirty(fs_info); | |
6603 | return err; | |
6604 | ||
6605 | out_unlock_inode: | |
6606 | unlock_new_inode(inode); | |
6607 | goto out_unlock; | |
6608 | ||
6609 | } | |
6610 | ||
6611 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6612 | struct dentry *dentry) | |
6613 | { | |
6614 | struct btrfs_trans_handle *trans = NULL; | |
6615 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6616 | struct inode *inode = d_inode(old_dentry); | |
6617 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6618 | u64 index; | |
6619 | int err; | |
6620 | int drop_inode = 0; | |
6621 | ||
6622 | /* do not allow sys_link's with other subvols of the same device */ | |
6623 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
6624 | return -EXDEV; | |
6625 | ||
6626 | if (inode->i_nlink >= BTRFS_LINK_MAX) | |
6627 | return -EMLINK; | |
6628 | ||
6629 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); | |
6630 | if (err) | |
6631 | goto fail; | |
6632 | ||
6633 | /* | |
6634 | * 2 items for inode and inode ref | |
6635 | * 2 items for dir items | |
6636 | * 1 item for parent inode | |
6637 | */ | |
6638 | trans = btrfs_start_transaction(root, 5); | |
6639 | if (IS_ERR(trans)) { | |
6640 | err = PTR_ERR(trans); | |
6641 | trans = NULL; | |
6642 | goto fail; | |
6643 | } | |
6644 | ||
6645 | /* There are several dir indexes for this inode, clear the cache. */ | |
6646 | BTRFS_I(inode)->dir_index = 0ULL; | |
6647 | inc_nlink(inode); | |
6648 | inode_inc_iversion(inode); | |
6649 | inode->i_ctime = current_time(inode); | |
6650 | ihold(inode); | |
6651 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); | |
6652 | ||
6653 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), | |
6654 | 1, index); | |
6655 | ||
6656 | if (err) { | |
6657 | drop_inode = 1; | |
6658 | } else { | |
6659 | struct dentry *parent = dentry->d_parent; | |
6660 | err = btrfs_update_inode(trans, root, inode); | |
6661 | if (err) | |
6662 | goto fail; | |
6663 | if (inode->i_nlink == 1) { | |
6664 | /* | |
6665 | * If new hard link count is 1, it's a file created | |
6666 | * with open(2) O_TMPFILE flag. | |
6667 | */ | |
6668 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); | |
6669 | if (err) | |
6670 | goto fail; | |
6671 | } | |
6672 | d_instantiate(dentry, inode); | |
6673 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); | |
6674 | } | |
6675 | ||
6676 | btrfs_balance_delayed_items(fs_info); | |
6677 | fail: | |
6678 | if (trans) | |
6679 | btrfs_end_transaction(trans); | |
6680 | if (drop_inode) { | |
6681 | inode_dec_link_count(inode); | |
6682 | iput(inode); | |
6683 | } | |
6684 | btrfs_btree_balance_dirty(fs_info); | |
6685 | return err; | |
6686 | } | |
6687 | ||
6688 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) | |
6689 | { | |
6690 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
6691 | struct inode *inode = NULL; | |
6692 | struct btrfs_trans_handle *trans; | |
6693 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6694 | int err = 0; | |
6695 | int drop_on_err = 0; | |
6696 | u64 objectid = 0; | |
6697 | u64 index = 0; | |
6698 | ||
6699 | /* | |
6700 | * 2 items for inode and ref | |
6701 | * 2 items for dir items | |
6702 | * 1 for xattr if selinux is on | |
6703 | */ | |
6704 | trans = btrfs_start_transaction(root, 5); | |
6705 | if (IS_ERR(trans)) | |
6706 | return PTR_ERR(trans); | |
6707 | ||
6708 | err = btrfs_find_free_ino(root, &objectid); | |
6709 | if (err) | |
6710 | goto out_fail; | |
6711 | ||
6712 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, | |
6713 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, | |
6714 | S_IFDIR | mode, &index); | |
6715 | if (IS_ERR(inode)) { | |
6716 | err = PTR_ERR(inode); | |
6717 | goto out_fail; | |
6718 | } | |
6719 | ||
6720 | drop_on_err = 1; | |
6721 | /* these must be set before we unlock the inode */ | |
6722 | inode->i_op = &btrfs_dir_inode_operations; | |
6723 | inode->i_fop = &btrfs_dir_file_operations; | |
6724 | ||
6725 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6726 | if (err) | |
6727 | goto out_fail_inode; | |
6728 | ||
6729 | btrfs_i_size_write(BTRFS_I(inode), 0); | |
6730 | err = btrfs_update_inode(trans, root, inode); | |
6731 | if (err) | |
6732 | goto out_fail_inode; | |
6733 | ||
6734 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), | |
6735 | dentry->d_name.name, | |
6736 | dentry->d_name.len, 0, index); | |
6737 | if (err) | |
6738 | goto out_fail_inode; | |
6739 | ||
6740 | d_instantiate(dentry, inode); | |
6741 | /* | |
6742 | * mkdir is special. We're unlocking after we call d_instantiate | |
6743 | * to avoid a race with nfsd calling d_instantiate. | |
6744 | */ | |
6745 | unlock_new_inode(inode); | |
6746 | drop_on_err = 0; | |
6747 | ||
6748 | out_fail: | |
6749 | btrfs_end_transaction(trans); | |
6750 | if (drop_on_err) { | |
6751 | inode_dec_link_count(inode); | |
6752 | iput(inode); | |
6753 | } | |
6754 | btrfs_balance_delayed_items(fs_info); | |
6755 | btrfs_btree_balance_dirty(fs_info); | |
6756 | return err; | |
6757 | ||
6758 | out_fail_inode: | |
6759 | unlock_new_inode(inode); | |
6760 | goto out_fail; | |
6761 | } | |
6762 | ||
6763 | /* Find next extent map of a given extent map, caller needs to ensure locks */ | |
6764 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6765 | { | |
6766 | struct rb_node *next; | |
6767 | ||
6768 | next = rb_next(&em->rb_node); | |
6769 | if (!next) | |
6770 | return NULL; | |
6771 | return container_of(next, struct extent_map, rb_node); | |
6772 | } | |
6773 | ||
6774 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6775 | { | |
6776 | struct rb_node *prev; | |
6777 | ||
6778 | prev = rb_prev(&em->rb_node); | |
6779 | if (!prev) | |
6780 | return NULL; | |
6781 | return container_of(prev, struct extent_map, rb_node); | |
6782 | } | |
6783 | ||
6784 | /* helper for btfs_get_extent. Given an existing extent in the tree, | |
6785 | * the existing extent is the nearest extent to map_start, | |
6786 | * and an extent that you want to insert, deal with overlap and insert | |
6787 | * the best fitted new extent into the tree. | |
6788 | */ | |
6789 | static int merge_extent_mapping(struct extent_map_tree *em_tree, | |
6790 | struct extent_map *existing, | |
6791 | struct extent_map *em, | |
6792 | u64 map_start) | |
6793 | { | |
6794 | struct extent_map *prev; | |
6795 | struct extent_map *next; | |
6796 | u64 start; | |
6797 | u64 end; | |
6798 | u64 start_diff; | |
6799 | ||
6800 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); | |
6801 | ||
6802 | if (existing->start > map_start) { | |
6803 | next = existing; | |
6804 | prev = prev_extent_map(next); | |
6805 | } else { | |
6806 | prev = existing; | |
6807 | next = next_extent_map(prev); | |
6808 | } | |
6809 | ||
6810 | start = prev ? extent_map_end(prev) : em->start; | |
6811 | start = max_t(u64, start, em->start); | |
6812 | end = next ? next->start : extent_map_end(em); | |
6813 | end = min_t(u64, end, extent_map_end(em)); | |
6814 | start_diff = start - em->start; | |
6815 | em->start = start; | |
6816 | em->len = end - start; | |
6817 | if (em->block_start < EXTENT_MAP_LAST_BYTE && | |
6818 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
6819 | em->block_start += start_diff; | |
6820 | em->block_len -= start_diff; | |
6821 | } | |
6822 | return add_extent_mapping(em_tree, em, 0); | |
6823 | } | |
6824 | ||
6825 | static noinline int uncompress_inline(struct btrfs_path *path, | |
6826 | struct page *page, | |
6827 | size_t pg_offset, u64 extent_offset, | |
6828 | struct btrfs_file_extent_item *item) | |
6829 | { | |
6830 | int ret; | |
6831 | struct extent_buffer *leaf = path->nodes[0]; | |
6832 | char *tmp; | |
6833 | size_t max_size; | |
6834 | unsigned long inline_size; | |
6835 | unsigned long ptr; | |
6836 | int compress_type; | |
6837 | ||
6838 | WARN_ON(pg_offset != 0); | |
6839 | compress_type = btrfs_file_extent_compression(leaf, item); | |
6840 | max_size = btrfs_file_extent_ram_bytes(leaf, item); | |
6841 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
6842 | btrfs_item_nr(path->slots[0])); | |
6843 | tmp = kmalloc(inline_size, GFP_NOFS); | |
6844 | if (!tmp) | |
6845 | return -ENOMEM; | |
6846 | ptr = btrfs_file_extent_inline_start(item); | |
6847 | ||
6848 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6849 | ||
6850 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); | |
6851 | ret = btrfs_decompress(compress_type, tmp, page, | |
6852 | extent_offset, inline_size, max_size); | |
6853 | ||
6854 | /* | |
6855 | * decompression code contains a memset to fill in any space between the end | |
6856 | * of the uncompressed data and the end of max_size in case the decompressed | |
6857 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6858 | * the end of an inline extent and the beginning of the next block, so we | |
6859 | * cover that region here. | |
6860 | */ | |
6861 | ||
6862 | if (max_size + pg_offset < PAGE_SIZE) { | |
6863 | char *map = kmap(page); | |
6864 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6865 | kunmap(page); | |
6866 | } | |
6867 | kfree(tmp); | |
6868 | return ret; | |
6869 | } | |
6870 | ||
6871 | /* | |
6872 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
6873 | * the ugly parts come from merging extents from the disk with the in-ram | |
6874 | * representation. This gets more complex because of the data=ordered code, | |
6875 | * where the in-ram extents might be locked pending data=ordered completion. | |
6876 | * | |
6877 | * This also copies inline extents directly into the page. | |
6878 | */ | |
6879 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, | |
6880 | struct page *page, | |
6881 | size_t pg_offset, u64 start, u64 len, | |
6882 | int create) | |
6883 | { | |
6884 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); | |
6885 | int ret; | |
6886 | int err = 0; | |
6887 | u64 extent_start = 0; | |
6888 | u64 extent_end = 0; | |
6889 | u64 objectid = btrfs_ino(inode); | |
6890 | u32 found_type; | |
6891 | struct btrfs_path *path = NULL; | |
6892 | struct btrfs_root *root = inode->root; | |
6893 | struct btrfs_file_extent_item *item; | |
6894 | struct extent_buffer *leaf; | |
6895 | struct btrfs_key found_key; | |
6896 | struct extent_map *em = NULL; | |
6897 | struct extent_map_tree *em_tree = &inode->extent_tree; | |
6898 | struct extent_io_tree *io_tree = &inode->io_tree; | |
6899 | struct btrfs_trans_handle *trans = NULL; | |
6900 | const bool new_inline = !page || create; | |
6901 | ||
6902 | again: | |
6903 | read_lock(&em_tree->lock); | |
6904 | em = lookup_extent_mapping(em_tree, start, len); | |
6905 | if (em) | |
6906 | em->bdev = fs_info->fs_devices->latest_bdev; | |
6907 | read_unlock(&em_tree->lock); | |
6908 | ||
6909 | if (em) { | |
6910 | if (em->start > start || em->start + em->len <= start) | |
6911 | free_extent_map(em); | |
6912 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
6913 | free_extent_map(em); | |
6914 | else | |
6915 | goto out; | |
6916 | } | |
6917 | em = alloc_extent_map(); | |
6918 | if (!em) { | |
6919 | err = -ENOMEM; | |
6920 | goto out; | |
6921 | } | |
6922 | em->bdev = fs_info->fs_devices->latest_bdev; | |
6923 | em->start = EXTENT_MAP_HOLE; | |
6924 | em->orig_start = EXTENT_MAP_HOLE; | |
6925 | em->len = (u64)-1; | |
6926 | em->block_len = (u64)-1; | |
6927 | ||
6928 | if (!path) { | |
6929 | path = btrfs_alloc_path(); | |
6930 | if (!path) { | |
6931 | err = -ENOMEM; | |
6932 | goto out; | |
6933 | } | |
6934 | /* | |
6935 | * Chances are we'll be called again, so go ahead and do | |
6936 | * readahead | |
6937 | */ | |
6938 | path->reada = READA_FORWARD; | |
6939 | } | |
6940 | ||
6941 | ret = btrfs_lookup_file_extent(trans, root, path, | |
6942 | objectid, start, trans != NULL); | |
6943 | if (ret < 0) { | |
6944 | err = ret; | |
6945 | goto out; | |
6946 | } | |
6947 | ||
6948 | if (ret != 0) { | |
6949 | if (path->slots[0] == 0) | |
6950 | goto not_found; | |
6951 | path->slots[0]--; | |
6952 | } | |
6953 | ||
6954 | leaf = path->nodes[0]; | |
6955 | item = btrfs_item_ptr(leaf, path->slots[0], | |
6956 | struct btrfs_file_extent_item); | |
6957 | /* are we inside the extent that was found? */ | |
6958 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6959 | found_type = found_key.type; | |
6960 | if (found_key.objectid != objectid || | |
6961 | found_type != BTRFS_EXTENT_DATA_KEY) { | |
6962 | /* | |
6963 | * If we backup past the first extent we want to move forward | |
6964 | * and see if there is an extent in front of us, otherwise we'll | |
6965 | * say there is a hole for our whole search range which can | |
6966 | * cause problems. | |
6967 | */ | |
6968 | extent_end = start; | |
6969 | goto next; | |
6970 | } | |
6971 | ||
6972 | found_type = btrfs_file_extent_type(leaf, item); | |
6973 | extent_start = found_key.offset; | |
6974 | if (found_type == BTRFS_FILE_EXTENT_REG || | |
6975 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6976 | extent_end = extent_start + | |
6977 | btrfs_file_extent_num_bytes(leaf, item); | |
6978 | ||
6979 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
6980 | extent_start); | |
6981 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
6982 | size_t size; | |
6983 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); | |
6984 | extent_end = ALIGN(extent_start + size, | |
6985 | fs_info->sectorsize); | |
6986 | ||
6987 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
6988 | path->slots[0], | |
6989 | extent_start); | |
6990 | } | |
6991 | next: | |
6992 | if (start >= extent_end) { | |
6993 | path->slots[0]++; | |
6994 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6995 | ret = btrfs_next_leaf(root, path); | |
6996 | if (ret < 0) { | |
6997 | err = ret; | |
6998 | goto out; | |
6999 | } | |
7000 | if (ret > 0) | |
7001 | goto not_found; | |
7002 | leaf = path->nodes[0]; | |
7003 | } | |
7004 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
7005 | if (found_key.objectid != objectid || | |
7006 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
7007 | goto not_found; | |
7008 | if (start + len <= found_key.offset) | |
7009 | goto not_found; | |
7010 | if (start > found_key.offset) | |
7011 | goto next; | |
7012 | em->start = start; | |
7013 | em->orig_start = start; | |
7014 | em->len = found_key.offset - start; | |
7015 | goto not_found_em; | |
7016 | } | |
7017 | ||
7018 | btrfs_extent_item_to_extent_map(inode, path, item, | |
7019 | new_inline, em); | |
7020 | ||
7021 | if (found_type == BTRFS_FILE_EXTENT_REG || | |
7022 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7023 | goto insert; | |
7024 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
7025 | unsigned long ptr; | |
7026 | char *map; | |
7027 | size_t size; | |
7028 | size_t extent_offset; | |
7029 | size_t copy_size; | |
7030 | ||
7031 | if (new_inline) | |
7032 | goto out; | |
7033 | ||
7034 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); | |
7035 | extent_offset = page_offset(page) + pg_offset - extent_start; | |
7036 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, | |
7037 | size - extent_offset); | |
7038 | em->start = extent_start + extent_offset; | |
7039 | em->len = ALIGN(copy_size, fs_info->sectorsize); | |
7040 | em->orig_block_len = em->len; | |
7041 | em->orig_start = em->start; | |
7042 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; | |
7043 | if (create == 0 && !PageUptodate(page)) { | |
7044 | if (btrfs_file_extent_compression(leaf, item) != | |
7045 | BTRFS_COMPRESS_NONE) { | |
7046 | ret = uncompress_inline(path, page, pg_offset, | |
7047 | extent_offset, item); | |
7048 | if (ret) { | |
7049 | err = ret; | |
7050 | goto out; | |
7051 | } | |
7052 | } else { | |
7053 | map = kmap(page); | |
7054 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7055 | copy_size); | |
7056 | if (pg_offset + copy_size < PAGE_SIZE) { | |
7057 | memset(map + pg_offset + copy_size, 0, | |
7058 | PAGE_SIZE - pg_offset - | |
7059 | copy_size); | |
7060 | } | |
7061 | kunmap(page); | |
7062 | } | |
7063 | flush_dcache_page(page); | |
7064 | } else if (create && PageUptodate(page)) { | |
7065 | BUG(); | |
7066 | if (!trans) { | |
7067 | kunmap(page); | |
7068 | free_extent_map(em); | |
7069 | em = NULL; | |
7070 | ||
7071 | btrfs_release_path(path); | |
7072 | trans = btrfs_join_transaction(root); | |
7073 | ||
7074 | if (IS_ERR(trans)) | |
7075 | return ERR_CAST(trans); | |
7076 | goto again; | |
7077 | } | |
7078 | map = kmap(page); | |
7079 | write_extent_buffer(leaf, map + pg_offset, ptr, | |
7080 | copy_size); | |
7081 | kunmap(page); | |
7082 | btrfs_mark_buffer_dirty(leaf); | |
7083 | } | |
7084 | set_extent_uptodate(io_tree, em->start, | |
7085 | extent_map_end(em) - 1, NULL, GFP_NOFS); | |
7086 | goto insert; | |
7087 | } | |
7088 | not_found: | |
7089 | em->start = start; | |
7090 | em->orig_start = start; | |
7091 | em->len = len; | |
7092 | not_found_em: | |
7093 | em->block_start = EXTENT_MAP_HOLE; | |
7094 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); | |
7095 | insert: | |
7096 | btrfs_release_path(path); | |
7097 | if (em->start > start || extent_map_end(em) <= start) { | |
7098 | btrfs_err(fs_info, | |
7099 | "bad extent! em: [%llu %llu] passed [%llu %llu]", | |
7100 | em->start, em->len, start, len); | |
7101 | err = -EIO; | |
7102 | goto out; | |
7103 | } | |
7104 | ||
7105 | err = 0; | |
7106 | write_lock(&em_tree->lock); | |
7107 | ret = add_extent_mapping(em_tree, em, 0); | |
7108 | /* it is possible that someone inserted the extent into the tree | |
7109 | * while we had the lock dropped. It is also possible that | |
7110 | * an overlapping map exists in the tree | |
7111 | */ | |
7112 | if (ret == -EEXIST) { | |
7113 | struct extent_map *existing; | |
7114 | ||
7115 | ret = 0; | |
7116 | ||
7117 | existing = search_extent_mapping(em_tree, start, len); | |
7118 | /* | |
7119 | * existing will always be non-NULL, since there must be | |
7120 | * extent causing the -EEXIST. | |
7121 | */ | |
7122 | if (existing->start == em->start && | |
7123 | extent_map_end(existing) >= extent_map_end(em) && | |
7124 | em->block_start == existing->block_start) { | |
7125 | /* | |
7126 | * The existing extent map already encompasses the | |
7127 | * entire extent map we tried to add. | |
7128 | */ | |
7129 | free_extent_map(em); | |
7130 | em = existing; | |
7131 | err = 0; | |
7132 | ||
7133 | } else if (start >= extent_map_end(existing) || | |
7134 | start <= existing->start) { | |
7135 | /* | |
7136 | * The existing extent map is the one nearest to | |
7137 | * the [start, start + len) range which overlaps | |
7138 | */ | |
7139 | err = merge_extent_mapping(em_tree, existing, | |
7140 | em, start); | |
7141 | free_extent_map(existing); | |
7142 | if (err) { | |
7143 | free_extent_map(em); | |
7144 | em = NULL; | |
7145 | } | |
7146 | } else { | |
7147 | free_extent_map(em); | |
7148 | em = existing; | |
7149 | err = 0; | |
7150 | } | |
7151 | } | |
7152 | write_unlock(&em_tree->lock); | |
7153 | out: | |
7154 | ||
7155 | trace_btrfs_get_extent(root, inode, em); | |
7156 | ||
7157 | btrfs_free_path(path); | |
7158 | if (trans) { | |
7159 | ret = btrfs_end_transaction(trans); | |
7160 | if (!err) | |
7161 | err = ret; | |
7162 | } | |
7163 | if (err) { | |
7164 | free_extent_map(em); | |
7165 | return ERR_PTR(err); | |
7166 | } | |
7167 | BUG_ON(!em); /* Error is always set */ | |
7168 | return em; | |
7169 | } | |
7170 | ||
7171 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, | |
7172 | struct page *page, | |
7173 | size_t pg_offset, u64 start, u64 len, | |
7174 | int create) | |
7175 | { | |
7176 | struct extent_map *em; | |
7177 | struct extent_map *hole_em = NULL; | |
7178 | u64 range_start = start; | |
7179 | u64 end; | |
7180 | u64 found; | |
7181 | u64 found_end; | |
7182 | int err = 0; | |
7183 | ||
7184 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7185 | if (IS_ERR(em)) | |
7186 | return em; | |
7187 | /* | |
7188 | * If our em maps to: | |
7189 | * - a hole or | |
7190 | * - a pre-alloc extent, | |
7191 | * there might actually be delalloc bytes behind it. | |
7192 | */ | |
7193 | if (em->block_start != EXTENT_MAP_HOLE && | |
7194 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7195 | return em; | |
7196 | else | |
7197 | hole_em = em; | |
7198 | ||
7199 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7200 | end = start + len; | |
7201 | if (end < start) | |
7202 | end = (u64)-1; | |
7203 | else | |
7204 | end -= 1; | |
7205 | ||
7206 | em = NULL; | |
7207 | ||
7208 | /* ok, we didn't find anything, lets look for delalloc */ | |
7209 | found = count_range_bits(&inode->io_tree, &range_start, | |
7210 | end, len, EXTENT_DELALLOC, 1); | |
7211 | found_end = range_start + found; | |
7212 | if (found_end < range_start) | |
7213 | found_end = (u64)-1; | |
7214 | ||
7215 | /* | |
7216 | * we didn't find anything useful, return | |
7217 | * the original results from get_extent() | |
7218 | */ | |
7219 | if (range_start > end || found_end <= start) { | |
7220 | em = hole_em; | |
7221 | hole_em = NULL; | |
7222 | goto out; | |
7223 | } | |
7224 | ||
7225 | /* adjust the range_start to make sure it doesn't | |
7226 | * go backwards from the start they passed in | |
7227 | */ | |
7228 | range_start = max(start, range_start); | |
7229 | found = found_end - range_start; | |
7230 | ||
7231 | if (found > 0) { | |
7232 | u64 hole_start = start; | |
7233 | u64 hole_len = len; | |
7234 | ||
7235 | em = alloc_extent_map(); | |
7236 | if (!em) { | |
7237 | err = -ENOMEM; | |
7238 | goto out; | |
7239 | } | |
7240 | /* | |
7241 | * when btrfs_get_extent can't find anything it | |
7242 | * returns one huge hole | |
7243 | * | |
7244 | * make sure what it found really fits our range, and | |
7245 | * adjust to make sure it is based on the start from | |
7246 | * the caller | |
7247 | */ | |
7248 | if (hole_em) { | |
7249 | u64 calc_end = extent_map_end(hole_em); | |
7250 | ||
7251 | if (calc_end <= start || (hole_em->start > end)) { | |
7252 | free_extent_map(hole_em); | |
7253 | hole_em = NULL; | |
7254 | } else { | |
7255 | hole_start = max(hole_em->start, start); | |
7256 | hole_len = calc_end - hole_start; | |
7257 | } | |
7258 | } | |
7259 | em->bdev = NULL; | |
7260 | if (hole_em && range_start > hole_start) { | |
7261 | /* our hole starts before our delalloc, so we | |
7262 | * have to return just the parts of the hole | |
7263 | * that go until the delalloc starts | |
7264 | */ | |
7265 | em->len = min(hole_len, | |
7266 | range_start - hole_start); | |
7267 | em->start = hole_start; | |
7268 | em->orig_start = hole_start; | |
7269 | /* | |
7270 | * don't adjust block start at all, | |
7271 | * it is fixed at EXTENT_MAP_HOLE | |
7272 | */ | |
7273 | em->block_start = hole_em->block_start; | |
7274 | em->block_len = hole_len; | |
7275 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) | |
7276 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
7277 | } else { | |
7278 | em->start = range_start; | |
7279 | em->len = found; | |
7280 | em->orig_start = range_start; | |
7281 | em->block_start = EXTENT_MAP_DELALLOC; | |
7282 | em->block_len = found; | |
7283 | } | |
7284 | } else if (hole_em) { | |
7285 | return hole_em; | |
7286 | } | |
7287 | out: | |
7288 | ||
7289 | free_extent_map(hole_em); | |
7290 | if (err) { | |
7291 | free_extent_map(em); | |
7292 | return ERR_PTR(err); | |
7293 | } | |
7294 | return em; | |
7295 | } | |
7296 | ||
7297 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, | |
7298 | const u64 start, | |
7299 | const u64 len, | |
7300 | const u64 orig_start, | |
7301 | const u64 block_start, | |
7302 | const u64 block_len, | |
7303 | const u64 orig_block_len, | |
7304 | const u64 ram_bytes, | |
7305 | const int type) | |
7306 | { | |
7307 | struct extent_map *em = NULL; | |
7308 | int ret; | |
7309 | ||
7310 | if (type != BTRFS_ORDERED_NOCOW) { | |
7311 | em = create_io_em(inode, start, len, orig_start, | |
7312 | block_start, block_len, orig_block_len, | |
7313 | ram_bytes, | |
7314 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7315 | type); | |
7316 | if (IS_ERR(em)) | |
7317 | goto out; | |
7318 | } | |
7319 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7320 | len, block_len, type); | |
7321 | if (ret) { | |
7322 | if (em) { | |
7323 | free_extent_map(em); | |
7324 | btrfs_drop_extent_cache(BTRFS_I(inode), start, | |
7325 | start + len - 1, 0); | |
7326 | } | |
7327 | em = ERR_PTR(ret); | |
7328 | } | |
7329 | out: | |
7330 | ||
7331 | return em; | |
7332 | } | |
7333 | ||
7334 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, | |
7335 | u64 start, u64 len) | |
7336 | { | |
7337 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7338 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7339 | struct extent_map *em; | |
7340 | struct btrfs_key ins; | |
7341 | u64 alloc_hint; | |
7342 | int ret; | |
7343 | ||
7344 | alloc_hint = get_extent_allocation_hint(inode, start, len); | |
7345 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, | |
7346 | 0, alloc_hint, &ins, 1, 1); | |
7347 | if (ret) | |
7348 | return ERR_PTR(ret); | |
7349 | ||
7350 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, | |
7351 | ins.objectid, ins.offset, ins.offset, | |
7352 | ins.offset, BTRFS_ORDERED_REGULAR); | |
7353 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
7354 | if (IS_ERR(em)) | |
7355 | btrfs_free_reserved_extent(fs_info, ins.objectid, | |
7356 | ins.offset, 1); | |
7357 | ||
7358 | return em; | |
7359 | } | |
7360 | ||
7361 | /* | |
7362 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7363 | * block must be cow'd | |
7364 | */ | |
7365 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, | |
7366 | u64 *orig_start, u64 *orig_block_len, | |
7367 | u64 *ram_bytes) | |
7368 | { | |
7369 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7370 | struct btrfs_path *path; | |
7371 | int ret; | |
7372 | struct extent_buffer *leaf; | |
7373 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7374 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
7375 | struct btrfs_file_extent_item *fi; | |
7376 | struct btrfs_key key; | |
7377 | u64 disk_bytenr; | |
7378 | u64 backref_offset; | |
7379 | u64 extent_end; | |
7380 | u64 num_bytes; | |
7381 | int slot; | |
7382 | int found_type; | |
7383 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); | |
7384 | ||
7385 | path = btrfs_alloc_path(); | |
7386 | if (!path) | |
7387 | return -ENOMEM; | |
7388 | ||
7389 | ret = btrfs_lookup_file_extent(NULL, root, path, | |
7390 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
7391 | if (ret < 0) | |
7392 | goto out; | |
7393 | ||
7394 | slot = path->slots[0]; | |
7395 | if (ret == 1) { | |
7396 | if (slot == 0) { | |
7397 | /* can't find the item, must cow */ | |
7398 | ret = 0; | |
7399 | goto out; | |
7400 | } | |
7401 | slot--; | |
7402 | } | |
7403 | ret = 0; | |
7404 | leaf = path->nodes[0]; | |
7405 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
7406 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || | |
7407 | key.type != BTRFS_EXTENT_DATA_KEY) { | |
7408 | /* not our file or wrong item type, must cow */ | |
7409 | goto out; | |
7410 | } | |
7411 | ||
7412 | if (key.offset > offset) { | |
7413 | /* Wrong offset, must cow */ | |
7414 | goto out; | |
7415 | } | |
7416 | ||
7417 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7418 | found_type = btrfs_file_extent_type(leaf, fi); | |
7419 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7420 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7421 | /* not a regular extent, must cow */ | |
7422 | goto out; | |
7423 | } | |
7424 | ||
7425 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7426 | goto out; | |
7427 | ||
7428 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); | |
7429 | if (extent_end <= offset) | |
7430 | goto out; | |
7431 | ||
7432 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
7433 | if (disk_bytenr == 0) | |
7434 | goto out; | |
7435 | ||
7436 | if (btrfs_file_extent_compression(leaf, fi) || | |
7437 | btrfs_file_extent_encryption(leaf, fi) || | |
7438 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7439 | goto out; | |
7440 | ||
7441 | backref_offset = btrfs_file_extent_offset(leaf, fi); | |
7442 | ||
7443 | if (orig_start) { | |
7444 | *orig_start = key.offset - backref_offset; | |
7445 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7446 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7447 | } | |
7448 | ||
7449 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) | |
7450 | goto out; | |
7451 | ||
7452 | num_bytes = min(offset + *len, extent_end) - offset; | |
7453 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7454 | u64 range_end; | |
7455 | ||
7456 | range_end = round_up(offset + num_bytes, | |
7457 | root->fs_info->sectorsize) - 1; | |
7458 | ret = test_range_bit(io_tree, offset, range_end, | |
7459 | EXTENT_DELALLOC, 0, NULL); | |
7460 | if (ret) { | |
7461 | ret = -EAGAIN; | |
7462 | goto out; | |
7463 | } | |
7464 | } | |
7465 | ||
7466 | btrfs_release_path(path); | |
7467 | ||
7468 | /* | |
7469 | * look for other files referencing this extent, if we | |
7470 | * find any we must cow | |
7471 | */ | |
7472 | ||
7473 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), | |
7474 | key.offset - backref_offset, disk_bytenr); | |
7475 | if (ret) { | |
7476 | ret = 0; | |
7477 | goto out; | |
7478 | } | |
7479 | ||
7480 | /* | |
7481 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7482 | * in this extent we are about to write. If there | |
7483 | * are any csums in that range we have to cow in order | |
7484 | * to keep the csums correct | |
7485 | */ | |
7486 | disk_bytenr += backref_offset; | |
7487 | disk_bytenr += offset - key.offset; | |
7488 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) | |
7489 | goto out; | |
7490 | /* | |
7491 | * all of the above have passed, it is safe to overwrite this extent | |
7492 | * without cow | |
7493 | */ | |
7494 | *len = num_bytes; | |
7495 | ret = 1; | |
7496 | out: | |
7497 | btrfs_free_path(path); | |
7498 | return ret; | |
7499 | } | |
7500 | ||
7501 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) | |
7502 | { | |
7503 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7504 | bool found = false; | |
7505 | void **pagep = NULL; | |
7506 | struct page *page = NULL; | |
7507 | unsigned long start_idx; | |
7508 | unsigned long end_idx; | |
7509 | ||
7510 | start_idx = start >> PAGE_SHIFT; | |
7511 | ||
7512 | /* | |
7513 | * end is the last byte in the last page. end == start is legal | |
7514 | */ | |
7515 | end_idx = end >> PAGE_SHIFT; | |
7516 | ||
7517 | rcu_read_lock(); | |
7518 | ||
7519 | /* Most of the code in this while loop is lifted from | |
7520 | * find_get_page. It's been modified to begin searching from a | |
7521 | * page and return just the first page found in that range. If the | |
7522 | * found idx is less than or equal to the end idx then we know that | |
7523 | * a page exists. If no pages are found or if those pages are | |
7524 | * outside of the range then we're fine (yay!) */ | |
7525 | while (page == NULL && | |
7526 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7527 | page = radix_tree_deref_slot(pagep); | |
7528 | if (unlikely(!page)) | |
7529 | break; | |
7530 | ||
7531 | if (radix_tree_exception(page)) { | |
7532 | if (radix_tree_deref_retry(page)) { | |
7533 | page = NULL; | |
7534 | continue; | |
7535 | } | |
7536 | /* | |
7537 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7538 | * here as an exceptional entry: so return it without | |
7539 | * attempting to raise page count. | |
7540 | */ | |
7541 | page = NULL; | |
7542 | break; /* TODO: Is this relevant for this use case? */ | |
7543 | } | |
7544 | ||
7545 | if (!page_cache_get_speculative(page)) { | |
7546 | page = NULL; | |
7547 | continue; | |
7548 | } | |
7549 | ||
7550 | /* | |
7551 | * Has the page moved? | |
7552 | * This is part of the lockless pagecache protocol. See | |
7553 | * include/linux/pagemap.h for details. | |
7554 | */ | |
7555 | if (unlikely(page != *pagep)) { | |
7556 | put_page(page); | |
7557 | page = NULL; | |
7558 | } | |
7559 | } | |
7560 | ||
7561 | if (page) { | |
7562 | if (page->index <= end_idx) | |
7563 | found = true; | |
7564 | put_page(page); | |
7565 | } | |
7566 | ||
7567 | rcu_read_unlock(); | |
7568 | return found; | |
7569 | } | |
7570 | ||
7571 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, | |
7572 | struct extent_state **cached_state, int writing) | |
7573 | { | |
7574 | struct btrfs_ordered_extent *ordered; | |
7575 | int ret = 0; | |
7576 | ||
7577 | while (1) { | |
7578 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7579 | cached_state); | |
7580 | /* | |
7581 | * We're concerned with the entire range that we're going to be | |
7582 | * doing DIO to, so we need to make sure there's no ordered | |
7583 | * extents in this range. | |
7584 | */ | |
7585 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, | |
7586 | lockend - lockstart + 1); | |
7587 | ||
7588 | /* | |
7589 | * We need to make sure there are no buffered pages in this | |
7590 | * range either, we could have raced between the invalidate in | |
7591 | * generic_file_direct_write and locking the extent. The | |
7592 | * invalidate needs to happen so that reads after a write do not | |
7593 | * get stale data. | |
7594 | */ | |
7595 | if (!ordered && | |
7596 | (!writing || | |
7597 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
7598 | break; | |
7599 | ||
7600 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7601 | cached_state, GFP_NOFS); | |
7602 | ||
7603 | if (ordered) { | |
7604 | /* | |
7605 | * If we are doing a DIO read and the ordered extent we | |
7606 | * found is for a buffered write, we can not wait for it | |
7607 | * to complete and retry, because if we do so we can | |
7608 | * deadlock with concurrent buffered writes on page | |
7609 | * locks. This happens only if our DIO read covers more | |
7610 | * than one extent map, if at this point has already | |
7611 | * created an ordered extent for a previous extent map | |
7612 | * and locked its range in the inode's io tree, and a | |
7613 | * concurrent write against that previous extent map's | |
7614 | * range and this range started (we unlock the ranges | |
7615 | * in the io tree only when the bios complete and | |
7616 | * buffered writes always lock pages before attempting | |
7617 | * to lock range in the io tree). | |
7618 | */ | |
7619 | if (writing || | |
7620 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7621 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7622 | else | |
7623 | ret = -ENOTBLK; | |
7624 | btrfs_put_ordered_extent(ordered); | |
7625 | } else { | |
7626 | /* | |
7627 | * We could trigger writeback for this range (and wait | |
7628 | * for it to complete) and then invalidate the pages for | |
7629 | * this range (through invalidate_inode_pages2_range()), | |
7630 | * but that can lead us to a deadlock with a concurrent | |
7631 | * call to readpages() (a buffered read or a defrag call | |
7632 | * triggered a readahead) on a page lock due to an | |
7633 | * ordered dio extent we created before but did not have | |
7634 | * yet a corresponding bio submitted (whence it can not | |
7635 | * complete), which makes readpages() wait for that | |
7636 | * ordered extent to complete while holding a lock on | |
7637 | * that page. | |
7638 | */ | |
7639 | ret = -ENOTBLK; | |
7640 | } | |
7641 | ||
7642 | if (ret) | |
7643 | break; | |
7644 | ||
7645 | cond_resched(); | |
7646 | } | |
7647 | ||
7648 | return ret; | |
7649 | } | |
7650 | ||
7651 | /* The callers of this must take lock_extent() */ | |
7652 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7653 | u64 orig_start, u64 block_start, | |
7654 | u64 block_len, u64 orig_block_len, | |
7655 | u64 ram_bytes, int compress_type, | |
7656 | int type) | |
7657 | { | |
7658 | struct extent_map_tree *em_tree; | |
7659 | struct extent_map *em; | |
7660 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7661 | int ret; | |
7662 | ||
7663 | ASSERT(type == BTRFS_ORDERED_PREALLOC || | |
7664 | type == BTRFS_ORDERED_COMPRESSED || | |
7665 | type == BTRFS_ORDERED_NOCOW || | |
7666 | type == BTRFS_ORDERED_REGULAR); | |
7667 | ||
7668 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7669 | em = alloc_extent_map(); | |
7670 | if (!em) | |
7671 | return ERR_PTR(-ENOMEM); | |
7672 | ||
7673 | em->start = start; | |
7674 | em->orig_start = orig_start; | |
7675 | em->len = len; | |
7676 | em->block_len = block_len; | |
7677 | em->block_start = block_start; | |
7678 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
7679 | em->orig_block_len = orig_block_len; | |
7680 | em->ram_bytes = ram_bytes; | |
7681 | em->generation = -1; | |
7682 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | |
7683 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7684 | set_bit(EXTENT_FLAG_FILLING, &em->flags); | |
7685 | } else if (type == BTRFS_ORDERED_COMPRESSED) { | |
7686 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
7687 | em->compress_type = compress_type; | |
7688 | } | |
7689 | ||
7690 | do { | |
7691 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, | |
7692 | em->start + em->len - 1, 0); | |
7693 | write_lock(&em_tree->lock); | |
7694 | ret = add_extent_mapping(em_tree, em, 1); | |
7695 | write_unlock(&em_tree->lock); | |
7696 | /* | |
7697 | * The caller has taken lock_extent(), who could race with us | |
7698 | * to add em? | |
7699 | */ | |
7700 | } while (ret == -EEXIST); | |
7701 | ||
7702 | if (ret) { | |
7703 | free_extent_map(em); | |
7704 | return ERR_PTR(ret); | |
7705 | } | |
7706 | ||
7707 | /* em got 2 refs now, callers needs to do free_extent_map once. */ | |
7708 | return em; | |
7709 | } | |
7710 | ||
7711 | static void adjust_dio_outstanding_extents(struct inode *inode, | |
7712 | struct btrfs_dio_data *dio_data, | |
7713 | const u64 len) | |
7714 | { | |
7715 | unsigned num_extents = count_max_extents(len); | |
7716 | ||
7717 | /* | |
7718 | * If we have an outstanding_extents count still set then we're | |
7719 | * within our reservation, otherwise we need to adjust our inode | |
7720 | * counter appropriately. | |
7721 | */ | |
7722 | if (dio_data->outstanding_extents >= num_extents) { | |
7723 | dio_data->outstanding_extents -= num_extents; | |
7724 | } else { | |
7725 | /* | |
7726 | * If dio write length has been split due to no large enough | |
7727 | * contiguous space, we need to compensate our inode counter | |
7728 | * appropriately. | |
7729 | */ | |
7730 | u64 num_needed = num_extents - dio_data->outstanding_extents; | |
7731 | ||
7732 | spin_lock(&BTRFS_I(inode)->lock); | |
7733 | BTRFS_I(inode)->outstanding_extents += num_needed; | |
7734 | spin_unlock(&BTRFS_I(inode)->lock); | |
7735 | } | |
7736 | } | |
7737 | ||
7738 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, | |
7739 | struct buffer_head *bh_result, int create) | |
7740 | { | |
7741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7742 | struct extent_map *em; | |
7743 | struct extent_state *cached_state = NULL; | |
7744 | struct btrfs_dio_data *dio_data = NULL; | |
7745 | u64 start = iblock << inode->i_blkbits; | |
7746 | u64 lockstart, lockend; | |
7747 | u64 len = bh_result->b_size; | |
7748 | int unlock_bits = EXTENT_LOCKED; | |
7749 | int ret = 0; | |
7750 | ||
7751 | if (create) | |
7752 | unlock_bits |= EXTENT_DIRTY; | |
7753 | else | |
7754 | len = min_t(u64, len, fs_info->sectorsize); | |
7755 | ||
7756 | lockstart = start; | |
7757 | lockend = start + len - 1; | |
7758 | ||
7759 | if (current->journal_info) { | |
7760 | /* | |
7761 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7762 | * that anything that needs to check if there's a transaction doesn't get | |
7763 | * confused. | |
7764 | */ | |
7765 | dio_data = current->journal_info; | |
7766 | current->journal_info = NULL; | |
7767 | } | |
7768 | ||
7769 | /* | |
7770 | * If this errors out it's because we couldn't invalidate pagecache for | |
7771 | * this range and we need to fallback to buffered. | |
7772 | */ | |
7773 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, | |
7774 | create)) { | |
7775 | ret = -ENOTBLK; | |
7776 | goto err; | |
7777 | } | |
7778 | ||
7779 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); | |
7780 | if (IS_ERR(em)) { | |
7781 | ret = PTR_ERR(em); | |
7782 | goto unlock_err; | |
7783 | } | |
7784 | ||
7785 | /* | |
7786 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7787 | * io. INLINE is special, and we could probably kludge it in here, but | |
7788 | * it's still buffered so for safety lets just fall back to the generic | |
7789 | * buffered path. | |
7790 | * | |
7791 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7792 | * decompress it, so there will be buffering required no matter what we | |
7793 | * do, so go ahead and fallback to buffered. | |
7794 | * | |
7795 | * We return -ENOTBLK because that's what makes DIO go ahead and go back | |
7796 | * to buffered IO. Don't blame me, this is the price we pay for using | |
7797 | * the generic code. | |
7798 | */ | |
7799 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7800 | em->block_start == EXTENT_MAP_INLINE) { | |
7801 | free_extent_map(em); | |
7802 | ret = -ENOTBLK; | |
7803 | goto unlock_err; | |
7804 | } | |
7805 | ||
7806 | /* Just a good old fashioned hole, return */ | |
7807 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7808 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7809 | free_extent_map(em); | |
7810 | goto unlock_err; | |
7811 | } | |
7812 | ||
7813 | /* | |
7814 | * We don't allocate a new extent in the following cases | |
7815 | * | |
7816 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7817 | * existing extent. | |
7818 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7819 | * just use the extent. | |
7820 | * | |
7821 | */ | |
7822 | if (!create) { | |
7823 | len = min(len, em->len - (start - em->start)); | |
7824 | lockstart = start + len; | |
7825 | goto unlock; | |
7826 | } | |
7827 | ||
7828 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7829 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7830 | em->block_start != EXTENT_MAP_HOLE)) { | |
7831 | int type; | |
7832 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7833 | ||
7834 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7835 | type = BTRFS_ORDERED_PREALLOC; | |
7836 | else | |
7837 | type = BTRFS_ORDERED_NOCOW; | |
7838 | len = min(len, em->len - (start - em->start)); | |
7839 | block_start = em->block_start + (start - em->start); | |
7840 | ||
7841 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
7842 | &orig_block_len, &ram_bytes) == 1 && | |
7843 | btrfs_inc_nocow_writers(fs_info, block_start)) { | |
7844 | struct extent_map *em2; | |
7845 | ||
7846 | em2 = btrfs_create_dio_extent(inode, start, len, | |
7847 | orig_start, block_start, | |
7848 | len, orig_block_len, | |
7849 | ram_bytes, type); | |
7850 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7851 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7852 | free_extent_map(em); | |
7853 | em = em2; | |
7854 | } | |
7855 | if (em2 && IS_ERR(em2)) { | |
7856 | ret = PTR_ERR(em2); | |
7857 | goto unlock_err; | |
7858 | } | |
7859 | /* | |
7860 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7861 | * use the existing or preallocated extent, so does not | |
7862 | * need to adjust btrfs_space_info's bytes_may_use. | |
7863 | */ | |
7864 | btrfs_free_reserved_data_space_noquota(inode, | |
7865 | start, len); | |
7866 | goto unlock; | |
7867 | } | |
7868 | } | |
7869 | ||
7870 | /* | |
7871 | * this will cow the extent, reset the len in case we changed | |
7872 | * it above | |
7873 | */ | |
7874 | len = bh_result->b_size; | |
7875 | free_extent_map(em); | |
7876 | em = btrfs_new_extent_direct(inode, start, len); | |
7877 | if (IS_ERR(em)) { | |
7878 | ret = PTR_ERR(em); | |
7879 | goto unlock_err; | |
7880 | } | |
7881 | len = min(len, em->len - (start - em->start)); | |
7882 | unlock: | |
7883 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7884 | inode->i_blkbits; | |
7885 | bh_result->b_size = len; | |
7886 | bh_result->b_bdev = em->bdev; | |
7887 | set_buffer_mapped(bh_result); | |
7888 | if (create) { | |
7889 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7890 | set_buffer_new(bh_result); | |
7891 | ||
7892 | /* | |
7893 | * Need to update the i_size under the extent lock so buffered | |
7894 | * readers will get the updated i_size when we unlock. | |
7895 | */ | |
7896 | if (!dio_data->overwrite && start + len > i_size_read(inode)) | |
7897 | i_size_write(inode, start + len); | |
7898 | ||
7899 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7900 | WARN_ON(dio_data->reserve < len); | |
7901 | dio_data->reserve -= len; | |
7902 | dio_data->unsubmitted_oe_range_end = start + len; | |
7903 | current->journal_info = dio_data; | |
7904 | } | |
7905 | ||
7906 | /* | |
7907 | * In the case of write we need to clear and unlock the entire range, | |
7908 | * in the case of read we need to unlock only the end area that we | |
7909 | * aren't using if there is any left over space. | |
7910 | */ | |
7911 | if (lockstart < lockend) { | |
7912 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | |
7913 | lockend, unlock_bits, 1, 0, | |
7914 | &cached_state, GFP_NOFS); | |
7915 | } else { | |
7916 | free_extent_state(cached_state); | |
7917 | } | |
7918 | ||
7919 | free_extent_map(em); | |
7920 | ||
7921 | return 0; | |
7922 | ||
7923 | unlock_err: | |
7924 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7925 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
7926 | err: | |
7927 | if (dio_data) | |
7928 | current->journal_info = dio_data; | |
7929 | /* | |
7930 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7931 | * write less data then expected, so that we don't underflow our inode's | |
7932 | * outstanding extents counter. | |
7933 | */ | |
7934 | if (create && dio_data) | |
7935 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7936 | ||
7937 | return ret; | |
7938 | } | |
7939 | ||
7940 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, | |
7941 | struct bio *bio, | |
7942 | int mirror_num) | |
7943 | { | |
7944 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7945 | blk_status_t ret; | |
7946 | ||
7947 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); | |
7948 | ||
7949 | bio_get(bio); | |
7950 | ||
7951 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7952 | if (ret) | |
7953 | goto err; | |
7954 | ||
7955 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); | |
7956 | err: | |
7957 | bio_put(bio); | |
7958 | return ret; | |
7959 | } | |
7960 | ||
7961 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7962 | struct bio *failed_bio, | |
7963 | struct io_failure_record *failrec, | |
7964 | int failed_mirror) | |
7965 | { | |
7966 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7967 | int num_copies; | |
7968 | ||
7969 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); | |
7970 | if (num_copies == 1) { | |
7971 | /* | |
7972 | * we only have a single copy of the data, so don't bother with | |
7973 | * all the retry and error correction code that follows. no | |
7974 | * matter what the error is, it is very likely to persist. | |
7975 | */ | |
7976 | btrfs_debug(fs_info, | |
7977 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7978 | num_copies, failrec->this_mirror, failed_mirror); | |
7979 | return 0; | |
7980 | } | |
7981 | ||
7982 | failrec->failed_mirror = failed_mirror; | |
7983 | failrec->this_mirror++; | |
7984 | if (failrec->this_mirror == failed_mirror) | |
7985 | failrec->this_mirror++; | |
7986 | ||
7987 | if (failrec->this_mirror > num_copies) { | |
7988 | btrfs_debug(fs_info, | |
7989 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7990 | num_copies, failrec->this_mirror, failed_mirror); | |
7991 | return 0; | |
7992 | } | |
7993 | ||
7994 | return 1; | |
7995 | } | |
7996 | ||
7997 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, | |
7998 | struct page *page, unsigned int pgoff, | |
7999 | u64 start, u64 end, int failed_mirror, | |
8000 | bio_end_io_t *repair_endio, void *repair_arg) | |
8001 | { | |
8002 | struct io_failure_record *failrec; | |
8003 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
8004 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8005 | struct bio *bio; | |
8006 | int isector; | |
8007 | int read_mode = 0; | |
8008 | int segs; | |
8009 | int ret; | |
8010 | blk_status_t status; | |
8011 | ||
8012 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); | |
8013 | ||
8014 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
8015 | if (ret) | |
8016 | return errno_to_blk_status(ret); | |
8017 | ||
8018 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
8019 | failed_mirror); | |
8020 | if (!ret) { | |
8021 | free_io_failure(failure_tree, io_tree, failrec); | |
8022 | return BLK_STS_IOERR; | |
8023 | } | |
8024 | ||
8025 | segs = bio_segments(failed_bio); | |
8026 | if (segs > 1 || | |
8027 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) | |
8028 | read_mode |= REQ_FAILFAST_DEV; | |
8029 | ||
8030 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
8031 | isector >>= inode->i_sb->s_blocksize_bits; | |
8032 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
8033 | pgoff, isector, repair_endio, repair_arg); | |
8034 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); | |
8035 | ||
8036 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
8037 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
8038 | read_mode, failrec->this_mirror, failrec->in_validation); | |
8039 | ||
8040 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); | |
8041 | if (status) { | |
8042 | free_io_failure(failure_tree, io_tree, failrec); | |
8043 | bio_put(bio); | |
8044 | } | |
8045 | ||
8046 | return status; | |
8047 | } | |
8048 | ||
8049 | struct btrfs_retry_complete { | |
8050 | struct completion done; | |
8051 | struct inode *inode; | |
8052 | u64 start; | |
8053 | int uptodate; | |
8054 | }; | |
8055 | ||
8056 | static void btrfs_retry_endio_nocsum(struct bio *bio) | |
8057 | { | |
8058 | struct btrfs_retry_complete *done = bio->bi_private; | |
8059 | struct inode *inode = done->inode; | |
8060 | struct bio_vec *bvec; | |
8061 | struct extent_io_tree *io_tree, *failure_tree; | |
8062 | int i; | |
8063 | ||
8064 | if (bio->bi_status) | |
8065 | goto end; | |
8066 | ||
8067 | ASSERT(bio->bi_vcnt == 1); | |
8068 | io_tree = &BTRFS_I(inode)->io_tree; | |
8069 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8070 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); | |
8071 | ||
8072 | done->uptodate = 1; | |
8073 | ASSERT(!bio_flagged(bio, BIO_CLONED)); | |
8074 | bio_for_each_segment_all(bvec, bio, i) | |
8075 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, | |
8076 | io_tree, done->start, bvec->bv_page, | |
8077 | btrfs_ino(BTRFS_I(inode)), 0); | |
8078 | end: | |
8079 | complete(&done->done); | |
8080 | bio_put(bio); | |
8081 | } | |
8082 | ||
8083 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, | |
8084 | struct btrfs_io_bio *io_bio) | |
8085 | { | |
8086 | struct btrfs_fs_info *fs_info; | |
8087 | struct bio_vec bvec; | |
8088 | struct bvec_iter iter; | |
8089 | struct btrfs_retry_complete done; | |
8090 | u64 start; | |
8091 | unsigned int pgoff; | |
8092 | u32 sectorsize; | |
8093 | int nr_sectors; | |
8094 | blk_status_t ret; | |
8095 | blk_status_t err = BLK_STS_OK; | |
8096 | ||
8097 | fs_info = BTRFS_I(inode)->root->fs_info; | |
8098 | sectorsize = fs_info->sectorsize; | |
8099 | ||
8100 | start = io_bio->logical; | |
8101 | done.inode = inode; | |
8102 | io_bio->bio.bi_iter = io_bio->iter; | |
8103 | ||
8104 | bio_for_each_segment(bvec, &io_bio->bio, iter) { | |
8105 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8106 | pgoff = bvec.bv_offset; | |
8107 | ||
8108 | next_block_or_try_again: | |
8109 | done.uptodate = 0; | |
8110 | done.start = start; | |
8111 | init_completion(&done.done); | |
8112 | ||
8113 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, | |
8114 | pgoff, start, start + sectorsize - 1, | |
8115 | io_bio->mirror_num, | |
8116 | btrfs_retry_endio_nocsum, &done); | |
8117 | if (ret) { | |
8118 | err = ret; | |
8119 | goto next; | |
8120 | } | |
8121 | ||
8122 | wait_for_completion(&done.done); | |
8123 | ||
8124 | if (!done.uptodate) { | |
8125 | /* We might have another mirror, so try again */ | |
8126 | goto next_block_or_try_again; | |
8127 | } | |
8128 | ||
8129 | next: | |
8130 | start += sectorsize; | |
8131 | ||
8132 | nr_sectors--; | |
8133 | if (nr_sectors) { | |
8134 | pgoff += sectorsize; | |
8135 | ASSERT(pgoff < PAGE_SIZE); | |
8136 | goto next_block_or_try_again; | |
8137 | } | |
8138 | } | |
8139 | ||
8140 | return err; | |
8141 | } | |
8142 | ||
8143 | static void btrfs_retry_endio(struct bio *bio) | |
8144 | { | |
8145 | struct btrfs_retry_complete *done = bio->bi_private; | |
8146 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8147 | struct extent_io_tree *io_tree, *failure_tree; | |
8148 | struct inode *inode = done->inode; | |
8149 | struct bio_vec *bvec; | |
8150 | int uptodate; | |
8151 | int ret; | |
8152 | int i; | |
8153 | ||
8154 | if (bio->bi_status) | |
8155 | goto end; | |
8156 | ||
8157 | uptodate = 1; | |
8158 | ||
8159 | ASSERT(bio->bi_vcnt == 1); | |
8160 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); | |
8161 | ||
8162 | io_tree = &BTRFS_I(inode)->io_tree; | |
8163 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8164 | ||
8165 | ASSERT(!bio_flagged(bio, BIO_CLONED)); | |
8166 | bio_for_each_segment_all(bvec, bio, i) { | |
8167 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, | |
8168 | bvec->bv_offset, done->start, | |
8169 | bvec->bv_len); | |
8170 | if (!ret) | |
8171 | clean_io_failure(BTRFS_I(inode)->root->fs_info, | |
8172 | failure_tree, io_tree, done->start, | |
8173 | bvec->bv_page, | |
8174 | btrfs_ino(BTRFS_I(inode)), | |
8175 | bvec->bv_offset); | |
8176 | else | |
8177 | uptodate = 0; | |
8178 | } | |
8179 | ||
8180 | done->uptodate = uptodate; | |
8181 | end: | |
8182 | complete(&done->done); | |
8183 | bio_put(bio); | |
8184 | } | |
8185 | ||
8186 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, | |
8187 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8188 | { | |
8189 | struct btrfs_fs_info *fs_info; | |
8190 | struct bio_vec bvec; | |
8191 | struct bvec_iter iter; | |
8192 | struct btrfs_retry_complete done; | |
8193 | u64 start; | |
8194 | u64 offset = 0; | |
8195 | u32 sectorsize; | |
8196 | int nr_sectors; | |
8197 | unsigned int pgoff; | |
8198 | int csum_pos; | |
8199 | bool uptodate = (err == 0); | |
8200 | int ret; | |
8201 | blk_status_t status; | |
8202 | ||
8203 | fs_info = BTRFS_I(inode)->root->fs_info; | |
8204 | sectorsize = fs_info->sectorsize; | |
8205 | ||
8206 | err = BLK_STS_OK; | |
8207 | start = io_bio->logical; | |
8208 | done.inode = inode; | |
8209 | io_bio->bio.bi_iter = io_bio->iter; | |
8210 | ||
8211 | bio_for_each_segment(bvec, &io_bio->bio, iter) { | |
8212 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8213 | ||
8214 | pgoff = bvec.bv_offset; | |
8215 | next_block: | |
8216 | if (uptodate) { | |
8217 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8218 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8219 | bvec.bv_page, pgoff, start, sectorsize); | |
8220 | if (likely(!ret)) | |
8221 | goto next; | |
8222 | } | |
8223 | try_again: | |
8224 | done.uptodate = 0; | |
8225 | done.start = start; | |
8226 | init_completion(&done.done); | |
8227 | ||
8228 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, | |
8229 | pgoff, start, start + sectorsize - 1, | |
8230 | io_bio->mirror_num, btrfs_retry_endio, | |
8231 | &done); | |
8232 | if (status) { | |
8233 | err = status; | |
8234 | goto next; | |
8235 | } | |
8236 | ||
8237 | wait_for_completion(&done.done); | |
8238 | ||
8239 | if (!done.uptodate) { | |
8240 | /* We might have another mirror, so try again */ | |
8241 | goto try_again; | |
8242 | } | |
8243 | next: | |
8244 | offset += sectorsize; | |
8245 | start += sectorsize; | |
8246 | ||
8247 | ASSERT(nr_sectors); | |
8248 | ||
8249 | nr_sectors--; | |
8250 | if (nr_sectors) { | |
8251 | pgoff += sectorsize; | |
8252 | ASSERT(pgoff < PAGE_SIZE); | |
8253 | goto next_block; | |
8254 | } | |
8255 | } | |
8256 | ||
8257 | return err; | |
8258 | } | |
8259 | ||
8260 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, | |
8261 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8262 | { | |
8263 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8264 | ||
8265 | if (skip_csum) { | |
8266 | if (unlikely(err)) | |
8267 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8268 | else | |
8269 | return BLK_STS_OK; | |
8270 | } else { | |
8271 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8272 | } | |
8273 | } | |
8274 | ||
8275 | static void btrfs_endio_direct_read(struct bio *bio) | |
8276 | { | |
8277 | struct btrfs_dio_private *dip = bio->bi_private; | |
8278 | struct inode *inode = dip->inode; | |
8279 | struct bio *dio_bio; | |
8280 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8281 | blk_status_t err = bio->bi_status; | |
8282 | ||
8283 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) { | |
8284 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
8285 | if (!err) | |
8286 | bio->bi_status = 0; | |
8287 | } | |
8288 | ||
8289 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, | |
8290 | dip->logical_offset + dip->bytes - 1); | |
8291 | dio_bio = dip->dio_bio; | |
8292 | ||
8293 | kfree(dip); | |
8294 | ||
8295 | dio_bio->bi_status = bio->bi_status; | |
8296 | dio_end_io(dio_bio); | |
8297 | ||
8298 | if (io_bio->end_io) | |
8299 | io_bio->end_io(io_bio, blk_status_to_errno(err)); | |
8300 | bio_put(bio); | |
8301 | } | |
8302 | ||
8303 | static void __endio_write_update_ordered(struct inode *inode, | |
8304 | const u64 offset, const u64 bytes, | |
8305 | const bool uptodate) | |
8306 | { | |
8307 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
8308 | struct btrfs_ordered_extent *ordered = NULL; | |
8309 | struct btrfs_workqueue *wq; | |
8310 | btrfs_work_func_t func; | |
8311 | u64 ordered_offset = offset; | |
8312 | u64 ordered_bytes = bytes; | |
8313 | u64 last_offset; | |
8314 | int ret; | |
8315 | ||
8316 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { | |
8317 | wq = fs_info->endio_freespace_worker; | |
8318 | func = btrfs_freespace_write_helper; | |
8319 | } else { | |
8320 | wq = fs_info->endio_write_workers; | |
8321 | func = btrfs_endio_write_helper; | |
8322 | } | |
8323 | ||
8324 | again: | |
8325 | last_offset = ordered_offset; | |
8326 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8327 | &ordered_offset, | |
8328 | ordered_bytes, | |
8329 | uptodate); | |
8330 | if (!ret) | |
8331 | goto out_test; | |
8332 | ||
8333 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); | |
8334 | btrfs_queue_work(wq, &ordered->work); | |
8335 | out_test: | |
8336 | /* | |
8337 | * If btrfs_dec_test_ordered_pending does not find any ordered extent | |
8338 | * in the range, we can exit. | |
8339 | */ | |
8340 | if (ordered_offset == last_offset) | |
8341 | return; | |
8342 | /* | |
8343 | * our bio might span multiple ordered extents. If we haven't | |
8344 | * completed the accounting for the whole dio, go back and try again | |
8345 | */ | |
8346 | if (ordered_offset < offset + bytes) { | |
8347 | ordered_bytes = offset + bytes - ordered_offset; | |
8348 | ordered = NULL; | |
8349 | goto again; | |
8350 | } | |
8351 | } | |
8352 | ||
8353 | static void btrfs_endio_direct_write(struct bio *bio) | |
8354 | { | |
8355 | struct btrfs_dio_private *dip = bio->bi_private; | |
8356 | struct bio *dio_bio = dip->dio_bio; | |
8357 | ||
8358 | __endio_write_update_ordered(dip->inode, dip->logical_offset, | |
8359 | dip->bytes, !bio->bi_status); | |
8360 | ||
8361 | kfree(dip); | |
8362 | ||
8363 | dio_bio->bi_status = bio->bi_status; | |
8364 | dio_end_io(dio_bio); | |
8365 | bio_put(bio); | |
8366 | } | |
8367 | ||
8368 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, | |
8369 | struct bio *bio, int mirror_num, | |
8370 | unsigned long bio_flags, u64 offset) | |
8371 | { | |
8372 | struct inode *inode = private_data; | |
8373 | blk_status_t ret; | |
8374 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); | |
8375 | BUG_ON(ret); /* -ENOMEM */ | |
8376 | return 0; | |
8377 | } | |
8378 | ||
8379 | static void btrfs_end_dio_bio(struct bio *bio) | |
8380 | { | |
8381 | struct btrfs_dio_private *dip = bio->bi_private; | |
8382 | blk_status_t err = bio->bi_status; | |
8383 | ||
8384 | if (err) | |
8385 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
8386 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", | |
8387 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), | |
8388 | bio->bi_opf, | |
8389 | (unsigned long long)bio->bi_iter.bi_sector, | |
8390 | bio->bi_iter.bi_size, err); | |
8391 | ||
8392 | if (dip->subio_endio) | |
8393 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
8394 | ||
8395 | if (err) { | |
8396 | dip->errors = 1; | |
8397 | ||
8398 | /* | |
8399 | * before atomic variable goto zero, we must make sure | |
8400 | * dip->errors is perceived to be set. | |
8401 | */ | |
8402 | smp_mb__before_atomic(); | |
8403 | } | |
8404 | ||
8405 | /* if there are more bios still pending for this dio, just exit */ | |
8406 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8407 | goto out; | |
8408 | ||
8409 | if (dip->errors) { | |
8410 | bio_io_error(dip->orig_bio); | |
8411 | } else { | |
8412 | dip->dio_bio->bi_status = 0; | |
8413 | bio_endio(dip->orig_bio); | |
8414 | } | |
8415 | out: | |
8416 | bio_put(bio); | |
8417 | } | |
8418 | ||
8419 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, | |
8420 | struct btrfs_dio_private *dip, | |
8421 | struct bio *bio, | |
8422 | u64 file_offset) | |
8423 | { | |
8424 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8425 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8426 | blk_status_t ret; | |
8427 | ||
8428 | /* | |
8429 | * We load all the csum data we need when we submit | |
8430 | * the first bio to reduce the csum tree search and | |
8431 | * contention. | |
8432 | */ | |
8433 | if (dip->logical_offset == file_offset) { | |
8434 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, | |
8435 | file_offset); | |
8436 | if (ret) | |
8437 | return ret; | |
8438 | } | |
8439 | ||
8440 | if (bio == dip->orig_bio) | |
8441 | return 0; | |
8442 | ||
8443 | file_offset -= dip->logical_offset; | |
8444 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8445 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8446 | ||
8447 | return 0; | |
8448 | } | |
8449 | ||
8450 | static inline blk_status_t | |
8451 | __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset, | |
8452 | int skip_sum, int async_submit) | |
8453 | { | |
8454 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
8455 | struct btrfs_dio_private *dip = bio->bi_private; | |
8456 | bool write = bio_op(bio) == REQ_OP_WRITE; | |
8457 | blk_status_t ret; | |
8458 | ||
8459 | if (async_submit) | |
8460 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8461 | ||
8462 | bio_get(bio); | |
8463 | ||
8464 | if (!write) { | |
8465 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); | |
8466 | if (ret) | |
8467 | goto err; | |
8468 | } | |
8469 | ||
8470 | if (skip_sum) | |
8471 | goto map; | |
8472 | ||
8473 | if (write && async_submit) { | |
8474 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, | |
8475 | file_offset, inode, | |
8476 | __btrfs_submit_bio_start_direct_io, | |
8477 | __btrfs_submit_bio_done); | |
8478 | goto err; | |
8479 | } else if (write) { | |
8480 | /* | |
8481 | * If we aren't doing async submit, calculate the csum of the | |
8482 | * bio now. | |
8483 | */ | |
8484 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); | |
8485 | if (ret) | |
8486 | goto err; | |
8487 | } else { | |
8488 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, | |
8489 | file_offset); | |
8490 | if (ret) | |
8491 | goto err; | |
8492 | } | |
8493 | map: | |
8494 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); | |
8495 | err: | |
8496 | bio_put(bio); | |
8497 | return ret; | |
8498 | } | |
8499 | ||
8500 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, | |
8501 | int skip_sum) | |
8502 | { | |
8503 | struct inode *inode = dip->inode; | |
8504 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
8505 | struct bio *bio; | |
8506 | struct bio *orig_bio = dip->orig_bio; | |
8507 | u64 start_sector = orig_bio->bi_iter.bi_sector; | |
8508 | u64 file_offset = dip->logical_offset; | |
8509 | u64 map_length; | |
8510 | int async_submit = 0; | |
8511 | u64 submit_len; | |
8512 | int clone_offset = 0; | |
8513 | int clone_len; | |
8514 | int ret; | |
8515 | blk_status_t status; | |
8516 | ||
8517 | map_length = orig_bio->bi_iter.bi_size; | |
8518 | submit_len = map_length; | |
8519 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, | |
8520 | &map_length, NULL, 0); | |
8521 | if (ret) | |
8522 | return -EIO; | |
8523 | ||
8524 | if (map_length >= submit_len) { | |
8525 | bio = orig_bio; | |
8526 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; | |
8527 | goto submit; | |
8528 | } | |
8529 | ||
8530 | /* async crcs make it difficult to collect full stripe writes. */ | |
8531 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
8532 | async_submit = 0; | |
8533 | else | |
8534 | async_submit = 1; | |
8535 | ||
8536 | /* bio split */ | |
8537 | ASSERT(map_length <= INT_MAX); | |
8538 | atomic_inc(&dip->pending_bios); | |
8539 | do { | |
8540 | clone_len = min_t(int, submit_len, map_length); | |
8541 | ||
8542 | /* | |
8543 | * This will never fail as it's passing GPF_NOFS and | |
8544 | * the allocation is backed by btrfs_bioset. | |
8545 | */ | |
8546 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, | |
8547 | clone_len); | |
8548 | bio->bi_private = dip; | |
8549 | bio->bi_end_io = btrfs_end_dio_bio; | |
8550 | btrfs_io_bio(bio)->logical = file_offset; | |
8551 | ||
8552 | ASSERT(submit_len >= clone_len); | |
8553 | submit_len -= clone_len; | |
8554 | if (submit_len == 0) | |
8555 | break; | |
8556 | ||
8557 | /* | |
8558 | * Increase the count before we submit the bio so we know | |
8559 | * the end IO handler won't happen before we increase the | |
8560 | * count. Otherwise, the dip might get freed before we're | |
8561 | * done setting it up. | |
8562 | */ | |
8563 | atomic_inc(&dip->pending_bios); | |
8564 | ||
8565 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, | |
8566 | async_submit); | |
8567 | if (status) { | |
8568 | bio_put(bio); | |
8569 | atomic_dec(&dip->pending_bios); | |
8570 | goto out_err; | |
8571 | } | |
8572 | ||
8573 | clone_offset += clone_len; | |
8574 | start_sector += clone_len >> 9; | |
8575 | file_offset += clone_len; | |
8576 | ||
8577 | map_length = submit_len; | |
8578 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8579 | start_sector << 9, &map_length, NULL, 0); | |
8580 | if (ret) | |
8581 | goto out_err; | |
8582 | } while (submit_len > 0); | |
8583 | ||
8584 | submit: | |
8585 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, | |
8586 | async_submit); | |
8587 | if (!status) | |
8588 | return 0; | |
8589 | ||
8590 | bio_put(bio); | |
8591 | out_err: | |
8592 | dip->errors = 1; | |
8593 | /* | |
8594 | * before atomic variable goto zero, we must | |
8595 | * make sure dip->errors is perceived to be set. | |
8596 | */ | |
8597 | smp_mb__before_atomic(); | |
8598 | if (atomic_dec_and_test(&dip->pending_bios)) | |
8599 | bio_io_error(dip->orig_bio); | |
8600 | ||
8601 | /* bio_end_io() will handle error, so we needn't return it */ | |
8602 | return 0; | |
8603 | } | |
8604 | ||
8605 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, | |
8606 | loff_t file_offset) | |
8607 | { | |
8608 | struct btrfs_dio_private *dip = NULL; | |
8609 | struct bio *bio = NULL; | |
8610 | struct btrfs_io_bio *io_bio; | |
8611 | int skip_sum; | |
8612 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); | |
8613 | int ret = 0; | |
8614 | ||
8615 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8616 | ||
8617 | bio = btrfs_bio_clone(dio_bio); | |
8618 | ||
8619 | dip = kzalloc(sizeof(*dip), GFP_NOFS); | |
8620 | if (!dip) { | |
8621 | ret = -ENOMEM; | |
8622 | goto free_ordered; | |
8623 | } | |
8624 | ||
8625 | dip->private = dio_bio->bi_private; | |
8626 | dip->inode = inode; | |
8627 | dip->logical_offset = file_offset; | |
8628 | dip->bytes = dio_bio->bi_iter.bi_size; | |
8629 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
8630 | bio->bi_private = dip; | |
8631 | dip->orig_bio = bio; | |
8632 | dip->dio_bio = dio_bio; | |
8633 | atomic_set(&dip->pending_bios, 0); | |
8634 | io_bio = btrfs_io_bio(bio); | |
8635 | io_bio->logical = file_offset; | |
8636 | ||
8637 | if (write) { | |
8638 | bio->bi_end_io = btrfs_endio_direct_write; | |
8639 | } else { | |
8640 | bio->bi_end_io = btrfs_endio_direct_read; | |
8641 | dip->subio_endio = btrfs_subio_endio_read; | |
8642 | } | |
8643 | ||
8644 | /* | |
8645 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8646 | * even if we fail to submit a bio, because in such case we do the | |
8647 | * corresponding error handling below and it must not be done a second | |
8648 | * time by btrfs_direct_IO(). | |
8649 | */ | |
8650 | if (write) { | |
8651 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8652 | ||
8653 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8654 | dip->bytes; | |
8655 | dio_data->unsubmitted_oe_range_start = | |
8656 | dio_data->unsubmitted_oe_range_end; | |
8657 | } | |
8658 | ||
8659 | ret = btrfs_submit_direct_hook(dip, skip_sum); | |
8660 | if (!ret) | |
8661 | return; | |
8662 | ||
8663 | if (io_bio->end_io) | |
8664 | io_bio->end_io(io_bio, ret); | |
8665 | ||
8666 | free_ordered: | |
8667 | /* | |
8668 | * If we arrived here it means either we failed to submit the dip | |
8669 | * or we either failed to clone the dio_bio or failed to allocate the | |
8670 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8671 | * call bio_endio against our io_bio so that we get proper resource | |
8672 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8673 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8674 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
8675 | */ | |
8676 | if (bio && dip) { | |
8677 | bio_io_error(bio); | |
8678 | /* | |
8679 | * The end io callbacks free our dip, do the final put on bio | |
8680 | * and all the cleanup and final put for dio_bio (through | |
8681 | * dio_end_io()). | |
8682 | */ | |
8683 | dip = NULL; | |
8684 | bio = NULL; | |
8685 | } else { | |
8686 | if (write) | |
8687 | __endio_write_update_ordered(inode, | |
8688 | file_offset, | |
8689 | dio_bio->bi_iter.bi_size, | |
8690 | false); | |
8691 | else | |
8692 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
8693 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
8694 | ||
8695 | dio_bio->bi_status = BLK_STS_IOERR; | |
8696 | /* | |
8697 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8698 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8699 | */ | |
8700 | dio_end_io(dio_bio); | |
8701 | } | |
8702 | if (bio) | |
8703 | bio_put(bio); | |
8704 | kfree(dip); | |
8705 | } | |
8706 | ||
8707 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, | |
8708 | struct kiocb *iocb, | |
8709 | const struct iov_iter *iter, loff_t offset) | |
8710 | { | |
8711 | int seg; | |
8712 | int i; | |
8713 | unsigned int blocksize_mask = fs_info->sectorsize - 1; | |
8714 | ssize_t retval = -EINVAL; | |
8715 | ||
8716 | if (offset & blocksize_mask) | |
8717 | goto out; | |
8718 | ||
8719 | if (iov_iter_alignment(iter) & blocksize_mask) | |
8720 | goto out; | |
8721 | ||
8722 | /* If this is a write we don't need to check anymore */ | |
8723 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) | |
8724 | return 0; | |
8725 | /* | |
8726 | * Check to make sure we don't have duplicate iov_base's in this | |
8727 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8728 | * when reading back. | |
8729 | */ | |
8730 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8731 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8732 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
8733 | goto out; | |
8734 | } | |
8735 | } | |
8736 | retval = 0; | |
8737 | out: | |
8738 | return retval; | |
8739 | } | |
8740 | ||
8741 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) | |
8742 | { | |
8743 | struct file *file = iocb->ki_filp; | |
8744 | struct inode *inode = file->f_mapping->host; | |
8745 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
8746 | struct btrfs_dio_data dio_data = { 0 }; | |
8747 | struct extent_changeset *data_reserved = NULL; | |
8748 | loff_t offset = iocb->ki_pos; | |
8749 | size_t count = 0; | |
8750 | int flags = 0; | |
8751 | bool wakeup = true; | |
8752 | bool relock = false; | |
8753 | ssize_t ret; | |
8754 | ||
8755 | if (check_direct_IO(fs_info, iocb, iter, offset)) | |
8756 | return 0; | |
8757 | ||
8758 | inode_dio_begin(inode); | |
8759 | smp_mb__after_atomic(); | |
8760 | ||
8761 | /* | |
8762 | * The generic stuff only does filemap_write_and_wait_range, which | |
8763 | * isn't enough if we've written compressed pages to this area, so | |
8764 | * we need to flush the dirty pages again to make absolutely sure | |
8765 | * that any outstanding dirty pages are on disk. | |
8766 | */ | |
8767 | count = iov_iter_count(iter); | |
8768 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
8769 | &BTRFS_I(inode)->runtime_flags)) | |
8770 | filemap_fdatawrite_range(inode->i_mapping, offset, | |
8771 | offset + count - 1); | |
8772 | ||
8773 | if (iov_iter_rw(iter) == WRITE) { | |
8774 | /* | |
8775 | * If the write DIO is beyond the EOF, we need update | |
8776 | * the isize, but it is protected by i_mutex. So we can | |
8777 | * not unlock the i_mutex at this case. | |
8778 | */ | |
8779 | if (offset + count <= inode->i_size) { | |
8780 | dio_data.overwrite = 1; | |
8781 | inode_unlock(inode); | |
8782 | relock = true; | |
8783 | } else if (iocb->ki_flags & IOCB_NOWAIT) { | |
8784 | ret = -EAGAIN; | |
8785 | goto out; | |
8786 | } | |
8787 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, | |
8788 | offset, count); | |
8789 | if (ret) | |
8790 | goto out; | |
8791 | dio_data.outstanding_extents = count_max_extents(count); | |
8792 | ||
8793 | /* | |
8794 | * We need to know how many extents we reserved so that we can | |
8795 | * do the accounting properly if we go over the number we | |
8796 | * originally calculated. Abuse current->journal_info for this. | |
8797 | */ | |
8798 | dio_data.reserve = round_up(count, | |
8799 | fs_info->sectorsize); | |
8800 | dio_data.unsubmitted_oe_range_start = (u64)offset; | |
8801 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
8802 | current->journal_info = &dio_data; | |
8803 | down_read(&BTRFS_I(inode)->dio_sem); | |
8804 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, | |
8805 | &BTRFS_I(inode)->runtime_flags)) { | |
8806 | inode_dio_end(inode); | |
8807 | flags = DIO_LOCKING | DIO_SKIP_HOLES; | |
8808 | wakeup = false; | |
8809 | } | |
8810 | ||
8811 | ret = __blockdev_direct_IO(iocb, inode, | |
8812 | fs_info->fs_devices->latest_bdev, | |
8813 | iter, btrfs_get_blocks_direct, NULL, | |
8814 | btrfs_submit_direct, flags); | |
8815 | if (iov_iter_rw(iter) == WRITE) { | |
8816 | up_read(&BTRFS_I(inode)->dio_sem); | |
8817 | current->journal_info = NULL; | |
8818 | if (ret < 0 && ret != -EIOCBQUEUED) { | |
8819 | if (dio_data.reserve) | |
8820 | btrfs_delalloc_release_space(inode, data_reserved, | |
8821 | offset, dio_data.reserve); | |
8822 | /* | |
8823 | * On error we might have left some ordered extents | |
8824 | * without submitting corresponding bios for them, so | |
8825 | * cleanup them up to avoid other tasks getting them | |
8826 | * and waiting for them to complete forever. | |
8827 | */ | |
8828 | if (dio_data.unsubmitted_oe_range_start < | |
8829 | dio_data.unsubmitted_oe_range_end) | |
8830 | __endio_write_update_ordered(inode, | |
8831 | dio_data.unsubmitted_oe_range_start, | |
8832 | dio_data.unsubmitted_oe_range_end - | |
8833 | dio_data.unsubmitted_oe_range_start, | |
8834 | false); | |
8835 | } else if (ret >= 0 && (size_t)ret < count) | |
8836 | btrfs_delalloc_release_space(inode, data_reserved, | |
8837 | offset, count - (size_t)ret); | |
8838 | } | |
8839 | out: | |
8840 | if (wakeup) | |
8841 | inode_dio_end(inode); | |
8842 | if (relock) | |
8843 | inode_lock(inode); | |
8844 | ||
8845 | extent_changeset_free(data_reserved); | |
8846 | return ret; | |
8847 | } | |
8848 | ||
8849 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) | |
8850 | ||
8851 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | |
8852 | __u64 start, __u64 len) | |
8853 | { | |
8854 | int ret; | |
8855 | ||
8856 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8857 | if (ret) | |
8858 | return ret; | |
8859 | ||
8860 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); | |
8861 | } | |
8862 | ||
8863 | int btrfs_readpage(struct file *file, struct page *page) | |
8864 | { | |
8865 | struct extent_io_tree *tree; | |
8866 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8867 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); | |
8868 | } | |
8869 | ||
8870 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) | |
8871 | { | |
8872 | struct extent_io_tree *tree; | |
8873 | struct inode *inode = page->mapping->host; | |
8874 | int ret; | |
8875 | ||
8876 | if (current->flags & PF_MEMALLOC) { | |
8877 | redirty_page_for_writepage(wbc, page); | |
8878 | unlock_page(page); | |
8879 | return 0; | |
8880 | } | |
8881 | ||
8882 | /* | |
8883 | * If we are under memory pressure we will call this directly from the | |
8884 | * VM, we need to make sure we have the inode referenced for the ordered | |
8885 | * extent. If not just return like we didn't do anything. | |
8886 | */ | |
8887 | if (!igrab(inode)) { | |
8888 | redirty_page_for_writepage(wbc, page); | |
8889 | return AOP_WRITEPAGE_ACTIVATE; | |
8890 | } | |
8891 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8892 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); | |
8893 | btrfs_add_delayed_iput(inode); | |
8894 | return ret; | |
8895 | } | |
8896 | ||
8897 | static int btrfs_writepages(struct address_space *mapping, | |
8898 | struct writeback_control *wbc) | |
8899 | { | |
8900 | struct extent_io_tree *tree; | |
8901 | ||
8902 | tree = &BTRFS_I(mapping->host)->io_tree; | |
8903 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); | |
8904 | } | |
8905 | ||
8906 | static int | |
8907 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8908 | struct list_head *pages, unsigned nr_pages) | |
8909 | { | |
8910 | struct extent_io_tree *tree; | |
8911 | tree = &BTRFS_I(mapping->host)->io_tree; | |
8912 | return extent_readpages(tree, mapping, pages, nr_pages, | |
8913 | btrfs_get_extent); | |
8914 | } | |
8915 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) | |
8916 | { | |
8917 | struct extent_io_tree *tree; | |
8918 | struct extent_map_tree *map; | |
8919 | int ret; | |
8920 | ||
8921 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8922 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
8923 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); | |
8924 | if (ret == 1) { | |
8925 | ClearPagePrivate(page); | |
8926 | set_page_private(page, 0); | |
8927 | put_page(page); | |
8928 | } | |
8929 | return ret; | |
8930 | } | |
8931 | ||
8932 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) | |
8933 | { | |
8934 | if (PageWriteback(page) || PageDirty(page)) | |
8935 | return 0; | |
8936 | return __btrfs_releasepage(page, gfp_flags); | |
8937 | } | |
8938 | ||
8939 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, | |
8940 | unsigned int length) | |
8941 | { | |
8942 | struct inode *inode = page->mapping->host; | |
8943 | struct extent_io_tree *tree; | |
8944 | struct btrfs_ordered_extent *ordered; | |
8945 | struct extent_state *cached_state = NULL; | |
8946 | u64 page_start = page_offset(page); | |
8947 | u64 page_end = page_start + PAGE_SIZE - 1; | |
8948 | u64 start; | |
8949 | u64 end; | |
8950 | int inode_evicting = inode->i_state & I_FREEING; | |
8951 | ||
8952 | /* | |
8953 | * we have the page locked, so new writeback can't start, | |
8954 | * and the dirty bit won't be cleared while we are here. | |
8955 | * | |
8956 | * Wait for IO on this page so that we can safely clear | |
8957 | * the PagePrivate2 bit and do ordered accounting | |
8958 | */ | |
8959 | wait_on_page_writeback(page); | |
8960 | ||
8961 | tree = &BTRFS_I(inode)->io_tree; | |
8962 | if (offset) { | |
8963 | btrfs_releasepage(page, GFP_NOFS); | |
8964 | return; | |
8965 | } | |
8966 | ||
8967 | if (!inode_evicting) | |
8968 | lock_extent_bits(tree, page_start, page_end, &cached_state); | |
8969 | again: | |
8970 | start = page_start; | |
8971 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, | |
8972 | page_end - start + 1); | |
8973 | if (ordered) { | |
8974 | end = min(page_end, ordered->file_offset + ordered->len - 1); | |
8975 | /* | |
8976 | * IO on this page will never be started, so we need | |
8977 | * to account for any ordered extents now | |
8978 | */ | |
8979 | if (!inode_evicting) | |
8980 | clear_extent_bit(tree, start, end, | |
8981 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
8982 | EXTENT_DELALLOC_NEW | | |
8983 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8984 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8985 | GFP_NOFS); | |
8986 | /* | |
8987 | * whoever cleared the private bit is responsible | |
8988 | * for the finish_ordered_io | |
8989 | */ | |
8990 | if (TestClearPagePrivate2(page)) { | |
8991 | struct btrfs_ordered_inode_tree *tree; | |
8992 | u64 new_len; | |
8993 | ||
8994 | tree = &BTRFS_I(inode)->ordered_tree; | |
8995 | ||
8996 | spin_lock_irq(&tree->lock); | |
8997 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8998 | new_len = start - ordered->file_offset; | |
8999 | if (new_len < ordered->truncated_len) | |
9000 | ordered->truncated_len = new_len; | |
9001 | spin_unlock_irq(&tree->lock); | |
9002 | ||
9003 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
9004 | start, | |
9005 | end - start + 1, 1)) | |
9006 | btrfs_finish_ordered_io(ordered); | |
9007 | } | |
9008 | btrfs_put_ordered_extent(ordered); | |
9009 | if (!inode_evicting) { | |
9010 | cached_state = NULL; | |
9011 | lock_extent_bits(tree, start, end, | |
9012 | &cached_state); | |
9013 | } | |
9014 | ||
9015 | start = end + 1; | |
9016 | if (start < page_end) | |
9017 | goto again; | |
9018 | } | |
9019 | ||
9020 | /* | |
9021 | * Qgroup reserved space handler | |
9022 | * Page here will be either | |
9023 | * 1) Already written to disk | |
9024 | * In this case, its reserved space is released from data rsv map | |
9025 | * and will be freed by delayed_ref handler finally. | |
9026 | * So even we call qgroup_free_data(), it won't decrease reserved | |
9027 | * space. | |
9028 | * 2) Not written to disk | |
9029 | * This means the reserved space should be freed here. However, | |
9030 | * if a truncate invalidates the page (by clearing PageDirty) | |
9031 | * and the page is accounted for while allocating extent | |
9032 | * in btrfs_check_data_free_space() we let delayed_ref to | |
9033 | * free the entire extent. | |
9034 | */ | |
9035 | if (PageDirty(page)) | |
9036 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); | |
9037 | if (!inode_evicting) { | |
9038 | clear_extent_bit(tree, page_start, page_end, | |
9039 | EXTENT_LOCKED | EXTENT_DIRTY | | |
9040 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | | |
9041 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
9042 | &cached_state, GFP_NOFS); | |
9043 | ||
9044 | __btrfs_releasepage(page, GFP_NOFS); | |
9045 | } | |
9046 | ||
9047 | ClearPageChecked(page); | |
9048 | if (PagePrivate(page)) { | |
9049 | ClearPagePrivate(page); | |
9050 | set_page_private(page, 0); | |
9051 | put_page(page); | |
9052 | } | |
9053 | } | |
9054 | ||
9055 | /* | |
9056 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
9057 | * called from a page fault handler when a page is first dirtied. Hence we must | |
9058 | * be careful to check for EOF conditions here. We set the page up correctly | |
9059 | * for a written page which means we get ENOSPC checking when writing into | |
9060 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
9061 | * support these features. | |
9062 | * | |
9063 | * We are not allowed to take the i_mutex here so we have to play games to | |
9064 | * protect against truncate races as the page could now be beyond EOF. Because | |
9065 | * vmtruncate() writes the inode size before removing pages, once we have the | |
9066 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
9067 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
9068 | * unlock the page. | |
9069 | */ | |
9070 | int btrfs_page_mkwrite(struct vm_fault *vmf) | |
9071 | { | |
9072 | struct page *page = vmf->page; | |
9073 | struct inode *inode = file_inode(vmf->vma->vm_file); | |
9074 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
9075 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
9076 | struct btrfs_ordered_extent *ordered; | |
9077 | struct extent_state *cached_state = NULL; | |
9078 | struct extent_changeset *data_reserved = NULL; | |
9079 | char *kaddr; | |
9080 | unsigned long zero_start; | |
9081 | loff_t size; | |
9082 | int ret; | |
9083 | int reserved = 0; | |
9084 | u64 reserved_space; | |
9085 | u64 page_start; | |
9086 | u64 page_end; | |
9087 | u64 end; | |
9088 | ||
9089 | reserved_space = PAGE_SIZE; | |
9090 | ||
9091 | sb_start_pagefault(inode->i_sb); | |
9092 | page_start = page_offset(page); | |
9093 | page_end = page_start + PAGE_SIZE - 1; | |
9094 | end = page_end; | |
9095 | ||
9096 | /* | |
9097 | * Reserving delalloc space after obtaining the page lock can lead to | |
9098 | * deadlock. For example, if a dirty page is locked by this function | |
9099 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
9100 | * dirty page write out, then the btrfs_writepage() function could | |
9101 | * end up waiting indefinitely to get a lock on the page currently | |
9102 | * being processed by btrfs_page_mkwrite() function. | |
9103 | */ | |
9104 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, | |
9105 | reserved_space); | |
9106 | if (!ret) { | |
9107 | ret = file_update_time(vmf->vma->vm_file); | |
9108 | reserved = 1; | |
9109 | } | |
9110 | if (ret) { | |
9111 | if (ret == -ENOMEM) | |
9112 | ret = VM_FAULT_OOM; | |
9113 | else /* -ENOSPC, -EIO, etc */ | |
9114 | ret = VM_FAULT_SIGBUS; | |
9115 | if (reserved) | |
9116 | goto out; | |
9117 | goto out_noreserve; | |
9118 | } | |
9119 | ||
9120 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ | |
9121 | again: | |
9122 | lock_page(page); | |
9123 | size = i_size_read(inode); | |
9124 | ||
9125 | if ((page->mapping != inode->i_mapping) || | |
9126 | (page_start >= size)) { | |
9127 | /* page got truncated out from underneath us */ | |
9128 | goto out_unlock; | |
9129 | } | |
9130 | wait_on_page_writeback(page); | |
9131 | ||
9132 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); | |
9133 | set_page_extent_mapped(page); | |
9134 | ||
9135 | /* | |
9136 | * we can't set the delalloc bits if there are pending ordered | |
9137 | * extents. Drop our locks and wait for them to finish | |
9138 | */ | |
9139 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, | |
9140 | PAGE_SIZE); | |
9141 | if (ordered) { | |
9142 | unlock_extent_cached(io_tree, page_start, page_end, | |
9143 | &cached_state, GFP_NOFS); | |
9144 | unlock_page(page); | |
9145 | btrfs_start_ordered_extent(inode, ordered, 1); | |
9146 | btrfs_put_ordered_extent(ordered); | |
9147 | goto again; | |
9148 | } | |
9149 | ||
9150 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { | |
9151 | reserved_space = round_up(size - page_start, | |
9152 | fs_info->sectorsize); | |
9153 | if (reserved_space < PAGE_SIZE) { | |
9154 | end = page_start + reserved_space - 1; | |
9155 | spin_lock(&BTRFS_I(inode)->lock); | |
9156 | BTRFS_I(inode)->outstanding_extents++; | |
9157 | spin_unlock(&BTRFS_I(inode)->lock); | |
9158 | btrfs_delalloc_release_space(inode, data_reserved, | |
9159 | page_start, PAGE_SIZE - reserved_space); | |
9160 | } | |
9161 | } | |
9162 | ||
9163 | /* | |
9164 | * page_mkwrite gets called when the page is firstly dirtied after it's | |
9165 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9166 | * bits, thus in this case for space account reason, we still need to | |
9167 | * clear any delalloc bits within this page range since we have to | |
9168 | * reserve data&meta space before lock_page() (see above comments). | |
9169 | */ | |
9170 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, | |
9171 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
9172 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
9173 | 0, 0, &cached_state, GFP_NOFS); | |
9174 | ||
9175 | ret = btrfs_set_extent_delalloc(inode, page_start, end, 0, | |
9176 | &cached_state, 0); | |
9177 | if (ret) { | |
9178 | unlock_extent_cached(io_tree, page_start, page_end, | |
9179 | &cached_state, GFP_NOFS); | |
9180 | ret = VM_FAULT_SIGBUS; | |
9181 | goto out_unlock; | |
9182 | } | |
9183 | ret = 0; | |
9184 | ||
9185 | /* page is wholly or partially inside EOF */ | |
9186 | if (page_start + PAGE_SIZE > size) | |
9187 | zero_start = size & ~PAGE_MASK; | |
9188 | else | |
9189 | zero_start = PAGE_SIZE; | |
9190 | ||
9191 | if (zero_start != PAGE_SIZE) { | |
9192 | kaddr = kmap(page); | |
9193 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); | |
9194 | flush_dcache_page(page); | |
9195 | kunmap(page); | |
9196 | } | |
9197 | ClearPageChecked(page); | |
9198 | set_page_dirty(page); | |
9199 | SetPageUptodate(page); | |
9200 | ||
9201 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
9202 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
9203 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; | |
9204 | ||
9205 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); | |
9206 | ||
9207 | out_unlock: | |
9208 | if (!ret) { | |
9209 | sb_end_pagefault(inode->i_sb); | |
9210 | extent_changeset_free(data_reserved); | |
9211 | return VM_FAULT_LOCKED; | |
9212 | } | |
9213 | unlock_page(page); | |
9214 | out: | |
9215 | btrfs_delalloc_release_space(inode, data_reserved, page_start, | |
9216 | reserved_space); | |
9217 | out_noreserve: | |
9218 | sb_end_pagefault(inode->i_sb); | |
9219 | extent_changeset_free(data_reserved); | |
9220 | return ret; | |
9221 | } | |
9222 | ||
9223 | static int btrfs_truncate(struct inode *inode) | |
9224 | { | |
9225 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
9226 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
9227 | struct btrfs_block_rsv *rsv; | |
9228 | int ret = 0; | |
9229 | int err = 0; | |
9230 | struct btrfs_trans_handle *trans; | |
9231 | u64 mask = fs_info->sectorsize - 1; | |
9232 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
9233 | ||
9234 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
9235 | (u64)-1); | |
9236 | if (ret) | |
9237 | return ret; | |
9238 | ||
9239 | /* | |
9240 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have | |
9241 | * 3 things going on here | |
9242 | * | |
9243 | * 1) We need to reserve space for our orphan item and the space to | |
9244 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9245 | * orphan item because we didn't reserve space to remove it. | |
9246 | * | |
9247 | * 2) We need to reserve space to update our inode. | |
9248 | * | |
9249 | * 3) We need to have something to cache all the space that is going to | |
9250 | * be free'd up by the truncate operation, but also have some slack | |
9251 | * space reserved in case it uses space during the truncate (thank you | |
9252 | * very much snapshotting). | |
9253 | * | |
9254 | * And we need these to all be separate. The fact is we can use a lot of | |
9255 | * space doing the truncate, and we have no earthly idea how much space | |
9256 | * we will use, so we need the truncate reservation to be separate so it | |
9257 | * doesn't end up using space reserved for updating the inode or | |
9258 | * removing the orphan item. We also need to be able to stop the | |
9259 | * transaction and start a new one, which means we need to be able to | |
9260 | * update the inode several times, and we have no idea of knowing how | |
9261 | * many times that will be, so we can't just reserve 1 item for the | |
9262 | * entirety of the operation, so that has to be done separately as well. | |
9263 | * Then there is the orphan item, which does indeed need to be held on | |
9264 | * to for the whole operation, and we need nobody to touch this reserved | |
9265 | * space except the orphan code. | |
9266 | * | |
9267 | * So that leaves us with | |
9268 | * | |
9269 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9270 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9271 | * transaction reservation. | |
9272 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9273 | * updating the inode. | |
9274 | */ | |
9275 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); | |
9276 | if (!rsv) | |
9277 | return -ENOMEM; | |
9278 | rsv->size = min_size; | |
9279 | rsv->failfast = 1; | |
9280 | ||
9281 | /* | |
9282 | * 1 for the truncate slack space | |
9283 | * 1 for updating the inode. | |
9284 | */ | |
9285 | trans = btrfs_start_transaction(root, 2); | |
9286 | if (IS_ERR(trans)) { | |
9287 | err = PTR_ERR(trans); | |
9288 | goto out; | |
9289 | } | |
9290 | ||
9291 | /* Migrate the slack space for the truncate to our reserve */ | |
9292 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, | |
9293 | min_size, 0); | |
9294 | BUG_ON(ret); | |
9295 | ||
9296 | /* | |
9297 | * So if we truncate and then write and fsync we normally would just | |
9298 | * write the extents that changed, which is a problem if we need to | |
9299 | * first truncate that entire inode. So set this flag so we write out | |
9300 | * all of the extents in the inode to the sync log so we're completely | |
9301 | * safe. | |
9302 | */ | |
9303 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
9304 | trans->block_rsv = rsv; | |
9305 | ||
9306 | while (1) { | |
9307 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9308 | inode->i_size, | |
9309 | BTRFS_EXTENT_DATA_KEY); | |
9310 | if (ret != -ENOSPC && ret != -EAGAIN) { | |
9311 | err = ret; | |
9312 | break; | |
9313 | } | |
9314 | ||
9315 | trans->block_rsv = &fs_info->trans_block_rsv; | |
9316 | ret = btrfs_update_inode(trans, root, inode); | |
9317 | if (ret) { | |
9318 | err = ret; | |
9319 | break; | |
9320 | } | |
9321 | ||
9322 | btrfs_end_transaction(trans); | |
9323 | btrfs_btree_balance_dirty(fs_info); | |
9324 | ||
9325 | trans = btrfs_start_transaction(root, 2); | |
9326 | if (IS_ERR(trans)) { | |
9327 | ret = err = PTR_ERR(trans); | |
9328 | trans = NULL; | |
9329 | break; | |
9330 | } | |
9331 | ||
9332 | btrfs_block_rsv_release(fs_info, rsv, -1); | |
9333 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, | |
9334 | rsv, min_size, 0); | |
9335 | BUG_ON(ret); /* shouldn't happen */ | |
9336 | trans->block_rsv = rsv; | |
9337 | } | |
9338 | ||
9339 | if (ret == 0 && inode->i_nlink > 0) { | |
9340 | trans->block_rsv = root->orphan_block_rsv; | |
9341 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); | |
9342 | if (ret) | |
9343 | err = ret; | |
9344 | } | |
9345 | ||
9346 | if (trans) { | |
9347 | trans->block_rsv = &fs_info->trans_block_rsv; | |
9348 | ret = btrfs_update_inode(trans, root, inode); | |
9349 | if (ret && !err) | |
9350 | err = ret; | |
9351 | ||
9352 | ret = btrfs_end_transaction(trans); | |
9353 | btrfs_btree_balance_dirty(fs_info); | |
9354 | } | |
9355 | out: | |
9356 | btrfs_free_block_rsv(fs_info, rsv); | |
9357 | ||
9358 | if (ret && !err) | |
9359 | err = ret; | |
9360 | ||
9361 | return err; | |
9362 | } | |
9363 | ||
9364 | /* | |
9365 | * create a new subvolume directory/inode (helper for the ioctl). | |
9366 | */ | |
9367 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, | |
9368 | struct btrfs_root *new_root, | |
9369 | struct btrfs_root *parent_root, | |
9370 | u64 new_dirid) | |
9371 | { | |
9372 | struct inode *inode; | |
9373 | int err; | |
9374 | u64 index = 0; | |
9375 | ||
9376 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, | |
9377 | new_dirid, new_dirid, | |
9378 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9379 | &index); | |
9380 | if (IS_ERR(inode)) | |
9381 | return PTR_ERR(inode); | |
9382 | inode->i_op = &btrfs_dir_inode_operations; | |
9383 | inode->i_fop = &btrfs_dir_file_operations; | |
9384 | ||
9385 | set_nlink(inode, 1); | |
9386 | btrfs_i_size_write(BTRFS_I(inode), 0); | |
9387 | unlock_new_inode(inode); | |
9388 | ||
9389 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); | |
9390 | if (err) | |
9391 | btrfs_err(new_root->fs_info, | |
9392 | "error inheriting subvolume %llu properties: %d", | |
9393 | new_root->root_key.objectid, err); | |
9394 | ||
9395 | err = btrfs_update_inode(trans, new_root, inode); | |
9396 | ||
9397 | iput(inode); | |
9398 | return err; | |
9399 | } | |
9400 | ||
9401 | struct inode *btrfs_alloc_inode(struct super_block *sb) | |
9402 | { | |
9403 | struct btrfs_inode *ei; | |
9404 | struct inode *inode; | |
9405 | ||
9406 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9407 | if (!ei) | |
9408 | return NULL; | |
9409 | ||
9410 | ei->root = NULL; | |
9411 | ei->generation = 0; | |
9412 | ei->last_trans = 0; | |
9413 | ei->last_sub_trans = 0; | |
9414 | ei->logged_trans = 0; | |
9415 | ei->delalloc_bytes = 0; | |
9416 | ei->new_delalloc_bytes = 0; | |
9417 | ei->defrag_bytes = 0; | |
9418 | ei->disk_i_size = 0; | |
9419 | ei->flags = 0; | |
9420 | ei->csum_bytes = 0; | |
9421 | ei->index_cnt = (u64)-1; | |
9422 | ei->dir_index = 0; | |
9423 | ei->last_unlink_trans = 0; | |
9424 | ei->last_log_commit = 0; | |
9425 | ei->delayed_iput_count = 0; | |
9426 | ||
9427 | spin_lock_init(&ei->lock); | |
9428 | ei->outstanding_extents = 0; | |
9429 | ei->reserved_extents = 0; | |
9430 | ||
9431 | ei->runtime_flags = 0; | |
9432 | ei->force_compress = BTRFS_COMPRESS_NONE; | |
9433 | ||
9434 | ei->delayed_node = NULL; | |
9435 | ||
9436 | ei->i_otime.tv_sec = 0; | |
9437 | ei->i_otime.tv_nsec = 0; | |
9438 | ||
9439 | inode = &ei->vfs_inode; | |
9440 | extent_map_tree_init(&ei->extent_tree); | |
9441 | extent_io_tree_init(&ei->io_tree, inode); | |
9442 | extent_io_tree_init(&ei->io_failure_tree, inode); | |
9443 | ei->io_tree.track_uptodate = 1; | |
9444 | ei->io_failure_tree.track_uptodate = 1; | |
9445 | atomic_set(&ei->sync_writers, 0); | |
9446 | mutex_init(&ei->log_mutex); | |
9447 | mutex_init(&ei->delalloc_mutex); | |
9448 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); | |
9449 | INIT_LIST_HEAD(&ei->delalloc_inodes); | |
9450 | INIT_LIST_HEAD(&ei->delayed_iput); | |
9451 | RB_CLEAR_NODE(&ei->rb_node); | |
9452 | init_rwsem(&ei->dio_sem); | |
9453 | ||
9454 | return inode; | |
9455 | } | |
9456 | ||
9457 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS | |
9458 | void btrfs_test_destroy_inode(struct inode *inode) | |
9459 | { | |
9460 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); | |
9461 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9462 | } | |
9463 | #endif | |
9464 | ||
9465 | static void btrfs_i_callback(struct rcu_head *head) | |
9466 | { | |
9467 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
9468 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9469 | } | |
9470 | ||
9471 | void btrfs_destroy_inode(struct inode *inode) | |
9472 | { | |
9473 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
9474 | struct btrfs_ordered_extent *ordered; | |
9475 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
9476 | ||
9477 | WARN_ON(!hlist_empty(&inode->i_dentry)); | |
9478 | WARN_ON(inode->i_data.nrpages); | |
9479 | WARN_ON(BTRFS_I(inode)->outstanding_extents); | |
9480 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
9481 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); | |
9482 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); | |
9483 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
9484 | WARN_ON(BTRFS_I(inode)->defrag_bytes); | |
9485 | ||
9486 | /* | |
9487 | * This can happen where we create an inode, but somebody else also | |
9488 | * created the same inode and we need to destroy the one we already | |
9489 | * created. | |
9490 | */ | |
9491 | if (!root) | |
9492 | goto free; | |
9493 | ||
9494 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
9495 | &BTRFS_I(inode)->runtime_flags)) { | |
9496 | btrfs_info(fs_info, "inode %llu still on the orphan list", | |
9497 | btrfs_ino(BTRFS_I(inode))); | |
9498 | atomic_dec(&root->orphan_inodes); | |
9499 | } | |
9500 | ||
9501 | while (1) { | |
9502 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); | |
9503 | if (!ordered) | |
9504 | break; | |
9505 | else { | |
9506 | btrfs_err(fs_info, | |
9507 | "found ordered extent %llu %llu on inode cleanup", | |
9508 | ordered->file_offset, ordered->len); | |
9509 | btrfs_remove_ordered_extent(inode, ordered); | |
9510 | btrfs_put_ordered_extent(ordered); | |
9511 | btrfs_put_ordered_extent(ordered); | |
9512 | } | |
9513 | } | |
9514 | btrfs_qgroup_check_reserved_leak(inode); | |
9515 | inode_tree_del(inode); | |
9516 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); | |
9517 | free: | |
9518 | call_rcu(&inode->i_rcu, btrfs_i_callback); | |
9519 | } | |
9520 | ||
9521 | int btrfs_drop_inode(struct inode *inode) | |
9522 | { | |
9523 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
9524 | ||
9525 | if (root == NULL) | |
9526 | return 1; | |
9527 | ||
9528 | /* the snap/subvol tree is on deleting */ | |
9529 | if (btrfs_root_refs(&root->root_item) == 0) | |
9530 | return 1; | |
9531 | else | |
9532 | return generic_drop_inode(inode); | |
9533 | } | |
9534 | ||
9535 | static void init_once(void *foo) | |
9536 | { | |
9537 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9538 | ||
9539 | inode_init_once(&ei->vfs_inode); | |
9540 | } | |
9541 | ||
9542 | void btrfs_destroy_cachep(void) | |
9543 | { | |
9544 | /* | |
9545 | * Make sure all delayed rcu free inodes are flushed before we | |
9546 | * destroy cache. | |
9547 | */ | |
9548 | rcu_barrier(); | |
9549 | kmem_cache_destroy(btrfs_inode_cachep); | |
9550 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9551 | kmem_cache_destroy(btrfs_path_cachep); | |
9552 | kmem_cache_destroy(btrfs_free_space_cachep); | |
9553 | } | |
9554 | ||
9555 | int btrfs_init_cachep(void) | |
9556 | { | |
9557 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", | |
9558 | sizeof(struct btrfs_inode), 0, | |
9559 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, | |
9560 | init_once); | |
9561 | if (!btrfs_inode_cachep) | |
9562 | goto fail; | |
9563 | ||
9564 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", | |
9565 | sizeof(struct btrfs_trans_handle), 0, | |
9566 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); | |
9567 | if (!btrfs_trans_handle_cachep) | |
9568 | goto fail; | |
9569 | ||
9570 | btrfs_path_cachep = kmem_cache_create("btrfs_path", | |
9571 | sizeof(struct btrfs_path), 0, | |
9572 | SLAB_MEM_SPREAD, NULL); | |
9573 | if (!btrfs_path_cachep) | |
9574 | goto fail; | |
9575 | ||
9576 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", | |
9577 | sizeof(struct btrfs_free_space), 0, | |
9578 | SLAB_MEM_SPREAD, NULL); | |
9579 | if (!btrfs_free_space_cachep) | |
9580 | goto fail; | |
9581 | ||
9582 | return 0; | |
9583 | fail: | |
9584 | btrfs_destroy_cachep(); | |
9585 | return -ENOMEM; | |
9586 | } | |
9587 | ||
9588 | static int btrfs_getattr(const struct path *path, struct kstat *stat, | |
9589 | u32 request_mask, unsigned int flags) | |
9590 | { | |
9591 | u64 delalloc_bytes; | |
9592 | struct inode *inode = d_inode(path->dentry); | |
9593 | u32 blocksize = inode->i_sb->s_blocksize; | |
9594 | u32 bi_flags = BTRFS_I(inode)->flags; | |
9595 | ||
9596 | stat->result_mask |= STATX_BTIME; | |
9597 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9598 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9599 | if (bi_flags & BTRFS_INODE_APPEND) | |
9600 | stat->attributes |= STATX_ATTR_APPEND; | |
9601 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9602 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9603 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9604 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9605 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9606 | stat->attributes |= STATX_ATTR_NODUMP; | |
9607 | ||
9608 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9609 | STATX_ATTR_COMPRESSED | | |
9610 | STATX_ATTR_IMMUTABLE | | |
9611 | STATX_ATTR_NODUMP); | |
9612 | ||
9613 | generic_fillattr(inode, stat); | |
9614 | stat->dev = BTRFS_I(inode)->root->anon_dev; | |
9615 | ||
9616 | spin_lock(&BTRFS_I(inode)->lock); | |
9617 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; | |
9618 | spin_unlock(&BTRFS_I(inode)->lock); | |
9619 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + | |
9620 | ALIGN(delalloc_bytes, blocksize)) >> 9; | |
9621 | return 0; | |
9622 | } | |
9623 | ||
9624 | static int btrfs_rename_exchange(struct inode *old_dir, | |
9625 | struct dentry *old_dentry, | |
9626 | struct inode *new_dir, | |
9627 | struct dentry *new_dentry) | |
9628 | { | |
9629 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); | |
9630 | struct btrfs_trans_handle *trans; | |
9631 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9632 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9633 | struct inode *new_inode = new_dentry->d_inode; | |
9634 | struct inode *old_inode = old_dentry->d_inode; | |
9635 | struct timespec ctime = current_time(old_inode); | |
9636 | struct dentry *parent; | |
9637 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); | |
9638 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
9639 | u64 old_idx = 0; | |
9640 | u64 new_idx = 0; | |
9641 | u64 root_objectid; | |
9642 | int ret; | |
9643 | bool root_log_pinned = false; | |
9644 | bool dest_log_pinned = false; | |
9645 | ||
9646 | /* we only allow rename subvolume link between subvolumes */ | |
9647 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9648 | return -EXDEV; | |
9649 | ||
9650 | /* close the race window with snapshot create/destroy ioctl */ | |
9651 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9652 | down_read(&fs_info->subvol_sem); | |
9653 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9654 | down_read(&fs_info->subvol_sem); | |
9655 | ||
9656 | /* | |
9657 | * We want to reserve the absolute worst case amount of items. So if | |
9658 | * both inodes are subvols and we need to unlink them then that would | |
9659 | * require 4 item modifications, but if they are both normal inodes it | |
9660 | * would require 5 item modifications, so we'll assume their normal | |
9661 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9662 | * should cover the worst case number of items we'll modify. | |
9663 | */ | |
9664 | trans = btrfs_start_transaction(root, 12); | |
9665 | if (IS_ERR(trans)) { | |
9666 | ret = PTR_ERR(trans); | |
9667 | goto out_notrans; | |
9668 | } | |
9669 | ||
9670 | /* | |
9671 | * We need to find a free sequence number both in the source and | |
9672 | * in the destination directory for the exchange. | |
9673 | */ | |
9674 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); | |
9675 | if (ret) | |
9676 | goto out_fail; | |
9677 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); | |
9678 | if (ret) | |
9679 | goto out_fail; | |
9680 | ||
9681 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9682 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9683 | ||
9684 | /* Reference for the source. */ | |
9685 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9686 | /* force full log commit if subvolume involved. */ | |
9687 | btrfs_set_log_full_commit(fs_info, trans); | |
9688 | } else { | |
9689 | btrfs_pin_log_trans(root); | |
9690 | root_log_pinned = true; | |
9691 | ret = btrfs_insert_inode_ref(trans, dest, | |
9692 | new_dentry->d_name.name, | |
9693 | new_dentry->d_name.len, | |
9694 | old_ino, | |
9695 | btrfs_ino(BTRFS_I(new_dir)), | |
9696 | old_idx); | |
9697 | if (ret) | |
9698 | goto out_fail; | |
9699 | } | |
9700 | ||
9701 | /* And now for the dest. */ | |
9702 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9703 | /* force full log commit if subvolume involved. */ | |
9704 | btrfs_set_log_full_commit(fs_info, trans); | |
9705 | } else { | |
9706 | btrfs_pin_log_trans(dest); | |
9707 | dest_log_pinned = true; | |
9708 | ret = btrfs_insert_inode_ref(trans, root, | |
9709 | old_dentry->d_name.name, | |
9710 | old_dentry->d_name.len, | |
9711 | new_ino, | |
9712 | btrfs_ino(BTRFS_I(old_dir)), | |
9713 | new_idx); | |
9714 | if (ret) | |
9715 | goto out_fail; | |
9716 | } | |
9717 | ||
9718 | /* Update inode version and ctime/mtime. */ | |
9719 | inode_inc_iversion(old_dir); | |
9720 | inode_inc_iversion(new_dir); | |
9721 | inode_inc_iversion(old_inode); | |
9722 | inode_inc_iversion(new_inode); | |
9723 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9724 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9725 | old_inode->i_ctime = ctime; | |
9726 | new_inode->i_ctime = ctime; | |
9727 | ||
9728 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
9729 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), | |
9730 | BTRFS_I(old_inode), 1); | |
9731 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9732 | BTRFS_I(new_inode), 1); | |
9733 | } | |
9734 | ||
9735 | /* src is a subvolume */ | |
9736 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9737 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9738 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9739 | root_objectid, | |
9740 | old_dentry->d_name.name, | |
9741 | old_dentry->d_name.len); | |
9742 | } else { /* src is an inode */ | |
9743 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), | |
9744 | BTRFS_I(old_dentry->d_inode), | |
9745 | old_dentry->d_name.name, | |
9746 | old_dentry->d_name.len); | |
9747 | if (!ret) | |
9748 | ret = btrfs_update_inode(trans, root, old_inode); | |
9749 | } | |
9750 | if (ret) { | |
9751 | btrfs_abort_transaction(trans, ret); | |
9752 | goto out_fail; | |
9753 | } | |
9754 | ||
9755 | /* dest is a subvolume */ | |
9756 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9757 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9758 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9759 | root_objectid, | |
9760 | new_dentry->d_name.name, | |
9761 | new_dentry->d_name.len); | |
9762 | } else { /* dest is an inode */ | |
9763 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), | |
9764 | BTRFS_I(new_dentry->d_inode), | |
9765 | new_dentry->d_name.name, | |
9766 | new_dentry->d_name.len); | |
9767 | if (!ret) | |
9768 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9769 | } | |
9770 | if (ret) { | |
9771 | btrfs_abort_transaction(trans, ret); | |
9772 | goto out_fail; | |
9773 | } | |
9774 | ||
9775 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), | |
9776 | new_dentry->d_name.name, | |
9777 | new_dentry->d_name.len, 0, old_idx); | |
9778 | if (ret) { | |
9779 | btrfs_abort_transaction(trans, ret); | |
9780 | goto out_fail; | |
9781 | } | |
9782 | ||
9783 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), | |
9784 | old_dentry->d_name.name, | |
9785 | old_dentry->d_name.len, 0, new_idx); | |
9786 | if (ret) { | |
9787 | btrfs_abort_transaction(trans, ret); | |
9788 | goto out_fail; | |
9789 | } | |
9790 | ||
9791 | if (old_inode->i_nlink == 1) | |
9792 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9793 | if (new_inode->i_nlink == 1) | |
9794 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9795 | ||
9796 | if (root_log_pinned) { | |
9797 | parent = new_dentry->d_parent; | |
9798 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), | |
9799 | parent); | |
9800 | btrfs_end_log_trans(root); | |
9801 | root_log_pinned = false; | |
9802 | } | |
9803 | if (dest_log_pinned) { | |
9804 | parent = old_dentry->d_parent; | |
9805 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), | |
9806 | parent); | |
9807 | btrfs_end_log_trans(dest); | |
9808 | dest_log_pinned = false; | |
9809 | } | |
9810 | out_fail: | |
9811 | /* | |
9812 | * If we have pinned a log and an error happened, we unpin tasks | |
9813 | * trying to sync the log and force them to fallback to a transaction | |
9814 | * commit if the log currently contains any of the inodes involved in | |
9815 | * this rename operation (to ensure we do not persist a log with an | |
9816 | * inconsistent state for any of these inodes or leading to any | |
9817 | * inconsistencies when replayed). If the transaction was aborted, the | |
9818 | * abortion reason is propagated to userspace when attempting to commit | |
9819 | * the transaction. If the log does not contain any of these inodes, we | |
9820 | * allow the tasks to sync it. | |
9821 | */ | |
9822 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
9823 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || | |
9824 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9825 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
9826 | (new_inode && | |
9827 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) | |
9828 | btrfs_set_log_full_commit(fs_info, trans); | |
9829 | ||
9830 | if (root_log_pinned) { | |
9831 | btrfs_end_log_trans(root); | |
9832 | root_log_pinned = false; | |
9833 | } | |
9834 | if (dest_log_pinned) { | |
9835 | btrfs_end_log_trans(dest); | |
9836 | dest_log_pinned = false; | |
9837 | } | |
9838 | } | |
9839 | ret = btrfs_end_transaction(trans); | |
9840 | out_notrans: | |
9841 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9842 | up_read(&fs_info->subvol_sem); | |
9843 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9844 | up_read(&fs_info->subvol_sem); | |
9845 | ||
9846 | return ret; | |
9847 | } | |
9848 | ||
9849 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9850 | struct btrfs_root *root, | |
9851 | struct inode *dir, | |
9852 | struct dentry *dentry) | |
9853 | { | |
9854 | int ret; | |
9855 | struct inode *inode; | |
9856 | u64 objectid; | |
9857 | u64 index; | |
9858 | ||
9859 | ret = btrfs_find_free_ino(root, &objectid); | |
9860 | if (ret) | |
9861 | return ret; | |
9862 | ||
9863 | inode = btrfs_new_inode(trans, root, dir, | |
9864 | dentry->d_name.name, | |
9865 | dentry->d_name.len, | |
9866 | btrfs_ino(BTRFS_I(dir)), | |
9867 | objectid, | |
9868 | S_IFCHR | WHITEOUT_MODE, | |
9869 | &index); | |
9870 | ||
9871 | if (IS_ERR(inode)) { | |
9872 | ret = PTR_ERR(inode); | |
9873 | return ret; | |
9874 | } | |
9875 | ||
9876 | inode->i_op = &btrfs_special_inode_operations; | |
9877 | init_special_inode(inode, inode->i_mode, | |
9878 | WHITEOUT_DEV); | |
9879 | ||
9880 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9881 | &dentry->d_name); | |
9882 | if (ret) | |
9883 | goto out; | |
9884 | ||
9885 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, | |
9886 | BTRFS_I(inode), 0, index); | |
9887 | if (ret) | |
9888 | goto out; | |
9889 | ||
9890 | ret = btrfs_update_inode(trans, root, inode); | |
9891 | out: | |
9892 | unlock_new_inode(inode); | |
9893 | if (ret) | |
9894 | inode_dec_link_count(inode); | |
9895 | iput(inode); | |
9896 | ||
9897 | return ret; | |
9898 | } | |
9899 | ||
9900 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |
9901 | struct inode *new_dir, struct dentry *new_dentry, | |
9902 | unsigned int flags) | |
9903 | { | |
9904 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); | |
9905 | struct btrfs_trans_handle *trans; | |
9906 | unsigned int trans_num_items; | |
9907 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9908 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9909 | struct inode *new_inode = d_inode(new_dentry); | |
9910 | struct inode *old_inode = d_inode(old_dentry); | |
9911 | u64 index = 0; | |
9912 | u64 root_objectid; | |
9913 | int ret; | |
9914 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); | |
9915 | bool log_pinned = false; | |
9916 | ||
9917 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
9918 | return -EPERM; | |
9919 | ||
9920 | /* we only allow rename subvolume link between subvolumes */ | |
9921 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9922 | return -EXDEV; | |
9923 | ||
9924 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || | |
9925 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) | |
9926 | return -ENOTEMPTY; | |
9927 | ||
9928 | if (S_ISDIR(old_inode->i_mode) && new_inode && | |
9929 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9930 | return -ENOTEMPTY; | |
9931 | ||
9932 | ||
9933 | /* check for collisions, even if the name isn't there */ | |
9934 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, | |
9935 | new_dentry->d_name.name, | |
9936 | new_dentry->d_name.len); | |
9937 | ||
9938 | if (ret) { | |
9939 | if (ret == -EEXIST) { | |
9940 | /* we shouldn't get | |
9941 | * eexist without a new_inode */ | |
9942 | if (WARN_ON(!new_inode)) { | |
9943 | return ret; | |
9944 | } | |
9945 | } else { | |
9946 | /* maybe -EOVERFLOW */ | |
9947 | return ret; | |
9948 | } | |
9949 | } | |
9950 | ret = 0; | |
9951 | ||
9952 | /* | |
9953 | * we're using rename to replace one file with another. Start IO on it | |
9954 | * now so we don't add too much work to the end of the transaction | |
9955 | */ | |
9956 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) | |
9957 | filemap_flush(old_inode->i_mapping); | |
9958 | ||
9959 | /* close the racy window with snapshot create/destroy ioctl */ | |
9960 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9961 | down_read(&fs_info->subvol_sem); | |
9962 | /* | |
9963 | * We want to reserve the absolute worst case amount of items. So if | |
9964 | * both inodes are subvols and we need to unlink them then that would | |
9965 | * require 4 item modifications, but if they are both normal inodes it | |
9966 | * would require 5 item modifications, so we'll assume they are normal | |
9967 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
9968 | * should cover the worst case number of items we'll modify. | |
9969 | * If our rename has the whiteout flag, we need more 5 units for the | |
9970 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9971 | * when selinux is enabled). | |
9972 | */ | |
9973 | trans_num_items = 11; | |
9974 | if (flags & RENAME_WHITEOUT) | |
9975 | trans_num_items += 5; | |
9976 | trans = btrfs_start_transaction(root, trans_num_items); | |
9977 | if (IS_ERR(trans)) { | |
9978 | ret = PTR_ERR(trans); | |
9979 | goto out_notrans; | |
9980 | } | |
9981 | ||
9982 | if (dest != root) | |
9983 | btrfs_record_root_in_trans(trans, dest); | |
9984 | ||
9985 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); | |
9986 | if (ret) | |
9987 | goto out_fail; | |
9988 | ||
9989 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9990 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
9991 | /* force full log commit if subvolume involved. */ | |
9992 | btrfs_set_log_full_commit(fs_info, trans); | |
9993 | } else { | |
9994 | btrfs_pin_log_trans(root); | |
9995 | log_pinned = true; | |
9996 | ret = btrfs_insert_inode_ref(trans, dest, | |
9997 | new_dentry->d_name.name, | |
9998 | new_dentry->d_name.len, | |
9999 | old_ino, | |
10000 | btrfs_ino(BTRFS_I(new_dir)), index); | |
10001 | if (ret) | |
10002 | goto out_fail; | |
10003 | } | |
10004 | ||
10005 | inode_inc_iversion(old_dir); | |
10006 | inode_inc_iversion(new_dir); | |
10007 | inode_inc_iversion(old_inode); | |
10008 | old_dir->i_ctime = old_dir->i_mtime = | |
10009 | new_dir->i_ctime = new_dir->i_mtime = | |
10010 | old_inode->i_ctime = current_time(old_dir); | |
10011 | ||
10012 | if (old_dentry->d_parent != new_dentry->d_parent) | |
10013 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), | |
10014 | BTRFS_I(old_inode), 1); | |
10015 | ||
10016 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
10017 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
10018 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
10019 | old_dentry->d_name.name, | |
10020 | old_dentry->d_name.len); | |
10021 | } else { | |
10022 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), | |
10023 | BTRFS_I(d_inode(old_dentry)), | |
10024 | old_dentry->d_name.name, | |
10025 | old_dentry->d_name.len); | |
10026 | if (!ret) | |
10027 | ret = btrfs_update_inode(trans, root, old_inode); | |
10028 | } | |
10029 | if (ret) { | |
10030 | btrfs_abort_transaction(trans, ret); | |
10031 | goto out_fail; | |
10032 | } | |
10033 | ||
10034 | if (new_inode) { | |
10035 | inode_inc_iversion(new_inode); | |
10036 | new_inode->i_ctime = current_time(new_inode); | |
10037 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == | |
10038 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { | |
10039 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
10040 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
10041 | root_objectid, | |
10042 | new_dentry->d_name.name, | |
10043 | new_dentry->d_name.len); | |
10044 | BUG_ON(new_inode->i_nlink == 0); | |
10045 | } else { | |
10046 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), | |
10047 | BTRFS_I(d_inode(new_dentry)), | |
10048 | new_dentry->d_name.name, | |
10049 | new_dentry->d_name.len); | |
10050 | } | |
10051 | if (!ret && new_inode->i_nlink == 0) | |
10052 | ret = btrfs_orphan_add(trans, | |
10053 | BTRFS_I(d_inode(new_dentry))); | |
10054 | if (ret) { | |
10055 | btrfs_abort_transaction(trans, ret); | |
10056 | goto out_fail; | |
10057 | } | |
10058 | } | |
10059 | ||
10060 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), | |
10061 | new_dentry->d_name.name, | |
10062 | new_dentry->d_name.len, 0, index); | |
10063 | if (ret) { | |
10064 | btrfs_abort_transaction(trans, ret); | |
10065 | goto out_fail; | |
10066 | } | |
10067 | ||
10068 | if (old_inode->i_nlink == 1) | |
10069 | BTRFS_I(old_inode)->dir_index = index; | |
10070 | ||
10071 | if (log_pinned) { | |
10072 | struct dentry *parent = new_dentry->d_parent; | |
10073 | ||
10074 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), | |
10075 | parent); | |
10076 | btrfs_end_log_trans(root); | |
10077 | log_pinned = false; | |
10078 | } | |
10079 | ||
10080 | if (flags & RENAME_WHITEOUT) { | |
10081 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10082 | old_dentry); | |
10083 | ||
10084 | if (ret) { | |
10085 | btrfs_abort_transaction(trans, ret); | |
10086 | goto out_fail; | |
10087 | } | |
10088 | } | |
10089 | out_fail: | |
10090 | /* | |
10091 | * If we have pinned the log and an error happened, we unpin tasks | |
10092 | * trying to sync the log and force them to fallback to a transaction | |
10093 | * commit if the log currently contains any of the inodes involved in | |
10094 | * this rename operation (to ensure we do not persist a log with an | |
10095 | * inconsistent state for any of these inodes or leading to any | |
10096 | * inconsistencies when replayed). If the transaction was aborted, the | |
10097 | * abortion reason is propagated to userspace when attempting to commit | |
10098 | * the transaction. If the log does not contain any of these inodes, we | |
10099 | * allow the tasks to sync it. | |
10100 | */ | |
10101 | if (ret && log_pinned) { | |
10102 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || | |
10103 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10104 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
10105 | (new_inode && | |
10106 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) | |
10107 | btrfs_set_log_full_commit(fs_info, trans); | |
10108 | ||
10109 | btrfs_end_log_trans(root); | |
10110 | log_pinned = false; | |
10111 | } | |
10112 | btrfs_end_transaction(trans); | |
10113 | out_notrans: | |
10114 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
10115 | up_read(&fs_info->subvol_sem); | |
10116 | ||
10117 | return ret; | |
10118 | } | |
10119 | ||
10120 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, | |
10121 | struct inode *new_dir, struct dentry *new_dentry, | |
10122 | unsigned int flags) | |
10123 | { | |
10124 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) | |
10125 | return -EINVAL; | |
10126 | ||
10127 | if (flags & RENAME_EXCHANGE) | |
10128 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10129 | new_dentry); | |
10130 | ||
10131 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
10132 | } | |
10133 | ||
10134 | static void btrfs_run_delalloc_work(struct btrfs_work *work) | |
10135 | { | |
10136 | struct btrfs_delalloc_work *delalloc_work; | |
10137 | struct inode *inode; | |
10138 | ||
10139 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10140 | work); | |
10141 | inode = delalloc_work->inode; | |
10142 | filemap_flush(inode->i_mapping); | |
10143 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10144 | &BTRFS_I(inode)->runtime_flags)) | |
10145 | filemap_flush(inode->i_mapping); | |
10146 | ||
10147 | if (delalloc_work->delay_iput) | |
10148 | btrfs_add_delayed_iput(inode); | |
10149 | else | |
10150 | iput(inode); | |
10151 | complete(&delalloc_work->completion); | |
10152 | } | |
10153 | ||
10154 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
10155 | int delay_iput) | |
10156 | { | |
10157 | struct btrfs_delalloc_work *work; | |
10158 | ||
10159 | work = kmalloc(sizeof(*work), GFP_NOFS); | |
10160 | if (!work) | |
10161 | return NULL; | |
10162 | ||
10163 | init_completion(&work->completion); | |
10164 | INIT_LIST_HEAD(&work->list); | |
10165 | work->inode = inode; | |
10166 | work->delay_iput = delay_iput; | |
10167 | WARN_ON_ONCE(!inode); | |
10168 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10169 | btrfs_run_delalloc_work, NULL, NULL); | |
10170 | ||
10171 | return work; | |
10172 | } | |
10173 | ||
10174 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10175 | { | |
10176 | wait_for_completion(&work->completion); | |
10177 | kfree(work); | |
10178 | } | |
10179 | ||
10180 | /* | |
10181 | * some fairly slow code that needs optimization. This walks the list | |
10182 | * of all the inodes with pending delalloc and forces them to disk. | |
10183 | */ | |
10184 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, | |
10185 | int nr) | |
10186 | { | |
10187 | struct btrfs_inode *binode; | |
10188 | struct inode *inode; | |
10189 | struct btrfs_delalloc_work *work, *next; | |
10190 | struct list_head works; | |
10191 | struct list_head splice; | |
10192 | int ret = 0; | |
10193 | ||
10194 | INIT_LIST_HEAD(&works); | |
10195 | INIT_LIST_HEAD(&splice); | |
10196 | ||
10197 | mutex_lock(&root->delalloc_mutex); | |
10198 | spin_lock(&root->delalloc_lock); | |
10199 | list_splice_init(&root->delalloc_inodes, &splice); | |
10200 | while (!list_empty(&splice)) { | |
10201 | binode = list_entry(splice.next, struct btrfs_inode, | |
10202 | delalloc_inodes); | |
10203 | ||
10204 | list_move_tail(&binode->delalloc_inodes, | |
10205 | &root->delalloc_inodes); | |
10206 | inode = igrab(&binode->vfs_inode); | |
10207 | if (!inode) { | |
10208 | cond_resched_lock(&root->delalloc_lock); | |
10209 | continue; | |
10210 | } | |
10211 | spin_unlock(&root->delalloc_lock); | |
10212 | ||
10213 | work = btrfs_alloc_delalloc_work(inode, delay_iput); | |
10214 | if (!work) { | |
10215 | if (delay_iput) | |
10216 | btrfs_add_delayed_iput(inode); | |
10217 | else | |
10218 | iput(inode); | |
10219 | ret = -ENOMEM; | |
10220 | goto out; | |
10221 | } | |
10222 | list_add_tail(&work->list, &works); | |
10223 | btrfs_queue_work(root->fs_info->flush_workers, | |
10224 | &work->work); | |
10225 | ret++; | |
10226 | if (nr != -1 && ret >= nr) | |
10227 | goto out; | |
10228 | cond_resched(); | |
10229 | spin_lock(&root->delalloc_lock); | |
10230 | } | |
10231 | spin_unlock(&root->delalloc_lock); | |
10232 | ||
10233 | out: | |
10234 | list_for_each_entry_safe(work, next, &works, list) { | |
10235 | list_del_init(&work->list); | |
10236 | btrfs_wait_and_free_delalloc_work(work); | |
10237 | } | |
10238 | ||
10239 | if (!list_empty_careful(&splice)) { | |
10240 | spin_lock(&root->delalloc_lock); | |
10241 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10242 | spin_unlock(&root->delalloc_lock); | |
10243 | } | |
10244 | mutex_unlock(&root->delalloc_mutex); | |
10245 | return ret; | |
10246 | } | |
10247 | ||
10248 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) | |
10249 | { | |
10250 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10251 | int ret; | |
10252 | ||
10253 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
10254 | return -EROFS; | |
10255 | ||
10256 | ret = __start_delalloc_inodes(root, delay_iput, -1); | |
10257 | if (ret > 0) | |
10258 | ret = 0; | |
10259 | /* | |
10260 | * the filemap_flush will queue IO into the worker threads, but | |
10261 | * we have to make sure the IO is actually started and that | |
10262 | * ordered extents get created before we return | |
10263 | */ | |
10264 | atomic_inc(&fs_info->async_submit_draining); | |
10265 | while (atomic_read(&fs_info->nr_async_submits) || | |
10266 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10267 | wait_event(fs_info->async_submit_wait, | |
10268 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10269 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10270 | } | |
10271 | atomic_dec(&fs_info->async_submit_draining); | |
10272 | return ret; | |
10273 | } | |
10274 | ||
10275 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, | |
10276 | int nr) | |
10277 | { | |
10278 | struct btrfs_root *root; | |
10279 | struct list_head splice; | |
10280 | int ret; | |
10281 | ||
10282 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
10283 | return -EROFS; | |
10284 | ||
10285 | INIT_LIST_HEAD(&splice); | |
10286 | ||
10287 | mutex_lock(&fs_info->delalloc_root_mutex); | |
10288 | spin_lock(&fs_info->delalloc_root_lock); | |
10289 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
10290 | while (!list_empty(&splice) && nr) { | |
10291 | root = list_first_entry(&splice, struct btrfs_root, | |
10292 | delalloc_root); | |
10293 | root = btrfs_grab_fs_root(root); | |
10294 | BUG_ON(!root); | |
10295 | list_move_tail(&root->delalloc_root, | |
10296 | &fs_info->delalloc_roots); | |
10297 | spin_unlock(&fs_info->delalloc_root_lock); | |
10298 | ||
10299 | ret = __start_delalloc_inodes(root, delay_iput, nr); | |
10300 | btrfs_put_fs_root(root); | |
10301 | if (ret < 0) | |
10302 | goto out; | |
10303 | ||
10304 | if (nr != -1) { | |
10305 | nr -= ret; | |
10306 | WARN_ON(nr < 0); | |
10307 | } | |
10308 | spin_lock(&fs_info->delalloc_root_lock); | |
10309 | } | |
10310 | spin_unlock(&fs_info->delalloc_root_lock); | |
10311 | ||
10312 | ret = 0; | |
10313 | atomic_inc(&fs_info->async_submit_draining); | |
10314 | while (atomic_read(&fs_info->nr_async_submits) || | |
10315 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10316 | wait_event(fs_info->async_submit_wait, | |
10317 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10318 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10319 | } | |
10320 | atomic_dec(&fs_info->async_submit_draining); | |
10321 | out: | |
10322 | if (!list_empty_careful(&splice)) { | |
10323 | spin_lock(&fs_info->delalloc_root_lock); | |
10324 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10325 | spin_unlock(&fs_info->delalloc_root_lock); | |
10326 | } | |
10327 | mutex_unlock(&fs_info->delalloc_root_mutex); | |
10328 | return ret; | |
10329 | } | |
10330 | ||
10331 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |
10332 | const char *symname) | |
10333 | { | |
10334 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
10335 | struct btrfs_trans_handle *trans; | |
10336 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10337 | struct btrfs_path *path; | |
10338 | struct btrfs_key key; | |
10339 | struct inode *inode = NULL; | |
10340 | int err; | |
10341 | int drop_inode = 0; | |
10342 | u64 objectid; | |
10343 | u64 index = 0; | |
10344 | int name_len; | |
10345 | int datasize; | |
10346 | unsigned long ptr; | |
10347 | struct btrfs_file_extent_item *ei; | |
10348 | struct extent_buffer *leaf; | |
10349 | ||
10350 | name_len = strlen(symname); | |
10351 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) | |
10352 | return -ENAMETOOLONG; | |
10353 | ||
10354 | /* | |
10355 | * 2 items for inode item and ref | |
10356 | * 2 items for dir items | |
10357 | * 1 item for updating parent inode item | |
10358 | * 1 item for the inline extent item | |
10359 | * 1 item for xattr if selinux is on | |
10360 | */ | |
10361 | trans = btrfs_start_transaction(root, 7); | |
10362 | if (IS_ERR(trans)) | |
10363 | return PTR_ERR(trans); | |
10364 | ||
10365 | err = btrfs_find_free_ino(root, &objectid); | |
10366 | if (err) | |
10367 | goto out_unlock; | |
10368 | ||
10369 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, | |
10370 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), | |
10371 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
10372 | if (IS_ERR(inode)) { | |
10373 | err = PTR_ERR(inode); | |
10374 | goto out_unlock; | |
10375 | } | |
10376 | ||
10377 | /* | |
10378 | * If the active LSM wants to access the inode during | |
10379 | * d_instantiate it needs these. Smack checks to see | |
10380 | * if the filesystem supports xattrs by looking at the | |
10381 | * ops vector. | |
10382 | */ | |
10383 | inode->i_fop = &btrfs_file_operations; | |
10384 | inode->i_op = &btrfs_file_inode_operations; | |
10385 | inode->i_mapping->a_ops = &btrfs_aops; | |
10386 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
10387 | ||
10388 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10389 | if (err) | |
10390 | goto out_unlock_inode; | |
10391 | ||
10392 | path = btrfs_alloc_path(); | |
10393 | if (!path) { | |
10394 | err = -ENOMEM; | |
10395 | goto out_unlock_inode; | |
10396 | } | |
10397 | key.objectid = btrfs_ino(BTRFS_I(inode)); | |
10398 | key.offset = 0; | |
10399 | key.type = BTRFS_EXTENT_DATA_KEY; | |
10400 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
10401 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10402 | datasize); | |
10403 | if (err) { | |
10404 | btrfs_free_path(path); | |
10405 | goto out_unlock_inode; | |
10406 | } | |
10407 | leaf = path->nodes[0]; | |
10408 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10409 | struct btrfs_file_extent_item); | |
10410 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10411 | btrfs_set_file_extent_type(leaf, ei, | |
10412 | BTRFS_FILE_EXTENT_INLINE); | |
10413 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
10414 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10415 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10416 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10417 | ||
10418 | ptr = btrfs_file_extent_inline_start(ei); | |
10419 | write_extent_buffer(leaf, symname, ptr, name_len); | |
10420 | btrfs_mark_buffer_dirty(leaf); | |
10421 | btrfs_free_path(path); | |
10422 | ||
10423 | inode->i_op = &btrfs_symlink_inode_operations; | |
10424 | inode_nohighmem(inode); | |
10425 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
10426 | inode_set_bytes(inode, name_len); | |
10427 | btrfs_i_size_write(BTRFS_I(inode), name_len); | |
10428 | err = btrfs_update_inode(trans, root, inode); | |
10429 | /* | |
10430 | * Last step, add directory indexes for our symlink inode. This is the | |
10431 | * last step to avoid extra cleanup of these indexes if an error happens | |
10432 | * elsewhere above. | |
10433 | */ | |
10434 | if (!err) | |
10435 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, | |
10436 | BTRFS_I(inode), 0, index); | |
10437 | if (err) { | |
10438 | drop_inode = 1; | |
10439 | goto out_unlock_inode; | |
10440 | } | |
10441 | ||
10442 | unlock_new_inode(inode); | |
10443 | d_instantiate(dentry, inode); | |
10444 | ||
10445 | out_unlock: | |
10446 | btrfs_end_transaction(trans); | |
10447 | if (drop_inode) { | |
10448 | inode_dec_link_count(inode); | |
10449 | iput(inode); | |
10450 | } | |
10451 | btrfs_btree_balance_dirty(fs_info); | |
10452 | return err; | |
10453 | ||
10454 | out_unlock_inode: | |
10455 | drop_inode = 1; | |
10456 | unlock_new_inode(inode); | |
10457 | goto out_unlock; | |
10458 | } | |
10459 | ||
10460 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, | |
10461 | u64 start, u64 num_bytes, u64 min_size, | |
10462 | loff_t actual_len, u64 *alloc_hint, | |
10463 | struct btrfs_trans_handle *trans) | |
10464 | { | |
10465 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
10466 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
10467 | struct extent_map *em; | |
10468 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
10469 | struct btrfs_key ins; | |
10470 | u64 cur_offset = start; | |
10471 | u64 i_size; | |
10472 | u64 cur_bytes; | |
10473 | u64 last_alloc = (u64)-1; | |
10474 | int ret = 0; | |
10475 | bool own_trans = true; | |
10476 | u64 end = start + num_bytes - 1; | |
10477 | ||
10478 | if (trans) | |
10479 | own_trans = false; | |
10480 | while (num_bytes > 0) { | |
10481 | if (own_trans) { | |
10482 | trans = btrfs_start_transaction(root, 3); | |
10483 | if (IS_ERR(trans)) { | |
10484 | ret = PTR_ERR(trans); | |
10485 | break; | |
10486 | } | |
10487 | } | |
10488 | ||
10489 | cur_bytes = min_t(u64, num_bytes, SZ_256M); | |
10490 | cur_bytes = max(cur_bytes, min_size); | |
10491 | /* | |
10492 | * If we are severely fragmented we could end up with really | |
10493 | * small allocations, so if the allocator is returning small | |
10494 | * chunks lets make its job easier by only searching for those | |
10495 | * sized chunks. | |
10496 | */ | |
10497 | cur_bytes = min(cur_bytes, last_alloc); | |
10498 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, | |
10499 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
10500 | if (ret) { | |
10501 | if (own_trans) | |
10502 | btrfs_end_transaction(trans); | |
10503 | break; | |
10504 | } | |
10505 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10506 | ||
10507 | last_alloc = ins.offset; | |
10508 | ret = insert_reserved_file_extent(trans, inode, | |
10509 | cur_offset, ins.objectid, | |
10510 | ins.offset, ins.offset, | |
10511 | ins.offset, 0, 0, 0, | |
10512 | BTRFS_FILE_EXTENT_PREALLOC); | |
10513 | if (ret) { | |
10514 | btrfs_free_reserved_extent(fs_info, ins.objectid, | |
10515 | ins.offset, 0); | |
10516 | btrfs_abort_transaction(trans, ret); | |
10517 | if (own_trans) | |
10518 | btrfs_end_transaction(trans); | |
10519 | break; | |
10520 | } | |
10521 | ||
10522 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, | |
10523 | cur_offset + ins.offset -1, 0); | |
10524 | ||
10525 | em = alloc_extent_map(); | |
10526 | if (!em) { | |
10527 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10528 | &BTRFS_I(inode)->runtime_flags); | |
10529 | goto next; | |
10530 | } | |
10531 | ||
10532 | em->start = cur_offset; | |
10533 | em->orig_start = cur_offset; | |
10534 | em->len = ins.offset; | |
10535 | em->block_start = ins.objectid; | |
10536 | em->block_len = ins.offset; | |
10537 | em->orig_block_len = ins.offset; | |
10538 | em->ram_bytes = ins.offset; | |
10539 | em->bdev = fs_info->fs_devices->latest_bdev; | |
10540 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
10541 | em->generation = trans->transid; | |
10542 | ||
10543 | while (1) { | |
10544 | write_lock(&em_tree->lock); | |
10545 | ret = add_extent_mapping(em_tree, em, 1); | |
10546 | write_unlock(&em_tree->lock); | |
10547 | if (ret != -EEXIST) | |
10548 | break; | |
10549 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, | |
10550 | cur_offset + ins.offset - 1, | |
10551 | 0); | |
10552 | } | |
10553 | free_extent_map(em); | |
10554 | next: | |
10555 | num_bytes -= ins.offset; | |
10556 | cur_offset += ins.offset; | |
10557 | *alloc_hint = ins.objectid + ins.offset; | |
10558 | ||
10559 | inode_inc_iversion(inode); | |
10560 | inode->i_ctime = current_time(inode); | |
10561 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; | |
10562 | if (!(mode & FALLOC_FL_KEEP_SIZE) && | |
10563 | (actual_len > inode->i_size) && | |
10564 | (cur_offset > inode->i_size)) { | |
10565 | if (cur_offset > actual_len) | |
10566 | i_size = actual_len; | |
10567 | else | |
10568 | i_size = cur_offset; | |
10569 | i_size_write(inode, i_size); | |
10570 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
10571 | } | |
10572 | ||
10573 | ret = btrfs_update_inode(trans, root, inode); | |
10574 | ||
10575 | if (ret) { | |
10576 | btrfs_abort_transaction(trans, ret); | |
10577 | if (own_trans) | |
10578 | btrfs_end_transaction(trans); | |
10579 | break; | |
10580 | } | |
10581 | ||
10582 | if (own_trans) | |
10583 | btrfs_end_transaction(trans); | |
10584 | } | |
10585 | if (cur_offset < end) | |
10586 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, | |
10587 | end - cur_offset + 1); | |
10588 | return ret; | |
10589 | } | |
10590 | ||
10591 | int btrfs_prealloc_file_range(struct inode *inode, int mode, | |
10592 | u64 start, u64 num_bytes, u64 min_size, | |
10593 | loff_t actual_len, u64 *alloc_hint) | |
10594 | { | |
10595 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10596 | min_size, actual_len, alloc_hint, | |
10597 | NULL); | |
10598 | } | |
10599 | ||
10600 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10601 | struct btrfs_trans_handle *trans, int mode, | |
10602 | u64 start, u64 num_bytes, u64 min_size, | |
10603 | loff_t actual_len, u64 *alloc_hint) | |
10604 | { | |
10605 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10606 | min_size, actual_len, alloc_hint, trans); | |
10607 | } | |
10608 | ||
10609 | static int btrfs_set_page_dirty(struct page *page) | |
10610 | { | |
10611 | return __set_page_dirty_nobuffers(page); | |
10612 | } | |
10613 | ||
10614 | static int btrfs_permission(struct inode *inode, int mask) | |
10615 | { | |
10616 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
10617 | umode_t mode = inode->i_mode; | |
10618 | ||
10619 | if (mask & MAY_WRITE && | |
10620 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10621 | if (btrfs_root_readonly(root)) | |
10622 | return -EROFS; | |
10623 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10624 | return -EACCES; | |
10625 | } | |
10626 | return generic_permission(inode, mask); | |
10627 | } | |
10628 | ||
10629 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) | |
10630 | { | |
10631 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
10632 | struct btrfs_trans_handle *trans; | |
10633 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10634 | struct inode *inode = NULL; | |
10635 | u64 objectid; | |
10636 | u64 index; | |
10637 | int ret = 0; | |
10638 | ||
10639 | /* | |
10640 | * 5 units required for adding orphan entry | |
10641 | */ | |
10642 | trans = btrfs_start_transaction(root, 5); | |
10643 | if (IS_ERR(trans)) | |
10644 | return PTR_ERR(trans); | |
10645 | ||
10646 | ret = btrfs_find_free_ino(root, &objectid); | |
10647 | if (ret) | |
10648 | goto out; | |
10649 | ||
10650 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
10651 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); | |
10652 | if (IS_ERR(inode)) { | |
10653 | ret = PTR_ERR(inode); | |
10654 | inode = NULL; | |
10655 | goto out; | |
10656 | } | |
10657 | ||
10658 | inode->i_fop = &btrfs_file_operations; | |
10659 | inode->i_op = &btrfs_file_inode_operations; | |
10660 | ||
10661 | inode->i_mapping->a_ops = &btrfs_aops; | |
10662 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
10663 | ||
10664 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); | |
10665 | if (ret) | |
10666 | goto out_inode; | |
10667 | ||
10668 | ret = btrfs_update_inode(trans, root, inode); | |
10669 | if (ret) | |
10670 | goto out_inode; | |
10671 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
10672 | if (ret) | |
10673 | goto out_inode; | |
10674 | ||
10675 | /* | |
10676 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10677 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10678 | * through: | |
10679 | * | |
10680 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10681 | */ | |
10682 | set_nlink(inode, 1); | |
10683 | unlock_new_inode(inode); | |
10684 | d_tmpfile(dentry, inode); | |
10685 | mark_inode_dirty(inode); | |
10686 | ||
10687 | out: | |
10688 | btrfs_end_transaction(trans); | |
10689 | if (ret) | |
10690 | iput(inode); | |
10691 | btrfs_balance_delayed_items(fs_info); | |
10692 | btrfs_btree_balance_dirty(fs_info); | |
10693 | return ret; | |
10694 | ||
10695 | out_inode: | |
10696 | unlock_new_inode(inode); | |
10697 | goto out; | |
10698 | ||
10699 | } | |
10700 | ||
10701 | __attribute__((const)) | |
10702 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) | |
10703 | { | |
10704 | return -EAGAIN; | |
10705 | } | |
10706 | ||
10707 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) | |
10708 | { | |
10709 | struct inode *inode = private_data; | |
10710 | return btrfs_sb(inode->i_sb); | |
10711 | } | |
10712 | ||
10713 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, | |
10714 | u64 start, u64 end) | |
10715 | { | |
10716 | struct inode *inode = private_data; | |
10717 | u64 isize; | |
10718 | ||
10719 | isize = i_size_read(inode); | |
10720 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
10721 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, | |
10722 | "%s: ino %llu isize %llu odd range [%llu,%llu]", | |
10723 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); | |
10724 | } | |
10725 | } | |
10726 | ||
10727 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) | |
10728 | { | |
10729 | struct inode *inode = private_data; | |
10730 | unsigned long index = start >> PAGE_SHIFT; | |
10731 | unsigned long end_index = end >> PAGE_SHIFT; | |
10732 | struct page *page; | |
10733 | ||
10734 | while (index <= end_index) { | |
10735 | page = find_get_page(inode->i_mapping, index); | |
10736 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10737 | set_page_writeback(page); | |
10738 | put_page(page); | |
10739 | index++; | |
10740 | } | |
10741 | } | |
10742 | ||
10743 | static const struct inode_operations btrfs_dir_inode_operations = { | |
10744 | .getattr = btrfs_getattr, | |
10745 | .lookup = btrfs_lookup, | |
10746 | .create = btrfs_create, | |
10747 | .unlink = btrfs_unlink, | |
10748 | .link = btrfs_link, | |
10749 | .mkdir = btrfs_mkdir, | |
10750 | .rmdir = btrfs_rmdir, | |
10751 | .rename = btrfs_rename2, | |
10752 | .symlink = btrfs_symlink, | |
10753 | .setattr = btrfs_setattr, | |
10754 | .mknod = btrfs_mknod, | |
10755 | .listxattr = btrfs_listxattr, | |
10756 | .permission = btrfs_permission, | |
10757 | .get_acl = btrfs_get_acl, | |
10758 | .set_acl = btrfs_set_acl, | |
10759 | .update_time = btrfs_update_time, | |
10760 | .tmpfile = btrfs_tmpfile, | |
10761 | }; | |
10762 | static const struct inode_operations btrfs_dir_ro_inode_operations = { | |
10763 | .lookup = btrfs_lookup, | |
10764 | .permission = btrfs_permission, | |
10765 | .update_time = btrfs_update_time, | |
10766 | }; | |
10767 | ||
10768 | static const struct file_operations btrfs_dir_file_operations = { | |
10769 | .llseek = generic_file_llseek, | |
10770 | .read = generic_read_dir, | |
10771 | .iterate_shared = btrfs_real_readdir, | |
10772 | .unlocked_ioctl = btrfs_ioctl, | |
10773 | #ifdef CONFIG_COMPAT | |
10774 | .compat_ioctl = btrfs_compat_ioctl, | |
10775 | #endif | |
10776 | .release = btrfs_release_file, | |
10777 | .fsync = btrfs_sync_file, | |
10778 | }; | |
10779 | ||
10780 | static const struct extent_io_ops btrfs_extent_io_ops = { | |
10781 | /* mandatory callbacks */ | |
10782 | .submit_bio_hook = btrfs_submit_bio_hook, | |
10783 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, | |
10784 | .merge_bio_hook = btrfs_merge_bio_hook, | |
10785 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, | |
10786 | .tree_fs_info = iotree_fs_info, | |
10787 | .set_range_writeback = btrfs_set_range_writeback, | |
10788 | ||
10789 | /* optional callbacks */ | |
10790 | .fill_delalloc = run_delalloc_range, | |
10791 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, | |
10792 | .writepage_start_hook = btrfs_writepage_start_hook, | |
10793 | .set_bit_hook = btrfs_set_bit_hook, | |
10794 | .clear_bit_hook = btrfs_clear_bit_hook, | |
10795 | .merge_extent_hook = btrfs_merge_extent_hook, | |
10796 | .split_extent_hook = btrfs_split_extent_hook, | |
10797 | .check_extent_io_range = btrfs_check_extent_io_range, | |
10798 | }; | |
10799 | ||
10800 | /* | |
10801 | * btrfs doesn't support the bmap operation because swapfiles | |
10802 | * use bmap to make a mapping of extents in the file. They assume | |
10803 | * these extents won't change over the life of the file and they | |
10804 | * use the bmap result to do IO directly to the drive. | |
10805 | * | |
10806 | * the btrfs bmap call would return logical addresses that aren't | |
10807 | * suitable for IO and they also will change frequently as COW | |
10808 | * operations happen. So, swapfile + btrfs == corruption. | |
10809 | * | |
10810 | * For now we're avoiding this by dropping bmap. | |
10811 | */ | |
10812 | static const struct address_space_operations btrfs_aops = { | |
10813 | .readpage = btrfs_readpage, | |
10814 | .writepage = btrfs_writepage, | |
10815 | .writepages = btrfs_writepages, | |
10816 | .readpages = btrfs_readpages, | |
10817 | .direct_IO = btrfs_direct_IO, | |
10818 | .invalidatepage = btrfs_invalidatepage, | |
10819 | .releasepage = btrfs_releasepage, | |
10820 | .set_page_dirty = btrfs_set_page_dirty, | |
10821 | .error_remove_page = generic_error_remove_page, | |
10822 | }; | |
10823 | ||
10824 | static const struct address_space_operations btrfs_symlink_aops = { | |
10825 | .readpage = btrfs_readpage, | |
10826 | .writepage = btrfs_writepage, | |
10827 | .invalidatepage = btrfs_invalidatepage, | |
10828 | .releasepage = btrfs_releasepage, | |
10829 | }; | |
10830 | ||
10831 | static const struct inode_operations btrfs_file_inode_operations = { | |
10832 | .getattr = btrfs_getattr, | |
10833 | .setattr = btrfs_setattr, | |
10834 | .listxattr = btrfs_listxattr, | |
10835 | .permission = btrfs_permission, | |
10836 | .fiemap = btrfs_fiemap, | |
10837 | .get_acl = btrfs_get_acl, | |
10838 | .set_acl = btrfs_set_acl, | |
10839 | .update_time = btrfs_update_time, | |
10840 | }; | |
10841 | static const struct inode_operations btrfs_special_inode_operations = { | |
10842 | .getattr = btrfs_getattr, | |
10843 | .setattr = btrfs_setattr, | |
10844 | .permission = btrfs_permission, | |
10845 | .listxattr = btrfs_listxattr, | |
10846 | .get_acl = btrfs_get_acl, | |
10847 | .set_acl = btrfs_set_acl, | |
10848 | .update_time = btrfs_update_time, | |
10849 | }; | |
10850 | static const struct inode_operations btrfs_symlink_inode_operations = { | |
10851 | .get_link = page_get_link, | |
10852 | .getattr = btrfs_getattr, | |
10853 | .setattr = btrfs_setattr, | |
10854 | .permission = btrfs_permission, | |
10855 | .listxattr = btrfs_listxattr, | |
10856 | .update_time = btrfs_update_time, | |
10857 | }; | |
10858 | ||
10859 | const struct dentry_operations btrfs_dentry_operations = { | |
10860 | .d_delete = btrfs_dentry_delete, | |
10861 | .d_release = btrfs_dentry_release, | |
10862 | }; |