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" |
c7f13d42 | 58 | #include "fs.h" |
ad1ac501 | 59 | #include "accessors.h" |
a0231804 | 60 | #include "extent-tree.h" |
45c40c8f | 61 | #include "root-tree.h" |
59b818e0 | 62 | #include "defrag.h" |
f2b39277 | 63 | #include "dir-item.h" |
39279cc3 CM |
64 | |
65 | struct btrfs_iget_args { | |
0202e83f | 66 | u64 ino; |
39279cc3 CM |
67 | struct btrfs_root *root; |
68 | }; | |
69 | ||
f28a4928 | 70 | struct btrfs_dio_data { |
f85781fb GR |
71 | ssize_t submitted; |
72 | struct extent_changeset *data_reserved; | |
f5585f4f FM |
73 | bool data_space_reserved; |
74 | bool nocow_done; | |
f28a4928 FM |
75 | }; |
76 | ||
a3e171a0 CH |
77 | struct btrfs_dio_private { |
78 | struct inode *inode; | |
79 | ||
80 | /* | |
81 | * Since DIO can use anonymous page, we cannot use page_offset() to | |
82 | * grab the file offset, thus need a dedicated member for file offset. | |
83 | */ | |
84 | u64 file_offset; | |
85 | /* Used for bio::bi_size */ | |
86 | u32 bytes; | |
87 | ||
88 | /* | |
89 | * References to this structure. There is one reference per in-flight | |
90 | * bio plus one while we're still setting up. | |
91 | */ | |
92 | refcount_t refs; | |
93 | ||
a3e171a0 | 94 | /* Array of checksums */ |
642c5d34 CH |
95 | u8 *csums; |
96 | ||
97 | /* This must be last */ | |
98 | struct bio bio; | |
a3e171a0 CH |
99 | }; |
100 | ||
642c5d34 CH |
101 | static struct bio_set btrfs_dio_bioset; |
102 | ||
88d2beec FM |
103 | struct btrfs_rename_ctx { |
104 | /* Output field. Stores the index number of the old directory entry. */ | |
105 | u64 index; | |
106 | }; | |
107 | ||
6e1d5dcc AD |
108 | static const struct inode_operations btrfs_dir_inode_operations; |
109 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
110 | static const struct inode_operations btrfs_special_inode_operations; |
111 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 112 | static const struct address_space_operations btrfs_aops; |
828c0950 | 113 | static const struct file_operations btrfs_dir_file_operations; |
39279cc3 CM |
114 | |
115 | static struct kmem_cache *btrfs_inode_cachep; | |
39279cc3 | 116 | |
3972f260 | 117 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 118 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
6e26c442 | 119 | static noinline int cow_file_range(struct btrfs_inode *inode, |
771ed689 | 120 | struct page *locked_page, |
74e9194a | 121 | u64 start, u64 end, int *page_started, |
898793d9 NA |
122 | unsigned long *nr_written, int unlock, |
123 | u64 *done_offset); | |
4b67c11d NB |
124 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
125 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
126 | u64 block_len, u64 orig_block_len, |
127 | u64 ram_bytes, int compress_type, | |
128 | int type); | |
7b128766 | 129 | |
f60acad3 JB |
130 | static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode, |
131 | u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) | |
132 | { | |
133 | struct btrfs_root *root = inode->root; | |
134 | const u32 csum_size = root->fs_info->csum_size; | |
135 | ||
136 | /* Output without objectid, which is more meaningful */ | |
137 | if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) { | |
138 | btrfs_warn_rl(root->fs_info, | |
139 | "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
140 | root->root_key.objectid, btrfs_ino(inode), | |
141 | logical_start, | |
142 | CSUM_FMT_VALUE(csum_size, csum), | |
143 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
144 | mirror_num); | |
145 | } else { | |
146 | btrfs_warn_rl(root->fs_info, | |
147 | "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
148 | root->root_key.objectid, btrfs_ino(inode), | |
149 | logical_start, | |
150 | CSUM_FMT_VALUE(csum_size, csum), | |
151 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
152 | mirror_num); | |
153 | } | |
154 | } | |
155 | ||
a14b78ad GR |
156 | /* |
157 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed | |
158 | * | |
159 | * ilock_flags can have the following bit set: | |
160 | * | |
161 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode | |
162 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt | |
163 | * return -EAGAIN | |
8318ba79 | 164 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
a14b78ad GR |
165 | */ |
166 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) | |
167 | { | |
168 | if (ilock_flags & BTRFS_ILOCK_SHARED) { | |
169 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
170 | if (!inode_trylock_shared(inode)) | |
171 | return -EAGAIN; | |
172 | else | |
173 | return 0; | |
174 | } | |
175 | inode_lock_shared(inode); | |
176 | } else { | |
177 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
178 | if (!inode_trylock(inode)) | |
179 | return -EAGAIN; | |
180 | else | |
181 | return 0; | |
182 | } | |
183 | inode_lock(inode); | |
184 | } | |
8318ba79 JB |
185 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
186 | down_write(&BTRFS_I(inode)->i_mmap_lock); | |
a14b78ad GR |
187 | return 0; |
188 | } | |
189 | ||
190 | /* | |
191 | * btrfs_inode_unlock - unock inode i_rwsem | |
192 | * | |
193 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() | |
194 | * to decide whether the lock acquired is shared or exclusive. | |
195 | */ | |
196 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) | |
197 | { | |
8318ba79 JB |
198 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
199 | up_write(&BTRFS_I(inode)->i_mmap_lock); | |
a14b78ad GR |
200 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
201 | inode_unlock_shared(inode); | |
202 | else | |
203 | inode_unlock(inode); | |
204 | } | |
205 | ||
52427260 QW |
206 | /* |
207 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 208 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
209 | * |
210 | * NOTE: caller must ensure that when an error happens, it can not call | |
211 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
212 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
213 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 214 | * extent (btrfs_finish_ordered_io()). |
52427260 | 215 | */ |
64e1db56 | 216 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
217 | struct page *locked_page, |
218 | u64 offset, u64 bytes) | |
52427260 | 219 | { |
63d71450 NA |
220 | unsigned long index = offset >> PAGE_SHIFT; |
221 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
99826e4c | 222 | u64 page_start, page_end; |
63d71450 NA |
223 | struct page *page; |
224 | ||
99826e4c NA |
225 | if (locked_page) { |
226 | page_start = page_offset(locked_page); | |
227 | page_end = page_start + PAGE_SIZE - 1; | |
228 | } | |
229 | ||
63d71450 | 230 | while (index <= end_index) { |
968f2566 QW |
231 | /* |
232 | * For locked page, we will call end_extent_writepage() on it | |
233 | * in run_delalloc_range() for the error handling. That | |
234 | * end_extent_writepage() function will call | |
235 | * btrfs_mark_ordered_io_finished() to clear page Ordered and | |
236 | * run the ordered extent accounting. | |
237 | * | |
238 | * Here we can't just clear the Ordered bit, or | |
239 | * btrfs_mark_ordered_io_finished() would skip the accounting | |
240 | * for the page range, and the ordered extent will never finish. | |
241 | */ | |
99826e4c | 242 | if (locked_page && index == (page_start >> PAGE_SHIFT)) { |
968f2566 QW |
243 | index++; |
244 | continue; | |
245 | } | |
64e1db56 | 246 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
247 | index++; |
248 | if (!page) | |
249 | continue; | |
968f2566 QW |
250 | |
251 | /* | |
252 | * Here we just clear all Ordered bits for every page in the | |
711f447b | 253 | * range, then btrfs_mark_ordered_io_finished() will handle |
968f2566 QW |
254 | * the ordered extent accounting for the range. |
255 | */ | |
b945a463 QW |
256 | btrfs_page_clamp_clear_ordered(inode->root->fs_info, page, |
257 | offset, bytes); | |
63d71450 NA |
258 | put_page(page); |
259 | } | |
d1051d6e | 260 | |
99826e4c NA |
261 | if (locked_page) { |
262 | /* The locked page covers the full range, nothing needs to be done */ | |
263 | if (bytes + offset <= page_start + PAGE_SIZE) | |
264 | return; | |
265 | /* | |
266 | * In case this page belongs to the delalloc range being | |
267 | * instantiated then skip it, since the first page of a range is | |
268 | * going to be properly cleaned up by the caller of | |
269 | * run_delalloc_range | |
270 | */ | |
271 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
272 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; | |
273 | offset = page_offset(locked_page) + PAGE_SIZE; | |
274 | } | |
d1051d6e NB |
275 | } |
276 | ||
711f447b | 277 | return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false); |
52427260 QW |
278 | } |
279 | ||
48a3b636 | 280 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 281 | |
f34f57a3 | 282 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
3538d68d | 283 | struct btrfs_new_inode_args *args) |
0279b4cd JO |
284 | { |
285 | int err; | |
286 | ||
3538d68d OS |
287 | if (args->default_acl) { |
288 | err = __btrfs_set_acl(trans, args->inode, args->default_acl, | |
289 | ACL_TYPE_DEFAULT); | |
290 | if (err) | |
291 | return err; | |
292 | } | |
293 | if (args->acl) { | |
294 | err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS); | |
295 | if (err) | |
296 | return err; | |
297 | } | |
298 | if (!args->default_acl && !args->acl) | |
299 | cache_no_acl(args->inode); | |
300 | return btrfs_xattr_security_init(trans, args->inode, args->dir, | |
301 | &args->dentry->d_name); | |
0279b4cd JO |
302 | } |
303 | ||
c8b97818 CM |
304 | /* |
305 | * this does all the hard work for inserting an inline extent into | |
306 | * the btree. The caller should have done a btrfs_drop_extents so that | |
307 | * no overlapping inline items exist in the btree | |
308 | */ | |
40f76580 | 309 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
8dd9872d OS |
310 | struct btrfs_path *path, |
311 | struct btrfs_inode *inode, bool extent_inserted, | |
312 | size_t size, size_t compressed_size, | |
fe3f566c | 313 | int compress_type, |
d9496e8a OS |
314 | struct page **compressed_pages, |
315 | bool update_i_size) | |
c8b97818 | 316 | { |
8dd9872d | 317 | struct btrfs_root *root = inode->root; |
c8b97818 CM |
318 | struct extent_buffer *leaf; |
319 | struct page *page = NULL; | |
320 | char *kaddr; | |
321 | unsigned long ptr; | |
322 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
323 | int ret; |
324 | size_t cur_size = size; | |
d9496e8a | 325 | u64 i_size; |
c8b97818 | 326 | |
982f1f5d JJB |
327 | ASSERT((compressed_size > 0 && compressed_pages) || |
328 | (compressed_size == 0 && !compressed_pages)); | |
329 | ||
fe3f566c | 330 | if (compressed_size && compressed_pages) |
c8b97818 | 331 | cur_size = compressed_size; |
c8b97818 | 332 | |
1acae57b FDBM |
333 | if (!extent_inserted) { |
334 | struct btrfs_key key; | |
335 | size_t datasize; | |
c8b97818 | 336 | |
8dd9872d OS |
337 | key.objectid = btrfs_ino(inode); |
338 | key.offset = 0; | |
962a298f | 339 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 340 | |
1acae57b | 341 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
1acae57b FDBM |
342 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
343 | datasize); | |
79b4f4c6 | 344 | if (ret) |
1acae57b | 345 | goto fail; |
c8b97818 CM |
346 | } |
347 | leaf = path->nodes[0]; | |
348 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
349 | struct btrfs_file_extent_item); | |
350 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
351 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
352 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
353 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
354 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
355 | ptr = btrfs_file_extent_inline_start(ei); | |
356 | ||
261507a0 | 357 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
358 | struct page *cpage; |
359 | int i = 0; | |
d397712b | 360 | while (compressed_size > 0) { |
c8b97818 | 361 | cpage = compressed_pages[i]; |
5b050f04 | 362 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 363 | PAGE_SIZE); |
c8b97818 | 364 | |
4cb2e5e8 | 365 | kaddr = kmap_local_page(cpage); |
c8b97818 | 366 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
4cb2e5e8 | 367 | kunmap_local(kaddr); |
c8b97818 CM |
368 | |
369 | i++; | |
370 | ptr += cur_size; | |
371 | compressed_size -= cur_size; | |
372 | } | |
373 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 374 | compress_type); |
c8b97818 | 375 | } else { |
8dd9872d | 376 | page = find_get_page(inode->vfs_inode.i_mapping, 0); |
c8b97818 | 377 | btrfs_set_file_extent_compression(leaf, ei, 0); |
4cb2e5e8 | 378 | kaddr = kmap_local_page(page); |
8dd9872d | 379 | write_extent_buffer(leaf, kaddr, ptr, size); |
4cb2e5e8 | 380 | kunmap_local(kaddr); |
09cbfeaf | 381 | put_page(page); |
c8b97818 CM |
382 | } |
383 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 384 | btrfs_release_path(path); |
c8b97818 | 385 | |
9ddc959e JB |
386 | /* |
387 | * We align size to sectorsize for inline extents just for simplicity | |
388 | * sake. | |
389 | */ | |
8dd9872d OS |
390 | ret = btrfs_inode_set_file_extent_range(inode, 0, |
391 | ALIGN(size, root->fs_info->sectorsize)); | |
9ddc959e JB |
392 | if (ret) |
393 | goto fail; | |
394 | ||
c2167754 | 395 | /* |
d9496e8a OS |
396 | * We're an inline extent, so nobody can extend the file past i_size |
397 | * without locking a page we already have locked. | |
c2167754 | 398 | * |
d9496e8a OS |
399 | * We must do any i_size and inode updates before we unlock the pages. |
400 | * Otherwise we could end up racing with unlink. | |
c2167754 | 401 | */ |
d9496e8a OS |
402 | i_size = i_size_read(&inode->vfs_inode); |
403 | if (update_i_size && size > i_size) { | |
404 | i_size_write(&inode->vfs_inode, size); | |
405 | i_size = size; | |
406 | } | |
407 | inode->disk_i_size = i_size; | |
8dd9872d | 408 | |
c8b97818 | 409 | fail: |
79b4f4c6 | 410 | return ret; |
c8b97818 CM |
411 | } |
412 | ||
413 | ||
414 | /* | |
415 | * conditionally insert an inline extent into the file. This | |
416 | * does the checks required to make sure the data is small enough | |
417 | * to fit as an inline extent. | |
418 | */ | |
8dd9872d OS |
419 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, |
420 | size_t compressed_size, | |
00361589 | 421 | int compress_type, |
d9496e8a OS |
422 | struct page **compressed_pages, |
423 | bool update_i_size) | |
c8b97818 | 424 | { |
5893dfb9 | 425 | struct btrfs_drop_extents_args drop_args = { 0 }; |
a0349401 | 426 | struct btrfs_root *root = inode->root; |
0b246afa | 427 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 428 | struct btrfs_trans_handle *trans; |
8dd9872d | 429 | u64 data_len = (compressed_size ?: size); |
c8b97818 | 430 | int ret; |
1acae57b | 431 | struct btrfs_path *path; |
c8b97818 | 432 | |
8dd9872d OS |
433 | /* |
434 | * We can create an inline extent if it ends at or beyond the current | |
435 | * i_size, is no larger than a sector (decompressed), and the (possibly | |
436 | * compressed) data fits in a leaf and the configured maximum inline | |
437 | * size. | |
438 | */ | |
439 | if (size < i_size_read(&inode->vfs_inode) || | |
440 | size > fs_info->sectorsize || | |
0b246afa | 441 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
8dd9872d | 442 | data_len > fs_info->max_inline) |
c8b97818 | 443 | return 1; |
c8b97818 | 444 | |
1acae57b FDBM |
445 | path = btrfs_alloc_path(); |
446 | if (!path) | |
447 | return -ENOMEM; | |
448 | ||
00361589 | 449 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
450 | if (IS_ERR(trans)) { |
451 | btrfs_free_path(path); | |
00361589 | 452 | return PTR_ERR(trans); |
1acae57b | 453 | } |
a0349401 | 454 | trans->block_rsv = &inode->block_rsv; |
00361589 | 455 | |
5893dfb9 | 456 | drop_args.path = path; |
8dd9872d OS |
457 | drop_args.start = 0; |
458 | drop_args.end = fs_info->sectorsize; | |
5893dfb9 FM |
459 | drop_args.drop_cache = true; |
460 | drop_args.replace_extent = true; | |
8dd9872d | 461 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len); |
5893dfb9 | 462 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
00361589 | 463 | if (ret) { |
66642832 | 464 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
465 | goto out; |
466 | } | |
c8b97818 | 467 | |
8dd9872d OS |
468 | ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted, |
469 | size, compressed_size, compress_type, | |
d9496e8a | 470 | compressed_pages, update_i_size); |
2adcac1a | 471 | if (ret && ret != -ENOSPC) { |
66642832 | 472 | btrfs_abort_transaction(trans, ret); |
00361589 | 473 | goto out; |
2adcac1a | 474 | } else if (ret == -ENOSPC) { |
00361589 JB |
475 | ret = 1; |
476 | goto out; | |
79787eaa | 477 | } |
2adcac1a | 478 | |
8dd9872d | 479 | btrfs_update_inode_bytes(inode, size, drop_args.bytes_found); |
9a56fcd1 | 480 | ret = btrfs_update_inode(trans, root, inode); |
2766ff61 FM |
481 | if (ret && ret != -ENOSPC) { |
482 | btrfs_abort_transaction(trans, ret); | |
483 | goto out; | |
484 | } else if (ret == -ENOSPC) { | |
485 | ret = 1; | |
486 | goto out; | |
487 | } | |
488 | ||
23e3337f | 489 | btrfs_set_inode_full_sync(inode); |
00361589 | 490 | out: |
94ed938a QW |
491 | /* |
492 | * Don't forget to free the reserved space, as for inlined extent | |
493 | * it won't count as data extent, free them directly here. | |
494 | * And at reserve time, it's always aligned to page size, so | |
495 | * just free one page here. | |
496 | */ | |
a0349401 | 497 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 498 | btrfs_free_path(path); |
3a45bb20 | 499 | btrfs_end_transaction(trans); |
00361589 | 500 | return ret; |
c8b97818 CM |
501 | } |
502 | ||
771ed689 CM |
503 | struct async_extent { |
504 | u64 start; | |
505 | u64 ram_size; | |
506 | u64 compressed_size; | |
507 | struct page **pages; | |
508 | unsigned long nr_pages; | |
261507a0 | 509 | int compress_type; |
771ed689 CM |
510 | struct list_head list; |
511 | }; | |
512 | ||
97db1204 | 513 | struct async_chunk { |
771ed689 | 514 | struct inode *inode; |
771ed689 CM |
515 | struct page *locked_page; |
516 | u64 start; | |
517 | u64 end; | |
bf9486d6 | 518 | blk_opf_t write_flags; |
771ed689 | 519 | struct list_head extents; |
ec39f769 | 520 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 521 | struct btrfs_work work; |
9e895a8f | 522 | struct async_cow *async_cow; |
771ed689 CM |
523 | }; |
524 | ||
97db1204 | 525 | struct async_cow { |
97db1204 NB |
526 | atomic_t num_chunks; |
527 | struct async_chunk chunks[]; | |
771ed689 CM |
528 | }; |
529 | ||
97db1204 | 530 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
531 | u64 start, u64 ram_size, |
532 | u64 compressed_size, | |
533 | struct page **pages, | |
261507a0 LZ |
534 | unsigned long nr_pages, |
535 | int compress_type) | |
771ed689 CM |
536 | { |
537 | struct async_extent *async_extent; | |
538 | ||
539 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 540 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
541 | async_extent->start = start; |
542 | async_extent->ram_size = ram_size; | |
543 | async_extent->compressed_size = compressed_size; | |
544 | async_extent->pages = pages; | |
545 | async_extent->nr_pages = nr_pages; | |
261507a0 | 546 | async_extent->compress_type = compress_type; |
771ed689 CM |
547 | list_add_tail(&async_extent->list, &cow->extents); |
548 | return 0; | |
549 | } | |
550 | ||
42c16da6 QW |
551 | /* |
552 | * Check if the inode needs to be submitted to compression, based on mount | |
553 | * options, defragmentation, properties or heuristics. | |
554 | */ | |
808a1292 NB |
555 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
556 | u64 end) | |
f79707b0 | 557 | { |
808a1292 | 558 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 559 | |
e6f9d696 | 560 | if (!btrfs_inode_can_compress(inode)) { |
42c16da6 QW |
561 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
562 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 563 | btrfs_ino(inode)); |
42c16da6 QW |
564 | return 0; |
565 | } | |
0cf9b244 QW |
566 | /* |
567 | * Special check for subpage. | |
568 | * | |
569 | * We lock the full page then run each delalloc range in the page, thus | |
570 | * for the following case, we will hit some subpage specific corner case: | |
571 | * | |
572 | * 0 32K 64K | |
573 | * | |///////| |///////| | |
574 | * \- A \- B | |
575 | * | |
576 | * In above case, both range A and range B will try to unlock the full | |
577 | * page [0, 64K), causing the one finished later will have page | |
578 | * unlocked already, triggering various page lock requirement BUG_ON()s. | |
579 | * | |
580 | * So here we add an artificial limit that subpage compression can only | |
581 | * if the range is fully page aligned. | |
582 | * | |
583 | * In theory we only need to ensure the first page is fully covered, but | |
584 | * the tailing partial page will be locked until the full compression | |
585 | * finishes, delaying the write of other range. | |
586 | * | |
587 | * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range | |
588 | * first to prevent any submitted async extent to unlock the full page. | |
589 | * By this, we can ensure for subpage case that only the last async_cow | |
590 | * will unlock the full page. | |
591 | */ | |
592 | if (fs_info->sectorsize < PAGE_SIZE) { | |
1280d2d1 FK |
593 | if (!PAGE_ALIGNED(start) || |
594 | !PAGE_ALIGNED(end + 1)) | |
0cf9b244 QW |
595 | return 0; |
596 | } | |
597 | ||
f79707b0 | 598 | /* force compress */ |
0b246afa | 599 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 600 | return 1; |
eec63c65 | 601 | /* defrag ioctl */ |
808a1292 | 602 | if (inode->defrag_compress) |
eec63c65 | 603 | return 1; |
f79707b0 | 604 | /* bad compression ratios */ |
808a1292 | 605 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 606 | return 0; |
0b246afa | 607 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
608 | inode->flags & BTRFS_INODE_COMPRESS || |
609 | inode->prop_compress) | |
610 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
611 | return 0; |
612 | } | |
613 | ||
6158e1ce | 614 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
558732df | 615 | u64 start, u64 end, u64 num_bytes, u32 small_write) |
26d30f85 AJ |
616 | { |
617 | /* If this is a small write inside eof, kick off a defrag */ | |
618 | if (num_bytes < small_write && | |
6158e1ce | 619 | (start > 0 || end + 1 < inode->disk_i_size)) |
558732df | 620 | btrfs_add_inode_defrag(NULL, inode, small_write); |
26d30f85 AJ |
621 | } |
622 | ||
d352ac68 | 623 | /* |
771ed689 CM |
624 | * we create compressed extents in two phases. The first |
625 | * phase compresses a range of pages that have already been | |
626 | * locked (both pages and state bits are locked). | |
c8b97818 | 627 | * |
771ed689 CM |
628 | * This is done inside an ordered work queue, and the compression |
629 | * is spread across many cpus. The actual IO submission is step | |
630 | * two, and the ordered work queue takes care of making sure that | |
631 | * happens in the same order things were put onto the queue by | |
632 | * writepages and friends. | |
c8b97818 | 633 | * |
771ed689 CM |
634 | * If this code finds it can't get good compression, it puts an |
635 | * entry onto the work queue to write the uncompressed bytes. This | |
636 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
637 | * are written in the same order that the flusher thread sent them |
638 | * down. | |
d352ac68 | 639 | */ |
ac3e9933 | 640 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 641 | { |
1368c6da | 642 | struct inode *inode = async_chunk->inode; |
0b246afa | 643 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 644 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
645 | u64 start = async_chunk->start; |
646 | u64 end = async_chunk->end; | |
c8b97818 | 647 | u64 actual_end; |
d98da499 | 648 | u64 i_size; |
e6dcd2dc | 649 | int ret = 0; |
c8b97818 CM |
650 | struct page **pages = NULL; |
651 | unsigned long nr_pages; | |
c8b97818 CM |
652 | unsigned long total_compressed = 0; |
653 | unsigned long total_in = 0; | |
c8b97818 CM |
654 | int i; |
655 | int will_compress; | |
0b246afa | 656 | int compress_type = fs_info->compress_type; |
ac3e9933 | 657 | int compressed_extents = 0; |
4adaa611 | 658 | int redirty = 0; |
b888db2b | 659 | |
6158e1ce NB |
660 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
661 | SZ_16K); | |
4cb5300b | 662 | |
d98da499 JB |
663 | /* |
664 | * We need to save i_size before now because it could change in between | |
665 | * us evaluating the size and assigning it. This is because we lock and | |
666 | * unlock the page in truncate and fallocate, and then modify the i_size | |
667 | * later on. | |
668 | * | |
669 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
670 | * does that for us. | |
671 | */ | |
672 | barrier(); | |
673 | i_size = i_size_read(inode); | |
674 | barrier(); | |
675 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
676 | again: |
677 | will_compress = 0; | |
09cbfeaf | 678 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
679 | nr_pages = min_t(unsigned long, nr_pages, |
680 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 681 | |
f03d9301 CM |
682 | /* |
683 | * we don't want to send crud past the end of i_size through | |
684 | * compression, that's just a waste of CPU time. So, if the | |
685 | * end of the file is before the start of our current | |
686 | * requested range of bytes, we bail out to the uncompressed | |
687 | * cleanup code that can deal with all of this. | |
688 | * | |
689 | * It isn't really the fastest way to fix things, but this is a | |
690 | * very uncommon corner. | |
691 | */ | |
692 | if (actual_end <= start) | |
693 | goto cleanup_and_bail_uncompressed; | |
694 | ||
c8b97818 CM |
695 | total_compressed = actual_end - start; |
696 | ||
4bcbb332 | 697 | /* |
0cf9b244 | 698 | * Skip compression for a small file range(<=blocksize) that |
01327610 | 699 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
700 | */ |
701 | if (total_compressed <= blocksize && | |
702 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
703 | goto cleanup_and_bail_uncompressed; | |
704 | ||
0cf9b244 QW |
705 | /* |
706 | * For subpage case, we require full page alignment for the sector | |
707 | * aligned range. | |
708 | * Thus we must also check against @actual_end, not just @end. | |
709 | */ | |
710 | if (blocksize < PAGE_SIZE) { | |
1280d2d1 FK |
711 | if (!PAGE_ALIGNED(start) || |
712 | !PAGE_ALIGNED(round_up(actual_end, blocksize))) | |
0cf9b244 QW |
713 | goto cleanup_and_bail_uncompressed; |
714 | } | |
715 | ||
069eac78 DS |
716 | total_compressed = min_t(unsigned long, total_compressed, |
717 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
718 | total_in = 0; |
719 | ret = 0; | |
db94535d | 720 | |
771ed689 CM |
721 | /* |
722 | * we do compression for mount -o compress and when the | |
723 | * inode has not been flagged as nocompress. This flag can | |
724 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 725 | */ |
4e965576 | 726 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
c8b97818 | 727 | WARN_ON(pages); |
31e818fe | 728 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
729 | if (!pages) { |
730 | /* just bail out to the uncompressed code */ | |
3527a018 | 731 | nr_pages = 0; |
560f7d75 LZ |
732 | goto cont; |
733 | } | |
c8b97818 | 734 | |
eec63c65 DS |
735 | if (BTRFS_I(inode)->defrag_compress) |
736 | compress_type = BTRFS_I(inode)->defrag_compress; | |
737 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 738 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 739 | |
4adaa611 CM |
740 | /* |
741 | * we need to call clear_page_dirty_for_io on each | |
742 | * page in the range. Otherwise applications with the file | |
743 | * mmap'd can wander in and change the page contents while | |
744 | * we are compressing them. | |
745 | * | |
746 | * If the compression fails for any reason, we set the pages | |
747 | * dirty again later on. | |
e9679de3 TT |
748 | * |
749 | * Note that the remaining part is redirtied, the start pointer | |
750 | * has moved, the end is the original one. | |
4adaa611 | 751 | */ |
e9679de3 TT |
752 | if (!redirty) { |
753 | extent_range_clear_dirty_for_io(inode, start, end); | |
754 | redirty = 1; | |
755 | } | |
f51d2b59 DS |
756 | |
757 | /* Compression level is applied here and only here */ | |
758 | ret = btrfs_compress_pages( | |
759 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 760 | inode->i_mapping, start, |
38c31464 | 761 | pages, |
4d3a800e | 762 | &nr_pages, |
261507a0 | 763 | &total_in, |
e5d74902 | 764 | &total_compressed); |
c8b97818 CM |
765 | |
766 | if (!ret) { | |
7073017a | 767 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 768 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
769 | |
770 | /* zero the tail end of the last page, we might be | |
771 | * sending it down to disk | |
772 | */ | |
d048b9c2 IW |
773 | if (offset) |
774 | memzero_page(page, offset, PAGE_SIZE - offset); | |
c8b97818 CM |
775 | will_compress = 1; |
776 | } | |
777 | } | |
560f7d75 | 778 | cont: |
7367253a QW |
779 | /* |
780 | * Check cow_file_range() for why we don't even try to create inline | |
781 | * extent for subpage case. | |
782 | */ | |
783 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
c8b97818 | 784 | /* lets try to make an inline extent */ |
6018ba0a | 785 | if (ret || total_in < actual_end) { |
c8b97818 | 786 | /* we didn't compress the entire range, try |
771ed689 | 787 | * to make an uncompressed inline extent. |
c8b97818 | 788 | */ |
8dd9872d | 789 | ret = cow_file_range_inline(BTRFS_I(inode), actual_end, |
a0349401 | 790 | 0, BTRFS_COMPRESS_NONE, |
d9496e8a | 791 | NULL, false); |
c8b97818 | 792 | } else { |
771ed689 | 793 | /* try making a compressed inline extent */ |
8dd9872d | 794 | ret = cow_file_range_inline(BTRFS_I(inode), actual_end, |
fe3f566c | 795 | total_compressed, |
d9496e8a OS |
796 | compress_type, pages, |
797 | false); | |
c8b97818 | 798 | } |
79787eaa | 799 | if (ret <= 0) { |
151a41bc | 800 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
801 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
802 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
803 | unsigned long page_error_op; |
804 | ||
e6eb4314 | 805 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 806 | |
771ed689 | 807 | /* |
79787eaa JM |
808 | * inline extent creation worked or returned error, |
809 | * we don't need to create any more async work items. | |
810 | * Unlock and free up our temp pages. | |
8b62f87b JB |
811 | * |
812 | * We use DO_ACCOUNTING here because we need the | |
813 | * delalloc_release_metadata to be done _after_ we drop | |
814 | * our outstanding extent for clearing delalloc for this | |
815 | * range. | |
771ed689 | 816 | */ |
ad7ff17b NB |
817 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
818 | NULL, | |
74e9194a | 819 | clear_flags, |
ba8b04c1 | 820 | PAGE_UNLOCK | |
6869b0a8 | 821 | PAGE_START_WRITEBACK | |
e6eb4314 | 822 | page_error_op | |
c2790a2e | 823 | PAGE_END_WRITEBACK); |
cecc8d90 | 824 | |
1e6e238c QW |
825 | /* |
826 | * Ensure we only free the compressed pages if we have | |
827 | * them allocated, as we can still reach here with | |
828 | * inode_need_compress() == false. | |
829 | */ | |
830 | if (pages) { | |
831 | for (i = 0; i < nr_pages; i++) { | |
832 | WARN_ON(pages[i]->mapping); | |
833 | put_page(pages[i]); | |
834 | } | |
835 | kfree(pages); | |
cecc8d90 | 836 | } |
cecc8d90 | 837 | return 0; |
c8b97818 CM |
838 | } |
839 | } | |
840 | ||
841 | if (will_compress) { | |
842 | /* | |
843 | * we aren't doing an inline extent round the compressed size | |
844 | * up to a block size boundary so the allocator does sane | |
845 | * things | |
846 | */ | |
fda2832f | 847 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
848 | |
849 | /* | |
850 | * one last check to make sure the compression is really a | |
170607eb TT |
851 | * win, compare the page count read with the blocks on disk, |
852 | * compression must free at least one sector size | |
c8b97818 | 853 | */ |
4c162778 | 854 | total_in = round_up(total_in, fs_info->sectorsize); |
170607eb | 855 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 856 | compressed_extents++; |
c8bb0c8b AS |
857 | |
858 | /* | |
859 | * The async work queues will take care of doing actual | |
860 | * allocation on disk for these compressed pages, and | |
861 | * will submit them to the elevator. | |
862 | */ | |
b5326271 | 863 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 864 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
865 | compress_type); |
866 | ||
1170862d TT |
867 | if (start + total_in < end) { |
868 | start += total_in; | |
c8bb0c8b AS |
869 | pages = NULL; |
870 | cond_resched(); | |
871 | goto again; | |
872 | } | |
ac3e9933 | 873 | return compressed_extents; |
c8b97818 CM |
874 | } |
875 | } | |
c8bb0c8b | 876 | if (pages) { |
c8b97818 CM |
877 | /* |
878 | * the compression code ran but failed to make things smaller, | |
879 | * free any pages it allocated and our page pointer array | |
880 | */ | |
4d3a800e | 881 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 882 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 883 | put_page(pages[i]); |
c8b97818 CM |
884 | } |
885 | kfree(pages); | |
886 | pages = NULL; | |
887 | total_compressed = 0; | |
4d3a800e | 888 | nr_pages = 0; |
c8b97818 CM |
889 | |
890 | /* flag the file so we don't compress in the future */ | |
0b246afa | 891 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 892 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 893 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 894 | } |
c8b97818 | 895 | } |
f03d9301 | 896 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
897 | /* |
898 | * No compression, but we still need to write the pages in the file | |
899 | * we've been given so far. redirty the locked page if it corresponds | |
900 | * to our extent and set things up for the async work queue to run | |
901 | * cow_file_range to do the normal delalloc dance. | |
902 | */ | |
1d53c9e6 CM |
903 | if (async_chunk->locked_page && |
904 | (page_offset(async_chunk->locked_page) >= start && | |
905 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 906 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 907 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 908 | } |
c8bb0c8b AS |
909 | |
910 | if (redirty) | |
911 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 912 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 913 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 914 | compressed_extents++; |
3b951516 | 915 | |
ac3e9933 | 916 | return compressed_extents; |
771ed689 | 917 | } |
771ed689 | 918 | |
40ae837b FM |
919 | static void free_async_extent_pages(struct async_extent *async_extent) |
920 | { | |
921 | int i; | |
922 | ||
923 | if (!async_extent->pages) | |
924 | return; | |
925 | ||
926 | for (i = 0; i < async_extent->nr_pages; i++) { | |
927 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 928 | put_page(async_extent->pages[i]); |
40ae837b FM |
929 | } |
930 | kfree(async_extent->pages); | |
931 | async_extent->nr_pages = 0; | |
932 | async_extent->pages = NULL; | |
771ed689 CM |
933 | } |
934 | ||
2b83a0ee QW |
935 | static int submit_uncompressed_range(struct btrfs_inode *inode, |
936 | struct async_extent *async_extent, | |
937 | struct page *locked_page) | |
771ed689 | 938 | { |
2b83a0ee QW |
939 | u64 start = async_extent->start; |
940 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
941 | unsigned long nr_written = 0; | |
942 | int page_started = 0; | |
943 | int ret; | |
771ed689 | 944 | |
2b83a0ee QW |
945 | /* |
946 | * Call cow_file_range() to run the delalloc range directly, since we | |
947 | * won't go to NOCOW or async path again. | |
948 | * | |
949 | * Also we call cow_file_range() with @unlock_page == 0, so that we | |
950 | * can directly submit them without interruption. | |
951 | */ | |
952 | ret = cow_file_range(inode, locked_page, start, end, &page_started, | |
898793d9 | 953 | &nr_written, 0, NULL); |
2b83a0ee QW |
954 | /* Inline extent inserted, page gets unlocked and everything is done */ |
955 | if (page_started) { | |
956 | ret = 0; | |
957 | goto out; | |
958 | } | |
959 | if (ret < 0) { | |
71aa147b NA |
960 | btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1); |
961 | if (locked_page) { | |
962 | const u64 page_start = page_offset(locked_page); | |
963 | const u64 page_end = page_start + PAGE_SIZE - 1; | |
964 | ||
965 | btrfs_page_set_error(inode->root->fs_info, locked_page, | |
966 | page_start, PAGE_SIZE); | |
967 | set_page_writeback(locked_page); | |
968 | end_page_writeback(locked_page); | |
969 | end_extent_writepage(locked_page, ret, page_start, page_end); | |
2b83a0ee | 970 | unlock_page(locked_page); |
71aa147b | 971 | } |
2b83a0ee QW |
972 | goto out; |
973 | } | |
771ed689 | 974 | |
2b83a0ee QW |
975 | ret = extent_write_locked_range(&inode->vfs_inode, start, end); |
976 | /* All pages will be unlocked, including @locked_page */ | |
977 | out: | |
978 | kfree(async_extent); | |
979 | return ret; | |
980 | } | |
79787eaa | 981 | |
b4ccace8 QW |
982 | static int submit_one_async_extent(struct btrfs_inode *inode, |
983 | struct async_chunk *async_chunk, | |
984 | struct async_extent *async_extent, | |
985 | u64 *alloc_hint) | |
771ed689 | 986 | { |
b4ccace8 QW |
987 | struct extent_io_tree *io_tree = &inode->io_tree; |
988 | struct btrfs_root *root = inode->root; | |
989 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 | 990 | struct btrfs_key ins; |
2b83a0ee | 991 | struct page *locked_page = NULL; |
771ed689 | 992 | struct extent_map *em; |
f5a84ee3 | 993 | int ret = 0; |
b4ccace8 QW |
994 | u64 start = async_extent->start; |
995 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
771ed689 | 996 | |
2b83a0ee QW |
997 | /* |
998 | * If async_chunk->locked_page is in the async_extent range, we need to | |
999 | * handle it. | |
1000 | */ | |
1001 | if (async_chunk->locked_page) { | |
1002 | u64 locked_page_start = page_offset(async_chunk->locked_page); | |
1003 | u64 locked_page_end = locked_page_start + PAGE_SIZE - 1; | |
3e04e7f1 | 1004 | |
2b83a0ee QW |
1005 | if (!(start >= locked_page_end || end <= locked_page_start)) |
1006 | locked_page = async_chunk->locked_page; | |
b4ccace8 | 1007 | } |
570eb97b | 1008 | lock_extent(io_tree, start, end, NULL); |
ce62003f | 1009 | |
2b83a0ee QW |
1010 | /* We have fall back to uncompressed write */ |
1011 | if (!async_extent->pages) | |
1012 | return submit_uncompressed_range(inode, async_extent, locked_page); | |
ce62003f | 1013 | |
b4ccace8 QW |
1014 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
1015 | async_extent->compressed_size, | |
1016 | async_extent->compressed_size, | |
1017 | 0, *alloc_hint, &ins, 1, 1); | |
1018 | if (ret) { | |
1019 | free_async_extent_pages(async_extent); | |
c2167754 | 1020 | /* |
b4ccace8 QW |
1021 | * Here we used to try again by going back to non-compressed |
1022 | * path for ENOSPC. But we can't reserve space even for | |
1023 | * compressed size, how could it work for uncompressed size | |
1024 | * which requires larger size? So here we directly go error | |
1025 | * path. | |
c2167754 | 1026 | */ |
b4ccace8 QW |
1027 | goto out_free; |
1028 | } | |
1029 | ||
1030 | /* Here we're doing allocation and writeback of the compressed pages */ | |
1031 | em = create_io_em(inode, start, | |
1032 | async_extent->ram_size, /* len */ | |
1033 | start, /* orig_start */ | |
1034 | ins.objectid, /* block_start */ | |
1035 | ins.offset, /* block_len */ | |
1036 | ins.offset, /* orig_block_len */ | |
1037 | async_extent->ram_size, /* ram_bytes */ | |
1038 | async_extent->compress_type, | |
1039 | BTRFS_ORDERED_COMPRESSED); | |
1040 | if (IS_ERR(em)) { | |
1041 | ret = PTR_ERR(em); | |
1042 | goto out_free_reserve; | |
1043 | } | |
1044 | free_extent_map(em); | |
771ed689 | 1045 | |
cb36a9bb OS |
1046 | ret = btrfs_add_ordered_extent(inode, start, /* file_offset */ |
1047 | async_extent->ram_size, /* num_bytes */ | |
1048 | async_extent->ram_size, /* ram_bytes */ | |
1049 | ins.objectid, /* disk_bytenr */ | |
1050 | ins.offset, /* disk_num_bytes */ | |
1051 | 0, /* offset */ | |
1052 | 1 << BTRFS_ORDERED_COMPRESSED, | |
1053 | async_extent->compress_type); | |
b4ccace8 | 1054 | if (ret) { |
4c0c8cfc | 1055 | btrfs_drop_extent_map_range(inode, start, end, false); |
b4ccace8 | 1056 | goto out_free_reserve; |
771ed689 | 1057 | } |
b4ccace8 QW |
1058 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
1059 | ||
1060 | /* Clear dirty, set writeback and unlock the pages. */ | |
1061 | extent_clear_unlock_delalloc(inode, start, end, | |
1062 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, | |
1063 | PAGE_UNLOCK | PAGE_START_WRITEBACK); | |
1064 | if (btrfs_submit_compressed_write(inode, start, /* file_offset */ | |
1065 | async_extent->ram_size, /* num_bytes */ | |
1066 | ins.objectid, /* disk_bytenr */ | |
1067 | ins.offset, /* compressed_len */ | |
1068 | async_extent->pages, /* compressed_pages */ | |
1069 | async_extent->nr_pages, | |
1070 | async_chunk->write_flags, | |
7c0c7269 | 1071 | async_chunk->blkcg_css, true)) { |
b4ccace8 QW |
1072 | const u64 start = async_extent->start; |
1073 | const u64 end = start + async_extent->ram_size - 1; | |
1074 | ||
1075 | btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0); | |
1076 | ||
1077 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
1078 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); | |
1079 | free_async_extent_pages(async_extent); | |
771ed689 | 1080 | } |
b4ccace8 QW |
1081 | *alloc_hint = ins.objectid + ins.offset; |
1082 | kfree(async_extent); | |
1083 | return ret; | |
1084 | ||
3e04e7f1 | 1085 | out_free_reserve: |
0b246afa | 1086 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1087 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1088 | out_free: |
b4ccace8 | 1089 | extent_clear_unlock_delalloc(inode, start, end, |
c2790a2e | 1090 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 1091 | EXTENT_DELALLOC_NEW | |
151a41bc | 1092 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
6869b0a8 QW |
1093 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
1094 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); | |
40ae837b | 1095 | free_async_extent_pages(async_extent); |
79787eaa | 1096 | kfree(async_extent); |
b4ccace8 QW |
1097 | return ret; |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Phase two of compressed writeback. This is the ordered portion of the code, | |
1102 | * which only gets called in the order the work was queued. We walk all the | |
1103 | * async extents created by compress_file_range and send them down to the disk. | |
1104 | */ | |
1105 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) | |
1106 | { | |
1107 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); | |
1108 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1109 | struct async_extent *async_extent; | |
1110 | u64 alloc_hint = 0; | |
1111 | int ret = 0; | |
1112 | ||
1113 | while (!list_empty(&async_chunk->extents)) { | |
1114 | u64 extent_start; | |
1115 | u64 ram_size; | |
1116 | ||
1117 | async_extent = list_entry(async_chunk->extents.next, | |
1118 | struct async_extent, list); | |
1119 | list_del(&async_extent->list); | |
1120 | extent_start = async_extent->start; | |
1121 | ram_size = async_extent->ram_size; | |
1122 | ||
1123 | ret = submit_one_async_extent(inode, async_chunk, async_extent, | |
1124 | &alloc_hint); | |
1125 | btrfs_debug(fs_info, | |
1126 | "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d", | |
1127 | inode->root->root_key.objectid, | |
1128 | btrfs_ino(inode), extent_start, ram_size, ret); | |
1129 | } | |
771ed689 CM |
1130 | } |
1131 | ||
43c69849 | 1132 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
1133 | u64 num_bytes) |
1134 | { | |
43c69849 | 1135 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
1136 | struct extent_map *em; |
1137 | u64 alloc_hint = 0; | |
1138 | ||
1139 | read_lock(&em_tree->lock); | |
1140 | em = search_extent_mapping(em_tree, start, num_bytes); | |
1141 | if (em) { | |
1142 | /* | |
1143 | * if block start isn't an actual block number then find the | |
1144 | * first block in this inode and use that as a hint. If that | |
1145 | * block is also bogus then just don't worry about it. | |
1146 | */ | |
1147 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
1148 | free_extent_map(em); | |
1149 | em = search_extent_mapping(em_tree, 0, 0); | |
1150 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
1151 | alloc_hint = em->block_start; | |
1152 | if (em) | |
1153 | free_extent_map(em); | |
1154 | } else { | |
1155 | alloc_hint = em->block_start; | |
1156 | free_extent_map(em); | |
1157 | } | |
1158 | } | |
1159 | read_unlock(&em_tree->lock); | |
1160 | ||
1161 | return alloc_hint; | |
1162 | } | |
1163 | ||
771ed689 CM |
1164 | /* |
1165 | * when extent_io.c finds a delayed allocation range in the file, | |
1166 | * the call backs end up in this code. The basic idea is to | |
1167 | * allocate extents on disk for the range, and create ordered data structs | |
1168 | * in ram to track those extents. | |
1169 | * | |
1170 | * locked_page is the page that writepage had locked already. We use | |
1171 | * it to make sure we don't do extra locks or unlocks. | |
1172 | * | |
1173 | * *page_started is set to one if we unlock locked_page and do everything | |
1174 | * required to start IO on it. It may be clean and already done with | |
1175 | * IO when we return. | |
9ce7466f NA |
1176 | * |
1177 | * When unlock == 1, we unlock the pages in successfully allocated regions. | |
1178 | * When unlock == 0, we leave them locked for writing them out. | |
1179 | * | |
1180 | * However, we unlock all the pages except @locked_page in case of failure. | |
1181 | * | |
1182 | * In summary, page locking state will be as follow: | |
1183 | * | |
1184 | * - page_started == 1 (return value) | |
1185 | * - All the pages are unlocked. IO is started. | |
1186 | * - Note that this can happen only on success | |
1187 | * - unlock == 1 | |
1188 | * - All the pages except @locked_page are unlocked in any case | |
1189 | * - unlock == 0 | |
1190 | * - On success, all the pages are locked for writing out them | |
1191 | * - On failure, all the pages except @locked_page are unlocked | |
1192 | * | |
1193 | * When a failure happens in the second or later iteration of the | |
1194 | * while-loop, the ordered extents created in previous iterations are kept | |
1195 | * intact. So, the caller must clean them up by calling | |
1196 | * btrfs_cleanup_ordered_extents(). See btrfs_run_delalloc_range() for | |
1197 | * example. | |
771ed689 | 1198 | */ |
6e26c442 | 1199 | static noinline int cow_file_range(struct btrfs_inode *inode, |
00361589 | 1200 | struct page *locked_page, |
74e9194a | 1201 | u64 start, u64 end, int *page_started, |
898793d9 NA |
1202 | unsigned long *nr_written, int unlock, |
1203 | u64 *done_offset) | |
771ed689 | 1204 | { |
6e26c442 NB |
1205 | struct btrfs_root *root = inode->root; |
1206 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 | 1207 | u64 alloc_hint = 0; |
9ce7466f | 1208 | u64 orig_start = start; |
771ed689 CM |
1209 | u64 num_bytes; |
1210 | unsigned long ram_size; | |
a315e68f | 1211 | u64 cur_alloc_size = 0; |
432cd2a1 | 1212 | u64 min_alloc_size; |
0b246afa | 1213 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
1214 | struct btrfs_key ins; |
1215 | struct extent_map *em; | |
a315e68f FM |
1216 | unsigned clear_bits; |
1217 | unsigned long page_ops; | |
1218 | bool extent_reserved = false; | |
771ed689 CM |
1219 | int ret = 0; |
1220 | ||
6e26c442 | 1221 | if (btrfs_is_free_space_inode(inode)) { |
29bce2f3 JB |
1222 | ret = -EINVAL; |
1223 | goto out_unlock; | |
02ecd2c2 | 1224 | } |
771ed689 | 1225 | |
fda2832f | 1226 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1227 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1228 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1229 | |
6e26c442 | 1230 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1231 | |
7367253a QW |
1232 | /* |
1233 | * Due to the page size limit, for subpage we can only trigger the | |
1234 | * writeback for the dirty sectors of page, that means data writeback | |
1235 | * is doing more writeback than what we want. | |
1236 | * | |
1237 | * This is especially unexpected for some call sites like fallocate, | |
1238 | * where we only increase i_size after everything is done. | |
1239 | * This means we can trigger inline extent even if we didn't want to. | |
1240 | * So here we skip inline extent creation completely. | |
1241 | */ | |
1242 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
8dd9872d OS |
1243 | u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode), |
1244 | end + 1); | |
1245 | ||
771ed689 | 1246 | /* lets try to make an inline extent */ |
8dd9872d | 1247 | ret = cow_file_range_inline(inode, actual_end, 0, |
d9496e8a | 1248 | BTRFS_COMPRESS_NONE, NULL, false); |
771ed689 | 1249 | if (ret == 0) { |
8b62f87b JB |
1250 | /* |
1251 | * We use DO_ACCOUNTING here because we need the | |
1252 | * delalloc_release_metadata to be run _after_ we drop | |
1253 | * our outstanding extent for clearing delalloc for this | |
1254 | * range. | |
1255 | */ | |
4750af3b QW |
1256 | extent_clear_unlock_delalloc(inode, start, end, |
1257 | locked_page, | |
c2790a2e | 1258 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1259 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1260 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 1261 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
771ed689 | 1262 | *nr_written = *nr_written + |
09cbfeaf | 1263 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1264 | *page_started = 1; |
4750af3b QW |
1265 | /* |
1266 | * locked_page is locked by the caller of | |
1267 | * writepage_delalloc(), not locked by | |
1268 | * __process_pages_contig(). | |
1269 | * | |
1270 | * We can't let __process_pages_contig() to unlock it, | |
1271 | * as it doesn't have any subpage::writers recorded. | |
1272 | * | |
1273 | * Here we manually unlock the page, since the caller | |
1274 | * can't use page_started to determine if it's an | |
1275 | * inline extent or a compressed extent. | |
1276 | */ | |
1277 | unlock_page(locked_page); | |
771ed689 | 1278 | goto out; |
79787eaa | 1279 | } else if (ret < 0) { |
79787eaa | 1280 | goto out_unlock; |
771ed689 CM |
1281 | } |
1282 | } | |
1283 | ||
6e26c442 | 1284 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 | 1285 | |
432cd2a1 FM |
1286 | /* |
1287 | * Relocation relies on the relocated extents to have exactly the same | |
1288 | * size as the original extents. Normally writeback for relocation data | |
1289 | * extents follows a NOCOW path because relocation preallocates the | |
1290 | * extents. However, due to an operation such as scrub turning a block | |
1291 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1292 | * an extent allocated during COW has exactly the requested size and can | |
1293 | * not be split into smaller extents, otherwise relocation breaks and | |
1294 | * fails during the stage where it updates the bytenr of file extent | |
1295 | * items. | |
1296 | */ | |
37f00a6d | 1297 | if (btrfs_is_data_reloc_root(root)) |
432cd2a1 FM |
1298 | min_alloc_size = num_bytes; |
1299 | else | |
1300 | min_alloc_size = fs_info->sectorsize; | |
1301 | ||
3752d22f AJ |
1302 | while (num_bytes > 0) { |
1303 | cur_alloc_size = num_bytes; | |
18513091 | 1304 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1305 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1306 | &ins, 1, 1); |
00361589 | 1307 | if (ret < 0) |
79787eaa | 1308 | goto out_unlock; |
a315e68f FM |
1309 | cur_alloc_size = ins.offset; |
1310 | extent_reserved = true; | |
d397712b | 1311 | |
771ed689 | 1312 | ram_size = ins.offset; |
6e26c442 | 1313 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1314 | start, /* orig_start */ |
1315 | ins.objectid, /* block_start */ | |
1316 | ins.offset, /* block_len */ | |
1317 | ins.offset, /* orig_block_len */ | |
1318 | ram_size, /* ram_bytes */ | |
1319 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1320 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1321 | if (IS_ERR(em)) { |
1322 | ret = PTR_ERR(em); | |
ace68bac | 1323 | goto out_reserve; |
090a127a | 1324 | } |
6f9994db | 1325 | free_extent_map(em); |
e6dcd2dc | 1326 | |
cb36a9bb OS |
1327 | ret = btrfs_add_ordered_extent(inode, start, ram_size, ram_size, |
1328 | ins.objectid, cur_alloc_size, 0, | |
1329 | 1 << BTRFS_ORDERED_REGULAR, | |
1330 | BTRFS_COMPRESS_NONE); | |
ace68bac | 1331 | if (ret) |
d9f85963 | 1332 | goto out_drop_extent_cache; |
c8b97818 | 1333 | |
37f00a6d | 1334 | if (btrfs_is_data_reloc_root(root)) { |
6e26c442 | 1335 | ret = btrfs_reloc_clone_csums(inode, start, |
17d217fe | 1336 | cur_alloc_size); |
4dbd80fb QW |
1337 | /* |
1338 | * Only drop cache here, and process as normal. | |
1339 | * | |
1340 | * We must not allow extent_clear_unlock_delalloc() | |
1341 | * at out_unlock label to free meta of this ordered | |
1342 | * extent, as its meta should be freed by | |
1343 | * btrfs_finish_ordered_io(). | |
1344 | * | |
1345 | * So we must continue until @start is increased to | |
1346 | * skip current ordered extent. | |
1347 | */ | |
00361589 | 1348 | if (ret) |
4c0c8cfc FM |
1349 | btrfs_drop_extent_map_range(inode, start, |
1350 | start + ram_size - 1, | |
1351 | false); | |
17d217fe YZ |
1352 | } |
1353 | ||
0b246afa | 1354 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1355 | |
f57ad937 QW |
1356 | /* |
1357 | * We're not doing compressed IO, don't unlock the first page | |
1358 | * (which the caller expects to stay locked), don't clear any | |
1359 | * dirty bits and don't set any writeback bits | |
8b62b72b | 1360 | * |
f57ad937 QW |
1361 | * Do set the Ordered (Private2) bit so we know this page was |
1362 | * properly setup for writepage. | |
c8b97818 | 1363 | */ |
a315e68f | 1364 | page_ops = unlock ? PAGE_UNLOCK : 0; |
f57ad937 | 1365 | page_ops |= PAGE_SET_ORDERED; |
a791e35e | 1366 | |
6e26c442 | 1367 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1368 | locked_page, |
c2790a2e | 1369 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1370 | page_ops); |
3752d22f AJ |
1371 | if (num_bytes < cur_alloc_size) |
1372 | num_bytes = 0; | |
4dbd80fb | 1373 | else |
3752d22f | 1374 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1375 | alloc_hint = ins.objectid + ins.offset; |
1376 | start += cur_alloc_size; | |
a315e68f | 1377 | extent_reserved = false; |
4dbd80fb QW |
1378 | |
1379 | /* | |
1380 | * btrfs_reloc_clone_csums() error, since start is increased | |
1381 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1382 | * free metadata of current ordered extent, we're OK to exit. | |
1383 | */ | |
1384 | if (ret) | |
1385 | goto out_unlock; | |
b888db2b | 1386 | } |
79787eaa | 1387 | out: |
be20aa9d | 1388 | return ret; |
b7d5b0a8 | 1389 | |
d9f85963 | 1390 | out_drop_extent_cache: |
4c0c8cfc | 1391 | btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false); |
ace68bac | 1392 | out_reserve: |
0b246afa | 1393 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1394 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1395 | out_unlock: |
898793d9 NA |
1396 | /* |
1397 | * If done_offset is non-NULL and ret == -EAGAIN, we expect the | |
1398 | * caller to write out the successfully allocated region and retry. | |
1399 | */ | |
1400 | if (done_offset && ret == -EAGAIN) { | |
1401 | if (orig_start < start) | |
1402 | *done_offset = start - 1; | |
1403 | else | |
1404 | *done_offset = start; | |
1405 | return ret; | |
1406 | } else if (ret == -EAGAIN) { | |
1407 | /* Convert to -ENOSPC since the caller cannot retry. */ | |
1408 | ret = -ENOSPC; | |
1409 | } | |
1410 | ||
9ce7466f NA |
1411 | /* |
1412 | * Now, we have three regions to clean up: | |
1413 | * | |
1414 | * |-------(1)----|---(2)---|-------------(3)----------| | |
1415 | * `- orig_start `- start `- start + cur_alloc_size `- end | |
1416 | * | |
1417 | * We process each region below. | |
1418 | */ | |
1419 | ||
a7e3b975 FM |
1420 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1421 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
6869b0a8 | 1422 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
9ce7466f | 1423 | |
a315e68f | 1424 | /* |
9ce7466f NA |
1425 | * For the range (1). We have already instantiated the ordered extents |
1426 | * for this region. They are cleaned up by | |
1427 | * btrfs_cleanup_ordered_extents() in e.g, | |
1428 | * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are | |
1429 | * already cleared in the above loop. And, EXTENT_DELALLOC_NEW | | |
1430 | * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup | |
1431 | * function. | |
1432 | * | |
1433 | * However, in case of unlock == 0, we still need to unlock the pages | |
1434 | * (except @locked_page) to ensure all the pages are unlocked. | |
1435 | */ | |
71aa147b NA |
1436 | if (!unlock && orig_start < start) { |
1437 | if (!locked_page) | |
1438 | mapping_set_error(inode->vfs_inode.i_mapping, ret); | |
9ce7466f NA |
1439 | extent_clear_unlock_delalloc(inode, orig_start, start - 1, |
1440 | locked_page, 0, page_ops); | |
71aa147b | 1441 | } |
9ce7466f | 1442 | |
a315e68f | 1443 | /* |
9ce7466f NA |
1444 | * For the range (2). If we reserved an extent for our delalloc range |
1445 | * (or a subrange) and failed to create the respective ordered extent, | |
1446 | * then it means that when we reserved the extent we decremented the | |
1447 | * extent's size from the data space_info's bytes_may_use counter and | |
1448 | * incremented the space_info's bytes_reserved counter by the same | |
1449 | * amount. We must make sure extent_clear_unlock_delalloc() does not try | |
1450 | * to decrement again the data space_info's bytes_may_use counter, | |
1451 | * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV. | |
a315e68f FM |
1452 | */ |
1453 | if (extent_reserved) { | |
6e26c442 | 1454 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1455 | start + cur_alloc_size - 1, |
a315e68f FM |
1456 | locked_page, |
1457 | clear_bits, | |
1458 | page_ops); | |
1459 | start += cur_alloc_size; | |
1460 | if (start >= end) | |
aaafa1eb | 1461 | return ret; |
a315e68f | 1462 | } |
9ce7466f NA |
1463 | |
1464 | /* | |
1465 | * For the range (3). We never touched the region. In addition to the | |
1466 | * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data | |
1467 | * space_info's bytes_may_use counter, reserved in | |
1468 | * btrfs_check_data_free_space(). | |
1469 | */ | |
6e26c442 | 1470 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1471 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1472 | page_ops); | |
aaafa1eb | 1473 | return ret; |
771ed689 | 1474 | } |
c8b97818 | 1475 | |
771ed689 CM |
1476 | /* |
1477 | * work queue call back to started compression on a file and pages | |
1478 | */ | |
1479 | static noinline void async_cow_start(struct btrfs_work *work) | |
1480 | { | |
b5326271 | 1481 | struct async_chunk *async_chunk; |
ac3e9933 | 1482 | int compressed_extents; |
771ed689 | 1483 | |
b5326271 | 1484 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1485 | |
ac3e9933 NB |
1486 | compressed_extents = compress_file_range(async_chunk); |
1487 | if (compressed_extents == 0) { | |
b5326271 NB |
1488 | btrfs_add_delayed_iput(async_chunk->inode); |
1489 | async_chunk->inode = NULL; | |
8180ef88 | 1490 | } |
771ed689 CM |
1491 | } |
1492 | ||
1493 | /* | |
1494 | * work queue call back to submit previously compressed pages | |
1495 | */ | |
1496 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1497 | { | |
c5a68aec NB |
1498 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1499 | work); | |
1500 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1501 | unsigned long nr_pages; |
1502 | ||
b5326271 | 1503 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1504 | PAGE_SHIFT; |
771ed689 | 1505 | |
4546d178 | 1506 | /* |
b5326271 | 1507 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1508 | * in which case we don't have anything to submit, yet we need to |
1509 | * always adjust ->async_delalloc_pages as its paired with the init | |
1510 | * happening in cow_file_range_async | |
1511 | */ | |
b5326271 NB |
1512 | if (async_chunk->inode) |
1513 | submit_compressed_extents(async_chunk); | |
ac98141d JB |
1514 | |
1515 | /* atomic_sub_return implies a barrier */ | |
1516 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < | |
1517 | 5 * SZ_1M) | |
1518 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1519 | } |
c8b97818 | 1520 | |
771ed689 CM |
1521 | static noinline void async_cow_free(struct btrfs_work *work) |
1522 | { | |
b5326271 | 1523 | struct async_chunk *async_chunk; |
9e895a8f | 1524 | struct async_cow *async_cow; |
97db1204 | 1525 | |
b5326271 NB |
1526 | async_chunk = container_of(work, struct async_chunk, work); |
1527 | if (async_chunk->inode) | |
1528 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1529 | if (async_chunk->blkcg_css) |
1530 | css_put(async_chunk->blkcg_css); | |
9e895a8f QW |
1531 | |
1532 | async_cow = async_chunk->async_cow; | |
1533 | if (atomic_dec_and_test(&async_cow->num_chunks)) | |
1534 | kvfree(async_cow); | |
771ed689 CM |
1535 | } |
1536 | ||
751b6431 | 1537 | static int cow_file_range_async(struct btrfs_inode *inode, |
ec39f769 CM |
1538 | struct writeback_control *wbc, |
1539 | struct page *locked_page, | |
771ed689 | 1540 | u64 start, u64 end, int *page_started, |
fac07d2b | 1541 | unsigned long *nr_written) |
771ed689 | 1542 | { |
751b6431 | 1543 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1544 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1545 | struct async_cow *ctx; |
1546 | struct async_chunk *async_chunk; | |
771ed689 CM |
1547 | unsigned long nr_pages; |
1548 | u64 cur_end; | |
97db1204 NB |
1549 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1550 | int i; | |
1551 | bool should_compress; | |
b1c16ac9 | 1552 | unsigned nofs_flag; |
bf9486d6 | 1553 | const blk_opf_t write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1554 | |
570eb97b | 1555 | unlock_extent(&inode->io_tree, start, end, NULL); |
97db1204 | 1556 | |
751b6431 | 1557 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
97db1204 NB |
1558 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
1559 | num_chunks = 1; | |
1560 | should_compress = false; | |
1561 | } else { | |
1562 | should_compress = true; | |
1563 | } | |
1564 | ||
b1c16ac9 NB |
1565 | nofs_flag = memalloc_nofs_save(); |
1566 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1567 | memalloc_nofs_restore(nofs_flag); | |
1568 | ||
97db1204 NB |
1569 | if (!ctx) { |
1570 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1571 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1572 | EXTENT_DO_ACCOUNTING; | |
6869b0a8 QW |
1573 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
1574 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; | |
97db1204 | 1575 | |
751b6431 NB |
1576 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1577 | clear_bits, page_ops); | |
97db1204 NB |
1578 | return -ENOMEM; |
1579 | } | |
1580 | ||
1581 | async_chunk = ctx->chunks; | |
1582 | atomic_set(&ctx->num_chunks, num_chunks); | |
1583 | ||
1584 | for (i = 0; i < num_chunks; i++) { | |
1585 | if (should_compress) | |
1586 | cur_end = min(end, start + SZ_512K - 1); | |
1587 | else | |
1588 | cur_end = end; | |
771ed689 | 1589 | |
bd4691a0 NB |
1590 | /* |
1591 | * igrab is called higher up in the call chain, take only the | |
1592 | * lightweight reference for the callback lifetime | |
1593 | */ | |
751b6431 | 1594 | ihold(&inode->vfs_inode); |
9e895a8f | 1595 | async_chunk[i].async_cow = ctx; |
751b6431 | 1596 | async_chunk[i].inode = &inode->vfs_inode; |
97db1204 NB |
1597 | async_chunk[i].start = start; |
1598 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1599 | async_chunk[i].write_flags = write_flags; |
1600 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1601 | ||
1d53c9e6 CM |
1602 | /* |
1603 | * The locked_page comes all the way from writepage and its | |
1604 | * the original page we were actually given. As we spread | |
1605 | * this large delalloc region across multiple async_chunk | |
1606 | * structs, only the first struct needs a pointer to locked_page | |
1607 | * | |
1608 | * This way we don't need racey decisions about who is supposed | |
1609 | * to unlock it. | |
1610 | */ | |
1611 | if (locked_page) { | |
ec39f769 CM |
1612 | /* |
1613 | * Depending on the compressibility, the pages might or | |
1614 | * might not go through async. We want all of them to | |
1615 | * be accounted against wbc once. Let's do it here | |
1616 | * before the paths diverge. wbc accounting is used | |
1617 | * only for foreign writeback detection and doesn't | |
1618 | * need full accuracy. Just account the whole thing | |
1619 | * against the first page. | |
1620 | */ | |
1621 | wbc_account_cgroup_owner(wbc, locked_page, | |
1622 | cur_end - start); | |
1d53c9e6 CM |
1623 | async_chunk[i].locked_page = locked_page; |
1624 | locked_page = NULL; | |
1625 | } else { | |
1626 | async_chunk[i].locked_page = NULL; | |
1627 | } | |
1628 | ||
ec39f769 CM |
1629 | if (blkcg_css != blkcg_root_css) { |
1630 | css_get(blkcg_css); | |
1631 | async_chunk[i].blkcg_css = blkcg_css; | |
1632 | } else { | |
1633 | async_chunk[i].blkcg_css = NULL; | |
1634 | } | |
1635 | ||
a0cac0ec OS |
1636 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1637 | async_cow_submit, async_cow_free); | |
771ed689 | 1638 | |
97db1204 | 1639 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1640 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1641 | |
97db1204 | 1642 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1643 | |
771ed689 CM |
1644 | *nr_written += nr_pages; |
1645 | start = cur_end + 1; | |
1646 | } | |
1647 | *page_started = 1; | |
1648 | return 0; | |
be20aa9d CM |
1649 | } |
1650 | ||
42c01100 NA |
1651 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
1652 | struct page *locked_page, u64 start, | |
1653 | u64 end, int *page_started, | |
1654 | unsigned long *nr_written) | |
1655 | { | |
898793d9 | 1656 | u64 done_offset = end; |
42c01100 | 1657 | int ret; |
898793d9 | 1658 | bool locked_page_done = false; |
42c01100 | 1659 | |
898793d9 NA |
1660 | while (start <= end) { |
1661 | ret = cow_file_range(inode, locked_page, start, end, page_started, | |
1662 | nr_written, 0, &done_offset); | |
1663 | if (ret && ret != -EAGAIN) | |
1664 | return ret; | |
42c01100 | 1665 | |
898793d9 NA |
1666 | if (*page_started) { |
1667 | ASSERT(ret == 0); | |
1668 | return 0; | |
1669 | } | |
1670 | ||
1671 | if (ret == 0) | |
1672 | done_offset = end; | |
1673 | ||
2ce543f4 | 1674 | if (done_offset == start) { |
d5b81ced NA |
1675 | wait_on_bit_io(&inode->root->fs_info->flags, |
1676 | BTRFS_FS_NEED_ZONE_FINISH, | |
1677 | TASK_UNINTERRUPTIBLE); | |
2ce543f4 NA |
1678 | continue; |
1679 | } | |
898793d9 NA |
1680 | |
1681 | if (!locked_page_done) { | |
1682 | __set_page_dirty_nobuffers(locked_page); | |
1683 | account_page_redirty(locked_page); | |
1684 | } | |
1685 | locked_page_done = true; | |
1686 | extent_write_locked_range(&inode->vfs_inode, start, done_offset); | |
1687 | ||
1688 | start = done_offset + 1; | |
1689 | } | |
42c01100 | 1690 | |
42c01100 NA |
1691 | *page_started = 1; |
1692 | ||
1693 | return 0; | |
1694 | } | |
1695 | ||
2ff7e61e | 1696 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
26ce9114 | 1697 | u64 bytenr, u64 num_bytes, bool nowait) |
17d217fe | 1698 | { |
fc28b25e | 1699 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); |
17d217fe | 1700 | struct btrfs_ordered_sum *sums; |
fc28b25e | 1701 | int ret; |
17d217fe YZ |
1702 | LIST_HEAD(list); |
1703 | ||
fc28b25e | 1704 | ret = btrfs_lookup_csums_range(csum_root, bytenr, |
26ce9114 JB |
1705 | bytenr + num_bytes - 1, &list, 0, |
1706 | nowait); | |
17d217fe YZ |
1707 | if (ret == 0 && list_empty(&list)) |
1708 | return 0; | |
1709 | ||
1710 | while (!list_empty(&list)) { | |
1711 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1712 | list_del(&sums->list); | |
1713 | kfree(sums); | |
1714 | } | |
58113753 LB |
1715 | if (ret < 0) |
1716 | return ret; | |
17d217fe YZ |
1717 | return 1; |
1718 | } | |
1719 | ||
8ba96f3d | 1720 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
467dc47e FM |
1721 | const u64 start, const u64 end, |
1722 | int *page_started, unsigned long *nr_written) | |
1723 | { | |
8ba96f3d | 1724 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
37f00a6d | 1725 | const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); |
2166e5ed | 1726 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1727 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1728 | u64 range_start = start; |
1729 | u64 count; | |
1730 | ||
1731 | /* | |
1732 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1733 | * made we had not enough available data space and therefore we did not | |
1734 | * reserve data space for it, since we though we could do NOCOW for the | |
1735 | * respective file range (either there is prealloc extent or the inode | |
1736 | * has the NOCOW bit set). | |
1737 | * | |
1738 | * However when we need to fallback to COW mode (because for example the | |
1739 | * block group for the corresponding extent was turned to RO mode by a | |
1740 | * scrub or relocation) we need to do the following: | |
1741 | * | |
1742 | * 1) We increment the bytes_may_use counter of the data space info. | |
1743 | * If COW succeeds, it allocates a new data extent and after doing | |
1744 | * that it decrements the space info's bytes_may_use counter and | |
1745 | * increments its bytes_reserved counter by the same amount (we do | |
1746 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1747 | * bytes_may_use counter to compensate (when space is reserved at | |
1748 | * buffered write time, the bytes_may_use counter is incremented); | |
1749 | * | |
1750 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1751 | * that if the COW path fails for any reason, it decrements (through | |
1752 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1753 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1754 | * |
1755 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1756 | * space cache inode or an inode of the data relocation tree, we must |
1757 | * also increment bytes_may_use of the data space_info for the same | |
1758 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1759 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1760 | * group that contains that extent to RO mode and therefore force COW |
1761 | * when starting writeback. | |
467dc47e | 1762 | */ |
2166e5ed | 1763 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
467dc47e | 1764 | EXTENT_NORESERVE, 0); |
6bd335b4 FM |
1765 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1766 | u64 bytes = count; | |
8ba96f3d | 1767 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1768 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1769 | ||
6bd335b4 FM |
1770 | if (is_space_ino || is_reloc_ino) |
1771 | bytes = range_bytes; | |
1772 | ||
467dc47e | 1773 | spin_lock(&sinfo->lock); |
2166e5ed | 1774 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1775 | spin_unlock(&sinfo->lock); |
1776 | ||
2166e5ed FM |
1777 | if (count > 0) |
1778 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
bd015294 | 1779 | NULL); |
467dc47e FM |
1780 | } |
1781 | ||
8ba96f3d | 1782 | return cow_file_range(inode, locked_page, start, end, page_started, |
898793d9 | 1783 | nr_written, 1, NULL); |
467dc47e FM |
1784 | } |
1785 | ||
619104ba FM |
1786 | struct can_nocow_file_extent_args { |
1787 | /* Input fields. */ | |
1788 | ||
1789 | /* Start file offset of the range we want to NOCOW. */ | |
1790 | u64 start; | |
1791 | /* End file offset (inclusive) of the range we want to NOCOW. */ | |
1792 | u64 end; | |
1793 | bool writeback_path; | |
1794 | bool strict; | |
1795 | /* | |
1796 | * Free the path passed to can_nocow_file_extent() once it's not needed | |
1797 | * anymore. | |
1798 | */ | |
1799 | bool free_path; | |
1800 | ||
1801 | /* Output fields. Only set when can_nocow_file_extent() returns 1. */ | |
1802 | ||
1803 | u64 disk_bytenr; | |
1804 | u64 disk_num_bytes; | |
1805 | u64 extent_offset; | |
1806 | /* Number of bytes that can be written to in NOCOW mode. */ | |
1807 | u64 num_bytes; | |
1808 | }; | |
1809 | ||
1810 | /* | |
1811 | * Check if we can NOCOW the file extent that the path points to. | |
1812 | * This function may return with the path released, so the caller should check | |
1813 | * if path->nodes[0] is NULL or not if it needs to use the path afterwards. | |
1814 | * | |
1815 | * Returns: < 0 on error | |
1816 | * 0 if we can not NOCOW | |
1817 | * 1 if we can NOCOW | |
1818 | */ | |
1819 | static int can_nocow_file_extent(struct btrfs_path *path, | |
1820 | struct btrfs_key *key, | |
1821 | struct btrfs_inode *inode, | |
1822 | struct can_nocow_file_extent_args *args) | |
1823 | { | |
1824 | const bool is_freespace_inode = btrfs_is_free_space_inode(inode); | |
1825 | struct extent_buffer *leaf = path->nodes[0]; | |
1826 | struct btrfs_root *root = inode->root; | |
1827 | struct btrfs_file_extent_item *fi; | |
1828 | u64 extent_end; | |
1829 | u8 extent_type; | |
1830 | int can_nocow = 0; | |
1831 | int ret = 0; | |
26ce9114 | 1832 | bool nowait = path->nowait; |
619104ba FM |
1833 | |
1834 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
1835 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1836 | ||
1837 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) | |
1838 | goto out; | |
1839 | ||
1840 | /* Can't access these fields unless we know it's not an inline extent. */ | |
1841 | args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1842 | args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
1843 | args->extent_offset = btrfs_file_extent_offset(leaf, fi); | |
1844 | ||
1845 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && | |
1846 | extent_type == BTRFS_FILE_EXTENT_REG) | |
1847 | goto out; | |
1848 | ||
1849 | /* | |
1850 | * If the extent was created before the generation where the last snapshot | |
1851 | * for its subvolume was created, then this implies the extent is shared, | |
1852 | * hence we must COW. | |
1853 | */ | |
a7bb6bd4 | 1854 | if (!args->strict && |
619104ba FM |
1855 | btrfs_file_extent_generation(leaf, fi) <= |
1856 | btrfs_root_last_snapshot(&root->root_item)) | |
1857 | goto out; | |
1858 | ||
1859 | /* An explicit hole, must COW. */ | |
1860 | if (args->disk_bytenr == 0) | |
1861 | goto out; | |
1862 | ||
1863 | /* Compressed/encrypted/encoded extents must be COWed. */ | |
1864 | if (btrfs_file_extent_compression(leaf, fi) || | |
1865 | btrfs_file_extent_encryption(leaf, fi) || | |
1866 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1867 | goto out; | |
1868 | ||
1869 | extent_end = btrfs_file_extent_end(path); | |
1870 | ||
1871 | /* | |
1872 | * The following checks can be expensive, as they need to take other | |
1873 | * locks and do btree or rbtree searches, so release the path to avoid | |
1874 | * blocking other tasks for too long. | |
1875 | */ | |
1876 | btrfs_release_path(path); | |
1877 | ||
1878 | ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), | |
1879 | key->offset - args->extent_offset, | |
1880 | args->disk_bytenr, false, path); | |
1881 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); | |
1882 | if (ret != 0) | |
1883 | goto out; | |
1884 | ||
1885 | if (args->free_path) { | |
1886 | /* | |
1887 | * We don't need the path anymore, plus through the | |
1888 | * csum_exist_in_range() call below we will end up allocating | |
1889 | * another path. So free the path to avoid unnecessary extra | |
1890 | * memory usage. | |
1891 | */ | |
1892 | btrfs_free_path(path); | |
1893 | path = NULL; | |
1894 | } | |
1895 | ||
1896 | /* If there are pending snapshots for this root, we must COW. */ | |
1897 | if (args->writeback_path && !is_freespace_inode && | |
1898 | atomic_read(&root->snapshot_force_cow)) | |
1899 | goto out; | |
1900 | ||
1901 | args->disk_bytenr += args->extent_offset; | |
1902 | args->disk_bytenr += args->start - key->offset; | |
1903 | args->num_bytes = min(args->end + 1, extent_end) - args->start; | |
1904 | ||
1905 | /* | |
1906 | * Force COW if csums exist in the range. This ensures that csums for a | |
1907 | * given extent are either valid or do not exist. | |
1908 | */ | |
26ce9114 JB |
1909 | ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes, |
1910 | nowait); | |
619104ba FM |
1911 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); |
1912 | if (ret != 0) | |
1913 | goto out; | |
1914 | ||
1915 | can_nocow = 1; | |
1916 | out: | |
1917 | if (args->free_path && path) | |
1918 | btrfs_free_path(path); | |
1919 | ||
1920 | return ret < 0 ? ret : can_nocow; | |
1921 | } | |
1922 | ||
d352ac68 CM |
1923 | /* |
1924 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1925 | * of the extents that exist in the file, and COWs the file as required. | |
1926 | * | |
1927 | * If no cow copies or snapshots exist, we write directly to the existing | |
1928 | * blocks on disk | |
1929 | */ | |
968322c8 | 1930 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1931 | struct page *locked_page, |
3e024846 | 1932 | const u64 start, const u64 end, |
6e65ae76 | 1933 | int *page_started, |
3e024846 | 1934 | unsigned long *nr_written) |
be20aa9d | 1935 | { |
968322c8 NB |
1936 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1937 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1938 | struct btrfs_path *path; |
3e024846 NB |
1939 | u64 cow_start = (u64)-1; |
1940 | u64 cur_offset = start; | |
8ecebf4d | 1941 | int ret; |
3e024846 | 1942 | bool check_prev = true; |
968322c8 | 1943 | u64 ino = btrfs_ino(inode); |
2306e83e | 1944 | struct btrfs_block_group *bg; |
762bf098 | 1945 | bool nocow = false; |
619104ba | 1946 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
be20aa9d CM |
1947 | |
1948 | path = btrfs_alloc_path(); | |
17ca04af | 1949 | if (!path) { |
968322c8 | 1950 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
c2790a2e | 1951 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1952 | EXTENT_DO_ACCOUNTING | |
1953 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
6869b0a8 | 1954 | PAGE_START_WRITEBACK | |
c2790a2e | 1955 | PAGE_END_WRITEBACK); |
d8926bb3 | 1956 | return -ENOMEM; |
17ca04af | 1957 | } |
82d5902d | 1958 | |
619104ba FM |
1959 | nocow_args.end = end; |
1960 | nocow_args.writeback_path = true; | |
1961 | ||
80ff3856 | 1962 | while (1) { |
3e024846 NB |
1963 | struct btrfs_key found_key; |
1964 | struct btrfs_file_extent_item *fi; | |
1965 | struct extent_buffer *leaf; | |
1966 | u64 extent_end; | |
3e024846 | 1967 | u64 ram_bytes; |
619104ba | 1968 | u64 nocow_end; |
3e024846 | 1969 | int extent_type; |
762bf098 NB |
1970 | |
1971 | nocow = false; | |
3e024846 | 1972 | |
e4c3b2dc | 1973 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1974 | cur_offset, 0); |
d788a349 | 1975 | if (ret < 0) |
79787eaa | 1976 | goto error; |
a6bd9cd1 NB |
1977 | |
1978 | /* | |
1979 | * If there is no extent for our range when doing the initial | |
1980 | * search, then go back to the previous slot as it will be the | |
1981 | * one containing the search offset | |
1982 | */ | |
80ff3856 YZ |
1983 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1984 | leaf = path->nodes[0]; | |
1985 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1986 | path->slots[0] - 1); | |
33345d01 | 1987 | if (found_key.objectid == ino && |
80ff3856 YZ |
1988 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1989 | path->slots[0]--; | |
1990 | } | |
3e024846 | 1991 | check_prev = false; |
80ff3856 | 1992 | next_slot: |
a6bd9cd1 | 1993 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1994 | leaf = path->nodes[0]; |
1995 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1996 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1997 | if (ret < 0) { |
1998 | if (cow_start != (u64)-1) | |
1999 | cur_offset = cow_start; | |
79787eaa | 2000 | goto error; |
e8916699 | 2001 | } |
80ff3856 YZ |
2002 | if (ret > 0) |
2003 | break; | |
2004 | leaf = path->nodes[0]; | |
2005 | } | |
be20aa9d | 2006 | |
80ff3856 YZ |
2007 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
2008 | ||
a6bd9cd1 | 2009 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
2010 | if (found_key.objectid > ino) |
2011 | break; | |
a6bd9cd1 NB |
2012 | /* |
2013 | * Keep searching until we find an EXTENT_ITEM or there are no | |
2014 | * more extents for this inode | |
2015 | */ | |
1d512cb7 FM |
2016 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
2017 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
2018 | path->slots[0]++; | |
2019 | goto next_slot; | |
2020 | } | |
a6bd9cd1 NB |
2021 | |
2022 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 2023 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
2024 | found_key.offset > end) |
2025 | break; | |
2026 | ||
a6bd9cd1 NB |
2027 | /* |
2028 | * If the found extent starts after requested offset, then | |
2029 | * adjust extent_end to be right before this extent begins | |
2030 | */ | |
80ff3856 YZ |
2031 | if (found_key.offset > cur_offset) { |
2032 | extent_end = found_key.offset; | |
e9061e21 | 2033 | extent_type = 0; |
80ff3856 YZ |
2034 | goto out_check; |
2035 | } | |
2036 | ||
a6bd9cd1 NB |
2037 | /* |
2038 | * Found extent which begins before our range and potentially | |
2039 | * intersect it | |
2040 | */ | |
80ff3856 YZ |
2041 | fi = btrfs_item_ptr(leaf, path->slots[0], |
2042 | struct btrfs_file_extent_item); | |
2043 | extent_type = btrfs_file_extent_type(leaf, fi); | |
619104ba FM |
2044 | /* If this is triggered then we have a memory corruption. */ |
2045 | ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES); | |
2046 | if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) { | |
2047 | ret = -EUCLEAN; | |
2048 | goto error; | |
2049 | } | |
cc95bef6 | 2050 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
619104ba | 2051 | extent_end = btrfs_file_extent_end(path); |
c65ca98f | 2052 | |
619104ba FM |
2053 | /* |
2054 | * If the extent we got ends before our current offset, skip to | |
2055 | * the next extent. | |
2056 | */ | |
2057 | if (extent_end <= cur_offset) { | |
2058 | path->slots[0]++; | |
2059 | goto next_slot; | |
2060 | } | |
c65ca98f | 2061 | |
619104ba FM |
2062 | nocow_args.start = cur_offset; |
2063 | ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args); | |
2064 | if (ret < 0) { | |
2065 | if (cow_start != (u64)-1) | |
2066 | cur_offset = cow_start; | |
2067 | goto error; | |
2068 | } else if (ret == 0) { | |
2069 | goto out_check; | |
2070 | } | |
58113753 | 2071 | |
619104ba | 2072 | ret = 0; |
2306e83e FM |
2073 | bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); |
2074 | if (bg) | |
3e024846 | 2075 | nocow = true; |
80ff3856 | 2076 | out_check: |
a6bd9cd1 NB |
2077 | /* |
2078 | * If nocow is false then record the beginning of the range | |
2079 | * that needs to be COWed | |
2080 | */ | |
80ff3856 YZ |
2081 | if (!nocow) { |
2082 | if (cow_start == (u64)-1) | |
2083 | cow_start = cur_offset; | |
2084 | cur_offset = extent_end; | |
2085 | if (cur_offset > end) | |
2086 | break; | |
c65ca98f FM |
2087 | if (!path->nodes[0]) |
2088 | continue; | |
80ff3856 YZ |
2089 | path->slots[0]++; |
2090 | goto next_slot; | |
7ea394f1 YZ |
2091 | } |
2092 | ||
a6bd9cd1 NB |
2093 | /* |
2094 | * COW range from cow_start to found_key.offset - 1. As the key | |
2095 | * will contain the beginning of the first extent that can be | |
2096 | * NOCOW, following one which needs to be COW'ed | |
2097 | */ | |
80ff3856 | 2098 | if (cow_start != (u64)-1) { |
968322c8 | 2099 | ret = fallback_to_cow(inode, locked_page, |
8ba96f3d | 2100 | cow_start, found_key.offset - 1, |
467dc47e | 2101 | page_started, nr_written); |
230ed397 | 2102 | if (ret) |
79787eaa | 2103 | goto error; |
80ff3856 | 2104 | cow_start = (u64)-1; |
7ea394f1 | 2105 | } |
80ff3856 | 2106 | |
619104ba FM |
2107 | nocow_end = cur_offset + nocow_args.num_bytes - 1; |
2108 | ||
d899e052 | 2109 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
619104ba | 2110 | u64 orig_start = found_key.offset - nocow_args.extent_offset; |
3e024846 | 2111 | struct extent_map *em; |
6f9994db | 2112 | |
619104ba | 2113 | em = create_io_em(inode, cur_offset, nocow_args.num_bytes, |
6f9994db | 2114 | orig_start, |
619104ba FM |
2115 | nocow_args.disk_bytenr, /* block_start */ |
2116 | nocow_args.num_bytes, /* block_len */ | |
2117 | nocow_args.disk_num_bytes, /* orig_block_len */ | |
6f9994db LB |
2118 | ram_bytes, BTRFS_COMPRESS_NONE, |
2119 | BTRFS_ORDERED_PREALLOC); | |
2120 | if (IS_ERR(em)) { | |
6f9994db LB |
2121 | ret = PTR_ERR(em); |
2122 | goto error; | |
d899e052 | 2123 | } |
6f9994db | 2124 | free_extent_map(em); |
cb36a9bb | 2125 | ret = btrfs_add_ordered_extent(inode, |
619104ba FM |
2126 | cur_offset, nocow_args.num_bytes, |
2127 | nocow_args.num_bytes, | |
2128 | nocow_args.disk_bytenr, | |
2129 | nocow_args.num_bytes, 0, | |
cb36a9bb OS |
2130 | 1 << BTRFS_ORDERED_PREALLOC, |
2131 | BTRFS_COMPRESS_NONE); | |
762bf098 | 2132 | if (ret) { |
4c0c8cfc FM |
2133 | btrfs_drop_extent_map_range(inode, cur_offset, |
2134 | nocow_end, false); | |
762bf098 NB |
2135 | goto error; |
2136 | } | |
d899e052 | 2137 | } else { |
968322c8 | 2138 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
619104ba FM |
2139 | nocow_args.num_bytes, |
2140 | nocow_args.num_bytes, | |
2141 | nocow_args.disk_bytenr, | |
2142 | nocow_args.num_bytes, | |
cb36a9bb OS |
2143 | 0, |
2144 | 1 << BTRFS_ORDERED_NOCOW, | |
2145 | BTRFS_COMPRESS_NONE); | |
762bf098 NB |
2146 | if (ret) |
2147 | goto error; | |
d899e052 | 2148 | } |
80ff3856 | 2149 | |
2306e83e FM |
2150 | if (nocow) { |
2151 | btrfs_dec_nocow_writers(bg); | |
2152 | nocow = false; | |
2153 | } | |
771ed689 | 2154 | |
37f00a6d | 2155 | if (btrfs_is_data_reloc_root(root)) |
4dbd80fb QW |
2156 | /* |
2157 | * Error handled later, as we must prevent | |
2158 | * extent_clear_unlock_delalloc() in error handler | |
2159 | * from freeing metadata of created ordered extent. | |
2160 | */ | |
968322c8 | 2161 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
619104ba | 2162 | nocow_args.num_bytes); |
efa56464 | 2163 | |
619104ba | 2164 | extent_clear_unlock_delalloc(inode, cur_offset, nocow_end, |
c2790a2e | 2165 | locked_page, EXTENT_LOCKED | |
18513091 WX |
2166 | EXTENT_DELALLOC | |
2167 | EXTENT_CLEAR_DATA_RESV, | |
f57ad937 | 2168 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
18513091 | 2169 | |
80ff3856 | 2170 | cur_offset = extent_end; |
4dbd80fb QW |
2171 | |
2172 | /* | |
2173 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
2174 | * handler, as metadata for created ordered extent will only | |
2175 | * be freed by btrfs_finish_ordered_io(). | |
2176 | */ | |
2177 | if (ret) | |
2178 | goto error; | |
80ff3856 YZ |
2179 | if (cur_offset > end) |
2180 | break; | |
be20aa9d | 2181 | } |
b3b4aa74 | 2182 | btrfs_release_path(path); |
80ff3856 | 2183 | |
506481b2 | 2184 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 2185 | cow_start = cur_offset; |
17ca04af | 2186 | |
80ff3856 | 2187 | if (cow_start != (u64)-1) { |
506481b2 | 2188 | cur_offset = end; |
968322c8 NB |
2189 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
2190 | page_started, nr_written); | |
d788a349 | 2191 | if (ret) |
79787eaa | 2192 | goto error; |
80ff3856 YZ |
2193 | } |
2194 | ||
79787eaa | 2195 | error: |
762bf098 | 2196 | if (nocow) |
2306e83e | 2197 | btrfs_dec_nocow_writers(bg); |
762bf098 | 2198 | |
17ca04af | 2199 | if (ret && cur_offset < end) |
968322c8 | 2200 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 2201 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
2202 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
2203 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 2204 | PAGE_START_WRITEBACK | |
c2790a2e | 2205 | PAGE_END_WRITEBACK); |
7ea394f1 | 2206 | btrfs_free_path(path); |
79787eaa | 2207 | return ret; |
be20aa9d CM |
2208 | } |
2209 | ||
6e65ae76 | 2210 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 | 2211 | { |
6e65ae76 GR |
2212 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
2213 | if (inode->defrag_bytes && | |
2214 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, | |
2215 | 0, NULL)) | |
2216 | return false; | |
2217 | return true; | |
2218 | } | |
2219 | return false; | |
47059d93 WS |
2220 | } |
2221 | ||
d352ac68 | 2222 | /* |
5eaad97a NB |
2223 | * Function to process delayed allocation (create CoW) for ranges which are |
2224 | * being touched for the first time. | |
d352ac68 | 2225 | */ |
98456b9c | 2226 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
5eaad97a NB |
2227 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
2228 | struct writeback_control *wbc) | |
be20aa9d | 2229 | { |
be20aa9d | 2230 | int ret; |
42c01100 | 2231 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
a2135011 | 2232 | |
2749f7ef QW |
2233 | /* |
2234 | * The range must cover part of the @locked_page, or the returned | |
2235 | * @page_started can confuse the caller. | |
2236 | */ | |
2237 | ASSERT(!(end <= page_offset(locked_page) || | |
2238 | start >= page_offset(locked_page) + PAGE_SIZE)); | |
2239 | ||
6e65ae76 | 2240 | if (should_nocow(inode, start, end)) { |
2adada88 JT |
2241 | /* |
2242 | * Normally on a zoned device we're only doing COW writes, but | |
2243 | * in case of relocation on a zoned filesystem we have taken | |
2244 | * precaution, that we're only writing sequentially. It's safe | |
2245 | * to use run_delalloc_nocow() here, like for regular | |
2246 | * preallocated inodes. | |
2247 | */ | |
9435be73 | 2248 | ASSERT(!zoned || btrfs_is_data_reloc_root(inode->root)); |
98456b9c | 2249 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
6e65ae76 | 2250 | page_started, nr_written); |
e6f9d696 | 2251 | } else if (!btrfs_inode_can_compress(inode) || |
98456b9c | 2252 | !inode_need_compress(inode, start, end)) { |
42c01100 NA |
2253 | if (zoned) |
2254 | ret = run_delalloc_zoned(inode, locked_page, start, end, | |
2255 | page_started, nr_written); | |
2256 | else | |
2257 | ret = cow_file_range(inode, locked_page, start, end, | |
898793d9 | 2258 | page_started, nr_written, 1, NULL); |
7ddf5a42 | 2259 | } else { |
98456b9c NB |
2260 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
2261 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, | |
fac07d2b | 2262 | page_started, nr_written); |
7ddf5a42 | 2263 | } |
7361b4ae | 2264 | ASSERT(ret <= 0); |
52427260 | 2265 | if (ret) |
98456b9c | 2266 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 2267 | end - start + 1); |
b888db2b CM |
2268 | return ret; |
2269 | } | |
2270 | ||
abbb55f4 NB |
2271 | void btrfs_split_delalloc_extent(struct inode *inode, |
2272 | struct extent_state *orig, u64 split) | |
9ed74f2d | 2273 | { |
f7b12a62 | 2274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
dcab6a3b JB |
2275 | u64 size; |
2276 | ||
0ca1f7ce | 2277 | /* not delalloc, ignore it */ |
9ed74f2d | 2278 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 2279 | return; |
9ed74f2d | 2280 | |
dcab6a3b | 2281 | size = orig->end - orig->start + 1; |
f7b12a62 | 2282 | if (size > fs_info->max_extent_size) { |
823bb20a | 2283 | u32 num_extents; |
dcab6a3b JB |
2284 | u64 new_size; |
2285 | ||
2286 | /* | |
5c848198 | 2287 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 2288 | * applies here, just in reverse. |
dcab6a3b JB |
2289 | */ |
2290 | new_size = orig->end - split + 1; | |
7d7672bc | 2291 | num_extents = count_max_extents(fs_info, new_size); |
ba117213 | 2292 | new_size = split - orig->start; |
7d7672bc NA |
2293 | num_extents += count_max_extents(fs_info, new_size); |
2294 | if (count_max_extents(fs_info, size) >= num_extents) | |
dcab6a3b JB |
2295 | return; |
2296 | } | |
2297 | ||
9e0baf60 | 2298 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 2299 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 2300 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
2301 | } |
2302 | ||
2303 | /* | |
5c848198 NB |
2304 | * Handle merged delayed allocation extents so we can keep track of new extents |
2305 | * that are just merged onto old extents, such as when we are doing sequential | |
2306 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 2307 | */ |
5c848198 NB |
2308 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
2309 | struct extent_state *other) | |
9ed74f2d | 2310 | { |
f7b12a62 | 2311 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
dcab6a3b | 2312 | u64 new_size, old_size; |
823bb20a | 2313 | u32 num_extents; |
dcab6a3b | 2314 | |
9ed74f2d JB |
2315 | /* not delalloc, ignore it */ |
2316 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 2317 | return; |
9ed74f2d | 2318 | |
8461a3de JB |
2319 | if (new->start > other->start) |
2320 | new_size = new->end - other->start + 1; | |
2321 | else | |
2322 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
2323 | |
2324 | /* we're not bigger than the max, unreserve the space and go */ | |
f7b12a62 | 2325 | if (new_size <= fs_info->max_extent_size) { |
dcab6a3b | 2326 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 2327 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
2328 | spin_unlock(&BTRFS_I(inode)->lock); |
2329 | return; | |
2330 | } | |
2331 | ||
2332 | /* | |
ba117213 JB |
2333 | * We have to add up either side to figure out how many extents were |
2334 | * accounted for before we merged into one big extent. If the number of | |
2335 | * extents we accounted for is <= the amount we need for the new range | |
2336 | * then we can return, otherwise drop. Think of it like this | |
2337 | * | |
2338 | * [ 4k][MAX_SIZE] | |
2339 | * | |
2340 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
2341 | * need 2 outstanding extents, on one side we have 1 and the other side | |
2342 | * we have 1 so they are == and we can return. But in this case | |
2343 | * | |
2344 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
2345 | * | |
2346 | * Each range on their own accounts for 2 extents, but merged together | |
2347 | * they are only 3 extents worth of accounting, so we need to drop in | |
2348 | * this case. | |
dcab6a3b | 2349 | */ |
ba117213 | 2350 | old_size = other->end - other->start + 1; |
7d7672bc | 2351 | num_extents = count_max_extents(fs_info, old_size); |
ba117213 | 2352 | old_size = new->end - new->start + 1; |
7d7672bc NA |
2353 | num_extents += count_max_extents(fs_info, old_size); |
2354 | if (count_max_extents(fs_info, new_size) >= num_extents) | |
dcab6a3b JB |
2355 | return; |
2356 | ||
9e0baf60 | 2357 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 2358 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 2359 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
2360 | } |
2361 | ||
eb73c1b7 MX |
2362 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
2363 | struct inode *inode) | |
2364 | { | |
0b246afa JM |
2365 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2366 | ||
eb73c1b7 MX |
2367 | spin_lock(&root->delalloc_lock); |
2368 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
2369 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
2370 | &root->delalloc_inodes); | |
2371 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
2372 | &BTRFS_I(inode)->runtime_flags); | |
2373 | root->nr_delalloc_inodes++; | |
2374 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 2375 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2376 | BUG_ON(!list_empty(&root->delalloc_root)); |
2377 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
2378 | &fs_info->delalloc_roots); |
2379 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
2380 | } |
2381 | } | |
2382 | spin_unlock(&root->delalloc_lock); | |
2383 | } | |
2384 | ||
2b877331 NB |
2385 | |
2386 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2387 | struct btrfs_inode *inode) | |
eb73c1b7 | 2388 | { |
3ffbd68c | 2389 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 2390 | |
9e3e97f4 NB |
2391 | if (!list_empty(&inode->delalloc_inodes)) { |
2392 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 2393 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2394 | &inode->runtime_flags); |
eb73c1b7 MX |
2395 | root->nr_delalloc_inodes--; |
2396 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 2397 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 2398 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2399 | BUG_ON(list_empty(&root->delalloc_root)); |
2400 | list_del_init(&root->delalloc_root); | |
0b246afa | 2401 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2402 | } |
2403 | } | |
2b877331 NB |
2404 | } |
2405 | ||
2406 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2407 | struct btrfs_inode *inode) | |
2408 | { | |
2409 | spin_lock(&root->delalloc_lock); | |
2410 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
2411 | spin_unlock(&root->delalloc_lock); |
2412 | } | |
2413 | ||
d352ac68 | 2414 | /* |
e06a1fc9 NB |
2415 | * Properly track delayed allocation bytes in the inode and to maintain the |
2416 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 2417 | */ |
e06a1fc9 | 2418 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
6d92b304 | 2419 | u32 bits) |
291d673e | 2420 | { |
0b246afa JM |
2421 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2422 | ||
6d92b304 | 2423 | if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC)) |
47059d93 | 2424 | WARN_ON(1); |
75eff68e CM |
2425 | /* |
2426 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2427 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2428 | * bit, which is only set or cleared with irqs on |
2429 | */ | |
6d92b304 | 2430 | if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e | 2431 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2432 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2433 | u32 num_extents = count_max_extents(fs_info, len); |
70ddc553 | 2434 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 2435 | |
8b62f87b JB |
2436 | spin_lock(&BTRFS_I(inode)->lock); |
2437 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
2438 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 2439 | |
6a3891c5 | 2440 | /* For sanity tests */ |
0b246afa | 2441 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2442 | return; |
2443 | ||
104b4e51 NB |
2444 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2445 | fs_info->delalloc_batch); | |
df0af1a5 | 2446 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 2447 | BTRFS_I(inode)->delalloc_bytes += len; |
6d92b304 | 2448 | if (bits & EXTENT_DEFRAG) |
47059d93 | 2449 | BTRFS_I(inode)->defrag_bytes += len; |
df0af1a5 | 2450 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
2451 | &BTRFS_I(inode)->runtime_flags)) |
2452 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 2453 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 2454 | } |
a7e3b975 FM |
2455 | |
2456 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2457 | (bits & EXTENT_DELALLOC_NEW)) { |
a7e3b975 FM |
2458 | spin_lock(&BTRFS_I(inode)->lock); |
2459 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
2460 | state->start; | |
2461 | spin_unlock(&BTRFS_I(inode)->lock); | |
2462 | } | |
291d673e CM |
2463 | } |
2464 | ||
d352ac68 | 2465 | /* |
a36bb5f9 NB |
2466 | * Once a range is no longer delalloc this function ensures that proper |
2467 | * accounting happens. | |
d352ac68 | 2468 | */ |
a36bb5f9 | 2469 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
6d92b304 | 2470 | struct extent_state *state, u32 bits) |
291d673e | 2471 | { |
a36bb5f9 NB |
2472 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
2473 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 2474 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2475 | u32 num_extents = count_max_extents(fs_info, len); |
47059d93 | 2476 | |
6d92b304 | 2477 | if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) { |
4a4b964f | 2478 | spin_lock(&inode->lock); |
6fc0ef68 | 2479 | inode->defrag_bytes -= len; |
4a4b964f FM |
2480 | spin_unlock(&inode->lock); |
2481 | } | |
47059d93 | 2482 | |
75eff68e CM |
2483 | /* |
2484 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2485 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2486 | * bit, which is only set or cleared with irqs on |
2487 | */ | |
6d92b304 | 2488 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2489 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2490 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2491 | |
8b62f87b JB |
2492 | spin_lock(&inode->lock); |
2493 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2494 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2495 | |
b6d08f06 JB |
2496 | /* |
2497 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2498 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2499 | * error. |
2500 | */ | |
6d92b304 | 2501 | if (bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2502 | root != fs_info->tree_root) |
43b18595 | 2503 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2504 | |
6a3891c5 | 2505 | /* For sanity tests. */ |
0b246afa | 2506 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2507 | return; |
2508 | ||
37f00a6d | 2509 | if (!btrfs_is_data_reloc_root(root) && |
a315e68f | 2510 | do_list && !(state->state & EXTENT_NORESERVE) && |
6d92b304 | 2511 | (bits & EXTENT_CLEAR_DATA_RESV)) |
9db5d510 | 2512 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2513 | |
104b4e51 NB |
2514 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2515 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2516 | spin_lock(&inode->lock); |
2517 | inode->delalloc_bytes -= len; | |
2518 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2519 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2520 | &inode->runtime_flags)) |
eb73c1b7 | 2521 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2522 | spin_unlock(&inode->lock); |
291d673e | 2523 | } |
a7e3b975 FM |
2524 | |
2525 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2526 | (bits & EXTENT_DELALLOC_NEW)) { |
a7e3b975 FM |
2527 | spin_lock(&inode->lock); |
2528 | ASSERT(inode->new_delalloc_bytes >= len); | |
2529 | inode->new_delalloc_bytes -= len; | |
6d92b304 | 2530 | if (bits & EXTENT_ADD_INODE_BYTES) |
2766ff61 | 2531 | inode_add_bytes(&inode->vfs_inode, len); |
a7e3b975 FM |
2532 | spin_unlock(&inode->lock); |
2533 | } | |
291d673e CM |
2534 | } |
2535 | ||
d352ac68 CM |
2536 | /* |
2537 | * in order to insert checksums into the metadata in large chunks, | |
2538 | * we wait until bio submission time. All the pages in the bio are | |
2539 | * checksummed and sums are attached onto the ordered extent record. | |
2540 | * | |
2541 | * At IO completion time the cums attached on the ordered extent record | |
2542 | * are inserted into the btree | |
2543 | */ | |
8896a08d | 2544 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
1941b64b | 2545 | u64 dio_file_offset) |
065631f6 | 2546 | { |
e331f6b1 | 2547 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, (u64)-1, false); |
4a69a410 | 2548 | } |
e015640f | 2549 | |
abb99cfd NA |
2550 | /* |
2551 | * Split an extent_map at [start, start + len] | |
2552 | * | |
2553 | * This function is intended to be used only for extract_ordered_extent(). | |
2554 | */ | |
2555 | static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len, | |
2556 | u64 pre, u64 post) | |
2557 | { | |
2558 | struct extent_map_tree *em_tree = &inode->extent_tree; | |
2559 | struct extent_map *em; | |
2560 | struct extent_map *split_pre = NULL; | |
2561 | struct extent_map *split_mid = NULL; | |
2562 | struct extent_map *split_post = NULL; | |
2563 | int ret = 0; | |
abb99cfd NA |
2564 | unsigned long flags; |
2565 | ||
2566 | /* Sanity check */ | |
2567 | if (pre == 0 && post == 0) | |
2568 | return 0; | |
2569 | ||
2570 | split_pre = alloc_extent_map(); | |
2571 | if (pre) | |
2572 | split_mid = alloc_extent_map(); | |
2573 | if (post) | |
2574 | split_post = alloc_extent_map(); | |
2575 | if (!split_pre || (pre && !split_mid) || (post && !split_post)) { | |
2576 | ret = -ENOMEM; | |
2577 | goto out; | |
2578 | } | |
2579 | ||
2580 | ASSERT(pre + post < len); | |
2581 | ||
570eb97b | 2582 | lock_extent(&inode->io_tree, start, start + len - 1, NULL); |
abb99cfd NA |
2583 | write_lock(&em_tree->lock); |
2584 | em = lookup_extent_mapping(em_tree, start, len); | |
2585 | if (!em) { | |
2586 | ret = -EIO; | |
2587 | goto out_unlock; | |
2588 | } | |
2589 | ||
2590 | ASSERT(em->len == len); | |
2591 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); | |
2592 | ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE); | |
63fb5879 NA |
2593 | ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
2594 | ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags)); | |
2595 | ASSERT(!list_empty(&em->list)); | |
abb99cfd NA |
2596 | |
2597 | flags = em->flags; | |
2598 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); | |
abb99cfd NA |
2599 | |
2600 | /* First, replace the em with a new extent_map starting from * em->start */ | |
2601 | split_pre->start = em->start; | |
2602 | split_pre->len = (pre ? pre : em->len - post); | |
2603 | split_pre->orig_start = split_pre->start; | |
2604 | split_pre->block_start = em->block_start; | |
2605 | split_pre->block_len = split_pre->len; | |
2606 | split_pre->orig_block_len = split_pre->block_len; | |
2607 | split_pre->ram_bytes = split_pre->len; | |
2608 | split_pre->flags = flags; | |
2609 | split_pre->compress_type = em->compress_type; | |
2610 | split_pre->generation = em->generation; | |
2611 | ||
63fb5879 | 2612 | replace_extent_mapping(em_tree, em, split_pre, 1); |
abb99cfd NA |
2613 | |
2614 | /* | |
2615 | * Now we only have an extent_map at: | |
2616 | * [em->start, em->start + pre] if pre != 0 | |
2617 | * [em->start, em->start + em->len - post] if pre == 0 | |
2618 | */ | |
2619 | ||
2620 | if (pre) { | |
2621 | /* Insert the middle extent_map */ | |
2622 | split_mid->start = em->start + pre; | |
2623 | split_mid->len = em->len - pre - post; | |
2624 | split_mid->orig_start = split_mid->start; | |
2625 | split_mid->block_start = em->block_start + pre; | |
2626 | split_mid->block_len = split_mid->len; | |
2627 | split_mid->orig_block_len = split_mid->block_len; | |
2628 | split_mid->ram_bytes = split_mid->len; | |
2629 | split_mid->flags = flags; | |
2630 | split_mid->compress_type = em->compress_type; | |
2631 | split_mid->generation = em->generation; | |
63fb5879 | 2632 | add_extent_mapping(em_tree, split_mid, 1); |
abb99cfd NA |
2633 | } |
2634 | ||
2635 | if (post) { | |
2636 | split_post->start = em->start + em->len - post; | |
2637 | split_post->len = post; | |
2638 | split_post->orig_start = split_post->start; | |
2639 | split_post->block_start = em->block_start + em->len - post; | |
2640 | split_post->block_len = split_post->len; | |
2641 | split_post->orig_block_len = split_post->block_len; | |
2642 | split_post->ram_bytes = split_post->len; | |
2643 | split_post->flags = flags; | |
2644 | split_post->compress_type = em->compress_type; | |
2645 | split_post->generation = em->generation; | |
63fb5879 | 2646 | add_extent_mapping(em_tree, split_post, 1); |
abb99cfd NA |
2647 | } |
2648 | ||
2649 | /* Once for us */ | |
2650 | free_extent_map(em); | |
2651 | /* Once for the tree */ | |
2652 | free_extent_map(em); | |
2653 | ||
2654 | out_unlock: | |
2655 | write_unlock(&em_tree->lock); | |
570eb97b | 2656 | unlock_extent(&inode->io_tree, start, start + len - 1, NULL); |
abb99cfd NA |
2657 | out: |
2658 | free_extent_map(split_pre); | |
2659 | free_extent_map(split_mid); | |
2660 | free_extent_map(split_post); | |
2661 | ||
2662 | return ret; | |
2663 | } | |
2664 | ||
d22002fd NA |
2665 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
2666 | struct bio *bio, loff_t file_offset) | |
2667 | { | |
2668 | struct btrfs_ordered_extent *ordered; | |
d22002fd | 2669 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
abb99cfd | 2670 | u64 file_len; |
d22002fd NA |
2671 | u64 len = bio->bi_iter.bi_size; |
2672 | u64 end = start + len; | |
2673 | u64 ordered_end; | |
2674 | u64 pre, post; | |
2675 | int ret = 0; | |
2676 | ||
2677 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); | |
2678 | if (WARN_ON_ONCE(!ordered)) | |
2679 | return BLK_STS_IOERR; | |
2680 | ||
2681 | /* No need to split */ | |
2682 | if (ordered->disk_num_bytes == len) | |
2683 | goto out; | |
2684 | ||
2685 | /* We cannot split once end_bio'd ordered extent */ | |
2686 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { | |
2687 | ret = -EINVAL; | |
2688 | goto out; | |
2689 | } | |
2690 | ||
2691 | /* We cannot split a compressed ordered extent */ | |
2692 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { | |
2693 | ret = -EINVAL; | |
2694 | goto out; | |
2695 | } | |
2696 | ||
2697 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; | |
2698 | /* bio must be in one ordered extent */ | |
2699 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { | |
2700 | ret = -EINVAL; | |
2701 | goto out; | |
2702 | } | |
2703 | ||
2704 | /* Checksum list should be empty */ | |
2705 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { | |
2706 | ret = -EINVAL; | |
2707 | goto out; | |
2708 | } | |
2709 | ||
abb99cfd | 2710 | file_len = ordered->num_bytes; |
d22002fd NA |
2711 | pre = start - ordered->disk_bytenr; |
2712 | post = ordered_end - end; | |
2713 | ||
2714 | ret = btrfs_split_ordered_extent(ordered, pre, post); | |
2715 | if (ret) | |
2716 | goto out; | |
abb99cfd | 2717 | ret = split_zoned_em(inode, file_offset, file_len, pre, post); |
d22002fd NA |
2718 | |
2719 | out: | |
d22002fd NA |
2720 | btrfs_put_ordered_extent(ordered); |
2721 | ||
2722 | return errno_to_blk_status(ret); | |
2723 | } | |
2724 | ||
c93104e7 | 2725 | void btrfs_submit_data_write_bio(struct inode *inode, struct bio *bio, int mirror_num) |
44b8bd7e | 2726 | { |
0b246afa | 2727 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
c93104e7 CH |
2728 | struct btrfs_inode *bi = BTRFS_I(inode); |
2729 | blk_status_t ret; | |
0417341e | 2730 | |
d22002fd | 2731 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
c93104e7 CH |
2732 | ret = extract_ordered_extent(bi, bio, |
2733 | page_offset(bio_first_bvec_all(bio)->bv_page)); | |
917f32a2 CH |
2734 | if (ret) { |
2735 | btrfs_bio_end_io(btrfs_bio(bio), ret); | |
2736 | return; | |
2737 | } | |
d22002fd NA |
2738 | } |
2739 | ||
c93104e7 | 2740 | /* |
82443fd5 CH |
2741 | * If we need to checksum, and the I/O is not issued by fsync and |
2742 | * friends, that is ->sync_writers != 0, defer the submission to a | |
2743 | * workqueue to parallelize it. | |
2744 | * | |
2745 | * Csum items for reloc roots have already been cloned at this point, | |
2746 | * so they are handled as part of the no-checksum case. | |
c93104e7 CH |
2747 | */ |
2748 | if (!(bi->flags & BTRFS_INODE_NODATASUM) && | |
82443fd5 CH |
2749 | !test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state) && |
2750 | !btrfs_is_data_reloc_root(bi->root)) { | |
ea1f0ced CH |
2751 | if (!atomic_read(&bi->sync_writers) && |
2752 | btrfs_wq_submit_bio(inode, bio, mirror_num, 0, | |
2753 | btrfs_submit_bio_start)) | |
ad357938 | 2754 | return; |
82443fd5 CH |
2755 | |
2756 | ret = btrfs_csum_one_bio(bi, bio, (u64)-1, false); | |
917f32a2 CH |
2757 | if (ret) { |
2758 | btrfs_bio_end_io(btrfs_bio(bio), ret); | |
2759 | return; | |
2760 | } | |
19b9bdb0 | 2761 | } |
1a722d8f | 2762 | btrfs_submit_bio(fs_info, bio, mirror_num); |
c93104e7 | 2763 | } |
19b9bdb0 | 2764 | |
c93104e7 CH |
2765 | void btrfs_submit_data_read_bio(struct inode *inode, struct bio *bio, |
2766 | int mirror_num, enum btrfs_compression_type compress_type) | |
2767 | { | |
2768 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2769 | blk_status_t ret; | |
61891923 | 2770 | |
c93104e7 CH |
2771 | if (compress_type != BTRFS_COMPRESS_NONE) { |
2772 | /* | |
2773 | * btrfs_submit_compressed_read will handle completing the bio | |
2774 | * if there were any errors, so just return here. | |
2775 | */ | |
2776 | btrfs_submit_compressed_read(inode, bio, mirror_num); | |
2777 | return; | |
19b9bdb0 CM |
2778 | } |
2779 | ||
81bd9328 CH |
2780 | /* Save the original iter for read repair */ |
2781 | btrfs_bio(bio)->iter = bio->bi_iter; | |
2782 | ||
c93104e7 CH |
2783 | /* |
2784 | * Lookup bio sums does extra checks around whether we need to csum or | |
2785 | * not, which is why we ignore skip_sum here. | |
2786 | */ | |
2787 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); | |
4e4cbee9 | 2788 | if (ret) { |
917f32a2 | 2789 | btrfs_bio_end_io(btrfs_bio(bio), ret); |
1a722d8f | 2790 | return; |
4246a0b6 | 2791 | } |
1a722d8f CH |
2792 | |
2793 | btrfs_submit_bio(fs_info, bio, mirror_num); | |
065631f6 | 2794 | } |
6885f308 | 2795 | |
d352ac68 CM |
2796 | /* |
2797 | * given a list of ordered sums record them in the inode. This happens | |
2798 | * at IO completion time based on sums calculated at bio submission time. | |
2799 | */ | |
510f85ed NB |
2800 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
2801 | struct list_head *list) | |
e6dcd2dc | 2802 | { |
e6dcd2dc | 2803 | struct btrfs_ordered_sum *sum; |
fc28b25e | 2804 | struct btrfs_root *csum_root = NULL; |
ac01f26a | 2805 | int ret; |
e6dcd2dc | 2806 | |
c6e30871 | 2807 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2808 | trans->adding_csums = true; |
fc28b25e JB |
2809 | if (!csum_root) |
2810 | csum_root = btrfs_csum_root(trans->fs_info, | |
2811 | sum->bytenr); | |
2812 | ret = btrfs_csum_file_blocks(trans, csum_root, sum); | |
7c2871a2 | 2813 | trans->adding_csums = false; |
ac01f26a NB |
2814 | if (ret) |
2815 | return ret; | |
e6dcd2dc CM |
2816 | } |
2817 | return 0; | |
2818 | } | |
2819 | ||
c3347309 FM |
2820 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
2821 | const u64 start, | |
2822 | const u64 len, | |
2823 | struct extent_state **cached_state) | |
2824 | { | |
2825 | u64 search_start = start; | |
2826 | const u64 end = start + len - 1; | |
2827 | ||
2828 | while (search_start < end) { | |
2829 | const u64 search_len = end - search_start + 1; | |
2830 | struct extent_map *em; | |
2831 | u64 em_len; | |
2832 | int ret = 0; | |
2833 | ||
2834 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); | |
2835 | if (IS_ERR(em)) | |
2836 | return PTR_ERR(em); | |
2837 | ||
2838 | if (em->block_start != EXTENT_MAP_HOLE) | |
2839 | goto next; | |
2840 | ||
2841 | em_len = em->len; | |
2842 | if (em->start < search_start) | |
2843 | em_len -= search_start - em->start; | |
2844 | if (em_len > search_len) | |
2845 | em_len = search_len; | |
2846 | ||
2847 | ret = set_extent_bit(&inode->io_tree, search_start, | |
2848 | search_start + em_len - 1, | |
994bcd1e | 2849 | EXTENT_DELALLOC_NEW, cached_state, |
291bbb1e | 2850 | GFP_NOFS); |
c3347309 FM |
2851 | next: |
2852 | search_start = extent_map_end(em); | |
2853 | free_extent_map(em); | |
2854 | if (ret) | |
2855 | return ret; | |
2856 | } | |
2857 | return 0; | |
2858 | } | |
2859 | ||
c2566f22 | 2860 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2861 | unsigned int extra_bits, |
330a5827 | 2862 | struct extent_state **cached_state) |
ea8c2819 | 2863 | { |
fdb1e121 | 2864 | WARN_ON(PAGE_ALIGNED(end)); |
c3347309 FM |
2865 | |
2866 | if (start >= i_size_read(&inode->vfs_inode) && | |
2867 | !(inode->flags & BTRFS_INODE_PREALLOC)) { | |
2868 | /* | |
2869 | * There can't be any extents following eof in this case so just | |
2870 | * set the delalloc new bit for the range directly. | |
2871 | */ | |
2872 | extra_bits |= EXTENT_DELALLOC_NEW; | |
2873 | } else { | |
2874 | int ret; | |
2875 | ||
2876 | ret = btrfs_find_new_delalloc_bytes(inode, start, | |
2877 | end + 1 - start, | |
2878 | cached_state); | |
2879 | if (ret) | |
2880 | return ret; | |
2881 | } | |
2882 | ||
c2566f22 NB |
2883 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
2884 | cached_state); | |
ea8c2819 CM |
2885 | } |
2886 | ||
d352ac68 | 2887 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2888 | struct btrfs_writepage_fixup { |
2889 | struct page *page; | |
f4b1363c | 2890 | struct inode *inode; |
247e743c CM |
2891 | struct btrfs_work work; |
2892 | }; | |
2893 | ||
b2950863 | 2894 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2895 | { |
2896 | struct btrfs_writepage_fixup *fixup; | |
2897 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2898 | struct extent_state *cached_state = NULL; |
364ecf36 | 2899 | struct extent_changeset *data_reserved = NULL; |
247e743c | 2900 | struct page *page; |
65d87f79 | 2901 | struct btrfs_inode *inode; |
247e743c CM |
2902 | u64 page_start; |
2903 | u64 page_end; | |
25f3c502 | 2904 | int ret = 0; |
f4b1363c | 2905 | bool free_delalloc_space = true; |
247e743c CM |
2906 | |
2907 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2908 | page = fixup->page; | |
65d87f79 | 2909 | inode = BTRFS_I(fixup->inode); |
f4b1363c JB |
2910 | page_start = page_offset(page); |
2911 | page_end = page_offset(page) + PAGE_SIZE - 1; | |
2912 | ||
2913 | /* | |
2914 | * This is similar to page_mkwrite, we need to reserve the space before | |
2915 | * we take the page lock. | |
2916 | */ | |
65d87f79 NB |
2917 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2918 | PAGE_SIZE); | |
4a096752 | 2919 | again: |
247e743c | 2920 | lock_page(page); |
25f3c502 CM |
2921 | |
2922 | /* | |
2923 | * Before we queued this fixup, we took a reference on the page. | |
2924 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2925 | * address space. | |
2926 | */ | |
f4b1363c JB |
2927 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2928 | /* | |
2929 | * Unfortunately this is a little tricky, either | |
2930 | * | |
2931 | * 1) We got here and our page had already been dealt with and | |
2932 | * we reserved our space, thus ret == 0, so we need to just | |
2933 | * drop our space reservation and bail. This can happen the | |
2934 | * first time we come into the fixup worker, or could happen | |
2935 | * while waiting for the ordered extent. | |
2936 | * 2) Our page was already dealt with, but we happened to get an | |
2937 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2938 | * this case we obviously don't have anything to release, but | |
2939 | * because the page was already dealt with we don't want to | |
2940 | * mark the page with an error, so make sure we're resetting | |
2941 | * ret to 0. This is why we have this check _before_ the ret | |
2942 | * check, because we do not want to have a surprise ENOSPC | |
2943 | * when the page was already properly dealt with. | |
2944 | */ | |
2945 | if (!ret) { | |
65d87f79 NB |
2946 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2947 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2948 | page_start, PAGE_SIZE, |
2949 | true); | |
2950 | } | |
2951 | ret = 0; | |
247e743c | 2952 | goto out_page; |
f4b1363c | 2953 | } |
247e743c | 2954 | |
25f3c502 | 2955 | /* |
f4b1363c JB |
2956 | * We can't mess with the page state unless it is locked, so now that |
2957 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2958 | */ |
f4b1363c JB |
2959 | if (ret) |
2960 | goto out_page; | |
247e743c | 2961 | |
570eb97b | 2962 | lock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2963 | |
2964 | /* already ordered? We're done */ | |
f57ad937 | 2965 | if (PageOrdered(page)) |
f4b1363c | 2966 | goto out_reserved; |
4a096752 | 2967 | |
65d87f79 | 2968 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2969 | if (ordered) { |
570eb97b JB |
2970 | unlock_extent(&inode->io_tree, page_start, page_end, |
2971 | &cached_state); | |
4a096752 | 2972 | unlock_page(page); |
c0a43603 | 2973 | btrfs_start_ordered_extent(ordered, 1); |
87826df0 | 2974 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2975 | goto again; |
2976 | } | |
247e743c | 2977 | |
65d87f79 | 2978 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2979 | &cached_state); |
25f3c502 | 2980 | if (ret) |
53687007 | 2981 | goto out_reserved; |
f3038ee3 | 2982 | |
25f3c502 CM |
2983 | /* |
2984 | * Everything went as planned, we're now the owner of a dirty page with | |
2985 | * delayed allocation bits set and space reserved for our COW | |
2986 | * destination. | |
2987 | * | |
2988 | * The page was dirty when we started, nothing should have cleaned it. | |
2989 | */ | |
2990 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2991 | free_delalloc_space = false; |
53687007 | 2992 | out_reserved: |
65d87f79 | 2993 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2994 | if (free_delalloc_space) |
65d87f79 NB |
2995 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2996 | PAGE_SIZE, true); | |
570eb97b | 2997 | unlock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
247e743c | 2998 | out_page: |
25f3c502 CM |
2999 | if (ret) { |
3000 | /* | |
3001 | * We hit ENOSPC or other errors. Update the mapping and page | |
3002 | * to reflect the errors and clean the page. | |
3003 | */ | |
3004 | mapping_set_error(page->mapping, ret); | |
3005 | end_extent_writepage(page, ret, page_start, page_end); | |
3006 | clear_page_dirty_for_io(page); | |
3007 | SetPageError(page); | |
3008 | } | |
e4f94347 | 3009 | btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE); |
247e743c | 3010 | unlock_page(page); |
09cbfeaf | 3011 | put_page(page); |
b897abec | 3012 | kfree(fixup); |
364ecf36 | 3013 | extent_changeset_free(data_reserved); |
f4b1363c JB |
3014 | /* |
3015 | * As a precaution, do a delayed iput in case it would be the last iput | |
3016 | * that could need flushing space. Recursing back to fixup worker would | |
3017 | * deadlock. | |
3018 | */ | |
65d87f79 | 3019 | btrfs_add_delayed_iput(&inode->vfs_inode); |
247e743c CM |
3020 | } |
3021 | ||
3022 | /* | |
3023 | * There are a few paths in the higher layers of the kernel that directly | |
3024 | * set the page dirty bit without asking the filesystem if it is a | |
3025 | * good idea. This causes problems because we want to make sure COW | |
3026 | * properly happens and the data=ordered rules are followed. | |
3027 | * | |
c8b97818 | 3028 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
3029 | * hasn't been properly setup for IO. We kick off an async process |
3030 | * to fix it up. The async helper will wait for ordered extents, set | |
3031 | * the delalloc bit and make it safe to write the page. | |
3032 | */ | |
a129ffb8 | 3033 | int btrfs_writepage_cow_fixup(struct page *page) |
247e743c CM |
3034 | { |
3035 | struct inode *inode = page->mapping->host; | |
0b246afa | 3036 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 3037 | struct btrfs_writepage_fixup *fixup; |
247e743c | 3038 | |
f57ad937 QW |
3039 | /* This page has ordered extent covering it already */ |
3040 | if (PageOrdered(page)) | |
247e743c CM |
3041 | return 0; |
3042 | ||
25f3c502 CM |
3043 | /* |
3044 | * PageChecked is set below when we create a fixup worker for this page, | |
3045 | * don't try to create another one if we're already PageChecked() | |
3046 | * | |
3047 | * The extent_io writepage code will redirty the page if we send back | |
3048 | * EAGAIN. | |
3049 | */ | |
247e743c CM |
3050 | if (PageChecked(page)) |
3051 | return -EAGAIN; | |
3052 | ||
3053 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
3054 | if (!fixup) | |
3055 | return -EAGAIN; | |
f421950f | 3056 | |
f4b1363c JB |
3057 | /* |
3058 | * We are already holding a reference to this inode from | |
3059 | * write_cache_pages. We need to hold it because the space reservation | |
3060 | * takes place outside of the page lock, and we can't trust | |
3061 | * page->mapping outside of the page lock. | |
3062 | */ | |
3063 | ihold(inode); | |
e4f94347 | 3064 | btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
09cbfeaf | 3065 | get_page(page); |
a0cac0ec | 3066 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 3067 | fixup->page = page; |
f4b1363c | 3068 | fixup->inode = inode; |
0b246afa | 3069 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
3070 | |
3071 | return -EAGAIN; | |
247e743c CM |
3072 | } |
3073 | ||
d899e052 | 3074 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 3075 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a | 3076 | struct btrfs_file_extent_item *stack_fi, |
2766ff61 | 3077 | const bool update_inode_bytes, |
9729f10a | 3078 | u64 qgroup_reserved) |
d899e052 | 3079 | { |
c553f94d | 3080 | struct btrfs_root *root = inode->root; |
2766ff61 | 3081 | const u64 sectorsize = root->fs_info->sectorsize; |
d899e052 YZ |
3082 | struct btrfs_path *path; |
3083 | struct extent_buffer *leaf; | |
3084 | struct btrfs_key ins; | |
203f44c5 QW |
3085 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
3086 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
cb36a9bb | 3087 | u64 offset = btrfs_stack_file_extent_offset(stack_fi); |
203f44c5 QW |
3088 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
3089 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
5893dfb9 | 3090 | struct btrfs_drop_extents_args drop_args = { 0 }; |
d899e052 YZ |
3091 | int ret; |
3092 | ||
3093 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
3094 | if (!path) |
3095 | return -ENOMEM; | |
d899e052 | 3096 | |
a1ed835e CM |
3097 | /* |
3098 | * we may be replacing one extent in the tree with another. | |
3099 | * The new extent is pinned in the extent map, and we don't want | |
3100 | * to drop it from the cache until it is completely in the btree. | |
3101 | * | |
3102 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
3103 | * the caller is expected to unpin it and allow it to be merged | |
3104 | * with the others. | |
3105 | */ | |
5893dfb9 FM |
3106 | drop_args.path = path; |
3107 | drop_args.start = file_pos; | |
3108 | drop_args.end = file_pos + num_bytes; | |
3109 | drop_args.replace_extent = true; | |
3110 | drop_args.extent_item_size = sizeof(*stack_fi); | |
3111 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); | |
79787eaa JM |
3112 | if (ret) |
3113 | goto out; | |
d899e052 | 3114 | |
5893dfb9 | 3115 | if (!drop_args.extent_inserted) { |
c553f94d | 3116 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
3117 | ins.offset = file_pos; |
3118 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
3119 | ||
1acae57b | 3120 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
203f44c5 | 3121 | sizeof(*stack_fi)); |
1acae57b FDBM |
3122 | if (ret) |
3123 | goto out; | |
3124 | } | |
d899e052 | 3125 | leaf = path->nodes[0]; |
203f44c5 QW |
3126 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
3127 | write_extent_buffer(leaf, stack_fi, | |
3128 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
3129 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 3130 | |
d899e052 | 3131 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 3132 | btrfs_release_path(path); |
d899e052 | 3133 | |
2766ff61 FM |
3134 | /* |
3135 | * If we dropped an inline extent here, we know the range where it is | |
3136 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the | |
1a9fd417 | 3137 | * number of bytes only for that range containing the inline extent. |
2766ff61 FM |
3138 | * The remaining of the range will be processed when clearning the |
3139 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. | |
3140 | */ | |
3141 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { | |
3142 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); | |
3143 | ||
3144 | inline_size = drop_args.bytes_found - inline_size; | |
3145 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); | |
3146 | drop_args.bytes_found -= inline_size; | |
3147 | num_bytes -= sectorsize; | |
3148 | } | |
3149 | ||
3150 | if (update_inode_bytes) | |
3151 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); | |
d899e052 YZ |
3152 | |
3153 | ins.objectid = disk_bytenr; | |
3154 | ins.offset = disk_num_bytes; | |
3155 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 3156 | |
c553f94d | 3157 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
3158 | if (ret) |
3159 | goto out; | |
3160 | ||
c553f94d | 3161 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
cb36a9bb OS |
3162 | file_pos - offset, |
3163 | qgroup_reserved, &ins); | |
79787eaa | 3164 | out: |
d899e052 | 3165 | btrfs_free_path(path); |
b9473439 | 3166 | |
79787eaa | 3167 | return ret; |
d899e052 YZ |
3168 | } |
3169 | ||
2ff7e61e | 3170 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
3171 | u64 start, u64 len) |
3172 | { | |
32da5386 | 3173 | struct btrfs_block_group *cache; |
e570fd27 | 3174 | |
0b246afa | 3175 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
3176 | ASSERT(cache); |
3177 | ||
3178 | spin_lock(&cache->lock); | |
3179 | cache->delalloc_bytes -= len; | |
3180 | spin_unlock(&cache->lock); | |
3181 | ||
3182 | btrfs_put_block_group(cache); | |
3183 | } | |
3184 | ||
203f44c5 | 3185 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
203f44c5 QW |
3186 | struct btrfs_ordered_extent *oe) |
3187 | { | |
3188 | struct btrfs_file_extent_item stack_fi; | |
2766ff61 | 3189 | bool update_inode_bytes; |
cb36a9bb OS |
3190 | u64 num_bytes = oe->num_bytes; |
3191 | u64 ram_bytes = oe->ram_bytes; | |
203f44c5 QW |
3192 | |
3193 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
3194 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
3195 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
3196 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
3197 | oe->disk_num_bytes); | |
cb36a9bb | 3198 | btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset); |
c1867eb3 DS |
3199 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) { |
3200 | num_bytes = oe->truncated_len; | |
3201 | ram_bytes = num_bytes; | |
3202 | } | |
cb36a9bb OS |
3203 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes); |
3204 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes); | |
203f44c5 QW |
3205 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
3206 | /* Encryption and other encoding is reserved and all 0 */ | |
3207 | ||
2766ff61 FM |
3208 | /* |
3209 | * For delalloc, when completing an ordered extent we update the inode's | |
3210 | * bytes when clearing the range in the inode's io tree, so pass false | |
3211 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), | |
3212 | * except if the ordered extent was truncated. | |
3213 | */ | |
3214 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || | |
7c0c7269 | 3215 | test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) || |
2766ff61 FM |
3216 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
3217 | ||
3c38c877 NB |
3218 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
3219 | oe->file_offset, &stack_fi, | |
2766ff61 | 3220 | update_inode_bytes, oe->qgroup_rsv); |
203f44c5 QW |
3221 | } |
3222 | ||
3223 | /* | |
3224 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
3225 | * an ordered extent if the range of bytes in the file it covers are |
3226 | * fully written. | |
3227 | */ | |
711f447b | 3228 | int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 3229 | { |
72e7e6ed NB |
3230 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
3231 | struct btrfs_root *root = inode->root; | |
3232 | struct btrfs_fs_info *fs_info = root->fs_info; | |
0ca1f7ce | 3233 | struct btrfs_trans_handle *trans = NULL; |
72e7e6ed | 3234 | struct extent_io_tree *io_tree = &inode->io_tree; |
2ac55d41 | 3235 | struct extent_state *cached_state = NULL; |
bffe633e | 3236 | u64 start, end; |
261507a0 | 3237 | int compress_type = 0; |
77cef2ec | 3238 | int ret = 0; |
bffe633e | 3239 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 3240 | bool freespace_inode; |
77cef2ec | 3241 | bool truncated = false; |
49940bdd | 3242 | bool clear_reserved_extent = true; |
2766ff61 | 3243 | unsigned int clear_bits = EXTENT_DEFRAG; |
a7e3b975 | 3244 | |
bffe633e OS |
3245 | start = ordered_extent->file_offset; |
3246 | end = start + ordered_extent->num_bytes - 1; | |
3247 | ||
a7e3b975 FM |
3248 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
3249 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
7c0c7269 OS |
3250 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) && |
3251 | !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags)) | |
2766ff61 | 3252 | clear_bits |= EXTENT_DELALLOC_NEW; |
e6dcd2dc | 3253 | |
72e7e6ed | 3254 | freespace_inode = btrfs_is_free_space_inode(inode); |
5f4403e1 IA |
3255 | if (!freespace_inode) |
3256 | btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent); | |
0cb59c99 | 3257 | |
5fd02043 JB |
3258 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
3259 | ret = -EIO; | |
3260 | goto out; | |
3261 | } | |
3262 | ||
be1a1d7a NA |
3263 | /* A valid bdev implies a write on a sequential zone */ |
3264 | if (ordered_extent->bdev) { | |
d8e3fb10 | 3265 | btrfs_rewrite_logical_zoned(ordered_extent); |
be1a1d7a NA |
3266 | btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, |
3267 | ordered_extent->disk_num_bytes); | |
3268 | } | |
d8e3fb10 | 3269 | |
72e7e6ed | 3270 | btrfs_free_io_failure_record(inode, start, end); |
f612496b | 3271 | |
77cef2ec JB |
3272 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
3273 | truncated = true; | |
3274 | logical_len = ordered_extent->truncated_len; | |
3275 | /* Truncated the entire extent, don't bother adding */ | |
3276 | if (!logical_len) | |
3277 | goto out; | |
3278 | } | |
3279 | ||
c2167754 | 3280 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 3281 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 3282 | |
72e7e6ed | 3283 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
3284 | if (freespace_inode) |
3285 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
3286 | else |
3287 | trans = btrfs_join_transaction(root); | |
3288 | if (IS_ERR(trans)) { | |
3289 | ret = PTR_ERR(trans); | |
3290 | trans = NULL; | |
3291 | goto out; | |
c2167754 | 3292 | } |
72e7e6ed | 3293 | trans->block_rsv = &inode->block_rsv; |
729f7961 | 3294 | ret = btrfs_update_inode_fallback(trans, root, inode); |
6c760c07 | 3295 | if (ret) /* -ENOMEM or corruption */ |
66642832 | 3296 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
3297 | goto out; |
3298 | } | |
e6dcd2dc | 3299 | |
2766ff61 | 3300 | clear_bits |= EXTENT_LOCKED; |
570eb97b | 3301 | lock_extent(io_tree, start, end, &cached_state); |
e6dcd2dc | 3302 | |
8d510121 NB |
3303 | if (freespace_inode) |
3304 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 3305 | else |
7a7eaa40 | 3306 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3307 | if (IS_ERR(trans)) { |
3308 | ret = PTR_ERR(trans); | |
3309 | trans = NULL; | |
a7e3b975 | 3310 | goto out; |
79787eaa | 3311 | } |
a79b7d4b | 3312 | |
72e7e6ed | 3313 | trans->block_rsv = &inode->block_rsv; |
c2167754 | 3314 | |
c8b97818 | 3315 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3316 | compress_type = ordered_extent->compress_type; |
d899e052 | 3317 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3318 | BUG_ON(compress_type); |
72e7e6ed | 3319 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
3320 | ordered_extent->file_offset, |
3321 | ordered_extent->file_offset + | |
77cef2ec | 3322 | logical_len); |
343d8a30 NA |
3323 | btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr, |
3324 | ordered_extent->disk_num_bytes); | |
d899e052 | 3325 | } else { |
0b246afa | 3326 | BUG_ON(root == fs_info->tree_root); |
3c38c877 | 3327 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
49940bdd JB |
3328 | if (!ret) { |
3329 | clear_reserved_extent = false; | |
2ff7e61e | 3330 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
3331 | ordered_extent->disk_bytenr, |
3332 | ordered_extent->disk_num_bytes); | |
49940bdd | 3333 | } |
d899e052 | 3334 | } |
72e7e6ed | 3335 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
bffe633e | 3336 | ordered_extent->num_bytes, trans->transid); |
79787eaa | 3337 | if (ret < 0) { |
66642832 | 3338 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3339 | goto out; |
79787eaa | 3340 | } |
2ac55d41 | 3341 | |
510f85ed | 3342 | ret = add_pending_csums(trans, &ordered_extent->list); |
ac01f26a NB |
3343 | if (ret) { |
3344 | btrfs_abort_transaction(trans, ret); | |
3345 | goto out; | |
3346 | } | |
e6dcd2dc | 3347 | |
2766ff61 FM |
3348 | /* |
3349 | * If this is a new delalloc range, clear its new delalloc flag to | |
3350 | * update the inode's number of bytes. This needs to be done first | |
3351 | * before updating the inode item. | |
3352 | */ | |
3353 | if ((clear_bits & EXTENT_DELALLOC_NEW) && | |
3354 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) | |
72e7e6ed | 3355 | clear_extent_bit(&inode->io_tree, start, end, |
2766ff61 | 3356 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
bd015294 | 3357 | &cached_state); |
2766ff61 | 3358 | |
72e7e6ed | 3359 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
729f7961 | 3360 | ret = btrfs_update_inode_fallback(trans, root, inode); |
6c760c07 | 3361 | if (ret) { /* -ENOMEM or corruption */ |
66642832 | 3362 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3363 | goto out; |
1ef30be1 JB |
3364 | } |
3365 | ret = 0; | |
c2167754 | 3366 | out: |
bd015294 | 3367 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
313facc5 | 3368 | &cached_state); |
a7e3b975 | 3369 | |
a698d075 | 3370 | if (trans) |
3a45bb20 | 3371 | btrfs_end_transaction(trans); |
0cb59c99 | 3372 | |
77cef2ec | 3373 | if (ret || truncated) { |
bffe633e | 3374 | u64 unwritten_start = start; |
77cef2ec | 3375 | |
d61bec08 JB |
3376 | /* |
3377 | * If we failed to finish this ordered extent for any reason we | |
3378 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered | |
3379 | * extent, and mark the inode with the error if it wasn't | |
3380 | * already set. Any error during writeback would have already | |
3381 | * set the mapping error, so we need to set it if we're the ones | |
3382 | * marking this ordered extent as failed. | |
3383 | */ | |
3384 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, | |
3385 | &ordered_extent->flags)) | |
3386 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); | |
3387 | ||
77cef2ec | 3388 | if (truncated) |
bffe633e OS |
3389 | unwritten_start += logical_len; |
3390 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec | 3391 | |
4c0c8cfc FM |
3392 | /* Drop extent maps for the part of the extent we didn't write. */ |
3393 | btrfs_drop_extent_map_range(inode, unwritten_start, end, false); | |
5fd02043 | 3394 | |
0bec9ef5 JB |
3395 | /* |
3396 | * If the ordered extent had an IOERR or something else went | |
3397 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3398 | * back to the allocator. We only free the extent in the |
3399 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3400 | * |
3401 | * If we made it past insert_reserved_file_extent before we | |
3402 | * errored out then we don't need to do this as the accounting | |
3403 | * has already been done. | |
0bec9ef5 | 3404 | */ |
77cef2ec | 3405 | if ((ret || !logical_len) && |
49940bdd | 3406 | clear_reserved_extent && |
77cef2ec | 3407 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
3408 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
3409 | /* | |
3410 | * Discard the range before returning it back to the | |
3411 | * free space pool | |
3412 | */ | |
46b27f50 | 3413 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 3414 | btrfs_discard_extent(fs_info, |
bffe633e OS |
3415 | ordered_extent->disk_bytenr, |
3416 | ordered_extent->disk_num_bytes, | |
3417 | NULL); | |
2ff7e61e | 3418 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
3419 | ordered_extent->disk_bytenr, |
3420 | ordered_extent->disk_num_bytes, 1); | |
4eaaec24 | 3421 | } |
0bec9ef5 JB |
3422 | } |
3423 | ||
5fd02043 | 3424 | /* |
8bad3c02 LB |
3425 | * This needs to be done to make sure anybody waiting knows we are done |
3426 | * updating everything for this ordered extent. | |
5fd02043 | 3427 | */ |
72e7e6ed | 3428 | btrfs_remove_ordered_extent(inode, ordered_extent); |
5fd02043 | 3429 | |
e6dcd2dc CM |
3430 | /* once for us */ |
3431 | btrfs_put_ordered_extent(ordered_extent); | |
3432 | /* once for the tree */ | |
3433 | btrfs_put_ordered_extent(ordered_extent); | |
3434 | ||
5fd02043 JB |
3435 | return ret; |
3436 | } | |
3437 | ||
38a39ac7 QW |
3438 | void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, |
3439 | struct page *page, u64 start, | |
25c1252a | 3440 | u64 end, bool uptodate) |
211f90e6 | 3441 | { |
38a39ac7 | 3442 | trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate); |
1abe9b8a | 3443 | |
711f447b | 3444 | btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start, uptodate); |
211f90e6 CM |
3445 | } |
3446 | ||
ae643a74 QW |
3447 | /* |
3448 | * Verify the checksum for a single sector without any extra action that depend | |
3449 | * on the type of I/O. | |
3450 | */ | |
3451 | int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page, | |
3452 | u32 pgoff, u8 *csum, const u8 * const csum_expected) | |
3453 | { | |
3454 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
3455 | char *kaddr; | |
3456 | ||
3457 | ASSERT(pgoff + fs_info->sectorsize <= PAGE_SIZE); | |
3458 | ||
3459 | shash->tfm = fs_info->csum_shash; | |
3460 | ||
3461 | kaddr = kmap_local_page(page) + pgoff; | |
3462 | crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum); | |
3463 | kunmap_local(kaddr); | |
3464 | ||
3465 | if (memcmp(csum, csum_expected, fs_info->csum_size)) | |
3466 | return -EIO; | |
3467 | return 0; | |
211f90e6 CM |
3468 | } |
3469 | ||
f119553f JB |
3470 | static u8 *btrfs_csum_ptr(const struct btrfs_fs_info *fs_info, u8 *csums, u64 offset) |
3471 | { | |
3472 | u64 offset_in_sectors = offset >> fs_info->sectorsize_bits; | |
3473 | ||
3474 | return csums + offset_in_sectors * fs_info->csum_size; | |
3475 | } | |
3476 | ||
265d4ac0 QW |
3477 | /* |
3478 | * check_data_csum - verify checksum of one sector of uncompressed data | |
7ffd27e3 | 3479 | * @inode: inode |
7959bd44 | 3480 | * @bbio: btrfs_bio which contains the csum |
7ffd27e3 | 3481 | * @bio_offset: offset to the beginning of the bio (in bytes) |
265d4ac0 QW |
3482 | * @page: page where is the data to be verified |
3483 | * @pgoff: offset inside the page | |
3484 | * | |
3485 | * The length of such check is always one sector size. | |
ae643a74 QW |
3486 | * |
3487 | * When csum mismatch is detected, we will also report the error and fill the | |
3488 | * corrupted range with zero. (Thus it needs the extra parameters) | |
265d4ac0 | 3489 | */ |
7959bd44 CH |
3490 | int btrfs_check_data_csum(struct inode *inode, struct btrfs_bio *bbio, |
3491 | u32 bio_offset, struct page *page, u32 pgoff) | |
dc380aea | 3492 | { |
d5178578 | 3493 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
265d4ac0 | 3494 | u32 len = fs_info->sectorsize; |
d5178578 JT |
3495 | u8 *csum_expected; |
3496 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 3497 | |
265d4ac0 QW |
3498 | ASSERT(pgoff + len <= PAGE_SIZE); |
3499 | ||
a89ce08c | 3500 | csum_expected = btrfs_csum_ptr(fs_info, bbio->csum, bio_offset); |
d5178578 | 3501 | |
ae643a74 | 3502 | if (btrfs_check_sector_csum(fs_info, page, pgoff, csum, csum_expected)) |
dc380aea | 3503 | goto zeroit; |
dc380aea | 3504 | return 0; |
ae643a74 | 3505 | |
dc380aea | 3506 | zeroit: |
7959bd44 CH |
3507 | btrfs_print_data_csum_error(BTRFS_I(inode), |
3508 | bbio->file_offset + bio_offset, | |
3509 | csum, csum_expected, bbio->mirror_num); | |
c3a3b19b QW |
3510 | if (bbio->device) |
3511 | btrfs_dev_stat_inc_and_print(bbio->device, | |
814723e0 | 3512 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
b06660b5 | 3513 | memzero_page(page, pgoff, len); |
dc380aea MX |
3514 | return -EIO; |
3515 | } | |
3516 | ||
d352ac68 | 3517 | /* |
7ffd27e3 | 3518 | * When reads are done, we need to check csums to verify the data is correct. |
4a54c8c1 JS |
3519 | * if there's a match, we allow the bio to finish. If not, the code in |
3520 | * extent_io.c will try to find good copies for us. | |
7ffd27e3 QW |
3521 | * |
3522 | * @bio_offset: offset to the beginning of the bio (in bytes) | |
3523 | * @start: file offset of the range start | |
3524 | * @end: file offset of the range end (inclusive) | |
08508fea QW |
3525 | * |
3526 | * Return a bitmap where bit set means a csum mismatch, and bit not set means | |
3527 | * csum match. | |
d352ac68 | 3528 | */ |
c3a3b19b QW |
3529 | unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio, |
3530 | u32 bio_offset, struct page *page, | |
3531 | u64 start, u64 end) | |
07157aac | 3532 | { |
07157aac | 3533 | struct inode *inode = page->mapping->host; |
e4f94347 | 3534 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
d1310b2e | 3535 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3536 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f44cf410 QW |
3537 | const u32 sectorsize = root->fs_info->sectorsize; |
3538 | u32 pg_off; | |
08508fea | 3539 | unsigned int result = 0; |
d1310b2e | 3540 | |
3670e645 | 3541 | /* |
e4f94347 QW |
3542 | * This only happens for NODATASUM or compressed read. |
3543 | * Normally this should be covered by above check for compressed read | |
3544 | * or the next check for NODATASUM. Just do a quicker exit here. | |
3670e645 | 3545 | */ |
c3a3b19b | 3546 | if (bbio->csum == NULL) |
dc380aea | 3547 | return 0; |
17d217fe | 3548 | |
6cbff00f | 3549 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
42437a63 JB |
3550 | return 0; |
3551 | ||
056c8311 | 3552 | if (unlikely(test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))) |
b6cda9bc | 3553 | return 0; |
d20f7043 | 3554 | |
f44cf410 QW |
3555 | ASSERT(page_offset(page) <= start && |
3556 | end <= page_offset(page) + PAGE_SIZE - 1); | |
3557 | for (pg_off = offset_in_page(start); | |
3558 | pg_off < offset_in_page(end); | |
3559 | pg_off += sectorsize, bio_offset += sectorsize) { | |
e3c62324 | 3560 | u64 file_offset = pg_off + page_offset(page); |
f44cf410 QW |
3561 | int ret; |
3562 | ||
37f00a6d | 3563 | if (btrfs_is_data_reloc_root(root) && |
e3c62324 QW |
3564 | test_range_bit(io_tree, file_offset, |
3565 | file_offset + sectorsize - 1, | |
3566 | EXTENT_NODATASUM, 1, NULL)) { | |
3567 | /* Skip the range without csum for data reloc inode */ | |
3568 | clear_extent_bits(io_tree, file_offset, | |
3569 | file_offset + sectorsize - 1, | |
3570 | EXTENT_NODATASUM); | |
3571 | continue; | |
3572 | } | |
7959bd44 | 3573 | ret = btrfs_check_data_csum(inode, bbio, bio_offset, page, pg_off); |
08508fea QW |
3574 | if (ret < 0) { |
3575 | const int nr_bit = (pg_off - offset_in_page(start)) >> | |
3576 | root->fs_info->sectorsize_bits; | |
3577 | ||
3578 | result |= (1U << nr_bit); | |
3579 | } | |
f44cf410 | 3580 | } |
08508fea | 3581 | return result; |
07157aac | 3582 | } |
b888db2b | 3583 | |
c1c3fac2 NB |
3584 | /* |
3585 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
3586 | * | |
3587 | * @inode: The inode we want to perform iput on | |
3588 | * | |
3589 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3590 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3591 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3592 | * transaction commit time/superblock commit/cleaner kthread. | |
3593 | */ | |
24bbcf04 YZ |
3594 | void btrfs_add_delayed_iput(struct inode *inode) |
3595 | { | |
0b246afa | 3596 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3597 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3598 | |
3599 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3600 | return; | |
3601 | ||
034f784d | 3602 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 3603 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
3604 | ASSERT(list_empty(&binode->delayed_iput)); |
3605 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 3606 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
3607 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3608 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3609 | } |
3610 | ||
63611e73 JB |
3611 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3612 | struct btrfs_inode *inode) | |
3613 | { | |
3614 | list_del_init(&inode->delayed_iput); | |
3615 | spin_unlock(&fs_info->delayed_iput_lock); | |
3616 | iput(&inode->vfs_inode); | |
3617 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3618 | wake_up(&fs_info->delayed_iputs_wait); | |
3619 | spin_lock(&fs_info->delayed_iput_lock); | |
3620 | } | |
3621 | ||
3622 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3623 | struct btrfs_inode *inode) | |
3624 | { | |
3625 | if (!list_empty(&inode->delayed_iput)) { | |
3626 | spin_lock(&fs_info->delayed_iput_lock); | |
3627 | if (!list_empty(&inode->delayed_iput)) | |
3628 | run_delayed_iput_locked(fs_info, inode); | |
3629 | spin_unlock(&fs_info->delayed_iput_lock); | |
3630 | } | |
3631 | } | |
3632 | ||
2ff7e61e | 3633 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3634 | { |
24bbcf04 | 3635 | |
24bbcf04 | 3636 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3637 | while (!list_empty(&fs_info->delayed_iputs)) { |
3638 | struct btrfs_inode *inode; | |
3639 | ||
3640 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3641 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3642 | run_delayed_iput_locked(fs_info, inode); |
71795ee5 | 3643 | cond_resched_lock(&fs_info->delayed_iput_lock); |
24bbcf04 | 3644 | } |
8089fe62 | 3645 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3646 | } |
3647 | ||
e43eec81 | 3648 | /* |
2639631d NB |
3649 | * Wait for flushing all delayed iputs |
3650 | * | |
3651 | * @fs_info: the filesystem | |
034f784d JB |
3652 | * |
3653 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3654 | * set. Once they are all done running we will return, unless we are killed in | |
3655 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3656 | * that might get blocked on the iputs. | |
2639631d NB |
3657 | * |
3658 | * Return EINTR if we were killed, 0 if nothing's pending | |
034f784d JB |
3659 | */ |
3660 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3661 | { | |
3662 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3663 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3664 | if (ret) | |
3665 | return -EINTR; | |
3666 | return 0; | |
3667 | } | |
3668 | ||
7b128766 | 3669 | /* |
f7e9e8fc OS |
3670 | * This creates an orphan entry for the given inode in case something goes wrong |
3671 | * in the middle of an unlink. | |
7b128766 | 3672 | */ |
73f2e545 | 3673 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3674 | struct btrfs_inode *inode) |
7b128766 | 3675 | { |
d68fc57b | 3676 | int ret; |
7b128766 | 3677 | |
27919067 OS |
3678 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3679 | if (ret && ret != -EEXIST) { | |
3680 | btrfs_abort_transaction(trans, ret); | |
3681 | return ret; | |
d68fc57b YZ |
3682 | } |
3683 | ||
d68fc57b | 3684 | return 0; |
7b128766 JB |
3685 | } |
3686 | ||
3687 | /* | |
f7e9e8fc OS |
3688 | * We have done the delete so we can go ahead and remove the orphan item for |
3689 | * this particular inode. | |
7b128766 | 3690 | */ |
48a3b636 | 3691 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3692 | struct btrfs_inode *inode) |
7b128766 | 3693 | { |
27919067 | 3694 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3695 | } |
3696 | ||
3697 | /* | |
3698 | * this cleans up any orphans that may be left on the list from the last use | |
3699 | * of this root. | |
3700 | */ | |
66b4ffd1 | 3701 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3702 | { |
0b246afa | 3703 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3704 | struct btrfs_path *path; |
3705 | struct extent_buffer *leaf; | |
7b128766 JB |
3706 | struct btrfs_key key, found_key; |
3707 | struct btrfs_trans_handle *trans; | |
3708 | struct inode *inode; | |
8f6d7f4f | 3709 | u64 last_objectid = 0; |
f7e9e8fc | 3710 | int ret = 0, nr_unlink = 0; |
7b128766 | 3711 | |
54230013 | 3712 | if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) |
66b4ffd1 | 3713 | return 0; |
c71bf099 YZ |
3714 | |
3715 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3716 | if (!path) { |
3717 | ret = -ENOMEM; | |
3718 | goto out; | |
3719 | } | |
e4058b54 | 3720 | path->reada = READA_BACK; |
7b128766 JB |
3721 | |
3722 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3723 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3724 | key.offset = (u64)-1; |
3725 | ||
7b128766 JB |
3726 | while (1) { |
3727 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3728 | if (ret < 0) |
3729 | goto out; | |
7b128766 JB |
3730 | |
3731 | /* | |
3732 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3733 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3734 | * find the key and see if we have stuff that matches |
3735 | */ | |
3736 | if (ret > 0) { | |
66b4ffd1 | 3737 | ret = 0; |
7b128766 JB |
3738 | if (path->slots[0] == 0) |
3739 | break; | |
3740 | path->slots[0]--; | |
3741 | } | |
3742 | ||
3743 | /* pull out the item */ | |
3744 | leaf = path->nodes[0]; | |
7b128766 JB |
3745 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3746 | ||
3747 | /* make sure the item matches what we want */ | |
3748 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3749 | break; | |
962a298f | 3750 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3751 | break; |
3752 | ||
3753 | /* release the path since we're done with it */ | |
b3b4aa74 | 3754 | btrfs_release_path(path); |
7b128766 JB |
3755 | |
3756 | /* | |
3757 | * this is where we are basically btrfs_lookup, without the | |
3758 | * crossing root thing. we store the inode number in the | |
3759 | * offset of the orphan item. | |
3760 | */ | |
8f6d7f4f JB |
3761 | |
3762 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3763 | btrfs_err(fs_info, |
3764 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3765 | ret = -EINVAL; |
3766 | goto out; | |
3767 | } | |
3768 | ||
3769 | last_objectid = found_key.offset; | |
3770 | ||
5d4f98a2 YZ |
3771 | found_key.objectid = found_key.offset; |
3772 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3773 | found_key.offset = 0; | |
0202e83f | 3774 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
8c6ffba0 | 3775 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3776 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3777 | goto out; |
7b128766 | 3778 | |
0b246afa | 3779 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 | 3780 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3781 | int is_dead_root = 0; |
3782 | ||
3783 | /* | |
0c0218e9 | 3784 | * This is an orphan in the tree root. Currently these |
f8e9e0b0 | 3785 | * could come from 2 sources: |
0c0218e9 | 3786 | * a) a root (snapshot/subvolume) deletion in progress |
f8e9e0b0 | 3787 | * b) a free space cache inode |
0c0218e9 FM |
3788 | * We need to distinguish those two, as the orphan item |
3789 | * for a root must not get deleted before the deletion | |
3790 | * of the snapshot/subvolume's tree completes. | |
3791 | * | |
3792 | * btrfs_find_orphan_roots() ran before us, which has | |
3793 | * found all deleted roots and loaded them into | |
fc7cbcd4 | 3794 | * fs_info->fs_roots_radix. So here we can find if an |
0c0218e9 | 3795 | * orphan item corresponds to a deleted root by looking |
fc7cbcd4 | 3796 | * up the root from that radix tree. |
f8e9e0b0 | 3797 | */ |
a619b3c7 | 3798 | |
fc7cbcd4 DS |
3799 | spin_lock(&fs_info->fs_roots_radix_lock); |
3800 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3801 | (unsigned long)found_key.objectid); | |
a619b3c7 RK |
3802 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
3803 | is_dead_root = 1; | |
fc7cbcd4 | 3804 | spin_unlock(&fs_info->fs_roots_radix_lock); |
a619b3c7 | 3805 | |
f8e9e0b0 AJ |
3806 | if (is_dead_root) { |
3807 | /* prevent this orphan from being found again */ | |
3808 | key.offset = found_key.objectid - 1; | |
3809 | continue; | |
3810 | } | |
f7e9e8fc | 3811 | |
f8e9e0b0 | 3812 | } |
f7e9e8fc | 3813 | |
7b128766 | 3814 | /* |
f7e9e8fc | 3815 | * If we have an inode with links, there are a couple of |
70524253 BB |
3816 | * possibilities: |
3817 | * | |
3818 | * 1. We were halfway through creating fsverity metadata for the | |
3819 | * file. In that case, the orphan item represents incomplete | |
3820 | * fsverity metadata which must be cleaned up with | |
3821 | * btrfs_drop_verity_items and deleting the orphan item. | |
3822 | ||
3823 | * 2. Old kernels (before v3.12) used to create an | |
f7e9e8fc OS |
3824 | * orphan item for truncate indicating that there were possibly |
3825 | * extent items past i_size that needed to be deleted. In v3.12, | |
3826 | * truncate was changed to update i_size in sync with the extent | |
3827 | * items, but the (useless) orphan item was still created. Since | |
3828 | * v4.18, we don't create the orphan item for truncate at all. | |
3829 | * | |
3830 | * So, this item could mean that we need to do a truncate, but | |
3831 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3832 | * and was not cleanly unmounted. The odds of that are quite | |
3833 | * slim, and it's a pain to do the truncate now, so just delete | |
3834 | * the orphan item. | |
3835 | * | |
3836 | * It's also possible that this orphan item was supposed to be | |
3837 | * deleted but wasn't. The inode number may have been reused, | |
3838 | * but either way, we can delete the orphan item. | |
7b128766 | 3839 | */ |
f7e9e8fc | 3840 | if (ret == -ENOENT || inode->i_nlink) { |
70524253 BB |
3841 | if (!ret) { |
3842 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); | |
f7e9e8fc | 3843 | iput(inode); |
70524253 BB |
3844 | if (ret) |
3845 | goto out; | |
3846 | } | |
a8c9e576 | 3847 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3848 | if (IS_ERR(trans)) { |
3849 | ret = PTR_ERR(trans); | |
3850 | goto out; | |
3851 | } | |
0b246afa JM |
3852 | btrfs_debug(fs_info, "auto deleting %Lu", |
3853 | found_key.objectid); | |
a8c9e576 JB |
3854 | ret = btrfs_del_orphan_item(trans, root, |
3855 | found_key.objectid); | |
3a45bb20 | 3856 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3857 | if (ret) |
3858 | goto out; | |
7b128766 JB |
3859 | continue; |
3860 | } | |
3861 | ||
f7e9e8fc | 3862 | nr_unlink++; |
7b128766 JB |
3863 | |
3864 | /* this will do delete_inode and everything for us */ | |
3865 | iput(inode); | |
3866 | } | |
3254c876 MX |
3867 | /* release the path since we're done with it */ |
3868 | btrfs_release_path(path); | |
3869 | ||
a575ceeb | 3870 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3871 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3872 | if (!IS_ERR(trans)) |
3a45bb20 | 3873 | btrfs_end_transaction(trans); |
d68fc57b | 3874 | } |
7b128766 JB |
3875 | |
3876 | if (nr_unlink) | |
0b246afa | 3877 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3878 | |
3879 | out: | |
3880 | if (ret) | |
0b246afa | 3881 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3882 | btrfs_free_path(path); |
3883 | return ret; | |
7b128766 JB |
3884 | } |
3885 | ||
46a53cca CM |
3886 | /* |
3887 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3888 | * don't find any xattrs, we know there can't be any acls. | |
3889 | * | |
3890 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3891 | */ | |
3892 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3893 | int slot, u64 objectid, |
3894 | int *first_xattr_slot) | |
46a53cca CM |
3895 | { |
3896 | u32 nritems = btrfs_header_nritems(leaf); | |
3897 | struct btrfs_key found_key; | |
f23b5a59 JB |
3898 | static u64 xattr_access = 0; |
3899 | static u64 xattr_default = 0; | |
46a53cca CM |
3900 | int scanned = 0; |
3901 | ||
f23b5a59 | 3902 | if (!xattr_access) { |
97d79299 AG |
3903 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3904 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3905 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3906 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3907 | } |
3908 | ||
46a53cca | 3909 | slot++; |
63541927 | 3910 | *first_xattr_slot = -1; |
46a53cca CM |
3911 | while (slot < nritems) { |
3912 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3913 | ||
3914 | /* we found a different objectid, there must not be acls */ | |
3915 | if (found_key.objectid != objectid) | |
3916 | return 0; | |
3917 | ||
3918 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3919 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3920 | if (*first_xattr_slot == -1) |
3921 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3922 | if (found_key.offset == xattr_access || |
3923 | found_key.offset == xattr_default) | |
3924 | return 1; | |
3925 | } | |
46a53cca CM |
3926 | |
3927 | /* | |
3928 | * we found a key greater than an xattr key, there can't | |
3929 | * be any acls later on | |
3930 | */ | |
3931 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3932 | return 0; | |
3933 | ||
3934 | slot++; | |
3935 | scanned++; | |
3936 | ||
3937 | /* | |
3938 | * it goes inode, inode backrefs, xattrs, extents, | |
3939 | * so if there are a ton of hard links to an inode there can | |
3940 | * be a lot of backrefs. Don't waste time searching too hard, | |
3941 | * this is just an optimization | |
3942 | */ | |
3943 | if (scanned >= 8) | |
3944 | break; | |
3945 | } | |
3946 | /* we hit the end of the leaf before we found an xattr or | |
3947 | * something larger than an xattr. We have to assume the inode | |
3948 | * has acls | |
3949 | */ | |
63541927 FDBM |
3950 | if (*first_xattr_slot == -1) |
3951 | *first_xattr_slot = slot; | |
46a53cca CM |
3952 | return 1; |
3953 | } | |
3954 | ||
d352ac68 CM |
3955 | /* |
3956 | * read an inode from the btree into the in-memory inode | |
3957 | */ | |
4222ea71 FM |
3958 | static int btrfs_read_locked_inode(struct inode *inode, |
3959 | struct btrfs_path *in_path) | |
39279cc3 | 3960 | { |
0b246afa | 3961 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3962 | struct btrfs_path *path = in_path; |
5f39d397 | 3963 | struct extent_buffer *leaf; |
39279cc3 CM |
3964 | struct btrfs_inode_item *inode_item; |
3965 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3966 | struct btrfs_key location; | |
67de1176 | 3967 | unsigned long ptr; |
46a53cca | 3968 | int maybe_acls; |
618e21d5 | 3969 | u32 rdev; |
39279cc3 | 3970 | int ret; |
2f7e33d4 | 3971 | bool filled = false; |
63541927 | 3972 | int first_xattr_slot; |
2f7e33d4 MX |
3973 | |
3974 | ret = btrfs_fill_inode(inode, &rdev); | |
3975 | if (!ret) | |
3976 | filled = true; | |
39279cc3 | 3977 | |
4222ea71 FM |
3978 | if (!path) { |
3979 | path = btrfs_alloc_path(); | |
3980 | if (!path) | |
3981 | return -ENOMEM; | |
3982 | } | |
1748f843 | 3983 | |
39279cc3 | 3984 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3985 | |
39279cc3 | 3986 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3987 | if (ret) { |
4222ea71 FM |
3988 | if (path != in_path) |
3989 | btrfs_free_path(path); | |
f5b3a417 | 3990 | return ret; |
67710892 | 3991 | } |
39279cc3 | 3992 | |
5f39d397 | 3993 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3994 | |
3995 | if (filled) | |
67de1176 | 3996 | goto cache_index; |
2f7e33d4 | 3997 | |
5f39d397 CM |
3998 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3999 | struct btrfs_inode_item); | |
5f39d397 | 4000 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 4001 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
4002 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
4003 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 4004 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
4005 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
4006 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 4007 | |
a937b979 DS |
4008 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
4009 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 4010 | |
a937b979 DS |
4011 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
4012 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 4013 | |
a937b979 DS |
4014 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
4015 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 4016 | |
9cc97d64 | 4017 | BTRFS_I(inode)->i_otime.tv_sec = |
4018 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
4019 | BTRFS_I(inode)->i_otime.tv_nsec = | |
4020 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 4021 | |
a76a3cd4 | 4022 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 4023 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
4024 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
4025 | ||
c7f88c4e JL |
4026 | inode_set_iversion_queried(inode, |
4027 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
4028 | inode->i_generation = BTRFS_I(inode)->generation; |
4029 | inode->i_rdev = 0; | |
4030 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
4031 | ||
4032 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
77eea05e BB |
4033 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
4034 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); | |
6e17d30b YD |
4035 | |
4036 | cache_index: | |
5dc562c5 JB |
4037 | /* |
4038 | * If we were modified in the current generation and evicted from memory | |
4039 | * and then re-read we need to do a full sync since we don't have any | |
4040 | * idea about which extents were modified before we were evicted from | |
4041 | * cache. | |
6e17d30b YD |
4042 | * |
4043 | * This is required for both inode re-read from disk and delayed inode | |
088aea3b | 4044 | * in delayed_nodes_tree. |
5dc562c5 | 4045 | */ |
0b246afa | 4046 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
4047 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
4048 | &BTRFS_I(inode)->runtime_flags); | |
4049 | ||
bde6c242 FM |
4050 | /* |
4051 | * We don't persist the id of the transaction where an unlink operation | |
4052 | * against the inode was last made. So here we assume the inode might | |
4053 | * have been evicted, and therefore the exact value of last_unlink_trans | |
4054 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
4055 | * between the inode and its parent if the inode is fsync'ed and the log | |
4056 | * replayed. For example, in the scenario: | |
4057 | * | |
4058 | * touch mydir/foo | |
4059 | * ln mydir/foo mydir/bar | |
4060 | * sync | |
4061 | * unlink mydir/bar | |
4062 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
4063 | * xfs_io -c fsync mydir/foo | |
4064 | * <power failure> | |
4065 | * mount fs, triggers fsync log replay | |
4066 | * | |
4067 | * We must make sure that when we fsync our inode foo we also log its | |
4068 | * parent inode, otherwise after log replay the parent still has the | |
4069 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
4070 | * and doesn't have an inode ref with the name "bar" anymore. | |
4071 | * | |
4072 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 4073 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
4074 | * transaction commits on fsync if our inode is a directory, or if our |
4075 | * inode is not a directory, logging its parent unnecessarily. | |
4076 | */ | |
4077 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
4078 | ||
3ebac17c FM |
4079 | /* |
4080 | * Same logic as for last_unlink_trans. We don't persist the generation | |
4081 | * of the last transaction where this inode was used for a reflink | |
4082 | * operation, so after eviction and reloading the inode we must be | |
4083 | * pessimistic and assume the last transaction that modified the inode. | |
4084 | */ | |
4085 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
4086 | ||
67de1176 MX |
4087 | path->slots[0]++; |
4088 | if (inode->i_nlink != 1 || | |
4089 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
4090 | goto cache_acl; | |
4091 | ||
4092 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 4093 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
4094 | goto cache_acl; |
4095 | ||
4096 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
4097 | if (location.type == BTRFS_INODE_REF_KEY) { | |
4098 | struct btrfs_inode_ref *ref; | |
4099 | ||
4100 | ref = (struct btrfs_inode_ref *)ptr; | |
4101 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
4102 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
4103 | struct btrfs_inode_extref *extref; | |
4104 | ||
4105 | extref = (struct btrfs_inode_extref *)ptr; | |
4106 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
4107 | extref); | |
4108 | } | |
2f7e33d4 | 4109 | cache_acl: |
46a53cca CM |
4110 | /* |
4111 | * try to precache a NULL acl entry for files that don't have | |
4112 | * any xattrs or acls | |
4113 | */ | |
33345d01 | 4114 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 4115 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
4116 | if (first_xattr_slot != -1) { |
4117 | path->slots[0] = first_xattr_slot; | |
4118 | ret = btrfs_load_inode_props(inode, path); | |
4119 | if (ret) | |
0b246afa | 4120 | btrfs_err(fs_info, |
351fd353 | 4121 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 4122 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
4123 | root->root_key.objectid, ret); |
4124 | } | |
4222ea71 FM |
4125 | if (path != in_path) |
4126 | btrfs_free_path(path); | |
63541927 | 4127 | |
72c04902 AV |
4128 | if (!maybe_acls) |
4129 | cache_no_acl(inode); | |
46a53cca | 4130 | |
39279cc3 | 4131 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
4132 | case S_IFREG: |
4133 | inode->i_mapping->a_ops = &btrfs_aops; | |
4134 | inode->i_fop = &btrfs_file_operations; | |
4135 | inode->i_op = &btrfs_file_inode_operations; | |
4136 | break; | |
4137 | case S_IFDIR: | |
4138 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 4139 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
4140 | break; |
4141 | case S_IFLNK: | |
4142 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 4143 | inode_nohighmem(inode); |
4779cc04 | 4144 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 4145 | break; |
618e21d5 | 4146 | default: |
0279b4cd | 4147 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
4148 | init_special_inode(inode, inode->i_mode, rdev); |
4149 | break; | |
39279cc3 | 4150 | } |
6cbff00f | 4151 | |
7b6a221e | 4152 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 4153 | return 0; |
39279cc3 CM |
4154 | } |
4155 | ||
d352ac68 CM |
4156 | /* |
4157 | * given a leaf and an inode, copy the inode fields into the leaf | |
4158 | */ | |
e02119d5 CM |
4159 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
4160 | struct extent_buffer *leaf, | |
5f39d397 | 4161 | struct btrfs_inode_item *item, |
39279cc3 CM |
4162 | struct inode *inode) |
4163 | { | |
51fab693 | 4164 | struct btrfs_map_token token; |
77eea05e | 4165 | u64 flags; |
51fab693 | 4166 | |
c82f823c | 4167 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 4168 | |
cc4c13d5 DS |
4169 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
4170 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
4171 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
4172 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
4173 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
4174 | ||
4175 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
4176 | inode->i_atime.tv_sec); | |
4177 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
4178 | inode->i_atime.tv_nsec); | |
4179 | ||
4180 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
4181 | inode->i_mtime.tv_sec); | |
4182 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
4183 | inode->i_mtime.tv_nsec); | |
4184 | ||
4185 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
4186 | inode->i_ctime.tv_sec); | |
4187 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
4188 | inode->i_ctime.tv_nsec); | |
4189 | ||
4190 | btrfs_set_token_timespec_sec(&token, &item->otime, | |
4191 | BTRFS_I(inode)->i_otime.tv_sec); | |
4192 | btrfs_set_token_timespec_nsec(&token, &item->otime, | |
4193 | BTRFS_I(inode)->i_otime.tv_nsec); | |
4194 | ||
4195 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
4196 | btrfs_set_token_inode_generation(&token, item, | |
4197 | BTRFS_I(inode)->generation); | |
4198 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
4199 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
4200 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
4201 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
4202 | BTRFS_I(inode)->ro_flags); | |
4203 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 4204 | btrfs_set_token_inode_block_group(&token, item, 0); |
39279cc3 CM |
4205 | } |
4206 | ||
d352ac68 CM |
4207 | /* |
4208 | * copy everything in the in-memory inode into the btree. | |
4209 | */ | |
2115133f | 4210 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
dfeb9e7c NB |
4211 | struct btrfs_root *root, |
4212 | struct btrfs_inode *inode) | |
39279cc3 CM |
4213 | { |
4214 | struct btrfs_inode_item *inode_item; | |
4215 | struct btrfs_path *path; | |
5f39d397 | 4216 | struct extent_buffer *leaf; |
39279cc3 CM |
4217 | int ret; |
4218 | ||
4219 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
4220 | if (!path) |
4221 | return -ENOMEM; | |
4222 | ||
dfeb9e7c | 4223 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
39279cc3 CM |
4224 | if (ret) { |
4225 | if (ret > 0) | |
4226 | ret = -ENOENT; | |
4227 | goto failed; | |
4228 | } | |
4229 | ||
5f39d397 CM |
4230 | leaf = path->nodes[0]; |
4231 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 4232 | struct btrfs_inode_item); |
39279cc3 | 4233 | |
dfeb9e7c | 4234 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
5f39d397 | 4235 | btrfs_mark_buffer_dirty(leaf); |
dfeb9e7c | 4236 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
4237 | ret = 0; |
4238 | failed: | |
39279cc3 CM |
4239 | btrfs_free_path(path); |
4240 | return ret; | |
4241 | } | |
4242 | ||
2115133f CM |
4243 | /* |
4244 | * copy everything in the in-memory inode into the btree. | |
4245 | */ | |
4246 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
9a56fcd1 NB |
4247 | struct btrfs_root *root, |
4248 | struct btrfs_inode *inode) | |
2115133f | 4249 | { |
0b246afa | 4250 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
4251 | int ret; |
4252 | ||
4253 | /* | |
4254 | * If the inode is a free space inode, we can deadlock during commit | |
4255 | * if we put it into the delayed code. | |
4256 | * | |
4257 | * The data relocation inode should also be directly updated | |
4258 | * without delay | |
4259 | */ | |
9a56fcd1 | 4260 | if (!btrfs_is_free_space_inode(inode) |
37f00a6d | 4261 | && !btrfs_is_data_reloc_root(root) |
0b246afa | 4262 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4263 | btrfs_update_root_times(trans, root); |
4264 | ||
9a56fcd1 | 4265 | ret = btrfs_delayed_update_inode(trans, root, inode); |
2115133f | 4266 | if (!ret) |
9a56fcd1 | 4267 | btrfs_set_inode_last_trans(trans, inode); |
2115133f CM |
4268 | return ret; |
4269 | } | |
4270 | ||
9a56fcd1 | 4271 | return btrfs_update_inode_item(trans, root, inode); |
2115133f CM |
4272 | } |
4273 | ||
729f7961 NB |
4274 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4275 | struct btrfs_root *root, struct btrfs_inode *inode) | |
2115133f CM |
4276 | { |
4277 | int ret; | |
4278 | ||
729f7961 | 4279 | ret = btrfs_update_inode(trans, root, inode); |
2115133f | 4280 | if (ret == -ENOSPC) |
729f7961 | 4281 | return btrfs_update_inode_item(trans, root, inode); |
2115133f CM |
4282 | return ret; |
4283 | } | |
4284 | ||
d352ac68 CM |
4285 | /* |
4286 | * unlink helper that gets used here in inode.c and in the tree logging | |
4287 | * recovery code. It remove a link in a directory with a given name, and | |
4288 | * also drops the back refs in the inode to the directory | |
4289 | */ | |
92986796 | 4290 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e NB |
4291 | struct btrfs_inode *dir, |
4292 | struct btrfs_inode *inode, | |
6db75318 | 4293 | const struct fscrypt_str *name, |
88d2beec | 4294 | struct btrfs_rename_ctx *rename_ctx) |
39279cc3 | 4295 | { |
4467af88 | 4296 | struct btrfs_root *root = dir->root; |
0b246afa | 4297 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4298 | struct btrfs_path *path; |
39279cc3 | 4299 | int ret = 0; |
39279cc3 | 4300 | struct btrfs_dir_item *di; |
aec7477b | 4301 | u64 index; |
33345d01 LZ |
4302 | u64 ino = btrfs_ino(inode); |
4303 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4304 | |
4305 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4306 | if (!path) { |
4307 | ret = -ENOMEM; | |
554233a6 | 4308 | goto out; |
54aa1f4d CM |
4309 | } |
4310 | ||
e43eec81 | 4311 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1); |
3cf5068f LB |
4312 | if (IS_ERR_OR_NULL(di)) { |
4313 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
4314 | goto err; |
4315 | } | |
39279cc3 | 4316 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4317 | if (ret) |
4318 | goto err; | |
b3b4aa74 | 4319 | btrfs_release_path(path); |
39279cc3 | 4320 | |
67de1176 MX |
4321 | /* |
4322 | * If we don't have dir index, we have to get it by looking up | |
4323 | * the inode ref, since we get the inode ref, remove it directly, | |
4324 | * it is unnecessary to do delayed deletion. | |
4325 | * | |
4326 | * But if we have dir index, needn't search inode ref to get it. | |
4327 | * Since the inode ref is close to the inode item, it is better | |
4328 | * that we delay to delete it, and just do this deletion when | |
4329 | * we update the inode item. | |
4330 | */ | |
4ec5934e | 4331 | if (inode->dir_index) { |
67de1176 MX |
4332 | ret = btrfs_delayed_delete_inode_ref(inode); |
4333 | if (!ret) { | |
4ec5934e | 4334 | index = inode->dir_index; |
67de1176 MX |
4335 | goto skip_backref; |
4336 | } | |
4337 | } | |
4338 | ||
e43eec81 | 4339 | ret = btrfs_del_inode_ref(trans, root, name, ino, dir_ino, &index); |
aec7477b | 4340 | if (ret) { |
0b246afa | 4341 | btrfs_info(fs_info, |
c2cf52eb | 4342 | "failed to delete reference to %.*s, inode %llu parent %llu", |
e43eec81 | 4343 | name->len, name->name, ino, dir_ino); |
66642832 | 4344 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4345 | goto err; |
4346 | } | |
67de1176 | 4347 | skip_backref: |
88d2beec FM |
4348 | if (rename_ctx) |
4349 | rename_ctx->index = index; | |
4350 | ||
9add2945 | 4351 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4352 | if (ret) { |
66642832 | 4353 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4354 | goto err; |
79787eaa | 4355 | } |
39279cc3 | 4356 | |
259c4b96 FM |
4357 | /* |
4358 | * If we are in a rename context, we don't need to update anything in the | |
4359 | * log. That will be done later during the rename by btrfs_log_new_name(). | |
143823cf | 4360 | * Besides that, doing it here would only cause extra unnecessary btree |
259c4b96 FM |
4361 | * operations on the log tree, increasing latency for applications. |
4362 | */ | |
4363 | if (!rename_ctx) { | |
e43eec81 STD |
4364 | btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino); |
4365 | btrfs_del_dir_entries_in_log(trans, root, name, dir, index); | |
259c4b96 | 4366 | } |
63611e73 JB |
4367 | |
4368 | /* | |
4369 | * If we have a pending delayed iput we could end up with the final iput | |
4370 | * being run in btrfs-cleaner context. If we have enough of these built | |
4371 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
4372 | * way to throttle the unlinks. Since we're currently holding a ref on | |
4373 | * the inode we can run the delayed iput here without any issues as the | |
4374 | * final iput won't be done until after we drop the ref we're currently | |
4375 | * holding. | |
4376 | */ | |
4377 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
4378 | err: |
4379 | btrfs_free_path(path); | |
e02119d5 CM |
4380 | if (ret) |
4381 | goto out; | |
4382 | ||
e43eec81 | 4383 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2); |
4ec5934e NB |
4384 | inode_inc_iversion(&inode->vfs_inode); |
4385 | inode_inc_iversion(&dir->vfs_inode); | |
c1867eb3 DS |
4386 | inode->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
4387 | dir->vfs_inode.i_mtime = inode->vfs_inode.i_ctime; | |
4388 | dir->vfs_inode.i_ctime = inode->vfs_inode.i_ctime; | |
9a56fcd1 | 4389 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4390 | out: |
39279cc3 CM |
4391 | return ret; |
4392 | } | |
4393 | ||
92986796 | 4394 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e | 4395 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
6db75318 | 4396 | const struct fscrypt_str *name) |
92986796 AV |
4397 | { |
4398 | int ret; | |
e43eec81 STD |
4399 | |
4400 | ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL); | |
92986796 | 4401 | if (!ret) { |
4ec5934e | 4402 | drop_nlink(&inode->vfs_inode); |
4467af88 | 4403 | ret = btrfs_update_inode(trans, inode->root, inode); |
92986796 AV |
4404 | } |
4405 | return ret; | |
4406 | } | |
39279cc3 | 4407 | |
a22285a6 YZ |
4408 | /* |
4409 | * helper to start transaction for unlink and rmdir. | |
4410 | * | |
d52be818 JB |
4411 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4412 | * if we cannot make our reservations the normal way try and see if there is | |
4413 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4414 | * allow the unlink to occur. | |
a22285a6 | 4415 | */ |
d52be818 | 4416 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4417 | { |
a22285a6 | 4418 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4419 | |
e70bea5f JB |
4420 | /* |
4421 | * 1 for the possible orphan item | |
4422 | * 1 for the dir item | |
4423 | * 1 for the dir index | |
4424 | * 1 for the inode ref | |
e70bea5f | 4425 | * 1 for the inode |
bca4ad7c | 4426 | * 1 for the parent inode |
e70bea5f | 4427 | */ |
bca4ad7c | 4428 | return btrfs_start_transaction_fallback_global_rsv(root, 6); |
a22285a6 YZ |
4429 | } |
4430 | ||
4431 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4432 | { | |
a22285a6 | 4433 | struct btrfs_trans_handle *trans; |
2b0143b5 | 4434 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4435 | int ret; |
ab3c5c18 | 4436 | struct fscrypt_name fname; |
ab3c5c18 STD |
4437 | |
4438 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); | |
4439 | if (ret) | |
4440 | return ret; | |
ab3c5c18 STD |
4441 | |
4442 | /* This needs to handle no-key deletions later on */ | |
a22285a6 | 4443 | |
d52be818 | 4444 | trans = __unlink_start_trans(dir); |
ab3c5c18 STD |
4445 | if (IS_ERR(trans)) { |
4446 | ret = PTR_ERR(trans); | |
4447 | goto fscrypt_free; | |
4448 | } | |
5f39d397 | 4449 | |
4ec5934e NB |
4450 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4451 | 0); | |
12fcfd22 | 4452 | |
e43eec81 | 4453 | ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4454 | &fname.disk_name); |
b532402e | 4455 | if (ret) |
ab3c5c18 | 4456 | goto end_trans; |
7b128766 | 4457 | |
a22285a6 | 4458 | if (inode->i_nlink == 0) { |
73f2e545 | 4459 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e | 4460 | if (ret) |
ab3c5c18 | 4461 | goto end_trans; |
a22285a6 | 4462 | } |
7b128766 | 4463 | |
ab3c5c18 | 4464 | end_trans: |
3a45bb20 | 4465 | btrfs_end_transaction(trans); |
4467af88 | 4466 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
ab3c5c18 STD |
4467 | fscrypt_free: |
4468 | fscrypt_free_filename(&fname); | |
39279cc3 CM |
4469 | return ret; |
4470 | } | |
4471 | ||
f60a2364 | 4472 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
045d3967 | 4473 | struct inode *dir, struct dentry *dentry) |
4df27c4d | 4474 | { |
401b3b19 | 4475 | struct btrfs_root *root = BTRFS_I(dir)->root; |
045d3967 | 4476 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
4477 | struct btrfs_path *path; |
4478 | struct extent_buffer *leaf; | |
4479 | struct btrfs_dir_item *di; | |
4480 | struct btrfs_key key; | |
4481 | u64 index; | |
4482 | int ret; | |
045d3967 | 4483 | u64 objectid; |
4a0cc7ca | 4484 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
ab3c5c18 STD |
4485 | struct fscrypt_name fname; |
4486 | ||
4487 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); | |
4488 | if (ret) | |
4489 | return ret; | |
ab3c5c18 STD |
4490 | |
4491 | /* This needs to handle no-key deletions later on */ | |
4df27c4d | 4492 | |
045d3967 JB |
4493 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
4494 | objectid = inode->root->root_key.objectid; | |
4495 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
4496 | objectid = inode->location.objectid; | |
4497 | } else { | |
4498 | WARN_ON(1); | |
ab3c5c18 | 4499 | fscrypt_free_filename(&fname); |
045d3967 JB |
4500 | return -EINVAL; |
4501 | } | |
4502 | ||
4df27c4d | 4503 | path = btrfs_alloc_path(); |
ab3c5c18 STD |
4504 | if (!path) { |
4505 | ret = -ENOMEM; | |
4506 | goto out; | |
4507 | } | |
4df27c4d | 4508 | |
6db75318 STD |
4509 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4510 | &fname.disk_name, -1); | |
79787eaa | 4511 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4512 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4513 | goto out; |
4514 | } | |
4df27c4d YZ |
4515 | |
4516 | leaf = path->nodes[0]; | |
4517 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4518 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4519 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4520 | if (ret) { |
66642832 | 4521 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4522 | goto out; |
4523 | } | |
b3b4aa74 | 4524 | btrfs_release_path(path); |
4df27c4d | 4525 | |
d49d3287 JB |
4526 | /* |
4527 | * This is a placeholder inode for a subvolume we didn't have a | |
4528 | * reference to at the time of the snapshot creation. In the meantime | |
4529 | * we could have renamed the real subvol link into our snapshot, so | |
1a9fd417 | 4530 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
d49d3287 JB |
4531 | * Instead simply lookup the dir_index_item for this entry so we can |
4532 | * remove it. Otherwise we know we have a ref to the root and we can | |
4533 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
4534 | */ | |
4535 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
6db75318 | 4536 | di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name); |
79787eaa JM |
4537 | if (IS_ERR_OR_NULL(di)) { |
4538 | if (!di) | |
4539 | ret = -ENOENT; | |
4540 | else | |
4541 | ret = PTR_ERR(di); | |
66642832 | 4542 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4543 | goto out; |
4544 | } | |
4df27c4d YZ |
4545 | |
4546 | leaf = path->nodes[0]; | |
4547 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 4548 | index = key.offset; |
d49d3287 JB |
4549 | btrfs_release_path(path); |
4550 | } else { | |
4551 | ret = btrfs_del_root_ref(trans, objectid, | |
4552 | root->root_key.objectid, dir_ino, | |
6db75318 | 4553 | &index, &fname.disk_name); |
d49d3287 JB |
4554 | if (ret) { |
4555 | btrfs_abort_transaction(trans, ret); | |
4556 | goto out; | |
4557 | } | |
4df27c4d YZ |
4558 | } |
4559 | ||
9add2945 | 4560 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 4561 | if (ret) { |
66642832 | 4562 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4563 | goto out; |
4564 | } | |
4df27c4d | 4565 | |
6db75318 | 4566 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - fname.disk_name.len * 2); |
0c4d2d95 | 4567 | inode_inc_iversion(dir); |
c1867eb3 DS |
4568 | dir->i_mtime = current_time(dir); |
4569 | dir->i_ctime = dir->i_mtime; | |
729f7961 | 4570 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
79787eaa | 4571 | if (ret) |
66642832 | 4572 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4573 | out: |
71d7aed0 | 4574 | btrfs_free_path(path); |
ab3c5c18 | 4575 | fscrypt_free_filename(&fname); |
79787eaa | 4576 | return ret; |
4df27c4d YZ |
4577 | } |
4578 | ||
ec42f167 MT |
4579 | /* |
4580 | * Helper to check if the subvolume references other subvolumes or if it's | |
4581 | * default. | |
4582 | */ | |
f60a2364 | 4583 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4584 | { |
4585 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4586 | struct btrfs_path *path; | |
4587 | struct btrfs_dir_item *di; | |
4588 | struct btrfs_key key; | |
6db75318 | 4589 | struct fscrypt_str name = FSTR_INIT("default", 7); |
ec42f167 MT |
4590 | u64 dir_id; |
4591 | int ret; | |
4592 | ||
4593 | path = btrfs_alloc_path(); | |
4594 | if (!path) | |
4595 | return -ENOMEM; | |
4596 | ||
4597 | /* Make sure this root isn't set as the default subvol */ | |
4598 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4599 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
e43eec81 | 4600 | dir_id, &name, 0); |
ec42f167 MT |
4601 | if (di && !IS_ERR(di)) { |
4602 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4603 | if (key.objectid == root->root_key.objectid) { | |
4604 | ret = -EPERM; | |
4605 | btrfs_err(fs_info, | |
4606 | "deleting default subvolume %llu is not allowed", | |
4607 | key.objectid); | |
4608 | goto out; | |
4609 | } | |
4610 | btrfs_release_path(path); | |
4611 | } | |
4612 | ||
4613 | key.objectid = root->root_key.objectid; | |
4614 | key.type = BTRFS_ROOT_REF_KEY; | |
4615 | key.offset = (u64)-1; | |
4616 | ||
4617 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4618 | if (ret < 0) | |
4619 | goto out; | |
4620 | BUG_ON(ret == 0); | |
4621 | ||
4622 | ret = 0; | |
4623 | if (path->slots[0] > 0) { | |
4624 | path->slots[0]--; | |
4625 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4626 | if (key.objectid == root->root_key.objectid && | |
4627 | key.type == BTRFS_ROOT_REF_KEY) | |
4628 | ret = -ENOTEMPTY; | |
4629 | } | |
4630 | out: | |
4631 | btrfs_free_path(path); | |
4632 | return ret; | |
4633 | } | |
4634 | ||
20a68004 NB |
4635 | /* Delete all dentries for inodes belonging to the root */ |
4636 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4637 | { | |
4638 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4639 | struct rb_node *node; | |
4640 | struct rb_node *prev; | |
4641 | struct btrfs_inode *entry; | |
4642 | struct inode *inode; | |
4643 | u64 objectid = 0; | |
4644 | ||
84961539 | 4645 | if (!BTRFS_FS_ERROR(fs_info)) |
20a68004 NB |
4646 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
4647 | ||
4648 | spin_lock(&root->inode_lock); | |
4649 | again: | |
4650 | node = root->inode_tree.rb_node; | |
4651 | prev = NULL; | |
4652 | while (node) { | |
4653 | prev = node; | |
4654 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4655 | ||
37508515 | 4656 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4657 | node = node->rb_left; |
37508515 | 4658 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4659 | node = node->rb_right; |
4660 | else | |
4661 | break; | |
4662 | } | |
4663 | if (!node) { | |
4664 | while (prev) { | |
4665 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4666 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4667 | node = prev; |
4668 | break; | |
4669 | } | |
4670 | prev = rb_next(prev); | |
4671 | } | |
4672 | } | |
4673 | while (node) { | |
4674 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4675 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4676 | inode = igrab(&entry->vfs_inode); |
4677 | if (inode) { | |
4678 | spin_unlock(&root->inode_lock); | |
4679 | if (atomic_read(&inode->i_count) > 1) | |
4680 | d_prune_aliases(inode); | |
4681 | /* | |
4682 | * btrfs_drop_inode will have it removed from the inode | |
4683 | * cache when its usage count hits zero. | |
4684 | */ | |
4685 | iput(inode); | |
4686 | cond_resched(); | |
4687 | spin_lock(&root->inode_lock); | |
4688 | goto again; | |
4689 | } | |
4690 | ||
4691 | if (cond_resched_lock(&root->inode_lock)) | |
4692 | goto again; | |
4693 | ||
4694 | node = rb_next(node); | |
4695 | } | |
4696 | spin_unlock(&root->inode_lock); | |
4697 | } | |
4698 | ||
f60a2364 MT |
4699 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
4700 | { | |
4701 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
4702 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4703 | struct inode *inode = d_inode(dentry); | |
4704 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4705 | struct btrfs_trans_handle *trans; | |
4706 | struct btrfs_block_rsv block_rsv; | |
4707 | u64 root_flags; | |
f60a2364 | 4708 | int ret; |
f60a2364 MT |
4709 | |
4710 | /* | |
4711 | * Don't allow to delete a subvolume with send in progress. This is | |
4712 | * inside the inode lock so the error handling that has to drop the bit | |
4713 | * again is not run concurrently. | |
4714 | */ | |
4715 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4716 | if (dest->send_in_progress) { |
f60a2364 MT |
4717 | spin_unlock(&dest->root_item_lock); |
4718 | btrfs_warn(fs_info, | |
4719 | "attempt to delete subvolume %llu during send", | |
4720 | dest->root_key.objectid); | |
4721 | return -EPERM; | |
4722 | } | |
60021bd7 KH |
4723 | if (atomic_read(&dest->nr_swapfiles)) { |
4724 | spin_unlock(&dest->root_item_lock); | |
4725 | btrfs_warn(fs_info, | |
4726 | "attempt to delete subvolume %llu with active swapfile", | |
4727 | root->root_key.objectid); | |
4728 | return -EPERM; | |
4729 | } | |
a7176f74 LF |
4730 | root_flags = btrfs_root_flags(&dest->root_item); |
4731 | btrfs_set_root_flags(&dest->root_item, | |
4732 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4733 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4734 | |
4735 | down_write(&fs_info->subvol_sem); | |
4736 | ||
ee0d904f NB |
4737 | ret = may_destroy_subvol(dest); |
4738 | if (ret) | |
f60a2364 MT |
4739 | goto out_up_write; |
4740 | ||
4741 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4742 | /* | |
4743 | * One for dir inode, | |
4744 | * two for dir entries, | |
4745 | * two for root ref/backref. | |
4746 | */ | |
ee0d904f NB |
4747 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
4748 | if (ret) | |
f60a2364 MT |
4749 | goto out_up_write; |
4750 | ||
4751 | trans = btrfs_start_transaction(root, 0); | |
4752 | if (IS_ERR(trans)) { | |
ee0d904f | 4753 | ret = PTR_ERR(trans); |
f60a2364 MT |
4754 | goto out_release; |
4755 | } | |
4756 | trans->block_rsv = &block_rsv; | |
4757 | trans->bytes_reserved = block_rsv.size; | |
4758 | ||
4759 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
4760 | ||
045d3967 | 4761 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 | 4762 | if (ret) { |
f60a2364 MT |
4763 | btrfs_abort_transaction(trans, ret); |
4764 | goto out_end_trans; | |
4765 | } | |
4766 | ||
2731f518 JB |
4767 | ret = btrfs_record_root_in_trans(trans, dest); |
4768 | if (ret) { | |
4769 | btrfs_abort_transaction(trans, ret); | |
4770 | goto out_end_trans; | |
4771 | } | |
f60a2364 MT |
4772 | |
4773 | memset(&dest->root_item.drop_progress, 0, | |
4774 | sizeof(dest->root_item.drop_progress)); | |
c8422684 | 4775 | btrfs_set_root_drop_level(&dest->root_item, 0); |
f60a2364 MT |
4776 | btrfs_set_root_refs(&dest->root_item, 0); |
4777 | ||
4778 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4779 | ret = btrfs_insert_orphan_item(trans, | |
4780 | fs_info->tree_root, | |
4781 | dest->root_key.objectid); | |
4782 | if (ret) { | |
4783 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4784 | goto out_end_trans; |
4785 | } | |
4786 | } | |
4787 | ||
d1957791 | 4788 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4789 | BTRFS_UUID_KEY_SUBVOL, |
4790 | dest->root_key.objectid); | |
4791 | if (ret && ret != -ENOENT) { | |
4792 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4793 | goto out_end_trans; |
4794 | } | |
4795 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4796 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4797 | dest->root_item.received_uuid, |
4798 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4799 | dest->root_key.objectid); | |
4800 | if (ret && ret != -ENOENT) { | |
4801 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4802 | goto out_end_trans; |
4803 | } | |
4804 | } | |
4805 | ||
082b6c97 QW |
4806 | free_anon_bdev(dest->anon_dev); |
4807 | dest->anon_dev = 0; | |
f60a2364 MT |
4808 | out_end_trans: |
4809 | trans->block_rsv = NULL; | |
4810 | trans->bytes_reserved = 0; | |
4811 | ret = btrfs_end_transaction(trans); | |
f60a2364 MT |
4812 | inode->i_flags |= S_DEAD; |
4813 | out_release: | |
e85fde51 | 4814 | btrfs_subvolume_release_metadata(root, &block_rsv); |
f60a2364 MT |
4815 | out_up_write: |
4816 | up_write(&fs_info->subvol_sem); | |
ee0d904f | 4817 | if (ret) { |
f60a2364 MT |
4818 | spin_lock(&dest->root_item_lock); |
4819 | root_flags = btrfs_root_flags(&dest->root_item); | |
4820 | btrfs_set_root_flags(&dest->root_item, | |
4821 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4822 | spin_unlock(&dest->root_item_lock); | |
4823 | } else { | |
4824 | d_invalidate(dentry); | |
20a68004 | 4825 | btrfs_prune_dentries(dest); |
f60a2364 | 4826 | ASSERT(dest->send_in_progress == 0); |
f60a2364 MT |
4827 | } |
4828 | ||
ee0d904f | 4829 | return ret; |
f60a2364 MT |
4830 | } |
4831 | ||
39279cc3 CM |
4832 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4833 | { | |
2b0143b5 | 4834 | struct inode *inode = d_inode(dentry); |
813febdb | 4835 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
1832a6d5 | 4836 | int err = 0; |
39279cc3 | 4837 | struct btrfs_trans_handle *trans; |
44f714da | 4838 | u64 last_unlink_trans; |
ab3c5c18 | 4839 | struct fscrypt_name fname; |
39279cc3 | 4840 | |
b3ae244e | 4841 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4842 | return -ENOTEMPTY; |
813febdb JB |
4843 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) { |
4844 | if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) { | |
4845 | btrfs_err(fs_info, | |
4846 | "extent tree v2 doesn't support snapshot deletion yet"); | |
4847 | return -EOPNOTSUPP; | |
4848 | } | |
a79a464d | 4849 | return btrfs_delete_subvolume(dir, dentry); |
813febdb | 4850 | } |
134d4512 | 4851 | |
ab3c5c18 STD |
4852 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
4853 | if (err) | |
4854 | return err; | |
ab3c5c18 STD |
4855 | |
4856 | /* This needs to handle no-key deletions later on */ | |
4857 | ||
d52be818 | 4858 | trans = __unlink_start_trans(dir); |
ab3c5c18 STD |
4859 | if (IS_ERR(trans)) { |
4860 | err = PTR_ERR(trans); | |
4861 | goto out_notrans; | |
4862 | } | |
5df6a9f6 | 4863 | |
4a0cc7ca | 4864 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 4865 | err = btrfs_unlink_subvol(trans, dir, dentry); |
4df27c4d YZ |
4866 | goto out; |
4867 | } | |
4868 | ||
73f2e545 | 4869 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4870 | if (err) |
4df27c4d | 4871 | goto out; |
7b128766 | 4872 | |
44f714da FM |
4873 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4874 | ||
39279cc3 | 4875 | /* now the directory is empty */ |
e43eec81 | 4876 | err = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4877 | &fname.disk_name); |
44f714da | 4878 | if (!err) { |
6ef06d27 | 4879 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4880 | /* |
4881 | * Propagate the last_unlink_trans value of the deleted dir to | |
4882 | * its parent directory. This is to prevent an unrecoverable | |
4883 | * log tree in the case we do something like this: | |
4884 | * 1) create dir foo | |
4885 | * 2) create snapshot under dir foo | |
4886 | * 3) delete the snapshot | |
4887 | * 4) rmdir foo | |
4888 | * 5) mkdir foo | |
4889 | * 6) fsync foo or some file inside foo | |
4890 | */ | |
4891 | if (last_unlink_trans >= trans->transid) | |
4892 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4893 | } | |
4df27c4d | 4894 | out: |
3a45bb20 | 4895 | btrfs_end_transaction(trans); |
ab3c5c18 | 4896 | out_notrans: |
813febdb | 4897 | btrfs_btree_balance_dirty(fs_info); |
ab3c5c18 | 4898 | fscrypt_free_filename(&fname); |
3954401f | 4899 | |
39279cc3 CM |
4900 | return err; |
4901 | } | |
4902 | ||
39279cc3 | 4903 | /* |
9703fefe | 4904 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4905 | * @inode - inode that we're zeroing |
4906 | * @from - the offset to start zeroing | |
4907 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4908 | * offset | |
4909 | * @front - zero up to the offset instead of from the offset on | |
4910 | * | |
9703fefe | 4911 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4912 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4913 | */ |
217f42eb NB |
4914 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
4915 | int front) | |
39279cc3 | 4916 | { |
217f42eb NB |
4917 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4918 | struct address_space *mapping = inode->vfs_inode.i_mapping; | |
4919 | struct extent_io_tree *io_tree = &inode->io_tree; | |
e6dcd2dc | 4920 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 4921 | struct extent_state *cached_state = NULL; |
364ecf36 | 4922 | struct extent_changeset *data_reserved = NULL; |
6d4572a9 | 4923 | bool only_release_metadata = false; |
0b246afa | 4924 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4925 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4926 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4927 | struct page *page; |
3b16a4e3 | 4928 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4929 | size_t write_bytes = blocksize; |
39279cc3 | 4930 | int ret = 0; |
9703fefe CR |
4931 | u64 block_start; |
4932 | u64 block_end; | |
39279cc3 | 4933 | |
b03ebd99 NB |
4934 | if (IS_ALIGNED(offset, blocksize) && |
4935 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4936 | goto out; |
9703fefe | 4937 | |
8b62f87b JB |
4938 | block_start = round_down(from, blocksize); |
4939 | block_end = block_start + blocksize - 1; | |
4940 | ||
217f42eb | 4941 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
1daedb1d | 4942 | blocksize, false); |
6d4572a9 | 4943 | if (ret < 0) { |
80f9d241 | 4944 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) { |
6d4572a9 QW |
4945 | /* For nocow case, no need to reserve data space */ |
4946 | only_release_metadata = true; | |
4947 | } else { | |
4948 | goto out; | |
4949 | } | |
4950 | } | |
d4135134 | 4951 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false); |
6d4572a9 QW |
4952 | if (ret < 0) { |
4953 | if (!only_release_metadata) | |
217f42eb NB |
4954 | btrfs_free_reserved_data_space(inode, data_reserved, |
4955 | block_start, blocksize); | |
6d4572a9 QW |
4956 | goto out; |
4957 | } | |
211c17f5 | 4958 | again: |
3b16a4e3 | 4959 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4960 | if (!page) { |
217f42eb NB |
4961 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
4962 | blocksize, true); | |
4963 | btrfs_delalloc_release_extents(inode, blocksize); | |
ac6a2b36 | 4964 | ret = -ENOMEM; |
39279cc3 | 4965 | goto out; |
5d5e103a | 4966 | } |
32443de3 QW |
4967 | ret = set_page_extent_mapped(page); |
4968 | if (ret < 0) | |
4969 | goto out_unlock; | |
e6dcd2dc | 4970 | |
39279cc3 | 4971 | if (!PageUptodate(page)) { |
fb12489b | 4972 | ret = btrfs_read_folio(NULL, page_folio(page)); |
39279cc3 | 4973 | lock_page(page); |
211c17f5 CM |
4974 | if (page->mapping != mapping) { |
4975 | unlock_page(page); | |
09cbfeaf | 4976 | put_page(page); |
211c17f5 CM |
4977 | goto again; |
4978 | } | |
39279cc3 CM |
4979 | if (!PageUptodate(page)) { |
4980 | ret = -EIO; | |
89642229 | 4981 | goto out_unlock; |
39279cc3 CM |
4982 | } |
4983 | } | |
211c17f5 | 4984 | wait_on_page_writeback(page); |
e6dcd2dc | 4985 | |
570eb97b | 4986 | lock_extent(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4987 | |
217f42eb | 4988 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4989 | if (ordered) { |
570eb97b | 4990 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4991 | unlock_page(page); |
09cbfeaf | 4992 | put_page(page); |
c0a43603 | 4993 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
4994 | btrfs_put_ordered_extent(ordered); |
4995 | goto again; | |
4996 | } | |
4997 | ||
217f42eb | 4998 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
e182163d | 4999 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
bd015294 | 5000 | &cached_state); |
5d5e103a | 5001 | |
217f42eb | 5002 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 5003 | &cached_state); |
9ed74f2d | 5004 | if (ret) { |
570eb97b | 5005 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
9ed74f2d JB |
5006 | goto out_unlock; |
5007 | } | |
5008 | ||
9703fefe | 5009 | if (offset != blocksize) { |
2aaa6655 | 5010 | if (!len) |
9703fefe | 5011 | len = blocksize - offset; |
2aaa6655 | 5012 | if (front) |
d048b9c2 IW |
5013 | memzero_page(page, (block_start - page_offset(page)), |
5014 | offset); | |
2aaa6655 | 5015 | else |
d048b9c2 IW |
5016 | memzero_page(page, (block_start - page_offset(page)) + offset, |
5017 | len); | |
e6dcd2dc | 5018 | } |
e4f94347 QW |
5019 | btrfs_page_clear_checked(fs_info, page, block_start, |
5020 | block_end + 1 - block_start); | |
6c9ac8be | 5021 | btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start); |
570eb97b | 5022 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 5023 | |
6d4572a9 | 5024 | if (only_release_metadata) |
217f42eb | 5025 | set_extent_bit(&inode->io_tree, block_start, block_end, |
291bbb1e | 5026 | EXTENT_NORESERVE, NULL, GFP_NOFS); |
6d4572a9 | 5027 | |
89642229 | 5028 | out_unlock: |
6d4572a9 QW |
5029 | if (ret) { |
5030 | if (only_release_metadata) | |
217f42eb | 5031 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
6d4572a9 | 5032 | else |
217f42eb | 5033 | btrfs_delalloc_release_space(inode, data_reserved, |
6d4572a9 QW |
5034 | block_start, blocksize, true); |
5035 | } | |
217f42eb | 5036 | btrfs_delalloc_release_extents(inode, blocksize); |
39279cc3 | 5037 | unlock_page(page); |
09cbfeaf | 5038 | put_page(page); |
39279cc3 | 5039 | out: |
6d4572a9 | 5040 | if (only_release_metadata) |
217f42eb | 5041 | btrfs_check_nocow_unlock(inode); |
364ecf36 | 5042 | extent_changeset_free(data_reserved); |
39279cc3 CM |
5043 | return ret; |
5044 | } | |
5045 | ||
a4ba6cc0 | 5046 | static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode, |
16e7549f JB |
5047 | u64 offset, u64 len) |
5048 | { | |
a4ba6cc0 | 5049 | struct btrfs_fs_info *fs_info = root->fs_info; |
16e7549f | 5050 | struct btrfs_trans_handle *trans; |
5893dfb9 | 5051 | struct btrfs_drop_extents_args drop_args = { 0 }; |
16e7549f JB |
5052 | int ret; |
5053 | ||
5054 | /* | |
cceaa89f FM |
5055 | * If NO_HOLES is enabled, we don't need to do anything. |
5056 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() | |
5057 | * or btrfs_update_inode() will be called, which guarantee that the next | |
5058 | * fsync will know this inode was changed and needs to be logged. | |
16e7549f | 5059 | */ |
cceaa89f | 5060 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f | 5061 | return 0; |
16e7549f JB |
5062 | |
5063 | /* | |
5064 | * 1 - for the one we're dropping | |
5065 | * 1 - for the one we're adding | |
5066 | * 1 - for updating the inode. | |
5067 | */ | |
5068 | trans = btrfs_start_transaction(root, 3); | |
5069 | if (IS_ERR(trans)) | |
5070 | return PTR_ERR(trans); | |
5071 | ||
5893dfb9 FM |
5072 | drop_args.start = offset; |
5073 | drop_args.end = offset + len; | |
5074 | drop_args.drop_cache = true; | |
5075 | ||
a4ba6cc0 | 5076 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
16e7549f | 5077 | if (ret) { |
66642832 | 5078 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 5079 | btrfs_end_transaction(trans); |
16e7549f JB |
5080 | return ret; |
5081 | } | |
5082 | ||
d1f68ba0 | 5083 | ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len); |
2766ff61 | 5084 | if (ret) { |
66642832 | 5085 | btrfs_abort_transaction(trans, ret); |
2766ff61 | 5086 | } else { |
a4ba6cc0 NB |
5087 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
5088 | btrfs_update_inode(trans, root, inode); | |
2766ff61 | 5089 | } |
3a45bb20 | 5090 | btrfs_end_transaction(trans); |
16e7549f JB |
5091 | return ret; |
5092 | } | |
5093 | ||
695a0d0d JB |
5094 | /* |
5095 | * This function puts in dummy file extents for the area we're creating a hole | |
5096 | * for. So if we are truncating this file to a larger size we need to insert | |
5097 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
5098 | * the range between oldsize and size | |
5099 | */ | |
b06359a3 | 5100 | int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 5101 | { |
b06359a3 NB |
5102 | struct btrfs_root *root = inode->root; |
5103 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5104 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a22285a6 | 5105 | struct extent_map *em = NULL; |
2ac55d41 | 5106 | struct extent_state *cached_state = NULL; |
0b246afa JM |
5107 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
5108 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
5109 | u64 last_byte; |
5110 | u64 cur_offset; | |
5111 | u64 hole_size; | |
9ed74f2d | 5112 | int err = 0; |
39279cc3 | 5113 | |
a71754fc | 5114 | /* |
9703fefe CR |
5115 | * If our size started in the middle of a block we need to zero out the |
5116 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
5117 | * expose stale data. |
5118 | */ | |
b06359a3 | 5119 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
5120 | if (err) |
5121 | return err; | |
5122 | ||
9036c102 YZ |
5123 | if (size <= hole_start) |
5124 | return 0; | |
5125 | ||
b06359a3 NB |
5126 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
5127 | &cached_state); | |
9036c102 YZ |
5128 | cur_offset = hole_start; |
5129 | while (1) { | |
b06359a3 | 5130 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
39b07b5d | 5131 | block_end - cur_offset); |
79787eaa JM |
5132 | if (IS_ERR(em)) { |
5133 | err = PTR_ERR(em); | |
f2767956 | 5134 | em = NULL; |
79787eaa JM |
5135 | break; |
5136 | } | |
9036c102 | 5137 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 5138 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
5139 | hole_size = last_byte - cur_offset; |
5140 | ||
8082510e | 5141 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 5142 | struct extent_map *hole_em; |
9ed74f2d | 5143 | |
b06359a3 NB |
5144 | err = maybe_insert_hole(root, inode, cur_offset, |
5145 | hole_size); | |
16e7549f | 5146 | if (err) |
3893e33b | 5147 | break; |
9ddc959e | 5148 | |
b06359a3 | 5149 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
5150 | cur_offset, hole_size); |
5151 | if (err) | |
5152 | break; | |
5153 | ||
5dc562c5 JB |
5154 | hole_em = alloc_extent_map(); |
5155 | if (!hole_em) { | |
a1ba4c08 FM |
5156 | btrfs_drop_extent_map_range(inode, cur_offset, |
5157 | cur_offset + hole_size - 1, | |
5158 | false); | |
23e3337f | 5159 | btrfs_set_inode_full_sync(inode); |
5dc562c5 JB |
5160 | goto next; |
5161 | } | |
5162 | hole_em->start = cur_offset; | |
5163 | hole_em->len = hole_size; | |
5164 | hole_em->orig_start = cur_offset; | |
8082510e | 5165 | |
5dc562c5 JB |
5166 | hole_em->block_start = EXTENT_MAP_HOLE; |
5167 | hole_em->block_len = 0; | |
b4939680 | 5168 | hole_em->orig_block_len = 0; |
cc95bef6 | 5169 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 5170 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 5171 | hole_em->generation = fs_info->generation; |
8082510e | 5172 | |
a1ba4c08 | 5173 | err = btrfs_replace_extent_map_range(inode, hole_em, true); |
5dc562c5 | 5174 | free_extent_map(hole_em); |
9ddc959e | 5175 | } else { |
b06359a3 | 5176 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
5177 | cur_offset, hole_size); |
5178 | if (err) | |
5179 | break; | |
9036c102 | 5180 | } |
16e7549f | 5181 | next: |
9036c102 | 5182 | free_extent_map(em); |
a22285a6 | 5183 | em = NULL; |
9036c102 | 5184 | cur_offset = last_byte; |
8082510e | 5185 | if (cur_offset >= block_end) |
9036c102 YZ |
5186 | break; |
5187 | } | |
a22285a6 | 5188 | free_extent_map(em); |
570eb97b | 5189 | unlock_extent(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
5190 | return err; |
5191 | } | |
39279cc3 | 5192 | |
3972f260 | 5193 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5194 | { |
f4a2f4c5 MX |
5195 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5196 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5197 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5198 | loff_t newsize = attr->ia_size; |
5199 | int mask = attr->ia_valid; | |
8082510e YZ |
5200 | int ret; |
5201 | ||
3972f260 ES |
5202 | /* |
5203 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5204 | * special case where we need to update the times despite not having | |
5205 | * these flags set. For all other operations the VFS set these flags | |
5206 | * explicitly if it wants a timestamp update. | |
5207 | */ | |
dff6efc3 CH |
5208 | if (newsize != oldsize) { |
5209 | inode_inc_iversion(inode); | |
c1867eb3 DS |
5210 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) { |
5211 | inode->i_mtime = current_time(inode); | |
5212 | inode->i_ctime = inode->i_mtime; | |
5213 | } | |
dff6efc3 | 5214 | } |
3972f260 | 5215 | |
a41ad394 | 5216 | if (newsize > oldsize) { |
9ea24bbe | 5217 | /* |
ea14b57f | 5218 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5219 | * This is to ensure the snapshot captures a fully consistent |
5220 | * state of this file - if the snapshot captures this expanding | |
5221 | * truncation, it must capture all writes that happened before | |
5222 | * this truncation. | |
5223 | */ | |
dcc3eb96 | 5224 | btrfs_drew_write_lock(&root->snapshot_lock); |
b06359a3 | 5225 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
9ea24bbe | 5226 | if (ret) { |
dcc3eb96 | 5227 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 5228 | return ret; |
9ea24bbe | 5229 | } |
8082510e | 5230 | |
f4a2f4c5 | 5231 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5232 | if (IS_ERR(trans)) { |
dcc3eb96 | 5233 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 5234 | return PTR_ERR(trans); |
9ea24bbe | 5235 | } |
f4a2f4c5 MX |
5236 | |
5237 | i_size_write(inode, newsize); | |
76aea537 | 5238 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
27772b68 | 5239 | pagecache_isize_extended(inode, oldsize, newsize); |
9a56fcd1 | 5240 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
dcc3eb96 | 5241 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 5242 | btrfs_end_transaction(trans); |
a41ad394 | 5243 | } else { |
24c0a722 NA |
5244 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5245 | ||
5246 | if (btrfs_is_zoned(fs_info)) { | |
5247 | ret = btrfs_wait_ordered_range(inode, | |
5248 | ALIGN(newsize, fs_info->sectorsize), | |
5249 | (u64)-1); | |
5250 | if (ret) | |
5251 | return ret; | |
5252 | } | |
8082510e | 5253 | |
a41ad394 JB |
5254 | /* |
5255 | * We're truncating a file that used to have good data down to | |
1fd4033d NB |
5256 | * zero. Make sure any new writes to the file get on disk |
5257 | * on close. | |
a41ad394 JB |
5258 | */ |
5259 | if (newsize == 0) | |
1fd4033d | 5260 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
72ac3c0d | 5261 | &BTRFS_I(inode)->runtime_flags); |
8082510e | 5262 | |
a41ad394 | 5263 | truncate_setsize(inode, newsize); |
2e60a51e | 5264 | |
2e60a51e | 5265 | inode_dio_wait(inode); |
2e60a51e | 5266 | |
213e8c55 | 5267 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
5268 | if (ret && inode->i_nlink) { |
5269 | int err; | |
5270 | ||
5271 | /* | |
f7e9e8fc OS |
5272 | * Truncate failed, so fix up the in-memory size. We |
5273 | * adjusted disk_i_size down as we removed extents, so | |
5274 | * wait for disk_i_size to be stable and then update the | |
5275 | * in-memory size to match. | |
7f4f6e0a | 5276 | */ |
f7e9e8fc | 5277 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5278 | if (err) |
f7e9e8fc OS |
5279 | return err; |
5280 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5281 | } |
8082510e YZ |
5282 | } |
5283 | ||
a41ad394 | 5284 | return ret; |
8082510e YZ |
5285 | } |
5286 | ||
549c7297 CB |
5287 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
5288 | struct iattr *attr) | |
9036c102 | 5289 | { |
2b0143b5 | 5290 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5291 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5292 | int err; |
39279cc3 | 5293 | |
b83cc969 LZ |
5294 | if (btrfs_root_readonly(root)) |
5295 | return -EROFS; | |
5296 | ||
d4d09464 | 5297 | err = setattr_prepare(mnt_userns, dentry, attr); |
9036c102 YZ |
5298 | if (err) |
5299 | return err; | |
2bf5a725 | 5300 | |
5a3f23d5 | 5301 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5302 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5303 | if (err) |
5304 | return err; | |
39279cc3 | 5305 | } |
9036c102 | 5306 | |
1025774c | 5307 | if (attr->ia_valid) { |
d4d09464 | 5308 | setattr_copy(mnt_userns, inode, attr); |
0c4d2d95 | 5309 | inode_inc_iversion(inode); |
22c44fe6 | 5310 | err = btrfs_dirty_inode(inode); |
1025774c | 5311 | |
22c44fe6 | 5312 | if (!err && attr->ia_valid & ATTR_MODE) |
d4d09464 | 5313 | err = posix_acl_chmod(mnt_userns, inode, inode->i_mode); |
1025774c | 5314 | } |
33268eaf | 5315 | |
39279cc3 CM |
5316 | return err; |
5317 | } | |
61295eb8 | 5318 | |
131e404a | 5319 | /* |
895586eb MWO |
5320 | * While truncating the inode pages during eviction, we get the VFS |
5321 | * calling btrfs_invalidate_folio() against each folio of the inode. This | |
5322 | * is slow because the calls to btrfs_invalidate_folio() result in a | |
570eb97b | 5323 | * huge amount of calls to lock_extent() and clear_extent_bit(), |
895586eb MWO |
5324 | * which keep merging and splitting extent_state structures over and over, |
5325 | * wasting lots of time. | |
131e404a | 5326 | * |
895586eb MWO |
5327 | * Therefore if the inode is being evicted, let btrfs_invalidate_folio() |
5328 | * skip all those expensive operations on a per folio basis and do only | |
5329 | * the ordered io finishing, while we release here the extent_map and | |
5330 | * extent_state structures, without the excessive merging and splitting. | |
131e404a FDBM |
5331 | */ |
5332 | static void evict_inode_truncate_pages(struct inode *inode) | |
5333 | { | |
5334 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
131e404a FDBM |
5335 | struct rb_node *node; |
5336 | ||
5337 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5338 | truncate_inode_pages_final(&inode->i_data); |
131e404a | 5339 | |
9c9d1b4f | 5340 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
131e404a | 5341 | |
6ca07097 FM |
5342 | /* |
5343 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
5344 | * We can have ongoing bios started by readahead that have |
5345 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
5346 | * still in progress (unlocked the pages in the bio but did not yet |
5347 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5348 | * ranges can still be locked and eviction started because before |
5349 | * submitting those bios, which are executed by a separate task (work | |
5350 | * queue kthread), inode references (inode->i_count) were not taken | |
5351 | * (which would be dropped in the end io callback of each bio). | |
5352 | * Therefore here we effectively end up waiting for those bios and | |
5353 | * anyone else holding locked ranges without having bumped the inode's | |
5354 | * reference count - if we don't do it, when they access the inode's | |
5355 | * io_tree to unlock a range it may be too late, leading to an | |
5356 | * use-after-free issue. | |
5357 | */ | |
131e404a FDBM |
5358 | spin_lock(&io_tree->lock); |
5359 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5360 | struct extent_state *state; | |
5361 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5362 | u64 start; |
5363 | u64 end; | |
421f0922 | 5364 | unsigned state_flags; |
131e404a FDBM |
5365 | |
5366 | node = rb_first(&io_tree->state); | |
5367 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5368 | start = state->start; |
5369 | end = state->end; | |
421f0922 | 5370 | state_flags = state->state; |
131e404a FDBM |
5371 | spin_unlock(&io_tree->lock); |
5372 | ||
570eb97b | 5373 | lock_extent(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5374 | |
5375 | /* | |
5376 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5377 | * and its reserved space won't be freed by delayed_ref. | |
5378 | * So we need to free its reserved space here. | |
895586eb | 5379 | * (Refer to comment in btrfs_invalidate_folio, case 2) |
b9d0b389 QW |
5380 | * |
5381 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5382 | */ | |
421f0922 | 5383 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f NB |
5384 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
5385 | end - start + 1); | |
b9d0b389 | 5386 | |
6ca07097 | 5387 | clear_extent_bit(io_tree, start, end, |
bd015294 | 5388 | EXTENT_CLEAR_ALL_BITS | EXTENT_DO_ACCOUNTING, |
e182163d | 5389 | &cached_state); |
131e404a | 5390 | |
7064dd5c | 5391 | cond_resched(); |
131e404a FDBM |
5392 | spin_lock(&io_tree->lock); |
5393 | } | |
5394 | spin_unlock(&io_tree->lock); | |
5395 | } | |
5396 | ||
4b9d7b59 | 5397 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5398 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5399 | { |
5400 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d3984c90 | 5401 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5402 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5403 | int ret; |
4b9d7b59 | 5404 | |
d3984c90 JB |
5405 | /* |
5406 | * Eviction should be taking place at some place safe because of our | |
5407 | * delayed iputs. However the normal flushing code will run delayed | |
5408 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5409 | * | |
5410 | * We reserve the delayed_refs_extra here again because we can't use | |
5411 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5412 | * above. We reserve our extra bit here because we generate a ton of | |
5413 | * delayed refs activity by truncating. | |
5414 | * | |
ee6adbfd JB |
5415 | * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can, |
5416 | * if we fail to make this reservation we can re-try without the | |
5417 | * delayed_refs_extra so we can make some forward progress. | |
d3984c90 | 5418 | */ |
9270501c | 5419 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, |
d3984c90 JB |
5420 | BTRFS_RESERVE_FLUSH_EVICT); |
5421 | if (ret) { | |
9270501c | 5422 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, |
ee6adbfd JB |
5423 | BTRFS_RESERVE_FLUSH_EVICT); |
5424 | if (ret) { | |
d3984c90 JB |
5425 | btrfs_warn(fs_info, |
5426 | "could not allocate space for delete; will truncate on mount"); | |
5427 | return ERR_PTR(-ENOSPC); | |
5428 | } | |
5429 | delayed_refs_extra = 0; | |
5430 | } | |
4b9d7b59 | 5431 | |
d3984c90 JB |
5432 | trans = btrfs_join_transaction(root); |
5433 | if (IS_ERR(trans)) | |
5434 | return trans; | |
5435 | ||
5436 | if (delayed_refs_extra) { | |
5437 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5438 | trans->bytes_reserved = delayed_refs_extra; | |
5439 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5440 | delayed_refs_extra, 1); | |
4b9d7b59 | 5441 | } |
d3984c90 | 5442 | return trans; |
4b9d7b59 OS |
5443 | } |
5444 | ||
bd555975 | 5445 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5446 | { |
0b246afa | 5447 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5448 | struct btrfs_trans_handle *trans; |
5449 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5450 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5451 | int ret; |
5452 | ||
1abe9b8a | 5453 | trace_btrfs_inode_evict(inode); |
5454 | ||
3d48d981 | 5455 | if (!root) { |
14605409 | 5456 | fsverity_cleanup_inode(inode); |
e8f1bc14 | 5457 | clear_inode(inode); |
3d48d981 NB |
5458 | return; |
5459 | } | |
5460 | ||
131e404a FDBM |
5461 | evict_inode_truncate_pages(inode); |
5462 | ||
69e9c6c6 SB |
5463 | if (inode->i_nlink && |
5464 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5465 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5466 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5467 | goto no_delete; |
5468 | ||
27919067 | 5469 | if (is_bad_inode(inode)) |
39279cc3 | 5470 | goto no_delete; |
5f39d397 | 5471 | |
7ab7956e | 5472 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5473 | |
7b40b695 | 5474 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5475 | goto no_delete; |
c71bf099 | 5476 | |
76dda93c | 5477 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5478 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5479 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5480 | goto no_delete; |
5481 | } | |
5482 | ||
2adc75d6 JB |
5483 | /* |
5484 | * This makes sure the inode item in tree is uptodate and the space for | |
5485 | * the inode update is released. | |
5486 | */ | |
aa79021f | 5487 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5488 | if (ret) |
0e8c36a9 | 5489 | goto no_delete; |
0e8c36a9 | 5490 | |
2adc75d6 JB |
5491 | /* |
5492 | * This drops any pending insert or delete operations we have for this | |
5493 | * inode. We could have a delayed dir index deletion queued up, but | |
5494 | * we're removing the inode completely so that'll be taken care of in | |
5495 | * the truncate. | |
5496 | */ | |
5497 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); | |
5498 | ||
2ff7e61e | 5499 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5500 | if (!rsv) |
4289a667 | 5501 | goto no_delete; |
2bd36e7b | 5502 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
710d5921 | 5503 | rsv->failfast = true; |
4289a667 | 5504 | |
6ef06d27 | 5505 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5506 | |
8082510e | 5507 | while (1) { |
d9ac19c3 | 5508 | struct btrfs_truncate_control control = { |
71d18b53 | 5509 | .inode = BTRFS_I(inode), |
487e81d2 | 5510 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 JB |
5511 | .new_size = 0, |
5512 | .min_type = 0, | |
5513 | }; | |
5514 | ||
ad80cf50 | 5515 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5516 | if (IS_ERR(trans)) |
5517 | goto free_rsv; | |
7b128766 | 5518 | |
4289a667 JB |
5519 | trans->block_rsv = rsv; |
5520 | ||
71d18b53 | 5521 | ret = btrfs_truncate_inode_items(trans, root, &control); |
27919067 OS |
5522 | trans->block_rsv = &fs_info->trans_block_rsv; |
5523 | btrfs_end_transaction(trans); | |
5524 | btrfs_btree_balance_dirty(fs_info); | |
5525 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5526 | goto free_rsv; | |
5527 | else if (!ret) | |
8082510e | 5528 | break; |
8082510e | 5529 | } |
5f39d397 | 5530 | |
4ef31a45 | 5531 | /* |
27919067 OS |
5532 | * Errors here aren't a big deal, it just means we leave orphan items in |
5533 | * the tree. They will be cleaned up on the next mount. If the inode | |
5534 | * number gets reused, cleanup deletes the orphan item without doing | |
5535 | * anything, and unlink reuses the existing orphan item. | |
5536 | * | |
5537 | * If it turns out that we are dropping too many of these, we might want | |
5538 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5539 | */ |
ad80cf50 | 5540 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5541 | if (!IS_ERR(trans)) { |
5542 | trans->block_rsv = rsv; | |
5543 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5544 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5545 | btrfs_end_transaction(trans); | |
5546 | } | |
54aa1f4d | 5547 | |
27919067 OS |
5548 | free_rsv: |
5549 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5550 | no_delete: |
27919067 OS |
5551 | /* |
5552 | * If we didn't successfully delete, the orphan item will still be in | |
5553 | * the tree and we'll retry on the next mount. Again, we might also want | |
5554 | * to retry these periodically in the future. | |
5555 | */ | |
f48d1cf5 | 5556 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
14605409 | 5557 | fsverity_cleanup_inode(inode); |
dbd5768f | 5558 | clear_inode(inode); |
39279cc3 CM |
5559 | } |
5560 | ||
5561 | /* | |
6bf9e4bd QW |
5562 | * Return the key found in the dir entry in the location pointer, fill @type |
5563 | * with BTRFS_FT_*, and return 0. | |
5564 | * | |
005d6712 SY |
5565 | * If no dir entries were found, returns -ENOENT. |
5566 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5567 | */ |
5568 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5569 | struct btrfs_key *location, u8 *type) |
39279cc3 | 5570 | { |
39279cc3 CM |
5571 | struct btrfs_dir_item *di; |
5572 | struct btrfs_path *path; | |
5573 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5574 | int ret = 0; |
ab3c5c18 | 5575 | struct fscrypt_name fname; |
39279cc3 CM |
5576 | |
5577 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5578 | if (!path) |
5579 | return -ENOMEM; | |
3954401f | 5580 | |
ab3c5c18 STD |
5581 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
5582 | if (ret) | |
5583 | goto out; | |
5584 | ||
ab3c5c18 STD |
5585 | /* This needs to handle no-key deletions later on */ |
5586 | ||
f85b7379 | 5587 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
6db75318 | 5588 | &fname.disk_name, 0); |
3cf5068f LB |
5589 | if (IS_ERR_OR_NULL(di)) { |
5590 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5591 | goto out; |
5592 | } | |
d397712b | 5593 | |
5f39d397 | 5594 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5595 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5596 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5597 | ret = -EUCLEAN; |
56a0e706 LB |
5598 | btrfs_warn(root->fs_info, |
5599 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
6db75318 | 5600 | __func__, fname.disk_name.name, btrfs_ino(BTRFS_I(dir)), |
56a0e706 | 5601 | location->objectid, location->type, location->offset); |
56a0e706 | 5602 | } |
6bf9e4bd | 5603 | if (!ret) |
94a48aef | 5604 | *type = btrfs_dir_ftype(path->nodes[0], di); |
39279cc3 | 5605 | out: |
ab3c5c18 | 5606 | fscrypt_free_filename(&fname); |
39279cc3 CM |
5607 | btrfs_free_path(path); |
5608 | return ret; | |
5609 | } | |
5610 | ||
5611 | /* | |
5612 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5613 | * needs to be changed to reflect the root directory of the tree root. This | |
5614 | * is kind of like crossing a mount point. | |
5615 | */ | |
2ff7e61e | 5616 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5617 | struct inode *dir, |
5618 | struct dentry *dentry, | |
5619 | struct btrfs_key *location, | |
5620 | struct btrfs_root **sub_root) | |
39279cc3 | 5621 | { |
4df27c4d YZ |
5622 | struct btrfs_path *path; |
5623 | struct btrfs_root *new_root; | |
5624 | struct btrfs_root_ref *ref; | |
5625 | struct extent_buffer *leaf; | |
1d4c08e0 | 5626 | struct btrfs_key key; |
4df27c4d YZ |
5627 | int ret; |
5628 | int err = 0; | |
ab3c5c18 | 5629 | struct fscrypt_name fname; |
ab3c5c18 STD |
5630 | |
5631 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname); | |
5632 | if (ret) | |
5633 | return ret; | |
5634 | ||
4df27c4d YZ |
5635 | path = btrfs_alloc_path(); |
5636 | if (!path) { | |
5637 | err = -ENOMEM; | |
5638 | goto out; | |
5639 | } | |
39279cc3 | 5640 | |
4df27c4d | 5641 | err = -ENOENT; |
1d4c08e0 DS |
5642 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5643 | key.type = BTRFS_ROOT_REF_KEY; | |
5644 | key.offset = location->objectid; | |
5645 | ||
0b246afa | 5646 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5647 | if (ret) { |
5648 | if (ret < 0) | |
5649 | err = ret; | |
5650 | goto out; | |
5651 | } | |
39279cc3 | 5652 | |
4df27c4d YZ |
5653 | leaf = path->nodes[0]; |
5654 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5655 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
6db75318 | 5656 | btrfs_root_ref_name_len(leaf, ref) != fname.disk_name.len) |
4df27c4d | 5657 | goto out; |
39279cc3 | 5658 | |
6db75318 STD |
5659 | ret = memcmp_extent_buffer(leaf, fname.disk_name.name, |
5660 | (unsigned long)(ref + 1), fname.disk_name.len); | |
4df27c4d YZ |
5661 | if (ret) |
5662 | goto out; | |
5663 | ||
b3b4aa74 | 5664 | btrfs_release_path(path); |
4df27c4d | 5665 | |
56e9357a | 5666 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5667 | if (IS_ERR(new_root)) { |
5668 | err = PTR_ERR(new_root); | |
5669 | goto out; | |
5670 | } | |
5671 | ||
4df27c4d YZ |
5672 | *sub_root = new_root; |
5673 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5674 | location->type = BTRFS_INODE_ITEM_KEY; | |
5675 | location->offset = 0; | |
5676 | err = 0; | |
5677 | out: | |
5678 | btrfs_free_path(path); | |
ab3c5c18 | 5679 | fscrypt_free_filename(&fname); |
4df27c4d | 5680 | return err; |
39279cc3 CM |
5681 | } |
5682 | ||
5d4f98a2 YZ |
5683 | static void inode_tree_add(struct inode *inode) |
5684 | { | |
5685 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5686 | struct btrfs_inode *entry; | |
03e860bd FNP |
5687 | struct rb_node **p; |
5688 | struct rb_node *parent; | |
cef21937 | 5689 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5690 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5691 | |
1d3382cb | 5692 | if (inode_unhashed(inode)) |
76dda93c | 5693 | return; |
e1409cef | 5694 | parent = NULL; |
5d4f98a2 | 5695 | spin_lock(&root->inode_lock); |
e1409cef | 5696 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5697 | while (*p) { |
5698 | parent = *p; | |
5699 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5700 | ||
37508515 | 5701 | if (ino < btrfs_ino(entry)) |
03e860bd | 5702 | p = &parent->rb_left; |
37508515 | 5703 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5704 | p = &parent->rb_right; |
5d4f98a2 YZ |
5705 | else { |
5706 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5707 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5708 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5709 | RB_CLEAR_NODE(parent); |
5710 | spin_unlock(&root->inode_lock); | |
cef21937 | 5711 | return; |
5d4f98a2 YZ |
5712 | } |
5713 | } | |
cef21937 FDBM |
5714 | rb_link_node(new, parent, p); |
5715 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5716 | spin_unlock(&root->inode_lock); |
5717 | } | |
5718 | ||
b79b7249 | 5719 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5720 | { |
b79b7249 | 5721 | struct btrfs_root *root = inode->root; |
76dda93c | 5722 | int empty = 0; |
5d4f98a2 | 5723 | |
03e860bd | 5724 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5725 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5726 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5727 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5728 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5729 | } |
03e860bd | 5730 | spin_unlock(&root->inode_lock); |
76dda93c | 5731 | |
69e9c6c6 | 5732 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5733 | spin_lock(&root->inode_lock); |
5734 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5735 | spin_unlock(&root->inode_lock); | |
5736 | if (empty) | |
5737 | btrfs_add_dead_root(root); | |
5738 | } | |
5739 | } | |
5740 | ||
5d4f98a2 | 5741 | |
e02119d5 CM |
5742 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5743 | { | |
5744 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5745 | |
5746 | inode->i_ino = args->ino; | |
5747 | BTRFS_I(inode)->location.objectid = args->ino; | |
5748 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5749 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5750 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5751 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
9b9b8854 JB |
5752 | |
5753 | if (args->root && args->root == args->root->fs_info->tree_root && | |
5754 | args->ino != BTRFS_BTREE_INODE_OBJECTID) | |
5755 | set_bit(BTRFS_INODE_FREE_SPACE_INODE, | |
5756 | &BTRFS_I(inode)->runtime_flags); | |
39279cc3 CM |
5757 | return 0; |
5758 | } | |
5759 | ||
5760 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5761 | { | |
5762 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5763 | |
5764 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5765 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5766 | } |
5767 | ||
0202e83f | 5768 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5769 | struct btrfs_root *root) |
39279cc3 CM |
5770 | { |
5771 | struct inode *inode; | |
5772 | struct btrfs_iget_args args; | |
0202e83f | 5773 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5774 | |
0202e83f | 5775 | args.ino = ino; |
39279cc3 CM |
5776 | args.root = root; |
5777 | ||
778ba82b | 5778 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5779 | btrfs_init_locked_inode, |
5780 | (void *)&args); | |
5781 | return inode; | |
5782 | } | |
5783 | ||
4c66e0d4 | 5784 | /* |
0202e83f | 5785 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5786 | * Path can be preallocated to prevent recursing back to iget through |
5787 | * allocator. NULL is also valid but may require an additional allocation | |
5788 | * later. | |
1a54ef8c | 5789 | */ |
0202e83f | 5790 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5791 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5792 | { |
5793 | struct inode *inode; | |
5794 | ||
0202e83f | 5795 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5796 | if (!inode) |
5d4f98a2 | 5797 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5798 | |
5799 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5800 | int ret; |
5801 | ||
4222ea71 | 5802 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5803 | if (!ret) { |
1748f843 MF |
5804 | inode_tree_add(inode); |
5805 | unlock_new_inode(inode); | |
1748f843 | 5806 | } else { |
f5b3a417 AV |
5807 | iget_failed(inode); |
5808 | /* | |
5809 | * ret > 0 can come from btrfs_search_slot called by | |
5810 | * btrfs_read_locked_inode, this means the inode item | |
5811 | * was not found. | |
5812 | */ | |
5813 | if (ret > 0) | |
5814 | ret = -ENOENT; | |
5815 | inode = ERR_PTR(ret); | |
1748f843 MF |
5816 | } |
5817 | } | |
5818 | ||
1a54ef8c BR |
5819 | return inode; |
5820 | } | |
5821 | ||
0202e83f | 5822 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5823 | { |
0202e83f | 5824 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5825 | } |
5826 | ||
4df27c4d YZ |
5827 | static struct inode *new_simple_dir(struct super_block *s, |
5828 | struct btrfs_key *key, | |
5829 | struct btrfs_root *root) | |
5830 | { | |
5831 | struct inode *inode = new_inode(s); | |
5832 | ||
5833 | if (!inode) | |
5834 | return ERR_PTR(-ENOMEM); | |
5835 | ||
5c8fd99f | 5836 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5837 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5838 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5839 | |
5840 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5841 | /* |
5842 | * We only need lookup, the rest is read-only and there's no inode | |
5843 | * associated with the dentry | |
5844 | */ | |
5845 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5846 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5847 | inode->i_fop = &simple_dir_operations; |
5848 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5849 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5850 | inode->i_atime = inode->i_mtime; |
5851 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5852 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5853 | |
5854 | return inode; | |
5855 | } | |
5856 | ||
a55e65b8 DS |
5857 | static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN); |
5858 | static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE); | |
5859 | static_assert(BTRFS_FT_DIR == FT_DIR); | |
5860 | static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV); | |
5861 | static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV); | |
5862 | static_assert(BTRFS_FT_FIFO == FT_FIFO); | |
5863 | static_assert(BTRFS_FT_SOCK == FT_SOCK); | |
5864 | static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK); | |
5865 | ||
6bf9e4bd QW |
5866 | static inline u8 btrfs_inode_type(struct inode *inode) |
5867 | { | |
6bf9e4bd QW |
5868 | return fs_umode_to_ftype(inode->i_mode); |
5869 | } | |
5870 | ||
3de4586c | 5871 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5872 | { |
0b246afa | 5873 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5874 | struct inode *inode; |
4df27c4d | 5875 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5876 | struct btrfs_root *sub_root = root; |
5877 | struct btrfs_key location; | |
6bf9e4bd | 5878 | u8 di_type = 0; |
b4aff1f8 | 5879 | int ret = 0; |
39279cc3 CM |
5880 | |
5881 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5882 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5883 | |
6bf9e4bd | 5884 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5885 | if (ret < 0) |
5886 | return ERR_PTR(ret); | |
5f39d397 | 5887 | |
4df27c4d | 5888 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5889 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5890 | if (IS_ERR(inode)) |
5891 | return inode; | |
5892 | ||
5893 | /* Do extra check against inode mode with di_type */ | |
5894 | if (btrfs_inode_type(inode) != di_type) { | |
5895 | btrfs_crit(fs_info, | |
5896 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5897 | inode->i_mode, btrfs_inode_type(inode), | |
5898 | di_type); | |
5899 | iput(inode); | |
5900 | return ERR_PTR(-EUCLEAN); | |
5901 | } | |
4df27c4d YZ |
5902 | return inode; |
5903 | } | |
5904 | ||
2ff7e61e | 5905 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5906 | &location, &sub_root); |
5907 | if (ret < 0) { | |
5908 | if (ret != -ENOENT) | |
5909 | inode = ERR_PTR(ret); | |
5910 | else | |
fc8b235f | 5911 | inode = new_simple_dir(dir->i_sb, &location, root); |
4df27c4d | 5912 | } else { |
0202e83f | 5913 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
00246528 | 5914 | btrfs_put_root(sub_root); |
76dda93c | 5915 | |
fc8b235f NB |
5916 | if (IS_ERR(inode)) |
5917 | return inode; | |
5918 | ||
0b246afa | 5919 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5920 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5921 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5922 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5923 | if (ret) { |
5924 | iput(inode); | |
66b4ffd1 | 5925 | inode = ERR_PTR(ret); |
01cd3367 | 5926 | } |
c71bf099 YZ |
5927 | } |
5928 | ||
3de4586c CM |
5929 | return inode; |
5930 | } | |
5931 | ||
fe15ce44 | 5932 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5933 | { |
5934 | struct btrfs_root *root; | |
2b0143b5 | 5935 | struct inode *inode = d_inode(dentry); |
76dda93c | 5936 | |
848cce0d | 5937 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5938 | inode = d_inode(dentry->d_parent); |
76dda93c | 5939 | |
848cce0d LZ |
5940 | if (inode) { |
5941 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5942 | if (btrfs_root_refs(&root->root_item) == 0) |
5943 | return 1; | |
848cce0d | 5944 | |
4a0cc7ca | 5945 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5946 | return 1; |
efefb143 | 5947 | } |
76dda93c YZ |
5948 | return 0; |
5949 | } | |
5950 | ||
3de4586c | 5951 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5952 | unsigned int flags) |
3de4586c | 5953 | { |
3837d208 | 5954 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5955 | |
3837d208 AV |
5956 | if (inode == ERR_PTR(-ENOENT)) |
5957 | inode = NULL; | |
41d28bca | 5958 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5959 | } |
5960 | ||
23b5ec74 JB |
5961 | /* |
5962 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5963 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5964 | * our information into that, and then dir_emit from the buffer. This is | |
5965 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5966 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5967 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5968 | * tree lock. | |
5969 | */ | |
5970 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5971 | { | |
5972 | struct btrfs_file_private *private; | |
5973 | ||
5974 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5975 | if (!private) | |
5976 | return -ENOMEM; | |
5977 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5978 | if (!private->filldir_buf) { | |
5979 | kfree(private); | |
5980 | return -ENOMEM; | |
5981 | } | |
5982 | file->private_data = private; | |
5983 | return 0; | |
5984 | } | |
5985 | ||
5986 | struct dir_entry { | |
5987 | u64 ino; | |
5988 | u64 offset; | |
5989 | unsigned type; | |
5990 | int name_len; | |
5991 | }; | |
5992 | ||
5993 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5994 | { | |
5995 | while (entries--) { | |
5996 | struct dir_entry *entry = addr; | |
5997 | char *name = (char *)(entry + 1); | |
5998 | ||
92d32170 DS |
5999 | ctx->pos = get_unaligned(&entry->offset); |
6000 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
6001 | get_unaligned(&entry->ino), | |
6002 | get_unaligned(&entry->type))) | |
23b5ec74 | 6003 | return 1; |
92d32170 DS |
6004 | addr += sizeof(struct dir_entry) + |
6005 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
6006 | ctx->pos++; |
6007 | } | |
6008 | return 0; | |
6009 | } | |
6010 | ||
9cdda8d3 | 6011 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 6012 | { |
9cdda8d3 | 6013 | struct inode *inode = file_inode(file); |
39279cc3 | 6014 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 6015 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
6016 | struct btrfs_dir_item *di; |
6017 | struct btrfs_key key; | |
5f39d397 | 6018 | struct btrfs_key found_key; |
39279cc3 | 6019 | struct btrfs_path *path; |
23b5ec74 | 6020 | void *addr; |
16cdcec7 MX |
6021 | struct list_head ins_list; |
6022 | struct list_head del_list; | |
39279cc3 | 6023 | int ret; |
5f39d397 CM |
6024 | char *name_ptr; |
6025 | int name_len; | |
23b5ec74 JB |
6026 | int entries = 0; |
6027 | int total_len = 0; | |
02dbfc99 | 6028 | bool put = false; |
c2951f32 | 6029 | struct btrfs_key location; |
5f39d397 | 6030 | |
9cdda8d3 AV |
6031 | if (!dir_emit_dots(file, ctx)) |
6032 | return 0; | |
6033 | ||
49593bfa | 6034 | path = btrfs_alloc_path(); |
16cdcec7 MX |
6035 | if (!path) |
6036 | return -ENOMEM; | |
ff5714cc | 6037 | |
23b5ec74 | 6038 | addr = private->filldir_buf; |
e4058b54 | 6039 | path->reada = READA_FORWARD; |
49593bfa | 6040 | |
c2951f32 JM |
6041 | INIT_LIST_HEAD(&ins_list); |
6042 | INIT_LIST_HEAD(&del_list); | |
6043 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 6044 | |
23b5ec74 | 6045 | again: |
c2951f32 | 6046 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 6047 | key.offset = ctx->pos; |
4a0cc7ca | 6048 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 6049 | |
a8ce68fd | 6050 | btrfs_for_each_slot(root, &key, &found_key, path, ret) { |
23b5ec74 | 6051 | struct dir_entry *entry; |
a8ce68fd | 6052 | struct extent_buffer *leaf = path->nodes[0]; |
94a48aef | 6053 | u8 ftype; |
5f39d397 CM |
6054 | |
6055 | if (found_key.objectid != key.objectid) | |
39279cc3 | 6056 | break; |
c2951f32 | 6057 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 6058 | break; |
9cdda8d3 | 6059 | if (found_key.offset < ctx->pos) |
a8ce68fd | 6060 | continue; |
c2951f32 | 6061 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
a8ce68fd GN |
6062 | continue; |
6063 | di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); | |
c2951f32 | 6064 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
6065 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
6066 | PAGE_SIZE) { | |
6067 | btrfs_release_path(path); | |
6068 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6069 | if (ret) | |
6070 | goto nopos; | |
6071 | addr = private->filldir_buf; | |
6072 | entries = 0; | |
6073 | total_len = 0; | |
6074 | goto again; | |
c2951f32 | 6075 | } |
23b5ec74 | 6076 | |
94a48aef | 6077 | ftype = btrfs_dir_flags_to_ftype(btrfs_dir_flags(leaf, di)); |
23b5ec74 | 6078 | entry = addr; |
23b5ec74 | 6079 | name_ptr = (char *)(entry + 1); |
94a48aef OS |
6080 | read_extent_buffer(leaf, name_ptr, |
6081 | (unsigned long)(di + 1), name_len); | |
6082 | put_unaligned(name_len, &entry->name_len); | |
6083 | put_unaligned(fs_ftype_to_dtype(ftype), &entry->type); | |
c2951f32 | 6084 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
6085 | put_unaligned(location.objectid, &entry->ino); |
6086 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
6087 | entries++; |
6088 | addr += sizeof(struct dir_entry) + name_len; | |
6089 | total_len += sizeof(struct dir_entry) + name_len; | |
39279cc3 | 6090 | } |
a8ce68fd GN |
6091 | /* Catch error encountered during iteration */ |
6092 | if (ret < 0) | |
6093 | goto err; | |
6094 | ||
23b5ec74 JB |
6095 | btrfs_release_path(path); |
6096 | ||
6097 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6098 | if (ret) | |
6099 | goto nopos; | |
49593bfa | 6100 | |
d2fbb2b5 | 6101 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 6102 | if (ret) |
bc4ef759 DS |
6103 | goto nopos; |
6104 | ||
db62efbb ZB |
6105 | /* |
6106 | * Stop new entries from being returned after we return the last | |
6107 | * entry. | |
6108 | * | |
6109 | * New directory entries are assigned a strictly increasing | |
6110 | * offset. This means that new entries created during readdir | |
6111 | * are *guaranteed* to be seen in the future by that readdir. | |
6112 | * This has broken buggy programs which operate on names as | |
6113 | * they're returned by readdir. Until we re-use freed offsets | |
6114 | * we have this hack to stop new entries from being returned | |
6115 | * under the assumption that they'll never reach this huge | |
6116 | * offset. | |
6117 | * | |
6118 | * This is being careful not to overflow 32bit loff_t unless the | |
6119 | * last entry requires it because doing so has broken 32bit apps | |
6120 | * in the past. | |
6121 | */ | |
c2951f32 JM |
6122 | if (ctx->pos >= INT_MAX) |
6123 | ctx->pos = LLONG_MAX; | |
6124 | else | |
6125 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6126 | nopos: |
6127 | ret = 0; | |
6128 | err: | |
02dbfc99 OS |
6129 | if (put) |
6130 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6131 | btrfs_free_path(path); |
39279cc3 CM |
6132 | return ret; |
6133 | } | |
6134 | ||
39279cc3 | 6135 | /* |
54aa1f4d | 6136 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6137 | * inode changes. But, it is most likely to find the inode in cache. |
6138 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6139 | * to keep or drop this code. | |
6140 | */ | |
48a3b636 | 6141 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6142 | { |
2ff7e61e | 6143 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6144 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6145 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6146 | int ret; |
6147 | ||
72ac3c0d | 6148 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6149 | return 0; |
39279cc3 | 6150 | |
7a7eaa40 | 6151 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6152 | if (IS_ERR(trans)) |
6153 | return PTR_ERR(trans); | |
8929ecfa | 6154 | |
9a56fcd1 | 6155 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
4d14c5cd | 6156 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
94b60442 | 6157 | /* whoops, lets try again with the full transaction */ |
3a45bb20 | 6158 | btrfs_end_transaction(trans); |
94b60442 | 6159 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6160 | if (IS_ERR(trans)) |
6161 | return PTR_ERR(trans); | |
8929ecfa | 6162 | |
9a56fcd1 | 6163 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
94b60442 | 6164 | } |
3a45bb20 | 6165 | btrfs_end_transaction(trans); |
16cdcec7 | 6166 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6167 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6168 | |
6169 | return ret; | |
6170 | } | |
6171 | ||
6172 | /* | |
6173 | * This is a copy of file_update_time. We need this so we can return error on | |
6174 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6175 | */ | |
95582b00 | 6176 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 6177 | int flags) |
22c44fe6 | 6178 | { |
2bc55652 | 6179 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 6180 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
6181 | |
6182 | if (btrfs_root_readonly(root)) | |
6183 | return -EROFS; | |
6184 | ||
e41f941a | 6185 | if (flags & S_VERSION) |
3a8c7231 | 6186 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
6187 | if (flags & S_CTIME) |
6188 | inode->i_ctime = *now; | |
6189 | if (flags & S_MTIME) | |
6190 | inode->i_mtime = *now; | |
6191 | if (flags & S_ATIME) | |
6192 | inode->i_atime = *now; | |
3a8c7231 | 6193 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
6194 | } |
6195 | ||
d352ac68 CM |
6196 | /* |
6197 | * find the highest existing sequence number in a directory | |
6198 | * and then set the in-memory index_cnt variable to reflect | |
6199 | * free sequence numbers | |
6200 | */ | |
4c570655 | 6201 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6202 | { |
4c570655 | 6203 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6204 | struct btrfs_key key, found_key; |
6205 | struct btrfs_path *path; | |
6206 | struct extent_buffer *leaf; | |
6207 | int ret; | |
6208 | ||
4c570655 | 6209 | key.objectid = btrfs_ino(inode); |
962a298f | 6210 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6211 | key.offset = (u64)-1; |
6212 | ||
6213 | path = btrfs_alloc_path(); | |
6214 | if (!path) | |
6215 | return -ENOMEM; | |
6216 | ||
6217 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6218 | if (ret < 0) | |
6219 | goto out; | |
6220 | /* FIXME: we should be able to handle this */ | |
6221 | if (ret == 0) | |
6222 | goto out; | |
6223 | ret = 0; | |
6224 | ||
aec7477b | 6225 | if (path->slots[0] == 0) { |
528ee697 | 6226 | inode->index_cnt = BTRFS_DIR_START_INDEX; |
aec7477b JB |
6227 | goto out; |
6228 | } | |
6229 | ||
6230 | path->slots[0]--; | |
6231 | ||
6232 | leaf = path->nodes[0]; | |
6233 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6234 | ||
4c570655 | 6235 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6236 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
528ee697 | 6237 | inode->index_cnt = BTRFS_DIR_START_INDEX; |
aec7477b JB |
6238 | goto out; |
6239 | } | |
6240 | ||
4c570655 | 6241 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6242 | out: |
6243 | btrfs_free_path(path); | |
6244 | return ret; | |
6245 | } | |
6246 | ||
d352ac68 CM |
6247 | /* |
6248 | * helper to find a free sequence number in a given directory. This current | |
6249 | * code is very simple, later versions will do smarter things in the btree | |
6250 | */ | |
877574e2 | 6251 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6252 | { |
6253 | int ret = 0; | |
6254 | ||
877574e2 NB |
6255 | if (dir->index_cnt == (u64)-1) { |
6256 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6257 | if (ret) { |
6258 | ret = btrfs_set_inode_index_count(dir); | |
6259 | if (ret) | |
6260 | return ret; | |
6261 | } | |
aec7477b JB |
6262 | } |
6263 | ||
877574e2 NB |
6264 | *index = dir->index_cnt; |
6265 | dir->index_cnt++; | |
aec7477b JB |
6266 | |
6267 | return ret; | |
6268 | } | |
6269 | ||
b0d5d10f CM |
6270 | static int btrfs_insert_inode_locked(struct inode *inode) |
6271 | { | |
6272 | struct btrfs_iget_args args; | |
0202e83f DS |
6273 | |
6274 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
6275 | args.root = BTRFS_I(inode)->root; |
6276 | ||
6277 | return insert_inode_locked4(inode, | |
6278 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6279 | btrfs_find_actor, &args); | |
6280 | } | |
6281 | ||
3538d68d OS |
6282 | int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args, |
6283 | unsigned int *trans_num_items) | |
6284 | { | |
6285 | struct inode *dir = args->dir; | |
6286 | struct inode *inode = args->inode; | |
6287 | int ret; | |
6288 | ||
ab3c5c18 STD |
6289 | if (!args->orphan) { |
6290 | ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0, | |
6291 | &args->fname); | |
6292 | if (ret) | |
6293 | return ret; | |
ab3c5c18 STD |
6294 | } |
6295 | ||
3538d68d | 6296 | ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl); |
ab3c5c18 STD |
6297 | if (ret) { |
6298 | fscrypt_free_filename(&args->fname); | |
3538d68d | 6299 | return ret; |
ab3c5c18 | 6300 | } |
3538d68d OS |
6301 | |
6302 | /* 1 to add inode item */ | |
6303 | *trans_num_items = 1; | |
6304 | /* 1 to add compression property */ | |
6305 | if (BTRFS_I(dir)->prop_compress) | |
6306 | (*trans_num_items)++; | |
6307 | /* 1 to add default ACL xattr */ | |
6308 | if (args->default_acl) | |
6309 | (*trans_num_items)++; | |
6310 | /* 1 to add access ACL xattr */ | |
6311 | if (args->acl) | |
6312 | (*trans_num_items)++; | |
6313 | #ifdef CONFIG_SECURITY | |
6314 | /* 1 to add LSM xattr */ | |
6315 | if (dir->i_security) | |
6316 | (*trans_num_items)++; | |
6317 | #endif | |
6318 | if (args->orphan) { | |
6319 | /* 1 to add orphan item */ | |
6320 | (*trans_num_items)++; | |
6321 | } else { | |
6322 | /* | |
3538d68d OS |
6323 | * 1 to add dir item |
6324 | * 1 to add dir index | |
6325 | * 1 to update parent inode item | |
97bdf1a9 FM |
6326 | * |
6327 | * No need for 1 unit for the inode ref item because it is | |
6328 | * inserted in a batch together with the inode item at | |
6329 | * btrfs_create_new_inode(). | |
3538d68d | 6330 | */ |
97bdf1a9 | 6331 | *trans_num_items += 3; |
3538d68d OS |
6332 | } |
6333 | return 0; | |
6334 | } | |
6335 | ||
6336 | void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args) | |
6337 | { | |
6338 | posix_acl_release(args->acl); | |
6339 | posix_acl_release(args->default_acl); | |
ab3c5c18 | 6340 | fscrypt_free_filename(&args->fname); |
3538d68d OS |
6341 | } |
6342 | ||
19aee8de AJ |
6343 | /* |
6344 | * Inherit flags from the parent inode. | |
6345 | * | |
6346 | * Currently only the compression flags and the cow flags are inherited. | |
6347 | */ | |
6348 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6349 | { | |
6350 | unsigned int flags; | |
6351 | ||
19aee8de AJ |
6352 | flags = BTRFS_I(dir)->flags; |
6353 | ||
6354 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6355 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6356 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6357 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6358 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6359 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6360 | } | |
6361 | ||
6362 | if (flags & BTRFS_INODE_NODATACOW) { | |
6363 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6364 | if (S_ISREG(inode->i_mode)) | |
6365 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6366 | } | |
6367 | ||
7b6a221e | 6368 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
6369 | } |
6370 | ||
3538d68d | 6371 | int btrfs_create_new_inode(struct btrfs_trans_handle *trans, |
caae78e0 | 6372 | struct btrfs_new_inode_args *args) |
39279cc3 | 6373 | { |
caae78e0 | 6374 | struct inode *dir = args->dir; |
3538d68d | 6375 | struct inode *inode = args->inode; |
6db75318 | 6376 | const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name; |
caae78e0 | 6377 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
3538d68d | 6378 | struct btrfs_root *root; |
5f39d397 | 6379 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6380 | struct btrfs_key *location; |
5f39d397 | 6381 | struct btrfs_path *path; |
6437d458 | 6382 | u64 objectid; |
9c58309d CM |
6383 | struct btrfs_inode_ref *ref; |
6384 | struct btrfs_key key[2]; | |
6385 | u32 sizes[2]; | |
b7ef5f3a | 6386 | struct btrfs_item_batch batch; |
9c58309d | 6387 | unsigned long ptr; |
39279cc3 | 6388 | int ret; |
39279cc3 | 6389 | |
5f39d397 | 6390 | path = btrfs_alloc_path(); |
d8926bb3 | 6391 | if (!path) |
a1fd0c35 | 6392 | return -ENOMEM; |
39279cc3 | 6393 | |
3538d68d OS |
6394 | if (!args->subvol) |
6395 | BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root); | |
6396 | root = BTRFS_I(inode)->root; | |
6397 | ||
6437d458 | 6398 | ret = btrfs_get_free_objectid(root, &objectid); |
caae78e0 OS |
6399 | if (ret) |
6400 | goto out; | |
581bb050 LZ |
6401 | inode->i_ino = objectid; |
6402 | ||
caae78e0 OS |
6403 | if (args->orphan) { |
6404 | /* | |
6405 | * O_TMPFILE, set link count to 0, so that after this point, we | |
6406 | * fill in an inode item with the correct link count. | |
6407 | */ | |
6408 | set_nlink(inode, 0); | |
6409 | } else { | |
1abe9b8a | 6410 | trace_btrfs_inode_request(dir); |
6411 | ||
caae78e0 OS |
6412 | ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index); |
6413 | if (ret) | |
6414 | goto out; | |
aec7477b | 6415 | } |
49024388 FM |
6416 | /* index_cnt is ignored for everything but a dir. */ |
6417 | BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX; | |
e02119d5 | 6418 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6419 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6420 | |
caae78e0 OS |
6421 | /* |
6422 | * Subvolumes don't inherit flags from their parent directory. | |
6423 | * Originally this was probably by accident, but we probably can't | |
6424 | * change it now without compatibility issues. | |
6425 | */ | |
6426 | if (!args->subvol) | |
6427 | btrfs_inherit_iflags(inode, dir); | |
305eaac0 | 6428 | |
a1fd0c35 | 6429 | if (S_ISREG(inode->i_mode)) { |
305eaac0 OS |
6430 | if (btrfs_test_opt(fs_info, NODATASUM)) |
6431 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6432 | if (btrfs_test_opt(fs_info, NODATACOW)) | |
6433 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | | |
6434 | BTRFS_INODE_NODATASUM; | |
6435 | } | |
6436 | ||
caae78e0 OS |
6437 | location = &BTRFS_I(inode)->location; |
6438 | location->objectid = objectid; | |
6439 | location->offset = 0; | |
6440 | location->type = BTRFS_INODE_ITEM_KEY; | |
6441 | ||
6442 | ret = btrfs_insert_inode_locked(inode); | |
6443 | if (ret < 0) { | |
6444 | if (!args->orphan) | |
6445 | BTRFS_I(dir)->index_cnt--; | |
6446 | goto out; | |
6447 | } | |
6448 | ||
5dc562c5 JB |
6449 | /* |
6450 | * We could have gotten an inode number from somebody who was fsynced | |
6451 | * and then removed in this same transaction, so let's just set full | |
6452 | * sync since it will be a full sync anyway and this will blow away the | |
6453 | * old info in the log. | |
6454 | */ | |
23e3337f | 6455 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 | 6456 | |
9c58309d | 6457 | key[0].objectid = objectid; |
962a298f | 6458 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6459 | key[0].offset = 0; |
6460 | ||
9c58309d | 6461 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 | 6462 | |
caae78e0 | 6463 | if (!args->orphan) { |
ef3b9af5 FM |
6464 | /* |
6465 | * Start new inodes with an inode_ref. This is slightly more | |
6466 | * efficient for small numbers of hard links since they will | |
6467 | * be packed into one item. Extended refs will kick in if we | |
6468 | * add more hard links than can fit in the ref item. | |
6469 | */ | |
6470 | key[1].objectid = objectid; | |
962a298f | 6471 | key[1].type = BTRFS_INODE_REF_KEY; |
caae78e0 | 6472 | if (args->subvol) { |
23c24ef8 | 6473 | key[1].offset = objectid; |
caae78e0 OS |
6474 | sizes[1] = 2 + sizeof(*ref); |
6475 | } else { | |
6476 | key[1].offset = btrfs_ino(BTRFS_I(dir)); | |
e43eec81 | 6477 | sizes[1] = name->len + sizeof(*ref); |
caae78e0 | 6478 | } |
ef3b9af5 | 6479 | } |
9c58309d | 6480 | |
b7ef5f3a FM |
6481 | batch.keys = &key[0]; |
6482 | batch.data_sizes = &sizes[0]; | |
caae78e0 OS |
6483 | batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]); |
6484 | batch.nr = args->orphan ? 1 : 2; | |
b7ef5f3a | 6485 | ret = btrfs_insert_empty_items(trans, root, path, &batch); |
caae78e0 OS |
6486 | if (ret != 0) { |
6487 | btrfs_abort_transaction(trans, ret); | |
6488 | goto discard; | |
6489 | } | |
5f39d397 | 6490 | |
c2050a45 | 6491 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6492 | inode->i_atime = inode->i_mtime; |
6493 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6494 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6495 | |
caae78e0 OS |
6496 | /* |
6497 | * We're going to fill the inode item now, so at this point the inode | |
6498 | * must be fully initialized. | |
6499 | */ | |
6500 | ||
5f39d397 CM |
6501 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6502 | struct btrfs_inode_item); | |
b159fa28 | 6503 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6504 | sizeof(*inode_item)); |
e02119d5 | 6505 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6506 | |
caae78e0 | 6507 | if (!args->orphan) { |
ef3b9af5 FM |
6508 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
6509 | struct btrfs_inode_ref); | |
ef3b9af5 | 6510 | ptr = (unsigned long)(ref + 1); |
caae78e0 OS |
6511 | if (args->subvol) { |
6512 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2); | |
6513 | btrfs_set_inode_ref_index(path->nodes[0], ref, 0); | |
6514 | write_extent_buffer(path->nodes[0], "..", ptr, 2); | |
6515 | } else { | |
e43eec81 STD |
6516 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, |
6517 | name->len); | |
caae78e0 OS |
6518 | btrfs_set_inode_ref_index(path->nodes[0], ref, |
6519 | BTRFS_I(inode)->dir_index); | |
e43eec81 STD |
6520 | write_extent_buffer(path->nodes[0], name->name, ptr, |
6521 | name->len); | |
caae78e0 | 6522 | } |
ef3b9af5 | 6523 | } |
9c58309d | 6524 | |
5f39d397 | 6525 | btrfs_mark_buffer_dirty(path->nodes[0]); |
814e7718 FM |
6526 | /* |
6527 | * We don't need the path anymore, plus inheriting properties, adding | |
6528 | * ACLs, security xattrs, orphan item or adding the link, will result in | |
6529 | * allocating yet another path. So just free our path. | |
6530 | */ | |
6531 | btrfs_free_path(path); | |
6532 | path = NULL; | |
5f39d397 | 6533 | |
6c3636eb STD |
6534 | if (args->subvol) { |
6535 | struct inode *parent; | |
6536 | ||
6537 | /* | |
6538 | * Subvolumes inherit properties from their parent subvolume, | |
6539 | * not the directory they were created in. | |
6540 | */ | |
6541 | parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID, | |
6542 | BTRFS_I(dir)->root); | |
6543 | if (IS_ERR(parent)) { | |
6544 | ret = PTR_ERR(parent); | |
6545 | } else { | |
6546 | ret = btrfs_inode_inherit_props(trans, inode, parent); | |
6547 | iput(parent); | |
6548 | } | |
6549 | } else { | |
6550 | ret = btrfs_inode_inherit_props(trans, inode, dir); | |
6551 | } | |
6552 | if (ret) { | |
6553 | btrfs_err(fs_info, | |
6554 | "error inheriting props for ino %llu (root %llu): %d", | |
6555 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, | |
6556 | ret); | |
6557 | } | |
6558 | ||
6559 | /* | |
6560 | * Subvolumes don't inherit ACLs or get passed to the LSM. This is | |
6561 | * probably a bug. | |
6562 | */ | |
6563 | if (!args->subvol) { | |
6564 | ret = btrfs_init_inode_security(trans, args); | |
6565 | if (ret) { | |
6566 | btrfs_abort_transaction(trans, ret); | |
6567 | goto discard; | |
6568 | } | |
6569 | } | |
6570 | ||
5d4f98a2 | 6571 | inode_tree_add(inode); |
1abe9b8a | 6572 | |
6573 | trace_btrfs_inode_new(inode); | |
d9094414 | 6574 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6575 | |
8ea05e3a AB |
6576 | btrfs_update_root_times(trans, root); |
6577 | ||
caae78e0 OS |
6578 | if (args->orphan) { |
6579 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
6580 | } else { | |
6581 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, | |
e43eec81 | 6582 | 0, BTRFS_I(inode)->dir_index); |
caae78e0 OS |
6583 | } |
6584 | if (ret) { | |
6585 | btrfs_abort_transaction(trans, ret); | |
6586 | goto discard; | |
6587 | } | |
63541927 | 6588 | |
814e7718 | 6589 | return 0; |
b0d5d10f | 6590 | |
caae78e0 | 6591 | discard: |
a1fd0c35 OS |
6592 | /* |
6593 | * discard_new_inode() calls iput(), but the caller owns the reference | |
6594 | * to the inode. | |
6595 | */ | |
6596 | ihold(inode); | |
32955c54 | 6597 | discard_new_inode(inode); |
caae78e0 | 6598 | out: |
5f39d397 | 6599 | btrfs_free_path(path); |
a1fd0c35 | 6600 | return ret; |
39279cc3 CM |
6601 | } |
6602 | ||
d352ac68 CM |
6603 | /* |
6604 | * utility function to add 'inode' into 'parent_inode' with | |
6605 | * a give name and a given sequence number. | |
6606 | * if 'add_backref' is true, also insert a backref from the | |
6607 | * inode to the parent directory. | |
6608 | */ | |
e02119d5 | 6609 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6610 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
6db75318 | 6611 | const struct fscrypt_str *name, int add_backref, u64 index) |
39279cc3 | 6612 | { |
4df27c4d | 6613 | int ret = 0; |
39279cc3 | 6614 | struct btrfs_key key; |
db0a669f NB |
6615 | struct btrfs_root *root = parent_inode->root; |
6616 | u64 ino = btrfs_ino(inode); | |
6617 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6618 | |
33345d01 | 6619 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6620 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6621 | } else { |
33345d01 | 6622 | key.objectid = ino; |
962a298f | 6623 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6624 | key.offset = 0; |
6625 | } | |
6626 | ||
33345d01 | 6627 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6628 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa | 6629 | root->root_key.objectid, parent_ino, |
e43eec81 | 6630 | index, name); |
4df27c4d | 6631 | } else if (add_backref) { |
e43eec81 STD |
6632 | ret = btrfs_insert_inode_ref(trans, root, name, |
6633 | ino, parent_ino, index); | |
4df27c4d | 6634 | } |
39279cc3 | 6635 | |
79787eaa JM |
6636 | /* Nothing to clean up yet */ |
6637 | if (ret) | |
6638 | return ret; | |
4df27c4d | 6639 | |
e43eec81 | 6640 | ret = btrfs_insert_dir_item(trans, name, parent_inode, &key, |
db0a669f | 6641 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6642 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6643 | goto fail_dir_item; |
6644 | else if (ret) { | |
66642832 | 6645 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6646 | return ret; |
39279cc3 | 6647 | } |
79787eaa | 6648 | |
db0a669f | 6649 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
e43eec81 | 6650 | name->len * 2); |
db0a669f | 6651 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6652 | /* |
6653 | * If we are replaying a log tree, we do not want to update the mtime | |
6654 | * and ctime of the parent directory with the current time, since the | |
6655 | * log replay procedure is responsible for setting them to their correct | |
6656 | * values (the ones it had when the fsync was done). | |
6657 | */ | |
6658 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6659 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6660 | ||
6661 | parent_inode->vfs_inode.i_mtime = now; | |
6662 | parent_inode->vfs_inode.i_ctime = now; | |
6663 | } | |
9a56fcd1 | 6664 | ret = btrfs_update_inode(trans, root, parent_inode); |
79787eaa | 6665 | if (ret) |
66642832 | 6666 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6667 | return ret; |
fe66a05a CM |
6668 | |
6669 | fail_dir_item: | |
6670 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6671 | u64 local_index; | |
6672 | int err; | |
3ee1c553 | 6673 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa | 6674 | root->root_key.objectid, parent_ino, |
e43eec81 | 6675 | &local_index, name); |
1690dd41 JT |
6676 | if (err) |
6677 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6678 | } else if (add_backref) { |
6679 | u64 local_index; | |
6680 | int err; | |
6681 | ||
e43eec81 STD |
6682 | err = btrfs_del_inode_ref(trans, root, name, ino, parent_ino, |
6683 | &local_index); | |
1690dd41 JT |
6684 | if (err) |
6685 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6686 | } |
1690dd41 JT |
6687 | |
6688 | /* Return the original error code */ | |
fe66a05a | 6689 | return ret; |
39279cc3 CM |
6690 | } |
6691 | ||
5f465bf1 OS |
6692 | static int btrfs_create_common(struct inode *dir, struct dentry *dentry, |
6693 | struct inode *inode) | |
618e21d5 | 6694 | { |
2ff7e61e | 6695 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 | 6696 | struct btrfs_root *root = BTRFS_I(dir)->root; |
3538d68d OS |
6697 | struct btrfs_new_inode_args new_inode_args = { |
6698 | .dir = dir, | |
6699 | .dentry = dentry, | |
6700 | .inode = inode, | |
6701 | }; | |
6702 | unsigned int trans_num_items; | |
5f465bf1 | 6703 | struct btrfs_trans_handle *trans; |
618e21d5 | 6704 | int err; |
618e21d5 | 6705 | |
3538d68d | 6706 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); |
caae78e0 OS |
6707 | if (err) |
6708 | goto out_inode; | |
3538d68d OS |
6709 | |
6710 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 6711 | if (IS_ERR(trans)) { |
3538d68d OS |
6712 | err = PTR_ERR(trans); |
6713 | goto out_new_inode_args; | |
a1fd0c35 | 6714 | } |
1832a6d5 | 6715 | |
caae78e0 OS |
6716 | err = btrfs_create_new_inode(trans, &new_inode_args); |
6717 | if (!err) | |
6718 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6719 | |
3a45bb20 | 6720 | btrfs_end_transaction(trans); |
5f465bf1 | 6721 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
6722 | out_new_inode_args: |
6723 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
6724 | out_inode: |
6725 | if (err) | |
6726 | iput(inode); | |
618e21d5 JB |
6727 | return err; |
6728 | } | |
6729 | ||
5f465bf1 OS |
6730 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
6731 | struct dentry *dentry, umode_t mode, dev_t rdev) | |
6732 | { | |
6733 | struct inode *inode; | |
6734 | ||
6735 | inode = new_inode(dir->i_sb); | |
6736 | if (!inode) | |
6737 | return -ENOMEM; | |
6738 | inode_init_owner(mnt_userns, inode, dir, mode); | |
6739 | inode->i_op = &btrfs_special_inode_operations; | |
6740 | init_special_inode(inode, inode->i_mode, rdev); | |
6741 | return btrfs_create_common(dir, dentry, inode); | |
6742 | } | |
6743 | ||
549c7297 CB |
6744 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
6745 | struct dentry *dentry, umode_t mode, bool excl) | |
39279cc3 | 6746 | { |
a1fd0c35 | 6747 | struct inode *inode; |
39279cc3 | 6748 | |
a1fd0c35 OS |
6749 | inode = new_inode(dir->i_sb); |
6750 | if (!inode) | |
6751 | return -ENOMEM; | |
6752 | inode_init_owner(mnt_userns, inode, dir, mode); | |
6753 | inode->i_fop = &btrfs_file_operations; | |
6754 | inode->i_op = &btrfs_file_inode_operations; | |
6755 | inode->i_mapping->a_ops = &btrfs_aops; | |
5f465bf1 | 6756 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6757 | } |
6758 | ||
6759 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6760 | struct dentry *dentry) | |
6761 | { | |
271dba45 | 6762 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6763 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6764 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6765 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
ab3c5c18 | 6766 | struct fscrypt_name fname; |
00e4e6b3 | 6767 | u64 index; |
39279cc3 CM |
6768 | int err; |
6769 | int drop_inode = 0; | |
6770 | ||
4a8be425 | 6771 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6772 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6773 | return -EXDEV; |
4a8be425 | 6774 | |
f186373f | 6775 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6776 | return -EMLINK; |
4a8be425 | 6777 | |
ab3c5c18 STD |
6778 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname); |
6779 | if (err) | |
6780 | goto fail; | |
6781 | ||
877574e2 | 6782 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6783 | if (err) |
6784 | goto fail; | |
6785 | ||
a22285a6 | 6786 | /* |
7e6b6465 | 6787 | * 2 items for inode and inode ref |
a22285a6 | 6788 | * 2 items for dir items |
7e6b6465 | 6789 | * 1 item for parent inode |
399b0bbf | 6790 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6791 | */ |
399b0bbf | 6792 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6793 | if (IS_ERR(trans)) { |
6794 | err = PTR_ERR(trans); | |
271dba45 | 6795 | trans = NULL; |
a22285a6 YZ |
6796 | goto fail; |
6797 | } | |
5f39d397 | 6798 | |
67de1176 MX |
6799 | /* There are several dir indexes for this inode, clear the cache. */ |
6800 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6801 | inc_nlink(inode); |
0c4d2d95 | 6802 | inode_inc_iversion(inode); |
c2050a45 | 6803 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6804 | ihold(inode); |
e9976151 | 6805 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6806 | |
81512e89 | 6807 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6db75318 | 6808 | &fname.disk_name, 1, index); |
5f39d397 | 6809 | |
a5719521 | 6810 | if (err) { |
54aa1f4d | 6811 | drop_inode = 1; |
a5719521 | 6812 | } else { |
10d9f309 | 6813 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6814 | |
9a56fcd1 | 6815 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
79787eaa JM |
6816 | if (err) |
6817 | goto fail; | |
ef3b9af5 FM |
6818 | if (inode->i_nlink == 1) { |
6819 | /* | |
6820 | * If new hard link count is 1, it's a file created | |
6821 | * with open(2) O_TMPFILE flag. | |
6822 | */ | |
3d6ae7bb | 6823 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6824 | if (err) |
6825 | goto fail; | |
6826 | } | |
08c422c2 | 6827 | d_instantiate(dentry, inode); |
88d2beec | 6828 | btrfs_log_new_name(trans, old_dentry, NULL, 0, parent); |
a5719521 | 6829 | } |
39279cc3 | 6830 | |
1832a6d5 | 6831 | fail: |
ab3c5c18 | 6832 | fscrypt_free_filename(&fname); |
271dba45 | 6833 | if (trans) |
3a45bb20 | 6834 | btrfs_end_transaction(trans); |
39279cc3 CM |
6835 | if (drop_inode) { |
6836 | inode_dec_link_count(inode); | |
6837 | iput(inode); | |
6838 | } | |
2ff7e61e | 6839 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6840 | return err; |
6841 | } | |
6842 | ||
549c7297 CB |
6843 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
6844 | struct dentry *dentry, umode_t mode) | |
39279cc3 | 6845 | { |
a1fd0c35 | 6846 | struct inode *inode; |
39279cc3 | 6847 | |
a1fd0c35 OS |
6848 | inode = new_inode(dir->i_sb); |
6849 | if (!inode) | |
6850 | return -ENOMEM; | |
6851 | inode_init_owner(mnt_userns, inode, dir, S_IFDIR | mode); | |
6852 | inode->i_op = &btrfs_dir_inode_operations; | |
6853 | inode->i_fop = &btrfs_dir_file_operations; | |
5f465bf1 | 6854 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6855 | } |
6856 | ||
c8b97818 | 6857 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6858 | struct page *page, |
c8b97818 CM |
6859 | size_t pg_offset, u64 extent_offset, |
6860 | struct btrfs_file_extent_item *item) | |
6861 | { | |
6862 | int ret; | |
6863 | struct extent_buffer *leaf = path->nodes[0]; | |
6864 | char *tmp; | |
6865 | size_t max_size; | |
6866 | unsigned long inline_size; | |
6867 | unsigned long ptr; | |
261507a0 | 6868 | int compress_type; |
c8b97818 CM |
6869 | |
6870 | WARN_ON(pg_offset != 0); | |
261507a0 | 6871 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 | 6872 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
437bd07e | 6873 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); |
c8b97818 | 6874 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6875 | if (!tmp) |
6876 | return -ENOMEM; | |
c8b97818 CM |
6877 | ptr = btrfs_file_extent_inline_start(item); |
6878 | ||
6879 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6880 | ||
09cbfeaf | 6881 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6882 | ret = btrfs_decompress(compress_type, tmp, page, |
6883 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6884 | |
6885 | /* | |
6886 | * decompression code contains a memset to fill in any space between the end | |
6887 | * of the uncompressed data and the end of max_size in case the decompressed | |
6888 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6889 | * the end of an inline extent and the beginning of the next block, so we | |
6890 | * cover that region here. | |
6891 | */ | |
6892 | ||
d048b9c2 IW |
6893 | if (max_size + pg_offset < PAGE_SIZE) |
6894 | memzero_page(page, pg_offset + max_size, | |
6895 | PAGE_SIZE - max_size - pg_offset); | |
c8b97818 | 6896 | kfree(tmp); |
166ae5a4 | 6897 | return ret; |
c8b97818 CM |
6898 | } |
6899 | ||
43dd529a DS |
6900 | /* |
6901 | * Lookup the first extent overlapping a range in a file. | |
6902 | * | |
39b07b5d OS |
6903 | * @inode: file to search in |
6904 | * @page: page to read extent data into if the extent is inline | |
6905 | * @pg_offset: offset into @page to copy to | |
6906 | * @start: file offset | |
6907 | * @len: length of range starting at @start | |
6908 | * | |
43dd529a DS |
6909 | * Return the first &struct extent_map which overlaps the given range, reading |
6910 | * it from the B-tree and caching it if necessary. Note that there may be more | |
6911 | * extents which overlap the given range after the returned extent_map. | |
d352ac68 | 6912 | * |
39b07b5d OS |
6913 | * If @page is not NULL and the extent is inline, this also reads the extent |
6914 | * data directly into the page and marks the extent up to date in the io_tree. | |
6915 | * | |
6916 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6917 | */ |
fc4f21b1 | 6918 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
39b07b5d OS |
6919 | struct page *page, size_t pg_offset, |
6920 | u64 start, u64 len) | |
a52d9a80 | 6921 | { |
3ffbd68c | 6922 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6923 | int ret = 0; |
a52d9a80 CM |
6924 | u64 extent_start = 0; |
6925 | u64 extent_end = 0; | |
fc4f21b1 | 6926 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6927 | int extent_type = -1; |
f421950f | 6928 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6929 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6930 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6931 | struct extent_buffer *leaf; |
6932 | struct btrfs_key found_key; | |
a52d9a80 | 6933 | struct extent_map *em = NULL; |
fc4f21b1 | 6934 | struct extent_map_tree *em_tree = &inode->extent_tree; |
a52d9a80 | 6935 | |
890871be | 6936 | read_lock(&em_tree->lock); |
d1310b2e | 6937 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6938 | read_unlock(&em_tree->lock); |
d1310b2e | 6939 | |
a52d9a80 | 6940 | if (em) { |
e1c4b745 CM |
6941 | if (em->start > start || em->start + em->len <= start) |
6942 | free_extent_map(em); | |
6943 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6944 | free_extent_map(em); |
6945 | else | |
6946 | goto out; | |
a52d9a80 | 6947 | } |
172ddd60 | 6948 | em = alloc_extent_map(); |
a52d9a80 | 6949 | if (!em) { |
1028d1c4 | 6950 | ret = -ENOMEM; |
d1310b2e | 6951 | goto out; |
a52d9a80 | 6952 | } |
d1310b2e | 6953 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6954 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6955 | em->len = (u64)-1; |
c8b97818 | 6956 | em->block_len = (u64)-1; |
f421950f | 6957 | |
bee6ec82 | 6958 | path = btrfs_alloc_path(); |
f421950f | 6959 | if (!path) { |
1028d1c4 | 6960 | ret = -ENOMEM; |
bee6ec82 | 6961 | goto out; |
f421950f CM |
6962 | } |
6963 | ||
bee6ec82 LB |
6964 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6965 | path->reada = READA_FORWARD; | |
4d7240f0 JB |
6966 | |
6967 | /* | |
6968 | * The same explanation in load_free_space_cache applies here as well, | |
6969 | * we only read when we're loading the free space cache, and at that | |
6970 | * point the commit_root has everything we need. | |
6971 | */ | |
6972 | if (btrfs_is_free_space_inode(inode)) { | |
6973 | path->search_commit_root = 1; | |
6974 | path->skip_locking = 1; | |
6975 | } | |
51899412 | 6976 | |
5c9a702e | 6977 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6978 | if (ret < 0) { |
a52d9a80 | 6979 | goto out; |
b8eeab7f | 6980 | } else if (ret > 0) { |
a52d9a80 CM |
6981 | if (path->slots[0] == 0) |
6982 | goto not_found; | |
6983 | path->slots[0]--; | |
1028d1c4 | 6984 | ret = 0; |
a52d9a80 CM |
6985 | } |
6986 | ||
5f39d397 CM |
6987 | leaf = path->nodes[0]; |
6988 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6989 | struct btrfs_file_extent_item); |
5f39d397 | 6990 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6991 | if (found_key.objectid != objectid || |
694c12ed | 6992 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6993 | /* |
6994 | * If we backup past the first extent we want to move forward | |
6995 | * and see if there is an extent in front of us, otherwise we'll | |
6996 | * say there is a hole for our whole search range which can | |
6997 | * cause problems. | |
6998 | */ | |
6999 | extent_end = start; | |
7000 | goto next; | |
a52d9a80 CM |
7001 | } |
7002 | ||
694c12ed | 7003 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 7004 | extent_start = found_key.offset; |
a5eeb3d1 | 7005 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
7006 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
7007 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
7008 | /* Only regular file could have regular/prealloc extent */ |
7009 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 7010 | ret = -EUCLEAN; |
6bf9e4bd QW |
7011 | btrfs_crit(fs_info, |
7012 | "regular/prealloc extent found for non-regular inode %llu", | |
7013 | btrfs_ino(inode)); | |
7014 | goto out; | |
7015 | } | |
09ed2f16 LB |
7016 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
7017 | extent_start); | |
694c12ed | 7018 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
7019 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
7020 | path->slots[0], | |
7021 | extent_start); | |
9036c102 | 7022 | } |
25a50341 | 7023 | next: |
9036c102 YZ |
7024 | if (start >= extent_end) { |
7025 | path->slots[0]++; | |
7026 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
7027 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 7028 | if (ret < 0) |
9036c102 | 7029 | goto out; |
1028d1c4 | 7030 | else if (ret > 0) |
9036c102 | 7031 | goto not_found; |
1028d1c4 | 7032 | |
9036c102 | 7033 | leaf = path->nodes[0]; |
a52d9a80 | 7034 | } |
9036c102 YZ |
7035 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
7036 | if (found_key.objectid != objectid || | |
7037 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
7038 | goto not_found; | |
7039 | if (start + len <= found_key.offset) | |
7040 | goto not_found; | |
e2eca69d WS |
7041 | if (start > found_key.offset) |
7042 | goto next; | |
02a033df NB |
7043 | |
7044 | /* New extent overlaps with existing one */ | |
9036c102 | 7045 | em->start = start; |
70c8a91c | 7046 | em->orig_start = start; |
9036c102 | 7047 | em->len = found_key.offset - start; |
02a033df NB |
7048 | em->block_start = EXTENT_MAP_HOLE; |
7049 | goto insert; | |
9036c102 YZ |
7050 | } |
7051 | ||
39b07b5d | 7052 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
7ffbb598 | 7053 | |
694c12ed NB |
7054 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
7055 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 7056 | goto insert; |
694c12ed | 7057 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 7058 | unsigned long ptr; |
a52d9a80 | 7059 | char *map; |
3326d1b0 CM |
7060 | size_t size; |
7061 | size_t extent_offset; | |
7062 | size_t copy_size; | |
a52d9a80 | 7063 | |
39b07b5d | 7064 | if (!page) |
689f9346 | 7065 | goto out; |
5f39d397 | 7066 | |
e41ca589 | 7067 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 7068 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7069 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7070 | size - extent_offset); | |
3326d1b0 | 7071 | em->start = extent_start + extent_offset; |
0b246afa | 7072 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7073 | em->orig_block_len = em->len; |
70c8a91c | 7074 | em->orig_start = em->start; |
689f9346 | 7075 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 | 7076 | |
bf46f52d | 7077 | if (!PageUptodate(page)) { |
261507a0 LZ |
7078 | if (btrfs_file_extent_compression(leaf, item) != |
7079 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7080 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7081 | extent_offset, item); |
1028d1c4 | 7082 | if (ret) |
166ae5a4 | 7083 | goto out; |
c8b97818 | 7084 | } else { |
58c1a35c | 7085 | map = kmap_local_page(page); |
c8b97818 CM |
7086 | read_extent_buffer(leaf, map + pg_offset, ptr, |
7087 | copy_size); | |
09cbfeaf | 7088 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7089 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7090 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7091 | copy_size); |
7092 | } | |
58c1a35c | 7093 | kunmap_local(map); |
c8b97818 | 7094 | } |
179e29e4 | 7095 | flush_dcache_page(page); |
a52d9a80 | 7096 | } |
a52d9a80 | 7097 | goto insert; |
a52d9a80 CM |
7098 | } |
7099 | not_found: | |
7100 | em->start = start; | |
70c8a91c | 7101 | em->orig_start = start; |
d1310b2e | 7102 | em->len = len; |
5f39d397 | 7103 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 7104 | insert: |
1028d1c4 | 7105 | ret = 0; |
b3b4aa74 | 7106 | btrfs_release_path(path); |
d1310b2e | 7107 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7108 | btrfs_err(fs_info, |
5d163e0e JM |
7109 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7110 | em->start, em->len, start, len); | |
1028d1c4 | 7111 | ret = -EIO; |
a52d9a80 CM |
7112 | goto out; |
7113 | } | |
d1310b2e | 7114 | |
890871be | 7115 | write_lock(&em_tree->lock); |
1028d1c4 | 7116 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 7117 | write_unlock(&em_tree->lock); |
a52d9a80 | 7118 | out: |
c6414280 | 7119 | btrfs_free_path(path); |
1abe9b8a | 7120 | |
fc4f21b1 | 7121 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7122 | |
1028d1c4 | 7123 | if (ret) { |
a52d9a80 | 7124 | free_extent_map(em); |
1028d1c4 | 7125 | return ERR_PTR(ret); |
a52d9a80 CM |
7126 | } |
7127 | return em; | |
7128 | } | |
7129 | ||
64f54188 | 7130 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
5f9a8a51 FM |
7131 | const u64 start, |
7132 | const u64 len, | |
7133 | const u64 orig_start, | |
7134 | const u64 block_start, | |
7135 | const u64 block_len, | |
7136 | const u64 orig_block_len, | |
7137 | const u64 ram_bytes, | |
7138 | const int type) | |
7139 | { | |
7140 | struct extent_map *em = NULL; | |
7141 | int ret; | |
7142 | ||
5f9a8a51 | 7143 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
7144 | em = create_io_em(inode, start, len, orig_start, block_start, |
7145 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
7146 | BTRFS_COMPRESS_NONE, /* compress_type */ |
7147 | type); | |
5f9a8a51 FM |
7148 | if (IS_ERR(em)) |
7149 | goto out; | |
7150 | } | |
cb36a9bb OS |
7151 | ret = btrfs_add_ordered_extent(inode, start, len, len, block_start, |
7152 | block_len, 0, | |
7153 | (1 << type) | | |
7154 | (1 << BTRFS_ORDERED_DIRECT), | |
7155 | BTRFS_COMPRESS_NONE); | |
5f9a8a51 FM |
7156 | if (ret) { |
7157 | if (em) { | |
7158 | free_extent_map(em); | |
4c0c8cfc FM |
7159 | btrfs_drop_extent_map_range(inode, start, |
7160 | start + len - 1, false); | |
5f9a8a51 FM |
7161 | } |
7162 | em = ERR_PTR(ret); | |
7163 | } | |
7164 | out: | |
5f9a8a51 FM |
7165 | |
7166 | return em; | |
7167 | } | |
7168 | ||
9fc6f911 | 7169 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
4b46fce2 JB |
7170 | u64 start, u64 len) |
7171 | { | |
9fc6f911 NB |
7172 | struct btrfs_root *root = inode->root; |
7173 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 7174 | struct extent_map *em; |
4b46fce2 JB |
7175 | struct btrfs_key ins; |
7176 | u64 alloc_hint; | |
7177 | int ret; | |
4b46fce2 | 7178 | |
9fc6f911 | 7179 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7180 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7181 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7182 | if (ret) |
7183 | return ERR_PTR(ret); | |
4b46fce2 | 7184 | |
9fc6f911 | 7185 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
5f9a8a51 | 7186 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 7187 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7188 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7189 | if (IS_ERR(em)) |
9fc6f911 NB |
7190 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
7191 | 1); | |
de0ee0ed | 7192 | |
4b46fce2 JB |
7193 | return em; |
7194 | } | |
7195 | ||
f4639636 | 7196 | static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
05947ae1 AJ |
7197 | { |
7198 | struct btrfs_block_group *block_group; | |
f4639636 | 7199 | bool readonly = false; |
05947ae1 AJ |
7200 | |
7201 | block_group = btrfs_lookup_block_group(fs_info, bytenr); | |
7202 | if (!block_group || block_group->ro) | |
f4639636 | 7203 | readonly = true; |
05947ae1 AJ |
7204 | if (block_group) |
7205 | btrfs_put_block_group(block_group); | |
7206 | return readonly; | |
7207 | } | |
7208 | ||
46bfbb5c | 7209 | /* |
e4ecaf90 QW |
7210 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
7211 | * | |
7212 | * @offset: File offset | |
7213 | * @len: The length to write, will be updated to the nocow writeable | |
7214 | * range | |
7215 | * @orig_start: (optional) Return the original file offset of the file extent | |
7216 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
7217 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
7218 | * @strict: if true, omit optimizations that might force us into unnecessary |
7219 | * cow. e.g., don't trust generation number. | |
e4ecaf90 | 7220 | * |
e4ecaf90 QW |
7221 | * Return: |
7222 | * >0 and update @len if we can do nocow write | |
7223 | * 0 if we can't do nocow write | |
7224 | * <0 if error happened | |
7225 | * | |
7226 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
7227 | * any ordered extents. | |
46bfbb5c | 7228 | */ |
00361589 | 7229 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 7230 | u64 *orig_start, u64 *orig_block_len, |
26ce9114 | 7231 | u64 *ram_bytes, bool nowait, bool strict) |
46bfbb5c | 7232 | { |
2ff7e61e | 7233 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
619104ba | 7234 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
46bfbb5c CM |
7235 | struct btrfs_path *path; |
7236 | int ret; | |
7237 | struct extent_buffer *leaf; | |
7238 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7239 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7240 | struct btrfs_file_extent_item *fi; |
7241 | struct btrfs_key key; | |
46bfbb5c | 7242 | int found_type; |
e77751aa | 7243 | |
46bfbb5c CM |
7244 | path = btrfs_alloc_path(); |
7245 | if (!path) | |
7246 | return -ENOMEM; | |
26ce9114 | 7247 | path->nowait = nowait; |
46bfbb5c | 7248 | |
f85b7379 DS |
7249 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7250 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7251 | if (ret < 0) |
7252 | goto out; | |
7253 | ||
46bfbb5c | 7254 | if (ret == 1) { |
619104ba | 7255 | if (path->slots[0] == 0) { |
46bfbb5c CM |
7256 | /* can't find the item, must cow */ |
7257 | ret = 0; | |
7258 | goto out; | |
7259 | } | |
619104ba | 7260 | path->slots[0]--; |
46bfbb5c CM |
7261 | } |
7262 | ret = 0; | |
7263 | leaf = path->nodes[0]; | |
619104ba | 7264 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
4a0cc7ca | 7265 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7266 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7267 | /* not our file or wrong item type, must cow */ | |
7268 | goto out; | |
7269 | } | |
7270 | ||
7271 | if (key.offset > offset) { | |
7272 | /* Wrong offset, must cow */ | |
7273 | goto out; | |
7274 | } | |
7275 | ||
619104ba | 7276 | if (btrfs_file_extent_end(path) <= offset) |
7ee9e440 JB |
7277 | goto out; |
7278 | ||
619104ba FM |
7279 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); |
7280 | found_type = btrfs_file_extent_type(leaf, fi); | |
7281 | if (ram_bytes) | |
7282 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
e77751aa | 7283 | |
619104ba FM |
7284 | nocow_args.start = offset; |
7285 | nocow_args.end = offset + *len - 1; | |
7286 | nocow_args.strict = strict; | |
7287 | nocow_args.free_path = true; | |
7ee9e440 | 7288 | |
619104ba FM |
7289 | ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args); |
7290 | /* can_nocow_file_extent() has freed the path. */ | |
7291 | path = NULL; | |
7ee9e440 | 7292 | |
619104ba FM |
7293 | if (ret != 1) { |
7294 | /* Treat errors as not being able to NOCOW. */ | |
7295 | ret = 0; | |
78d4295b | 7296 | goto out; |
7ee9e440 | 7297 | } |
eb384b55 | 7298 | |
619104ba FM |
7299 | ret = 0; |
7300 | if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr)) | |
46bfbb5c | 7301 | goto out; |
7b2b7085 | 7302 | |
619104ba FM |
7303 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
7304 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7b2b7085 MX |
7305 | u64 range_end; |
7306 | ||
619104ba | 7307 | range_end = round_up(offset + nocow_args.num_bytes, |
da17066c | 7308 | root->fs_info->sectorsize) - 1; |
7b2b7085 MX |
7309 | ret = test_range_bit(io_tree, offset, range_end, |
7310 | EXTENT_DELALLOC, 0, NULL); | |
7311 | if (ret) { | |
7312 | ret = -EAGAIN; | |
7313 | goto out; | |
7314 | } | |
7315 | } | |
7316 | ||
619104ba FM |
7317 | if (orig_start) |
7318 | *orig_start = key.offset - nocow_args.extent_offset; | |
7319 | if (orig_block_len) | |
7320 | *orig_block_len = nocow_args.disk_num_bytes; | |
00361589 | 7321 | |
619104ba | 7322 | *len = nocow_args.num_bytes; |
46bfbb5c CM |
7323 | ret = 1; |
7324 | out: | |
7325 | btrfs_free_path(path); | |
7326 | return ret; | |
7327 | } | |
7328 | ||
eb838e73 | 7329 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
59094403 FM |
7330 | struct extent_state **cached_state, |
7331 | unsigned int iomap_flags) | |
eb838e73 | 7332 | { |
59094403 FM |
7333 | const bool writing = (iomap_flags & IOMAP_WRITE); |
7334 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); | |
7335 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
eb838e73 JB |
7336 | struct btrfs_ordered_extent *ordered; |
7337 | int ret = 0; | |
7338 | ||
7339 | while (1) { | |
59094403 | 7340 | if (nowait) { |
83ae4133 JB |
7341 | if (!try_lock_extent(io_tree, lockstart, lockend, |
7342 | cached_state)) | |
59094403 FM |
7343 | return -EAGAIN; |
7344 | } else { | |
570eb97b | 7345 | lock_extent(io_tree, lockstart, lockend, cached_state); |
59094403 | 7346 | } |
eb838e73 JB |
7347 | /* |
7348 | * We're concerned with the entire range that we're going to be | |
01327610 | 7349 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7350 | * extents in this range. |
7351 | */ | |
a776c6fa | 7352 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7353 | lockend - lockstart + 1); |
7354 | ||
7355 | /* | |
7356 | * We need to make sure there are no buffered pages in this | |
7357 | * range either, we could have raced between the invalidate in | |
7358 | * generic_file_direct_write and locking the extent. The | |
7359 | * invalidate needs to happen so that reads after a write do not | |
7360 | * get stale data. | |
7361 | */ | |
fc4adbff | 7362 | if (!ordered && |
051c98eb DS |
7363 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7364 | lockstart, lockend))) | |
eb838e73 JB |
7365 | break; |
7366 | ||
570eb97b | 7367 | unlock_extent(io_tree, lockstart, lockend, cached_state); |
eb838e73 JB |
7368 | |
7369 | if (ordered) { | |
59094403 FM |
7370 | if (nowait) { |
7371 | btrfs_put_ordered_extent(ordered); | |
7372 | ret = -EAGAIN; | |
7373 | break; | |
7374 | } | |
ade77029 FM |
7375 | /* |
7376 | * If we are doing a DIO read and the ordered extent we | |
7377 | * found is for a buffered write, we can not wait for it | |
7378 | * to complete and retry, because if we do so we can | |
7379 | * deadlock with concurrent buffered writes on page | |
7380 | * locks. This happens only if our DIO read covers more | |
7381 | * than one extent map, if at this point has already | |
7382 | * created an ordered extent for a previous extent map | |
7383 | * and locked its range in the inode's io tree, and a | |
7384 | * concurrent write against that previous extent map's | |
7385 | * range and this range started (we unlock the ranges | |
7386 | * in the io tree only when the bios complete and | |
7387 | * buffered writes always lock pages before attempting | |
7388 | * to lock range in the io tree). | |
7389 | */ | |
7390 | if (writing || | |
7391 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
c0a43603 | 7392 | btrfs_start_ordered_extent(ordered, 1); |
ade77029 | 7393 | else |
59094403 | 7394 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7395 | btrfs_put_ordered_extent(ordered); |
7396 | } else { | |
eb838e73 | 7397 | /* |
b850ae14 FM |
7398 | * We could trigger writeback for this range (and wait |
7399 | * for it to complete) and then invalidate the pages for | |
7400 | * this range (through invalidate_inode_pages2_range()), | |
7401 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7402 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7403 | * triggered a readahead) on a page lock due to an |
7404 | * ordered dio extent we created before but did not have | |
7405 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7406 | * complete), which makes readahead wait for that |
b850ae14 FM |
7407 | * ordered extent to complete while holding a lock on |
7408 | * that page. | |
eb838e73 | 7409 | */ |
59094403 | 7410 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7411 | } |
7412 | ||
ade77029 FM |
7413 | if (ret) |
7414 | break; | |
7415 | ||
eb838e73 JB |
7416 | cond_resched(); |
7417 | } | |
7418 | ||
7419 | return ret; | |
7420 | } | |
7421 | ||
6f9994db | 7422 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7423 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7424 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7425 | u64 block_len, u64 orig_block_len, |
7426 | u64 ram_bytes, int compress_type, | |
7427 | int type) | |
69ffb543 | 7428 | { |
69ffb543 | 7429 | struct extent_map *em; |
69ffb543 JB |
7430 | int ret; |
7431 | ||
6f9994db LB |
7432 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7433 | type == BTRFS_ORDERED_COMPRESSED || | |
7434 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7435 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7436 | |
69ffb543 JB |
7437 | em = alloc_extent_map(); |
7438 | if (!em) | |
7439 | return ERR_PTR(-ENOMEM); | |
7440 | ||
7441 | em->start = start; | |
7442 | em->orig_start = orig_start; | |
7443 | em->len = len; | |
7444 | em->block_len = block_len; | |
7445 | em->block_start = block_start; | |
b4939680 | 7446 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7447 | em->ram_bytes = ram_bytes; |
70c8a91c | 7448 | em->generation = -1; |
69ffb543 | 7449 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7450 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7451 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7452 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7453 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7454 | em->compress_type = compress_type; | |
7455 | } | |
69ffb543 | 7456 | |
a1ba4c08 | 7457 | ret = btrfs_replace_extent_map_range(inode, em, true); |
69ffb543 JB |
7458 | if (ret) { |
7459 | free_extent_map(em); | |
7460 | return ERR_PTR(ret); | |
7461 | } | |
7462 | ||
6f9994db | 7463 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7464 | return em; |
7465 | } | |
7466 | ||
1c8d0175 | 7467 | |
c5794e51 | 7468 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7469 | struct inode *inode, |
7470 | struct btrfs_dio_data *dio_data, | |
d7a8ab4e FM |
7471 | u64 start, u64 len, |
7472 | unsigned int iomap_flags) | |
c5794e51 | 7473 | { |
d4135134 | 7474 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); |
c5794e51 NB |
7475 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7476 | struct extent_map *em = *map; | |
f0bfa76a FM |
7477 | int type; |
7478 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
2306e83e | 7479 | struct btrfs_block_group *bg; |
f0bfa76a FM |
7480 | bool can_nocow = false; |
7481 | bool space_reserved = false; | |
6d82ad13 | 7482 | u64 prev_len; |
c5794e51 NB |
7483 | int ret = 0; |
7484 | ||
7485 | /* | |
7486 | * We don't allocate a new extent in the following cases | |
7487 | * | |
7488 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7489 | * existing extent. | |
7490 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7491 | * just use the extent. | |
7492 | * | |
7493 | */ | |
7494 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7495 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7496 | em->block_start != EXTENT_MAP_HOLE)) { | |
c5794e51 NB |
7497 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
7498 | type = BTRFS_ORDERED_PREALLOC; | |
7499 | else | |
7500 | type = BTRFS_ORDERED_NOCOW; | |
7501 | len = min(len, em->len - (start - em->start)); | |
7502 | block_start = em->block_start + (start - em->start); | |
7503 | ||
7504 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
26ce9114 | 7505 | &orig_block_len, &ram_bytes, false, false) == 1) { |
2306e83e FM |
7506 | bg = btrfs_inc_nocow_writers(fs_info, block_start); |
7507 | if (bg) | |
7508 | can_nocow = true; | |
7509 | } | |
f0bfa76a | 7510 | } |
c5794e51 | 7511 | |
6d82ad13 | 7512 | prev_len = len; |
f0bfa76a FM |
7513 | if (can_nocow) { |
7514 | struct extent_map *em2; | |
7515 | ||
7516 | /* We can NOCOW, so only need to reserve metadata space. */ | |
d4135134 FM |
7517 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, |
7518 | nowait); | |
f0bfa76a FM |
7519 | if (ret < 0) { |
7520 | /* Our caller expects us to free the input extent map. */ | |
7521 | free_extent_map(em); | |
7522 | *map = NULL; | |
2306e83e | 7523 | btrfs_dec_nocow_writers(bg); |
d4135134 FM |
7524 | if (nowait && (ret == -ENOSPC || ret == -EDQUOT)) |
7525 | ret = -EAGAIN; | |
f0bfa76a FM |
7526 | goto out; |
7527 | } | |
7528 | space_reserved = true; | |
7529 | ||
7530 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, | |
7531 | orig_start, block_start, | |
7532 | len, orig_block_len, | |
7533 | ram_bytes, type); | |
2306e83e | 7534 | btrfs_dec_nocow_writers(bg); |
f0bfa76a FM |
7535 | if (type == BTRFS_ORDERED_PREALLOC) { |
7536 | free_extent_map(em); | |
c1867eb3 DS |
7537 | *map = em2; |
7538 | em = em2; | |
f0bfa76a | 7539 | } |
c5794e51 | 7540 | |
f0bfa76a FM |
7541 | if (IS_ERR(em2)) { |
7542 | ret = PTR_ERR(em2); | |
7543 | goto out; | |
c5794e51 | 7544 | } |
f5585f4f FM |
7545 | |
7546 | dio_data->nocow_done = true; | |
f0bfa76a | 7547 | } else { |
f0bfa76a FM |
7548 | /* Our caller expects us to free the input extent map. */ |
7549 | free_extent_map(em); | |
7550 | *map = NULL; | |
7551 | ||
d4135134 | 7552 | if (nowait) |
d7a8ab4e FM |
7553 | return -EAGAIN; |
7554 | ||
f5585f4f FM |
7555 | /* |
7556 | * If we could not allocate data space before locking the file | |
7557 | * range and we can't do a NOCOW write, then we have to fail. | |
7558 | */ | |
7559 | if (!dio_data->data_space_reserved) | |
7560 | return -ENOSPC; | |
7561 | ||
7562 | /* | |
7563 | * We have to COW and we have already reserved data space before, | |
7564 | * so now we reserve only metadata. | |
7565 | */ | |
7566 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, | |
7567 | false); | |
f0bfa76a FM |
7568 | if (ret < 0) |
7569 | goto out; | |
7570 | space_reserved = true; | |
7571 | ||
7572 | em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); | |
7573 | if (IS_ERR(em)) { | |
7574 | ret = PTR_ERR(em); | |
7575 | goto out; | |
7576 | } | |
7577 | *map = em; | |
7578 | len = min(len, em->len - (start - em->start)); | |
7579 | if (len < prev_len) | |
f5585f4f FM |
7580 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
7581 | prev_len - len, true); | |
c5794e51 NB |
7582 | } |
7583 | ||
f0bfa76a FM |
7584 | /* |
7585 | * We have created our ordered extent, so we can now release our reservation | |
7586 | * for an outstanding extent. | |
7587 | */ | |
6d82ad13 | 7588 | btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len); |
c5794e51 | 7589 | |
c5794e51 NB |
7590 | /* |
7591 | * Need to update the i_size under the extent lock so buffered | |
7592 | * readers will get the updated i_size when we unlock. | |
7593 | */ | |
f85781fb | 7594 | if (start + len > i_size_read(inode)) |
c5794e51 | 7595 | i_size_write(inode, start + len); |
c5794e51 | 7596 | out: |
f0bfa76a FM |
7597 | if (ret && space_reserved) { |
7598 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); | |
f5585f4f | 7599 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true); |
f0bfa76a | 7600 | } |
c5794e51 NB |
7601 | return ret; |
7602 | } | |
7603 | ||
f85781fb GR |
7604 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7605 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7606 | struct iomap *srcmap) | |
4b46fce2 | 7607 | { |
491a6d01 | 7608 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
0b246afa | 7609 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7610 | struct extent_map *em; |
eb838e73 | 7611 | struct extent_state *cached_state = NULL; |
491a6d01 | 7612 | struct btrfs_dio_data *dio_data = iter->private; |
eb838e73 | 7613 | u64 lockstart, lockend; |
f85781fb | 7614 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7615 | int ret = 0; |
f85781fb | 7616 | u64 len = length; |
f5585f4f | 7617 | const u64 data_alloc_len = length; |
f85781fb | 7618 | bool unlock_extents = false; |
eb838e73 | 7619 | |
79d3d1d1 JB |
7620 | /* |
7621 | * We could potentially fault if we have a buffer > PAGE_SIZE, and if | |
7622 | * we're NOWAIT we may submit a bio for a partial range and return | |
7623 | * EIOCBQUEUED, which would result in an errant short read. | |
7624 | * | |
7625 | * The best way to handle this would be to allow for partial completions | |
7626 | * of iocb's, so we could submit the partial bio, return and fault in | |
7627 | * the rest of the pages, and then submit the io for the rest of the | |
7628 | * range. However we don't have that currently, so simply return | |
7629 | * -EAGAIN at this point so that the normal path is used. | |
7630 | */ | |
7631 | if (!write && (flags & IOMAP_NOWAIT) && length > PAGE_SIZE) | |
7632 | return -EAGAIN; | |
7633 | ||
ee5b46a3 CH |
7634 | /* |
7635 | * Cap the size of reads to that usually seen in buffered I/O as we need | |
7636 | * to allocate a contiguous array for the checksums. | |
7637 | */ | |
f85781fb | 7638 | if (!write) |
ee5b46a3 | 7639 | len = min_t(u64, len, fs_info->sectorsize * BTRFS_MAX_BIO_SECTORS); |
eb838e73 | 7640 | |
c329861d JB |
7641 | lockstart = start; |
7642 | lockend = start + len - 1; | |
7643 | ||
f85781fb | 7644 | /* |
b023e675 FM |
7645 | * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't |
7646 | * enough if we've written compressed pages to this area, so we need to | |
7647 | * flush the dirty pages again to make absolutely sure that any | |
7648 | * outstanding dirty pages are on disk - the first flush only starts | |
7649 | * compression on the data, while keeping the pages locked, so by the | |
7650 | * time the second flush returns we know bios for the compressed pages | |
7651 | * were submitted and finished, and the pages no longer under writeback. | |
7652 | * | |
7653 | * If we have a NOWAIT request and we have any pages in the range that | |
7654 | * are locked, likely due to compression still in progress, we don't want | |
7655 | * to block on page locks. We also don't want to block on pages marked as | |
7656 | * dirty or under writeback (same as for the non-compression case). | |
7657 | * iomap_dio_rw() did the same check, but after that and before we got | |
7658 | * here, mmap'ed writes may have happened or buffered reads started | |
7659 | * (readpage() and readahead(), which lock pages), as we haven't locked | |
7660 | * the file range yet. | |
f85781fb GR |
7661 | */ |
7662 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7663 | &BTRFS_I(inode)->runtime_flags)) { | |
b023e675 FM |
7664 | if (flags & IOMAP_NOWAIT) { |
7665 | if (filemap_range_needs_writeback(inode->i_mapping, | |
7666 | lockstart, lockend)) | |
7667 | return -EAGAIN; | |
7668 | } else { | |
7669 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7670 | start + length - 1); | |
7671 | if (ret) | |
7672 | return ret; | |
7673 | } | |
f85781fb GR |
7674 | } |
7675 | ||
491a6d01 | 7676 | memset(dio_data, 0, sizeof(*dio_data)); |
f85781fb | 7677 | |
f5585f4f FM |
7678 | /* |
7679 | * We always try to allocate data space and must do it before locking | |
7680 | * the file range, to avoid deadlocks with concurrent writes to the same | |
7681 | * range if the range has several extents and the writes don't expand the | |
7682 | * current i_size (the inode lock is taken in shared mode). If we fail to | |
7683 | * allocate data space here we continue and later, after locking the | |
7684 | * file range, we fail with ENOSPC only if we figure out we can not do a | |
7685 | * NOCOW write. | |
7686 | */ | |
7687 | if (write && !(flags & IOMAP_NOWAIT)) { | |
7688 | ret = btrfs_check_data_free_space(BTRFS_I(inode), | |
7689 | &dio_data->data_reserved, | |
1daedb1d | 7690 | start, data_alloc_len, false); |
f5585f4f FM |
7691 | if (!ret) |
7692 | dio_data->data_space_reserved = true; | |
7693 | else if (ret && !(BTRFS_I(inode)->flags & | |
7694 | (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) | |
7695 | goto err; | |
7696 | } | |
e1cbbfa5 | 7697 | |
eb838e73 JB |
7698 | /* |
7699 | * If this errors out it's because we couldn't invalidate pagecache for | |
59094403 FM |
7700 | * this range and we need to fallback to buffered IO, or we are doing a |
7701 | * NOWAIT read/write and we need to block. | |
eb838e73 | 7702 | */ |
59094403 FM |
7703 | ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags); |
7704 | if (ret < 0) | |
9c9464cc | 7705 | goto err; |
eb838e73 | 7706 | |
39b07b5d | 7707 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7708 | if (IS_ERR(em)) { |
7709 | ret = PTR_ERR(em); | |
7710 | goto unlock_err; | |
7711 | } | |
4b46fce2 JB |
7712 | |
7713 | /* | |
7714 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7715 | * io. INLINE is special, and we could probably kludge it in here, but | |
7716 | * it's still buffered so for safety lets just fall back to the generic | |
7717 | * buffered path. | |
7718 | * | |
7719 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7720 | * decompress it, so there will be buffering required no matter what we | |
7721 | * do, so go ahead and fallback to buffered. | |
7722 | * | |
01327610 | 7723 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7724 | * to buffered IO. Don't blame me, this is the price we pay for using |
7725 | * the generic code. | |
7726 | */ | |
7727 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7728 | em->block_start == EXTENT_MAP_INLINE) { | |
7729 | free_extent_map(em); | |
a4527e18 FM |
7730 | /* |
7731 | * If we are in a NOWAIT context, return -EAGAIN in order to | |
7732 | * fallback to buffered IO. This is not only because we can | |
7733 | * block with buffered IO (no support for NOWAIT semantics at | |
7734 | * the moment) but also to avoid returning short reads to user | |
7735 | * space - this happens if we were able to read some data from | |
7736 | * previous non-compressed extents and then when we fallback to | |
7737 | * buffered IO, at btrfs_file_read_iter() by calling | |
7738 | * filemap_read(), we fail to fault in pages for the read buffer, | |
7739 | * in which case filemap_read() returns a short read (the number | |
7740 | * of bytes previously read is > 0, so it does not return -EFAULT). | |
7741 | */ | |
7742 | ret = (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK; | |
eb838e73 | 7743 | goto unlock_err; |
4b46fce2 JB |
7744 | } |
7745 | ||
f85781fb | 7746 | len = min(len, em->len - (start - em->start)); |
ca93e44b FM |
7747 | |
7748 | /* | |
7749 | * If we have a NOWAIT request and the range contains multiple extents | |
7750 | * (or a mix of extents and holes), then we return -EAGAIN to make the | |
7751 | * caller fallback to a context where it can do a blocking (without | |
7752 | * NOWAIT) request. This way we avoid doing partial IO and returning | |
7753 | * success to the caller, which is not optimal for writes and for reads | |
7754 | * it can result in unexpected behaviour for an application. | |
7755 | * | |
7756 | * When doing a read, because we use IOMAP_DIO_PARTIAL when calling | |
7757 | * iomap_dio_rw(), we can end up returning less data then what the caller | |
7758 | * asked for, resulting in an unexpected, and incorrect, short read. | |
7759 | * That is, the caller asked to read N bytes and we return less than that, | |
7760 | * which is wrong unless we are crossing EOF. This happens if we get a | |
7761 | * page fault error when trying to fault in pages for the buffer that is | |
7762 | * associated to the struct iov_iter passed to iomap_dio_rw(), and we | |
7763 | * have previously submitted bios for other extents in the range, in | |
7764 | * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of | |
7765 | * those bios have completed by the time we get the page fault error, | |
7766 | * which we return back to our caller - we should only return EIOCBQUEUED | |
7767 | * after we have submitted bios for all the extents in the range. | |
7768 | */ | |
7769 | if ((flags & IOMAP_NOWAIT) && len < length) { | |
7770 | free_extent_map(em); | |
7771 | ret = -EAGAIN; | |
7772 | goto unlock_err; | |
7773 | } | |
7774 | ||
f85781fb GR |
7775 | if (write) { |
7776 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
d7a8ab4e | 7777 | start, len, flags); |
c5794e51 NB |
7778 | if (ret < 0) |
7779 | goto unlock_err; | |
f85781fb GR |
7780 | unlock_extents = true; |
7781 | /* Recalc len in case the new em is smaller than requested */ | |
7782 | len = min(len, em->len - (start - em->start)); | |
f5585f4f FM |
7783 | if (dio_data->data_space_reserved) { |
7784 | u64 release_offset; | |
7785 | u64 release_len = 0; | |
7786 | ||
7787 | if (dio_data->nocow_done) { | |
7788 | release_offset = start; | |
7789 | release_len = data_alloc_len; | |
7790 | } else if (len < data_alloc_len) { | |
7791 | release_offset = start + len; | |
7792 | release_len = data_alloc_len - len; | |
7793 | } | |
7794 | ||
7795 | if (release_len > 0) | |
7796 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7797 | dio_data->data_reserved, | |
7798 | release_offset, | |
7799 | release_len); | |
7800 | } | |
c5794e51 | 7801 | } else { |
1c8d0175 NB |
7802 | /* |
7803 | * We need to unlock only the end area that we aren't using. | |
7804 | * The rest is going to be unlocked by the endio routine. | |
7805 | */ | |
f85781fb GR |
7806 | lockstart = start + len; |
7807 | if (lockstart < lockend) | |
7808 | unlock_extents = true; | |
7809 | } | |
7810 | ||
7811 | if (unlock_extents) | |
570eb97b JB |
7812 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7813 | &cached_state); | |
f85781fb GR |
7814 | else |
7815 | free_extent_state(cached_state); | |
7816 | ||
7817 | /* | |
7818 | * Translate extent map information to iomap. | |
7819 | * We trim the extents (and move the addr) even though iomap code does | |
7820 | * that, since we have locked only the parts we are performing I/O in. | |
7821 | */ | |
7822 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
7823 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { | |
7824 | iomap->addr = IOMAP_NULL_ADDR; | |
7825 | iomap->type = IOMAP_HOLE; | |
7826 | } else { | |
7827 | iomap->addr = em->block_start + (start - em->start); | |
7828 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7829 | } |
f85781fb | 7830 | iomap->offset = start; |
d24fa5c1 | 7831 | iomap->bdev = fs_info->fs_devices->latest_dev->bdev; |
f85781fb | 7832 | iomap->length = len; |
a43a67a2 | 7833 | |
e380adfc | 7834 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start)) |
544d24f9 NA |
7835 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
7836 | ||
4b46fce2 JB |
7837 | free_extent_map(em); |
7838 | ||
7839 | return 0; | |
eb838e73 JB |
7840 | |
7841 | unlock_err: | |
570eb97b JB |
7842 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7843 | &cached_state); | |
9c9464cc | 7844 | err: |
f5585f4f FM |
7845 | if (dio_data->data_space_reserved) { |
7846 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7847 | dio_data->data_reserved, | |
7848 | start, data_alloc_len); | |
7849 | extent_changeset_free(dio_data->data_reserved); | |
7850 | } | |
7851 | ||
f85781fb GR |
7852 | return ret; |
7853 | } | |
7854 | ||
7855 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7856 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7857 | { | |
491a6d01 CH |
7858 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
7859 | struct btrfs_dio_data *dio_data = iter->private; | |
f85781fb GR |
7860 | size_t submitted = dio_data->submitted; |
7861 | const bool write = !!(flags & IOMAP_WRITE); | |
491a6d01 | 7862 | int ret = 0; |
f85781fb GR |
7863 | |
7864 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7865 | /* If reading from a hole, unlock and return */ | |
570eb97b JB |
7866 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1, |
7867 | NULL); | |
491a6d01 | 7868 | return 0; |
f85781fb GR |
7869 | } |
7870 | ||
7871 | if (submitted < length) { | |
7872 | pos += submitted; | |
7873 | length -= submitted; | |
7874 | if (write) | |
711f447b CH |
7875 | btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL, |
7876 | pos, length, false); | |
f85781fb GR |
7877 | else |
7878 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
570eb97b | 7879 | pos + length - 1, NULL); |
f85781fb GR |
7880 | ret = -ENOTBLK; |
7881 | } | |
7882 | ||
f0bfa76a | 7883 | if (write) |
f85781fb | 7884 | extent_changeset_free(dio_data->data_reserved); |
8b110e39 MX |
7885 | return ret; |
7886 | } | |
7887 | ||
769b4f24 | 7888 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
8b110e39 | 7889 | { |
769b4f24 OS |
7890 | /* |
7891 | * This implies a barrier so that stores to dio_bio->bi_status before | |
7892 | * this and loads of dio_bio->bi_status after this are fully ordered. | |
7893 | */ | |
7894 | if (!refcount_dec_and_test(&dip->refs)) | |
7895 | return; | |
8b110e39 | 7896 | |
642c5d34 | 7897 | if (btrfs_op(&dip->bio) == BTRFS_MAP_WRITE) { |
711f447b CH |
7898 | btrfs_mark_ordered_io_finished(BTRFS_I(dip->inode), NULL, |
7899 | dip->file_offset, dip->bytes, | |
7900 | !dip->bio.bi_status); | |
769b4f24 OS |
7901 | } else { |
7902 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, | |
47926ab5 | 7903 | dip->file_offset, |
570eb97b | 7904 | dip->file_offset + dip->bytes - 1, NULL); |
8b110e39 MX |
7905 | } |
7906 | ||
642c5d34 CH |
7907 | kfree(dip->csums); |
7908 | bio_endio(&dip->bio); | |
8b110e39 MX |
7909 | } |
7910 | ||
ad357938 | 7911 | static void submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
cb3a12d9 DS |
7912 | int mirror_num, |
7913 | enum btrfs_compression_type compress_type) | |
8b110e39 | 7914 | { |
917f32a2 | 7915 | struct btrfs_dio_private *dip = btrfs_bio(bio)->private; |
2ff7e61e | 7916 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 | 7917 | |
37226b21 | 7918 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7919 | |
77d5d689 | 7920 | refcount_inc(&dip->refs); |
1a722d8f | 7921 | btrfs_submit_bio(fs_info, bio, mirror_num); |
8b110e39 MX |
7922 | } |
7923 | ||
f4f39fc5 | 7924 | static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip, |
c3a3b19b | 7925 | struct btrfs_bio *bbio, |
fd9d6670 | 7926 | const bool uptodate) |
4b46fce2 | 7927 | { |
f4f39fc5 | 7928 | struct inode *inode = dip->inode; |
fd9d6670 | 7929 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
fd9d6670 | 7930 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
58efbc9f | 7931 | blk_status_t err = BLK_STS_OK; |
1e87770c CH |
7932 | struct bvec_iter iter; |
7933 | struct bio_vec bv; | |
7934 | u32 offset; | |
7935 | ||
7936 | btrfs_bio_for_each_sector(fs_info, bv, bbio, iter, offset) { | |
7937 | u64 start = bbio->file_offset + offset; | |
7938 | ||
7939 | if (uptodate && | |
7959bd44 CH |
7940 | (!csum || !btrfs_check_data_csum(inode, bbio, offset, bv.bv_page, |
7941 | bv.bv_offset))) { | |
0d0a762c JB |
7942 | btrfs_clean_io_failure(BTRFS_I(inode), start, |
7943 | bv.bv_page, bv.bv_offset); | |
1e87770c CH |
7944 | } else { |
7945 | int ret; | |
4b46fce2 | 7946 | |
7aa51232 CH |
7947 | ret = btrfs_repair_one_sector(inode, bbio, offset, |
7948 | bv.bv_page, bv.bv_offset, | |
1e87770c CH |
7949 | submit_dio_repair_bio); |
7950 | if (ret) | |
7951 | err = errno_to_blk_status(ret); | |
2dabb324 | 7952 | } |
2c30c71b | 7953 | } |
c1dc0896 | 7954 | |
c1dc0896 | 7955 | return err; |
14543774 FM |
7956 | } |
7957 | ||
8896a08d | 7958 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
1941b64b QW |
7959 | struct bio *bio, |
7960 | u64 dio_file_offset) | |
eaf25d93 | 7961 | { |
e331f6b1 | 7962 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, false); |
eaf25d93 CM |
7963 | } |
7964 | ||
917f32a2 | 7965 | static void btrfs_end_dio_bio(struct btrfs_bio *bbio) |
e65e1535 | 7966 | { |
917f32a2 CH |
7967 | struct btrfs_dio_private *dip = bbio->private; |
7968 | struct bio *bio = &bbio->bio; | |
4e4cbee9 | 7969 | blk_status_t err = bio->bi_status; |
e65e1535 | 7970 | |
8b110e39 MX |
7971 | if (err) |
7972 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 7973 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 | 7974 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
1201b58b | 7975 | bio->bi_opf, bio->bi_iter.bi_sector, |
8b110e39 MX |
7976 | bio->bi_iter.bi_size, err); |
7977 | ||
f4f39fc5 | 7978 | if (bio_op(bio) == REQ_OP_READ) |
0fdf977d | 7979 | err = btrfs_check_read_dio_bio(dip, bbio, !err); |
e65e1535 | 7980 | |
769b4f24 | 7981 | if (err) |
642c5d34 | 7982 | dip->bio.bi_status = err; |
e65e1535 | 7983 | |
0fdf977d | 7984 | btrfs_record_physical_zoned(dip->inode, bbio->file_offset, bio); |
544d24f9 | 7985 | |
e65e1535 | 7986 | bio_put(bio); |
769b4f24 | 7987 | btrfs_dio_private_put(dip); |
c1dc0896 MX |
7988 | } |
7989 | ||
37899117 CH |
7990 | static void btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
7991 | u64 file_offset, int async_submit) | |
e65e1535 | 7992 | { |
0b246afa | 7993 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
917f32a2 | 7994 | struct btrfs_dio_private *dip = btrfs_bio(bio)->private; |
4e4cbee9 | 7995 | blk_status_t ret; |
e65e1535 | 7996 | |
81bd9328 CH |
7997 | /* Save the original iter for read repair */ |
7998 | if (btrfs_op(bio) == BTRFS_MAP_READ) | |
7999 | btrfs_bio(bio)->iter = bio->bi_iter; | |
e65e1535 | 8000 | |
e6961cac | 8001 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
8002 | goto map; |
8003 | ||
d7b9416f | 8004 | if (btrfs_op(bio) == BTRFS_MAP_WRITE) { |
c93104e7 | 8005 | /* Check btrfs_submit_data_write_bio() for async submit rules */ |
ea1f0ced CH |
8006 | if (async_submit && !atomic_read(&BTRFS_I(inode)->sync_writers) && |
8007 | btrfs_wq_submit_bio(inode, bio, 0, file_offset, | |
8008 | btrfs_submit_bio_start_direct_io)) | |
37899117 | 8009 | return; |
ea1f0ced | 8010 | |
1ae39938 JB |
8011 | /* |
8012 | * If we aren't doing async submit, calculate the csum of the | |
8013 | * bio now. | |
8014 | */ | |
e331f6b1 | 8015 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, false); |
37899117 | 8016 | if (ret) { |
917f32a2 | 8017 | btrfs_bio_end_io(btrfs_bio(bio), ret); |
37899117 CH |
8018 | return; |
8019 | } | |
23ea8e5a | 8020 | } else { |
a89ce08c CH |
8021 | btrfs_bio(bio)->csum = btrfs_csum_ptr(fs_info, dip->csums, |
8022 | file_offset - dip->file_offset); | |
c2db1073 | 8023 | } |
1ae39938 | 8024 | map: |
1a722d8f | 8025 | btrfs_submit_bio(fs_info, bio, 0); |
e65e1535 MX |
8026 | } |
8027 | ||
3e08773c | 8028 | static void btrfs_submit_direct(const struct iomap_iter *iter, |
f85781fb | 8029 | struct bio *dio_bio, loff_t file_offset) |
c36cac28 | 8030 | { |
642c5d34 CH |
8031 | struct btrfs_dio_private *dip = |
8032 | container_of(dio_bio, struct btrfs_dio_private, bio); | |
a6d3d495 | 8033 | struct inode *inode = iter->inode; |
cfe94440 | 8034 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
0b246afa | 8035 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
769b4f24 OS |
8036 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
8037 | BTRFS_BLOCK_GROUP_RAID56_MASK); | |
e65e1535 | 8038 | struct bio *bio; |
c36cac28 | 8039 | u64 start_sector; |
1ae39938 | 8040 | int async_submit = 0; |
725130ba | 8041 | u64 submit_len; |
42b5d73b NA |
8042 | u64 clone_offset = 0; |
8043 | u64 clone_len; | |
42034313 | 8044 | u64 logical; |
5f4dc8fc | 8045 | int ret; |
58efbc9f | 8046 | blk_status_t status; |
89b798ad | 8047 | struct btrfs_io_geometry geom; |
491a6d01 | 8048 | struct btrfs_dio_data *dio_data = iter->private; |
42034313 | 8049 | struct extent_map *em = NULL; |
e65e1535 | 8050 | |
642c5d34 CH |
8051 | dip->inode = inode; |
8052 | dip->file_offset = file_offset; | |
8053 | dip->bytes = dio_bio->bi_iter.bi_size; | |
8054 | refcount_set(&dip->refs, 1); | |
8055 | dip->csums = NULL; | |
8056 | ||
8057 | if (!write && !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
8058 | unsigned int nr_sectors = | |
8059 | (dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits); | |
facc8a22 | 8060 | |
85879573 OS |
8061 | /* |
8062 | * Load the csums up front to reduce csum tree searches and | |
8063 | * contention when submitting bios. | |
8064 | */ | |
642c5d34 CH |
8065 | status = BLK_STS_RESOURCE; |
8066 | dip->csums = kcalloc(nr_sectors, fs_info->csum_size, GFP_NOFS); | |
063b1f21 | 8067 | if (!dip->csums) |
642c5d34 CH |
8068 | goto out_err; |
8069 | ||
6275193e | 8070 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
85879573 OS |
8071 | if (status != BLK_STS_OK) |
8072 | goto out_err; | |
02f57c7a JB |
8073 | } |
8074 | ||
769b4f24 OS |
8075 | start_sector = dio_bio->bi_iter.bi_sector; |
8076 | submit_len = dio_bio->bi_iter.bi_size; | |
53b381b3 | 8077 | |
3c91ee69 | 8078 | do { |
42034313 MR |
8079 | logical = start_sector << 9; |
8080 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); | |
8081 | if (IS_ERR(em)) { | |
8082 | status = errno_to_blk_status(PTR_ERR(em)); | |
8083 | em = NULL; | |
8084 | goto out_err_em; | |
8085 | } | |
8086 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), | |
43c0d1a5 | 8087 | logical, &geom); |
769b4f24 OS |
8088 | if (ret) { |
8089 | status = errno_to_blk_status(ret); | |
42034313 | 8090 | goto out_err_em; |
769b4f24 | 8091 | } |
769b4f24 | 8092 | |
42b5d73b NA |
8093 | clone_len = min(submit_len, geom.len); |
8094 | ASSERT(clone_len <= UINT_MAX); | |
02f57c7a | 8095 | |
725130ba LB |
8096 | /* |
8097 | * This will never fail as it's passing GPF_NOFS and | |
8098 | * the allocation is backed by btrfs_bioset. | |
8099 | */ | |
917f32a2 CH |
8100 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len, |
8101 | btrfs_end_dio_bio, dip); | |
00d82525 | 8102 | btrfs_bio(bio)->file_offset = file_offset; |
725130ba | 8103 | |
544d24f9 NA |
8104 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
8105 | status = extract_ordered_extent(BTRFS_I(inode), bio, | |
8106 | file_offset); | |
8107 | if (status) { | |
8108 | bio_put(bio); | |
8109 | goto out_err; | |
8110 | } | |
8111 | } | |
8112 | ||
725130ba LB |
8113 | ASSERT(submit_len >= clone_len); |
8114 | submit_len -= clone_len; | |
e65e1535 | 8115 | |
725130ba LB |
8116 | /* |
8117 | * Increase the count before we submit the bio so we know | |
8118 | * the end IO handler won't happen before we increase the | |
8119 | * count. Otherwise, the dip might get freed before we're | |
8120 | * done setting it up. | |
769b4f24 OS |
8121 | * |
8122 | * We transfer the initial reference to the last bio, so we | |
8123 | * don't need to increment the reference count for the last one. | |
725130ba | 8124 | */ |
769b4f24 OS |
8125 | if (submit_len > 0) { |
8126 | refcount_inc(&dip->refs); | |
8127 | /* | |
8128 | * If we are submitting more than one bio, submit them | |
8129 | * all asynchronously. The exception is RAID 5 or 6, as | |
8130 | * asynchronous checksums make it difficult to collect | |
8131 | * full stripe writes. | |
8132 | */ | |
8133 | if (!raid56) | |
8134 | async_submit = 1; | |
8135 | } | |
e65e1535 | 8136 | |
37899117 | 8137 | btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
e65e1535 | 8138 | |
f85781fb | 8139 | dio_data->submitted += clone_len; |
725130ba LB |
8140 | clone_offset += clone_len; |
8141 | start_sector += clone_len >> 9; | |
8142 | file_offset += clone_len; | |
42034313 MR |
8143 | |
8144 | free_extent_map(em); | |
3c91ee69 | 8145 | } while (submit_len > 0); |
3e08773c | 8146 | return; |
e65e1535 | 8147 | |
42034313 MR |
8148 | out_err_em: |
8149 | free_extent_map(em); | |
e65e1535 | 8150 | out_err: |
642c5d34 | 8151 | dio_bio->bi_status = status; |
769b4f24 | 8152 | btrfs_dio_private_put(dip); |
4b46fce2 JB |
8153 | } |
8154 | ||
36e8c622 | 8155 | static const struct iomap_ops btrfs_dio_iomap_ops = { |
f85781fb GR |
8156 | .iomap_begin = btrfs_dio_iomap_begin, |
8157 | .iomap_end = btrfs_dio_iomap_end, | |
8158 | }; | |
8159 | ||
36e8c622 | 8160 | static const struct iomap_dio_ops btrfs_dio_ops = { |
f85781fb | 8161 | .submit_io = btrfs_submit_direct, |
642c5d34 | 8162 | .bio_set = &btrfs_dio_bioset, |
f85781fb GR |
8163 | }; |
8164 | ||
8184620a | 8165 | ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter, size_t done_before) |
36e8c622 | 8166 | { |
491a6d01 CH |
8167 | struct btrfs_dio_data data; |
8168 | ||
36e8c622 | 8169 | return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, |
8184620a FM |
8170 | IOMAP_DIO_PARTIAL, &data, done_before); |
8171 | } | |
8172 | ||
8173 | struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter, | |
8174 | size_t done_before) | |
8175 | { | |
8176 | struct btrfs_dio_data data; | |
8177 | ||
8178 | return __iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, | |
8179 | IOMAP_DIO_PARTIAL, &data, done_before); | |
36e8c622 CH |
8180 | } |
8181 | ||
1506fcc8 | 8182 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 8183 | u64 start, u64 len) |
1506fcc8 | 8184 | { |
05dadc09 TI |
8185 | int ret; |
8186 | ||
45dd052e | 8187 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
8188 | if (ret) |
8189 | return ret; | |
8190 | ||
33a86cfa FM |
8191 | /* |
8192 | * fiemap_prep() called filemap_write_and_wait() for the whole possible | |
8193 | * file range (0 to LLONG_MAX), but that is not enough if we have | |
8194 | * compression enabled. The first filemap_fdatawrite_range() only kicks | |
8195 | * in the compression of data (in an async thread) and will return | |
8196 | * before the compression is done and writeback is started. A second | |
8197 | * filemap_fdatawrite_range() is needed to wait for the compression to | |
ac3c0d36 FM |
8198 | * complete and writeback to start. We also need to wait for ordered |
8199 | * extents to complete, because our fiemap implementation uses mainly | |
8200 | * file extent items to list the extents, searching for extent maps | |
8201 | * only for file ranges with holes or prealloc extents to figure out | |
8202 | * if we have delalloc in those ranges. | |
33a86cfa FM |
8203 | */ |
8204 | if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) { | |
ac3c0d36 | 8205 | ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX); |
33a86cfa FM |
8206 | if (ret) |
8207 | return ret; | |
8208 | } | |
8209 | ||
facee0a0 | 8210 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
1506fcc8 YS |
8211 | } |
8212 | ||
48a3b636 ES |
8213 | static int btrfs_writepages(struct address_space *mapping, |
8214 | struct writeback_control *wbc) | |
b293f02e | 8215 | { |
8ae225a8 | 8216 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8217 | } |
8218 | ||
ba206a02 | 8219 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 8220 | { |
ba206a02 | 8221 | extent_readahead(rac); |
3ab2fb5a | 8222 | } |
2a3ff0ad | 8223 | |
7c11d0ae | 8224 | /* |
f913cff3 | 8225 | * For release_folio() and invalidate_folio() we have a race window where |
895586eb | 8226 | * folio_end_writeback() is called but the subpage spinlock is not yet released. |
7c11d0ae QW |
8227 | * If we continue to release/invalidate the page, we could cause use-after-free |
8228 | * for subpage spinlock. So this function is to spin and wait for subpage | |
8229 | * spinlock. | |
8230 | */ | |
8231 | static void wait_subpage_spinlock(struct page *page) | |
8232 | { | |
8233 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
8234 | struct btrfs_subpage *subpage; | |
8235 | ||
fbca46eb | 8236 | if (!btrfs_is_subpage(fs_info, page)) |
7c11d0ae QW |
8237 | return; |
8238 | ||
8239 | ASSERT(PagePrivate(page) && page->private); | |
8240 | subpage = (struct btrfs_subpage *)page->private; | |
8241 | ||
8242 | /* | |
8243 | * This may look insane as we just acquire the spinlock and release it, | |
8244 | * without doing anything. But we just want to make sure no one is | |
8245 | * still holding the subpage spinlock. | |
8246 | * And since the page is not dirty nor writeback, and we have page | |
8247 | * locked, the only possible way to hold a spinlock is from the endio | |
8248 | * function to clear page writeback. | |
8249 | * | |
8250 | * Here we just acquire the spinlock so that all existing callers | |
8251 | * should exit and we're safe to release/invalidate the page. | |
8252 | */ | |
8253 | spin_lock_irq(&subpage->lock); | |
8254 | spin_unlock_irq(&subpage->lock); | |
8255 | } | |
8256 | ||
f913cff3 | 8257 | static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
9ebefb18 | 8258 | { |
f913cff3 | 8259 | int ret = try_release_extent_mapping(&folio->page, gfp_flags); |
7c11d0ae QW |
8260 | |
8261 | if (ret == 1) { | |
f913cff3 MWO |
8262 | wait_subpage_spinlock(&folio->page); |
8263 | clear_page_extent_mapped(&folio->page); | |
7c11d0ae | 8264 | } |
a52d9a80 | 8265 | return ret; |
39279cc3 CM |
8266 | } |
8267 | ||
f913cff3 | 8268 | static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
e6dcd2dc | 8269 | { |
f913cff3 MWO |
8270 | if (folio_test_writeback(folio) || folio_test_dirty(folio)) |
8271 | return false; | |
8272 | return __btrfs_release_folio(folio, gfp_flags); | |
e6dcd2dc CM |
8273 | } |
8274 | ||
f8e66081 | 8275 | #ifdef CONFIG_MIGRATION |
e7a60a17 MWO |
8276 | static int btrfs_migrate_folio(struct address_space *mapping, |
8277 | struct folio *dst, struct folio *src, | |
f8e66081 RG |
8278 | enum migrate_mode mode) |
8279 | { | |
e7a60a17 | 8280 | int ret = filemap_migrate_folio(mapping, dst, src, mode); |
f8e66081 | 8281 | |
f8e66081 RG |
8282 | if (ret != MIGRATEPAGE_SUCCESS) |
8283 | return ret; | |
8284 | ||
e7a60a17 MWO |
8285 | if (folio_test_ordered(src)) { |
8286 | folio_clear_ordered(src); | |
8287 | folio_set_ordered(dst); | |
f8e66081 RG |
8288 | } |
8289 | ||
f8e66081 RG |
8290 | return MIGRATEPAGE_SUCCESS; |
8291 | } | |
e7a60a17 MWO |
8292 | #else |
8293 | #define btrfs_migrate_folio NULL | |
f8e66081 RG |
8294 | #endif |
8295 | ||
895586eb MWO |
8296 | static void btrfs_invalidate_folio(struct folio *folio, size_t offset, |
8297 | size_t length) | |
39279cc3 | 8298 | { |
895586eb | 8299 | struct btrfs_inode *inode = BTRFS_I(folio->mapping->host); |
b945a463 | 8300 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
53ac7ead | 8301 | struct extent_io_tree *tree = &inode->io_tree; |
2ac55d41 | 8302 | struct extent_state *cached_state = NULL; |
895586eb MWO |
8303 | u64 page_start = folio_pos(folio); |
8304 | u64 page_end = page_start + folio_size(folio) - 1; | |
3b835840 | 8305 | u64 cur; |
53ac7ead | 8306 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 8307 | |
8b62b72b | 8308 | /* |
895586eb MWO |
8309 | * We have folio locked so no new ordered extent can be created on this |
8310 | * page, nor bio can be submitted for this folio. | |
8b62b72b | 8311 | * |
895586eb MWO |
8312 | * But already submitted bio can still be finished on this folio. |
8313 | * Furthermore, endio function won't skip folio which has Ordered | |
f57ad937 | 8314 | * (Private2) already cleared, so it's possible for endio and |
895586eb MWO |
8315 | * invalidate_folio to do the same ordered extent accounting twice |
8316 | * on one folio. | |
266a2586 QW |
8317 | * |
8318 | * So here we wait for any submitted bios to finish, so that we won't | |
895586eb | 8319 | * do double ordered extent accounting on the same folio. |
8b62b72b | 8320 | */ |
895586eb MWO |
8321 | folio_wait_writeback(folio); |
8322 | wait_subpage_spinlock(&folio->page); | |
8b62b72b | 8323 | |
bcd77455 QW |
8324 | /* |
8325 | * For subpage case, we have call sites like | |
8326 | * btrfs_punch_hole_lock_range() which passes range not aligned to | |
8327 | * sectorsize. | |
895586eb MWO |
8328 | * If the range doesn't cover the full folio, we don't need to and |
8329 | * shouldn't clear page extent mapped, as folio->private can still | |
bcd77455 QW |
8330 | * record subpage dirty bits for other part of the range. |
8331 | * | |
895586eb MWO |
8332 | * For cases that invalidate the full folio even the range doesn't |
8333 | * cover the full folio, like invalidating the last folio, we're | |
bcd77455 QW |
8334 | * still safe to wait for ordered extent to finish. |
8335 | */ | |
5a60542c | 8336 | if (!(offset == 0 && length == folio_size(folio))) { |
f913cff3 | 8337 | btrfs_release_folio(folio, GFP_NOFS); |
e6dcd2dc CM |
8338 | return; |
8339 | } | |
131e404a FDBM |
8340 | |
8341 | if (!inode_evicting) | |
570eb97b | 8342 | lock_extent(tree, page_start, page_end, &cached_state); |
951c80f8 | 8343 | |
3b835840 QW |
8344 | cur = page_start; |
8345 | while (cur < page_end) { | |
8346 | struct btrfs_ordered_extent *ordered; | |
3b835840 | 8347 | u64 range_end; |
b945a463 | 8348 | u32 range_len; |
bd015294 | 8349 | u32 extra_flags = 0; |
3b835840 QW |
8350 | |
8351 | ordered = btrfs_lookup_first_ordered_range(inode, cur, | |
8352 | page_end + 1 - cur); | |
8353 | if (!ordered) { | |
8354 | range_end = page_end; | |
8355 | /* | |
8356 | * No ordered extent covering this range, we are safe | |
8357 | * to delete all extent states in the range. | |
8358 | */ | |
bd015294 | 8359 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8360 | goto next; |
8361 | } | |
8362 | if (ordered->file_offset > cur) { | |
8363 | /* | |
8364 | * There is a range between [cur, oe->file_offset) not | |
8365 | * covered by any ordered extent. | |
8366 | * We are safe to delete all extent states, and handle | |
8367 | * the ordered extent in the next iteration. | |
8368 | */ | |
8369 | range_end = ordered->file_offset - 1; | |
bd015294 | 8370 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8371 | goto next; |
8372 | } | |
8373 | ||
8374 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, | |
8375 | page_end); | |
b945a463 QW |
8376 | ASSERT(range_end + 1 - cur < U32_MAX); |
8377 | range_len = range_end + 1 - cur; | |
895586eb | 8378 | if (!btrfs_page_test_ordered(fs_info, &folio->page, cur, range_len)) { |
3b835840 | 8379 | /* |
f57ad937 QW |
8380 | * If Ordered (Private2) is cleared, it means endio has |
8381 | * already been executed for the range. | |
3b835840 QW |
8382 | * We can't delete the extent states as |
8383 | * btrfs_finish_ordered_io() may still use some of them. | |
8384 | */ | |
3b835840 QW |
8385 | goto next; |
8386 | } | |
895586eb | 8387 | btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len); |
3b835840 | 8388 | |
eb84ae03 | 8389 | /* |
2766ff61 FM |
8390 | * IO on this page will never be started, so we need to account |
8391 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW | |
8392 | * here, must leave that up for the ordered extent completion. | |
3b835840 QW |
8393 | * |
8394 | * This will also unlock the range for incoming | |
8395 | * btrfs_finish_ordered_io(). | |
eb84ae03 | 8396 | */ |
131e404a | 8397 | if (!inode_evicting) |
3b835840 | 8398 | clear_extent_bit(tree, cur, range_end, |
2766ff61 | 8399 | EXTENT_DELALLOC | |
131e404a | 8400 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
bd015294 | 8401 | EXTENT_DEFRAG, &cached_state); |
3b835840 QW |
8402 | |
8403 | spin_lock_irq(&inode->ordered_tree.lock); | |
8404 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8405 | ordered->truncated_len = min(ordered->truncated_len, | |
8406 | cur - ordered->file_offset); | |
8407 | spin_unlock_irq(&inode->ordered_tree.lock); | |
8408 | ||
bd015294 JB |
8409 | /* |
8410 | * If the ordered extent has finished, we're safe to delete all | |
8411 | * the extent states of the range, otherwise | |
8412 | * btrfs_finish_ordered_io() will get executed by endio for | |
8413 | * other pages, so we can't delete extent states. | |
8414 | */ | |
3b835840 | 8415 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
f41b6ba9 | 8416 | cur, range_end + 1 - cur)) { |
3b835840 QW |
8417 | btrfs_finish_ordered_io(ordered); |
8418 | /* | |
8419 | * The ordered extent has finished, now we're again | |
8420 | * safe to delete all extent states of the range. | |
8421 | */ | |
bd015294 | 8422 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8423 | } |
8424 | next: | |
8425 | if (ordered) | |
8426 | btrfs_put_ordered_extent(ordered); | |
8b62b72b | 8427 | /* |
3b835840 QW |
8428 | * Qgroup reserved space handler |
8429 | * Sector(s) here will be either: | |
266a2586 | 8430 | * |
3b835840 QW |
8431 | * 1) Already written to disk or bio already finished |
8432 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. | |
8433 | * Qgroup will be handled by its qgroup_record then. | |
8434 | * btrfs_qgroup_free_data() call will do nothing here. | |
8435 | * | |
8436 | * 2) Not written to disk yet | |
8437 | * Then btrfs_qgroup_free_data() call will clear the | |
8438 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup | |
8439 | * reserved data space. | |
8440 | * Since the IO will never happen for this page. | |
8b62b72b | 8441 | */ |
3b835840 | 8442 | btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur); |
131e404a | 8443 | if (!inode_evicting) { |
3b835840 QW |
8444 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
8445 | EXTENT_DELALLOC | EXTENT_UPTODATE | | |
bd015294 JB |
8446 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG | |
8447 | extra_flags, &cached_state); | |
131e404a | 8448 | } |
3b835840 | 8449 | cur = range_end + 1; |
131e404a | 8450 | } |
b9d0b389 | 8451 | /* |
3b835840 | 8452 | * We have iterated through all ordered extents of the page, the page |
f57ad937 QW |
8453 | * should not have Ordered (Private2) anymore, or the above iteration |
8454 | * did something wrong. | |
b9d0b389 | 8455 | */ |
895586eb MWO |
8456 | ASSERT(!folio_test_ordered(folio)); |
8457 | btrfs_page_clear_checked(fs_info, &folio->page, folio_pos(folio), folio_size(folio)); | |
3b835840 | 8458 | if (!inode_evicting) |
f913cff3 | 8459 | __btrfs_release_folio(folio, GFP_NOFS); |
895586eb | 8460 | clear_page_extent_mapped(&folio->page); |
39279cc3 CM |
8461 | } |
8462 | ||
9ebefb18 CM |
8463 | /* |
8464 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8465 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8466 | * be careful to check for EOF conditions here. We set the page up correctly | |
8467 | * for a written page which means we get ENOSPC checking when writing into | |
8468 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8469 | * support these features. | |
8470 | * | |
8471 | * We are not allowed to take the i_mutex here so we have to play games to | |
8472 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8473 | * truncate_setsize() writes the inode size before removing pages, once we have |
8474 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8475 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8476 | * unlock the page. | |
8477 | */ | |
a528a241 | 8478 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8479 | { |
c2ec175c | 8480 | struct page *page = vmf->page; |
11bac800 | 8481 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8482 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8483 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8484 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8485 | struct extent_state *cached_state = NULL; |
364ecf36 | 8486 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 8487 | unsigned long zero_start; |
9ebefb18 | 8488 | loff_t size; |
a528a241 SJ |
8489 | vm_fault_t ret; |
8490 | int ret2; | |
9998eb70 | 8491 | int reserved = 0; |
d0b7da88 | 8492 | u64 reserved_space; |
a52d9a80 | 8493 | u64 page_start; |
e6dcd2dc | 8494 | u64 page_end; |
d0b7da88 CR |
8495 | u64 end; |
8496 | ||
09cbfeaf | 8497 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8498 | |
b2b5ef5c | 8499 | sb_start_pagefault(inode->i_sb); |
df480633 | 8500 | page_start = page_offset(page); |
09cbfeaf | 8501 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8502 | end = page_end; |
df480633 | 8503 | |
d0b7da88 CR |
8504 | /* |
8505 | * Reserving delalloc space after obtaining the page lock can lead to | |
8506 | * deadlock. For example, if a dirty page is locked by this function | |
8507 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
f3e90c1c | 8508 | * dirty page write out, then the btrfs_writepages() function could |
d0b7da88 CR |
8509 | * end up waiting indefinitely to get a lock on the page currently |
8510 | * being processed by btrfs_page_mkwrite() function. | |
8511 | */ | |
e5b7231e NB |
8512 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8513 | page_start, reserved_space); | |
a528a241 SJ |
8514 | if (!ret2) { |
8515 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8516 | reserved = 1; |
8517 | } | |
a528a241 SJ |
8518 | if (ret2) { |
8519 | ret = vmf_error(ret2); | |
9998eb70 CM |
8520 | if (reserved) |
8521 | goto out; | |
8522 | goto out_noreserve; | |
56a76f82 | 8523 | } |
1832a6d5 | 8524 | |
56a76f82 | 8525 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8526 | again: |
8318ba79 | 8527 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8528 | lock_page(page); |
9ebefb18 | 8529 | size = i_size_read(inode); |
a52d9a80 | 8530 | |
9ebefb18 | 8531 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8532 | (page_start >= size)) { |
9ebefb18 CM |
8533 | /* page got truncated out from underneath us */ |
8534 | goto out_unlock; | |
8535 | } | |
e6dcd2dc CM |
8536 | wait_on_page_writeback(page); |
8537 | ||
570eb97b | 8538 | lock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8539 | ret2 = set_page_extent_mapped(page); |
8540 | if (ret2 < 0) { | |
8541 | ret = vmf_error(ret2); | |
570eb97b | 8542 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8543 | goto out_unlock; |
8544 | } | |
e6dcd2dc | 8545 | |
eb84ae03 CM |
8546 | /* |
8547 | * we can't set the delalloc bits if there are pending ordered | |
8548 | * extents. Drop our locks and wait for them to finish | |
8549 | */ | |
a776c6fa NB |
8550 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8551 | PAGE_SIZE); | |
e6dcd2dc | 8552 | if (ordered) { |
570eb97b | 8553 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc | 8554 | unlock_page(page); |
8318ba79 | 8555 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
c0a43603 | 8556 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
8557 | btrfs_put_ordered_extent(ordered); |
8558 | goto again; | |
8559 | } | |
8560 | ||
09cbfeaf | 8561 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8562 | reserved_space = round_up(size - page_start, |
0b246afa | 8563 | fs_info->sectorsize); |
09cbfeaf | 8564 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8565 | end = page_start + reserved_space - 1; |
86d52921 NB |
8566 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8567 | data_reserved, page_start, | |
8568 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8569 | } |
8570 | } | |
8571 | ||
fbf19087 | 8572 | /* |
5416034f LB |
8573 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8574 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8575 | * bits, thus in this case for space account reason, we still need to | |
8576 | * clear any delalloc bits within this page range since we have to | |
8577 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8578 | */ |
d0b7da88 | 8579 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d | 8580 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
bd015294 | 8581 | EXTENT_DEFRAG, &cached_state); |
fbf19087 | 8582 | |
c2566f22 | 8583 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8584 | &cached_state); |
a528a241 | 8585 | if (ret2) { |
570eb97b | 8586 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
9ed74f2d JB |
8587 | ret = VM_FAULT_SIGBUS; |
8588 | goto out_unlock; | |
8589 | } | |
9ebefb18 CM |
8590 | |
8591 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8592 | if (page_start + PAGE_SIZE > size) |
7073017a | 8593 | zero_start = offset_in_page(size); |
9ebefb18 | 8594 | else |
09cbfeaf | 8595 | zero_start = PAGE_SIZE; |
9ebefb18 | 8596 | |
21a8935e | 8597 | if (zero_start != PAGE_SIZE) |
d048b9c2 | 8598 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
21a8935e | 8599 | |
e4f94347 | 8600 | btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE); |
2d8ec40e QW |
8601 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
8602 | btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start); | |
5a3f23d5 | 8603 | |
bc0939fc | 8604 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
257c62e1 | 8605 | |
570eb97b | 8606 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
8318ba79 | 8607 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8608 | |
76de60ed YY |
8609 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8610 | sb_end_pagefault(inode->i_sb); | |
8611 | extent_changeset_free(data_reserved); | |
8612 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8613 | |
8614 | out_unlock: | |
9ebefb18 | 8615 | unlock_page(page); |
8318ba79 | 8616 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
1832a6d5 | 8617 | out: |
8702ba93 | 8618 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8619 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8620 | reserved_space, (ret != 0)); |
9998eb70 | 8621 | out_noreserve: |
b2b5ef5c | 8622 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8623 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8624 | return ret; |
8625 | } | |
8626 | ||
213e8c55 | 8627 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8628 | { |
d9ac19c3 | 8629 | struct btrfs_truncate_control control = { |
71d18b53 | 8630 | .inode = BTRFS_I(inode), |
487e81d2 | 8631 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 | 8632 | .min_type = BTRFS_EXTENT_DATA_KEY, |
655807b8 | 8633 | .clear_extent_range = true, |
d9ac19c3 | 8634 | }; |
0b246afa | 8635 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8636 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8637 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8638 | int ret; |
39279cc3 | 8639 | struct btrfs_trans_handle *trans; |
0b246afa | 8640 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 8641 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8642 | |
213e8c55 FM |
8643 | if (!skip_writeback) { |
8644 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8645 | (u64)-1); | |
8646 | if (ret) | |
8647 | return ret; | |
8648 | } | |
39279cc3 | 8649 | |
fcb80c2a | 8650 | /* |
f7e9e8fc OS |
8651 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8652 | * things going on here: | |
fcb80c2a | 8653 | * |
f7e9e8fc | 8654 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8655 | * |
f7e9e8fc | 8656 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8657 | * be free'd up by the truncate operation, but also have some slack |
8658 | * space reserved in case it uses space during the truncate (thank you | |
8659 | * very much snapshotting). | |
8660 | * | |
f7e9e8fc | 8661 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8662 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8663 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8664 | * doesn't end up using space reserved for updating the inode. We also |
8665 | * need to be able to stop the transaction and start a new one, which | |
8666 | * means we need to be able to update the inode several times, and we | |
8667 | * have no idea of knowing how many times that will be, so we can't just | |
8668 | * reserve 1 item for the entirety of the operation, so that has to be | |
8669 | * done separately as well. | |
fcb80c2a JB |
8670 | * |
8671 | * So that leaves us with | |
8672 | * | |
f7e9e8fc | 8673 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8674 | * transaction reservation. |
f7e9e8fc | 8675 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8676 | * updating the inode. |
8677 | */ | |
2ff7e61e | 8678 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8679 | if (!rsv) |
8680 | return -ENOMEM; | |
4a338542 | 8681 | rsv->size = min_size; |
710d5921 | 8682 | rsv->failfast = true; |
f0cd846e | 8683 | |
907cbceb | 8684 | /* |
07127184 | 8685 | * 1 for the truncate slack space |
907cbceb JB |
8686 | * 1 for updating the inode. |
8687 | */ | |
f3fe820c | 8688 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8689 | if (IS_ERR(trans)) { |
ad7e1a74 | 8690 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8691 | goto out; |
8692 | } | |
f0cd846e | 8693 | |
907cbceb | 8694 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8695 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8696 | min_size, false); |
fcb80c2a | 8697 | BUG_ON(ret); |
f0cd846e | 8698 | |
ca7e70f5 | 8699 | trans->block_rsv = rsv; |
907cbceb | 8700 | |
8082510e | 8701 | while (1) { |
9a4a1429 JB |
8702 | struct extent_state *cached_state = NULL; |
8703 | const u64 new_size = inode->i_size; | |
8704 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); | |
8705 | ||
d9ac19c3 | 8706 | control.new_size = new_size; |
570eb97b | 8707 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
9a4a1429 JB |
8708 | &cached_state); |
8709 | /* | |
8710 | * We want to drop from the next block forward in case this new | |
8711 | * size is not block aligned since we will be keeping the last | |
8712 | * block of the extent just the way it is. | |
8713 | */ | |
4c0c8cfc FM |
8714 | btrfs_drop_extent_map_range(BTRFS_I(inode), |
8715 | ALIGN(new_size, fs_info->sectorsize), | |
8716 | (u64)-1, false); | |
9a4a1429 | 8717 | |
71d18b53 | 8718 | ret = btrfs_truncate_inode_items(trans, root, &control); |
c2ddb612 | 8719 | |
462b728e | 8720 | inode_sub_bytes(inode, control.sub_bytes); |
c2ddb612 JB |
8721 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size); |
8722 | ||
570eb97b JB |
8723 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
8724 | &cached_state); | |
9a4a1429 | 8725 | |
ddfae63c | 8726 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8727 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8728 | break; |
39279cc3 | 8729 | |
9a56fcd1 | 8730 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
ad7e1a74 | 8731 | if (ret) |
3893e33b | 8732 | break; |
ca7e70f5 | 8733 | |
3a45bb20 | 8734 | btrfs_end_transaction(trans); |
2ff7e61e | 8735 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8736 | |
8737 | trans = btrfs_start_transaction(root, 2); | |
8738 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8739 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8740 | trans = NULL; |
8741 | break; | |
8742 | } | |
8743 | ||
63f018be | 8744 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8745 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8746 | rsv, min_size, false); |
ca7e70f5 JB |
8747 | BUG_ON(ret); /* shouldn't happen */ |
8748 | trans->block_rsv = rsv; | |
8082510e YZ |
8749 | } |
8750 | ||
ddfae63c JB |
8751 | /* |
8752 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
54f03ab1 JB |
8753 | * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we |
8754 | * know we've truncated everything except the last little bit, and can | |
8755 | * do btrfs_truncate_block and then update the disk_i_size. | |
ddfae63c | 8756 | */ |
54f03ab1 | 8757 | if (ret == BTRFS_NEED_TRUNCATE_BLOCK) { |
ddfae63c JB |
8758 | btrfs_end_transaction(trans); |
8759 | btrfs_btree_balance_dirty(fs_info); | |
8760 | ||
217f42eb | 8761 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
ddfae63c JB |
8762 | if (ret) |
8763 | goto out; | |
8764 | trans = btrfs_start_transaction(root, 1); | |
8765 | if (IS_ERR(trans)) { | |
8766 | ret = PTR_ERR(trans); | |
8767 | goto out; | |
8768 | } | |
76aea537 | 8769 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
ddfae63c JB |
8770 | } |
8771 | ||
917c16b2 | 8772 | if (trans) { |
ad7e1a74 OS |
8773 | int ret2; |
8774 | ||
0b246afa | 8775 | trans->block_rsv = &fs_info->trans_block_rsv; |
9a56fcd1 | 8776 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
ad7e1a74 OS |
8777 | if (ret2 && !ret) |
8778 | ret = ret2; | |
7b128766 | 8779 | |
ad7e1a74 OS |
8780 | ret2 = btrfs_end_transaction(trans); |
8781 | if (ret2 && !ret) | |
8782 | ret = ret2; | |
2ff7e61e | 8783 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8784 | } |
fcb80c2a | 8785 | out: |
2ff7e61e | 8786 | btrfs_free_block_rsv(fs_info, rsv); |
0d7d3165 FM |
8787 | /* |
8788 | * So if we truncate and then write and fsync we normally would just | |
8789 | * write the extents that changed, which is a problem if we need to | |
8790 | * first truncate that entire inode. So set this flag so we write out | |
8791 | * all of the extents in the inode to the sync log so we're completely | |
8792 | * safe. | |
8793 | * | |
8794 | * If no extents were dropped or trimmed we don't need to force the next | |
8795 | * fsync to truncate all the inode's items from the log and re-log them | |
8796 | * all. This means the truncate operation did not change the file size, | |
8797 | * or changed it to a smaller size but there was only an implicit hole | |
8798 | * between the old i_size and the new i_size, and there were no prealloc | |
8799 | * extents beyond i_size to drop. | |
8800 | */ | |
d9ac19c3 | 8801 | if (control.extents_found > 0) |
23e3337f | 8802 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
fcb80c2a | 8803 | |
ad7e1a74 | 8804 | return ret; |
39279cc3 CM |
8805 | } |
8806 | ||
a1fd0c35 OS |
8807 | struct inode *btrfs_new_subvol_inode(struct user_namespace *mnt_userns, |
8808 | struct inode *dir) | |
8809 | { | |
8810 | struct inode *inode; | |
8811 | ||
8812 | inode = new_inode(dir->i_sb); | |
8813 | if (inode) { | |
8814 | /* | |
8815 | * Subvolumes don't inherit the sgid bit or the parent's gid if | |
8816 | * the parent's sgid bit is set. This is probably a bug. | |
8817 | */ | |
8818 | inode_init_owner(mnt_userns, inode, NULL, | |
8819 | S_IFDIR | (~current_umask() & S_IRWXUGO)); | |
8820 | inode->i_op = &btrfs_dir_inode_operations; | |
8821 | inode->i_fop = &btrfs_dir_file_operations; | |
8822 | } | |
8823 | return inode; | |
8824 | } | |
8825 | ||
39279cc3 CM |
8826 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8827 | { | |
69fe2d75 | 8828 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8829 | struct btrfs_inode *ei; |
2ead6ae7 | 8830 | struct inode *inode; |
39279cc3 | 8831 | |
fd60b288 | 8832 | ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
8833 | if (!ei) |
8834 | return NULL; | |
2ead6ae7 YZ |
8835 | |
8836 | ei->root = NULL; | |
2ead6ae7 | 8837 | ei->generation = 0; |
15ee9bc7 | 8838 | ei->last_trans = 0; |
257c62e1 | 8839 | ei->last_sub_trans = 0; |
e02119d5 | 8840 | ei->logged_trans = 0; |
2ead6ae7 | 8841 | ei->delalloc_bytes = 0; |
a7e3b975 | 8842 | ei->new_delalloc_bytes = 0; |
47059d93 | 8843 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8844 | ei->disk_i_size = 0; |
8845 | ei->flags = 0; | |
77eea05e | 8846 | ei->ro_flags = 0; |
7709cde3 | 8847 | ei->csum_bytes = 0; |
2ead6ae7 | 8848 | ei->index_cnt = (u64)-1; |
67de1176 | 8849 | ei->dir_index = 0; |
2ead6ae7 | 8850 | ei->last_unlink_trans = 0; |
3ebac17c | 8851 | ei->last_reflink_trans = 0; |
46d8bc34 | 8852 | ei->last_log_commit = 0; |
2ead6ae7 | 8853 | |
9e0baf60 | 8854 | spin_lock_init(&ei->lock); |
87c11705 | 8855 | spin_lock_init(&ei->io_failure_lock); |
9e0baf60 | 8856 | ei->outstanding_extents = 0; |
69fe2d75 JB |
8857 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8858 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8859 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8860 | ei->runtime_flags = 0; |
b52aa8c9 | 8861 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8862 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8863 | |
16cdcec7 MX |
8864 | ei->delayed_node = NULL; |
8865 | ||
9cc97d64 | 8866 | ei->i_otime.tv_sec = 0; |
8867 | ei->i_otime.tv_nsec = 0; | |
8868 | ||
2ead6ae7 | 8869 | inode = &ei->vfs_inode; |
a8067e02 | 8870 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 | 8871 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
41a2ee75 | 8872 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
efb0645b | 8873 | IO_TREE_INODE_FILE_EXTENT, NULL); |
87c11705 | 8874 | ei->io_failure_tree = RB_ROOT; |
b812ce28 | 8875 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8876 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 8877 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8878 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8879 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8880 | RB_CLEAR_NODE(&ei->rb_node); |
8318ba79 | 8881 | init_rwsem(&ei->i_mmap_lock); |
2ead6ae7 YZ |
8882 | |
8883 | return inode; | |
39279cc3 CM |
8884 | } |
8885 | ||
aaedb55b JB |
8886 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8887 | void btrfs_test_destroy_inode(struct inode *inode) | |
8888 | { | |
4c0c8cfc | 8889 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
aaedb55b JB |
8890 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8891 | } | |
8892 | #endif | |
8893 | ||
26602cab | 8894 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8895 | { |
fa0d7e3d NP |
8896 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8897 | } | |
8898 | ||
633cc816 | 8899 | void btrfs_destroy_inode(struct inode *vfs_inode) |
39279cc3 | 8900 | { |
e6dcd2dc | 8901 | struct btrfs_ordered_extent *ordered; |
633cc816 NB |
8902 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
8903 | struct btrfs_root *root = inode->root; | |
5f4403e1 | 8904 | bool freespace_inode; |
5a3f23d5 | 8905 | |
633cc816 NB |
8906 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
8907 | WARN_ON(vfs_inode->i_data.nrpages); | |
8908 | WARN_ON(inode->block_rsv.reserved); | |
8909 | WARN_ON(inode->block_rsv.size); | |
8910 | WARN_ON(inode->outstanding_extents); | |
dc287224 FM |
8911 | if (!S_ISDIR(vfs_inode->i_mode)) { |
8912 | WARN_ON(inode->delalloc_bytes); | |
8913 | WARN_ON(inode->new_delalloc_bytes); | |
8914 | } | |
633cc816 NB |
8915 | WARN_ON(inode->csum_bytes); |
8916 | WARN_ON(inode->defrag_bytes); | |
39279cc3 | 8917 | |
a6dbd429 JB |
8918 | /* |
8919 | * This can happen where we create an inode, but somebody else also | |
8920 | * created the same inode and we need to destroy the one we already | |
8921 | * created. | |
8922 | */ | |
8923 | if (!root) | |
26602cab | 8924 | return; |
a6dbd429 | 8925 | |
5f4403e1 IA |
8926 | /* |
8927 | * If this is a free space inode do not take the ordered extents lockdep | |
8928 | * map. | |
8929 | */ | |
8930 | freespace_inode = btrfs_is_free_space_inode(inode); | |
8931 | ||
d397712b | 8932 | while (1) { |
633cc816 | 8933 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
e6dcd2dc CM |
8934 | if (!ordered) |
8935 | break; | |
8936 | else { | |
633cc816 | 8937 | btrfs_err(root->fs_info, |
5d163e0e | 8938 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8939 | ordered->file_offset, ordered->num_bytes); |
5f4403e1 IA |
8940 | |
8941 | if (!freespace_inode) | |
8942 | btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent); | |
8943 | ||
71fe0a55 | 8944 | btrfs_remove_ordered_extent(inode, ordered); |
e6dcd2dc CM |
8945 | btrfs_put_ordered_extent(ordered); |
8946 | btrfs_put_ordered_extent(ordered); | |
8947 | } | |
8948 | } | |
633cc816 NB |
8949 | btrfs_qgroup_check_reserved_leak(inode); |
8950 | inode_tree_del(inode); | |
4c0c8cfc | 8951 | btrfs_drop_extent_map_range(inode, 0, (u64)-1, false); |
633cc816 NB |
8952 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
8953 | btrfs_put_root(inode->root); | |
39279cc3 CM |
8954 | } |
8955 | ||
45321ac5 | 8956 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8957 | { |
8958 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8959 | |
6379ef9f NA |
8960 | if (root == NULL) |
8961 | return 1; | |
8962 | ||
fa6ac876 | 8963 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8964 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8965 | return 1; |
76dda93c | 8966 | else |
45321ac5 | 8967 | return generic_drop_inode(inode); |
76dda93c YZ |
8968 | } |
8969 | ||
0ee0fda0 | 8970 | static void init_once(void *foo) |
39279cc3 | 8971 | { |
0d031dc4 | 8972 | struct btrfs_inode *ei = foo; |
39279cc3 CM |
8973 | |
8974 | inode_init_once(&ei->vfs_inode); | |
8975 | } | |
8976 | ||
e67c718b | 8977 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8978 | { |
8c0a8537 KS |
8979 | /* |
8980 | * Make sure all delayed rcu free inodes are flushed before we | |
8981 | * destroy cache. | |
8982 | */ | |
8983 | rcu_barrier(); | |
642c5d34 | 8984 | bioset_exit(&btrfs_dio_bioset); |
5598e900 | 8985 | kmem_cache_destroy(btrfs_inode_cachep); |
39279cc3 CM |
8986 | } |
8987 | ||
f5c29bd9 | 8988 | int __init btrfs_init_cachep(void) |
39279cc3 | 8989 | { |
837e1972 | 8990 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8991 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8992 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8993 | init_once); | |
39279cc3 CM |
8994 | if (!btrfs_inode_cachep) |
8995 | goto fail; | |
9601e3f6 | 8996 | |
642c5d34 CH |
8997 | if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE, |
8998 | offsetof(struct btrfs_dio_private, bio), | |
8999 | BIOSET_NEED_BVECS)) | |
9000 | goto fail; | |
9001 | ||
39279cc3 CM |
9002 | return 0; |
9003 | fail: | |
9004 | btrfs_destroy_cachep(); | |
9005 | return -ENOMEM; | |
9006 | } | |
9007 | ||
549c7297 CB |
9008 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
9009 | const struct path *path, struct kstat *stat, | |
a528d35e | 9010 | u32 request_mask, unsigned int flags) |
39279cc3 | 9011 | { |
df0af1a5 | 9012 | u64 delalloc_bytes; |
2766ff61 | 9013 | u64 inode_bytes; |
a528d35e | 9014 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9015 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 | 9016 | u32 bi_flags = BTRFS_I(inode)->flags; |
14605409 | 9017 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
04a87e34 YS |
9018 | |
9019 | stat->result_mask |= STATX_BTIME; | |
9020 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9021 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9022 | if (bi_flags & BTRFS_INODE_APPEND) | |
9023 | stat->attributes |= STATX_ATTR_APPEND; | |
9024 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9025 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9026 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9027 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9028 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9029 | stat->attributes |= STATX_ATTR_NODUMP; | |
14605409 BB |
9030 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
9031 | stat->attributes |= STATX_ATTR_VERITY; | |
04a87e34 YS |
9032 | |
9033 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9034 | STATX_ATTR_COMPRESSED | | |
9035 | STATX_ATTR_IMMUTABLE | | |
9036 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9037 | |
c020d2ea | 9038 | generic_fillattr(mnt_userns, inode, stat); |
0ee5dc67 | 9039 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9040 | |
9041 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9042 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
2766ff61 | 9043 | inode_bytes = inode_get_bytes(inode); |
df0af1a5 | 9044 | spin_unlock(&BTRFS_I(inode)->lock); |
2766ff61 | 9045 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
df0af1a5 | 9046 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9047 | return 0; |
9048 | } | |
9049 | ||
cdd1fedf DF |
9050 | static int btrfs_rename_exchange(struct inode *old_dir, |
9051 | struct dentry *old_dentry, | |
9052 | struct inode *new_dir, | |
9053 | struct dentry *new_dentry) | |
9054 | { | |
0b246afa | 9055 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf | 9056 | struct btrfs_trans_handle *trans; |
c1621871 | 9057 | unsigned int trans_num_items; |
cdd1fedf DF |
9058 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
9059 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9060 | struct inode *new_inode = new_dentry->d_inode; | |
9061 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 9062 | struct timespec64 ctime = current_time(old_inode); |
88d2beec FM |
9063 | struct btrfs_rename_ctx old_rename_ctx; |
9064 | struct btrfs_rename_ctx new_rename_ctx; | |
4a0cc7ca NB |
9065 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9066 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9067 | u64 old_idx = 0; |
9068 | u64 new_idx = 0; | |
cdd1fedf | 9069 | int ret; |
75b463d2 | 9070 | int ret2; |
dc09ef35 | 9071 | bool need_abort = false; |
ab3c5c18 | 9072 | struct fscrypt_name old_fname, new_fname; |
6db75318 | 9073 | struct fscrypt_str *old_name, *new_name; |
cdd1fedf | 9074 | |
3f79f6f6 N |
9075 | /* |
9076 | * For non-subvolumes allow exchange only within one subvolume, in the | |
9077 | * same inode namespace. Two subvolumes (represented as directory) can | |
9078 | * be exchanged as they're a logical link and have a fixed inode number. | |
9079 | */ | |
9080 | if (root != dest && | |
9081 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || | |
9082 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) | |
cdd1fedf DF |
9083 | return -EXDEV; |
9084 | ||
ab3c5c18 STD |
9085 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
9086 | if (ret) | |
9087 | return ret; | |
9088 | ||
9089 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); | |
9090 | if (ret) { | |
9091 | fscrypt_free_filename(&old_fname); | |
9092 | return ret; | |
9093 | } | |
9094 | ||
6db75318 STD |
9095 | old_name = &old_fname.disk_name; |
9096 | new_name = &new_fname.disk_name; | |
ab3c5c18 | 9097 | |
cdd1fedf | 9098 | /* close the race window with snapshot create/destroy ioctl */ |
943eb3bf JB |
9099 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
9100 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9101 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9102 | |
9103 | /* | |
c1621871 OS |
9104 | * For each inode: |
9105 | * 1 to remove old dir item | |
9106 | * 1 to remove old dir index | |
9107 | * 1 to add new dir item | |
9108 | * 1 to add new dir index | |
9109 | * 1 to update parent inode | |
9110 | * | |
9111 | * If the parents are the same, we only need to account for one | |
cdd1fedf | 9112 | */ |
c1621871 OS |
9113 | trans_num_items = (old_dir == new_dir ? 9 : 10); |
9114 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9115 | /* | |
9116 | * 1 to remove old root ref | |
9117 | * 1 to remove old root backref | |
9118 | * 1 to add new root ref | |
9119 | * 1 to add new root backref | |
9120 | */ | |
9121 | trans_num_items += 4; | |
9122 | } else { | |
9123 | /* | |
9124 | * 1 to update inode item | |
9125 | * 1 to remove old inode ref | |
9126 | * 1 to add new inode ref | |
9127 | */ | |
9128 | trans_num_items += 3; | |
9129 | } | |
9130 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9131 | trans_num_items += 4; | |
9132 | else | |
9133 | trans_num_items += 3; | |
9134 | trans = btrfs_start_transaction(root, trans_num_items); | |
cdd1fedf DF |
9135 | if (IS_ERR(trans)) { |
9136 | ret = PTR_ERR(trans); | |
9137 | goto out_notrans; | |
9138 | } | |
9139 | ||
00aa8e87 JB |
9140 | if (dest != root) { |
9141 | ret = btrfs_record_root_in_trans(trans, dest); | |
9142 | if (ret) | |
9143 | goto out_fail; | |
9144 | } | |
3e174099 | 9145 | |
cdd1fedf DF |
9146 | /* |
9147 | * We need to find a free sequence number both in the source and | |
9148 | * in the destination directory for the exchange. | |
9149 | */ | |
877574e2 | 9150 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9151 | if (ret) |
9152 | goto out_fail; | |
877574e2 | 9153 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9154 | if (ret) |
9155 | goto out_fail; | |
9156 | ||
9157 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9158 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9159 | ||
9160 | /* Reference for the source. */ | |
9161 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9162 | /* force full log commit if subvolume involved. */ | |
90787766 | 9163 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 9164 | } else { |
6db75318 | 9165 | ret = btrfs_insert_inode_ref(trans, dest, new_name, old_ino, |
f85b7379 DS |
9166 | btrfs_ino(BTRFS_I(new_dir)), |
9167 | old_idx); | |
cdd1fedf DF |
9168 | if (ret) |
9169 | goto out_fail; | |
dc09ef35 | 9170 | need_abort = true; |
cdd1fedf DF |
9171 | } |
9172 | ||
9173 | /* And now for the dest. */ | |
9174 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9175 | /* force full log commit if subvolume involved. */ | |
90787766 | 9176 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 9177 | } else { |
6db75318 | 9178 | ret = btrfs_insert_inode_ref(trans, root, old_name, new_ino, |
f85b7379 DS |
9179 | btrfs_ino(BTRFS_I(old_dir)), |
9180 | new_idx); | |
dc09ef35 JB |
9181 | if (ret) { |
9182 | if (need_abort) | |
9183 | btrfs_abort_transaction(trans, ret); | |
cdd1fedf | 9184 | goto out_fail; |
dc09ef35 | 9185 | } |
cdd1fedf DF |
9186 | } |
9187 | ||
9188 | /* Update inode version and ctime/mtime. */ | |
9189 | inode_inc_iversion(old_dir); | |
9190 | inode_inc_iversion(new_dir); | |
9191 | inode_inc_iversion(old_inode); | |
9192 | inode_inc_iversion(new_inode); | |
c1867eb3 DS |
9193 | old_dir->i_mtime = ctime; |
9194 | old_dir->i_ctime = ctime; | |
9195 | new_dir->i_mtime = ctime; | |
9196 | new_dir->i_ctime = ctime; | |
cdd1fedf DF |
9197 | old_inode->i_ctime = ctime; |
9198 | new_inode->i_ctime = ctime; | |
9199 | ||
9200 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9201 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9202 | BTRFS_I(old_inode), 1); | |
9203 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9204 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9205 | } |
9206 | ||
9207 | /* src is a subvolume */ | |
9208 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 9209 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
cdd1fedf | 9210 | } else { /* src is an inode */ |
4467af88 | 9211 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
4ec5934e | 9212 | BTRFS_I(old_dentry->d_inode), |
6db75318 | 9213 | old_name, &old_rename_ctx); |
cdd1fedf | 9214 | if (!ret) |
9a56fcd1 | 9215 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
cdd1fedf DF |
9216 | } |
9217 | if (ret) { | |
66642832 | 9218 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9219 | goto out_fail; |
9220 | } | |
9221 | ||
9222 | /* dest is a subvolume */ | |
9223 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 9224 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
cdd1fedf | 9225 | } else { /* dest is an inode */ |
4467af88 | 9226 | ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9227 | BTRFS_I(new_dentry->d_inode), |
6db75318 | 9228 | new_name, &new_rename_ctx); |
cdd1fedf | 9229 | if (!ret) |
9a56fcd1 | 9230 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
cdd1fedf DF |
9231 | } |
9232 | if (ret) { | |
66642832 | 9233 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9234 | goto out_fail; |
9235 | } | |
9236 | ||
db0a669f | 9237 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 9238 | new_name, 0, old_idx); |
cdd1fedf | 9239 | if (ret) { |
66642832 | 9240 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9241 | goto out_fail; |
9242 | } | |
9243 | ||
db0a669f | 9244 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
6db75318 | 9245 | old_name, 0, new_idx); |
cdd1fedf | 9246 | if (ret) { |
66642832 | 9247 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9248 | goto out_fail; |
9249 | } | |
9250 | ||
9251 | if (old_inode->i_nlink == 1) | |
9252 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9253 | if (new_inode->i_nlink == 1) | |
9254 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9255 | ||
259c4b96 FM |
9256 | /* |
9257 | * Now pin the logs of the roots. We do it to ensure that no other task | |
9258 | * can sync the logs while we are in progress with the rename, because | |
9259 | * that could result in an inconsistency in case any of the inodes that | |
9260 | * are part of this rename operation were logged before. | |
9261 | */ | |
9262 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
9263 | btrfs_pin_log_trans(root); | |
9264 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
9265 | btrfs_pin_log_trans(dest); | |
9266 | ||
9267 | /* Do the log updates for all inodes. */ | |
9268 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
d5f5bd54 | 9269 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 9270 | old_rename_ctx.index, new_dentry->d_parent); |
259c4b96 | 9271 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 9272 | btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir), |
88d2beec | 9273 | new_rename_ctx.index, old_dentry->d_parent); |
259c4b96 FM |
9274 | |
9275 | /* Now unpin the logs. */ | |
9276 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
9277 | btrfs_end_log_trans(root); | |
9278 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
cdd1fedf | 9279 | btrfs_end_log_trans(dest); |
cdd1fedf | 9280 | out_fail: |
75b463d2 FM |
9281 | ret2 = btrfs_end_transaction(trans); |
9282 | ret = ret ? ret : ret2; | |
cdd1fedf | 9283 | out_notrans: |
943eb3bf JB |
9284 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
9285 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9286 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9287 | |
ab3c5c18 STD |
9288 | fscrypt_free_filename(&new_fname); |
9289 | fscrypt_free_filename(&old_fname); | |
cdd1fedf DF |
9290 | return ret; |
9291 | } | |
9292 | ||
a1fd0c35 OS |
9293 | static struct inode *new_whiteout_inode(struct user_namespace *mnt_userns, |
9294 | struct inode *dir) | |
9295 | { | |
9296 | struct inode *inode; | |
9297 | ||
9298 | inode = new_inode(dir->i_sb); | |
9299 | if (inode) { | |
9300 | inode_init_owner(mnt_userns, inode, dir, | |
9301 | S_IFCHR | WHITEOUT_MODE); | |
9302 | inode->i_op = &btrfs_special_inode_operations; | |
9303 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
9304 | } | |
9305 | return inode; | |
9306 | } | |
9307 | ||
ca07274c CB |
9308 | static int btrfs_rename(struct user_namespace *mnt_userns, |
9309 | struct inode *old_dir, struct dentry *old_dentry, | |
9310 | struct inode *new_dir, struct dentry *new_dentry, | |
9311 | unsigned int flags) | |
39279cc3 | 9312 | { |
0b246afa | 9313 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
3538d68d OS |
9314 | struct btrfs_new_inode_args whiteout_args = { |
9315 | .dir = old_dir, | |
9316 | .dentry = old_dentry, | |
9317 | }; | |
39279cc3 | 9318 | struct btrfs_trans_handle *trans; |
5062af35 | 9319 | unsigned int trans_num_items; |
39279cc3 | 9320 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9321 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9322 | struct inode *new_inode = d_inode(new_dentry); |
9323 | struct inode *old_inode = d_inode(old_dentry); | |
88d2beec | 9324 | struct btrfs_rename_ctx rename_ctx; |
00e4e6b3 | 9325 | u64 index = 0; |
39279cc3 | 9326 | int ret; |
75b463d2 | 9327 | int ret2; |
4a0cc7ca | 9328 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
ab3c5c18 | 9329 | struct fscrypt_name old_fname, new_fname; |
39279cc3 | 9330 | |
4a0cc7ca | 9331 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9332 | return -EPERM; |
9333 | ||
4df27c4d | 9334 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9335 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9336 | return -EXDEV; |
9337 | ||
33345d01 | 9338 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9339 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9340 | return -ENOTEMPTY; |
5f39d397 | 9341 | |
4df27c4d YZ |
9342 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9343 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9344 | return -ENOTEMPTY; | |
9c52057c | 9345 | |
ab3c5c18 STD |
9346 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
9347 | if (ret) | |
9348 | return ret; | |
9349 | ||
9350 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); | |
9351 | if (ret) { | |
9352 | fscrypt_free_filename(&old_fname); | |
9353 | return ret; | |
9354 | } | |
9355 | ||
9c52057c | 9356 | /* check for collisions, even if the name isn't there */ |
6db75318 | 9357 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name); |
9c52057c CM |
9358 | if (ret) { |
9359 | if (ret == -EEXIST) { | |
9360 | /* we shouldn't get | |
9361 | * eexist without a new_inode */ | |
fae7f21c | 9362 | if (WARN_ON(!new_inode)) { |
ab3c5c18 | 9363 | goto out_fscrypt_names; |
9c52057c CM |
9364 | } |
9365 | } else { | |
9366 | /* maybe -EOVERFLOW */ | |
ab3c5c18 | 9367 | goto out_fscrypt_names; |
9c52057c CM |
9368 | } |
9369 | } | |
9370 | ret = 0; | |
9371 | ||
5a3f23d5 | 9372 | /* |
8d875f95 CM |
9373 | * we're using rename to replace one file with another. Start IO on it |
9374 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9375 | */ |
8d875f95 | 9376 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9377 | filemap_flush(old_inode->i_mapping); |
9378 | ||
a1fd0c35 | 9379 | if (flags & RENAME_WHITEOUT) { |
3538d68d OS |
9380 | whiteout_args.inode = new_whiteout_inode(mnt_userns, old_dir); |
9381 | if (!whiteout_args.inode) | |
a1fd0c35 | 9382 | return -ENOMEM; |
3538d68d OS |
9383 | ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items); |
9384 | if (ret) | |
9385 | goto out_whiteout_inode; | |
9386 | } else { | |
9387 | /* 1 to update the old parent inode. */ | |
9388 | trans_num_items = 1; | |
a1fd0c35 OS |
9389 | } |
9390 | ||
c1621871 OS |
9391 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
9392 | /* Close the race window with snapshot create/destroy ioctl */ | |
0b246afa | 9393 | down_read(&fs_info->subvol_sem); |
c1621871 OS |
9394 | /* |
9395 | * 1 to remove old root ref | |
9396 | * 1 to remove old root backref | |
9397 | * 1 to add new root ref | |
9398 | * 1 to add new root backref | |
9399 | */ | |
3538d68d | 9400 | trans_num_items += 4; |
c1621871 OS |
9401 | } else { |
9402 | /* | |
9403 | * 1 to update inode | |
9404 | * 1 to remove old inode ref | |
9405 | * 1 to add new inode ref | |
9406 | */ | |
3538d68d | 9407 | trans_num_items += 3; |
c1621871 | 9408 | } |
a22285a6 | 9409 | /* |
c1621871 OS |
9410 | * 1 to remove old dir item |
9411 | * 1 to remove old dir index | |
c1621871 OS |
9412 | * 1 to add new dir item |
9413 | * 1 to add new dir index | |
a22285a6 | 9414 | */ |
3538d68d OS |
9415 | trans_num_items += 4; |
9416 | /* 1 to update new parent inode if it's not the same as the old parent */ | |
c1621871 OS |
9417 | if (new_dir != old_dir) |
9418 | trans_num_items++; | |
9419 | if (new_inode) { | |
9420 | /* | |
9421 | * 1 to update inode | |
9422 | * 1 to remove inode ref | |
9423 | * 1 to remove dir item | |
9424 | * 1 to remove dir index | |
9425 | * 1 to possibly add orphan item | |
9426 | */ | |
9427 | trans_num_items += 5; | |
9428 | } | |
5062af35 | 9429 | trans = btrfs_start_transaction(root, trans_num_items); |
b44c59a8 | 9430 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9431 | ret = PTR_ERR(trans); |
9432 | goto out_notrans; | |
9433 | } | |
76dda93c | 9434 | |
b0fec6fd JB |
9435 | if (dest != root) { |
9436 | ret = btrfs_record_root_in_trans(trans, dest); | |
9437 | if (ret) | |
9438 | goto out_fail; | |
9439 | } | |
5f39d397 | 9440 | |
877574e2 | 9441 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9442 | if (ret) |
9443 | goto out_fail; | |
5a3f23d5 | 9444 | |
67de1176 | 9445 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9446 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9447 | /* force full log commit if subvolume involved. */ |
90787766 | 9448 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9449 | } else { |
6db75318 STD |
9450 | ret = btrfs_insert_inode_ref(trans, dest, &new_fname.disk_name, |
9451 | old_ino, btrfs_ino(BTRFS_I(new_dir)), | |
9452 | index); | |
a5719521 YZ |
9453 | if (ret) |
9454 | goto out_fail; | |
4df27c4d | 9455 | } |
5a3f23d5 | 9456 | |
0c4d2d95 JB |
9457 | inode_inc_iversion(old_dir); |
9458 | inode_inc_iversion(new_dir); | |
9459 | inode_inc_iversion(old_inode); | |
c1867eb3 DS |
9460 | old_dir->i_mtime = current_time(old_dir); |
9461 | old_dir->i_ctime = old_dir->i_mtime; | |
9462 | new_dir->i_mtime = old_dir->i_mtime; | |
9463 | new_dir->i_ctime = old_dir->i_mtime; | |
9464 | old_inode->i_ctime = old_dir->i_mtime; | |
5f39d397 | 9465 | |
12fcfd22 | 9466 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9467 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9468 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9469 | |
33345d01 | 9470 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
045d3967 | 9471 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
4df27c4d | 9472 | } else { |
4467af88 | 9473 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
e43eec81 | 9474 | BTRFS_I(d_inode(old_dentry)), |
6db75318 | 9475 | &old_fname.disk_name, &rename_ctx); |
92986796 | 9476 | if (!ret) |
9a56fcd1 | 9477 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
4df27c4d | 9478 | } |
79787eaa | 9479 | if (ret) { |
66642832 | 9480 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9481 | goto out_fail; |
9482 | } | |
39279cc3 CM |
9483 | |
9484 | if (new_inode) { | |
0c4d2d95 | 9485 | inode_inc_iversion(new_inode); |
c2050a45 | 9486 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9487 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9488 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 9489 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
4df27c4d YZ |
9490 | BUG_ON(new_inode->i_nlink == 0); |
9491 | } else { | |
4467af88 | 9492 | ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9493 | BTRFS_I(d_inode(new_dentry)), |
6db75318 | 9494 | &new_fname.disk_name); |
4df27c4d | 9495 | } |
4ef31a45 | 9496 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9497 | ret = btrfs_orphan_add(trans, |
9498 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9499 | if (ret) { |
66642832 | 9500 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9501 | goto out_fail; |
9502 | } | |
39279cc3 | 9503 | } |
aec7477b | 9504 | |
db0a669f | 9505 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 9506 | &new_fname.disk_name, 0, index); |
79787eaa | 9507 | if (ret) { |
66642832 | 9508 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9509 | goto out_fail; |
9510 | } | |
39279cc3 | 9511 | |
67de1176 MX |
9512 | if (old_inode->i_nlink == 1) |
9513 | BTRFS_I(old_inode)->dir_index = index; | |
9514 | ||
259c4b96 | 9515 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 9516 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 9517 | rename_ctx.index, new_dentry->d_parent); |
cdd1fedf DF |
9518 | |
9519 | if (flags & RENAME_WHITEOUT) { | |
caae78e0 | 9520 | ret = btrfs_create_new_inode(trans, &whiteout_args); |
cdd1fedf | 9521 | if (ret) { |
66642832 | 9522 | btrfs_abort_transaction(trans, ret); |
cdd1fedf | 9523 | goto out_fail; |
caae78e0 OS |
9524 | } else { |
9525 | unlock_new_inode(whiteout_args.inode); | |
9526 | iput(whiteout_args.inode); | |
9527 | whiteout_args.inode = NULL; | |
cdd1fedf | 9528 | } |
4df27c4d | 9529 | } |
39279cc3 | 9530 | out_fail: |
75b463d2 FM |
9531 | ret2 = btrfs_end_transaction(trans); |
9532 | ret = ret ? ret : ret2; | |
b44c59a8 | 9533 | out_notrans: |
33345d01 | 9534 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9535 | up_read(&fs_info->subvol_sem); |
a1fd0c35 | 9536 | if (flags & RENAME_WHITEOUT) |
3538d68d OS |
9537 | btrfs_new_inode_args_destroy(&whiteout_args); |
9538 | out_whiteout_inode: | |
9539 | if (flags & RENAME_WHITEOUT) | |
9540 | iput(whiteout_args.inode); | |
ab3c5c18 STD |
9541 | out_fscrypt_names: |
9542 | fscrypt_free_filename(&old_fname); | |
9543 | fscrypt_free_filename(&new_fname); | |
39279cc3 CM |
9544 | return ret; |
9545 | } | |
9546 | ||
549c7297 CB |
9547 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
9548 | struct dentry *old_dentry, struct inode *new_dir, | |
9549 | struct dentry *new_dentry, unsigned int flags) | |
80ace85c | 9550 | { |
ca6dee6b FM |
9551 | int ret; |
9552 | ||
cdd1fedf | 9553 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9554 | return -EINVAL; |
9555 | ||
cdd1fedf | 9556 | if (flags & RENAME_EXCHANGE) |
ca6dee6b FM |
9557 | ret = btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
9558 | new_dentry); | |
9559 | else | |
9560 | ret = btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir, | |
9561 | new_dentry, flags); | |
cdd1fedf | 9562 | |
ca6dee6b FM |
9563 | btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info); |
9564 | ||
9565 | return ret; | |
80ace85c MS |
9566 | } |
9567 | ||
3a2f8c07 NB |
9568 | struct btrfs_delalloc_work { |
9569 | struct inode *inode; | |
9570 | struct completion completion; | |
9571 | struct list_head list; | |
9572 | struct btrfs_work work; | |
9573 | }; | |
9574 | ||
8ccf6f19 MX |
9575 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9576 | { | |
9577 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9578 | struct inode *inode; |
8ccf6f19 MX |
9579 | |
9580 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9581 | work); | |
9f23e289 | 9582 | inode = delalloc_work->inode; |
30424601 DS |
9583 | filemap_flush(inode->i_mapping); |
9584 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9585 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9586 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9587 | |
076da91c | 9588 | iput(inode); |
8ccf6f19 MX |
9589 | complete(&delalloc_work->completion); |
9590 | } | |
9591 | ||
3a2f8c07 | 9592 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9593 | { |
9594 | struct btrfs_delalloc_work *work; | |
9595 | ||
100d5702 | 9596 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9597 | if (!work) |
9598 | return NULL; | |
9599 | ||
9600 | init_completion(&work->completion); | |
9601 | INIT_LIST_HEAD(&work->list); | |
9602 | work->inode = inode; | |
a0cac0ec | 9603 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
9604 | |
9605 | return work; | |
9606 | } | |
9607 | ||
d352ac68 CM |
9608 | /* |
9609 | * some fairly slow code that needs optimization. This walks the list | |
9610 | * of all the inodes with pending delalloc and forces them to disk. | |
9611 | */ | |
e076ab2a JB |
9612 | static int start_delalloc_inodes(struct btrfs_root *root, |
9613 | struct writeback_control *wbc, bool snapshot, | |
3d45f221 | 9614 | bool in_reclaim_context) |
ea8c2819 | 9615 | { |
ea8c2819 | 9616 | struct btrfs_inode *binode; |
5b21f2ed | 9617 | struct inode *inode; |
8ccf6f19 MX |
9618 | struct btrfs_delalloc_work *work, *next; |
9619 | struct list_head works; | |
1eafa6c7 | 9620 | struct list_head splice; |
8ccf6f19 | 9621 | int ret = 0; |
e076ab2a | 9622 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
ea8c2819 | 9623 | |
8ccf6f19 | 9624 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9625 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9626 | |
573bfb72 | 9627 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9628 | spin_lock(&root->delalloc_lock); |
9629 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9630 | while (!list_empty(&splice)) { |
9631 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9632 | delalloc_inodes); |
1eafa6c7 | 9633 | |
eb73c1b7 MX |
9634 | list_move_tail(&binode->delalloc_inodes, |
9635 | &root->delalloc_inodes); | |
3d45f221 FM |
9636 | |
9637 | if (in_reclaim_context && | |
9638 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) | |
9639 | continue; | |
9640 | ||
5b21f2ed | 9641 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9642 | if (!inode) { |
eb73c1b7 | 9643 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9644 | continue; |
df0af1a5 | 9645 | } |
eb73c1b7 | 9646 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9647 | |
3cd24c69 EL |
9648 | if (snapshot) |
9649 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9650 | &binode->runtime_flags); | |
e076ab2a JB |
9651 | if (full_flush) { |
9652 | work = btrfs_alloc_delalloc_work(inode); | |
9653 | if (!work) { | |
9654 | iput(inode); | |
9655 | ret = -ENOMEM; | |
9656 | goto out; | |
9657 | } | |
9658 | list_add_tail(&work->list, &works); | |
9659 | btrfs_queue_work(root->fs_info->flush_workers, | |
9660 | &work->work); | |
9661 | } else { | |
b3776305 | 9662 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
e076ab2a JB |
9663 | btrfs_add_delayed_iput(inode); |
9664 | if (ret || wbc->nr_to_write <= 0) | |
b4912139 JB |
9665 | goto out; |
9666 | } | |
5b21f2ed | 9667 | cond_resched(); |
eb73c1b7 | 9668 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9669 | } |
eb73c1b7 | 9670 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9671 | |
a1ecaabb | 9672 | out: |
eb73c1b7 MX |
9673 | list_for_each_entry_safe(work, next, &works, list) { |
9674 | list_del_init(&work->list); | |
40012f96 NB |
9675 | wait_for_completion(&work->completion); |
9676 | kfree(work); | |
eb73c1b7 MX |
9677 | } |
9678 | ||
81f1d390 | 9679 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9680 | spin_lock(&root->delalloc_lock); |
9681 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9682 | spin_unlock(&root->delalloc_lock); | |
9683 | } | |
573bfb72 | 9684 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9685 | return ret; |
9686 | } | |
1eafa6c7 | 9687 | |
f9baa501 | 9688 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context) |
eb73c1b7 | 9689 | { |
e076ab2a JB |
9690 | struct writeback_control wbc = { |
9691 | .nr_to_write = LONG_MAX, | |
9692 | .sync_mode = WB_SYNC_NONE, | |
9693 | .range_start = 0, | |
9694 | .range_end = LLONG_MAX, | |
9695 | }; | |
0b246afa | 9696 | struct btrfs_fs_info *fs_info = root->fs_info; |
1eafa6c7 | 9697 | |
84961539 | 9698 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9699 | return -EROFS; |
9700 | ||
f9baa501 | 9701 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
eb73c1b7 MX |
9702 | } |
9703 | ||
9db4dc24 | 9704 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr, |
3d45f221 | 9705 | bool in_reclaim_context) |
eb73c1b7 | 9706 | { |
e076ab2a | 9707 | struct writeback_control wbc = { |
9db4dc24 | 9708 | .nr_to_write = nr, |
e076ab2a JB |
9709 | .sync_mode = WB_SYNC_NONE, |
9710 | .range_start = 0, | |
9711 | .range_end = LLONG_MAX, | |
9712 | }; | |
eb73c1b7 MX |
9713 | struct btrfs_root *root; |
9714 | struct list_head splice; | |
9715 | int ret; | |
9716 | ||
84961539 | 9717 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9718 | return -EROFS; |
9719 | ||
9720 | INIT_LIST_HEAD(&splice); | |
9721 | ||
573bfb72 | 9722 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9723 | spin_lock(&fs_info->delalloc_root_lock); |
9724 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
d7830b71 | 9725 | while (!list_empty(&splice)) { |
e076ab2a JB |
9726 | /* |
9727 | * Reset nr_to_write here so we know that we're doing a full | |
9728 | * flush. | |
9729 | */ | |
9db4dc24 | 9730 | if (nr == LONG_MAX) |
e076ab2a JB |
9731 | wbc.nr_to_write = LONG_MAX; |
9732 | ||
eb73c1b7 MX |
9733 | root = list_first_entry(&splice, struct btrfs_root, |
9734 | delalloc_root); | |
00246528 | 9735 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9736 | BUG_ON(!root); |
9737 | list_move_tail(&root->delalloc_root, | |
9738 | &fs_info->delalloc_roots); | |
9739 | spin_unlock(&fs_info->delalloc_root_lock); | |
9740 | ||
e076ab2a | 9741 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
00246528 | 9742 | btrfs_put_root(root); |
e076ab2a | 9743 | if (ret < 0 || wbc.nr_to_write <= 0) |
eb73c1b7 | 9744 | goto out; |
eb73c1b7 | 9745 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9746 | } |
eb73c1b7 | 9747 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9748 | |
6c255e67 | 9749 | ret = 0; |
eb73c1b7 | 9750 | out: |
81f1d390 | 9751 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9752 | spin_lock(&fs_info->delalloc_root_lock); |
9753 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9754 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9755 | } |
573bfb72 | 9756 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9757 | return ret; |
ea8c2819 CM |
9758 | } |
9759 | ||
549c7297 CB |
9760 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
9761 | struct dentry *dentry, const char *symname) | |
39279cc3 | 9762 | { |
0b246afa | 9763 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9764 | struct btrfs_trans_handle *trans; |
9765 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9766 | struct btrfs_path *path; | |
9767 | struct btrfs_key key; | |
a1fd0c35 | 9768 | struct inode *inode; |
3538d68d OS |
9769 | struct btrfs_new_inode_args new_inode_args = { |
9770 | .dir = dir, | |
9771 | .dentry = dentry, | |
9772 | }; | |
9773 | unsigned int trans_num_items; | |
39279cc3 | 9774 | int err; |
39279cc3 CM |
9775 | int name_len; |
9776 | int datasize; | |
5f39d397 | 9777 | unsigned long ptr; |
39279cc3 | 9778 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9779 | struct extent_buffer *leaf; |
39279cc3 | 9780 | |
f06becc4 | 9781 | name_len = strlen(symname); |
0b246afa | 9782 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9783 | return -ENAMETOOLONG; |
1832a6d5 | 9784 | |
a1fd0c35 OS |
9785 | inode = new_inode(dir->i_sb); |
9786 | if (!inode) | |
9787 | return -ENOMEM; | |
9788 | inode_init_owner(mnt_userns, inode, dir, S_IFLNK | S_IRWXUGO); | |
9789 | inode->i_op = &btrfs_symlink_inode_operations; | |
9790 | inode_nohighmem(inode); | |
9791 | inode->i_mapping->a_ops = &btrfs_aops; | |
caae78e0 OS |
9792 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
9793 | inode_set_bytes(inode, name_len); | |
a1fd0c35 | 9794 | |
3538d68d OS |
9795 | new_inode_args.inode = inode; |
9796 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
9797 | if (err) |
9798 | goto out_inode; | |
3538d68d OS |
9799 | /* 1 additional item for the inline extent */ |
9800 | trans_num_items++; | |
9801 | ||
9802 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 9803 | if (IS_ERR(trans)) { |
3538d68d OS |
9804 | err = PTR_ERR(trans); |
9805 | goto out_new_inode_args; | |
a1fd0c35 | 9806 | } |
1832a6d5 | 9807 | |
caae78e0 | 9808 | err = btrfs_create_new_inode(trans, &new_inode_args); |
b0d5d10f | 9809 | if (err) |
caae78e0 | 9810 | goto out; |
ad19db71 | 9811 | |
39279cc3 | 9812 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9813 | if (!path) { |
9814 | err = -ENOMEM; | |
caae78e0 OS |
9815 | btrfs_abort_transaction(trans, err); |
9816 | discard_new_inode(inode); | |
9817 | inode = NULL; | |
9818 | goto out; | |
d8926bb3 | 9819 | } |
4a0cc7ca | 9820 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9821 | key.offset = 0; |
962a298f | 9822 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9823 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9824 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9825 | datasize); | |
54aa1f4d | 9826 | if (err) { |
caae78e0 | 9827 | btrfs_abort_transaction(trans, err); |
b0839166 | 9828 | btrfs_free_path(path); |
caae78e0 OS |
9829 | discard_new_inode(inode); |
9830 | inode = NULL; | |
9831 | goto out; | |
54aa1f4d | 9832 | } |
5f39d397 CM |
9833 | leaf = path->nodes[0]; |
9834 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9835 | struct btrfs_file_extent_item); | |
9836 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9837 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9838 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9839 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9840 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9841 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9842 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9843 | ||
39279cc3 | 9844 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9845 | write_extent_buffer(leaf, symname, ptr, name_len); |
9846 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9847 | btrfs_free_path(path); |
5f39d397 | 9848 | |
1e2e547a | 9849 | d_instantiate_new(dentry, inode); |
caae78e0 OS |
9850 | err = 0; |
9851 | out: | |
3a45bb20 | 9852 | btrfs_end_transaction(trans); |
2ff7e61e | 9853 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
9854 | out_new_inode_args: |
9855 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
9856 | out_inode: |
9857 | if (err) | |
9858 | iput(inode); | |
39279cc3 CM |
9859 | return err; |
9860 | } | |
16432985 | 9861 | |
8fccebfa FM |
9862 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
9863 | struct btrfs_trans_handle *trans_in, | |
90dffd0c NB |
9864 | struct btrfs_inode *inode, |
9865 | struct btrfs_key *ins, | |
203f44c5 QW |
9866 | u64 file_offset) |
9867 | { | |
9868 | struct btrfs_file_extent_item stack_fi; | |
bf385648 | 9869 | struct btrfs_replace_extent_info extent_info; |
8fccebfa FM |
9870 | struct btrfs_trans_handle *trans = trans_in; |
9871 | struct btrfs_path *path; | |
203f44c5 QW |
9872 | u64 start = ins->objectid; |
9873 | u64 len = ins->offset; | |
fbf48bb0 | 9874 | int qgroup_released; |
9729f10a | 9875 | int ret; |
203f44c5 QW |
9876 | |
9877 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9878 | ||
9879 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9880 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9881 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9882 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9883 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9884 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9885 | /* Encryption and other encoding is reserved and all 0 */ | |
9886 | ||
fbf48bb0 QW |
9887 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
9888 | if (qgroup_released < 0) | |
9889 | return ERR_PTR(qgroup_released); | |
8fccebfa FM |
9890 | |
9891 | if (trans) { | |
90dffd0c | 9892 | ret = insert_reserved_file_extent(trans, inode, |
2766ff61 | 9893 | file_offset, &stack_fi, |
fbf48bb0 | 9894 | true, qgroup_released); |
8fccebfa | 9895 | if (ret) |
a3ee79bd | 9896 | goto free_qgroup; |
8fccebfa FM |
9897 | return trans; |
9898 | } | |
9899 | ||
9900 | extent_info.disk_offset = start; | |
9901 | extent_info.disk_len = len; | |
9902 | extent_info.data_offset = 0; | |
9903 | extent_info.data_len = len; | |
9904 | extent_info.file_offset = file_offset; | |
9905 | extent_info.extent_buf = (char *)&stack_fi; | |
8fccebfa | 9906 | extent_info.is_new_extent = true; |
983d8209 | 9907 | extent_info.update_times = true; |
fbf48bb0 | 9908 | extent_info.qgroup_reserved = qgroup_released; |
8fccebfa FM |
9909 | extent_info.insertions = 0; |
9910 | ||
9911 | path = btrfs_alloc_path(); | |
a3ee79bd QW |
9912 | if (!path) { |
9913 | ret = -ENOMEM; | |
9914 | goto free_qgroup; | |
9915 | } | |
8fccebfa | 9916 | |
bfc78479 | 9917 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
8fccebfa FM |
9918 | file_offset + len - 1, &extent_info, |
9919 | &trans); | |
9920 | btrfs_free_path(path); | |
9921 | if (ret) | |
a3ee79bd | 9922 | goto free_qgroup; |
8fccebfa | 9923 | return trans; |
a3ee79bd QW |
9924 | |
9925 | free_qgroup: | |
9926 | /* | |
9927 | * We have released qgroup data range at the beginning of the function, | |
9928 | * and normally qgroup_released bytes will be freed when committing | |
9929 | * transaction. | |
9930 | * But if we error out early, we have to free what we have released | |
9931 | * or we leak qgroup data reservation. | |
9932 | */ | |
9933 | btrfs_qgroup_free_refroot(inode->root->fs_info, | |
9934 | inode->root->root_key.objectid, qgroup_released, | |
9935 | BTRFS_QGROUP_RSV_DATA); | |
9936 | return ERR_PTR(ret); | |
203f44c5 | 9937 | } |
8fccebfa | 9938 | |
0af3d00b JB |
9939 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9940 | u64 start, u64 num_bytes, u64 min_size, | |
9941 | loff_t actual_len, u64 *alloc_hint, | |
9942 | struct btrfs_trans_handle *trans) | |
d899e052 | 9943 | { |
0b246afa | 9944 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 | 9945 | struct extent_map *em; |
d899e052 YZ |
9946 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9947 | struct btrfs_key ins; | |
d899e052 | 9948 | u64 cur_offset = start; |
b778cf96 | 9949 | u64 clear_offset = start; |
55a61d1d | 9950 | u64 i_size; |
154ea289 | 9951 | u64 cur_bytes; |
0b670dc4 | 9952 | u64 last_alloc = (u64)-1; |
d899e052 | 9953 | int ret = 0; |
0af3d00b | 9954 | bool own_trans = true; |
18513091 | 9955 | u64 end = start + num_bytes - 1; |
d899e052 | 9956 | |
0af3d00b JB |
9957 | if (trans) |
9958 | own_trans = false; | |
d899e052 | 9959 | while (num_bytes > 0) { |
ee22184b | 9960 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9961 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9962 | /* |
9963 | * If we are severely fragmented we could end up with really | |
9964 | * small allocations, so if the allocator is returning small | |
9965 | * chunks lets make its job easier by only searching for those | |
9966 | * sized chunks. | |
9967 | */ | |
9968 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9969 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9970 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
8fccebfa | 9971 | if (ret) |
a22285a6 | 9972 | break; |
b778cf96 JB |
9973 | |
9974 | /* | |
9975 | * We've reserved this space, and thus converted it from | |
9976 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9977 | * from here on out we will only need to clear our reservation | |
9978 | * for the remaining unreserved area, so advance our | |
9979 | * clear_offset by our extent size. | |
9980 | */ | |
9981 | clear_offset += ins.offset; | |
5a303d5d | 9982 | |
0b670dc4 | 9983 | last_alloc = ins.offset; |
90dffd0c NB |
9984 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
9985 | &ins, cur_offset); | |
1afc708d FM |
9986 | /* |
9987 | * Now that we inserted the prealloc extent we can finally | |
9988 | * decrement the number of reservations in the block group. | |
9989 | * If we did it before, we could race with relocation and have | |
9990 | * relocation miss the reserved extent, making it fail later. | |
9991 | */ | |
9992 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
8fccebfa FM |
9993 | if (IS_ERR(trans)) { |
9994 | ret = PTR_ERR(trans); | |
2ff7e61e | 9995 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9996 | ins.offset, 0); |
79787eaa JM |
9997 | break; |
9998 | } | |
31193213 | 9999 | |
5dc562c5 JB |
10000 | em = alloc_extent_map(); |
10001 | if (!em) { | |
a1ba4c08 FM |
10002 | btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset, |
10003 | cur_offset + ins.offset - 1, false); | |
23e3337f | 10004 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 JB |
10005 | goto next; |
10006 | } | |
10007 | ||
10008 | em->start = cur_offset; | |
10009 | em->orig_start = cur_offset; | |
10010 | em->len = ins.offset; | |
10011 | em->block_start = ins.objectid; | |
10012 | em->block_len = ins.offset; | |
b4939680 | 10013 | em->orig_block_len = ins.offset; |
cc95bef6 | 10014 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
10015 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10016 | em->generation = trans->transid; | |
10017 | ||
a1ba4c08 | 10018 | ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true); |
5dc562c5 JB |
10019 | free_extent_map(em); |
10020 | next: | |
d899e052 YZ |
10021 | num_bytes -= ins.offset; |
10022 | cur_offset += ins.offset; | |
efa56464 | 10023 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10024 | |
0c4d2d95 | 10025 | inode_inc_iversion(inode); |
c2050a45 | 10026 | inode->i_ctime = current_time(inode); |
6cbff00f | 10027 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10028 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10029 | (actual_len > inode->i_size) && |
10030 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10031 | if (cur_offset > actual_len) |
55a61d1d | 10032 | i_size = actual_len; |
d1ea6a61 | 10033 | else |
55a61d1d JB |
10034 | i_size = cur_offset; |
10035 | i_size_write(inode, i_size); | |
76aea537 | 10036 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
5a303d5d YZ |
10037 | } |
10038 | ||
9a56fcd1 | 10039 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
79787eaa JM |
10040 | |
10041 | if (ret) { | |
66642832 | 10042 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10043 | if (own_trans) |
3a45bb20 | 10044 | btrfs_end_transaction(trans); |
79787eaa JM |
10045 | break; |
10046 | } | |
d899e052 | 10047 | |
8fccebfa | 10048 | if (own_trans) { |
3a45bb20 | 10049 | btrfs_end_transaction(trans); |
8fccebfa FM |
10050 | trans = NULL; |
10051 | } | |
5a303d5d | 10052 | } |
b778cf96 | 10053 | if (clear_offset < end) |
25ce28ca | 10054 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 10055 | end - clear_offset + 1); |
d899e052 YZ |
10056 | return ret; |
10057 | } | |
10058 | ||
0af3d00b JB |
10059 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10060 | u64 start, u64 num_bytes, u64 min_size, | |
10061 | loff_t actual_len, u64 *alloc_hint) | |
10062 | { | |
10063 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10064 | min_size, actual_len, alloc_hint, | |
10065 | NULL); | |
10066 | } | |
10067 | ||
10068 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10069 | struct btrfs_trans_handle *trans, int mode, | |
10070 | u64 start, u64 num_bytes, u64 min_size, | |
10071 | loff_t actual_len, u64 *alloc_hint) | |
10072 | { | |
10073 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10074 | min_size, actual_len, alloc_hint, trans); | |
10075 | } | |
10076 | ||
549c7297 CB |
10077 | static int btrfs_permission(struct user_namespace *mnt_userns, |
10078 | struct inode *inode, int mask) | |
fdebe2bd | 10079 | { |
b83cc969 | 10080 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10081 | umode_t mode = inode->i_mode; |
b83cc969 | 10082 | |
cb6db4e5 JM |
10083 | if (mask & MAY_WRITE && |
10084 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10085 | if (btrfs_root_readonly(root)) | |
10086 | return -EROFS; | |
10087 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10088 | return -EACCES; | |
10089 | } | |
3bc71ba0 | 10090 | return generic_permission(mnt_userns, inode, mask); |
fdebe2bd | 10091 | } |
39279cc3 | 10092 | |
549c7297 | 10093 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
863f144f | 10094 | struct file *file, umode_t mode) |
ef3b9af5 | 10095 | { |
2ff7e61e | 10096 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10097 | struct btrfs_trans_handle *trans; |
10098 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
a1fd0c35 | 10099 | struct inode *inode; |
3538d68d OS |
10100 | struct btrfs_new_inode_args new_inode_args = { |
10101 | .dir = dir, | |
863f144f | 10102 | .dentry = file->f_path.dentry, |
3538d68d OS |
10103 | .orphan = true, |
10104 | }; | |
10105 | unsigned int trans_num_items; | |
a1fd0c35 OS |
10106 | int ret; |
10107 | ||
10108 | inode = new_inode(dir->i_sb); | |
10109 | if (!inode) | |
10110 | return -ENOMEM; | |
10111 | inode_init_owner(mnt_userns, inode, dir, mode); | |
10112 | inode->i_fop = &btrfs_file_operations; | |
10113 | inode->i_op = &btrfs_file_inode_operations; | |
10114 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 | 10115 | |
3538d68d OS |
10116 | new_inode_args.inode = inode; |
10117 | ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
10118 | if (ret) |
10119 | goto out_inode; | |
3538d68d OS |
10120 | |
10121 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 10122 | if (IS_ERR(trans)) { |
3538d68d OS |
10123 | ret = PTR_ERR(trans); |
10124 | goto out_new_inode_args; | |
a1fd0c35 | 10125 | } |
ef3b9af5 | 10126 | |
caae78e0 | 10127 | ret = btrfs_create_new_inode(trans, &new_inode_args); |
ef3b9af5 | 10128 | |
5762b5c9 | 10129 | /* |
3538d68d OS |
10130 | * We set number of links to 0 in btrfs_create_new_inode(), and here we |
10131 | * set it to 1 because d_tmpfile() will issue a warning if the count is | |
10132 | * 0, through: | |
5762b5c9 FM |
10133 | * |
10134 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10135 | */ | |
10136 | set_nlink(inode, 1); | |
caae78e0 OS |
10137 | |
10138 | if (!ret) { | |
863f144f | 10139 | d_tmpfile(file, inode); |
caae78e0 OS |
10140 | unlock_new_inode(inode); |
10141 | mark_inode_dirty(inode); | |
10142 | } | |
10143 | ||
3a45bb20 | 10144 | btrfs_end_transaction(trans); |
2ff7e61e | 10145 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
10146 | out_new_inode_args: |
10147 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
10148 | out_inode: |
10149 | if (ret) | |
10150 | iput(inode); | |
863f144f | 10151 | return finish_open_simple(file, ret); |
ef3b9af5 FM |
10152 | } |
10153 | ||
d2a91064 | 10154 | void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) |
c6100a4b | 10155 | { |
d2a91064 | 10156 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
c6100a4b JB |
10157 | unsigned long index = start >> PAGE_SHIFT; |
10158 | unsigned long end_index = end >> PAGE_SHIFT; | |
10159 | struct page *page; | |
d2a91064 | 10160 | u32 len; |
c6100a4b | 10161 | |
d2a91064 QW |
10162 | ASSERT(end + 1 - start <= U32_MAX); |
10163 | len = end + 1 - start; | |
c6100a4b | 10164 | while (index <= end_index) { |
d2a91064 | 10165 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
c6100a4b | 10166 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
d2a91064 QW |
10167 | |
10168 | btrfs_page_set_writeback(fs_info, page, start, len); | |
c6100a4b JB |
10169 | put_page(page); |
10170 | index++; | |
10171 | } | |
10172 | } | |
10173 | ||
3ea4dc5b OS |
10174 | int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info, |
10175 | int compress_type) | |
1881fba8 OS |
10176 | { |
10177 | switch (compress_type) { | |
10178 | case BTRFS_COMPRESS_NONE: | |
10179 | return BTRFS_ENCODED_IO_COMPRESSION_NONE; | |
10180 | case BTRFS_COMPRESS_ZLIB: | |
10181 | return BTRFS_ENCODED_IO_COMPRESSION_ZLIB; | |
10182 | case BTRFS_COMPRESS_LZO: | |
10183 | /* | |
10184 | * The LZO format depends on the sector size. 64K is the maximum | |
10185 | * sector size that we support. | |
10186 | */ | |
10187 | if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K) | |
10188 | return -EINVAL; | |
10189 | return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + | |
10190 | (fs_info->sectorsize_bits - 12); | |
10191 | case BTRFS_COMPRESS_ZSTD: | |
10192 | return BTRFS_ENCODED_IO_COMPRESSION_ZSTD; | |
10193 | default: | |
10194 | return -EUCLEAN; | |
10195 | } | |
10196 | } | |
10197 | ||
10198 | static ssize_t btrfs_encoded_read_inline( | |
10199 | struct kiocb *iocb, | |
10200 | struct iov_iter *iter, u64 start, | |
10201 | u64 lockend, | |
10202 | struct extent_state **cached_state, | |
10203 | u64 extent_start, size_t count, | |
10204 | struct btrfs_ioctl_encoded_io_args *encoded, | |
10205 | bool *unlocked) | |
10206 | { | |
10207 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10208 | struct btrfs_root *root = inode->root; | |
10209 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10210 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10211 | struct btrfs_path *path; | |
10212 | struct extent_buffer *leaf; | |
10213 | struct btrfs_file_extent_item *item; | |
10214 | u64 ram_bytes; | |
10215 | unsigned long ptr; | |
10216 | void *tmp; | |
10217 | ssize_t ret; | |
10218 | ||
10219 | path = btrfs_alloc_path(); | |
10220 | if (!path) { | |
10221 | ret = -ENOMEM; | |
10222 | goto out; | |
10223 | } | |
10224 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), | |
10225 | extent_start, 0); | |
10226 | if (ret) { | |
10227 | if (ret > 0) { | |
10228 | /* The extent item disappeared? */ | |
10229 | ret = -EIO; | |
10230 | } | |
10231 | goto out; | |
10232 | } | |
10233 | leaf = path->nodes[0]; | |
10234 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
10235 | ||
10236 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); | |
10237 | ptr = btrfs_file_extent_inline_start(item); | |
10238 | ||
10239 | encoded->len = min_t(u64, extent_start + ram_bytes, | |
10240 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
10241 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
10242 | btrfs_file_extent_compression(leaf, item)); | |
10243 | if (ret < 0) | |
10244 | goto out; | |
10245 | encoded->compression = ret; | |
10246 | if (encoded->compression) { | |
10247 | size_t inline_size; | |
10248 | ||
10249 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
10250 | path->slots[0]); | |
10251 | if (inline_size > count) { | |
10252 | ret = -ENOBUFS; | |
10253 | goto out; | |
10254 | } | |
10255 | count = inline_size; | |
10256 | encoded->unencoded_len = ram_bytes; | |
10257 | encoded->unencoded_offset = iocb->ki_pos - extent_start; | |
10258 | } else { | |
10259 | count = min_t(u64, count, encoded->len); | |
10260 | encoded->len = count; | |
10261 | encoded->unencoded_len = count; | |
10262 | ptr += iocb->ki_pos - extent_start; | |
10263 | } | |
10264 | ||
10265 | tmp = kmalloc(count, GFP_NOFS); | |
10266 | if (!tmp) { | |
10267 | ret = -ENOMEM; | |
10268 | goto out; | |
10269 | } | |
10270 | read_extent_buffer(leaf, tmp, ptr, count); | |
10271 | btrfs_release_path(path); | |
570eb97b | 10272 | unlock_extent(io_tree, start, lockend, cached_state); |
1881fba8 OS |
10273 | btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); |
10274 | *unlocked = true; | |
10275 | ||
10276 | ret = copy_to_iter(tmp, count, iter); | |
10277 | if (ret != count) | |
10278 | ret = -EFAULT; | |
10279 | kfree(tmp); | |
10280 | out: | |
10281 | btrfs_free_path(path); | |
10282 | return ret; | |
10283 | } | |
10284 | ||
10285 | struct btrfs_encoded_read_private { | |
10286 | struct btrfs_inode *inode; | |
10287 | u64 file_offset; | |
10288 | wait_queue_head_t wait; | |
10289 | atomic_t pending; | |
10290 | blk_status_t status; | |
10291 | bool skip_csum; | |
10292 | }; | |
10293 | ||
10294 | static blk_status_t submit_encoded_read_bio(struct btrfs_inode *inode, | |
10295 | struct bio *bio, int mirror_num) | |
10296 | { | |
917f32a2 | 10297 | struct btrfs_encoded_read_private *priv = btrfs_bio(bio)->private; |
1881fba8 OS |
10298 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
10299 | blk_status_t ret; | |
10300 | ||
10301 | if (!priv->skip_csum) { | |
10302 | ret = btrfs_lookup_bio_sums(&inode->vfs_inode, bio, NULL); | |
10303 | if (ret) | |
10304 | return ret; | |
10305 | } | |
10306 | ||
1881fba8 | 10307 | atomic_inc(&priv->pending); |
1a722d8f CH |
10308 | btrfs_submit_bio(fs_info, bio, mirror_num); |
10309 | return BLK_STS_OK; | |
1881fba8 OS |
10310 | } |
10311 | ||
10312 | static blk_status_t btrfs_encoded_read_verify_csum(struct btrfs_bio *bbio) | |
10313 | { | |
10314 | const bool uptodate = (bbio->bio.bi_status == BLK_STS_OK); | |
917f32a2 | 10315 | struct btrfs_encoded_read_private *priv = bbio->private; |
1881fba8 OS |
10316 | struct btrfs_inode *inode = priv->inode; |
10317 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
10318 | u32 sectorsize = fs_info->sectorsize; | |
10319 | struct bio_vec *bvec; | |
10320 | struct bvec_iter_all iter_all; | |
1881fba8 OS |
10321 | u32 bio_offset = 0; |
10322 | ||
10323 | if (priv->skip_csum || !uptodate) | |
10324 | return bbio->bio.bi_status; | |
10325 | ||
10326 | bio_for_each_segment_all(bvec, &bbio->bio, iter_all) { | |
10327 | unsigned int i, nr_sectors, pgoff; | |
10328 | ||
10329 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); | |
10330 | pgoff = bvec->bv_offset; | |
10331 | for (i = 0; i < nr_sectors; i++) { | |
10332 | ASSERT(pgoff < PAGE_SIZE); | |
7959bd44 CH |
10333 | if (btrfs_check_data_csum(&inode->vfs_inode, bbio, bio_offset, |
10334 | bvec->bv_page, pgoff)) | |
1881fba8 | 10335 | return BLK_STS_IOERR; |
1881fba8 OS |
10336 | bio_offset += sectorsize; |
10337 | pgoff += sectorsize; | |
10338 | } | |
10339 | } | |
10340 | return BLK_STS_OK; | |
10341 | } | |
10342 | ||
917f32a2 | 10343 | static void btrfs_encoded_read_endio(struct btrfs_bio *bbio) |
1881fba8 | 10344 | { |
917f32a2 | 10345 | struct btrfs_encoded_read_private *priv = bbio->private; |
1881fba8 OS |
10346 | blk_status_t status; |
10347 | ||
10348 | status = btrfs_encoded_read_verify_csum(bbio); | |
10349 | if (status) { | |
10350 | /* | |
10351 | * The memory barrier implied by the atomic_dec_return() here | |
10352 | * pairs with the memory barrier implied by the | |
10353 | * atomic_dec_return() or io_wait_event() in | |
10354 | * btrfs_encoded_read_regular_fill_pages() to ensure that this | |
10355 | * write is observed before the load of status in | |
10356 | * btrfs_encoded_read_regular_fill_pages(). | |
10357 | */ | |
10358 | WRITE_ONCE(priv->status, status); | |
10359 | } | |
10360 | if (!atomic_dec_return(&priv->pending)) | |
10361 | wake_up(&priv->wait); | |
10362 | btrfs_bio_free_csum(bbio); | |
917f32a2 | 10363 | bio_put(&bbio->bio); |
1881fba8 OS |
10364 | } |
10365 | ||
3ea4dc5b OS |
10366 | int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, |
10367 | u64 file_offset, u64 disk_bytenr, | |
10368 | u64 disk_io_size, struct page **pages) | |
1881fba8 OS |
10369 | { |
10370 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
10371 | struct btrfs_encoded_read_private priv = { | |
10372 | .inode = inode, | |
10373 | .file_offset = file_offset, | |
10374 | .pending = ATOMIC_INIT(1), | |
10375 | .skip_csum = (inode->flags & BTRFS_INODE_NODATASUM), | |
10376 | }; | |
10377 | unsigned long i = 0; | |
10378 | u64 cur = 0; | |
10379 | int ret; | |
10380 | ||
10381 | init_waitqueue_head(&priv.wait); | |
10382 | /* | |
10383 | * Submit bios for the extent, splitting due to bio or stripe limits as | |
10384 | * necessary. | |
10385 | */ | |
10386 | while (cur < disk_io_size) { | |
10387 | struct extent_map *em; | |
10388 | struct btrfs_io_geometry geom; | |
10389 | struct bio *bio = NULL; | |
10390 | u64 remaining; | |
10391 | ||
10392 | em = btrfs_get_chunk_map(fs_info, disk_bytenr + cur, | |
10393 | disk_io_size - cur); | |
10394 | if (IS_ERR(em)) { | |
10395 | ret = PTR_ERR(em); | |
10396 | } else { | |
10397 | ret = btrfs_get_io_geometry(fs_info, em, BTRFS_MAP_READ, | |
10398 | disk_bytenr + cur, &geom); | |
10399 | free_extent_map(em); | |
10400 | } | |
10401 | if (ret) { | |
10402 | WRITE_ONCE(priv.status, errno_to_blk_status(ret)); | |
10403 | break; | |
10404 | } | |
10405 | remaining = min(geom.len, disk_io_size - cur); | |
10406 | while (bio || remaining) { | |
10407 | size_t bytes = min_t(u64, remaining, PAGE_SIZE); | |
10408 | ||
10409 | if (!bio) { | |
917f32a2 CH |
10410 | bio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, |
10411 | btrfs_encoded_read_endio, | |
10412 | &priv); | |
1881fba8 OS |
10413 | bio->bi_iter.bi_sector = |
10414 | (disk_bytenr + cur) >> SECTOR_SHIFT; | |
1881fba8 OS |
10415 | } |
10416 | ||
10417 | if (!bytes || | |
10418 | bio_add_page(bio, pages[i], bytes, 0) < bytes) { | |
10419 | blk_status_t status; | |
10420 | ||
10421 | status = submit_encoded_read_bio(inode, bio, 0); | |
10422 | if (status) { | |
10423 | WRITE_ONCE(priv.status, status); | |
10424 | bio_put(bio); | |
10425 | goto out; | |
10426 | } | |
10427 | bio = NULL; | |
10428 | continue; | |
10429 | } | |
10430 | ||
10431 | i++; | |
10432 | cur += bytes; | |
10433 | remaining -= bytes; | |
10434 | } | |
10435 | } | |
10436 | ||
10437 | out: | |
10438 | if (atomic_dec_return(&priv.pending)) | |
10439 | io_wait_event(priv.wait, !atomic_read(&priv.pending)); | |
10440 | /* See btrfs_encoded_read_endio() for ordering. */ | |
10441 | return blk_status_to_errno(READ_ONCE(priv.status)); | |
10442 | } | |
10443 | ||
10444 | static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb, | |
10445 | struct iov_iter *iter, | |
10446 | u64 start, u64 lockend, | |
10447 | struct extent_state **cached_state, | |
10448 | u64 disk_bytenr, u64 disk_io_size, | |
10449 | size_t count, bool compressed, | |
10450 | bool *unlocked) | |
10451 | { | |
10452 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10453 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10454 | struct page **pages; | |
10455 | unsigned long nr_pages, i; | |
10456 | u64 cur; | |
10457 | size_t page_offset; | |
10458 | ssize_t ret; | |
10459 | ||
10460 | nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE); | |
10461 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); | |
10462 | if (!pages) | |
10463 | return -ENOMEM; | |
dd137dd1 STD |
10464 | ret = btrfs_alloc_page_array(nr_pages, pages); |
10465 | if (ret) { | |
10466 | ret = -ENOMEM; | |
10467 | goto out; | |
1881fba8 | 10468 | } |
1881fba8 OS |
10469 | |
10470 | ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr, | |
10471 | disk_io_size, pages); | |
10472 | if (ret) | |
10473 | goto out; | |
10474 | ||
570eb97b | 10475 | unlock_extent(io_tree, start, lockend, cached_state); |
1881fba8 OS |
10476 | btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); |
10477 | *unlocked = true; | |
10478 | ||
10479 | if (compressed) { | |
10480 | i = 0; | |
10481 | page_offset = 0; | |
10482 | } else { | |
10483 | i = (iocb->ki_pos - start) >> PAGE_SHIFT; | |
10484 | page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1); | |
10485 | } | |
10486 | cur = 0; | |
10487 | while (cur < count) { | |
10488 | size_t bytes = min_t(size_t, count - cur, | |
10489 | PAGE_SIZE - page_offset); | |
10490 | ||
10491 | if (copy_page_to_iter(pages[i], page_offset, bytes, | |
10492 | iter) != bytes) { | |
10493 | ret = -EFAULT; | |
10494 | goto out; | |
10495 | } | |
10496 | i++; | |
10497 | cur += bytes; | |
10498 | page_offset = 0; | |
10499 | } | |
10500 | ret = count; | |
10501 | out: | |
10502 | for (i = 0; i < nr_pages; i++) { | |
10503 | if (pages[i]) | |
10504 | __free_page(pages[i]); | |
10505 | } | |
10506 | kfree(pages); | |
10507 | return ret; | |
10508 | } | |
10509 | ||
10510 | ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, | |
10511 | struct btrfs_ioctl_encoded_io_args *encoded) | |
10512 | { | |
10513 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10514 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
10515 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10516 | ssize_t ret; | |
10517 | size_t count = iov_iter_count(iter); | |
10518 | u64 start, lockend, disk_bytenr, disk_io_size; | |
10519 | struct extent_state *cached_state = NULL; | |
10520 | struct extent_map *em; | |
10521 | bool unlocked = false; | |
10522 | ||
10523 | file_accessed(iocb->ki_filp); | |
10524 | ||
10525 | btrfs_inode_lock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); | |
10526 | ||
10527 | if (iocb->ki_pos >= inode->vfs_inode.i_size) { | |
10528 | btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); | |
10529 | return 0; | |
10530 | } | |
10531 | start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize); | |
10532 | /* | |
10533 | * We don't know how long the extent containing iocb->ki_pos is, but if | |
10534 | * it's compressed we know that it won't be longer than this. | |
10535 | */ | |
10536 | lockend = start + BTRFS_MAX_UNCOMPRESSED - 1; | |
10537 | ||
10538 | for (;;) { | |
10539 | struct btrfs_ordered_extent *ordered; | |
10540 | ||
10541 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, | |
10542 | lockend - start + 1); | |
10543 | if (ret) | |
10544 | goto out_unlock_inode; | |
570eb97b | 10545 | lock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10546 | ordered = btrfs_lookup_ordered_range(inode, start, |
10547 | lockend - start + 1); | |
10548 | if (!ordered) | |
10549 | break; | |
10550 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10551 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10552 | cond_resched(); |
10553 | } | |
10554 | ||
10555 | em = btrfs_get_extent(inode, NULL, 0, start, lockend - start + 1); | |
10556 | if (IS_ERR(em)) { | |
10557 | ret = PTR_ERR(em); | |
10558 | goto out_unlock_extent; | |
10559 | } | |
10560 | ||
10561 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10562 | u64 extent_start = em->start; | |
10563 | ||
10564 | /* | |
10565 | * For inline extents we get everything we need out of the | |
10566 | * extent item. | |
10567 | */ | |
10568 | free_extent_map(em); | |
10569 | em = NULL; | |
10570 | ret = btrfs_encoded_read_inline(iocb, iter, start, lockend, | |
10571 | &cached_state, extent_start, | |
10572 | count, encoded, &unlocked); | |
10573 | goto out; | |
10574 | } | |
10575 | ||
10576 | /* | |
10577 | * We only want to return up to EOF even if the extent extends beyond | |
10578 | * that. | |
10579 | */ | |
10580 | encoded->len = min_t(u64, extent_map_end(em), | |
10581 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
10582 | if (em->block_start == EXTENT_MAP_HOLE || | |
10583 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
10584 | disk_bytenr = EXTENT_MAP_HOLE; | |
10585 | count = min_t(u64, count, encoded->len); | |
10586 | encoded->len = count; | |
10587 | encoded->unencoded_len = count; | |
10588 | } else if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10589 | disk_bytenr = em->block_start; | |
10590 | /* | |
10591 | * Bail if the buffer isn't large enough to return the whole | |
10592 | * compressed extent. | |
10593 | */ | |
10594 | if (em->block_len > count) { | |
10595 | ret = -ENOBUFS; | |
10596 | goto out_em; | |
10597 | } | |
c1867eb3 DS |
10598 | disk_io_size = em->block_len; |
10599 | count = em->block_len; | |
1881fba8 OS |
10600 | encoded->unencoded_len = em->ram_bytes; |
10601 | encoded->unencoded_offset = iocb->ki_pos - em->orig_start; | |
10602 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
10603 | em->compress_type); | |
10604 | if (ret < 0) | |
10605 | goto out_em; | |
10606 | encoded->compression = ret; | |
10607 | } else { | |
10608 | disk_bytenr = em->block_start + (start - em->start); | |
10609 | if (encoded->len > count) | |
10610 | encoded->len = count; | |
10611 | /* | |
10612 | * Don't read beyond what we locked. This also limits the page | |
10613 | * allocations that we'll do. | |
10614 | */ | |
10615 | disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start; | |
10616 | count = start + disk_io_size - iocb->ki_pos; | |
10617 | encoded->len = count; | |
10618 | encoded->unencoded_len = count; | |
10619 | disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize); | |
10620 | } | |
10621 | free_extent_map(em); | |
10622 | em = NULL; | |
10623 | ||
10624 | if (disk_bytenr == EXTENT_MAP_HOLE) { | |
570eb97b | 10625 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10626 | btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); |
10627 | unlocked = true; | |
10628 | ret = iov_iter_zero(count, iter); | |
10629 | if (ret != count) | |
10630 | ret = -EFAULT; | |
10631 | } else { | |
10632 | ret = btrfs_encoded_read_regular(iocb, iter, start, lockend, | |
10633 | &cached_state, disk_bytenr, | |
10634 | disk_io_size, count, | |
10635 | encoded->compression, | |
10636 | &unlocked); | |
10637 | } | |
10638 | ||
10639 | out: | |
10640 | if (ret >= 0) | |
10641 | iocb->ki_pos += encoded->len; | |
10642 | out_em: | |
10643 | free_extent_map(em); | |
10644 | out_unlock_extent: | |
10645 | if (!unlocked) | |
570eb97b | 10646 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10647 | out_unlock_inode: |
10648 | if (!unlocked) | |
10649 | btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED); | |
10650 | return ret; | |
10651 | } | |
10652 | ||
7c0c7269 OS |
10653 | ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from, |
10654 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
10655 | { | |
10656 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10657 | struct btrfs_root *root = inode->root; | |
10658 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10659 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10660 | struct extent_changeset *data_reserved = NULL; | |
10661 | struct extent_state *cached_state = NULL; | |
10662 | int compression; | |
10663 | size_t orig_count; | |
10664 | u64 start, end; | |
10665 | u64 num_bytes, ram_bytes, disk_num_bytes; | |
10666 | unsigned long nr_pages, i; | |
10667 | struct page **pages; | |
10668 | struct btrfs_key ins; | |
10669 | bool extent_reserved = false; | |
10670 | struct extent_map *em; | |
10671 | ssize_t ret; | |
10672 | ||
10673 | switch (encoded->compression) { | |
10674 | case BTRFS_ENCODED_IO_COMPRESSION_ZLIB: | |
10675 | compression = BTRFS_COMPRESS_ZLIB; | |
10676 | break; | |
10677 | case BTRFS_ENCODED_IO_COMPRESSION_ZSTD: | |
10678 | compression = BTRFS_COMPRESS_ZSTD; | |
10679 | break; | |
10680 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K: | |
10681 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K: | |
10682 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K: | |
10683 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K: | |
10684 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K: | |
10685 | /* The sector size must match for LZO. */ | |
10686 | if (encoded->compression - | |
10687 | BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 != | |
10688 | fs_info->sectorsize_bits) | |
10689 | return -EINVAL; | |
10690 | compression = BTRFS_COMPRESS_LZO; | |
10691 | break; | |
10692 | default: | |
10693 | return -EINVAL; | |
10694 | } | |
10695 | if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE) | |
10696 | return -EINVAL; | |
10697 | ||
10698 | orig_count = iov_iter_count(from); | |
10699 | ||
10700 | /* The extent size must be sane. */ | |
10701 | if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED || | |
10702 | orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0) | |
10703 | return -EINVAL; | |
10704 | ||
10705 | /* | |
10706 | * The compressed data must be smaller than the decompressed data. | |
10707 | * | |
10708 | * It's of course possible for data to compress to larger or the same | |
10709 | * size, but the buffered I/O path falls back to no compression for such | |
10710 | * data, and we don't want to break any assumptions by creating these | |
10711 | * extents. | |
10712 | * | |
10713 | * Note that this is less strict than the current check we have that the | |
10714 | * compressed data must be at least one sector smaller than the | |
10715 | * decompressed data. We only want to enforce the weaker requirement | |
10716 | * from old kernels that it is at least one byte smaller. | |
10717 | */ | |
10718 | if (orig_count >= encoded->unencoded_len) | |
10719 | return -EINVAL; | |
10720 | ||
10721 | /* The extent must start on a sector boundary. */ | |
10722 | start = iocb->ki_pos; | |
10723 | if (!IS_ALIGNED(start, fs_info->sectorsize)) | |
10724 | return -EINVAL; | |
10725 | ||
10726 | /* | |
10727 | * The extent must end on a sector boundary. However, we allow a write | |
10728 | * which ends at or extends i_size to have an unaligned length; we round | |
10729 | * up the extent size and set i_size to the unaligned end. | |
10730 | */ | |
10731 | if (start + encoded->len < inode->vfs_inode.i_size && | |
10732 | !IS_ALIGNED(start + encoded->len, fs_info->sectorsize)) | |
10733 | return -EINVAL; | |
10734 | ||
10735 | /* Finally, the offset in the unencoded data must be sector-aligned. */ | |
10736 | if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize)) | |
10737 | return -EINVAL; | |
10738 | ||
10739 | num_bytes = ALIGN(encoded->len, fs_info->sectorsize); | |
10740 | ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize); | |
10741 | end = start + num_bytes - 1; | |
10742 | ||
10743 | /* | |
10744 | * If the extent cannot be inline, the compressed data on disk must be | |
10745 | * sector-aligned. For convenience, we extend it with zeroes if it | |
10746 | * isn't. | |
10747 | */ | |
10748 | disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize); | |
10749 | nr_pages = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE); | |
10750 | pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT); | |
10751 | if (!pages) | |
10752 | return -ENOMEM; | |
10753 | for (i = 0; i < nr_pages; i++) { | |
10754 | size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from)); | |
10755 | char *kaddr; | |
10756 | ||
10757 | pages[i] = alloc_page(GFP_KERNEL_ACCOUNT); | |
10758 | if (!pages[i]) { | |
10759 | ret = -ENOMEM; | |
10760 | goto out_pages; | |
10761 | } | |
70826b6b | 10762 | kaddr = kmap_local_page(pages[i]); |
7c0c7269 | 10763 | if (copy_from_iter(kaddr, bytes, from) != bytes) { |
70826b6b | 10764 | kunmap_local(kaddr); |
7c0c7269 OS |
10765 | ret = -EFAULT; |
10766 | goto out_pages; | |
10767 | } | |
10768 | if (bytes < PAGE_SIZE) | |
10769 | memset(kaddr + bytes, 0, PAGE_SIZE - bytes); | |
70826b6b | 10770 | kunmap_local(kaddr); |
7c0c7269 OS |
10771 | } |
10772 | ||
10773 | for (;;) { | |
10774 | struct btrfs_ordered_extent *ordered; | |
10775 | ||
10776 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes); | |
10777 | if (ret) | |
10778 | goto out_pages; | |
10779 | ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping, | |
10780 | start >> PAGE_SHIFT, | |
10781 | end >> PAGE_SHIFT); | |
10782 | if (ret) | |
10783 | goto out_pages; | |
570eb97b | 10784 | lock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10785 | ordered = btrfs_lookup_ordered_range(inode, start, num_bytes); |
10786 | if (!ordered && | |
10787 | !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end)) | |
10788 | break; | |
10789 | if (ordered) | |
10790 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10791 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10792 | cond_resched(); |
10793 | } | |
10794 | ||
10795 | /* | |
10796 | * We don't use the higher-level delalloc space functions because our | |
10797 | * num_bytes and disk_num_bytes are different. | |
10798 | */ | |
10799 | ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes); | |
10800 | if (ret) | |
10801 | goto out_unlock; | |
10802 | ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes); | |
10803 | if (ret) | |
10804 | goto out_free_data_space; | |
d4135134 FM |
10805 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes, |
10806 | false); | |
7c0c7269 OS |
10807 | if (ret) |
10808 | goto out_qgroup_free_data; | |
10809 | ||
10810 | /* Try an inline extent first. */ | |
10811 | if (start == 0 && encoded->unencoded_len == encoded->len && | |
10812 | encoded->unencoded_offset == 0) { | |
10813 | ret = cow_file_range_inline(inode, encoded->len, orig_count, | |
10814 | compression, pages, true); | |
10815 | if (ret <= 0) { | |
10816 | if (ret == 0) | |
10817 | ret = orig_count; | |
10818 | goto out_delalloc_release; | |
10819 | } | |
10820 | } | |
10821 | ||
10822 | ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes, | |
10823 | disk_num_bytes, 0, 0, &ins, 1, 1); | |
10824 | if (ret) | |
10825 | goto out_delalloc_release; | |
10826 | extent_reserved = true; | |
10827 | ||
10828 | em = create_io_em(inode, start, num_bytes, | |
10829 | start - encoded->unencoded_offset, ins.objectid, | |
10830 | ins.offset, ins.offset, ram_bytes, compression, | |
10831 | BTRFS_ORDERED_COMPRESSED); | |
10832 | if (IS_ERR(em)) { | |
10833 | ret = PTR_ERR(em); | |
10834 | goto out_free_reserved; | |
10835 | } | |
10836 | free_extent_map(em); | |
10837 | ||
10838 | ret = btrfs_add_ordered_extent(inode, start, num_bytes, ram_bytes, | |
10839 | ins.objectid, ins.offset, | |
10840 | encoded->unencoded_offset, | |
10841 | (1 << BTRFS_ORDERED_ENCODED) | | |
10842 | (1 << BTRFS_ORDERED_COMPRESSED), | |
10843 | compression); | |
10844 | if (ret) { | |
4c0c8cfc | 10845 | btrfs_drop_extent_map_range(inode, start, end, false); |
7c0c7269 OS |
10846 | goto out_free_reserved; |
10847 | } | |
10848 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10849 | ||
10850 | if (start + encoded->len > inode->vfs_inode.i_size) | |
10851 | i_size_write(&inode->vfs_inode, start + encoded->len); | |
10852 | ||
570eb97b | 10853 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10854 | |
10855 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10856 | ||
10857 | if (btrfs_submit_compressed_write(inode, start, num_bytes, ins.objectid, | |
10858 | ins.offset, pages, nr_pages, 0, NULL, | |
10859 | false)) { | |
10860 | btrfs_writepage_endio_finish_ordered(inode, pages[0], start, end, 0); | |
10861 | ret = -EIO; | |
10862 | goto out_pages; | |
10863 | } | |
10864 | ret = orig_count; | |
10865 | goto out; | |
10866 | ||
10867 | out_free_reserved: | |
10868 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10869 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); | |
10870 | out_delalloc_release: | |
10871 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10872 | btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0); | |
10873 | out_qgroup_free_data: | |
10874 | if (ret < 0) | |
10875 | btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes); | |
10876 | out_free_data_space: | |
10877 | /* | |
10878 | * If btrfs_reserve_extent() succeeded, then we already decremented | |
10879 | * bytes_may_use. | |
10880 | */ | |
10881 | if (!extent_reserved) | |
10882 | btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes); | |
10883 | out_unlock: | |
570eb97b | 10884 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10885 | out_pages: |
10886 | for (i = 0; i < nr_pages; i++) { | |
10887 | if (pages[i]) | |
10888 | __free_page(pages[i]); | |
10889 | } | |
10890 | kvfree(pages); | |
10891 | out: | |
10892 | if (ret >= 0) | |
10893 | iocb->ki_pos += encoded->len; | |
10894 | return ret; | |
10895 | } | |
10896 | ||
ed46ff3d OS |
10897 | #ifdef CONFIG_SWAP |
10898 | /* | |
10899 | * Add an entry indicating a block group or device which is pinned by a | |
10900 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10901 | * negative errno on failure. | |
10902 | */ | |
10903 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10904 | bool is_block_group) | |
10905 | { | |
10906 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10907 | struct btrfs_swapfile_pin *sp, *entry; | |
10908 | struct rb_node **p; | |
10909 | struct rb_node *parent = NULL; | |
10910 | ||
10911 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10912 | if (!sp) | |
10913 | return -ENOMEM; | |
10914 | sp->ptr = ptr; | |
10915 | sp->inode = inode; | |
10916 | sp->is_block_group = is_block_group; | |
195a49ea | 10917 | sp->bg_extent_count = 1; |
ed46ff3d OS |
10918 | |
10919 | spin_lock(&fs_info->swapfile_pins_lock); | |
10920 | p = &fs_info->swapfile_pins.rb_node; | |
10921 | while (*p) { | |
10922 | parent = *p; | |
10923 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10924 | if (sp->ptr < entry->ptr || | |
10925 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10926 | p = &(*p)->rb_left; | |
10927 | } else if (sp->ptr > entry->ptr || | |
10928 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10929 | p = &(*p)->rb_right; | |
10930 | } else { | |
195a49ea FM |
10931 | if (is_block_group) |
10932 | entry->bg_extent_count++; | |
ed46ff3d OS |
10933 | spin_unlock(&fs_info->swapfile_pins_lock); |
10934 | kfree(sp); | |
10935 | return 1; | |
10936 | } | |
10937 | } | |
10938 | rb_link_node(&sp->node, parent, p); | |
10939 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10940 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10941 | return 0; | |
10942 | } | |
10943 | ||
10944 | /* Free all of the entries pinned by this swapfile. */ | |
10945 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10946 | { | |
10947 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10948 | struct btrfs_swapfile_pin *sp; | |
10949 | struct rb_node *node, *next; | |
10950 | ||
10951 | spin_lock(&fs_info->swapfile_pins_lock); | |
10952 | node = rb_first(&fs_info->swapfile_pins); | |
10953 | while (node) { | |
10954 | next = rb_next(node); | |
10955 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10956 | if (sp->inode == inode) { | |
10957 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
195a49ea FM |
10958 | if (sp->is_block_group) { |
10959 | btrfs_dec_block_group_swap_extents(sp->ptr, | |
10960 | sp->bg_extent_count); | |
ed46ff3d | 10961 | btrfs_put_block_group(sp->ptr); |
195a49ea | 10962 | } |
ed46ff3d OS |
10963 | kfree(sp); |
10964 | } | |
10965 | node = next; | |
10966 | } | |
10967 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10968 | } | |
10969 | ||
10970 | struct btrfs_swap_info { | |
10971 | u64 start; | |
10972 | u64 block_start; | |
10973 | u64 block_len; | |
10974 | u64 lowest_ppage; | |
10975 | u64 highest_ppage; | |
10976 | unsigned long nr_pages; | |
10977 | int nr_extents; | |
10978 | }; | |
10979 | ||
10980 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10981 | struct btrfs_swap_info *bsi) | |
10982 | { | |
10983 | unsigned long nr_pages; | |
c2f82263 | 10984 | unsigned long max_pages; |
ed46ff3d OS |
10985 | u64 first_ppage, first_ppage_reported, next_ppage; |
10986 | int ret; | |
10987 | ||
c2f82263 FM |
10988 | /* |
10989 | * Our swapfile may have had its size extended after the swap header was | |
10990 | * written. In that case activating the swapfile should not go beyond | |
10991 | * the max size set in the swap header. | |
10992 | */ | |
10993 | if (bsi->nr_pages >= sis->max) | |
10994 | return 0; | |
10995 | ||
10996 | max_pages = sis->max - bsi->nr_pages; | |
ed46ff3d OS |
10997 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
10998 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
10999 | PAGE_SIZE) >> PAGE_SHIFT; | |
11000 | ||
11001 | if (first_ppage >= next_ppage) | |
11002 | return 0; | |
11003 | nr_pages = next_ppage - first_ppage; | |
c2f82263 | 11004 | nr_pages = min(nr_pages, max_pages); |
ed46ff3d OS |
11005 | |
11006 | first_ppage_reported = first_ppage; | |
11007 | if (bsi->start == 0) | |
11008 | first_ppage_reported++; | |
11009 | if (bsi->lowest_ppage > first_ppage_reported) | |
11010 | bsi->lowest_ppage = first_ppage_reported; | |
11011 | if (bsi->highest_ppage < (next_ppage - 1)) | |
11012 | bsi->highest_ppage = next_ppage - 1; | |
11013 | ||
11014 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
11015 | if (ret < 0) | |
11016 | return ret; | |
11017 | bsi->nr_extents += ret; | |
11018 | bsi->nr_pages += nr_pages; | |
11019 | return 0; | |
11020 | } | |
11021 | ||
11022 | static void btrfs_swap_deactivate(struct file *file) | |
11023 | { | |
11024 | struct inode *inode = file_inode(file); | |
11025 | ||
11026 | btrfs_free_swapfile_pins(inode); | |
11027 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
11028 | } | |
11029 | ||
11030 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
11031 | sector_t *span) | |
11032 | { | |
11033 | struct inode *inode = file_inode(file); | |
dd0734f2 FM |
11034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
11035 | struct btrfs_fs_info *fs_info = root->fs_info; | |
ed46ff3d OS |
11036 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
11037 | struct extent_state *cached_state = NULL; | |
11038 | struct extent_map *em = NULL; | |
11039 | struct btrfs_device *device = NULL; | |
11040 | struct btrfs_swap_info bsi = { | |
11041 | .lowest_ppage = (sector_t)-1ULL, | |
11042 | }; | |
11043 | int ret = 0; | |
11044 | u64 isize; | |
11045 | u64 start; | |
11046 | ||
11047 | /* | |
11048 | * If the swap file was just created, make sure delalloc is done. If the | |
11049 | * file changes again after this, the user is doing something stupid and | |
11050 | * we don't really care. | |
11051 | */ | |
11052 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
11053 | if (ret) | |
11054 | return ret; | |
11055 | ||
11056 | /* | |
11057 | * The inode is locked, so these flags won't change after we check them. | |
11058 | */ | |
11059 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
11060 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
11061 | return -EINVAL; | |
11062 | } | |
11063 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
11064 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
11065 | return -EINVAL; | |
11066 | } | |
11067 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
11068 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
11069 | return -EINVAL; | |
11070 | } | |
11071 | ||
11072 | /* | |
11073 | * Balance or device remove/replace/resize can move stuff around from | |
c3e1f96c GR |
11074 | * under us. The exclop protection makes sure they aren't running/won't |
11075 | * run concurrently while we are mapping the swap extents, and | |
11076 | * fs_info->swapfile_pins prevents them from running while the swap | |
11077 | * file is active and moving the extents. Note that this also prevents | |
11078 | * a concurrent device add which isn't actually necessary, but it's not | |
ed46ff3d OS |
11079 | * really worth the trouble to allow it. |
11080 | */ | |
c3e1f96c | 11081 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
ed46ff3d OS |
11082 | btrfs_warn(fs_info, |
11083 | "cannot activate swapfile while exclusive operation is running"); | |
11084 | return -EBUSY; | |
11085 | } | |
dd0734f2 FM |
11086 | |
11087 | /* | |
11088 | * Prevent snapshot creation while we are activating the swap file. | |
11089 | * We do not want to race with snapshot creation. If snapshot creation | |
11090 | * already started before we bumped nr_swapfiles from 0 to 1 and | |
11091 | * completes before the first write into the swap file after it is | |
11092 | * activated, than that write would fallback to COW. | |
11093 | */ | |
11094 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { | |
11095 | btrfs_exclop_finish(fs_info); | |
11096 | btrfs_warn(fs_info, | |
11097 | "cannot activate swapfile because snapshot creation is in progress"); | |
11098 | return -EINVAL; | |
11099 | } | |
ed46ff3d OS |
11100 | /* |
11101 | * Snapshots can create extents which require COW even if NODATACOW is | |
11102 | * set. We use this counter to prevent snapshots. We must increment it | |
11103 | * before walking the extents because we don't want a concurrent | |
11104 | * snapshot to run after we've already checked the extents. | |
60021bd7 KH |
11105 | * |
11106 | * It is possible that subvolume is marked for deletion but still not | |
11107 | * removed yet. To prevent this race, we check the root status before | |
11108 | * activating the swapfile. | |
ed46ff3d | 11109 | */ |
60021bd7 KH |
11110 | spin_lock(&root->root_item_lock); |
11111 | if (btrfs_root_dead(root)) { | |
11112 | spin_unlock(&root->root_item_lock); | |
11113 | ||
11114 | btrfs_exclop_finish(fs_info); | |
11115 | btrfs_warn(fs_info, | |
11116 | "cannot activate swapfile because subvolume %llu is being deleted", | |
11117 | root->root_key.objectid); | |
11118 | return -EPERM; | |
11119 | } | |
dd0734f2 | 11120 | atomic_inc(&root->nr_swapfiles); |
60021bd7 | 11121 | spin_unlock(&root->root_item_lock); |
ed46ff3d OS |
11122 | |
11123 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
11124 | ||
570eb97b | 11125 | lock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
11126 | start = 0; |
11127 | while (start < isize) { | |
11128 | u64 logical_block_start, physical_block_start; | |
32da5386 | 11129 | struct btrfs_block_group *bg; |
ed46ff3d OS |
11130 | u64 len = isize - start; |
11131 | ||
39b07b5d | 11132 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
11133 | if (IS_ERR(em)) { |
11134 | ret = PTR_ERR(em); | |
11135 | goto out; | |
11136 | } | |
11137 | ||
11138 | if (em->block_start == EXTENT_MAP_HOLE) { | |
11139 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
11140 | ret = -EINVAL; | |
11141 | goto out; | |
11142 | } | |
11143 | if (em->block_start == EXTENT_MAP_INLINE) { | |
11144 | /* | |
11145 | * It's unlikely we'll ever actually find ourselves | |
11146 | * here, as a file small enough to fit inline won't be | |
11147 | * big enough to store more than the swap header, but in | |
11148 | * case something changes in the future, let's catch it | |
11149 | * here rather than later. | |
11150 | */ | |
11151 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
11152 | ret = -EINVAL; | |
11153 | goto out; | |
11154 | } | |
11155 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
11156 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
11157 | ret = -EINVAL; | |
11158 | goto out; | |
11159 | } | |
11160 | ||
11161 | logical_block_start = em->block_start + (start - em->start); | |
11162 | len = min(len, em->len - (start - em->start)); | |
11163 | free_extent_map(em); | |
11164 | em = NULL; | |
11165 | ||
26ce9114 | 11166 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true); |
ed46ff3d OS |
11167 | if (ret < 0) { |
11168 | goto out; | |
11169 | } else if (ret) { | |
11170 | ret = 0; | |
11171 | } else { | |
11172 | btrfs_warn(fs_info, | |
11173 | "swapfile must not be copy-on-write"); | |
11174 | ret = -EINVAL; | |
11175 | goto out; | |
11176 | } | |
11177 | ||
11178 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
11179 | if (IS_ERR(em)) { | |
11180 | ret = PTR_ERR(em); | |
11181 | goto out; | |
11182 | } | |
11183 | ||
11184 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
11185 | btrfs_warn(fs_info, | |
11186 | "swapfile must have single data profile"); | |
11187 | ret = -EINVAL; | |
11188 | goto out; | |
11189 | } | |
11190 | ||
11191 | if (device == NULL) { | |
11192 | device = em->map_lookup->stripes[0].dev; | |
11193 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
11194 | if (ret == 1) | |
11195 | ret = 0; | |
11196 | else if (ret) | |
11197 | goto out; | |
11198 | } else if (device != em->map_lookup->stripes[0].dev) { | |
11199 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
11200 | ret = -EINVAL; | |
11201 | goto out; | |
11202 | } | |
11203 | ||
11204 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
11205 | (logical_block_start - em->start)); | |
11206 | len = min(len, em->len - (logical_block_start - em->start)); | |
11207 | free_extent_map(em); | |
11208 | em = NULL; | |
11209 | ||
11210 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
11211 | if (!bg) { | |
11212 | btrfs_warn(fs_info, | |
11213 | "could not find block group containing swapfile"); | |
11214 | ret = -EINVAL; | |
11215 | goto out; | |
11216 | } | |
11217 | ||
195a49ea FM |
11218 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
11219 | btrfs_warn(fs_info, | |
11220 | "block group for swapfile at %llu is read-only%s", | |
11221 | bg->start, | |
11222 | atomic_read(&fs_info->scrubs_running) ? | |
11223 | " (scrub running)" : ""); | |
11224 | btrfs_put_block_group(bg); | |
11225 | ret = -EINVAL; | |
11226 | goto out; | |
11227 | } | |
11228 | ||
ed46ff3d OS |
11229 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
11230 | if (ret) { | |
11231 | btrfs_put_block_group(bg); | |
11232 | if (ret == 1) | |
11233 | ret = 0; | |
11234 | else | |
11235 | goto out; | |
11236 | } | |
11237 | ||
11238 | if (bsi.block_len && | |
11239 | bsi.block_start + bsi.block_len == physical_block_start) { | |
11240 | bsi.block_len += len; | |
11241 | } else { | |
11242 | if (bsi.block_len) { | |
11243 | ret = btrfs_add_swap_extent(sis, &bsi); | |
11244 | if (ret) | |
11245 | goto out; | |
11246 | } | |
11247 | bsi.start = start; | |
11248 | bsi.block_start = physical_block_start; | |
11249 | bsi.block_len = len; | |
11250 | } | |
11251 | ||
11252 | start += len; | |
11253 | } | |
11254 | ||
11255 | if (bsi.block_len) | |
11256 | ret = btrfs_add_swap_extent(sis, &bsi); | |
11257 | ||
11258 | out: | |
11259 | if (!IS_ERR_OR_NULL(em)) | |
11260 | free_extent_map(em); | |
11261 | ||
570eb97b | 11262 | unlock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
11263 | |
11264 | if (ret) | |
11265 | btrfs_swap_deactivate(file); | |
11266 | ||
dd0734f2 FM |
11267 | btrfs_drew_write_unlock(&root->snapshot_lock); |
11268 | ||
c3e1f96c | 11269 | btrfs_exclop_finish(fs_info); |
ed46ff3d OS |
11270 | |
11271 | if (ret) | |
11272 | return ret; | |
11273 | ||
11274 | if (device) | |
11275 | sis->bdev = device->bdev; | |
11276 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
11277 | sis->max = bsi.nr_pages; | |
11278 | sis->pages = bsi.nr_pages - 1; | |
11279 | sis->highest_bit = bsi.nr_pages - 1; | |
11280 | return bsi.nr_extents; | |
11281 | } | |
11282 | #else | |
11283 | static void btrfs_swap_deactivate(struct file *file) | |
11284 | { | |
11285 | } | |
11286 | ||
11287 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
11288 | sector_t *span) | |
11289 | { | |
11290 | return -EOPNOTSUPP; | |
11291 | } | |
11292 | #endif | |
11293 | ||
2766ff61 FM |
11294 | /* |
11295 | * Update the number of bytes used in the VFS' inode. When we replace extents in | |
11296 | * a range (clone, dedupe, fallocate's zero range), we must update the number of | |
11297 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls | |
11298 | * always get a correct value. | |
11299 | */ | |
11300 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, | |
11301 | const u64 add_bytes, | |
11302 | const u64 del_bytes) | |
11303 | { | |
11304 | if (add_bytes == del_bytes) | |
11305 | return; | |
11306 | ||
11307 | spin_lock(&inode->lock); | |
11308 | if (del_bytes > 0) | |
11309 | inode_sub_bytes(&inode->vfs_inode, del_bytes); | |
11310 | if (add_bytes > 0) | |
11311 | inode_add_bytes(&inode->vfs_inode, add_bytes); | |
11312 | spin_unlock(&inode->lock); | |
11313 | } | |
11314 | ||
43dd529a | 11315 | /* |
63c34cb4 FM |
11316 | * Verify that there are no ordered extents for a given file range. |
11317 | * | |
11318 | * @inode: The target inode. | |
11319 | * @start: Start offset of the file range, should be sector size aligned. | |
11320 | * @end: End offset (inclusive) of the file range, its value +1 should be | |
11321 | * sector size aligned. | |
11322 | * | |
11323 | * This should typically be used for cases where we locked an inode's VFS lock in | |
11324 | * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode, | |
11325 | * we have flushed all delalloc in the range, we have waited for all ordered | |
11326 | * extents in the range to complete and finally we have locked the file range in | |
11327 | * the inode's io_tree. | |
11328 | */ | |
11329 | void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end) | |
11330 | { | |
11331 | struct btrfs_root *root = inode->root; | |
11332 | struct btrfs_ordered_extent *ordered; | |
11333 | ||
11334 | if (!IS_ENABLED(CONFIG_BTRFS_ASSERT)) | |
11335 | return; | |
11336 | ||
11337 | ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start); | |
11338 | if (ordered) { | |
11339 | btrfs_err(root->fs_info, | |
11340 | "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])", | |
11341 | start, end, btrfs_ino(inode), root->root_key.objectid, | |
11342 | ordered->file_offset, | |
11343 | ordered->file_offset + ordered->num_bytes - 1); | |
11344 | btrfs_put_ordered_extent(ordered); | |
11345 | } | |
11346 | ||
11347 | ASSERT(ordered == NULL); | |
11348 | } | |
11349 | ||
6e1d5dcc | 11350 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 11351 | .getattr = btrfs_getattr, |
39279cc3 CM |
11352 | .lookup = btrfs_lookup, |
11353 | .create = btrfs_create, | |
11354 | .unlink = btrfs_unlink, | |
11355 | .link = btrfs_link, | |
11356 | .mkdir = btrfs_mkdir, | |
11357 | .rmdir = btrfs_rmdir, | |
2773bf00 | 11358 | .rename = btrfs_rename2, |
39279cc3 CM |
11359 | .symlink = btrfs_symlink, |
11360 | .setattr = btrfs_setattr, | |
618e21d5 | 11361 | .mknod = btrfs_mknod, |
5103e947 | 11362 | .listxattr = btrfs_listxattr, |
fdebe2bd | 11363 | .permission = btrfs_permission, |
4e34e719 | 11364 | .get_acl = btrfs_get_acl, |
996a710d | 11365 | .set_acl = btrfs_set_acl, |
93fd63c2 | 11366 | .update_time = btrfs_update_time, |
ef3b9af5 | 11367 | .tmpfile = btrfs_tmpfile, |
97fc2977 MS |
11368 | .fileattr_get = btrfs_fileattr_get, |
11369 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 11370 | }; |
76dda93c | 11371 | |
828c0950 | 11372 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
11373 | .llseek = generic_file_llseek, |
11374 | .read = generic_read_dir, | |
02dbfc99 | 11375 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 11376 | .open = btrfs_opendir, |
34287aa3 | 11377 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 11378 | #ifdef CONFIG_COMPAT |
4c63c245 | 11379 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 11380 | #endif |
6bf13c0c | 11381 | .release = btrfs_release_file, |
e02119d5 | 11382 | .fsync = btrfs_sync_file, |
39279cc3 CM |
11383 | }; |
11384 | ||
35054394 CM |
11385 | /* |
11386 | * btrfs doesn't support the bmap operation because swapfiles | |
11387 | * use bmap to make a mapping of extents in the file. They assume | |
11388 | * these extents won't change over the life of the file and they | |
11389 | * use the bmap result to do IO directly to the drive. | |
11390 | * | |
11391 | * the btrfs bmap call would return logical addresses that aren't | |
11392 | * suitable for IO and they also will change frequently as COW | |
11393 | * operations happen. So, swapfile + btrfs == corruption. | |
11394 | * | |
11395 | * For now we're avoiding this by dropping bmap. | |
11396 | */ | |
7f09410b | 11397 | static const struct address_space_operations btrfs_aops = { |
fb12489b | 11398 | .read_folio = btrfs_read_folio, |
b293f02e | 11399 | .writepages = btrfs_writepages, |
ba206a02 | 11400 | .readahead = btrfs_readahead, |
f85781fb | 11401 | .direct_IO = noop_direct_IO, |
895586eb | 11402 | .invalidate_folio = btrfs_invalidate_folio, |
f913cff3 | 11403 | .release_folio = btrfs_release_folio, |
e7a60a17 | 11404 | .migrate_folio = btrfs_migrate_folio, |
187c82cb | 11405 | .dirty_folio = filemap_dirty_folio, |
465fdd97 | 11406 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
11407 | .swap_activate = btrfs_swap_activate, |
11408 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
11409 | }; |
11410 | ||
6e1d5dcc | 11411 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
11412 | .getattr = btrfs_getattr, |
11413 | .setattr = btrfs_setattr, | |
5103e947 | 11414 | .listxattr = btrfs_listxattr, |
fdebe2bd | 11415 | .permission = btrfs_permission, |
1506fcc8 | 11416 | .fiemap = btrfs_fiemap, |
4e34e719 | 11417 | .get_acl = btrfs_get_acl, |
996a710d | 11418 | .set_acl = btrfs_set_acl, |
e41f941a | 11419 | .update_time = btrfs_update_time, |
97fc2977 MS |
11420 | .fileattr_get = btrfs_fileattr_get, |
11421 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 11422 | }; |
6e1d5dcc | 11423 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
11424 | .getattr = btrfs_getattr, |
11425 | .setattr = btrfs_setattr, | |
fdebe2bd | 11426 | .permission = btrfs_permission, |
33268eaf | 11427 | .listxattr = btrfs_listxattr, |
4e34e719 | 11428 | .get_acl = btrfs_get_acl, |
996a710d | 11429 | .set_acl = btrfs_set_acl, |
e41f941a | 11430 | .update_time = btrfs_update_time, |
618e21d5 | 11431 | }; |
6e1d5dcc | 11432 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 11433 | .get_link = page_get_link, |
f209561a | 11434 | .getattr = btrfs_getattr, |
22c44fe6 | 11435 | .setattr = btrfs_setattr, |
fdebe2bd | 11436 | .permission = btrfs_permission, |
0279b4cd | 11437 | .listxattr = btrfs_listxattr, |
e41f941a | 11438 | .update_time = btrfs_update_time, |
39279cc3 | 11439 | }; |
76dda93c | 11440 | |
82d339d9 | 11441 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
11442 | .d_delete = btrfs_dentry_delete, |
11443 | }; |