]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 CM |
4 | */ |
5 | ||
7999096f | 6 | #include <crypto/hash.h> |
8f18cf13 | 7 | #include <linux/kernel.h> |
065631f6 | 8 | #include <linux/bio.h> |
348332e0 | 9 | #include <linux/blk-cgroup.h> |
f2eb0a24 | 10 | #include <linux/file.h> |
39279cc3 CM |
11 | #include <linux/fs.h> |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/highmem.h> | |
14 | #include <linux/time.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/string.h> | |
39279cc3 | 17 | #include <linux/backing-dev.h> |
39279cc3 | 18 | #include <linux/writeback.h> |
39279cc3 | 19 | #include <linux/compat.h> |
5103e947 | 20 | #include <linux/xattr.h> |
33268eaf | 21 | #include <linux/posix_acl.h> |
d899e052 | 22 | #include <linux/falloc.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
7a36ddec | 24 | #include <linux/ratelimit.h> |
55e301fd | 25 | #include <linux/btrfs.h> |
53b381b3 | 26 | #include <linux/blkdev.h> |
f23b5a59 | 27 | #include <linux/posix_acl_xattr.h> |
e2e40f2c | 28 | #include <linux/uio.h> |
69fe2d75 | 29 | #include <linux/magic.h> |
ae5e165d | 30 | #include <linux/iversion.h> |
ed46ff3d | 31 | #include <linux/swap.h> |
f8e66081 | 32 | #include <linux/migrate.h> |
b1c16ac9 | 33 | #include <linux/sched/mm.h> |
f85781fb | 34 | #include <linux/iomap.h> |
92d32170 | 35 | #include <asm/unaligned.h> |
14605409 | 36 | #include <linux/fsverity.h> |
602cbe91 | 37 | #include "misc.h" |
39279cc3 CM |
38 | #include "ctree.h" |
39 | #include "disk-io.h" | |
40 | #include "transaction.h" | |
41 | #include "btrfs_inode.h" | |
39279cc3 | 42 | #include "print-tree.h" |
e6dcd2dc | 43 | #include "ordered-data.h" |
95819c05 | 44 | #include "xattr.h" |
e02119d5 | 45 | #include "tree-log.h" |
4a54c8c1 | 46 | #include "volumes.h" |
c8b97818 | 47 | #include "compression.h" |
b4ce94de | 48 | #include "locking.h" |
dc89e982 | 49 | #include "free-space-cache.h" |
63541927 | 50 | #include "props.h" |
31193213 | 51 | #include "qgroup.h" |
86736342 | 52 | #include "delalloc-space.h" |
aac0023c | 53 | #include "block-group.h" |
467dc47e | 54 | #include "space-info.h" |
d8e3fb10 | 55 | #include "zoned.h" |
b945a463 | 56 | #include "subpage.h" |
26c2c454 | 57 | #include "inode-item.h" |
39279cc3 CM |
58 | |
59 | struct btrfs_iget_args { | |
0202e83f | 60 | u64 ino; |
39279cc3 CM |
61 | struct btrfs_root *root; |
62 | }; | |
63 | ||
f28a4928 | 64 | struct btrfs_dio_data { |
f85781fb GR |
65 | ssize_t submitted; |
66 | struct extent_changeset *data_reserved; | |
f28a4928 FM |
67 | }; |
68 | ||
6e1d5dcc AD |
69 | static const struct inode_operations btrfs_dir_inode_operations; |
70 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
71 | static const struct inode_operations btrfs_special_inode_operations; |
72 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 73 | static const struct address_space_operations btrfs_aops; |
828c0950 | 74 | static const struct file_operations btrfs_dir_file_operations; |
39279cc3 CM |
75 | |
76 | static struct kmem_cache *btrfs_inode_cachep; | |
77 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 78 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 79 | struct kmem_cache *btrfs_free_space_cachep; |
3acd4850 | 80 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
39279cc3 | 81 | |
3972f260 | 82 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 83 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
5fd02043 | 84 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
6e26c442 | 85 | static noinline int cow_file_range(struct btrfs_inode *inode, |
771ed689 | 86 | struct page *locked_page, |
74e9194a | 87 | u64 start, u64 end, int *page_started, |
330a5827 | 88 | unsigned long *nr_written, int unlock); |
4b67c11d NB |
89 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
90 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
91 | u64 block_len, u64 orig_block_len, |
92 | u64 ram_bytes, int compress_type, | |
93 | int type); | |
7b128766 | 94 | |
b672b5c1 | 95 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
96 | const u64 offset, const u64 bytes, |
97 | const bool uptodate); | |
98 | ||
a14b78ad GR |
99 | /* |
100 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed | |
101 | * | |
102 | * ilock_flags can have the following bit set: | |
103 | * | |
104 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode | |
105 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt | |
106 | * return -EAGAIN | |
8318ba79 | 107 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
a14b78ad GR |
108 | */ |
109 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) | |
110 | { | |
111 | if (ilock_flags & BTRFS_ILOCK_SHARED) { | |
112 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
113 | if (!inode_trylock_shared(inode)) | |
114 | return -EAGAIN; | |
115 | else | |
116 | return 0; | |
117 | } | |
118 | inode_lock_shared(inode); | |
119 | } else { | |
120 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
121 | if (!inode_trylock(inode)) | |
122 | return -EAGAIN; | |
123 | else | |
124 | return 0; | |
125 | } | |
126 | inode_lock(inode); | |
127 | } | |
8318ba79 JB |
128 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
129 | down_write(&BTRFS_I(inode)->i_mmap_lock); | |
a14b78ad GR |
130 | return 0; |
131 | } | |
132 | ||
133 | /* | |
134 | * btrfs_inode_unlock - unock inode i_rwsem | |
135 | * | |
136 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() | |
137 | * to decide whether the lock acquired is shared or exclusive. | |
138 | */ | |
139 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) | |
140 | { | |
8318ba79 JB |
141 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
142 | up_write(&BTRFS_I(inode)->i_mmap_lock); | |
a14b78ad GR |
143 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
144 | inode_unlock_shared(inode); | |
145 | else | |
146 | inode_unlock(inode); | |
147 | } | |
148 | ||
52427260 QW |
149 | /* |
150 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 151 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
152 | * |
153 | * NOTE: caller must ensure that when an error happens, it can not call | |
154 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
155 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
156 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 157 | * extent (btrfs_finish_ordered_io()). |
52427260 | 158 | */ |
64e1db56 | 159 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
160 | struct page *locked_page, |
161 | u64 offset, u64 bytes) | |
52427260 | 162 | { |
63d71450 NA |
163 | unsigned long index = offset >> PAGE_SHIFT; |
164 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
d1051d6e NB |
165 | u64 page_start = page_offset(locked_page); |
166 | u64 page_end = page_start + PAGE_SIZE - 1; | |
167 | ||
63d71450 NA |
168 | struct page *page; |
169 | ||
170 | while (index <= end_index) { | |
968f2566 QW |
171 | /* |
172 | * For locked page, we will call end_extent_writepage() on it | |
173 | * in run_delalloc_range() for the error handling. That | |
174 | * end_extent_writepage() function will call | |
175 | * btrfs_mark_ordered_io_finished() to clear page Ordered and | |
176 | * run the ordered extent accounting. | |
177 | * | |
178 | * Here we can't just clear the Ordered bit, or | |
179 | * btrfs_mark_ordered_io_finished() would skip the accounting | |
180 | * for the page range, and the ordered extent will never finish. | |
181 | */ | |
182 | if (index == (page_offset(locked_page) >> PAGE_SHIFT)) { | |
183 | index++; | |
184 | continue; | |
185 | } | |
64e1db56 | 186 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
187 | index++; |
188 | if (!page) | |
189 | continue; | |
968f2566 QW |
190 | |
191 | /* | |
192 | * Here we just clear all Ordered bits for every page in the | |
193 | * range, then __endio_write_update_ordered() will handle | |
194 | * the ordered extent accounting for the range. | |
195 | */ | |
b945a463 QW |
196 | btrfs_page_clamp_clear_ordered(inode->root->fs_info, page, |
197 | offset, bytes); | |
63d71450 NA |
198 | put_page(page); |
199 | } | |
d1051d6e | 200 | |
b945a463 QW |
201 | /* The locked page covers the full range, nothing needs to be done */ |
202 | if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE) | |
203 | return; | |
d1051d6e NB |
204 | /* |
205 | * In case this page belongs to the delalloc range being instantiated | |
206 | * then skip it, since the first page of a range is going to be | |
207 | * properly cleaned up by the caller of run_delalloc_range | |
208 | */ | |
209 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
b945a463 QW |
210 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; |
211 | offset = page_offset(locked_page) + PAGE_SIZE; | |
d1051d6e NB |
212 | } |
213 | ||
64e1db56 | 214 | return __endio_write_update_ordered(inode, offset, bytes, false); |
52427260 QW |
215 | } |
216 | ||
48a3b636 | 217 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 218 | |
f34f57a3 | 219 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
220 | struct inode *inode, struct inode *dir, |
221 | const struct qstr *qstr) | |
0279b4cd JO |
222 | { |
223 | int err; | |
224 | ||
f34f57a3 | 225 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 226 | if (!err) |
2a7dba39 | 227 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
228 | return err; |
229 | } | |
230 | ||
c8b97818 CM |
231 | /* |
232 | * this does all the hard work for inserting an inline extent into | |
233 | * the btree. The caller should have done a btrfs_drop_extents so that | |
234 | * no overlapping inline items exist in the btree | |
235 | */ | |
40f76580 | 236 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
5893dfb9 | 237 | struct btrfs_path *path, bool extent_inserted, |
c8b97818 CM |
238 | struct btrfs_root *root, struct inode *inode, |
239 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 240 | int compress_type, |
c8b97818 CM |
241 | struct page **compressed_pages) |
242 | { | |
c8b97818 CM |
243 | struct extent_buffer *leaf; |
244 | struct page *page = NULL; | |
245 | char *kaddr; | |
246 | unsigned long ptr; | |
247 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
248 | int ret; |
249 | size_t cur_size = size; | |
c8b97818 | 250 | unsigned long offset; |
c8b97818 | 251 | |
982f1f5d JJB |
252 | ASSERT((compressed_size > 0 && compressed_pages) || |
253 | (compressed_size == 0 && !compressed_pages)); | |
254 | ||
fe3f566c | 255 | if (compressed_size && compressed_pages) |
c8b97818 | 256 | cur_size = compressed_size; |
c8b97818 | 257 | |
1acae57b FDBM |
258 | if (!extent_inserted) { |
259 | struct btrfs_key key; | |
260 | size_t datasize; | |
c8b97818 | 261 | |
4a0cc7ca | 262 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 263 | key.offset = start; |
962a298f | 264 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 265 | |
1acae57b | 266 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
1acae57b FDBM |
267 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
268 | datasize); | |
79b4f4c6 | 269 | if (ret) |
1acae57b | 270 | goto fail; |
c8b97818 CM |
271 | } |
272 | leaf = path->nodes[0]; | |
273 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
274 | struct btrfs_file_extent_item); | |
275 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
276 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
277 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
278 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
279 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
280 | ptr = btrfs_file_extent_inline_start(ei); | |
281 | ||
261507a0 | 282 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
283 | struct page *cpage; |
284 | int i = 0; | |
d397712b | 285 | while (compressed_size > 0) { |
c8b97818 | 286 | cpage = compressed_pages[i]; |
5b050f04 | 287 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 288 | PAGE_SIZE); |
c8b97818 | 289 | |
3a60f653 | 290 | kaddr = kmap_atomic(cpage); |
c8b97818 | 291 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
3a60f653 | 292 | kunmap_atomic(kaddr); |
c8b97818 CM |
293 | |
294 | i++; | |
295 | ptr += cur_size; | |
296 | compressed_size -= cur_size; | |
297 | } | |
298 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 299 | compress_type); |
c8b97818 CM |
300 | } else { |
301 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 302 | start >> PAGE_SHIFT); |
c8b97818 | 303 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 304 | kaddr = kmap_atomic(page); |
7073017a | 305 | offset = offset_in_page(start); |
c8b97818 | 306 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 307 | kunmap_atomic(kaddr); |
09cbfeaf | 308 | put_page(page); |
c8b97818 CM |
309 | } |
310 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 311 | btrfs_release_path(path); |
c8b97818 | 312 | |
9ddc959e JB |
313 | /* |
314 | * We align size to sectorsize for inline extents just for simplicity | |
315 | * sake. | |
316 | */ | |
317 | size = ALIGN(size, root->fs_info->sectorsize); | |
318 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); | |
319 | if (ret) | |
320 | goto fail; | |
321 | ||
c2167754 YZ |
322 | /* |
323 | * we're an inline extent, so nobody can | |
324 | * extend the file past i_size without locking | |
325 | * a page we already have locked. | |
326 | * | |
327 | * We must do any isize and inode updates | |
328 | * before we unlock the pages. Otherwise we | |
329 | * could end up racing with unlink. | |
330 | */ | |
c8b97818 | 331 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
c8b97818 | 332 | fail: |
79b4f4c6 | 333 | return ret; |
c8b97818 CM |
334 | } |
335 | ||
336 | ||
337 | /* | |
338 | * conditionally insert an inline extent into the file. This | |
339 | * does the checks required to make sure the data is small enough | |
340 | * to fit as an inline extent. | |
341 | */ | |
a0349401 | 342 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, |
00361589 JB |
343 | u64 end, size_t compressed_size, |
344 | int compress_type, | |
345 | struct page **compressed_pages) | |
c8b97818 | 346 | { |
5893dfb9 | 347 | struct btrfs_drop_extents_args drop_args = { 0 }; |
a0349401 | 348 | struct btrfs_root *root = inode->root; |
0b246afa | 349 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 350 | struct btrfs_trans_handle *trans; |
a0349401 | 351 | u64 isize = i_size_read(&inode->vfs_inode); |
c8b97818 CM |
352 | u64 actual_end = min(end + 1, isize); |
353 | u64 inline_len = actual_end - start; | |
0b246afa | 354 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
355 | u64 data_len = inline_len; |
356 | int ret; | |
1acae57b | 357 | struct btrfs_path *path; |
c8b97818 CM |
358 | |
359 | if (compressed_size) | |
360 | data_len = compressed_size; | |
361 | ||
362 | if (start > 0 || | |
0b246afa JM |
363 | actual_end > fs_info->sectorsize || |
364 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 365 | (!compressed_size && |
0b246afa | 366 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 367 | end + 1 < isize || |
0b246afa | 368 | data_len > fs_info->max_inline) { |
c8b97818 CM |
369 | return 1; |
370 | } | |
371 | ||
1acae57b FDBM |
372 | path = btrfs_alloc_path(); |
373 | if (!path) | |
374 | return -ENOMEM; | |
375 | ||
00361589 | 376 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
377 | if (IS_ERR(trans)) { |
378 | btrfs_free_path(path); | |
00361589 | 379 | return PTR_ERR(trans); |
1acae57b | 380 | } |
a0349401 | 381 | trans->block_rsv = &inode->block_rsv; |
00361589 | 382 | |
5893dfb9 FM |
383 | drop_args.path = path; |
384 | drop_args.start = start; | |
385 | drop_args.end = aligned_end; | |
386 | drop_args.drop_cache = true; | |
387 | drop_args.replace_extent = true; | |
388 | ||
1acae57b | 389 | if (compressed_size && compressed_pages) |
5893dfb9 | 390 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
1acae57b FDBM |
391 | compressed_size); |
392 | else | |
5893dfb9 | 393 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
1acae57b FDBM |
394 | inline_len); |
395 | ||
5893dfb9 | 396 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
00361589 | 397 | if (ret) { |
66642832 | 398 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
399 | goto out; |
400 | } | |
c8b97818 CM |
401 | |
402 | if (isize > actual_end) | |
403 | inline_len = min_t(u64, isize, actual_end); | |
5893dfb9 | 404 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
a0349401 | 405 | root, &inode->vfs_inode, start, |
c8b97818 | 406 | inline_len, compressed_size, |
fe3f566c | 407 | compress_type, compressed_pages); |
2adcac1a | 408 | if (ret && ret != -ENOSPC) { |
66642832 | 409 | btrfs_abort_transaction(trans, ret); |
00361589 | 410 | goto out; |
2adcac1a | 411 | } else if (ret == -ENOSPC) { |
00361589 JB |
412 | ret = 1; |
413 | goto out; | |
79787eaa | 414 | } |
2adcac1a | 415 | |
2766ff61 | 416 | btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found); |
9a56fcd1 | 417 | ret = btrfs_update_inode(trans, root, inode); |
2766ff61 FM |
418 | if (ret && ret != -ENOSPC) { |
419 | btrfs_abort_transaction(trans, ret); | |
420 | goto out; | |
421 | } else if (ret == -ENOSPC) { | |
422 | ret = 1; | |
423 | goto out; | |
424 | } | |
425 | ||
a0349401 | 426 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
00361589 | 427 | out: |
94ed938a QW |
428 | /* |
429 | * Don't forget to free the reserved space, as for inlined extent | |
430 | * it won't count as data extent, free them directly here. | |
431 | * And at reserve time, it's always aligned to page size, so | |
432 | * just free one page here. | |
433 | */ | |
a0349401 | 434 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 435 | btrfs_free_path(path); |
3a45bb20 | 436 | btrfs_end_transaction(trans); |
00361589 | 437 | return ret; |
c8b97818 CM |
438 | } |
439 | ||
771ed689 CM |
440 | struct async_extent { |
441 | u64 start; | |
442 | u64 ram_size; | |
443 | u64 compressed_size; | |
444 | struct page **pages; | |
445 | unsigned long nr_pages; | |
261507a0 | 446 | int compress_type; |
771ed689 CM |
447 | struct list_head list; |
448 | }; | |
449 | ||
97db1204 | 450 | struct async_chunk { |
771ed689 | 451 | struct inode *inode; |
771ed689 CM |
452 | struct page *locked_page; |
453 | u64 start; | |
454 | u64 end; | |
f82b7359 | 455 | unsigned int write_flags; |
771ed689 | 456 | struct list_head extents; |
ec39f769 | 457 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 458 | struct btrfs_work work; |
9e895a8f | 459 | struct async_cow *async_cow; |
771ed689 CM |
460 | }; |
461 | ||
97db1204 | 462 | struct async_cow { |
97db1204 NB |
463 | atomic_t num_chunks; |
464 | struct async_chunk chunks[]; | |
771ed689 CM |
465 | }; |
466 | ||
97db1204 | 467 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
468 | u64 start, u64 ram_size, |
469 | u64 compressed_size, | |
470 | struct page **pages, | |
261507a0 LZ |
471 | unsigned long nr_pages, |
472 | int compress_type) | |
771ed689 CM |
473 | { |
474 | struct async_extent *async_extent; | |
475 | ||
476 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 477 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
478 | async_extent->start = start; |
479 | async_extent->ram_size = ram_size; | |
480 | async_extent->compressed_size = compressed_size; | |
481 | async_extent->pages = pages; | |
482 | async_extent->nr_pages = nr_pages; | |
261507a0 | 483 | async_extent->compress_type = compress_type; |
771ed689 CM |
484 | list_add_tail(&async_extent->list, &cow->extents); |
485 | return 0; | |
486 | } | |
487 | ||
42c16da6 QW |
488 | /* |
489 | * Check if the inode has flags compatible with compression | |
490 | */ | |
99c88dc7 | 491 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
42c16da6 | 492 | { |
99c88dc7 NB |
493 | if (inode->flags & BTRFS_INODE_NODATACOW || |
494 | inode->flags & BTRFS_INODE_NODATASUM) | |
42c16da6 QW |
495 | return false; |
496 | return true; | |
497 | } | |
498 | ||
499 | /* | |
500 | * Check if the inode needs to be submitted to compression, based on mount | |
501 | * options, defragmentation, properties or heuristics. | |
502 | */ | |
808a1292 NB |
503 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
504 | u64 end) | |
f79707b0 | 505 | { |
808a1292 | 506 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 507 | |
808a1292 | 508 | if (!inode_can_compress(inode)) { |
42c16da6 QW |
509 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
510 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 511 | btrfs_ino(inode)); |
42c16da6 QW |
512 | return 0; |
513 | } | |
0cf9b244 QW |
514 | /* |
515 | * Special check for subpage. | |
516 | * | |
517 | * We lock the full page then run each delalloc range in the page, thus | |
518 | * for the following case, we will hit some subpage specific corner case: | |
519 | * | |
520 | * 0 32K 64K | |
521 | * | |///////| |///////| | |
522 | * \- A \- B | |
523 | * | |
524 | * In above case, both range A and range B will try to unlock the full | |
525 | * page [0, 64K), causing the one finished later will have page | |
526 | * unlocked already, triggering various page lock requirement BUG_ON()s. | |
527 | * | |
528 | * So here we add an artificial limit that subpage compression can only | |
529 | * if the range is fully page aligned. | |
530 | * | |
531 | * In theory we only need to ensure the first page is fully covered, but | |
532 | * the tailing partial page will be locked until the full compression | |
533 | * finishes, delaying the write of other range. | |
534 | * | |
535 | * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range | |
536 | * first to prevent any submitted async extent to unlock the full page. | |
537 | * By this, we can ensure for subpage case that only the last async_cow | |
538 | * will unlock the full page. | |
539 | */ | |
540 | if (fs_info->sectorsize < PAGE_SIZE) { | |
541 | if (!IS_ALIGNED(start, PAGE_SIZE) || | |
542 | !IS_ALIGNED(end + 1, PAGE_SIZE)) | |
543 | return 0; | |
544 | } | |
545 | ||
f79707b0 | 546 | /* force compress */ |
0b246afa | 547 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 548 | return 1; |
eec63c65 | 549 | /* defrag ioctl */ |
808a1292 | 550 | if (inode->defrag_compress) |
eec63c65 | 551 | return 1; |
f79707b0 | 552 | /* bad compression ratios */ |
808a1292 | 553 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 554 | return 0; |
0b246afa | 555 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
556 | inode->flags & BTRFS_INODE_COMPRESS || |
557 | inode->prop_compress) | |
558 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
559 | return 0; |
560 | } | |
561 | ||
6158e1ce | 562 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
563 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
564 | { | |
565 | /* If this is a small write inside eof, kick off a defrag */ | |
566 | if (num_bytes < small_write && | |
6158e1ce | 567 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
568 | btrfs_add_inode_defrag(NULL, inode); |
569 | } | |
570 | ||
d352ac68 | 571 | /* |
771ed689 CM |
572 | * we create compressed extents in two phases. The first |
573 | * phase compresses a range of pages that have already been | |
574 | * locked (both pages and state bits are locked). | |
c8b97818 | 575 | * |
771ed689 CM |
576 | * This is done inside an ordered work queue, and the compression |
577 | * is spread across many cpus. The actual IO submission is step | |
578 | * two, and the ordered work queue takes care of making sure that | |
579 | * happens in the same order things were put onto the queue by | |
580 | * writepages and friends. | |
c8b97818 | 581 | * |
771ed689 CM |
582 | * If this code finds it can't get good compression, it puts an |
583 | * entry onto the work queue to write the uncompressed bytes. This | |
584 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
585 | * are written in the same order that the flusher thread sent them |
586 | * down. | |
d352ac68 | 587 | */ |
ac3e9933 | 588 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 589 | { |
1368c6da | 590 | struct inode *inode = async_chunk->inode; |
0b246afa | 591 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 592 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
593 | u64 start = async_chunk->start; |
594 | u64 end = async_chunk->end; | |
c8b97818 | 595 | u64 actual_end; |
d98da499 | 596 | u64 i_size; |
e6dcd2dc | 597 | int ret = 0; |
c8b97818 CM |
598 | struct page **pages = NULL; |
599 | unsigned long nr_pages; | |
c8b97818 CM |
600 | unsigned long total_compressed = 0; |
601 | unsigned long total_in = 0; | |
c8b97818 CM |
602 | int i; |
603 | int will_compress; | |
0b246afa | 604 | int compress_type = fs_info->compress_type; |
ac3e9933 | 605 | int compressed_extents = 0; |
4adaa611 | 606 | int redirty = 0; |
b888db2b | 607 | |
6158e1ce NB |
608 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
609 | SZ_16K); | |
4cb5300b | 610 | |
d98da499 JB |
611 | /* |
612 | * We need to save i_size before now because it could change in between | |
613 | * us evaluating the size and assigning it. This is because we lock and | |
614 | * unlock the page in truncate and fallocate, and then modify the i_size | |
615 | * later on. | |
616 | * | |
617 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
618 | * does that for us. | |
619 | */ | |
620 | barrier(); | |
621 | i_size = i_size_read(inode); | |
622 | barrier(); | |
623 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
624 | again: |
625 | will_compress = 0; | |
09cbfeaf | 626 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
627 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
628 | nr_pages = min_t(unsigned long, nr_pages, | |
629 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 630 | |
f03d9301 CM |
631 | /* |
632 | * we don't want to send crud past the end of i_size through | |
633 | * compression, that's just a waste of CPU time. So, if the | |
634 | * end of the file is before the start of our current | |
635 | * requested range of bytes, we bail out to the uncompressed | |
636 | * cleanup code that can deal with all of this. | |
637 | * | |
638 | * It isn't really the fastest way to fix things, but this is a | |
639 | * very uncommon corner. | |
640 | */ | |
641 | if (actual_end <= start) | |
642 | goto cleanup_and_bail_uncompressed; | |
643 | ||
c8b97818 CM |
644 | total_compressed = actual_end - start; |
645 | ||
4bcbb332 | 646 | /* |
0cf9b244 | 647 | * Skip compression for a small file range(<=blocksize) that |
01327610 | 648 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
649 | */ |
650 | if (total_compressed <= blocksize && | |
651 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
652 | goto cleanup_and_bail_uncompressed; | |
653 | ||
0cf9b244 QW |
654 | /* |
655 | * For subpage case, we require full page alignment for the sector | |
656 | * aligned range. | |
657 | * Thus we must also check against @actual_end, not just @end. | |
658 | */ | |
659 | if (blocksize < PAGE_SIZE) { | |
660 | if (!IS_ALIGNED(start, PAGE_SIZE) || | |
661 | !IS_ALIGNED(round_up(actual_end, blocksize), PAGE_SIZE)) | |
662 | goto cleanup_and_bail_uncompressed; | |
663 | } | |
664 | ||
069eac78 DS |
665 | total_compressed = min_t(unsigned long, total_compressed, |
666 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
667 | total_in = 0; |
668 | ret = 0; | |
db94535d | 669 | |
771ed689 CM |
670 | /* |
671 | * we do compression for mount -o compress and when the | |
672 | * inode has not been flagged as nocompress. This flag can | |
673 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 674 | */ |
4e965576 | 675 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
c8b97818 | 676 | WARN_ON(pages); |
31e818fe | 677 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
678 | if (!pages) { |
679 | /* just bail out to the uncompressed code */ | |
3527a018 | 680 | nr_pages = 0; |
560f7d75 LZ |
681 | goto cont; |
682 | } | |
c8b97818 | 683 | |
eec63c65 DS |
684 | if (BTRFS_I(inode)->defrag_compress) |
685 | compress_type = BTRFS_I(inode)->defrag_compress; | |
686 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 687 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 688 | |
4adaa611 CM |
689 | /* |
690 | * we need to call clear_page_dirty_for_io on each | |
691 | * page in the range. Otherwise applications with the file | |
692 | * mmap'd can wander in and change the page contents while | |
693 | * we are compressing them. | |
694 | * | |
695 | * If the compression fails for any reason, we set the pages | |
696 | * dirty again later on. | |
e9679de3 TT |
697 | * |
698 | * Note that the remaining part is redirtied, the start pointer | |
699 | * has moved, the end is the original one. | |
4adaa611 | 700 | */ |
e9679de3 TT |
701 | if (!redirty) { |
702 | extent_range_clear_dirty_for_io(inode, start, end); | |
703 | redirty = 1; | |
704 | } | |
f51d2b59 DS |
705 | |
706 | /* Compression level is applied here and only here */ | |
707 | ret = btrfs_compress_pages( | |
708 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 709 | inode->i_mapping, start, |
38c31464 | 710 | pages, |
4d3a800e | 711 | &nr_pages, |
261507a0 | 712 | &total_in, |
e5d74902 | 713 | &total_compressed); |
c8b97818 CM |
714 | |
715 | if (!ret) { | |
7073017a | 716 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 717 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
718 | |
719 | /* zero the tail end of the last page, we might be | |
720 | * sending it down to disk | |
721 | */ | |
d048b9c2 IW |
722 | if (offset) |
723 | memzero_page(page, offset, PAGE_SIZE - offset); | |
c8b97818 CM |
724 | will_compress = 1; |
725 | } | |
726 | } | |
560f7d75 | 727 | cont: |
7367253a QW |
728 | /* |
729 | * Check cow_file_range() for why we don't even try to create inline | |
730 | * extent for subpage case. | |
731 | */ | |
732 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
c8b97818 | 733 | /* lets try to make an inline extent */ |
6018ba0a | 734 | if (ret || total_in < actual_end) { |
c8b97818 | 735 | /* we didn't compress the entire range, try |
771ed689 | 736 | * to make an uncompressed inline extent. |
c8b97818 | 737 | */ |
a0349401 NB |
738 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
739 | 0, BTRFS_COMPRESS_NONE, | |
740 | NULL); | |
c8b97818 | 741 | } else { |
771ed689 | 742 | /* try making a compressed inline extent */ |
a0349401 | 743 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
fe3f566c LZ |
744 | total_compressed, |
745 | compress_type, pages); | |
c8b97818 | 746 | } |
79787eaa | 747 | if (ret <= 0) { |
151a41bc | 748 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
749 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
750 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
751 | unsigned long page_error_op; |
752 | ||
e6eb4314 | 753 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 754 | |
771ed689 | 755 | /* |
79787eaa JM |
756 | * inline extent creation worked or returned error, |
757 | * we don't need to create any more async work items. | |
758 | * Unlock and free up our temp pages. | |
8b62f87b JB |
759 | * |
760 | * We use DO_ACCOUNTING here because we need the | |
761 | * delalloc_release_metadata to be done _after_ we drop | |
762 | * our outstanding extent for clearing delalloc for this | |
763 | * range. | |
771ed689 | 764 | */ |
ad7ff17b NB |
765 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
766 | NULL, | |
74e9194a | 767 | clear_flags, |
ba8b04c1 | 768 | PAGE_UNLOCK | |
6869b0a8 | 769 | PAGE_START_WRITEBACK | |
e6eb4314 | 770 | page_error_op | |
c2790a2e | 771 | PAGE_END_WRITEBACK); |
cecc8d90 | 772 | |
1e6e238c QW |
773 | /* |
774 | * Ensure we only free the compressed pages if we have | |
775 | * them allocated, as we can still reach here with | |
776 | * inode_need_compress() == false. | |
777 | */ | |
778 | if (pages) { | |
779 | for (i = 0; i < nr_pages; i++) { | |
780 | WARN_ON(pages[i]->mapping); | |
781 | put_page(pages[i]); | |
782 | } | |
783 | kfree(pages); | |
cecc8d90 | 784 | } |
cecc8d90 | 785 | return 0; |
c8b97818 CM |
786 | } |
787 | } | |
788 | ||
789 | if (will_compress) { | |
790 | /* | |
791 | * we aren't doing an inline extent round the compressed size | |
792 | * up to a block size boundary so the allocator does sane | |
793 | * things | |
794 | */ | |
fda2832f | 795 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
796 | |
797 | /* | |
798 | * one last check to make sure the compression is really a | |
170607eb TT |
799 | * win, compare the page count read with the blocks on disk, |
800 | * compression must free at least one sector size | |
c8b97818 | 801 | */ |
4c162778 | 802 | total_in = round_up(total_in, fs_info->sectorsize); |
170607eb | 803 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 804 | compressed_extents++; |
c8bb0c8b AS |
805 | |
806 | /* | |
807 | * The async work queues will take care of doing actual | |
808 | * allocation on disk for these compressed pages, and | |
809 | * will submit them to the elevator. | |
810 | */ | |
b5326271 | 811 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 812 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
813 | compress_type); |
814 | ||
1170862d TT |
815 | if (start + total_in < end) { |
816 | start += total_in; | |
c8bb0c8b AS |
817 | pages = NULL; |
818 | cond_resched(); | |
819 | goto again; | |
820 | } | |
ac3e9933 | 821 | return compressed_extents; |
c8b97818 CM |
822 | } |
823 | } | |
c8bb0c8b | 824 | if (pages) { |
c8b97818 CM |
825 | /* |
826 | * the compression code ran but failed to make things smaller, | |
827 | * free any pages it allocated and our page pointer array | |
828 | */ | |
4d3a800e | 829 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 830 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 831 | put_page(pages[i]); |
c8b97818 CM |
832 | } |
833 | kfree(pages); | |
834 | pages = NULL; | |
835 | total_compressed = 0; | |
4d3a800e | 836 | nr_pages = 0; |
c8b97818 CM |
837 | |
838 | /* flag the file so we don't compress in the future */ | |
0b246afa | 839 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 840 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 841 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 842 | } |
c8b97818 | 843 | } |
f03d9301 | 844 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
845 | /* |
846 | * No compression, but we still need to write the pages in the file | |
847 | * we've been given so far. redirty the locked page if it corresponds | |
848 | * to our extent and set things up for the async work queue to run | |
849 | * cow_file_range to do the normal delalloc dance. | |
850 | */ | |
1d53c9e6 CM |
851 | if (async_chunk->locked_page && |
852 | (page_offset(async_chunk->locked_page) >= start && | |
853 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 854 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 855 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 856 | } |
c8bb0c8b AS |
857 | |
858 | if (redirty) | |
859 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 860 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 861 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 862 | compressed_extents++; |
3b951516 | 863 | |
ac3e9933 | 864 | return compressed_extents; |
771ed689 | 865 | } |
771ed689 | 866 | |
40ae837b FM |
867 | static void free_async_extent_pages(struct async_extent *async_extent) |
868 | { | |
869 | int i; | |
870 | ||
871 | if (!async_extent->pages) | |
872 | return; | |
873 | ||
874 | for (i = 0; i < async_extent->nr_pages; i++) { | |
875 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 876 | put_page(async_extent->pages[i]); |
40ae837b FM |
877 | } |
878 | kfree(async_extent->pages); | |
879 | async_extent->nr_pages = 0; | |
880 | async_extent->pages = NULL; | |
771ed689 CM |
881 | } |
882 | ||
2b83a0ee QW |
883 | static int submit_uncompressed_range(struct btrfs_inode *inode, |
884 | struct async_extent *async_extent, | |
885 | struct page *locked_page) | |
771ed689 | 886 | { |
2b83a0ee QW |
887 | u64 start = async_extent->start; |
888 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
889 | unsigned long nr_written = 0; | |
890 | int page_started = 0; | |
891 | int ret; | |
771ed689 | 892 | |
2b83a0ee QW |
893 | /* |
894 | * Call cow_file_range() to run the delalloc range directly, since we | |
895 | * won't go to NOCOW or async path again. | |
896 | * | |
897 | * Also we call cow_file_range() with @unlock_page == 0, so that we | |
898 | * can directly submit them without interruption. | |
899 | */ | |
900 | ret = cow_file_range(inode, locked_page, start, end, &page_started, | |
901 | &nr_written, 0); | |
902 | /* Inline extent inserted, page gets unlocked and everything is done */ | |
903 | if (page_started) { | |
904 | ret = 0; | |
905 | goto out; | |
906 | } | |
907 | if (ret < 0) { | |
908 | if (locked_page) | |
909 | unlock_page(locked_page); | |
910 | goto out; | |
911 | } | |
771ed689 | 912 | |
2b83a0ee QW |
913 | ret = extent_write_locked_range(&inode->vfs_inode, start, end); |
914 | /* All pages will be unlocked, including @locked_page */ | |
915 | out: | |
916 | kfree(async_extent); | |
917 | return ret; | |
918 | } | |
79787eaa | 919 | |
b4ccace8 QW |
920 | static int submit_one_async_extent(struct btrfs_inode *inode, |
921 | struct async_chunk *async_chunk, | |
922 | struct async_extent *async_extent, | |
923 | u64 *alloc_hint) | |
771ed689 | 924 | { |
b4ccace8 QW |
925 | struct extent_io_tree *io_tree = &inode->io_tree; |
926 | struct btrfs_root *root = inode->root; | |
927 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 | 928 | struct btrfs_key ins; |
2b83a0ee | 929 | struct page *locked_page = NULL; |
771ed689 | 930 | struct extent_map *em; |
f5a84ee3 | 931 | int ret = 0; |
b4ccace8 QW |
932 | u64 start = async_extent->start; |
933 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
771ed689 | 934 | |
2b83a0ee QW |
935 | /* |
936 | * If async_chunk->locked_page is in the async_extent range, we need to | |
937 | * handle it. | |
938 | */ | |
939 | if (async_chunk->locked_page) { | |
940 | u64 locked_page_start = page_offset(async_chunk->locked_page); | |
941 | u64 locked_page_end = locked_page_start + PAGE_SIZE - 1; | |
3e04e7f1 | 942 | |
2b83a0ee QW |
943 | if (!(start >= locked_page_end || end <= locked_page_start)) |
944 | locked_page = async_chunk->locked_page; | |
b4ccace8 | 945 | } |
2b83a0ee | 946 | lock_extent(io_tree, start, end); |
ce62003f | 947 | |
2b83a0ee QW |
948 | /* We have fall back to uncompressed write */ |
949 | if (!async_extent->pages) | |
950 | return submit_uncompressed_range(inode, async_extent, locked_page); | |
ce62003f | 951 | |
b4ccace8 QW |
952 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
953 | async_extent->compressed_size, | |
954 | async_extent->compressed_size, | |
955 | 0, *alloc_hint, &ins, 1, 1); | |
956 | if (ret) { | |
957 | free_async_extent_pages(async_extent); | |
c2167754 | 958 | /* |
b4ccace8 QW |
959 | * Here we used to try again by going back to non-compressed |
960 | * path for ENOSPC. But we can't reserve space even for | |
961 | * compressed size, how could it work for uncompressed size | |
962 | * which requires larger size? So here we directly go error | |
963 | * path. | |
c2167754 | 964 | */ |
b4ccace8 QW |
965 | goto out_free; |
966 | } | |
967 | ||
968 | /* Here we're doing allocation and writeback of the compressed pages */ | |
969 | em = create_io_em(inode, start, | |
970 | async_extent->ram_size, /* len */ | |
971 | start, /* orig_start */ | |
972 | ins.objectid, /* block_start */ | |
973 | ins.offset, /* block_len */ | |
974 | ins.offset, /* orig_block_len */ | |
975 | async_extent->ram_size, /* ram_bytes */ | |
976 | async_extent->compress_type, | |
977 | BTRFS_ORDERED_COMPRESSED); | |
978 | if (IS_ERR(em)) { | |
979 | ret = PTR_ERR(em); | |
980 | goto out_free_reserve; | |
981 | } | |
982 | free_extent_map(em); | |
771ed689 | 983 | |
b4ccace8 QW |
984 | ret = btrfs_add_ordered_extent_compress(inode, start, /* file_offset */ |
985 | ins.objectid, /* disk_bytenr */ | |
986 | async_extent->ram_size, /* num_bytes */ | |
987 | ins.offset, /* disk_num_bytes */ | |
988 | async_extent->compress_type); | |
989 | if (ret) { | |
990 | btrfs_drop_extent_cache(inode, start, end, 0); | |
991 | goto out_free_reserve; | |
771ed689 | 992 | } |
b4ccace8 QW |
993 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
994 | ||
995 | /* Clear dirty, set writeback and unlock the pages. */ | |
996 | extent_clear_unlock_delalloc(inode, start, end, | |
997 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, | |
998 | PAGE_UNLOCK | PAGE_START_WRITEBACK); | |
999 | if (btrfs_submit_compressed_write(inode, start, /* file_offset */ | |
1000 | async_extent->ram_size, /* num_bytes */ | |
1001 | ins.objectid, /* disk_bytenr */ | |
1002 | ins.offset, /* compressed_len */ | |
1003 | async_extent->pages, /* compressed_pages */ | |
1004 | async_extent->nr_pages, | |
1005 | async_chunk->write_flags, | |
1006 | async_chunk->blkcg_css)) { | |
1007 | const u64 start = async_extent->start; | |
1008 | const u64 end = start + async_extent->ram_size - 1; | |
1009 | ||
1010 | btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0); | |
1011 | ||
1012 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
1013 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); | |
1014 | free_async_extent_pages(async_extent); | |
771ed689 | 1015 | } |
b4ccace8 QW |
1016 | *alloc_hint = ins.objectid + ins.offset; |
1017 | kfree(async_extent); | |
1018 | return ret; | |
1019 | ||
3e04e7f1 | 1020 | out_free_reserve: |
0b246afa | 1021 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1022 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1023 | out_free: |
b4ccace8 | 1024 | extent_clear_unlock_delalloc(inode, start, end, |
c2790a2e | 1025 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 1026 | EXTENT_DELALLOC_NEW | |
151a41bc | 1027 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
6869b0a8 QW |
1028 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
1029 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); | |
40ae837b | 1030 | free_async_extent_pages(async_extent); |
79787eaa | 1031 | kfree(async_extent); |
b4ccace8 QW |
1032 | return ret; |
1033 | } | |
1034 | ||
1035 | /* | |
1036 | * Phase two of compressed writeback. This is the ordered portion of the code, | |
1037 | * which only gets called in the order the work was queued. We walk all the | |
1038 | * async extents created by compress_file_range and send them down to the disk. | |
1039 | */ | |
1040 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) | |
1041 | { | |
1042 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); | |
1043 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1044 | struct async_extent *async_extent; | |
1045 | u64 alloc_hint = 0; | |
1046 | int ret = 0; | |
1047 | ||
1048 | while (!list_empty(&async_chunk->extents)) { | |
1049 | u64 extent_start; | |
1050 | u64 ram_size; | |
1051 | ||
1052 | async_extent = list_entry(async_chunk->extents.next, | |
1053 | struct async_extent, list); | |
1054 | list_del(&async_extent->list); | |
1055 | extent_start = async_extent->start; | |
1056 | ram_size = async_extent->ram_size; | |
1057 | ||
1058 | ret = submit_one_async_extent(inode, async_chunk, async_extent, | |
1059 | &alloc_hint); | |
1060 | btrfs_debug(fs_info, | |
1061 | "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d", | |
1062 | inode->root->root_key.objectid, | |
1063 | btrfs_ino(inode), extent_start, ram_size, ret); | |
1064 | } | |
771ed689 CM |
1065 | } |
1066 | ||
43c69849 | 1067 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
1068 | u64 num_bytes) |
1069 | { | |
43c69849 | 1070 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
1071 | struct extent_map *em; |
1072 | u64 alloc_hint = 0; | |
1073 | ||
1074 | read_lock(&em_tree->lock); | |
1075 | em = search_extent_mapping(em_tree, start, num_bytes); | |
1076 | if (em) { | |
1077 | /* | |
1078 | * if block start isn't an actual block number then find the | |
1079 | * first block in this inode and use that as a hint. If that | |
1080 | * block is also bogus then just don't worry about it. | |
1081 | */ | |
1082 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
1083 | free_extent_map(em); | |
1084 | em = search_extent_mapping(em_tree, 0, 0); | |
1085 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
1086 | alloc_hint = em->block_start; | |
1087 | if (em) | |
1088 | free_extent_map(em); | |
1089 | } else { | |
1090 | alloc_hint = em->block_start; | |
1091 | free_extent_map(em); | |
1092 | } | |
1093 | } | |
1094 | read_unlock(&em_tree->lock); | |
1095 | ||
1096 | return alloc_hint; | |
1097 | } | |
1098 | ||
771ed689 CM |
1099 | /* |
1100 | * when extent_io.c finds a delayed allocation range in the file, | |
1101 | * the call backs end up in this code. The basic idea is to | |
1102 | * allocate extents on disk for the range, and create ordered data structs | |
1103 | * in ram to track those extents. | |
1104 | * | |
1105 | * locked_page is the page that writepage had locked already. We use | |
1106 | * it to make sure we don't do extra locks or unlocks. | |
1107 | * | |
1108 | * *page_started is set to one if we unlock locked_page and do everything | |
1109 | * required to start IO on it. It may be clean and already done with | |
1110 | * IO when we return. | |
1111 | */ | |
6e26c442 | 1112 | static noinline int cow_file_range(struct btrfs_inode *inode, |
00361589 | 1113 | struct page *locked_page, |
74e9194a | 1114 | u64 start, u64 end, int *page_started, |
330a5827 | 1115 | unsigned long *nr_written, int unlock) |
771ed689 | 1116 | { |
6e26c442 NB |
1117 | struct btrfs_root *root = inode->root; |
1118 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 CM |
1119 | u64 alloc_hint = 0; |
1120 | u64 num_bytes; | |
1121 | unsigned long ram_size; | |
a315e68f | 1122 | u64 cur_alloc_size = 0; |
432cd2a1 | 1123 | u64 min_alloc_size; |
0b246afa | 1124 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
1125 | struct btrfs_key ins; |
1126 | struct extent_map *em; | |
a315e68f FM |
1127 | unsigned clear_bits; |
1128 | unsigned long page_ops; | |
1129 | bool extent_reserved = false; | |
771ed689 CM |
1130 | int ret = 0; |
1131 | ||
6e26c442 | 1132 | if (btrfs_is_free_space_inode(inode)) { |
02ecd2c2 | 1133 | WARN_ON_ONCE(1); |
29bce2f3 JB |
1134 | ret = -EINVAL; |
1135 | goto out_unlock; | |
02ecd2c2 | 1136 | } |
771ed689 | 1137 | |
fda2832f | 1138 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1139 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1140 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1141 | |
6e26c442 | 1142 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1143 | |
7367253a QW |
1144 | /* |
1145 | * Due to the page size limit, for subpage we can only trigger the | |
1146 | * writeback for the dirty sectors of page, that means data writeback | |
1147 | * is doing more writeback than what we want. | |
1148 | * | |
1149 | * This is especially unexpected for some call sites like fallocate, | |
1150 | * where we only increase i_size after everything is done. | |
1151 | * This means we can trigger inline extent even if we didn't want to. | |
1152 | * So here we skip inline extent creation completely. | |
1153 | */ | |
1154 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
771ed689 | 1155 | /* lets try to make an inline extent */ |
6e26c442 | 1156 | ret = cow_file_range_inline(inode, start, end, 0, |
d02c0e20 | 1157 | BTRFS_COMPRESS_NONE, NULL); |
771ed689 | 1158 | if (ret == 0) { |
8b62f87b JB |
1159 | /* |
1160 | * We use DO_ACCOUNTING here because we need the | |
1161 | * delalloc_release_metadata to be run _after_ we drop | |
1162 | * our outstanding extent for clearing delalloc for this | |
1163 | * range. | |
1164 | */ | |
4750af3b QW |
1165 | extent_clear_unlock_delalloc(inode, start, end, |
1166 | locked_page, | |
c2790a2e | 1167 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1168 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1169 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 1170 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
771ed689 | 1171 | *nr_written = *nr_written + |
09cbfeaf | 1172 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1173 | *page_started = 1; |
4750af3b QW |
1174 | /* |
1175 | * locked_page is locked by the caller of | |
1176 | * writepage_delalloc(), not locked by | |
1177 | * __process_pages_contig(). | |
1178 | * | |
1179 | * We can't let __process_pages_contig() to unlock it, | |
1180 | * as it doesn't have any subpage::writers recorded. | |
1181 | * | |
1182 | * Here we manually unlock the page, since the caller | |
1183 | * can't use page_started to determine if it's an | |
1184 | * inline extent or a compressed extent. | |
1185 | */ | |
1186 | unlock_page(locked_page); | |
771ed689 | 1187 | goto out; |
79787eaa | 1188 | } else if (ret < 0) { |
79787eaa | 1189 | goto out_unlock; |
771ed689 CM |
1190 | } |
1191 | } | |
1192 | ||
6e26c442 NB |
1193 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
1194 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | |
771ed689 | 1195 | |
432cd2a1 FM |
1196 | /* |
1197 | * Relocation relies on the relocated extents to have exactly the same | |
1198 | * size as the original extents. Normally writeback for relocation data | |
1199 | * extents follows a NOCOW path because relocation preallocates the | |
1200 | * extents. However, due to an operation such as scrub turning a block | |
1201 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1202 | * an extent allocated during COW has exactly the requested size and can | |
1203 | * not be split into smaller extents, otherwise relocation breaks and | |
1204 | * fails during the stage where it updates the bytenr of file extent | |
1205 | * items. | |
1206 | */ | |
37f00a6d | 1207 | if (btrfs_is_data_reloc_root(root)) |
432cd2a1 FM |
1208 | min_alloc_size = num_bytes; |
1209 | else | |
1210 | min_alloc_size = fs_info->sectorsize; | |
1211 | ||
3752d22f AJ |
1212 | while (num_bytes > 0) { |
1213 | cur_alloc_size = num_bytes; | |
18513091 | 1214 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1215 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1216 | &ins, 1, 1); |
00361589 | 1217 | if (ret < 0) |
79787eaa | 1218 | goto out_unlock; |
a315e68f FM |
1219 | cur_alloc_size = ins.offset; |
1220 | extent_reserved = true; | |
d397712b | 1221 | |
771ed689 | 1222 | ram_size = ins.offset; |
6e26c442 | 1223 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1224 | start, /* orig_start */ |
1225 | ins.objectid, /* block_start */ | |
1226 | ins.offset, /* block_len */ | |
1227 | ins.offset, /* orig_block_len */ | |
1228 | ram_size, /* ram_bytes */ | |
1229 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1230 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1231 | if (IS_ERR(em)) { |
1232 | ret = PTR_ERR(em); | |
ace68bac | 1233 | goto out_reserve; |
090a127a | 1234 | } |
6f9994db | 1235 | free_extent_map(em); |
e6dcd2dc | 1236 | |
6e26c442 | 1237 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
3c198fe0 QW |
1238 | ram_size, cur_alloc_size, |
1239 | BTRFS_ORDERED_REGULAR); | |
ace68bac | 1240 | if (ret) |
d9f85963 | 1241 | goto out_drop_extent_cache; |
c8b97818 | 1242 | |
37f00a6d | 1243 | if (btrfs_is_data_reloc_root(root)) { |
6e26c442 | 1244 | ret = btrfs_reloc_clone_csums(inode, start, |
17d217fe | 1245 | cur_alloc_size); |
4dbd80fb QW |
1246 | /* |
1247 | * Only drop cache here, and process as normal. | |
1248 | * | |
1249 | * We must not allow extent_clear_unlock_delalloc() | |
1250 | * at out_unlock label to free meta of this ordered | |
1251 | * extent, as its meta should be freed by | |
1252 | * btrfs_finish_ordered_io(). | |
1253 | * | |
1254 | * So we must continue until @start is increased to | |
1255 | * skip current ordered extent. | |
1256 | */ | |
00361589 | 1257 | if (ret) |
6e26c442 | 1258 | btrfs_drop_extent_cache(inode, start, |
4dbd80fb | 1259 | start + ram_size - 1, 0); |
17d217fe YZ |
1260 | } |
1261 | ||
0b246afa | 1262 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1263 | |
f57ad937 QW |
1264 | /* |
1265 | * We're not doing compressed IO, don't unlock the first page | |
1266 | * (which the caller expects to stay locked), don't clear any | |
1267 | * dirty bits and don't set any writeback bits | |
8b62b72b | 1268 | * |
f57ad937 QW |
1269 | * Do set the Ordered (Private2) bit so we know this page was |
1270 | * properly setup for writepage. | |
c8b97818 | 1271 | */ |
a315e68f | 1272 | page_ops = unlock ? PAGE_UNLOCK : 0; |
f57ad937 | 1273 | page_ops |= PAGE_SET_ORDERED; |
a791e35e | 1274 | |
6e26c442 | 1275 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1276 | locked_page, |
c2790a2e | 1277 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1278 | page_ops); |
3752d22f AJ |
1279 | if (num_bytes < cur_alloc_size) |
1280 | num_bytes = 0; | |
4dbd80fb | 1281 | else |
3752d22f | 1282 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1283 | alloc_hint = ins.objectid + ins.offset; |
1284 | start += cur_alloc_size; | |
a315e68f | 1285 | extent_reserved = false; |
4dbd80fb QW |
1286 | |
1287 | /* | |
1288 | * btrfs_reloc_clone_csums() error, since start is increased | |
1289 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1290 | * free metadata of current ordered extent, we're OK to exit. | |
1291 | */ | |
1292 | if (ret) | |
1293 | goto out_unlock; | |
b888db2b | 1294 | } |
79787eaa | 1295 | out: |
be20aa9d | 1296 | return ret; |
b7d5b0a8 | 1297 | |
d9f85963 | 1298 | out_drop_extent_cache: |
6e26c442 | 1299 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
ace68bac | 1300 | out_reserve: |
0b246afa | 1301 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1302 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1303 | out_unlock: |
a7e3b975 FM |
1304 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1305 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
6869b0a8 | 1306 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
a315e68f FM |
1307 | /* |
1308 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1309 | * failed to create the respective ordered extent, then it means that | |
1310 | * when we reserved the extent we decremented the extent's size from | |
1311 | * the data space_info's bytes_may_use counter and incremented the | |
1312 | * space_info's bytes_reserved counter by the same amount. We must make | |
1313 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1314 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1315 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1316 | */ | |
1317 | if (extent_reserved) { | |
6e26c442 | 1318 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1319 | start + cur_alloc_size - 1, |
a315e68f FM |
1320 | locked_page, |
1321 | clear_bits, | |
1322 | page_ops); | |
1323 | start += cur_alloc_size; | |
1324 | if (start >= end) | |
1325 | goto out; | |
1326 | } | |
6e26c442 | 1327 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1328 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1329 | page_ops); | |
79787eaa | 1330 | goto out; |
771ed689 | 1331 | } |
c8b97818 | 1332 | |
771ed689 CM |
1333 | /* |
1334 | * work queue call back to started compression on a file and pages | |
1335 | */ | |
1336 | static noinline void async_cow_start(struct btrfs_work *work) | |
1337 | { | |
b5326271 | 1338 | struct async_chunk *async_chunk; |
ac3e9933 | 1339 | int compressed_extents; |
771ed689 | 1340 | |
b5326271 | 1341 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1342 | |
ac3e9933 NB |
1343 | compressed_extents = compress_file_range(async_chunk); |
1344 | if (compressed_extents == 0) { | |
b5326271 NB |
1345 | btrfs_add_delayed_iput(async_chunk->inode); |
1346 | async_chunk->inode = NULL; | |
8180ef88 | 1347 | } |
771ed689 CM |
1348 | } |
1349 | ||
1350 | /* | |
1351 | * work queue call back to submit previously compressed pages | |
1352 | */ | |
1353 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1354 | { | |
c5a68aec NB |
1355 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1356 | work); | |
1357 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1358 | unsigned long nr_pages; |
1359 | ||
b5326271 | 1360 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1361 | PAGE_SHIFT; |
771ed689 | 1362 | |
4546d178 | 1363 | /* |
b5326271 | 1364 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1365 | * in which case we don't have anything to submit, yet we need to |
1366 | * always adjust ->async_delalloc_pages as its paired with the init | |
1367 | * happening in cow_file_range_async | |
1368 | */ | |
b5326271 NB |
1369 | if (async_chunk->inode) |
1370 | submit_compressed_extents(async_chunk); | |
ac98141d JB |
1371 | |
1372 | /* atomic_sub_return implies a barrier */ | |
1373 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < | |
1374 | 5 * SZ_1M) | |
1375 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1376 | } |
c8b97818 | 1377 | |
771ed689 CM |
1378 | static noinline void async_cow_free(struct btrfs_work *work) |
1379 | { | |
b5326271 | 1380 | struct async_chunk *async_chunk; |
9e895a8f | 1381 | struct async_cow *async_cow; |
97db1204 | 1382 | |
b5326271 NB |
1383 | async_chunk = container_of(work, struct async_chunk, work); |
1384 | if (async_chunk->inode) | |
1385 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1386 | if (async_chunk->blkcg_css) |
1387 | css_put(async_chunk->blkcg_css); | |
9e895a8f QW |
1388 | |
1389 | async_cow = async_chunk->async_cow; | |
1390 | if (atomic_dec_and_test(&async_cow->num_chunks)) | |
1391 | kvfree(async_cow); | |
771ed689 CM |
1392 | } |
1393 | ||
751b6431 | 1394 | static int cow_file_range_async(struct btrfs_inode *inode, |
ec39f769 CM |
1395 | struct writeback_control *wbc, |
1396 | struct page *locked_page, | |
771ed689 | 1397 | u64 start, u64 end, int *page_started, |
fac07d2b | 1398 | unsigned long *nr_written) |
771ed689 | 1399 | { |
751b6431 | 1400 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1401 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1402 | struct async_cow *ctx; |
1403 | struct async_chunk *async_chunk; | |
771ed689 CM |
1404 | unsigned long nr_pages; |
1405 | u64 cur_end; | |
97db1204 NB |
1406 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1407 | int i; | |
1408 | bool should_compress; | |
b1c16ac9 | 1409 | unsigned nofs_flag; |
fac07d2b | 1410 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1411 | |
751b6431 | 1412 | unlock_extent(&inode->io_tree, start, end); |
97db1204 | 1413 | |
751b6431 | 1414 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
97db1204 NB |
1415 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
1416 | num_chunks = 1; | |
1417 | should_compress = false; | |
1418 | } else { | |
1419 | should_compress = true; | |
1420 | } | |
1421 | ||
b1c16ac9 NB |
1422 | nofs_flag = memalloc_nofs_save(); |
1423 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1424 | memalloc_nofs_restore(nofs_flag); | |
1425 | ||
97db1204 NB |
1426 | if (!ctx) { |
1427 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1428 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1429 | EXTENT_DO_ACCOUNTING; | |
6869b0a8 QW |
1430 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
1431 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; | |
97db1204 | 1432 | |
751b6431 NB |
1433 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1434 | clear_bits, page_ops); | |
97db1204 NB |
1435 | return -ENOMEM; |
1436 | } | |
1437 | ||
1438 | async_chunk = ctx->chunks; | |
1439 | atomic_set(&ctx->num_chunks, num_chunks); | |
1440 | ||
1441 | for (i = 0; i < num_chunks; i++) { | |
1442 | if (should_compress) | |
1443 | cur_end = min(end, start + SZ_512K - 1); | |
1444 | else | |
1445 | cur_end = end; | |
771ed689 | 1446 | |
bd4691a0 NB |
1447 | /* |
1448 | * igrab is called higher up in the call chain, take only the | |
1449 | * lightweight reference for the callback lifetime | |
1450 | */ | |
751b6431 | 1451 | ihold(&inode->vfs_inode); |
9e895a8f | 1452 | async_chunk[i].async_cow = ctx; |
751b6431 | 1453 | async_chunk[i].inode = &inode->vfs_inode; |
97db1204 NB |
1454 | async_chunk[i].start = start; |
1455 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1456 | async_chunk[i].write_flags = write_flags; |
1457 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1458 | ||
1d53c9e6 CM |
1459 | /* |
1460 | * The locked_page comes all the way from writepage and its | |
1461 | * the original page we were actually given. As we spread | |
1462 | * this large delalloc region across multiple async_chunk | |
1463 | * structs, only the first struct needs a pointer to locked_page | |
1464 | * | |
1465 | * This way we don't need racey decisions about who is supposed | |
1466 | * to unlock it. | |
1467 | */ | |
1468 | if (locked_page) { | |
ec39f769 CM |
1469 | /* |
1470 | * Depending on the compressibility, the pages might or | |
1471 | * might not go through async. We want all of them to | |
1472 | * be accounted against wbc once. Let's do it here | |
1473 | * before the paths diverge. wbc accounting is used | |
1474 | * only for foreign writeback detection and doesn't | |
1475 | * need full accuracy. Just account the whole thing | |
1476 | * against the first page. | |
1477 | */ | |
1478 | wbc_account_cgroup_owner(wbc, locked_page, | |
1479 | cur_end - start); | |
1d53c9e6 CM |
1480 | async_chunk[i].locked_page = locked_page; |
1481 | locked_page = NULL; | |
1482 | } else { | |
1483 | async_chunk[i].locked_page = NULL; | |
1484 | } | |
1485 | ||
ec39f769 CM |
1486 | if (blkcg_css != blkcg_root_css) { |
1487 | css_get(blkcg_css); | |
1488 | async_chunk[i].blkcg_css = blkcg_css; | |
1489 | } else { | |
1490 | async_chunk[i].blkcg_css = NULL; | |
1491 | } | |
1492 | ||
a0cac0ec OS |
1493 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1494 | async_cow_submit, async_cow_free); | |
771ed689 | 1495 | |
97db1204 | 1496 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1497 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1498 | |
97db1204 | 1499 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1500 | |
771ed689 CM |
1501 | *nr_written += nr_pages; |
1502 | start = cur_end + 1; | |
1503 | } | |
1504 | *page_started = 1; | |
1505 | return 0; | |
be20aa9d CM |
1506 | } |
1507 | ||
42c01100 NA |
1508 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
1509 | struct page *locked_page, u64 start, | |
1510 | u64 end, int *page_started, | |
1511 | unsigned long *nr_written) | |
1512 | { | |
1513 | int ret; | |
1514 | ||
1515 | ret = cow_file_range(inode, locked_page, start, end, page_started, | |
1516 | nr_written, 0); | |
1517 | if (ret) | |
1518 | return ret; | |
1519 | ||
1520 | if (*page_started) | |
1521 | return 0; | |
1522 | ||
1523 | __set_page_dirty_nobuffers(locked_page); | |
1524 | account_page_redirty(locked_page); | |
2bd0fc93 | 1525 | extent_write_locked_range(&inode->vfs_inode, start, end); |
42c01100 NA |
1526 | *page_started = 1; |
1527 | ||
1528 | return 0; | |
1529 | } | |
1530 | ||
2ff7e61e | 1531 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1532 | u64 bytenr, u64 num_bytes) |
1533 | { | |
fc28b25e | 1534 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); |
17d217fe | 1535 | struct btrfs_ordered_sum *sums; |
fc28b25e | 1536 | int ret; |
17d217fe YZ |
1537 | LIST_HEAD(list); |
1538 | ||
fc28b25e | 1539 | ret = btrfs_lookup_csums_range(csum_root, bytenr, |
a2de733c | 1540 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1541 | if (ret == 0 && list_empty(&list)) |
1542 | return 0; | |
1543 | ||
1544 | while (!list_empty(&list)) { | |
1545 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1546 | list_del(&sums->list); | |
1547 | kfree(sums); | |
1548 | } | |
58113753 LB |
1549 | if (ret < 0) |
1550 | return ret; | |
17d217fe YZ |
1551 | return 1; |
1552 | } | |
1553 | ||
8ba96f3d | 1554 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
467dc47e FM |
1555 | const u64 start, const u64 end, |
1556 | int *page_started, unsigned long *nr_written) | |
1557 | { | |
8ba96f3d | 1558 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
37f00a6d | 1559 | const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); |
2166e5ed | 1560 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1561 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1562 | u64 range_start = start; |
1563 | u64 count; | |
1564 | ||
1565 | /* | |
1566 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1567 | * made we had not enough available data space and therefore we did not | |
1568 | * reserve data space for it, since we though we could do NOCOW for the | |
1569 | * respective file range (either there is prealloc extent or the inode | |
1570 | * has the NOCOW bit set). | |
1571 | * | |
1572 | * However when we need to fallback to COW mode (because for example the | |
1573 | * block group for the corresponding extent was turned to RO mode by a | |
1574 | * scrub or relocation) we need to do the following: | |
1575 | * | |
1576 | * 1) We increment the bytes_may_use counter of the data space info. | |
1577 | * If COW succeeds, it allocates a new data extent and after doing | |
1578 | * that it decrements the space info's bytes_may_use counter and | |
1579 | * increments its bytes_reserved counter by the same amount (we do | |
1580 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1581 | * bytes_may_use counter to compensate (when space is reserved at | |
1582 | * buffered write time, the bytes_may_use counter is incremented); | |
1583 | * | |
1584 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1585 | * that if the COW path fails for any reason, it decrements (through | |
1586 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1587 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1588 | * |
1589 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1590 | * space cache inode or an inode of the data relocation tree, we must |
1591 | * also increment bytes_may_use of the data space_info for the same | |
1592 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1593 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1594 | * group that contains that extent to RO mode and therefore force COW |
1595 | * when starting writeback. | |
467dc47e | 1596 | */ |
2166e5ed | 1597 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
467dc47e | 1598 | EXTENT_NORESERVE, 0); |
6bd335b4 FM |
1599 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1600 | u64 bytes = count; | |
8ba96f3d | 1601 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1602 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1603 | ||
6bd335b4 FM |
1604 | if (is_space_ino || is_reloc_ino) |
1605 | bytes = range_bytes; | |
1606 | ||
467dc47e | 1607 | spin_lock(&sinfo->lock); |
2166e5ed | 1608 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1609 | spin_unlock(&sinfo->lock); |
1610 | ||
2166e5ed FM |
1611 | if (count > 0) |
1612 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
1613 | 0, 0, NULL); | |
467dc47e FM |
1614 | } |
1615 | ||
8ba96f3d NB |
1616 | return cow_file_range(inode, locked_page, start, end, page_started, |
1617 | nr_written, 1); | |
467dc47e FM |
1618 | } |
1619 | ||
d352ac68 CM |
1620 | /* |
1621 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1622 | * of the extents that exist in the file, and COWs the file as required. | |
1623 | * | |
1624 | * If no cow copies or snapshots exist, we write directly to the existing | |
1625 | * blocks on disk | |
1626 | */ | |
968322c8 | 1627 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1628 | struct page *locked_page, |
3e024846 | 1629 | const u64 start, const u64 end, |
6e65ae76 | 1630 | int *page_started, |
3e024846 | 1631 | unsigned long *nr_written) |
be20aa9d | 1632 | { |
968322c8 NB |
1633 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1634 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1635 | struct btrfs_path *path; |
3e024846 NB |
1636 | u64 cow_start = (u64)-1; |
1637 | u64 cur_offset = start; | |
8ecebf4d | 1638 | int ret; |
3e024846 | 1639 | bool check_prev = true; |
968322c8 NB |
1640 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
1641 | u64 ino = btrfs_ino(inode); | |
762bf098 NB |
1642 | bool nocow = false; |
1643 | u64 disk_bytenr = 0; | |
6e65ae76 | 1644 | const bool force = inode->flags & BTRFS_INODE_NODATACOW; |
be20aa9d CM |
1645 | |
1646 | path = btrfs_alloc_path(); | |
17ca04af | 1647 | if (!path) { |
968322c8 | 1648 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
c2790a2e | 1649 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1650 | EXTENT_DO_ACCOUNTING | |
1651 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
6869b0a8 | 1652 | PAGE_START_WRITEBACK | |
c2790a2e | 1653 | PAGE_END_WRITEBACK); |
d8926bb3 | 1654 | return -ENOMEM; |
17ca04af | 1655 | } |
82d5902d | 1656 | |
80ff3856 | 1657 | while (1) { |
3e024846 NB |
1658 | struct btrfs_key found_key; |
1659 | struct btrfs_file_extent_item *fi; | |
1660 | struct extent_buffer *leaf; | |
1661 | u64 extent_end; | |
1662 | u64 extent_offset; | |
3e024846 NB |
1663 | u64 num_bytes = 0; |
1664 | u64 disk_num_bytes; | |
3e024846 NB |
1665 | u64 ram_bytes; |
1666 | int extent_type; | |
762bf098 NB |
1667 | |
1668 | nocow = false; | |
3e024846 | 1669 | |
e4c3b2dc | 1670 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1671 | cur_offset, 0); |
d788a349 | 1672 | if (ret < 0) |
79787eaa | 1673 | goto error; |
a6bd9cd1 NB |
1674 | |
1675 | /* | |
1676 | * If there is no extent for our range when doing the initial | |
1677 | * search, then go back to the previous slot as it will be the | |
1678 | * one containing the search offset | |
1679 | */ | |
80ff3856 YZ |
1680 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1681 | leaf = path->nodes[0]; | |
1682 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1683 | path->slots[0] - 1); | |
33345d01 | 1684 | if (found_key.objectid == ino && |
80ff3856 YZ |
1685 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1686 | path->slots[0]--; | |
1687 | } | |
3e024846 | 1688 | check_prev = false; |
80ff3856 | 1689 | next_slot: |
a6bd9cd1 | 1690 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1691 | leaf = path->nodes[0]; |
1692 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1693 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1694 | if (ret < 0) { |
1695 | if (cow_start != (u64)-1) | |
1696 | cur_offset = cow_start; | |
79787eaa | 1697 | goto error; |
e8916699 | 1698 | } |
80ff3856 YZ |
1699 | if (ret > 0) |
1700 | break; | |
1701 | leaf = path->nodes[0]; | |
1702 | } | |
be20aa9d | 1703 | |
80ff3856 YZ |
1704 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1705 | ||
a6bd9cd1 | 1706 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
1707 | if (found_key.objectid > ino) |
1708 | break; | |
a6bd9cd1 NB |
1709 | /* |
1710 | * Keep searching until we find an EXTENT_ITEM or there are no | |
1711 | * more extents for this inode | |
1712 | */ | |
1d512cb7 FM |
1713 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
1714 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1715 | path->slots[0]++; | |
1716 | goto next_slot; | |
1717 | } | |
a6bd9cd1 NB |
1718 | |
1719 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 1720 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
1721 | found_key.offset > end) |
1722 | break; | |
1723 | ||
a6bd9cd1 NB |
1724 | /* |
1725 | * If the found extent starts after requested offset, then | |
1726 | * adjust extent_end to be right before this extent begins | |
1727 | */ | |
80ff3856 YZ |
1728 | if (found_key.offset > cur_offset) { |
1729 | extent_end = found_key.offset; | |
e9061e21 | 1730 | extent_type = 0; |
80ff3856 YZ |
1731 | goto out_check; |
1732 | } | |
1733 | ||
a6bd9cd1 NB |
1734 | /* |
1735 | * Found extent which begins before our range and potentially | |
1736 | * intersect it | |
1737 | */ | |
80ff3856 YZ |
1738 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1739 | struct btrfs_file_extent_item); | |
1740 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1741 | ||
cc95bef6 | 1742 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1743 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1744 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1745 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1746 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1747 | extent_end = found_key.offset + |
1748 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1749 | disk_num_bytes = |
1750 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
a6bd9cd1 | 1751 | /* |
de7999af FM |
1752 | * If the extent we got ends before our current offset, |
1753 | * skip to the next extent. | |
a6bd9cd1 | 1754 | */ |
de7999af | 1755 | if (extent_end <= cur_offset) { |
80ff3856 YZ |
1756 | path->slots[0]++; |
1757 | goto next_slot; | |
1758 | } | |
a6bd9cd1 | 1759 | /* Skip holes */ |
17d217fe YZ |
1760 | if (disk_bytenr == 0) |
1761 | goto out_check; | |
a6bd9cd1 | 1762 | /* Skip compressed/encrypted/encoded extents */ |
80ff3856 YZ |
1763 | if (btrfs_file_extent_compression(leaf, fi) || |
1764 | btrfs_file_extent_encryption(leaf, fi) || | |
1765 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1766 | goto out_check; | |
78d4295b | 1767 | /* |
a6bd9cd1 NB |
1768 | * If extent is created before the last volume's snapshot |
1769 | * this implies the extent is shared, hence we can't do | |
1770 | * nocow. This is the same check as in | |
1771 | * btrfs_cross_ref_exist but without calling | |
1772 | * btrfs_search_slot. | |
78d4295b | 1773 | */ |
3e024846 | 1774 | if (!freespace_inode && |
27a7ff55 | 1775 | btrfs_file_extent_generation(leaf, fi) <= |
78d4295b EL |
1776 | btrfs_root_last_snapshot(&root->root_item)) |
1777 | goto out_check; | |
d899e052 YZ |
1778 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1779 | goto out_check; | |
c65ca98f FM |
1780 | |
1781 | /* | |
1782 | * The following checks can be expensive, as they need to | |
1783 | * take other locks and do btree or rbtree searches, so | |
1784 | * release the path to avoid blocking other tasks for too | |
1785 | * long. | |
1786 | */ | |
1787 | btrfs_release_path(path); | |
1788 | ||
58113753 LB |
1789 | ret = btrfs_cross_ref_exist(root, ino, |
1790 | found_key.offset - | |
a84d5d42 | 1791 | extent_offset, disk_bytenr, false); |
58113753 LB |
1792 | if (ret) { |
1793 | /* | |
1794 | * ret could be -EIO if the above fails to read | |
1795 | * metadata. | |
1796 | */ | |
1797 | if (ret < 0) { | |
1798 | if (cow_start != (u64)-1) | |
1799 | cur_offset = cow_start; | |
1800 | goto error; | |
1801 | } | |
1802 | ||
3e024846 | 1803 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1804 | goto out_check; |
58113753 | 1805 | } |
5d4f98a2 | 1806 | disk_bytenr += extent_offset; |
17d217fe YZ |
1807 | disk_bytenr += cur_offset - found_key.offset; |
1808 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 | 1809 | /* |
a6bd9cd1 NB |
1810 | * If there are pending snapshots for this root, we |
1811 | * fall into common COW way | |
e9894fd3 | 1812 | */ |
3e024846 | 1813 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
8ecebf4d | 1814 | goto out_check; |
17d217fe YZ |
1815 | /* |
1816 | * force cow if csum exists in the range. | |
1817 | * this ensure that csum for a given extent are | |
1818 | * either valid or do not exist. | |
1819 | */ | |
58113753 LB |
1820 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1821 | num_bytes); | |
1822 | if (ret) { | |
58113753 LB |
1823 | /* |
1824 | * ret could be -EIO if the above fails to read | |
1825 | * metadata. | |
1826 | */ | |
1827 | if (ret < 0) { | |
1828 | if (cow_start != (u64)-1) | |
1829 | cur_offset = cow_start; | |
1830 | goto error; | |
1831 | } | |
3e024846 | 1832 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1833 | goto out_check; |
91e1f56a | 1834 | } |
20903032 | 1835 | /* If the extent's block group is RO, we must COW */ |
8ecebf4d | 1836 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1837 | goto out_check; |
3e024846 | 1838 | nocow = true; |
80ff3856 | 1839 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e8e21007 NB |
1840 | extent_end = found_key.offset + ram_bytes; |
1841 | extent_end = ALIGN(extent_end, fs_info->sectorsize); | |
922f0518 NB |
1842 | /* Skip extents outside of our requested range */ |
1843 | if (extent_end <= start) { | |
1844 | path->slots[0]++; | |
1845 | goto next_slot; | |
1846 | } | |
80ff3856 | 1847 | } else { |
e8e21007 | 1848 | /* If this triggers then we have a memory corruption */ |
290342f6 | 1849 | BUG(); |
80ff3856 YZ |
1850 | } |
1851 | out_check: | |
a6bd9cd1 NB |
1852 | /* |
1853 | * If nocow is false then record the beginning of the range | |
1854 | * that needs to be COWed | |
1855 | */ | |
80ff3856 YZ |
1856 | if (!nocow) { |
1857 | if (cow_start == (u64)-1) | |
1858 | cow_start = cur_offset; | |
1859 | cur_offset = extent_end; | |
1860 | if (cur_offset > end) | |
1861 | break; | |
c65ca98f FM |
1862 | if (!path->nodes[0]) |
1863 | continue; | |
80ff3856 YZ |
1864 | path->slots[0]++; |
1865 | goto next_slot; | |
7ea394f1 YZ |
1866 | } |
1867 | ||
a6bd9cd1 NB |
1868 | /* |
1869 | * COW range from cow_start to found_key.offset - 1. As the key | |
1870 | * will contain the beginning of the first extent that can be | |
1871 | * NOCOW, following one which needs to be COW'ed | |
1872 | */ | |
80ff3856 | 1873 | if (cow_start != (u64)-1) { |
968322c8 | 1874 | ret = fallback_to_cow(inode, locked_page, |
8ba96f3d | 1875 | cow_start, found_key.offset - 1, |
467dc47e | 1876 | page_started, nr_written); |
230ed397 | 1877 | if (ret) |
79787eaa | 1878 | goto error; |
80ff3856 | 1879 | cow_start = (u64)-1; |
7ea394f1 | 1880 | } |
80ff3856 | 1881 | |
d899e052 | 1882 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db | 1883 | u64 orig_start = found_key.offset - extent_offset; |
3e024846 | 1884 | struct extent_map *em; |
6f9994db | 1885 | |
968322c8 | 1886 | em = create_io_em(inode, cur_offset, num_bytes, |
6f9994db LB |
1887 | orig_start, |
1888 | disk_bytenr, /* block_start */ | |
1889 | num_bytes, /* block_len */ | |
1890 | disk_num_bytes, /* orig_block_len */ | |
1891 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1892 | BTRFS_ORDERED_PREALLOC); | |
1893 | if (IS_ERR(em)) { | |
6f9994db LB |
1894 | ret = PTR_ERR(em); |
1895 | goto error; | |
d899e052 | 1896 | } |
6f9994db | 1897 | free_extent_map(em); |
968322c8 | 1898 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1899 | disk_bytenr, num_bytes, |
1900 | num_bytes, | |
1901 | BTRFS_ORDERED_PREALLOC); | |
762bf098 | 1902 | if (ret) { |
968322c8 | 1903 | btrfs_drop_extent_cache(inode, cur_offset, |
762bf098 NB |
1904 | cur_offset + num_bytes - 1, |
1905 | 0); | |
1906 | goto error; | |
1907 | } | |
d899e052 | 1908 | } else { |
968322c8 | 1909 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1910 | disk_bytenr, num_bytes, |
1911 | num_bytes, | |
1912 | BTRFS_ORDERED_NOCOW); | |
762bf098 NB |
1913 | if (ret) |
1914 | goto error; | |
d899e052 | 1915 | } |
80ff3856 | 1916 | |
f78c436c | 1917 | if (nocow) |
0b246afa | 1918 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
762bf098 | 1919 | nocow = false; |
771ed689 | 1920 | |
37f00a6d | 1921 | if (btrfs_is_data_reloc_root(root)) |
4dbd80fb QW |
1922 | /* |
1923 | * Error handled later, as we must prevent | |
1924 | * extent_clear_unlock_delalloc() in error handler | |
1925 | * from freeing metadata of created ordered extent. | |
1926 | */ | |
968322c8 | 1927 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
efa56464 | 1928 | num_bytes); |
efa56464 | 1929 | |
968322c8 | 1930 | extent_clear_unlock_delalloc(inode, cur_offset, |
74e9194a | 1931 | cur_offset + num_bytes - 1, |
c2790a2e | 1932 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1933 | EXTENT_DELALLOC | |
1934 | EXTENT_CLEAR_DATA_RESV, | |
f57ad937 | 1935 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
18513091 | 1936 | |
80ff3856 | 1937 | cur_offset = extent_end; |
4dbd80fb QW |
1938 | |
1939 | /* | |
1940 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1941 | * handler, as metadata for created ordered extent will only | |
1942 | * be freed by btrfs_finish_ordered_io(). | |
1943 | */ | |
1944 | if (ret) | |
1945 | goto error; | |
80ff3856 YZ |
1946 | if (cur_offset > end) |
1947 | break; | |
be20aa9d | 1948 | } |
b3b4aa74 | 1949 | btrfs_release_path(path); |
80ff3856 | 1950 | |
506481b2 | 1951 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1952 | cow_start = cur_offset; |
17ca04af | 1953 | |
80ff3856 | 1954 | if (cow_start != (u64)-1) { |
506481b2 | 1955 | cur_offset = end; |
968322c8 NB |
1956 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
1957 | page_started, nr_written); | |
d788a349 | 1958 | if (ret) |
79787eaa | 1959 | goto error; |
80ff3856 YZ |
1960 | } |
1961 | ||
79787eaa | 1962 | error: |
762bf098 NB |
1963 | if (nocow) |
1964 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1965 | ||
17ca04af | 1966 | if (ret && cur_offset < end) |
968322c8 | 1967 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 1968 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1969 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1970 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 1971 | PAGE_START_WRITEBACK | |
c2790a2e | 1972 | PAGE_END_WRITEBACK); |
7ea394f1 | 1973 | btrfs_free_path(path); |
79787eaa | 1974 | return ret; |
be20aa9d CM |
1975 | } |
1976 | ||
6e65ae76 | 1977 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 | 1978 | { |
6e65ae76 GR |
1979 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
1980 | if (inode->defrag_bytes && | |
1981 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, | |
1982 | 0, NULL)) | |
1983 | return false; | |
1984 | return true; | |
1985 | } | |
1986 | return false; | |
47059d93 WS |
1987 | } |
1988 | ||
d352ac68 | 1989 | /* |
5eaad97a NB |
1990 | * Function to process delayed allocation (create CoW) for ranges which are |
1991 | * being touched for the first time. | |
d352ac68 | 1992 | */ |
98456b9c | 1993 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
5eaad97a NB |
1994 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1995 | struct writeback_control *wbc) | |
be20aa9d | 1996 | { |
be20aa9d | 1997 | int ret; |
42c01100 | 1998 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
a2135011 | 1999 | |
2749f7ef QW |
2000 | /* |
2001 | * The range must cover part of the @locked_page, or the returned | |
2002 | * @page_started can confuse the caller. | |
2003 | */ | |
2004 | ASSERT(!(end <= page_offset(locked_page) || | |
2005 | start >= page_offset(locked_page) + PAGE_SIZE)); | |
2006 | ||
6e65ae76 | 2007 | if (should_nocow(inode, start, end)) { |
2adada88 JT |
2008 | /* |
2009 | * Normally on a zoned device we're only doing COW writes, but | |
2010 | * in case of relocation on a zoned filesystem we have taken | |
2011 | * precaution, that we're only writing sequentially. It's safe | |
2012 | * to use run_delalloc_nocow() here, like for regular | |
2013 | * preallocated inodes. | |
2014 | */ | |
2015 | ASSERT(!zoned || | |
2016 | (zoned && btrfs_is_data_reloc_root(inode->root))); | |
98456b9c | 2017 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
6e65ae76 | 2018 | page_started, nr_written); |
98456b9c NB |
2019 | } else if (!inode_can_compress(inode) || |
2020 | !inode_need_compress(inode, start, end)) { | |
42c01100 NA |
2021 | if (zoned) |
2022 | ret = run_delalloc_zoned(inode, locked_page, start, end, | |
2023 | page_started, nr_written); | |
2024 | else | |
2025 | ret = cow_file_range(inode, locked_page, start, end, | |
2026 | page_started, nr_written, 1); | |
7ddf5a42 | 2027 | } else { |
98456b9c NB |
2028 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
2029 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, | |
fac07d2b | 2030 | page_started, nr_written); |
7ddf5a42 | 2031 | } |
7361b4ae | 2032 | ASSERT(ret <= 0); |
52427260 | 2033 | if (ret) |
98456b9c | 2034 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 2035 | end - start + 1); |
b888db2b CM |
2036 | return ret; |
2037 | } | |
2038 | ||
abbb55f4 NB |
2039 | void btrfs_split_delalloc_extent(struct inode *inode, |
2040 | struct extent_state *orig, u64 split) | |
9ed74f2d | 2041 | { |
dcab6a3b JB |
2042 | u64 size; |
2043 | ||
0ca1f7ce | 2044 | /* not delalloc, ignore it */ |
9ed74f2d | 2045 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 2046 | return; |
9ed74f2d | 2047 | |
dcab6a3b JB |
2048 | size = orig->end - orig->start + 1; |
2049 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 2050 | u32 num_extents; |
dcab6a3b JB |
2051 | u64 new_size; |
2052 | ||
2053 | /* | |
5c848198 | 2054 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 2055 | * applies here, just in reverse. |
dcab6a3b JB |
2056 | */ |
2057 | new_size = orig->end - split + 1; | |
823bb20a | 2058 | num_extents = count_max_extents(new_size); |
ba117213 | 2059 | new_size = split - orig->start; |
823bb20a DS |
2060 | num_extents += count_max_extents(new_size); |
2061 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
2062 | return; |
2063 | } | |
2064 | ||
9e0baf60 | 2065 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 2066 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 2067 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
2068 | } |
2069 | ||
2070 | /* | |
5c848198 NB |
2071 | * Handle merged delayed allocation extents so we can keep track of new extents |
2072 | * that are just merged onto old extents, such as when we are doing sequential | |
2073 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 2074 | */ |
5c848198 NB |
2075 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
2076 | struct extent_state *other) | |
9ed74f2d | 2077 | { |
dcab6a3b | 2078 | u64 new_size, old_size; |
823bb20a | 2079 | u32 num_extents; |
dcab6a3b | 2080 | |
9ed74f2d JB |
2081 | /* not delalloc, ignore it */ |
2082 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 2083 | return; |
9ed74f2d | 2084 | |
8461a3de JB |
2085 | if (new->start > other->start) |
2086 | new_size = new->end - other->start + 1; | |
2087 | else | |
2088 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
2089 | |
2090 | /* we're not bigger than the max, unreserve the space and go */ | |
2091 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
2092 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 2093 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
2094 | spin_unlock(&BTRFS_I(inode)->lock); |
2095 | return; | |
2096 | } | |
2097 | ||
2098 | /* | |
ba117213 JB |
2099 | * We have to add up either side to figure out how many extents were |
2100 | * accounted for before we merged into one big extent. If the number of | |
2101 | * extents we accounted for is <= the amount we need for the new range | |
2102 | * then we can return, otherwise drop. Think of it like this | |
2103 | * | |
2104 | * [ 4k][MAX_SIZE] | |
2105 | * | |
2106 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
2107 | * need 2 outstanding extents, on one side we have 1 and the other side | |
2108 | * we have 1 so they are == and we can return. But in this case | |
2109 | * | |
2110 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
2111 | * | |
2112 | * Each range on their own accounts for 2 extents, but merged together | |
2113 | * they are only 3 extents worth of accounting, so we need to drop in | |
2114 | * this case. | |
dcab6a3b | 2115 | */ |
ba117213 | 2116 | old_size = other->end - other->start + 1; |
823bb20a | 2117 | num_extents = count_max_extents(old_size); |
ba117213 | 2118 | old_size = new->end - new->start + 1; |
823bb20a DS |
2119 | num_extents += count_max_extents(old_size); |
2120 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
2121 | return; |
2122 | ||
9e0baf60 | 2123 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 2124 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 2125 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
2126 | } |
2127 | ||
eb73c1b7 MX |
2128 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
2129 | struct inode *inode) | |
2130 | { | |
0b246afa JM |
2131 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2132 | ||
eb73c1b7 MX |
2133 | spin_lock(&root->delalloc_lock); |
2134 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
2135 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
2136 | &root->delalloc_inodes); | |
2137 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
2138 | &BTRFS_I(inode)->runtime_flags); | |
2139 | root->nr_delalloc_inodes++; | |
2140 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 2141 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2142 | BUG_ON(!list_empty(&root->delalloc_root)); |
2143 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
2144 | &fs_info->delalloc_roots); |
2145 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
2146 | } |
2147 | } | |
2148 | spin_unlock(&root->delalloc_lock); | |
2149 | } | |
2150 | ||
2b877331 NB |
2151 | |
2152 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2153 | struct btrfs_inode *inode) | |
eb73c1b7 | 2154 | { |
3ffbd68c | 2155 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 2156 | |
9e3e97f4 NB |
2157 | if (!list_empty(&inode->delalloc_inodes)) { |
2158 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 2159 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2160 | &inode->runtime_flags); |
eb73c1b7 MX |
2161 | root->nr_delalloc_inodes--; |
2162 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 2163 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 2164 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2165 | BUG_ON(list_empty(&root->delalloc_root)); |
2166 | list_del_init(&root->delalloc_root); | |
0b246afa | 2167 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2168 | } |
2169 | } | |
2b877331 NB |
2170 | } |
2171 | ||
2172 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2173 | struct btrfs_inode *inode) | |
2174 | { | |
2175 | spin_lock(&root->delalloc_lock); | |
2176 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
2177 | spin_unlock(&root->delalloc_lock); |
2178 | } | |
2179 | ||
d352ac68 | 2180 | /* |
e06a1fc9 NB |
2181 | * Properly track delayed allocation bytes in the inode and to maintain the |
2182 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 2183 | */ |
e06a1fc9 NB |
2184 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
2185 | unsigned *bits) | |
291d673e | 2186 | { |
0b246afa JM |
2187 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2188 | ||
47059d93 WS |
2189 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
2190 | WARN_ON(1); | |
75eff68e CM |
2191 | /* |
2192 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2193 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2194 | * bit, which is only set or cleared with irqs on |
2195 | */ | |
0ca1f7ce | 2196 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 2197 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2198 | u64 len = state->end + 1 - state->start; |
8b62f87b | 2199 | u32 num_extents = count_max_extents(len); |
70ddc553 | 2200 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 2201 | |
8b62f87b JB |
2202 | spin_lock(&BTRFS_I(inode)->lock); |
2203 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
2204 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 2205 | |
6a3891c5 | 2206 | /* For sanity tests */ |
0b246afa | 2207 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2208 | return; |
2209 | ||
104b4e51 NB |
2210 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2211 | fs_info->delalloc_batch); | |
df0af1a5 | 2212 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 2213 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
2214 | if (*bits & EXTENT_DEFRAG) |
2215 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 2216 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
2217 | &BTRFS_I(inode)->runtime_flags)) |
2218 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 2219 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 2220 | } |
a7e3b975 FM |
2221 | |
2222 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
2223 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2224 | spin_lock(&BTRFS_I(inode)->lock); | |
2225 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
2226 | state->start; | |
2227 | spin_unlock(&BTRFS_I(inode)->lock); | |
2228 | } | |
291d673e CM |
2229 | } |
2230 | ||
d352ac68 | 2231 | /* |
a36bb5f9 NB |
2232 | * Once a range is no longer delalloc this function ensures that proper |
2233 | * accounting happens. | |
d352ac68 | 2234 | */ |
a36bb5f9 NB |
2235 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
2236 | struct extent_state *state, unsigned *bits) | |
291d673e | 2237 | { |
a36bb5f9 NB |
2238 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
2239 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 2240 | u64 len = state->end + 1 - state->start; |
823bb20a | 2241 | u32 num_extents = count_max_extents(len); |
47059d93 | 2242 | |
4a4b964f FM |
2243 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
2244 | spin_lock(&inode->lock); | |
6fc0ef68 | 2245 | inode->defrag_bytes -= len; |
4a4b964f FM |
2246 | spin_unlock(&inode->lock); |
2247 | } | |
47059d93 | 2248 | |
75eff68e CM |
2249 | /* |
2250 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2251 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2252 | * bit, which is only set or cleared with irqs on |
2253 | */ | |
0ca1f7ce | 2254 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2255 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2256 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2257 | |
8b62f87b JB |
2258 | spin_lock(&inode->lock); |
2259 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2260 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2261 | |
b6d08f06 JB |
2262 | /* |
2263 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2264 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2265 | * error. |
2266 | */ | |
a315e68f | 2267 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2268 | root != fs_info->tree_root) |
43b18595 | 2269 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2270 | |
6a3891c5 | 2271 | /* For sanity tests. */ |
0b246afa | 2272 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2273 | return; |
2274 | ||
37f00a6d | 2275 | if (!btrfs_is_data_reloc_root(root) && |
a315e68f FM |
2276 | do_list && !(state->state & EXTENT_NORESERVE) && |
2277 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
9db5d510 | 2278 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2279 | |
104b4e51 NB |
2280 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2281 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2282 | spin_lock(&inode->lock); |
2283 | inode->delalloc_bytes -= len; | |
2284 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2285 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2286 | &inode->runtime_flags)) |
eb73c1b7 | 2287 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2288 | spin_unlock(&inode->lock); |
291d673e | 2289 | } |
a7e3b975 FM |
2290 | |
2291 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
2292 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2293 | spin_lock(&inode->lock); | |
2294 | ASSERT(inode->new_delalloc_bytes >= len); | |
2295 | inode->new_delalloc_bytes -= len; | |
2766ff61 FM |
2296 | if (*bits & EXTENT_ADD_INODE_BYTES) |
2297 | inode_add_bytes(&inode->vfs_inode, len); | |
a7e3b975 FM |
2298 | spin_unlock(&inode->lock); |
2299 | } | |
291d673e CM |
2300 | } |
2301 | ||
d352ac68 CM |
2302 | /* |
2303 | * in order to insert checksums into the metadata in large chunks, | |
2304 | * we wait until bio submission time. All the pages in the bio are | |
2305 | * checksummed and sums are attached onto the ordered extent record. | |
2306 | * | |
2307 | * At IO completion time the cums attached on the ordered extent record | |
2308 | * are inserted into the btree | |
2309 | */ | |
8896a08d | 2310 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
1941b64b | 2311 | u64 dio_file_offset) |
065631f6 | 2312 | { |
c965d640 | 2313 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
4a69a410 | 2314 | } |
e015640f | 2315 | |
abb99cfd NA |
2316 | /* |
2317 | * Split an extent_map at [start, start + len] | |
2318 | * | |
2319 | * This function is intended to be used only for extract_ordered_extent(). | |
2320 | */ | |
2321 | static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len, | |
2322 | u64 pre, u64 post) | |
2323 | { | |
2324 | struct extent_map_tree *em_tree = &inode->extent_tree; | |
2325 | struct extent_map *em; | |
2326 | struct extent_map *split_pre = NULL; | |
2327 | struct extent_map *split_mid = NULL; | |
2328 | struct extent_map *split_post = NULL; | |
2329 | int ret = 0; | |
abb99cfd NA |
2330 | unsigned long flags; |
2331 | ||
2332 | /* Sanity check */ | |
2333 | if (pre == 0 && post == 0) | |
2334 | return 0; | |
2335 | ||
2336 | split_pre = alloc_extent_map(); | |
2337 | if (pre) | |
2338 | split_mid = alloc_extent_map(); | |
2339 | if (post) | |
2340 | split_post = alloc_extent_map(); | |
2341 | if (!split_pre || (pre && !split_mid) || (post && !split_post)) { | |
2342 | ret = -ENOMEM; | |
2343 | goto out; | |
2344 | } | |
2345 | ||
2346 | ASSERT(pre + post < len); | |
2347 | ||
2348 | lock_extent(&inode->io_tree, start, start + len - 1); | |
2349 | write_lock(&em_tree->lock); | |
2350 | em = lookup_extent_mapping(em_tree, start, len); | |
2351 | if (!em) { | |
2352 | ret = -EIO; | |
2353 | goto out_unlock; | |
2354 | } | |
2355 | ||
2356 | ASSERT(em->len == len); | |
2357 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); | |
2358 | ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE); | |
63fb5879 NA |
2359 | ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
2360 | ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags)); | |
2361 | ASSERT(!list_empty(&em->list)); | |
abb99cfd NA |
2362 | |
2363 | flags = em->flags; | |
2364 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); | |
abb99cfd NA |
2365 | |
2366 | /* First, replace the em with a new extent_map starting from * em->start */ | |
2367 | split_pre->start = em->start; | |
2368 | split_pre->len = (pre ? pre : em->len - post); | |
2369 | split_pre->orig_start = split_pre->start; | |
2370 | split_pre->block_start = em->block_start; | |
2371 | split_pre->block_len = split_pre->len; | |
2372 | split_pre->orig_block_len = split_pre->block_len; | |
2373 | split_pre->ram_bytes = split_pre->len; | |
2374 | split_pre->flags = flags; | |
2375 | split_pre->compress_type = em->compress_type; | |
2376 | split_pre->generation = em->generation; | |
2377 | ||
63fb5879 | 2378 | replace_extent_mapping(em_tree, em, split_pre, 1); |
abb99cfd NA |
2379 | |
2380 | /* | |
2381 | * Now we only have an extent_map at: | |
2382 | * [em->start, em->start + pre] if pre != 0 | |
2383 | * [em->start, em->start + em->len - post] if pre == 0 | |
2384 | */ | |
2385 | ||
2386 | if (pre) { | |
2387 | /* Insert the middle extent_map */ | |
2388 | split_mid->start = em->start + pre; | |
2389 | split_mid->len = em->len - pre - post; | |
2390 | split_mid->orig_start = split_mid->start; | |
2391 | split_mid->block_start = em->block_start + pre; | |
2392 | split_mid->block_len = split_mid->len; | |
2393 | split_mid->orig_block_len = split_mid->block_len; | |
2394 | split_mid->ram_bytes = split_mid->len; | |
2395 | split_mid->flags = flags; | |
2396 | split_mid->compress_type = em->compress_type; | |
2397 | split_mid->generation = em->generation; | |
63fb5879 | 2398 | add_extent_mapping(em_tree, split_mid, 1); |
abb99cfd NA |
2399 | } |
2400 | ||
2401 | if (post) { | |
2402 | split_post->start = em->start + em->len - post; | |
2403 | split_post->len = post; | |
2404 | split_post->orig_start = split_post->start; | |
2405 | split_post->block_start = em->block_start + em->len - post; | |
2406 | split_post->block_len = split_post->len; | |
2407 | split_post->orig_block_len = split_post->block_len; | |
2408 | split_post->ram_bytes = split_post->len; | |
2409 | split_post->flags = flags; | |
2410 | split_post->compress_type = em->compress_type; | |
2411 | split_post->generation = em->generation; | |
63fb5879 | 2412 | add_extent_mapping(em_tree, split_post, 1); |
abb99cfd NA |
2413 | } |
2414 | ||
2415 | /* Once for us */ | |
2416 | free_extent_map(em); | |
2417 | /* Once for the tree */ | |
2418 | free_extent_map(em); | |
2419 | ||
2420 | out_unlock: | |
2421 | write_unlock(&em_tree->lock); | |
2422 | unlock_extent(&inode->io_tree, start, start + len - 1); | |
2423 | out: | |
2424 | free_extent_map(split_pre); | |
2425 | free_extent_map(split_mid); | |
2426 | free_extent_map(split_post); | |
2427 | ||
2428 | return ret; | |
2429 | } | |
2430 | ||
d22002fd NA |
2431 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
2432 | struct bio *bio, loff_t file_offset) | |
2433 | { | |
2434 | struct btrfs_ordered_extent *ordered; | |
d22002fd | 2435 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
abb99cfd | 2436 | u64 file_len; |
d22002fd NA |
2437 | u64 len = bio->bi_iter.bi_size; |
2438 | u64 end = start + len; | |
2439 | u64 ordered_end; | |
2440 | u64 pre, post; | |
2441 | int ret = 0; | |
2442 | ||
2443 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); | |
2444 | if (WARN_ON_ONCE(!ordered)) | |
2445 | return BLK_STS_IOERR; | |
2446 | ||
2447 | /* No need to split */ | |
2448 | if (ordered->disk_num_bytes == len) | |
2449 | goto out; | |
2450 | ||
2451 | /* We cannot split once end_bio'd ordered extent */ | |
2452 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { | |
2453 | ret = -EINVAL; | |
2454 | goto out; | |
2455 | } | |
2456 | ||
2457 | /* We cannot split a compressed ordered extent */ | |
2458 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { | |
2459 | ret = -EINVAL; | |
2460 | goto out; | |
2461 | } | |
2462 | ||
2463 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; | |
2464 | /* bio must be in one ordered extent */ | |
2465 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { | |
2466 | ret = -EINVAL; | |
2467 | goto out; | |
2468 | } | |
2469 | ||
2470 | /* Checksum list should be empty */ | |
2471 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { | |
2472 | ret = -EINVAL; | |
2473 | goto out; | |
2474 | } | |
2475 | ||
abb99cfd | 2476 | file_len = ordered->num_bytes; |
d22002fd NA |
2477 | pre = start - ordered->disk_bytenr; |
2478 | post = ordered_end - end; | |
2479 | ||
2480 | ret = btrfs_split_ordered_extent(ordered, pre, post); | |
2481 | if (ret) | |
2482 | goto out; | |
abb99cfd | 2483 | ret = split_zoned_em(inode, file_offset, file_len, pre, post); |
d22002fd NA |
2484 | |
2485 | out: | |
d22002fd NA |
2486 | btrfs_put_ordered_extent(ordered); |
2487 | ||
2488 | return errno_to_blk_status(ret); | |
2489 | } | |
2490 | ||
d352ac68 | 2491 | /* |
cad321ad | 2492 | * extent_io.c submission hook. This does the right thing for csum calculation |
4c274bc6 LB |
2493 | * on write, or reading the csums from the tree before a read. |
2494 | * | |
2495 | * Rules about async/sync submit, | |
2496 | * a) read: sync submit | |
2497 | * | |
2498 | * b) write without checksum: sync submit | |
2499 | * | |
2500 | * c) write with checksum: | |
2501 | * c-1) if bio is issued by fsync: sync submit | |
2502 | * (sync_writers != 0) | |
2503 | * | |
2504 | * c-2) if root is reloc root: sync submit | |
2505 | * (only in case of buffered IO) | |
2506 | * | |
2507 | * c-3) otherwise: async submit | |
d352ac68 | 2508 | */ |
908930f3 NB |
2509 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
2510 | int mirror_num, unsigned long bio_flags) | |
50489a57 | 2511 | |
44b8bd7e | 2512 | { |
0b246afa | 2513 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 2514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 2515 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 2516 | blk_status_t ret = 0; |
19b9bdb0 | 2517 | int skip_sum; |
b812ce28 | 2518 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 2519 | |
42437a63 | 2520 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
056c8311 | 2521 | test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); |
cad321ad | 2522 | |
70ddc553 | 2523 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 2524 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 2525 | |
d22002fd NA |
2526 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
2527 | struct page *page = bio_first_bvec_all(bio)->bv_page; | |
2528 | loff_t file_offset = page_offset(page); | |
2529 | ||
2530 | ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset); | |
2531 | if (ret) | |
2532 | goto out; | |
2533 | } | |
2534 | ||
cfe94440 | 2535 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
0b246afa | 2536 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 2537 | if (ret) |
61891923 | 2538 | goto out; |
5fd02043 | 2539 | |
d20f7043 | 2540 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
2541 | ret = btrfs_submit_compressed_read(inode, bio, |
2542 | mirror_num, | |
2543 | bio_flags); | |
2544 | goto out; | |
334c16d8 JB |
2545 | } else { |
2546 | /* | |
2547 | * Lookup bio sums does extra checks around whether we | |
2548 | * need to csum or not, which is why we ignore skip_sum | |
2549 | * here. | |
2550 | */ | |
6275193e | 2551 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 2552 | if (ret) |
61891923 | 2553 | goto out; |
c2db1073 | 2554 | } |
4d1b5fb4 | 2555 | goto mapit; |
b812ce28 | 2556 | } else if (async && !skip_sum) { |
17d217fe | 2557 | /* csum items have already been cloned */ |
37f00a6d | 2558 | if (btrfs_is_data_reloc_root(root)) |
17d217fe | 2559 | goto mapit; |
19b9bdb0 | 2560 | /* we're doing a write, do the async checksumming */ |
8896a08d QW |
2561 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
2562 | 0, btrfs_submit_bio_start); | |
61891923 | 2563 | goto out; |
b812ce28 | 2564 | } else if (!skip_sum) { |
bd242a08 | 2565 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
b812ce28 JB |
2566 | if (ret) |
2567 | goto out; | |
19b9bdb0 CM |
2568 | } |
2569 | ||
0b86a832 | 2570 | mapit: |
08635bae | 2571 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
61891923 SB |
2572 | |
2573 | out: | |
4e4cbee9 CH |
2574 | if (ret) { |
2575 | bio->bi_status = ret; | |
4246a0b6 CH |
2576 | bio_endio(bio); |
2577 | } | |
61891923 | 2578 | return ret; |
065631f6 | 2579 | } |
6885f308 | 2580 | |
d352ac68 CM |
2581 | /* |
2582 | * given a list of ordered sums record them in the inode. This happens | |
2583 | * at IO completion time based on sums calculated at bio submission time. | |
2584 | */ | |
510f85ed NB |
2585 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
2586 | struct list_head *list) | |
e6dcd2dc | 2587 | { |
e6dcd2dc | 2588 | struct btrfs_ordered_sum *sum; |
fc28b25e | 2589 | struct btrfs_root *csum_root = NULL; |
ac01f26a | 2590 | int ret; |
e6dcd2dc | 2591 | |
c6e30871 | 2592 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2593 | trans->adding_csums = true; |
fc28b25e JB |
2594 | if (!csum_root) |
2595 | csum_root = btrfs_csum_root(trans->fs_info, | |
2596 | sum->bytenr); | |
2597 | ret = btrfs_csum_file_blocks(trans, csum_root, sum); | |
7c2871a2 | 2598 | trans->adding_csums = false; |
ac01f26a NB |
2599 | if (ret) |
2600 | return ret; | |
e6dcd2dc CM |
2601 | } |
2602 | return 0; | |
2603 | } | |
2604 | ||
c3347309 FM |
2605 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
2606 | const u64 start, | |
2607 | const u64 len, | |
2608 | struct extent_state **cached_state) | |
2609 | { | |
2610 | u64 search_start = start; | |
2611 | const u64 end = start + len - 1; | |
2612 | ||
2613 | while (search_start < end) { | |
2614 | const u64 search_len = end - search_start + 1; | |
2615 | struct extent_map *em; | |
2616 | u64 em_len; | |
2617 | int ret = 0; | |
2618 | ||
2619 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); | |
2620 | if (IS_ERR(em)) | |
2621 | return PTR_ERR(em); | |
2622 | ||
2623 | if (em->block_start != EXTENT_MAP_HOLE) | |
2624 | goto next; | |
2625 | ||
2626 | em_len = em->len; | |
2627 | if (em->start < search_start) | |
2628 | em_len -= search_start - em->start; | |
2629 | if (em_len > search_len) | |
2630 | em_len = search_len; | |
2631 | ||
2632 | ret = set_extent_bit(&inode->io_tree, search_start, | |
2633 | search_start + em_len - 1, | |
1cab5e72 NB |
2634 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
2635 | GFP_NOFS, NULL); | |
c3347309 FM |
2636 | next: |
2637 | search_start = extent_map_end(em); | |
2638 | free_extent_map(em); | |
2639 | if (ret) | |
2640 | return ret; | |
2641 | } | |
2642 | return 0; | |
2643 | } | |
2644 | ||
c2566f22 | 2645 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2646 | unsigned int extra_bits, |
330a5827 | 2647 | struct extent_state **cached_state) |
ea8c2819 | 2648 | { |
fdb1e121 | 2649 | WARN_ON(PAGE_ALIGNED(end)); |
c3347309 FM |
2650 | |
2651 | if (start >= i_size_read(&inode->vfs_inode) && | |
2652 | !(inode->flags & BTRFS_INODE_PREALLOC)) { | |
2653 | /* | |
2654 | * There can't be any extents following eof in this case so just | |
2655 | * set the delalloc new bit for the range directly. | |
2656 | */ | |
2657 | extra_bits |= EXTENT_DELALLOC_NEW; | |
2658 | } else { | |
2659 | int ret; | |
2660 | ||
2661 | ret = btrfs_find_new_delalloc_bytes(inode, start, | |
2662 | end + 1 - start, | |
2663 | cached_state); | |
2664 | if (ret) | |
2665 | return ret; | |
2666 | } | |
2667 | ||
c2566f22 NB |
2668 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
2669 | cached_state); | |
ea8c2819 CM |
2670 | } |
2671 | ||
d352ac68 | 2672 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2673 | struct btrfs_writepage_fixup { |
2674 | struct page *page; | |
f4b1363c | 2675 | struct inode *inode; |
247e743c CM |
2676 | struct btrfs_work work; |
2677 | }; | |
2678 | ||
b2950863 | 2679 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2680 | { |
2681 | struct btrfs_writepage_fixup *fixup; | |
2682 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2683 | struct extent_state *cached_state = NULL; |
364ecf36 | 2684 | struct extent_changeset *data_reserved = NULL; |
247e743c | 2685 | struct page *page; |
65d87f79 | 2686 | struct btrfs_inode *inode; |
247e743c CM |
2687 | u64 page_start; |
2688 | u64 page_end; | |
25f3c502 | 2689 | int ret = 0; |
f4b1363c | 2690 | bool free_delalloc_space = true; |
247e743c CM |
2691 | |
2692 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2693 | page = fixup->page; | |
65d87f79 | 2694 | inode = BTRFS_I(fixup->inode); |
f4b1363c JB |
2695 | page_start = page_offset(page); |
2696 | page_end = page_offset(page) + PAGE_SIZE - 1; | |
2697 | ||
2698 | /* | |
2699 | * This is similar to page_mkwrite, we need to reserve the space before | |
2700 | * we take the page lock. | |
2701 | */ | |
65d87f79 NB |
2702 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2703 | PAGE_SIZE); | |
4a096752 | 2704 | again: |
247e743c | 2705 | lock_page(page); |
25f3c502 CM |
2706 | |
2707 | /* | |
2708 | * Before we queued this fixup, we took a reference on the page. | |
2709 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2710 | * address space. | |
2711 | */ | |
f4b1363c JB |
2712 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2713 | /* | |
2714 | * Unfortunately this is a little tricky, either | |
2715 | * | |
2716 | * 1) We got here and our page had already been dealt with and | |
2717 | * we reserved our space, thus ret == 0, so we need to just | |
2718 | * drop our space reservation and bail. This can happen the | |
2719 | * first time we come into the fixup worker, or could happen | |
2720 | * while waiting for the ordered extent. | |
2721 | * 2) Our page was already dealt with, but we happened to get an | |
2722 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2723 | * this case we obviously don't have anything to release, but | |
2724 | * because the page was already dealt with we don't want to | |
2725 | * mark the page with an error, so make sure we're resetting | |
2726 | * ret to 0. This is why we have this check _before_ the ret | |
2727 | * check, because we do not want to have a surprise ENOSPC | |
2728 | * when the page was already properly dealt with. | |
2729 | */ | |
2730 | if (!ret) { | |
65d87f79 NB |
2731 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2732 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2733 | page_start, PAGE_SIZE, |
2734 | true); | |
2735 | } | |
2736 | ret = 0; | |
247e743c | 2737 | goto out_page; |
f4b1363c | 2738 | } |
247e743c | 2739 | |
25f3c502 | 2740 | /* |
f4b1363c JB |
2741 | * We can't mess with the page state unless it is locked, so now that |
2742 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2743 | */ |
f4b1363c JB |
2744 | if (ret) |
2745 | goto out_page; | |
247e743c | 2746 | |
65d87f79 | 2747 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2748 | |
2749 | /* already ordered? We're done */ | |
f57ad937 | 2750 | if (PageOrdered(page)) |
f4b1363c | 2751 | goto out_reserved; |
4a096752 | 2752 | |
65d87f79 | 2753 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2754 | if (ordered) { |
65d87f79 NB |
2755 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
2756 | &cached_state); | |
4a096752 | 2757 | unlock_page(page); |
c0a43603 | 2758 | btrfs_start_ordered_extent(ordered, 1); |
87826df0 | 2759 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2760 | goto again; |
2761 | } | |
247e743c | 2762 | |
65d87f79 | 2763 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2764 | &cached_state); |
25f3c502 | 2765 | if (ret) |
53687007 | 2766 | goto out_reserved; |
f3038ee3 | 2767 | |
25f3c502 CM |
2768 | /* |
2769 | * Everything went as planned, we're now the owner of a dirty page with | |
2770 | * delayed allocation bits set and space reserved for our COW | |
2771 | * destination. | |
2772 | * | |
2773 | * The page was dirty when we started, nothing should have cleaned it. | |
2774 | */ | |
2775 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2776 | free_delalloc_space = false; |
53687007 | 2777 | out_reserved: |
65d87f79 | 2778 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2779 | if (free_delalloc_space) |
65d87f79 NB |
2780 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2781 | PAGE_SIZE, true); | |
2782 | unlock_extent_cached(&inode->io_tree, page_start, page_end, | |
e43bbe5e | 2783 | &cached_state); |
247e743c | 2784 | out_page: |
25f3c502 CM |
2785 | if (ret) { |
2786 | /* | |
2787 | * We hit ENOSPC or other errors. Update the mapping and page | |
2788 | * to reflect the errors and clean the page. | |
2789 | */ | |
2790 | mapping_set_error(page->mapping, ret); | |
2791 | end_extent_writepage(page, ret, page_start, page_end); | |
2792 | clear_page_dirty_for_io(page); | |
2793 | SetPageError(page); | |
2794 | } | |
e4f94347 | 2795 | btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE); |
247e743c | 2796 | unlock_page(page); |
09cbfeaf | 2797 | put_page(page); |
b897abec | 2798 | kfree(fixup); |
364ecf36 | 2799 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2800 | /* |
2801 | * As a precaution, do a delayed iput in case it would be the last iput | |
2802 | * that could need flushing space. Recursing back to fixup worker would | |
2803 | * deadlock. | |
2804 | */ | |
65d87f79 | 2805 | btrfs_add_delayed_iput(&inode->vfs_inode); |
247e743c CM |
2806 | } |
2807 | ||
2808 | /* | |
2809 | * There are a few paths in the higher layers of the kernel that directly | |
2810 | * set the page dirty bit without asking the filesystem if it is a | |
2811 | * good idea. This causes problems because we want to make sure COW | |
2812 | * properly happens and the data=ordered rules are followed. | |
2813 | * | |
c8b97818 | 2814 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2815 | * hasn't been properly setup for IO. We kick off an async process |
2816 | * to fix it up. The async helper will wait for ordered extents, set | |
2817 | * the delalloc bit and make it safe to write the page. | |
2818 | */ | |
a129ffb8 | 2819 | int btrfs_writepage_cow_fixup(struct page *page) |
247e743c CM |
2820 | { |
2821 | struct inode *inode = page->mapping->host; | |
0b246afa | 2822 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2823 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2824 | |
f57ad937 QW |
2825 | /* This page has ordered extent covering it already */ |
2826 | if (PageOrdered(page)) | |
247e743c CM |
2827 | return 0; |
2828 | ||
25f3c502 CM |
2829 | /* |
2830 | * PageChecked is set below when we create a fixup worker for this page, | |
2831 | * don't try to create another one if we're already PageChecked() | |
2832 | * | |
2833 | * The extent_io writepage code will redirty the page if we send back | |
2834 | * EAGAIN. | |
2835 | */ | |
247e743c CM |
2836 | if (PageChecked(page)) |
2837 | return -EAGAIN; | |
2838 | ||
2839 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2840 | if (!fixup) | |
2841 | return -EAGAIN; | |
f421950f | 2842 | |
f4b1363c JB |
2843 | /* |
2844 | * We are already holding a reference to this inode from | |
2845 | * write_cache_pages. We need to hold it because the space reservation | |
2846 | * takes place outside of the page lock, and we can't trust | |
2847 | * page->mapping outside of the page lock. | |
2848 | */ | |
2849 | ihold(inode); | |
e4f94347 | 2850 | btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
09cbfeaf | 2851 | get_page(page); |
a0cac0ec | 2852 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 2853 | fixup->page = page; |
f4b1363c | 2854 | fixup->inode = inode; |
0b246afa | 2855 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2856 | |
2857 | return -EAGAIN; | |
247e743c CM |
2858 | } |
2859 | ||
d899e052 | 2860 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 2861 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a | 2862 | struct btrfs_file_extent_item *stack_fi, |
2766ff61 | 2863 | const bool update_inode_bytes, |
9729f10a | 2864 | u64 qgroup_reserved) |
d899e052 | 2865 | { |
c553f94d | 2866 | struct btrfs_root *root = inode->root; |
2766ff61 | 2867 | const u64 sectorsize = root->fs_info->sectorsize; |
d899e052 YZ |
2868 | struct btrfs_path *path; |
2869 | struct extent_buffer *leaf; | |
2870 | struct btrfs_key ins; | |
203f44c5 QW |
2871 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2872 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
2873 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); | |
2874 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
5893dfb9 | 2875 | struct btrfs_drop_extents_args drop_args = { 0 }; |
d899e052 YZ |
2876 | int ret; |
2877 | ||
2878 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2879 | if (!path) |
2880 | return -ENOMEM; | |
d899e052 | 2881 | |
a1ed835e CM |
2882 | /* |
2883 | * we may be replacing one extent in the tree with another. | |
2884 | * The new extent is pinned in the extent map, and we don't want | |
2885 | * to drop it from the cache until it is completely in the btree. | |
2886 | * | |
2887 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2888 | * the caller is expected to unpin it and allow it to be merged | |
2889 | * with the others. | |
2890 | */ | |
5893dfb9 FM |
2891 | drop_args.path = path; |
2892 | drop_args.start = file_pos; | |
2893 | drop_args.end = file_pos + num_bytes; | |
2894 | drop_args.replace_extent = true; | |
2895 | drop_args.extent_item_size = sizeof(*stack_fi); | |
2896 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); | |
79787eaa JM |
2897 | if (ret) |
2898 | goto out; | |
d899e052 | 2899 | |
5893dfb9 | 2900 | if (!drop_args.extent_inserted) { |
c553f94d | 2901 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
2902 | ins.offset = file_pos; |
2903 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2904 | ||
1acae57b | 2905 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
203f44c5 | 2906 | sizeof(*stack_fi)); |
1acae57b FDBM |
2907 | if (ret) |
2908 | goto out; | |
2909 | } | |
d899e052 | 2910 | leaf = path->nodes[0]; |
203f44c5 QW |
2911 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2912 | write_extent_buffer(leaf, stack_fi, | |
2913 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2914 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2915 | |
d899e052 | 2916 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2917 | btrfs_release_path(path); |
d899e052 | 2918 | |
2766ff61 FM |
2919 | /* |
2920 | * If we dropped an inline extent here, we know the range where it is | |
2921 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the | |
1a9fd417 | 2922 | * number of bytes only for that range containing the inline extent. |
2766ff61 FM |
2923 | * The remaining of the range will be processed when clearning the |
2924 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. | |
2925 | */ | |
2926 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { | |
2927 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); | |
2928 | ||
2929 | inline_size = drop_args.bytes_found - inline_size; | |
2930 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); | |
2931 | drop_args.bytes_found -= inline_size; | |
2932 | num_bytes -= sectorsize; | |
2933 | } | |
2934 | ||
2935 | if (update_inode_bytes) | |
2936 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); | |
d899e052 YZ |
2937 | |
2938 | ins.objectid = disk_bytenr; | |
2939 | ins.offset = disk_num_bytes; | |
2940 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2941 | |
c553f94d | 2942 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
2943 | if (ret) |
2944 | goto out; | |
2945 | ||
c553f94d | 2946 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
9729f10a | 2947 | file_pos, qgroup_reserved, &ins); |
79787eaa | 2948 | out: |
d899e052 | 2949 | btrfs_free_path(path); |
b9473439 | 2950 | |
79787eaa | 2951 | return ret; |
d899e052 YZ |
2952 | } |
2953 | ||
2ff7e61e | 2954 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2955 | u64 start, u64 len) |
2956 | { | |
32da5386 | 2957 | struct btrfs_block_group *cache; |
e570fd27 | 2958 | |
0b246afa | 2959 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2960 | ASSERT(cache); |
2961 | ||
2962 | spin_lock(&cache->lock); | |
2963 | cache->delalloc_bytes -= len; | |
2964 | spin_unlock(&cache->lock); | |
2965 | ||
2966 | btrfs_put_block_group(cache); | |
2967 | } | |
2968 | ||
203f44c5 | 2969 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
203f44c5 QW |
2970 | struct btrfs_ordered_extent *oe) |
2971 | { | |
2972 | struct btrfs_file_extent_item stack_fi; | |
2973 | u64 logical_len; | |
2766ff61 | 2974 | bool update_inode_bytes; |
203f44c5 QW |
2975 | |
2976 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2977 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2978 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2979 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2980 | oe->disk_num_bytes); | |
2981 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) | |
2982 | logical_len = oe->truncated_len; | |
2983 | else | |
2984 | logical_len = oe->num_bytes; | |
2985 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); | |
2986 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); | |
2987 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); | |
2988 | /* Encryption and other encoding is reserved and all 0 */ | |
2989 | ||
2766ff61 FM |
2990 | /* |
2991 | * For delalloc, when completing an ordered extent we update the inode's | |
2992 | * bytes when clearing the range in the inode's io tree, so pass false | |
2993 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), | |
2994 | * except if the ordered extent was truncated. | |
2995 | */ | |
2996 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || | |
2997 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); | |
2998 | ||
3c38c877 NB |
2999 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
3000 | oe->file_offset, &stack_fi, | |
2766ff61 | 3001 | update_inode_bytes, oe->qgroup_rsv); |
203f44c5 QW |
3002 | } |
3003 | ||
3004 | /* | |
3005 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
3006 | * an ordered extent if the range of bytes in the file it covers are |
3007 | * fully written. | |
3008 | */ | |
5fd02043 | 3009 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 3010 | { |
72e7e6ed NB |
3011 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
3012 | struct btrfs_root *root = inode->root; | |
3013 | struct btrfs_fs_info *fs_info = root->fs_info; | |
0ca1f7ce | 3014 | struct btrfs_trans_handle *trans = NULL; |
72e7e6ed | 3015 | struct extent_io_tree *io_tree = &inode->io_tree; |
2ac55d41 | 3016 | struct extent_state *cached_state = NULL; |
bffe633e | 3017 | u64 start, end; |
261507a0 | 3018 | int compress_type = 0; |
77cef2ec | 3019 | int ret = 0; |
bffe633e | 3020 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 3021 | bool freespace_inode; |
77cef2ec | 3022 | bool truncated = false; |
49940bdd | 3023 | bool clear_reserved_extent = true; |
2766ff61 | 3024 | unsigned int clear_bits = EXTENT_DEFRAG; |
a7e3b975 | 3025 | |
bffe633e OS |
3026 | start = ordered_extent->file_offset; |
3027 | end = start + ordered_extent->num_bytes - 1; | |
3028 | ||
a7e3b975 FM |
3029 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
3030 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
3031 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2766ff61 | 3032 | clear_bits |= EXTENT_DELALLOC_NEW; |
e6dcd2dc | 3033 | |
72e7e6ed | 3034 | freespace_inode = btrfs_is_free_space_inode(inode); |
0cb59c99 | 3035 | |
5fd02043 JB |
3036 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
3037 | ret = -EIO; | |
3038 | goto out; | |
3039 | } | |
3040 | ||
be1a1d7a NA |
3041 | /* A valid bdev implies a write on a sequential zone */ |
3042 | if (ordered_extent->bdev) { | |
d8e3fb10 | 3043 | btrfs_rewrite_logical_zoned(ordered_extent); |
be1a1d7a NA |
3044 | btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, |
3045 | ordered_extent->disk_num_bytes); | |
3046 | } | |
d8e3fb10 | 3047 | |
72e7e6ed | 3048 | btrfs_free_io_failure_record(inode, start, end); |
f612496b | 3049 | |
77cef2ec JB |
3050 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
3051 | truncated = true; | |
3052 | logical_len = ordered_extent->truncated_len; | |
3053 | /* Truncated the entire extent, don't bother adding */ | |
3054 | if (!logical_len) | |
3055 | goto out; | |
3056 | } | |
3057 | ||
c2167754 | 3058 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 3059 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 3060 | |
72e7e6ed | 3061 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
3062 | if (freespace_inode) |
3063 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
3064 | else |
3065 | trans = btrfs_join_transaction(root); | |
3066 | if (IS_ERR(trans)) { | |
3067 | ret = PTR_ERR(trans); | |
3068 | trans = NULL; | |
3069 | goto out; | |
c2167754 | 3070 | } |
72e7e6ed | 3071 | trans->block_rsv = &inode->block_rsv; |
729f7961 | 3072 | ret = btrfs_update_inode_fallback(trans, root, inode); |
6c760c07 | 3073 | if (ret) /* -ENOMEM or corruption */ |
66642832 | 3074 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
3075 | goto out; |
3076 | } | |
e6dcd2dc | 3077 | |
2766ff61 | 3078 | clear_bits |= EXTENT_LOCKED; |
bffe633e | 3079 | lock_extent_bits(io_tree, start, end, &cached_state); |
e6dcd2dc | 3080 | |
8d510121 NB |
3081 | if (freespace_inode) |
3082 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 3083 | else |
7a7eaa40 | 3084 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3085 | if (IS_ERR(trans)) { |
3086 | ret = PTR_ERR(trans); | |
3087 | trans = NULL; | |
a7e3b975 | 3088 | goto out; |
79787eaa | 3089 | } |
a79b7d4b | 3090 | |
72e7e6ed | 3091 | trans->block_rsv = &inode->block_rsv; |
c2167754 | 3092 | |
c8b97818 | 3093 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3094 | compress_type = ordered_extent->compress_type; |
d899e052 | 3095 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3096 | BUG_ON(compress_type); |
72e7e6ed | 3097 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
3098 | ordered_extent->file_offset, |
3099 | ordered_extent->file_offset + | |
77cef2ec | 3100 | logical_len); |
d899e052 | 3101 | } else { |
0b246afa | 3102 | BUG_ON(root == fs_info->tree_root); |
3c38c877 | 3103 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
49940bdd JB |
3104 | if (!ret) { |
3105 | clear_reserved_extent = false; | |
2ff7e61e | 3106 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
3107 | ordered_extent->disk_bytenr, |
3108 | ordered_extent->disk_num_bytes); | |
49940bdd | 3109 | } |
d899e052 | 3110 | } |
72e7e6ed | 3111 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
bffe633e | 3112 | ordered_extent->num_bytes, trans->transid); |
79787eaa | 3113 | if (ret < 0) { |
66642832 | 3114 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3115 | goto out; |
79787eaa | 3116 | } |
2ac55d41 | 3117 | |
510f85ed | 3118 | ret = add_pending_csums(trans, &ordered_extent->list); |
ac01f26a NB |
3119 | if (ret) { |
3120 | btrfs_abort_transaction(trans, ret); | |
3121 | goto out; | |
3122 | } | |
e6dcd2dc | 3123 | |
2766ff61 FM |
3124 | /* |
3125 | * If this is a new delalloc range, clear its new delalloc flag to | |
3126 | * update the inode's number of bytes. This needs to be done first | |
3127 | * before updating the inode item. | |
3128 | */ | |
3129 | if ((clear_bits & EXTENT_DELALLOC_NEW) && | |
3130 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) | |
72e7e6ed | 3131 | clear_extent_bit(&inode->io_tree, start, end, |
2766ff61 FM |
3132 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
3133 | 0, 0, &cached_state); | |
3134 | ||
72e7e6ed | 3135 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
729f7961 | 3136 | ret = btrfs_update_inode_fallback(trans, root, inode); |
6c760c07 | 3137 | if (ret) { /* -ENOMEM or corruption */ |
66642832 | 3138 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3139 | goto out; |
1ef30be1 JB |
3140 | } |
3141 | ret = 0; | |
c2167754 | 3142 | out: |
72e7e6ed | 3143 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
bffe633e | 3144 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
313facc5 | 3145 | &cached_state); |
a7e3b975 | 3146 | |
a698d075 | 3147 | if (trans) |
3a45bb20 | 3148 | btrfs_end_transaction(trans); |
0cb59c99 | 3149 | |
77cef2ec | 3150 | if (ret || truncated) { |
bffe633e | 3151 | u64 unwritten_start = start; |
77cef2ec | 3152 | |
d61bec08 JB |
3153 | /* |
3154 | * If we failed to finish this ordered extent for any reason we | |
3155 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered | |
3156 | * extent, and mark the inode with the error if it wasn't | |
3157 | * already set. Any error during writeback would have already | |
3158 | * set the mapping error, so we need to set it if we're the ones | |
3159 | * marking this ordered extent as failed. | |
3160 | */ | |
3161 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, | |
3162 | &ordered_extent->flags)) | |
3163 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); | |
3164 | ||
77cef2ec | 3165 | if (truncated) |
bffe633e OS |
3166 | unwritten_start += logical_len; |
3167 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec JB |
3168 | |
3169 | /* Drop the cache for the part of the extent we didn't write. */ | |
72e7e6ed | 3170 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
5fd02043 | 3171 | |
0bec9ef5 JB |
3172 | /* |
3173 | * If the ordered extent had an IOERR or something else went | |
3174 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3175 | * back to the allocator. We only free the extent in the |
3176 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3177 | * |
3178 | * If we made it past insert_reserved_file_extent before we | |
3179 | * errored out then we don't need to do this as the accounting | |
3180 | * has already been done. | |
0bec9ef5 | 3181 | */ |
77cef2ec | 3182 | if ((ret || !logical_len) && |
49940bdd | 3183 | clear_reserved_extent && |
77cef2ec | 3184 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
3185 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
3186 | /* | |
3187 | * Discard the range before returning it back to the | |
3188 | * free space pool | |
3189 | */ | |
46b27f50 | 3190 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 3191 | btrfs_discard_extent(fs_info, |
bffe633e OS |
3192 | ordered_extent->disk_bytenr, |
3193 | ordered_extent->disk_num_bytes, | |
3194 | NULL); | |
2ff7e61e | 3195 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
3196 | ordered_extent->disk_bytenr, |
3197 | ordered_extent->disk_num_bytes, 1); | |
4eaaec24 | 3198 | } |
0bec9ef5 JB |
3199 | } |
3200 | ||
5fd02043 | 3201 | /* |
8bad3c02 LB |
3202 | * This needs to be done to make sure anybody waiting knows we are done |
3203 | * updating everything for this ordered extent. | |
5fd02043 | 3204 | */ |
72e7e6ed | 3205 | btrfs_remove_ordered_extent(inode, ordered_extent); |
5fd02043 | 3206 | |
e6dcd2dc CM |
3207 | /* once for us */ |
3208 | btrfs_put_ordered_extent(ordered_extent); | |
3209 | /* once for the tree */ | |
3210 | btrfs_put_ordered_extent(ordered_extent); | |
3211 | ||
5fd02043 JB |
3212 | return ret; |
3213 | } | |
3214 | ||
3215 | static void finish_ordered_fn(struct btrfs_work *work) | |
3216 | { | |
3217 | struct btrfs_ordered_extent *ordered_extent; | |
3218 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3219 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3220 | } |
3221 | ||
38a39ac7 QW |
3222 | void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, |
3223 | struct page *page, u64 start, | |
25c1252a | 3224 | u64 end, bool uptodate) |
211f90e6 | 3225 | { |
38a39ac7 | 3226 | trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate); |
1abe9b8a | 3227 | |
e65f152e QW |
3228 | btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start, |
3229 | finish_ordered_fn, uptodate); | |
211f90e6 CM |
3230 | } |
3231 | ||
265d4ac0 QW |
3232 | /* |
3233 | * check_data_csum - verify checksum of one sector of uncompressed data | |
7ffd27e3 | 3234 | * @inode: inode |
265d4ac0 | 3235 | * @io_bio: btrfs_io_bio which contains the csum |
7ffd27e3 | 3236 | * @bio_offset: offset to the beginning of the bio (in bytes) |
265d4ac0 QW |
3237 | * @page: page where is the data to be verified |
3238 | * @pgoff: offset inside the page | |
c1d6abda | 3239 | * @start: logical offset in the file |
265d4ac0 QW |
3240 | * |
3241 | * The length of such check is always one sector size. | |
3242 | */ | |
c3a3b19b | 3243 | static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio, |
c1d6abda OS |
3244 | u32 bio_offset, struct page *page, u32 pgoff, |
3245 | u64 start) | |
dc380aea | 3246 | { |
d5178578 JT |
3247 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
3248 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
dc380aea | 3249 | char *kaddr; |
265d4ac0 | 3250 | u32 len = fs_info->sectorsize; |
223486c2 | 3251 | const u32 csum_size = fs_info->csum_size; |
7ffd27e3 | 3252 | unsigned int offset_sectors; |
d5178578 JT |
3253 | u8 *csum_expected; |
3254 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 3255 | |
265d4ac0 QW |
3256 | ASSERT(pgoff + len <= PAGE_SIZE); |
3257 | ||
7ffd27e3 | 3258 | offset_sectors = bio_offset >> fs_info->sectorsize_bits; |
c3a3b19b | 3259 | csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size; |
dc380aea MX |
3260 | |
3261 | kaddr = kmap_atomic(page); | |
d5178578 JT |
3262 | shash->tfm = fs_info->csum_shash; |
3263 | ||
fd08001f | 3264 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
d5178578 JT |
3265 | |
3266 | if (memcmp(csum, csum_expected, csum_size)) | |
dc380aea MX |
3267 | goto zeroit; |
3268 | ||
3269 | kunmap_atomic(kaddr); | |
3270 | return 0; | |
3271 | zeroit: | |
c1d6abda | 3272 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
c3a3b19b QW |
3273 | bbio->mirror_num); |
3274 | if (bbio->device) | |
3275 | btrfs_dev_stat_inc_and_print(bbio->device, | |
814723e0 | 3276 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
dc380aea MX |
3277 | memset(kaddr + pgoff, 1, len); |
3278 | flush_dcache_page(page); | |
3279 | kunmap_atomic(kaddr); | |
dc380aea MX |
3280 | return -EIO; |
3281 | } | |
3282 | ||
d352ac68 | 3283 | /* |
7ffd27e3 | 3284 | * When reads are done, we need to check csums to verify the data is correct. |
4a54c8c1 JS |
3285 | * if there's a match, we allow the bio to finish. If not, the code in |
3286 | * extent_io.c will try to find good copies for us. | |
7ffd27e3 QW |
3287 | * |
3288 | * @bio_offset: offset to the beginning of the bio (in bytes) | |
3289 | * @start: file offset of the range start | |
3290 | * @end: file offset of the range end (inclusive) | |
08508fea QW |
3291 | * |
3292 | * Return a bitmap where bit set means a csum mismatch, and bit not set means | |
3293 | * csum match. | |
d352ac68 | 3294 | */ |
c3a3b19b QW |
3295 | unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio, |
3296 | u32 bio_offset, struct page *page, | |
3297 | u64 start, u64 end) | |
07157aac | 3298 | { |
07157aac | 3299 | struct inode *inode = page->mapping->host; |
e4f94347 | 3300 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
d1310b2e | 3301 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3302 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f44cf410 QW |
3303 | const u32 sectorsize = root->fs_info->sectorsize; |
3304 | u32 pg_off; | |
08508fea | 3305 | unsigned int result = 0; |
d1310b2e | 3306 | |
e4f94347 QW |
3307 | if (btrfs_page_test_checked(fs_info, page, start, end + 1 - start)) { |
3308 | btrfs_page_clear_checked(fs_info, page, start, end + 1 - start); | |
dc380aea | 3309 | return 0; |
d20f7043 | 3310 | } |
6cbff00f | 3311 | |
3670e645 | 3312 | /* |
e4f94347 QW |
3313 | * This only happens for NODATASUM or compressed read. |
3314 | * Normally this should be covered by above check for compressed read | |
3315 | * or the next check for NODATASUM. Just do a quicker exit here. | |
3670e645 | 3316 | */ |
c3a3b19b | 3317 | if (bbio->csum == NULL) |
dc380aea | 3318 | return 0; |
17d217fe | 3319 | |
6cbff00f | 3320 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
42437a63 JB |
3321 | return 0; |
3322 | ||
056c8311 | 3323 | if (unlikely(test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))) |
b6cda9bc | 3324 | return 0; |
d20f7043 | 3325 | |
f44cf410 QW |
3326 | ASSERT(page_offset(page) <= start && |
3327 | end <= page_offset(page) + PAGE_SIZE - 1); | |
3328 | for (pg_off = offset_in_page(start); | |
3329 | pg_off < offset_in_page(end); | |
3330 | pg_off += sectorsize, bio_offset += sectorsize) { | |
e3c62324 | 3331 | u64 file_offset = pg_off + page_offset(page); |
f44cf410 QW |
3332 | int ret; |
3333 | ||
37f00a6d | 3334 | if (btrfs_is_data_reloc_root(root) && |
e3c62324 QW |
3335 | test_range_bit(io_tree, file_offset, |
3336 | file_offset + sectorsize - 1, | |
3337 | EXTENT_NODATASUM, 1, NULL)) { | |
3338 | /* Skip the range without csum for data reloc inode */ | |
3339 | clear_extent_bits(io_tree, file_offset, | |
3340 | file_offset + sectorsize - 1, | |
3341 | EXTENT_NODATASUM); | |
3342 | continue; | |
3343 | } | |
c3a3b19b | 3344 | ret = check_data_csum(inode, bbio, bio_offset, page, pg_off, |
c1d6abda | 3345 | page_offset(page) + pg_off); |
08508fea QW |
3346 | if (ret < 0) { |
3347 | const int nr_bit = (pg_off - offset_in_page(start)) >> | |
3348 | root->fs_info->sectorsize_bits; | |
3349 | ||
3350 | result |= (1U << nr_bit); | |
3351 | } | |
f44cf410 | 3352 | } |
08508fea | 3353 | return result; |
07157aac | 3354 | } |
b888db2b | 3355 | |
c1c3fac2 NB |
3356 | /* |
3357 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
3358 | * | |
3359 | * @inode: The inode we want to perform iput on | |
3360 | * | |
3361 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3362 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3363 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3364 | * transaction commit time/superblock commit/cleaner kthread. | |
3365 | */ | |
24bbcf04 YZ |
3366 | void btrfs_add_delayed_iput(struct inode *inode) |
3367 | { | |
0b246afa | 3368 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3369 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3370 | |
3371 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3372 | return; | |
3373 | ||
034f784d | 3374 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 3375 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
3376 | ASSERT(list_empty(&binode->delayed_iput)); |
3377 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 3378 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
3379 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3380 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3381 | } |
3382 | ||
63611e73 JB |
3383 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3384 | struct btrfs_inode *inode) | |
3385 | { | |
3386 | list_del_init(&inode->delayed_iput); | |
3387 | spin_unlock(&fs_info->delayed_iput_lock); | |
3388 | iput(&inode->vfs_inode); | |
3389 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3390 | wake_up(&fs_info->delayed_iputs_wait); | |
3391 | spin_lock(&fs_info->delayed_iput_lock); | |
3392 | } | |
3393 | ||
3394 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3395 | struct btrfs_inode *inode) | |
3396 | { | |
3397 | if (!list_empty(&inode->delayed_iput)) { | |
3398 | spin_lock(&fs_info->delayed_iput_lock); | |
3399 | if (!list_empty(&inode->delayed_iput)) | |
3400 | run_delayed_iput_locked(fs_info, inode); | |
3401 | spin_unlock(&fs_info->delayed_iput_lock); | |
3402 | } | |
3403 | } | |
3404 | ||
2ff7e61e | 3405 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3406 | { |
24bbcf04 | 3407 | |
24bbcf04 | 3408 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3409 | while (!list_empty(&fs_info->delayed_iputs)) { |
3410 | struct btrfs_inode *inode; | |
3411 | ||
3412 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3413 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3414 | run_delayed_iput_locked(fs_info, inode); |
71795ee5 | 3415 | cond_resched_lock(&fs_info->delayed_iput_lock); |
24bbcf04 | 3416 | } |
8089fe62 | 3417 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3418 | } |
3419 | ||
034f784d | 3420 | /** |
2639631d NB |
3421 | * Wait for flushing all delayed iputs |
3422 | * | |
3423 | * @fs_info: the filesystem | |
034f784d JB |
3424 | * |
3425 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3426 | * set. Once they are all done running we will return, unless we are killed in | |
3427 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3428 | * that might get blocked on the iputs. | |
2639631d NB |
3429 | * |
3430 | * Return EINTR if we were killed, 0 if nothing's pending | |
034f784d JB |
3431 | */ |
3432 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3433 | { | |
3434 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3435 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3436 | if (ret) | |
3437 | return -EINTR; | |
3438 | return 0; | |
3439 | } | |
3440 | ||
7b128766 | 3441 | /* |
f7e9e8fc OS |
3442 | * This creates an orphan entry for the given inode in case something goes wrong |
3443 | * in the middle of an unlink. | |
7b128766 | 3444 | */ |
73f2e545 | 3445 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3446 | struct btrfs_inode *inode) |
7b128766 | 3447 | { |
d68fc57b | 3448 | int ret; |
7b128766 | 3449 | |
27919067 OS |
3450 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3451 | if (ret && ret != -EEXIST) { | |
3452 | btrfs_abort_transaction(trans, ret); | |
3453 | return ret; | |
d68fc57b YZ |
3454 | } |
3455 | ||
d68fc57b | 3456 | return 0; |
7b128766 JB |
3457 | } |
3458 | ||
3459 | /* | |
f7e9e8fc OS |
3460 | * We have done the delete so we can go ahead and remove the orphan item for |
3461 | * this particular inode. | |
7b128766 | 3462 | */ |
48a3b636 | 3463 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3464 | struct btrfs_inode *inode) |
7b128766 | 3465 | { |
27919067 | 3466 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3467 | } |
3468 | ||
3469 | /* | |
3470 | * this cleans up any orphans that may be left on the list from the last use | |
3471 | * of this root. | |
3472 | */ | |
66b4ffd1 | 3473 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3474 | { |
0b246afa | 3475 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3476 | struct btrfs_path *path; |
3477 | struct extent_buffer *leaf; | |
7b128766 JB |
3478 | struct btrfs_key key, found_key; |
3479 | struct btrfs_trans_handle *trans; | |
3480 | struct inode *inode; | |
8f6d7f4f | 3481 | u64 last_objectid = 0; |
f7e9e8fc | 3482 | int ret = 0, nr_unlink = 0; |
7b128766 | 3483 | |
54230013 | 3484 | if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) |
66b4ffd1 | 3485 | return 0; |
c71bf099 YZ |
3486 | |
3487 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3488 | if (!path) { |
3489 | ret = -ENOMEM; | |
3490 | goto out; | |
3491 | } | |
e4058b54 | 3492 | path->reada = READA_BACK; |
7b128766 JB |
3493 | |
3494 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3495 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3496 | key.offset = (u64)-1; |
3497 | ||
7b128766 JB |
3498 | while (1) { |
3499 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3500 | if (ret < 0) |
3501 | goto out; | |
7b128766 JB |
3502 | |
3503 | /* | |
3504 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3505 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3506 | * find the key and see if we have stuff that matches |
3507 | */ | |
3508 | if (ret > 0) { | |
66b4ffd1 | 3509 | ret = 0; |
7b128766 JB |
3510 | if (path->slots[0] == 0) |
3511 | break; | |
3512 | path->slots[0]--; | |
3513 | } | |
3514 | ||
3515 | /* pull out the item */ | |
3516 | leaf = path->nodes[0]; | |
7b128766 JB |
3517 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3518 | ||
3519 | /* make sure the item matches what we want */ | |
3520 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3521 | break; | |
962a298f | 3522 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3523 | break; |
3524 | ||
3525 | /* release the path since we're done with it */ | |
b3b4aa74 | 3526 | btrfs_release_path(path); |
7b128766 JB |
3527 | |
3528 | /* | |
3529 | * this is where we are basically btrfs_lookup, without the | |
3530 | * crossing root thing. we store the inode number in the | |
3531 | * offset of the orphan item. | |
3532 | */ | |
8f6d7f4f JB |
3533 | |
3534 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3535 | btrfs_err(fs_info, |
3536 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3537 | ret = -EINVAL; |
3538 | goto out; | |
3539 | } | |
3540 | ||
3541 | last_objectid = found_key.offset; | |
3542 | ||
5d4f98a2 YZ |
3543 | found_key.objectid = found_key.offset; |
3544 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3545 | found_key.offset = 0; | |
0202e83f | 3546 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
8c6ffba0 | 3547 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3548 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3549 | goto out; |
7b128766 | 3550 | |
0b246afa | 3551 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 | 3552 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3553 | int is_dead_root = 0; |
3554 | ||
3555 | /* | |
0c0218e9 | 3556 | * This is an orphan in the tree root. Currently these |
f8e9e0b0 | 3557 | * could come from 2 sources: |
0c0218e9 | 3558 | * a) a root (snapshot/subvolume) deletion in progress |
f8e9e0b0 | 3559 | * b) a free space cache inode |
0c0218e9 FM |
3560 | * We need to distinguish those two, as the orphan item |
3561 | * for a root must not get deleted before the deletion | |
3562 | * of the snapshot/subvolume's tree completes. | |
3563 | * | |
3564 | * btrfs_find_orphan_roots() ran before us, which has | |
3565 | * found all deleted roots and loaded them into | |
3566 | * fs_info->fs_roots_radix. So here we can find if an | |
3567 | * orphan item corresponds to a deleted root by looking | |
3568 | * up the root from that radix tree. | |
f8e9e0b0 | 3569 | */ |
a619b3c7 RK |
3570 | |
3571 | spin_lock(&fs_info->fs_roots_radix_lock); | |
3572 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3573 | (unsigned long)found_key.objectid); | |
3574 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) | |
3575 | is_dead_root = 1; | |
3576 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
3577 | ||
f8e9e0b0 AJ |
3578 | if (is_dead_root) { |
3579 | /* prevent this orphan from being found again */ | |
3580 | key.offset = found_key.objectid - 1; | |
3581 | continue; | |
3582 | } | |
f7e9e8fc | 3583 | |
f8e9e0b0 | 3584 | } |
f7e9e8fc | 3585 | |
7b128766 | 3586 | /* |
f7e9e8fc | 3587 | * If we have an inode with links, there are a couple of |
70524253 BB |
3588 | * possibilities: |
3589 | * | |
3590 | * 1. We were halfway through creating fsverity metadata for the | |
3591 | * file. In that case, the orphan item represents incomplete | |
3592 | * fsverity metadata which must be cleaned up with | |
3593 | * btrfs_drop_verity_items and deleting the orphan item. | |
3594 | ||
3595 | * 2. Old kernels (before v3.12) used to create an | |
f7e9e8fc OS |
3596 | * orphan item for truncate indicating that there were possibly |
3597 | * extent items past i_size that needed to be deleted. In v3.12, | |
3598 | * truncate was changed to update i_size in sync with the extent | |
3599 | * items, but the (useless) orphan item was still created. Since | |
3600 | * v4.18, we don't create the orphan item for truncate at all. | |
3601 | * | |
3602 | * So, this item could mean that we need to do a truncate, but | |
3603 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3604 | * and was not cleanly unmounted. The odds of that are quite | |
3605 | * slim, and it's a pain to do the truncate now, so just delete | |
3606 | * the orphan item. | |
3607 | * | |
3608 | * It's also possible that this orphan item was supposed to be | |
3609 | * deleted but wasn't. The inode number may have been reused, | |
3610 | * but either way, we can delete the orphan item. | |
7b128766 | 3611 | */ |
f7e9e8fc | 3612 | if (ret == -ENOENT || inode->i_nlink) { |
70524253 BB |
3613 | if (!ret) { |
3614 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); | |
f7e9e8fc | 3615 | iput(inode); |
70524253 BB |
3616 | if (ret) |
3617 | goto out; | |
3618 | } | |
a8c9e576 | 3619 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3620 | if (IS_ERR(trans)) { |
3621 | ret = PTR_ERR(trans); | |
3622 | goto out; | |
3623 | } | |
0b246afa JM |
3624 | btrfs_debug(fs_info, "auto deleting %Lu", |
3625 | found_key.objectid); | |
a8c9e576 JB |
3626 | ret = btrfs_del_orphan_item(trans, root, |
3627 | found_key.objectid); | |
3a45bb20 | 3628 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3629 | if (ret) |
3630 | goto out; | |
7b128766 JB |
3631 | continue; |
3632 | } | |
3633 | ||
f7e9e8fc | 3634 | nr_unlink++; |
7b128766 JB |
3635 | |
3636 | /* this will do delete_inode and everything for us */ | |
3637 | iput(inode); | |
3638 | } | |
3254c876 MX |
3639 | /* release the path since we're done with it */ |
3640 | btrfs_release_path(path); | |
3641 | ||
a575ceeb | 3642 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3643 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3644 | if (!IS_ERR(trans)) |
3a45bb20 | 3645 | btrfs_end_transaction(trans); |
d68fc57b | 3646 | } |
7b128766 JB |
3647 | |
3648 | if (nr_unlink) | |
0b246afa | 3649 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3650 | |
3651 | out: | |
3652 | if (ret) | |
0b246afa | 3653 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3654 | btrfs_free_path(path); |
3655 | return ret; | |
7b128766 JB |
3656 | } |
3657 | ||
46a53cca CM |
3658 | /* |
3659 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3660 | * don't find any xattrs, we know there can't be any acls. | |
3661 | * | |
3662 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3663 | */ | |
3664 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3665 | int slot, u64 objectid, |
3666 | int *first_xattr_slot) | |
46a53cca CM |
3667 | { |
3668 | u32 nritems = btrfs_header_nritems(leaf); | |
3669 | struct btrfs_key found_key; | |
f23b5a59 JB |
3670 | static u64 xattr_access = 0; |
3671 | static u64 xattr_default = 0; | |
46a53cca CM |
3672 | int scanned = 0; |
3673 | ||
f23b5a59 | 3674 | if (!xattr_access) { |
97d79299 AG |
3675 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3676 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3677 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3678 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3679 | } |
3680 | ||
46a53cca | 3681 | slot++; |
63541927 | 3682 | *first_xattr_slot = -1; |
46a53cca CM |
3683 | while (slot < nritems) { |
3684 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3685 | ||
3686 | /* we found a different objectid, there must not be acls */ | |
3687 | if (found_key.objectid != objectid) | |
3688 | return 0; | |
3689 | ||
3690 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3691 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3692 | if (*first_xattr_slot == -1) |
3693 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3694 | if (found_key.offset == xattr_access || |
3695 | found_key.offset == xattr_default) | |
3696 | return 1; | |
3697 | } | |
46a53cca CM |
3698 | |
3699 | /* | |
3700 | * we found a key greater than an xattr key, there can't | |
3701 | * be any acls later on | |
3702 | */ | |
3703 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3704 | return 0; | |
3705 | ||
3706 | slot++; | |
3707 | scanned++; | |
3708 | ||
3709 | /* | |
3710 | * it goes inode, inode backrefs, xattrs, extents, | |
3711 | * so if there are a ton of hard links to an inode there can | |
3712 | * be a lot of backrefs. Don't waste time searching too hard, | |
3713 | * this is just an optimization | |
3714 | */ | |
3715 | if (scanned >= 8) | |
3716 | break; | |
3717 | } | |
3718 | /* we hit the end of the leaf before we found an xattr or | |
3719 | * something larger than an xattr. We have to assume the inode | |
3720 | * has acls | |
3721 | */ | |
63541927 FDBM |
3722 | if (*first_xattr_slot == -1) |
3723 | *first_xattr_slot = slot; | |
46a53cca CM |
3724 | return 1; |
3725 | } | |
3726 | ||
d352ac68 CM |
3727 | /* |
3728 | * read an inode from the btree into the in-memory inode | |
3729 | */ | |
4222ea71 FM |
3730 | static int btrfs_read_locked_inode(struct inode *inode, |
3731 | struct btrfs_path *in_path) | |
39279cc3 | 3732 | { |
0b246afa | 3733 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3734 | struct btrfs_path *path = in_path; |
5f39d397 | 3735 | struct extent_buffer *leaf; |
39279cc3 CM |
3736 | struct btrfs_inode_item *inode_item; |
3737 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3738 | struct btrfs_key location; | |
67de1176 | 3739 | unsigned long ptr; |
46a53cca | 3740 | int maybe_acls; |
618e21d5 | 3741 | u32 rdev; |
39279cc3 | 3742 | int ret; |
2f7e33d4 | 3743 | bool filled = false; |
63541927 | 3744 | int first_xattr_slot; |
2f7e33d4 MX |
3745 | |
3746 | ret = btrfs_fill_inode(inode, &rdev); | |
3747 | if (!ret) | |
3748 | filled = true; | |
39279cc3 | 3749 | |
4222ea71 FM |
3750 | if (!path) { |
3751 | path = btrfs_alloc_path(); | |
3752 | if (!path) | |
3753 | return -ENOMEM; | |
3754 | } | |
1748f843 | 3755 | |
39279cc3 | 3756 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3757 | |
39279cc3 | 3758 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3759 | if (ret) { |
4222ea71 FM |
3760 | if (path != in_path) |
3761 | btrfs_free_path(path); | |
f5b3a417 | 3762 | return ret; |
67710892 | 3763 | } |
39279cc3 | 3764 | |
5f39d397 | 3765 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3766 | |
3767 | if (filled) | |
67de1176 | 3768 | goto cache_index; |
2f7e33d4 | 3769 | |
5f39d397 CM |
3770 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3771 | struct btrfs_inode_item); | |
5f39d397 | 3772 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3773 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3774 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3775 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3776 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3777 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3778 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3779 | |
a937b979 DS |
3780 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3781 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3782 | |
a937b979 DS |
3783 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3784 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3785 | |
a937b979 DS |
3786 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3787 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3788 | |
9cc97d64 | 3789 | BTRFS_I(inode)->i_otime.tv_sec = |
3790 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3791 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3792 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3793 | |
a76a3cd4 | 3794 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3795 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3796 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3797 | ||
c7f88c4e JL |
3798 | inode_set_iversion_queried(inode, |
3799 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3800 | inode->i_generation = BTRFS_I(inode)->generation; |
3801 | inode->i_rdev = 0; | |
3802 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3803 | ||
3804 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
77eea05e BB |
3805 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
3806 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); | |
6e17d30b YD |
3807 | |
3808 | cache_index: | |
5dc562c5 JB |
3809 | /* |
3810 | * If we were modified in the current generation and evicted from memory | |
3811 | * and then re-read we need to do a full sync since we don't have any | |
3812 | * idea about which extents were modified before we were evicted from | |
3813 | * cache. | |
6e17d30b YD |
3814 | * |
3815 | * This is required for both inode re-read from disk and delayed inode | |
3816 | * in delayed_nodes_tree. | |
5dc562c5 | 3817 | */ |
0b246afa | 3818 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3819 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3820 | &BTRFS_I(inode)->runtime_flags); | |
3821 | ||
bde6c242 FM |
3822 | /* |
3823 | * We don't persist the id of the transaction where an unlink operation | |
3824 | * against the inode was last made. So here we assume the inode might | |
3825 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3826 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3827 | * between the inode and its parent if the inode is fsync'ed and the log | |
3828 | * replayed. For example, in the scenario: | |
3829 | * | |
3830 | * touch mydir/foo | |
3831 | * ln mydir/foo mydir/bar | |
3832 | * sync | |
3833 | * unlink mydir/bar | |
3834 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3835 | * xfs_io -c fsync mydir/foo | |
3836 | * <power failure> | |
3837 | * mount fs, triggers fsync log replay | |
3838 | * | |
3839 | * We must make sure that when we fsync our inode foo we also log its | |
3840 | * parent inode, otherwise after log replay the parent still has the | |
3841 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3842 | * and doesn't have an inode ref with the name "bar" anymore. | |
3843 | * | |
3844 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3845 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3846 | * transaction commits on fsync if our inode is a directory, or if our |
3847 | * inode is not a directory, logging its parent unnecessarily. | |
3848 | */ | |
3849 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3850 | ||
3ebac17c FM |
3851 | /* |
3852 | * Same logic as for last_unlink_trans. We don't persist the generation | |
3853 | * of the last transaction where this inode was used for a reflink | |
3854 | * operation, so after eviction and reloading the inode we must be | |
3855 | * pessimistic and assume the last transaction that modified the inode. | |
3856 | */ | |
3857 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
3858 | ||
67de1176 MX |
3859 | path->slots[0]++; |
3860 | if (inode->i_nlink != 1 || | |
3861 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3862 | goto cache_acl; | |
3863 | ||
3864 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3865 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3866 | goto cache_acl; |
3867 | ||
3868 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3869 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3870 | struct btrfs_inode_ref *ref; | |
3871 | ||
3872 | ref = (struct btrfs_inode_ref *)ptr; | |
3873 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3874 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3875 | struct btrfs_inode_extref *extref; | |
3876 | ||
3877 | extref = (struct btrfs_inode_extref *)ptr; | |
3878 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3879 | extref); | |
3880 | } | |
2f7e33d4 | 3881 | cache_acl: |
46a53cca CM |
3882 | /* |
3883 | * try to precache a NULL acl entry for files that don't have | |
3884 | * any xattrs or acls | |
3885 | */ | |
33345d01 | 3886 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3887 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3888 | if (first_xattr_slot != -1) { |
3889 | path->slots[0] = first_xattr_slot; | |
3890 | ret = btrfs_load_inode_props(inode, path); | |
3891 | if (ret) | |
0b246afa | 3892 | btrfs_err(fs_info, |
351fd353 | 3893 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3894 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3895 | root->root_key.objectid, ret); |
3896 | } | |
4222ea71 FM |
3897 | if (path != in_path) |
3898 | btrfs_free_path(path); | |
63541927 | 3899 | |
72c04902 AV |
3900 | if (!maybe_acls) |
3901 | cache_no_acl(inode); | |
46a53cca | 3902 | |
39279cc3 | 3903 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3904 | case S_IFREG: |
3905 | inode->i_mapping->a_ops = &btrfs_aops; | |
3906 | inode->i_fop = &btrfs_file_operations; | |
3907 | inode->i_op = &btrfs_file_inode_operations; | |
3908 | break; | |
3909 | case S_IFDIR: | |
3910 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3911 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3912 | break; |
3913 | case S_IFLNK: | |
3914 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3915 | inode_nohighmem(inode); |
4779cc04 | 3916 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3917 | break; |
618e21d5 | 3918 | default: |
0279b4cd | 3919 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3920 | init_special_inode(inode, inode->i_mode, rdev); |
3921 | break; | |
39279cc3 | 3922 | } |
6cbff00f | 3923 | |
7b6a221e | 3924 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3925 | return 0; |
39279cc3 CM |
3926 | } |
3927 | ||
d352ac68 CM |
3928 | /* |
3929 | * given a leaf and an inode, copy the inode fields into the leaf | |
3930 | */ | |
e02119d5 CM |
3931 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3932 | struct extent_buffer *leaf, | |
5f39d397 | 3933 | struct btrfs_inode_item *item, |
39279cc3 CM |
3934 | struct inode *inode) |
3935 | { | |
51fab693 | 3936 | struct btrfs_map_token token; |
77eea05e | 3937 | u64 flags; |
51fab693 | 3938 | |
c82f823c | 3939 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3940 | |
cc4c13d5 DS |
3941 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3942 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3943 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3944 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3945 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3946 | ||
3947 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
3948 | inode->i_atime.tv_sec); | |
3949 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
3950 | inode->i_atime.tv_nsec); | |
3951 | ||
3952 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
3953 | inode->i_mtime.tv_sec); | |
3954 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
3955 | inode->i_mtime.tv_nsec); | |
3956 | ||
3957 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
3958 | inode->i_ctime.tv_sec); | |
3959 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
3960 | inode->i_ctime.tv_nsec); | |
3961 | ||
3962 | btrfs_set_token_timespec_sec(&token, &item->otime, | |
3963 | BTRFS_I(inode)->i_otime.tv_sec); | |
3964 | btrfs_set_token_timespec_nsec(&token, &item->otime, | |
3965 | BTRFS_I(inode)->i_otime.tv_nsec); | |
3966 | ||
3967 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3968 | btrfs_set_token_inode_generation(&token, item, | |
3969 | BTRFS_I(inode)->generation); | |
3970 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3971 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3972 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
3973 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
3974 | BTRFS_I(inode)->ro_flags); | |
3975 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 3976 | btrfs_set_token_inode_block_group(&token, item, 0); |
39279cc3 CM |
3977 | } |
3978 | ||
d352ac68 CM |
3979 | /* |
3980 | * copy everything in the in-memory inode into the btree. | |
3981 | */ | |
2115133f | 3982 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
dfeb9e7c NB |
3983 | struct btrfs_root *root, |
3984 | struct btrfs_inode *inode) | |
39279cc3 CM |
3985 | { |
3986 | struct btrfs_inode_item *inode_item; | |
3987 | struct btrfs_path *path; | |
5f39d397 | 3988 | struct extent_buffer *leaf; |
39279cc3 CM |
3989 | int ret; |
3990 | ||
3991 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3992 | if (!path) |
3993 | return -ENOMEM; | |
3994 | ||
dfeb9e7c | 3995 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
39279cc3 CM |
3996 | if (ret) { |
3997 | if (ret > 0) | |
3998 | ret = -ENOENT; | |
3999 | goto failed; | |
4000 | } | |
4001 | ||
5f39d397 CM |
4002 | leaf = path->nodes[0]; |
4003 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 4004 | struct btrfs_inode_item); |
39279cc3 | 4005 | |
dfeb9e7c | 4006 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
5f39d397 | 4007 | btrfs_mark_buffer_dirty(leaf); |
dfeb9e7c | 4008 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
4009 | ret = 0; |
4010 | failed: | |
39279cc3 CM |
4011 | btrfs_free_path(path); |
4012 | return ret; | |
4013 | } | |
4014 | ||
2115133f CM |
4015 | /* |
4016 | * copy everything in the in-memory inode into the btree. | |
4017 | */ | |
4018 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
9a56fcd1 NB |
4019 | struct btrfs_root *root, |
4020 | struct btrfs_inode *inode) | |
2115133f | 4021 | { |
0b246afa | 4022 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
4023 | int ret; |
4024 | ||
4025 | /* | |
4026 | * If the inode is a free space inode, we can deadlock during commit | |
4027 | * if we put it into the delayed code. | |
4028 | * | |
4029 | * The data relocation inode should also be directly updated | |
4030 | * without delay | |
4031 | */ | |
9a56fcd1 | 4032 | if (!btrfs_is_free_space_inode(inode) |
37f00a6d | 4033 | && !btrfs_is_data_reloc_root(root) |
0b246afa | 4034 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4035 | btrfs_update_root_times(trans, root); |
4036 | ||
9a56fcd1 | 4037 | ret = btrfs_delayed_update_inode(trans, root, inode); |
2115133f | 4038 | if (!ret) |
9a56fcd1 | 4039 | btrfs_set_inode_last_trans(trans, inode); |
2115133f CM |
4040 | return ret; |
4041 | } | |
4042 | ||
9a56fcd1 | 4043 | return btrfs_update_inode_item(trans, root, inode); |
2115133f CM |
4044 | } |
4045 | ||
729f7961 NB |
4046 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4047 | struct btrfs_root *root, struct btrfs_inode *inode) | |
2115133f CM |
4048 | { |
4049 | int ret; | |
4050 | ||
729f7961 | 4051 | ret = btrfs_update_inode(trans, root, inode); |
2115133f | 4052 | if (ret == -ENOSPC) |
729f7961 | 4053 | return btrfs_update_inode_item(trans, root, inode); |
2115133f CM |
4054 | return ret; |
4055 | } | |
4056 | ||
d352ac68 CM |
4057 | /* |
4058 | * unlink helper that gets used here in inode.c and in the tree logging | |
4059 | * recovery code. It remove a link in a directory with a given name, and | |
4060 | * also drops the back refs in the inode to the directory | |
4061 | */ | |
92986796 | 4062 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e NB |
4063 | struct btrfs_inode *dir, |
4064 | struct btrfs_inode *inode, | |
92986796 | 4065 | const char *name, int name_len) |
39279cc3 | 4066 | { |
4467af88 | 4067 | struct btrfs_root *root = dir->root; |
0b246afa | 4068 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4069 | struct btrfs_path *path; |
39279cc3 | 4070 | int ret = 0; |
39279cc3 | 4071 | struct btrfs_dir_item *di; |
aec7477b | 4072 | u64 index; |
33345d01 LZ |
4073 | u64 ino = btrfs_ino(inode); |
4074 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4075 | |
4076 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4077 | if (!path) { |
4078 | ret = -ENOMEM; | |
554233a6 | 4079 | goto out; |
54aa1f4d CM |
4080 | } |
4081 | ||
33345d01 | 4082 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 4083 | name, name_len, -1); |
3cf5068f LB |
4084 | if (IS_ERR_OR_NULL(di)) { |
4085 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
4086 | goto err; |
4087 | } | |
39279cc3 | 4088 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4089 | if (ret) |
4090 | goto err; | |
b3b4aa74 | 4091 | btrfs_release_path(path); |
39279cc3 | 4092 | |
67de1176 MX |
4093 | /* |
4094 | * If we don't have dir index, we have to get it by looking up | |
4095 | * the inode ref, since we get the inode ref, remove it directly, | |
4096 | * it is unnecessary to do delayed deletion. | |
4097 | * | |
4098 | * But if we have dir index, needn't search inode ref to get it. | |
4099 | * Since the inode ref is close to the inode item, it is better | |
4100 | * that we delay to delete it, and just do this deletion when | |
4101 | * we update the inode item. | |
4102 | */ | |
4ec5934e | 4103 | if (inode->dir_index) { |
67de1176 MX |
4104 | ret = btrfs_delayed_delete_inode_ref(inode); |
4105 | if (!ret) { | |
4ec5934e | 4106 | index = inode->dir_index; |
67de1176 MX |
4107 | goto skip_backref; |
4108 | } | |
4109 | } | |
4110 | ||
33345d01 LZ |
4111 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4112 | dir_ino, &index); | |
aec7477b | 4113 | if (ret) { |
0b246afa | 4114 | btrfs_info(fs_info, |
c2cf52eb | 4115 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4116 | name_len, name, ino, dir_ino); |
66642832 | 4117 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4118 | goto err; |
4119 | } | |
67de1176 | 4120 | skip_backref: |
9add2945 | 4121 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4122 | if (ret) { |
66642832 | 4123 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4124 | goto err; |
79787eaa | 4125 | } |
39279cc3 | 4126 | |
9a35fc95 JB |
4127 | btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4128 | dir_ino); | |
4129 | btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, index); | |
63611e73 JB |
4130 | |
4131 | /* | |
4132 | * If we have a pending delayed iput we could end up with the final iput | |
4133 | * being run in btrfs-cleaner context. If we have enough of these built | |
4134 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
4135 | * way to throttle the unlinks. Since we're currently holding a ref on | |
4136 | * the inode we can run the delayed iput here without any issues as the | |
4137 | * final iput won't be done until after we drop the ref we're currently | |
4138 | * holding. | |
4139 | */ | |
4140 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
4141 | err: |
4142 | btrfs_free_path(path); | |
e02119d5 CM |
4143 | if (ret) |
4144 | goto out; | |
4145 | ||
6ef06d27 | 4146 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4147 | inode_inc_iversion(&inode->vfs_inode); |
4148 | inode_inc_iversion(&dir->vfs_inode); | |
4149 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4150 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
9a56fcd1 | 4151 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4152 | out: |
39279cc3 CM |
4153 | return ret; |
4154 | } | |
4155 | ||
92986796 | 4156 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e | 4157 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4158 | const char *name, int name_len) |
4159 | { | |
4160 | int ret; | |
4467af88 | 4161 | ret = __btrfs_unlink_inode(trans, dir, inode, name, name_len); |
92986796 | 4162 | if (!ret) { |
4ec5934e | 4163 | drop_nlink(&inode->vfs_inode); |
4467af88 | 4164 | ret = btrfs_update_inode(trans, inode->root, inode); |
92986796 AV |
4165 | } |
4166 | return ret; | |
4167 | } | |
39279cc3 | 4168 | |
a22285a6 YZ |
4169 | /* |
4170 | * helper to start transaction for unlink and rmdir. | |
4171 | * | |
d52be818 JB |
4172 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4173 | * if we cannot make our reservations the normal way try and see if there is | |
4174 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4175 | * allow the unlink to occur. | |
a22285a6 | 4176 | */ |
d52be818 | 4177 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4178 | { |
a22285a6 | 4179 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4180 | |
e70bea5f JB |
4181 | /* |
4182 | * 1 for the possible orphan item | |
4183 | * 1 for the dir item | |
4184 | * 1 for the dir index | |
4185 | * 1 for the inode ref | |
e70bea5f JB |
4186 | * 1 for the inode |
4187 | */ | |
7f9fe614 | 4188 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
a22285a6 YZ |
4189 | } |
4190 | ||
4191 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4192 | { | |
a22285a6 | 4193 | struct btrfs_trans_handle *trans; |
2b0143b5 | 4194 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4195 | int ret; |
a22285a6 | 4196 | |
d52be818 | 4197 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4198 | if (IS_ERR(trans)) |
4199 | return PTR_ERR(trans); | |
5f39d397 | 4200 | |
4ec5934e NB |
4201 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4202 | 0); | |
12fcfd22 | 4203 | |
4467af88 | 4204 | ret = btrfs_unlink_inode(trans, BTRFS_I(dir), |
4ec5934e NB |
4205 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
4206 | dentry->d_name.len); | |
b532402e TI |
4207 | if (ret) |
4208 | goto out; | |
7b128766 | 4209 | |
a22285a6 | 4210 | if (inode->i_nlink == 0) { |
73f2e545 | 4211 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4212 | if (ret) |
4213 | goto out; | |
a22285a6 | 4214 | } |
7b128766 | 4215 | |
b532402e | 4216 | out: |
3a45bb20 | 4217 | btrfs_end_transaction(trans); |
4467af88 | 4218 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
39279cc3 CM |
4219 | return ret; |
4220 | } | |
4221 | ||
f60a2364 | 4222 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
045d3967 | 4223 | struct inode *dir, struct dentry *dentry) |
4df27c4d | 4224 | { |
401b3b19 | 4225 | struct btrfs_root *root = BTRFS_I(dir)->root; |
045d3967 | 4226 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
4227 | struct btrfs_path *path; |
4228 | struct extent_buffer *leaf; | |
4229 | struct btrfs_dir_item *di; | |
4230 | struct btrfs_key key; | |
045d3967 JB |
4231 | const char *name = dentry->d_name.name; |
4232 | int name_len = dentry->d_name.len; | |
4df27c4d YZ |
4233 | u64 index; |
4234 | int ret; | |
045d3967 | 4235 | u64 objectid; |
4a0cc7ca | 4236 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d | 4237 | |
045d3967 JB |
4238 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
4239 | objectid = inode->root->root_key.objectid; | |
4240 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
4241 | objectid = inode->location.objectid; | |
4242 | } else { | |
4243 | WARN_ON(1); | |
4244 | return -EINVAL; | |
4245 | } | |
4246 | ||
4df27c4d YZ |
4247 | path = btrfs_alloc_path(); |
4248 | if (!path) | |
4249 | return -ENOMEM; | |
4250 | ||
33345d01 | 4251 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4252 | name, name_len, -1); |
79787eaa | 4253 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4254 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4255 | goto out; |
4256 | } | |
4df27c4d YZ |
4257 | |
4258 | leaf = path->nodes[0]; | |
4259 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4260 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4261 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4262 | if (ret) { |
66642832 | 4263 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4264 | goto out; |
4265 | } | |
b3b4aa74 | 4266 | btrfs_release_path(path); |
4df27c4d | 4267 | |
d49d3287 JB |
4268 | /* |
4269 | * This is a placeholder inode for a subvolume we didn't have a | |
4270 | * reference to at the time of the snapshot creation. In the meantime | |
4271 | * we could have renamed the real subvol link into our snapshot, so | |
1a9fd417 | 4272 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
d49d3287 JB |
4273 | * Instead simply lookup the dir_index_item for this entry so we can |
4274 | * remove it. Otherwise we know we have a ref to the root and we can | |
4275 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
4276 | */ | |
4277 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
33345d01 | 4278 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4279 | name, name_len); |
79787eaa JM |
4280 | if (IS_ERR_OR_NULL(di)) { |
4281 | if (!di) | |
4282 | ret = -ENOENT; | |
4283 | else | |
4284 | ret = PTR_ERR(di); | |
66642832 | 4285 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4286 | goto out; |
4287 | } | |
4df27c4d YZ |
4288 | |
4289 | leaf = path->nodes[0]; | |
4290 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 4291 | index = key.offset; |
d49d3287 JB |
4292 | btrfs_release_path(path); |
4293 | } else { | |
4294 | ret = btrfs_del_root_ref(trans, objectid, | |
4295 | root->root_key.objectid, dir_ino, | |
4296 | &index, name, name_len); | |
4297 | if (ret) { | |
4298 | btrfs_abort_transaction(trans, ret); | |
4299 | goto out; | |
4300 | } | |
4df27c4d YZ |
4301 | } |
4302 | ||
9add2945 | 4303 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 4304 | if (ret) { |
66642832 | 4305 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4306 | goto out; |
4307 | } | |
4df27c4d | 4308 | |
6ef06d27 | 4309 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4310 | inode_inc_iversion(dir); |
c2050a45 | 4311 | dir->i_mtime = dir->i_ctime = current_time(dir); |
729f7961 | 4312 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
79787eaa | 4313 | if (ret) |
66642832 | 4314 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4315 | out: |
71d7aed0 | 4316 | btrfs_free_path(path); |
79787eaa | 4317 | return ret; |
4df27c4d YZ |
4318 | } |
4319 | ||
ec42f167 MT |
4320 | /* |
4321 | * Helper to check if the subvolume references other subvolumes or if it's | |
4322 | * default. | |
4323 | */ | |
f60a2364 | 4324 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4325 | { |
4326 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4327 | struct btrfs_path *path; | |
4328 | struct btrfs_dir_item *di; | |
4329 | struct btrfs_key key; | |
4330 | u64 dir_id; | |
4331 | int ret; | |
4332 | ||
4333 | path = btrfs_alloc_path(); | |
4334 | if (!path) | |
4335 | return -ENOMEM; | |
4336 | ||
4337 | /* Make sure this root isn't set as the default subvol */ | |
4338 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4339 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
4340 | dir_id, "default", 7, 0); | |
4341 | if (di && !IS_ERR(di)) { | |
4342 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4343 | if (key.objectid == root->root_key.objectid) { | |
4344 | ret = -EPERM; | |
4345 | btrfs_err(fs_info, | |
4346 | "deleting default subvolume %llu is not allowed", | |
4347 | key.objectid); | |
4348 | goto out; | |
4349 | } | |
4350 | btrfs_release_path(path); | |
4351 | } | |
4352 | ||
4353 | key.objectid = root->root_key.objectid; | |
4354 | key.type = BTRFS_ROOT_REF_KEY; | |
4355 | key.offset = (u64)-1; | |
4356 | ||
4357 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4358 | if (ret < 0) | |
4359 | goto out; | |
4360 | BUG_ON(ret == 0); | |
4361 | ||
4362 | ret = 0; | |
4363 | if (path->slots[0] > 0) { | |
4364 | path->slots[0]--; | |
4365 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4366 | if (key.objectid == root->root_key.objectid && | |
4367 | key.type == BTRFS_ROOT_REF_KEY) | |
4368 | ret = -ENOTEMPTY; | |
4369 | } | |
4370 | out: | |
4371 | btrfs_free_path(path); | |
4372 | return ret; | |
4373 | } | |
4374 | ||
20a68004 NB |
4375 | /* Delete all dentries for inodes belonging to the root */ |
4376 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4377 | { | |
4378 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4379 | struct rb_node *node; | |
4380 | struct rb_node *prev; | |
4381 | struct btrfs_inode *entry; | |
4382 | struct inode *inode; | |
4383 | u64 objectid = 0; | |
4384 | ||
84961539 | 4385 | if (!BTRFS_FS_ERROR(fs_info)) |
20a68004 NB |
4386 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
4387 | ||
4388 | spin_lock(&root->inode_lock); | |
4389 | again: | |
4390 | node = root->inode_tree.rb_node; | |
4391 | prev = NULL; | |
4392 | while (node) { | |
4393 | prev = node; | |
4394 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4395 | ||
37508515 | 4396 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4397 | node = node->rb_left; |
37508515 | 4398 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4399 | node = node->rb_right; |
4400 | else | |
4401 | break; | |
4402 | } | |
4403 | if (!node) { | |
4404 | while (prev) { | |
4405 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4406 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4407 | node = prev; |
4408 | break; | |
4409 | } | |
4410 | prev = rb_next(prev); | |
4411 | } | |
4412 | } | |
4413 | while (node) { | |
4414 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4415 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4416 | inode = igrab(&entry->vfs_inode); |
4417 | if (inode) { | |
4418 | spin_unlock(&root->inode_lock); | |
4419 | if (atomic_read(&inode->i_count) > 1) | |
4420 | d_prune_aliases(inode); | |
4421 | /* | |
4422 | * btrfs_drop_inode will have it removed from the inode | |
4423 | * cache when its usage count hits zero. | |
4424 | */ | |
4425 | iput(inode); | |
4426 | cond_resched(); | |
4427 | spin_lock(&root->inode_lock); | |
4428 | goto again; | |
4429 | } | |
4430 | ||
4431 | if (cond_resched_lock(&root->inode_lock)) | |
4432 | goto again; | |
4433 | ||
4434 | node = rb_next(node); | |
4435 | } | |
4436 | spin_unlock(&root->inode_lock); | |
4437 | } | |
4438 | ||
f60a2364 MT |
4439 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
4440 | { | |
4441 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
4442 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4443 | struct inode *inode = d_inode(dentry); | |
4444 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4445 | struct btrfs_trans_handle *trans; | |
4446 | struct btrfs_block_rsv block_rsv; | |
4447 | u64 root_flags; | |
f60a2364 | 4448 | int ret; |
f60a2364 MT |
4449 | |
4450 | /* | |
4451 | * Don't allow to delete a subvolume with send in progress. This is | |
4452 | * inside the inode lock so the error handling that has to drop the bit | |
4453 | * again is not run concurrently. | |
4454 | */ | |
4455 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4456 | if (dest->send_in_progress) { |
f60a2364 MT |
4457 | spin_unlock(&dest->root_item_lock); |
4458 | btrfs_warn(fs_info, | |
4459 | "attempt to delete subvolume %llu during send", | |
4460 | dest->root_key.objectid); | |
4461 | return -EPERM; | |
4462 | } | |
a7176f74 LF |
4463 | root_flags = btrfs_root_flags(&dest->root_item); |
4464 | btrfs_set_root_flags(&dest->root_item, | |
4465 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4466 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4467 | |
4468 | down_write(&fs_info->subvol_sem); | |
4469 | ||
ee0d904f NB |
4470 | ret = may_destroy_subvol(dest); |
4471 | if (ret) | |
f60a2364 MT |
4472 | goto out_up_write; |
4473 | ||
4474 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4475 | /* | |
4476 | * One for dir inode, | |
4477 | * two for dir entries, | |
4478 | * two for root ref/backref. | |
4479 | */ | |
ee0d904f NB |
4480 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
4481 | if (ret) | |
f60a2364 MT |
4482 | goto out_up_write; |
4483 | ||
4484 | trans = btrfs_start_transaction(root, 0); | |
4485 | if (IS_ERR(trans)) { | |
ee0d904f | 4486 | ret = PTR_ERR(trans); |
f60a2364 MT |
4487 | goto out_release; |
4488 | } | |
4489 | trans->block_rsv = &block_rsv; | |
4490 | trans->bytes_reserved = block_rsv.size; | |
4491 | ||
4492 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
4493 | ||
045d3967 | 4494 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 | 4495 | if (ret) { |
f60a2364 MT |
4496 | btrfs_abort_transaction(trans, ret); |
4497 | goto out_end_trans; | |
4498 | } | |
4499 | ||
2731f518 JB |
4500 | ret = btrfs_record_root_in_trans(trans, dest); |
4501 | if (ret) { | |
4502 | btrfs_abort_transaction(trans, ret); | |
4503 | goto out_end_trans; | |
4504 | } | |
f60a2364 MT |
4505 | |
4506 | memset(&dest->root_item.drop_progress, 0, | |
4507 | sizeof(dest->root_item.drop_progress)); | |
c8422684 | 4508 | btrfs_set_root_drop_level(&dest->root_item, 0); |
f60a2364 MT |
4509 | btrfs_set_root_refs(&dest->root_item, 0); |
4510 | ||
4511 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4512 | ret = btrfs_insert_orphan_item(trans, | |
4513 | fs_info->tree_root, | |
4514 | dest->root_key.objectid); | |
4515 | if (ret) { | |
4516 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4517 | goto out_end_trans; |
4518 | } | |
4519 | } | |
4520 | ||
d1957791 | 4521 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4522 | BTRFS_UUID_KEY_SUBVOL, |
4523 | dest->root_key.objectid); | |
4524 | if (ret && ret != -ENOENT) { | |
4525 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4526 | goto out_end_trans; |
4527 | } | |
4528 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4529 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4530 | dest->root_item.received_uuid, |
4531 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4532 | dest->root_key.objectid); | |
4533 | if (ret && ret != -ENOENT) { | |
4534 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4535 | goto out_end_trans; |
4536 | } | |
4537 | } | |
4538 | ||
082b6c97 QW |
4539 | free_anon_bdev(dest->anon_dev); |
4540 | dest->anon_dev = 0; | |
f60a2364 MT |
4541 | out_end_trans: |
4542 | trans->block_rsv = NULL; | |
4543 | trans->bytes_reserved = 0; | |
4544 | ret = btrfs_end_transaction(trans); | |
f60a2364 MT |
4545 | inode->i_flags |= S_DEAD; |
4546 | out_release: | |
e85fde51 | 4547 | btrfs_subvolume_release_metadata(root, &block_rsv); |
f60a2364 MT |
4548 | out_up_write: |
4549 | up_write(&fs_info->subvol_sem); | |
ee0d904f | 4550 | if (ret) { |
f60a2364 MT |
4551 | spin_lock(&dest->root_item_lock); |
4552 | root_flags = btrfs_root_flags(&dest->root_item); | |
4553 | btrfs_set_root_flags(&dest->root_item, | |
4554 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4555 | spin_unlock(&dest->root_item_lock); | |
4556 | } else { | |
4557 | d_invalidate(dentry); | |
20a68004 | 4558 | btrfs_prune_dentries(dest); |
f60a2364 | 4559 | ASSERT(dest->send_in_progress == 0); |
f60a2364 MT |
4560 | } |
4561 | ||
ee0d904f | 4562 | return ret; |
f60a2364 MT |
4563 | } |
4564 | ||
39279cc3 CM |
4565 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4566 | { | |
2b0143b5 | 4567 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4568 | int err = 0; |
39279cc3 | 4569 | struct btrfs_trans_handle *trans; |
44f714da | 4570 | u64 last_unlink_trans; |
39279cc3 | 4571 | |
b3ae244e | 4572 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4573 | return -ENOTEMPTY; |
4a0cc7ca | 4574 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4575 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4576 | |
d52be818 | 4577 | trans = __unlink_start_trans(dir); |
a22285a6 | 4578 | if (IS_ERR(trans)) |
5df6a9f6 | 4579 | return PTR_ERR(trans); |
5df6a9f6 | 4580 | |
4a0cc7ca | 4581 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 4582 | err = btrfs_unlink_subvol(trans, dir, dentry); |
4df27c4d YZ |
4583 | goto out; |
4584 | } | |
4585 | ||
73f2e545 | 4586 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4587 | if (err) |
4df27c4d | 4588 | goto out; |
7b128766 | 4589 | |
44f714da FM |
4590 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4591 | ||
39279cc3 | 4592 | /* now the directory is empty */ |
4467af88 | 4593 | err = btrfs_unlink_inode(trans, BTRFS_I(dir), |
4ec5934e NB |
4594 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
4595 | dentry->d_name.len); | |
44f714da | 4596 | if (!err) { |
6ef06d27 | 4597 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4598 | /* |
4599 | * Propagate the last_unlink_trans value of the deleted dir to | |
4600 | * its parent directory. This is to prevent an unrecoverable | |
4601 | * log tree in the case we do something like this: | |
4602 | * 1) create dir foo | |
4603 | * 2) create snapshot under dir foo | |
4604 | * 3) delete the snapshot | |
4605 | * 4) rmdir foo | |
4606 | * 5) mkdir foo | |
4607 | * 6) fsync foo or some file inside foo | |
4608 | */ | |
4609 | if (last_unlink_trans >= trans->transid) | |
4610 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4611 | } | |
4df27c4d | 4612 | out: |
3a45bb20 | 4613 | btrfs_end_transaction(trans); |
4467af88 | 4614 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
3954401f | 4615 | |
39279cc3 CM |
4616 | return err; |
4617 | } | |
4618 | ||
39279cc3 | 4619 | /* |
9703fefe | 4620 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4621 | * @inode - inode that we're zeroing |
4622 | * @from - the offset to start zeroing | |
4623 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4624 | * offset | |
4625 | * @front - zero up to the offset instead of from the offset on | |
4626 | * | |
9703fefe | 4627 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4628 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4629 | */ |
217f42eb NB |
4630 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
4631 | int front) | |
39279cc3 | 4632 | { |
217f42eb NB |
4633 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4634 | struct address_space *mapping = inode->vfs_inode.i_mapping; | |
4635 | struct extent_io_tree *io_tree = &inode->io_tree; | |
e6dcd2dc | 4636 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 4637 | struct extent_state *cached_state = NULL; |
364ecf36 | 4638 | struct extent_changeset *data_reserved = NULL; |
6d4572a9 | 4639 | bool only_release_metadata = false; |
0b246afa | 4640 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4641 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4642 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4643 | struct page *page; |
3b16a4e3 | 4644 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4645 | size_t write_bytes = blocksize; |
39279cc3 | 4646 | int ret = 0; |
9703fefe CR |
4647 | u64 block_start; |
4648 | u64 block_end; | |
39279cc3 | 4649 | |
b03ebd99 NB |
4650 | if (IS_ALIGNED(offset, blocksize) && |
4651 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4652 | goto out; |
9703fefe | 4653 | |
8b62f87b JB |
4654 | block_start = round_down(from, blocksize); |
4655 | block_end = block_start + blocksize - 1; | |
4656 | ||
217f42eb NB |
4657 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
4658 | blocksize); | |
6d4572a9 | 4659 | if (ret < 0) { |
217f42eb | 4660 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
6d4572a9 QW |
4661 | /* For nocow case, no need to reserve data space */ |
4662 | only_release_metadata = true; | |
4663 | } else { | |
4664 | goto out; | |
4665 | } | |
4666 | } | |
217f42eb | 4667 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
6d4572a9 QW |
4668 | if (ret < 0) { |
4669 | if (!only_release_metadata) | |
217f42eb NB |
4670 | btrfs_free_reserved_data_space(inode, data_reserved, |
4671 | block_start, blocksize); | |
6d4572a9 QW |
4672 | goto out; |
4673 | } | |
211c17f5 | 4674 | again: |
3b16a4e3 | 4675 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4676 | if (!page) { |
217f42eb NB |
4677 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
4678 | blocksize, true); | |
4679 | btrfs_delalloc_release_extents(inode, blocksize); | |
ac6a2b36 | 4680 | ret = -ENOMEM; |
39279cc3 | 4681 | goto out; |
5d5e103a | 4682 | } |
32443de3 QW |
4683 | ret = set_page_extent_mapped(page); |
4684 | if (ret < 0) | |
4685 | goto out_unlock; | |
e6dcd2dc | 4686 | |
39279cc3 | 4687 | if (!PageUptodate(page)) { |
9ebefb18 | 4688 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4689 | lock_page(page); |
211c17f5 CM |
4690 | if (page->mapping != mapping) { |
4691 | unlock_page(page); | |
09cbfeaf | 4692 | put_page(page); |
211c17f5 CM |
4693 | goto again; |
4694 | } | |
39279cc3 CM |
4695 | if (!PageUptodate(page)) { |
4696 | ret = -EIO; | |
89642229 | 4697 | goto out_unlock; |
39279cc3 CM |
4698 | } |
4699 | } | |
211c17f5 | 4700 | wait_on_page_writeback(page); |
e6dcd2dc | 4701 | |
9703fefe | 4702 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4703 | |
217f42eb | 4704 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4705 | if (ordered) { |
9703fefe | 4706 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4707 | &cached_state); |
e6dcd2dc | 4708 | unlock_page(page); |
09cbfeaf | 4709 | put_page(page); |
c0a43603 | 4710 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
4711 | btrfs_put_ordered_extent(ordered); |
4712 | goto again; | |
4713 | } | |
4714 | ||
217f42eb | 4715 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
e182163d OS |
4716 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
4717 | 0, 0, &cached_state); | |
5d5e103a | 4718 | |
217f42eb | 4719 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 4720 | &cached_state); |
9ed74f2d | 4721 | if (ret) { |
9703fefe | 4722 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4723 | &cached_state); |
9ed74f2d JB |
4724 | goto out_unlock; |
4725 | } | |
4726 | ||
9703fefe | 4727 | if (offset != blocksize) { |
2aaa6655 | 4728 | if (!len) |
9703fefe | 4729 | len = blocksize - offset; |
2aaa6655 | 4730 | if (front) |
d048b9c2 IW |
4731 | memzero_page(page, (block_start - page_offset(page)), |
4732 | offset); | |
2aaa6655 | 4733 | else |
d048b9c2 IW |
4734 | memzero_page(page, (block_start - page_offset(page)) + offset, |
4735 | len); | |
e6dcd2dc | 4736 | flush_dcache_page(page); |
e6dcd2dc | 4737 | } |
e4f94347 QW |
4738 | btrfs_page_clear_checked(fs_info, page, block_start, |
4739 | block_end + 1 - block_start); | |
6c9ac8be | 4740 | btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start); |
e43bbe5e | 4741 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4742 | |
6d4572a9 | 4743 | if (only_release_metadata) |
217f42eb | 4744 | set_extent_bit(&inode->io_tree, block_start, block_end, |
1cab5e72 | 4745 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
6d4572a9 | 4746 | |
89642229 | 4747 | out_unlock: |
6d4572a9 QW |
4748 | if (ret) { |
4749 | if (only_release_metadata) | |
217f42eb | 4750 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
6d4572a9 | 4751 | else |
217f42eb | 4752 | btrfs_delalloc_release_space(inode, data_reserved, |
6d4572a9 QW |
4753 | block_start, blocksize, true); |
4754 | } | |
217f42eb | 4755 | btrfs_delalloc_release_extents(inode, blocksize); |
39279cc3 | 4756 | unlock_page(page); |
09cbfeaf | 4757 | put_page(page); |
39279cc3 | 4758 | out: |
6d4572a9 | 4759 | if (only_release_metadata) |
217f42eb | 4760 | btrfs_check_nocow_unlock(inode); |
364ecf36 | 4761 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4762 | return ret; |
4763 | } | |
4764 | ||
a4ba6cc0 | 4765 | static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode, |
16e7549f JB |
4766 | u64 offset, u64 len) |
4767 | { | |
a4ba6cc0 | 4768 | struct btrfs_fs_info *fs_info = root->fs_info; |
16e7549f | 4769 | struct btrfs_trans_handle *trans; |
5893dfb9 | 4770 | struct btrfs_drop_extents_args drop_args = { 0 }; |
16e7549f JB |
4771 | int ret; |
4772 | ||
4773 | /* | |
cceaa89f FM |
4774 | * If NO_HOLES is enabled, we don't need to do anything. |
4775 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() | |
4776 | * or btrfs_update_inode() will be called, which guarantee that the next | |
4777 | * fsync will know this inode was changed and needs to be logged. | |
16e7549f | 4778 | */ |
cceaa89f | 4779 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f | 4780 | return 0; |
16e7549f JB |
4781 | |
4782 | /* | |
4783 | * 1 - for the one we're dropping | |
4784 | * 1 - for the one we're adding | |
4785 | * 1 - for updating the inode. | |
4786 | */ | |
4787 | trans = btrfs_start_transaction(root, 3); | |
4788 | if (IS_ERR(trans)) | |
4789 | return PTR_ERR(trans); | |
4790 | ||
5893dfb9 FM |
4791 | drop_args.start = offset; |
4792 | drop_args.end = offset + len; | |
4793 | drop_args.drop_cache = true; | |
4794 | ||
a4ba6cc0 | 4795 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
16e7549f | 4796 | if (ret) { |
66642832 | 4797 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4798 | btrfs_end_transaction(trans); |
16e7549f JB |
4799 | return ret; |
4800 | } | |
4801 | ||
a4ba6cc0 | 4802 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
f85b7379 | 4803 | offset, 0, 0, len, 0, len, 0, 0, 0); |
2766ff61 | 4804 | if (ret) { |
66642832 | 4805 | btrfs_abort_transaction(trans, ret); |
2766ff61 | 4806 | } else { |
a4ba6cc0 NB |
4807 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
4808 | btrfs_update_inode(trans, root, inode); | |
2766ff61 | 4809 | } |
3a45bb20 | 4810 | btrfs_end_transaction(trans); |
16e7549f JB |
4811 | return ret; |
4812 | } | |
4813 | ||
695a0d0d JB |
4814 | /* |
4815 | * This function puts in dummy file extents for the area we're creating a hole | |
4816 | * for. So if we are truncating this file to a larger size we need to insert | |
4817 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4818 | * the range between oldsize and size | |
4819 | */ | |
b06359a3 | 4820 | int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4821 | { |
b06359a3 NB |
4822 | struct btrfs_root *root = inode->root; |
4823 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4824 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a22285a6 | 4825 | struct extent_map *em = NULL; |
2ac55d41 | 4826 | struct extent_state *cached_state = NULL; |
b06359a3 | 4827 | struct extent_map_tree *em_tree = &inode->extent_tree; |
0b246afa JM |
4828 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4829 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4830 | u64 last_byte; |
4831 | u64 cur_offset; | |
4832 | u64 hole_size; | |
9ed74f2d | 4833 | int err = 0; |
39279cc3 | 4834 | |
a71754fc | 4835 | /* |
9703fefe CR |
4836 | * If our size started in the middle of a block we need to zero out the |
4837 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4838 | * expose stale data. |
4839 | */ | |
b06359a3 | 4840 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4841 | if (err) |
4842 | return err; | |
4843 | ||
9036c102 YZ |
4844 | if (size <= hole_start) |
4845 | return 0; | |
4846 | ||
b06359a3 NB |
4847 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
4848 | &cached_state); | |
9036c102 YZ |
4849 | cur_offset = hole_start; |
4850 | while (1) { | |
b06359a3 | 4851 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
39b07b5d | 4852 | block_end - cur_offset); |
79787eaa JM |
4853 | if (IS_ERR(em)) { |
4854 | err = PTR_ERR(em); | |
f2767956 | 4855 | em = NULL; |
79787eaa JM |
4856 | break; |
4857 | } | |
9036c102 | 4858 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4859 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4860 | hole_size = last_byte - cur_offset; |
4861 | ||
8082510e | 4862 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4863 | struct extent_map *hole_em; |
9ed74f2d | 4864 | |
b06359a3 NB |
4865 | err = maybe_insert_hole(root, inode, cur_offset, |
4866 | hole_size); | |
16e7549f | 4867 | if (err) |
3893e33b | 4868 | break; |
9ddc959e | 4869 | |
b06359a3 | 4870 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4871 | cur_offset, hole_size); |
4872 | if (err) | |
4873 | break; | |
4874 | ||
b06359a3 | 4875 | btrfs_drop_extent_cache(inode, cur_offset, |
5dc562c5 JB |
4876 | cur_offset + hole_size - 1, 0); |
4877 | hole_em = alloc_extent_map(); | |
4878 | if (!hole_em) { | |
4879 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
b06359a3 | 4880 | &inode->runtime_flags); |
5dc562c5 JB |
4881 | goto next; |
4882 | } | |
4883 | hole_em->start = cur_offset; | |
4884 | hole_em->len = hole_size; | |
4885 | hole_em->orig_start = cur_offset; | |
8082510e | 4886 | |
5dc562c5 JB |
4887 | hole_em->block_start = EXTENT_MAP_HOLE; |
4888 | hole_em->block_len = 0; | |
b4939680 | 4889 | hole_em->orig_block_len = 0; |
cc95bef6 | 4890 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 4891 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4892 | hole_em->generation = fs_info->generation; |
8082510e | 4893 | |
5dc562c5 JB |
4894 | while (1) { |
4895 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4896 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4897 | write_unlock(&em_tree->lock); |
4898 | if (err != -EEXIST) | |
4899 | break; | |
b06359a3 | 4900 | btrfs_drop_extent_cache(inode, cur_offset, |
5dc562c5 JB |
4901 | cur_offset + |
4902 | hole_size - 1, 0); | |
4903 | } | |
4904 | free_extent_map(hole_em); | |
9ddc959e | 4905 | } else { |
b06359a3 | 4906 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4907 | cur_offset, hole_size); |
4908 | if (err) | |
4909 | break; | |
9036c102 | 4910 | } |
16e7549f | 4911 | next: |
9036c102 | 4912 | free_extent_map(em); |
a22285a6 | 4913 | em = NULL; |
9036c102 | 4914 | cur_offset = last_byte; |
8082510e | 4915 | if (cur_offset >= block_end) |
9036c102 YZ |
4916 | break; |
4917 | } | |
a22285a6 | 4918 | free_extent_map(em); |
e43bbe5e | 4919 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4920 | return err; |
4921 | } | |
39279cc3 | 4922 | |
3972f260 | 4923 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4924 | { |
f4a2f4c5 MX |
4925 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4926 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4927 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4928 | loff_t newsize = attr->ia_size; |
4929 | int mask = attr->ia_valid; | |
8082510e YZ |
4930 | int ret; |
4931 | ||
3972f260 ES |
4932 | /* |
4933 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4934 | * special case where we need to update the times despite not having | |
4935 | * these flags set. For all other operations the VFS set these flags | |
4936 | * explicitly if it wants a timestamp update. | |
4937 | */ | |
dff6efc3 CH |
4938 | if (newsize != oldsize) { |
4939 | inode_inc_iversion(inode); | |
4940 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4941 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 4942 | current_time(inode); |
dff6efc3 | 4943 | } |
3972f260 | 4944 | |
a41ad394 | 4945 | if (newsize > oldsize) { |
9ea24bbe | 4946 | /* |
ea14b57f | 4947 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
4948 | * This is to ensure the snapshot captures a fully consistent |
4949 | * state of this file - if the snapshot captures this expanding | |
4950 | * truncation, it must capture all writes that happened before | |
4951 | * this truncation. | |
4952 | */ | |
dcc3eb96 | 4953 | btrfs_drew_write_lock(&root->snapshot_lock); |
b06359a3 | 4954 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
9ea24bbe | 4955 | if (ret) { |
dcc3eb96 | 4956 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 4957 | return ret; |
9ea24bbe | 4958 | } |
8082510e | 4959 | |
f4a2f4c5 | 4960 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 4961 | if (IS_ERR(trans)) { |
dcc3eb96 | 4962 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 4963 | return PTR_ERR(trans); |
9ea24bbe | 4964 | } |
f4a2f4c5 MX |
4965 | |
4966 | i_size_write(inode, newsize); | |
76aea537 | 4967 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
27772b68 | 4968 | pagecache_isize_extended(inode, oldsize, newsize); |
9a56fcd1 | 4969 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
dcc3eb96 | 4970 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 4971 | btrfs_end_transaction(trans); |
a41ad394 | 4972 | } else { |
24c0a722 NA |
4973 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4974 | ||
4975 | if (btrfs_is_zoned(fs_info)) { | |
4976 | ret = btrfs_wait_ordered_range(inode, | |
4977 | ALIGN(newsize, fs_info->sectorsize), | |
4978 | (u64)-1); | |
4979 | if (ret) | |
4980 | return ret; | |
4981 | } | |
8082510e | 4982 | |
a41ad394 JB |
4983 | /* |
4984 | * We're truncating a file that used to have good data down to | |
1fd4033d NB |
4985 | * zero. Make sure any new writes to the file get on disk |
4986 | * on close. | |
a41ad394 JB |
4987 | */ |
4988 | if (newsize == 0) | |
1fd4033d | 4989 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
72ac3c0d | 4990 | &BTRFS_I(inode)->runtime_flags); |
8082510e | 4991 | |
a41ad394 | 4992 | truncate_setsize(inode, newsize); |
2e60a51e | 4993 | |
2e60a51e | 4994 | inode_dio_wait(inode); |
2e60a51e | 4995 | |
213e8c55 | 4996 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
4997 | if (ret && inode->i_nlink) { |
4998 | int err; | |
4999 | ||
5000 | /* | |
f7e9e8fc OS |
5001 | * Truncate failed, so fix up the in-memory size. We |
5002 | * adjusted disk_i_size down as we removed extents, so | |
5003 | * wait for disk_i_size to be stable and then update the | |
5004 | * in-memory size to match. | |
7f4f6e0a | 5005 | */ |
f7e9e8fc | 5006 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5007 | if (err) |
f7e9e8fc OS |
5008 | return err; |
5009 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5010 | } |
8082510e YZ |
5011 | } |
5012 | ||
a41ad394 | 5013 | return ret; |
8082510e YZ |
5014 | } |
5015 | ||
549c7297 CB |
5016 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
5017 | struct iattr *attr) | |
9036c102 | 5018 | { |
2b0143b5 | 5019 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5020 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5021 | int err; |
39279cc3 | 5022 | |
b83cc969 LZ |
5023 | if (btrfs_root_readonly(root)) |
5024 | return -EROFS; | |
5025 | ||
d4d09464 | 5026 | err = setattr_prepare(mnt_userns, dentry, attr); |
9036c102 YZ |
5027 | if (err) |
5028 | return err; | |
2bf5a725 | 5029 | |
5a3f23d5 | 5030 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5031 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5032 | if (err) |
5033 | return err; | |
39279cc3 | 5034 | } |
9036c102 | 5035 | |
1025774c | 5036 | if (attr->ia_valid) { |
d4d09464 | 5037 | setattr_copy(mnt_userns, inode, attr); |
0c4d2d95 | 5038 | inode_inc_iversion(inode); |
22c44fe6 | 5039 | err = btrfs_dirty_inode(inode); |
1025774c | 5040 | |
22c44fe6 | 5041 | if (!err && attr->ia_valid & ATTR_MODE) |
d4d09464 | 5042 | err = posix_acl_chmod(mnt_userns, inode, inode->i_mode); |
1025774c | 5043 | } |
33268eaf | 5044 | |
39279cc3 CM |
5045 | return err; |
5046 | } | |
61295eb8 | 5047 | |
131e404a FDBM |
5048 | /* |
5049 | * While truncating the inode pages during eviction, we get the VFS calling | |
5050 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5051 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5052 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5053 | * extent_state structures over and over, wasting lots of time. | |
5054 | * | |
5055 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5056 | * those expensive operations on a per page basis and do only the ordered io | |
5057 | * finishing, while we release here the extent_map and extent_state structures, | |
5058 | * without the excessive merging and splitting. | |
5059 | */ | |
5060 | static void evict_inode_truncate_pages(struct inode *inode) | |
5061 | { | |
5062 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5063 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5064 | struct rb_node *node; | |
5065 | ||
5066 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5067 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5068 | |
5069 | write_lock(&map_tree->lock); | |
07e1ce09 | 5070 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
5071 | struct extent_map *em; |
5072 | ||
07e1ce09 | 5073 | node = rb_first_cached(&map_tree->map); |
131e404a | 5074 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
5075 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5076 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5077 | remove_extent_mapping(map_tree, em); |
5078 | free_extent_map(em); | |
7064dd5c FM |
5079 | if (need_resched()) { |
5080 | write_unlock(&map_tree->lock); | |
5081 | cond_resched(); | |
5082 | write_lock(&map_tree->lock); | |
5083 | } | |
131e404a FDBM |
5084 | } |
5085 | write_unlock(&map_tree->lock); | |
5086 | ||
6ca07097 FM |
5087 | /* |
5088 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
5089 | * We can have ongoing bios started by readahead that have |
5090 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
5091 | * still in progress (unlocked the pages in the bio but did not yet |
5092 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5093 | * ranges can still be locked and eviction started because before |
5094 | * submitting those bios, which are executed by a separate task (work | |
5095 | * queue kthread), inode references (inode->i_count) were not taken | |
5096 | * (which would be dropped in the end io callback of each bio). | |
5097 | * Therefore here we effectively end up waiting for those bios and | |
5098 | * anyone else holding locked ranges without having bumped the inode's | |
5099 | * reference count - if we don't do it, when they access the inode's | |
5100 | * io_tree to unlock a range it may be too late, leading to an | |
5101 | * use-after-free issue. | |
5102 | */ | |
131e404a FDBM |
5103 | spin_lock(&io_tree->lock); |
5104 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5105 | struct extent_state *state; | |
5106 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5107 | u64 start; |
5108 | u64 end; | |
421f0922 | 5109 | unsigned state_flags; |
131e404a FDBM |
5110 | |
5111 | node = rb_first(&io_tree->state); | |
5112 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5113 | start = state->start; |
5114 | end = state->end; | |
421f0922 | 5115 | state_flags = state->state; |
131e404a FDBM |
5116 | spin_unlock(&io_tree->lock); |
5117 | ||
ff13db41 | 5118 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5119 | |
5120 | /* | |
5121 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5122 | * and its reserved space won't be freed by delayed_ref. | |
5123 | * So we need to free its reserved space here. | |
5124 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5125 | * | |
5126 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5127 | */ | |
421f0922 | 5128 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f NB |
5129 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
5130 | end - start + 1); | |
b9d0b389 | 5131 | |
6ca07097 | 5132 | clear_extent_bit(io_tree, start, end, |
e182163d OS |
5133 | EXTENT_LOCKED | EXTENT_DELALLOC | |
5134 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
5135 | &cached_state); | |
131e404a | 5136 | |
7064dd5c | 5137 | cond_resched(); |
131e404a FDBM |
5138 | spin_lock(&io_tree->lock); |
5139 | } | |
5140 | spin_unlock(&io_tree->lock); | |
5141 | } | |
5142 | ||
4b9d7b59 | 5143 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5144 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5145 | { |
5146 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d3984c90 | 5147 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5148 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5149 | int ret; |
4b9d7b59 | 5150 | |
d3984c90 JB |
5151 | /* |
5152 | * Eviction should be taking place at some place safe because of our | |
5153 | * delayed iputs. However the normal flushing code will run delayed | |
5154 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5155 | * | |
5156 | * We reserve the delayed_refs_extra here again because we can't use | |
5157 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5158 | * above. We reserve our extra bit here because we generate a ton of | |
5159 | * delayed refs activity by truncating. | |
5160 | * | |
ee6adbfd JB |
5161 | * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can, |
5162 | * if we fail to make this reservation we can re-try without the | |
5163 | * delayed_refs_extra so we can make some forward progress. | |
d3984c90 | 5164 | */ |
9270501c | 5165 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, |
d3984c90 JB |
5166 | BTRFS_RESERVE_FLUSH_EVICT); |
5167 | if (ret) { | |
9270501c | 5168 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, |
ee6adbfd JB |
5169 | BTRFS_RESERVE_FLUSH_EVICT); |
5170 | if (ret) { | |
d3984c90 JB |
5171 | btrfs_warn(fs_info, |
5172 | "could not allocate space for delete; will truncate on mount"); | |
5173 | return ERR_PTR(-ENOSPC); | |
5174 | } | |
5175 | delayed_refs_extra = 0; | |
5176 | } | |
4b9d7b59 | 5177 | |
d3984c90 JB |
5178 | trans = btrfs_join_transaction(root); |
5179 | if (IS_ERR(trans)) | |
5180 | return trans; | |
5181 | ||
5182 | if (delayed_refs_extra) { | |
5183 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5184 | trans->bytes_reserved = delayed_refs_extra; | |
5185 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5186 | delayed_refs_extra, 1); | |
4b9d7b59 | 5187 | } |
d3984c90 | 5188 | return trans; |
4b9d7b59 OS |
5189 | } |
5190 | ||
bd555975 | 5191 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5192 | { |
0b246afa | 5193 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5194 | struct btrfs_trans_handle *trans; |
5195 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5196 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5197 | int ret; |
5198 | ||
1abe9b8a | 5199 | trace_btrfs_inode_evict(inode); |
5200 | ||
3d48d981 | 5201 | if (!root) { |
14605409 | 5202 | fsverity_cleanup_inode(inode); |
e8f1bc14 | 5203 | clear_inode(inode); |
3d48d981 NB |
5204 | return; |
5205 | } | |
5206 | ||
131e404a FDBM |
5207 | evict_inode_truncate_pages(inode); |
5208 | ||
69e9c6c6 SB |
5209 | if (inode->i_nlink && |
5210 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5211 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5212 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5213 | goto no_delete; |
5214 | ||
27919067 | 5215 | if (is_bad_inode(inode)) |
39279cc3 | 5216 | goto no_delete; |
5f39d397 | 5217 | |
7ab7956e | 5218 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5219 | |
7b40b695 | 5220 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5221 | goto no_delete; |
c71bf099 | 5222 | |
76dda93c | 5223 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5224 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5225 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5226 | goto no_delete; |
5227 | } | |
5228 | ||
2adc75d6 JB |
5229 | /* |
5230 | * This makes sure the inode item in tree is uptodate and the space for | |
5231 | * the inode update is released. | |
5232 | */ | |
aa79021f | 5233 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5234 | if (ret) |
0e8c36a9 | 5235 | goto no_delete; |
0e8c36a9 | 5236 | |
2adc75d6 JB |
5237 | /* |
5238 | * This drops any pending insert or delete operations we have for this | |
5239 | * inode. We could have a delayed dir index deletion queued up, but | |
5240 | * we're removing the inode completely so that'll be taken care of in | |
5241 | * the truncate. | |
5242 | */ | |
5243 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); | |
5244 | ||
2ff7e61e | 5245 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5246 | if (!rsv) |
4289a667 | 5247 | goto no_delete; |
2bd36e7b | 5248 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
ca7e70f5 | 5249 | rsv->failfast = 1; |
4289a667 | 5250 | |
6ef06d27 | 5251 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5252 | |
8082510e | 5253 | while (1) { |
d9ac19c3 | 5254 | struct btrfs_truncate_control control = { |
487e81d2 | 5255 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 JB |
5256 | .new_size = 0, |
5257 | .min_type = 0, | |
5258 | }; | |
5259 | ||
ad80cf50 | 5260 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5261 | if (IS_ERR(trans)) |
5262 | goto free_rsv; | |
7b128766 | 5263 | |
4289a667 JB |
5264 | trans->block_rsv = rsv; |
5265 | ||
50743398 | 5266 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
d9ac19c3 | 5267 | &control); |
27919067 OS |
5268 | trans->block_rsv = &fs_info->trans_block_rsv; |
5269 | btrfs_end_transaction(trans); | |
5270 | btrfs_btree_balance_dirty(fs_info); | |
5271 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5272 | goto free_rsv; | |
5273 | else if (!ret) | |
8082510e | 5274 | break; |
8082510e | 5275 | } |
5f39d397 | 5276 | |
4ef31a45 | 5277 | /* |
27919067 OS |
5278 | * Errors here aren't a big deal, it just means we leave orphan items in |
5279 | * the tree. They will be cleaned up on the next mount. If the inode | |
5280 | * number gets reused, cleanup deletes the orphan item without doing | |
5281 | * anything, and unlink reuses the existing orphan item. | |
5282 | * | |
5283 | * If it turns out that we are dropping too many of these, we might want | |
5284 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5285 | */ |
ad80cf50 | 5286 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5287 | if (!IS_ERR(trans)) { |
5288 | trans->block_rsv = rsv; | |
5289 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5290 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5291 | btrfs_end_transaction(trans); | |
5292 | } | |
54aa1f4d | 5293 | |
27919067 OS |
5294 | free_rsv: |
5295 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5296 | no_delete: |
27919067 OS |
5297 | /* |
5298 | * If we didn't successfully delete, the orphan item will still be in | |
5299 | * the tree and we'll retry on the next mount. Again, we might also want | |
5300 | * to retry these periodically in the future. | |
5301 | */ | |
f48d1cf5 | 5302 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
14605409 | 5303 | fsverity_cleanup_inode(inode); |
dbd5768f | 5304 | clear_inode(inode); |
39279cc3 CM |
5305 | } |
5306 | ||
5307 | /* | |
6bf9e4bd QW |
5308 | * Return the key found in the dir entry in the location pointer, fill @type |
5309 | * with BTRFS_FT_*, and return 0. | |
5310 | * | |
005d6712 SY |
5311 | * If no dir entries were found, returns -ENOENT. |
5312 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5313 | */ |
5314 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5315 | struct btrfs_key *location, u8 *type) |
39279cc3 CM |
5316 | { |
5317 | const char *name = dentry->d_name.name; | |
5318 | int namelen = dentry->d_name.len; | |
5319 | struct btrfs_dir_item *di; | |
5320 | struct btrfs_path *path; | |
5321 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5322 | int ret = 0; |
39279cc3 CM |
5323 | |
5324 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5325 | if (!path) |
5326 | return -ENOMEM; | |
3954401f | 5327 | |
f85b7379 DS |
5328 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5329 | name, namelen, 0); | |
3cf5068f LB |
5330 | if (IS_ERR_OR_NULL(di)) { |
5331 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5332 | goto out; |
5333 | } | |
d397712b | 5334 | |
5f39d397 | 5335 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5336 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5337 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5338 | ret = -EUCLEAN; |
56a0e706 LB |
5339 | btrfs_warn(root->fs_info, |
5340 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5341 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5342 | location->objectid, location->type, location->offset); | |
56a0e706 | 5343 | } |
6bf9e4bd QW |
5344 | if (!ret) |
5345 | *type = btrfs_dir_type(path->nodes[0], di); | |
39279cc3 | 5346 | out: |
39279cc3 CM |
5347 | btrfs_free_path(path); |
5348 | return ret; | |
5349 | } | |
5350 | ||
5351 | /* | |
5352 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5353 | * needs to be changed to reflect the root directory of the tree root. This | |
5354 | * is kind of like crossing a mount point. | |
5355 | */ | |
2ff7e61e | 5356 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5357 | struct inode *dir, |
5358 | struct dentry *dentry, | |
5359 | struct btrfs_key *location, | |
5360 | struct btrfs_root **sub_root) | |
39279cc3 | 5361 | { |
4df27c4d YZ |
5362 | struct btrfs_path *path; |
5363 | struct btrfs_root *new_root; | |
5364 | struct btrfs_root_ref *ref; | |
5365 | struct extent_buffer *leaf; | |
1d4c08e0 | 5366 | struct btrfs_key key; |
4df27c4d YZ |
5367 | int ret; |
5368 | int err = 0; | |
39279cc3 | 5369 | |
4df27c4d YZ |
5370 | path = btrfs_alloc_path(); |
5371 | if (!path) { | |
5372 | err = -ENOMEM; | |
5373 | goto out; | |
5374 | } | |
39279cc3 | 5375 | |
4df27c4d | 5376 | err = -ENOENT; |
1d4c08e0 DS |
5377 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5378 | key.type = BTRFS_ROOT_REF_KEY; | |
5379 | key.offset = location->objectid; | |
5380 | ||
0b246afa | 5381 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5382 | if (ret) { |
5383 | if (ret < 0) | |
5384 | err = ret; | |
5385 | goto out; | |
5386 | } | |
39279cc3 | 5387 | |
4df27c4d YZ |
5388 | leaf = path->nodes[0]; |
5389 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5390 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5391 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5392 | goto out; | |
39279cc3 | 5393 | |
4df27c4d YZ |
5394 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5395 | (unsigned long)(ref + 1), | |
5396 | dentry->d_name.len); | |
5397 | if (ret) | |
5398 | goto out; | |
5399 | ||
b3b4aa74 | 5400 | btrfs_release_path(path); |
4df27c4d | 5401 | |
56e9357a | 5402 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5403 | if (IS_ERR(new_root)) { |
5404 | err = PTR_ERR(new_root); | |
5405 | goto out; | |
5406 | } | |
5407 | ||
4df27c4d YZ |
5408 | *sub_root = new_root; |
5409 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5410 | location->type = BTRFS_INODE_ITEM_KEY; | |
5411 | location->offset = 0; | |
5412 | err = 0; | |
5413 | out: | |
5414 | btrfs_free_path(path); | |
5415 | return err; | |
39279cc3 CM |
5416 | } |
5417 | ||
5d4f98a2 YZ |
5418 | static void inode_tree_add(struct inode *inode) |
5419 | { | |
5420 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5421 | struct btrfs_inode *entry; | |
03e860bd NP |
5422 | struct rb_node **p; |
5423 | struct rb_node *parent; | |
cef21937 | 5424 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5425 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5426 | |
1d3382cb | 5427 | if (inode_unhashed(inode)) |
76dda93c | 5428 | return; |
e1409cef | 5429 | parent = NULL; |
5d4f98a2 | 5430 | spin_lock(&root->inode_lock); |
e1409cef | 5431 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5432 | while (*p) { |
5433 | parent = *p; | |
5434 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5435 | ||
37508515 | 5436 | if (ino < btrfs_ino(entry)) |
03e860bd | 5437 | p = &parent->rb_left; |
37508515 | 5438 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5439 | p = &parent->rb_right; |
5d4f98a2 YZ |
5440 | else { |
5441 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5442 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5443 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5444 | RB_CLEAR_NODE(parent); |
5445 | spin_unlock(&root->inode_lock); | |
cef21937 | 5446 | return; |
5d4f98a2 YZ |
5447 | } |
5448 | } | |
cef21937 FDBM |
5449 | rb_link_node(new, parent, p); |
5450 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5451 | spin_unlock(&root->inode_lock); |
5452 | } | |
5453 | ||
b79b7249 | 5454 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5455 | { |
b79b7249 | 5456 | struct btrfs_root *root = inode->root; |
76dda93c | 5457 | int empty = 0; |
5d4f98a2 | 5458 | |
03e860bd | 5459 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5460 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5461 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5462 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5463 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5464 | } |
03e860bd | 5465 | spin_unlock(&root->inode_lock); |
76dda93c | 5466 | |
69e9c6c6 | 5467 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5468 | spin_lock(&root->inode_lock); |
5469 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5470 | spin_unlock(&root->inode_lock); | |
5471 | if (empty) | |
5472 | btrfs_add_dead_root(root); | |
5473 | } | |
5474 | } | |
5475 | ||
5d4f98a2 | 5476 | |
e02119d5 CM |
5477 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5478 | { | |
5479 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5480 | |
5481 | inode->i_ino = args->ino; | |
5482 | BTRFS_I(inode)->location.objectid = args->ino; | |
5483 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5484 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5485 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5486 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
39279cc3 CM |
5487 | return 0; |
5488 | } | |
5489 | ||
5490 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5491 | { | |
5492 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5493 | |
5494 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5495 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5496 | } |
5497 | ||
0202e83f | 5498 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5499 | struct btrfs_root *root) |
39279cc3 CM |
5500 | { |
5501 | struct inode *inode; | |
5502 | struct btrfs_iget_args args; | |
0202e83f | 5503 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5504 | |
0202e83f | 5505 | args.ino = ino; |
39279cc3 CM |
5506 | args.root = root; |
5507 | ||
778ba82b | 5508 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5509 | btrfs_init_locked_inode, |
5510 | (void *)&args); | |
5511 | return inode; | |
5512 | } | |
5513 | ||
4c66e0d4 | 5514 | /* |
0202e83f | 5515 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5516 | * Path can be preallocated to prevent recursing back to iget through |
5517 | * allocator. NULL is also valid but may require an additional allocation | |
5518 | * later. | |
1a54ef8c | 5519 | */ |
0202e83f | 5520 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5521 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5522 | { |
5523 | struct inode *inode; | |
5524 | ||
0202e83f | 5525 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5526 | if (!inode) |
5d4f98a2 | 5527 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5528 | |
5529 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5530 | int ret; |
5531 | ||
4222ea71 | 5532 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5533 | if (!ret) { |
1748f843 MF |
5534 | inode_tree_add(inode); |
5535 | unlock_new_inode(inode); | |
1748f843 | 5536 | } else { |
f5b3a417 AV |
5537 | iget_failed(inode); |
5538 | /* | |
5539 | * ret > 0 can come from btrfs_search_slot called by | |
5540 | * btrfs_read_locked_inode, this means the inode item | |
5541 | * was not found. | |
5542 | */ | |
5543 | if (ret > 0) | |
5544 | ret = -ENOENT; | |
5545 | inode = ERR_PTR(ret); | |
1748f843 MF |
5546 | } |
5547 | } | |
5548 | ||
1a54ef8c BR |
5549 | return inode; |
5550 | } | |
5551 | ||
0202e83f | 5552 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5553 | { |
0202e83f | 5554 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5555 | } |
5556 | ||
4df27c4d YZ |
5557 | static struct inode *new_simple_dir(struct super_block *s, |
5558 | struct btrfs_key *key, | |
5559 | struct btrfs_root *root) | |
5560 | { | |
5561 | struct inode *inode = new_inode(s); | |
5562 | ||
5563 | if (!inode) | |
5564 | return ERR_PTR(-ENOMEM); | |
5565 | ||
5c8fd99f | 5566 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5567 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5568 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5569 | |
5570 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5571 | /* |
5572 | * We only need lookup, the rest is read-only and there's no inode | |
5573 | * associated with the dentry | |
5574 | */ | |
5575 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5576 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5577 | inode->i_fop = &simple_dir_operations; |
5578 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5579 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5580 | inode->i_atime = inode->i_mtime; |
5581 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5582 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5583 | |
5584 | return inode; | |
5585 | } | |
5586 | ||
6bf9e4bd QW |
5587 | static inline u8 btrfs_inode_type(struct inode *inode) |
5588 | { | |
5589 | /* | |
5590 | * Compile-time asserts that generic FT_* types still match | |
5591 | * BTRFS_FT_* types | |
5592 | */ | |
5593 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); | |
5594 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); | |
5595 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); | |
5596 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); | |
5597 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); | |
5598 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); | |
5599 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); | |
5600 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); | |
5601 | ||
5602 | return fs_umode_to_ftype(inode->i_mode); | |
5603 | } | |
5604 | ||
3de4586c | 5605 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5606 | { |
0b246afa | 5607 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5608 | struct inode *inode; |
4df27c4d | 5609 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5610 | struct btrfs_root *sub_root = root; |
5611 | struct btrfs_key location; | |
6bf9e4bd | 5612 | u8 di_type = 0; |
b4aff1f8 | 5613 | int ret = 0; |
39279cc3 CM |
5614 | |
5615 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5616 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5617 | |
6bf9e4bd | 5618 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5619 | if (ret < 0) |
5620 | return ERR_PTR(ret); | |
5f39d397 | 5621 | |
4df27c4d | 5622 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5623 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5624 | if (IS_ERR(inode)) |
5625 | return inode; | |
5626 | ||
5627 | /* Do extra check against inode mode with di_type */ | |
5628 | if (btrfs_inode_type(inode) != di_type) { | |
5629 | btrfs_crit(fs_info, | |
5630 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5631 | inode->i_mode, btrfs_inode_type(inode), | |
5632 | di_type); | |
5633 | iput(inode); | |
5634 | return ERR_PTR(-EUCLEAN); | |
5635 | } | |
4df27c4d YZ |
5636 | return inode; |
5637 | } | |
5638 | ||
2ff7e61e | 5639 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5640 | &location, &sub_root); |
5641 | if (ret < 0) { | |
5642 | if (ret != -ENOENT) | |
5643 | inode = ERR_PTR(ret); | |
5644 | else | |
5645 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5646 | } else { | |
0202e83f | 5647 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
39279cc3 | 5648 | } |
8727002f | 5649 | if (root != sub_root) |
00246528 | 5650 | btrfs_put_root(sub_root); |
76dda93c | 5651 | |
34d19bad | 5652 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5653 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5654 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5655 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5656 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5657 | if (ret) { |
5658 | iput(inode); | |
66b4ffd1 | 5659 | inode = ERR_PTR(ret); |
01cd3367 | 5660 | } |
c71bf099 YZ |
5661 | } |
5662 | ||
3de4586c CM |
5663 | return inode; |
5664 | } | |
5665 | ||
fe15ce44 | 5666 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5667 | { |
5668 | struct btrfs_root *root; | |
2b0143b5 | 5669 | struct inode *inode = d_inode(dentry); |
76dda93c | 5670 | |
848cce0d | 5671 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5672 | inode = d_inode(dentry->d_parent); |
76dda93c | 5673 | |
848cce0d LZ |
5674 | if (inode) { |
5675 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5676 | if (btrfs_root_refs(&root->root_item) == 0) |
5677 | return 1; | |
848cce0d | 5678 | |
4a0cc7ca | 5679 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5680 | return 1; |
efefb143 | 5681 | } |
76dda93c YZ |
5682 | return 0; |
5683 | } | |
5684 | ||
3de4586c | 5685 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5686 | unsigned int flags) |
3de4586c | 5687 | { |
3837d208 | 5688 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5689 | |
3837d208 AV |
5690 | if (inode == ERR_PTR(-ENOENT)) |
5691 | inode = NULL; | |
41d28bca | 5692 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5693 | } |
5694 | ||
23b5ec74 JB |
5695 | /* |
5696 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5697 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5698 | * our information into that, and then dir_emit from the buffer. This is | |
5699 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5700 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5701 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5702 | * tree lock. | |
5703 | */ | |
5704 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5705 | { | |
5706 | struct btrfs_file_private *private; | |
5707 | ||
5708 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5709 | if (!private) | |
5710 | return -ENOMEM; | |
5711 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5712 | if (!private->filldir_buf) { | |
5713 | kfree(private); | |
5714 | return -ENOMEM; | |
5715 | } | |
5716 | file->private_data = private; | |
5717 | return 0; | |
5718 | } | |
5719 | ||
5720 | struct dir_entry { | |
5721 | u64 ino; | |
5722 | u64 offset; | |
5723 | unsigned type; | |
5724 | int name_len; | |
5725 | }; | |
5726 | ||
5727 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5728 | { | |
5729 | while (entries--) { | |
5730 | struct dir_entry *entry = addr; | |
5731 | char *name = (char *)(entry + 1); | |
5732 | ||
92d32170 DS |
5733 | ctx->pos = get_unaligned(&entry->offset); |
5734 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5735 | get_unaligned(&entry->ino), | |
5736 | get_unaligned(&entry->type))) | |
23b5ec74 | 5737 | return 1; |
92d32170 DS |
5738 | addr += sizeof(struct dir_entry) + |
5739 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5740 | ctx->pos++; |
5741 | } | |
5742 | return 0; | |
5743 | } | |
5744 | ||
9cdda8d3 | 5745 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5746 | { |
9cdda8d3 | 5747 | struct inode *inode = file_inode(file); |
39279cc3 | 5748 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5749 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5750 | struct btrfs_dir_item *di; |
5751 | struct btrfs_key key; | |
5f39d397 | 5752 | struct btrfs_key found_key; |
39279cc3 | 5753 | struct btrfs_path *path; |
23b5ec74 | 5754 | void *addr; |
16cdcec7 MX |
5755 | struct list_head ins_list; |
5756 | struct list_head del_list; | |
39279cc3 | 5757 | int ret; |
5f39d397 | 5758 | struct extent_buffer *leaf; |
39279cc3 | 5759 | int slot; |
5f39d397 CM |
5760 | char *name_ptr; |
5761 | int name_len; | |
23b5ec74 JB |
5762 | int entries = 0; |
5763 | int total_len = 0; | |
02dbfc99 | 5764 | bool put = false; |
c2951f32 | 5765 | struct btrfs_key location; |
5f39d397 | 5766 | |
9cdda8d3 AV |
5767 | if (!dir_emit_dots(file, ctx)) |
5768 | return 0; | |
5769 | ||
49593bfa | 5770 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5771 | if (!path) |
5772 | return -ENOMEM; | |
ff5714cc | 5773 | |
23b5ec74 | 5774 | addr = private->filldir_buf; |
e4058b54 | 5775 | path->reada = READA_FORWARD; |
49593bfa | 5776 | |
c2951f32 JM |
5777 | INIT_LIST_HEAD(&ins_list); |
5778 | INIT_LIST_HEAD(&del_list); | |
5779 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5780 | |
23b5ec74 | 5781 | again: |
c2951f32 | 5782 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5783 | key.offset = ctx->pos; |
4a0cc7ca | 5784 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5785 | |
39279cc3 CM |
5786 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5787 | if (ret < 0) | |
5788 | goto err; | |
49593bfa DW |
5789 | |
5790 | while (1) { | |
23b5ec74 JB |
5791 | struct dir_entry *entry; |
5792 | ||
5f39d397 | 5793 | leaf = path->nodes[0]; |
39279cc3 | 5794 | slot = path->slots[0]; |
b9e03af0 LZ |
5795 | if (slot >= btrfs_header_nritems(leaf)) { |
5796 | ret = btrfs_next_leaf(root, path); | |
5797 | if (ret < 0) | |
5798 | goto err; | |
5799 | else if (ret > 0) | |
5800 | break; | |
5801 | continue; | |
39279cc3 | 5802 | } |
3de4586c | 5803 | |
5f39d397 CM |
5804 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5805 | ||
5806 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5807 | break; |
c2951f32 | 5808 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5809 | break; |
9cdda8d3 | 5810 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5811 | goto next; |
c2951f32 | 5812 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5813 | goto next; |
39279cc3 | 5814 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 5815 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5816 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5817 | PAGE_SIZE) { | |
5818 | btrfs_release_path(path); | |
5819 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5820 | if (ret) | |
5821 | goto nopos; | |
5822 | addr = private->filldir_buf; | |
5823 | entries = 0; | |
5824 | total_len = 0; | |
5825 | goto again; | |
c2951f32 | 5826 | } |
23b5ec74 JB |
5827 | |
5828 | entry = addr; | |
92d32170 | 5829 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 5830 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
5831 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5832 | name_len); | |
7d157c3d | 5833 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
92d32170 | 5834 | &entry->type); |
c2951f32 | 5835 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5836 | put_unaligned(location.objectid, &entry->ino); |
5837 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5838 | entries++; |
5839 | addr += sizeof(struct dir_entry) + name_len; | |
5840 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5841 | next: |
5842 | path->slots[0]++; | |
39279cc3 | 5843 | } |
23b5ec74 JB |
5844 | btrfs_release_path(path); |
5845 | ||
5846 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5847 | if (ret) | |
5848 | goto nopos; | |
49593bfa | 5849 | |
d2fbb2b5 | 5850 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5851 | if (ret) |
bc4ef759 DS |
5852 | goto nopos; |
5853 | ||
db62efbb ZB |
5854 | /* |
5855 | * Stop new entries from being returned after we return the last | |
5856 | * entry. | |
5857 | * | |
5858 | * New directory entries are assigned a strictly increasing | |
5859 | * offset. This means that new entries created during readdir | |
5860 | * are *guaranteed* to be seen in the future by that readdir. | |
5861 | * This has broken buggy programs which operate on names as | |
5862 | * they're returned by readdir. Until we re-use freed offsets | |
5863 | * we have this hack to stop new entries from being returned | |
5864 | * under the assumption that they'll never reach this huge | |
5865 | * offset. | |
5866 | * | |
5867 | * This is being careful not to overflow 32bit loff_t unless the | |
5868 | * last entry requires it because doing so has broken 32bit apps | |
5869 | * in the past. | |
5870 | */ | |
c2951f32 JM |
5871 | if (ctx->pos >= INT_MAX) |
5872 | ctx->pos = LLONG_MAX; | |
5873 | else | |
5874 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5875 | nopos: |
5876 | ret = 0; | |
5877 | err: | |
02dbfc99 OS |
5878 | if (put) |
5879 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5880 | btrfs_free_path(path); |
39279cc3 CM |
5881 | return ret; |
5882 | } | |
5883 | ||
39279cc3 | 5884 | /* |
54aa1f4d | 5885 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5886 | * inode changes. But, it is most likely to find the inode in cache. |
5887 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5888 | * to keep or drop this code. | |
5889 | */ | |
48a3b636 | 5890 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 5891 | { |
2ff7e61e | 5892 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5893 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5894 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5895 | int ret; |
5896 | ||
72ac3c0d | 5897 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5898 | return 0; |
39279cc3 | 5899 | |
7a7eaa40 | 5900 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5901 | if (IS_ERR(trans)) |
5902 | return PTR_ERR(trans); | |
8929ecfa | 5903 | |
9a56fcd1 | 5904 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
4d14c5cd | 5905 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
94b60442 | 5906 | /* whoops, lets try again with the full transaction */ |
3a45bb20 | 5907 | btrfs_end_transaction(trans); |
94b60442 | 5908 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
5909 | if (IS_ERR(trans)) |
5910 | return PTR_ERR(trans); | |
8929ecfa | 5911 | |
9a56fcd1 | 5912 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
94b60442 | 5913 | } |
3a45bb20 | 5914 | btrfs_end_transaction(trans); |
16cdcec7 | 5915 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 5916 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
5917 | |
5918 | return ret; | |
5919 | } | |
5920 | ||
5921 | /* | |
5922 | * This is a copy of file_update_time. We need this so we can return error on | |
5923 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5924 | */ | |
95582b00 | 5925 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 5926 | int flags) |
22c44fe6 | 5927 | { |
2bc55652 | 5928 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 5929 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
5930 | |
5931 | if (btrfs_root_readonly(root)) | |
5932 | return -EROFS; | |
5933 | ||
e41f941a | 5934 | if (flags & S_VERSION) |
3a8c7231 | 5935 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
5936 | if (flags & S_CTIME) |
5937 | inode->i_ctime = *now; | |
5938 | if (flags & S_MTIME) | |
5939 | inode->i_mtime = *now; | |
5940 | if (flags & S_ATIME) | |
5941 | inode->i_atime = *now; | |
3a8c7231 | 5942 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
5943 | } |
5944 | ||
d352ac68 CM |
5945 | /* |
5946 | * find the highest existing sequence number in a directory | |
5947 | * and then set the in-memory index_cnt variable to reflect | |
5948 | * free sequence numbers | |
5949 | */ | |
4c570655 | 5950 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 5951 | { |
4c570655 | 5952 | struct btrfs_root *root = inode->root; |
aec7477b JB |
5953 | struct btrfs_key key, found_key; |
5954 | struct btrfs_path *path; | |
5955 | struct extent_buffer *leaf; | |
5956 | int ret; | |
5957 | ||
4c570655 | 5958 | key.objectid = btrfs_ino(inode); |
962a298f | 5959 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5960 | key.offset = (u64)-1; |
5961 | ||
5962 | path = btrfs_alloc_path(); | |
5963 | if (!path) | |
5964 | return -ENOMEM; | |
5965 | ||
5966 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5967 | if (ret < 0) | |
5968 | goto out; | |
5969 | /* FIXME: we should be able to handle this */ | |
5970 | if (ret == 0) | |
5971 | goto out; | |
5972 | ret = 0; | |
5973 | ||
5974 | /* | |
5975 | * MAGIC NUMBER EXPLANATION: | |
5976 | * since we search a directory based on f_pos we have to start at 2 | |
5977 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5978 | * else has to start at 2 | |
5979 | */ | |
5980 | if (path->slots[0] == 0) { | |
4c570655 | 5981 | inode->index_cnt = 2; |
aec7477b JB |
5982 | goto out; |
5983 | } | |
5984 | ||
5985 | path->slots[0]--; | |
5986 | ||
5987 | leaf = path->nodes[0]; | |
5988 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5989 | ||
4c570655 | 5990 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5991 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 5992 | inode->index_cnt = 2; |
aec7477b JB |
5993 | goto out; |
5994 | } | |
5995 | ||
4c570655 | 5996 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
5997 | out: |
5998 | btrfs_free_path(path); | |
5999 | return ret; | |
6000 | } | |
6001 | ||
d352ac68 CM |
6002 | /* |
6003 | * helper to find a free sequence number in a given directory. This current | |
6004 | * code is very simple, later versions will do smarter things in the btree | |
6005 | */ | |
877574e2 | 6006 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6007 | { |
6008 | int ret = 0; | |
6009 | ||
877574e2 NB |
6010 | if (dir->index_cnt == (u64)-1) { |
6011 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6012 | if (ret) { |
6013 | ret = btrfs_set_inode_index_count(dir); | |
6014 | if (ret) | |
6015 | return ret; | |
6016 | } | |
aec7477b JB |
6017 | } |
6018 | ||
877574e2 NB |
6019 | *index = dir->index_cnt; |
6020 | dir->index_cnt++; | |
aec7477b JB |
6021 | |
6022 | return ret; | |
6023 | } | |
6024 | ||
b0d5d10f CM |
6025 | static int btrfs_insert_inode_locked(struct inode *inode) |
6026 | { | |
6027 | struct btrfs_iget_args args; | |
0202e83f DS |
6028 | |
6029 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
6030 | args.root = BTRFS_I(inode)->root; |
6031 | ||
6032 | return insert_inode_locked4(inode, | |
6033 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6034 | btrfs_find_actor, &args); | |
6035 | } | |
6036 | ||
19aee8de AJ |
6037 | /* |
6038 | * Inherit flags from the parent inode. | |
6039 | * | |
6040 | * Currently only the compression flags and the cow flags are inherited. | |
6041 | */ | |
6042 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6043 | { | |
6044 | unsigned int flags; | |
6045 | ||
6046 | if (!dir) | |
6047 | return; | |
6048 | ||
6049 | flags = BTRFS_I(dir)->flags; | |
6050 | ||
6051 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6052 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6053 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6054 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6055 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6056 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6057 | } | |
6058 | ||
6059 | if (flags & BTRFS_INODE_NODATACOW) { | |
6060 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6061 | if (S_ISREG(inode->i_mode)) | |
6062 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6063 | } | |
6064 | ||
7b6a221e | 6065 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
6066 | } |
6067 | ||
39279cc3 CM |
6068 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6069 | struct btrfs_root *root, | |
b3b6f5b9 | 6070 | struct user_namespace *mnt_userns, |
aec7477b | 6071 | struct inode *dir, |
9c58309d | 6072 | const char *name, int name_len, |
175a4eb7 AV |
6073 | u64 ref_objectid, u64 objectid, |
6074 | umode_t mode, u64 *index) | |
39279cc3 | 6075 | { |
0b246afa | 6076 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6077 | struct inode *inode; |
5f39d397 | 6078 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6079 | struct btrfs_key *location; |
5f39d397 | 6080 | struct btrfs_path *path; |
9c58309d CM |
6081 | struct btrfs_inode_ref *ref; |
6082 | struct btrfs_key key[2]; | |
6083 | u32 sizes[2]; | |
b7ef5f3a | 6084 | struct btrfs_item_batch batch; |
9c58309d | 6085 | unsigned long ptr; |
11a19a90 | 6086 | unsigned int nofs_flag; |
39279cc3 | 6087 | int ret; |
39279cc3 | 6088 | |
5f39d397 | 6089 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6090 | if (!path) |
6091 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6092 | |
11a19a90 | 6093 | nofs_flag = memalloc_nofs_save(); |
0b246afa | 6094 | inode = new_inode(fs_info->sb); |
11a19a90 | 6095 | memalloc_nofs_restore(nofs_flag); |
8fb27640 YS |
6096 | if (!inode) { |
6097 | btrfs_free_path(path); | |
39279cc3 | 6098 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6099 | } |
39279cc3 | 6100 | |
5762b5c9 FM |
6101 | /* |
6102 | * O_TMPFILE, set link count to 0, so that after this point, | |
6103 | * we fill in an inode item with the correct link count. | |
6104 | */ | |
6105 | if (!name) | |
6106 | set_nlink(inode, 0); | |
6107 | ||
581bb050 LZ |
6108 | /* |
6109 | * we have to initialize this early, so we can reclaim the inode | |
6110 | * number if we fail afterwards in this function. | |
6111 | */ | |
6112 | inode->i_ino = objectid; | |
6113 | ||
ef3b9af5 | 6114 | if (dir && name) { |
1abe9b8a | 6115 | trace_btrfs_inode_request(dir); |
6116 | ||
877574e2 | 6117 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6118 | if (ret) { |
8fb27640 | 6119 | btrfs_free_path(path); |
09771430 | 6120 | iput(inode); |
aec7477b | 6121 | return ERR_PTR(ret); |
09771430 | 6122 | } |
ef3b9af5 FM |
6123 | } else if (dir) { |
6124 | *index = 0; | |
aec7477b JB |
6125 | } |
6126 | /* | |
6127 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 6128 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
6129 | * number |
6130 | */ | |
6131 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6132 | BTRFS_I(inode)->dir_index = *index; |
5c8fd99f | 6133 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
e02119d5 | 6134 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6135 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6136 | |
5dc562c5 JB |
6137 | /* |
6138 | * We could have gotten an inode number from somebody who was fsynced | |
6139 | * and then removed in this same transaction, so let's just set full | |
6140 | * sync since it will be a full sync anyway and this will blow away the | |
6141 | * old info in the log. | |
6142 | */ | |
6143 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6144 | ||
9c58309d | 6145 | key[0].objectid = objectid; |
962a298f | 6146 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6147 | key[0].offset = 0; |
6148 | ||
9c58309d | 6149 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6150 | |
6151 | if (name) { | |
6152 | /* | |
6153 | * Start new inodes with an inode_ref. This is slightly more | |
6154 | * efficient for small numbers of hard links since they will | |
6155 | * be packed into one item. Extended refs will kick in if we | |
6156 | * add more hard links than can fit in the ref item. | |
6157 | */ | |
6158 | key[1].objectid = objectid; | |
962a298f | 6159 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6160 | key[1].offset = ref_objectid; |
6161 | ||
6162 | sizes[1] = name_len + sizeof(*ref); | |
6163 | } | |
9c58309d | 6164 | |
b0d5d10f CM |
6165 | location = &BTRFS_I(inode)->location; |
6166 | location->objectid = objectid; | |
6167 | location->offset = 0; | |
962a298f | 6168 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6169 | |
6170 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6171 | if (ret < 0) { |
6172 | iput(inode); | |
b0d5d10f | 6173 | goto fail; |
32955c54 | 6174 | } |
b0d5d10f | 6175 | |
b7ef5f3a FM |
6176 | batch.keys = &key[0]; |
6177 | batch.data_sizes = &sizes[0]; | |
6178 | batch.total_data_size = sizes[0] + (name ? sizes[1] : 0); | |
6179 | batch.nr = name ? 2 : 1; | |
6180 | ret = btrfs_insert_empty_items(trans, root, path, &batch); | |
9c58309d | 6181 | if (ret != 0) |
b0d5d10f | 6182 | goto fail_unlock; |
5f39d397 | 6183 | |
b3b6f5b9 | 6184 | inode_init_owner(mnt_userns, inode, dir, mode); |
a76a3cd4 | 6185 | inode_set_bytes(inode, 0); |
9cc97d64 | 6186 | |
c2050a45 | 6187 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6188 | inode->i_atime = inode->i_mtime; |
6189 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6190 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6191 | |
5f39d397 CM |
6192 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6193 | struct btrfs_inode_item); | |
b159fa28 | 6194 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6195 | sizeof(*inode_item)); |
e02119d5 | 6196 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6197 | |
ef3b9af5 FM |
6198 | if (name) { |
6199 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6200 | struct btrfs_inode_ref); | |
6201 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6202 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6203 | ptr = (unsigned long)(ref + 1); | |
6204 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6205 | } | |
9c58309d | 6206 | |
5f39d397 CM |
6207 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6208 | btrfs_free_path(path); | |
6209 | ||
6cbff00f CH |
6210 | btrfs_inherit_iflags(inode, dir); |
6211 | ||
569254b0 | 6212 | if (S_ISREG(mode)) { |
0b246afa | 6213 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6214 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6215 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6216 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6217 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6218 | } |
6219 | ||
5d4f98a2 | 6220 | inode_tree_add(inode); |
1abe9b8a | 6221 | |
6222 | trace_btrfs_inode_new(inode); | |
d9094414 | 6223 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6224 | |
8ea05e3a AB |
6225 | btrfs_update_root_times(trans, root); |
6226 | ||
63541927 FDBM |
6227 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6228 | if (ret) | |
0b246afa | 6229 | btrfs_err(fs_info, |
63541927 | 6230 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6231 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6232 | |
39279cc3 | 6233 | return inode; |
b0d5d10f CM |
6234 | |
6235 | fail_unlock: | |
32955c54 | 6236 | discard_new_inode(inode); |
5f39d397 | 6237 | fail: |
ef3b9af5 | 6238 | if (dir && name) |
aec7477b | 6239 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6240 | btrfs_free_path(path); |
6241 | return ERR_PTR(ret); | |
39279cc3 CM |
6242 | } |
6243 | ||
d352ac68 CM |
6244 | /* |
6245 | * utility function to add 'inode' into 'parent_inode' with | |
6246 | * a give name and a given sequence number. | |
6247 | * if 'add_backref' is true, also insert a backref from the | |
6248 | * inode to the parent directory. | |
6249 | */ | |
e02119d5 | 6250 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6251 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6252 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6253 | { |
4df27c4d | 6254 | int ret = 0; |
39279cc3 | 6255 | struct btrfs_key key; |
db0a669f NB |
6256 | struct btrfs_root *root = parent_inode->root; |
6257 | u64 ino = btrfs_ino(inode); | |
6258 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6259 | |
33345d01 | 6260 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6261 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6262 | } else { |
33345d01 | 6263 | key.objectid = ino; |
962a298f | 6264 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6265 | key.offset = 0; |
6266 | } | |
6267 | ||
33345d01 | 6268 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6269 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6270 | root->root_key.objectid, parent_ino, |
6271 | index, name, name_len); | |
4df27c4d | 6272 | } else if (add_backref) { |
33345d01 LZ |
6273 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6274 | parent_ino, index); | |
4df27c4d | 6275 | } |
39279cc3 | 6276 | |
79787eaa JM |
6277 | /* Nothing to clean up yet */ |
6278 | if (ret) | |
6279 | return ret; | |
4df27c4d | 6280 | |
684572df | 6281 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6282 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6283 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6284 | goto fail_dir_item; |
6285 | else if (ret) { | |
66642832 | 6286 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6287 | return ret; |
39279cc3 | 6288 | } |
79787eaa | 6289 | |
db0a669f | 6290 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6291 | name_len * 2); |
db0a669f | 6292 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6293 | /* |
6294 | * If we are replaying a log tree, we do not want to update the mtime | |
6295 | * and ctime of the parent directory with the current time, since the | |
6296 | * log replay procedure is responsible for setting them to their correct | |
6297 | * values (the ones it had when the fsync was done). | |
6298 | */ | |
6299 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6300 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6301 | ||
6302 | parent_inode->vfs_inode.i_mtime = now; | |
6303 | parent_inode->vfs_inode.i_ctime = now; | |
6304 | } | |
9a56fcd1 | 6305 | ret = btrfs_update_inode(trans, root, parent_inode); |
79787eaa | 6306 | if (ret) |
66642832 | 6307 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6308 | return ret; |
fe66a05a CM |
6309 | |
6310 | fail_dir_item: | |
6311 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6312 | u64 local_index; | |
6313 | int err; | |
3ee1c553 | 6314 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6315 | root->root_key.objectid, parent_ino, |
6316 | &local_index, name, name_len); | |
1690dd41 JT |
6317 | if (err) |
6318 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6319 | } else if (add_backref) { |
6320 | u64 local_index; | |
6321 | int err; | |
6322 | ||
6323 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6324 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6325 | if (err) |
6326 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6327 | } |
1690dd41 JT |
6328 | |
6329 | /* Return the original error code */ | |
fe66a05a | 6330 | return ret; |
39279cc3 CM |
6331 | } |
6332 | ||
6333 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6334 | struct btrfs_inode *dir, struct dentry *dentry, |
6335 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6336 | { |
a1b075d2 JB |
6337 | int err = btrfs_add_link(trans, dir, inode, |
6338 | dentry->d_name.name, dentry->d_name.len, | |
6339 | backref, index); | |
39279cc3 CM |
6340 | if (err > 0) |
6341 | err = -EEXIST; | |
6342 | return err; | |
6343 | } | |
6344 | ||
549c7297 CB |
6345 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
6346 | struct dentry *dentry, umode_t mode, dev_t rdev) | |
618e21d5 | 6347 | { |
2ff7e61e | 6348 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6349 | struct btrfs_trans_handle *trans; |
6350 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6351 | struct inode *inode = NULL; |
618e21d5 | 6352 | int err; |
618e21d5 | 6353 | u64 objectid; |
00e4e6b3 | 6354 | u64 index = 0; |
618e21d5 | 6355 | |
9ed74f2d JB |
6356 | /* |
6357 | * 2 for inode item and ref | |
6358 | * 2 for dir items | |
6359 | * 1 for xattr if selinux is on | |
6360 | */ | |
a22285a6 YZ |
6361 | trans = btrfs_start_transaction(root, 5); |
6362 | if (IS_ERR(trans)) | |
6363 | return PTR_ERR(trans); | |
1832a6d5 | 6364 | |
543068a2 | 6365 | err = btrfs_get_free_objectid(root, &objectid); |
581bb050 LZ |
6366 | if (err) |
6367 | goto out_unlock; | |
6368 | ||
72105277 | 6369 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
b3b6f5b9 CB |
6370 | dentry->d_name.name, dentry->d_name.len, |
6371 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); | |
7cf96da3 TI |
6372 | if (IS_ERR(inode)) { |
6373 | err = PTR_ERR(inode); | |
32955c54 | 6374 | inode = NULL; |
618e21d5 | 6375 | goto out_unlock; |
7cf96da3 | 6376 | } |
618e21d5 | 6377 | |
ad19db71 CS |
6378 | /* |
6379 | * If the active LSM wants to access the inode during | |
6380 | * d_instantiate it needs these. Smack checks to see | |
6381 | * if the filesystem supports xattrs by looking at the | |
6382 | * ops vector. | |
6383 | */ | |
ad19db71 | 6384 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6385 | init_special_inode(inode, inode->i_mode, rdev); |
6386 | ||
6387 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6388 | if (err) |
32955c54 | 6389 | goto out_unlock; |
b0d5d10f | 6390 | |
cef415af NB |
6391 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6392 | 0, index); | |
32955c54 AV |
6393 | if (err) |
6394 | goto out_unlock; | |
6395 | ||
9a56fcd1 | 6396 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
32955c54 | 6397 | d_instantiate_new(dentry, inode); |
b0d5d10f | 6398 | |
618e21d5 | 6399 | out_unlock: |
3a45bb20 | 6400 | btrfs_end_transaction(trans); |
2ff7e61e | 6401 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6402 | if (err && inode) { |
618e21d5 | 6403 | inode_dec_link_count(inode); |
32955c54 | 6404 | discard_new_inode(inode); |
618e21d5 | 6405 | } |
618e21d5 JB |
6406 | return err; |
6407 | } | |
6408 | ||
549c7297 CB |
6409 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
6410 | struct dentry *dentry, umode_t mode, bool excl) | |
39279cc3 | 6411 | { |
2ff7e61e | 6412 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6413 | struct btrfs_trans_handle *trans; |
6414 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6415 | struct inode *inode = NULL; |
a22285a6 | 6416 | int err; |
39279cc3 | 6417 | u64 objectid; |
00e4e6b3 | 6418 | u64 index = 0; |
39279cc3 | 6419 | |
9ed74f2d JB |
6420 | /* |
6421 | * 2 for inode item and ref | |
6422 | * 2 for dir items | |
6423 | * 1 for xattr if selinux is on | |
6424 | */ | |
a22285a6 YZ |
6425 | trans = btrfs_start_transaction(root, 5); |
6426 | if (IS_ERR(trans)) | |
6427 | return PTR_ERR(trans); | |
9ed74f2d | 6428 | |
543068a2 | 6429 | err = btrfs_get_free_objectid(root, &objectid); |
581bb050 LZ |
6430 | if (err) |
6431 | goto out_unlock; | |
6432 | ||
e93ca491 | 6433 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
b3b6f5b9 CB |
6434 | dentry->d_name.name, dentry->d_name.len, |
6435 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); | |
7cf96da3 TI |
6436 | if (IS_ERR(inode)) { |
6437 | err = PTR_ERR(inode); | |
32955c54 | 6438 | inode = NULL; |
39279cc3 | 6439 | goto out_unlock; |
7cf96da3 | 6440 | } |
ad19db71 CS |
6441 | /* |
6442 | * If the active LSM wants to access the inode during | |
6443 | * d_instantiate it needs these. Smack checks to see | |
6444 | * if the filesystem supports xattrs by looking at the | |
6445 | * ops vector. | |
6446 | */ | |
6447 | inode->i_fop = &btrfs_file_operations; | |
6448 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6449 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6450 | |
6451 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6452 | if (err) | |
32955c54 | 6453 | goto out_unlock; |
b0d5d10f | 6454 | |
9a56fcd1 | 6455 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
b0d5d10f | 6456 | if (err) |
32955c54 | 6457 | goto out_unlock; |
ad19db71 | 6458 | |
cef415af NB |
6459 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6460 | 0, index); | |
39279cc3 | 6461 | if (err) |
32955c54 | 6462 | goto out_unlock; |
43baa579 | 6463 | |
1e2e547a | 6464 | d_instantiate_new(dentry, inode); |
43baa579 | 6465 | |
39279cc3 | 6466 | out_unlock: |
3a45bb20 | 6467 | btrfs_end_transaction(trans); |
32955c54 | 6468 | if (err && inode) { |
39279cc3 | 6469 | inode_dec_link_count(inode); |
32955c54 | 6470 | discard_new_inode(inode); |
39279cc3 | 6471 | } |
2ff7e61e | 6472 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6473 | return err; |
6474 | } | |
6475 | ||
6476 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6477 | struct dentry *dentry) | |
6478 | { | |
271dba45 | 6479 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6480 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6481 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6482 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6483 | u64 index; |
39279cc3 CM |
6484 | int err; |
6485 | int drop_inode = 0; | |
6486 | ||
4a8be425 | 6487 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6488 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6489 | return -EXDEV; |
4a8be425 | 6490 | |
f186373f | 6491 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6492 | return -EMLINK; |
4a8be425 | 6493 | |
877574e2 | 6494 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6495 | if (err) |
6496 | goto fail; | |
6497 | ||
a22285a6 | 6498 | /* |
7e6b6465 | 6499 | * 2 items for inode and inode ref |
a22285a6 | 6500 | * 2 items for dir items |
7e6b6465 | 6501 | * 1 item for parent inode |
399b0bbf | 6502 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6503 | */ |
399b0bbf | 6504 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6505 | if (IS_ERR(trans)) { |
6506 | err = PTR_ERR(trans); | |
271dba45 | 6507 | trans = NULL; |
a22285a6 YZ |
6508 | goto fail; |
6509 | } | |
5f39d397 | 6510 | |
67de1176 MX |
6511 | /* There are several dir indexes for this inode, clear the cache. */ |
6512 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6513 | inc_nlink(inode); |
0c4d2d95 | 6514 | inode_inc_iversion(inode); |
c2050a45 | 6515 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6516 | ihold(inode); |
e9976151 | 6517 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6518 | |
cef415af NB |
6519 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6520 | 1, index); | |
5f39d397 | 6521 | |
a5719521 | 6522 | if (err) { |
54aa1f4d | 6523 | drop_inode = 1; |
a5719521 | 6524 | } else { |
10d9f309 | 6525 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6526 | |
9a56fcd1 | 6527 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
79787eaa JM |
6528 | if (err) |
6529 | goto fail; | |
ef3b9af5 FM |
6530 | if (inode->i_nlink == 1) { |
6531 | /* | |
6532 | * If new hard link count is 1, it's a file created | |
6533 | * with open(2) O_TMPFILE flag. | |
6534 | */ | |
3d6ae7bb | 6535 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6536 | if (err) |
6537 | goto fail; | |
6538 | } | |
08c422c2 | 6539 | d_instantiate(dentry, inode); |
75b463d2 | 6540 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6541 | } |
39279cc3 | 6542 | |
1832a6d5 | 6543 | fail: |
271dba45 | 6544 | if (trans) |
3a45bb20 | 6545 | btrfs_end_transaction(trans); |
39279cc3 CM |
6546 | if (drop_inode) { |
6547 | inode_dec_link_count(inode); | |
6548 | iput(inode); | |
6549 | } | |
2ff7e61e | 6550 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6551 | return err; |
6552 | } | |
6553 | ||
549c7297 CB |
6554 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
6555 | struct dentry *dentry, umode_t mode) | |
39279cc3 | 6556 | { |
2ff7e61e | 6557 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6558 | struct inode *inode = NULL; |
39279cc3 CM |
6559 | struct btrfs_trans_handle *trans; |
6560 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6561 | int err = 0; | |
b9d86667 | 6562 | u64 objectid = 0; |
00e4e6b3 | 6563 | u64 index = 0; |
39279cc3 | 6564 | |
9ed74f2d JB |
6565 | /* |
6566 | * 2 items for inode and ref | |
6567 | * 2 items for dir items | |
6568 | * 1 for xattr if selinux is on | |
6569 | */ | |
a22285a6 YZ |
6570 | trans = btrfs_start_transaction(root, 5); |
6571 | if (IS_ERR(trans)) | |
6572 | return PTR_ERR(trans); | |
39279cc3 | 6573 | |
543068a2 | 6574 | err = btrfs_get_free_objectid(root, &objectid); |
581bb050 LZ |
6575 | if (err) |
6576 | goto out_fail; | |
6577 | ||
b0b3e44d | 6578 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
b3b6f5b9 CB |
6579 | dentry->d_name.name, dentry->d_name.len, |
6580 | btrfs_ino(BTRFS_I(dir)), objectid, | |
f85b7379 | 6581 | S_IFDIR | mode, &index); |
39279cc3 CM |
6582 | if (IS_ERR(inode)) { |
6583 | err = PTR_ERR(inode); | |
32955c54 | 6584 | inode = NULL; |
39279cc3 CM |
6585 | goto out_fail; |
6586 | } | |
5f39d397 | 6587 | |
b0d5d10f CM |
6588 | /* these must be set before we unlock the inode */ |
6589 | inode->i_op = &btrfs_dir_inode_operations; | |
6590 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6591 | |
2a7dba39 | 6592 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6593 | if (err) |
32955c54 | 6594 | goto out_fail; |
39279cc3 | 6595 | |
6ef06d27 | 6596 | btrfs_i_size_write(BTRFS_I(inode), 0); |
9a56fcd1 | 6597 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
39279cc3 | 6598 | if (err) |
32955c54 | 6599 | goto out_fail; |
5f39d397 | 6600 | |
db0a669f NB |
6601 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6602 | dentry->d_name.name, | |
6603 | dentry->d_name.len, 0, index); | |
39279cc3 | 6604 | if (err) |
32955c54 | 6605 | goto out_fail; |
5f39d397 | 6606 | |
1e2e547a | 6607 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
6608 | |
6609 | out_fail: | |
3a45bb20 | 6610 | btrfs_end_transaction(trans); |
32955c54 | 6611 | if (err && inode) { |
c7cfb8a5 | 6612 | inode_dec_link_count(inode); |
32955c54 | 6613 | discard_new_inode(inode); |
c7cfb8a5 | 6614 | } |
2ff7e61e | 6615 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6616 | return err; |
6617 | } | |
6618 | ||
c8b97818 | 6619 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6620 | struct page *page, |
c8b97818 CM |
6621 | size_t pg_offset, u64 extent_offset, |
6622 | struct btrfs_file_extent_item *item) | |
6623 | { | |
6624 | int ret; | |
6625 | struct extent_buffer *leaf = path->nodes[0]; | |
6626 | char *tmp; | |
6627 | size_t max_size; | |
6628 | unsigned long inline_size; | |
6629 | unsigned long ptr; | |
261507a0 | 6630 | int compress_type; |
c8b97818 CM |
6631 | |
6632 | WARN_ON(pg_offset != 0); | |
261507a0 | 6633 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 | 6634 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
437bd07e | 6635 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); |
c8b97818 | 6636 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6637 | if (!tmp) |
6638 | return -ENOMEM; | |
c8b97818 CM |
6639 | ptr = btrfs_file_extent_inline_start(item); |
6640 | ||
6641 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6642 | ||
09cbfeaf | 6643 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6644 | ret = btrfs_decompress(compress_type, tmp, page, |
6645 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6646 | |
6647 | /* | |
6648 | * decompression code contains a memset to fill in any space between the end | |
6649 | * of the uncompressed data and the end of max_size in case the decompressed | |
6650 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6651 | * the end of an inline extent and the beginning of the next block, so we | |
6652 | * cover that region here. | |
6653 | */ | |
6654 | ||
d048b9c2 IW |
6655 | if (max_size + pg_offset < PAGE_SIZE) |
6656 | memzero_page(page, pg_offset + max_size, | |
6657 | PAGE_SIZE - max_size - pg_offset); | |
c8b97818 | 6658 | kfree(tmp); |
166ae5a4 | 6659 | return ret; |
c8b97818 CM |
6660 | } |
6661 | ||
39b07b5d OS |
6662 | /** |
6663 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. | |
6664 | * @inode: file to search in | |
6665 | * @page: page to read extent data into if the extent is inline | |
6666 | * @pg_offset: offset into @page to copy to | |
6667 | * @start: file offset | |
6668 | * @len: length of range starting at @start | |
6669 | * | |
6670 | * This returns the first &struct extent_map which overlaps with the given | |
6671 | * range, reading it from the B-tree and caching it if necessary. Note that | |
6672 | * there may be more extents which overlap the given range after the returned | |
6673 | * extent_map. | |
d352ac68 | 6674 | * |
39b07b5d OS |
6675 | * If @page is not NULL and the extent is inline, this also reads the extent |
6676 | * data directly into the page and marks the extent up to date in the io_tree. | |
6677 | * | |
6678 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6679 | */ |
fc4f21b1 | 6680 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
39b07b5d OS |
6681 | struct page *page, size_t pg_offset, |
6682 | u64 start, u64 len) | |
a52d9a80 | 6683 | { |
3ffbd68c | 6684 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6685 | int ret = 0; |
a52d9a80 CM |
6686 | u64 extent_start = 0; |
6687 | u64 extent_end = 0; | |
fc4f21b1 | 6688 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6689 | int extent_type = -1; |
f421950f | 6690 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6691 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6692 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6693 | struct extent_buffer *leaf; |
6694 | struct btrfs_key found_key; | |
a52d9a80 | 6695 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6696 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6697 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6698 | |
890871be | 6699 | read_lock(&em_tree->lock); |
d1310b2e | 6700 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6701 | read_unlock(&em_tree->lock); |
d1310b2e | 6702 | |
a52d9a80 | 6703 | if (em) { |
e1c4b745 CM |
6704 | if (em->start > start || em->start + em->len <= start) |
6705 | free_extent_map(em); | |
6706 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6707 | free_extent_map(em); |
6708 | else | |
6709 | goto out; | |
a52d9a80 | 6710 | } |
172ddd60 | 6711 | em = alloc_extent_map(); |
a52d9a80 | 6712 | if (!em) { |
1028d1c4 | 6713 | ret = -ENOMEM; |
d1310b2e | 6714 | goto out; |
a52d9a80 | 6715 | } |
d1310b2e | 6716 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6717 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6718 | em->len = (u64)-1; |
c8b97818 | 6719 | em->block_len = (u64)-1; |
f421950f | 6720 | |
bee6ec82 | 6721 | path = btrfs_alloc_path(); |
f421950f | 6722 | if (!path) { |
1028d1c4 | 6723 | ret = -ENOMEM; |
bee6ec82 | 6724 | goto out; |
f421950f CM |
6725 | } |
6726 | ||
bee6ec82 LB |
6727 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6728 | path->reada = READA_FORWARD; | |
4d7240f0 JB |
6729 | |
6730 | /* | |
6731 | * The same explanation in load_free_space_cache applies here as well, | |
6732 | * we only read when we're loading the free space cache, and at that | |
6733 | * point the commit_root has everything we need. | |
6734 | */ | |
6735 | if (btrfs_is_free_space_inode(inode)) { | |
6736 | path->search_commit_root = 1; | |
6737 | path->skip_locking = 1; | |
6738 | } | |
51899412 | 6739 | |
5c9a702e | 6740 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6741 | if (ret < 0) { |
a52d9a80 | 6742 | goto out; |
b8eeab7f | 6743 | } else if (ret > 0) { |
a52d9a80 CM |
6744 | if (path->slots[0] == 0) |
6745 | goto not_found; | |
6746 | path->slots[0]--; | |
1028d1c4 | 6747 | ret = 0; |
a52d9a80 CM |
6748 | } |
6749 | ||
5f39d397 CM |
6750 | leaf = path->nodes[0]; |
6751 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6752 | struct btrfs_file_extent_item); |
5f39d397 | 6753 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6754 | if (found_key.objectid != objectid || |
694c12ed | 6755 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6756 | /* |
6757 | * If we backup past the first extent we want to move forward | |
6758 | * and see if there is an extent in front of us, otherwise we'll | |
6759 | * say there is a hole for our whole search range which can | |
6760 | * cause problems. | |
6761 | */ | |
6762 | extent_end = start; | |
6763 | goto next; | |
a52d9a80 CM |
6764 | } |
6765 | ||
694c12ed | 6766 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6767 | extent_start = found_key.offset; |
a5eeb3d1 | 6768 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6769 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6770 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6771 | /* Only regular file could have regular/prealloc extent */ |
6772 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 6773 | ret = -EUCLEAN; |
6bf9e4bd QW |
6774 | btrfs_crit(fs_info, |
6775 | "regular/prealloc extent found for non-regular inode %llu", | |
6776 | btrfs_ino(inode)); | |
6777 | goto out; | |
6778 | } | |
09ed2f16 LB |
6779 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6780 | extent_start); | |
694c12ed | 6781 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6782 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6783 | path->slots[0], | |
6784 | extent_start); | |
9036c102 | 6785 | } |
25a50341 | 6786 | next: |
9036c102 YZ |
6787 | if (start >= extent_end) { |
6788 | path->slots[0]++; | |
6789 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6790 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 6791 | if (ret < 0) |
9036c102 | 6792 | goto out; |
1028d1c4 | 6793 | else if (ret > 0) |
9036c102 | 6794 | goto not_found; |
1028d1c4 | 6795 | |
9036c102 | 6796 | leaf = path->nodes[0]; |
a52d9a80 | 6797 | } |
9036c102 YZ |
6798 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6799 | if (found_key.objectid != objectid || | |
6800 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6801 | goto not_found; | |
6802 | if (start + len <= found_key.offset) | |
6803 | goto not_found; | |
e2eca69d WS |
6804 | if (start > found_key.offset) |
6805 | goto next; | |
02a033df NB |
6806 | |
6807 | /* New extent overlaps with existing one */ | |
9036c102 | 6808 | em->start = start; |
70c8a91c | 6809 | em->orig_start = start; |
9036c102 | 6810 | em->len = found_key.offset - start; |
02a033df NB |
6811 | em->block_start = EXTENT_MAP_HOLE; |
6812 | goto insert; | |
9036c102 YZ |
6813 | } |
6814 | ||
39b07b5d | 6815 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
7ffbb598 | 6816 | |
694c12ed NB |
6817 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6818 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6819 | goto insert; |
694c12ed | 6820 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 6821 | unsigned long ptr; |
a52d9a80 | 6822 | char *map; |
3326d1b0 CM |
6823 | size_t size; |
6824 | size_t extent_offset; | |
6825 | size_t copy_size; | |
a52d9a80 | 6826 | |
39b07b5d | 6827 | if (!page) |
689f9346 | 6828 | goto out; |
5f39d397 | 6829 | |
e41ca589 | 6830 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 6831 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6832 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6833 | size - extent_offset); | |
3326d1b0 | 6834 | em->start = extent_start + extent_offset; |
0b246afa | 6835 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 6836 | em->orig_block_len = em->len; |
70c8a91c | 6837 | em->orig_start = em->start; |
689f9346 | 6838 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 | 6839 | |
bf46f52d | 6840 | if (!PageUptodate(page)) { |
261507a0 LZ |
6841 | if (btrfs_file_extent_compression(leaf, item) != |
6842 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6843 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6844 | extent_offset, item); |
1028d1c4 | 6845 | if (ret) |
166ae5a4 | 6846 | goto out; |
c8b97818 | 6847 | } else { |
58c1a35c | 6848 | map = kmap_local_page(page); |
c8b97818 CM |
6849 | read_extent_buffer(leaf, map + pg_offset, ptr, |
6850 | copy_size); | |
09cbfeaf | 6851 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6852 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6853 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6854 | copy_size); |
6855 | } | |
58c1a35c | 6856 | kunmap_local(map); |
c8b97818 | 6857 | } |
179e29e4 | 6858 | flush_dcache_page(page); |
a52d9a80 | 6859 | } |
d1310b2e | 6860 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6861 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6862 | goto insert; |
a52d9a80 CM |
6863 | } |
6864 | not_found: | |
6865 | em->start = start; | |
70c8a91c | 6866 | em->orig_start = start; |
d1310b2e | 6867 | em->len = len; |
5f39d397 | 6868 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6869 | insert: |
1028d1c4 | 6870 | ret = 0; |
b3b4aa74 | 6871 | btrfs_release_path(path); |
d1310b2e | 6872 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6873 | btrfs_err(fs_info, |
5d163e0e JM |
6874 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6875 | em->start, em->len, start, len); | |
1028d1c4 | 6876 | ret = -EIO; |
a52d9a80 CM |
6877 | goto out; |
6878 | } | |
d1310b2e | 6879 | |
890871be | 6880 | write_lock(&em_tree->lock); |
1028d1c4 | 6881 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6882 | write_unlock(&em_tree->lock); |
a52d9a80 | 6883 | out: |
c6414280 | 6884 | btrfs_free_path(path); |
1abe9b8a | 6885 | |
fc4f21b1 | 6886 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6887 | |
1028d1c4 | 6888 | if (ret) { |
a52d9a80 | 6889 | free_extent_map(em); |
1028d1c4 | 6890 | return ERR_PTR(ret); |
a52d9a80 CM |
6891 | } |
6892 | return em; | |
6893 | } | |
6894 | ||
fc4f21b1 | 6895 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 6896 | u64 start, u64 len) |
ec29ed5b CM |
6897 | { |
6898 | struct extent_map *em; | |
6899 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 6900 | u64 delalloc_start = start; |
ec29ed5b | 6901 | u64 end; |
f3714ef4 NB |
6902 | u64 delalloc_len; |
6903 | u64 delalloc_end; | |
ec29ed5b CM |
6904 | int err = 0; |
6905 | ||
39b07b5d | 6906 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
ec29ed5b CM |
6907 | if (IS_ERR(em)) |
6908 | return em; | |
9986277e DC |
6909 | /* |
6910 | * If our em maps to: | |
6911 | * - a hole or | |
6912 | * - a pre-alloc extent, | |
6913 | * there might actually be delalloc bytes behind it. | |
6914 | */ | |
6915 | if (em->block_start != EXTENT_MAP_HOLE && | |
6916 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6917 | return em; | |
6918 | else | |
6919 | hole_em = em; | |
ec29ed5b CM |
6920 | |
6921 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6922 | end = start + len; | |
6923 | if (end < start) | |
6924 | end = (u64)-1; | |
6925 | else | |
6926 | end -= 1; | |
6927 | ||
6928 | em = NULL; | |
6929 | ||
6930 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 6931 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 6932 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
6933 | delalloc_end = delalloc_start + delalloc_len; |
6934 | if (delalloc_end < delalloc_start) | |
6935 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
6936 | |
6937 | /* | |
f3714ef4 NB |
6938 | * We didn't find anything useful, return the original results from |
6939 | * get_extent() | |
ec29ed5b | 6940 | */ |
f3714ef4 | 6941 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
6942 | em = hole_em; |
6943 | hole_em = NULL; | |
6944 | goto out; | |
6945 | } | |
6946 | ||
f3714ef4 NB |
6947 | /* |
6948 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
6949 | * the start they passed in | |
ec29ed5b | 6950 | */ |
f3714ef4 NB |
6951 | delalloc_start = max(start, delalloc_start); |
6952 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 6953 | |
f3714ef4 NB |
6954 | if (delalloc_len > 0) { |
6955 | u64 hole_start; | |
02950af4 | 6956 | u64 hole_len; |
f3714ef4 | 6957 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 6958 | |
172ddd60 | 6959 | em = alloc_extent_map(); |
ec29ed5b CM |
6960 | if (!em) { |
6961 | err = -ENOMEM; | |
6962 | goto out; | |
6963 | } | |
f3714ef4 NB |
6964 | |
6965 | ASSERT(hole_em); | |
ec29ed5b | 6966 | /* |
f3714ef4 NB |
6967 | * When btrfs_get_extent can't find anything it returns one |
6968 | * huge hole | |
ec29ed5b | 6969 | * |
f3714ef4 NB |
6970 | * Make sure what it found really fits our range, and adjust to |
6971 | * make sure it is based on the start from the caller | |
ec29ed5b | 6972 | */ |
f3714ef4 NB |
6973 | if (hole_end <= start || hole_em->start > end) { |
6974 | free_extent_map(hole_em); | |
6975 | hole_em = NULL; | |
6976 | } else { | |
6977 | hole_start = max(hole_em->start, start); | |
6978 | hole_len = hole_end - hole_start; | |
ec29ed5b | 6979 | } |
f3714ef4 NB |
6980 | |
6981 | if (hole_em && delalloc_start > hole_start) { | |
6982 | /* | |
6983 | * Our hole starts before our delalloc, so we have to | |
6984 | * return just the parts of the hole that go until the | |
6985 | * delalloc starts | |
ec29ed5b | 6986 | */ |
f3714ef4 | 6987 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
6988 | em->start = hole_start; |
6989 | em->orig_start = hole_start; | |
6990 | /* | |
f3714ef4 NB |
6991 | * Don't adjust block start at all, it is fixed at |
6992 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
6993 | */ |
6994 | em->block_start = hole_em->block_start; | |
6995 | em->block_len = hole_len; | |
f9e4fb53 LB |
6996 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6997 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 6998 | } else { |
f3714ef4 NB |
6999 | /* |
7000 | * Hole is out of passed range or it starts after | |
7001 | * delalloc range | |
7002 | */ | |
7003 | em->start = delalloc_start; | |
7004 | em->len = delalloc_len; | |
7005 | em->orig_start = delalloc_start; | |
ec29ed5b | 7006 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 7007 | em->block_len = delalloc_len; |
ec29ed5b | 7008 | } |
bf8d32b9 | 7009 | } else { |
ec29ed5b CM |
7010 | return hole_em; |
7011 | } | |
7012 | out: | |
7013 | ||
7014 | free_extent_map(hole_em); | |
7015 | if (err) { | |
7016 | free_extent_map(em); | |
7017 | return ERR_PTR(err); | |
7018 | } | |
7019 | return em; | |
7020 | } | |
7021 | ||
64f54188 | 7022 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
5f9a8a51 FM |
7023 | const u64 start, |
7024 | const u64 len, | |
7025 | const u64 orig_start, | |
7026 | const u64 block_start, | |
7027 | const u64 block_len, | |
7028 | const u64 orig_block_len, | |
7029 | const u64 ram_bytes, | |
7030 | const int type) | |
7031 | { | |
7032 | struct extent_map *em = NULL; | |
7033 | int ret; | |
7034 | ||
5f9a8a51 | 7035 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
7036 | em = create_io_em(inode, start, len, orig_start, block_start, |
7037 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
7038 | BTRFS_COMPRESS_NONE, /* compress_type */ |
7039 | type); | |
5f9a8a51 FM |
7040 | if (IS_ERR(em)) |
7041 | goto out; | |
7042 | } | |
64f54188 NB |
7043 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
7044 | block_len, type); | |
5f9a8a51 FM |
7045 | if (ret) { |
7046 | if (em) { | |
7047 | free_extent_map(em); | |
64f54188 | 7048 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
5f9a8a51 FM |
7049 | } |
7050 | em = ERR_PTR(ret); | |
7051 | } | |
7052 | out: | |
5f9a8a51 FM |
7053 | |
7054 | return em; | |
7055 | } | |
7056 | ||
9fc6f911 | 7057 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
4b46fce2 JB |
7058 | u64 start, u64 len) |
7059 | { | |
9fc6f911 NB |
7060 | struct btrfs_root *root = inode->root; |
7061 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 7062 | struct extent_map *em; |
4b46fce2 JB |
7063 | struct btrfs_key ins; |
7064 | u64 alloc_hint; | |
7065 | int ret; | |
4b46fce2 | 7066 | |
9fc6f911 | 7067 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7068 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7069 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7070 | if (ret) |
7071 | return ERR_PTR(ret); | |
4b46fce2 | 7072 | |
9fc6f911 | 7073 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
5f9a8a51 | 7074 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 7075 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7076 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7077 | if (IS_ERR(em)) |
9fc6f911 NB |
7078 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
7079 | 1); | |
de0ee0ed | 7080 | |
4b46fce2 JB |
7081 | return em; |
7082 | } | |
7083 | ||
f4639636 | 7084 | static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
05947ae1 AJ |
7085 | { |
7086 | struct btrfs_block_group *block_group; | |
f4639636 | 7087 | bool readonly = false; |
05947ae1 AJ |
7088 | |
7089 | block_group = btrfs_lookup_block_group(fs_info, bytenr); | |
7090 | if (!block_group || block_group->ro) | |
f4639636 | 7091 | readonly = true; |
05947ae1 AJ |
7092 | if (block_group) |
7093 | btrfs_put_block_group(block_group); | |
7094 | return readonly; | |
7095 | } | |
7096 | ||
46bfbb5c | 7097 | /* |
e4ecaf90 QW |
7098 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
7099 | * | |
7100 | * @offset: File offset | |
7101 | * @len: The length to write, will be updated to the nocow writeable | |
7102 | * range | |
7103 | * @orig_start: (optional) Return the original file offset of the file extent | |
7104 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
7105 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
7106 | * @strict: if true, omit optimizations that might force us into unnecessary |
7107 | * cow. e.g., don't trust generation number. | |
e4ecaf90 | 7108 | * |
e4ecaf90 QW |
7109 | * Return: |
7110 | * >0 and update @len if we can do nocow write | |
7111 | * 0 if we can't do nocow write | |
7112 | * <0 if error happened | |
7113 | * | |
7114 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
7115 | * any ordered extents. | |
46bfbb5c | 7116 | */ |
00361589 | 7117 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 7118 | u64 *orig_start, u64 *orig_block_len, |
a84d5d42 | 7119 | u64 *ram_bytes, bool strict) |
46bfbb5c | 7120 | { |
2ff7e61e | 7121 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7122 | struct btrfs_path *path; |
7123 | int ret; | |
7124 | struct extent_buffer *leaf; | |
7125 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7126 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7127 | struct btrfs_file_extent_item *fi; |
7128 | struct btrfs_key key; | |
7129 | u64 disk_bytenr; | |
7130 | u64 backref_offset; | |
7131 | u64 extent_end; | |
7132 | u64 num_bytes; | |
7133 | int slot; | |
7134 | int found_type; | |
7ee9e440 | 7135 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7136 | |
46bfbb5c CM |
7137 | path = btrfs_alloc_path(); |
7138 | if (!path) | |
7139 | return -ENOMEM; | |
7140 | ||
f85b7379 DS |
7141 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7142 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7143 | if (ret < 0) |
7144 | goto out; | |
7145 | ||
7146 | slot = path->slots[0]; | |
7147 | if (ret == 1) { | |
7148 | if (slot == 0) { | |
7149 | /* can't find the item, must cow */ | |
7150 | ret = 0; | |
7151 | goto out; | |
7152 | } | |
7153 | slot--; | |
7154 | } | |
7155 | ret = 0; | |
7156 | leaf = path->nodes[0]; | |
7157 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7158 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7159 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7160 | /* not our file or wrong item type, must cow */ | |
7161 | goto out; | |
7162 | } | |
7163 | ||
7164 | if (key.offset > offset) { | |
7165 | /* Wrong offset, must cow */ | |
7166 | goto out; | |
7167 | } | |
7168 | ||
7169 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7170 | found_type = btrfs_file_extent_type(leaf, fi); | |
7171 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7172 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7173 | /* not a regular extent, must cow */ | |
7174 | goto out; | |
7175 | } | |
7ee9e440 JB |
7176 | |
7177 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7178 | goto out; | |
7179 | ||
e77751aa MX |
7180 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7181 | if (extent_end <= offset) | |
7182 | goto out; | |
7183 | ||
46bfbb5c | 7184 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7185 | if (disk_bytenr == 0) |
7186 | goto out; | |
7187 | ||
7188 | if (btrfs_file_extent_compression(leaf, fi) || | |
7189 | btrfs_file_extent_encryption(leaf, fi) || | |
7190 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7191 | goto out; | |
7192 | ||
78d4295b EL |
7193 | /* |
7194 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7195 | * unnecessary search. | |
7196 | */ | |
a84d5d42 BB |
7197 | if (!strict && |
7198 | (btrfs_file_extent_generation(leaf, fi) <= | |
7199 | btrfs_root_last_snapshot(&root->root_item))) | |
78d4295b EL |
7200 | goto out; |
7201 | ||
46bfbb5c CM |
7202 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7203 | ||
7ee9e440 JB |
7204 | if (orig_start) { |
7205 | *orig_start = key.offset - backref_offset; | |
7206 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7207 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7208 | } | |
eb384b55 | 7209 | |
2ff7e61e | 7210 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7211 | goto out; |
7b2b7085 MX |
7212 | |
7213 | num_bytes = min(offset + *len, extent_end) - offset; | |
7214 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7215 | u64 range_end; | |
7216 | ||
da17066c JM |
7217 | range_end = round_up(offset + num_bytes, |
7218 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7219 | ret = test_range_bit(io_tree, offset, range_end, |
7220 | EXTENT_DELALLOC, 0, NULL); | |
7221 | if (ret) { | |
7222 | ret = -EAGAIN; | |
7223 | goto out; | |
7224 | } | |
7225 | } | |
7226 | ||
1bda19eb | 7227 | btrfs_release_path(path); |
46bfbb5c CM |
7228 | |
7229 | /* | |
7230 | * look for other files referencing this extent, if we | |
7231 | * find any we must cow | |
7232 | */ | |
00361589 | 7233 | |
e4c3b2dc | 7234 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
a84d5d42 BB |
7235 | key.offset - backref_offset, disk_bytenr, |
7236 | strict); | |
00361589 JB |
7237 | if (ret) { |
7238 | ret = 0; | |
7239 | goto out; | |
7240 | } | |
46bfbb5c CM |
7241 | |
7242 | /* | |
7243 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7244 | * in this extent we are about to write. If there | |
7245 | * are any csums in that range we have to cow in order | |
7246 | * to keep the csums correct | |
7247 | */ | |
7248 | disk_bytenr += backref_offset; | |
7249 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7250 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7251 | goto out; | |
46bfbb5c CM |
7252 | /* |
7253 | * all of the above have passed, it is safe to overwrite this extent | |
7254 | * without cow | |
7255 | */ | |
eb384b55 | 7256 | *len = num_bytes; |
46bfbb5c CM |
7257 | ret = 1; |
7258 | out: | |
7259 | btrfs_free_path(path); | |
7260 | return ret; | |
7261 | } | |
7262 | ||
eb838e73 | 7263 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
f85781fb | 7264 | struct extent_state **cached_state, bool writing) |
eb838e73 JB |
7265 | { |
7266 | struct btrfs_ordered_extent *ordered; | |
7267 | int ret = 0; | |
7268 | ||
7269 | while (1) { | |
7270 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7271 | cached_state); |
eb838e73 JB |
7272 | /* |
7273 | * We're concerned with the entire range that we're going to be | |
01327610 | 7274 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7275 | * extents in this range. |
7276 | */ | |
a776c6fa | 7277 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7278 | lockend - lockstart + 1); |
7279 | ||
7280 | /* | |
7281 | * We need to make sure there are no buffered pages in this | |
7282 | * range either, we could have raced between the invalidate in | |
7283 | * generic_file_direct_write and locking the extent. The | |
7284 | * invalidate needs to happen so that reads after a write do not | |
7285 | * get stale data. | |
7286 | */ | |
fc4adbff | 7287 | if (!ordered && |
051c98eb DS |
7288 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7289 | lockstart, lockend))) | |
eb838e73 JB |
7290 | break; |
7291 | ||
7292 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7293 | cached_state); |
eb838e73 JB |
7294 | |
7295 | if (ordered) { | |
ade77029 FM |
7296 | /* |
7297 | * If we are doing a DIO read and the ordered extent we | |
7298 | * found is for a buffered write, we can not wait for it | |
7299 | * to complete and retry, because if we do so we can | |
7300 | * deadlock with concurrent buffered writes on page | |
7301 | * locks. This happens only if our DIO read covers more | |
7302 | * than one extent map, if at this point has already | |
7303 | * created an ordered extent for a previous extent map | |
7304 | * and locked its range in the inode's io tree, and a | |
7305 | * concurrent write against that previous extent map's | |
7306 | * range and this range started (we unlock the ranges | |
7307 | * in the io tree only when the bios complete and | |
7308 | * buffered writes always lock pages before attempting | |
7309 | * to lock range in the io tree). | |
7310 | */ | |
7311 | if (writing || | |
7312 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
c0a43603 | 7313 | btrfs_start_ordered_extent(ordered, 1); |
ade77029 FM |
7314 | else |
7315 | ret = -ENOTBLK; | |
eb838e73 JB |
7316 | btrfs_put_ordered_extent(ordered); |
7317 | } else { | |
eb838e73 | 7318 | /* |
b850ae14 FM |
7319 | * We could trigger writeback for this range (and wait |
7320 | * for it to complete) and then invalidate the pages for | |
7321 | * this range (through invalidate_inode_pages2_range()), | |
7322 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7323 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7324 | * triggered a readahead) on a page lock due to an |
7325 | * ordered dio extent we created before but did not have | |
7326 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7327 | * complete), which makes readahead wait for that |
b850ae14 FM |
7328 | * ordered extent to complete while holding a lock on |
7329 | * that page. | |
eb838e73 | 7330 | */ |
b850ae14 | 7331 | ret = -ENOTBLK; |
eb838e73 JB |
7332 | } |
7333 | ||
ade77029 FM |
7334 | if (ret) |
7335 | break; | |
7336 | ||
eb838e73 JB |
7337 | cond_resched(); |
7338 | } | |
7339 | ||
7340 | return ret; | |
7341 | } | |
7342 | ||
6f9994db | 7343 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7344 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7345 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7346 | u64 block_len, u64 orig_block_len, |
7347 | u64 ram_bytes, int compress_type, | |
7348 | int type) | |
69ffb543 JB |
7349 | { |
7350 | struct extent_map_tree *em_tree; | |
7351 | struct extent_map *em; | |
69ffb543 JB |
7352 | int ret; |
7353 | ||
6f9994db LB |
7354 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7355 | type == BTRFS_ORDERED_COMPRESSED || | |
7356 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7357 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7358 | |
4b67c11d | 7359 | em_tree = &inode->extent_tree; |
69ffb543 JB |
7360 | em = alloc_extent_map(); |
7361 | if (!em) | |
7362 | return ERR_PTR(-ENOMEM); | |
7363 | ||
7364 | em->start = start; | |
7365 | em->orig_start = orig_start; | |
7366 | em->len = len; | |
7367 | em->block_len = block_len; | |
7368 | em->block_start = block_start; | |
b4939680 | 7369 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7370 | em->ram_bytes = ram_bytes; |
70c8a91c | 7371 | em->generation = -1; |
69ffb543 | 7372 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7373 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7374 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7375 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7376 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7377 | em->compress_type = compress_type; | |
7378 | } | |
69ffb543 JB |
7379 | |
7380 | do { | |
4b67c11d NB |
7381 | btrfs_drop_extent_cache(inode, em->start, |
7382 | em->start + em->len - 1, 0); | |
69ffb543 | 7383 | write_lock(&em_tree->lock); |
09a2a8f9 | 7384 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7385 | write_unlock(&em_tree->lock); |
6f9994db LB |
7386 | /* |
7387 | * The caller has taken lock_extent(), who could race with us | |
7388 | * to add em? | |
7389 | */ | |
69ffb543 JB |
7390 | } while (ret == -EEXIST); |
7391 | ||
7392 | if (ret) { | |
7393 | free_extent_map(em); | |
7394 | return ERR_PTR(ret); | |
7395 | } | |
7396 | ||
6f9994db | 7397 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7398 | return em; |
7399 | } | |
7400 | ||
1c8d0175 | 7401 | |
c5794e51 | 7402 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7403 | struct inode *inode, |
7404 | struct btrfs_dio_data *dio_data, | |
7405 | u64 start, u64 len) | |
7406 | { | |
7407 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7408 | struct extent_map *em = *map; | |
f0bfa76a FM |
7409 | int type; |
7410 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7411 | bool can_nocow = false; | |
7412 | bool space_reserved = false; | |
c5794e51 NB |
7413 | int ret = 0; |
7414 | ||
7415 | /* | |
7416 | * We don't allocate a new extent in the following cases | |
7417 | * | |
7418 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7419 | * existing extent. | |
7420 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7421 | * just use the extent. | |
7422 | * | |
7423 | */ | |
7424 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7425 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7426 | em->block_start != EXTENT_MAP_HOLE)) { | |
c5794e51 NB |
7427 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
7428 | type = BTRFS_ORDERED_PREALLOC; | |
7429 | else | |
7430 | type = BTRFS_ORDERED_NOCOW; | |
7431 | len = min(len, em->len - (start - em->start)); | |
7432 | block_start = em->block_start + (start - em->start); | |
7433 | ||
7434 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
a84d5d42 | 7435 | &orig_block_len, &ram_bytes, false) == 1 && |
f0bfa76a FM |
7436 | btrfs_inc_nocow_writers(fs_info, block_start)) |
7437 | can_nocow = true; | |
7438 | } | |
c5794e51 | 7439 | |
f0bfa76a FM |
7440 | if (can_nocow) { |
7441 | struct extent_map *em2; | |
7442 | ||
7443 | /* We can NOCOW, so only need to reserve metadata space. */ | |
7444 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len); | |
7445 | if (ret < 0) { | |
7446 | /* Our caller expects us to free the input extent map. */ | |
7447 | free_extent_map(em); | |
7448 | *map = NULL; | |
c5794e51 | 7449 | btrfs_dec_nocow_writers(fs_info, block_start); |
f0bfa76a FM |
7450 | goto out; |
7451 | } | |
7452 | space_reserved = true; | |
7453 | ||
7454 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, | |
7455 | orig_start, block_start, | |
7456 | len, orig_block_len, | |
7457 | ram_bytes, type); | |
7458 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7459 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7460 | free_extent_map(em); | |
7461 | *map = em = em2; | |
7462 | } | |
c5794e51 | 7463 | |
f0bfa76a FM |
7464 | if (IS_ERR(em2)) { |
7465 | ret = PTR_ERR(em2); | |
7466 | goto out; | |
c5794e51 | 7467 | } |
f0bfa76a FM |
7468 | } else { |
7469 | const u64 prev_len = len; | |
c5794e51 | 7470 | |
f0bfa76a FM |
7471 | /* Our caller expects us to free the input extent map. */ |
7472 | free_extent_map(em); | |
7473 | *map = NULL; | |
7474 | ||
7475 | /* We have to COW, so need to reserve metadata and data space. */ | |
7476 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), | |
7477 | &dio_data->data_reserved, | |
7478 | start, len); | |
7479 | if (ret < 0) | |
7480 | goto out; | |
7481 | space_reserved = true; | |
7482 | ||
7483 | em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); | |
7484 | if (IS_ERR(em)) { | |
7485 | ret = PTR_ERR(em); | |
7486 | goto out; | |
7487 | } | |
7488 | *map = em; | |
7489 | len = min(len, em->len - (start - em->start)); | |
7490 | if (len < prev_len) | |
7491 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7492 | dio_data->data_reserved, | |
7493 | start + len, prev_len - len, | |
7494 | true); | |
c5794e51 NB |
7495 | } |
7496 | ||
f0bfa76a FM |
7497 | /* |
7498 | * We have created our ordered extent, so we can now release our reservation | |
7499 | * for an outstanding extent. | |
7500 | */ | |
7501 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); | |
c5794e51 | 7502 | |
c5794e51 NB |
7503 | /* |
7504 | * Need to update the i_size under the extent lock so buffered | |
7505 | * readers will get the updated i_size when we unlock. | |
7506 | */ | |
f85781fb | 7507 | if (start + len > i_size_read(inode)) |
c5794e51 | 7508 | i_size_write(inode, start + len); |
c5794e51 | 7509 | out: |
f0bfa76a FM |
7510 | if (ret && space_reserved) { |
7511 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); | |
7512 | if (can_nocow) { | |
7513 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true); | |
7514 | } else { | |
7515 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7516 | dio_data->data_reserved, | |
7517 | start, len, true); | |
7518 | extent_changeset_free(dio_data->data_reserved); | |
7519 | dio_data->data_reserved = NULL; | |
7520 | } | |
7521 | } | |
c5794e51 NB |
7522 | return ret; |
7523 | } | |
7524 | ||
f85781fb GR |
7525 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7526 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7527 | struct iomap *srcmap) | |
4b46fce2 | 7528 | { |
0b246afa | 7529 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7530 | struct extent_map *em; |
eb838e73 | 7531 | struct extent_state *cached_state = NULL; |
50745b0a | 7532 | struct btrfs_dio_data *dio_data = NULL; |
eb838e73 | 7533 | u64 lockstart, lockend; |
f85781fb | 7534 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7535 | int ret = 0; |
f85781fb GR |
7536 | u64 len = length; |
7537 | bool unlock_extents = false; | |
eb838e73 | 7538 | |
f85781fb | 7539 | if (!write) |
0b246afa | 7540 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7541 | |
c329861d JB |
7542 | lockstart = start; |
7543 | lockend = start + len - 1; | |
7544 | ||
f85781fb GR |
7545 | /* |
7546 | * The generic stuff only does filemap_write_and_wait_range, which | |
7547 | * isn't enough if we've written compressed pages to this area, so we | |
7548 | * need to flush the dirty pages again to make absolutely sure that any | |
7549 | * outstanding dirty pages are on disk. | |
7550 | */ | |
7551 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7552 | &BTRFS_I(inode)->runtime_flags)) { | |
7553 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7554 | start + length - 1); | |
7555 | if (ret) | |
7556 | return ret; | |
7557 | } | |
7558 | ||
7559 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); | |
7560 | if (!dio_data) | |
7561 | return -ENOMEM; | |
7562 | ||
f85781fb GR |
7563 | iomap->private = dio_data; |
7564 | ||
e1cbbfa5 | 7565 | |
eb838e73 JB |
7566 | /* |
7567 | * If this errors out it's because we couldn't invalidate pagecache for | |
7568 | * this range and we need to fallback to buffered. | |
7569 | */ | |
f85781fb | 7570 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
9c9464cc FM |
7571 | ret = -ENOTBLK; |
7572 | goto err; | |
7573 | } | |
eb838e73 | 7574 | |
39b07b5d | 7575 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7576 | if (IS_ERR(em)) { |
7577 | ret = PTR_ERR(em); | |
7578 | goto unlock_err; | |
7579 | } | |
4b46fce2 JB |
7580 | |
7581 | /* | |
7582 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7583 | * io. INLINE is special, and we could probably kludge it in here, but | |
7584 | * it's still buffered so for safety lets just fall back to the generic | |
7585 | * buffered path. | |
7586 | * | |
7587 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7588 | * decompress it, so there will be buffering required no matter what we | |
7589 | * do, so go ahead and fallback to buffered. | |
7590 | * | |
01327610 | 7591 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7592 | * to buffered IO. Don't blame me, this is the price we pay for using |
7593 | * the generic code. | |
7594 | */ | |
7595 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7596 | em->block_start == EXTENT_MAP_INLINE) { | |
7597 | free_extent_map(em); | |
eb838e73 JB |
7598 | ret = -ENOTBLK; |
7599 | goto unlock_err; | |
4b46fce2 JB |
7600 | } |
7601 | ||
f85781fb GR |
7602 | len = min(len, em->len - (start - em->start)); |
7603 | if (write) { | |
7604 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
7605 | start, len); | |
c5794e51 NB |
7606 | if (ret < 0) |
7607 | goto unlock_err; | |
f85781fb GR |
7608 | unlock_extents = true; |
7609 | /* Recalc len in case the new em is smaller than requested */ | |
7610 | len = min(len, em->len - (start - em->start)); | |
c5794e51 | 7611 | } else { |
1c8d0175 NB |
7612 | /* |
7613 | * We need to unlock only the end area that we aren't using. | |
7614 | * The rest is going to be unlocked by the endio routine. | |
7615 | */ | |
f85781fb GR |
7616 | lockstart = start + len; |
7617 | if (lockstart < lockend) | |
7618 | unlock_extents = true; | |
7619 | } | |
7620 | ||
7621 | if (unlock_extents) | |
7622 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
7623 | lockstart, lockend, &cached_state); | |
7624 | else | |
7625 | free_extent_state(cached_state); | |
7626 | ||
7627 | /* | |
7628 | * Translate extent map information to iomap. | |
7629 | * We trim the extents (and move the addr) even though iomap code does | |
7630 | * that, since we have locked only the parts we are performing I/O in. | |
7631 | */ | |
7632 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
7633 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { | |
7634 | iomap->addr = IOMAP_NULL_ADDR; | |
7635 | iomap->type = IOMAP_HOLE; | |
7636 | } else { | |
7637 | iomap->addr = em->block_start + (start - em->start); | |
7638 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7639 | } |
f85781fb | 7640 | iomap->offset = start; |
d24fa5c1 | 7641 | iomap->bdev = fs_info->fs_devices->latest_dev->bdev; |
f85781fb | 7642 | iomap->length = len; |
a43a67a2 | 7643 | |
e380adfc | 7644 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start)) |
544d24f9 NA |
7645 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
7646 | ||
4b46fce2 JB |
7647 | free_extent_map(em); |
7648 | ||
7649 | return 0; | |
eb838e73 JB |
7650 | |
7651 | unlock_err: | |
e182163d OS |
7652 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7653 | &cached_state); | |
9c9464cc | 7654 | err: |
f0bfa76a FM |
7655 | kfree(dio_data); |
7656 | ||
f85781fb GR |
7657 | return ret; |
7658 | } | |
7659 | ||
7660 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7661 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7662 | { | |
7663 | int ret = 0; | |
7664 | struct btrfs_dio_data *dio_data = iomap->private; | |
7665 | size_t submitted = dio_data->submitted; | |
7666 | const bool write = !!(flags & IOMAP_WRITE); | |
7667 | ||
7668 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7669 | /* If reading from a hole, unlock and return */ | |
7670 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); | |
7671 | goto out; | |
7672 | } | |
7673 | ||
7674 | if (submitted < length) { | |
7675 | pos += submitted; | |
7676 | length -= submitted; | |
7677 | if (write) | |
7678 | __endio_write_update_ordered(BTRFS_I(inode), pos, | |
7679 | length, false); | |
7680 | else | |
7681 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
7682 | pos + length - 1); | |
7683 | ret = -ENOTBLK; | |
7684 | } | |
7685 | ||
f0bfa76a | 7686 | if (write) |
f85781fb | 7687 | extent_changeset_free(dio_data->data_reserved); |
f85781fb GR |
7688 | out: |
7689 | kfree(dio_data); | |
7690 | iomap->private = NULL; | |
7691 | ||
8b110e39 MX |
7692 | return ret; |
7693 | } | |
7694 | ||
769b4f24 | 7695 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
8b110e39 | 7696 | { |
769b4f24 OS |
7697 | /* |
7698 | * This implies a barrier so that stores to dio_bio->bi_status before | |
7699 | * this and loads of dio_bio->bi_status after this are fully ordered. | |
7700 | */ | |
7701 | if (!refcount_dec_and_test(&dip->refs)) | |
7702 | return; | |
8b110e39 | 7703 | |
cfe94440 | 7704 | if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { |
b672b5c1 | 7705 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
47926ab5 | 7706 | dip->file_offset, |
769b4f24 OS |
7707 | dip->bytes, |
7708 | !dip->dio_bio->bi_status); | |
7709 | } else { | |
7710 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, | |
47926ab5 QW |
7711 | dip->file_offset, |
7712 | dip->file_offset + dip->bytes - 1); | |
8b110e39 MX |
7713 | } |
7714 | ||
f85781fb | 7715 | bio_endio(dip->dio_bio); |
769b4f24 | 7716 | kfree(dip); |
8b110e39 MX |
7717 | } |
7718 | ||
77d5d689 OS |
7719 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7720 | int mirror_num, | |
7721 | unsigned long bio_flags) | |
8b110e39 | 7722 | { |
77d5d689 | 7723 | struct btrfs_dio_private *dip = bio->bi_private; |
2ff7e61e | 7724 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7725 | blk_status_t ret; |
8b110e39 | 7726 | |
37226b21 | 7727 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7728 | |
5c047a69 | 7729 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
8b110e39 | 7730 | if (ret) |
ea057f6d | 7731 | return ret; |
8b110e39 | 7732 | |
77d5d689 | 7733 | refcount_inc(&dip->refs); |
08635bae | 7734 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
8b110e39 | 7735 | if (ret) |
fd9d6670 | 7736 | refcount_dec(&dip->refs); |
77d5d689 | 7737 | return ret; |
8b110e39 MX |
7738 | } |
7739 | ||
f4f39fc5 | 7740 | static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip, |
c3a3b19b | 7741 | struct btrfs_bio *bbio, |
fd9d6670 | 7742 | const bool uptodate) |
4b46fce2 | 7743 | { |
f4f39fc5 | 7744 | struct inode *inode = dip->inode; |
fd9d6670 OS |
7745 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
7746 | const u32 sectorsize = fs_info->sectorsize; | |
7747 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
7748 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
7749 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); | |
17347cec LB |
7750 | struct bio_vec bvec; |
7751 | struct bvec_iter iter; | |
f4f39fc5 QW |
7752 | const u64 orig_file_offset = dip->file_offset; |
7753 | u64 start = orig_file_offset; | |
7ffd27e3 | 7754 | u32 bio_offset = 0; |
58efbc9f | 7755 | blk_status_t err = BLK_STS_OK; |
4b46fce2 | 7756 | |
c3a3b19b | 7757 | __bio_for_each_segment(bvec, &bbio->bio, iter, bbio->iter) { |
fd9d6670 | 7758 | unsigned int i, nr_sectors, pgoff; |
8b110e39 | 7759 | |
17347cec LB |
7760 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
7761 | pgoff = bvec.bv_offset; | |
fd9d6670 | 7762 | for (i = 0; i < nr_sectors; i++) { |
97bf5a55 | 7763 | ASSERT(pgoff < PAGE_SIZE); |
fd9d6670 | 7764 | if (uptodate && |
c3a3b19b | 7765 | (!csum || !check_data_csum(inode, bbio, |
c1d6abda OS |
7766 | bio_offset, bvec.bv_page, |
7767 | pgoff, start))) { | |
fd9d6670 OS |
7768 | clean_io_failure(fs_info, failure_tree, io_tree, |
7769 | start, bvec.bv_page, | |
7770 | btrfs_ino(BTRFS_I(inode)), | |
7771 | pgoff); | |
7772 | } else { | |
150e4b05 | 7773 | int ret; |
fd9d6670 | 7774 | |
f4f39fc5 | 7775 | ASSERT((start - orig_file_offset) < UINT_MAX); |
150e4b05 | 7776 | ret = btrfs_repair_one_sector(inode, |
c3a3b19b | 7777 | &bbio->bio, |
f4f39fc5 | 7778 | start - orig_file_offset, |
150e4b05 | 7779 | bvec.bv_page, pgoff, |
c3a3b19b | 7780 | start, bbio->mirror_num, |
150e4b05 QW |
7781 | submit_dio_repair_bio); |
7782 | if (ret) | |
7783 | err = errno_to_blk_status(ret); | |
fd9d6670 OS |
7784 | } |
7785 | start += sectorsize; | |
7ffd27e3 QW |
7786 | ASSERT(bio_offset + sectorsize > bio_offset); |
7787 | bio_offset += sectorsize; | |
2dabb324 | 7788 | pgoff += sectorsize; |
2dabb324 | 7789 | } |
2c30c71b | 7790 | } |
c1dc0896 MX |
7791 | return err; |
7792 | } | |
7793 | ||
b672b5c1 | 7794 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
7795 | const u64 offset, const u64 bytes, |
7796 | const bool uptodate) | |
4b46fce2 | 7797 | { |
e65f152e QW |
7798 | btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, |
7799 | finish_ordered_fn, uptodate); | |
14543774 FM |
7800 | } |
7801 | ||
8896a08d | 7802 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
1941b64b QW |
7803 | struct bio *bio, |
7804 | u64 dio_file_offset) | |
eaf25d93 | 7805 | { |
1941b64b | 7806 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1); |
eaf25d93 CM |
7807 | } |
7808 | ||
4246a0b6 | 7809 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
7810 | { |
7811 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 7812 | blk_status_t err = bio->bi_status; |
e65e1535 | 7813 | |
8b110e39 MX |
7814 | if (err) |
7815 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 7816 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 | 7817 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
1201b58b | 7818 | bio->bi_opf, bio->bi_iter.bi_sector, |
8b110e39 MX |
7819 | bio->bi_iter.bi_size, err); |
7820 | ||
f4f39fc5 QW |
7821 | if (bio_op(bio) == REQ_OP_READ) |
7822 | err = btrfs_check_read_dio_bio(dip, btrfs_bio(bio), !err); | |
e65e1535 | 7823 | |
769b4f24 OS |
7824 | if (err) |
7825 | dip->dio_bio->bi_status = err; | |
e65e1535 | 7826 | |
47926ab5 | 7827 | btrfs_record_physical_zoned(dip->inode, dip->file_offset, bio); |
544d24f9 | 7828 | |
e65e1535 | 7829 | bio_put(bio); |
769b4f24 | 7830 | btrfs_dio_private_put(dip); |
c1dc0896 MX |
7831 | } |
7832 | ||
d0ee3934 DS |
7833 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
7834 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 7835 | { |
0b246afa | 7836 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 7837 | struct btrfs_dio_private *dip = bio->bi_private; |
cfe94440 | 7838 | bool write = btrfs_op(bio) == BTRFS_MAP_WRITE; |
4e4cbee9 | 7839 | blk_status_t ret; |
e65e1535 | 7840 | |
4c274bc6 | 7841 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
7842 | if (async_submit) |
7843 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
7844 | ||
5fd02043 | 7845 | if (!write) { |
0b246afa | 7846 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
7847 | if (ret) |
7848 | goto err; | |
7849 | } | |
e65e1535 | 7850 | |
e6961cac | 7851 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
7852 | goto map; |
7853 | ||
7854 | if (write && async_submit) { | |
1941b64b | 7855 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset, |
e288c080 | 7856 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 7857 | goto err; |
1ae39938 JB |
7858 | } else if (write) { |
7859 | /* | |
7860 | * If we aren't doing async submit, calculate the csum of the | |
7861 | * bio now. | |
7862 | */ | |
bd242a08 | 7863 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
1ae39938 JB |
7864 | if (ret) |
7865 | goto err; | |
23ea8e5a | 7866 | } else { |
85879573 OS |
7867 | u64 csum_offset; |
7868 | ||
47926ab5 | 7869 | csum_offset = file_offset - dip->file_offset; |
265fdfa6 | 7870 | csum_offset >>= fs_info->sectorsize_bits; |
55fc29be | 7871 | csum_offset *= fs_info->csum_size; |
c3a3b19b | 7872 | btrfs_bio(bio)->csum = dip->csums + csum_offset; |
c2db1073 | 7873 | } |
1ae39938 | 7874 | map: |
08635bae | 7875 | ret = btrfs_map_bio(fs_info, bio, 0); |
e65e1535 | 7876 | err: |
e65e1535 MX |
7877 | return ret; |
7878 | } | |
7879 | ||
c36cac28 OS |
7880 | /* |
7881 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked | |
7882 | * or ordered extents whether or not we submit any bios. | |
7883 | */ | |
7884 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, | |
7885 | struct inode *inode, | |
7886 | loff_t file_offset) | |
e65e1535 | 7887 | { |
cfe94440 | 7888 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
85879573 OS |
7889 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
7890 | size_t dip_size; | |
c36cac28 | 7891 | struct btrfs_dio_private *dip; |
c36cac28 | 7892 | |
85879573 OS |
7893 | dip_size = sizeof(*dip); |
7894 | if (!write && csum) { | |
7895 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
85879573 OS |
7896 | size_t nblocks; |
7897 | ||
265fdfa6 | 7898 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
223486c2 | 7899 | dip_size += fs_info->csum_size * nblocks; |
85879573 OS |
7900 | } |
7901 | ||
7902 | dip = kzalloc(dip_size, GFP_NOFS); | |
c36cac28 OS |
7903 | if (!dip) |
7904 | return NULL; | |
7905 | ||
c36cac28 | 7906 | dip->inode = inode; |
47926ab5 | 7907 | dip->file_offset = file_offset; |
c36cac28 | 7908 | dip->bytes = dio_bio->bi_iter.bi_size; |
1201b58b | 7909 | dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9; |
c36cac28 | 7910 | dip->dio_bio = dio_bio; |
e3b318d1 | 7911 | refcount_set(&dip->refs, 1); |
c36cac28 OS |
7912 | return dip; |
7913 | } | |
7914 | ||
3e08773c | 7915 | static void btrfs_submit_direct(const struct iomap_iter *iter, |
f85781fb | 7916 | struct bio *dio_bio, loff_t file_offset) |
c36cac28 | 7917 | { |
a6d3d495 | 7918 | struct inode *inode = iter->inode; |
cfe94440 | 7919 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
0b246afa | 7920 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
769b4f24 OS |
7921 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
7922 | BTRFS_BLOCK_GROUP_RAID56_MASK); | |
c36cac28 | 7923 | struct btrfs_dio_private *dip; |
e65e1535 | 7924 | struct bio *bio; |
c36cac28 | 7925 | u64 start_sector; |
1ae39938 | 7926 | int async_submit = 0; |
725130ba | 7927 | u64 submit_len; |
42b5d73b NA |
7928 | u64 clone_offset = 0; |
7929 | u64 clone_len; | |
42034313 | 7930 | u64 logical; |
5f4dc8fc | 7931 | int ret; |
58efbc9f | 7932 | blk_status_t status; |
89b798ad | 7933 | struct btrfs_io_geometry geom; |
a6d3d495 | 7934 | struct btrfs_dio_data *dio_data = iter->iomap.private; |
42034313 | 7935 | struct extent_map *em = NULL; |
e65e1535 | 7936 | |
c36cac28 OS |
7937 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
7938 | if (!dip) { | |
7939 | if (!write) { | |
7940 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
7941 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
7942 | } | |
7943 | dio_bio->bi_status = BLK_STS_RESOURCE; | |
f85781fb | 7944 | bio_endio(dio_bio); |
3e08773c | 7945 | return; |
c36cac28 | 7946 | } |
facc8a22 | 7947 | |
334c16d8 | 7948 | if (!write) { |
85879573 OS |
7949 | /* |
7950 | * Load the csums up front to reduce csum tree searches and | |
7951 | * contention when submitting bios. | |
334c16d8 JB |
7952 | * |
7953 | * If we have csums disabled this will do nothing. | |
85879573 | 7954 | */ |
6275193e | 7955 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
85879573 OS |
7956 | if (status != BLK_STS_OK) |
7957 | goto out_err; | |
02f57c7a JB |
7958 | } |
7959 | ||
769b4f24 OS |
7960 | start_sector = dio_bio->bi_iter.bi_sector; |
7961 | submit_len = dio_bio->bi_iter.bi_size; | |
53b381b3 | 7962 | |
3c91ee69 | 7963 | do { |
42034313 MR |
7964 | logical = start_sector << 9; |
7965 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); | |
7966 | if (IS_ERR(em)) { | |
7967 | status = errno_to_blk_status(PTR_ERR(em)); | |
7968 | em = NULL; | |
7969 | goto out_err_em; | |
7970 | } | |
7971 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), | |
43c0d1a5 | 7972 | logical, &geom); |
769b4f24 OS |
7973 | if (ret) { |
7974 | status = errno_to_blk_status(ret); | |
42034313 | 7975 | goto out_err_em; |
769b4f24 | 7976 | } |
769b4f24 | 7977 | |
42b5d73b NA |
7978 | clone_len = min(submit_len, geom.len); |
7979 | ASSERT(clone_len <= UINT_MAX); | |
02f57c7a | 7980 | |
725130ba LB |
7981 | /* |
7982 | * This will never fail as it's passing GPF_NOFS and | |
7983 | * the allocation is backed by btrfs_bioset. | |
7984 | */ | |
769b4f24 | 7985 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
725130ba LB |
7986 | bio->bi_private = dip; |
7987 | bio->bi_end_io = btrfs_end_dio_bio; | |
725130ba | 7988 | |
544d24f9 NA |
7989 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
7990 | status = extract_ordered_extent(BTRFS_I(inode), bio, | |
7991 | file_offset); | |
7992 | if (status) { | |
7993 | bio_put(bio); | |
7994 | goto out_err; | |
7995 | } | |
7996 | } | |
7997 | ||
725130ba LB |
7998 | ASSERT(submit_len >= clone_len); |
7999 | submit_len -= clone_len; | |
e65e1535 | 8000 | |
725130ba LB |
8001 | /* |
8002 | * Increase the count before we submit the bio so we know | |
8003 | * the end IO handler won't happen before we increase the | |
8004 | * count. Otherwise, the dip might get freed before we're | |
8005 | * done setting it up. | |
769b4f24 OS |
8006 | * |
8007 | * We transfer the initial reference to the last bio, so we | |
8008 | * don't need to increment the reference count for the last one. | |
725130ba | 8009 | */ |
769b4f24 OS |
8010 | if (submit_len > 0) { |
8011 | refcount_inc(&dip->refs); | |
8012 | /* | |
8013 | * If we are submitting more than one bio, submit them | |
8014 | * all asynchronously. The exception is RAID 5 or 6, as | |
8015 | * asynchronous checksums make it difficult to collect | |
8016 | * full stripe writes. | |
8017 | */ | |
8018 | if (!raid56) | |
8019 | async_submit = 1; | |
8020 | } | |
e65e1535 | 8021 | |
d0ee3934 | 8022 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
8023 | async_submit); |
8024 | if (status) { | |
725130ba | 8025 | bio_put(bio); |
769b4f24 OS |
8026 | if (submit_len > 0) |
8027 | refcount_dec(&dip->refs); | |
42034313 | 8028 | goto out_err_em; |
725130ba | 8029 | } |
e65e1535 | 8030 | |
f85781fb | 8031 | dio_data->submitted += clone_len; |
725130ba LB |
8032 | clone_offset += clone_len; |
8033 | start_sector += clone_len >> 9; | |
8034 | file_offset += clone_len; | |
42034313 MR |
8035 | |
8036 | free_extent_map(em); | |
3c91ee69 | 8037 | } while (submit_len > 0); |
3e08773c | 8038 | return; |
e65e1535 | 8039 | |
42034313 MR |
8040 | out_err_em: |
8041 | free_extent_map(em); | |
e65e1535 | 8042 | out_err: |
769b4f24 OS |
8043 | dip->dio_bio->bi_status = status; |
8044 | btrfs_dio_private_put(dip); | |
4b46fce2 JB |
8045 | } |
8046 | ||
4e4cabec | 8047 | const struct iomap_ops btrfs_dio_iomap_ops = { |
f85781fb GR |
8048 | .iomap_begin = btrfs_dio_iomap_begin, |
8049 | .iomap_end = btrfs_dio_iomap_end, | |
8050 | }; | |
8051 | ||
4e4cabec | 8052 | const struct iomap_dio_ops btrfs_dio_ops = { |
f85781fb GR |
8053 | .submit_io = btrfs_submit_direct, |
8054 | }; | |
8055 | ||
1506fcc8 | 8056 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 8057 | u64 start, u64 len) |
1506fcc8 | 8058 | { |
05dadc09 TI |
8059 | int ret; |
8060 | ||
45dd052e | 8061 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
8062 | if (ret) |
8063 | return ret; | |
8064 | ||
facee0a0 | 8065 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
1506fcc8 YS |
8066 | } |
8067 | ||
a52d9a80 | 8068 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8069 | { |
0f208812 NB |
8070 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
8071 | u64 start = page_offset(page); | |
8072 | u64 end = start + PAGE_SIZE - 1; | |
390ed29b | 8073 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
c1be9c1a NB |
8074 | int ret; |
8075 | ||
0f208812 NB |
8076 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
8077 | ||
390ed29b QW |
8078 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); |
8079 | if (bio_ctrl.bio) | |
8080 | ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags); | |
c1be9c1a | 8081 | return ret; |
9ebefb18 | 8082 | } |
1832a6d5 | 8083 | |
a52d9a80 | 8084 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8085 | { |
be7bd730 JB |
8086 | struct inode *inode = page->mapping->host; |
8087 | int ret; | |
b888db2b CM |
8088 | |
8089 | if (current->flags & PF_MEMALLOC) { | |
8090 | redirty_page_for_writepage(wbc, page); | |
8091 | unlock_page(page); | |
8092 | return 0; | |
8093 | } | |
be7bd730 JB |
8094 | |
8095 | /* | |
8096 | * If we are under memory pressure we will call this directly from the | |
8097 | * VM, we need to make sure we have the inode referenced for the ordered | |
8098 | * extent. If not just return like we didn't do anything. | |
8099 | */ | |
8100 | if (!igrab(inode)) { | |
8101 | redirty_page_for_writepage(wbc, page); | |
8102 | return AOP_WRITEPAGE_ACTIVATE; | |
8103 | } | |
0a9b0e53 | 8104 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8105 | btrfs_add_delayed_iput(inode); |
8106 | return ret; | |
9ebefb18 CM |
8107 | } |
8108 | ||
48a3b636 ES |
8109 | static int btrfs_writepages(struct address_space *mapping, |
8110 | struct writeback_control *wbc) | |
b293f02e | 8111 | { |
8ae225a8 | 8112 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8113 | } |
8114 | ||
ba206a02 | 8115 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 8116 | { |
ba206a02 | 8117 | extent_readahead(rac); |
3ab2fb5a | 8118 | } |
2a3ff0ad | 8119 | |
7c11d0ae QW |
8120 | /* |
8121 | * For releasepage() and invalidatepage() we have a race window where | |
8122 | * end_page_writeback() is called but the subpage spinlock is not yet released. | |
8123 | * If we continue to release/invalidate the page, we could cause use-after-free | |
8124 | * for subpage spinlock. So this function is to spin and wait for subpage | |
8125 | * spinlock. | |
8126 | */ | |
8127 | static void wait_subpage_spinlock(struct page *page) | |
8128 | { | |
8129 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
8130 | struct btrfs_subpage *subpage; | |
8131 | ||
8132 | if (fs_info->sectorsize == PAGE_SIZE) | |
8133 | return; | |
8134 | ||
8135 | ASSERT(PagePrivate(page) && page->private); | |
8136 | subpage = (struct btrfs_subpage *)page->private; | |
8137 | ||
8138 | /* | |
8139 | * This may look insane as we just acquire the spinlock and release it, | |
8140 | * without doing anything. But we just want to make sure no one is | |
8141 | * still holding the subpage spinlock. | |
8142 | * And since the page is not dirty nor writeback, and we have page | |
8143 | * locked, the only possible way to hold a spinlock is from the endio | |
8144 | * function to clear page writeback. | |
8145 | * | |
8146 | * Here we just acquire the spinlock so that all existing callers | |
8147 | * should exit and we're safe to release/invalidate the page. | |
8148 | */ | |
8149 | spin_lock_irq(&subpage->lock); | |
8150 | spin_unlock_irq(&subpage->lock); | |
8151 | } | |
8152 | ||
e6dcd2dc | 8153 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8154 | { |
477a30ba | 8155 | int ret = try_release_extent_mapping(page, gfp_flags); |
7c11d0ae QW |
8156 | |
8157 | if (ret == 1) { | |
8158 | wait_subpage_spinlock(page); | |
32443de3 | 8159 | clear_page_extent_mapped(page); |
7c11d0ae | 8160 | } |
a52d9a80 | 8161 | return ret; |
39279cc3 CM |
8162 | } |
8163 | ||
e6dcd2dc CM |
8164 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8165 | { | |
98509cfc CM |
8166 | if (PageWriteback(page) || PageDirty(page)) |
8167 | return 0; | |
3ba7ab22 | 8168 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8169 | } |
8170 | ||
f8e66081 RG |
8171 | #ifdef CONFIG_MIGRATION |
8172 | static int btrfs_migratepage(struct address_space *mapping, | |
8173 | struct page *newpage, struct page *page, | |
8174 | enum migrate_mode mode) | |
8175 | { | |
8176 | int ret; | |
8177 | ||
8178 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); | |
8179 | if (ret != MIGRATEPAGE_SUCCESS) | |
8180 | return ret; | |
8181 | ||
d1b89bc0 GJ |
8182 | if (page_has_private(page)) |
8183 | attach_page_private(newpage, detach_page_private(page)); | |
f8e66081 | 8184 | |
f57ad937 QW |
8185 | if (PageOrdered(page)) { |
8186 | ClearPageOrdered(page); | |
8187 | SetPageOrdered(newpage); | |
f8e66081 RG |
8188 | } |
8189 | ||
8190 | if (mode != MIGRATE_SYNC_NO_COPY) | |
8191 | migrate_page_copy(newpage, page); | |
8192 | else | |
8193 | migrate_page_states(newpage, page); | |
8194 | return MIGRATEPAGE_SUCCESS; | |
8195 | } | |
8196 | #endif | |
8197 | ||
d47992f8 LC |
8198 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8199 | unsigned int length) | |
39279cc3 | 8200 | { |
53ac7ead | 8201 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
b945a463 | 8202 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
53ac7ead | 8203 | struct extent_io_tree *tree = &inode->io_tree; |
2ac55d41 | 8204 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8205 | u64 page_start = page_offset(page); |
09cbfeaf | 8206 | u64 page_end = page_start + PAGE_SIZE - 1; |
3b835840 | 8207 | u64 cur; |
53ac7ead | 8208 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 8209 | |
8b62b72b | 8210 | /* |
266a2586 QW |
8211 | * We have page locked so no new ordered extent can be created on this |
8212 | * page, nor bio can be submitted for this page. | |
8b62b72b | 8213 | * |
266a2586 | 8214 | * But already submitted bio can still be finished on this page. |
f57ad937 QW |
8215 | * Furthermore, endio function won't skip page which has Ordered |
8216 | * (Private2) already cleared, so it's possible for endio and | |
8217 | * invalidatepage to do the same ordered extent accounting twice | |
8218 | * on one page. | |
266a2586 QW |
8219 | * |
8220 | * So here we wait for any submitted bios to finish, so that we won't | |
8221 | * do double ordered extent accounting on the same page. | |
8b62b72b | 8222 | */ |
e6dcd2dc | 8223 | wait_on_page_writeback(page); |
7c11d0ae | 8224 | wait_subpage_spinlock(page); |
8b62b72b | 8225 | |
bcd77455 QW |
8226 | /* |
8227 | * For subpage case, we have call sites like | |
8228 | * btrfs_punch_hole_lock_range() which passes range not aligned to | |
8229 | * sectorsize. | |
8230 | * If the range doesn't cover the full page, we don't need to and | |
8231 | * shouldn't clear page extent mapped, as page->private can still | |
8232 | * record subpage dirty bits for other part of the range. | |
8233 | * | |
8234 | * For cases that can invalidate the full even the range doesn't | |
8235 | * cover the full page, like invalidating the last page, we're | |
8236 | * still safe to wait for ordered extent to finish. | |
8237 | */ | |
8238 | if (!(offset == 0 && length == PAGE_SIZE)) { | |
e6dcd2dc CM |
8239 | btrfs_releasepage(page, GFP_NOFS); |
8240 | return; | |
8241 | } | |
131e404a FDBM |
8242 | |
8243 | if (!inode_evicting) | |
ff13db41 | 8244 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
951c80f8 | 8245 | |
3b835840 QW |
8246 | cur = page_start; |
8247 | while (cur < page_end) { | |
8248 | struct btrfs_ordered_extent *ordered; | |
8249 | bool delete_states; | |
8250 | u64 range_end; | |
b945a463 | 8251 | u32 range_len; |
3b835840 QW |
8252 | |
8253 | ordered = btrfs_lookup_first_ordered_range(inode, cur, | |
8254 | page_end + 1 - cur); | |
8255 | if (!ordered) { | |
8256 | range_end = page_end; | |
8257 | /* | |
8258 | * No ordered extent covering this range, we are safe | |
8259 | * to delete all extent states in the range. | |
8260 | */ | |
8261 | delete_states = true; | |
8262 | goto next; | |
8263 | } | |
8264 | if (ordered->file_offset > cur) { | |
8265 | /* | |
8266 | * There is a range between [cur, oe->file_offset) not | |
8267 | * covered by any ordered extent. | |
8268 | * We are safe to delete all extent states, and handle | |
8269 | * the ordered extent in the next iteration. | |
8270 | */ | |
8271 | range_end = ordered->file_offset - 1; | |
8272 | delete_states = true; | |
8273 | goto next; | |
8274 | } | |
8275 | ||
8276 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, | |
8277 | page_end); | |
b945a463 QW |
8278 | ASSERT(range_end + 1 - cur < U32_MAX); |
8279 | range_len = range_end + 1 - cur; | |
8280 | if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) { | |
3b835840 | 8281 | /* |
f57ad937 QW |
8282 | * If Ordered (Private2) is cleared, it means endio has |
8283 | * already been executed for the range. | |
3b835840 QW |
8284 | * We can't delete the extent states as |
8285 | * btrfs_finish_ordered_io() may still use some of them. | |
8286 | */ | |
8287 | delete_states = false; | |
8288 | goto next; | |
8289 | } | |
b945a463 | 8290 | btrfs_page_clear_ordered(fs_info, page, cur, range_len); |
3b835840 | 8291 | |
eb84ae03 | 8292 | /* |
2766ff61 FM |
8293 | * IO on this page will never be started, so we need to account |
8294 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW | |
8295 | * here, must leave that up for the ordered extent completion. | |
3b835840 QW |
8296 | * |
8297 | * This will also unlock the range for incoming | |
8298 | * btrfs_finish_ordered_io(). | |
eb84ae03 | 8299 | */ |
131e404a | 8300 | if (!inode_evicting) |
3b835840 | 8301 | clear_extent_bit(tree, cur, range_end, |
2766ff61 | 8302 | EXTENT_DELALLOC | |
131e404a | 8303 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8304 | EXTENT_DEFRAG, 1, 0, &cached_state); |
3b835840 QW |
8305 | |
8306 | spin_lock_irq(&inode->ordered_tree.lock); | |
8307 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8308 | ordered->truncated_len = min(ordered->truncated_len, | |
8309 | cur - ordered->file_offset); | |
8310 | spin_unlock_irq(&inode->ordered_tree.lock); | |
8311 | ||
8312 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
f41b6ba9 | 8313 | cur, range_end + 1 - cur)) { |
3b835840 QW |
8314 | btrfs_finish_ordered_io(ordered); |
8315 | /* | |
8316 | * The ordered extent has finished, now we're again | |
8317 | * safe to delete all extent states of the range. | |
8318 | */ | |
8319 | delete_states = true; | |
8320 | } else { | |
8321 | /* | |
8322 | * btrfs_finish_ordered_io() will get executed by endio | |
8323 | * of other pages, thus we can't delete extent states | |
8324 | * anymore | |
8325 | */ | |
8326 | delete_states = false; | |
8327 | } | |
8328 | next: | |
8329 | if (ordered) | |
8330 | btrfs_put_ordered_extent(ordered); | |
8b62b72b | 8331 | /* |
3b835840 QW |
8332 | * Qgroup reserved space handler |
8333 | * Sector(s) here will be either: | |
266a2586 | 8334 | * |
3b835840 QW |
8335 | * 1) Already written to disk or bio already finished |
8336 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. | |
8337 | * Qgroup will be handled by its qgroup_record then. | |
8338 | * btrfs_qgroup_free_data() call will do nothing here. | |
8339 | * | |
8340 | * 2) Not written to disk yet | |
8341 | * Then btrfs_qgroup_free_data() call will clear the | |
8342 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup | |
8343 | * reserved data space. | |
8344 | * Since the IO will never happen for this page. | |
8b62b72b | 8345 | */ |
3b835840 | 8346 | btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur); |
131e404a | 8347 | if (!inode_evicting) { |
3b835840 QW |
8348 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
8349 | EXTENT_DELALLOC | EXTENT_UPTODATE | | |
8350 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, | |
8351 | delete_states, &cached_state); | |
131e404a | 8352 | } |
3b835840 | 8353 | cur = range_end + 1; |
131e404a | 8354 | } |
b9d0b389 | 8355 | /* |
3b835840 | 8356 | * We have iterated through all ordered extents of the page, the page |
f57ad937 QW |
8357 | * should not have Ordered (Private2) anymore, or the above iteration |
8358 | * did something wrong. | |
b9d0b389 | 8359 | */ |
f57ad937 | 8360 | ASSERT(!PageOrdered(page)); |
e4f94347 | 8361 | btrfs_page_clear_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
3b835840 | 8362 | if (!inode_evicting) |
131e404a | 8363 | __btrfs_releasepage(page, GFP_NOFS); |
32443de3 | 8364 | clear_page_extent_mapped(page); |
39279cc3 CM |
8365 | } |
8366 | ||
9ebefb18 CM |
8367 | /* |
8368 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8369 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8370 | * be careful to check for EOF conditions here. We set the page up correctly | |
8371 | * for a written page which means we get ENOSPC checking when writing into | |
8372 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8373 | * support these features. | |
8374 | * | |
8375 | * We are not allowed to take the i_mutex here so we have to play games to | |
8376 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8377 | * truncate_setsize() writes the inode size before removing pages, once we have |
8378 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8379 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8380 | * unlock the page. | |
8381 | */ | |
a528a241 | 8382 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8383 | { |
c2ec175c | 8384 | struct page *page = vmf->page; |
11bac800 | 8385 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8386 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8387 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8388 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8389 | struct extent_state *cached_state = NULL; |
364ecf36 | 8390 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 8391 | unsigned long zero_start; |
9ebefb18 | 8392 | loff_t size; |
a528a241 SJ |
8393 | vm_fault_t ret; |
8394 | int ret2; | |
9998eb70 | 8395 | int reserved = 0; |
d0b7da88 | 8396 | u64 reserved_space; |
a52d9a80 | 8397 | u64 page_start; |
e6dcd2dc | 8398 | u64 page_end; |
d0b7da88 CR |
8399 | u64 end; |
8400 | ||
09cbfeaf | 8401 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8402 | |
b2b5ef5c | 8403 | sb_start_pagefault(inode->i_sb); |
df480633 | 8404 | page_start = page_offset(page); |
09cbfeaf | 8405 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8406 | end = page_end; |
df480633 | 8407 | |
d0b7da88 CR |
8408 | /* |
8409 | * Reserving delalloc space after obtaining the page lock can lead to | |
8410 | * deadlock. For example, if a dirty page is locked by this function | |
8411 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8412 | * dirty page write out, then the btrfs_writepage() function could | |
8413 | * end up waiting indefinitely to get a lock on the page currently | |
8414 | * being processed by btrfs_page_mkwrite() function. | |
8415 | */ | |
e5b7231e NB |
8416 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8417 | page_start, reserved_space); | |
a528a241 SJ |
8418 | if (!ret2) { |
8419 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8420 | reserved = 1; |
8421 | } | |
a528a241 SJ |
8422 | if (ret2) { |
8423 | ret = vmf_error(ret2); | |
9998eb70 CM |
8424 | if (reserved) |
8425 | goto out; | |
8426 | goto out_noreserve; | |
56a76f82 | 8427 | } |
1832a6d5 | 8428 | |
56a76f82 | 8429 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8430 | again: |
8318ba79 | 8431 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8432 | lock_page(page); |
9ebefb18 | 8433 | size = i_size_read(inode); |
a52d9a80 | 8434 | |
9ebefb18 | 8435 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8436 | (page_start >= size)) { |
9ebefb18 CM |
8437 | /* page got truncated out from underneath us */ |
8438 | goto out_unlock; | |
8439 | } | |
e6dcd2dc CM |
8440 | wait_on_page_writeback(page); |
8441 | ||
ff13db41 | 8442 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8443 | ret2 = set_page_extent_mapped(page); |
8444 | if (ret2 < 0) { | |
8445 | ret = vmf_error(ret2); | |
8446 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); | |
8447 | goto out_unlock; | |
8448 | } | |
e6dcd2dc | 8449 | |
eb84ae03 CM |
8450 | /* |
8451 | * we can't set the delalloc bits if there are pending ordered | |
8452 | * extents. Drop our locks and wait for them to finish | |
8453 | */ | |
a776c6fa NB |
8454 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8455 | PAGE_SIZE); | |
e6dcd2dc | 8456 | if (ordered) { |
2ac55d41 | 8457 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8458 | &cached_state); |
e6dcd2dc | 8459 | unlock_page(page); |
8318ba79 | 8460 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
c0a43603 | 8461 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
8462 | btrfs_put_ordered_extent(ordered); |
8463 | goto again; | |
8464 | } | |
8465 | ||
09cbfeaf | 8466 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8467 | reserved_space = round_up(size - page_start, |
0b246afa | 8468 | fs_info->sectorsize); |
09cbfeaf | 8469 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8470 | end = page_start + reserved_space - 1; |
86d52921 NB |
8471 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8472 | data_reserved, page_start, | |
8473 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8474 | } |
8475 | } | |
8476 | ||
fbf19087 | 8477 | /* |
5416034f LB |
8478 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8479 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8480 | * bits, thus in this case for space account reason, we still need to | |
8481 | * clear any delalloc bits within this page range since we have to | |
8482 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8483 | */ |
d0b7da88 | 8484 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d OS |
8485 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
8486 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
fbf19087 | 8487 | |
c2566f22 | 8488 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8489 | &cached_state); |
a528a241 | 8490 | if (ret2) { |
2ac55d41 | 8491 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8492 | &cached_state); |
9ed74f2d JB |
8493 | ret = VM_FAULT_SIGBUS; |
8494 | goto out_unlock; | |
8495 | } | |
9ebefb18 CM |
8496 | |
8497 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8498 | if (page_start + PAGE_SIZE > size) |
7073017a | 8499 | zero_start = offset_in_page(size); |
9ebefb18 | 8500 | else |
09cbfeaf | 8501 | zero_start = PAGE_SIZE; |
9ebefb18 | 8502 | |
09cbfeaf | 8503 | if (zero_start != PAGE_SIZE) { |
d048b9c2 | 8504 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
e6dcd2dc | 8505 | flush_dcache_page(page); |
e6dcd2dc | 8506 | } |
e4f94347 | 8507 | btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE); |
2d8ec40e QW |
8508 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
8509 | btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start); | |
5a3f23d5 | 8510 | |
bc0939fc | 8511 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
257c62e1 | 8512 | |
e43bbe5e | 8513 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
8318ba79 | 8514 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8515 | |
76de60ed YY |
8516 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8517 | sb_end_pagefault(inode->i_sb); | |
8518 | extent_changeset_free(data_reserved); | |
8519 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8520 | |
8521 | out_unlock: | |
9ebefb18 | 8522 | unlock_page(page); |
8318ba79 | 8523 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
1832a6d5 | 8524 | out: |
8702ba93 | 8525 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8526 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8527 | reserved_space, (ret != 0)); |
9998eb70 | 8528 | out_noreserve: |
b2b5ef5c | 8529 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8530 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8531 | return ret; |
8532 | } | |
8533 | ||
213e8c55 | 8534 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8535 | { |
d9ac19c3 | 8536 | struct btrfs_truncate_control control = { |
487e81d2 | 8537 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 | 8538 | .min_type = BTRFS_EXTENT_DATA_KEY, |
655807b8 | 8539 | .clear_extent_range = true, |
d9ac19c3 | 8540 | }; |
0b246afa | 8541 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8542 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8543 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8544 | int ret; |
39279cc3 | 8545 | struct btrfs_trans_handle *trans; |
0b246afa | 8546 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 8547 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8548 | |
213e8c55 FM |
8549 | if (!skip_writeback) { |
8550 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8551 | (u64)-1); | |
8552 | if (ret) | |
8553 | return ret; | |
8554 | } | |
39279cc3 | 8555 | |
fcb80c2a | 8556 | /* |
f7e9e8fc OS |
8557 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8558 | * things going on here: | |
fcb80c2a | 8559 | * |
f7e9e8fc | 8560 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8561 | * |
f7e9e8fc | 8562 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8563 | * be free'd up by the truncate operation, but also have some slack |
8564 | * space reserved in case it uses space during the truncate (thank you | |
8565 | * very much snapshotting). | |
8566 | * | |
f7e9e8fc | 8567 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8568 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8569 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8570 | * doesn't end up using space reserved for updating the inode. We also |
8571 | * need to be able to stop the transaction and start a new one, which | |
8572 | * means we need to be able to update the inode several times, and we | |
8573 | * have no idea of knowing how many times that will be, so we can't just | |
8574 | * reserve 1 item for the entirety of the operation, so that has to be | |
8575 | * done separately as well. | |
fcb80c2a JB |
8576 | * |
8577 | * So that leaves us with | |
8578 | * | |
f7e9e8fc | 8579 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8580 | * transaction reservation. |
f7e9e8fc | 8581 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8582 | * updating the inode. |
8583 | */ | |
2ff7e61e | 8584 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8585 | if (!rsv) |
8586 | return -ENOMEM; | |
4a338542 | 8587 | rsv->size = min_size; |
ca7e70f5 | 8588 | rsv->failfast = 1; |
f0cd846e | 8589 | |
907cbceb | 8590 | /* |
07127184 | 8591 | * 1 for the truncate slack space |
907cbceb JB |
8592 | * 1 for updating the inode. |
8593 | */ | |
f3fe820c | 8594 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8595 | if (IS_ERR(trans)) { |
ad7e1a74 | 8596 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8597 | goto out; |
8598 | } | |
f0cd846e | 8599 | |
907cbceb | 8600 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8601 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8602 | min_size, false); |
fcb80c2a | 8603 | BUG_ON(ret); |
f0cd846e | 8604 | |
ca7e70f5 | 8605 | trans->block_rsv = rsv; |
907cbceb | 8606 | |
8082510e | 8607 | while (1) { |
9a4a1429 JB |
8608 | struct extent_state *cached_state = NULL; |
8609 | const u64 new_size = inode->i_size; | |
8610 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); | |
8611 | ||
d9ac19c3 | 8612 | control.new_size = new_size; |
9a4a1429 JB |
8613 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
8614 | &cached_state); | |
8615 | /* | |
8616 | * We want to drop from the next block forward in case this new | |
8617 | * size is not block aligned since we will be keeping the last | |
8618 | * block of the extent just the way it is. | |
8619 | */ | |
8620 | btrfs_drop_extent_cache(BTRFS_I(inode), | |
8621 | ALIGN(new_size, fs_info->sectorsize), | |
8622 | (u64)-1, 0); | |
8623 | ||
50743398 | 8624 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
d9ac19c3 | 8625 | &control); |
c2ddb612 | 8626 | |
462b728e | 8627 | inode_sub_bytes(inode, control.sub_bytes); |
c2ddb612 JB |
8628 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size); |
8629 | ||
9a4a1429 JB |
8630 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
8631 | (u64)-1, &cached_state); | |
8632 | ||
ddfae63c | 8633 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8634 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8635 | break; |
39279cc3 | 8636 | |
9a56fcd1 | 8637 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
ad7e1a74 | 8638 | if (ret) |
3893e33b | 8639 | break; |
ca7e70f5 | 8640 | |
3a45bb20 | 8641 | btrfs_end_transaction(trans); |
2ff7e61e | 8642 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8643 | |
8644 | trans = btrfs_start_transaction(root, 2); | |
8645 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8646 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8647 | trans = NULL; |
8648 | break; | |
8649 | } | |
8650 | ||
63f018be | 8651 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8652 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8653 | rsv, min_size, false); |
ca7e70f5 JB |
8654 | BUG_ON(ret); /* shouldn't happen */ |
8655 | trans->block_rsv = rsv; | |
8082510e YZ |
8656 | } |
8657 | ||
ddfae63c JB |
8658 | /* |
8659 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
54f03ab1 JB |
8660 | * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we |
8661 | * know we've truncated everything except the last little bit, and can | |
8662 | * do btrfs_truncate_block and then update the disk_i_size. | |
ddfae63c | 8663 | */ |
54f03ab1 | 8664 | if (ret == BTRFS_NEED_TRUNCATE_BLOCK) { |
ddfae63c JB |
8665 | btrfs_end_transaction(trans); |
8666 | btrfs_btree_balance_dirty(fs_info); | |
8667 | ||
217f42eb | 8668 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
ddfae63c JB |
8669 | if (ret) |
8670 | goto out; | |
8671 | trans = btrfs_start_transaction(root, 1); | |
8672 | if (IS_ERR(trans)) { | |
8673 | ret = PTR_ERR(trans); | |
8674 | goto out; | |
8675 | } | |
76aea537 | 8676 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
ddfae63c JB |
8677 | } |
8678 | ||
917c16b2 | 8679 | if (trans) { |
ad7e1a74 OS |
8680 | int ret2; |
8681 | ||
0b246afa | 8682 | trans->block_rsv = &fs_info->trans_block_rsv; |
9a56fcd1 | 8683 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
ad7e1a74 OS |
8684 | if (ret2 && !ret) |
8685 | ret = ret2; | |
7b128766 | 8686 | |
ad7e1a74 OS |
8687 | ret2 = btrfs_end_transaction(trans); |
8688 | if (ret2 && !ret) | |
8689 | ret = ret2; | |
2ff7e61e | 8690 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8691 | } |
fcb80c2a | 8692 | out: |
2ff7e61e | 8693 | btrfs_free_block_rsv(fs_info, rsv); |
0d7d3165 FM |
8694 | /* |
8695 | * So if we truncate and then write and fsync we normally would just | |
8696 | * write the extents that changed, which is a problem if we need to | |
8697 | * first truncate that entire inode. So set this flag so we write out | |
8698 | * all of the extents in the inode to the sync log so we're completely | |
8699 | * safe. | |
8700 | * | |
8701 | * If no extents were dropped or trimmed we don't need to force the next | |
8702 | * fsync to truncate all the inode's items from the log and re-log them | |
8703 | * all. This means the truncate operation did not change the file size, | |
8704 | * or changed it to a smaller size but there was only an implicit hole | |
8705 | * between the old i_size and the new i_size, and there were no prealloc | |
8706 | * extents beyond i_size to drop. | |
8707 | */ | |
d9ac19c3 | 8708 | if (control.extents_found > 0) |
0d7d3165 | 8709 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
fcb80c2a | 8710 | |
ad7e1a74 | 8711 | return ret; |
39279cc3 CM |
8712 | } |
8713 | ||
d352ac68 CM |
8714 | /* |
8715 | * create a new subvolume directory/inode (helper for the ioctl). | |
8716 | */ | |
d2fb3437 | 8717 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 | 8718 | struct btrfs_root *new_root, |
4d4340c9 CB |
8719 | struct btrfs_root *parent_root, |
8720 | struct user_namespace *mnt_userns) | |
39279cc3 | 8721 | { |
39279cc3 | 8722 | struct inode *inode; |
76dda93c | 8723 | int err; |
00e4e6b3 | 8724 | u64 index = 0; |
23125104 NB |
8725 | u64 ino; |
8726 | ||
8727 | err = btrfs_get_free_objectid(new_root, &ino); | |
8728 | if (err < 0) | |
8729 | return err; | |
39279cc3 | 8730 | |
4d4340c9 | 8731 | inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2, |
b3b6f5b9 | 8732 | ino, ino, |
12fc9d09 FA |
8733 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
8734 | &index); | |
54aa1f4d | 8735 | if (IS_ERR(inode)) |
f46b5a66 | 8736 | return PTR_ERR(inode); |
39279cc3 CM |
8737 | inode->i_op = &btrfs_dir_inode_operations; |
8738 | inode->i_fop = &btrfs_dir_file_operations; | |
8739 | ||
bfe86848 | 8740 | set_nlink(inode, 1); |
6ef06d27 | 8741 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 8742 | unlock_new_inode(inode); |
3b96362c | 8743 | |
63541927 FDBM |
8744 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8745 | if (err) | |
8746 | btrfs_err(new_root->fs_info, | |
351fd353 | 8747 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8748 | new_root->root_key.objectid, err); |
8749 | ||
9a56fcd1 | 8750 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
cb8e7090 | 8751 | |
76dda93c | 8752 | iput(inode); |
ce598979 | 8753 | return err; |
39279cc3 CM |
8754 | } |
8755 | ||
39279cc3 CM |
8756 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8757 | { | |
69fe2d75 | 8758 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8759 | struct btrfs_inode *ei; |
2ead6ae7 | 8760 | struct inode *inode; |
39279cc3 | 8761 | |
712e36c5 | 8762 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
8763 | if (!ei) |
8764 | return NULL; | |
2ead6ae7 YZ |
8765 | |
8766 | ei->root = NULL; | |
2ead6ae7 | 8767 | ei->generation = 0; |
15ee9bc7 | 8768 | ei->last_trans = 0; |
257c62e1 | 8769 | ei->last_sub_trans = 0; |
e02119d5 | 8770 | ei->logged_trans = 0; |
2ead6ae7 | 8771 | ei->delalloc_bytes = 0; |
a7e3b975 | 8772 | ei->new_delalloc_bytes = 0; |
47059d93 | 8773 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8774 | ei->disk_i_size = 0; |
8775 | ei->flags = 0; | |
77eea05e | 8776 | ei->ro_flags = 0; |
7709cde3 | 8777 | ei->csum_bytes = 0; |
2ead6ae7 | 8778 | ei->index_cnt = (u64)-1; |
67de1176 | 8779 | ei->dir_index = 0; |
2ead6ae7 | 8780 | ei->last_unlink_trans = 0; |
3ebac17c | 8781 | ei->last_reflink_trans = 0; |
46d8bc34 | 8782 | ei->last_log_commit = 0; |
2ead6ae7 | 8783 | |
9e0baf60 JB |
8784 | spin_lock_init(&ei->lock); |
8785 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8786 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8787 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8788 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8789 | ei->runtime_flags = 0; |
b52aa8c9 | 8790 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8791 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8792 | |
16cdcec7 MX |
8793 | ei->delayed_node = NULL; |
8794 | ||
9cc97d64 | 8795 | ei->i_otime.tv_sec = 0; |
8796 | ei->i_otime.tv_nsec = 0; | |
8797 | ||
2ead6ae7 | 8798 | inode = &ei->vfs_inode; |
a8067e02 | 8799 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 QW |
8800 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
8801 | extent_io_tree_init(fs_info, &ei->io_failure_tree, | |
8802 | IO_TREE_INODE_IO_FAILURE, inode); | |
41a2ee75 JB |
8803 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
8804 | IO_TREE_INODE_FILE_EXTENT, inode); | |
7b439738 DS |
8805 | ei->io_tree.track_uptodate = true; |
8806 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 8807 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8808 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 8809 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8810 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8811 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8812 | RB_CLEAR_NODE(&ei->rb_node); |
8318ba79 | 8813 | init_rwsem(&ei->i_mmap_lock); |
2ead6ae7 YZ |
8814 | |
8815 | return inode; | |
39279cc3 CM |
8816 | } |
8817 | ||
aaedb55b JB |
8818 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8819 | void btrfs_test_destroy_inode(struct inode *inode) | |
8820 | { | |
dcdbc059 | 8821 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
8822 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8823 | } | |
8824 | #endif | |
8825 | ||
26602cab | 8826 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8827 | { |
fa0d7e3d NP |
8828 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8829 | } | |
8830 | ||
633cc816 | 8831 | void btrfs_destroy_inode(struct inode *vfs_inode) |
39279cc3 | 8832 | { |
e6dcd2dc | 8833 | struct btrfs_ordered_extent *ordered; |
633cc816 NB |
8834 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
8835 | struct btrfs_root *root = inode->root; | |
5a3f23d5 | 8836 | |
633cc816 NB |
8837 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
8838 | WARN_ON(vfs_inode->i_data.nrpages); | |
8839 | WARN_ON(inode->block_rsv.reserved); | |
8840 | WARN_ON(inode->block_rsv.size); | |
8841 | WARN_ON(inode->outstanding_extents); | |
dc287224 FM |
8842 | if (!S_ISDIR(vfs_inode->i_mode)) { |
8843 | WARN_ON(inode->delalloc_bytes); | |
8844 | WARN_ON(inode->new_delalloc_bytes); | |
8845 | } | |
633cc816 NB |
8846 | WARN_ON(inode->csum_bytes); |
8847 | WARN_ON(inode->defrag_bytes); | |
39279cc3 | 8848 | |
a6dbd429 JB |
8849 | /* |
8850 | * This can happen where we create an inode, but somebody else also | |
8851 | * created the same inode and we need to destroy the one we already | |
8852 | * created. | |
8853 | */ | |
8854 | if (!root) | |
26602cab | 8855 | return; |
a6dbd429 | 8856 | |
d397712b | 8857 | while (1) { |
633cc816 | 8858 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
e6dcd2dc CM |
8859 | if (!ordered) |
8860 | break; | |
8861 | else { | |
633cc816 | 8862 | btrfs_err(root->fs_info, |
5d163e0e | 8863 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8864 | ordered->file_offset, ordered->num_bytes); |
71fe0a55 | 8865 | btrfs_remove_ordered_extent(inode, ordered); |
e6dcd2dc CM |
8866 | btrfs_put_ordered_extent(ordered); |
8867 | btrfs_put_ordered_extent(ordered); | |
8868 | } | |
8869 | } | |
633cc816 NB |
8870 | btrfs_qgroup_check_reserved_leak(inode); |
8871 | inode_tree_del(inode); | |
8872 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
8873 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); | |
8874 | btrfs_put_root(inode->root); | |
39279cc3 CM |
8875 | } |
8876 | ||
45321ac5 | 8877 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8878 | { |
8879 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8880 | |
6379ef9f NA |
8881 | if (root == NULL) |
8882 | return 1; | |
8883 | ||
fa6ac876 | 8884 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8885 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8886 | return 1; |
76dda93c | 8887 | else |
45321ac5 | 8888 | return generic_drop_inode(inode); |
76dda93c YZ |
8889 | } |
8890 | ||
0ee0fda0 | 8891 | static void init_once(void *foo) |
39279cc3 CM |
8892 | { |
8893 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8894 | ||
8895 | inode_init_once(&ei->vfs_inode); | |
8896 | } | |
8897 | ||
e67c718b | 8898 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8899 | { |
8c0a8537 KS |
8900 | /* |
8901 | * Make sure all delayed rcu free inodes are flushed before we | |
8902 | * destroy cache. | |
8903 | */ | |
8904 | rcu_barrier(); | |
5598e900 KM |
8905 | kmem_cache_destroy(btrfs_inode_cachep); |
8906 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
8907 | kmem_cache_destroy(btrfs_path_cachep); |
8908 | kmem_cache_destroy(btrfs_free_space_cachep); | |
3acd4850 | 8909 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
39279cc3 CM |
8910 | } |
8911 | ||
f5c29bd9 | 8912 | int __init btrfs_init_cachep(void) |
39279cc3 | 8913 | { |
837e1972 | 8914 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8915 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8916 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8917 | init_once); | |
39279cc3 CM |
8918 | if (!btrfs_inode_cachep) |
8919 | goto fail; | |
9601e3f6 | 8920 | |
837e1972 | 8921 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 8922 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 8923 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8924 | if (!btrfs_trans_handle_cachep) |
8925 | goto fail; | |
9601e3f6 | 8926 | |
837e1972 | 8927 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 8928 | sizeof(struct btrfs_path), 0, |
fba4b697 | 8929 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8930 | if (!btrfs_path_cachep) |
8931 | goto fail; | |
9601e3f6 | 8932 | |
837e1972 | 8933 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 8934 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 8935 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
8936 | if (!btrfs_free_space_cachep) |
8937 | goto fail; | |
8938 | ||
3acd4850 CL |
8939 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
8940 | PAGE_SIZE, PAGE_SIZE, | |
34e49994 | 8941 | SLAB_MEM_SPREAD, NULL); |
3acd4850 CL |
8942 | if (!btrfs_free_space_bitmap_cachep) |
8943 | goto fail; | |
8944 | ||
39279cc3 CM |
8945 | return 0; |
8946 | fail: | |
8947 | btrfs_destroy_cachep(); | |
8948 | return -ENOMEM; | |
8949 | } | |
8950 | ||
549c7297 CB |
8951 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
8952 | const struct path *path, struct kstat *stat, | |
a528d35e | 8953 | u32 request_mask, unsigned int flags) |
39279cc3 | 8954 | { |
df0af1a5 | 8955 | u64 delalloc_bytes; |
2766ff61 | 8956 | u64 inode_bytes; |
a528d35e | 8957 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 8958 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 | 8959 | u32 bi_flags = BTRFS_I(inode)->flags; |
14605409 | 8960 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
04a87e34 YS |
8961 | |
8962 | stat->result_mask |= STATX_BTIME; | |
8963 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
8964 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
8965 | if (bi_flags & BTRFS_INODE_APPEND) | |
8966 | stat->attributes |= STATX_ATTR_APPEND; | |
8967 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8968 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8969 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8970 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8971 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8972 | stat->attributes |= STATX_ATTR_NODUMP; | |
14605409 BB |
8973 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
8974 | stat->attributes |= STATX_ATTR_VERITY; | |
04a87e34 YS |
8975 | |
8976 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8977 | STATX_ATTR_COMPRESSED | | |
8978 | STATX_ATTR_IMMUTABLE | | |
8979 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8980 | |
c020d2ea | 8981 | generic_fillattr(mnt_userns, inode, stat); |
0ee5dc67 | 8982 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8983 | |
8984 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8985 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
2766ff61 | 8986 | inode_bytes = inode_get_bytes(inode); |
df0af1a5 | 8987 | spin_unlock(&BTRFS_I(inode)->lock); |
2766ff61 | 8988 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
df0af1a5 | 8989 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
8990 | return 0; |
8991 | } | |
8992 | ||
cdd1fedf DF |
8993 | static int btrfs_rename_exchange(struct inode *old_dir, |
8994 | struct dentry *old_dentry, | |
8995 | struct inode *new_dir, | |
8996 | struct dentry *new_dentry) | |
8997 | { | |
0b246afa | 8998 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
8999 | struct btrfs_trans_handle *trans; |
9000 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9001 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9002 | struct inode *new_inode = new_dentry->d_inode; | |
9003 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 9004 | struct timespec64 ctime = current_time(old_inode); |
4a0cc7ca NB |
9005 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9006 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9007 | u64 old_idx = 0; |
9008 | u64 new_idx = 0; | |
cdd1fedf | 9009 | int ret; |
75b463d2 | 9010 | int ret2; |
86e8aa0e FM |
9011 | bool root_log_pinned = false; |
9012 | bool dest_log_pinned = false; | |
dc09ef35 | 9013 | bool need_abort = false; |
cdd1fedf | 9014 | |
3f79f6f6 N |
9015 | /* |
9016 | * For non-subvolumes allow exchange only within one subvolume, in the | |
9017 | * same inode namespace. Two subvolumes (represented as directory) can | |
9018 | * be exchanged as they're a logical link and have a fixed inode number. | |
9019 | */ | |
9020 | if (root != dest && | |
9021 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || | |
9022 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) | |
cdd1fedf DF |
9023 | return -EXDEV; |
9024 | ||
9025 | /* close the race window with snapshot create/destroy ioctl */ | |
943eb3bf JB |
9026 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
9027 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9028 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9029 | |
9030 | /* | |
9031 | * We want to reserve the absolute worst case amount of items. So if | |
9032 | * both inodes are subvols and we need to unlink them then that would | |
9033 | * require 4 item modifications, but if they are both normal inodes it | |
9034 | * would require 5 item modifications, so we'll assume their normal | |
9035 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9036 | * should cover the worst case number of items we'll modify. | |
9037 | */ | |
9038 | trans = btrfs_start_transaction(root, 12); | |
9039 | if (IS_ERR(trans)) { | |
9040 | ret = PTR_ERR(trans); | |
9041 | goto out_notrans; | |
9042 | } | |
9043 | ||
00aa8e87 JB |
9044 | if (dest != root) { |
9045 | ret = btrfs_record_root_in_trans(trans, dest); | |
9046 | if (ret) | |
9047 | goto out_fail; | |
9048 | } | |
3e174099 | 9049 | |
cdd1fedf DF |
9050 | /* |
9051 | * We need to find a free sequence number both in the source and | |
9052 | * in the destination directory for the exchange. | |
9053 | */ | |
877574e2 | 9054 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9055 | if (ret) |
9056 | goto out_fail; | |
877574e2 | 9057 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9058 | if (ret) |
9059 | goto out_fail; | |
9060 | ||
9061 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9062 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9063 | ||
9064 | /* Reference for the source. */ | |
9065 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9066 | /* force full log commit if subvolume involved. */ | |
90787766 | 9067 | btrfs_set_log_full_commit(trans); |
cdd1fedf DF |
9068 | } else { |
9069 | ret = btrfs_insert_inode_ref(trans, dest, | |
9070 | new_dentry->d_name.name, | |
9071 | new_dentry->d_name.len, | |
9072 | old_ino, | |
f85b7379 DS |
9073 | btrfs_ino(BTRFS_I(new_dir)), |
9074 | old_idx); | |
cdd1fedf DF |
9075 | if (ret) |
9076 | goto out_fail; | |
dc09ef35 | 9077 | need_abort = true; |
cdd1fedf DF |
9078 | } |
9079 | ||
9080 | /* And now for the dest. */ | |
9081 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9082 | /* force full log commit if subvolume involved. */ | |
90787766 | 9083 | btrfs_set_log_full_commit(trans); |
cdd1fedf DF |
9084 | } else { |
9085 | ret = btrfs_insert_inode_ref(trans, root, | |
9086 | old_dentry->d_name.name, | |
9087 | old_dentry->d_name.len, | |
9088 | new_ino, | |
f85b7379 DS |
9089 | btrfs_ino(BTRFS_I(old_dir)), |
9090 | new_idx); | |
dc09ef35 JB |
9091 | if (ret) { |
9092 | if (need_abort) | |
9093 | btrfs_abort_transaction(trans, ret); | |
cdd1fedf | 9094 | goto out_fail; |
dc09ef35 | 9095 | } |
cdd1fedf DF |
9096 | } |
9097 | ||
9098 | /* Update inode version and ctime/mtime. */ | |
9099 | inode_inc_iversion(old_dir); | |
9100 | inode_inc_iversion(new_dir); | |
9101 | inode_inc_iversion(old_inode); | |
9102 | inode_inc_iversion(new_inode); | |
9103 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9104 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9105 | old_inode->i_ctime = ctime; | |
9106 | new_inode->i_ctime = ctime; | |
9107 | ||
9108 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9109 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9110 | BTRFS_I(old_inode), 1); | |
9111 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9112 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9113 | } |
9114 | ||
bd54f381 FM |
9115 | /* |
9116 | * Now pin the logs of the roots. We do it to ensure that no other task | |
9117 | * can sync the logs while we are in progress with the rename, because | |
9118 | * that could result in an inconsistency in case any of the inodes that | |
9119 | * are part of this rename operation were logged before. | |
9120 | * | |
9121 | * We pin the logs even if at this precise moment none of the inodes was | |
9122 | * logged before. This is because right after we checked for that, some | |
9123 | * other task fsyncing some other inode not involved with this rename | |
9124 | * operation could log that one of our inodes exists. | |
9125 | * | |
9126 | * We don't need to pin the logs before the above calls to | |
9127 | * btrfs_insert_inode_ref(), since those don't ever need to change a log. | |
9128 | */ | |
9129 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
9130 | btrfs_pin_log_trans(root); | |
9131 | root_log_pinned = true; | |
9132 | } | |
9133 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
9134 | btrfs_pin_log_trans(dest); | |
9135 | dest_log_pinned = true; | |
9136 | } | |
9137 | ||
cdd1fedf DF |
9138 | /* src is a subvolume */ |
9139 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 9140 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
cdd1fedf | 9141 | } else { /* src is an inode */ |
4467af88 | 9142 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
4ec5934e | 9143 | BTRFS_I(old_dentry->d_inode), |
cdd1fedf DF |
9144 | old_dentry->d_name.name, |
9145 | old_dentry->d_name.len); | |
9146 | if (!ret) | |
9a56fcd1 | 9147 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
cdd1fedf DF |
9148 | } |
9149 | if (ret) { | |
66642832 | 9150 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9151 | goto out_fail; |
9152 | } | |
9153 | ||
9154 | /* dest is a subvolume */ | |
9155 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 9156 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
cdd1fedf | 9157 | } else { /* dest is an inode */ |
4467af88 | 9158 | ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9159 | BTRFS_I(new_dentry->d_inode), |
cdd1fedf DF |
9160 | new_dentry->d_name.name, |
9161 | new_dentry->d_name.len); | |
9162 | if (!ret) | |
9a56fcd1 | 9163 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
cdd1fedf DF |
9164 | } |
9165 | if (ret) { | |
66642832 | 9166 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9167 | goto out_fail; |
9168 | } | |
9169 | ||
db0a669f | 9170 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9171 | new_dentry->d_name.name, |
9172 | new_dentry->d_name.len, 0, old_idx); | |
9173 | if (ret) { | |
66642832 | 9174 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9175 | goto out_fail; |
9176 | } | |
9177 | ||
db0a669f | 9178 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9179 | old_dentry->d_name.name, |
9180 | old_dentry->d_name.len, 0, new_idx); | |
9181 | if (ret) { | |
66642832 | 9182 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9183 | goto out_fail; |
9184 | } | |
9185 | ||
9186 | if (old_inode->i_nlink == 1) | |
9187 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9188 | if (new_inode->i_nlink == 1) | |
9189 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9190 | ||
86e8aa0e | 9191 | if (root_log_pinned) { |
75b463d2 FM |
9192 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9193 | new_dentry->d_parent); | |
cdd1fedf | 9194 | btrfs_end_log_trans(root); |
86e8aa0e | 9195 | root_log_pinned = false; |
cdd1fedf | 9196 | } |
86e8aa0e | 9197 | if (dest_log_pinned) { |
75b463d2 FM |
9198 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
9199 | old_dentry->d_parent); | |
cdd1fedf | 9200 | btrfs_end_log_trans(dest); |
86e8aa0e | 9201 | dest_log_pinned = false; |
cdd1fedf DF |
9202 | } |
9203 | out_fail: | |
86e8aa0e FM |
9204 | /* |
9205 | * If we have pinned a log and an error happened, we unpin tasks | |
9206 | * trying to sync the log and force them to fallback to a transaction | |
9207 | * commit if the log currently contains any of the inodes involved in | |
9208 | * this rename operation (to ensure we do not persist a log with an | |
9209 | * inconsistent state for any of these inodes or leading to any | |
9210 | * inconsistencies when replayed). If the transaction was aborted, the | |
9211 | * abortion reason is propagated to userspace when attempting to commit | |
9212 | * the transaction. If the log does not contain any of these inodes, we | |
9213 | * allow the tasks to sync it. | |
9214 | */ | |
9215 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9216 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9217 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9218 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
1c167b87 | 9219 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)) |
90787766 | 9220 | btrfs_set_log_full_commit(trans); |
86e8aa0e FM |
9221 | |
9222 | if (root_log_pinned) { | |
9223 | btrfs_end_log_trans(root); | |
9224 | root_log_pinned = false; | |
9225 | } | |
9226 | if (dest_log_pinned) { | |
9227 | btrfs_end_log_trans(dest); | |
9228 | dest_log_pinned = false; | |
9229 | } | |
9230 | } | |
75b463d2 FM |
9231 | ret2 = btrfs_end_transaction(trans); |
9232 | ret = ret ? ret : ret2; | |
cdd1fedf | 9233 | out_notrans: |
943eb3bf JB |
9234 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
9235 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9236 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9237 | |
9238 | return ret; | |
9239 | } | |
9240 | ||
9241 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9242 | struct btrfs_root *root, | |
ca07274c | 9243 | struct user_namespace *mnt_userns, |
cdd1fedf DF |
9244 | struct inode *dir, |
9245 | struct dentry *dentry) | |
9246 | { | |
9247 | int ret; | |
9248 | struct inode *inode; | |
9249 | u64 objectid; | |
9250 | u64 index; | |
9251 | ||
543068a2 | 9252 | ret = btrfs_get_free_objectid(root, &objectid); |
cdd1fedf DF |
9253 | if (ret) |
9254 | return ret; | |
9255 | ||
ca07274c | 9256 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
cdd1fedf DF |
9257 | dentry->d_name.name, |
9258 | dentry->d_name.len, | |
4a0cc7ca | 9259 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9260 | objectid, |
9261 | S_IFCHR | WHITEOUT_MODE, | |
9262 | &index); | |
9263 | ||
9264 | if (IS_ERR(inode)) { | |
9265 | ret = PTR_ERR(inode); | |
9266 | return ret; | |
9267 | } | |
9268 | ||
9269 | inode->i_op = &btrfs_special_inode_operations; | |
9270 | init_special_inode(inode, inode->i_mode, | |
9271 | WHITEOUT_DEV); | |
9272 | ||
9273 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9274 | &dentry->d_name); | |
9275 | if (ret) | |
c9901618 | 9276 | goto out; |
cdd1fedf | 9277 | |
cef415af NB |
9278 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9279 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9280 | if (ret) |
c9901618 | 9281 | goto out; |
cdd1fedf | 9282 | |
9a56fcd1 | 9283 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
c9901618 | 9284 | out: |
cdd1fedf | 9285 | unlock_new_inode(inode); |
c9901618 FM |
9286 | if (ret) |
9287 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9288 | iput(inode); |
9289 | ||
c9901618 | 9290 | return ret; |
cdd1fedf DF |
9291 | } |
9292 | ||
ca07274c CB |
9293 | static int btrfs_rename(struct user_namespace *mnt_userns, |
9294 | struct inode *old_dir, struct dentry *old_dentry, | |
9295 | struct inode *new_dir, struct dentry *new_dentry, | |
9296 | unsigned int flags) | |
39279cc3 | 9297 | { |
0b246afa | 9298 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9299 | struct btrfs_trans_handle *trans; |
5062af35 | 9300 | unsigned int trans_num_items; |
39279cc3 | 9301 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9302 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9303 | struct inode *new_inode = d_inode(new_dentry); |
9304 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9305 | u64 index = 0; |
39279cc3 | 9306 | int ret; |
75b463d2 | 9307 | int ret2; |
4a0cc7ca | 9308 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9309 | bool log_pinned = false; |
39279cc3 | 9310 | |
4a0cc7ca | 9311 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9312 | return -EPERM; |
9313 | ||
4df27c4d | 9314 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9315 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9316 | return -EXDEV; |
9317 | ||
33345d01 | 9318 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9319 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9320 | return -ENOTEMPTY; |
5f39d397 | 9321 | |
4df27c4d YZ |
9322 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9323 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9324 | return -ENOTEMPTY; | |
9c52057c CM |
9325 | |
9326 | ||
9327 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9328 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9329 | new_dentry->d_name.name, |
9330 | new_dentry->d_name.len); | |
9331 | ||
9332 | if (ret) { | |
9333 | if (ret == -EEXIST) { | |
9334 | /* we shouldn't get | |
9335 | * eexist without a new_inode */ | |
fae7f21c | 9336 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9337 | return ret; |
9338 | } | |
9339 | } else { | |
9340 | /* maybe -EOVERFLOW */ | |
9341 | return ret; | |
9342 | } | |
9343 | } | |
9344 | ret = 0; | |
9345 | ||
5a3f23d5 | 9346 | /* |
8d875f95 CM |
9347 | * we're using rename to replace one file with another. Start IO on it |
9348 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9349 | */ |
8d875f95 | 9350 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9351 | filemap_flush(old_inode->i_mapping); |
9352 | ||
76dda93c | 9353 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9354 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9355 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9356 | /* |
9357 | * We want to reserve the absolute worst case amount of items. So if | |
9358 | * both inodes are subvols and we need to unlink them then that would | |
9359 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9360 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9361 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9362 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9363 | * If our rename has the whiteout flag, we need more 5 units for the |
9364 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9365 | * when selinux is enabled). | |
a22285a6 | 9366 | */ |
5062af35 FM |
9367 | trans_num_items = 11; |
9368 | if (flags & RENAME_WHITEOUT) | |
9369 | trans_num_items += 5; | |
9370 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9371 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9372 | ret = PTR_ERR(trans); |
9373 | goto out_notrans; | |
9374 | } | |
76dda93c | 9375 | |
b0fec6fd JB |
9376 | if (dest != root) { |
9377 | ret = btrfs_record_root_in_trans(trans, dest); | |
9378 | if (ret) | |
9379 | goto out_fail; | |
9380 | } | |
5f39d397 | 9381 | |
877574e2 | 9382 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9383 | if (ret) |
9384 | goto out_fail; | |
5a3f23d5 | 9385 | |
67de1176 | 9386 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9387 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9388 | /* force full log commit if subvolume involved. */ |
90787766 | 9389 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9390 | } else { |
a5719521 YZ |
9391 | ret = btrfs_insert_inode_ref(trans, dest, |
9392 | new_dentry->d_name.name, | |
9393 | new_dentry->d_name.len, | |
33345d01 | 9394 | old_ino, |
4a0cc7ca | 9395 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9396 | if (ret) |
9397 | goto out_fail; | |
4df27c4d | 9398 | } |
5a3f23d5 | 9399 | |
0c4d2d95 JB |
9400 | inode_inc_iversion(old_dir); |
9401 | inode_inc_iversion(new_dir); | |
9402 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9403 | old_dir->i_ctime = old_dir->i_mtime = |
9404 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9405 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9406 | |
12fcfd22 | 9407 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9408 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9409 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9410 | |
33345d01 | 9411 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
045d3967 | 9412 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
4df27c4d | 9413 | } else { |
bd54f381 FM |
9414 | /* |
9415 | * Now pin the log. We do it to ensure that no other task can | |
9416 | * sync the log while we are in progress with the rename, as | |
9417 | * that could result in an inconsistency in case any of the | |
9418 | * inodes that are part of this rename operation were logged | |
9419 | * before. | |
9420 | * | |
9421 | * We pin the log even if at this precise moment none of the | |
9422 | * inodes was logged before. This is because right after we | |
9423 | * checked for that, some other task fsyncing some other inode | |
9424 | * not involved with this rename operation could log that one of | |
9425 | * our inodes exists. | |
9426 | * | |
9427 | * We don't need to pin the logs before the above call to | |
9428 | * btrfs_insert_inode_ref(), since that does not need to change | |
9429 | * a log. | |
9430 | */ | |
9431 | btrfs_pin_log_trans(root); | |
9432 | log_pinned = true; | |
4467af88 | 9433 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
4ec5934e | 9434 | BTRFS_I(d_inode(old_dentry)), |
92986796 AV |
9435 | old_dentry->d_name.name, |
9436 | old_dentry->d_name.len); | |
9437 | if (!ret) | |
9a56fcd1 | 9438 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
4df27c4d | 9439 | } |
79787eaa | 9440 | if (ret) { |
66642832 | 9441 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9442 | goto out_fail; |
9443 | } | |
39279cc3 CM |
9444 | |
9445 | if (new_inode) { | |
0c4d2d95 | 9446 | inode_inc_iversion(new_inode); |
c2050a45 | 9447 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9448 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9449 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 9450 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
4df27c4d YZ |
9451 | BUG_ON(new_inode->i_nlink == 0); |
9452 | } else { | |
4467af88 | 9453 | ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9454 | BTRFS_I(d_inode(new_dentry)), |
4df27c4d YZ |
9455 | new_dentry->d_name.name, |
9456 | new_dentry->d_name.len); | |
9457 | } | |
4ef31a45 | 9458 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9459 | ret = btrfs_orphan_add(trans, |
9460 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9461 | if (ret) { |
66642832 | 9462 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9463 | goto out_fail; |
9464 | } | |
39279cc3 | 9465 | } |
aec7477b | 9466 | |
db0a669f | 9467 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 9468 | new_dentry->d_name.name, |
a5719521 | 9469 | new_dentry->d_name.len, 0, index); |
79787eaa | 9470 | if (ret) { |
66642832 | 9471 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9472 | goto out_fail; |
9473 | } | |
39279cc3 | 9474 | |
67de1176 MX |
9475 | if (old_inode->i_nlink == 1) |
9476 | BTRFS_I(old_inode)->dir_index = index; | |
9477 | ||
3dc9e8f7 | 9478 | if (log_pinned) { |
75b463d2 FM |
9479 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9480 | new_dentry->d_parent); | |
4df27c4d | 9481 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9482 | log_pinned = false; |
4df27c4d | 9483 | } |
cdd1fedf DF |
9484 | |
9485 | if (flags & RENAME_WHITEOUT) { | |
ca07274c CB |
9486 | ret = btrfs_whiteout_for_rename(trans, root, mnt_userns, |
9487 | old_dir, old_dentry); | |
cdd1fedf DF |
9488 | |
9489 | if (ret) { | |
66642832 | 9490 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9491 | goto out_fail; |
9492 | } | |
4df27c4d | 9493 | } |
39279cc3 | 9494 | out_fail: |
3dc9e8f7 FM |
9495 | /* |
9496 | * If we have pinned the log and an error happened, we unpin tasks | |
9497 | * trying to sync the log and force them to fallback to a transaction | |
9498 | * commit if the log currently contains any of the inodes involved in | |
9499 | * this rename operation (to ensure we do not persist a log with an | |
9500 | * inconsistent state for any of these inodes or leading to any | |
9501 | * inconsistencies when replayed). If the transaction was aborted, the | |
9502 | * abortion reason is propagated to userspace when attempting to commit | |
9503 | * the transaction. If the log does not contain any of these inodes, we | |
9504 | * allow the tasks to sync it. | |
9505 | */ | |
9506 | if (ret && log_pinned) { | |
0f8939b8 NB |
9507 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9508 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9509 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9510 | (new_inode && |
0f8939b8 | 9511 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 9512 | btrfs_set_log_full_commit(trans); |
3dc9e8f7 FM |
9513 | |
9514 | btrfs_end_log_trans(root); | |
9515 | log_pinned = false; | |
9516 | } | |
75b463d2 FM |
9517 | ret2 = btrfs_end_transaction(trans); |
9518 | ret = ret ? ret : ret2; | |
b44c59a8 | 9519 | out_notrans: |
33345d01 | 9520 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9521 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9522 | |
39279cc3 CM |
9523 | return ret; |
9524 | } | |
9525 | ||
549c7297 CB |
9526 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
9527 | struct dentry *old_dentry, struct inode *new_dir, | |
9528 | struct dentry *new_dentry, unsigned int flags) | |
80ace85c | 9529 | { |
cdd1fedf | 9530 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9531 | return -EINVAL; |
9532 | ||
cdd1fedf DF |
9533 | if (flags & RENAME_EXCHANGE) |
9534 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9535 | new_dentry); | |
9536 | ||
ca07274c CB |
9537 | return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir, |
9538 | new_dentry, flags); | |
80ace85c MS |
9539 | } |
9540 | ||
3a2f8c07 NB |
9541 | struct btrfs_delalloc_work { |
9542 | struct inode *inode; | |
9543 | struct completion completion; | |
9544 | struct list_head list; | |
9545 | struct btrfs_work work; | |
9546 | }; | |
9547 | ||
8ccf6f19 MX |
9548 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9549 | { | |
9550 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9551 | struct inode *inode; |
8ccf6f19 MX |
9552 | |
9553 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9554 | work); | |
9f23e289 | 9555 | inode = delalloc_work->inode; |
30424601 DS |
9556 | filemap_flush(inode->i_mapping); |
9557 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9558 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9559 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9560 | |
076da91c | 9561 | iput(inode); |
8ccf6f19 MX |
9562 | complete(&delalloc_work->completion); |
9563 | } | |
9564 | ||
3a2f8c07 | 9565 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9566 | { |
9567 | struct btrfs_delalloc_work *work; | |
9568 | ||
100d5702 | 9569 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9570 | if (!work) |
9571 | return NULL; | |
9572 | ||
9573 | init_completion(&work->completion); | |
9574 | INIT_LIST_HEAD(&work->list); | |
9575 | work->inode = inode; | |
a0cac0ec | 9576 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
9577 | |
9578 | return work; | |
9579 | } | |
9580 | ||
d352ac68 CM |
9581 | /* |
9582 | * some fairly slow code that needs optimization. This walks the list | |
9583 | * of all the inodes with pending delalloc and forces them to disk. | |
9584 | */ | |
e076ab2a JB |
9585 | static int start_delalloc_inodes(struct btrfs_root *root, |
9586 | struct writeback_control *wbc, bool snapshot, | |
3d45f221 | 9587 | bool in_reclaim_context) |
ea8c2819 | 9588 | { |
ea8c2819 | 9589 | struct btrfs_inode *binode; |
5b21f2ed | 9590 | struct inode *inode; |
8ccf6f19 MX |
9591 | struct btrfs_delalloc_work *work, *next; |
9592 | struct list_head works; | |
1eafa6c7 | 9593 | struct list_head splice; |
8ccf6f19 | 9594 | int ret = 0; |
e076ab2a | 9595 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
ea8c2819 | 9596 | |
8ccf6f19 | 9597 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9598 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9599 | |
573bfb72 | 9600 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9601 | spin_lock(&root->delalloc_lock); |
9602 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9603 | while (!list_empty(&splice)) { |
9604 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9605 | delalloc_inodes); |
1eafa6c7 | 9606 | |
eb73c1b7 MX |
9607 | list_move_tail(&binode->delalloc_inodes, |
9608 | &root->delalloc_inodes); | |
3d45f221 FM |
9609 | |
9610 | if (in_reclaim_context && | |
9611 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) | |
9612 | continue; | |
9613 | ||
5b21f2ed | 9614 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9615 | if (!inode) { |
eb73c1b7 | 9616 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9617 | continue; |
df0af1a5 | 9618 | } |
eb73c1b7 | 9619 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9620 | |
3cd24c69 EL |
9621 | if (snapshot) |
9622 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9623 | &binode->runtime_flags); | |
e076ab2a JB |
9624 | if (full_flush) { |
9625 | work = btrfs_alloc_delalloc_work(inode); | |
9626 | if (!work) { | |
9627 | iput(inode); | |
9628 | ret = -ENOMEM; | |
9629 | goto out; | |
9630 | } | |
9631 | list_add_tail(&work->list, &works); | |
9632 | btrfs_queue_work(root->fs_info->flush_workers, | |
9633 | &work->work); | |
9634 | } else { | |
b3776305 | 9635 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
e076ab2a JB |
9636 | btrfs_add_delayed_iput(inode); |
9637 | if (ret || wbc->nr_to_write <= 0) | |
b4912139 JB |
9638 | goto out; |
9639 | } | |
5b21f2ed | 9640 | cond_resched(); |
eb73c1b7 | 9641 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9642 | } |
eb73c1b7 | 9643 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9644 | |
a1ecaabb | 9645 | out: |
eb73c1b7 MX |
9646 | list_for_each_entry_safe(work, next, &works, list) { |
9647 | list_del_init(&work->list); | |
40012f96 NB |
9648 | wait_for_completion(&work->completion); |
9649 | kfree(work); | |
eb73c1b7 MX |
9650 | } |
9651 | ||
81f1d390 | 9652 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9653 | spin_lock(&root->delalloc_lock); |
9654 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9655 | spin_unlock(&root->delalloc_lock); | |
9656 | } | |
573bfb72 | 9657 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9658 | return ret; |
9659 | } | |
1eafa6c7 | 9660 | |
f9baa501 | 9661 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context) |
eb73c1b7 | 9662 | { |
e076ab2a JB |
9663 | struct writeback_control wbc = { |
9664 | .nr_to_write = LONG_MAX, | |
9665 | .sync_mode = WB_SYNC_NONE, | |
9666 | .range_start = 0, | |
9667 | .range_end = LLONG_MAX, | |
9668 | }; | |
0b246afa | 9669 | struct btrfs_fs_info *fs_info = root->fs_info; |
1eafa6c7 | 9670 | |
84961539 | 9671 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9672 | return -EROFS; |
9673 | ||
f9baa501 | 9674 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
eb73c1b7 MX |
9675 | } |
9676 | ||
9db4dc24 | 9677 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr, |
3d45f221 | 9678 | bool in_reclaim_context) |
eb73c1b7 | 9679 | { |
e076ab2a | 9680 | struct writeback_control wbc = { |
9db4dc24 | 9681 | .nr_to_write = nr, |
e076ab2a JB |
9682 | .sync_mode = WB_SYNC_NONE, |
9683 | .range_start = 0, | |
9684 | .range_end = LLONG_MAX, | |
9685 | }; | |
eb73c1b7 MX |
9686 | struct btrfs_root *root; |
9687 | struct list_head splice; | |
9688 | int ret; | |
9689 | ||
84961539 | 9690 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9691 | return -EROFS; |
9692 | ||
9693 | INIT_LIST_HEAD(&splice); | |
9694 | ||
573bfb72 | 9695 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9696 | spin_lock(&fs_info->delalloc_root_lock); |
9697 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
d7830b71 | 9698 | while (!list_empty(&splice)) { |
e076ab2a JB |
9699 | /* |
9700 | * Reset nr_to_write here so we know that we're doing a full | |
9701 | * flush. | |
9702 | */ | |
9db4dc24 | 9703 | if (nr == LONG_MAX) |
e076ab2a JB |
9704 | wbc.nr_to_write = LONG_MAX; |
9705 | ||
eb73c1b7 MX |
9706 | root = list_first_entry(&splice, struct btrfs_root, |
9707 | delalloc_root); | |
00246528 | 9708 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9709 | BUG_ON(!root); |
9710 | list_move_tail(&root->delalloc_root, | |
9711 | &fs_info->delalloc_roots); | |
9712 | spin_unlock(&fs_info->delalloc_root_lock); | |
9713 | ||
e076ab2a | 9714 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
00246528 | 9715 | btrfs_put_root(root); |
e076ab2a | 9716 | if (ret < 0 || wbc.nr_to_write <= 0) |
eb73c1b7 | 9717 | goto out; |
eb73c1b7 | 9718 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9719 | } |
eb73c1b7 | 9720 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9721 | |
6c255e67 | 9722 | ret = 0; |
eb73c1b7 | 9723 | out: |
81f1d390 | 9724 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9725 | spin_lock(&fs_info->delalloc_root_lock); |
9726 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9727 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9728 | } |
573bfb72 | 9729 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9730 | return ret; |
ea8c2819 CM |
9731 | } |
9732 | ||
549c7297 CB |
9733 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
9734 | struct dentry *dentry, const char *symname) | |
39279cc3 | 9735 | { |
0b246afa | 9736 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9737 | struct btrfs_trans_handle *trans; |
9738 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9739 | struct btrfs_path *path; | |
9740 | struct btrfs_key key; | |
1832a6d5 | 9741 | struct inode *inode = NULL; |
39279cc3 | 9742 | int err; |
39279cc3 | 9743 | u64 objectid; |
67871254 | 9744 | u64 index = 0; |
39279cc3 CM |
9745 | int name_len; |
9746 | int datasize; | |
5f39d397 | 9747 | unsigned long ptr; |
39279cc3 | 9748 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9749 | struct extent_buffer *leaf; |
39279cc3 | 9750 | |
f06becc4 | 9751 | name_len = strlen(symname); |
0b246afa | 9752 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9753 | return -ENAMETOOLONG; |
1832a6d5 | 9754 | |
9ed74f2d JB |
9755 | /* |
9756 | * 2 items for inode item and ref | |
9757 | * 2 items for dir items | |
9269d12b FM |
9758 | * 1 item for updating parent inode item |
9759 | * 1 item for the inline extent item | |
9ed74f2d JB |
9760 | * 1 item for xattr if selinux is on |
9761 | */ | |
9269d12b | 9762 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
9763 | if (IS_ERR(trans)) |
9764 | return PTR_ERR(trans); | |
1832a6d5 | 9765 | |
543068a2 | 9766 | err = btrfs_get_free_objectid(root, &objectid); |
581bb050 LZ |
9767 | if (err) |
9768 | goto out_unlock; | |
9769 | ||
5a052108 | 9770 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
b3b6f5b9 CB |
9771 | dentry->d_name.name, dentry->d_name.len, |
9772 | btrfs_ino(BTRFS_I(dir)), objectid, | |
9773 | S_IFLNK | S_IRWXUGO, &index); | |
7cf96da3 TI |
9774 | if (IS_ERR(inode)) { |
9775 | err = PTR_ERR(inode); | |
32955c54 | 9776 | inode = NULL; |
39279cc3 | 9777 | goto out_unlock; |
7cf96da3 | 9778 | } |
39279cc3 | 9779 | |
ad19db71 CS |
9780 | /* |
9781 | * If the active LSM wants to access the inode during | |
9782 | * d_instantiate it needs these. Smack checks to see | |
9783 | * if the filesystem supports xattrs by looking at the | |
9784 | * ops vector. | |
9785 | */ | |
9786 | inode->i_fop = &btrfs_file_operations; | |
9787 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9788 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9789 | |
9790 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9791 | if (err) | |
32955c54 | 9792 | goto out_unlock; |
ad19db71 | 9793 | |
39279cc3 | 9794 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9795 | if (!path) { |
9796 | err = -ENOMEM; | |
32955c54 | 9797 | goto out_unlock; |
d8926bb3 | 9798 | } |
4a0cc7ca | 9799 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9800 | key.offset = 0; |
962a298f | 9801 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9802 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9803 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9804 | datasize); | |
54aa1f4d | 9805 | if (err) { |
b0839166 | 9806 | btrfs_free_path(path); |
32955c54 | 9807 | goto out_unlock; |
54aa1f4d | 9808 | } |
5f39d397 CM |
9809 | leaf = path->nodes[0]; |
9810 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9811 | struct btrfs_file_extent_item); | |
9812 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9813 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9814 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9815 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9816 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9817 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9818 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9819 | ||
39279cc3 | 9820 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9821 | write_extent_buffer(leaf, symname, ptr, name_len); |
9822 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9823 | btrfs_free_path(path); |
5f39d397 | 9824 | |
39279cc3 | 9825 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 9826 | inode_nohighmem(inode); |
d899e052 | 9827 | inode_set_bytes(inode, name_len); |
6ef06d27 | 9828 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
9a56fcd1 | 9829 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
d50866d0 FM |
9830 | /* |
9831 | * Last step, add directory indexes for our symlink inode. This is the | |
9832 | * last step to avoid extra cleanup of these indexes if an error happens | |
9833 | * elsewhere above. | |
9834 | */ | |
9835 | if (!err) | |
cef415af NB |
9836 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9837 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
9838 | if (err) |
9839 | goto out_unlock; | |
b0d5d10f | 9840 | |
1e2e547a | 9841 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
9842 | |
9843 | out_unlock: | |
3a45bb20 | 9844 | btrfs_end_transaction(trans); |
32955c54 | 9845 | if (err && inode) { |
39279cc3 | 9846 | inode_dec_link_count(inode); |
32955c54 | 9847 | discard_new_inode(inode); |
39279cc3 | 9848 | } |
2ff7e61e | 9849 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
9850 | return err; |
9851 | } | |
16432985 | 9852 | |
8fccebfa FM |
9853 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
9854 | struct btrfs_trans_handle *trans_in, | |
90dffd0c NB |
9855 | struct btrfs_inode *inode, |
9856 | struct btrfs_key *ins, | |
203f44c5 QW |
9857 | u64 file_offset) |
9858 | { | |
9859 | struct btrfs_file_extent_item stack_fi; | |
bf385648 | 9860 | struct btrfs_replace_extent_info extent_info; |
8fccebfa FM |
9861 | struct btrfs_trans_handle *trans = trans_in; |
9862 | struct btrfs_path *path; | |
203f44c5 QW |
9863 | u64 start = ins->objectid; |
9864 | u64 len = ins->offset; | |
fbf48bb0 | 9865 | int qgroup_released; |
9729f10a | 9866 | int ret; |
203f44c5 QW |
9867 | |
9868 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9869 | ||
9870 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9871 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9872 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9873 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9874 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9875 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9876 | /* Encryption and other encoding is reserved and all 0 */ | |
9877 | ||
fbf48bb0 QW |
9878 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
9879 | if (qgroup_released < 0) | |
9880 | return ERR_PTR(qgroup_released); | |
8fccebfa FM |
9881 | |
9882 | if (trans) { | |
90dffd0c | 9883 | ret = insert_reserved_file_extent(trans, inode, |
2766ff61 | 9884 | file_offset, &stack_fi, |
fbf48bb0 | 9885 | true, qgroup_released); |
8fccebfa | 9886 | if (ret) |
a3ee79bd | 9887 | goto free_qgroup; |
8fccebfa FM |
9888 | return trans; |
9889 | } | |
9890 | ||
9891 | extent_info.disk_offset = start; | |
9892 | extent_info.disk_len = len; | |
9893 | extent_info.data_offset = 0; | |
9894 | extent_info.data_len = len; | |
9895 | extent_info.file_offset = file_offset; | |
9896 | extent_info.extent_buf = (char *)&stack_fi; | |
8fccebfa | 9897 | extent_info.is_new_extent = true; |
fbf48bb0 | 9898 | extent_info.qgroup_reserved = qgroup_released; |
8fccebfa FM |
9899 | extent_info.insertions = 0; |
9900 | ||
9901 | path = btrfs_alloc_path(); | |
a3ee79bd QW |
9902 | if (!path) { |
9903 | ret = -ENOMEM; | |
9904 | goto free_qgroup; | |
9905 | } | |
8fccebfa | 9906 | |
bfc78479 | 9907 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
8fccebfa FM |
9908 | file_offset + len - 1, &extent_info, |
9909 | &trans); | |
9910 | btrfs_free_path(path); | |
9911 | if (ret) | |
a3ee79bd | 9912 | goto free_qgroup; |
8fccebfa | 9913 | return trans; |
a3ee79bd QW |
9914 | |
9915 | free_qgroup: | |
9916 | /* | |
9917 | * We have released qgroup data range at the beginning of the function, | |
9918 | * and normally qgroup_released bytes will be freed when committing | |
9919 | * transaction. | |
9920 | * But if we error out early, we have to free what we have released | |
9921 | * or we leak qgroup data reservation. | |
9922 | */ | |
9923 | btrfs_qgroup_free_refroot(inode->root->fs_info, | |
9924 | inode->root->root_key.objectid, qgroup_released, | |
9925 | BTRFS_QGROUP_RSV_DATA); | |
9926 | return ERR_PTR(ret); | |
203f44c5 | 9927 | } |
8fccebfa | 9928 | |
0af3d00b JB |
9929 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9930 | u64 start, u64 num_bytes, u64 min_size, | |
9931 | loff_t actual_len, u64 *alloc_hint, | |
9932 | struct btrfs_trans_handle *trans) | |
d899e052 | 9933 | { |
0b246afa | 9934 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
9935 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9936 | struct extent_map *em; | |
d899e052 YZ |
9937 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9938 | struct btrfs_key ins; | |
d899e052 | 9939 | u64 cur_offset = start; |
b778cf96 | 9940 | u64 clear_offset = start; |
55a61d1d | 9941 | u64 i_size; |
154ea289 | 9942 | u64 cur_bytes; |
0b670dc4 | 9943 | u64 last_alloc = (u64)-1; |
d899e052 | 9944 | int ret = 0; |
0af3d00b | 9945 | bool own_trans = true; |
18513091 | 9946 | u64 end = start + num_bytes - 1; |
d899e052 | 9947 | |
0af3d00b JB |
9948 | if (trans) |
9949 | own_trans = false; | |
d899e052 | 9950 | while (num_bytes > 0) { |
ee22184b | 9951 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9952 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9953 | /* |
9954 | * If we are severely fragmented we could end up with really | |
9955 | * small allocations, so if the allocator is returning small | |
9956 | * chunks lets make its job easier by only searching for those | |
9957 | * sized chunks. | |
9958 | */ | |
9959 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9960 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9961 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
8fccebfa | 9962 | if (ret) |
a22285a6 | 9963 | break; |
b778cf96 JB |
9964 | |
9965 | /* | |
9966 | * We've reserved this space, and thus converted it from | |
9967 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9968 | * from here on out we will only need to clear our reservation | |
9969 | * for the remaining unreserved area, so advance our | |
9970 | * clear_offset by our extent size. | |
9971 | */ | |
9972 | clear_offset += ins.offset; | |
5a303d5d | 9973 | |
0b670dc4 | 9974 | last_alloc = ins.offset; |
90dffd0c NB |
9975 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
9976 | &ins, cur_offset); | |
1afc708d FM |
9977 | /* |
9978 | * Now that we inserted the prealloc extent we can finally | |
9979 | * decrement the number of reservations in the block group. | |
9980 | * If we did it before, we could race with relocation and have | |
9981 | * relocation miss the reserved extent, making it fail later. | |
9982 | */ | |
9983 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
8fccebfa FM |
9984 | if (IS_ERR(trans)) { |
9985 | ret = PTR_ERR(trans); | |
2ff7e61e | 9986 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9987 | ins.offset, 0); |
79787eaa JM |
9988 | break; |
9989 | } | |
31193213 | 9990 | |
dcdbc059 | 9991 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 9992 | cur_offset + ins.offset -1, 0); |
5a303d5d | 9993 | |
5dc562c5 JB |
9994 | em = alloc_extent_map(); |
9995 | if (!em) { | |
9996 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9997 | &BTRFS_I(inode)->runtime_flags); | |
9998 | goto next; | |
9999 | } | |
10000 | ||
10001 | em->start = cur_offset; | |
10002 | em->orig_start = cur_offset; | |
10003 | em->len = ins.offset; | |
10004 | em->block_start = ins.objectid; | |
10005 | em->block_len = ins.offset; | |
b4939680 | 10006 | em->orig_block_len = ins.offset; |
cc95bef6 | 10007 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
10008 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10009 | em->generation = trans->transid; | |
10010 | ||
10011 | while (1) { | |
10012 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10013 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10014 | write_unlock(&em_tree->lock); |
10015 | if (ret != -EEXIST) | |
10016 | break; | |
dcdbc059 | 10017 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10018 | cur_offset + ins.offset - 1, |
10019 | 0); | |
10020 | } | |
10021 | free_extent_map(em); | |
10022 | next: | |
d899e052 YZ |
10023 | num_bytes -= ins.offset; |
10024 | cur_offset += ins.offset; | |
efa56464 | 10025 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10026 | |
0c4d2d95 | 10027 | inode_inc_iversion(inode); |
c2050a45 | 10028 | inode->i_ctime = current_time(inode); |
6cbff00f | 10029 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10030 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10031 | (actual_len > inode->i_size) && |
10032 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10033 | if (cur_offset > actual_len) |
55a61d1d | 10034 | i_size = actual_len; |
d1ea6a61 | 10035 | else |
55a61d1d JB |
10036 | i_size = cur_offset; |
10037 | i_size_write(inode, i_size); | |
76aea537 | 10038 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
5a303d5d YZ |
10039 | } |
10040 | ||
9a56fcd1 | 10041 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
79787eaa JM |
10042 | |
10043 | if (ret) { | |
66642832 | 10044 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10045 | if (own_trans) |
3a45bb20 | 10046 | btrfs_end_transaction(trans); |
79787eaa JM |
10047 | break; |
10048 | } | |
d899e052 | 10049 | |
8fccebfa | 10050 | if (own_trans) { |
3a45bb20 | 10051 | btrfs_end_transaction(trans); |
8fccebfa FM |
10052 | trans = NULL; |
10053 | } | |
5a303d5d | 10054 | } |
b778cf96 | 10055 | if (clear_offset < end) |
25ce28ca | 10056 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 10057 | end - clear_offset + 1); |
d899e052 YZ |
10058 | return ret; |
10059 | } | |
10060 | ||
0af3d00b JB |
10061 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10062 | u64 start, u64 num_bytes, u64 min_size, | |
10063 | loff_t actual_len, u64 *alloc_hint) | |
10064 | { | |
10065 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10066 | min_size, actual_len, alloc_hint, | |
10067 | NULL); | |
10068 | } | |
10069 | ||
10070 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10071 | struct btrfs_trans_handle *trans, int mode, | |
10072 | u64 start, u64 num_bytes, u64 min_size, | |
10073 | loff_t actual_len, u64 *alloc_hint) | |
10074 | { | |
10075 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10076 | min_size, actual_len, alloc_hint, trans); | |
10077 | } | |
10078 | ||
e6dcd2dc CM |
10079 | static int btrfs_set_page_dirty(struct page *page) |
10080 | { | |
e6dcd2dc CM |
10081 | return __set_page_dirty_nobuffers(page); |
10082 | } | |
10083 | ||
549c7297 CB |
10084 | static int btrfs_permission(struct user_namespace *mnt_userns, |
10085 | struct inode *inode, int mask) | |
fdebe2bd | 10086 | { |
b83cc969 | 10087 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10088 | umode_t mode = inode->i_mode; |
b83cc969 | 10089 | |
cb6db4e5 JM |
10090 | if (mask & MAY_WRITE && |
10091 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10092 | if (btrfs_root_readonly(root)) | |
10093 | return -EROFS; | |
10094 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10095 | return -EACCES; | |
10096 | } | |
3bc71ba0 | 10097 | return generic_permission(mnt_userns, inode, mask); |
fdebe2bd | 10098 | } |
39279cc3 | 10099 | |
549c7297 CB |
10100 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
10101 | struct dentry *dentry, umode_t mode) | |
ef3b9af5 | 10102 | { |
2ff7e61e | 10103 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10104 | struct btrfs_trans_handle *trans; |
10105 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10106 | struct inode *inode = NULL; | |
10107 | u64 objectid; | |
10108 | u64 index; | |
10109 | int ret = 0; | |
10110 | ||
10111 | /* | |
10112 | * 5 units required for adding orphan entry | |
10113 | */ | |
10114 | trans = btrfs_start_transaction(root, 5); | |
10115 | if (IS_ERR(trans)) | |
10116 | return PTR_ERR(trans); | |
10117 | ||
543068a2 | 10118 | ret = btrfs_get_free_objectid(root, &objectid); |
ef3b9af5 FM |
10119 | if (ret) |
10120 | goto out; | |
10121 | ||
98b6ab5f | 10122 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0, |
f85b7379 | 10123 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10124 | if (IS_ERR(inode)) { |
10125 | ret = PTR_ERR(inode); | |
10126 | inode = NULL; | |
10127 | goto out; | |
10128 | } | |
10129 | ||
ef3b9af5 FM |
10130 | inode->i_fop = &btrfs_file_operations; |
10131 | inode->i_op = &btrfs_file_inode_operations; | |
10132 | ||
10133 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 | 10134 | |
b0d5d10f CM |
10135 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10136 | if (ret) | |
32955c54 | 10137 | goto out; |
b0d5d10f | 10138 | |
9a56fcd1 | 10139 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
b0d5d10f | 10140 | if (ret) |
32955c54 | 10141 | goto out; |
73f2e545 | 10142 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10143 | if (ret) |
32955c54 | 10144 | goto out; |
ef3b9af5 | 10145 | |
5762b5c9 FM |
10146 | /* |
10147 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10148 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10149 | * through: | |
10150 | * | |
10151 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10152 | */ | |
10153 | set_nlink(inode, 1); | |
ef3b9af5 | 10154 | d_tmpfile(dentry, inode); |
32955c54 | 10155 | unlock_new_inode(inode); |
ef3b9af5 | 10156 | mark_inode_dirty(inode); |
ef3b9af5 | 10157 | out: |
3a45bb20 | 10158 | btrfs_end_transaction(trans); |
32955c54 AV |
10159 | if (ret && inode) |
10160 | discard_new_inode(inode); | |
2ff7e61e | 10161 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
10162 | return ret; |
10163 | } | |
10164 | ||
d2a91064 | 10165 | void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) |
c6100a4b | 10166 | { |
d2a91064 | 10167 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
c6100a4b JB |
10168 | unsigned long index = start >> PAGE_SHIFT; |
10169 | unsigned long end_index = end >> PAGE_SHIFT; | |
10170 | struct page *page; | |
d2a91064 | 10171 | u32 len; |
c6100a4b | 10172 | |
d2a91064 QW |
10173 | ASSERT(end + 1 - start <= U32_MAX); |
10174 | len = end + 1 - start; | |
c6100a4b | 10175 | while (index <= end_index) { |
d2a91064 | 10176 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
c6100a4b | 10177 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
d2a91064 QW |
10178 | |
10179 | btrfs_page_set_writeback(fs_info, page, start, len); | |
c6100a4b JB |
10180 | put_page(page); |
10181 | index++; | |
10182 | } | |
10183 | } | |
10184 | ||
ed46ff3d OS |
10185 | #ifdef CONFIG_SWAP |
10186 | /* | |
10187 | * Add an entry indicating a block group or device which is pinned by a | |
10188 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10189 | * negative errno on failure. | |
10190 | */ | |
10191 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10192 | bool is_block_group) | |
10193 | { | |
10194 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10195 | struct btrfs_swapfile_pin *sp, *entry; | |
10196 | struct rb_node **p; | |
10197 | struct rb_node *parent = NULL; | |
10198 | ||
10199 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10200 | if (!sp) | |
10201 | return -ENOMEM; | |
10202 | sp->ptr = ptr; | |
10203 | sp->inode = inode; | |
10204 | sp->is_block_group = is_block_group; | |
195a49ea | 10205 | sp->bg_extent_count = 1; |
ed46ff3d OS |
10206 | |
10207 | spin_lock(&fs_info->swapfile_pins_lock); | |
10208 | p = &fs_info->swapfile_pins.rb_node; | |
10209 | while (*p) { | |
10210 | parent = *p; | |
10211 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10212 | if (sp->ptr < entry->ptr || | |
10213 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10214 | p = &(*p)->rb_left; | |
10215 | } else if (sp->ptr > entry->ptr || | |
10216 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10217 | p = &(*p)->rb_right; | |
10218 | } else { | |
195a49ea FM |
10219 | if (is_block_group) |
10220 | entry->bg_extent_count++; | |
ed46ff3d OS |
10221 | spin_unlock(&fs_info->swapfile_pins_lock); |
10222 | kfree(sp); | |
10223 | return 1; | |
10224 | } | |
10225 | } | |
10226 | rb_link_node(&sp->node, parent, p); | |
10227 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10228 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10229 | return 0; | |
10230 | } | |
10231 | ||
10232 | /* Free all of the entries pinned by this swapfile. */ | |
10233 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10234 | { | |
10235 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10236 | struct btrfs_swapfile_pin *sp; | |
10237 | struct rb_node *node, *next; | |
10238 | ||
10239 | spin_lock(&fs_info->swapfile_pins_lock); | |
10240 | node = rb_first(&fs_info->swapfile_pins); | |
10241 | while (node) { | |
10242 | next = rb_next(node); | |
10243 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10244 | if (sp->inode == inode) { | |
10245 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
195a49ea FM |
10246 | if (sp->is_block_group) { |
10247 | btrfs_dec_block_group_swap_extents(sp->ptr, | |
10248 | sp->bg_extent_count); | |
ed46ff3d | 10249 | btrfs_put_block_group(sp->ptr); |
195a49ea | 10250 | } |
ed46ff3d OS |
10251 | kfree(sp); |
10252 | } | |
10253 | node = next; | |
10254 | } | |
10255 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10256 | } | |
10257 | ||
10258 | struct btrfs_swap_info { | |
10259 | u64 start; | |
10260 | u64 block_start; | |
10261 | u64 block_len; | |
10262 | u64 lowest_ppage; | |
10263 | u64 highest_ppage; | |
10264 | unsigned long nr_pages; | |
10265 | int nr_extents; | |
10266 | }; | |
10267 | ||
10268 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10269 | struct btrfs_swap_info *bsi) | |
10270 | { | |
10271 | unsigned long nr_pages; | |
10272 | u64 first_ppage, first_ppage_reported, next_ppage; | |
10273 | int ret; | |
10274 | ||
10275 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
10276 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
10277 | PAGE_SIZE) >> PAGE_SHIFT; | |
10278 | ||
10279 | if (first_ppage >= next_ppage) | |
10280 | return 0; | |
10281 | nr_pages = next_ppage - first_ppage; | |
10282 | ||
10283 | first_ppage_reported = first_ppage; | |
10284 | if (bsi->start == 0) | |
10285 | first_ppage_reported++; | |
10286 | if (bsi->lowest_ppage > first_ppage_reported) | |
10287 | bsi->lowest_ppage = first_ppage_reported; | |
10288 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10289 | bsi->highest_ppage = next_ppage - 1; | |
10290 | ||
10291 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10292 | if (ret < 0) | |
10293 | return ret; | |
10294 | bsi->nr_extents += ret; | |
10295 | bsi->nr_pages += nr_pages; | |
10296 | return 0; | |
10297 | } | |
10298 | ||
10299 | static void btrfs_swap_deactivate(struct file *file) | |
10300 | { | |
10301 | struct inode *inode = file_inode(file); | |
10302 | ||
10303 | btrfs_free_swapfile_pins(inode); | |
10304 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10305 | } | |
10306 | ||
10307 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10308 | sector_t *span) | |
10309 | { | |
10310 | struct inode *inode = file_inode(file); | |
dd0734f2 FM |
10311 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10312 | struct btrfs_fs_info *fs_info = root->fs_info; | |
ed46ff3d OS |
10313 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
10314 | struct extent_state *cached_state = NULL; | |
10315 | struct extent_map *em = NULL; | |
10316 | struct btrfs_device *device = NULL; | |
10317 | struct btrfs_swap_info bsi = { | |
10318 | .lowest_ppage = (sector_t)-1ULL, | |
10319 | }; | |
10320 | int ret = 0; | |
10321 | u64 isize; | |
10322 | u64 start; | |
10323 | ||
10324 | /* | |
10325 | * If the swap file was just created, make sure delalloc is done. If the | |
10326 | * file changes again after this, the user is doing something stupid and | |
10327 | * we don't really care. | |
10328 | */ | |
10329 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10330 | if (ret) | |
10331 | return ret; | |
10332 | ||
10333 | /* | |
10334 | * The inode is locked, so these flags won't change after we check them. | |
10335 | */ | |
10336 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10337 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10338 | return -EINVAL; | |
10339 | } | |
10340 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10341 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10342 | return -EINVAL; | |
10343 | } | |
10344 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10345 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10346 | return -EINVAL; | |
10347 | } | |
10348 | ||
10349 | /* | |
10350 | * Balance or device remove/replace/resize can move stuff around from | |
c3e1f96c GR |
10351 | * under us. The exclop protection makes sure they aren't running/won't |
10352 | * run concurrently while we are mapping the swap extents, and | |
10353 | * fs_info->swapfile_pins prevents them from running while the swap | |
10354 | * file is active and moving the extents. Note that this also prevents | |
10355 | * a concurrent device add which isn't actually necessary, but it's not | |
ed46ff3d OS |
10356 | * really worth the trouble to allow it. |
10357 | */ | |
c3e1f96c | 10358 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
ed46ff3d OS |
10359 | btrfs_warn(fs_info, |
10360 | "cannot activate swapfile while exclusive operation is running"); | |
10361 | return -EBUSY; | |
10362 | } | |
dd0734f2 FM |
10363 | |
10364 | /* | |
10365 | * Prevent snapshot creation while we are activating the swap file. | |
10366 | * We do not want to race with snapshot creation. If snapshot creation | |
10367 | * already started before we bumped nr_swapfiles from 0 to 1 and | |
10368 | * completes before the first write into the swap file after it is | |
10369 | * activated, than that write would fallback to COW. | |
10370 | */ | |
10371 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { | |
10372 | btrfs_exclop_finish(fs_info); | |
10373 | btrfs_warn(fs_info, | |
10374 | "cannot activate swapfile because snapshot creation is in progress"); | |
10375 | return -EINVAL; | |
10376 | } | |
ed46ff3d OS |
10377 | /* |
10378 | * Snapshots can create extents which require COW even if NODATACOW is | |
10379 | * set. We use this counter to prevent snapshots. We must increment it | |
10380 | * before walking the extents because we don't want a concurrent | |
10381 | * snapshot to run after we've already checked the extents. | |
10382 | */ | |
dd0734f2 | 10383 | atomic_inc(&root->nr_swapfiles); |
ed46ff3d OS |
10384 | |
10385 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10386 | ||
10387 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10388 | start = 0; | |
10389 | while (start < isize) { | |
10390 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10391 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10392 | u64 len = isize - start; |
10393 | ||
39b07b5d | 10394 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
10395 | if (IS_ERR(em)) { |
10396 | ret = PTR_ERR(em); | |
10397 | goto out; | |
10398 | } | |
10399 | ||
10400 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10401 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10402 | ret = -EINVAL; | |
10403 | goto out; | |
10404 | } | |
10405 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10406 | /* | |
10407 | * It's unlikely we'll ever actually find ourselves | |
10408 | * here, as a file small enough to fit inline won't be | |
10409 | * big enough to store more than the swap header, but in | |
10410 | * case something changes in the future, let's catch it | |
10411 | * here rather than later. | |
10412 | */ | |
10413 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10414 | ret = -EINVAL; | |
10415 | goto out; | |
10416 | } | |
10417 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10418 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10419 | ret = -EINVAL; | |
10420 | goto out; | |
10421 | } | |
10422 | ||
10423 | logical_block_start = em->block_start + (start - em->start); | |
10424 | len = min(len, em->len - (start - em->start)); | |
10425 | free_extent_map(em); | |
10426 | em = NULL; | |
10427 | ||
a84d5d42 | 10428 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
ed46ff3d OS |
10429 | if (ret < 0) { |
10430 | goto out; | |
10431 | } else if (ret) { | |
10432 | ret = 0; | |
10433 | } else { | |
10434 | btrfs_warn(fs_info, | |
10435 | "swapfile must not be copy-on-write"); | |
10436 | ret = -EINVAL; | |
10437 | goto out; | |
10438 | } | |
10439 | ||
10440 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10441 | if (IS_ERR(em)) { | |
10442 | ret = PTR_ERR(em); | |
10443 | goto out; | |
10444 | } | |
10445 | ||
10446 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10447 | btrfs_warn(fs_info, | |
10448 | "swapfile must have single data profile"); | |
10449 | ret = -EINVAL; | |
10450 | goto out; | |
10451 | } | |
10452 | ||
10453 | if (device == NULL) { | |
10454 | device = em->map_lookup->stripes[0].dev; | |
10455 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10456 | if (ret == 1) | |
10457 | ret = 0; | |
10458 | else if (ret) | |
10459 | goto out; | |
10460 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10461 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10462 | ret = -EINVAL; | |
10463 | goto out; | |
10464 | } | |
10465 | ||
10466 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10467 | (logical_block_start - em->start)); | |
10468 | len = min(len, em->len - (logical_block_start - em->start)); | |
10469 | free_extent_map(em); | |
10470 | em = NULL; | |
10471 | ||
10472 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10473 | if (!bg) { | |
10474 | btrfs_warn(fs_info, | |
10475 | "could not find block group containing swapfile"); | |
10476 | ret = -EINVAL; | |
10477 | goto out; | |
10478 | } | |
10479 | ||
195a49ea FM |
10480 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
10481 | btrfs_warn(fs_info, | |
10482 | "block group for swapfile at %llu is read-only%s", | |
10483 | bg->start, | |
10484 | atomic_read(&fs_info->scrubs_running) ? | |
10485 | " (scrub running)" : ""); | |
10486 | btrfs_put_block_group(bg); | |
10487 | ret = -EINVAL; | |
10488 | goto out; | |
10489 | } | |
10490 | ||
ed46ff3d OS |
10491 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
10492 | if (ret) { | |
10493 | btrfs_put_block_group(bg); | |
10494 | if (ret == 1) | |
10495 | ret = 0; | |
10496 | else | |
10497 | goto out; | |
10498 | } | |
10499 | ||
10500 | if (bsi.block_len && | |
10501 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10502 | bsi.block_len += len; | |
10503 | } else { | |
10504 | if (bsi.block_len) { | |
10505 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10506 | if (ret) | |
10507 | goto out; | |
10508 | } | |
10509 | bsi.start = start; | |
10510 | bsi.block_start = physical_block_start; | |
10511 | bsi.block_len = len; | |
10512 | } | |
10513 | ||
10514 | start += len; | |
10515 | } | |
10516 | ||
10517 | if (bsi.block_len) | |
10518 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10519 | ||
10520 | out: | |
10521 | if (!IS_ERR_OR_NULL(em)) | |
10522 | free_extent_map(em); | |
10523 | ||
10524 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10525 | ||
10526 | if (ret) | |
10527 | btrfs_swap_deactivate(file); | |
10528 | ||
dd0734f2 FM |
10529 | btrfs_drew_write_unlock(&root->snapshot_lock); |
10530 | ||
c3e1f96c | 10531 | btrfs_exclop_finish(fs_info); |
ed46ff3d OS |
10532 | |
10533 | if (ret) | |
10534 | return ret; | |
10535 | ||
10536 | if (device) | |
10537 | sis->bdev = device->bdev; | |
10538 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10539 | sis->max = bsi.nr_pages; | |
10540 | sis->pages = bsi.nr_pages - 1; | |
10541 | sis->highest_bit = bsi.nr_pages - 1; | |
10542 | return bsi.nr_extents; | |
10543 | } | |
10544 | #else | |
10545 | static void btrfs_swap_deactivate(struct file *file) | |
10546 | { | |
10547 | } | |
10548 | ||
10549 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10550 | sector_t *span) | |
10551 | { | |
10552 | return -EOPNOTSUPP; | |
10553 | } | |
10554 | #endif | |
10555 | ||
2766ff61 FM |
10556 | /* |
10557 | * Update the number of bytes used in the VFS' inode. When we replace extents in | |
10558 | * a range (clone, dedupe, fallocate's zero range), we must update the number of | |
10559 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls | |
10560 | * always get a correct value. | |
10561 | */ | |
10562 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, | |
10563 | const u64 add_bytes, | |
10564 | const u64 del_bytes) | |
10565 | { | |
10566 | if (add_bytes == del_bytes) | |
10567 | return; | |
10568 | ||
10569 | spin_lock(&inode->lock); | |
10570 | if (del_bytes > 0) | |
10571 | inode_sub_bytes(&inode->vfs_inode, del_bytes); | |
10572 | if (add_bytes > 0) | |
10573 | inode_add_bytes(&inode->vfs_inode, add_bytes); | |
10574 | spin_unlock(&inode->lock); | |
10575 | } | |
10576 | ||
6e1d5dcc | 10577 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10578 | .getattr = btrfs_getattr, |
39279cc3 CM |
10579 | .lookup = btrfs_lookup, |
10580 | .create = btrfs_create, | |
10581 | .unlink = btrfs_unlink, | |
10582 | .link = btrfs_link, | |
10583 | .mkdir = btrfs_mkdir, | |
10584 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10585 | .rename = btrfs_rename2, |
39279cc3 CM |
10586 | .symlink = btrfs_symlink, |
10587 | .setattr = btrfs_setattr, | |
618e21d5 | 10588 | .mknod = btrfs_mknod, |
5103e947 | 10589 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10590 | .permission = btrfs_permission, |
4e34e719 | 10591 | .get_acl = btrfs_get_acl, |
996a710d | 10592 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10593 | .update_time = btrfs_update_time, |
ef3b9af5 | 10594 | .tmpfile = btrfs_tmpfile, |
97fc2977 MS |
10595 | .fileattr_get = btrfs_fileattr_get, |
10596 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 10597 | }; |
76dda93c | 10598 | |
828c0950 | 10599 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10600 | .llseek = generic_file_llseek, |
10601 | .read = generic_read_dir, | |
02dbfc99 | 10602 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10603 | .open = btrfs_opendir, |
34287aa3 | 10604 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10605 | #ifdef CONFIG_COMPAT |
4c63c245 | 10606 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10607 | #endif |
6bf13c0c | 10608 | .release = btrfs_release_file, |
e02119d5 | 10609 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10610 | }; |
10611 | ||
35054394 CM |
10612 | /* |
10613 | * btrfs doesn't support the bmap operation because swapfiles | |
10614 | * use bmap to make a mapping of extents in the file. They assume | |
10615 | * these extents won't change over the life of the file and they | |
10616 | * use the bmap result to do IO directly to the drive. | |
10617 | * | |
10618 | * the btrfs bmap call would return logical addresses that aren't | |
10619 | * suitable for IO and they also will change frequently as COW | |
10620 | * operations happen. So, swapfile + btrfs == corruption. | |
10621 | * | |
10622 | * For now we're avoiding this by dropping bmap. | |
10623 | */ | |
7f09410b | 10624 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10625 | .readpage = btrfs_readpage, |
10626 | .writepage = btrfs_writepage, | |
b293f02e | 10627 | .writepages = btrfs_writepages, |
ba206a02 | 10628 | .readahead = btrfs_readahead, |
f85781fb | 10629 | .direct_IO = noop_direct_IO, |
a52d9a80 CM |
10630 | .invalidatepage = btrfs_invalidatepage, |
10631 | .releasepage = btrfs_releasepage, | |
f8e66081 RG |
10632 | #ifdef CONFIG_MIGRATION |
10633 | .migratepage = btrfs_migratepage, | |
10634 | #endif | |
e6dcd2dc | 10635 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10636 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10637 | .swap_activate = btrfs_swap_activate, |
10638 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10639 | }; |
10640 | ||
6e1d5dcc | 10641 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10642 | .getattr = btrfs_getattr, |
10643 | .setattr = btrfs_setattr, | |
5103e947 | 10644 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10645 | .permission = btrfs_permission, |
1506fcc8 | 10646 | .fiemap = btrfs_fiemap, |
4e34e719 | 10647 | .get_acl = btrfs_get_acl, |
996a710d | 10648 | .set_acl = btrfs_set_acl, |
e41f941a | 10649 | .update_time = btrfs_update_time, |
97fc2977 MS |
10650 | .fileattr_get = btrfs_fileattr_get, |
10651 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 10652 | }; |
6e1d5dcc | 10653 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10654 | .getattr = btrfs_getattr, |
10655 | .setattr = btrfs_setattr, | |
fdebe2bd | 10656 | .permission = btrfs_permission, |
33268eaf | 10657 | .listxattr = btrfs_listxattr, |
4e34e719 | 10658 | .get_acl = btrfs_get_acl, |
996a710d | 10659 | .set_acl = btrfs_set_acl, |
e41f941a | 10660 | .update_time = btrfs_update_time, |
618e21d5 | 10661 | }; |
6e1d5dcc | 10662 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10663 | .get_link = page_get_link, |
f209561a | 10664 | .getattr = btrfs_getattr, |
22c44fe6 | 10665 | .setattr = btrfs_setattr, |
fdebe2bd | 10666 | .permission = btrfs_permission, |
0279b4cd | 10667 | .listxattr = btrfs_listxattr, |
e41f941a | 10668 | .update_time = btrfs_update_time, |
39279cc3 | 10669 | }; |
76dda93c | 10670 | |
82d339d9 | 10671 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
10672 | .d_delete = btrfs_dentry_delete, |
10673 | }; |