]>
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" |
103c1972 | 46 | #include "bio.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" |
7c8ede16 | 64 | #include "file-item.h" |
c7a03b52 | 65 | #include "uuid-tree.h" |
7572dec8 | 66 | #include "ioctl.h" |
af142b6f | 67 | #include "file.h" |
33cf97a7 | 68 | #include "acl.h" |
67707479 | 69 | #include "relocation.h" |
5c11adcc | 70 | #include "verity.h" |
7f0add25 | 71 | #include "super.h" |
aa5d3003 | 72 | #include "orphan.h" |
b9a9a850 | 73 | #include "backref.h" |
02c372e1 | 74 | #include "raid-stripe-tree.h" |
39279cc3 CM |
75 | |
76 | struct btrfs_iget_args { | |
0202e83f | 77 | u64 ino; |
39279cc3 CM |
78 | struct btrfs_root *root; |
79 | }; | |
80 | ||
f28a4928 | 81 | struct btrfs_dio_data { |
f85781fb GR |
82 | ssize_t submitted; |
83 | struct extent_changeset *data_reserved; | |
53f2c206 | 84 | struct btrfs_ordered_extent *ordered; |
f5585f4f FM |
85 | bool data_space_reserved; |
86 | bool nocow_done; | |
f28a4928 FM |
87 | }; |
88 | ||
a3e171a0 | 89 | struct btrfs_dio_private { |
67d66982 | 90 | /* Range of I/O */ |
a3e171a0 | 91 | u64 file_offset; |
a3e171a0 CH |
92 | u32 bytes; |
93 | ||
642c5d34 | 94 | /* This must be last */ |
67d66982 | 95 | struct btrfs_bio bbio; |
a3e171a0 CH |
96 | }; |
97 | ||
642c5d34 CH |
98 | static struct bio_set btrfs_dio_bioset; |
99 | ||
88d2beec FM |
100 | struct btrfs_rename_ctx { |
101 | /* Output field. Stores the index number of the old directory entry. */ | |
102 | u64 index; | |
103 | }; | |
104 | ||
b9a9a850 QW |
105 | /* |
106 | * Used by data_reloc_print_warning_inode() to pass needed info for filename | |
107 | * resolution and output of error message. | |
108 | */ | |
109 | struct data_reloc_warn { | |
110 | struct btrfs_path path; | |
111 | struct btrfs_fs_info *fs_info; | |
112 | u64 extent_item_size; | |
113 | u64 logical; | |
114 | int mirror_num; | |
115 | }; | |
116 | ||
516095cd DS |
117 | /* |
118 | * For the file_extent_tree, we want to hold the inode lock when we lookup and | |
119 | * update the disk_i_size, but lockdep will complain because our io_tree we hold | |
120 | * the tree lock and get the inode lock when setting delalloc. These two things | |
121 | * are unrelated, so make a class for the file_extent_tree so we don't get the | |
122 | * two locking patterns mixed up. | |
123 | */ | |
124 | static struct lock_class_key file_extent_tree_class; | |
125 | ||
6e1d5dcc AD |
126 | static const struct inode_operations btrfs_dir_inode_operations; |
127 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
128 | static const struct inode_operations btrfs_special_inode_operations; |
129 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 130 | static const struct address_space_operations btrfs_aops; |
828c0950 | 131 | static const struct file_operations btrfs_dir_file_operations; |
39279cc3 CM |
132 | |
133 | static struct kmem_cache *btrfs_inode_cachep; | |
39279cc3 | 134 | |
3972f260 | 135 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
d9dcae67 | 136 | static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback); |
ba9145ad | 137 | |
256b0cf9 CH |
138 | static noinline int run_delalloc_cow(struct btrfs_inode *inode, |
139 | struct page *locked_page, u64 start, | |
140 | u64 end, struct writeback_control *wbc, | |
141 | bool pages_dirty); | |
4b67c11d NB |
142 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
143 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
144 | u64 block_len, u64 orig_block_len, |
145 | u64 ram_bytes, int compress_type, | |
146 | int type); | |
7b128766 | 147 | |
b9a9a850 QW |
148 | static int data_reloc_print_warning_inode(u64 inum, u64 offset, u64 num_bytes, |
149 | u64 root, void *warn_ctx) | |
150 | { | |
151 | struct data_reloc_warn *warn = warn_ctx; | |
152 | struct btrfs_fs_info *fs_info = warn->fs_info; | |
153 | struct extent_buffer *eb; | |
154 | struct btrfs_inode_item *inode_item; | |
155 | struct inode_fs_paths *ipath = NULL; | |
156 | struct btrfs_root *local_root; | |
157 | struct btrfs_key key; | |
158 | unsigned int nofs_flag; | |
159 | u32 nlink; | |
160 | int ret; | |
161 | ||
162 | local_root = btrfs_get_fs_root(fs_info, root, true); | |
163 | if (IS_ERR(local_root)) { | |
164 | ret = PTR_ERR(local_root); | |
165 | goto err; | |
166 | } | |
167 | ||
168 | /* This makes the path point to (inum INODE_ITEM ioff). */ | |
169 | key.objectid = inum; | |
170 | key.type = BTRFS_INODE_ITEM_KEY; | |
171 | key.offset = 0; | |
172 | ||
173 | ret = btrfs_search_slot(NULL, local_root, &key, &warn->path, 0, 0); | |
174 | if (ret) { | |
175 | btrfs_put_root(local_root); | |
176 | btrfs_release_path(&warn->path); | |
177 | goto err; | |
178 | } | |
179 | ||
180 | eb = warn->path.nodes[0]; | |
181 | inode_item = btrfs_item_ptr(eb, warn->path.slots[0], struct btrfs_inode_item); | |
182 | nlink = btrfs_inode_nlink(eb, inode_item); | |
183 | btrfs_release_path(&warn->path); | |
184 | ||
185 | nofs_flag = memalloc_nofs_save(); | |
186 | ipath = init_ipath(4096, local_root, &warn->path); | |
187 | memalloc_nofs_restore(nofs_flag); | |
188 | if (IS_ERR(ipath)) { | |
189 | btrfs_put_root(local_root); | |
190 | ret = PTR_ERR(ipath); | |
191 | ipath = NULL; | |
192 | /* | |
193 | * -ENOMEM, not a critical error, just output an generic error | |
194 | * without filename. | |
195 | */ | |
196 | btrfs_warn(fs_info, | |
197 | "checksum error at logical %llu mirror %u root %llu, inode %llu offset %llu", | |
198 | warn->logical, warn->mirror_num, root, inum, offset); | |
199 | return ret; | |
200 | } | |
201 | ret = paths_from_inode(inum, ipath); | |
202 | if (ret < 0) | |
203 | goto err; | |
204 | ||
205 | /* | |
206 | * We deliberately ignore the bit ipath might have been too small to | |
207 | * hold all of the paths here | |
208 | */ | |
209 | for (int i = 0; i < ipath->fspath->elem_cnt; i++) { | |
210 | btrfs_warn(fs_info, | |
211 | "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu length %u links %u (path: %s)", | |
212 | warn->logical, warn->mirror_num, root, inum, offset, | |
213 | fs_info->sectorsize, nlink, | |
214 | (char *)(unsigned long)ipath->fspath->val[i]); | |
215 | } | |
216 | ||
217 | btrfs_put_root(local_root); | |
218 | free_ipath(ipath); | |
219 | return 0; | |
220 | ||
221 | err: | |
222 | btrfs_warn(fs_info, | |
223 | "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu, path resolving failed with ret=%d", | |
224 | warn->logical, warn->mirror_num, root, inum, offset, ret); | |
225 | ||
226 | free_ipath(ipath); | |
227 | return ret; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Do extra user-friendly error output (e.g. lookup all the affected files). | |
232 | * | |
233 | * Return true if we succeeded doing the backref lookup. | |
234 | * Return false if such lookup failed, and has to fallback to the old error message. | |
235 | */ | |
236 | static void print_data_reloc_error(const struct btrfs_inode *inode, u64 file_off, | |
237 | const u8 *csum, const u8 *csum_expected, | |
238 | int mirror_num) | |
239 | { | |
240 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
241 | struct btrfs_path path = { 0 }; | |
242 | struct btrfs_key found_key = { 0 }; | |
243 | struct extent_buffer *eb; | |
244 | struct btrfs_extent_item *ei; | |
245 | const u32 csum_size = fs_info->csum_size; | |
246 | u64 logical; | |
247 | u64 flags; | |
248 | u32 item_size; | |
249 | int ret; | |
250 | ||
251 | mutex_lock(&fs_info->reloc_mutex); | |
252 | logical = btrfs_get_reloc_bg_bytenr(fs_info); | |
253 | mutex_unlock(&fs_info->reloc_mutex); | |
254 | ||
255 | if (logical == U64_MAX) { | |
256 | btrfs_warn_rl(fs_info, "has data reloc tree but no running relocation"); | |
257 | btrfs_warn_rl(fs_info, | |
258 | "csum failed root %lld ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
259 | inode->root->root_key.objectid, btrfs_ino(inode), file_off, | |
260 | CSUM_FMT_VALUE(csum_size, csum), | |
261 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
262 | mirror_num); | |
263 | return; | |
264 | } | |
265 | ||
266 | logical += file_off; | |
267 | btrfs_warn_rl(fs_info, | |
268 | "csum failed root %lld ino %llu off %llu logical %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
269 | inode->root->root_key.objectid, | |
270 | btrfs_ino(inode), file_off, logical, | |
271 | CSUM_FMT_VALUE(csum_size, csum), | |
272 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
273 | mirror_num); | |
274 | ||
275 | ret = extent_from_logical(fs_info, logical, &path, &found_key, &flags); | |
276 | if (ret < 0) { | |
277 | btrfs_err_rl(fs_info, "failed to lookup extent item for logical %llu: %d", | |
278 | logical, ret); | |
279 | return; | |
280 | } | |
281 | eb = path.nodes[0]; | |
282 | ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item); | |
283 | item_size = btrfs_item_size(eb, path.slots[0]); | |
284 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
285 | unsigned long ptr = 0; | |
286 | u64 ref_root; | |
287 | u8 ref_level; | |
288 | ||
b7f9945a | 289 | while (true) { |
b9a9a850 QW |
290 | ret = tree_backref_for_extent(&ptr, eb, &found_key, ei, |
291 | item_size, &ref_root, | |
292 | &ref_level); | |
b7f9945a QW |
293 | if (ret < 0) { |
294 | btrfs_warn_rl(fs_info, | |
295 | "failed to resolve tree backref for logical %llu: %d", | |
296 | logical, ret); | |
297 | break; | |
298 | } | |
299 | if (ret > 0) | |
300 | break; | |
301 | ||
b9a9a850 QW |
302 | btrfs_warn_rl(fs_info, |
303 | "csum error at logical %llu mirror %u: metadata %s (level %d) in tree %llu", | |
304 | logical, mirror_num, | |
305 | (ref_level ? "node" : "leaf"), | |
b7f9945a QW |
306 | ref_level, ref_root); |
307 | } | |
b9a9a850 QW |
308 | btrfs_release_path(&path); |
309 | } else { | |
310 | struct btrfs_backref_walk_ctx ctx = { 0 }; | |
311 | struct data_reloc_warn reloc_warn = { 0 }; | |
312 | ||
313 | btrfs_release_path(&path); | |
314 | ||
315 | ctx.bytenr = found_key.objectid; | |
316 | ctx.extent_item_pos = logical - found_key.objectid; | |
317 | ctx.fs_info = fs_info; | |
318 | ||
319 | reloc_warn.logical = logical; | |
320 | reloc_warn.extent_item_size = found_key.offset; | |
321 | reloc_warn.mirror_num = mirror_num; | |
322 | reloc_warn.fs_info = fs_info; | |
323 | ||
324 | iterate_extent_inodes(&ctx, true, | |
325 | data_reloc_print_warning_inode, &reloc_warn); | |
326 | } | |
327 | } | |
328 | ||
f60acad3 JB |
329 | static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode, |
330 | u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) | |
331 | { | |
332 | struct btrfs_root *root = inode->root; | |
333 | const u32 csum_size = root->fs_info->csum_size; | |
334 | ||
b9a9a850 QW |
335 | /* For data reloc tree, it's better to do a backref lookup instead. */ |
336 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
337 | return print_data_reloc_error(inode, logical_start, csum, | |
338 | csum_expected, mirror_num); | |
339 | ||
f60acad3 JB |
340 | /* Output without objectid, which is more meaningful */ |
341 | if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) { | |
342 | btrfs_warn_rl(root->fs_info, | |
343 | "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
344 | root->root_key.objectid, btrfs_ino(inode), | |
345 | logical_start, | |
346 | CSUM_FMT_VALUE(csum_size, csum), | |
347 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
348 | mirror_num); | |
349 | } else { | |
350 | btrfs_warn_rl(root->fs_info, | |
351 | "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
352 | root->root_key.objectid, btrfs_ino(inode), | |
353 | logical_start, | |
354 | CSUM_FMT_VALUE(csum_size, csum), | |
355 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
356 | mirror_num); | |
357 | } | |
358 | } | |
359 | ||
a14b78ad | 360 | /* |
9580503b | 361 | * Lock inode i_rwsem based on arguments passed. |
a14b78ad GR |
362 | * |
363 | * ilock_flags can have the following bit set: | |
364 | * | |
365 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode | |
366 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt | |
367 | * return -EAGAIN | |
8318ba79 | 368 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
a14b78ad | 369 | */ |
29b6352b | 370 | int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags) |
a14b78ad GR |
371 | { |
372 | if (ilock_flags & BTRFS_ILOCK_SHARED) { | |
373 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
29b6352b | 374 | if (!inode_trylock_shared(&inode->vfs_inode)) |
a14b78ad GR |
375 | return -EAGAIN; |
376 | else | |
377 | return 0; | |
378 | } | |
29b6352b | 379 | inode_lock_shared(&inode->vfs_inode); |
a14b78ad GR |
380 | } else { |
381 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
29b6352b | 382 | if (!inode_trylock(&inode->vfs_inode)) |
a14b78ad GR |
383 | return -EAGAIN; |
384 | else | |
385 | return 0; | |
386 | } | |
29b6352b | 387 | inode_lock(&inode->vfs_inode); |
a14b78ad | 388 | } |
8318ba79 | 389 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
29b6352b | 390 | down_write(&inode->i_mmap_lock); |
a14b78ad GR |
391 | return 0; |
392 | } | |
393 | ||
394 | /* | |
9580503b | 395 | * Unock inode i_rwsem. |
a14b78ad GR |
396 | * |
397 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() | |
398 | * to decide whether the lock acquired is shared or exclusive. | |
399 | */ | |
e5d4d75b | 400 | void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags) |
a14b78ad | 401 | { |
8318ba79 | 402 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
e5d4d75b | 403 | up_write(&inode->i_mmap_lock); |
a14b78ad | 404 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
e5d4d75b | 405 | inode_unlock_shared(&inode->vfs_inode); |
a14b78ad | 406 | else |
e5d4d75b | 407 | inode_unlock(&inode->vfs_inode); |
a14b78ad GR |
408 | } |
409 | ||
52427260 QW |
410 | /* |
411 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 412 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
413 | * |
414 | * NOTE: caller must ensure that when an error happens, it can not call | |
415 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
416 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
417 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 418 | * extent (btrfs_finish_ordered_io()). |
52427260 | 419 | */ |
64e1db56 | 420 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
421 | struct page *locked_page, |
422 | u64 offset, u64 bytes) | |
52427260 | 423 | { |
63d71450 NA |
424 | unsigned long index = offset >> PAGE_SHIFT; |
425 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
0e47b25c | 426 | u64 page_start = 0, page_end = 0; |
63d71450 NA |
427 | struct page *page; |
428 | ||
99826e4c NA |
429 | if (locked_page) { |
430 | page_start = page_offset(locked_page); | |
431 | page_end = page_start + PAGE_SIZE - 1; | |
432 | } | |
433 | ||
63d71450 | 434 | while (index <= end_index) { |
968f2566 | 435 | /* |
9783e4de CH |
436 | * For locked page, we will call btrfs_mark_ordered_io_finished |
437 | * through btrfs_mark_ordered_io_finished() on it | |
438 | * in run_delalloc_range() for the error handling, which will | |
439 | * clear page Ordered and run the ordered extent accounting. | |
968f2566 QW |
440 | * |
441 | * Here we can't just clear the Ordered bit, or | |
442 | * btrfs_mark_ordered_io_finished() would skip the accounting | |
443 | * for the page range, and the ordered extent will never finish. | |
444 | */ | |
99826e4c | 445 | if (locked_page && index == (page_start >> PAGE_SHIFT)) { |
968f2566 QW |
446 | index++; |
447 | continue; | |
448 | } | |
64e1db56 | 449 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
450 | index++; |
451 | if (!page) | |
452 | continue; | |
968f2566 QW |
453 | |
454 | /* | |
455 | * Here we just clear all Ordered bits for every page in the | |
711f447b | 456 | * range, then btrfs_mark_ordered_io_finished() will handle |
968f2566 QW |
457 | * the ordered extent accounting for the range. |
458 | */ | |
b945a463 QW |
459 | btrfs_page_clamp_clear_ordered(inode->root->fs_info, page, |
460 | offset, bytes); | |
63d71450 NA |
461 | put_page(page); |
462 | } | |
d1051d6e | 463 | |
99826e4c NA |
464 | if (locked_page) { |
465 | /* The locked page covers the full range, nothing needs to be done */ | |
466 | if (bytes + offset <= page_start + PAGE_SIZE) | |
467 | return; | |
468 | /* | |
469 | * In case this page belongs to the delalloc range being | |
470 | * instantiated then skip it, since the first page of a range is | |
471 | * going to be properly cleaned up by the caller of | |
472 | * run_delalloc_range | |
473 | */ | |
474 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
475 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; | |
476 | offset = page_offset(locked_page) + PAGE_SIZE; | |
477 | } | |
d1051d6e NB |
478 | } |
479 | ||
711f447b | 480 | return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false); |
52427260 QW |
481 | } |
482 | ||
7152b425 | 483 | static int btrfs_dirty_inode(struct btrfs_inode *inode); |
7b128766 | 484 | |
f34f57a3 | 485 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
3538d68d | 486 | struct btrfs_new_inode_args *args) |
0279b4cd JO |
487 | { |
488 | int err; | |
489 | ||
3538d68d OS |
490 | if (args->default_acl) { |
491 | err = __btrfs_set_acl(trans, args->inode, args->default_acl, | |
492 | ACL_TYPE_DEFAULT); | |
493 | if (err) | |
494 | return err; | |
495 | } | |
496 | if (args->acl) { | |
497 | err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS); | |
498 | if (err) | |
499 | return err; | |
500 | } | |
501 | if (!args->default_acl && !args->acl) | |
502 | cache_no_acl(args->inode); | |
503 | return btrfs_xattr_security_init(trans, args->inode, args->dir, | |
504 | &args->dentry->d_name); | |
0279b4cd JO |
505 | } |
506 | ||
c8b97818 CM |
507 | /* |
508 | * this does all the hard work for inserting an inline extent into | |
509 | * the btree. The caller should have done a btrfs_drop_extents so that | |
510 | * no overlapping inline items exist in the btree | |
511 | */ | |
40f76580 | 512 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
8dd9872d OS |
513 | struct btrfs_path *path, |
514 | struct btrfs_inode *inode, bool extent_inserted, | |
515 | size_t size, size_t compressed_size, | |
fe3f566c | 516 | int compress_type, |
d9496e8a OS |
517 | struct page **compressed_pages, |
518 | bool update_i_size) | |
c8b97818 | 519 | { |
8dd9872d | 520 | struct btrfs_root *root = inode->root; |
c8b97818 CM |
521 | struct extent_buffer *leaf; |
522 | struct page *page = NULL; | |
523 | char *kaddr; | |
524 | unsigned long ptr; | |
525 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
526 | int ret; |
527 | size_t cur_size = size; | |
d9496e8a | 528 | u64 i_size; |
c8b97818 | 529 | |
982f1f5d JJB |
530 | ASSERT((compressed_size > 0 && compressed_pages) || |
531 | (compressed_size == 0 && !compressed_pages)); | |
532 | ||
fe3f566c | 533 | if (compressed_size && compressed_pages) |
c8b97818 | 534 | cur_size = compressed_size; |
c8b97818 | 535 | |
1acae57b FDBM |
536 | if (!extent_inserted) { |
537 | struct btrfs_key key; | |
538 | size_t datasize; | |
c8b97818 | 539 | |
8dd9872d OS |
540 | key.objectid = btrfs_ino(inode); |
541 | key.offset = 0; | |
962a298f | 542 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 543 | |
1acae57b | 544 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
1acae57b FDBM |
545 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
546 | datasize); | |
79b4f4c6 | 547 | if (ret) |
1acae57b | 548 | goto fail; |
c8b97818 CM |
549 | } |
550 | leaf = path->nodes[0]; | |
551 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
552 | struct btrfs_file_extent_item); | |
553 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
554 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
555 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
556 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
557 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
558 | ptr = btrfs_file_extent_inline_start(ei); | |
559 | ||
261507a0 | 560 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
561 | struct page *cpage; |
562 | int i = 0; | |
d397712b | 563 | while (compressed_size > 0) { |
c8b97818 | 564 | cpage = compressed_pages[i]; |
5b050f04 | 565 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 566 | PAGE_SIZE); |
c8b97818 | 567 | |
4cb2e5e8 | 568 | kaddr = kmap_local_page(cpage); |
c8b97818 | 569 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
4cb2e5e8 | 570 | kunmap_local(kaddr); |
c8b97818 CM |
571 | |
572 | i++; | |
573 | ptr += cur_size; | |
574 | compressed_size -= cur_size; | |
575 | } | |
576 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 577 | compress_type); |
c8b97818 | 578 | } else { |
8dd9872d | 579 | page = find_get_page(inode->vfs_inode.i_mapping, 0); |
c8b97818 | 580 | btrfs_set_file_extent_compression(leaf, ei, 0); |
4cb2e5e8 | 581 | kaddr = kmap_local_page(page); |
8dd9872d | 582 | write_extent_buffer(leaf, kaddr, ptr, size); |
4cb2e5e8 | 583 | kunmap_local(kaddr); |
09cbfeaf | 584 | put_page(page); |
c8b97818 | 585 | } |
50564b65 | 586 | btrfs_mark_buffer_dirty(trans, leaf); |
1acae57b | 587 | btrfs_release_path(path); |
c8b97818 | 588 | |
9ddc959e JB |
589 | /* |
590 | * We align size to sectorsize for inline extents just for simplicity | |
591 | * sake. | |
592 | */ | |
8dd9872d OS |
593 | ret = btrfs_inode_set_file_extent_range(inode, 0, |
594 | ALIGN(size, root->fs_info->sectorsize)); | |
9ddc959e JB |
595 | if (ret) |
596 | goto fail; | |
597 | ||
c2167754 | 598 | /* |
d9496e8a OS |
599 | * We're an inline extent, so nobody can extend the file past i_size |
600 | * without locking a page we already have locked. | |
c2167754 | 601 | * |
d9496e8a OS |
602 | * We must do any i_size and inode updates before we unlock the pages. |
603 | * Otherwise we could end up racing with unlink. | |
c2167754 | 604 | */ |
d9496e8a OS |
605 | i_size = i_size_read(&inode->vfs_inode); |
606 | if (update_i_size && size > i_size) { | |
607 | i_size_write(&inode->vfs_inode, size); | |
608 | i_size = size; | |
609 | } | |
610 | inode->disk_i_size = i_size; | |
8dd9872d | 611 | |
c8b97818 | 612 | fail: |
79b4f4c6 | 613 | return ret; |
c8b97818 CM |
614 | } |
615 | ||
616 | ||
617 | /* | |
618 | * conditionally insert an inline extent into the file. This | |
619 | * does the checks required to make sure the data is small enough | |
620 | * to fit as an inline extent. | |
621 | */ | |
8dd9872d OS |
622 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, |
623 | size_t compressed_size, | |
00361589 | 624 | int compress_type, |
d9496e8a OS |
625 | struct page **compressed_pages, |
626 | bool update_i_size) | |
c8b97818 | 627 | { |
5893dfb9 | 628 | struct btrfs_drop_extents_args drop_args = { 0 }; |
a0349401 | 629 | struct btrfs_root *root = inode->root; |
0b246afa | 630 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 631 | struct btrfs_trans_handle *trans; |
8dd9872d | 632 | u64 data_len = (compressed_size ?: size); |
c8b97818 | 633 | int ret; |
1acae57b | 634 | struct btrfs_path *path; |
c8b97818 | 635 | |
8dd9872d OS |
636 | /* |
637 | * We can create an inline extent if it ends at or beyond the current | |
638 | * i_size, is no larger than a sector (decompressed), and the (possibly | |
639 | * compressed) data fits in a leaf and the configured maximum inline | |
640 | * size. | |
641 | */ | |
642 | if (size < i_size_read(&inode->vfs_inode) || | |
643 | size > fs_info->sectorsize || | |
0b246afa | 644 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
8dd9872d | 645 | data_len > fs_info->max_inline) |
c8b97818 | 646 | return 1; |
c8b97818 | 647 | |
1acae57b FDBM |
648 | path = btrfs_alloc_path(); |
649 | if (!path) | |
650 | return -ENOMEM; | |
651 | ||
00361589 | 652 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
653 | if (IS_ERR(trans)) { |
654 | btrfs_free_path(path); | |
00361589 | 655 | return PTR_ERR(trans); |
1acae57b | 656 | } |
a0349401 | 657 | trans->block_rsv = &inode->block_rsv; |
00361589 | 658 | |
5893dfb9 | 659 | drop_args.path = path; |
8dd9872d OS |
660 | drop_args.start = 0; |
661 | drop_args.end = fs_info->sectorsize; | |
5893dfb9 FM |
662 | drop_args.drop_cache = true; |
663 | drop_args.replace_extent = true; | |
8dd9872d | 664 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len); |
5893dfb9 | 665 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
00361589 | 666 | if (ret) { |
66642832 | 667 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
668 | goto out; |
669 | } | |
c8b97818 | 670 | |
8dd9872d OS |
671 | ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted, |
672 | size, compressed_size, compress_type, | |
d9496e8a | 673 | compressed_pages, update_i_size); |
2adcac1a | 674 | if (ret && ret != -ENOSPC) { |
66642832 | 675 | btrfs_abort_transaction(trans, ret); |
00361589 | 676 | goto out; |
2adcac1a | 677 | } else if (ret == -ENOSPC) { |
00361589 JB |
678 | ret = 1; |
679 | goto out; | |
79787eaa | 680 | } |
2adcac1a | 681 | |
8dd9872d | 682 | btrfs_update_inode_bytes(inode, size, drop_args.bytes_found); |
8b9d0322 | 683 | ret = btrfs_update_inode(trans, inode); |
2766ff61 FM |
684 | if (ret && ret != -ENOSPC) { |
685 | btrfs_abort_transaction(trans, ret); | |
686 | goto out; | |
687 | } else if (ret == -ENOSPC) { | |
688 | ret = 1; | |
689 | goto out; | |
690 | } | |
691 | ||
23e3337f | 692 | btrfs_set_inode_full_sync(inode); |
00361589 | 693 | out: |
94ed938a QW |
694 | /* |
695 | * Don't forget to free the reserved space, as for inlined extent | |
696 | * it won't count as data extent, free them directly here. | |
697 | * And at reserve time, it's always aligned to page size, so | |
698 | * just free one page here. | |
699 | */ | |
9e65bfca | 700 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE, NULL); |
1acae57b | 701 | btrfs_free_path(path); |
3a45bb20 | 702 | btrfs_end_transaction(trans); |
00361589 | 703 | return ret; |
c8b97818 CM |
704 | } |
705 | ||
771ed689 CM |
706 | struct async_extent { |
707 | u64 start; | |
708 | u64 ram_size; | |
709 | u64 compressed_size; | |
710 | struct page **pages; | |
711 | unsigned long nr_pages; | |
261507a0 | 712 | int compress_type; |
771ed689 CM |
713 | struct list_head list; |
714 | }; | |
715 | ||
97db1204 | 716 | struct async_chunk { |
99a81a44 | 717 | struct btrfs_inode *inode; |
771ed689 CM |
718 | struct page *locked_page; |
719 | u64 start; | |
720 | u64 end; | |
bf9486d6 | 721 | blk_opf_t write_flags; |
771ed689 | 722 | struct list_head extents; |
ec39f769 | 723 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 724 | struct btrfs_work work; |
9e895a8f | 725 | struct async_cow *async_cow; |
771ed689 CM |
726 | }; |
727 | ||
97db1204 | 728 | struct async_cow { |
97db1204 NB |
729 | atomic_t num_chunks; |
730 | struct async_chunk chunks[]; | |
771ed689 CM |
731 | }; |
732 | ||
97db1204 | 733 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
734 | u64 start, u64 ram_size, |
735 | u64 compressed_size, | |
736 | struct page **pages, | |
261507a0 LZ |
737 | unsigned long nr_pages, |
738 | int compress_type) | |
771ed689 CM |
739 | { |
740 | struct async_extent *async_extent; | |
741 | ||
742 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 743 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
744 | async_extent->start = start; |
745 | async_extent->ram_size = ram_size; | |
746 | async_extent->compressed_size = compressed_size; | |
747 | async_extent->pages = pages; | |
748 | async_extent->nr_pages = nr_pages; | |
261507a0 | 749 | async_extent->compress_type = compress_type; |
771ed689 CM |
750 | list_add_tail(&async_extent->list, &cow->extents); |
751 | return 0; | |
752 | } | |
753 | ||
42c16da6 QW |
754 | /* |
755 | * Check if the inode needs to be submitted to compression, based on mount | |
756 | * options, defragmentation, properties or heuristics. | |
757 | */ | |
808a1292 NB |
758 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
759 | u64 end) | |
f79707b0 | 760 | { |
808a1292 | 761 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 762 | |
e6f9d696 | 763 | if (!btrfs_inode_can_compress(inode)) { |
42c16da6 QW |
764 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
765 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 766 | btrfs_ino(inode)); |
42c16da6 QW |
767 | return 0; |
768 | } | |
0cf9b244 QW |
769 | /* |
770 | * Special check for subpage. | |
771 | * | |
772 | * We lock the full page then run each delalloc range in the page, thus | |
773 | * for the following case, we will hit some subpage specific corner case: | |
774 | * | |
775 | * 0 32K 64K | |
776 | * | |///////| |///////| | |
777 | * \- A \- B | |
778 | * | |
779 | * In above case, both range A and range B will try to unlock the full | |
780 | * page [0, 64K), causing the one finished later will have page | |
781 | * unlocked already, triggering various page lock requirement BUG_ON()s. | |
782 | * | |
783 | * So here we add an artificial limit that subpage compression can only | |
784 | * if the range is fully page aligned. | |
785 | * | |
786 | * In theory we only need to ensure the first page is fully covered, but | |
787 | * the tailing partial page will be locked until the full compression | |
788 | * finishes, delaying the write of other range. | |
789 | * | |
790 | * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range | |
791 | * first to prevent any submitted async extent to unlock the full page. | |
792 | * By this, we can ensure for subpage case that only the last async_cow | |
793 | * will unlock the full page. | |
794 | */ | |
795 | if (fs_info->sectorsize < PAGE_SIZE) { | |
1280d2d1 FK |
796 | if (!PAGE_ALIGNED(start) || |
797 | !PAGE_ALIGNED(end + 1)) | |
0cf9b244 QW |
798 | return 0; |
799 | } | |
800 | ||
f79707b0 | 801 | /* force compress */ |
0b246afa | 802 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 803 | return 1; |
eec63c65 | 804 | /* defrag ioctl */ |
808a1292 | 805 | if (inode->defrag_compress) |
eec63c65 | 806 | return 1; |
f79707b0 | 807 | /* bad compression ratios */ |
808a1292 | 808 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 809 | return 0; |
0b246afa | 810 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
811 | inode->flags & BTRFS_INODE_COMPRESS || |
812 | inode->prop_compress) | |
813 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
814 | return 0; |
815 | } | |
816 | ||
6158e1ce | 817 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
558732df | 818 | u64 start, u64 end, u64 num_bytes, u32 small_write) |
26d30f85 AJ |
819 | { |
820 | /* If this is a small write inside eof, kick off a defrag */ | |
821 | if (num_bytes < small_write && | |
6158e1ce | 822 | (start > 0 || end + 1 < inode->disk_i_size)) |
558732df | 823 | btrfs_add_inode_defrag(NULL, inode, small_write); |
26d30f85 AJ |
824 | } |
825 | ||
d352ac68 | 826 | /* |
c15d8cf2 | 827 | * Work queue call back to started compression on a file and pages. |
c8b97818 | 828 | * |
c15d8cf2 CH |
829 | * This is done inside an ordered work queue, and the compression is spread |
830 | * across many cpus. The actual IO submission is step two, and the ordered work | |
831 | * queue takes care of making sure that happens in the same order things were | |
832 | * put onto the queue by writepages and friends. | |
c8b97818 | 833 | * |
c15d8cf2 CH |
834 | * If this code finds it can't get good compression, it puts an entry onto the |
835 | * work queue to write the uncompressed bytes. This makes sure that both | |
836 | * compressed inodes and uncompressed inodes are written in the same order that | |
837 | * the flusher thread sent them down. | |
d352ac68 | 838 | */ |
c15d8cf2 | 839 | static void compress_file_range(struct btrfs_work *work) |
b888db2b | 840 | { |
c15d8cf2 CH |
841 | struct async_chunk *async_chunk = |
842 | container_of(work, struct async_chunk, work); | |
99a01bd6 DS |
843 | struct btrfs_inode *inode = async_chunk->inode; |
844 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
a994310a | 845 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
0b246afa | 846 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
847 | u64 start = async_chunk->start; |
848 | u64 end = async_chunk->end; | |
c8b97818 | 849 | u64 actual_end; |
d98da499 | 850 | u64 i_size; |
e6dcd2dc | 851 | int ret = 0; |
e94e54e8 | 852 | struct page **pages; |
c8b97818 | 853 | unsigned long nr_pages; |
c8b97818 CM |
854 | unsigned long total_compressed = 0; |
855 | unsigned long total_in = 0; | |
e94e54e8 | 856 | unsigned int poff; |
c8b97818 | 857 | int i; |
0b246afa | 858 | int compress_type = fs_info->compress_type; |
b888db2b | 859 | |
99a01bd6 | 860 | inode_should_defrag(inode, start, end, end - start + 1, SZ_16K); |
4cb5300b | 861 | |
44962ca3 CH |
862 | /* |
863 | * We need to call clear_page_dirty_for_io on each page in the range. | |
864 | * Otherwise applications with the file mmap'd can wander in and change | |
865 | * the page contents while we are compressing them. | |
866 | */ | |
867 | extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end); | |
868 | ||
d98da499 JB |
869 | /* |
870 | * We need to save i_size before now because it could change in between | |
871 | * us evaluating the size and assigning it. This is because we lock and | |
872 | * unlock the page in truncate and fallocate, and then modify the i_size | |
873 | * later on. | |
874 | * | |
875 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
876 | * does that for us. | |
877 | */ | |
878 | barrier(); | |
99a01bd6 | 879 | i_size = i_size_read(&inode->vfs_inode); |
d98da499 JB |
880 | barrier(); |
881 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 | 882 | again: |
e94e54e8 | 883 | pages = NULL; |
09cbfeaf | 884 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
544fe4a9 | 885 | nr_pages = min_t(unsigned long, nr_pages, BTRFS_MAX_COMPRESSED_PAGES); |
be20aa9d | 886 | |
f03d9301 CM |
887 | /* |
888 | * we don't want to send crud past the end of i_size through | |
889 | * compression, that's just a waste of CPU time. So, if the | |
890 | * end of the file is before the start of our current | |
891 | * requested range of bytes, we bail out to the uncompressed | |
892 | * cleanup code that can deal with all of this. | |
893 | * | |
894 | * It isn't really the fastest way to fix things, but this is a | |
895 | * very uncommon corner. | |
896 | */ | |
897 | if (actual_end <= start) | |
898 | goto cleanup_and_bail_uncompressed; | |
899 | ||
c8b97818 CM |
900 | total_compressed = actual_end - start; |
901 | ||
4bcbb332 | 902 | /* |
0cf9b244 | 903 | * Skip compression for a small file range(<=blocksize) that |
01327610 | 904 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
905 | */ |
906 | if (total_compressed <= blocksize && | |
99a01bd6 | 907 | (start > 0 || end + 1 < inode->disk_i_size)) |
4bcbb332 SW |
908 | goto cleanup_and_bail_uncompressed; |
909 | ||
0cf9b244 QW |
910 | /* |
911 | * For subpage case, we require full page alignment for the sector | |
912 | * aligned range. | |
913 | * Thus we must also check against @actual_end, not just @end. | |
914 | */ | |
915 | if (blocksize < PAGE_SIZE) { | |
1280d2d1 FK |
916 | if (!PAGE_ALIGNED(start) || |
917 | !PAGE_ALIGNED(round_up(actual_end, blocksize))) | |
0cf9b244 QW |
918 | goto cleanup_and_bail_uncompressed; |
919 | } | |
920 | ||
069eac78 DS |
921 | total_compressed = min_t(unsigned long, total_compressed, |
922 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
923 | total_in = 0; |
924 | ret = 0; | |
db94535d | 925 | |
771ed689 | 926 | /* |
e94e54e8 CH |
927 | * We do compression for mount -o compress and when the inode has not |
928 | * been flagged as NOCOMPRESS. This flag can change at any time if we | |
929 | * discover bad compression ratios. | |
c8b97818 | 930 | */ |
e94e54e8 | 931 | if (!inode_need_compress(inode, start, end)) |
6a7167bf | 932 | goto cleanup_and_bail_uncompressed; |
261507a0 | 933 | |
e94e54e8 CH |
934 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
935 | if (!pages) { | |
4adaa611 | 936 | /* |
e94e54e8 CH |
937 | * Memory allocation failure is not a fatal error, we can fall |
938 | * back to uncompressed code. | |
4adaa611 | 939 | */ |
6a7167bf | 940 | goto cleanup_and_bail_uncompressed; |
e94e54e8 | 941 | } |
f51d2b59 | 942 | |
e94e54e8 CH |
943 | if (inode->defrag_compress) |
944 | compress_type = inode->defrag_compress; | |
945 | else if (inode->prop_compress) | |
946 | compress_type = inode->prop_compress; | |
947 | ||
e94e54e8 CH |
948 | /* Compression level is applied here. */ |
949 | ret = btrfs_compress_pages(compress_type | (fs_info->compress_level << 4), | |
950 | mapping, start, pages, &nr_pages, &total_in, | |
951 | &total_compressed); | |
952 | if (ret) | |
184aa1ff | 953 | goto mark_incompressible; |
c8b97818 | 954 | |
e94e54e8 CH |
955 | /* |
956 | * Zero the tail end of the last page, as we might be sending it down | |
957 | * to disk. | |
958 | */ | |
959 | poff = offset_in_page(total_compressed); | |
960 | if (poff) | |
961 | memzero_page(pages[nr_pages - 1], poff, PAGE_SIZE - poff); | |
c8b97818 | 962 | |
7367253a | 963 | /* |
6a7167bf CH |
964 | * Try to create an inline extent. |
965 | * | |
966 | * If we didn't compress the entire range, try to create an uncompressed | |
967 | * inline extent, else a compressed one. | |
968 | * | |
7367253a | 969 | * Check cow_file_range() for why we don't even try to create inline |
e94e54e8 | 970 | * extent for the subpage case. |
7367253a QW |
971 | */ |
972 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
6a7167bf CH |
973 | if (total_in < actual_end) { |
974 | ret = cow_file_range_inline(inode, actual_end, 0, | |
975 | BTRFS_COMPRESS_NONE, NULL, | |
976 | false); | |
c8b97818 | 977 | } else { |
99a01bd6 | 978 | ret = cow_file_range_inline(inode, actual_end, |
fe3f566c | 979 | total_compressed, |
d9496e8a OS |
980 | compress_type, pages, |
981 | false); | |
c8b97818 | 982 | } |
79787eaa | 983 | if (ret <= 0) { |
151a41bc | 984 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
985 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
986 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 | 987 | |
a994310a CH |
988 | if (ret < 0) |
989 | mapping_set_error(mapping, -EIO); | |
151a41bc | 990 | |
771ed689 | 991 | /* |
79787eaa JM |
992 | * inline extent creation worked or returned error, |
993 | * we don't need to create any more async work items. | |
994 | * Unlock and free up our temp pages. | |
8b62f87b JB |
995 | * |
996 | * We use DO_ACCOUNTING here because we need the | |
997 | * delalloc_release_metadata to be done _after_ we drop | |
998 | * our outstanding extent for clearing delalloc for this | |
999 | * range. | |
771ed689 | 1000 | */ |
99a01bd6 | 1001 | extent_clear_unlock_delalloc(inode, start, end, |
ad7ff17b | 1002 | NULL, |
74e9194a | 1003 | clear_flags, |
ba8b04c1 | 1004 | PAGE_UNLOCK | |
6869b0a8 | 1005 | PAGE_START_WRITEBACK | |
c2790a2e | 1006 | PAGE_END_WRITEBACK); |
f778b6b8 | 1007 | goto free_pages; |
c8b97818 CM |
1008 | } |
1009 | } | |
1010 | ||
e94e54e8 CH |
1011 | /* |
1012 | * We aren't doing an inline extent. Round the compressed size up to a | |
1013 | * block size boundary so the allocator does sane things. | |
1014 | */ | |
1015 | total_compressed = ALIGN(total_compressed, blocksize); | |
c8b97818 | 1016 | |
e94e54e8 CH |
1017 | /* |
1018 | * One last check to make sure the compression is really a win, compare | |
1019 | * the page count read with the blocks on disk, compression must free at | |
1020 | * least one sector. | |
1021 | */ | |
1022 | total_in = round_up(total_in, fs_info->sectorsize); | |
1023 | if (total_compressed + blocksize > total_in) | |
184aa1ff | 1024 | goto mark_incompressible; |
c8bb0c8b | 1025 | |
e94e54e8 CH |
1026 | /* |
1027 | * The async work queues will take care of doing actual allocation on | |
1028 | * disk for these compressed pages, and will submit the bios. | |
1029 | */ | |
1030 | add_async_extent(async_chunk, start, total_in, total_compressed, pages, | |
1031 | nr_pages, compress_type); | |
1032 | if (start + total_in < end) { | |
1033 | start += total_in; | |
1034 | cond_resched(); | |
1035 | goto again; | |
c8b97818 | 1036 | } |
e94e54e8 CH |
1037 | return; |
1038 | ||
184aa1ff CH |
1039 | mark_incompressible: |
1040 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress) | |
1041 | inode->flags |= BTRFS_INODE_NOCOMPRESS; | |
e94e54e8 | 1042 | cleanup_and_bail_uncompressed: |
b5326271 | 1043 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 1044 | BTRFS_COMPRESS_NONE); |
f778b6b8 | 1045 | free_pages: |
c8bb0c8b | 1046 | if (pages) { |
4d3a800e | 1047 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 1048 | WARN_ON(pages[i]->mapping); |
9ba965dc | 1049 | btrfs_free_compr_page(pages[i]); |
c8b97818 CM |
1050 | } |
1051 | kfree(pages); | |
c8b97818 | 1052 | } |
771ed689 | 1053 | } |
771ed689 | 1054 | |
40ae837b FM |
1055 | static void free_async_extent_pages(struct async_extent *async_extent) |
1056 | { | |
1057 | int i; | |
1058 | ||
1059 | if (!async_extent->pages) | |
1060 | return; | |
1061 | ||
1062 | for (i = 0; i < async_extent->nr_pages; i++) { | |
1063 | WARN_ON(async_extent->pages[i]->mapping); | |
9ba965dc | 1064 | btrfs_free_compr_page(async_extent->pages[i]); |
40ae837b FM |
1065 | } |
1066 | kfree(async_extent->pages); | |
1067 | async_extent->nr_pages = 0; | |
1068 | async_extent->pages = NULL; | |
771ed689 CM |
1069 | } |
1070 | ||
ff20d6a4 CH |
1071 | static void submit_uncompressed_range(struct btrfs_inode *inode, |
1072 | struct async_extent *async_extent, | |
1073 | struct page *locked_page) | |
771ed689 | 1074 | { |
2b83a0ee QW |
1075 | u64 start = async_extent->start; |
1076 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
2b83a0ee | 1077 | int ret; |
7027f871 CH |
1078 | struct writeback_control wbc = { |
1079 | .sync_mode = WB_SYNC_ALL, | |
1080 | .range_start = start, | |
1081 | .range_end = end, | |
1082 | .no_cgroup_owner = 1, | |
1083 | }; | |
771ed689 | 1084 | |
256b0cf9 CH |
1085 | wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode); |
1086 | ret = run_delalloc_cow(inode, locked_page, start, end, &wbc, false); | |
1087 | wbc_detach_inode(&wbc); | |
2b83a0ee | 1088 | if (ret < 0) { |
71aa147b NA |
1089 | btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1); |
1090 | if (locked_page) { | |
1091 | const u64 page_start = page_offset(locked_page); | |
71aa147b | 1092 | |
71aa147b NA |
1093 | set_page_writeback(locked_page); |
1094 | end_page_writeback(locked_page); | |
9783e4de CH |
1095 | btrfs_mark_ordered_io_finished(inode, locked_page, |
1096 | page_start, PAGE_SIZE, | |
1097 | !ret); | |
9783e4de | 1098 | mapping_set_error(locked_page->mapping, ret); |
2b83a0ee | 1099 | unlock_page(locked_page); |
71aa147b | 1100 | } |
2b83a0ee | 1101 | } |
2b83a0ee | 1102 | } |
79787eaa | 1103 | |
84f262f0 CH |
1104 | static void submit_one_async_extent(struct async_chunk *async_chunk, |
1105 | struct async_extent *async_extent, | |
1106 | u64 *alloc_hint) | |
771ed689 | 1107 | { |
84f262f0 | 1108 | struct btrfs_inode *inode = async_chunk->inode; |
b4ccace8 QW |
1109 | struct extent_io_tree *io_tree = &inode->io_tree; |
1110 | struct btrfs_root *root = inode->root; | |
1111 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d611935b | 1112 | struct btrfs_ordered_extent *ordered; |
771ed689 | 1113 | struct btrfs_key ins; |
2b83a0ee | 1114 | struct page *locked_page = NULL; |
771ed689 | 1115 | struct extent_map *em; |
f5a84ee3 | 1116 | int ret = 0; |
b4ccace8 QW |
1117 | u64 start = async_extent->start; |
1118 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
771ed689 | 1119 | |
896d7c1a CH |
1120 | if (async_chunk->blkcg_css) |
1121 | kthread_associate_blkcg(async_chunk->blkcg_css); | |
1122 | ||
2b83a0ee QW |
1123 | /* |
1124 | * If async_chunk->locked_page is in the async_extent range, we need to | |
1125 | * handle it. | |
1126 | */ | |
1127 | if (async_chunk->locked_page) { | |
1128 | u64 locked_page_start = page_offset(async_chunk->locked_page); | |
1129 | u64 locked_page_end = locked_page_start + PAGE_SIZE - 1; | |
3e04e7f1 | 1130 | |
2b83a0ee QW |
1131 | if (!(start >= locked_page_end || end <= locked_page_start)) |
1132 | locked_page = async_chunk->locked_page; | |
b4ccace8 | 1133 | } |
570eb97b | 1134 | lock_extent(io_tree, start, end, NULL); |
ce62003f | 1135 | |
67583468 | 1136 | if (async_extent->compress_type == BTRFS_COMPRESS_NONE) { |
ff20d6a4 | 1137 | submit_uncompressed_range(inode, async_extent, locked_page); |
e43a6210 CH |
1138 | goto done; |
1139 | } | |
ce62003f | 1140 | |
b4ccace8 QW |
1141 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
1142 | async_extent->compressed_size, | |
1143 | async_extent->compressed_size, | |
1144 | 0, *alloc_hint, &ins, 1, 1); | |
1145 | if (ret) { | |
c2167754 | 1146 | /* |
b4ccace8 QW |
1147 | * Here we used to try again by going back to non-compressed |
1148 | * path for ENOSPC. But we can't reserve space even for | |
1149 | * compressed size, how could it work for uncompressed size | |
1150 | * which requires larger size? So here we directly go error | |
1151 | * path. | |
c2167754 | 1152 | */ |
b4ccace8 QW |
1153 | goto out_free; |
1154 | } | |
1155 | ||
1156 | /* Here we're doing allocation and writeback of the compressed pages */ | |
1157 | em = create_io_em(inode, start, | |
1158 | async_extent->ram_size, /* len */ | |
1159 | start, /* orig_start */ | |
1160 | ins.objectid, /* block_start */ | |
1161 | ins.offset, /* block_len */ | |
1162 | ins.offset, /* orig_block_len */ | |
1163 | async_extent->ram_size, /* ram_bytes */ | |
1164 | async_extent->compress_type, | |
1165 | BTRFS_ORDERED_COMPRESSED); | |
1166 | if (IS_ERR(em)) { | |
1167 | ret = PTR_ERR(em); | |
1168 | goto out_free_reserve; | |
1169 | } | |
1170 | free_extent_map(em); | |
771ed689 | 1171 | |
d611935b | 1172 | ordered = btrfs_alloc_ordered_extent(inode, start, /* file_offset */ |
cb36a9bb OS |
1173 | async_extent->ram_size, /* num_bytes */ |
1174 | async_extent->ram_size, /* ram_bytes */ | |
1175 | ins.objectid, /* disk_bytenr */ | |
1176 | ins.offset, /* disk_num_bytes */ | |
1177 | 0, /* offset */ | |
1178 | 1 << BTRFS_ORDERED_COMPRESSED, | |
1179 | async_extent->compress_type); | |
d611935b | 1180 | if (IS_ERR(ordered)) { |
4c0c8cfc | 1181 | btrfs_drop_extent_map_range(inode, start, end, false); |
d611935b | 1182 | ret = PTR_ERR(ordered); |
b4ccace8 | 1183 | goto out_free_reserve; |
771ed689 | 1184 | } |
b4ccace8 QW |
1185 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
1186 | ||
1187 | /* Clear dirty, set writeback and unlock the pages. */ | |
1188 | extent_clear_unlock_delalloc(inode, start, end, | |
1189 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, | |
1190 | PAGE_UNLOCK | PAGE_START_WRITEBACK); | |
d611935b | 1191 | btrfs_submit_compressed_write(ordered, |
b4ccace8 QW |
1192 | async_extent->pages, /* compressed_pages */ |
1193 | async_extent->nr_pages, | |
05d06a5c | 1194 | async_chunk->write_flags, true); |
b4ccace8 | 1195 | *alloc_hint = ins.objectid + ins.offset; |
e43a6210 | 1196 | done: |
896d7c1a CH |
1197 | if (async_chunk->blkcg_css) |
1198 | kthread_associate_blkcg(NULL); | |
b4ccace8 | 1199 | kfree(async_extent); |
84f262f0 | 1200 | return; |
b4ccace8 | 1201 | |
3e04e7f1 | 1202 | out_free_reserve: |
0b246afa | 1203 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1204 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1205 | out_free: |
a994310a | 1206 | mapping_set_error(inode->vfs_inode.i_mapping, -EIO); |
b4ccace8 | 1207 | extent_clear_unlock_delalloc(inode, start, end, |
c2790a2e | 1208 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 1209 | EXTENT_DELALLOC_NEW | |
151a41bc | 1210 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
6869b0a8 | 1211 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
a994310a | 1212 | PAGE_END_WRITEBACK); |
40ae837b | 1213 | free_async_extent_pages(async_extent); |
84f262f0 CH |
1214 | if (async_chunk->blkcg_css) |
1215 | kthread_associate_blkcg(NULL); | |
1216 | btrfs_debug(fs_info, | |
b4ccace8 | 1217 | "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d", |
84f262f0 CH |
1218 | root->root_key.objectid, btrfs_ino(inode), start, |
1219 | async_extent->ram_size, ret); | |
1220 | kfree(async_extent); | |
771ed689 CM |
1221 | } |
1222 | ||
43c69849 | 1223 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
1224 | u64 num_bytes) |
1225 | { | |
43c69849 | 1226 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
1227 | struct extent_map *em; |
1228 | u64 alloc_hint = 0; | |
1229 | ||
1230 | read_lock(&em_tree->lock); | |
1231 | em = search_extent_mapping(em_tree, start, num_bytes); | |
1232 | if (em) { | |
1233 | /* | |
1234 | * if block start isn't an actual block number then find the | |
1235 | * first block in this inode and use that as a hint. If that | |
1236 | * block is also bogus then just don't worry about it. | |
1237 | */ | |
1238 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
1239 | free_extent_map(em); | |
1240 | em = search_extent_mapping(em_tree, 0, 0); | |
1241 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
1242 | alloc_hint = em->block_start; | |
1243 | if (em) | |
1244 | free_extent_map(em); | |
1245 | } else { | |
1246 | alloc_hint = em->block_start; | |
1247 | free_extent_map(em); | |
1248 | } | |
1249 | } | |
1250 | read_unlock(&em_tree->lock); | |
1251 | ||
1252 | return alloc_hint; | |
1253 | } | |
1254 | ||
771ed689 CM |
1255 | /* |
1256 | * when extent_io.c finds a delayed allocation range in the file, | |
1257 | * the call backs end up in this code. The basic idea is to | |
1258 | * allocate extents on disk for the range, and create ordered data structs | |
1259 | * in ram to track those extents. | |
1260 | * | |
1261 | * locked_page is the page that writepage had locked already. We use | |
1262 | * it to make sure we don't do extra locks or unlocks. | |
1263 | * | |
ba9145ad | 1264 | * When this function fails, it unlocks all pages except @locked_page. |
9ce7466f | 1265 | * |
c56cbe90 CH |
1266 | * When this function successfully creates an inline extent, it returns 1 and |
1267 | * unlocks all pages including locked_page and starts I/O on them. | |
ba9145ad CH |
1268 | * (In reality inline extents are limited to a single page, so locked_page is |
1269 | * the only page handled anyway). | |
9ce7466f | 1270 | * |
ba9145ad CH |
1271 | * When this function succeed and creates a normal extent, the page locking |
1272 | * status depends on the passed in flags: | |
9ce7466f | 1273 | * |
ba9145ad CH |
1274 | * - If @keep_locked is set, all pages are kept locked. |
1275 | * - Else all pages except for @locked_page are unlocked. | |
9ce7466f NA |
1276 | * |
1277 | * When a failure happens in the second or later iteration of the | |
1278 | * while-loop, the ordered extents created in previous iterations are kept | |
1279 | * intact. So, the caller must clean them up by calling | |
1280 | * btrfs_cleanup_ordered_extents(). See btrfs_run_delalloc_range() for | |
1281 | * example. | |
771ed689 | 1282 | */ |
6e26c442 | 1283 | static noinline int cow_file_range(struct btrfs_inode *inode, |
c56cbe90 CH |
1284 | struct page *locked_page, u64 start, u64 end, |
1285 | u64 *done_offset, | |
53ffb30a | 1286 | bool keep_locked, bool no_inline) |
771ed689 | 1287 | { |
6e26c442 NB |
1288 | struct btrfs_root *root = inode->root; |
1289 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 | 1290 | u64 alloc_hint = 0; |
9ce7466f | 1291 | u64 orig_start = start; |
771ed689 CM |
1292 | u64 num_bytes; |
1293 | unsigned long ram_size; | |
a315e68f | 1294 | u64 cur_alloc_size = 0; |
432cd2a1 | 1295 | u64 min_alloc_size; |
0b246afa | 1296 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
1297 | struct btrfs_key ins; |
1298 | struct extent_map *em; | |
a315e68f FM |
1299 | unsigned clear_bits; |
1300 | unsigned long page_ops; | |
1301 | bool extent_reserved = false; | |
771ed689 CM |
1302 | int ret = 0; |
1303 | ||
6e26c442 | 1304 | if (btrfs_is_free_space_inode(inode)) { |
29bce2f3 JB |
1305 | ret = -EINVAL; |
1306 | goto out_unlock; | |
02ecd2c2 | 1307 | } |
771ed689 | 1308 | |
fda2832f | 1309 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1310 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1311 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1312 | |
6e26c442 | 1313 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1314 | |
7367253a QW |
1315 | /* |
1316 | * Due to the page size limit, for subpage we can only trigger the | |
1317 | * writeback for the dirty sectors of page, that means data writeback | |
1318 | * is doing more writeback than what we want. | |
1319 | * | |
1320 | * This is especially unexpected for some call sites like fallocate, | |
1321 | * where we only increase i_size after everything is done. | |
1322 | * This means we can trigger inline extent even if we didn't want to. | |
1323 | * So here we skip inline extent creation completely. | |
1324 | */ | |
53ffb30a | 1325 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE && !no_inline) { |
8dd9872d OS |
1326 | u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode), |
1327 | end + 1); | |
1328 | ||
771ed689 | 1329 | /* lets try to make an inline extent */ |
8dd9872d | 1330 | ret = cow_file_range_inline(inode, actual_end, 0, |
d9496e8a | 1331 | BTRFS_COMPRESS_NONE, NULL, false); |
771ed689 | 1332 | if (ret == 0) { |
8b62f87b JB |
1333 | /* |
1334 | * We use DO_ACCOUNTING here because we need the | |
1335 | * delalloc_release_metadata to be run _after_ we drop | |
1336 | * our outstanding extent for clearing delalloc for this | |
1337 | * range. | |
1338 | */ | |
4750af3b QW |
1339 | extent_clear_unlock_delalloc(inode, start, end, |
1340 | locked_page, | |
c2790a2e | 1341 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1342 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1343 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 1344 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
4750af3b QW |
1345 | /* |
1346 | * locked_page is locked by the caller of | |
1347 | * writepage_delalloc(), not locked by | |
1348 | * __process_pages_contig(). | |
1349 | * | |
1350 | * We can't let __process_pages_contig() to unlock it, | |
1351 | * as it doesn't have any subpage::writers recorded. | |
1352 | * | |
1353 | * Here we manually unlock the page, since the caller | |
c56cbe90 CH |
1354 | * can't determine if it's an inline extent or a |
1355 | * compressed extent. | |
4750af3b QW |
1356 | */ |
1357 | unlock_page(locked_page); | |
6e144bf1 CH |
1358 | ret = 1; |
1359 | goto done; | |
79787eaa | 1360 | } else if (ret < 0) { |
79787eaa | 1361 | goto out_unlock; |
771ed689 CM |
1362 | } |
1363 | } | |
1364 | ||
6e26c442 | 1365 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 | 1366 | |
432cd2a1 FM |
1367 | /* |
1368 | * Relocation relies on the relocated extents to have exactly the same | |
1369 | * size as the original extents. Normally writeback for relocation data | |
1370 | * extents follows a NOCOW path because relocation preallocates the | |
1371 | * extents. However, due to an operation such as scrub turning a block | |
1372 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1373 | * an extent allocated during COW has exactly the requested size and can | |
1374 | * not be split into smaller extents, otherwise relocation breaks and | |
1375 | * fails during the stage where it updates the bytenr of file extent | |
1376 | * items. | |
1377 | */ | |
37f00a6d | 1378 | if (btrfs_is_data_reloc_root(root)) |
432cd2a1 FM |
1379 | min_alloc_size = num_bytes; |
1380 | else | |
1381 | min_alloc_size = fs_info->sectorsize; | |
1382 | ||
3752d22f | 1383 | while (num_bytes > 0) { |
34bfaf15 CH |
1384 | struct btrfs_ordered_extent *ordered; |
1385 | ||
3752d22f | 1386 | cur_alloc_size = num_bytes; |
18513091 | 1387 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1388 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1389 | &ins, 1, 1); |
6e144bf1 CH |
1390 | if (ret == -EAGAIN) { |
1391 | /* | |
1392 | * btrfs_reserve_extent only returns -EAGAIN for zoned | |
1393 | * file systems, which is an indication that there are | |
1394 | * no active zones to allocate from at the moment. | |
1395 | * | |
1396 | * If this is the first loop iteration, wait for at | |
1397 | * least one zone to finish before retrying the | |
1398 | * allocation. Otherwise ask the caller to write out | |
1399 | * the already allocated blocks before coming back to | |
1400 | * us, or return -ENOSPC if it can't handle retries. | |
1401 | */ | |
1402 | ASSERT(btrfs_is_zoned(fs_info)); | |
1403 | if (start == orig_start) { | |
1404 | wait_on_bit_io(&inode->root->fs_info->flags, | |
1405 | BTRFS_FS_NEED_ZONE_FINISH, | |
1406 | TASK_UNINTERRUPTIBLE); | |
1407 | continue; | |
1408 | } | |
1409 | if (done_offset) { | |
1410 | *done_offset = start - 1; | |
1411 | return 0; | |
1412 | } | |
1413 | ret = -ENOSPC; | |
1414 | } | |
00361589 | 1415 | if (ret < 0) |
79787eaa | 1416 | goto out_unlock; |
a315e68f FM |
1417 | cur_alloc_size = ins.offset; |
1418 | extent_reserved = true; | |
d397712b | 1419 | |
771ed689 | 1420 | ram_size = ins.offset; |
6e26c442 | 1421 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1422 | start, /* orig_start */ |
1423 | ins.objectid, /* block_start */ | |
1424 | ins.offset, /* block_len */ | |
1425 | ins.offset, /* orig_block_len */ | |
1426 | ram_size, /* ram_bytes */ | |
1427 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1428 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1429 | if (IS_ERR(em)) { |
1430 | ret = PTR_ERR(em); | |
ace68bac | 1431 | goto out_reserve; |
090a127a | 1432 | } |
6f9994db | 1433 | free_extent_map(em); |
e6dcd2dc | 1434 | |
34bfaf15 CH |
1435 | ordered = btrfs_alloc_ordered_extent(inode, start, ram_size, |
1436 | ram_size, ins.objectid, cur_alloc_size, | |
1437 | 0, 1 << BTRFS_ORDERED_REGULAR, | |
1438 | BTRFS_COMPRESS_NONE); | |
1439 | if (IS_ERR(ordered)) { | |
1440 | ret = PTR_ERR(ordered); | |
d9f85963 | 1441 | goto out_drop_extent_cache; |
34bfaf15 | 1442 | } |
c8b97818 | 1443 | |
37f00a6d | 1444 | if (btrfs_is_data_reloc_root(root)) { |
34bfaf15 CH |
1445 | ret = btrfs_reloc_clone_csums(ordered); |
1446 | ||
4dbd80fb QW |
1447 | /* |
1448 | * Only drop cache here, and process as normal. | |
1449 | * | |
1450 | * We must not allow extent_clear_unlock_delalloc() | |
1451 | * at out_unlock label to free meta of this ordered | |
1452 | * extent, as its meta should be freed by | |
1453 | * btrfs_finish_ordered_io(). | |
1454 | * | |
1455 | * So we must continue until @start is increased to | |
1456 | * skip current ordered extent. | |
1457 | */ | |
00361589 | 1458 | if (ret) |
4c0c8cfc FM |
1459 | btrfs_drop_extent_map_range(inode, start, |
1460 | start + ram_size - 1, | |
1461 | false); | |
17d217fe | 1462 | } |
34bfaf15 | 1463 | btrfs_put_ordered_extent(ordered); |
17d217fe | 1464 | |
0b246afa | 1465 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1466 | |
f57ad937 QW |
1467 | /* |
1468 | * We're not doing compressed IO, don't unlock the first page | |
1469 | * (which the caller expects to stay locked), don't clear any | |
1470 | * dirty bits and don't set any writeback bits | |
8b62b72b | 1471 | * |
f57ad937 QW |
1472 | * Do set the Ordered (Private2) bit so we know this page was |
1473 | * properly setup for writepage. | |
c8b97818 | 1474 | */ |
ba9145ad | 1475 | page_ops = (keep_locked ? 0 : PAGE_UNLOCK); |
f57ad937 | 1476 | page_ops |= PAGE_SET_ORDERED; |
a791e35e | 1477 | |
6e26c442 | 1478 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1479 | locked_page, |
c2790a2e | 1480 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1481 | page_ops); |
3752d22f AJ |
1482 | if (num_bytes < cur_alloc_size) |
1483 | num_bytes = 0; | |
4dbd80fb | 1484 | else |
3752d22f | 1485 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1486 | alloc_hint = ins.objectid + ins.offset; |
1487 | start += cur_alloc_size; | |
a315e68f | 1488 | extent_reserved = false; |
4dbd80fb QW |
1489 | |
1490 | /* | |
1491 | * btrfs_reloc_clone_csums() error, since start is increased | |
1492 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1493 | * free metadata of current ordered extent, we're OK to exit. | |
1494 | */ | |
1495 | if (ret) | |
1496 | goto out_unlock; | |
b888db2b | 1497 | } |
6e144bf1 CH |
1498 | done: |
1499 | if (done_offset) | |
1500 | *done_offset = end; | |
be20aa9d | 1501 | return ret; |
b7d5b0a8 | 1502 | |
d9f85963 | 1503 | out_drop_extent_cache: |
4c0c8cfc | 1504 | btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false); |
ace68bac | 1505 | out_reserve: |
0b246afa | 1506 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1507 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1508 | out_unlock: |
9ce7466f NA |
1509 | /* |
1510 | * Now, we have three regions to clean up: | |
1511 | * | |
1512 | * |-------(1)----|---(2)---|-------------(3)----------| | |
1513 | * `- orig_start `- start `- start + cur_alloc_size `- end | |
1514 | * | |
1515 | * We process each region below. | |
1516 | */ | |
1517 | ||
a7e3b975 FM |
1518 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1519 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
6869b0a8 | 1520 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
9ce7466f | 1521 | |
a315e68f | 1522 | /* |
9ce7466f NA |
1523 | * For the range (1). We have already instantiated the ordered extents |
1524 | * for this region. They are cleaned up by | |
1525 | * btrfs_cleanup_ordered_extents() in e.g, | |
1526 | * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are | |
1527 | * already cleared in the above loop. And, EXTENT_DELALLOC_NEW | | |
1528 | * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup | |
1529 | * function. | |
1530 | * | |
ba9145ad | 1531 | * However, in case of @keep_locked, we still need to unlock the pages |
9ce7466f NA |
1532 | * (except @locked_page) to ensure all the pages are unlocked. |
1533 | */ | |
ba9145ad | 1534 | if (keep_locked && orig_start < start) { |
71aa147b NA |
1535 | if (!locked_page) |
1536 | mapping_set_error(inode->vfs_inode.i_mapping, ret); | |
9ce7466f NA |
1537 | extent_clear_unlock_delalloc(inode, orig_start, start - 1, |
1538 | locked_page, 0, page_ops); | |
71aa147b | 1539 | } |
9ce7466f | 1540 | |
a315e68f | 1541 | /* |
9ce7466f NA |
1542 | * For the range (2). If we reserved an extent for our delalloc range |
1543 | * (or a subrange) and failed to create the respective ordered extent, | |
1544 | * then it means that when we reserved the extent we decremented the | |
1545 | * extent's size from the data space_info's bytes_may_use counter and | |
1546 | * incremented the space_info's bytes_reserved counter by the same | |
1547 | * amount. We must make sure extent_clear_unlock_delalloc() does not try | |
1548 | * to decrement again the data space_info's bytes_may_use counter, | |
1549 | * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV. | |
a315e68f FM |
1550 | */ |
1551 | if (extent_reserved) { | |
6e26c442 | 1552 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1553 | start + cur_alloc_size - 1, |
a315e68f FM |
1554 | locked_page, |
1555 | clear_bits, | |
1556 | page_ops); | |
1557 | start += cur_alloc_size; | |
a315e68f | 1558 | } |
9ce7466f NA |
1559 | |
1560 | /* | |
1561 | * For the range (3). We never touched the region. In addition to the | |
1562 | * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data | |
1563 | * space_info's bytes_may_use counter, reserved in | |
1564 | * btrfs_check_data_free_space(). | |
1565 | */ | |
12b2d64e CH |
1566 | if (start < end) { |
1567 | clear_bits |= EXTENT_CLEAR_DATA_RESV; | |
1568 | extent_clear_unlock_delalloc(inode, start, end, locked_page, | |
1569 | clear_bits, page_ops); | |
1570 | } | |
aaafa1eb | 1571 | return ret; |
771ed689 | 1572 | } |
c8b97818 | 1573 | |
771ed689 | 1574 | /* |
c15d8cf2 CH |
1575 | * Phase two of compressed writeback. This is the ordered portion of the code, |
1576 | * which only gets called in the order the work was queued. We walk all the | |
1577 | * async extents created by compress_file_range and send them down to the disk. | |
078b8b90 DS |
1578 | * |
1579 | * If called with @do_free == true then it'll try to finish the work and free | |
1580 | * the work struct eventually. | |
771ed689 | 1581 | */ |
078b8b90 | 1582 | static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_free) |
771ed689 | 1583 | { |
c5a68aec NB |
1584 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1585 | work); | |
1586 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
00d31d17 | 1587 | struct async_extent *async_extent; |
771ed689 | 1588 | unsigned long nr_pages; |
00d31d17 | 1589 | u64 alloc_hint = 0; |
771ed689 | 1590 | |
078b8b90 DS |
1591 | if (do_free) { |
1592 | struct async_chunk *async_chunk; | |
1593 | struct async_cow *async_cow; | |
1594 | ||
1595 | async_chunk = container_of(work, struct async_chunk, work); | |
1596 | btrfs_add_delayed_iput(async_chunk->inode); | |
1597 | if (async_chunk->blkcg_css) | |
1598 | css_put(async_chunk->blkcg_css); | |
1599 | ||
1600 | async_cow = async_chunk->async_cow; | |
1601 | if (atomic_dec_and_test(&async_cow->num_chunks)) | |
1602 | kvfree(async_cow); | |
1603 | return; | |
1604 | } | |
1605 | ||
b5326271 | 1606 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1607 | PAGE_SHIFT; |
771ed689 | 1608 | |
00d31d17 CH |
1609 | while (!list_empty(&async_chunk->extents)) { |
1610 | async_extent = list_entry(async_chunk->extents.next, | |
1611 | struct async_extent, list); | |
1612 | list_del(&async_extent->list); | |
1613 | submit_one_async_extent(async_chunk, async_extent, &alloc_hint); | |
1614 | } | |
ac98141d JB |
1615 | |
1616 | /* atomic_sub_return implies a barrier */ | |
1617 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < | |
1618 | 5 * SZ_1M) | |
1619 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1620 | } |
c8b97818 | 1621 | |
bb7b05fe | 1622 | static bool run_delalloc_compressed(struct btrfs_inode *inode, |
c56cbe90 CH |
1623 | struct page *locked_page, u64 start, |
1624 | u64 end, struct writeback_control *wbc) | |
771ed689 | 1625 | { |
751b6431 | 1626 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1627 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1628 | struct async_cow *ctx; |
1629 | struct async_chunk *async_chunk; | |
771ed689 | 1630 | unsigned long nr_pages; |
97db1204 NB |
1631 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1632 | int i; | |
b1c16ac9 | 1633 | unsigned nofs_flag; |
bf9486d6 | 1634 | const blk_opf_t write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1635 | |
b1c16ac9 NB |
1636 | nofs_flag = memalloc_nofs_save(); |
1637 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1638 | memalloc_nofs_restore(nofs_flag); | |
973fb26e CH |
1639 | if (!ctx) |
1640 | return false; | |
b1c16ac9 | 1641 | |
973fb26e CH |
1642 | unlock_extent(&inode->io_tree, start, end, NULL); |
1643 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); | |
97db1204 NB |
1644 | |
1645 | async_chunk = ctx->chunks; | |
1646 | atomic_set(&ctx->num_chunks, num_chunks); | |
1647 | ||
1648 | for (i = 0; i < num_chunks; i++) { | |
973fb26e | 1649 | u64 cur_end = min(end, start + SZ_512K - 1); |
771ed689 | 1650 | |
bd4691a0 NB |
1651 | /* |
1652 | * igrab is called higher up in the call chain, take only the | |
1653 | * lightweight reference for the callback lifetime | |
1654 | */ | |
751b6431 | 1655 | ihold(&inode->vfs_inode); |
9e895a8f | 1656 | async_chunk[i].async_cow = ctx; |
99a81a44 | 1657 | async_chunk[i].inode = inode; |
97db1204 NB |
1658 | async_chunk[i].start = start; |
1659 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1660 | async_chunk[i].write_flags = write_flags; |
1661 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1662 | ||
1d53c9e6 CM |
1663 | /* |
1664 | * The locked_page comes all the way from writepage and its | |
1665 | * the original page we were actually given. As we spread | |
1666 | * this large delalloc region across multiple async_chunk | |
1667 | * structs, only the first struct needs a pointer to locked_page | |
1668 | * | |
1669 | * This way we don't need racey decisions about who is supposed | |
1670 | * to unlock it. | |
1671 | */ | |
1672 | if (locked_page) { | |
ec39f769 CM |
1673 | /* |
1674 | * Depending on the compressibility, the pages might or | |
1675 | * might not go through async. We want all of them to | |
1676 | * be accounted against wbc once. Let's do it here | |
1677 | * before the paths diverge. wbc accounting is used | |
1678 | * only for foreign writeback detection and doesn't | |
1679 | * need full accuracy. Just account the whole thing | |
1680 | * against the first page. | |
1681 | */ | |
1682 | wbc_account_cgroup_owner(wbc, locked_page, | |
1683 | cur_end - start); | |
1d53c9e6 CM |
1684 | async_chunk[i].locked_page = locked_page; |
1685 | locked_page = NULL; | |
1686 | } else { | |
1687 | async_chunk[i].locked_page = NULL; | |
1688 | } | |
1689 | ||
ec39f769 CM |
1690 | if (blkcg_css != blkcg_root_css) { |
1691 | css_get(blkcg_css); | |
1692 | async_chunk[i].blkcg_css = blkcg_css; | |
3480373e | 1693 | async_chunk[i].write_flags |= REQ_BTRFS_CGROUP_PUNT; |
ec39f769 CM |
1694 | } else { |
1695 | async_chunk[i].blkcg_css = NULL; | |
1696 | } | |
1697 | ||
c15d8cf2 | 1698 | btrfs_init_work(&async_chunk[i].work, compress_file_range, |
078b8b90 | 1699 | submit_compressed_extents); |
771ed689 | 1700 | |
97db1204 | 1701 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1702 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1703 | |
97db1204 | 1704 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1705 | |
771ed689 CM |
1706 | start = cur_end + 1; |
1707 | } | |
973fb26e | 1708 | return true; |
be20aa9d CM |
1709 | } |
1710 | ||
256b0cf9 CH |
1711 | /* |
1712 | * Run the delalloc range from start to end, and write back any dirty pages | |
1713 | * covered by the range. | |
1714 | */ | |
1715 | static noinline int run_delalloc_cow(struct btrfs_inode *inode, | |
1716 | struct page *locked_page, u64 start, | |
1717 | u64 end, struct writeback_control *wbc, | |
1718 | bool pages_dirty) | |
42c01100 | 1719 | { |
898793d9 | 1720 | u64 done_offset = end; |
42c01100 NA |
1721 | int ret; |
1722 | ||
898793d9 | 1723 | while (start <= end) { |
c56cbe90 CH |
1724 | ret = cow_file_range(inode, locked_page, start, end, &done_offset, |
1725 | true, false); | |
6e144bf1 | 1726 | if (ret) |
898793d9 | 1727 | return ret; |
778b8785 | 1728 | extent_write_locked_range(&inode->vfs_inode, locked_page, start, |
256b0cf9 | 1729 | done_offset, wbc, pages_dirty); |
898793d9 NA |
1730 | start = done_offset + 1; |
1731 | } | |
42c01100 | 1732 | |
c56cbe90 | 1733 | return 1; |
42c01100 NA |
1734 | } |
1735 | ||
2ff7e61e | 1736 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
26ce9114 | 1737 | u64 bytenr, u64 num_bytes, bool nowait) |
17d217fe | 1738 | { |
fc28b25e | 1739 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); |
17d217fe | 1740 | struct btrfs_ordered_sum *sums; |
fc28b25e | 1741 | int ret; |
17d217fe YZ |
1742 | LIST_HEAD(list); |
1743 | ||
97e38239 QW |
1744 | ret = btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, |
1745 | &list, 0, nowait); | |
17d217fe YZ |
1746 | if (ret == 0 && list_empty(&list)) |
1747 | return 0; | |
1748 | ||
1749 | while (!list_empty(&list)) { | |
1750 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1751 | list_del(&sums->list); | |
1752 | kfree(sums); | |
1753 | } | |
58113753 LB |
1754 | if (ret < 0) |
1755 | return ret; | |
17d217fe YZ |
1756 | return 1; |
1757 | } | |
1758 | ||
8ba96f3d | 1759 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
53ffb30a | 1760 | const u64 start, const u64 end) |
467dc47e | 1761 | { |
8ba96f3d | 1762 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
37f00a6d | 1763 | const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); |
2166e5ed | 1764 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1765 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1766 | u64 range_start = start; |
1767 | u64 count; | |
53ffb30a | 1768 | int ret; |
467dc47e FM |
1769 | |
1770 | /* | |
1771 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1772 | * made we had not enough available data space and therefore we did not | |
1773 | * reserve data space for it, since we though we could do NOCOW for the | |
1774 | * respective file range (either there is prealloc extent or the inode | |
1775 | * has the NOCOW bit set). | |
1776 | * | |
1777 | * However when we need to fallback to COW mode (because for example the | |
1778 | * block group for the corresponding extent was turned to RO mode by a | |
1779 | * scrub or relocation) we need to do the following: | |
1780 | * | |
1781 | * 1) We increment the bytes_may_use counter of the data space info. | |
1782 | * If COW succeeds, it allocates a new data extent and after doing | |
1783 | * that it decrements the space info's bytes_may_use counter and | |
1784 | * increments its bytes_reserved counter by the same amount (we do | |
1785 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1786 | * bytes_may_use counter to compensate (when space is reserved at | |
1787 | * buffered write time, the bytes_may_use counter is incremented); | |
1788 | * | |
1789 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1790 | * that if the COW path fails for any reason, it decrements (through | |
1791 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1792 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1793 | * |
1794 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1795 | * space cache inode or an inode of the data relocation tree, we must |
1796 | * also increment bytes_may_use of the data space_info for the same | |
1797 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1798 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1799 | * group that contains that extent to RO mode and therefore force COW |
1800 | * when starting writeback. | |
467dc47e | 1801 | */ |
2166e5ed | 1802 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
8c6e53a7 | 1803 | EXTENT_NORESERVE, 0, NULL); |
6bd335b4 FM |
1804 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1805 | u64 bytes = count; | |
8ba96f3d | 1806 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1807 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1808 | ||
6bd335b4 FM |
1809 | if (is_space_ino || is_reloc_ino) |
1810 | bytes = range_bytes; | |
1811 | ||
467dc47e | 1812 | spin_lock(&sinfo->lock); |
2166e5ed | 1813 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1814 | spin_unlock(&sinfo->lock); |
1815 | ||
2166e5ed FM |
1816 | if (count > 0) |
1817 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
bd015294 | 1818 | NULL); |
467dc47e FM |
1819 | } |
1820 | ||
53ffb30a CH |
1821 | /* |
1822 | * Don't try to create inline extents, as a mix of inline extent that | |
1823 | * is written out and unlocked directly and a normal NOCOW extent | |
1824 | * doesn't work. | |
1825 | */ | |
c56cbe90 CH |
1826 | ret = cow_file_range(inode, locked_page, start, end, NULL, false, true); |
1827 | ASSERT(ret != 1); | |
53ffb30a | 1828 | return ret; |
467dc47e FM |
1829 | } |
1830 | ||
619104ba FM |
1831 | struct can_nocow_file_extent_args { |
1832 | /* Input fields. */ | |
1833 | ||
1834 | /* Start file offset of the range we want to NOCOW. */ | |
1835 | u64 start; | |
1836 | /* End file offset (inclusive) of the range we want to NOCOW. */ | |
1837 | u64 end; | |
1838 | bool writeback_path; | |
1839 | bool strict; | |
1840 | /* | |
1841 | * Free the path passed to can_nocow_file_extent() once it's not needed | |
1842 | * anymore. | |
1843 | */ | |
1844 | bool free_path; | |
1845 | ||
1846 | /* Output fields. Only set when can_nocow_file_extent() returns 1. */ | |
1847 | ||
1848 | u64 disk_bytenr; | |
1849 | u64 disk_num_bytes; | |
1850 | u64 extent_offset; | |
1851 | /* Number of bytes that can be written to in NOCOW mode. */ | |
1852 | u64 num_bytes; | |
1853 | }; | |
1854 | ||
1855 | /* | |
1856 | * Check if we can NOCOW the file extent that the path points to. | |
1857 | * This function may return with the path released, so the caller should check | |
1858 | * if path->nodes[0] is NULL or not if it needs to use the path afterwards. | |
1859 | * | |
1860 | * Returns: < 0 on error | |
1861 | * 0 if we can not NOCOW | |
1862 | * 1 if we can NOCOW | |
1863 | */ | |
1864 | static int can_nocow_file_extent(struct btrfs_path *path, | |
1865 | struct btrfs_key *key, | |
1866 | struct btrfs_inode *inode, | |
1867 | struct can_nocow_file_extent_args *args) | |
1868 | { | |
1869 | const bool is_freespace_inode = btrfs_is_free_space_inode(inode); | |
1870 | struct extent_buffer *leaf = path->nodes[0]; | |
1871 | struct btrfs_root *root = inode->root; | |
1872 | struct btrfs_file_extent_item *fi; | |
1873 | u64 extent_end; | |
1874 | u8 extent_type; | |
1875 | int can_nocow = 0; | |
1876 | int ret = 0; | |
26ce9114 | 1877 | bool nowait = path->nowait; |
619104ba FM |
1878 | |
1879 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
1880 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1881 | ||
1882 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) | |
1883 | goto out; | |
1884 | ||
1885 | /* Can't access these fields unless we know it's not an inline extent. */ | |
1886 | args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1887 | args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
1888 | args->extent_offset = btrfs_file_extent_offset(leaf, fi); | |
1889 | ||
1890 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && | |
1891 | extent_type == BTRFS_FILE_EXTENT_REG) | |
1892 | goto out; | |
1893 | ||
1894 | /* | |
1895 | * If the extent was created before the generation where the last snapshot | |
1896 | * for its subvolume was created, then this implies the extent is shared, | |
1897 | * hence we must COW. | |
1898 | */ | |
a7bb6bd4 | 1899 | if (!args->strict && |
619104ba FM |
1900 | btrfs_file_extent_generation(leaf, fi) <= |
1901 | btrfs_root_last_snapshot(&root->root_item)) | |
1902 | goto out; | |
1903 | ||
1904 | /* An explicit hole, must COW. */ | |
1905 | if (args->disk_bytenr == 0) | |
1906 | goto out; | |
1907 | ||
1908 | /* Compressed/encrypted/encoded extents must be COWed. */ | |
1909 | if (btrfs_file_extent_compression(leaf, fi) || | |
1910 | btrfs_file_extent_encryption(leaf, fi) || | |
1911 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1912 | goto out; | |
1913 | ||
1914 | extent_end = btrfs_file_extent_end(path); | |
1915 | ||
1916 | /* | |
1917 | * The following checks can be expensive, as they need to take other | |
1918 | * locks and do btree or rbtree searches, so release the path to avoid | |
1919 | * blocking other tasks for too long. | |
1920 | */ | |
1921 | btrfs_release_path(path); | |
1922 | ||
1923 | ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), | |
1924 | key->offset - args->extent_offset, | |
deccae40 | 1925 | args->disk_bytenr, args->strict, path); |
619104ba FM |
1926 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); |
1927 | if (ret != 0) | |
1928 | goto out; | |
1929 | ||
1930 | if (args->free_path) { | |
1931 | /* | |
1932 | * We don't need the path anymore, plus through the | |
1933 | * csum_exist_in_range() call below we will end up allocating | |
1934 | * another path. So free the path to avoid unnecessary extra | |
1935 | * memory usage. | |
1936 | */ | |
1937 | btrfs_free_path(path); | |
1938 | path = NULL; | |
1939 | } | |
1940 | ||
1941 | /* If there are pending snapshots for this root, we must COW. */ | |
1942 | if (args->writeback_path && !is_freespace_inode && | |
1943 | atomic_read(&root->snapshot_force_cow)) | |
1944 | goto out; | |
1945 | ||
1946 | args->disk_bytenr += args->extent_offset; | |
1947 | args->disk_bytenr += args->start - key->offset; | |
1948 | args->num_bytes = min(args->end + 1, extent_end) - args->start; | |
1949 | ||
1950 | /* | |
1951 | * Force COW if csums exist in the range. This ensures that csums for a | |
1952 | * given extent are either valid or do not exist. | |
1953 | */ | |
26ce9114 JB |
1954 | ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes, |
1955 | nowait); | |
619104ba FM |
1956 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); |
1957 | if (ret != 0) | |
1958 | goto out; | |
1959 | ||
1960 | can_nocow = 1; | |
1961 | out: | |
1962 | if (args->free_path && path) | |
1963 | btrfs_free_path(path); | |
1964 | ||
1965 | return ret < 0 ? ret : can_nocow; | |
1966 | } | |
1967 | ||
d352ac68 CM |
1968 | /* |
1969 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1970 | * of the extents that exist in the file, and COWs the file as required. | |
1971 | * | |
1972 | * If no cow copies or snapshots exist, we write directly to the existing | |
1973 | * blocks on disk | |
1974 | */ | |
968322c8 | 1975 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1976 | struct page *locked_page, |
53ffb30a | 1977 | const u64 start, const u64 end) |
be20aa9d | 1978 | { |
968322c8 NB |
1979 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1980 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1981 | struct btrfs_path *path; |
3e024846 NB |
1982 | u64 cow_start = (u64)-1; |
1983 | u64 cur_offset = start; | |
8ecebf4d | 1984 | int ret; |
3e024846 | 1985 | bool check_prev = true; |
968322c8 | 1986 | u64 ino = btrfs_ino(inode); |
619104ba | 1987 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
be20aa9d | 1988 | |
76c5126e CH |
1989 | /* |
1990 | * Normally on a zoned device we're only doing COW writes, but in case | |
1991 | * of relocation on a zoned filesystem serializes I/O so that we're only | |
1992 | * writing sequentially and can end up here as well. | |
1993 | */ | |
1994 | ASSERT(!btrfs_is_zoned(fs_info) || btrfs_is_data_reloc_root(root)); | |
1995 | ||
be20aa9d | 1996 | path = btrfs_alloc_path(); |
17ca04af | 1997 | if (!path) { |
38dc8889 CH |
1998 | ret = -ENOMEM; |
1999 | goto error; | |
17ca04af | 2000 | } |
82d5902d | 2001 | |
619104ba FM |
2002 | nocow_args.end = end; |
2003 | nocow_args.writeback_path = true; | |
2004 | ||
80ff3856 | 2005 | while (1) { |
18f62b86 | 2006 | struct btrfs_block_group *nocow_bg = NULL; |
34bfaf15 | 2007 | struct btrfs_ordered_extent *ordered; |
3e024846 NB |
2008 | struct btrfs_key found_key; |
2009 | struct btrfs_file_extent_item *fi; | |
2010 | struct extent_buffer *leaf; | |
2011 | u64 extent_end; | |
3e024846 | 2012 | u64 ram_bytes; |
619104ba | 2013 | u64 nocow_end; |
3e024846 | 2014 | int extent_type; |
3daea5fd | 2015 | bool is_prealloc; |
762bf098 | 2016 | |
e4c3b2dc | 2017 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 2018 | cur_offset, 0); |
d788a349 | 2019 | if (ret < 0) |
79787eaa | 2020 | goto error; |
a6bd9cd1 NB |
2021 | |
2022 | /* | |
2023 | * If there is no extent for our range when doing the initial | |
2024 | * search, then go back to the previous slot as it will be the | |
2025 | * one containing the search offset | |
2026 | */ | |
80ff3856 YZ |
2027 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
2028 | leaf = path->nodes[0]; | |
2029 | btrfs_item_key_to_cpu(leaf, &found_key, | |
2030 | path->slots[0] - 1); | |
33345d01 | 2031 | if (found_key.objectid == ino && |
80ff3856 YZ |
2032 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
2033 | path->slots[0]--; | |
2034 | } | |
3e024846 | 2035 | check_prev = false; |
80ff3856 | 2036 | next_slot: |
a6bd9cd1 | 2037 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
2038 | leaf = path->nodes[0]; |
2039 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
2040 | ret = btrfs_next_leaf(root, path); | |
953fa5ce | 2041 | if (ret < 0) |
79787eaa | 2042 | goto error; |
80ff3856 YZ |
2043 | if (ret > 0) |
2044 | break; | |
2045 | leaf = path->nodes[0]; | |
2046 | } | |
be20aa9d | 2047 | |
80ff3856 YZ |
2048 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
2049 | ||
a6bd9cd1 | 2050 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
2051 | if (found_key.objectid > ino) |
2052 | break; | |
a6bd9cd1 NB |
2053 | /* |
2054 | * Keep searching until we find an EXTENT_ITEM or there are no | |
2055 | * more extents for this inode | |
2056 | */ | |
1d512cb7 FM |
2057 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
2058 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
2059 | path->slots[0]++; | |
2060 | goto next_slot; | |
2061 | } | |
a6bd9cd1 NB |
2062 | |
2063 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 2064 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
2065 | found_key.offset > end) |
2066 | break; | |
2067 | ||
a6bd9cd1 NB |
2068 | /* |
2069 | * If the found extent starts after requested offset, then | |
2070 | * adjust extent_end to be right before this extent begins | |
2071 | */ | |
80ff3856 YZ |
2072 | if (found_key.offset > cur_offset) { |
2073 | extent_end = found_key.offset; | |
e9061e21 | 2074 | extent_type = 0; |
18f62b86 | 2075 | goto must_cow; |
80ff3856 YZ |
2076 | } |
2077 | ||
a6bd9cd1 NB |
2078 | /* |
2079 | * Found extent which begins before our range and potentially | |
2080 | * intersect it | |
2081 | */ | |
80ff3856 YZ |
2082 | fi = btrfs_item_ptr(leaf, path->slots[0], |
2083 | struct btrfs_file_extent_item); | |
2084 | extent_type = btrfs_file_extent_type(leaf, fi); | |
619104ba FM |
2085 | /* If this is triggered then we have a memory corruption. */ |
2086 | ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES); | |
2087 | if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) { | |
2088 | ret = -EUCLEAN; | |
2089 | goto error; | |
2090 | } | |
cc95bef6 | 2091 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
619104ba | 2092 | extent_end = btrfs_file_extent_end(path); |
c65ca98f | 2093 | |
619104ba FM |
2094 | /* |
2095 | * If the extent we got ends before our current offset, skip to | |
2096 | * the next extent. | |
2097 | */ | |
2098 | if (extent_end <= cur_offset) { | |
2099 | path->slots[0]++; | |
2100 | goto next_slot; | |
2101 | } | |
c65ca98f | 2102 | |
619104ba FM |
2103 | nocow_args.start = cur_offset; |
2104 | ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args); | |
953fa5ce | 2105 | if (ret < 0) |
619104ba | 2106 | goto error; |
953fa5ce | 2107 | if (ret == 0) |
18f62b86 | 2108 | goto must_cow; |
58113753 | 2109 | |
619104ba | 2110 | ret = 0; |
18f62b86 CH |
2111 | nocow_bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); |
2112 | if (!nocow_bg) { | |
2113 | must_cow: | |
2114 | /* | |
2115 | * If we can't perform NOCOW writeback for the range, | |
2116 | * then record the beginning of the range that needs to | |
2117 | * be COWed. It will be written out before the next | |
2118 | * NOCOW range if we find one, or when exiting this | |
2119 | * loop. | |
2120 | */ | |
80ff3856 YZ |
2121 | if (cow_start == (u64)-1) |
2122 | cow_start = cur_offset; | |
2123 | cur_offset = extent_end; | |
2124 | if (cur_offset > end) | |
2125 | break; | |
c65ca98f FM |
2126 | if (!path->nodes[0]) |
2127 | continue; | |
80ff3856 YZ |
2128 | path->slots[0]++; |
2129 | goto next_slot; | |
7ea394f1 YZ |
2130 | } |
2131 | ||
a6bd9cd1 NB |
2132 | /* |
2133 | * COW range from cow_start to found_key.offset - 1. As the key | |
2134 | * will contain the beginning of the first extent that can be | |
2135 | * NOCOW, following one which needs to be COW'ed | |
2136 | */ | |
80ff3856 | 2137 | if (cow_start != (u64)-1) { |
968322c8 | 2138 | ret = fallback_to_cow(inode, locked_page, |
53ffb30a | 2139 | cow_start, found_key.offset - 1); |
80ff3856 | 2140 | cow_start = (u64)-1; |
18f62b86 CH |
2141 | if (ret) { |
2142 | btrfs_dec_nocow_writers(nocow_bg); | |
79787eaa | 2143 | goto error; |
18f62b86 | 2144 | } |
7ea394f1 | 2145 | } |
80ff3856 | 2146 | |
619104ba | 2147 | nocow_end = cur_offset + nocow_args.num_bytes - 1; |
3daea5fd CH |
2148 | is_prealloc = extent_type == BTRFS_FILE_EXTENT_PREALLOC; |
2149 | if (is_prealloc) { | |
619104ba | 2150 | u64 orig_start = found_key.offset - nocow_args.extent_offset; |
3e024846 | 2151 | struct extent_map *em; |
6f9994db | 2152 | |
619104ba | 2153 | em = create_io_em(inode, cur_offset, nocow_args.num_bytes, |
6f9994db | 2154 | orig_start, |
619104ba FM |
2155 | nocow_args.disk_bytenr, /* block_start */ |
2156 | nocow_args.num_bytes, /* block_len */ | |
2157 | nocow_args.disk_num_bytes, /* orig_block_len */ | |
6f9994db LB |
2158 | ram_bytes, BTRFS_COMPRESS_NONE, |
2159 | BTRFS_ORDERED_PREALLOC); | |
2160 | if (IS_ERR(em)) { | |
18f62b86 | 2161 | btrfs_dec_nocow_writers(nocow_bg); |
6f9994db LB |
2162 | ret = PTR_ERR(em); |
2163 | goto error; | |
d899e052 | 2164 | } |
6f9994db | 2165 | free_extent_map(em); |
3daea5fd CH |
2166 | } |
2167 | ||
34bfaf15 | 2168 | ordered = btrfs_alloc_ordered_extent(inode, cur_offset, |
3daea5fd CH |
2169 | nocow_args.num_bytes, nocow_args.num_bytes, |
2170 | nocow_args.disk_bytenr, nocow_args.num_bytes, 0, | |
2171 | is_prealloc | |
2172 | ? (1 << BTRFS_ORDERED_PREALLOC) | |
2173 | : (1 << BTRFS_ORDERED_NOCOW), | |
2174 | BTRFS_COMPRESS_NONE); | |
18f62b86 | 2175 | btrfs_dec_nocow_writers(nocow_bg); |
34bfaf15 | 2176 | if (IS_ERR(ordered)) { |
3daea5fd | 2177 | if (is_prealloc) { |
4c0c8cfc FM |
2178 | btrfs_drop_extent_map_range(inode, cur_offset, |
2179 | nocow_end, false); | |
762bf098 | 2180 | } |
34bfaf15 | 2181 | ret = PTR_ERR(ordered); |
3daea5fd | 2182 | goto error; |
d899e052 | 2183 | } |
80ff3856 | 2184 | |
37f00a6d | 2185 | if (btrfs_is_data_reloc_root(root)) |
4dbd80fb QW |
2186 | /* |
2187 | * Error handled later, as we must prevent | |
2188 | * extent_clear_unlock_delalloc() in error handler | |
2189 | * from freeing metadata of created ordered extent. | |
2190 | */ | |
34bfaf15 CH |
2191 | ret = btrfs_reloc_clone_csums(ordered); |
2192 | btrfs_put_ordered_extent(ordered); | |
efa56464 | 2193 | |
619104ba | 2194 | extent_clear_unlock_delalloc(inode, cur_offset, nocow_end, |
c2790a2e | 2195 | locked_page, EXTENT_LOCKED | |
18513091 WX |
2196 | EXTENT_DELALLOC | |
2197 | EXTENT_CLEAR_DATA_RESV, | |
f57ad937 | 2198 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
18513091 | 2199 | |
80ff3856 | 2200 | cur_offset = extent_end; |
4dbd80fb QW |
2201 | |
2202 | /* | |
2203 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
2204 | * handler, as metadata for created ordered extent will only | |
2205 | * be freed by btrfs_finish_ordered_io(). | |
2206 | */ | |
2207 | if (ret) | |
2208 | goto error; | |
80ff3856 YZ |
2209 | if (cur_offset > end) |
2210 | break; | |
be20aa9d | 2211 | } |
b3b4aa74 | 2212 | btrfs_release_path(path); |
80ff3856 | 2213 | |
506481b2 | 2214 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 2215 | cow_start = cur_offset; |
17ca04af | 2216 | |
80ff3856 | 2217 | if (cow_start != (u64)-1) { |
506481b2 | 2218 | cur_offset = end; |
53ffb30a | 2219 | ret = fallback_to_cow(inode, locked_page, cow_start, end); |
953fa5ce | 2220 | cow_start = (u64)-1; |
d788a349 | 2221 | if (ret) |
79787eaa | 2222 | goto error; |
80ff3856 YZ |
2223 | } |
2224 | ||
18f62b86 CH |
2225 | btrfs_free_path(path); |
2226 | return 0; | |
762bf098 | 2227 | |
18f62b86 | 2228 | error: |
953fa5ce CH |
2229 | /* |
2230 | * If an error happened while a COW region is outstanding, cur_offset | |
2231 | * needs to be reset to cow_start to ensure the COW region is unlocked | |
2232 | * as well. | |
2233 | */ | |
2234 | if (cow_start != (u64)-1) | |
2235 | cur_offset = cow_start; | |
18f62b86 | 2236 | if (cur_offset < end) |
968322c8 | 2237 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 2238 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
2239 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
2240 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 2241 | PAGE_START_WRITEBACK | |
c2790a2e | 2242 | PAGE_END_WRITEBACK); |
7ea394f1 | 2243 | btrfs_free_path(path); |
79787eaa | 2244 | return ret; |
be20aa9d CM |
2245 | } |
2246 | ||
6e65ae76 | 2247 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 | 2248 | { |
6e65ae76 GR |
2249 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
2250 | if (inode->defrag_bytes && | |
99be1a66 | 2251 | test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG)) |
6e65ae76 GR |
2252 | return false; |
2253 | return true; | |
2254 | } | |
2255 | return false; | |
47059d93 WS |
2256 | } |
2257 | ||
d352ac68 | 2258 | /* |
5eaad97a NB |
2259 | * Function to process delayed allocation (create CoW) for ranges which are |
2260 | * being touched for the first time. | |
d352ac68 | 2261 | */ |
98456b9c | 2262 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
c56cbe90 | 2263 | u64 start, u64 end, struct writeback_control *wbc) |
be20aa9d | 2264 | { |
42c01100 | 2265 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
c56cbe90 | 2266 | int ret; |
a2135011 | 2267 | |
2749f7ef | 2268 | /* |
c56cbe90 CH |
2269 | * The range must cover part of the @locked_page, or a return of 1 |
2270 | * can confuse the caller. | |
2749f7ef QW |
2271 | */ |
2272 | ASSERT(!(end <= page_offset(locked_page) || | |
2273 | start >= page_offset(locked_page) + PAGE_SIZE)); | |
2274 | ||
6e65ae76 | 2275 | if (should_nocow(inode, start, end)) { |
53ffb30a | 2276 | ret = run_delalloc_nocow(inode, locked_page, start, end); |
973fb26e | 2277 | goto out; |
7ddf5a42 | 2278 | } |
973fb26e CH |
2279 | |
2280 | if (btrfs_inode_can_compress(inode) && | |
2281 | inode_need_compress(inode, start, end) && | |
c56cbe90 CH |
2282 | run_delalloc_compressed(inode, locked_page, start, end, wbc)) |
2283 | return 1; | |
973fb26e CH |
2284 | |
2285 | if (zoned) | |
256b0cf9 CH |
2286 | ret = run_delalloc_cow(inode, locked_page, start, end, wbc, |
2287 | true); | |
973fb26e | 2288 | else |
c56cbe90 CH |
2289 | ret = cow_file_range(inode, locked_page, start, end, NULL, |
2290 | false, false); | |
973fb26e CH |
2291 | |
2292 | out: | |
c56cbe90 | 2293 | if (ret < 0) |
98456b9c | 2294 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 2295 | end - start + 1); |
b888db2b CM |
2296 | return ret; |
2297 | } | |
2298 | ||
62798a49 | 2299 | void btrfs_split_delalloc_extent(struct btrfs_inode *inode, |
abbb55f4 | 2300 | struct extent_state *orig, u64 split) |
9ed74f2d | 2301 | { |
62798a49 | 2302 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
dcab6a3b JB |
2303 | u64 size; |
2304 | ||
0ca1f7ce | 2305 | /* not delalloc, ignore it */ |
9ed74f2d | 2306 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 2307 | return; |
9ed74f2d | 2308 | |
dcab6a3b | 2309 | size = orig->end - orig->start + 1; |
f7b12a62 | 2310 | if (size > fs_info->max_extent_size) { |
823bb20a | 2311 | u32 num_extents; |
dcab6a3b JB |
2312 | u64 new_size; |
2313 | ||
2314 | /* | |
5c848198 | 2315 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 2316 | * applies here, just in reverse. |
dcab6a3b JB |
2317 | */ |
2318 | new_size = orig->end - split + 1; | |
7d7672bc | 2319 | num_extents = count_max_extents(fs_info, new_size); |
ba117213 | 2320 | new_size = split - orig->start; |
7d7672bc NA |
2321 | num_extents += count_max_extents(fs_info, new_size); |
2322 | if (count_max_extents(fs_info, size) >= num_extents) | |
dcab6a3b JB |
2323 | return; |
2324 | } | |
2325 | ||
62798a49 DS |
2326 | spin_lock(&inode->lock); |
2327 | btrfs_mod_outstanding_extents(inode, 1); | |
2328 | spin_unlock(&inode->lock); | |
9ed74f2d JB |
2329 | } |
2330 | ||
2331 | /* | |
5c848198 NB |
2332 | * Handle merged delayed allocation extents so we can keep track of new extents |
2333 | * that are just merged onto old extents, such as when we are doing sequential | |
2334 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 2335 | */ |
2454151c | 2336 | void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new, |
5c848198 | 2337 | struct extent_state *other) |
9ed74f2d | 2338 | { |
2454151c | 2339 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
dcab6a3b | 2340 | u64 new_size, old_size; |
823bb20a | 2341 | u32 num_extents; |
dcab6a3b | 2342 | |
9ed74f2d JB |
2343 | /* not delalloc, ignore it */ |
2344 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 2345 | return; |
9ed74f2d | 2346 | |
8461a3de JB |
2347 | if (new->start > other->start) |
2348 | new_size = new->end - other->start + 1; | |
2349 | else | |
2350 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
2351 | |
2352 | /* we're not bigger than the max, unreserve the space and go */ | |
f7b12a62 | 2353 | if (new_size <= fs_info->max_extent_size) { |
2454151c DS |
2354 | spin_lock(&inode->lock); |
2355 | btrfs_mod_outstanding_extents(inode, -1); | |
2356 | spin_unlock(&inode->lock); | |
dcab6a3b JB |
2357 | return; |
2358 | } | |
2359 | ||
2360 | /* | |
ba117213 JB |
2361 | * We have to add up either side to figure out how many extents were |
2362 | * accounted for before we merged into one big extent. If the number of | |
2363 | * extents we accounted for is <= the amount we need for the new range | |
2364 | * then we can return, otherwise drop. Think of it like this | |
2365 | * | |
2366 | * [ 4k][MAX_SIZE] | |
2367 | * | |
2368 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
2369 | * need 2 outstanding extents, on one side we have 1 and the other side | |
2370 | * we have 1 so they are == and we can return. But in this case | |
2371 | * | |
2372 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
2373 | * | |
2374 | * Each range on their own accounts for 2 extents, but merged together | |
2375 | * they are only 3 extents worth of accounting, so we need to drop in | |
2376 | * this case. | |
dcab6a3b | 2377 | */ |
ba117213 | 2378 | old_size = other->end - other->start + 1; |
7d7672bc | 2379 | num_extents = count_max_extents(fs_info, old_size); |
ba117213 | 2380 | old_size = new->end - new->start + 1; |
7d7672bc NA |
2381 | num_extents += count_max_extents(fs_info, old_size); |
2382 | if (count_max_extents(fs_info, new_size) >= num_extents) | |
dcab6a3b JB |
2383 | return; |
2384 | ||
2454151c DS |
2385 | spin_lock(&inode->lock); |
2386 | btrfs_mod_outstanding_extents(inode, -1); | |
2387 | spin_unlock(&inode->lock); | |
9ed74f2d JB |
2388 | } |
2389 | ||
eb73c1b7 | 2390 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
82ca5a04 | 2391 | struct btrfs_inode *inode) |
eb73c1b7 | 2392 | { |
82ca5a04 | 2393 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
0b246afa | 2394 | |
eb73c1b7 | 2395 | spin_lock(&root->delalloc_lock); |
82ca5a04 DS |
2396 | if (list_empty(&inode->delalloc_inodes)) { |
2397 | list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes); | |
2398 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, &inode->runtime_flags); | |
eb73c1b7 MX |
2399 | root->nr_delalloc_inodes++; |
2400 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 2401 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2402 | BUG_ON(!list_empty(&root->delalloc_root)); |
2403 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
2404 | &fs_info->delalloc_roots); |
2405 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
2406 | } |
2407 | } | |
2408 | spin_unlock(&root->delalloc_lock); | |
2409 | } | |
2410 | ||
2b877331 NB |
2411 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
2412 | struct btrfs_inode *inode) | |
eb73c1b7 | 2413 | { |
3ffbd68c | 2414 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 2415 | |
9e3e97f4 NB |
2416 | if (!list_empty(&inode->delalloc_inodes)) { |
2417 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 2418 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2419 | &inode->runtime_flags); |
eb73c1b7 MX |
2420 | root->nr_delalloc_inodes--; |
2421 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 2422 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 2423 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2424 | BUG_ON(list_empty(&root->delalloc_root)); |
2425 | list_del_init(&root->delalloc_root); | |
0b246afa | 2426 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2427 | } |
2428 | } | |
2b877331 NB |
2429 | } |
2430 | ||
2431 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2432 | struct btrfs_inode *inode) | |
2433 | { | |
2434 | spin_lock(&root->delalloc_lock); | |
2435 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
2436 | spin_unlock(&root->delalloc_lock); |
2437 | } | |
2438 | ||
d352ac68 | 2439 | /* |
e06a1fc9 NB |
2440 | * Properly track delayed allocation bytes in the inode and to maintain the |
2441 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 2442 | */ |
4c5d166f | 2443 | void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state, |
6d92b304 | 2444 | u32 bits) |
291d673e | 2445 | { |
4c5d166f | 2446 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
0b246afa | 2447 | |
6d92b304 | 2448 | if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC)) |
47059d93 | 2449 | WARN_ON(1); |
75eff68e CM |
2450 | /* |
2451 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2452 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2453 | * bit, which is only set or cleared with irqs on |
2454 | */ | |
6d92b304 | 2455 | if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
4c5d166f | 2456 | struct btrfs_root *root = inode->root; |
0ca1f7ce | 2457 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2458 | u32 num_extents = count_max_extents(fs_info, len); |
4c5d166f | 2459 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 2460 | |
4c5d166f DS |
2461 | spin_lock(&inode->lock); |
2462 | btrfs_mod_outstanding_extents(inode, num_extents); | |
2463 | spin_unlock(&inode->lock); | |
287a0ab9 | 2464 | |
6a3891c5 | 2465 | /* For sanity tests */ |
0b246afa | 2466 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2467 | return; |
2468 | ||
104b4e51 NB |
2469 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2470 | fs_info->delalloc_batch); | |
4c5d166f DS |
2471 | spin_lock(&inode->lock); |
2472 | inode->delalloc_bytes += len; | |
6d92b304 | 2473 | if (bits & EXTENT_DEFRAG) |
4c5d166f | 2474 | inode->defrag_bytes += len; |
df0af1a5 | 2475 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
4c5d166f | 2476 | &inode->runtime_flags)) |
eb73c1b7 | 2477 | btrfs_add_delalloc_inodes(root, inode); |
4c5d166f | 2478 | spin_unlock(&inode->lock); |
291d673e | 2479 | } |
a7e3b975 FM |
2480 | |
2481 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2482 | (bits & EXTENT_DELALLOC_NEW)) { |
4c5d166f DS |
2483 | spin_lock(&inode->lock); |
2484 | inode->new_delalloc_bytes += state->end + 1 - state->start; | |
2485 | spin_unlock(&inode->lock); | |
a7e3b975 | 2486 | } |
291d673e CM |
2487 | } |
2488 | ||
d352ac68 | 2489 | /* |
a36bb5f9 NB |
2490 | * Once a range is no longer delalloc this function ensures that proper |
2491 | * accounting happens. | |
d352ac68 | 2492 | */ |
bd54766e | 2493 | void btrfs_clear_delalloc_extent(struct btrfs_inode *inode, |
6d92b304 | 2494 | struct extent_state *state, u32 bits) |
291d673e | 2495 | { |
bd54766e | 2496 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
47059d93 | 2497 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2498 | u32 num_extents = count_max_extents(fs_info, len); |
47059d93 | 2499 | |
6d92b304 | 2500 | if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) { |
4a4b964f | 2501 | spin_lock(&inode->lock); |
6fc0ef68 | 2502 | inode->defrag_bytes -= len; |
4a4b964f FM |
2503 | spin_unlock(&inode->lock); |
2504 | } | |
47059d93 | 2505 | |
75eff68e CM |
2506 | /* |
2507 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2508 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2509 | * bit, which is only set or cleared with irqs on |
2510 | */ | |
6d92b304 | 2511 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2512 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2513 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2514 | |
8b62f87b JB |
2515 | spin_lock(&inode->lock); |
2516 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2517 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2518 | |
b6d08f06 JB |
2519 | /* |
2520 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2521 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2522 | * error. |
2523 | */ | |
6d92b304 | 2524 | if (bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2525 | root != fs_info->tree_root) |
43b18595 | 2526 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2527 | |
6a3891c5 | 2528 | /* For sanity tests. */ |
0b246afa | 2529 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2530 | return; |
2531 | ||
37f00a6d | 2532 | if (!btrfs_is_data_reloc_root(root) && |
a315e68f | 2533 | do_list && !(state->state & EXTENT_NORESERVE) && |
6d92b304 | 2534 | (bits & EXTENT_CLEAR_DATA_RESV)) |
9db5d510 | 2535 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2536 | |
104b4e51 NB |
2537 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2538 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2539 | spin_lock(&inode->lock); |
2540 | inode->delalloc_bytes -= len; | |
2541 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2542 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2543 | &inode->runtime_flags)) |
eb73c1b7 | 2544 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2545 | spin_unlock(&inode->lock); |
291d673e | 2546 | } |
a7e3b975 FM |
2547 | |
2548 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2549 | (bits & EXTENT_DELALLOC_NEW)) { |
a7e3b975 FM |
2550 | spin_lock(&inode->lock); |
2551 | ASSERT(inode->new_delalloc_bytes >= len); | |
2552 | inode->new_delalloc_bytes -= len; | |
6d92b304 | 2553 | if (bits & EXTENT_ADD_INODE_BYTES) |
2766ff61 | 2554 | inode_add_bytes(&inode->vfs_inode, len); |
a7e3b975 FM |
2555 | spin_unlock(&inode->lock); |
2556 | } | |
291d673e CM |
2557 | } |
2558 | ||
71df088c CH |
2559 | static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio, |
2560 | struct btrfs_ordered_extent *ordered) | |
d22002fd | 2561 | { |
69ccf3f4 CH |
2562 | u64 start = (u64)bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT; |
2563 | u64 len = bbio->bio.bi_iter.bi_size; | |
b0307e28 | 2564 | struct btrfs_ordered_extent *new; |
ebdb44a0 | 2565 | int ret; |
d22002fd | 2566 | |
11d33ab6 | 2567 | /* Must always be called for the beginning of an ordered extent. */ |
7edd339c CH |
2568 | if (WARN_ON_ONCE(start != ordered->disk_bytenr)) |
2569 | return -EINVAL; | |
d22002fd | 2570 | |
11d33ab6 | 2571 | /* No need to split if the ordered extent covers the entire bio. */ |
ec63b84d CH |
2572 | if (ordered->disk_num_bytes == len) { |
2573 | refcount_inc(&ordered->refs); | |
2574 | bbio->ordered = ordered; | |
7edd339c | 2575 | return 0; |
ec63b84d | 2576 | } |
d22002fd | 2577 | |
f0f5329a BB |
2578 | /* |
2579 | * Don't split the extent_map for NOCOW extents, as we're writing into | |
2580 | * a pre-existing one. | |
2581 | */ | |
ebdb44a0 CH |
2582 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
2583 | ret = split_extent_map(bbio->inode, bbio->file_offset, | |
f000bc6f CH |
2584 | ordered->num_bytes, len, |
2585 | ordered->disk_bytenr); | |
ebdb44a0 CH |
2586 | if (ret) |
2587 | return ret; | |
2588 | } | |
f0f5329a | 2589 | |
b0307e28 CH |
2590 | new = btrfs_split_ordered_extent(ordered, len); |
2591 | if (IS_ERR(new)) | |
2592 | return PTR_ERR(new); | |
ec63b84d | 2593 | bbio->ordered = new; |
b0307e28 | 2594 | return 0; |
d22002fd NA |
2595 | } |
2596 | ||
d352ac68 CM |
2597 | /* |
2598 | * given a list of ordered sums record them in the inode. This happens | |
2599 | * at IO completion time based on sums calculated at bio submission time. | |
2600 | */ | |
510f85ed NB |
2601 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
2602 | struct list_head *list) | |
e6dcd2dc | 2603 | { |
e6dcd2dc | 2604 | struct btrfs_ordered_sum *sum; |
fc28b25e | 2605 | struct btrfs_root *csum_root = NULL; |
ac01f26a | 2606 | int ret; |
e6dcd2dc | 2607 | |
c6e30871 | 2608 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2609 | trans->adding_csums = true; |
fc28b25e JB |
2610 | if (!csum_root) |
2611 | csum_root = btrfs_csum_root(trans->fs_info, | |
5cfe76f8 | 2612 | sum->logical); |
fc28b25e | 2613 | ret = btrfs_csum_file_blocks(trans, csum_root, sum); |
7c2871a2 | 2614 | trans->adding_csums = false; |
ac01f26a NB |
2615 | if (ret) |
2616 | return ret; | |
e6dcd2dc CM |
2617 | } |
2618 | return 0; | |
2619 | } | |
2620 | ||
c3347309 FM |
2621 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
2622 | const u64 start, | |
2623 | const u64 len, | |
2624 | struct extent_state **cached_state) | |
2625 | { | |
2626 | u64 search_start = start; | |
2627 | const u64 end = start + len - 1; | |
2628 | ||
2629 | while (search_start < end) { | |
2630 | const u64 search_len = end - search_start + 1; | |
2631 | struct extent_map *em; | |
2632 | u64 em_len; | |
2633 | int ret = 0; | |
2634 | ||
2635 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); | |
2636 | if (IS_ERR(em)) | |
2637 | return PTR_ERR(em); | |
2638 | ||
2639 | if (em->block_start != EXTENT_MAP_HOLE) | |
2640 | goto next; | |
2641 | ||
2642 | em_len = em->len; | |
2643 | if (em->start < search_start) | |
2644 | em_len -= search_start - em->start; | |
2645 | if (em_len > search_len) | |
2646 | em_len = search_len; | |
2647 | ||
2648 | ret = set_extent_bit(&inode->io_tree, search_start, | |
2649 | search_start + em_len - 1, | |
1d126800 | 2650 | EXTENT_DELALLOC_NEW, cached_state); |
c3347309 FM |
2651 | next: |
2652 | search_start = extent_map_end(em); | |
2653 | free_extent_map(em); | |
2654 | if (ret) | |
2655 | return ret; | |
2656 | } | |
2657 | return 0; | |
2658 | } | |
2659 | ||
c2566f22 | 2660 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2661 | unsigned int extra_bits, |
330a5827 | 2662 | struct extent_state **cached_state) |
ea8c2819 | 2663 | { |
fdb1e121 | 2664 | WARN_ON(PAGE_ALIGNED(end)); |
c3347309 FM |
2665 | |
2666 | if (start >= i_size_read(&inode->vfs_inode) && | |
2667 | !(inode->flags & BTRFS_INODE_PREALLOC)) { | |
2668 | /* | |
2669 | * There can't be any extents following eof in this case so just | |
2670 | * set the delalloc new bit for the range directly. | |
2671 | */ | |
2672 | extra_bits |= EXTENT_DELALLOC_NEW; | |
2673 | } else { | |
2674 | int ret; | |
2675 | ||
2676 | ret = btrfs_find_new_delalloc_bytes(inode, start, | |
2677 | end + 1 - start, | |
2678 | cached_state); | |
2679 | if (ret) | |
2680 | return ret; | |
2681 | } | |
2682 | ||
66240ab1 | 2683 | return set_extent_bit(&inode->io_tree, start, end, |
1d126800 | 2684 | EXTENT_DELALLOC | extra_bits, cached_state); |
ea8c2819 CM |
2685 | } |
2686 | ||
d352ac68 | 2687 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2688 | struct btrfs_writepage_fixup { |
2689 | struct page *page; | |
36eeaef5 | 2690 | struct btrfs_inode *inode; |
247e743c CM |
2691 | struct btrfs_work work; |
2692 | }; | |
2693 | ||
b2950863 | 2694 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c | 2695 | { |
9783e4de CH |
2696 | struct btrfs_writepage_fixup *fixup = |
2697 | container_of(work, struct btrfs_writepage_fixup, work); | |
247e743c | 2698 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 2699 | struct extent_state *cached_state = NULL; |
364ecf36 | 2700 | struct extent_changeset *data_reserved = NULL; |
9783e4de CH |
2701 | struct page *page = fixup->page; |
2702 | struct btrfs_inode *inode = fixup->inode; | |
2703 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
2704 | u64 page_start = page_offset(page); | |
2705 | u64 page_end = page_offset(page) + PAGE_SIZE - 1; | |
25f3c502 | 2706 | int ret = 0; |
f4b1363c | 2707 | bool free_delalloc_space = true; |
247e743c | 2708 | |
f4b1363c JB |
2709 | /* |
2710 | * This is similar to page_mkwrite, we need to reserve the space before | |
2711 | * we take the page lock. | |
2712 | */ | |
65d87f79 NB |
2713 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2714 | PAGE_SIZE); | |
4a096752 | 2715 | again: |
247e743c | 2716 | lock_page(page); |
25f3c502 CM |
2717 | |
2718 | /* | |
2719 | * Before we queued this fixup, we took a reference on the page. | |
2720 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2721 | * address space. | |
2722 | */ | |
f4b1363c JB |
2723 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2724 | /* | |
2725 | * Unfortunately this is a little tricky, either | |
2726 | * | |
2727 | * 1) We got here and our page had already been dealt with and | |
2728 | * we reserved our space, thus ret == 0, so we need to just | |
2729 | * drop our space reservation and bail. This can happen the | |
2730 | * first time we come into the fixup worker, or could happen | |
2731 | * while waiting for the ordered extent. | |
2732 | * 2) Our page was already dealt with, but we happened to get an | |
2733 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2734 | * this case we obviously don't have anything to release, but | |
2735 | * because the page was already dealt with we don't want to | |
2736 | * mark the page with an error, so make sure we're resetting | |
2737 | * ret to 0. This is why we have this check _before_ the ret | |
2738 | * check, because we do not want to have a surprise ENOSPC | |
2739 | * when the page was already properly dealt with. | |
2740 | */ | |
2741 | if (!ret) { | |
65d87f79 NB |
2742 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2743 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2744 | page_start, PAGE_SIZE, |
2745 | true); | |
2746 | } | |
2747 | ret = 0; | |
247e743c | 2748 | goto out_page; |
f4b1363c | 2749 | } |
247e743c | 2750 | |
25f3c502 | 2751 | /* |
f4b1363c JB |
2752 | * We can't mess with the page state unless it is locked, so now that |
2753 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2754 | */ |
f4b1363c JB |
2755 | if (ret) |
2756 | goto out_page; | |
247e743c | 2757 | |
570eb97b | 2758 | lock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2759 | |
2760 | /* already ordered? We're done */ | |
f57ad937 | 2761 | if (PageOrdered(page)) |
f4b1363c | 2762 | goto out_reserved; |
4a096752 | 2763 | |
65d87f79 | 2764 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2765 | if (ordered) { |
570eb97b JB |
2766 | unlock_extent(&inode->io_tree, page_start, page_end, |
2767 | &cached_state); | |
4a096752 | 2768 | unlock_page(page); |
36d45567 | 2769 | btrfs_start_ordered_extent(ordered); |
87826df0 | 2770 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2771 | goto again; |
2772 | } | |
247e743c | 2773 | |
65d87f79 | 2774 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2775 | &cached_state); |
25f3c502 | 2776 | if (ret) |
53687007 | 2777 | goto out_reserved; |
f3038ee3 | 2778 | |
25f3c502 CM |
2779 | /* |
2780 | * Everything went as planned, we're now the owner of a dirty page with | |
2781 | * delayed allocation bits set and space reserved for our COW | |
2782 | * destination. | |
2783 | * | |
2784 | * The page was dirty when we started, nothing should have cleaned it. | |
2785 | */ | |
2786 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2787 | free_delalloc_space = false; |
53687007 | 2788 | out_reserved: |
65d87f79 | 2789 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2790 | if (free_delalloc_space) |
65d87f79 NB |
2791 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2792 | PAGE_SIZE, true); | |
570eb97b | 2793 | unlock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
247e743c | 2794 | out_page: |
25f3c502 CM |
2795 | if (ret) { |
2796 | /* | |
2797 | * We hit ENOSPC or other errors. Update the mapping and page | |
2798 | * to reflect the errors and clean the page. | |
2799 | */ | |
2800 | mapping_set_error(page->mapping, ret); | |
9783e4de CH |
2801 | btrfs_mark_ordered_io_finished(inode, page, page_start, |
2802 | PAGE_SIZE, !ret); | |
25f3c502 | 2803 | clear_page_dirty_for_io(page); |
25f3c502 | 2804 | } |
9783e4de | 2805 | btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE); |
247e743c | 2806 | unlock_page(page); |
09cbfeaf | 2807 | put_page(page); |
b897abec | 2808 | kfree(fixup); |
364ecf36 | 2809 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2810 | /* |
2811 | * As a precaution, do a delayed iput in case it would be the last iput | |
2812 | * that could need flushing space. Recursing back to fixup worker would | |
2813 | * deadlock. | |
2814 | */ | |
e55cf7ca | 2815 | btrfs_add_delayed_iput(inode); |
247e743c CM |
2816 | } |
2817 | ||
2818 | /* | |
2819 | * There are a few paths in the higher layers of the kernel that directly | |
2820 | * set the page dirty bit without asking the filesystem if it is a | |
2821 | * good idea. This causes problems because we want to make sure COW | |
2822 | * properly happens and the data=ordered rules are followed. | |
2823 | * | |
c8b97818 | 2824 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2825 | * hasn't been properly setup for IO. We kick off an async process |
2826 | * to fix it up. The async helper will wait for ordered extents, set | |
2827 | * the delalloc bit and make it safe to write the page. | |
2828 | */ | |
a129ffb8 | 2829 | int btrfs_writepage_cow_fixup(struct page *page) |
247e743c CM |
2830 | { |
2831 | struct inode *inode = page->mapping->host; | |
0b246afa | 2832 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2833 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2834 | |
f57ad937 QW |
2835 | /* This page has ordered extent covering it already */ |
2836 | if (PageOrdered(page)) | |
247e743c CM |
2837 | return 0; |
2838 | ||
25f3c502 CM |
2839 | /* |
2840 | * PageChecked is set below when we create a fixup worker for this page, | |
2841 | * don't try to create another one if we're already PageChecked() | |
2842 | * | |
2843 | * The extent_io writepage code will redirty the page if we send back | |
2844 | * EAGAIN. | |
2845 | */ | |
247e743c CM |
2846 | if (PageChecked(page)) |
2847 | return -EAGAIN; | |
2848 | ||
2849 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2850 | if (!fixup) | |
2851 | return -EAGAIN; | |
f421950f | 2852 | |
f4b1363c JB |
2853 | /* |
2854 | * We are already holding a reference to this inode from | |
2855 | * write_cache_pages. We need to hold it because the space reservation | |
2856 | * takes place outside of the page lock, and we can't trust | |
2857 | * page->mapping outside of the page lock. | |
2858 | */ | |
2859 | ihold(inode); | |
e4f94347 | 2860 | btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
09cbfeaf | 2861 | get_page(page); |
078b8b90 | 2862 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL); |
247e743c | 2863 | fixup->page = page; |
36eeaef5 | 2864 | fixup->inode = BTRFS_I(inode); |
0b246afa | 2865 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2866 | |
2867 | return -EAGAIN; | |
247e743c CM |
2868 | } |
2869 | ||
d899e052 | 2870 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 2871 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a | 2872 | struct btrfs_file_extent_item *stack_fi, |
2766ff61 | 2873 | const bool update_inode_bytes, |
9729f10a | 2874 | u64 qgroup_reserved) |
d899e052 | 2875 | { |
c553f94d | 2876 | struct btrfs_root *root = inode->root; |
2766ff61 | 2877 | const u64 sectorsize = root->fs_info->sectorsize; |
d899e052 YZ |
2878 | struct btrfs_path *path; |
2879 | struct extent_buffer *leaf; | |
2880 | struct btrfs_key ins; | |
203f44c5 QW |
2881 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2882 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
cb36a9bb | 2883 | u64 offset = btrfs_stack_file_extent_offset(stack_fi); |
203f44c5 QW |
2884 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
2885 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
5893dfb9 | 2886 | struct btrfs_drop_extents_args drop_args = { 0 }; |
d899e052 YZ |
2887 | int ret; |
2888 | ||
2889 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2890 | if (!path) |
2891 | return -ENOMEM; | |
d899e052 | 2892 | |
a1ed835e CM |
2893 | /* |
2894 | * we may be replacing one extent in the tree with another. | |
2895 | * The new extent is pinned in the extent map, and we don't want | |
2896 | * to drop it from the cache until it is completely in the btree. | |
2897 | * | |
2898 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2899 | * the caller is expected to unpin it and allow it to be merged | |
2900 | * with the others. | |
2901 | */ | |
5893dfb9 FM |
2902 | drop_args.path = path; |
2903 | drop_args.start = file_pos; | |
2904 | drop_args.end = file_pos + num_bytes; | |
2905 | drop_args.replace_extent = true; | |
2906 | drop_args.extent_item_size = sizeof(*stack_fi); | |
2907 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); | |
79787eaa JM |
2908 | if (ret) |
2909 | goto out; | |
d899e052 | 2910 | |
5893dfb9 | 2911 | if (!drop_args.extent_inserted) { |
c553f94d | 2912 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
2913 | ins.offset = file_pos; |
2914 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2915 | ||
1acae57b | 2916 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
203f44c5 | 2917 | sizeof(*stack_fi)); |
1acae57b FDBM |
2918 | if (ret) |
2919 | goto out; | |
2920 | } | |
d899e052 | 2921 | leaf = path->nodes[0]; |
203f44c5 QW |
2922 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2923 | write_extent_buffer(leaf, stack_fi, | |
2924 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2925 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2926 | |
50564b65 | 2927 | btrfs_mark_buffer_dirty(trans, leaf); |
ce195332 | 2928 | btrfs_release_path(path); |
d899e052 | 2929 | |
2766ff61 FM |
2930 | /* |
2931 | * If we dropped an inline extent here, we know the range where it is | |
2932 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the | |
1a9fd417 | 2933 | * number of bytes only for that range containing the inline extent. |
2766ff61 FM |
2934 | * The remaining of the range will be processed when clearning the |
2935 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. | |
2936 | */ | |
2937 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { | |
2938 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); | |
2939 | ||
2940 | inline_size = drop_args.bytes_found - inline_size; | |
2941 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); | |
2942 | drop_args.bytes_found -= inline_size; | |
2943 | num_bytes -= sectorsize; | |
2944 | } | |
2945 | ||
2946 | if (update_inode_bytes) | |
2947 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); | |
d899e052 YZ |
2948 | |
2949 | ins.objectid = disk_bytenr; | |
2950 | ins.offset = disk_num_bytes; | |
2951 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2952 | |
c553f94d | 2953 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
2954 | if (ret) |
2955 | goto out; | |
2956 | ||
c553f94d | 2957 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
cb36a9bb OS |
2958 | file_pos - offset, |
2959 | qgroup_reserved, &ins); | |
79787eaa | 2960 | out: |
d899e052 | 2961 | btrfs_free_path(path); |
b9473439 | 2962 | |
79787eaa | 2963 | return ret; |
d899e052 YZ |
2964 | } |
2965 | ||
2ff7e61e | 2966 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2967 | u64 start, u64 len) |
2968 | { | |
32da5386 | 2969 | struct btrfs_block_group *cache; |
e570fd27 | 2970 | |
0b246afa | 2971 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2972 | ASSERT(cache); |
2973 | ||
2974 | spin_lock(&cache->lock); | |
2975 | cache->delalloc_bytes -= len; | |
2976 | spin_unlock(&cache->lock); | |
2977 | ||
2978 | btrfs_put_block_group(cache); | |
2979 | } | |
2980 | ||
203f44c5 | 2981 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
203f44c5 QW |
2982 | struct btrfs_ordered_extent *oe) |
2983 | { | |
2984 | struct btrfs_file_extent_item stack_fi; | |
2766ff61 | 2985 | bool update_inode_bytes; |
cb36a9bb OS |
2986 | u64 num_bytes = oe->num_bytes; |
2987 | u64 ram_bytes = oe->ram_bytes; | |
203f44c5 QW |
2988 | |
2989 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2990 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2991 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2992 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2993 | oe->disk_num_bytes); | |
cb36a9bb | 2994 | btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset); |
c1867eb3 DS |
2995 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) { |
2996 | num_bytes = oe->truncated_len; | |
2997 | ram_bytes = num_bytes; | |
2998 | } | |
cb36a9bb OS |
2999 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes); |
3000 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes); | |
203f44c5 QW |
3001 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
3002 | /* Encryption and other encoding is reserved and all 0 */ | |
3003 | ||
2766ff61 FM |
3004 | /* |
3005 | * For delalloc, when completing an ordered extent we update the inode's | |
3006 | * bytes when clearing the range in the inode's io tree, so pass false | |
3007 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), | |
3008 | * except if the ordered extent was truncated. | |
3009 | */ | |
3010 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || | |
7c0c7269 | 3011 | test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) || |
2766ff61 FM |
3012 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
3013 | ||
3c38c877 NB |
3014 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
3015 | oe->file_offset, &stack_fi, | |
2766ff61 | 3016 | update_inode_bytes, oe->qgroup_rsv); |
203f44c5 QW |
3017 | } |
3018 | ||
3019 | /* | |
3020 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
3021 | * an ordered extent if the range of bytes in the file it covers are |
3022 | * fully written. | |
3023 | */ | |
71df088c | 3024 | int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 3025 | { |
72e7e6ed NB |
3026 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
3027 | struct btrfs_root *root = inode->root; | |
3028 | struct btrfs_fs_info *fs_info = root->fs_info; | |
0ca1f7ce | 3029 | struct btrfs_trans_handle *trans = NULL; |
72e7e6ed | 3030 | struct extent_io_tree *io_tree = &inode->io_tree; |
2ac55d41 | 3031 | struct extent_state *cached_state = NULL; |
bffe633e | 3032 | u64 start, end; |
261507a0 | 3033 | int compress_type = 0; |
77cef2ec | 3034 | int ret = 0; |
bffe633e | 3035 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 3036 | bool freespace_inode; |
77cef2ec | 3037 | bool truncated = false; |
49940bdd | 3038 | bool clear_reserved_extent = true; |
2766ff61 | 3039 | unsigned int clear_bits = EXTENT_DEFRAG; |
a7e3b975 | 3040 | |
bffe633e OS |
3041 | start = ordered_extent->file_offset; |
3042 | end = start + ordered_extent->num_bytes - 1; | |
3043 | ||
a7e3b975 FM |
3044 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
3045 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
7c0c7269 OS |
3046 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) && |
3047 | !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags)) | |
2766ff61 | 3048 | clear_bits |= EXTENT_DELALLOC_NEW; |
e6dcd2dc | 3049 | |
72e7e6ed | 3050 | freespace_inode = btrfs_is_free_space_inode(inode); |
5f4403e1 IA |
3051 | if (!freespace_inode) |
3052 | btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent); | |
0cb59c99 | 3053 | |
5fd02043 JB |
3054 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
3055 | ret = -EIO; | |
3056 | goto out; | |
3057 | } | |
3058 | ||
71df088c | 3059 | if (btrfs_is_zoned(fs_info)) |
be1a1d7a NA |
3060 | btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, |
3061 | ordered_extent->disk_num_bytes); | |
d8e3fb10 | 3062 | |
77cef2ec JB |
3063 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
3064 | truncated = true; | |
3065 | logical_len = ordered_extent->truncated_len; | |
3066 | /* Truncated the entire extent, don't bother adding */ | |
3067 | if (!logical_len) | |
3068 | goto out; | |
3069 | } | |
3070 | ||
c2167754 | 3071 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 3072 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 3073 | |
72e7e6ed | 3074 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
3075 | if (freespace_inode) |
3076 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
3077 | else |
3078 | trans = btrfs_join_transaction(root); | |
3079 | if (IS_ERR(trans)) { | |
3080 | ret = PTR_ERR(trans); | |
3081 | trans = NULL; | |
3082 | goto out; | |
c2167754 | 3083 | } |
72e7e6ed | 3084 | trans->block_rsv = &inode->block_rsv; |
0a5d0dc5 | 3085 | ret = btrfs_update_inode_fallback(trans, inode); |
6c760c07 | 3086 | if (ret) /* -ENOMEM or corruption */ |
66642832 | 3087 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
3088 | goto out; |
3089 | } | |
e6dcd2dc | 3090 | |
2766ff61 | 3091 | clear_bits |= EXTENT_LOCKED; |
570eb97b | 3092 | lock_extent(io_tree, start, end, &cached_state); |
e6dcd2dc | 3093 | |
8d510121 NB |
3094 | if (freespace_inode) |
3095 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 3096 | else |
7a7eaa40 | 3097 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3098 | if (IS_ERR(trans)) { |
3099 | ret = PTR_ERR(trans); | |
3100 | trans = NULL; | |
a7e3b975 | 3101 | goto out; |
79787eaa | 3102 | } |
a79b7d4b | 3103 | |
72e7e6ed | 3104 | trans->block_rsv = &inode->block_rsv; |
c2167754 | 3105 | |
02c372e1 JT |
3106 | ret = btrfs_insert_raid_extent(trans, ordered_extent); |
3107 | if (ret) | |
3108 | goto out; | |
3109 | ||
c8b97818 | 3110 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3111 | compress_type = ordered_extent->compress_type; |
d899e052 | 3112 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3113 | BUG_ON(compress_type); |
72e7e6ed | 3114 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
3115 | ordered_extent->file_offset, |
3116 | ordered_extent->file_offset + | |
77cef2ec | 3117 | logical_len); |
343d8a30 NA |
3118 | btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr, |
3119 | ordered_extent->disk_num_bytes); | |
d899e052 | 3120 | } else { |
0b246afa | 3121 | BUG_ON(root == fs_info->tree_root); |
3c38c877 | 3122 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
49940bdd JB |
3123 | if (!ret) { |
3124 | clear_reserved_extent = false; | |
2ff7e61e | 3125 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
3126 | ordered_extent->disk_bytenr, |
3127 | ordered_extent->disk_num_bytes); | |
49940bdd | 3128 | } |
d899e052 | 3129 | } |
00deaf04 | 3130 | unpin_extent_cache(inode, ordered_extent->file_offset, |
bffe633e | 3131 | ordered_extent->num_bytes, trans->transid); |
79787eaa | 3132 | if (ret < 0) { |
66642832 | 3133 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3134 | goto out; |
79787eaa | 3135 | } |
2ac55d41 | 3136 | |
510f85ed | 3137 | ret = add_pending_csums(trans, &ordered_extent->list); |
ac01f26a NB |
3138 | if (ret) { |
3139 | btrfs_abort_transaction(trans, ret); | |
3140 | goto out; | |
3141 | } | |
e6dcd2dc | 3142 | |
2766ff61 FM |
3143 | /* |
3144 | * If this is a new delalloc range, clear its new delalloc flag to | |
3145 | * update the inode's number of bytes. This needs to be done first | |
3146 | * before updating the inode item. | |
3147 | */ | |
3148 | if ((clear_bits & EXTENT_DELALLOC_NEW) && | |
3149 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) | |
72e7e6ed | 3150 | clear_extent_bit(&inode->io_tree, start, end, |
2766ff61 | 3151 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
bd015294 | 3152 | &cached_state); |
2766ff61 | 3153 | |
72e7e6ed | 3154 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
0a5d0dc5 | 3155 | ret = btrfs_update_inode_fallback(trans, inode); |
6c760c07 | 3156 | if (ret) { /* -ENOMEM or corruption */ |
66642832 | 3157 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3158 | goto out; |
1ef30be1 JB |
3159 | } |
3160 | ret = 0; | |
c2167754 | 3161 | out: |
bd015294 | 3162 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
313facc5 | 3163 | &cached_state); |
a7e3b975 | 3164 | |
a698d075 | 3165 | if (trans) |
3a45bb20 | 3166 | btrfs_end_transaction(trans); |
0cb59c99 | 3167 | |
77cef2ec | 3168 | if (ret || truncated) { |
bffe633e | 3169 | u64 unwritten_start = start; |
77cef2ec | 3170 | |
d61bec08 JB |
3171 | /* |
3172 | * If we failed to finish this ordered extent for any reason we | |
3173 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered | |
3174 | * extent, and mark the inode with the error if it wasn't | |
3175 | * already set. Any error during writeback would have already | |
3176 | * set the mapping error, so we need to set it if we're the ones | |
3177 | * marking this ordered extent as failed. | |
3178 | */ | |
3179 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, | |
3180 | &ordered_extent->flags)) | |
3181 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); | |
3182 | ||
77cef2ec | 3183 | if (truncated) |
bffe633e OS |
3184 | unwritten_start += logical_len; |
3185 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec | 3186 | |
4c0c8cfc FM |
3187 | /* Drop extent maps for the part of the extent we didn't write. */ |
3188 | btrfs_drop_extent_map_range(inode, unwritten_start, end, false); | |
5fd02043 | 3189 | |
0bec9ef5 JB |
3190 | /* |
3191 | * If the ordered extent had an IOERR or something else went | |
3192 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3193 | * back to the allocator. We only free the extent in the |
3194 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3195 | * |
3196 | * If we made it past insert_reserved_file_extent before we | |
3197 | * errored out then we don't need to do this as the accounting | |
3198 | * has already been done. | |
0bec9ef5 | 3199 | */ |
77cef2ec | 3200 | if ((ret || !logical_len) && |
49940bdd | 3201 | clear_reserved_extent && |
77cef2ec | 3202 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
3203 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
3204 | /* | |
3205 | * Discard the range before returning it back to the | |
3206 | * free space pool | |
3207 | */ | |
46b27f50 | 3208 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 3209 | btrfs_discard_extent(fs_info, |
bffe633e OS |
3210 | ordered_extent->disk_bytenr, |
3211 | ordered_extent->disk_num_bytes, | |
3212 | NULL); | |
2ff7e61e | 3213 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
3214 | ordered_extent->disk_bytenr, |
3215 | ordered_extent->disk_num_bytes, 1); | |
e28b0211 BB |
3216 | /* |
3217 | * Actually free the qgroup rsv which was released when | |
3218 | * the ordered extent was created. | |
3219 | */ | |
3220 | btrfs_qgroup_free_refroot(fs_info, inode->root->root_key.objectid, | |
3221 | ordered_extent->qgroup_rsv, | |
3222 | BTRFS_QGROUP_RSV_DATA); | |
4eaaec24 | 3223 | } |
0bec9ef5 JB |
3224 | } |
3225 | ||
5fd02043 | 3226 | /* |
8bad3c02 LB |
3227 | * This needs to be done to make sure anybody waiting knows we are done |
3228 | * updating everything for this ordered extent. | |
5fd02043 | 3229 | */ |
72e7e6ed | 3230 | btrfs_remove_ordered_extent(inode, ordered_extent); |
5fd02043 | 3231 | |
e6dcd2dc CM |
3232 | /* once for us */ |
3233 | btrfs_put_ordered_extent(ordered_extent); | |
3234 | /* once for the tree */ | |
3235 | btrfs_put_ordered_extent(ordered_extent); | |
3236 | ||
5fd02043 JB |
3237 | return ret; |
3238 | } | |
3239 | ||
71df088c CH |
3240 | int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered) |
3241 | { | |
3242 | if (btrfs_is_zoned(btrfs_sb(ordered->inode->i_sb)) && | |
02c372e1 JT |
3243 | !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) && |
3244 | list_empty(&ordered->bioc_list)) | |
71df088c CH |
3245 | btrfs_finish_ordered_zoned(ordered); |
3246 | return btrfs_finish_one_ordered(ordered); | |
3247 | } | |
3248 | ||
ae643a74 QW |
3249 | /* |
3250 | * Verify the checksum for a single sector without any extra action that depend | |
3251 | * on the type of I/O. | |
3252 | */ | |
3253 | int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page, | |
3254 | u32 pgoff, u8 *csum, const u8 * const csum_expected) | |
3255 | { | |
3256 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
3257 | char *kaddr; | |
3258 | ||
3259 | ASSERT(pgoff + fs_info->sectorsize <= PAGE_SIZE); | |
3260 | ||
3261 | shash->tfm = fs_info->csum_shash; | |
3262 | ||
3263 | kaddr = kmap_local_page(page) + pgoff; | |
3264 | crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum); | |
3265 | kunmap_local(kaddr); | |
3266 | ||
3267 | if (memcmp(csum, csum_expected, fs_info->csum_size)) | |
3268 | return -EIO; | |
3269 | return 0; | |
211f90e6 CM |
3270 | } |
3271 | ||
265d4ac0 | 3272 | /* |
e5219044 CH |
3273 | * Verify the checksum of a single data sector. |
3274 | * | |
3275 | * @bbio: btrfs_io_bio which contains the csum | |
3276 | * @dev: device the sector is on | |
7ffd27e3 | 3277 | * @bio_offset: offset to the beginning of the bio (in bytes) |
e5219044 | 3278 | * @bv: bio_vec to check |
265d4ac0 | 3279 | * |
e5219044 CH |
3280 | * Check if the checksum on a data block is valid. When a checksum mismatch is |
3281 | * detected, report the error and fill the corrupted range with zero. | |
ae643a74 | 3282 | * |
e5219044 | 3283 | * Return %true if the sector is ok or had no checksum to start with, else %false. |
265d4ac0 | 3284 | */ |
e5219044 CH |
3285 | bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev, |
3286 | u32 bio_offset, struct bio_vec *bv) | |
dc380aea | 3287 | { |
e5219044 | 3288 | struct btrfs_inode *inode = bbio->inode; |
621af94a | 3289 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
e5219044 CH |
3290 | u64 file_offset = bbio->file_offset + bio_offset; |
3291 | u64 end = file_offset + bv->bv_len - 1; | |
d5178578 JT |
3292 | u8 *csum_expected; |
3293 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 3294 | |
3d49d0d3 | 3295 | ASSERT(bv->bv_len == fs_info->sectorsize); |
265d4ac0 | 3296 | |
e5219044 CH |
3297 | if (!bbio->csum) |
3298 | return true; | |
d5178578 | 3299 | |
e5219044 CH |
3300 | if (btrfs_is_data_reloc_root(inode->root) && |
3301 | test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM, | |
893fe243 | 3302 | NULL)) { |
e5219044 CH |
3303 | /* Skip the range without csum for data reloc inode */ |
3304 | clear_extent_bits(&inode->io_tree, file_offset, end, | |
3305 | EXTENT_NODATASUM); | |
3306 | return true; | |
3307 | } | |
3308 | ||
fa13661c JT |
3309 | csum_expected = bbio->csum + (bio_offset >> fs_info->sectorsize_bits) * |
3310 | fs_info->csum_size; | |
3d49d0d3 CH |
3311 | if (btrfs_check_sector_csum(fs_info, bv->bv_page, bv->bv_offset, csum, |
3312 | csum_expected)) | |
dc380aea | 3313 | goto zeroit; |
e5219044 | 3314 | return true; |
ae643a74 | 3315 | |
dc380aea | 3316 | zeroit: |
3d49d0d3 CH |
3317 | btrfs_print_data_csum_error(inode, file_offset, csum, csum_expected, |
3318 | bbio->mirror_num); | |
3319 | if (dev) | |
3320 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS); | |
3321 | memzero_bvec(bv); | |
3322 | return false; | |
07157aac | 3323 | } |
b888db2b | 3324 | |
c1c3fac2 | 3325 | /* |
9580503b | 3326 | * Perform a delayed iput on @inode. |
c1c3fac2 NB |
3327 | * |
3328 | * @inode: The inode we want to perform iput on | |
3329 | * | |
3330 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3331 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3332 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3333 | * transaction commit time/superblock commit/cleaner kthread. | |
3334 | */ | |
e55cf7ca | 3335 | void btrfs_add_delayed_iput(struct btrfs_inode *inode) |
24bbcf04 | 3336 | { |
e55cf7ca | 3337 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
866e98a4 | 3338 | unsigned long flags; |
24bbcf04 | 3339 | |
e55cf7ca | 3340 | if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1)) |
24bbcf04 YZ |
3341 | return; |
3342 | ||
034f784d | 3343 | atomic_inc(&fs_info->nr_delayed_iputs); |
866e98a4 FM |
3344 | /* |
3345 | * Need to be irq safe here because we can be called from either an irq | |
3346 | * context (see bio.c and btrfs_put_ordered_extent()) or a non-irq | |
3347 | * context. | |
3348 | */ | |
3349 | spin_lock_irqsave(&fs_info->delayed_iput_lock, flags); | |
e55cf7ca DS |
3350 | ASSERT(list_empty(&inode->delayed_iput)); |
3351 | list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs); | |
866e98a4 | 3352 | spin_unlock_irqrestore(&fs_info->delayed_iput_lock, flags); |
fd340d0f JB |
3353 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3354 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3355 | } |
3356 | ||
63611e73 JB |
3357 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3358 | struct btrfs_inode *inode) | |
3359 | { | |
3360 | list_del_init(&inode->delayed_iput); | |
866e98a4 | 3361 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3362 | iput(&inode->vfs_inode); |
3363 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3364 | wake_up(&fs_info->delayed_iputs_wait); | |
866e98a4 | 3365 | spin_lock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3366 | } |
3367 | ||
3368 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3369 | struct btrfs_inode *inode) | |
3370 | { | |
3371 | if (!list_empty(&inode->delayed_iput)) { | |
866e98a4 | 3372 | spin_lock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3373 | if (!list_empty(&inode->delayed_iput)) |
3374 | run_delayed_iput_locked(fs_info, inode); | |
866e98a4 | 3375 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3376 | } |
3377 | } | |
3378 | ||
2ff7e61e | 3379 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3380 | { |
866e98a4 FM |
3381 | /* |
3382 | * btrfs_put_ordered_extent() can run in irq context (see bio.c), which | |
3383 | * calls btrfs_add_delayed_iput() and that needs to lock | |
3384 | * fs_info->delayed_iput_lock. So we need to disable irqs here to | |
3385 | * prevent a deadlock. | |
3386 | */ | |
3387 | spin_lock_irq(&fs_info->delayed_iput_lock); | |
8089fe62 DS |
3388 | while (!list_empty(&fs_info->delayed_iputs)) { |
3389 | struct btrfs_inode *inode; | |
3390 | ||
3391 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3392 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3393 | run_delayed_iput_locked(fs_info, inode); |
866e98a4 FM |
3394 | if (need_resched()) { |
3395 | spin_unlock_irq(&fs_info->delayed_iput_lock); | |
3396 | cond_resched(); | |
3397 | spin_lock_irq(&fs_info->delayed_iput_lock); | |
3398 | } | |
24bbcf04 | 3399 | } |
866e98a4 | 3400 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3401 | } |
3402 | ||
e43eec81 | 3403 | /* |
2639631d NB |
3404 | * Wait for flushing all delayed iputs |
3405 | * | |
3406 | * @fs_info: the filesystem | |
034f784d JB |
3407 | * |
3408 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3409 | * set. Once they are all done running we will return, unless we are killed in | |
3410 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3411 | * that might get blocked on the iputs. | |
2639631d NB |
3412 | * |
3413 | * Return EINTR if we were killed, 0 if nothing's pending | |
034f784d JB |
3414 | */ |
3415 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3416 | { | |
3417 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3418 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3419 | if (ret) | |
3420 | return -EINTR; | |
3421 | return 0; | |
3422 | } | |
3423 | ||
7b128766 | 3424 | /* |
f7e9e8fc OS |
3425 | * This creates an orphan entry for the given inode in case something goes wrong |
3426 | * in the middle of an unlink. | |
7b128766 | 3427 | */ |
73f2e545 | 3428 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3429 | struct btrfs_inode *inode) |
7b128766 | 3430 | { |
d68fc57b | 3431 | int ret; |
7b128766 | 3432 | |
27919067 OS |
3433 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3434 | if (ret && ret != -EEXIST) { | |
3435 | btrfs_abort_transaction(trans, ret); | |
3436 | return ret; | |
d68fc57b YZ |
3437 | } |
3438 | ||
d68fc57b | 3439 | return 0; |
7b128766 JB |
3440 | } |
3441 | ||
3442 | /* | |
f7e9e8fc OS |
3443 | * We have done the delete so we can go ahead and remove the orphan item for |
3444 | * this particular inode. | |
7b128766 | 3445 | */ |
48a3b636 | 3446 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3447 | struct btrfs_inode *inode) |
7b128766 | 3448 | { |
27919067 | 3449 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3450 | } |
3451 | ||
3452 | /* | |
3453 | * this cleans up any orphans that may be left on the list from the last use | |
3454 | * of this root. | |
3455 | */ | |
66b4ffd1 | 3456 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3457 | { |
0b246afa | 3458 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3459 | struct btrfs_path *path; |
3460 | struct extent_buffer *leaf; | |
7b128766 JB |
3461 | struct btrfs_key key, found_key; |
3462 | struct btrfs_trans_handle *trans; | |
3463 | struct inode *inode; | |
8f6d7f4f | 3464 | u64 last_objectid = 0; |
f7e9e8fc | 3465 | int ret = 0, nr_unlink = 0; |
7b128766 | 3466 | |
54230013 | 3467 | if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) |
66b4ffd1 | 3468 | return 0; |
c71bf099 YZ |
3469 | |
3470 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3471 | if (!path) { |
3472 | ret = -ENOMEM; | |
3473 | goto out; | |
3474 | } | |
e4058b54 | 3475 | path->reada = READA_BACK; |
7b128766 JB |
3476 | |
3477 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3478 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3479 | key.offset = (u64)-1; |
3480 | ||
7b128766 JB |
3481 | while (1) { |
3482 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3483 | if (ret < 0) |
3484 | goto out; | |
7b128766 JB |
3485 | |
3486 | /* | |
3487 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3488 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3489 | * find the key and see if we have stuff that matches |
3490 | */ | |
3491 | if (ret > 0) { | |
66b4ffd1 | 3492 | ret = 0; |
7b128766 JB |
3493 | if (path->slots[0] == 0) |
3494 | break; | |
3495 | path->slots[0]--; | |
3496 | } | |
3497 | ||
3498 | /* pull out the item */ | |
3499 | leaf = path->nodes[0]; | |
7b128766 JB |
3500 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3501 | ||
3502 | /* make sure the item matches what we want */ | |
3503 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3504 | break; | |
962a298f | 3505 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3506 | break; |
3507 | ||
3508 | /* release the path since we're done with it */ | |
b3b4aa74 | 3509 | btrfs_release_path(path); |
7b128766 JB |
3510 | |
3511 | /* | |
3512 | * this is where we are basically btrfs_lookup, without the | |
3513 | * crossing root thing. we store the inode number in the | |
3514 | * offset of the orphan item. | |
3515 | */ | |
8f6d7f4f JB |
3516 | |
3517 | if (found_key.offset == last_objectid) { | |
a7f8de50 FM |
3518 | /* |
3519 | * We found the same inode as before. This means we were | |
3520 | * not able to remove its items via eviction triggered | |
3521 | * by an iput(). A transaction abort may have happened, | |
3522 | * due to -ENOSPC for example, so try to grab the error | |
3523 | * that lead to a transaction abort, if any. | |
3524 | */ | |
0b246afa JM |
3525 | btrfs_err(fs_info, |
3526 | "Error removing orphan entry, stopping orphan cleanup"); | |
a7f8de50 | 3527 | ret = BTRFS_FS_ERROR(fs_info) ?: -EINVAL; |
8f6d7f4f JB |
3528 | goto out; |
3529 | } | |
3530 | ||
3531 | last_objectid = found_key.offset; | |
3532 | ||
5d4f98a2 YZ |
3533 | found_key.objectid = found_key.offset; |
3534 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3535 | found_key.offset = 0; | |
0202e83f | 3536 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
cbaee87f FM |
3537 | if (IS_ERR(inode)) { |
3538 | ret = PTR_ERR(inode); | |
3539 | inode = NULL; | |
3540 | if (ret != -ENOENT) | |
3541 | goto out; | |
3542 | } | |
7b128766 | 3543 | |
cbaee87f | 3544 | if (!inode && root == fs_info->tree_root) { |
f8e9e0b0 | 3545 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3546 | int is_dead_root = 0; |
3547 | ||
3548 | /* | |
0c0218e9 | 3549 | * This is an orphan in the tree root. Currently these |
f8e9e0b0 | 3550 | * could come from 2 sources: |
0c0218e9 | 3551 | * a) a root (snapshot/subvolume) deletion in progress |
f8e9e0b0 | 3552 | * b) a free space cache inode |
0c0218e9 FM |
3553 | * We need to distinguish those two, as the orphan item |
3554 | * for a root must not get deleted before the deletion | |
3555 | * of the snapshot/subvolume's tree completes. | |
3556 | * | |
3557 | * btrfs_find_orphan_roots() ran before us, which has | |
3558 | * found all deleted roots and loaded them into | |
fc7cbcd4 | 3559 | * fs_info->fs_roots_radix. So here we can find if an |
0c0218e9 | 3560 | * orphan item corresponds to a deleted root by looking |
fc7cbcd4 | 3561 | * up the root from that radix tree. |
f8e9e0b0 | 3562 | */ |
a619b3c7 | 3563 | |
fc7cbcd4 DS |
3564 | spin_lock(&fs_info->fs_roots_radix_lock); |
3565 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3566 | (unsigned long)found_key.objectid); | |
a619b3c7 RK |
3567 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
3568 | is_dead_root = 1; | |
fc7cbcd4 | 3569 | spin_unlock(&fs_info->fs_roots_radix_lock); |
a619b3c7 | 3570 | |
f8e9e0b0 AJ |
3571 | if (is_dead_root) { |
3572 | /* prevent this orphan from being found again */ | |
3573 | key.offset = found_key.objectid - 1; | |
3574 | continue; | |
3575 | } | |
f7e9e8fc | 3576 | |
f8e9e0b0 | 3577 | } |
f7e9e8fc | 3578 | |
7b128766 | 3579 | /* |
f7e9e8fc | 3580 | * If we have an inode with links, there are a couple of |
70524253 BB |
3581 | * possibilities: |
3582 | * | |
3583 | * 1. We were halfway through creating fsverity metadata for the | |
3584 | * file. In that case, the orphan item represents incomplete | |
3585 | * fsverity metadata which must be cleaned up with | |
3586 | * btrfs_drop_verity_items and deleting the orphan item. | |
3587 | ||
3588 | * 2. Old kernels (before v3.12) used to create an | |
f7e9e8fc OS |
3589 | * orphan item for truncate indicating that there were possibly |
3590 | * extent items past i_size that needed to be deleted. In v3.12, | |
3591 | * truncate was changed to update i_size in sync with the extent | |
3592 | * items, but the (useless) orphan item was still created. Since | |
3593 | * v4.18, we don't create the orphan item for truncate at all. | |
3594 | * | |
3595 | * So, this item could mean that we need to do a truncate, but | |
3596 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3597 | * and was not cleanly unmounted. The odds of that are quite | |
3598 | * slim, and it's a pain to do the truncate now, so just delete | |
3599 | * the orphan item. | |
3600 | * | |
3601 | * It's also possible that this orphan item was supposed to be | |
3602 | * deleted but wasn't. The inode number may have been reused, | |
3603 | * but either way, we can delete the orphan item. | |
7b128766 | 3604 | */ |
cbaee87f FM |
3605 | if (!inode || inode->i_nlink) { |
3606 | if (inode) { | |
70524253 | 3607 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); |
f7e9e8fc | 3608 | iput(inode); |
b777d279 | 3609 | inode = NULL; |
70524253 BB |
3610 | if (ret) |
3611 | goto out; | |
3612 | } | |
a8c9e576 | 3613 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3614 | if (IS_ERR(trans)) { |
3615 | ret = PTR_ERR(trans); | |
3616 | goto out; | |
3617 | } | |
0b246afa JM |
3618 | btrfs_debug(fs_info, "auto deleting %Lu", |
3619 | found_key.objectid); | |
a8c9e576 JB |
3620 | ret = btrfs_del_orphan_item(trans, root, |
3621 | found_key.objectid); | |
3a45bb20 | 3622 | btrfs_end_transaction(trans); |
cbaee87f | 3623 | if (ret) |
4ef31a45 | 3624 | goto out; |
7b128766 JB |
3625 | continue; |
3626 | } | |
3627 | ||
f7e9e8fc | 3628 | nr_unlink++; |
7b128766 JB |
3629 | |
3630 | /* this will do delete_inode and everything for us */ | |
3631 | iput(inode); | |
3632 | } | |
3254c876 MX |
3633 | /* release the path since we're done with it */ |
3634 | btrfs_release_path(path); | |
3635 | ||
a575ceeb | 3636 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3637 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3638 | if (!IS_ERR(trans)) |
3a45bb20 | 3639 | btrfs_end_transaction(trans); |
d68fc57b | 3640 | } |
7b128766 JB |
3641 | |
3642 | if (nr_unlink) | |
0b246afa | 3643 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3644 | |
3645 | out: | |
3646 | if (ret) | |
0b246afa | 3647 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3648 | btrfs_free_path(path); |
3649 | return ret; | |
7b128766 JB |
3650 | } |
3651 | ||
46a53cca CM |
3652 | /* |
3653 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3654 | * don't find any xattrs, we know there can't be any acls. | |
3655 | * | |
3656 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3657 | */ | |
3658 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3659 | int slot, u64 objectid, |
3660 | int *first_xattr_slot) | |
46a53cca CM |
3661 | { |
3662 | u32 nritems = btrfs_header_nritems(leaf); | |
3663 | struct btrfs_key found_key; | |
f23b5a59 JB |
3664 | static u64 xattr_access = 0; |
3665 | static u64 xattr_default = 0; | |
46a53cca CM |
3666 | int scanned = 0; |
3667 | ||
f23b5a59 | 3668 | if (!xattr_access) { |
97d79299 AG |
3669 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3670 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3671 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3672 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3673 | } |
3674 | ||
46a53cca | 3675 | slot++; |
63541927 | 3676 | *first_xattr_slot = -1; |
46a53cca CM |
3677 | while (slot < nritems) { |
3678 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3679 | ||
3680 | /* we found a different objectid, there must not be acls */ | |
3681 | if (found_key.objectid != objectid) | |
3682 | return 0; | |
3683 | ||
3684 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3685 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3686 | if (*first_xattr_slot == -1) |
3687 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3688 | if (found_key.offset == xattr_access || |
3689 | found_key.offset == xattr_default) | |
3690 | return 1; | |
3691 | } | |
46a53cca CM |
3692 | |
3693 | /* | |
3694 | * we found a key greater than an xattr key, there can't | |
3695 | * be any acls later on | |
3696 | */ | |
3697 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3698 | return 0; | |
3699 | ||
3700 | slot++; | |
3701 | scanned++; | |
3702 | ||
3703 | /* | |
3704 | * it goes inode, inode backrefs, xattrs, extents, | |
3705 | * so if there are a ton of hard links to an inode there can | |
3706 | * be a lot of backrefs. Don't waste time searching too hard, | |
3707 | * this is just an optimization | |
3708 | */ | |
3709 | if (scanned >= 8) | |
3710 | break; | |
3711 | } | |
3712 | /* we hit the end of the leaf before we found an xattr or | |
3713 | * something larger than an xattr. We have to assume the inode | |
3714 | * has acls | |
3715 | */ | |
63541927 FDBM |
3716 | if (*first_xattr_slot == -1) |
3717 | *first_xattr_slot = slot; | |
46a53cca CM |
3718 | return 1; |
3719 | } | |
3720 | ||
d352ac68 CM |
3721 | /* |
3722 | * read an inode from the btree into the in-memory inode | |
3723 | */ | |
4222ea71 FM |
3724 | static int btrfs_read_locked_inode(struct inode *inode, |
3725 | struct btrfs_path *in_path) | |
39279cc3 | 3726 | { |
0b246afa | 3727 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3728 | struct btrfs_path *path = in_path; |
5f39d397 | 3729 | struct extent_buffer *leaf; |
39279cc3 CM |
3730 | struct btrfs_inode_item *inode_item; |
3731 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3732 | struct btrfs_key location; | |
67de1176 | 3733 | unsigned long ptr; |
46a53cca | 3734 | int maybe_acls; |
618e21d5 | 3735 | u32 rdev; |
39279cc3 | 3736 | int ret; |
2f7e33d4 | 3737 | bool filled = false; |
63541927 | 3738 | int first_xattr_slot; |
2f7e33d4 MX |
3739 | |
3740 | ret = btrfs_fill_inode(inode, &rdev); | |
3741 | if (!ret) | |
3742 | filled = true; | |
39279cc3 | 3743 | |
4222ea71 FM |
3744 | if (!path) { |
3745 | path = btrfs_alloc_path(); | |
3746 | if (!path) | |
3747 | return -ENOMEM; | |
3748 | } | |
1748f843 | 3749 | |
39279cc3 | 3750 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3751 | |
39279cc3 | 3752 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3753 | if (ret) { |
4222ea71 FM |
3754 | if (path != in_path) |
3755 | btrfs_free_path(path); | |
f5b3a417 | 3756 | return ret; |
67710892 | 3757 | } |
39279cc3 | 3758 | |
5f39d397 | 3759 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3760 | |
3761 | if (filled) | |
67de1176 | 3762 | goto cache_index; |
2f7e33d4 | 3763 | |
5f39d397 CM |
3764 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3765 | struct btrfs_inode_item); | |
5f39d397 | 3766 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3767 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3768 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3769 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3770 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3771 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3772 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3773 | |
b1c38a13 JL |
3774 | inode_set_atime(inode, btrfs_timespec_sec(leaf, &inode_item->atime), |
3775 | btrfs_timespec_nsec(leaf, &inode_item->atime)); | |
5f39d397 | 3776 | |
b1c38a13 JL |
3777 | inode_set_mtime(inode, btrfs_timespec_sec(leaf, &inode_item->mtime), |
3778 | btrfs_timespec_nsec(leaf, &inode_item->mtime)); | |
5f39d397 | 3779 | |
2a9462de JL |
3780 | inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime), |
3781 | btrfs_timespec_nsec(leaf, &inode_item->ctime)); | |
5f39d397 | 3782 | |
c6e8f898 DS |
3783 | BTRFS_I(inode)->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime); |
3784 | BTRFS_I(inode)->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3785 | |
a76a3cd4 | 3786 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3787 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3788 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3789 | ||
c7f88c4e JL |
3790 | inode_set_iversion_queried(inode, |
3791 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3792 | inode->i_generation = BTRFS_I(inode)->generation; |
3793 | inode->i_rdev = 0; | |
3794 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3795 | ||
3796 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
77eea05e BB |
3797 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
3798 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); | |
6e17d30b YD |
3799 | |
3800 | cache_index: | |
5dc562c5 JB |
3801 | /* |
3802 | * If we were modified in the current generation and evicted from memory | |
3803 | * and then re-read we need to do a full sync since we don't have any | |
3804 | * idea about which extents were modified before we were evicted from | |
3805 | * cache. | |
6e17d30b YD |
3806 | * |
3807 | * This is required for both inode re-read from disk and delayed inode | |
088aea3b | 3808 | * in delayed_nodes_tree. |
5dc562c5 | 3809 | */ |
4a4f8fe2 | 3810 | if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info)) |
5dc562c5 JB |
3811 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3812 | &BTRFS_I(inode)->runtime_flags); | |
3813 | ||
bde6c242 FM |
3814 | /* |
3815 | * We don't persist the id of the transaction where an unlink operation | |
3816 | * against the inode was last made. So here we assume the inode might | |
3817 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3818 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3819 | * between the inode and its parent if the inode is fsync'ed and the log | |
3820 | * replayed. For example, in the scenario: | |
3821 | * | |
3822 | * touch mydir/foo | |
3823 | * ln mydir/foo mydir/bar | |
3824 | * sync | |
3825 | * unlink mydir/bar | |
3826 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3827 | * xfs_io -c fsync mydir/foo | |
3828 | * <power failure> | |
3829 | * mount fs, triggers fsync log replay | |
3830 | * | |
3831 | * We must make sure that when we fsync our inode foo we also log its | |
3832 | * parent inode, otherwise after log replay the parent still has the | |
3833 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3834 | * and doesn't have an inode ref with the name "bar" anymore. | |
3835 | * | |
3836 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3837 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3838 | * transaction commits on fsync if our inode is a directory, or if our |
3839 | * inode is not a directory, logging its parent unnecessarily. | |
3840 | */ | |
3841 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3842 | ||
3ebac17c FM |
3843 | /* |
3844 | * Same logic as for last_unlink_trans. We don't persist the generation | |
3845 | * of the last transaction where this inode was used for a reflink | |
3846 | * operation, so after eviction and reloading the inode we must be | |
3847 | * pessimistic and assume the last transaction that modified the inode. | |
3848 | */ | |
3849 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
3850 | ||
67de1176 MX |
3851 | path->slots[0]++; |
3852 | if (inode->i_nlink != 1 || | |
3853 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3854 | goto cache_acl; | |
3855 | ||
3856 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3857 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3858 | goto cache_acl; |
3859 | ||
3860 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3861 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3862 | struct btrfs_inode_ref *ref; | |
3863 | ||
3864 | ref = (struct btrfs_inode_ref *)ptr; | |
3865 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3866 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3867 | struct btrfs_inode_extref *extref; | |
3868 | ||
3869 | extref = (struct btrfs_inode_extref *)ptr; | |
3870 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3871 | extref); | |
3872 | } | |
2f7e33d4 | 3873 | cache_acl: |
46a53cca CM |
3874 | /* |
3875 | * try to precache a NULL acl entry for files that don't have | |
3876 | * any xattrs or acls | |
3877 | */ | |
33345d01 | 3878 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3879 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3880 | if (first_xattr_slot != -1) { |
3881 | path->slots[0] = first_xattr_slot; | |
3882 | ret = btrfs_load_inode_props(inode, path); | |
3883 | if (ret) | |
0b246afa | 3884 | btrfs_err(fs_info, |
351fd353 | 3885 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3886 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3887 | root->root_key.objectid, ret); |
3888 | } | |
4222ea71 FM |
3889 | if (path != in_path) |
3890 | btrfs_free_path(path); | |
63541927 | 3891 | |
72c04902 AV |
3892 | if (!maybe_acls) |
3893 | cache_no_acl(inode); | |
46a53cca | 3894 | |
39279cc3 | 3895 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3896 | case S_IFREG: |
3897 | inode->i_mapping->a_ops = &btrfs_aops; | |
3898 | inode->i_fop = &btrfs_file_operations; | |
3899 | inode->i_op = &btrfs_file_inode_operations; | |
3900 | break; | |
3901 | case S_IFDIR: | |
3902 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3903 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3904 | break; |
3905 | case S_IFLNK: | |
3906 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3907 | inode_nohighmem(inode); |
4779cc04 | 3908 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3909 | break; |
618e21d5 | 3910 | default: |
0279b4cd | 3911 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3912 | init_special_inode(inode, inode->i_mode, rdev); |
3913 | break; | |
39279cc3 | 3914 | } |
6cbff00f | 3915 | |
7b6a221e | 3916 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3917 | return 0; |
39279cc3 CM |
3918 | } |
3919 | ||
d352ac68 CM |
3920 | /* |
3921 | * given a leaf and an inode, copy the inode fields into the leaf | |
3922 | */ | |
e02119d5 CM |
3923 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3924 | struct extent_buffer *leaf, | |
5f39d397 | 3925 | struct btrfs_inode_item *item, |
39279cc3 CM |
3926 | struct inode *inode) |
3927 | { | |
51fab693 | 3928 | struct btrfs_map_token token; |
77eea05e | 3929 | u64 flags; |
51fab693 | 3930 | |
c82f823c | 3931 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3932 | |
cc4c13d5 DS |
3933 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3934 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3935 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3936 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3937 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3938 | ||
3939 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
b1c38a13 | 3940 | inode_get_atime_sec(inode)); |
cc4c13d5 | 3941 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
b1c38a13 | 3942 | inode_get_atime_nsec(inode)); |
cc4c13d5 DS |
3943 | |
3944 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
b1c38a13 | 3945 | inode_get_mtime_sec(inode)); |
cc4c13d5 | 3946 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
b1c38a13 | 3947 | inode_get_mtime_nsec(inode)); |
cc4c13d5 DS |
3948 | |
3949 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
b1c38a13 | 3950 | inode_get_ctime_sec(inode)); |
cc4c13d5 | 3951 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
b1c38a13 | 3952 | inode_get_ctime_nsec(inode)); |
cc4c13d5 | 3953 | |
c6e8f898 DS |
3954 | btrfs_set_token_timespec_sec(&token, &item->otime, BTRFS_I(inode)->i_otime_sec); |
3955 | btrfs_set_token_timespec_nsec(&token, &item->otime, BTRFS_I(inode)->i_otime_nsec); | |
cc4c13d5 DS |
3956 | |
3957 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3958 | btrfs_set_token_inode_generation(&token, item, | |
3959 | BTRFS_I(inode)->generation); | |
3960 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3961 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3962 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
3963 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
3964 | BTRFS_I(inode)->ro_flags); | |
3965 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 3966 | btrfs_set_token_inode_block_group(&token, item, 0); |
39279cc3 CM |
3967 | } |
3968 | ||
d352ac68 CM |
3969 | /* |
3970 | * copy everything in the in-memory inode into the btree. | |
3971 | */ | |
2115133f | 3972 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
07a274a8 | 3973 | struct btrfs_inode *inode) |
39279cc3 CM |
3974 | { |
3975 | struct btrfs_inode_item *inode_item; | |
3976 | struct btrfs_path *path; | |
5f39d397 | 3977 | struct extent_buffer *leaf; |
39279cc3 CM |
3978 | int ret; |
3979 | ||
3980 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3981 | if (!path) |
3982 | return -ENOMEM; | |
3983 | ||
07a274a8 | 3984 | ret = btrfs_lookup_inode(trans, inode->root, path, &inode->location, 1); |
39279cc3 CM |
3985 | if (ret) { |
3986 | if (ret > 0) | |
3987 | ret = -ENOENT; | |
3988 | goto failed; | |
3989 | } | |
3990 | ||
5f39d397 CM |
3991 | leaf = path->nodes[0]; |
3992 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3993 | struct btrfs_inode_item); |
39279cc3 | 3994 | |
dfeb9e7c | 3995 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
50564b65 | 3996 | btrfs_mark_buffer_dirty(trans, leaf); |
dfeb9e7c | 3997 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3998 | ret = 0; |
3999 | failed: | |
39279cc3 CM |
4000 | btrfs_free_path(path); |
4001 | return ret; | |
4002 | } | |
4003 | ||
2115133f CM |
4004 | /* |
4005 | * copy everything in the in-memory inode into the btree. | |
4006 | */ | |
cddaaacc | 4007 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
cddaaacc | 4008 | struct btrfs_inode *inode) |
2115133f | 4009 | { |
8b9d0322 | 4010 | struct btrfs_root *root = inode->root; |
0b246afa | 4011 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
4012 | int ret; |
4013 | ||
4014 | /* | |
4015 | * If the inode is a free space inode, we can deadlock during commit | |
4016 | * if we put it into the delayed code. | |
4017 | * | |
4018 | * The data relocation inode should also be directly updated | |
4019 | * without delay | |
4020 | */ | |
9a56fcd1 | 4021 | if (!btrfs_is_free_space_inode(inode) |
37f00a6d | 4022 | && !btrfs_is_data_reloc_root(root) |
0b246afa | 4023 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4024 | btrfs_update_root_times(trans, root); |
4025 | ||
04bd8e94 | 4026 | ret = btrfs_delayed_update_inode(trans, inode); |
2115133f | 4027 | if (!ret) |
9a56fcd1 | 4028 | btrfs_set_inode_last_trans(trans, inode); |
2115133f CM |
4029 | return ret; |
4030 | } | |
4031 | ||
07a274a8 | 4032 | return btrfs_update_inode_item(trans, inode); |
2115133f CM |
4033 | } |
4034 | ||
729f7961 | 4035 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
0a5d0dc5 | 4036 | struct btrfs_inode *inode) |
2115133f CM |
4037 | { |
4038 | int ret; | |
4039 | ||
8b9d0322 | 4040 | ret = btrfs_update_inode(trans, inode); |
2115133f | 4041 | if (ret == -ENOSPC) |
07a274a8 | 4042 | return btrfs_update_inode_item(trans, inode); |
2115133f CM |
4043 | return ret; |
4044 | } | |
4045 | ||
d352ac68 CM |
4046 | /* |
4047 | * unlink helper that gets used here in inode.c and in the tree logging | |
4048 | * recovery code. It remove a link in a directory with a given name, and | |
4049 | * also drops the back refs in the inode to the directory | |
4050 | */ | |
92986796 | 4051 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e NB |
4052 | struct btrfs_inode *dir, |
4053 | struct btrfs_inode *inode, | |
6db75318 | 4054 | const struct fscrypt_str *name, |
88d2beec | 4055 | struct btrfs_rename_ctx *rename_ctx) |
39279cc3 | 4056 | { |
4467af88 | 4057 | struct btrfs_root *root = dir->root; |
0b246afa | 4058 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4059 | struct btrfs_path *path; |
39279cc3 | 4060 | int ret = 0; |
39279cc3 | 4061 | struct btrfs_dir_item *di; |
aec7477b | 4062 | u64 index; |
33345d01 LZ |
4063 | u64 ino = btrfs_ino(inode); |
4064 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4065 | |
4066 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4067 | if (!path) { |
4068 | ret = -ENOMEM; | |
554233a6 | 4069 | goto out; |
54aa1f4d CM |
4070 | } |
4071 | ||
e43eec81 | 4072 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1); |
3cf5068f LB |
4073 | if (IS_ERR_OR_NULL(di)) { |
4074 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
4075 | goto err; |
4076 | } | |
39279cc3 | 4077 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4078 | if (ret) |
4079 | goto err; | |
b3b4aa74 | 4080 | btrfs_release_path(path); |
39279cc3 | 4081 | |
67de1176 MX |
4082 | /* |
4083 | * If we don't have dir index, we have to get it by looking up | |
4084 | * the inode ref, since we get the inode ref, remove it directly, | |
4085 | * it is unnecessary to do delayed deletion. | |
4086 | * | |
4087 | * But if we have dir index, needn't search inode ref to get it. | |
4088 | * Since the inode ref is close to the inode item, it is better | |
4089 | * that we delay to delete it, and just do this deletion when | |
4090 | * we update the inode item. | |
4091 | */ | |
4ec5934e | 4092 | if (inode->dir_index) { |
67de1176 MX |
4093 | ret = btrfs_delayed_delete_inode_ref(inode); |
4094 | if (!ret) { | |
4ec5934e | 4095 | index = inode->dir_index; |
67de1176 MX |
4096 | goto skip_backref; |
4097 | } | |
4098 | } | |
4099 | ||
e43eec81 | 4100 | ret = btrfs_del_inode_ref(trans, root, name, ino, dir_ino, &index); |
aec7477b | 4101 | if (ret) { |
0b246afa | 4102 | btrfs_info(fs_info, |
c2cf52eb | 4103 | "failed to delete reference to %.*s, inode %llu parent %llu", |
e43eec81 | 4104 | name->len, name->name, ino, dir_ino); |
66642832 | 4105 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4106 | goto err; |
4107 | } | |
67de1176 | 4108 | skip_backref: |
88d2beec FM |
4109 | if (rename_ctx) |
4110 | rename_ctx->index = index; | |
4111 | ||
9add2945 | 4112 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4113 | if (ret) { |
66642832 | 4114 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4115 | goto err; |
79787eaa | 4116 | } |
39279cc3 | 4117 | |
259c4b96 FM |
4118 | /* |
4119 | * If we are in a rename context, we don't need to update anything in the | |
4120 | * log. That will be done later during the rename by btrfs_log_new_name(). | |
143823cf | 4121 | * Besides that, doing it here would only cause extra unnecessary btree |
259c4b96 FM |
4122 | * operations on the log tree, increasing latency for applications. |
4123 | */ | |
4124 | if (!rename_ctx) { | |
e43eec81 STD |
4125 | btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino); |
4126 | btrfs_del_dir_entries_in_log(trans, root, name, dir, index); | |
259c4b96 | 4127 | } |
63611e73 JB |
4128 | |
4129 | /* | |
4130 | * If we have a pending delayed iput we could end up with the final iput | |
4131 | * being run in btrfs-cleaner context. If we have enough of these built | |
4132 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
4133 | * way to throttle the unlinks. Since we're currently holding a ref on | |
4134 | * the inode we can run the delayed iput here without any issues as the | |
4135 | * final iput won't be done until after we drop the ref we're currently | |
4136 | * holding. | |
4137 | */ | |
4138 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
4139 | err: |
4140 | btrfs_free_path(path); | |
e02119d5 CM |
4141 | if (ret) |
4142 | goto out; | |
4143 | ||
e43eec81 | 4144 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2); |
4ec5934e NB |
4145 | inode_inc_iversion(&inode->vfs_inode); |
4146 | inode_inc_iversion(&dir->vfs_inode); | |
b1c38a13 | 4147 | inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode)); |
8b9d0322 | 4148 | ret = btrfs_update_inode(trans, dir); |
e02119d5 | 4149 | out: |
39279cc3 CM |
4150 | return ret; |
4151 | } | |
4152 | ||
92986796 | 4153 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e | 4154 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
6db75318 | 4155 | const struct fscrypt_str *name) |
92986796 AV |
4156 | { |
4157 | int ret; | |
e43eec81 STD |
4158 | |
4159 | ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL); | |
92986796 | 4160 | if (!ret) { |
4ec5934e | 4161 | drop_nlink(&inode->vfs_inode); |
8b9d0322 | 4162 | ret = btrfs_update_inode(trans, inode); |
92986796 AV |
4163 | } |
4164 | return ret; | |
4165 | } | |
39279cc3 | 4166 | |
a22285a6 YZ |
4167 | /* |
4168 | * helper to start transaction for unlink and rmdir. | |
4169 | * | |
d52be818 JB |
4170 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4171 | * if we cannot make our reservations the normal way try and see if there is | |
4172 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4173 | * allow the unlink to occur. | |
a22285a6 | 4174 | */ |
e569b1d5 | 4175 | static struct btrfs_trans_handle *__unlink_start_trans(struct btrfs_inode *dir) |
4df27c4d | 4176 | { |
e569b1d5 | 4177 | struct btrfs_root *root = dir->root; |
4df27c4d | 4178 | |
5630e2bc FM |
4179 | return btrfs_start_transaction_fallback_global_rsv(root, |
4180 | BTRFS_UNLINK_METADATA_UNITS); | |
a22285a6 YZ |
4181 | } |
4182 | ||
4183 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4184 | { | |
a22285a6 | 4185 | struct btrfs_trans_handle *trans; |
2b0143b5 | 4186 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4187 | int ret; |
ab3c5c18 | 4188 | struct fscrypt_name fname; |
a22285a6 | 4189 | |
ab3c5c18 STD |
4190 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
4191 | if (ret) | |
4192 | return ret; | |
ab3c5c18 STD |
4193 | |
4194 | /* This needs to handle no-key deletions later on */ | |
a22285a6 | 4195 | |
e569b1d5 | 4196 | trans = __unlink_start_trans(BTRFS_I(dir)); |
ab3c5c18 STD |
4197 | if (IS_ERR(trans)) { |
4198 | ret = PTR_ERR(trans); | |
4199 | goto fscrypt_free; | |
4200 | } | |
5f39d397 | 4201 | |
4ec5934e | 4202 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
59fcf388 | 4203 | false); |
12fcfd22 | 4204 | |
e43eec81 | 4205 | ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4206 | &fname.disk_name); |
b532402e | 4207 | if (ret) |
ab3c5c18 | 4208 | goto end_trans; |
7b128766 | 4209 | |
a22285a6 | 4210 | if (inode->i_nlink == 0) { |
73f2e545 | 4211 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e | 4212 | if (ret) |
ab3c5c18 | 4213 | goto end_trans; |
a22285a6 | 4214 | } |
7b128766 | 4215 | |
ab3c5c18 | 4216 | end_trans: |
3a45bb20 | 4217 | btrfs_end_transaction(trans); |
4467af88 | 4218 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
ab3c5c18 STD |
4219 | fscrypt_free: |
4220 | fscrypt_free_filename(&fname); | |
39279cc3 CM |
4221 | return ret; |
4222 | } | |
4223 | ||
f60a2364 | 4224 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
5b7544cb | 4225 | struct btrfs_inode *dir, struct dentry *dentry) |
4df27c4d | 4226 | { |
5b7544cb | 4227 | struct btrfs_root *root = dir->root; |
045d3967 | 4228 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
4229 | struct btrfs_path *path; |
4230 | struct extent_buffer *leaf; | |
4231 | struct btrfs_dir_item *di; | |
4232 | struct btrfs_key key; | |
4233 | u64 index; | |
4234 | int ret; | |
045d3967 | 4235 | u64 objectid; |
5b7544cb | 4236 | u64 dir_ino = btrfs_ino(dir); |
ab3c5c18 STD |
4237 | struct fscrypt_name fname; |
4238 | ||
5b7544cb | 4239 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); |
ab3c5c18 STD |
4240 | if (ret) |
4241 | return ret; | |
ab3c5c18 STD |
4242 | |
4243 | /* This needs to handle no-key deletions later on */ | |
4df27c4d | 4244 | |
045d3967 JB |
4245 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
4246 | objectid = inode->root->root_key.objectid; | |
4247 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
4248 | objectid = inode->location.objectid; | |
4249 | } else { | |
4250 | WARN_ON(1); | |
ab3c5c18 | 4251 | fscrypt_free_filename(&fname); |
045d3967 JB |
4252 | return -EINVAL; |
4253 | } | |
4254 | ||
4df27c4d | 4255 | path = btrfs_alloc_path(); |
ab3c5c18 STD |
4256 | if (!path) { |
4257 | ret = -ENOMEM; | |
4258 | goto out; | |
4259 | } | |
4df27c4d | 4260 | |
33345d01 | 4261 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
6db75318 | 4262 | &fname.disk_name, -1); |
79787eaa | 4263 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4264 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4265 | goto out; |
4266 | } | |
4df27c4d YZ |
4267 | |
4268 | leaf = path->nodes[0]; | |
4269 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4270 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4271 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4272 | if (ret) { |
66642832 | 4273 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4274 | goto out; |
4275 | } | |
b3b4aa74 | 4276 | btrfs_release_path(path); |
4df27c4d | 4277 | |
d49d3287 JB |
4278 | /* |
4279 | * This is a placeholder inode for a subvolume we didn't have a | |
4280 | * reference to at the time of the snapshot creation. In the meantime | |
4281 | * we could have renamed the real subvol link into our snapshot, so | |
1a9fd417 | 4282 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
d49d3287 JB |
4283 | * Instead simply lookup the dir_index_item for this entry so we can |
4284 | * remove it. Otherwise we know we have a ref to the root and we can | |
4285 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
4286 | */ | |
4287 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
6db75318 | 4288 | di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name); |
79787eaa JM |
4289 | if (IS_ERR_OR_NULL(di)) { |
4290 | if (!di) | |
4291 | ret = -ENOENT; | |
4292 | else | |
4293 | ret = PTR_ERR(di); | |
66642832 | 4294 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4295 | goto out; |
4296 | } | |
4df27c4d YZ |
4297 | |
4298 | leaf = path->nodes[0]; | |
4299 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 4300 | index = key.offset; |
d49d3287 JB |
4301 | btrfs_release_path(path); |
4302 | } else { | |
4303 | ret = btrfs_del_root_ref(trans, objectid, | |
4304 | root->root_key.objectid, dir_ino, | |
6db75318 | 4305 | &index, &fname.disk_name); |
d49d3287 JB |
4306 | if (ret) { |
4307 | btrfs_abort_transaction(trans, ret); | |
4308 | goto out; | |
4309 | } | |
4df27c4d YZ |
4310 | } |
4311 | ||
5b7544cb | 4312 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4313 | if (ret) { |
66642832 | 4314 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4315 | goto out; |
4316 | } | |
4df27c4d | 4317 | |
5b7544cb DS |
4318 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2); |
4319 | inode_inc_iversion(&dir->vfs_inode); | |
b1c38a13 | 4320 | inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode)); |
0a5d0dc5 | 4321 | ret = btrfs_update_inode_fallback(trans, dir); |
79787eaa | 4322 | if (ret) |
66642832 | 4323 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4324 | out: |
71d7aed0 | 4325 | btrfs_free_path(path); |
ab3c5c18 | 4326 | fscrypt_free_filename(&fname); |
79787eaa | 4327 | return ret; |
4df27c4d YZ |
4328 | } |
4329 | ||
ec42f167 MT |
4330 | /* |
4331 | * Helper to check if the subvolume references other subvolumes or if it's | |
4332 | * default. | |
4333 | */ | |
f60a2364 | 4334 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4335 | { |
4336 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4337 | struct btrfs_path *path; | |
4338 | struct btrfs_dir_item *di; | |
4339 | struct btrfs_key key; | |
6db75318 | 4340 | struct fscrypt_str name = FSTR_INIT("default", 7); |
ec42f167 MT |
4341 | u64 dir_id; |
4342 | int ret; | |
4343 | ||
4344 | path = btrfs_alloc_path(); | |
4345 | if (!path) | |
4346 | return -ENOMEM; | |
4347 | ||
4348 | /* Make sure this root isn't set as the default subvol */ | |
4349 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4350 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
e43eec81 | 4351 | dir_id, &name, 0); |
ec42f167 MT |
4352 | if (di && !IS_ERR(di)) { |
4353 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4354 | if (key.objectid == root->root_key.objectid) { | |
4355 | ret = -EPERM; | |
4356 | btrfs_err(fs_info, | |
4357 | "deleting default subvolume %llu is not allowed", | |
4358 | key.objectid); | |
4359 | goto out; | |
4360 | } | |
4361 | btrfs_release_path(path); | |
4362 | } | |
4363 | ||
4364 | key.objectid = root->root_key.objectid; | |
4365 | key.type = BTRFS_ROOT_REF_KEY; | |
4366 | key.offset = (u64)-1; | |
4367 | ||
4368 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4369 | if (ret < 0) | |
4370 | goto out; | |
4371 | BUG_ON(ret == 0); | |
4372 | ||
4373 | ret = 0; | |
4374 | if (path->slots[0] > 0) { | |
4375 | path->slots[0]--; | |
4376 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4377 | if (key.objectid == root->root_key.objectid && | |
4378 | key.type == BTRFS_ROOT_REF_KEY) | |
4379 | ret = -ENOTEMPTY; | |
4380 | } | |
4381 | out: | |
4382 | btrfs_free_path(path); | |
4383 | return ret; | |
4384 | } | |
4385 | ||
20a68004 NB |
4386 | /* Delete all dentries for inodes belonging to the root */ |
4387 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4388 | { | |
4389 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4390 | struct rb_node *node; | |
4391 | struct rb_node *prev; | |
4392 | struct btrfs_inode *entry; | |
4393 | struct inode *inode; | |
4394 | u64 objectid = 0; | |
4395 | ||
84961539 | 4396 | if (!BTRFS_FS_ERROR(fs_info)) |
20a68004 NB |
4397 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
4398 | ||
4399 | spin_lock(&root->inode_lock); | |
4400 | again: | |
4401 | node = root->inode_tree.rb_node; | |
4402 | prev = NULL; | |
4403 | while (node) { | |
4404 | prev = node; | |
4405 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4406 | ||
37508515 | 4407 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4408 | node = node->rb_left; |
37508515 | 4409 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4410 | node = node->rb_right; |
4411 | else | |
4412 | break; | |
4413 | } | |
4414 | if (!node) { | |
4415 | while (prev) { | |
4416 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4417 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4418 | node = prev; |
4419 | break; | |
4420 | } | |
4421 | prev = rb_next(prev); | |
4422 | } | |
4423 | } | |
4424 | while (node) { | |
4425 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4426 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4427 | inode = igrab(&entry->vfs_inode); |
4428 | if (inode) { | |
4429 | spin_unlock(&root->inode_lock); | |
4430 | if (atomic_read(&inode->i_count) > 1) | |
4431 | d_prune_aliases(inode); | |
4432 | /* | |
4433 | * btrfs_drop_inode will have it removed from the inode | |
4434 | * cache when its usage count hits zero. | |
4435 | */ | |
4436 | iput(inode); | |
4437 | cond_resched(); | |
4438 | spin_lock(&root->inode_lock); | |
4439 | goto again; | |
4440 | } | |
4441 | ||
4442 | if (cond_resched_lock(&root->inode_lock)) | |
4443 | goto again; | |
4444 | ||
4445 | node = rb_next(node); | |
4446 | } | |
4447 | spin_unlock(&root->inode_lock); | |
4448 | } | |
4449 | ||
3c4f91e2 | 4450 | int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry) |
f60a2364 MT |
4451 | { |
4452 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
3c4f91e2 | 4453 | struct btrfs_root *root = dir->root; |
f60a2364 MT |
4454 | struct inode *inode = d_inode(dentry); |
4455 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4456 | struct btrfs_trans_handle *trans; | |
4457 | struct btrfs_block_rsv block_rsv; | |
4458 | u64 root_flags; | |
f60a2364 | 4459 | int ret; |
f60a2364 MT |
4460 | |
4461 | /* | |
4462 | * Don't allow to delete a subvolume with send in progress. This is | |
4463 | * inside the inode lock so the error handling that has to drop the bit | |
4464 | * again is not run concurrently. | |
4465 | */ | |
4466 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4467 | if (dest->send_in_progress) { |
f60a2364 MT |
4468 | spin_unlock(&dest->root_item_lock); |
4469 | btrfs_warn(fs_info, | |
4470 | "attempt to delete subvolume %llu during send", | |
4471 | dest->root_key.objectid); | |
4472 | return -EPERM; | |
4473 | } | |
60021bd7 KH |
4474 | if (atomic_read(&dest->nr_swapfiles)) { |
4475 | spin_unlock(&dest->root_item_lock); | |
4476 | btrfs_warn(fs_info, | |
4477 | "attempt to delete subvolume %llu with active swapfile", | |
4478 | root->root_key.objectid); | |
4479 | return -EPERM; | |
4480 | } | |
a7176f74 LF |
4481 | root_flags = btrfs_root_flags(&dest->root_item); |
4482 | btrfs_set_root_flags(&dest->root_item, | |
4483 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4484 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4485 | |
4486 | down_write(&fs_info->subvol_sem); | |
4487 | ||
ee0d904f NB |
4488 | ret = may_destroy_subvol(dest); |
4489 | if (ret) | |
f60a2364 MT |
4490 | goto out_up_write; |
4491 | ||
4492 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4493 | /* | |
4494 | * One for dir inode, | |
4495 | * two for dir entries, | |
4496 | * two for root ref/backref. | |
4497 | */ | |
ee0d904f NB |
4498 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
4499 | if (ret) | |
f60a2364 MT |
4500 | goto out_up_write; |
4501 | ||
4502 | trans = btrfs_start_transaction(root, 0); | |
4503 | if (IS_ERR(trans)) { | |
ee0d904f | 4504 | ret = PTR_ERR(trans); |
f60a2364 MT |
4505 | goto out_release; |
4506 | } | |
4507 | trans->block_rsv = &block_rsv; | |
4508 | trans->bytes_reserved = block_rsv.size; | |
4509 | ||
3c4f91e2 | 4510 | btrfs_record_snapshot_destroy(trans, dir); |
f60a2364 | 4511 | |
045d3967 | 4512 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 | 4513 | if (ret) { |
f60a2364 MT |
4514 | btrfs_abort_transaction(trans, ret); |
4515 | goto out_end_trans; | |
4516 | } | |
4517 | ||
2731f518 JB |
4518 | ret = btrfs_record_root_in_trans(trans, dest); |
4519 | if (ret) { | |
4520 | btrfs_abort_transaction(trans, ret); | |
4521 | goto out_end_trans; | |
4522 | } | |
f60a2364 MT |
4523 | |
4524 | memset(&dest->root_item.drop_progress, 0, | |
4525 | sizeof(dest->root_item.drop_progress)); | |
c8422684 | 4526 | btrfs_set_root_drop_level(&dest->root_item, 0); |
f60a2364 MT |
4527 | btrfs_set_root_refs(&dest->root_item, 0); |
4528 | ||
4529 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4530 | ret = btrfs_insert_orphan_item(trans, | |
4531 | fs_info->tree_root, | |
4532 | dest->root_key.objectid); | |
4533 | if (ret) { | |
4534 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4535 | goto out_end_trans; |
4536 | } | |
4537 | } | |
4538 | ||
d1957791 | 4539 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4540 | BTRFS_UUID_KEY_SUBVOL, |
4541 | dest->root_key.objectid); | |
4542 | if (ret && ret != -ENOENT) { | |
4543 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4544 | goto out_end_trans; |
4545 | } | |
4546 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4547 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4548 | dest->root_item.received_uuid, |
4549 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4550 | dest->root_key.objectid); | |
4551 | if (ret && ret != -ENOENT) { | |
4552 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4553 | goto out_end_trans; |
4554 | } | |
4555 | } | |
4556 | ||
082b6c97 QW |
4557 | free_anon_bdev(dest->anon_dev); |
4558 | dest->anon_dev = 0; | |
f60a2364 MT |
4559 | out_end_trans: |
4560 | trans->block_rsv = NULL; | |
4561 | trans->bytes_reserved = 0; | |
4562 | ret = btrfs_end_transaction(trans); | |
f60a2364 MT |
4563 | inode->i_flags |= S_DEAD; |
4564 | out_release: | |
e85fde51 | 4565 | btrfs_subvolume_release_metadata(root, &block_rsv); |
f60a2364 MT |
4566 | out_up_write: |
4567 | up_write(&fs_info->subvol_sem); | |
ee0d904f | 4568 | if (ret) { |
f60a2364 MT |
4569 | spin_lock(&dest->root_item_lock); |
4570 | root_flags = btrfs_root_flags(&dest->root_item); | |
4571 | btrfs_set_root_flags(&dest->root_item, | |
4572 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4573 | spin_unlock(&dest->root_item_lock); | |
4574 | } else { | |
4575 | d_invalidate(dentry); | |
20a68004 | 4576 | btrfs_prune_dentries(dest); |
f60a2364 | 4577 | ASSERT(dest->send_in_progress == 0); |
f60a2364 MT |
4578 | } |
4579 | ||
ee0d904f | 4580 | return ret; |
f60a2364 MT |
4581 | } |
4582 | ||
39279cc3 CM |
4583 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4584 | { | |
2b0143b5 | 4585 | struct inode *inode = d_inode(dentry); |
813febdb | 4586 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
1832a6d5 | 4587 | int err = 0; |
39279cc3 | 4588 | struct btrfs_trans_handle *trans; |
44f714da | 4589 | u64 last_unlink_trans; |
ab3c5c18 | 4590 | struct fscrypt_name fname; |
39279cc3 | 4591 | |
b3ae244e | 4592 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4593 | return -ENOTEMPTY; |
813febdb JB |
4594 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) { |
4595 | if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) { | |
4596 | btrfs_err(fs_info, | |
4597 | "extent tree v2 doesn't support snapshot deletion yet"); | |
4598 | return -EOPNOTSUPP; | |
4599 | } | |
3c4f91e2 | 4600 | return btrfs_delete_subvolume(BTRFS_I(dir), dentry); |
813febdb | 4601 | } |
134d4512 | 4602 | |
ab3c5c18 STD |
4603 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
4604 | if (err) | |
4605 | return err; | |
ab3c5c18 STD |
4606 | |
4607 | /* This needs to handle no-key deletions later on */ | |
4608 | ||
e569b1d5 | 4609 | trans = __unlink_start_trans(BTRFS_I(dir)); |
ab3c5c18 STD |
4610 | if (IS_ERR(trans)) { |
4611 | err = PTR_ERR(trans); | |
4612 | goto out_notrans; | |
4613 | } | |
5df6a9f6 | 4614 | |
4a0cc7ca | 4615 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
5b7544cb | 4616 | err = btrfs_unlink_subvol(trans, BTRFS_I(dir), dentry); |
4df27c4d YZ |
4617 | goto out; |
4618 | } | |
4619 | ||
73f2e545 | 4620 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4621 | if (err) |
4df27c4d | 4622 | goto out; |
7b128766 | 4623 | |
44f714da FM |
4624 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4625 | ||
39279cc3 | 4626 | /* now the directory is empty */ |
e43eec81 | 4627 | err = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4628 | &fname.disk_name); |
44f714da | 4629 | if (!err) { |
6ef06d27 | 4630 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4631 | /* |
4632 | * Propagate the last_unlink_trans value of the deleted dir to | |
4633 | * its parent directory. This is to prevent an unrecoverable | |
4634 | * log tree in the case we do something like this: | |
4635 | * 1) create dir foo | |
4636 | * 2) create snapshot under dir foo | |
4637 | * 3) delete the snapshot | |
4638 | * 4) rmdir foo | |
4639 | * 5) mkdir foo | |
4640 | * 6) fsync foo or some file inside foo | |
4641 | */ | |
4642 | if (last_unlink_trans >= trans->transid) | |
4643 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4644 | } | |
4df27c4d | 4645 | out: |
3a45bb20 | 4646 | btrfs_end_transaction(trans); |
ab3c5c18 | 4647 | out_notrans: |
813febdb | 4648 | btrfs_btree_balance_dirty(fs_info); |
ab3c5c18 | 4649 | fscrypt_free_filename(&fname); |
3954401f | 4650 | |
39279cc3 CM |
4651 | return err; |
4652 | } | |
4653 | ||
39279cc3 | 4654 | /* |
9580503b DS |
4655 | * Read, zero a chunk and write a block. |
4656 | * | |
2aaa6655 JB |
4657 | * @inode - inode that we're zeroing |
4658 | * @from - the offset to start zeroing | |
4659 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4660 | * offset | |
4661 | * @front - zero up to the offset instead of from the offset on | |
4662 | * | |
9703fefe | 4663 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4664 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4665 | */ |
217f42eb NB |
4666 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
4667 | int front) | |
39279cc3 | 4668 | { |
217f42eb NB |
4669 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4670 | struct address_space *mapping = inode->vfs_inode.i_mapping; | |
4671 | struct extent_io_tree *io_tree = &inode->io_tree; | |
e6dcd2dc | 4672 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 4673 | struct extent_state *cached_state = NULL; |
364ecf36 | 4674 | struct extent_changeset *data_reserved = NULL; |
6d4572a9 | 4675 | bool only_release_metadata = false; |
0b246afa | 4676 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4677 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4678 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4679 | struct page *page; |
3b16a4e3 | 4680 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4681 | size_t write_bytes = blocksize; |
39279cc3 | 4682 | int ret = 0; |
9703fefe CR |
4683 | u64 block_start; |
4684 | u64 block_end; | |
39279cc3 | 4685 | |
b03ebd99 NB |
4686 | if (IS_ALIGNED(offset, blocksize) && |
4687 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4688 | goto out; |
9703fefe | 4689 | |
8b62f87b JB |
4690 | block_start = round_down(from, blocksize); |
4691 | block_end = block_start + blocksize - 1; | |
4692 | ||
217f42eb | 4693 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
1daedb1d | 4694 | blocksize, false); |
6d4572a9 | 4695 | if (ret < 0) { |
80f9d241 | 4696 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) { |
6d4572a9 QW |
4697 | /* For nocow case, no need to reserve data space */ |
4698 | only_release_metadata = true; | |
4699 | } else { | |
4700 | goto out; | |
4701 | } | |
4702 | } | |
d4135134 | 4703 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false); |
6d4572a9 QW |
4704 | if (ret < 0) { |
4705 | if (!only_release_metadata) | |
217f42eb NB |
4706 | btrfs_free_reserved_data_space(inode, data_reserved, |
4707 | block_start, blocksize); | |
6d4572a9 QW |
4708 | goto out; |
4709 | } | |
211c17f5 | 4710 | again: |
3b16a4e3 | 4711 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4712 | if (!page) { |
217f42eb NB |
4713 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
4714 | blocksize, true); | |
4715 | btrfs_delalloc_release_extents(inode, blocksize); | |
ac6a2b36 | 4716 | ret = -ENOMEM; |
39279cc3 | 4717 | goto out; |
5d5e103a | 4718 | } |
e6dcd2dc | 4719 | |
39279cc3 | 4720 | if (!PageUptodate(page)) { |
fb12489b | 4721 | ret = btrfs_read_folio(NULL, page_folio(page)); |
39279cc3 | 4722 | lock_page(page); |
211c17f5 CM |
4723 | if (page->mapping != mapping) { |
4724 | unlock_page(page); | |
09cbfeaf | 4725 | put_page(page); |
211c17f5 CM |
4726 | goto again; |
4727 | } | |
39279cc3 CM |
4728 | if (!PageUptodate(page)) { |
4729 | ret = -EIO; | |
89642229 | 4730 | goto out_unlock; |
39279cc3 CM |
4731 | } |
4732 | } | |
17b17fcd JB |
4733 | |
4734 | /* | |
4735 | * We unlock the page after the io is completed and then re-lock it | |
4736 | * above. release_folio() could have come in between that and cleared | |
cfbf07e2 | 4737 | * folio private, but left the page in the mapping. Set the page mapped |
17b17fcd JB |
4738 | * here to make sure it's properly set for the subpage stuff. |
4739 | */ | |
4740 | ret = set_page_extent_mapped(page); | |
4741 | if (ret < 0) | |
4742 | goto out_unlock; | |
4743 | ||
211c17f5 | 4744 | wait_on_page_writeback(page); |
e6dcd2dc | 4745 | |
570eb97b | 4746 | lock_extent(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4747 | |
217f42eb | 4748 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4749 | if (ordered) { |
570eb97b | 4750 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4751 | unlock_page(page); |
09cbfeaf | 4752 | put_page(page); |
36d45567 | 4753 | btrfs_start_ordered_extent(ordered); |
e6dcd2dc CM |
4754 | btrfs_put_ordered_extent(ordered); |
4755 | goto again; | |
4756 | } | |
4757 | ||
217f42eb | 4758 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
e182163d | 4759 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
bd015294 | 4760 | &cached_state); |
5d5e103a | 4761 | |
217f42eb | 4762 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 4763 | &cached_state); |
9ed74f2d | 4764 | if (ret) { |
570eb97b | 4765 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
9ed74f2d JB |
4766 | goto out_unlock; |
4767 | } | |
4768 | ||
9703fefe | 4769 | if (offset != blocksize) { |
2aaa6655 | 4770 | if (!len) |
9703fefe | 4771 | len = blocksize - offset; |
2aaa6655 | 4772 | if (front) |
d048b9c2 IW |
4773 | memzero_page(page, (block_start - page_offset(page)), |
4774 | offset); | |
2aaa6655 | 4775 | else |
d048b9c2 IW |
4776 | memzero_page(page, (block_start - page_offset(page)) + offset, |
4777 | len); | |
e6dcd2dc | 4778 | } |
e4f94347 QW |
4779 | btrfs_page_clear_checked(fs_info, page, block_start, |
4780 | block_end + 1 - block_start); | |
6c9ac8be | 4781 | btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start); |
570eb97b | 4782 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4783 | |
6d4572a9 | 4784 | if (only_release_metadata) |
217f42eb | 4785 | set_extent_bit(&inode->io_tree, block_start, block_end, |
1d126800 | 4786 | EXTENT_NORESERVE, NULL); |
6d4572a9 | 4787 | |
89642229 | 4788 | out_unlock: |
6d4572a9 QW |
4789 | if (ret) { |
4790 | if (only_release_metadata) | |
217f42eb | 4791 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
6d4572a9 | 4792 | else |
217f42eb | 4793 | btrfs_delalloc_release_space(inode, data_reserved, |
6d4572a9 QW |
4794 | block_start, blocksize, true); |
4795 | } | |
217f42eb | 4796 | btrfs_delalloc_release_extents(inode, blocksize); |
39279cc3 | 4797 | unlock_page(page); |
09cbfeaf | 4798 | put_page(page); |
39279cc3 | 4799 | out: |
6d4572a9 | 4800 | if (only_release_metadata) |
217f42eb | 4801 | btrfs_check_nocow_unlock(inode); |
364ecf36 | 4802 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4803 | return ret; |
4804 | } | |
4805 | ||
0a325e62 | 4806 | static int maybe_insert_hole(struct btrfs_inode *inode, u64 offset, u64 len) |
16e7549f | 4807 | { |
0a325e62 | 4808 | struct btrfs_root *root = inode->root; |
a4ba6cc0 | 4809 | struct btrfs_fs_info *fs_info = root->fs_info; |
16e7549f | 4810 | struct btrfs_trans_handle *trans; |
5893dfb9 | 4811 | struct btrfs_drop_extents_args drop_args = { 0 }; |
16e7549f JB |
4812 | int ret; |
4813 | ||
4814 | /* | |
cceaa89f FM |
4815 | * If NO_HOLES is enabled, we don't need to do anything. |
4816 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() | |
4817 | * or btrfs_update_inode() will be called, which guarantee that the next | |
4818 | * fsync will know this inode was changed and needs to be logged. | |
16e7549f | 4819 | */ |
cceaa89f | 4820 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f | 4821 | return 0; |
16e7549f JB |
4822 | |
4823 | /* | |
4824 | * 1 - for the one we're dropping | |
4825 | * 1 - for the one we're adding | |
4826 | * 1 - for updating the inode. | |
4827 | */ | |
4828 | trans = btrfs_start_transaction(root, 3); | |
4829 | if (IS_ERR(trans)) | |
4830 | return PTR_ERR(trans); | |
4831 | ||
5893dfb9 FM |
4832 | drop_args.start = offset; |
4833 | drop_args.end = offset + len; | |
4834 | drop_args.drop_cache = true; | |
4835 | ||
a4ba6cc0 | 4836 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
16e7549f | 4837 | if (ret) { |
66642832 | 4838 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4839 | btrfs_end_transaction(trans); |
16e7549f JB |
4840 | return ret; |
4841 | } | |
4842 | ||
d1f68ba0 | 4843 | ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len); |
2766ff61 | 4844 | if (ret) { |
66642832 | 4845 | btrfs_abort_transaction(trans, ret); |
2766ff61 | 4846 | } else { |
a4ba6cc0 | 4847 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
8b9d0322 | 4848 | btrfs_update_inode(trans, inode); |
2766ff61 | 4849 | } |
3a45bb20 | 4850 | btrfs_end_transaction(trans); |
16e7549f JB |
4851 | return ret; |
4852 | } | |
4853 | ||
695a0d0d JB |
4854 | /* |
4855 | * This function puts in dummy file extents for the area we're creating a hole | |
4856 | * for. So if we are truncating this file to a larger size we need to insert | |
4857 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4858 | * the range between oldsize and size | |
4859 | */ | |
b06359a3 | 4860 | int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4861 | { |
b06359a3 NB |
4862 | struct btrfs_root *root = inode->root; |
4863 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4864 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a22285a6 | 4865 | struct extent_map *em = NULL; |
2ac55d41 | 4866 | struct extent_state *cached_state = NULL; |
0b246afa JM |
4867 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4868 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4869 | u64 last_byte; |
4870 | u64 cur_offset; | |
4871 | u64 hole_size; | |
9ed74f2d | 4872 | int err = 0; |
39279cc3 | 4873 | |
a71754fc | 4874 | /* |
9703fefe CR |
4875 | * If our size started in the middle of a block we need to zero out the |
4876 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4877 | * expose stale data. |
4878 | */ | |
b06359a3 | 4879 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4880 | if (err) |
4881 | return err; | |
4882 | ||
9036c102 YZ |
4883 | if (size <= hole_start) |
4884 | return 0; | |
4885 | ||
b06359a3 NB |
4886 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
4887 | &cached_state); | |
9036c102 YZ |
4888 | cur_offset = hole_start; |
4889 | while (1) { | |
b06359a3 | 4890 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
39b07b5d | 4891 | block_end - cur_offset); |
79787eaa JM |
4892 | if (IS_ERR(em)) { |
4893 | err = PTR_ERR(em); | |
f2767956 | 4894 | em = NULL; |
79787eaa JM |
4895 | break; |
4896 | } | |
9036c102 | 4897 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4898 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4899 | hole_size = last_byte - cur_offset; |
4900 | ||
f86f7a75 | 4901 | if (!(em->flags & EXTENT_FLAG_PREALLOC)) { |
5dc562c5 | 4902 | struct extent_map *hole_em; |
9ed74f2d | 4903 | |
0a325e62 | 4904 | err = maybe_insert_hole(inode, cur_offset, hole_size); |
16e7549f | 4905 | if (err) |
3893e33b | 4906 | break; |
9ddc959e | 4907 | |
b06359a3 | 4908 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4909 | cur_offset, hole_size); |
4910 | if (err) | |
4911 | break; | |
4912 | ||
5dc562c5 JB |
4913 | hole_em = alloc_extent_map(); |
4914 | if (!hole_em) { | |
a1ba4c08 FM |
4915 | btrfs_drop_extent_map_range(inode, cur_offset, |
4916 | cur_offset + hole_size - 1, | |
4917 | false); | |
23e3337f | 4918 | btrfs_set_inode_full_sync(inode); |
5dc562c5 JB |
4919 | goto next; |
4920 | } | |
4921 | hole_em->start = cur_offset; | |
4922 | hole_em->len = hole_size; | |
4923 | hole_em->orig_start = cur_offset; | |
8082510e | 4924 | |
5dc562c5 JB |
4925 | hole_em->block_start = EXTENT_MAP_HOLE; |
4926 | hole_em->block_len = 0; | |
b4939680 | 4927 | hole_em->orig_block_len = 0; |
cc95bef6 | 4928 | hole_em->ram_bytes = hole_size; |
4a4f8fe2 | 4929 | hole_em->generation = btrfs_get_fs_generation(fs_info); |
8082510e | 4930 | |
a1ba4c08 | 4931 | err = btrfs_replace_extent_map_range(inode, hole_em, true); |
5dc562c5 | 4932 | free_extent_map(hole_em); |
9ddc959e | 4933 | } else { |
b06359a3 | 4934 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4935 | cur_offset, hole_size); |
4936 | if (err) | |
4937 | break; | |
9036c102 | 4938 | } |
16e7549f | 4939 | next: |
9036c102 | 4940 | free_extent_map(em); |
a22285a6 | 4941 | em = NULL; |
9036c102 | 4942 | cur_offset = last_byte; |
8082510e | 4943 | if (cur_offset >= block_end) |
9036c102 YZ |
4944 | break; |
4945 | } | |
a22285a6 | 4946 | free_extent_map(em); |
570eb97b | 4947 | unlock_extent(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4948 | return err; |
4949 | } | |
39279cc3 | 4950 | |
3972f260 | 4951 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4952 | { |
f4a2f4c5 MX |
4953 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4954 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4955 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4956 | loff_t newsize = attr->ia_size; |
4957 | int mask = attr->ia_valid; | |
8082510e YZ |
4958 | int ret; |
4959 | ||
3972f260 ES |
4960 | /* |
4961 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4962 | * special case where we need to update the times despite not having | |
4963 | * these flags set. For all other operations the VFS set these flags | |
4964 | * explicitly if it wants a timestamp update. | |
4965 | */ | |
dff6efc3 CH |
4966 | if (newsize != oldsize) { |
4967 | inode_inc_iversion(inode); | |
c1867eb3 | 4968 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) { |
b1c38a13 JL |
4969 | inode_set_mtime_to_ts(inode, |
4970 | inode_set_ctime_current(inode)); | |
c1867eb3 | 4971 | } |
dff6efc3 | 4972 | } |
3972f260 | 4973 | |
a41ad394 | 4974 | if (newsize > oldsize) { |
9ea24bbe | 4975 | /* |
ea14b57f | 4976 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
4977 | * This is to ensure the snapshot captures a fully consistent |
4978 | * state of this file - if the snapshot captures this expanding | |
4979 | * truncation, it must capture all writes that happened before | |
4980 | * this truncation. | |
4981 | */ | |
dcc3eb96 | 4982 | btrfs_drew_write_lock(&root->snapshot_lock); |
b06359a3 | 4983 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
9ea24bbe | 4984 | if (ret) { |
dcc3eb96 | 4985 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 4986 | return ret; |
9ea24bbe | 4987 | } |
8082510e | 4988 | |
f4a2f4c5 | 4989 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 4990 | if (IS_ERR(trans)) { |
dcc3eb96 | 4991 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 4992 | return PTR_ERR(trans); |
9ea24bbe | 4993 | } |
f4a2f4c5 MX |
4994 | |
4995 | i_size_write(inode, newsize); | |
76aea537 | 4996 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
27772b68 | 4997 | pagecache_isize_extended(inode, oldsize, newsize); |
8b9d0322 | 4998 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
dcc3eb96 | 4999 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 5000 | btrfs_end_transaction(trans); |
a41ad394 | 5001 | } else { |
24c0a722 NA |
5002 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5003 | ||
5004 | if (btrfs_is_zoned(fs_info)) { | |
5005 | ret = btrfs_wait_ordered_range(inode, | |
5006 | ALIGN(newsize, fs_info->sectorsize), | |
5007 | (u64)-1); | |
5008 | if (ret) | |
5009 | return ret; | |
5010 | } | |
8082510e | 5011 | |
a41ad394 JB |
5012 | /* |
5013 | * We're truncating a file that used to have good data down to | |
1fd4033d NB |
5014 | * zero. Make sure any new writes to the file get on disk |
5015 | * on close. | |
a41ad394 JB |
5016 | */ |
5017 | if (newsize == 0) | |
1fd4033d | 5018 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
72ac3c0d | 5019 | &BTRFS_I(inode)->runtime_flags); |
8082510e | 5020 | |
a41ad394 | 5021 | truncate_setsize(inode, newsize); |
2e60a51e | 5022 | |
2e60a51e | 5023 | inode_dio_wait(inode); |
2e60a51e | 5024 | |
d9dcae67 | 5025 | ret = btrfs_truncate(BTRFS_I(inode), newsize == oldsize); |
7f4f6e0a JB |
5026 | if (ret && inode->i_nlink) { |
5027 | int err; | |
5028 | ||
5029 | /* | |
f7e9e8fc OS |
5030 | * Truncate failed, so fix up the in-memory size. We |
5031 | * adjusted disk_i_size down as we removed extents, so | |
5032 | * wait for disk_i_size to be stable and then update the | |
5033 | * in-memory size to match. | |
7f4f6e0a | 5034 | */ |
f7e9e8fc | 5035 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5036 | if (err) |
f7e9e8fc OS |
5037 | return err; |
5038 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5039 | } |
8082510e YZ |
5040 | } |
5041 | ||
a41ad394 | 5042 | return ret; |
8082510e YZ |
5043 | } |
5044 | ||
c1632a0f | 5045 | static int btrfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 5046 | struct iattr *attr) |
9036c102 | 5047 | { |
2b0143b5 | 5048 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5049 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5050 | int err; |
39279cc3 | 5051 | |
b83cc969 LZ |
5052 | if (btrfs_root_readonly(root)) |
5053 | return -EROFS; | |
5054 | ||
c1632a0f | 5055 | err = setattr_prepare(idmap, dentry, attr); |
9036c102 YZ |
5056 | if (err) |
5057 | return err; | |
2bf5a725 | 5058 | |
5a3f23d5 | 5059 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5060 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5061 | if (err) |
5062 | return err; | |
39279cc3 | 5063 | } |
9036c102 | 5064 | |
1025774c | 5065 | if (attr->ia_valid) { |
c1632a0f | 5066 | setattr_copy(idmap, inode, attr); |
0c4d2d95 | 5067 | inode_inc_iversion(inode); |
7152b425 | 5068 | err = btrfs_dirty_inode(BTRFS_I(inode)); |
1025774c | 5069 | |
22c44fe6 | 5070 | if (!err && attr->ia_valid & ATTR_MODE) |
13e83a49 | 5071 | err = posix_acl_chmod(idmap, dentry, inode->i_mode); |
1025774c | 5072 | } |
33268eaf | 5073 | |
39279cc3 CM |
5074 | return err; |
5075 | } | |
61295eb8 | 5076 | |
131e404a | 5077 | /* |
895586eb MWO |
5078 | * While truncating the inode pages during eviction, we get the VFS |
5079 | * calling btrfs_invalidate_folio() against each folio of the inode. This | |
5080 | * is slow because the calls to btrfs_invalidate_folio() result in a | |
570eb97b | 5081 | * huge amount of calls to lock_extent() and clear_extent_bit(), |
895586eb MWO |
5082 | * which keep merging and splitting extent_state structures over and over, |
5083 | * wasting lots of time. | |
131e404a | 5084 | * |
895586eb MWO |
5085 | * Therefore if the inode is being evicted, let btrfs_invalidate_folio() |
5086 | * skip all those expensive operations on a per folio basis and do only | |
5087 | * the ordered io finishing, while we release here the extent_map and | |
5088 | * extent_state structures, without the excessive merging and splitting. | |
131e404a FDBM |
5089 | */ |
5090 | static void evict_inode_truncate_pages(struct inode *inode) | |
5091 | { | |
5092 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
131e404a FDBM |
5093 | struct rb_node *node; |
5094 | ||
5095 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5096 | truncate_inode_pages_final(&inode->i_data); |
131e404a | 5097 | |
9c9d1b4f | 5098 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
131e404a | 5099 | |
6ca07097 FM |
5100 | /* |
5101 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
5102 | * We can have ongoing bios started by readahead that have |
5103 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
5104 | * still in progress (unlocked the pages in the bio but did not yet |
5105 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5106 | * ranges can still be locked and eviction started because before |
5107 | * submitting those bios, which are executed by a separate task (work | |
5108 | * queue kthread), inode references (inode->i_count) were not taken | |
5109 | * (which would be dropped in the end io callback of each bio). | |
5110 | * Therefore here we effectively end up waiting for those bios and | |
5111 | * anyone else holding locked ranges without having bumped the inode's | |
5112 | * reference count - if we don't do it, when they access the inode's | |
5113 | * io_tree to unlock a range it may be too late, leading to an | |
5114 | * use-after-free issue. | |
5115 | */ | |
131e404a FDBM |
5116 | spin_lock(&io_tree->lock); |
5117 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5118 | struct extent_state *state; | |
5119 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5120 | u64 start; |
5121 | u64 end; | |
421f0922 | 5122 | unsigned state_flags; |
131e404a FDBM |
5123 | |
5124 | node = rb_first(&io_tree->state); | |
5125 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5126 | start = state->start; |
5127 | end = state->end; | |
421f0922 | 5128 | state_flags = state->state; |
131e404a FDBM |
5129 | spin_unlock(&io_tree->lock); |
5130 | ||
570eb97b | 5131 | lock_extent(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5132 | |
5133 | /* | |
5134 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5135 | * and its reserved space won't be freed by delayed_ref. | |
5136 | * So we need to free its reserved space here. | |
895586eb | 5137 | * (Refer to comment in btrfs_invalidate_folio, case 2) |
b9d0b389 QW |
5138 | * |
5139 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5140 | */ | |
421f0922 | 5141 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f | 5142 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
9e65bfca | 5143 | end - start + 1, NULL); |
b9d0b389 | 5144 | |
6ca07097 | 5145 | clear_extent_bit(io_tree, start, end, |
bd015294 | 5146 | EXTENT_CLEAR_ALL_BITS | EXTENT_DO_ACCOUNTING, |
e182163d | 5147 | &cached_state); |
131e404a | 5148 | |
7064dd5c | 5149 | cond_resched(); |
131e404a FDBM |
5150 | spin_lock(&io_tree->lock); |
5151 | } | |
5152 | spin_unlock(&io_tree->lock); | |
5153 | } | |
5154 | ||
4b9d7b59 | 5155 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5156 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5157 | { |
5158 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d3984c90 | 5159 | struct btrfs_trans_handle *trans; |
b13d57db | 5160 | u64 delayed_refs_extra = btrfs_calc_delayed_ref_bytes(fs_info, 1); |
d3984c90 | 5161 | int ret; |
4b9d7b59 | 5162 | |
d3984c90 JB |
5163 | /* |
5164 | * Eviction should be taking place at some place safe because of our | |
5165 | * delayed iputs. However the normal flushing code will run delayed | |
5166 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5167 | * | |
5168 | * We reserve the delayed_refs_extra here again because we can't use | |
5169 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5170 | * above. We reserve our extra bit here because we generate a ton of | |
5171 | * delayed refs activity by truncating. | |
5172 | * | |
ee6adbfd JB |
5173 | * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can, |
5174 | * if we fail to make this reservation we can re-try without the | |
5175 | * delayed_refs_extra so we can make some forward progress. | |
d3984c90 | 5176 | */ |
9270501c | 5177 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, |
d3984c90 JB |
5178 | BTRFS_RESERVE_FLUSH_EVICT); |
5179 | if (ret) { | |
9270501c | 5180 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, |
ee6adbfd JB |
5181 | BTRFS_RESERVE_FLUSH_EVICT); |
5182 | if (ret) { | |
d3984c90 JB |
5183 | btrfs_warn(fs_info, |
5184 | "could not allocate space for delete; will truncate on mount"); | |
5185 | return ERR_PTR(-ENOSPC); | |
5186 | } | |
5187 | delayed_refs_extra = 0; | |
5188 | } | |
4b9d7b59 | 5189 | |
d3984c90 JB |
5190 | trans = btrfs_join_transaction(root); |
5191 | if (IS_ERR(trans)) | |
5192 | return trans; | |
5193 | ||
5194 | if (delayed_refs_extra) { | |
5195 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5196 | trans->bytes_reserved = delayed_refs_extra; | |
5197 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
4e0527de | 5198 | delayed_refs_extra, true); |
4b9d7b59 | 5199 | } |
d3984c90 | 5200 | return trans; |
4b9d7b59 OS |
5201 | } |
5202 | ||
bd555975 | 5203 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5204 | { |
0b246afa | 5205 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5206 | struct btrfs_trans_handle *trans; |
5207 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
b7b1167c | 5208 | struct btrfs_block_rsv *rsv = NULL; |
39279cc3 CM |
5209 | int ret; |
5210 | ||
1abe9b8a | 5211 | trace_btrfs_inode_evict(inode); |
5212 | ||
3d48d981 | 5213 | if (!root) { |
14605409 | 5214 | fsverity_cleanup_inode(inode); |
e8f1bc14 | 5215 | clear_inode(inode); |
3d48d981 NB |
5216 | return; |
5217 | } | |
5218 | ||
131e404a FDBM |
5219 | evict_inode_truncate_pages(inode); |
5220 | ||
69e9c6c6 SB |
5221 | if (inode->i_nlink && |
5222 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5223 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5224 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
b7b1167c | 5225 | goto out; |
bd555975 | 5226 | |
27919067 | 5227 | if (is_bad_inode(inode)) |
b7b1167c | 5228 | goto out; |
5f39d397 | 5229 | |
7b40b695 | 5230 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
b7b1167c | 5231 | goto out; |
c71bf099 | 5232 | |
76dda93c | 5233 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5234 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5235 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
b7b1167c | 5236 | goto out; |
76dda93c YZ |
5237 | } |
5238 | ||
2adc75d6 JB |
5239 | /* |
5240 | * This makes sure the inode item in tree is uptodate and the space for | |
5241 | * the inode update is released. | |
5242 | */ | |
aa79021f | 5243 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5244 | if (ret) |
b7b1167c | 5245 | goto out; |
0e8c36a9 | 5246 | |
2adc75d6 JB |
5247 | /* |
5248 | * This drops any pending insert or delete operations we have for this | |
5249 | * inode. We could have a delayed dir index deletion queued up, but | |
5250 | * we're removing the inode completely so that'll be taken care of in | |
5251 | * the truncate. | |
5252 | */ | |
5253 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); | |
5254 | ||
2ff7e61e | 5255 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5256 | if (!rsv) |
b7b1167c | 5257 | goto out; |
2bd36e7b | 5258 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
710d5921 | 5259 | rsv->failfast = true; |
4289a667 | 5260 | |
6ef06d27 | 5261 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5262 | |
8082510e | 5263 | while (1) { |
d9ac19c3 | 5264 | struct btrfs_truncate_control control = { |
71d18b53 | 5265 | .inode = BTRFS_I(inode), |
487e81d2 | 5266 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 JB |
5267 | .new_size = 0, |
5268 | .min_type = 0, | |
5269 | }; | |
5270 | ||
ad80cf50 | 5271 | trans = evict_refill_and_join(root, rsv); |
27919067 | 5272 | if (IS_ERR(trans)) |
b7b1167c | 5273 | goto out; |
7b128766 | 5274 | |
4289a667 JB |
5275 | trans->block_rsv = rsv; |
5276 | ||
71d18b53 | 5277 | ret = btrfs_truncate_inode_items(trans, root, &control); |
27919067 OS |
5278 | trans->block_rsv = &fs_info->trans_block_rsv; |
5279 | btrfs_end_transaction(trans); | |
afa4b0af FM |
5280 | /* |
5281 | * We have not added new delayed items for our inode after we | |
5282 | * have flushed its delayed items, so no need to throttle on | |
5283 | * delayed items. However we have modified extent buffers. | |
5284 | */ | |
5285 | btrfs_btree_balance_dirty_nodelay(fs_info); | |
27919067 | 5286 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
b7b1167c | 5287 | goto out; |
27919067 | 5288 | else if (!ret) |
8082510e | 5289 | break; |
8082510e | 5290 | } |
5f39d397 | 5291 | |
4ef31a45 | 5292 | /* |
27919067 OS |
5293 | * Errors here aren't a big deal, it just means we leave orphan items in |
5294 | * the tree. They will be cleaned up on the next mount. If the inode | |
5295 | * number gets reused, cleanup deletes the orphan item without doing | |
5296 | * anything, and unlink reuses the existing orphan item. | |
5297 | * | |
5298 | * If it turns out that we are dropping too many of these, we might want | |
5299 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5300 | */ |
ad80cf50 | 5301 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5302 | if (!IS_ERR(trans)) { |
5303 | trans->block_rsv = rsv; | |
5304 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5305 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5306 | btrfs_end_transaction(trans); | |
5307 | } | |
54aa1f4d | 5308 | |
b7b1167c | 5309 | out: |
27919067 | 5310 | btrfs_free_block_rsv(fs_info, rsv); |
27919067 OS |
5311 | /* |
5312 | * If we didn't successfully delete, the orphan item will still be in | |
5313 | * the tree and we'll retry on the next mount. Again, we might also want | |
5314 | * to retry these periodically in the future. | |
5315 | */ | |
f48d1cf5 | 5316 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
14605409 | 5317 | fsverity_cleanup_inode(inode); |
dbd5768f | 5318 | clear_inode(inode); |
39279cc3 CM |
5319 | } |
5320 | ||
5321 | /* | |
6bf9e4bd QW |
5322 | * Return the key found in the dir entry in the location pointer, fill @type |
5323 | * with BTRFS_FT_*, and return 0. | |
5324 | * | |
005d6712 SY |
5325 | * If no dir entries were found, returns -ENOENT. |
5326 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 | 5327 | */ |
d1de429b | 5328 | static int btrfs_inode_by_name(struct btrfs_inode *dir, struct dentry *dentry, |
6bf9e4bd | 5329 | struct btrfs_key *location, u8 *type) |
39279cc3 | 5330 | { |
39279cc3 CM |
5331 | struct btrfs_dir_item *di; |
5332 | struct btrfs_path *path; | |
d1de429b | 5333 | struct btrfs_root *root = dir->root; |
0d9f7f3e | 5334 | int ret = 0; |
ab3c5c18 | 5335 | struct fscrypt_name fname; |
39279cc3 CM |
5336 | |
5337 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5338 | if (!path) |
5339 | return -ENOMEM; | |
3954401f | 5340 | |
d1de429b | 5341 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); |
10a8857a | 5342 | if (ret < 0) |
ab3c5c18 | 5343 | goto out; |
10a8857a STD |
5344 | /* |
5345 | * fscrypt_setup_filename() should never return a positive value, but | |
5346 | * gcc on sparc/parisc thinks it can, so assert that doesn't happen. | |
5347 | */ | |
5348 | ASSERT(ret == 0); | |
ab3c5c18 | 5349 | |
ab3c5c18 STD |
5350 | /* This needs to handle no-key deletions later on */ |
5351 | ||
d1de429b | 5352 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), |
6db75318 | 5353 | &fname.disk_name, 0); |
3cf5068f LB |
5354 | if (IS_ERR_OR_NULL(di)) { |
5355 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5356 | goto out; |
5357 | } | |
d397712b | 5358 | |
5f39d397 | 5359 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5360 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5361 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5362 | ret = -EUCLEAN; |
56a0e706 LB |
5363 | btrfs_warn(root->fs_info, |
5364 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
d1de429b | 5365 | __func__, fname.disk_name.name, btrfs_ino(dir), |
56a0e706 | 5366 | location->objectid, location->type, location->offset); |
56a0e706 | 5367 | } |
6bf9e4bd | 5368 | if (!ret) |
94a48aef | 5369 | *type = btrfs_dir_ftype(path->nodes[0], di); |
39279cc3 | 5370 | out: |
ab3c5c18 | 5371 | fscrypt_free_filename(&fname); |
39279cc3 CM |
5372 | btrfs_free_path(path); |
5373 | return ret; | |
5374 | } | |
5375 | ||
5376 | /* | |
5377 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5378 | * needs to be changed to reflect the root directory of the tree root. This | |
5379 | * is kind of like crossing a mount point. | |
5380 | */ | |
2ff7e61e | 5381 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
3c1b1c4c | 5382 | struct btrfs_inode *dir, |
4df27c4d YZ |
5383 | struct dentry *dentry, |
5384 | struct btrfs_key *location, | |
5385 | struct btrfs_root **sub_root) | |
39279cc3 | 5386 | { |
4df27c4d YZ |
5387 | struct btrfs_path *path; |
5388 | struct btrfs_root *new_root; | |
5389 | struct btrfs_root_ref *ref; | |
5390 | struct extent_buffer *leaf; | |
1d4c08e0 | 5391 | struct btrfs_key key; |
4df27c4d YZ |
5392 | int ret; |
5393 | int err = 0; | |
ab3c5c18 | 5394 | struct fscrypt_name fname; |
ab3c5c18 | 5395 | |
3c1b1c4c | 5396 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 0, &fname); |
ab3c5c18 STD |
5397 | if (ret) |
5398 | return ret; | |
39279cc3 | 5399 | |
4df27c4d YZ |
5400 | path = btrfs_alloc_path(); |
5401 | if (!path) { | |
5402 | err = -ENOMEM; | |
5403 | goto out; | |
5404 | } | |
39279cc3 | 5405 | |
4df27c4d | 5406 | err = -ENOENT; |
3c1b1c4c | 5407 | key.objectid = dir->root->root_key.objectid; |
1d4c08e0 DS |
5408 | key.type = BTRFS_ROOT_REF_KEY; |
5409 | key.offset = location->objectid; | |
5410 | ||
0b246afa | 5411 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5412 | if (ret) { |
5413 | if (ret < 0) | |
5414 | err = ret; | |
5415 | goto out; | |
5416 | } | |
39279cc3 | 5417 | |
4df27c4d YZ |
5418 | leaf = path->nodes[0]; |
5419 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
3c1b1c4c | 5420 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
6db75318 | 5421 | btrfs_root_ref_name_len(leaf, ref) != fname.disk_name.len) |
4df27c4d | 5422 | goto out; |
39279cc3 | 5423 | |
6db75318 STD |
5424 | ret = memcmp_extent_buffer(leaf, fname.disk_name.name, |
5425 | (unsigned long)(ref + 1), fname.disk_name.len); | |
4df27c4d YZ |
5426 | if (ret) |
5427 | goto out; | |
5428 | ||
b3b4aa74 | 5429 | btrfs_release_path(path); |
4df27c4d | 5430 | |
56e9357a | 5431 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5432 | if (IS_ERR(new_root)) { |
5433 | err = PTR_ERR(new_root); | |
5434 | goto out; | |
5435 | } | |
5436 | ||
4df27c4d YZ |
5437 | *sub_root = new_root; |
5438 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5439 | location->type = BTRFS_INODE_ITEM_KEY; | |
5440 | location->offset = 0; | |
5441 | err = 0; | |
5442 | out: | |
5443 | btrfs_free_path(path); | |
ab3c5c18 | 5444 | fscrypt_free_filename(&fname); |
4df27c4d | 5445 | return err; |
39279cc3 CM |
5446 | } |
5447 | ||
4c45a4f4 | 5448 | static void inode_tree_add(struct btrfs_inode *inode) |
5d4f98a2 | 5449 | { |
4c45a4f4 | 5450 | struct btrfs_root *root = inode->root; |
5d4f98a2 | 5451 | struct btrfs_inode *entry; |
03e860bd NP |
5452 | struct rb_node **p; |
5453 | struct rb_node *parent; | |
4c45a4f4 DS |
5454 | struct rb_node *new = &inode->rb_node; |
5455 | u64 ino = btrfs_ino(inode); | |
5d4f98a2 | 5456 | |
4c45a4f4 | 5457 | if (inode_unhashed(&inode->vfs_inode)) |
76dda93c | 5458 | return; |
e1409cef | 5459 | parent = NULL; |
5d4f98a2 | 5460 | spin_lock(&root->inode_lock); |
e1409cef | 5461 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5462 | while (*p) { |
5463 | parent = *p; | |
5464 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5465 | ||
37508515 | 5466 | if (ino < btrfs_ino(entry)) |
03e860bd | 5467 | p = &parent->rb_left; |
37508515 | 5468 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5469 | p = &parent->rb_right; |
5d4f98a2 YZ |
5470 | else { |
5471 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5472 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5473 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5474 | RB_CLEAR_NODE(parent); |
5475 | spin_unlock(&root->inode_lock); | |
cef21937 | 5476 | return; |
5d4f98a2 YZ |
5477 | } |
5478 | } | |
cef21937 FDBM |
5479 | rb_link_node(new, parent, p); |
5480 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5481 | spin_unlock(&root->inode_lock); |
5482 | } | |
5483 | ||
b79b7249 | 5484 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5485 | { |
b79b7249 | 5486 | struct btrfs_root *root = inode->root; |
76dda93c | 5487 | int empty = 0; |
5d4f98a2 | 5488 | |
03e860bd | 5489 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5490 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5491 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5492 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5493 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5494 | } |
03e860bd | 5495 | spin_unlock(&root->inode_lock); |
76dda93c | 5496 | |
69e9c6c6 | 5497 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5498 | spin_lock(&root->inode_lock); |
5499 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5500 | spin_unlock(&root->inode_lock); | |
5501 | if (empty) | |
5502 | btrfs_add_dead_root(root); | |
5503 | } | |
5504 | } | |
5505 | ||
5d4f98a2 | 5506 | |
e02119d5 CM |
5507 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5508 | { | |
5509 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5510 | |
5511 | inode->i_ino = args->ino; | |
5512 | BTRFS_I(inode)->location.objectid = args->ino; | |
5513 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5514 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5515 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5516 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
9b9b8854 JB |
5517 | |
5518 | if (args->root && args->root == args->root->fs_info->tree_root && | |
5519 | args->ino != BTRFS_BTREE_INODE_OBJECTID) | |
5520 | set_bit(BTRFS_INODE_FREE_SPACE_INODE, | |
5521 | &BTRFS_I(inode)->runtime_flags); | |
39279cc3 CM |
5522 | return 0; |
5523 | } | |
5524 | ||
5525 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5526 | { | |
5527 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5528 | |
5529 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5530 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5531 | } |
5532 | ||
0202e83f | 5533 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5534 | struct btrfs_root *root) |
39279cc3 CM |
5535 | { |
5536 | struct inode *inode; | |
5537 | struct btrfs_iget_args args; | |
0202e83f | 5538 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5539 | |
0202e83f | 5540 | args.ino = ino; |
39279cc3 CM |
5541 | args.root = root; |
5542 | ||
778ba82b | 5543 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5544 | btrfs_init_locked_inode, |
5545 | (void *)&args); | |
5546 | return inode; | |
5547 | } | |
5548 | ||
4c66e0d4 | 5549 | /* |
0202e83f | 5550 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5551 | * Path can be preallocated to prevent recursing back to iget through |
5552 | * allocator. NULL is also valid but may require an additional allocation | |
5553 | * later. | |
1a54ef8c | 5554 | */ |
0202e83f | 5555 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5556 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5557 | { |
5558 | struct inode *inode; | |
5559 | ||
0202e83f | 5560 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5561 | if (!inode) |
5d4f98a2 | 5562 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5563 | |
5564 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5565 | int ret; |
5566 | ||
4222ea71 | 5567 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5568 | if (!ret) { |
4c45a4f4 | 5569 | inode_tree_add(BTRFS_I(inode)); |
1748f843 | 5570 | unlock_new_inode(inode); |
1748f843 | 5571 | } else { |
f5b3a417 AV |
5572 | iget_failed(inode); |
5573 | /* | |
5574 | * ret > 0 can come from btrfs_search_slot called by | |
5575 | * btrfs_read_locked_inode, this means the inode item | |
5576 | * was not found. | |
5577 | */ | |
5578 | if (ret > 0) | |
5579 | ret = -ENOENT; | |
5580 | inode = ERR_PTR(ret); | |
1748f843 MF |
5581 | } |
5582 | } | |
5583 | ||
1a54ef8c BR |
5584 | return inode; |
5585 | } | |
5586 | ||
0202e83f | 5587 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5588 | { |
0202e83f | 5589 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5590 | } |
5591 | ||
94628ad9 | 5592 | static struct inode *new_simple_dir(struct inode *dir, |
4df27c4d YZ |
5593 | struct btrfs_key *key, |
5594 | struct btrfs_root *root) | |
5595 | { | |
d5acbc60 | 5596 | struct timespec64 ts; |
94628ad9 | 5597 | struct inode *inode = new_inode(dir->i_sb); |
4df27c4d YZ |
5598 | |
5599 | if (!inode) | |
5600 | return ERR_PTR(-ENOMEM); | |
5601 | ||
5c8fd99f | 5602 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5603 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5604 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5605 | |
5606 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5607 | /* |
5608 | * We only need lookup, the rest is read-only and there's no inode | |
5609 | * associated with the dentry | |
5610 | */ | |
5611 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5612 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5613 | inode->i_fop = &simple_dir_operations; |
5614 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
d5acbc60 LT |
5615 | |
5616 | ts = inode_set_ctime_current(inode); | |
5617 | inode_set_mtime_to_ts(inode, ts); | |
b1c38a13 | 5618 | inode_set_atime_to_ts(inode, inode_get_atime(dir)); |
d5acbc60 LT |
5619 | BTRFS_I(inode)->i_otime_sec = ts.tv_sec; |
5620 | BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec; | |
5621 | ||
94628ad9 LT |
5622 | inode->i_uid = dir->i_uid; |
5623 | inode->i_gid = dir->i_gid; | |
4df27c4d YZ |
5624 | |
5625 | return inode; | |
5626 | } | |
5627 | ||
a55e65b8 DS |
5628 | static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN); |
5629 | static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE); | |
5630 | static_assert(BTRFS_FT_DIR == FT_DIR); | |
5631 | static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV); | |
5632 | static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV); | |
5633 | static_assert(BTRFS_FT_FIFO == FT_FIFO); | |
5634 | static_assert(BTRFS_FT_SOCK == FT_SOCK); | |
5635 | static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK); | |
5636 | ||
6bf9e4bd QW |
5637 | static inline u8 btrfs_inode_type(struct inode *inode) |
5638 | { | |
6bf9e4bd QW |
5639 | return fs_umode_to_ftype(inode->i_mode); |
5640 | } | |
5641 | ||
3de4586c | 5642 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5643 | { |
0b246afa | 5644 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5645 | struct inode *inode; |
4df27c4d | 5646 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5647 | struct btrfs_root *sub_root = root; |
5648 | struct btrfs_key location; | |
6bf9e4bd | 5649 | u8 di_type = 0; |
b4aff1f8 | 5650 | int ret = 0; |
39279cc3 CM |
5651 | |
5652 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5653 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5654 | |
d1de429b | 5655 | ret = btrfs_inode_by_name(BTRFS_I(dir), dentry, &location, &di_type); |
39279cc3 CM |
5656 | if (ret < 0) |
5657 | return ERR_PTR(ret); | |
5f39d397 | 5658 | |
4df27c4d | 5659 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5660 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5661 | if (IS_ERR(inode)) |
5662 | return inode; | |
5663 | ||
5664 | /* Do extra check against inode mode with di_type */ | |
5665 | if (btrfs_inode_type(inode) != di_type) { | |
5666 | btrfs_crit(fs_info, | |
5667 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5668 | inode->i_mode, btrfs_inode_type(inode), | |
5669 | di_type); | |
5670 | iput(inode); | |
5671 | return ERR_PTR(-EUCLEAN); | |
5672 | } | |
4df27c4d YZ |
5673 | return inode; |
5674 | } | |
5675 | ||
3c1b1c4c | 5676 | ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry, |
4df27c4d YZ |
5677 | &location, &sub_root); |
5678 | if (ret < 0) { | |
5679 | if (ret != -ENOENT) | |
5680 | inode = ERR_PTR(ret); | |
5681 | else | |
94628ad9 | 5682 | inode = new_simple_dir(dir, &location, root); |
4df27c4d | 5683 | } else { |
0202e83f | 5684 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
00246528 | 5685 | btrfs_put_root(sub_root); |
76dda93c | 5686 | |
fc8b235f NB |
5687 | if (IS_ERR(inode)) |
5688 | return inode; | |
5689 | ||
0b246afa | 5690 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5691 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5692 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5693 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5694 | if (ret) { |
5695 | iput(inode); | |
66b4ffd1 | 5696 | inode = ERR_PTR(ret); |
01cd3367 | 5697 | } |
c71bf099 YZ |
5698 | } |
5699 | ||
3de4586c CM |
5700 | return inode; |
5701 | } | |
5702 | ||
fe15ce44 | 5703 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5704 | { |
5705 | struct btrfs_root *root; | |
2b0143b5 | 5706 | struct inode *inode = d_inode(dentry); |
76dda93c | 5707 | |
848cce0d | 5708 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5709 | inode = d_inode(dentry->d_parent); |
76dda93c | 5710 | |
848cce0d LZ |
5711 | if (inode) { |
5712 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5713 | if (btrfs_root_refs(&root->root_item) == 0) |
5714 | return 1; | |
848cce0d | 5715 | |
4a0cc7ca | 5716 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5717 | return 1; |
efefb143 | 5718 | } |
76dda93c YZ |
5719 | return 0; |
5720 | } | |
5721 | ||
3de4586c | 5722 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5723 | unsigned int flags) |
3de4586c | 5724 | { |
3837d208 | 5725 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5726 | |
3837d208 AV |
5727 | if (inode == ERR_PTR(-ENOENT)) |
5728 | inode = NULL; | |
41d28bca | 5729 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5730 | } |
5731 | ||
9b378f6a FM |
5732 | /* |
5733 | * Find the highest existing sequence number in a directory and then set the | |
5734 | * in-memory index_cnt variable to the first free sequence number. | |
5735 | */ | |
5736 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) | |
5737 | { | |
5738 | struct btrfs_root *root = inode->root; | |
5739 | struct btrfs_key key, found_key; | |
5740 | struct btrfs_path *path; | |
5741 | struct extent_buffer *leaf; | |
5742 | int ret; | |
5743 | ||
5744 | key.objectid = btrfs_ino(inode); | |
5745 | key.type = BTRFS_DIR_INDEX_KEY; | |
5746 | key.offset = (u64)-1; | |
5747 | ||
5748 | path = btrfs_alloc_path(); | |
5749 | if (!path) | |
5750 | return -ENOMEM; | |
5751 | ||
5752 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5753 | if (ret < 0) | |
5754 | goto out; | |
5755 | /* FIXME: we should be able to handle this */ | |
5756 | if (ret == 0) | |
5757 | goto out; | |
5758 | ret = 0; | |
5759 | ||
5760 | if (path->slots[0] == 0) { | |
5761 | inode->index_cnt = BTRFS_DIR_START_INDEX; | |
5762 | goto out; | |
5763 | } | |
5764 | ||
5765 | path->slots[0]--; | |
5766 | ||
5767 | leaf = path->nodes[0]; | |
5768 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5769 | ||
5770 | if (found_key.objectid != btrfs_ino(inode) || | |
5771 | found_key.type != BTRFS_DIR_INDEX_KEY) { | |
5772 | inode->index_cnt = BTRFS_DIR_START_INDEX; | |
5773 | goto out; | |
5774 | } | |
5775 | ||
5776 | inode->index_cnt = found_key.offset + 1; | |
5777 | out: | |
5778 | btrfs_free_path(path); | |
5779 | return ret; | |
5780 | } | |
5781 | ||
5782 | static int btrfs_get_dir_last_index(struct btrfs_inode *dir, u64 *index) | |
5783 | { | |
8e7f82de | 5784 | int ret = 0; |
9b378f6a | 5785 | |
8e7f82de FM |
5786 | btrfs_inode_lock(dir, 0); |
5787 | if (dir->index_cnt == (u64)-1) { | |
9b378f6a FM |
5788 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5789 | if (ret) { | |
5790 | ret = btrfs_set_inode_index_count(dir); | |
5791 | if (ret) | |
8e7f82de | 5792 | goto out; |
9b378f6a FM |
5793 | } |
5794 | } | |
5795 | ||
35795036 FM |
5796 | /* index_cnt is the index number of next new entry, so decrement it. */ |
5797 | *index = dir->index_cnt - 1; | |
8e7f82de FM |
5798 | out: |
5799 | btrfs_inode_unlock(dir, 0); | |
9b378f6a | 5800 | |
8e7f82de | 5801 | return ret; |
9b378f6a FM |
5802 | } |
5803 | ||
23b5ec74 JB |
5804 | /* |
5805 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5806 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5807 | * our information into that, and then dir_emit from the buffer. This is | |
5808 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5809 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5810 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5811 | * tree lock. | |
5812 | */ | |
5813 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5814 | { | |
5815 | struct btrfs_file_private *private; | |
9b378f6a FM |
5816 | u64 last_index; |
5817 | int ret; | |
5818 | ||
5819 | ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index); | |
5820 | if (ret) | |
5821 | return ret; | |
23b5ec74 JB |
5822 | |
5823 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5824 | if (!private) | |
5825 | return -ENOMEM; | |
9b378f6a | 5826 | private->last_index = last_index; |
23b5ec74 JB |
5827 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
5828 | if (!private->filldir_buf) { | |
5829 | kfree(private); | |
5830 | return -ENOMEM; | |
5831 | } | |
5832 | file->private_data = private; | |
5833 | return 0; | |
5834 | } | |
5835 | ||
e60aa5da FM |
5836 | static loff_t btrfs_dir_llseek(struct file *file, loff_t offset, int whence) |
5837 | { | |
5838 | struct btrfs_file_private *private = file->private_data; | |
5839 | int ret; | |
5840 | ||
5841 | ret = btrfs_get_dir_last_index(BTRFS_I(file_inode(file)), | |
5842 | &private->last_index); | |
5843 | if (ret) | |
5844 | return ret; | |
5845 | ||
5846 | return generic_file_llseek(file, offset, whence); | |
5847 | } | |
5848 | ||
23b5ec74 JB |
5849 | struct dir_entry { |
5850 | u64 ino; | |
5851 | u64 offset; | |
5852 | unsigned type; | |
5853 | int name_len; | |
5854 | }; | |
5855 | ||
5856 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5857 | { | |
5858 | while (entries--) { | |
5859 | struct dir_entry *entry = addr; | |
5860 | char *name = (char *)(entry + 1); | |
5861 | ||
92d32170 DS |
5862 | ctx->pos = get_unaligned(&entry->offset); |
5863 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5864 | get_unaligned(&entry->ino), | |
5865 | get_unaligned(&entry->type))) | |
23b5ec74 | 5866 | return 1; |
92d32170 DS |
5867 | addr += sizeof(struct dir_entry) + |
5868 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5869 | ctx->pos++; |
5870 | } | |
5871 | return 0; | |
5872 | } | |
5873 | ||
9cdda8d3 | 5874 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5875 | { |
9cdda8d3 | 5876 | struct inode *inode = file_inode(file); |
39279cc3 | 5877 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5878 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5879 | struct btrfs_dir_item *di; |
5880 | struct btrfs_key key; | |
5f39d397 | 5881 | struct btrfs_key found_key; |
39279cc3 | 5882 | struct btrfs_path *path; |
23b5ec74 | 5883 | void *addr; |
84af994b RJ |
5884 | LIST_HEAD(ins_list); |
5885 | LIST_HEAD(del_list); | |
39279cc3 | 5886 | int ret; |
5f39d397 CM |
5887 | char *name_ptr; |
5888 | int name_len; | |
23b5ec74 JB |
5889 | int entries = 0; |
5890 | int total_len = 0; | |
02dbfc99 | 5891 | bool put = false; |
c2951f32 | 5892 | struct btrfs_key location; |
5f39d397 | 5893 | |
9cdda8d3 AV |
5894 | if (!dir_emit_dots(file, ctx)) |
5895 | return 0; | |
5896 | ||
49593bfa | 5897 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5898 | if (!path) |
5899 | return -ENOMEM; | |
ff5714cc | 5900 | |
23b5ec74 | 5901 | addr = private->filldir_buf; |
e4058b54 | 5902 | path->reada = READA_FORWARD; |
49593bfa | 5903 | |
9b378f6a FM |
5904 | put = btrfs_readdir_get_delayed_items(inode, private->last_index, |
5905 | &ins_list, &del_list); | |
16cdcec7 | 5906 | |
23b5ec74 | 5907 | again: |
c2951f32 | 5908 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5909 | key.offset = ctx->pos; |
4a0cc7ca | 5910 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5911 | |
a8ce68fd | 5912 | btrfs_for_each_slot(root, &key, &found_key, path, ret) { |
23b5ec74 | 5913 | struct dir_entry *entry; |
a8ce68fd | 5914 | struct extent_buffer *leaf = path->nodes[0]; |
94a48aef | 5915 | u8 ftype; |
5f39d397 CM |
5916 | |
5917 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5918 | break; |
c2951f32 | 5919 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5920 | break; |
9cdda8d3 | 5921 | if (found_key.offset < ctx->pos) |
a8ce68fd | 5922 | continue; |
9b378f6a FM |
5923 | if (found_key.offset > private->last_index) |
5924 | break; | |
c2951f32 | 5925 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
a8ce68fd GN |
5926 | continue; |
5927 | di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); | |
c2951f32 | 5928 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5929 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5930 | PAGE_SIZE) { | |
5931 | btrfs_release_path(path); | |
5932 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5933 | if (ret) | |
5934 | goto nopos; | |
5935 | addr = private->filldir_buf; | |
5936 | entries = 0; | |
5937 | total_len = 0; | |
5938 | goto again; | |
c2951f32 | 5939 | } |
23b5ec74 | 5940 | |
94a48aef | 5941 | ftype = btrfs_dir_flags_to_ftype(btrfs_dir_flags(leaf, di)); |
23b5ec74 | 5942 | entry = addr; |
23b5ec74 | 5943 | name_ptr = (char *)(entry + 1); |
94a48aef OS |
5944 | read_extent_buffer(leaf, name_ptr, |
5945 | (unsigned long)(di + 1), name_len); | |
5946 | put_unaligned(name_len, &entry->name_len); | |
5947 | put_unaligned(fs_ftype_to_dtype(ftype), &entry->type); | |
c2951f32 | 5948 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5949 | put_unaligned(location.objectid, &entry->ino); |
5950 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5951 | entries++; |
5952 | addr += sizeof(struct dir_entry) + name_len; | |
5953 | total_len += sizeof(struct dir_entry) + name_len; | |
39279cc3 | 5954 | } |
a8ce68fd GN |
5955 | /* Catch error encountered during iteration */ |
5956 | if (ret < 0) | |
5957 | goto err; | |
5958 | ||
23b5ec74 JB |
5959 | btrfs_release_path(path); |
5960 | ||
5961 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5962 | if (ret) | |
5963 | goto nopos; | |
49593bfa | 5964 | |
d2fbb2b5 | 5965 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5966 | if (ret) |
bc4ef759 DS |
5967 | goto nopos; |
5968 | ||
db62efbb ZB |
5969 | /* |
5970 | * Stop new entries from being returned after we return the last | |
5971 | * entry. | |
5972 | * | |
5973 | * New directory entries are assigned a strictly increasing | |
5974 | * offset. This means that new entries created during readdir | |
5975 | * are *guaranteed* to be seen in the future by that readdir. | |
5976 | * This has broken buggy programs which operate on names as | |
5977 | * they're returned by readdir. Until we re-use freed offsets | |
5978 | * we have this hack to stop new entries from being returned | |
5979 | * under the assumption that they'll never reach this huge | |
5980 | * offset. | |
5981 | * | |
5982 | * This is being careful not to overflow 32bit loff_t unless the | |
5983 | * last entry requires it because doing so has broken 32bit apps | |
5984 | * in the past. | |
5985 | */ | |
c2951f32 JM |
5986 | if (ctx->pos >= INT_MAX) |
5987 | ctx->pos = LLONG_MAX; | |
5988 | else | |
5989 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5990 | nopos: |
5991 | ret = 0; | |
5992 | err: | |
02dbfc99 OS |
5993 | if (put) |
5994 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5995 | btrfs_free_path(path); |
39279cc3 CM |
5996 | return ret; |
5997 | } | |
5998 | ||
39279cc3 | 5999 | /* |
54aa1f4d | 6000 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6001 | * inode changes. But, it is most likely to find the inode in cache. |
6002 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6003 | * to keep or drop this code. | |
6004 | */ | |
7152b425 | 6005 | static int btrfs_dirty_inode(struct btrfs_inode *inode) |
39279cc3 | 6006 | { |
7152b425 DS |
6007 | struct btrfs_root *root = inode->root; |
6008 | struct btrfs_fs_info *fs_info = root->fs_info; | |
39279cc3 | 6009 | struct btrfs_trans_handle *trans; |
8929ecfa YZ |
6010 | int ret; |
6011 | ||
7152b425 | 6012 | if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags)) |
22c44fe6 | 6013 | return 0; |
39279cc3 | 6014 | |
7a7eaa40 | 6015 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6016 | if (IS_ERR(trans)) |
6017 | return PTR_ERR(trans); | |
8929ecfa | 6018 | |
8b9d0322 | 6019 | ret = btrfs_update_inode(trans, inode); |
2199cb0f | 6020 | if (ret == -ENOSPC || ret == -EDQUOT) { |
94b60442 | 6021 | /* whoops, lets try again with the full transaction */ |
3a45bb20 | 6022 | btrfs_end_transaction(trans); |
94b60442 | 6023 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6024 | if (IS_ERR(trans)) |
6025 | return PTR_ERR(trans); | |
8929ecfa | 6026 | |
8b9d0322 | 6027 | ret = btrfs_update_inode(trans, inode); |
94b60442 | 6028 | } |
3a45bb20 | 6029 | btrfs_end_transaction(trans); |
7152b425 | 6030 | if (inode->delayed_node) |
2ff7e61e | 6031 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6032 | |
6033 | return ret; | |
6034 | } | |
6035 | ||
6036 | /* | |
6037 | * This is a copy of file_update_time. We need this so we can return error on | |
6038 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6039 | */ | |
913e9928 | 6040 | static int btrfs_update_time(struct inode *inode, int flags) |
22c44fe6 | 6041 | { |
2bc55652 | 6042 | struct btrfs_root *root = BTRFS_I(inode)->root; |
a666ce9b | 6043 | bool dirty; |
2bc55652 AB |
6044 | |
6045 | if (btrfs_root_readonly(root)) | |
6046 | return -EROFS; | |
6047 | ||
bb7cc0a6 | 6048 | dirty = inode_update_timestamps(inode, flags); |
7152b425 | 6049 | return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0; |
39279cc3 CM |
6050 | } |
6051 | ||
d352ac68 CM |
6052 | /* |
6053 | * helper to find a free sequence number in a given directory. This current | |
6054 | * code is very simple, later versions will do smarter things in the btree | |
6055 | */ | |
877574e2 | 6056 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6057 | { |
6058 | int ret = 0; | |
6059 | ||
877574e2 NB |
6060 | if (dir->index_cnt == (u64)-1) { |
6061 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6062 | if (ret) { |
6063 | ret = btrfs_set_inode_index_count(dir); | |
6064 | if (ret) | |
6065 | return ret; | |
6066 | } | |
aec7477b JB |
6067 | } |
6068 | ||
877574e2 NB |
6069 | *index = dir->index_cnt; |
6070 | dir->index_cnt++; | |
aec7477b JB |
6071 | |
6072 | return ret; | |
6073 | } | |
6074 | ||
b0d5d10f CM |
6075 | static int btrfs_insert_inode_locked(struct inode *inode) |
6076 | { | |
6077 | struct btrfs_iget_args args; | |
0202e83f DS |
6078 | |
6079 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
6080 | args.root = BTRFS_I(inode)->root; |
6081 | ||
6082 | return insert_inode_locked4(inode, | |
6083 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6084 | btrfs_find_actor, &args); | |
6085 | } | |
6086 | ||
3538d68d OS |
6087 | int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args, |
6088 | unsigned int *trans_num_items) | |
6089 | { | |
6090 | struct inode *dir = args->dir; | |
6091 | struct inode *inode = args->inode; | |
6092 | int ret; | |
6093 | ||
ab3c5c18 STD |
6094 | if (!args->orphan) { |
6095 | ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0, | |
6096 | &args->fname); | |
6097 | if (ret) | |
6098 | return ret; | |
ab3c5c18 STD |
6099 | } |
6100 | ||
3538d68d | 6101 | ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl); |
ab3c5c18 STD |
6102 | if (ret) { |
6103 | fscrypt_free_filename(&args->fname); | |
3538d68d | 6104 | return ret; |
ab3c5c18 | 6105 | } |
3538d68d OS |
6106 | |
6107 | /* 1 to add inode item */ | |
6108 | *trans_num_items = 1; | |
6109 | /* 1 to add compression property */ | |
6110 | if (BTRFS_I(dir)->prop_compress) | |
6111 | (*trans_num_items)++; | |
6112 | /* 1 to add default ACL xattr */ | |
6113 | if (args->default_acl) | |
6114 | (*trans_num_items)++; | |
6115 | /* 1 to add access ACL xattr */ | |
6116 | if (args->acl) | |
6117 | (*trans_num_items)++; | |
6118 | #ifdef CONFIG_SECURITY | |
6119 | /* 1 to add LSM xattr */ | |
6120 | if (dir->i_security) | |
6121 | (*trans_num_items)++; | |
6122 | #endif | |
6123 | if (args->orphan) { | |
6124 | /* 1 to add orphan item */ | |
6125 | (*trans_num_items)++; | |
6126 | } else { | |
6127 | /* | |
3538d68d OS |
6128 | * 1 to add dir item |
6129 | * 1 to add dir index | |
6130 | * 1 to update parent inode item | |
97bdf1a9 FM |
6131 | * |
6132 | * No need for 1 unit for the inode ref item because it is | |
6133 | * inserted in a batch together with the inode item at | |
6134 | * btrfs_create_new_inode(). | |
3538d68d | 6135 | */ |
97bdf1a9 | 6136 | *trans_num_items += 3; |
3538d68d OS |
6137 | } |
6138 | return 0; | |
6139 | } | |
6140 | ||
6141 | void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args) | |
6142 | { | |
6143 | posix_acl_release(args->acl); | |
6144 | posix_acl_release(args->default_acl); | |
ab3c5c18 | 6145 | fscrypt_free_filename(&args->fname); |
3538d68d OS |
6146 | } |
6147 | ||
19aee8de AJ |
6148 | /* |
6149 | * Inherit flags from the parent inode. | |
6150 | * | |
6151 | * Currently only the compression flags and the cow flags are inherited. | |
6152 | */ | |
7a0443f0 | 6153 | static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode *dir) |
19aee8de AJ |
6154 | { |
6155 | unsigned int flags; | |
6156 | ||
7a0443f0 | 6157 | flags = dir->flags; |
19aee8de AJ |
6158 | |
6159 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
7a0443f0 DS |
6160 | inode->flags &= ~BTRFS_INODE_COMPRESS; |
6161 | inode->flags |= BTRFS_INODE_NOCOMPRESS; | |
19aee8de | 6162 | } else if (flags & BTRFS_INODE_COMPRESS) { |
7a0443f0 DS |
6163 | inode->flags &= ~BTRFS_INODE_NOCOMPRESS; |
6164 | inode->flags |= BTRFS_INODE_COMPRESS; | |
19aee8de AJ |
6165 | } |
6166 | ||
6167 | if (flags & BTRFS_INODE_NODATACOW) { | |
7a0443f0 DS |
6168 | inode->flags |= BTRFS_INODE_NODATACOW; |
6169 | if (S_ISREG(inode->vfs_inode.i_mode)) | |
6170 | inode->flags |= BTRFS_INODE_NODATASUM; | |
19aee8de AJ |
6171 | } |
6172 | ||
7a0443f0 | 6173 | btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode); |
19aee8de AJ |
6174 | } |
6175 | ||
3538d68d | 6176 | int btrfs_create_new_inode(struct btrfs_trans_handle *trans, |
caae78e0 | 6177 | struct btrfs_new_inode_args *args) |
39279cc3 | 6178 | { |
d5acbc60 | 6179 | struct timespec64 ts; |
caae78e0 | 6180 | struct inode *dir = args->dir; |
3538d68d | 6181 | struct inode *inode = args->inode; |
6db75318 | 6182 | const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name; |
caae78e0 | 6183 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
3538d68d | 6184 | struct btrfs_root *root; |
5f39d397 | 6185 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6186 | struct btrfs_key *location; |
5f39d397 | 6187 | struct btrfs_path *path; |
6437d458 | 6188 | u64 objectid; |
9c58309d CM |
6189 | struct btrfs_inode_ref *ref; |
6190 | struct btrfs_key key[2]; | |
6191 | u32 sizes[2]; | |
b7ef5f3a | 6192 | struct btrfs_item_batch batch; |
9c58309d | 6193 | unsigned long ptr; |
39279cc3 | 6194 | int ret; |
39279cc3 | 6195 | |
5f39d397 | 6196 | path = btrfs_alloc_path(); |
d8926bb3 | 6197 | if (!path) |
a1fd0c35 | 6198 | return -ENOMEM; |
39279cc3 | 6199 | |
3538d68d OS |
6200 | if (!args->subvol) |
6201 | BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root); | |
6202 | root = BTRFS_I(inode)->root; | |
6203 | ||
6437d458 | 6204 | ret = btrfs_get_free_objectid(root, &objectid); |
caae78e0 OS |
6205 | if (ret) |
6206 | goto out; | |
581bb050 LZ |
6207 | inode->i_ino = objectid; |
6208 | ||
caae78e0 OS |
6209 | if (args->orphan) { |
6210 | /* | |
6211 | * O_TMPFILE, set link count to 0, so that after this point, we | |
6212 | * fill in an inode item with the correct link count. | |
6213 | */ | |
6214 | set_nlink(inode, 0); | |
6215 | } else { | |
1abe9b8a | 6216 | trace_btrfs_inode_request(dir); |
6217 | ||
caae78e0 OS |
6218 | ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index); |
6219 | if (ret) | |
6220 | goto out; | |
aec7477b | 6221 | } |
49024388 FM |
6222 | /* index_cnt is ignored for everything but a dir. */ |
6223 | BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX; | |
e02119d5 | 6224 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6225 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6226 | |
ed9b50a1 JB |
6227 | /* |
6228 | * We don't have any capability xattrs set here yet, shortcut any | |
6229 | * queries for the xattrs here. If we add them later via the inode | |
6230 | * security init path or any other path this flag will be cleared. | |
6231 | */ | |
6232 | set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); | |
6233 | ||
caae78e0 OS |
6234 | /* |
6235 | * Subvolumes don't inherit flags from their parent directory. | |
6236 | * Originally this was probably by accident, but we probably can't | |
6237 | * change it now without compatibility issues. | |
6238 | */ | |
6239 | if (!args->subvol) | |
7a0443f0 | 6240 | btrfs_inherit_iflags(BTRFS_I(inode), BTRFS_I(dir)); |
305eaac0 | 6241 | |
a1fd0c35 | 6242 | if (S_ISREG(inode->i_mode)) { |
305eaac0 OS |
6243 | if (btrfs_test_opt(fs_info, NODATASUM)) |
6244 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6245 | if (btrfs_test_opt(fs_info, NODATACOW)) | |
6246 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | | |
6247 | BTRFS_INODE_NODATASUM; | |
6248 | } | |
6249 | ||
caae78e0 OS |
6250 | location = &BTRFS_I(inode)->location; |
6251 | location->objectid = objectid; | |
6252 | location->offset = 0; | |
6253 | location->type = BTRFS_INODE_ITEM_KEY; | |
6254 | ||
6255 | ret = btrfs_insert_inode_locked(inode); | |
6256 | if (ret < 0) { | |
6257 | if (!args->orphan) | |
6258 | BTRFS_I(dir)->index_cnt--; | |
6259 | goto out; | |
6260 | } | |
6261 | ||
5dc562c5 JB |
6262 | /* |
6263 | * We could have gotten an inode number from somebody who was fsynced | |
6264 | * and then removed in this same transaction, so let's just set full | |
6265 | * sync since it will be a full sync anyway and this will blow away the | |
6266 | * old info in the log. | |
6267 | */ | |
23e3337f | 6268 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 | 6269 | |
9c58309d | 6270 | key[0].objectid = objectid; |
962a298f | 6271 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6272 | key[0].offset = 0; |
6273 | ||
9c58309d | 6274 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 | 6275 | |
caae78e0 | 6276 | if (!args->orphan) { |
ef3b9af5 FM |
6277 | /* |
6278 | * Start new inodes with an inode_ref. This is slightly more | |
6279 | * efficient for small numbers of hard links since they will | |
6280 | * be packed into one item. Extended refs will kick in if we | |
6281 | * add more hard links than can fit in the ref item. | |
6282 | */ | |
6283 | key[1].objectid = objectid; | |
962a298f | 6284 | key[1].type = BTRFS_INODE_REF_KEY; |
caae78e0 | 6285 | if (args->subvol) { |
23c24ef8 | 6286 | key[1].offset = objectid; |
caae78e0 OS |
6287 | sizes[1] = 2 + sizeof(*ref); |
6288 | } else { | |
6289 | key[1].offset = btrfs_ino(BTRFS_I(dir)); | |
e43eec81 | 6290 | sizes[1] = name->len + sizeof(*ref); |
caae78e0 | 6291 | } |
ef3b9af5 | 6292 | } |
9c58309d | 6293 | |
b7ef5f3a FM |
6294 | batch.keys = &key[0]; |
6295 | batch.data_sizes = &sizes[0]; | |
caae78e0 OS |
6296 | batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]); |
6297 | batch.nr = args->orphan ? 1 : 2; | |
b7ef5f3a | 6298 | ret = btrfs_insert_empty_items(trans, root, path, &batch); |
caae78e0 OS |
6299 | if (ret != 0) { |
6300 | btrfs_abort_transaction(trans, ret); | |
6301 | goto discard; | |
6302 | } | |
5f39d397 | 6303 | |
d5acbc60 LT |
6304 | ts = simple_inode_init_ts(inode); |
6305 | BTRFS_I(inode)->i_otime_sec = ts.tv_sec; | |
6306 | BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec; | |
9cc97d64 | 6307 | |
caae78e0 OS |
6308 | /* |
6309 | * We're going to fill the inode item now, so at this point the inode | |
6310 | * must be fully initialized. | |
6311 | */ | |
6312 | ||
5f39d397 CM |
6313 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6314 | struct btrfs_inode_item); | |
b159fa28 | 6315 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6316 | sizeof(*inode_item)); |
e02119d5 | 6317 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6318 | |
caae78e0 | 6319 | if (!args->orphan) { |
ef3b9af5 FM |
6320 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
6321 | struct btrfs_inode_ref); | |
ef3b9af5 | 6322 | ptr = (unsigned long)(ref + 1); |
caae78e0 OS |
6323 | if (args->subvol) { |
6324 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2); | |
6325 | btrfs_set_inode_ref_index(path->nodes[0], ref, 0); | |
6326 | write_extent_buffer(path->nodes[0], "..", ptr, 2); | |
6327 | } else { | |
e43eec81 STD |
6328 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, |
6329 | name->len); | |
caae78e0 OS |
6330 | btrfs_set_inode_ref_index(path->nodes[0], ref, |
6331 | BTRFS_I(inode)->dir_index); | |
e43eec81 STD |
6332 | write_extent_buffer(path->nodes[0], name->name, ptr, |
6333 | name->len); | |
caae78e0 | 6334 | } |
ef3b9af5 | 6335 | } |
9c58309d | 6336 | |
50564b65 | 6337 | btrfs_mark_buffer_dirty(trans, path->nodes[0]); |
814e7718 FM |
6338 | /* |
6339 | * We don't need the path anymore, plus inheriting properties, adding | |
6340 | * ACLs, security xattrs, orphan item or adding the link, will result in | |
6341 | * allocating yet another path. So just free our path. | |
6342 | */ | |
6343 | btrfs_free_path(path); | |
6344 | path = NULL; | |
5f39d397 | 6345 | |
6c3636eb STD |
6346 | if (args->subvol) { |
6347 | struct inode *parent; | |
6348 | ||
6349 | /* | |
6350 | * Subvolumes inherit properties from their parent subvolume, | |
6351 | * not the directory they were created in. | |
6352 | */ | |
6353 | parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID, | |
6354 | BTRFS_I(dir)->root); | |
6355 | if (IS_ERR(parent)) { | |
6356 | ret = PTR_ERR(parent); | |
6357 | } else { | |
6358 | ret = btrfs_inode_inherit_props(trans, inode, parent); | |
6359 | iput(parent); | |
6360 | } | |
6361 | } else { | |
6362 | ret = btrfs_inode_inherit_props(trans, inode, dir); | |
6363 | } | |
6364 | if (ret) { | |
6365 | btrfs_err(fs_info, | |
6366 | "error inheriting props for ino %llu (root %llu): %d", | |
6367 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, | |
6368 | ret); | |
6369 | } | |
6370 | ||
6371 | /* | |
6372 | * Subvolumes don't inherit ACLs or get passed to the LSM. This is | |
6373 | * probably a bug. | |
6374 | */ | |
6375 | if (!args->subvol) { | |
6376 | ret = btrfs_init_inode_security(trans, args); | |
6377 | if (ret) { | |
6378 | btrfs_abort_transaction(trans, ret); | |
6379 | goto discard; | |
6380 | } | |
6381 | } | |
6382 | ||
4c45a4f4 | 6383 | inode_tree_add(BTRFS_I(inode)); |
1abe9b8a | 6384 | |
6385 | trace_btrfs_inode_new(inode); | |
d9094414 | 6386 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6387 | |
8ea05e3a AB |
6388 | btrfs_update_root_times(trans, root); |
6389 | ||
caae78e0 OS |
6390 | if (args->orphan) { |
6391 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
6392 | } else { | |
6393 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, | |
e43eec81 | 6394 | 0, BTRFS_I(inode)->dir_index); |
caae78e0 OS |
6395 | } |
6396 | if (ret) { | |
6397 | btrfs_abort_transaction(trans, ret); | |
6398 | goto discard; | |
6399 | } | |
63541927 | 6400 | |
814e7718 | 6401 | return 0; |
b0d5d10f | 6402 | |
caae78e0 | 6403 | discard: |
a1fd0c35 OS |
6404 | /* |
6405 | * discard_new_inode() calls iput(), but the caller owns the reference | |
6406 | * to the inode. | |
6407 | */ | |
6408 | ihold(inode); | |
32955c54 | 6409 | discard_new_inode(inode); |
caae78e0 | 6410 | out: |
5f39d397 | 6411 | btrfs_free_path(path); |
a1fd0c35 | 6412 | return ret; |
39279cc3 CM |
6413 | } |
6414 | ||
d352ac68 CM |
6415 | /* |
6416 | * utility function to add 'inode' into 'parent_inode' with | |
6417 | * a give name and a given sequence number. | |
6418 | * if 'add_backref' is true, also insert a backref from the | |
6419 | * inode to the parent directory. | |
6420 | */ | |
e02119d5 | 6421 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6422 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
6db75318 | 6423 | const struct fscrypt_str *name, int add_backref, u64 index) |
39279cc3 | 6424 | { |
4df27c4d | 6425 | int ret = 0; |
39279cc3 | 6426 | struct btrfs_key key; |
db0a669f NB |
6427 | struct btrfs_root *root = parent_inode->root; |
6428 | u64 ino = btrfs_ino(inode); | |
6429 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6430 | |
33345d01 | 6431 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6432 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6433 | } else { |
33345d01 | 6434 | key.objectid = ino; |
962a298f | 6435 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6436 | key.offset = 0; |
6437 | } | |
6438 | ||
33345d01 | 6439 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6440 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa | 6441 | root->root_key.objectid, parent_ino, |
e43eec81 | 6442 | index, name); |
4df27c4d | 6443 | } else if (add_backref) { |
e43eec81 STD |
6444 | ret = btrfs_insert_inode_ref(trans, root, name, |
6445 | ino, parent_ino, index); | |
4df27c4d | 6446 | } |
39279cc3 | 6447 | |
79787eaa JM |
6448 | /* Nothing to clean up yet */ |
6449 | if (ret) | |
6450 | return ret; | |
4df27c4d | 6451 | |
e43eec81 | 6452 | ret = btrfs_insert_dir_item(trans, name, parent_inode, &key, |
db0a669f | 6453 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6454 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6455 | goto fail_dir_item; |
6456 | else if (ret) { | |
66642832 | 6457 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6458 | return ret; |
39279cc3 | 6459 | } |
79787eaa | 6460 | |
db0a669f | 6461 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
e43eec81 | 6462 | name->len * 2); |
db0a669f | 6463 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6464 | /* |
6465 | * If we are replaying a log tree, we do not want to update the mtime | |
6466 | * and ctime of the parent directory with the current time, since the | |
6467 | * log replay procedure is responsible for setting them to their correct | |
6468 | * values (the ones it had when the fsync was done). | |
6469 | */ | |
2a9462de | 6470 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) |
b1c38a13 JL |
6471 | inode_set_mtime_to_ts(&parent_inode->vfs_inode, |
6472 | inode_set_ctime_current(&parent_inode->vfs_inode)); | |
5338e43a | 6473 | |
8b9d0322 | 6474 | ret = btrfs_update_inode(trans, parent_inode); |
79787eaa | 6475 | if (ret) |
66642832 | 6476 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6477 | return ret; |
fe66a05a CM |
6478 | |
6479 | fail_dir_item: | |
6480 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6481 | u64 local_index; | |
6482 | int err; | |
3ee1c553 | 6483 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa | 6484 | root->root_key.objectid, parent_ino, |
e43eec81 | 6485 | &local_index, name); |
1690dd41 JT |
6486 | if (err) |
6487 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6488 | } else if (add_backref) { |
6489 | u64 local_index; | |
6490 | int err; | |
6491 | ||
e43eec81 STD |
6492 | err = btrfs_del_inode_ref(trans, root, name, ino, parent_ino, |
6493 | &local_index); | |
1690dd41 JT |
6494 | if (err) |
6495 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6496 | } |
1690dd41 JT |
6497 | |
6498 | /* Return the original error code */ | |
fe66a05a | 6499 | return ret; |
39279cc3 CM |
6500 | } |
6501 | ||
5f465bf1 OS |
6502 | static int btrfs_create_common(struct inode *dir, struct dentry *dentry, |
6503 | struct inode *inode) | |
618e21d5 | 6504 | { |
2ff7e61e | 6505 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 | 6506 | struct btrfs_root *root = BTRFS_I(dir)->root; |
3538d68d OS |
6507 | struct btrfs_new_inode_args new_inode_args = { |
6508 | .dir = dir, | |
6509 | .dentry = dentry, | |
6510 | .inode = inode, | |
6511 | }; | |
6512 | unsigned int trans_num_items; | |
5f465bf1 | 6513 | struct btrfs_trans_handle *trans; |
618e21d5 | 6514 | int err; |
618e21d5 | 6515 | |
3538d68d | 6516 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); |
caae78e0 OS |
6517 | if (err) |
6518 | goto out_inode; | |
3538d68d OS |
6519 | |
6520 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 6521 | if (IS_ERR(trans)) { |
3538d68d OS |
6522 | err = PTR_ERR(trans); |
6523 | goto out_new_inode_args; | |
a1fd0c35 | 6524 | } |
1832a6d5 | 6525 | |
caae78e0 OS |
6526 | err = btrfs_create_new_inode(trans, &new_inode_args); |
6527 | if (!err) | |
6528 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6529 | |
3a45bb20 | 6530 | btrfs_end_transaction(trans); |
5f465bf1 | 6531 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
6532 | out_new_inode_args: |
6533 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
6534 | out_inode: |
6535 | if (err) | |
6536 | iput(inode); | |
618e21d5 JB |
6537 | return err; |
6538 | } | |
6539 | ||
5ebb29be | 6540 | static int btrfs_mknod(struct mnt_idmap *idmap, struct inode *dir, |
5f465bf1 OS |
6541 | struct dentry *dentry, umode_t mode, dev_t rdev) |
6542 | { | |
6543 | struct inode *inode; | |
6544 | ||
6545 | inode = new_inode(dir->i_sb); | |
6546 | if (!inode) | |
6547 | return -ENOMEM; | |
f2d40141 | 6548 | inode_init_owner(idmap, inode, dir, mode); |
5f465bf1 OS |
6549 | inode->i_op = &btrfs_special_inode_operations; |
6550 | init_special_inode(inode, inode->i_mode, rdev); | |
6551 | return btrfs_create_common(dir, dentry, inode); | |
6552 | } | |
6553 | ||
6c960e68 | 6554 | static int btrfs_create(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 6555 | struct dentry *dentry, umode_t mode, bool excl) |
39279cc3 | 6556 | { |
a1fd0c35 | 6557 | struct inode *inode; |
39279cc3 | 6558 | |
a1fd0c35 OS |
6559 | inode = new_inode(dir->i_sb); |
6560 | if (!inode) | |
6561 | return -ENOMEM; | |
f2d40141 | 6562 | inode_init_owner(idmap, inode, dir, mode); |
a1fd0c35 OS |
6563 | inode->i_fop = &btrfs_file_operations; |
6564 | inode->i_op = &btrfs_file_inode_operations; | |
6565 | inode->i_mapping->a_ops = &btrfs_aops; | |
5f465bf1 | 6566 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6567 | } |
6568 | ||
6569 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6570 | struct dentry *dentry) | |
6571 | { | |
271dba45 | 6572 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6573 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6574 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6575 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
ab3c5c18 | 6576 | struct fscrypt_name fname; |
00e4e6b3 | 6577 | u64 index; |
39279cc3 CM |
6578 | int err; |
6579 | int drop_inode = 0; | |
6580 | ||
4a8be425 | 6581 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6582 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6583 | return -EXDEV; |
4a8be425 | 6584 | |
f186373f | 6585 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6586 | return -EMLINK; |
4a8be425 | 6587 | |
ab3c5c18 STD |
6588 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname); |
6589 | if (err) | |
6590 | goto fail; | |
6591 | ||
877574e2 | 6592 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6593 | if (err) |
6594 | goto fail; | |
6595 | ||
a22285a6 | 6596 | /* |
7e6b6465 | 6597 | * 2 items for inode and inode ref |
a22285a6 | 6598 | * 2 items for dir items |
7e6b6465 | 6599 | * 1 item for parent inode |
399b0bbf | 6600 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6601 | */ |
399b0bbf | 6602 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6603 | if (IS_ERR(trans)) { |
6604 | err = PTR_ERR(trans); | |
271dba45 | 6605 | trans = NULL; |
a22285a6 YZ |
6606 | goto fail; |
6607 | } | |
5f39d397 | 6608 | |
67de1176 MX |
6609 | /* There are several dir indexes for this inode, clear the cache. */ |
6610 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6611 | inc_nlink(inode); |
0c4d2d95 | 6612 | inode_inc_iversion(inode); |
2a9462de | 6613 | inode_set_ctime_current(inode); |
7de9c6ee | 6614 | ihold(inode); |
e9976151 | 6615 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6616 | |
81512e89 | 6617 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6db75318 | 6618 | &fname.disk_name, 1, index); |
5f39d397 | 6619 | |
a5719521 | 6620 | if (err) { |
54aa1f4d | 6621 | drop_inode = 1; |
a5719521 | 6622 | } else { |
10d9f309 | 6623 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6624 | |
8b9d0322 | 6625 | err = btrfs_update_inode(trans, BTRFS_I(inode)); |
79787eaa JM |
6626 | if (err) |
6627 | goto fail; | |
ef3b9af5 FM |
6628 | if (inode->i_nlink == 1) { |
6629 | /* | |
6630 | * If new hard link count is 1, it's a file created | |
6631 | * with open(2) O_TMPFILE flag. | |
6632 | */ | |
3d6ae7bb | 6633 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6634 | if (err) |
6635 | goto fail; | |
6636 | } | |
08c422c2 | 6637 | d_instantiate(dentry, inode); |
88d2beec | 6638 | btrfs_log_new_name(trans, old_dentry, NULL, 0, parent); |
a5719521 | 6639 | } |
39279cc3 | 6640 | |
1832a6d5 | 6641 | fail: |
ab3c5c18 | 6642 | fscrypt_free_filename(&fname); |
271dba45 | 6643 | if (trans) |
3a45bb20 | 6644 | btrfs_end_transaction(trans); |
39279cc3 CM |
6645 | if (drop_inode) { |
6646 | inode_dec_link_count(inode); | |
6647 | iput(inode); | |
6648 | } | |
2ff7e61e | 6649 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6650 | return err; |
6651 | } | |
6652 | ||
c54bd91e | 6653 | static int btrfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 6654 | struct dentry *dentry, umode_t mode) |
39279cc3 | 6655 | { |
a1fd0c35 | 6656 | struct inode *inode; |
39279cc3 | 6657 | |
a1fd0c35 OS |
6658 | inode = new_inode(dir->i_sb); |
6659 | if (!inode) | |
6660 | return -ENOMEM; | |
f2d40141 | 6661 | inode_init_owner(idmap, inode, dir, S_IFDIR | mode); |
a1fd0c35 OS |
6662 | inode->i_op = &btrfs_dir_inode_operations; |
6663 | inode->i_fop = &btrfs_dir_file_operations; | |
5f465bf1 | 6664 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6665 | } |
6666 | ||
c8b97818 | 6667 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6668 | struct page *page, |
c8b97818 CM |
6669 | struct btrfs_file_extent_item *item) |
6670 | { | |
6671 | int ret; | |
6672 | struct extent_buffer *leaf = path->nodes[0]; | |
6673 | char *tmp; | |
6674 | size_t max_size; | |
6675 | unsigned long inline_size; | |
6676 | unsigned long ptr; | |
261507a0 | 6677 | int compress_type; |
c8b97818 | 6678 | |
261507a0 | 6679 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 | 6680 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
437bd07e | 6681 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); |
c8b97818 | 6682 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6683 | if (!tmp) |
6684 | return -ENOMEM; | |
c8b97818 CM |
6685 | ptr = btrfs_file_extent_inline_start(item); |
6686 | ||
6687 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6688 | ||
09cbfeaf | 6689 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
a982fc82 | 6690 | ret = btrfs_decompress(compress_type, tmp, page, 0, inline_size, max_size); |
e1699d2d ZB |
6691 | |
6692 | /* | |
6693 | * decompression code contains a memset to fill in any space between the end | |
6694 | * of the uncompressed data and the end of max_size in case the decompressed | |
6695 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6696 | * the end of an inline extent and the beginning of the next block, so we | |
6697 | * cover that region here. | |
6698 | */ | |
6699 | ||
a982fc82 QW |
6700 | if (max_size < PAGE_SIZE) |
6701 | memzero_page(page, max_size, PAGE_SIZE - max_size); | |
c8b97818 | 6702 | kfree(tmp); |
166ae5a4 | 6703 | return ret; |
c8b97818 CM |
6704 | } |
6705 | ||
a982fc82 QW |
6706 | static int read_inline_extent(struct btrfs_inode *inode, struct btrfs_path *path, |
6707 | struct page *page) | |
6708 | { | |
6709 | struct btrfs_file_extent_item *fi; | |
6710 | void *kaddr; | |
6711 | size_t copy_size; | |
6712 | ||
6713 | if (!page || PageUptodate(page)) | |
6714 | return 0; | |
6715 | ||
6716 | ASSERT(page_offset(page) == 0); | |
6717 | ||
6718 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
6719 | struct btrfs_file_extent_item); | |
6720 | if (btrfs_file_extent_compression(path->nodes[0], fi) != BTRFS_COMPRESS_NONE) | |
6721 | return uncompress_inline(path, page, fi); | |
6722 | ||
6723 | copy_size = min_t(u64, PAGE_SIZE, | |
6724 | btrfs_file_extent_ram_bytes(path->nodes[0], fi)); | |
6725 | kaddr = kmap_local_page(page); | |
6726 | read_extent_buffer(path->nodes[0], kaddr, | |
6727 | btrfs_file_extent_inline_start(fi), copy_size); | |
6728 | kunmap_local(kaddr); | |
6729 | if (copy_size < PAGE_SIZE) | |
6730 | memzero_page(page, copy_size, PAGE_SIZE - copy_size); | |
6731 | return 0; | |
6732 | } | |
6733 | ||
43dd529a DS |
6734 | /* |
6735 | * Lookup the first extent overlapping a range in a file. | |
6736 | * | |
39b07b5d OS |
6737 | * @inode: file to search in |
6738 | * @page: page to read extent data into if the extent is inline | |
6739 | * @pg_offset: offset into @page to copy to | |
6740 | * @start: file offset | |
6741 | * @len: length of range starting at @start | |
6742 | * | |
43dd529a DS |
6743 | * Return the first &struct extent_map which overlaps the given range, reading |
6744 | * it from the B-tree and caching it if necessary. Note that there may be more | |
6745 | * extents which overlap the given range after the returned extent_map. | |
d352ac68 | 6746 | * |
39b07b5d OS |
6747 | * If @page is not NULL and the extent is inline, this also reads the extent |
6748 | * data directly into the page and marks the extent up to date in the io_tree. | |
6749 | * | |
6750 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6751 | */ |
fc4f21b1 | 6752 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
39b07b5d OS |
6753 | struct page *page, size_t pg_offset, |
6754 | u64 start, u64 len) | |
a52d9a80 | 6755 | { |
3ffbd68c | 6756 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6757 | int ret = 0; |
a52d9a80 CM |
6758 | u64 extent_start = 0; |
6759 | u64 extent_end = 0; | |
fc4f21b1 | 6760 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6761 | int extent_type = -1; |
f421950f | 6762 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6763 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6764 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6765 | struct extent_buffer *leaf; |
6766 | struct btrfs_key found_key; | |
a52d9a80 | 6767 | struct extent_map *em = NULL; |
fc4f21b1 | 6768 | struct extent_map_tree *em_tree = &inode->extent_tree; |
a52d9a80 | 6769 | |
890871be | 6770 | read_lock(&em_tree->lock); |
d1310b2e | 6771 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6772 | read_unlock(&em_tree->lock); |
d1310b2e | 6773 | |
a52d9a80 | 6774 | if (em) { |
e1c4b745 CM |
6775 | if (em->start > start || em->start + em->len <= start) |
6776 | free_extent_map(em); | |
6777 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6778 | free_extent_map(em); |
6779 | else | |
6780 | goto out; | |
a52d9a80 | 6781 | } |
172ddd60 | 6782 | em = alloc_extent_map(); |
a52d9a80 | 6783 | if (!em) { |
1028d1c4 | 6784 | ret = -ENOMEM; |
d1310b2e | 6785 | goto out; |
a52d9a80 | 6786 | } |
d1310b2e | 6787 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6788 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6789 | em->len = (u64)-1; |
c8b97818 | 6790 | em->block_len = (u64)-1; |
f421950f | 6791 | |
bee6ec82 | 6792 | path = btrfs_alloc_path(); |
f421950f | 6793 | if (!path) { |
1028d1c4 | 6794 | ret = -ENOMEM; |
bee6ec82 | 6795 | goto out; |
f421950f CM |
6796 | } |
6797 | ||
bee6ec82 LB |
6798 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6799 | path->reada = READA_FORWARD; | |
4d7240f0 JB |
6800 | |
6801 | /* | |
6802 | * The same explanation in load_free_space_cache applies here as well, | |
6803 | * we only read when we're loading the free space cache, and at that | |
6804 | * point the commit_root has everything we need. | |
6805 | */ | |
6806 | if (btrfs_is_free_space_inode(inode)) { | |
6807 | path->search_commit_root = 1; | |
6808 | path->skip_locking = 1; | |
6809 | } | |
51899412 | 6810 | |
5c9a702e | 6811 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6812 | if (ret < 0) { |
a52d9a80 | 6813 | goto out; |
b8eeab7f | 6814 | } else if (ret > 0) { |
a52d9a80 CM |
6815 | if (path->slots[0] == 0) |
6816 | goto not_found; | |
6817 | path->slots[0]--; | |
1028d1c4 | 6818 | ret = 0; |
a52d9a80 CM |
6819 | } |
6820 | ||
5f39d397 CM |
6821 | leaf = path->nodes[0]; |
6822 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6823 | struct btrfs_file_extent_item); |
5f39d397 | 6824 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6825 | if (found_key.objectid != objectid || |
694c12ed | 6826 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6827 | /* |
6828 | * If we backup past the first extent we want to move forward | |
6829 | * and see if there is an extent in front of us, otherwise we'll | |
6830 | * say there is a hole for our whole search range which can | |
6831 | * cause problems. | |
6832 | */ | |
6833 | extent_end = start; | |
6834 | goto next; | |
a52d9a80 CM |
6835 | } |
6836 | ||
694c12ed | 6837 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6838 | extent_start = found_key.offset; |
a5eeb3d1 | 6839 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6840 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6841 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6842 | /* Only regular file could have regular/prealloc extent */ |
6843 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 6844 | ret = -EUCLEAN; |
6bf9e4bd QW |
6845 | btrfs_crit(fs_info, |
6846 | "regular/prealloc extent found for non-regular inode %llu", | |
6847 | btrfs_ino(inode)); | |
6848 | goto out; | |
6849 | } | |
09ed2f16 LB |
6850 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6851 | extent_start); | |
694c12ed | 6852 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6853 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6854 | path->slots[0], | |
6855 | extent_start); | |
9036c102 | 6856 | } |
25a50341 | 6857 | next: |
9036c102 YZ |
6858 | if (start >= extent_end) { |
6859 | path->slots[0]++; | |
6860 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6861 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 6862 | if (ret < 0) |
9036c102 | 6863 | goto out; |
1028d1c4 | 6864 | else if (ret > 0) |
9036c102 | 6865 | goto not_found; |
1028d1c4 | 6866 | |
9036c102 | 6867 | leaf = path->nodes[0]; |
a52d9a80 | 6868 | } |
9036c102 YZ |
6869 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6870 | if (found_key.objectid != objectid || | |
6871 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6872 | goto not_found; | |
6873 | if (start + len <= found_key.offset) | |
6874 | goto not_found; | |
e2eca69d WS |
6875 | if (start > found_key.offset) |
6876 | goto next; | |
02a033df NB |
6877 | |
6878 | /* New extent overlaps with existing one */ | |
9036c102 | 6879 | em->start = start; |
70c8a91c | 6880 | em->orig_start = start; |
9036c102 | 6881 | em->len = found_key.offset - start; |
02a033df NB |
6882 | em->block_start = EXTENT_MAP_HOLE; |
6883 | goto insert; | |
9036c102 YZ |
6884 | } |
6885 | ||
280f15cb | 6886 | btrfs_extent_item_to_extent_map(inode, path, item, em); |
7ffbb598 | 6887 | |
694c12ed NB |
6888 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6889 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6890 | goto insert; |
694c12ed | 6891 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
affc5424 QW |
6892 | /* |
6893 | * Inline extent can only exist at file offset 0. This is | |
6894 | * ensured by tree-checker and inline extent creation path. | |
6895 | * Thus all members representing file offsets should be zero. | |
6896 | */ | |
affc5424 QW |
6897 | ASSERT(pg_offset == 0); |
6898 | ASSERT(extent_start == 0); | |
6899 | ASSERT(em->start == 0); | |
5f39d397 | 6900 | |
a196a894 QW |
6901 | /* |
6902 | * btrfs_extent_item_to_extent_map() should have properly | |
6903 | * initialized em members already. | |
6904 | * | |
6905 | * Other members are not utilized for inline extents. | |
6906 | */ | |
6907 | ASSERT(em->block_start == EXTENT_MAP_INLINE); | |
946c2923 | 6908 | ASSERT(em->len == fs_info->sectorsize); |
e49aabd9 | 6909 | |
a982fc82 QW |
6910 | ret = read_inline_extent(inode, path, page); |
6911 | if (ret < 0) | |
6912 | goto out; | |
a52d9a80 | 6913 | goto insert; |
a52d9a80 CM |
6914 | } |
6915 | not_found: | |
6916 | em->start = start; | |
70c8a91c | 6917 | em->orig_start = start; |
d1310b2e | 6918 | em->len = len; |
5f39d397 | 6919 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6920 | insert: |
1028d1c4 | 6921 | ret = 0; |
b3b4aa74 | 6922 | btrfs_release_path(path); |
d1310b2e | 6923 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6924 | btrfs_err(fs_info, |
5d163e0e JM |
6925 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6926 | em->start, em->len, start, len); | |
1028d1c4 | 6927 | ret = -EIO; |
a52d9a80 CM |
6928 | goto out; |
6929 | } | |
d1310b2e | 6930 | |
890871be | 6931 | write_lock(&em_tree->lock); |
1028d1c4 | 6932 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6933 | write_unlock(&em_tree->lock); |
a52d9a80 | 6934 | out: |
c6414280 | 6935 | btrfs_free_path(path); |
1abe9b8a | 6936 | |
fc4f21b1 | 6937 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6938 | |
1028d1c4 | 6939 | if (ret) { |
a52d9a80 | 6940 | free_extent_map(em); |
1028d1c4 | 6941 | return ERR_PTR(ret); |
a52d9a80 CM |
6942 | } |
6943 | return em; | |
6944 | } | |
6945 | ||
64f54188 | 6946 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
53f2c206 | 6947 | struct btrfs_dio_data *dio_data, |
5f9a8a51 FM |
6948 | const u64 start, |
6949 | const u64 len, | |
6950 | const u64 orig_start, | |
6951 | const u64 block_start, | |
6952 | const u64 block_len, | |
6953 | const u64 orig_block_len, | |
6954 | const u64 ram_bytes, | |
6955 | const int type) | |
6956 | { | |
6957 | struct extent_map *em = NULL; | |
53f2c206 | 6958 | struct btrfs_ordered_extent *ordered; |
5f9a8a51 | 6959 | |
5f9a8a51 | 6960 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
6961 | em = create_io_em(inode, start, len, orig_start, block_start, |
6962 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
6963 | BTRFS_COMPRESS_NONE, /* compress_type */ |
6964 | type); | |
5f9a8a51 FM |
6965 | if (IS_ERR(em)) |
6966 | goto out; | |
6967 | } | |
53f2c206 BB |
6968 | ordered = btrfs_alloc_ordered_extent(inode, start, len, len, |
6969 | block_start, block_len, 0, | |
6970 | (1 << type) | | |
6971 | (1 << BTRFS_ORDERED_DIRECT), | |
6972 | BTRFS_COMPRESS_NONE); | |
6973 | if (IS_ERR(ordered)) { | |
5f9a8a51 FM |
6974 | if (em) { |
6975 | free_extent_map(em); | |
4c0c8cfc FM |
6976 | btrfs_drop_extent_map_range(inode, start, |
6977 | start + len - 1, false); | |
5f9a8a51 | 6978 | } |
53f2c206 BB |
6979 | em = ERR_CAST(ordered); |
6980 | } else { | |
6981 | ASSERT(!dio_data->ordered); | |
6982 | dio_data->ordered = ordered; | |
5f9a8a51 FM |
6983 | } |
6984 | out: | |
5f9a8a51 FM |
6985 | |
6986 | return em; | |
6987 | } | |
6988 | ||
9fc6f911 | 6989 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
53f2c206 | 6990 | struct btrfs_dio_data *dio_data, |
4b46fce2 JB |
6991 | u64 start, u64 len) |
6992 | { | |
9fc6f911 NB |
6993 | struct btrfs_root *root = inode->root; |
6994 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 6995 | struct extent_map *em; |
4b46fce2 JB |
6996 | struct btrfs_key ins; |
6997 | u64 alloc_hint; | |
6998 | int ret; | |
4b46fce2 | 6999 | |
9fc6f911 | 7000 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
776a838f | 7001 | again: |
0b246afa | 7002 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7003 | 0, alloc_hint, &ins, 1, 1); |
776a838f NA |
7004 | if (ret == -EAGAIN) { |
7005 | ASSERT(btrfs_is_zoned(fs_info)); | |
7006 | wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH, | |
7007 | TASK_UNINTERRUPTIBLE); | |
7008 | goto again; | |
7009 | } | |
00361589 JB |
7010 | if (ret) |
7011 | return ERR_PTR(ret); | |
4b46fce2 | 7012 | |
53f2c206 | 7013 | em = btrfs_create_dio_extent(inode, dio_data, start, ins.offset, start, |
5f9a8a51 | 7014 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 7015 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7016 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7017 | if (IS_ERR(em)) |
9fc6f911 NB |
7018 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
7019 | 1); | |
de0ee0ed | 7020 | |
4b46fce2 JB |
7021 | return em; |
7022 | } | |
7023 | ||
f4639636 | 7024 | static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
05947ae1 AJ |
7025 | { |
7026 | struct btrfs_block_group *block_group; | |
f4639636 | 7027 | bool readonly = false; |
05947ae1 AJ |
7028 | |
7029 | block_group = btrfs_lookup_block_group(fs_info, bytenr); | |
7030 | if (!block_group || block_group->ro) | |
f4639636 | 7031 | readonly = true; |
05947ae1 AJ |
7032 | if (block_group) |
7033 | btrfs_put_block_group(block_group); | |
7034 | return readonly; | |
7035 | } | |
7036 | ||
46bfbb5c | 7037 | /* |
e4ecaf90 QW |
7038 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
7039 | * | |
7040 | * @offset: File offset | |
7041 | * @len: The length to write, will be updated to the nocow writeable | |
7042 | * range | |
7043 | * @orig_start: (optional) Return the original file offset of the file extent | |
7044 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
7045 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
7046 | * @strict: if true, omit optimizations that might force us into unnecessary |
7047 | * cow. e.g., don't trust generation number. | |
e4ecaf90 | 7048 | * |
e4ecaf90 QW |
7049 | * Return: |
7050 | * >0 and update @len if we can do nocow write | |
7051 | * 0 if we can't do nocow write | |
7052 | * <0 if error happened | |
7053 | * | |
7054 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
7055 | * any ordered extents. | |
46bfbb5c | 7056 | */ |
00361589 | 7057 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 7058 | u64 *orig_start, u64 *orig_block_len, |
26ce9114 | 7059 | u64 *ram_bytes, bool nowait, bool strict) |
46bfbb5c | 7060 | { |
2ff7e61e | 7061 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
619104ba | 7062 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
46bfbb5c CM |
7063 | struct btrfs_path *path; |
7064 | int ret; | |
7065 | struct extent_buffer *leaf; | |
7066 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7067 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7068 | struct btrfs_file_extent_item *fi; |
7069 | struct btrfs_key key; | |
46bfbb5c | 7070 | int found_type; |
e77751aa | 7071 | |
46bfbb5c CM |
7072 | path = btrfs_alloc_path(); |
7073 | if (!path) | |
7074 | return -ENOMEM; | |
26ce9114 | 7075 | path->nowait = nowait; |
46bfbb5c | 7076 | |
f85b7379 DS |
7077 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7078 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7079 | if (ret < 0) |
7080 | goto out; | |
7081 | ||
46bfbb5c | 7082 | if (ret == 1) { |
619104ba | 7083 | if (path->slots[0] == 0) { |
46bfbb5c CM |
7084 | /* can't find the item, must cow */ |
7085 | ret = 0; | |
7086 | goto out; | |
7087 | } | |
619104ba | 7088 | path->slots[0]--; |
46bfbb5c CM |
7089 | } |
7090 | ret = 0; | |
7091 | leaf = path->nodes[0]; | |
619104ba | 7092 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
4a0cc7ca | 7093 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7094 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7095 | /* not our file or wrong item type, must cow */ | |
7096 | goto out; | |
7097 | } | |
7098 | ||
7099 | if (key.offset > offset) { | |
7100 | /* Wrong offset, must cow */ | |
7101 | goto out; | |
7102 | } | |
7103 | ||
619104ba | 7104 | if (btrfs_file_extent_end(path) <= offset) |
7ee9e440 JB |
7105 | goto out; |
7106 | ||
619104ba FM |
7107 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); |
7108 | found_type = btrfs_file_extent_type(leaf, fi); | |
7109 | if (ram_bytes) | |
7110 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
e77751aa | 7111 | |
619104ba FM |
7112 | nocow_args.start = offset; |
7113 | nocow_args.end = offset + *len - 1; | |
7114 | nocow_args.strict = strict; | |
7115 | nocow_args.free_path = true; | |
7ee9e440 | 7116 | |
619104ba FM |
7117 | ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args); |
7118 | /* can_nocow_file_extent() has freed the path. */ | |
7119 | path = NULL; | |
7ee9e440 | 7120 | |
619104ba FM |
7121 | if (ret != 1) { |
7122 | /* Treat errors as not being able to NOCOW. */ | |
7123 | ret = 0; | |
78d4295b | 7124 | goto out; |
7ee9e440 | 7125 | } |
eb384b55 | 7126 | |
619104ba FM |
7127 | ret = 0; |
7128 | if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr)) | |
46bfbb5c | 7129 | goto out; |
7b2b7085 | 7130 | |
619104ba FM |
7131 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
7132 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7b2b7085 MX |
7133 | u64 range_end; |
7134 | ||
619104ba | 7135 | range_end = round_up(offset + nocow_args.num_bytes, |
da17066c | 7136 | root->fs_info->sectorsize) - 1; |
99be1a66 | 7137 | ret = test_range_bit_exists(io_tree, offset, range_end, EXTENT_DELALLOC); |
7b2b7085 MX |
7138 | if (ret) { |
7139 | ret = -EAGAIN; | |
7140 | goto out; | |
7141 | } | |
7142 | } | |
7143 | ||
619104ba FM |
7144 | if (orig_start) |
7145 | *orig_start = key.offset - nocow_args.extent_offset; | |
7146 | if (orig_block_len) | |
7147 | *orig_block_len = nocow_args.disk_num_bytes; | |
00361589 | 7148 | |
619104ba | 7149 | *len = nocow_args.num_bytes; |
46bfbb5c CM |
7150 | ret = 1; |
7151 | out: | |
7152 | btrfs_free_path(path); | |
7153 | return ret; | |
7154 | } | |
7155 | ||
eb838e73 | 7156 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
59094403 FM |
7157 | struct extent_state **cached_state, |
7158 | unsigned int iomap_flags) | |
eb838e73 | 7159 | { |
59094403 FM |
7160 | const bool writing = (iomap_flags & IOMAP_WRITE); |
7161 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); | |
7162 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
eb838e73 JB |
7163 | struct btrfs_ordered_extent *ordered; |
7164 | int ret = 0; | |
7165 | ||
7166 | while (1) { | |
59094403 | 7167 | if (nowait) { |
83ae4133 JB |
7168 | if (!try_lock_extent(io_tree, lockstart, lockend, |
7169 | cached_state)) | |
59094403 FM |
7170 | return -EAGAIN; |
7171 | } else { | |
570eb97b | 7172 | lock_extent(io_tree, lockstart, lockend, cached_state); |
59094403 | 7173 | } |
eb838e73 JB |
7174 | /* |
7175 | * We're concerned with the entire range that we're going to be | |
01327610 | 7176 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7177 | * extents in this range. |
7178 | */ | |
a776c6fa | 7179 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7180 | lockend - lockstart + 1); |
7181 | ||
7182 | /* | |
7183 | * We need to make sure there are no buffered pages in this | |
7184 | * range either, we could have raced between the invalidate in | |
7185 | * generic_file_direct_write and locking the extent. The | |
7186 | * invalidate needs to happen so that reads after a write do not | |
7187 | * get stale data. | |
7188 | */ | |
fc4adbff | 7189 | if (!ordered && |
051c98eb DS |
7190 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7191 | lockstart, lockend))) | |
eb838e73 JB |
7192 | break; |
7193 | ||
570eb97b | 7194 | unlock_extent(io_tree, lockstart, lockend, cached_state); |
eb838e73 JB |
7195 | |
7196 | if (ordered) { | |
59094403 FM |
7197 | if (nowait) { |
7198 | btrfs_put_ordered_extent(ordered); | |
7199 | ret = -EAGAIN; | |
7200 | break; | |
7201 | } | |
ade77029 FM |
7202 | /* |
7203 | * If we are doing a DIO read and the ordered extent we | |
7204 | * found is for a buffered write, we can not wait for it | |
7205 | * to complete and retry, because if we do so we can | |
7206 | * deadlock with concurrent buffered writes on page | |
7207 | * locks. This happens only if our DIO read covers more | |
7208 | * than one extent map, if at this point has already | |
7209 | * created an ordered extent for a previous extent map | |
7210 | * and locked its range in the inode's io tree, and a | |
7211 | * concurrent write against that previous extent map's | |
7212 | * range and this range started (we unlock the ranges | |
7213 | * in the io tree only when the bios complete and | |
7214 | * buffered writes always lock pages before attempting | |
7215 | * to lock range in the io tree). | |
7216 | */ | |
7217 | if (writing || | |
7218 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
36d45567 | 7219 | btrfs_start_ordered_extent(ordered); |
ade77029 | 7220 | else |
59094403 | 7221 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7222 | btrfs_put_ordered_extent(ordered); |
7223 | } else { | |
eb838e73 | 7224 | /* |
b850ae14 FM |
7225 | * We could trigger writeback for this range (and wait |
7226 | * for it to complete) and then invalidate the pages for | |
7227 | * this range (through invalidate_inode_pages2_range()), | |
7228 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7229 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7230 | * triggered a readahead) on a page lock due to an |
7231 | * ordered dio extent we created before but did not have | |
7232 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7233 | * complete), which makes readahead wait for that |
b850ae14 FM |
7234 | * ordered extent to complete while holding a lock on |
7235 | * that page. | |
eb838e73 | 7236 | */ |
59094403 | 7237 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7238 | } |
7239 | ||
ade77029 FM |
7240 | if (ret) |
7241 | break; | |
7242 | ||
eb838e73 JB |
7243 | cond_resched(); |
7244 | } | |
7245 | ||
7246 | return ret; | |
7247 | } | |
7248 | ||
6f9994db | 7249 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7250 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7251 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7252 | u64 block_len, u64 orig_block_len, |
7253 | u64 ram_bytes, int compress_type, | |
7254 | int type) | |
69ffb543 | 7255 | { |
69ffb543 | 7256 | struct extent_map *em; |
69ffb543 JB |
7257 | int ret; |
7258 | ||
6f9994db LB |
7259 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7260 | type == BTRFS_ORDERED_COMPRESSED || | |
7261 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7262 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7263 | |
69ffb543 JB |
7264 | em = alloc_extent_map(); |
7265 | if (!em) | |
7266 | return ERR_PTR(-ENOMEM); | |
7267 | ||
7268 | em->start = start; | |
7269 | em->orig_start = orig_start; | |
7270 | em->len = len; | |
7271 | em->block_len = block_len; | |
7272 | em->block_start = block_start; | |
b4939680 | 7273 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7274 | em->ram_bytes = ram_bytes; |
70c8a91c | 7275 | em->generation = -1; |
f86f7a75 FM |
7276 | em->flags |= EXTENT_FLAG_PINNED; |
7277 | if (type == BTRFS_ORDERED_PREALLOC) | |
7278 | em->flags |= EXTENT_FLAG_FILLING; | |
7279 | else if (type == BTRFS_ORDERED_COMPRESSED) | |
7280 | extent_map_set_compression(em, compress_type); | |
69ffb543 | 7281 | |
a1ba4c08 | 7282 | ret = btrfs_replace_extent_map_range(inode, em, true); |
69ffb543 JB |
7283 | if (ret) { |
7284 | free_extent_map(em); | |
7285 | return ERR_PTR(ret); | |
7286 | } | |
7287 | ||
6f9994db | 7288 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7289 | return em; |
7290 | } | |
7291 | ||
1c8d0175 | 7292 | |
c5794e51 | 7293 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7294 | struct inode *inode, |
7295 | struct btrfs_dio_data *dio_data, | |
7833b865 | 7296 | u64 start, u64 *lenp, |
d7a8ab4e | 7297 | unsigned int iomap_flags) |
c5794e51 | 7298 | { |
d4135134 | 7299 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); |
c5794e51 NB |
7300 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7301 | struct extent_map *em = *map; | |
f0bfa76a FM |
7302 | int type; |
7303 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
2306e83e | 7304 | struct btrfs_block_group *bg; |
f0bfa76a FM |
7305 | bool can_nocow = false; |
7306 | bool space_reserved = false; | |
7833b865 | 7307 | u64 len = *lenp; |
6d82ad13 | 7308 | u64 prev_len; |
c5794e51 NB |
7309 | int ret = 0; |
7310 | ||
7311 | /* | |
7312 | * We don't allocate a new extent in the following cases | |
7313 | * | |
7314 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7315 | * existing extent. | |
7316 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7317 | * just use the extent. | |
7318 | * | |
7319 | */ | |
f86f7a75 | 7320 | if ((em->flags & EXTENT_FLAG_PREALLOC) || |
c5794e51 NB |
7321 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
7322 | em->block_start != EXTENT_MAP_HOLE)) { | |
f86f7a75 | 7323 | if (em->flags & EXTENT_FLAG_PREALLOC) |
c5794e51 NB |
7324 | type = BTRFS_ORDERED_PREALLOC; |
7325 | else | |
7326 | type = BTRFS_ORDERED_NOCOW; | |
7327 | len = min(len, em->len - (start - em->start)); | |
7328 | block_start = em->block_start + (start - em->start); | |
7329 | ||
7330 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
26ce9114 | 7331 | &orig_block_len, &ram_bytes, false, false) == 1) { |
2306e83e FM |
7332 | bg = btrfs_inc_nocow_writers(fs_info, block_start); |
7333 | if (bg) | |
7334 | can_nocow = true; | |
7335 | } | |
f0bfa76a | 7336 | } |
c5794e51 | 7337 | |
6d82ad13 | 7338 | prev_len = len; |
f0bfa76a FM |
7339 | if (can_nocow) { |
7340 | struct extent_map *em2; | |
7341 | ||
7342 | /* We can NOCOW, so only need to reserve metadata space. */ | |
d4135134 FM |
7343 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, |
7344 | nowait); | |
f0bfa76a FM |
7345 | if (ret < 0) { |
7346 | /* Our caller expects us to free the input extent map. */ | |
7347 | free_extent_map(em); | |
7348 | *map = NULL; | |
2306e83e | 7349 | btrfs_dec_nocow_writers(bg); |
d4135134 FM |
7350 | if (nowait && (ret == -ENOSPC || ret == -EDQUOT)) |
7351 | ret = -EAGAIN; | |
f0bfa76a FM |
7352 | goto out; |
7353 | } | |
7354 | space_reserved = true; | |
7355 | ||
53f2c206 | 7356 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), dio_data, start, len, |
f0bfa76a FM |
7357 | orig_start, block_start, |
7358 | len, orig_block_len, | |
7359 | ram_bytes, type); | |
2306e83e | 7360 | btrfs_dec_nocow_writers(bg); |
f0bfa76a FM |
7361 | if (type == BTRFS_ORDERED_PREALLOC) { |
7362 | free_extent_map(em); | |
c1867eb3 DS |
7363 | *map = em2; |
7364 | em = em2; | |
f0bfa76a | 7365 | } |
c5794e51 | 7366 | |
f0bfa76a FM |
7367 | if (IS_ERR(em2)) { |
7368 | ret = PTR_ERR(em2); | |
7369 | goto out; | |
c5794e51 | 7370 | } |
f5585f4f FM |
7371 | |
7372 | dio_data->nocow_done = true; | |
f0bfa76a | 7373 | } else { |
f0bfa76a FM |
7374 | /* Our caller expects us to free the input extent map. */ |
7375 | free_extent_map(em); | |
7376 | *map = NULL; | |
7377 | ||
7833b865 CH |
7378 | if (nowait) { |
7379 | ret = -EAGAIN; | |
7380 | goto out; | |
7381 | } | |
d7a8ab4e | 7382 | |
f5585f4f FM |
7383 | /* |
7384 | * If we could not allocate data space before locking the file | |
7385 | * range and we can't do a NOCOW write, then we have to fail. | |
7386 | */ | |
7833b865 CH |
7387 | if (!dio_data->data_space_reserved) { |
7388 | ret = -ENOSPC; | |
7389 | goto out; | |
7390 | } | |
f5585f4f FM |
7391 | |
7392 | /* | |
7393 | * We have to COW and we have already reserved data space before, | |
7394 | * so now we reserve only metadata. | |
7395 | */ | |
7396 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, | |
7397 | false); | |
f0bfa76a FM |
7398 | if (ret < 0) |
7399 | goto out; | |
7400 | space_reserved = true; | |
7401 | ||
53f2c206 | 7402 | em = btrfs_new_extent_direct(BTRFS_I(inode), dio_data, start, len); |
f0bfa76a FM |
7403 | if (IS_ERR(em)) { |
7404 | ret = PTR_ERR(em); | |
7405 | goto out; | |
7406 | } | |
7407 | *map = em; | |
7408 | len = min(len, em->len - (start - em->start)); | |
7409 | if (len < prev_len) | |
f5585f4f FM |
7410 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
7411 | prev_len - len, true); | |
c5794e51 NB |
7412 | } |
7413 | ||
f0bfa76a FM |
7414 | /* |
7415 | * We have created our ordered extent, so we can now release our reservation | |
7416 | * for an outstanding extent. | |
7417 | */ | |
6d82ad13 | 7418 | btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len); |
c5794e51 | 7419 | |
c5794e51 NB |
7420 | /* |
7421 | * Need to update the i_size under the extent lock so buffered | |
7422 | * readers will get the updated i_size when we unlock. | |
7423 | */ | |
f85781fb | 7424 | if (start + len > i_size_read(inode)) |
c5794e51 | 7425 | i_size_write(inode, start + len); |
c5794e51 | 7426 | out: |
f0bfa76a FM |
7427 | if (ret && space_reserved) { |
7428 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); | |
f5585f4f | 7429 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true); |
f0bfa76a | 7430 | } |
7833b865 | 7431 | *lenp = len; |
c5794e51 NB |
7432 | return ret; |
7433 | } | |
7434 | ||
f85781fb GR |
7435 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7436 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7437 | struct iomap *srcmap) | |
4b46fce2 | 7438 | { |
491a6d01 | 7439 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
0b246afa | 7440 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7441 | struct extent_map *em; |
eb838e73 | 7442 | struct extent_state *cached_state = NULL; |
491a6d01 | 7443 | struct btrfs_dio_data *dio_data = iter->private; |
eb838e73 | 7444 | u64 lockstart, lockend; |
f85781fb | 7445 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7446 | int ret = 0; |
f85781fb | 7447 | u64 len = length; |
f5585f4f | 7448 | const u64 data_alloc_len = length; |
f85781fb | 7449 | bool unlock_extents = false; |
eb838e73 | 7450 | |
79d3d1d1 JB |
7451 | /* |
7452 | * We could potentially fault if we have a buffer > PAGE_SIZE, and if | |
7453 | * we're NOWAIT we may submit a bio for a partial range and return | |
7454 | * EIOCBQUEUED, which would result in an errant short read. | |
7455 | * | |
7456 | * The best way to handle this would be to allow for partial completions | |
7457 | * of iocb's, so we could submit the partial bio, return and fault in | |
7458 | * the rest of the pages, and then submit the io for the rest of the | |
7459 | * range. However we don't have that currently, so simply return | |
7460 | * -EAGAIN at this point so that the normal path is used. | |
7461 | */ | |
7462 | if (!write && (flags & IOMAP_NOWAIT) && length > PAGE_SIZE) | |
7463 | return -EAGAIN; | |
7464 | ||
ee5b46a3 CH |
7465 | /* |
7466 | * Cap the size of reads to that usually seen in buffered I/O as we need | |
7467 | * to allocate a contiguous array for the checksums. | |
7468 | */ | |
f85781fb | 7469 | if (!write) |
ee5b46a3 | 7470 | len = min_t(u64, len, fs_info->sectorsize * BTRFS_MAX_BIO_SECTORS); |
eb838e73 | 7471 | |
c329861d JB |
7472 | lockstart = start; |
7473 | lockend = start + len - 1; | |
7474 | ||
f85781fb | 7475 | /* |
b023e675 FM |
7476 | * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't |
7477 | * enough if we've written compressed pages to this area, so we need to | |
7478 | * flush the dirty pages again to make absolutely sure that any | |
7479 | * outstanding dirty pages are on disk - the first flush only starts | |
7480 | * compression on the data, while keeping the pages locked, so by the | |
7481 | * time the second flush returns we know bios for the compressed pages | |
7482 | * were submitted and finished, and the pages no longer under writeback. | |
7483 | * | |
7484 | * If we have a NOWAIT request and we have any pages in the range that | |
7485 | * are locked, likely due to compression still in progress, we don't want | |
7486 | * to block on page locks. We also don't want to block on pages marked as | |
7487 | * dirty or under writeback (same as for the non-compression case). | |
7488 | * iomap_dio_rw() did the same check, but after that and before we got | |
7489 | * here, mmap'ed writes may have happened or buffered reads started | |
7490 | * (readpage() and readahead(), which lock pages), as we haven't locked | |
7491 | * the file range yet. | |
f85781fb GR |
7492 | */ |
7493 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7494 | &BTRFS_I(inode)->runtime_flags)) { | |
b023e675 FM |
7495 | if (flags & IOMAP_NOWAIT) { |
7496 | if (filemap_range_needs_writeback(inode->i_mapping, | |
7497 | lockstart, lockend)) | |
7498 | return -EAGAIN; | |
7499 | } else { | |
7500 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7501 | start + length - 1); | |
7502 | if (ret) | |
7503 | return ret; | |
7504 | } | |
f85781fb GR |
7505 | } |
7506 | ||
491a6d01 | 7507 | memset(dio_data, 0, sizeof(*dio_data)); |
f85781fb | 7508 | |
f5585f4f FM |
7509 | /* |
7510 | * We always try to allocate data space and must do it before locking | |
7511 | * the file range, to avoid deadlocks with concurrent writes to the same | |
7512 | * range if the range has several extents and the writes don't expand the | |
7513 | * current i_size (the inode lock is taken in shared mode). If we fail to | |
7514 | * allocate data space here we continue and later, after locking the | |
7515 | * file range, we fail with ENOSPC only if we figure out we can not do a | |
7516 | * NOCOW write. | |
7517 | */ | |
7518 | if (write && !(flags & IOMAP_NOWAIT)) { | |
7519 | ret = btrfs_check_data_free_space(BTRFS_I(inode), | |
7520 | &dio_data->data_reserved, | |
1daedb1d | 7521 | start, data_alloc_len, false); |
f5585f4f FM |
7522 | if (!ret) |
7523 | dio_data->data_space_reserved = true; | |
7524 | else if (ret && !(BTRFS_I(inode)->flags & | |
7525 | (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) | |
7526 | goto err; | |
7527 | } | |
e1cbbfa5 | 7528 | |
eb838e73 JB |
7529 | /* |
7530 | * If this errors out it's because we couldn't invalidate pagecache for | |
59094403 FM |
7531 | * this range and we need to fallback to buffered IO, or we are doing a |
7532 | * NOWAIT read/write and we need to block. | |
eb838e73 | 7533 | */ |
59094403 FM |
7534 | ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags); |
7535 | if (ret < 0) | |
9c9464cc | 7536 | goto err; |
eb838e73 | 7537 | |
39b07b5d | 7538 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7539 | if (IS_ERR(em)) { |
7540 | ret = PTR_ERR(em); | |
7541 | goto unlock_err; | |
7542 | } | |
4b46fce2 JB |
7543 | |
7544 | /* | |
7545 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7546 | * io. INLINE is special, and we could probably kludge it in here, but | |
7547 | * it's still buffered so for safety lets just fall back to the generic | |
7548 | * buffered path. | |
7549 | * | |
7550 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7551 | * decompress it, so there will be buffering required no matter what we | |
7552 | * do, so go ahead and fallback to buffered. | |
7553 | * | |
01327610 | 7554 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7555 | * to buffered IO. Don't blame me, this is the price we pay for using |
7556 | * the generic code. | |
7557 | */ | |
f86f7a75 | 7558 | if (extent_map_is_compressed(em) || |
4b46fce2 JB |
7559 | em->block_start == EXTENT_MAP_INLINE) { |
7560 | free_extent_map(em); | |
a4527e18 FM |
7561 | /* |
7562 | * If we are in a NOWAIT context, return -EAGAIN in order to | |
7563 | * fallback to buffered IO. This is not only because we can | |
7564 | * block with buffered IO (no support for NOWAIT semantics at | |
7565 | * the moment) but also to avoid returning short reads to user | |
7566 | * space - this happens if we were able to read some data from | |
7567 | * previous non-compressed extents and then when we fallback to | |
7568 | * buffered IO, at btrfs_file_read_iter() by calling | |
7569 | * filemap_read(), we fail to fault in pages for the read buffer, | |
7570 | * in which case filemap_read() returns a short read (the number | |
7571 | * of bytes previously read is > 0, so it does not return -EFAULT). | |
7572 | */ | |
7573 | ret = (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK; | |
eb838e73 | 7574 | goto unlock_err; |
4b46fce2 JB |
7575 | } |
7576 | ||
f85781fb | 7577 | len = min(len, em->len - (start - em->start)); |
ca93e44b FM |
7578 | |
7579 | /* | |
7580 | * If we have a NOWAIT request and the range contains multiple extents | |
7581 | * (or a mix of extents and holes), then we return -EAGAIN to make the | |
7582 | * caller fallback to a context where it can do a blocking (without | |
7583 | * NOWAIT) request. This way we avoid doing partial IO and returning | |
7584 | * success to the caller, which is not optimal for writes and for reads | |
7585 | * it can result in unexpected behaviour for an application. | |
7586 | * | |
7587 | * When doing a read, because we use IOMAP_DIO_PARTIAL when calling | |
7588 | * iomap_dio_rw(), we can end up returning less data then what the caller | |
7589 | * asked for, resulting in an unexpected, and incorrect, short read. | |
7590 | * That is, the caller asked to read N bytes and we return less than that, | |
7591 | * which is wrong unless we are crossing EOF. This happens if we get a | |
7592 | * page fault error when trying to fault in pages for the buffer that is | |
7593 | * associated to the struct iov_iter passed to iomap_dio_rw(), and we | |
7594 | * have previously submitted bios for other extents in the range, in | |
7595 | * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of | |
7596 | * those bios have completed by the time we get the page fault error, | |
7597 | * which we return back to our caller - we should only return EIOCBQUEUED | |
7598 | * after we have submitted bios for all the extents in the range. | |
7599 | */ | |
7600 | if ((flags & IOMAP_NOWAIT) && len < length) { | |
7601 | free_extent_map(em); | |
7602 | ret = -EAGAIN; | |
7603 | goto unlock_err; | |
7604 | } | |
7605 | ||
f85781fb GR |
7606 | if (write) { |
7607 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
7833b865 | 7608 | start, &len, flags); |
c5794e51 NB |
7609 | if (ret < 0) |
7610 | goto unlock_err; | |
f85781fb GR |
7611 | unlock_extents = true; |
7612 | /* Recalc len in case the new em is smaller than requested */ | |
7613 | len = min(len, em->len - (start - em->start)); | |
f5585f4f FM |
7614 | if (dio_data->data_space_reserved) { |
7615 | u64 release_offset; | |
7616 | u64 release_len = 0; | |
7617 | ||
7618 | if (dio_data->nocow_done) { | |
7619 | release_offset = start; | |
7620 | release_len = data_alloc_len; | |
7621 | } else if (len < data_alloc_len) { | |
7622 | release_offset = start + len; | |
7623 | release_len = data_alloc_len - len; | |
7624 | } | |
7625 | ||
7626 | if (release_len > 0) | |
7627 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7628 | dio_data->data_reserved, | |
7629 | release_offset, | |
7630 | release_len); | |
7631 | } | |
c5794e51 | 7632 | } else { |
1c8d0175 NB |
7633 | /* |
7634 | * We need to unlock only the end area that we aren't using. | |
7635 | * The rest is going to be unlocked by the endio routine. | |
7636 | */ | |
f85781fb GR |
7637 | lockstart = start + len; |
7638 | if (lockstart < lockend) | |
7639 | unlock_extents = true; | |
7640 | } | |
7641 | ||
7642 | if (unlock_extents) | |
570eb97b JB |
7643 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7644 | &cached_state); | |
f85781fb GR |
7645 | else |
7646 | free_extent_state(cached_state); | |
7647 | ||
7648 | /* | |
7649 | * Translate extent map information to iomap. | |
7650 | * We trim the extents (and move the addr) even though iomap code does | |
7651 | * that, since we have locked only the parts we are performing I/O in. | |
7652 | */ | |
7653 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
f86f7a75 | 7654 | ((em->flags & EXTENT_FLAG_PREALLOC) && !write)) { |
f85781fb GR |
7655 | iomap->addr = IOMAP_NULL_ADDR; |
7656 | iomap->type = IOMAP_HOLE; | |
7657 | } else { | |
7658 | iomap->addr = em->block_start + (start - em->start); | |
7659 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7660 | } |
f85781fb | 7661 | iomap->offset = start; |
d24fa5c1 | 7662 | iomap->bdev = fs_info->fs_devices->latest_dev->bdev; |
f85781fb | 7663 | iomap->length = len; |
4b46fce2 JB |
7664 | free_extent_map(em); |
7665 | ||
7666 | return 0; | |
eb838e73 JB |
7667 | |
7668 | unlock_err: | |
570eb97b JB |
7669 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7670 | &cached_state); | |
9c9464cc | 7671 | err: |
f5585f4f FM |
7672 | if (dio_data->data_space_reserved) { |
7673 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7674 | dio_data->data_reserved, | |
7675 | start, data_alloc_len); | |
7676 | extent_changeset_free(dio_data->data_reserved); | |
7677 | } | |
7678 | ||
f85781fb GR |
7679 | return ret; |
7680 | } | |
7681 | ||
7682 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7683 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7684 | { | |
491a6d01 CH |
7685 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
7686 | struct btrfs_dio_data *dio_data = iter->private; | |
f85781fb GR |
7687 | size_t submitted = dio_data->submitted; |
7688 | const bool write = !!(flags & IOMAP_WRITE); | |
491a6d01 | 7689 | int ret = 0; |
f85781fb GR |
7690 | |
7691 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7692 | /* If reading from a hole, unlock and return */ | |
570eb97b JB |
7693 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1, |
7694 | NULL); | |
491a6d01 | 7695 | return 0; |
f85781fb GR |
7696 | } |
7697 | ||
7698 | if (submitted < length) { | |
7699 | pos += submitted; | |
7700 | length -= submitted; | |
7701 | if (write) | |
b41b6f69 CH |
7702 | btrfs_finish_ordered_extent(dio_data->ordered, NULL, |
7703 | pos, length, false); | |
f85781fb GR |
7704 | else |
7705 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
570eb97b | 7706 | pos + length - 1, NULL); |
f85781fb GR |
7707 | ret = -ENOTBLK; |
7708 | } | |
53f2c206 BB |
7709 | if (write) { |
7710 | btrfs_put_ordered_extent(dio_data->ordered); | |
7711 | dio_data->ordered = NULL; | |
7712 | } | |
f85781fb | 7713 | |
f0bfa76a | 7714 | if (write) |
f85781fb | 7715 | extent_changeset_free(dio_data->data_reserved); |
8b110e39 MX |
7716 | return ret; |
7717 | } | |
7718 | ||
67d66982 | 7719 | static void btrfs_dio_end_io(struct btrfs_bio *bbio) |
8b110e39 | 7720 | { |
67d66982 CH |
7721 | struct btrfs_dio_private *dip = |
7722 | container_of(bbio, struct btrfs_dio_private, bbio); | |
7723 | struct btrfs_inode *inode = bbio->inode; | |
917f32a2 | 7724 | struct bio *bio = &bbio->bio; |
e65e1535 | 7725 | |
67d66982 CH |
7726 | if (bio->bi_status) { |
7727 | btrfs_warn(inode->root->fs_info, | |
7728 | "direct IO failed ino %llu op 0x%0x offset %#llx len %u err no %d", | |
7729 | btrfs_ino(inode), bio->bi_opf, | |
7730 | dip->file_offset, dip->bytes, bio->bi_status); | |
7609afac | 7731 | } |
1ae39938 | 7732 | |
b41b6f69 CH |
7733 | if (btrfs_op(bio) == BTRFS_MAP_WRITE) { |
7734 | btrfs_finish_ordered_extent(bbio->ordered, NULL, | |
7735 | dip->file_offset, dip->bytes, | |
7736 | !bio->bi_status); | |
7737 | } else { | |
67d66982 CH |
7738 | unlock_extent(&inode->io_tree, dip->file_offset, |
7739 | dip->file_offset + dip->bytes - 1, NULL); | |
b41b6f69 | 7740 | } |
ea1f0ced | 7741 | |
67d66982 CH |
7742 | bbio->bio.bi_private = bbio->private; |
7743 | iomap_dio_bio_end_io(bio); | |
e65e1535 MX |
7744 | } |
7745 | ||
67d66982 CH |
7746 | static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio, |
7747 | loff_t file_offset) | |
c36cac28 | 7748 | { |
67d66982 | 7749 | struct btrfs_bio *bbio = btrfs_bio(bio); |
642c5d34 | 7750 | struct btrfs_dio_private *dip = |
67d66982 | 7751 | container_of(bbio, struct btrfs_dio_private, bbio); |
491a6d01 | 7752 | struct btrfs_dio_data *dio_data = iter->private; |
544d24f9 | 7753 | |
4317ff00 QW |
7754 | btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info, |
7755 | btrfs_dio_end_io, bio->bi_private); | |
7756 | bbio->inode = BTRFS_I(iter->inode); | |
67d66982 | 7757 | bbio->file_offset = file_offset; |
e65e1535 | 7758 | |
67d66982 CH |
7759 | dip->file_offset = file_offset; |
7760 | dip->bytes = bio->bi_iter.bi_size; | |
e65e1535 | 7761 | |
67d66982 | 7762 | dio_data->submitted += bio->bi_iter.bi_size; |
b73a6fd1 BB |
7763 | |
7764 | /* | |
7765 | * Check if we are doing a partial write. If we are, we need to split | |
7766 | * the ordered extent to match the submitted bio. Hang on to the | |
7767 | * remaining unfinishable ordered_extent in dio_data so that it can be | |
7768 | * cancelled in iomap_end to avoid a deadlock wherein faulting the | |
7769 | * remaining pages is blocked on the outstanding ordered extent. | |
7770 | */ | |
7771 | if (iter->flags & IOMAP_WRITE) { | |
7772 | int ret; | |
7773 | ||
7774 | ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered); | |
7775 | if (ret) { | |
7cad645e CH |
7776 | btrfs_finish_ordered_extent(dio_data->ordered, NULL, |
7777 | file_offset, dip->bytes, | |
7778 | !ret); | |
7779 | bio->bi_status = errno_to_blk_status(ret); | |
7780 | iomap_dio_bio_end_io(bio); | |
b73a6fd1 BB |
7781 | return; |
7782 | } | |
7783 | } | |
7784 | ||
ae42a154 | 7785 | btrfs_submit_bio(bbio, 0); |
4b46fce2 JB |
7786 | } |
7787 | ||
36e8c622 | 7788 | static const struct iomap_ops btrfs_dio_iomap_ops = { |
f85781fb GR |
7789 | .iomap_begin = btrfs_dio_iomap_begin, |
7790 | .iomap_end = btrfs_dio_iomap_end, | |
7791 | }; | |
7792 | ||
36e8c622 | 7793 | static const struct iomap_dio_ops btrfs_dio_ops = { |
67d66982 | 7794 | .submit_io = btrfs_dio_submit_io, |
642c5d34 | 7795 | .bio_set = &btrfs_dio_bioset, |
f85781fb GR |
7796 | }; |
7797 | ||
8184620a | 7798 | ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter, size_t done_before) |
36e8c622 | 7799 | { |
53f2c206 | 7800 | struct btrfs_dio_data data = { 0 }; |
491a6d01 | 7801 | |
36e8c622 | 7802 | return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, |
8184620a FM |
7803 | IOMAP_DIO_PARTIAL, &data, done_before); |
7804 | } | |
7805 | ||
7806 | struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter, | |
7807 | size_t done_before) | |
7808 | { | |
53f2c206 | 7809 | struct btrfs_dio_data data = { 0 }; |
8184620a FM |
7810 | |
7811 | return __iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, | |
7812 | IOMAP_DIO_PARTIAL, &data, done_before); | |
36e8c622 CH |
7813 | } |
7814 | ||
1506fcc8 | 7815 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 7816 | u64 start, u64 len) |
1506fcc8 | 7817 | { |
05dadc09 TI |
7818 | int ret; |
7819 | ||
45dd052e | 7820 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
7821 | if (ret) |
7822 | return ret; | |
7823 | ||
33a86cfa FM |
7824 | /* |
7825 | * fiemap_prep() called filemap_write_and_wait() for the whole possible | |
7826 | * file range (0 to LLONG_MAX), but that is not enough if we have | |
7827 | * compression enabled. The first filemap_fdatawrite_range() only kicks | |
7828 | * in the compression of data (in an async thread) and will return | |
7829 | * before the compression is done and writeback is started. A second | |
7830 | * filemap_fdatawrite_range() is needed to wait for the compression to | |
ac3c0d36 FM |
7831 | * complete and writeback to start. We also need to wait for ordered |
7832 | * extents to complete, because our fiemap implementation uses mainly | |
7833 | * file extent items to list the extents, searching for extent maps | |
7834 | * only for file ranges with holes or prealloc extents to figure out | |
7835 | * if we have delalloc in those ranges. | |
33a86cfa FM |
7836 | */ |
7837 | if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) { | |
ac3c0d36 | 7838 | ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX); |
33a86cfa FM |
7839 | if (ret) |
7840 | return ret; | |
7841 | } | |
7842 | ||
facee0a0 | 7843 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
1506fcc8 YS |
7844 | } |
7845 | ||
48a3b636 ES |
7846 | static int btrfs_writepages(struct address_space *mapping, |
7847 | struct writeback_control *wbc) | |
b293f02e | 7848 | { |
8ae225a8 | 7849 | return extent_writepages(mapping, wbc); |
b293f02e CM |
7850 | } |
7851 | ||
ba206a02 | 7852 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 7853 | { |
ba206a02 | 7854 | extent_readahead(rac); |
3ab2fb5a | 7855 | } |
2a3ff0ad | 7856 | |
7c11d0ae | 7857 | /* |
f913cff3 | 7858 | * For release_folio() and invalidate_folio() we have a race window where |
895586eb | 7859 | * folio_end_writeback() is called but the subpage spinlock is not yet released. |
7c11d0ae QW |
7860 | * If we continue to release/invalidate the page, we could cause use-after-free |
7861 | * for subpage spinlock. So this function is to spin and wait for subpage | |
7862 | * spinlock. | |
7863 | */ | |
7864 | static void wait_subpage_spinlock(struct page *page) | |
7865 | { | |
7866 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
cfbf07e2 | 7867 | struct folio *folio = page_folio(page); |
7c11d0ae QW |
7868 | struct btrfs_subpage *subpage; |
7869 | ||
fbca46eb | 7870 | if (!btrfs_is_subpage(fs_info, page)) |
7c11d0ae QW |
7871 | return; |
7872 | ||
cfbf07e2 QW |
7873 | ASSERT(folio_test_private(folio) && folio_get_private(folio)); |
7874 | subpage = folio_get_private(folio); | |
7c11d0ae QW |
7875 | |
7876 | /* | |
7877 | * This may look insane as we just acquire the spinlock and release it, | |
7878 | * without doing anything. But we just want to make sure no one is | |
7879 | * still holding the subpage spinlock. | |
7880 | * And since the page is not dirty nor writeback, and we have page | |
7881 | * locked, the only possible way to hold a spinlock is from the endio | |
7882 | * function to clear page writeback. | |
7883 | * | |
7884 | * Here we just acquire the spinlock so that all existing callers | |
7885 | * should exit and we're safe to release/invalidate the page. | |
7886 | */ | |
7887 | spin_lock_irq(&subpage->lock); | |
7888 | spin_unlock_irq(&subpage->lock); | |
7889 | } | |
7890 | ||
f913cff3 | 7891 | static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
9ebefb18 | 7892 | { |
f913cff3 | 7893 | int ret = try_release_extent_mapping(&folio->page, gfp_flags); |
7c11d0ae QW |
7894 | |
7895 | if (ret == 1) { | |
f913cff3 MWO |
7896 | wait_subpage_spinlock(&folio->page); |
7897 | clear_page_extent_mapped(&folio->page); | |
7c11d0ae | 7898 | } |
a52d9a80 | 7899 | return ret; |
39279cc3 CM |
7900 | } |
7901 | ||
f913cff3 | 7902 | static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
e6dcd2dc | 7903 | { |
f913cff3 MWO |
7904 | if (folio_test_writeback(folio) || folio_test_dirty(folio)) |
7905 | return false; | |
7906 | return __btrfs_release_folio(folio, gfp_flags); | |
e6dcd2dc CM |
7907 | } |
7908 | ||
f8e66081 | 7909 | #ifdef CONFIG_MIGRATION |
e7a60a17 MWO |
7910 | static int btrfs_migrate_folio(struct address_space *mapping, |
7911 | struct folio *dst, struct folio *src, | |
f8e66081 RG |
7912 | enum migrate_mode mode) |
7913 | { | |
e7a60a17 | 7914 | int ret = filemap_migrate_folio(mapping, dst, src, mode); |
f8e66081 | 7915 | |
f8e66081 RG |
7916 | if (ret != MIGRATEPAGE_SUCCESS) |
7917 | return ret; | |
7918 | ||
e7a60a17 MWO |
7919 | if (folio_test_ordered(src)) { |
7920 | folio_clear_ordered(src); | |
7921 | folio_set_ordered(dst); | |
f8e66081 RG |
7922 | } |
7923 | ||
f8e66081 RG |
7924 | return MIGRATEPAGE_SUCCESS; |
7925 | } | |
e7a60a17 MWO |
7926 | #else |
7927 | #define btrfs_migrate_folio NULL | |
f8e66081 RG |
7928 | #endif |
7929 | ||
895586eb MWO |
7930 | static void btrfs_invalidate_folio(struct folio *folio, size_t offset, |
7931 | size_t length) | |
39279cc3 | 7932 | { |
895586eb | 7933 | struct btrfs_inode *inode = BTRFS_I(folio->mapping->host); |
b945a463 | 7934 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
53ac7ead | 7935 | struct extent_io_tree *tree = &inode->io_tree; |
2ac55d41 | 7936 | struct extent_state *cached_state = NULL; |
895586eb MWO |
7937 | u64 page_start = folio_pos(folio); |
7938 | u64 page_end = page_start + folio_size(folio) - 1; | |
3b835840 | 7939 | u64 cur; |
53ac7ead | 7940 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 7941 | |
8b62b72b | 7942 | /* |
895586eb MWO |
7943 | * We have folio locked so no new ordered extent can be created on this |
7944 | * page, nor bio can be submitted for this folio. | |
8b62b72b | 7945 | * |
895586eb MWO |
7946 | * But already submitted bio can still be finished on this folio. |
7947 | * Furthermore, endio function won't skip folio which has Ordered | |
f57ad937 | 7948 | * (Private2) already cleared, so it's possible for endio and |
895586eb MWO |
7949 | * invalidate_folio to do the same ordered extent accounting twice |
7950 | * on one folio. | |
266a2586 QW |
7951 | * |
7952 | * So here we wait for any submitted bios to finish, so that we won't | |
895586eb | 7953 | * do double ordered extent accounting on the same folio. |
8b62b72b | 7954 | */ |
895586eb MWO |
7955 | folio_wait_writeback(folio); |
7956 | wait_subpage_spinlock(&folio->page); | |
8b62b72b | 7957 | |
bcd77455 QW |
7958 | /* |
7959 | * For subpage case, we have call sites like | |
7960 | * btrfs_punch_hole_lock_range() which passes range not aligned to | |
7961 | * sectorsize. | |
895586eb MWO |
7962 | * If the range doesn't cover the full folio, we don't need to and |
7963 | * shouldn't clear page extent mapped, as folio->private can still | |
bcd77455 QW |
7964 | * record subpage dirty bits for other part of the range. |
7965 | * | |
895586eb MWO |
7966 | * For cases that invalidate the full folio even the range doesn't |
7967 | * cover the full folio, like invalidating the last folio, we're | |
bcd77455 QW |
7968 | * still safe to wait for ordered extent to finish. |
7969 | */ | |
5a60542c | 7970 | if (!(offset == 0 && length == folio_size(folio))) { |
f913cff3 | 7971 | btrfs_release_folio(folio, GFP_NOFS); |
e6dcd2dc CM |
7972 | return; |
7973 | } | |
131e404a FDBM |
7974 | |
7975 | if (!inode_evicting) | |
570eb97b | 7976 | lock_extent(tree, page_start, page_end, &cached_state); |
951c80f8 | 7977 | |
3b835840 QW |
7978 | cur = page_start; |
7979 | while (cur < page_end) { | |
7980 | struct btrfs_ordered_extent *ordered; | |
3b835840 | 7981 | u64 range_end; |
b945a463 | 7982 | u32 range_len; |
bd015294 | 7983 | u32 extra_flags = 0; |
3b835840 QW |
7984 | |
7985 | ordered = btrfs_lookup_first_ordered_range(inode, cur, | |
7986 | page_end + 1 - cur); | |
7987 | if (!ordered) { | |
7988 | range_end = page_end; | |
7989 | /* | |
7990 | * No ordered extent covering this range, we are safe | |
7991 | * to delete all extent states in the range. | |
7992 | */ | |
bd015294 | 7993 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
7994 | goto next; |
7995 | } | |
7996 | if (ordered->file_offset > cur) { | |
7997 | /* | |
7998 | * There is a range between [cur, oe->file_offset) not | |
7999 | * covered by any ordered extent. | |
8000 | * We are safe to delete all extent states, and handle | |
8001 | * the ordered extent in the next iteration. | |
8002 | */ | |
8003 | range_end = ordered->file_offset - 1; | |
bd015294 | 8004 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8005 | goto next; |
8006 | } | |
8007 | ||
8008 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, | |
8009 | page_end); | |
b945a463 QW |
8010 | ASSERT(range_end + 1 - cur < U32_MAX); |
8011 | range_len = range_end + 1 - cur; | |
895586eb | 8012 | if (!btrfs_page_test_ordered(fs_info, &folio->page, cur, range_len)) { |
3b835840 | 8013 | /* |
f57ad937 QW |
8014 | * If Ordered (Private2) is cleared, it means endio has |
8015 | * already been executed for the range. | |
3b835840 QW |
8016 | * We can't delete the extent states as |
8017 | * btrfs_finish_ordered_io() may still use some of them. | |
8018 | */ | |
3b835840 QW |
8019 | goto next; |
8020 | } | |
895586eb | 8021 | btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len); |
3b835840 | 8022 | |
eb84ae03 | 8023 | /* |
2766ff61 FM |
8024 | * IO on this page will never be started, so we need to account |
8025 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW | |
8026 | * here, must leave that up for the ordered extent completion. | |
3b835840 QW |
8027 | * |
8028 | * This will also unlock the range for incoming | |
8029 | * btrfs_finish_ordered_io(). | |
eb84ae03 | 8030 | */ |
131e404a | 8031 | if (!inode_evicting) |
3b835840 | 8032 | clear_extent_bit(tree, cur, range_end, |
2766ff61 | 8033 | EXTENT_DELALLOC | |
131e404a | 8034 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
bd015294 | 8035 | EXTENT_DEFRAG, &cached_state); |
3b835840 | 8036 | |
54c65371 | 8037 | spin_lock_irq(&inode->ordered_tree_lock); |
3b835840 QW |
8038 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
8039 | ordered->truncated_len = min(ordered->truncated_len, | |
8040 | cur - ordered->file_offset); | |
54c65371 | 8041 | spin_unlock_irq(&inode->ordered_tree_lock); |
3b835840 | 8042 | |
bd015294 JB |
8043 | /* |
8044 | * If the ordered extent has finished, we're safe to delete all | |
8045 | * the extent states of the range, otherwise | |
8046 | * btrfs_finish_ordered_io() will get executed by endio for | |
8047 | * other pages, so we can't delete extent states. | |
8048 | */ | |
3b835840 | 8049 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
f41b6ba9 | 8050 | cur, range_end + 1 - cur)) { |
3b835840 QW |
8051 | btrfs_finish_ordered_io(ordered); |
8052 | /* | |
8053 | * The ordered extent has finished, now we're again | |
8054 | * safe to delete all extent states of the range. | |
8055 | */ | |
bd015294 | 8056 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8057 | } |
8058 | next: | |
8059 | if (ordered) | |
8060 | btrfs_put_ordered_extent(ordered); | |
8b62b72b | 8061 | /* |
3b835840 QW |
8062 | * Qgroup reserved space handler |
8063 | * Sector(s) here will be either: | |
266a2586 | 8064 | * |
3b835840 QW |
8065 | * 1) Already written to disk or bio already finished |
8066 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. | |
8067 | * Qgroup will be handled by its qgroup_record then. | |
8068 | * btrfs_qgroup_free_data() call will do nothing here. | |
8069 | * | |
8070 | * 2) Not written to disk yet | |
8071 | * Then btrfs_qgroup_free_data() call will clear the | |
8072 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup | |
8073 | * reserved data space. | |
8074 | * Since the IO will never happen for this page. | |
8b62b72b | 8075 | */ |
9e65bfca | 8076 | btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL); |
131e404a | 8077 | if (!inode_evicting) { |
3b835840 QW |
8078 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
8079 | EXTENT_DELALLOC | EXTENT_UPTODATE | | |
bd015294 JB |
8080 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG | |
8081 | extra_flags, &cached_state); | |
131e404a | 8082 | } |
3b835840 | 8083 | cur = range_end + 1; |
131e404a | 8084 | } |
b9d0b389 | 8085 | /* |
3b835840 | 8086 | * We have iterated through all ordered extents of the page, the page |
f57ad937 QW |
8087 | * should not have Ordered (Private2) anymore, or the above iteration |
8088 | * did something wrong. | |
b9d0b389 | 8089 | */ |
895586eb MWO |
8090 | ASSERT(!folio_test_ordered(folio)); |
8091 | btrfs_page_clear_checked(fs_info, &folio->page, folio_pos(folio), folio_size(folio)); | |
3b835840 | 8092 | if (!inode_evicting) |
f913cff3 | 8093 | __btrfs_release_folio(folio, GFP_NOFS); |
895586eb | 8094 | clear_page_extent_mapped(&folio->page); |
39279cc3 CM |
8095 | } |
8096 | ||
9ebefb18 CM |
8097 | /* |
8098 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8099 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8100 | * be careful to check for EOF conditions here. We set the page up correctly | |
8101 | * for a written page which means we get ENOSPC checking when writing into | |
8102 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8103 | * support these features. | |
8104 | * | |
8105 | * We are not allowed to take the i_mutex here so we have to play games to | |
8106 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8107 | * truncate_setsize() writes the inode size before removing pages, once we have |
8108 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8109 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8110 | * unlock the page. | |
8111 | */ | |
a528a241 | 8112 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8113 | { |
c2ec175c | 8114 | struct page *page = vmf->page; |
11bac800 | 8115 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8116 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8117 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8118 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8119 | struct extent_state *cached_state = NULL; |
364ecf36 | 8120 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 8121 | unsigned long zero_start; |
9ebefb18 | 8122 | loff_t size; |
a528a241 SJ |
8123 | vm_fault_t ret; |
8124 | int ret2; | |
9998eb70 | 8125 | int reserved = 0; |
d0b7da88 | 8126 | u64 reserved_space; |
a52d9a80 | 8127 | u64 page_start; |
e6dcd2dc | 8128 | u64 page_end; |
d0b7da88 CR |
8129 | u64 end; |
8130 | ||
09cbfeaf | 8131 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8132 | |
b2b5ef5c | 8133 | sb_start_pagefault(inode->i_sb); |
df480633 | 8134 | page_start = page_offset(page); |
09cbfeaf | 8135 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8136 | end = page_end; |
df480633 | 8137 | |
d0b7da88 CR |
8138 | /* |
8139 | * Reserving delalloc space after obtaining the page lock can lead to | |
8140 | * deadlock. For example, if a dirty page is locked by this function | |
8141 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
f3e90c1c | 8142 | * dirty page write out, then the btrfs_writepages() function could |
d0b7da88 CR |
8143 | * end up waiting indefinitely to get a lock on the page currently |
8144 | * being processed by btrfs_page_mkwrite() function. | |
8145 | */ | |
e5b7231e NB |
8146 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8147 | page_start, reserved_space); | |
a528a241 SJ |
8148 | if (!ret2) { |
8149 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8150 | reserved = 1; |
8151 | } | |
a528a241 SJ |
8152 | if (ret2) { |
8153 | ret = vmf_error(ret2); | |
9998eb70 CM |
8154 | if (reserved) |
8155 | goto out; | |
8156 | goto out_noreserve; | |
56a76f82 | 8157 | } |
1832a6d5 | 8158 | |
56a76f82 | 8159 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8160 | again: |
8318ba79 | 8161 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8162 | lock_page(page); |
9ebefb18 | 8163 | size = i_size_read(inode); |
a52d9a80 | 8164 | |
9ebefb18 | 8165 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8166 | (page_start >= size)) { |
9ebefb18 CM |
8167 | /* page got truncated out from underneath us */ |
8168 | goto out_unlock; | |
8169 | } | |
e6dcd2dc CM |
8170 | wait_on_page_writeback(page); |
8171 | ||
570eb97b | 8172 | lock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8173 | ret2 = set_page_extent_mapped(page); |
8174 | if (ret2 < 0) { | |
8175 | ret = vmf_error(ret2); | |
570eb97b | 8176 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8177 | goto out_unlock; |
8178 | } | |
e6dcd2dc | 8179 | |
eb84ae03 CM |
8180 | /* |
8181 | * we can't set the delalloc bits if there are pending ordered | |
8182 | * extents. Drop our locks and wait for them to finish | |
8183 | */ | |
a776c6fa NB |
8184 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8185 | PAGE_SIZE); | |
e6dcd2dc | 8186 | if (ordered) { |
570eb97b | 8187 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc | 8188 | unlock_page(page); |
8318ba79 | 8189 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
36d45567 | 8190 | btrfs_start_ordered_extent(ordered); |
e6dcd2dc CM |
8191 | btrfs_put_ordered_extent(ordered); |
8192 | goto again; | |
8193 | } | |
8194 | ||
09cbfeaf | 8195 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8196 | reserved_space = round_up(size - page_start, |
0b246afa | 8197 | fs_info->sectorsize); |
09cbfeaf | 8198 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8199 | end = page_start + reserved_space - 1; |
86d52921 NB |
8200 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8201 | data_reserved, page_start, | |
8202 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8203 | } |
8204 | } | |
8205 | ||
fbf19087 | 8206 | /* |
5416034f LB |
8207 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8208 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8209 | * bits, thus in this case for space account reason, we still need to | |
8210 | * clear any delalloc bits within this page range since we have to | |
8211 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8212 | */ |
d0b7da88 | 8213 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d | 8214 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
bd015294 | 8215 | EXTENT_DEFRAG, &cached_state); |
fbf19087 | 8216 | |
c2566f22 | 8217 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8218 | &cached_state); |
a528a241 | 8219 | if (ret2) { |
570eb97b | 8220 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
9ed74f2d JB |
8221 | ret = VM_FAULT_SIGBUS; |
8222 | goto out_unlock; | |
8223 | } | |
9ebefb18 CM |
8224 | |
8225 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8226 | if (page_start + PAGE_SIZE > size) |
7073017a | 8227 | zero_start = offset_in_page(size); |
9ebefb18 | 8228 | else |
09cbfeaf | 8229 | zero_start = PAGE_SIZE; |
9ebefb18 | 8230 | |
21a8935e | 8231 | if (zero_start != PAGE_SIZE) |
d048b9c2 | 8232 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
21a8935e | 8233 | |
e4f94347 | 8234 | btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE); |
2d8ec40e QW |
8235 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
8236 | btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start); | |
5a3f23d5 | 8237 | |
bc0939fc | 8238 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
257c62e1 | 8239 | |
570eb97b | 8240 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
8318ba79 | 8241 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8242 | |
76de60ed YY |
8243 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8244 | sb_end_pagefault(inode->i_sb); | |
8245 | extent_changeset_free(data_reserved); | |
8246 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8247 | |
8248 | out_unlock: | |
9ebefb18 | 8249 | unlock_page(page); |
8318ba79 | 8250 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
1832a6d5 | 8251 | out: |
8702ba93 | 8252 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8253 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8254 | reserved_space, (ret != 0)); |
9998eb70 | 8255 | out_noreserve: |
b2b5ef5c | 8256 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8257 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8258 | return ret; |
8259 | } | |
8260 | ||
d9dcae67 | 8261 | static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) |
39279cc3 | 8262 | { |
d9ac19c3 | 8263 | struct btrfs_truncate_control control = { |
d9dcae67 DS |
8264 | .inode = inode, |
8265 | .ino = btrfs_ino(inode), | |
d9ac19c3 | 8266 | .min_type = BTRFS_EXTENT_DATA_KEY, |
655807b8 | 8267 | .clear_extent_range = true, |
d9ac19c3 | 8268 | }; |
d9dcae67 DS |
8269 | struct btrfs_root *root = inode->root; |
8270 | struct btrfs_fs_info *fs_info = root->fs_info; | |
fcb80c2a | 8271 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8272 | int ret; |
39279cc3 | 8273 | struct btrfs_trans_handle *trans; |
0b246afa | 8274 | u64 mask = fs_info->sectorsize - 1; |
6822b3f7 | 8275 | const u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8276 | |
213e8c55 | 8277 | if (!skip_writeback) { |
d9dcae67 DS |
8278 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, |
8279 | inode->vfs_inode.i_size & (~mask), | |
213e8c55 FM |
8280 | (u64)-1); |
8281 | if (ret) | |
8282 | return ret; | |
8283 | } | |
39279cc3 | 8284 | |
fcb80c2a | 8285 | /* |
f7e9e8fc OS |
8286 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8287 | * things going on here: | |
fcb80c2a | 8288 | * |
f7e9e8fc | 8289 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8290 | * |
f7e9e8fc | 8291 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8292 | * be free'd up by the truncate operation, but also have some slack |
8293 | * space reserved in case it uses space during the truncate (thank you | |
8294 | * very much snapshotting). | |
8295 | * | |
f7e9e8fc | 8296 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8297 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8298 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8299 | * doesn't end up using space reserved for updating the inode. We also |
8300 | * need to be able to stop the transaction and start a new one, which | |
8301 | * means we need to be able to update the inode several times, and we | |
8302 | * have no idea of knowing how many times that will be, so we can't just | |
8303 | * reserve 1 item for the entirety of the operation, so that has to be | |
8304 | * done separately as well. | |
fcb80c2a JB |
8305 | * |
8306 | * So that leaves us with | |
8307 | * | |
f7e9e8fc | 8308 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8309 | * transaction reservation. |
f7e9e8fc | 8310 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8311 | * updating the inode. |
8312 | */ | |
2ff7e61e | 8313 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8314 | if (!rsv) |
8315 | return -ENOMEM; | |
4a338542 | 8316 | rsv->size = min_size; |
710d5921 | 8317 | rsv->failfast = true; |
f0cd846e | 8318 | |
907cbceb | 8319 | /* |
07127184 | 8320 | * 1 for the truncate slack space |
907cbceb JB |
8321 | * 1 for updating the inode. |
8322 | */ | |
f3fe820c | 8323 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8324 | if (IS_ERR(trans)) { |
ad7e1a74 | 8325 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8326 | goto out; |
8327 | } | |
f0cd846e | 8328 | |
907cbceb | 8329 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8330 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8331 | min_size, false); |
6822b3f7 FM |
8332 | /* |
8333 | * We have reserved 2 metadata units when we started the transaction and | |
8334 | * min_size matches 1 unit, so this should never fail, but if it does, | |
8335 | * it's not critical we just fail truncation. | |
8336 | */ | |
8337 | if (WARN_ON(ret)) { | |
8338 | btrfs_end_transaction(trans); | |
8339 | goto out; | |
8340 | } | |
f0cd846e | 8341 | |
ca7e70f5 | 8342 | trans->block_rsv = rsv; |
907cbceb | 8343 | |
8082510e | 8344 | while (1) { |
9a4a1429 | 8345 | struct extent_state *cached_state = NULL; |
d9dcae67 | 8346 | const u64 new_size = inode->vfs_inode.i_size; |
9a4a1429 JB |
8347 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
8348 | ||
d9ac19c3 | 8349 | control.new_size = new_size; |
d9dcae67 | 8350 | lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); |
9a4a1429 JB |
8351 | /* |
8352 | * We want to drop from the next block forward in case this new | |
8353 | * size is not block aligned since we will be keeping the last | |
8354 | * block of the extent just the way it is. | |
8355 | */ | |
d9dcae67 | 8356 | btrfs_drop_extent_map_range(inode, |
4c0c8cfc FM |
8357 | ALIGN(new_size, fs_info->sectorsize), |
8358 | (u64)-1, false); | |
9a4a1429 | 8359 | |
71d18b53 | 8360 | ret = btrfs_truncate_inode_items(trans, root, &control); |
c2ddb612 | 8361 | |
d9dcae67 DS |
8362 | inode_sub_bytes(&inode->vfs_inode, control.sub_bytes); |
8363 | btrfs_inode_safe_disk_i_size_write(inode, control.last_size); | |
c2ddb612 | 8364 | |
d9dcae67 | 8365 | unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); |
9a4a1429 | 8366 | |
ddfae63c | 8367 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8368 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8369 | break; |
39279cc3 | 8370 | |
8b9d0322 | 8371 | ret = btrfs_update_inode(trans, inode); |
ad7e1a74 | 8372 | if (ret) |
3893e33b | 8373 | break; |
ca7e70f5 | 8374 | |
3a45bb20 | 8375 | btrfs_end_transaction(trans); |
2ff7e61e | 8376 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8377 | |
8378 | trans = btrfs_start_transaction(root, 2); | |
8379 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8380 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8381 | trans = NULL; |
8382 | break; | |
8383 | } | |
8384 | ||
63f018be | 8385 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8386 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8387 | rsv, min_size, false); |
6822b3f7 FM |
8388 | /* |
8389 | * We have reserved 2 metadata units when we started the | |
8390 | * transaction and min_size matches 1 unit, so this should never | |
8391 | * fail, but if it does, it's not critical we just fail truncation. | |
8392 | */ | |
8393 | if (WARN_ON(ret)) | |
8394 | break; | |
8395 | ||
ca7e70f5 | 8396 | trans->block_rsv = rsv; |
8082510e YZ |
8397 | } |
8398 | ||
ddfae63c JB |
8399 | /* |
8400 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
54f03ab1 JB |
8401 | * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we |
8402 | * know we've truncated everything except the last little bit, and can | |
8403 | * do btrfs_truncate_block and then update the disk_i_size. | |
ddfae63c | 8404 | */ |
54f03ab1 | 8405 | if (ret == BTRFS_NEED_TRUNCATE_BLOCK) { |
ddfae63c JB |
8406 | btrfs_end_transaction(trans); |
8407 | btrfs_btree_balance_dirty(fs_info); | |
8408 | ||
d9dcae67 | 8409 | ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, 0, 0); |
ddfae63c JB |
8410 | if (ret) |
8411 | goto out; | |
8412 | trans = btrfs_start_transaction(root, 1); | |
8413 | if (IS_ERR(trans)) { | |
8414 | ret = PTR_ERR(trans); | |
8415 | goto out; | |
8416 | } | |
d9dcae67 | 8417 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
ddfae63c JB |
8418 | } |
8419 | ||
917c16b2 | 8420 | if (trans) { |
ad7e1a74 OS |
8421 | int ret2; |
8422 | ||
0b246afa | 8423 | trans->block_rsv = &fs_info->trans_block_rsv; |
8b9d0322 | 8424 | ret2 = btrfs_update_inode(trans, inode); |
ad7e1a74 OS |
8425 | if (ret2 && !ret) |
8426 | ret = ret2; | |
7b128766 | 8427 | |
ad7e1a74 OS |
8428 | ret2 = btrfs_end_transaction(trans); |
8429 | if (ret2 && !ret) | |
8430 | ret = ret2; | |
2ff7e61e | 8431 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8432 | } |
fcb80c2a | 8433 | out: |
2ff7e61e | 8434 | btrfs_free_block_rsv(fs_info, rsv); |
0d7d3165 FM |
8435 | /* |
8436 | * So if we truncate and then write and fsync we normally would just | |
8437 | * write the extents that changed, which is a problem if we need to | |
8438 | * first truncate that entire inode. So set this flag so we write out | |
8439 | * all of the extents in the inode to the sync log so we're completely | |
8440 | * safe. | |
8441 | * | |
8442 | * If no extents were dropped or trimmed we don't need to force the next | |
8443 | * fsync to truncate all the inode's items from the log and re-log them | |
8444 | * all. This means the truncate operation did not change the file size, | |
8445 | * or changed it to a smaller size but there was only an implicit hole | |
8446 | * between the old i_size and the new i_size, and there were no prealloc | |
8447 | * extents beyond i_size to drop. | |
8448 | */ | |
d9ac19c3 | 8449 | if (control.extents_found > 0) |
d9dcae67 | 8450 | btrfs_set_inode_full_sync(inode); |
fcb80c2a | 8451 | |
ad7e1a74 | 8452 | return ret; |
39279cc3 CM |
8453 | } |
8454 | ||
f2d40141 | 8455 | struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap, |
a1fd0c35 OS |
8456 | struct inode *dir) |
8457 | { | |
8458 | struct inode *inode; | |
8459 | ||
8460 | inode = new_inode(dir->i_sb); | |
8461 | if (inode) { | |
8462 | /* | |
8463 | * Subvolumes don't inherit the sgid bit or the parent's gid if | |
8464 | * the parent's sgid bit is set. This is probably a bug. | |
8465 | */ | |
f2d40141 | 8466 | inode_init_owner(idmap, inode, NULL, |
a1fd0c35 OS |
8467 | S_IFDIR | (~current_umask() & S_IRWXUGO)); |
8468 | inode->i_op = &btrfs_dir_inode_operations; | |
8469 | inode->i_fop = &btrfs_dir_file_operations; | |
8470 | } | |
8471 | return inode; | |
8472 | } | |
8473 | ||
39279cc3 CM |
8474 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8475 | { | |
69fe2d75 | 8476 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8477 | struct btrfs_inode *ei; |
2ead6ae7 | 8478 | struct inode *inode; |
637e6e0f DS |
8479 | struct extent_io_tree *file_extent_tree = NULL; |
8480 | ||
8481 | /* Self tests may pass a NULL fs_info. */ | |
8482 | if (fs_info && !btrfs_fs_incompat(fs_info, NO_HOLES)) { | |
8483 | file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL); | |
8484 | if (!file_extent_tree) | |
8485 | return NULL; | |
8486 | } | |
39279cc3 | 8487 | |
fd60b288 | 8488 | ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL); |
637e6e0f DS |
8489 | if (!ei) { |
8490 | kfree(file_extent_tree); | |
39279cc3 | 8491 | return NULL; |
637e6e0f | 8492 | } |
2ead6ae7 YZ |
8493 | |
8494 | ei->root = NULL; | |
2ead6ae7 | 8495 | ei->generation = 0; |
15ee9bc7 | 8496 | ei->last_trans = 0; |
257c62e1 | 8497 | ei->last_sub_trans = 0; |
e02119d5 | 8498 | ei->logged_trans = 0; |
2ead6ae7 | 8499 | ei->delalloc_bytes = 0; |
a7e3b975 | 8500 | ei->new_delalloc_bytes = 0; |
47059d93 | 8501 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8502 | ei->disk_i_size = 0; |
8503 | ei->flags = 0; | |
77eea05e | 8504 | ei->ro_flags = 0; |
7709cde3 | 8505 | ei->csum_bytes = 0; |
2ead6ae7 | 8506 | ei->index_cnt = (u64)-1; |
67de1176 | 8507 | ei->dir_index = 0; |
2ead6ae7 | 8508 | ei->last_unlink_trans = 0; |
3ebac17c | 8509 | ei->last_reflink_trans = 0; |
46d8bc34 | 8510 | ei->last_log_commit = 0; |
2ead6ae7 | 8511 | |
9e0baf60 JB |
8512 | spin_lock_init(&ei->lock); |
8513 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8514 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8515 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8516 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8517 | ei->runtime_flags = 0; |
b52aa8c9 | 8518 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8519 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8520 | |
16cdcec7 MX |
8521 | ei->delayed_node = NULL; |
8522 | ||
c6e8f898 DS |
8523 | ei->i_otime_sec = 0; |
8524 | ei->i_otime_nsec = 0; | |
9cc97d64 | 8525 | |
2ead6ae7 | 8526 | inode = &ei->vfs_inode; |
a8067e02 | 8527 | extent_map_tree_init(&ei->extent_tree); |
738290c0 DS |
8528 | |
8529 | /* This io tree sets the valid inode. */ | |
35da5a7e | 8530 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO); |
0988fc7b | 8531 | ei->io_tree.inode = ei; |
738290c0 | 8532 | |
637e6e0f DS |
8533 | ei->file_extent_tree = file_extent_tree; |
8534 | if (file_extent_tree) { | |
8535 | extent_io_tree_init(fs_info, ei->file_extent_tree, | |
8536 | IO_TREE_INODE_FILE_EXTENT); | |
8537 | /* Lockdep class is set only for the file extent tree. */ | |
8538 | lockdep_set_class(&ei->file_extent_tree->lock, &file_extent_tree_class); | |
8539 | } | |
2ead6ae7 | 8540 | mutex_init(&ei->log_mutex); |
54c65371 DS |
8541 | spin_lock_init(&ei->ordered_tree_lock); |
8542 | ei->ordered_tree = RB_ROOT; | |
8543 | ei->ordered_tree_last = NULL; | |
2ead6ae7 | 8544 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8545 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8546 | RB_CLEAR_NODE(&ei->rb_node); |
8318ba79 | 8547 | init_rwsem(&ei->i_mmap_lock); |
2ead6ae7 YZ |
8548 | |
8549 | return inode; | |
39279cc3 CM |
8550 | } |
8551 | ||
aaedb55b JB |
8552 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8553 | void btrfs_test_destroy_inode(struct inode *inode) | |
8554 | { | |
4c0c8cfc | 8555 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
637e6e0f | 8556 | kfree(BTRFS_I(inode)->file_extent_tree); |
aaedb55b JB |
8557 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8558 | } | |
8559 | #endif | |
8560 | ||
26602cab | 8561 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8562 | { |
637e6e0f | 8563 | kfree(BTRFS_I(inode)->file_extent_tree); |
fa0d7e3d NP |
8564 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8565 | } | |
8566 | ||
633cc816 | 8567 | void btrfs_destroy_inode(struct inode *vfs_inode) |
39279cc3 | 8568 | { |
e6dcd2dc | 8569 | struct btrfs_ordered_extent *ordered; |
633cc816 NB |
8570 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
8571 | struct btrfs_root *root = inode->root; | |
5f4403e1 | 8572 | bool freespace_inode; |
5a3f23d5 | 8573 | |
633cc816 NB |
8574 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
8575 | WARN_ON(vfs_inode->i_data.nrpages); | |
8576 | WARN_ON(inode->block_rsv.reserved); | |
8577 | WARN_ON(inode->block_rsv.size); | |
8578 | WARN_ON(inode->outstanding_extents); | |
dc287224 FM |
8579 | if (!S_ISDIR(vfs_inode->i_mode)) { |
8580 | WARN_ON(inode->delalloc_bytes); | |
8581 | WARN_ON(inode->new_delalloc_bytes); | |
8582 | } | |
633cc816 NB |
8583 | WARN_ON(inode->csum_bytes); |
8584 | WARN_ON(inode->defrag_bytes); | |
39279cc3 | 8585 | |
a6dbd429 JB |
8586 | /* |
8587 | * This can happen where we create an inode, but somebody else also | |
8588 | * created the same inode and we need to destroy the one we already | |
8589 | * created. | |
8590 | */ | |
8591 | if (!root) | |
26602cab | 8592 | return; |
a6dbd429 | 8593 | |
5f4403e1 IA |
8594 | /* |
8595 | * If this is a free space inode do not take the ordered extents lockdep | |
8596 | * map. | |
8597 | */ | |
8598 | freespace_inode = btrfs_is_free_space_inode(inode); | |
8599 | ||
d397712b | 8600 | while (1) { |
633cc816 | 8601 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
e6dcd2dc CM |
8602 | if (!ordered) |
8603 | break; | |
8604 | else { | |
633cc816 | 8605 | btrfs_err(root->fs_info, |
5d163e0e | 8606 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8607 | ordered->file_offset, ordered->num_bytes); |
5f4403e1 IA |
8608 | |
8609 | if (!freespace_inode) | |
8610 | btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent); | |
8611 | ||
71fe0a55 | 8612 | btrfs_remove_ordered_extent(inode, ordered); |
e6dcd2dc CM |
8613 | btrfs_put_ordered_extent(ordered); |
8614 | btrfs_put_ordered_extent(ordered); | |
8615 | } | |
8616 | } | |
633cc816 NB |
8617 | btrfs_qgroup_check_reserved_leak(inode); |
8618 | inode_tree_del(inode); | |
4c0c8cfc | 8619 | btrfs_drop_extent_map_range(inode, 0, (u64)-1, false); |
633cc816 NB |
8620 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
8621 | btrfs_put_root(inode->root); | |
39279cc3 CM |
8622 | } |
8623 | ||
45321ac5 | 8624 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8625 | { |
8626 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8627 | |
6379ef9f NA |
8628 | if (root == NULL) |
8629 | return 1; | |
8630 | ||
fa6ac876 | 8631 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8632 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8633 | return 1; |
76dda93c | 8634 | else |
45321ac5 | 8635 | return generic_drop_inode(inode); |
76dda93c YZ |
8636 | } |
8637 | ||
0ee0fda0 | 8638 | static void init_once(void *foo) |
39279cc3 | 8639 | { |
0d031dc4 | 8640 | struct btrfs_inode *ei = foo; |
39279cc3 CM |
8641 | |
8642 | inode_init_once(&ei->vfs_inode); | |
8643 | } | |
8644 | ||
e67c718b | 8645 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8646 | { |
8c0a8537 KS |
8647 | /* |
8648 | * Make sure all delayed rcu free inodes are flushed before we | |
8649 | * destroy cache. | |
8650 | */ | |
8651 | rcu_barrier(); | |
642c5d34 | 8652 | bioset_exit(&btrfs_dio_bioset); |
5598e900 | 8653 | kmem_cache_destroy(btrfs_inode_cachep); |
39279cc3 CM |
8654 | } |
8655 | ||
f5c29bd9 | 8656 | int __init btrfs_init_cachep(void) |
39279cc3 | 8657 | { |
837e1972 | 8658 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8659 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8660 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8661 | init_once); | |
39279cc3 CM |
8662 | if (!btrfs_inode_cachep) |
8663 | goto fail; | |
9601e3f6 | 8664 | |
642c5d34 | 8665 | if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE, |
67d66982 | 8666 | offsetof(struct btrfs_dio_private, bbio.bio), |
642c5d34 CH |
8667 | BIOSET_NEED_BVECS)) |
8668 | goto fail; | |
8669 | ||
39279cc3 CM |
8670 | return 0; |
8671 | fail: | |
8672 | btrfs_destroy_cachep(); | |
8673 | return -ENOMEM; | |
8674 | } | |
8675 | ||
b74d24f7 | 8676 | static int btrfs_getattr(struct mnt_idmap *idmap, |
549c7297 | 8677 | const struct path *path, struct kstat *stat, |
a528d35e | 8678 | u32 request_mask, unsigned int flags) |
39279cc3 | 8679 | { |
df0af1a5 | 8680 | u64 delalloc_bytes; |
2766ff61 | 8681 | u64 inode_bytes; |
a528d35e | 8682 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 8683 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 | 8684 | u32 bi_flags = BTRFS_I(inode)->flags; |
14605409 | 8685 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
04a87e34 YS |
8686 | |
8687 | stat->result_mask |= STATX_BTIME; | |
c6e8f898 DS |
8688 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec; |
8689 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec; | |
04a87e34 YS |
8690 | if (bi_flags & BTRFS_INODE_APPEND) |
8691 | stat->attributes |= STATX_ATTR_APPEND; | |
8692 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8693 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8694 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8695 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8696 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8697 | stat->attributes |= STATX_ATTR_NODUMP; | |
14605409 BB |
8698 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
8699 | stat->attributes |= STATX_ATTR_VERITY; | |
04a87e34 YS |
8700 | |
8701 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8702 | STATX_ATTR_COMPRESSED | | |
8703 | STATX_ATTR_IMMUTABLE | | |
8704 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8705 | |
0d72b928 | 8706 | generic_fillattr(idmap, request_mask, inode, stat); |
0ee5dc67 | 8707 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8708 | |
8709 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8710 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
2766ff61 | 8711 | inode_bytes = inode_get_bytes(inode); |
df0af1a5 | 8712 | spin_unlock(&BTRFS_I(inode)->lock); |
2766ff61 | 8713 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
29e70be2 | 8714 | ALIGN(delalloc_bytes, blocksize)) >> SECTOR_SHIFT; |
39279cc3 CM |
8715 | return 0; |
8716 | } | |
8717 | ||
cdd1fedf DF |
8718 | static int btrfs_rename_exchange(struct inode *old_dir, |
8719 | struct dentry *old_dentry, | |
8720 | struct inode *new_dir, | |
8721 | struct dentry *new_dentry) | |
8722 | { | |
0b246afa | 8723 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf | 8724 | struct btrfs_trans_handle *trans; |
c1621871 | 8725 | unsigned int trans_num_items; |
cdd1fedf DF |
8726 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
8727 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
8728 | struct inode *new_inode = new_dentry->d_inode; | |
8729 | struct inode *old_inode = old_dentry->d_inode; | |
88d2beec FM |
8730 | struct btrfs_rename_ctx old_rename_ctx; |
8731 | struct btrfs_rename_ctx new_rename_ctx; | |
4a0cc7ca NB |
8732 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
8733 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
8734 | u64 old_idx = 0; |
8735 | u64 new_idx = 0; | |
cdd1fedf | 8736 | int ret; |
75b463d2 | 8737 | int ret2; |
dc09ef35 | 8738 | bool need_abort = false; |
ab3c5c18 | 8739 | struct fscrypt_name old_fname, new_fname; |
6db75318 | 8740 | struct fscrypt_str *old_name, *new_name; |
cdd1fedf | 8741 | |
3f79f6f6 N |
8742 | /* |
8743 | * For non-subvolumes allow exchange only within one subvolume, in the | |
8744 | * same inode namespace. Two subvolumes (represented as directory) can | |
8745 | * be exchanged as they're a logical link and have a fixed inode number. | |
8746 | */ | |
8747 | if (root != dest && | |
8748 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || | |
8749 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) | |
cdd1fedf DF |
8750 | return -EXDEV; |
8751 | ||
ab3c5c18 STD |
8752 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
8753 | if (ret) | |
8754 | return ret; | |
8755 | ||
8756 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); | |
8757 | if (ret) { | |
8758 | fscrypt_free_filename(&old_fname); | |
8759 | return ret; | |
8760 | } | |
8761 | ||
6db75318 STD |
8762 | old_name = &old_fname.disk_name; |
8763 | new_name = &new_fname.disk_name; | |
ab3c5c18 | 8764 | |
cdd1fedf | 8765 | /* close the race window with snapshot create/destroy ioctl */ |
943eb3bf JB |
8766 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
8767 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8768 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8769 | |
8770 | /* | |
c1621871 OS |
8771 | * For each inode: |
8772 | * 1 to remove old dir item | |
8773 | * 1 to remove old dir index | |
8774 | * 1 to add new dir item | |
8775 | * 1 to add new dir index | |
8776 | * 1 to update parent inode | |
8777 | * | |
8778 | * If the parents are the same, we only need to account for one | |
cdd1fedf | 8779 | */ |
c1621871 OS |
8780 | trans_num_items = (old_dir == new_dir ? 9 : 10); |
8781 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8782 | /* | |
8783 | * 1 to remove old root ref | |
8784 | * 1 to remove old root backref | |
8785 | * 1 to add new root ref | |
8786 | * 1 to add new root backref | |
8787 | */ | |
8788 | trans_num_items += 4; | |
8789 | } else { | |
8790 | /* | |
8791 | * 1 to update inode item | |
8792 | * 1 to remove old inode ref | |
8793 | * 1 to add new inode ref | |
8794 | */ | |
8795 | trans_num_items += 3; | |
8796 | } | |
8797 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
8798 | trans_num_items += 4; | |
8799 | else | |
8800 | trans_num_items += 3; | |
8801 | trans = btrfs_start_transaction(root, trans_num_items); | |
cdd1fedf DF |
8802 | if (IS_ERR(trans)) { |
8803 | ret = PTR_ERR(trans); | |
8804 | goto out_notrans; | |
8805 | } | |
8806 | ||
00aa8e87 JB |
8807 | if (dest != root) { |
8808 | ret = btrfs_record_root_in_trans(trans, dest); | |
8809 | if (ret) | |
8810 | goto out_fail; | |
8811 | } | |
3e174099 | 8812 | |
cdd1fedf DF |
8813 | /* |
8814 | * We need to find a free sequence number both in the source and | |
8815 | * in the destination directory for the exchange. | |
8816 | */ | |
877574e2 | 8817 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
8818 | if (ret) |
8819 | goto out_fail; | |
877574e2 | 8820 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
8821 | if (ret) |
8822 | goto out_fail; | |
8823 | ||
8824 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
8825 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
8826 | ||
8827 | /* Reference for the source. */ | |
8828 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8829 | /* force full log commit if subvolume involved. */ | |
90787766 | 8830 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8831 | } else { |
6db75318 | 8832 | ret = btrfs_insert_inode_ref(trans, dest, new_name, old_ino, |
f85b7379 DS |
8833 | btrfs_ino(BTRFS_I(new_dir)), |
8834 | old_idx); | |
cdd1fedf DF |
8835 | if (ret) |
8836 | goto out_fail; | |
dc09ef35 | 8837 | need_abort = true; |
cdd1fedf DF |
8838 | } |
8839 | ||
8840 | /* And now for the dest. */ | |
8841 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8842 | /* force full log commit if subvolume involved. */ | |
90787766 | 8843 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8844 | } else { |
6db75318 | 8845 | ret = btrfs_insert_inode_ref(trans, root, old_name, new_ino, |
f85b7379 DS |
8846 | btrfs_ino(BTRFS_I(old_dir)), |
8847 | new_idx); | |
dc09ef35 JB |
8848 | if (ret) { |
8849 | if (need_abort) | |
8850 | btrfs_abort_transaction(trans, ret); | |
cdd1fedf | 8851 | goto out_fail; |
dc09ef35 | 8852 | } |
cdd1fedf DF |
8853 | } |
8854 | ||
8855 | /* Update inode version and ctime/mtime. */ | |
8856 | inode_inc_iversion(old_dir); | |
8857 | inode_inc_iversion(new_dir); | |
8858 | inode_inc_iversion(old_inode); | |
8859 | inode_inc_iversion(new_inode); | |
130f1eca | 8860 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); |
cdd1fedf DF |
8861 | |
8862 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 | 8863 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
59fcf388 | 8864 | BTRFS_I(old_inode), true); |
f85b7379 | 8865 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
59fcf388 | 8866 | BTRFS_I(new_inode), true); |
cdd1fedf DF |
8867 | } |
8868 | ||
8869 | /* src is a subvolume */ | |
8870 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
5b7544cb | 8871 | ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); |
cdd1fedf | 8872 | } else { /* src is an inode */ |
4467af88 | 8873 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
4ec5934e | 8874 | BTRFS_I(old_dentry->d_inode), |
6db75318 | 8875 | old_name, &old_rename_ctx); |
cdd1fedf | 8876 | if (!ret) |
8b9d0322 | 8877 | ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); |
cdd1fedf DF |
8878 | } |
8879 | if (ret) { | |
66642832 | 8880 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8881 | goto out_fail; |
8882 | } | |
8883 | ||
8884 | /* dest is a subvolume */ | |
8885 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
5b7544cb | 8886 | ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); |
cdd1fedf | 8887 | } else { /* dest is an inode */ |
4467af88 | 8888 | ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 8889 | BTRFS_I(new_dentry->d_inode), |
6db75318 | 8890 | new_name, &new_rename_ctx); |
cdd1fedf | 8891 | if (!ret) |
8b9d0322 | 8892 | ret = btrfs_update_inode(trans, BTRFS_I(new_inode)); |
cdd1fedf DF |
8893 | } |
8894 | if (ret) { | |
66642832 | 8895 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8896 | goto out_fail; |
8897 | } | |
8898 | ||
db0a669f | 8899 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 8900 | new_name, 0, old_idx); |
cdd1fedf | 8901 | if (ret) { |
66642832 | 8902 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8903 | goto out_fail; |
8904 | } | |
8905 | ||
db0a669f | 8906 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
6db75318 | 8907 | old_name, 0, new_idx); |
cdd1fedf | 8908 | if (ret) { |
66642832 | 8909 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8910 | goto out_fail; |
8911 | } | |
8912 | ||
8913 | if (old_inode->i_nlink == 1) | |
8914 | BTRFS_I(old_inode)->dir_index = old_idx; | |
8915 | if (new_inode->i_nlink == 1) | |
8916 | BTRFS_I(new_inode)->dir_index = new_idx; | |
8917 | ||
259c4b96 FM |
8918 | /* |
8919 | * Now pin the logs of the roots. We do it to ensure that no other task | |
8920 | * can sync the logs while we are in progress with the rename, because | |
8921 | * that could result in an inconsistency in case any of the inodes that | |
8922 | * are part of this rename operation were logged before. | |
8923 | */ | |
8924 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8925 | btrfs_pin_log_trans(root); | |
8926 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8927 | btrfs_pin_log_trans(dest); | |
8928 | ||
8929 | /* Do the log updates for all inodes. */ | |
8930 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
d5f5bd54 | 8931 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 8932 | old_rename_ctx.index, new_dentry->d_parent); |
259c4b96 | 8933 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 8934 | btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir), |
88d2beec | 8935 | new_rename_ctx.index, old_dentry->d_parent); |
259c4b96 FM |
8936 | |
8937 | /* Now unpin the logs. */ | |
8938 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8939 | btrfs_end_log_trans(root); | |
8940 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
cdd1fedf | 8941 | btrfs_end_log_trans(dest); |
cdd1fedf | 8942 | out_fail: |
75b463d2 FM |
8943 | ret2 = btrfs_end_transaction(trans); |
8944 | ret = ret ? ret : ret2; | |
cdd1fedf | 8945 | out_notrans: |
943eb3bf JB |
8946 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
8947 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8948 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 8949 | |
ab3c5c18 STD |
8950 | fscrypt_free_filename(&new_fname); |
8951 | fscrypt_free_filename(&old_fname); | |
cdd1fedf DF |
8952 | return ret; |
8953 | } | |
8954 | ||
f2d40141 | 8955 | static struct inode *new_whiteout_inode(struct mnt_idmap *idmap, |
a1fd0c35 OS |
8956 | struct inode *dir) |
8957 | { | |
8958 | struct inode *inode; | |
8959 | ||
8960 | inode = new_inode(dir->i_sb); | |
8961 | if (inode) { | |
f2d40141 | 8962 | inode_init_owner(idmap, inode, dir, |
a1fd0c35 OS |
8963 | S_IFCHR | WHITEOUT_MODE); |
8964 | inode->i_op = &btrfs_special_inode_operations; | |
8965 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
8966 | } | |
8967 | return inode; | |
8968 | } | |
8969 | ||
f2d40141 | 8970 | static int btrfs_rename(struct mnt_idmap *idmap, |
ca07274c CB |
8971 | struct inode *old_dir, struct dentry *old_dentry, |
8972 | struct inode *new_dir, struct dentry *new_dentry, | |
8973 | unsigned int flags) | |
39279cc3 | 8974 | { |
0b246afa | 8975 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
3538d68d OS |
8976 | struct btrfs_new_inode_args whiteout_args = { |
8977 | .dir = old_dir, | |
8978 | .dentry = old_dentry, | |
8979 | }; | |
39279cc3 | 8980 | struct btrfs_trans_handle *trans; |
5062af35 | 8981 | unsigned int trans_num_items; |
39279cc3 | 8982 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 8983 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
8984 | struct inode *new_inode = d_inode(new_dentry); |
8985 | struct inode *old_inode = d_inode(old_dentry); | |
88d2beec | 8986 | struct btrfs_rename_ctx rename_ctx; |
00e4e6b3 | 8987 | u64 index = 0; |
39279cc3 | 8988 | int ret; |
75b463d2 | 8989 | int ret2; |
4a0cc7ca | 8990 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
ab3c5c18 | 8991 | struct fscrypt_name old_fname, new_fname; |
39279cc3 | 8992 | |
4a0cc7ca | 8993 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
8994 | return -EPERM; |
8995 | ||
4df27c4d | 8996 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 8997 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
8998 | return -EXDEV; |
8999 | ||
33345d01 | 9000 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9001 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9002 | return -ENOTEMPTY; |
5f39d397 | 9003 | |
4df27c4d YZ |
9004 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9005 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9006 | return -ENOTEMPTY; | |
9c52057c | 9007 | |
ab3c5c18 STD |
9008 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
9009 | if (ret) | |
9010 | return ret; | |
9c52057c | 9011 | |
ab3c5c18 STD |
9012 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); |
9013 | if (ret) { | |
9014 | fscrypt_free_filename(&old_fname); | |
9015 | return ret; | |
9016 | } | |
9c52057c | 9017 | |
9c52057c | 9018 | /* check for collisions, even if the name isn't there */ |
6db75318 | 9019 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name); |
9c52057c CM |
9020 | if (ret) { |
9021 | if (ret == -EEXIST) { | |
9022 | /* we shouldn't get | |
9023 | * eexist without a new_inode */ | |
fae7f21c | 9024 | if (WARN_ON(!new_inode)) { |
ab3c5c18 | 9025 | goto out_fscrypt_names; |
9c52057c CM |
9026 | } |
9027 | } else { | |
9028 | /* maybe -EOVERFLOW */ | |
ab3c5c18 | 9029 | goto out_fscrypt_names; |
9c52057c CM |
9030 | } |
9031 | } | |
9032 | ret = 0; | |
9033 | ||
5a3f23d5 | 9034 | /* |
8d875f95 CM |
9035 | * we're using rename to replace one file with another. Start IO on it |
9036 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9037 | */ |
8d875f95 | 9038 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9039 | filemap_flush(old_inode->i_mapping); |
9040 | ||
a1fd0c35 | 9041 | if (flags & RENAME_WHITEOUT) { |
f2d40141 | 9042 | whiteout_args.inode = new_whiteout_inode(idmap, old_dir); |
abe3bf74 CJ |
9043 | if (!whiteout_args.inode) { |
9044 | ret = -ENOMEM; | |
9045 | goto out_fscrypt_names; | |
9046 | } | |
3538d68d OS |
9047 | ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items); |
9048 | if (ret) | |
9049 | goto out_whiteout_inode; | |
9050 | } else { | |
9051 | /* 1 to update the old parent inode. */ | |
9052 | trans_num_items = 1; | |
a1fd0c35 OS |
9053 | } |
9054 | ||
c1621871 OS |
9055 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
9056 | /* Close the race window with snapshot create/destroy ioctl */ | |
0b246afa | 9057 | down_read(&fs_info->subvol_sem); |
c1621871 OS |
9058 | /* |
9059 | * 1 to remove old root ref | |
9060 | * 1 to remove old root backref | |
9061 | * 1 to add new root ref | |
9062 | * 1 to add new root backref | |
9063 | */ | |
3538d68d | 9064 | trans_num_items += 4; |
c1621871 OS |
9065 | } else { |
9066 | /* | |
9067 | * 1 to update inode | |
9068 | * 1 to remove old inode ref | |
9069 | * 1 to add new inode ref | |
9070 | */ | |
3538d68d | 9071 | trans_num_items += 3; |
c1621871 | 9072 | } |
a22285a6 | 9073 | /* |
c1621871 OS |
9074 | * 1 to remove old dir item |
9075 | * 1 to remove old dir index | |
c1621871 OS |
9076 | * 1 to add new dir item |
9077 | * 1 to add new dir index | |
a22285a6 | 9078 | */ |
3538d68d OS |
9079 | trans_num_items += 4; |
9080 | /* 1 to update new parent inode if it's not the same as the old parent */ | |
c1621871 OS |
9081 | if (new_dir != old_dir) |
9082 | trans_num_items++; | |
9083 | if (new_inode) { | |
9084 | /* | |
9085 | * 1 to update inode | |
9086 | * 1 to remove inode ref | |
9087 | * 1 to remove dir item | |
9088 | * 1 to remove dir index | |
9089 | * 1 to possibly add orphan item | |
9090 | */ | |
9091 | trans_num_items += 5; | |
9092 | } | |
5062af35 | 9093 | trans = btrfs_start_transaction(root, trans_num_items); |
b44c59a8 | 9094 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9095 | ret = PTR_ERR(trans); |
9096 | goto out_notrans; | |
9097 | } | |
76dda93c | 9098 | |
b0fec6fd JB |
9099 | if (dest != root) { |
9100 | ret = btrfs_record_root_in_trans(trans, dest); | |
9101 | if (ret) | |
9102 | goto out_fail; | |
9103 | } | |
5f39d397 | 9104 | |
877574e2 | 9105 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9106 | if (ret) |
9107 | goto out_fail; | |
5a3f23d5 | 9108 | |
67de1176 | 9109 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9110 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9111 | /* force full log commit if subvolume involved. */ |
90787766 | 9112 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9113 | } else { |
6db75318 STD |
9114 | ret = btrfs_insert_inode_ref(trans, dest, &new_fname.disk_name, |
9115 | old_ino, btrfs_ino(BTRFS_I(new_dir)), | |
9116 | index); | |
a5719521 YZ |
9117 | if (ret) |
9118 | goto out_fail; | |
4df27c4d | 9119 | } |
5a3f23d5 | 9120 | |
0c4d2d95 JB |
9121 | inode_inc_iversion(old_dir); |
9122 | inode_inc_iversion(new_dir); | |
9123 | inode_inc_iversion(old_inode); | |
130f1eca | 9124 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); |
5f39d397 | 9125 | |
12fcfd22 | 9126 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 | 9127 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
59fcf388 | 9128 | BTRFS_I(old_inode), true); |
12fcfd22 | 9129 | |
33345d01 | 9130 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
5b7544cb | 9131 | ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); |
4df27c4d | 9132 | } else { |
4467af88 | 9133 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
e43eec81 | 9134 | BTRFS_I(d_inode(old_dentry)), |
6db75318 | 9135 | &old_fname.disk_name, &rename_ctx); |
92986796 | 9136 | if (!ret) |
8b9d0322 | 9137 | ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); |
4df27c4d | 9138 | } |
79787eaa | 9139 | if (ret) { |
66642832 | 9140 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9141 | goto out_fail; |
9142 | } | |
39279cc3 CM |
9143 | |
9144 | if (new_inode) { | |
0c4d2d95 | 9145 | inode_inc_iversion(new_inode); |
4a0cc7ca | 9146 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9147 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
5b7544cb | 9148 | ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); |
4df27c4d YZ |
9149 | BUG_ON(new_inode->i_nlink == 0); |
9150 | } else { | |
4467af88 | 9151 | ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9152 | BTRFS_I(d_inode(new_dentry)), |
6db75318 | 9153 | &new_fname.disk_name); |
4df27c4d | 9154 | } |
4ef31a45 | 9155 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9156 | ret = btrfs_orphan_add(trans, |
9157 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9158 | if (ret) { |
66642832 | 9159 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9160 | goto out_fail; |
9161 | } | |
39279cc3 | 9162 | } |
aec7477b | 9163 | |
db0a669f | 9164 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 9165 | &new_fname.disk_name, 0, index); |
79787eaa | 9166 | if (ret) { |
66642832 | 9167 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9168 | goto out_fail; |
9169 | } | |
39279cc3 | 9170 | |
67de1176 MX |
9171 | if (old_inode->i_nlink == 1) |
9172 | BTRFS_I(old_inode)->dir_index = index; | |
9173 | ||
259c4b96 | 9174 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 9175 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 9176 | rename_ctx.index, new_dentry->d_parent); |
cdd1fedf DF |
9177 | |
9178 | if (flags & RENAME_WHITEOUT) { | |
caae78e0 | 9179 | ret = btrfs_create_new_inode(trans, &whiteout_args); |
cdd1fedf | 9180 | if (ret) { |
66642832 | 9181 | btrfs_abort_transaction(trans, ret); |
cdd1fedf | 9182 | goto out_fail; |
caae78e0 OS |
9183 | } else { |
9184 | unlock_new_inode(whiteout_args.inode); | |
9185 | iput(whiteout_args.inode); | |
9186 | whiteout_args.inode = NULL; | |
cdd1fedf | 9187 | } |
4df27c4d | 9188 | } |
39279cc3 | 9189 | out_fail: |
75b463d2 FM |
9190 | ret2 = btrfs_end_transaction(trans); |
9191 | ret = ret ? ret : ret2; | |
b44c59a8 | 9192 | out_notrans: |
33345d01 | 9193 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9194 | up_read(&fs_info->subvol_sem); |
a1fd0c35 | 9195 | if (flags & RENAME_WHITEOUT) |
3538d68d OS |
9196 | btrfs_new_inode_args_destroy(&whiteout_args); |
9197 | out_whiteout_inode: | |
9198 | if (flags & RENAME_WHITEOUT) | |
9199 | iput(whiteout_args.inode); | |
ab3c5c18 STD |
9200 | out_fscrypt_names: |
9201 | fscrypt_free_filename(&old_fname); | |
9202 | fscrypt_free_filename(&new_fname); | |
39279cc3 CM |
9203 | return ret; |
9204 | } | |
9205 | ||
e18275ae | 9206 | static int btrfs_rename2(struct mnt_idmap *idmap, struct inode *old_dir, |
549c7297 CB |
9207 | struct dentry *old_dentry, struct inode *new_dir, |
9208 | struct dentry *new_dentry, unsigned int flags) | |
80ace85c | 9209 | { |
ca6dee6b FM |
9210 | int ret; |
9211 | ||
cdd1fedf | 9212 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9213 | return -EINVAL; |
9214 | ||
cdd1fedf | 9215 | if (flags & RENAME_EXCHANGE) |
ca6dee6b FM |
9216 | ret = btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
9217 | new_dentry); | |
9218 | else | |
f2d40141 | 9219 | ret = btrfs_rename(idmap, old_dir, old_dentry, new_dir, |
ca6dee6b | 9220 | new_dentry, flags); |
cdd1fedf | 9221 | |
ca6dee6b FM |
9222 | btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info); |
9223 | ||
9224 | return ret; | |
80ace85c MS |
9225 | } |
9226 | ||
3a2f8c07 NB |
9227 | struct btrfs_delalloc_work { |
9228 | struct inode *inode; | |
9229 | struct completion completion; | |
9230 | struct list_head list; | |
9231 | struct btrfs_work work; | |
9232 | }; | |
9233 | ||
8ccf6f19 MX |
9234 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9235 | { | |
9236 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9237 | struct inode *inode; |
8ccf6f19 MX |
9238 | |
9239 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9240 | work); | |
9f23e289 | 9241 | inode = delalloc_work->inode; |
30424601 DS |
9242 | filemap_flush(inode->i_mapping); |
9243 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9244 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9245 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9246 | |
076da91c | 9247 | iput(inode); |
8ccf6f19 MX |
9248 | complete(&delalloc_work->completion); |
9249 | } | |
9250 | ||
3a2f8c07 | 9251 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9252 | { |
9253 | struct btrfs_delalloc_work *work; | |
9254 | ||
100d5702 | 9255 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9256 | if (!work) |
9257 | return NULL; | |
9258 | ||
9259 | init_completion(&work->completion); | |
9260 | INIT_LIST_HEAD(&work->list); | |
9261 | work->inode = inode; | |
078b8b90 | 9262 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL); |
8ccf6f19 MX |
9263 | |
9264 | return work; | |
9265 | } | |
9266 | ||
d352ac68 CM |
9267 | /* |
9268 | * some fairly slow code that needs optimization. This walks the list | |
9269 | * of all the inodes with pending delalloc and forces them to disk. | |
9270 | */ | |
e076ab2a JB |
9271 | static int start_delalloc_inodes(struct btrfs_root *root, |
9272 | struct writeback_control *wbc, bool snapshot, | |
3d45f221 | 9273 | bool in_reclaim_context) |
ea8c2819 | 9274 | { |
ea8c2819 | 9275 | struct btrfs_inode *binode; |
5b21f2ed | 9276 | struct inode *inode; |
8ccf6f19 | 9277 | struct btrfs_delalloc_work *work, *next; |
84af994b RJ |
9278 | LIST_HEAD(works); |
9279 | LIST_HEAD(splice); | |
8ccf6f19 | 9280 | int ret = 0; |
e076ab2a | 9281 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
ea8c2819 | 9282 | |
573bfb72 | 9283 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9284 | spin_lock(&root->delalloc_lock); |
9285 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9286 | while (!list_empty(&splice)) { |
9287 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9288 | delalloc_inodes); |
1eafa6c7 | 9289 | |
eb73c1b7 MX |
9290 | list_move_tail(&binode->delalloc_inodes, |
9291 | &root->delalloc_inodes); | |
3d45f221 FM |
9292 | |
9293 | if (in_reclaim_context && | |
9294 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) | |
9295 | continue; | |
9296 | ||
5b21f2ed | 9297 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9298 | if (!inode) { |
eb73c1b7 | 9299 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9300 | continue; |
df0af1a5 | 9301 | } |
eb73c1b7 | 9302 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9303 | |
3cd24c69 EL |
9304 | if (snapshot) |
9305 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9306 | &binode->runtime_flags); | |
e076ab2a JB |
9307 | if (full_flush) { |
9308 | work = btrfs_alloc_delalloc_work(inode); | |
9309 | if (!work) { | |
9310 | iput(inode); | |
9311 | ret = -ENOMEM; | |
9312 | goto out; | |
9313 | } | |
9314 | list_add_tail(&work->list, &works); | |
9315 | btrfs_queue_work(root->fs_info->flush_workers, | |
9316 | &work->work); | |
9317 | } else { | |
b3776305 | 9318 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
e55cf7ca | 9319 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
e076ab2a | 9320 | if (ret || wbc->nr_to_write <= 0) |
b4912139 JB |
9321 | goto out; |
9322 | } | |
5b21f2ed | 9323 | cond_resched(); |
eb73c1b7 | 9324 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9325 | } |
eb73c1b7 | 9326 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9327 | |
a1ecaabb | 9328 | out: |
eb73c1b7 MX |
9329 | list_for_each_entry_safe(work, next, &works, list) { |
9330 | list_del_init(&work->list); | |
40012f96 NB |
9331 | wait_for_completion(&work->completion); |
9332 | kfree(work); | |
eb73c1b7 MX |
9333 | } |
9334 | ||
81f1d390 | 9335 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9336 | spin_lock(&root->delalloc_lock); |
9337 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9338 | spin_unlock(&root->delalloc_lock); | |
9339 | } | |
573bfb72 | 9340 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9341 | return ret; |
9342 | } | |
1eafa6c7 | 9343 | |
f9baa501 | 9344 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context) |
eb73c1b7 | 9345 | { |
e076ab2a JB |
9346 | struct writeback_control wbc = { |
9347 | .nr_to_write = LONG_MAX, | |
9348 | .sync_mode = WB_SYNC_NONE, | |
9349 | .range_start = 0, | |
9350 | .range_end = LLONG_MAX, | |
9351 | }; | |
0b246afa | 9352 | struct btrfs_fs_info *fs_info = root->fs_info; |
1eafa6c7 | 9353 | |
84961539 | 9354 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9355 | return -EROFS; |
9356 | ||
f9baa501 | 9357 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
eb73c1b7 MX |
9358 | } |
9359 | ||
9db4dc24 | 9360 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr, |
3d45f221 | 9361 | bool in_reclaim_context) |
eb73c1b7 | 9362 | { |
e076ab2a | 9363 | struct writeback_control wbc = { |
9db4dc24 | 9364 | .nr_to_write = nr, |
e076ab2a JB |
9365 | .sync_mode = WB_SYNC_NONE, |
9366 | .range_start = 0, | |
9367 | .range_end = LLONG_MAX, | |
9368 | }; | |
eb73c1b7 | 9369 | struct btrfs_root *root; |
84af994b | 9370 | LIST_HEAD(splice); |
eb73c1b7 MX |
9371 | int ret; |
9372 | ||
84961539 | 9373 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9374 | return -EROFS; |
9375 | ||
573bfb72 | 9376 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9377 | spin_lock(&fs_info->delalloc_root_lock); |
9378 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
d7830b71 | 9379 | while (!list_empty(&splice)) { |
e076ab2a JB |
9380 | /* |
9381 | * Reset nr_to_write here so we know that we're doing a full | |
9382 | * flush. | |
9383 | */ | |
9db4dc24 | 9384 | if (nr == LONG_MAX) |
e076ab2a JB |
9385 | wbc.nr_to_write = LONG_MAX; |
9386 | ||
eb73c1b7 MX |
9387 | root = list_first_entry(&splice, struct btrfs_root, |
9388 | delalloc_root); | |
00246528 | 9389 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9390 | BUG_ON(!root); |
9391 | list_move_tail(&root->delalloc_root, | |
9392 | &fs_info->delalloc_roots); | |
9393 | spin_unlock(&fs_info->delalloc_root_lock); | |
9394 | ||
e076ab2a | 9395 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
00246528 | 9396 | btrfs_put_root(root); |
e076ab2a | 9397 | if (ret < 0 || wbc.nr_to_write <= 0) |
eb73c1b7 | 9398 | goto out; |
eb73c1b7 | 9399 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9400 | } |
eb73c1b7 | 9401 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9402 | |
6c255e67 | 9403 | ret = 0; |
eb73c1b7 | 9404 | out: |
81f1d390 | 9405 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9406 | spin_lock(&fs_info->delalloc_root_lock); |
9407 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9408 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9409 | } |
573bfb72 | 9410 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9411 | return ret; |
ea8c2819 CM |
9412 | } |
9413 | ||
7a77db95 | 9414 | static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 9415 | struct dentry *dentry, const char *symname) |
39279cc3 | 9416 | { |
0b246afa | 9417 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9418 | struct btrfs_trans_handle *trans; |
9419 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9420 | struct btrfs_path *path; | |
9421 | struct btrfs_key key; | |
a1fd0c35 | 9422 | struct inode *inode; |
3538d68d OS |
9423 | struct btrfs_new_inode_args new_inode_args = { |
9424 | .dir = dir, | |
9425 | .dentry = dentry, | |
9426 | }; | |
9427 | unsigned int trans_num_items; | |
39279cc3 | 9428 | int err; |
39279cc3 CM |
9429 | int name_len; |
9430 | int datasize; | |
5f39d397 | 9431 | unsigned long ptr; |
39279cc3 | 9432 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9433 | struct extent_buffer *leaf; |
39279cc3 | 9434 | |
f06becc4 | 9435 | name_len = strlen(symname); |
0b246afa | 9436 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9437 | return -ENAMETOOLONG; |
1832a6d5 | 9438 | |
a1fd0c35 OS |
9439 | inode = new_inode(dir->i_sb); |
9440 | if (!inode) | |
9441 | return -ENOMEM; | |
f2d40141 | 9442 | inode_init_owner(idmap, inode, dir, S_IFLNK | S_IRWXUGO); |
a1fd0c35 OS |
9443 | inode->i_op = &btrfs_symlink_inode_operations; |
9444 | inode_nohighmem(inode); | |
9445 | inode->i_mapping->a_ops = &btrfs_aops; | |
caae78e0 OS |
9446 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
9447 | inode_set_bytes(inode, name_len); | |
a1fd0c35 | 9448 | |
3538d68d OS |
9449 | new_inode_args.inode = inode; |
9450 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
9451 | if (err) |
9452 | goto out_inode; | |
3538d68d OS |
9453 | /* 1 additional item for the inline extent */ |
9454 | trans_num_items++; | |
9455 | ||
9456 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 9457 | if (IS_ERR(trans)) { |
3538d68d OS |
9458 | err = PTR_ERR(trans); |
9459 | goto out_new_inode_args; | |
a1fd0c35 | 9460 | } |
1832a6d5 | 9461 | |
caae78e0 | 9462 | err = btrfs_create_new_inode(trans, &new_inode_args); |
b0d5d10f | 9463 | if (err) |
caae78e0 | 9464 | goto out; |
ad19db71 | 9465 | |
39279cc3 | 9466 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9467 | if (!path) { |
9468 | err = -ENOMEM; | |
caae78e0 OS |
9469 | btrfs_abort_transaction(trans, err); |
9470 | discard_new_inode(inode); | |
9471 | inode = NULL; | |
9472 | goto out; | |
d8926bb3 | 9473 | } |
4a0cc7ca | 9474 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9475 | key.offset = 0; |
962a298f | 9476 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9477 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9478 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9479 | datasize); | |
54aa1f4d | 9480 | if (err) { |
caae78e0 | 9481 | btrfs_abort_transaction(trans, err); |
b0839166 | 9482 | btrfs_free_path(path); |
caae78e0 OS |
9483 | discard_new_inode(inode); |
9484 | inode = NULL; | |
9485 | goto out; | |
54aa1f4d | 9486 | } |
5f39d397 CM |
9487 | leaf = path->nodes[0]; |
9488 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9489 | struct btrfs_file_extent_item); | |
9490 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9491 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9492 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9493 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9494 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9495 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9496 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9497 | ||
39279cc3 | 9498 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 | 9499 | write_extent_buffer(leaf, symname, ptr, name_len); |
50564b65 | 9500 | btrfs_mark_buffer_dirty(trans, leaf); |
39279cc3 | 9501 | btrfs_free_path(path); |
5f39d397 | 9502 | |
1e2e547a | 9503 | d_instantiate_new(dentry, inode); |
caae78e0 OS |
9504 | err = 0; |
9505 | out: | |
3a45bb20 | 9506 | btrfs_end_transaction(trans); |
2ff7e61e | 9507 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
9508 | out_new_inode_args: |
9509 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
9510 | out_inode: |
9511 | if (err) | |
9512 | iput(inode); | |
39279cc3 CM |
9513 | return err; |
9514 | } | |
16432985 | 9515 | |
8fccebfa FM |
9516 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
9517 | struct btrfs_trans_handle *trans_in, | |
90dffd0c NB |
9518 | struct btrfs_inode *inode, |
9519 | struct btrfs_key *ins, | |
203f44c5 QW |
9520 | u64 file_offset) |
9521 | { | |
9522 | struct btrfs_file_extent_item stack_fi; | |
bf385648 | 9523 | struct btrfs_replace_extent_info extent_info; |
8fccebfa FM |
9524 | struct btrfs_trans_handle *trans = trans_in; |
9525 | struct btrfs_path *path; | |
203f44c5 QW |
9526 | u64 start = ins->objectid; |
9527 | u64 len = ins->offset; | |
9e65bfca | 9528 | u64 qgroup_released = 0; |
9729f10a | 9529 | int ret; |
203f44c5 QW |
9530 | |
9531 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9532 | ||
9533 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9534 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9535 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9536 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9537 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9538 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9539 | /* Encryption and other encoding is reserved and all 0 */ | |
9540 | ||
9e65bfca BB |
9541 | ret = btrfs_qgroup_release_data(inode, file_offset, len, &qgroup_released); |
9542 | if (ret < 0) | |
9543 | return ERR_PTR(ret); | |
8fccebfa FM |
9544 | |
9545 | if (trans) { | |
90dffd0c | 9546 | ret = insert_reserved_file_extent(trans, inode, |
2766ff61 | 9547 | file_offset, &stack_fi, |
fbf48bb0 | 9548 | true, qgroup_released); |
8fccebfa | 9549 | if (ret) |
a3ee79bd | 9550 | goto free_qgroup; |
8fccebfa FM |
9551 | return trans; |
9552 | } | |
9553 | ||
9554 | extent_info.disk_offset = start; | |
9555 | extent_info.disk_len = len; | |
9556 | extent_info.data_offset = 0; | |
9557 | extent_info.data_len = len; | |
9558 | extent_info.file_offset = file_offset; | |
9559 | extent_info.extent_buf = (char *)&stack_fi; | |
8fccebfa | 9560 | extent_info.is_new_extent = true; |
983d8209 | 9561 | extent_info.update_times = true; |
fbf48bb0 | 9562 | extent_info.qgroup_reserved = qgroup_released; |
8fccebfa FM |
9563 | extent_info.insertions = 0; |
9564 | ||
9565 | path = btrfs_alloc_path(); | |
a3ee79bd QW |
9566 | if (!path) { |
9567 | ret = -ENOMEM; | |
9568 | goto free_qgroup; | |
9569 | } | |
8fccebfa | 9570 | |
bfc78479 | 9571 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
8fccebfa FM |
9572 | file_offset + len - 1, &extent_info, |
9573 | &trans); | |
9574 | btrfs_free_path(path); | |
9575 | if (ret) | |
a3ee79bd | 9576 | goto free_qgroup; |
8fccebfa | 9577 | return trans; |
a3ee79bd QW |
9578 | |
9579 | free_qgroup: | |
9580 | /* | |
9581 | * We have released qgroup data range at the beginning of the function, | |
9582 | * and normally qgroup_released bytes will be freed when committing | |
9583 | * transaction. | |
9584 | * But if we error out early, we have to free what we have released | |
9585 | * or we leak qgroup data reservation. | |
9586 | */ | |
9587 | btrfs_qgroup_free_refroot(inode->root->fs_info, | |
9588 | inode->root->root_key.objectid, qgroup_released, | |
9589 | BTRFS_QGROUP_RSV_DATA); | |
9590 | return ERR_PTR(ret); | |
203f44c5 | 9591 | } |
8fccebfa | 9592 | |
0af3d00b JB |
9593 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9594 | u64 start, u64 num_bytes, u64 min_size, | |
9595 | loff_t actual_len, u64 *alloc_hint, | |
9596 | struct btrfs_trans_handle *trans) | |
d899e052 | 9597 | { |
0b246afa | 9598 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 | 9599 | struct extent_map *em; |
d899e052 YZ |
9600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9601 | struct btrfs_key ins; | |
d899e052 | 9602 | u64 cur_offset = start; |
b778cf96 | 9603 | u64 clear_offset = start; |
55a61d1d | 9604 | u64 i_size; |
154ea289 | 9605 | u64 cur_bytes; |
0b670dc4 | 9606 | u64 last_alloc = (u64)-1; |
d899e052 | 9607 | int ret = 0; |
0af3d00b | 9608 | bool own_trans = true; |
18513091 | 9609 | u64 end = start + num_bytes - 1; |
d899e052 | 9610 | |
0af3d00b JB |
9611 | if (trans) |
9612 | own_trans = false; | |
d899e052 | 9613 | while (num_bytes > 0) { |
ee22184b | 9614 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9615 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9616 | /* |
9617 | * If we are severely fragmented we could end up with really | |
9618 | * small allocations, so if the allocator is returning small | |
9619 | * chunks lets make its job easier by only searching for those | |
9620 | * sized chunks. | |
9621 | */ | |
9622 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9623 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9624 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
8fccebfa | 9625 | if (ret) |
a22285a6 | 9626 | break; |
b778cf96 JB |
9627 | |
9628 | /* | |
9629 | * We've reserved this space, and thus converted it from | |
9630 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9631 | * from here on out we will only need to clear our reservation | |
9632 | * for the remaining unreserved area, so advance our | |
9633 | * clear_offset by our extent size. | |
9634 | */ | |
9635 | clear_offset += ins.offset; | |
5a303d5d | 9636 | |
0b670dc4 | 9637 | last_alloc = ins.offset; |
90dffd0c NB |
9638 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
9639 | &ins, cur_offset); | |
1afc708d FM |
9640 | /* |
9641 | * Now that we inserted the prealloc extent we can finally | |
9642 | * decrement the number of reservations in the block group. | |
9643 | * If we did it before, we could race with relocation and have | |
9644 | * relocation miss the reserved extent, making it fail later. | |
9645 | */ | |
9646 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
8fccebfa FM |
9647 | if (IS_ERR(trans)) { |
9648 | ret = PTR_ERR(trans); | |
2ff7e61e | 9649 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9650 | ins.offset, 0); |
79787eaa JM |
9651 | break; |
9652 | } | |
31193213 | 9653 | |
5dc562c5 JB |
9654 | em = alloc_extent_map(); |
9655 | if (!em) { | |
a1ba4c08 FM |
9656 | btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset, |
9657 | cur_offset + ins.offset - 1, false); | |
23e3337f | 9658 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 JB |
9659 | goto next; |
9660 | } | |
9661 | ||
9662 | em->start = cur_offset; | |
9663 | em->orig_start = cur_offset; | |
9664 | em->len = ins.offset; | |
9665 | em->block_start = ins.objectid; | |
9666 | em->block_len = ins.offset; | |
b4939680 | 9667 | em->orig_block_len = ins.offset; |
cc95bef6 | 9668 | em->ram_bytes = ins.offset; |
f86f7a75 | 9669 | em->flags |= EXTENT_FLAG_PREALLOC; |
5dc562c5 JB |
9670 | em->generation = trans->transid; |
9671 | ||
a1ba4c08 | 9672 | ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true); |
5dc562c5 JB |
9673 | free_extent_map(em); |
9674 | next: | |
d899e052 YZ |
9675 | num_bytes -= ins.offset; |
9676 | cur_offset += ins.offset; | |
efa56464 | 9677 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9678 | |
0c4d2d95 | 9679 | inode_inc_iversion(inode); |
2a9462de | 9680 | inode_set_ctime_current(inode); |
6cbff00f | 9681 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9682 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9683 | (actual_len > inode->i_size) && |
9684 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9685 | if (cur_offset > actual_len) |
55a61d1d | 9686 | i_size = actual_len; |
d1ea6a61 | 9687 | else |
55a61d1d JB |
9688 | i_size = cur_offset; |
9689 | i_size_write(inode, i_size); | |
76aea537 | 9690 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
5a303d5d YZ |
9691 | } |
9692 | ||
8b9d0322 | 9693 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
79787eaa JM |
9694 | |
9695 | if (ret) { | |
66642832 | 9696 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9697 | if (own_trans) |
3a45bb20 | 9698 | btrfs_end_transaction(trans); |
79787eaa JM |
9699 | break; |
9700 | } | |
d899e052 | 9701 | |
8fccebfa | 9702 | if (own_trans) { |
3a45bb20 | 9703 | btrfs_end_transaction(trans); |
8fccebfa FM |
9704 | trans = NULL; |
9705 | } | |
5a303d5d | 9706 | } |
b778cf96 | 9707 | if (clear_offset < end) |
25ce28ca | 9708 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 9709 | end - clear_offset + 1); |
d899e052 YZ |
9710 | return ret; |
9711 | } | |
9712 | ||
0af3d00b JB |
9713 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9714 | u64 start, u64 num_bytes, u64 min_size, | |
9715 | loff_t actual_len, u64 *alloc_hint) | |
9716 | { | |
9717 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9718 | min_size, actual_len, alloc_hint, | |
9719 | NULL); | |
9720 | } | |
9721 | ||
9722 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9723 | struct btrfs_trans_handle *trans, int mode, | |
9724 | u64 start, u64 num_bytes, u64 min_size, | |
9725 | loff_t actual_len, u64 *alloc_hint) | |
9726 | { | |
9727 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9728 | min_size, actual_len, alloc_hint, trans); | |
9729 | } | |
9730 | ||
4609e1f1 | 9731 | static int btrfs_permission(struct mnt_idmap *idmap, |
549c7297 | 9732 | struct inode *inode, int mask) |
fdebe2bd | 9733 | { |
b83cc969 | 9734 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9735 | umode_t mode = inode->i_mode; |
b83cc969 | 9736 | |
cb6db4e5 JM |
9737 | if (mask & MAY_WRITE && |
9738 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9739 | if (btrfs_root_readonly(root)) | |
9740 | return -EROFS; | |
9741 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9742 | return -EACCES; | |
9743 | } | |
4609e1f1 | 9744 | return generic_permission(idmap, inode, mask); |
fdebe2bd | 9745 | } |
39279cc3 | 9746 | |
011e2b71 | 9747 | static int btrfs_tmpfile(struct mnt_idmap *idmap, struct inode *dir, |
863f144f | 9748 | struct file *file, umode_t mode) |
ef3b9af5 | 9749 | { |
2ff7e61e | 9750 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
9751 | struct btrfs_trans_handle *trans; |
9752 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
a1fd0c35 | 9753 | struct inode *inode; |
3538d68d OS |
9754 | struct btrfs_new_inode_args new_inode_args = { |
9755 | .dir = dir, | |
863f144f | 9756 | .dentry = file->f_path.dentry, |
3538d68d OS |
9757 | .orphan = true, |
9758 | }; | |
9759 | unsigned int trans_num_items; | |
a1fd0c35 OS |
9760 | int ret; |
9761 | ||
9762 | inode = new_inode(dir->i_sb); | |
9763 | if (!inode) | |
9764 | return -ENOMEM; | |
f2d40141 | 9765 | inode_init_owner(idmap, inode, dir, mode); |
a1fd0c35 OS |
9766 | inode->i_fop = &btrfs_file_operations; |
9767 | inode->i_op = &btrfs_file_inode_operations; | |
9768 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 | 9769 | |
3538d68d OS |
9770 | new_inode_args.inode = inode; |
9771 | ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
9772 | if (ret) |
9773 | goto out_inode; | |
3538d68d OS |
9774 | |
9775 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 9776 | if (IS_ERR(trans)) { |
3538d68d OS |
9777 | ret = PTR_ERR(trans); |
9778 | goto out_new_inode_args; | |
a1fd0c35 | 9779 | } |
ef3b9af5 | 9780 | |
caae78e0 | 9781 | ret = btrfs_create_new_inode(trans, &new_inode_args); |
ef3b9af5 | 9782 | |
5762b5c9 | 9783 | /* |
3538d68d OS |
9784 | * We set number of links to 0 in btrfs_create_new_inode(), and here we |
9785 | * set it to 1 because d_tmpfile() will issue a warning if the count is | |
9786 | * 0, through: | |
5762b5c9 FM |
9787 | * |
9788 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9789 | */ | |
9790 | set_nlink(inode, 1); | |
caae78e0 OS |
9791 | |
9792 | if (!ret) { | |
863f144f | 9793 | d_tmpfile(file, inode); |
caae78e0 OS |
9794 | unlock_new_inode(inode); |
9795 | mark_inode_dirty(inode); | |
9796 | } | |
9797 | ||
3a45bb20 | 9798 | btrfs_end_transaction(trans); |
2ff7e61e | 9799 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
9800 | out_new_inode_args: |
9801 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
9802 | out_inode: |
9803 | if (ret) | |
9804 | iput(inode); | |
863f144f | 9805 | return finish_open_simple(file, ret); |
ef3b9af5 FM |
9806 | } |
9807 | ||
d2a91064 | 9808 | void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) |
c6100a4b | 9809 | { |
d2a91064 | 9810 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
c6100a4b JB |
9811 | unsigned long index = start >> PAGE_SHIFT; |
9812 | unsigned long end_index = end >> PAGE_SHIFT; | |
9813 | struct page *page; | |
d2a91064 | 9814 | u32 len; |
c6100a4b | 9815 | |
d2a91064 QW |
9816 | ASSERT(end + 1 - start <= U32_MAX); |
9817 | len = end + 1 - start; | |
c6100a4b | 9818 | while (index <= end_index) { |
d2a91064 | 9819 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
c6100a4b | 9820 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
d2a91064 QW |
9821 | |
9822 | btrfs_page_set_writeback(fs_info, page, start, len); | |
c6100a4b JB |
9823 | put_page(page); |
9824 | index++; | |
9825 | } | |
9826 | } | |
9827 | ||
3ea4dc5b OS |
9828 | int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info, |
9829 | int compress_type) | |
1881fba8 OS |
9830 | { |
9831 | switch (compress_type) { | |
9832 | case BTRFS_COMPRESS_NONE: | |
9833 | return BTRFS_ENCODED_IO_COMPRESSION_NONE; | |
9834 | case BTRFS_COMPRESS_ZLIB: | |
9835 | return BTRFS_ENCODED_IO_COMPRESSION_ZLIB; | |
9836 | case BTRFS_COMPRESS_LZO: | |
9837 | /* | |
9838 | * The LZO format depends on the sector size. 64K is the maximum | |
9839 | * sector size that we support. | |
9840 | */ | |
9841 | if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K) | |
9842 | return -EINVAL; | |
9843 | return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + | |
9844 | (fs_info->sectorsize_bits - 12); | |
9845 | case BTRFS_COMPRESS_ZSTD: | |
9846 | return BTRFS_ENCODED_IO_COMPRESSION_ZSTD; | |
9847 | default: | |
9848 | return -EUCLEAN; | |
9849 | } | |
9850 | } | |
9851 | ||
9852 | static ssize_t btrfs_encoded_read_inline( | |
9853 | struct kiocb *iocb, | |
9854 | struct iov_iter *iter, u64 start, | |
9855 | u64 lockend, | |
9856 | struct extent_state **cached_state, | |
9857 | u64 extent_start, size_t count, | |
9858 | struct btrfs_ioctl_encoded_io_args *encoded, | |
9859 | bool *unlocked) | |
9860 | { | |
9861 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
9862 | struct btrfs_root *root = inode->root; | |
9863 | struct btrfs_fs_info *fs_info = root->fs_info; | |
9864 | struct extent_io_tree *io_tree = &inode->io_tree; | |
9865 | struct btrfs_path *path; | |
9866 | struct extent_buffer *leaf; | |
9867 | struct btrfs_file_extent_item *item; | |
9868 | u64 ram_bytes; | |
9869 | unsigned long ptr; | |
9870 | void *tmp; | |
9871 | ssize_t ret; | |
9872 | ||
9873 | path = btrfs_alloc_path(); | |
9874 | if (!path) { | |
9875 | ret = -ENOMEM; | |
9876 | goto out; | |
9877 | } | |
9878 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), | |
9879 | extent_start, 0); | |
9880 | if (ret) { | |
9881 | if (ret > 0) { | |
9882 | /* The extent item disappeared? */ | |
9883 | ret = -EIO; | |
9884 | } | |
9885 | goto out; | |
9886 | } | |
9887 | leaf = path->nodes[0]; | |
9888 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
9889 | ||
9890 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); | |
9891 | ptr = btrfs_file_extent_inline_start(item); | |
9892 | ||
9893 | encoded->len = min_t(u64, extent_start + ram_bytes, | |
9894 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
9895 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
9896 | btrfs_file_extent_compression(leaf, item)); | |
9897 | if (ret < 0) | |
9898 | goto out; | |
9899 | encoded->compression = ret; | |
9900 | if (encoded->compression) { | |
9901 | size_t inline_size; | |
9902 | ||
9903 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
9904 | path->slots[0]); | |
9905 | if (inline_size > count) { | |
9906 | ret = -ENOBUFS; | |
9907 | goto out; | |
9908 | } | |
9909 | count = inline_size; | |
9910 | encoded->unencoded_len = ram_bytes; | |
9911 | encoded->unencoded_offset = iocb->ki_pos - extent_start; | |
9912 | } else { | |
9913 | count = min_t(u64, count, encoded->len); | |
9914 | encoded->len = count; | |
9915 | encoded->unencoded_len = count; | |
9916 | ptr += iocb->ki_pos - extent_start; | |
9917 | } | |
9918 | ||
9919 | tmp = kmalloc(count, GFP_NOFS); | |
9920 | if (!tmp) { | |
9921 | ret = -ENOMEM; | |
9922 | goto out; | |
9923 | } | |
9924 | read_extent_buffer(leaf, tmp, ptr, count); | |
9925 | btrfs_release_path(path); | |
570eb97b | 9926 | unlock_extent(io_tree, start, lockend, cached_state); |
e5d4d75b | 9927 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
9928 | *unlocked = true; |
9929 | ||
9930 | ret = copy_to_iter(tmp, count, iter); | |
9931 | if (ret != count) | |
9932 | ret = -EFAULT; | |
9933 | kfree(tmp); | |
9934 | out: | |
9935 | btrfs_free_path(path); | |
9936 | return ret; | |
9937 | } | |
9938 | ||
9939 | struct btrfs_encoded_read_private { | |
1881fba8 OS |
9940 | wait_queue_head_t wait; |
9941 | atomic_t pending; | |
9942 | blk_status_t status; | |
1881fba8 OS |
9943 | }; |
9944 | ||
917f32a2 | 9945 | static void btrfs_encoded_read_endio(struct btrfs_bio *bbio) |
1881fba8 | 9946 | { |
917f32a2 | 9947 | struct btrfs_encoded_read_private *priv = bbio->private; |
1881fba8 | 9948 | |
7609afac | 9949 | if (bbio->bio.bi_status) { |
1881fba8 OS |
9950 | /* |
9951 | * The memory barrier implied by the atomic_dec_return() here | |
9952 | * pairs with the memory barrier implied by the | |
9953 | * atomic_dec_return() or io_wait_event() in | |
9954 | * btrfs_encoded_read_regular_fill_pages() to ensure that this | |
9955 | * write is observed before the load of status in | |
9956 | * btrfs_encoded_read_regular_fill_pages(). | |
9957 | */ | |
7609afac | 9958 | WRITE_ONCE(priv->status, bbio->bio.bi_status); |
1881fba8 OS |
9959 | } |
9960 | if (!atomic_dec_return(&priv->pending)) | |
9961 | wake_up(&priv->wait); | |
917f32a2 | 9962 | bio_put(&bbio->bio); |
1881fba8 OS |
9963 | } |
9964 | ||
3ea4dc5b OS |
9965 | int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, |
9966 | u64 file_offset, u64 disk_bytenr, | |
9967 | u64 disk_io_size, struct page **pages) | |
1881fba8 | 9968 | { |
4317ff00 | 9969 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1881fba8 | 9970 | struct btrfs_encoded_read_private priv = { |
1881fba8 | 9971 | .pending = ATOMIC_INIT(1), |
1881fba8 OS |
9972 | }; |
9973 | unsigned long i = 0; | |
b41bbd29 | 9974 | struct btrfs_bio *bbio; |
1881fba8 OS |
9975 | |
9976 | init_waitqueue_head(&priv.wait); | |
1881fba8 | 9977 | |
4317ff00 QW |
9978 | bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info, |
9979 | btrfs_encoded_read_endio, &priv); | |
b41bbd29 | 9980 | bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; |
4317ff00 | 9981 | bbio->inode = inode; |
1881fba8 | 9982 | |
34f888ce CH |
9983 | do { |
9984 | size_t bytes = min_t(u64, disk_io_size, PAGE_SIZE); | |
9985 | ||
b41bbd29 | 9986 | if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) { |
34f888ce | 9987 | atomic_inc(&priv.pending); |
b41bbd29 | 9988 | btrfs_submit_bio(bbio, 0); |
34f888ce | 9989 | |
4317ff00 | 9990 | bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info, |
b41bbd29 CH |
9991 | btrfs_encoded_read_endio, &priv); |
9992 | bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; | |
4317ff00 | 9993 | bbio->inode = inode; |
34f888ce | 9994 | continue; |
1881fba8 | 9995 | } |
34f888ce CH |
9996 | |
9997 | i++; | |
9998 | disk_bytenr += bytes; | |
9999 | disk_io_size -= bytes; | |
10000 | } while (disk_io_size); | |
10001 | ||
10002 | atomic_inc(&priv.pending); | |
b41bbd29 | 10003 | btrfs_submit_bio(bbio, 0); |
1881fba8 | 10004 | |
1881fba8 OS |
10005 | if (atomic_dec_return(&priv.pending)) |
10006 | io_wait_event(priv.wait, !atomic_read(&priv.pending)); | |
10007 | /* See btrfs_encoded_read_endio() for ordering. */ | |
10008 | return blk_status_to_errno(READ_ONCE(priv.status)); | |
10009 | } | |
10010 | ||
10011 | static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb, | |
10012 | struct iov_iter *iter, | |
10013 | u64 start, u64 lockend, | |
10014 | struct extent_state **cached_state, | |
10015 | u64 disk_bytenr, u64 disk_io_size, | |
10016 | size_t count, bool compressed, | |
10017 | bool *unlocked) | |
10018 | { | |
10019 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10020 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10021 | struct page **pages; | |
10022 | unsigned long nr_pages, i; | |
10023 | u64 cur; | |
10024 | size_t page_offset; | |
10025 | ssize_t ret; | |
10026 | ||
10027 | nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE); | |
10028 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); | |
10029 | if (!pages) | |
10030 | return -ENOMEM; | |
dd137dd1 STD |
10031 | ret = btrfs_alloc_page_array(nr_pages, pages); |
10032 | if (ret) { | |
10033 | ret = -ENOMEM; | |
10034 | goto out; | |
1881fba8 | 10035 | } |
1881fba8 OS |
10036 | |
10037 | ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr, | |
10038 | disk_io_size, pages); | |
10039 | if (ret) | |
10040 | goto out; | |
10041 | ||
570eb97b | 10042 | unlock_extent(io_tree, start, lockend, cached_state); |
e5d4d75b | 10043 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10044 | *unlocked = true; |
10045 | ||
10046 | if (compressed) { | |
10047 | i = 0; | |
10048 | page_offset = 0; | |
10049 | } else { | |
10050 | i = (iocb->ki_pos - start) >> PAGE_SHIFT; | |
10051 | page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1); | |
10052 | } | |
10053 | cur = 0; | |
10054 | while (cur < count) { | |
10055 | size_t bytes = min_t(size_t, count - cur, | |
10056 | PAGE_SIZE - page_offset); | |
10057 | ||
10058 | if (copy_page_to_iter(pages[i], page_offset, bytes, | |
10059 | iter) != bytes) { | |
10060 | ret = -EFAULT; | |
10061 | goto out; | |
10062 | } | |
10063 | i++; | |
10064 | cur += bytes; | |
10065 | page_offset = 0; | |
10066 | } | |
10067 | ret = count; | |
10068 | out: | |
10069 | for (i = 0; i < nr_pages; i++) { | |
10070 | if (pages[i]) | |
10071 | __free_page(pages[i]); | |
10072 | } | |
10073 | kfree(pages); | |
10074 | return ret; | |
10075 | } | |
10076 | ||
10077 | ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, | |
10078 | struct btrfs_ioctl_encoded_io_args *encoded) | |
10079 | { | |
10080 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10081 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
10082 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10083 | ssize_t ret; | |
10084 | size_t count = iov_iter_count(iter); | |
10085 | u64 start, lockend, disk_bytenr, disk_io_size; | |
10086 | struct extent_state *cached_state = NULL; | |
10087 | struct extent_map *em; | |
10088 | bool unlocked = false; | |
10089 | ||
10090 | file_accessed(iocb->ki_filp); | |
10091 | ||
29b6352b | 10092 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10093 | |
10094 | if (iocb->ki_pos >= inode->vfs_inode.i_size) { | |
e5d4d75b | 10095 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10096 | return 0; |
10097 | } | |
10098 | start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize); | |
10099 | /* | |
10100 | * We don't know how long the extent containing iocb->ki_pos is, but if | |
10101 | * it's compressed we know that it won't be longer than this. | |
10102 | */ | |
10103 | lockend = start + BTRFS_MAX_UNCOMPRESSED - 1; | |
10104 | ||
10105 | for (;;) { | |
10106 | struct btrfs_ordered_extent *ordered; | |
10107 | ||
10108 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, | |
10109 | lockend - start + 1); | |
10110 | if (ret) | |
10111 | goto out_unlock_inode; | |
570eb97b | 10112 | lock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10113 | ordered = btrfs_lookup_ordered_range(inode, start, |
10114 | lockend - start + 1); | |
10115 | if (!ordered) | |
10116 | break; | |
10117 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10118 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10119 | cond_resched(); |
10120 | } | |
10121 | ||
10122 | em = btrfs_get_extent(inode, NULL, 0, start, lockend - start + 1); | |
10123 | if (IS_ERR(em)) { | |
10124 | ret = PTR_ERR(em); | |
10125 | goto out_unlock_extent; | |
10126 | } | |
10127 | ||
10128 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10129 | u64 extent_start = em->start; | |
10130 | ||
10131 | /* | |
10132 | * For inline extents we get everything we need out of the | |
10133 | * extent item. | |
10134 | */ | |
10135 | free_extent_map(em); | |
10136 | em = NULL; | |
10137 | ret = btrfs_encoded_read_inline(iocb, iter, start, lockend, | |
10138 | &cached_state, extent_start, | |
10139 | count, encoded, &unlocked); | |
10140 | goto out; | |
10141 | } | |
10142 | ||
10143 | /* | |
10144 | * We only want to return up to EOF even if the extent extends beyond | |
10145 | * that. | |
10146 | */ | |
10147 | encoded->len = min_t(u64, extent_map_end(em), | |
10148 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
10149 | if (em->block_start == EXTENT_MAP_HOLE || | |
f86f7a75 | 10150 | (em->flags & EXTENT_FLAG_PREALLOC)) { |
1881fba8 OS |
10151 | disk_bytenr = EXTENT_MAP_HOLE; |
10152 | count = min_t(u64, count, encoded->len); | |
10153 | encoded->len = count; | |
10154 | encoded->unencoded_len = count; | |
f86f7a75 | 10155 | } else if (extent_map_is_compressed(em)) { |
1881fba8 OS |
10156 | disk_bytenr = em->block_start; |
10157 | /* | |
10158 | * Bail if the buffer isn't large enough to return the whole | |
10159 | * compressed extent. | |
10160 | */ | |
10161 | if (em->block_len > count) { | |
10162 | ret = -ENOBUFS; | |
10163 | goto out_em; | |
10164 | } | |
c1867eb3 DS |
10165 | disk_io_size = em->block_len; |
10166 | count = em->block_len; | |
1881fba8 OS |
10167 | encoded->unencoded_len = em->ram_bytes; |
10168 | encoded->unencoded_offset = iocb->ki_pos - em->orig_start; | |
10169 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
f86f7a75 | 10170 | extent_map_compression(em)); |
1881fba8 OS |
10171 | if (ret < 0) |
10172 | goto out_em; | |
10173 | encoded->compression = ret; | |
10174 | } else { | |
10175 | disk_bytenr = em->block_start + (start - em->start); | |
10176 | if (encoded->len > count) | |
10177 | encoded->len = count; | |
10178 | /* | |
10179 | * Don't read beyond what we locked. This also limits the page | |
10180 | * allocations that we'll do. | |
10181 | */ | |
10182 | disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start; | |
10183 | count = start + disk_io_size - iocb->ki_pos; | |
10184 | encoded->len = count; | |
10185 | encoded->unencoded_len = count; | |
10186 | disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize); | |
10187 | } | |
10188 | free_extent_map(em); | |
10189 | em = NULL; | |
10190 | ||
10191 | if (disk_bytenr == EXTENT_MAP_HOLE) { | |
570eb97b | 10192 | unlock_extent(io_tree, start, lockend, &cached_state); |
e5d4d75b | 10193 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10194 | unlocked = true; |
10195 | ret = iov_iter_zero(count, iter); | |
10196 | if (ret != count) | |
10197 | ret = -EFAULT; | |
10198 | } else { | |
10199 | ret = btrfs_encoded_read_regular(iocb, iter, start, lockend, | |
10200 | &cached_state, disk_bytenr, | |
10201 | disk_io_size, count, | |
10202 | encoded->compression, | |
10203 | &unlocked); | |
10204 | } | |
10205 | ||
10206 | out: | |
10207 | if (ret >= 0) | |
10208 | iocb->ki_pos += encoded->len; | |
10209 | out_em: | |
10210 | free_extent_map(em); | |
10211 | out_unlock_extent: | |
10212 | if (!unlocked) | |
570eb97b | 10213 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10214 | out_unlock_inode: |
10215 | if (!unlocked) | |
e5d4d75b | 10216 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10217 | return ret; |
10218 | } | |
10219 | ||
7c0c7269 OS |
10220 | ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from, |
10221 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
10222 | { | |
10223 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10224 | struct btrfs_root *root = inode->root; | |
10225 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10226 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10227 | struct extent_changeset *data_reserved = NULL; | |
10228 | struct extent_state *cached_state = NULL; | |
d611935b | 10229 | struct btrfs_ordered_extent *ordered; |
7c0c7269 OS |
10230 | int compression; |
10231 | size_t orig_count; | |
10232 | u64 start, end; | |
10233 | u64 num_bytes, ram_bytes, disk_num_bytes; | |
10234 | unsigned long nr_pages, i; | |
10235 | struct page **pages; | |
10236 | struct btrfs_key ins; | |
10237 | bool extent_reserved = false; | |
10238 | struct extent_map *em; | |
10239 | ssize_t ret; | |
10240 | ||
10241 | switch (encoded->compression) { | |
10242 | case BTRFS_ENCODED_IO_COMPRESSION_ZLIB: | |
10243 | compression = BTRFS_COMPRESS_ZLIB; | |
10244 | break; | |
10245 | case BTRFS_ENCODED_IO_COMPRESSION_ZSTD: | |
10246 | compression = BTRFS_COMPRESS_ZSTD; | |
10247 | break; | |
10248 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K: | |
10249 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K: | |
10250 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K: | |
10251 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K: | |
10252 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K: | |
10253 | /* The sector size must match for LZO. */ | |
10254 | if (encoded->compression - | |
10255 | BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 != | |
10256 | fs_info->sectorsize_bits) | |
10257 | return -EINVAL; | |
10258 | compression = BTRFS_COMPRESS_LZO; | |
10259 | break; | |
10260 | default: | |
10261 | return -EINVAL; | |
10262 | } | |
10263 | if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE) | |
10264 | return -EINVAL; | |
10265 | ||
10266 | orig_count = iov_iter_count(from); | |
10267 | ||
10268 | /* The extent size must be sane. */ | |
10269 | if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED || | |
10270 | orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0) | |
10271 | return -EINVAL; | |
10272 | ||
10273 | /* | |
10274 | * The compressed data must be smaller than the decompressed data. | |
10275 | * | |
10276 | * It's of course possible for data to compress to larger or the same | |
10277 | * size, but the buffered I/O path falls back to no compression for such | |
10278 | * data, and we don't want to break any assumptions by creating these | |
10279 | * extents. | |
10280 | * | |
10281 | * Note that this is less strict than the current check we have that the | |
10282 | * compressed data must be at least one sector smaller than the | |
10283 | * decompressed data. We only want to enforce the weaker requirement | |
10284 | * from old kernels that it is at least one byte smaller. | |
10285 | */ | |
10286 | if (orig_count >= encoded->unencoded_len) | |
10287 | return -EINVAL; | |
10288 | ||
10289 | /* The extent must start on a sector boundary. */ | |
10290 | start = iocb->ki_pos; | |
10291 | if (!IS_ALIGNED(start, fs_info->sectorsize)) | |
10292 | return -EINVAL; | |
10293 | ||
10294 | /* | |
10295 | * The extent must end on a sector boundary. However, we allow a write | |
10296 | * which ends at or extends i_size to have an unaligned length; we round | |
10297 | * up the extent size and set i_size to the unaligned end. | |
10298 | */ | |
10299 | if (start + encoded->len < inode->vfs_inode.i_size && | |
10300 | !IS_ALIGNED(start + encoded->len, fs_info->sectorsize)) | |
10301 | return -EINVAL; | |
10302 | ||
10303 | /* Finally, the offset in the unencoded data must be sector-aligned. */ | |
10304 | if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize)) | |
10305 | return -EINVAL; | |
10306 | ||
10307 | num_bytes = ALIGN(encoded->len, fs_info->sectorsize); | |
10308 | ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize); | |
10309 | end = start + num_bytes - 1; | |
10310 | ||
10311 | /* | |
10312 | * If the extent cannot be inline, the compressed data on disk must be | |
10313 | * sector-aligned. For convenience, we extend it with zeroes if it | |
10314 | * isn't. | |
10315 | */ | |
10316 | disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize); | |
10317 | nr_pages = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE); | |
10318 | pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT); | |
10319 | if (!pages) | |
10320 | return -ENOMEM; | |
10321 | for (i = 0; i < nr_pages; i++) { | |
10322 | size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from)); | |
10323 | char *kaddr; | |
10324 | ||
10325 | pages[i] = alloc_page(GFP_KERNEL_ACCOUNT); | |
10326 | if (!pages[i]) { | |
10327 | ret = -ENOMEM; | |
10328 | goto out_pages; | |
10329 | } | |
70826b6b | 10330 | kaddr = kmap_local_page(pages[i]); |
7c0c7269 | 10331 | if (copy_from_iter(kaddr, bytes, from) != bytes) { |
70826b6b | 10332 | kunmap_local(kaddr); |
7c0c7269 OS |
10333 | ret = -EFAULT; |
10334 | goto out_pages; | |
10335 | } | |
10336 | if (bytes < PAGE_SIZE) | |
10337 | memset(kaddr + bytes, 0, PAGE_SIZE - bytes); | |
70826b6b | 10338 | kunmap_local(kaddr); |
7c0c7269 OS |
10339 | } |
10340 | ||
10341 | for (;;) { | |
10342 | struct btrfs_ordered_extent *ordered; | |
10343 | ||
10344 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes); | |
10345 | if (ret) | |
10346 | goto out_pages; | |
10347 | ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping, | |
10348 | start >> PAGE_SHIFT, | |
10349 | end >> PAGE_SHIFT); | |
10350 | if (ret) | |
10351 | goto out_pages; | |
570eb97b | 10352 | lock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10353 | ordered = btrfs_lookup_ordered_range(inode, start, num_bytes); |
10354 | if (!ordered && | |
10355 | !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end)) | |
10356 | break; | |
10357 | if (ordered) | |
10358 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10359 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10360 | cond_resched(); |
10361 | } | |
10362 | ||
10363 | /* | |
10364 | * We don't use the higher-level delalloc space functions because our | |
10365 | * num_bytes and disk_num_bytes are different. | |
10366 | */ | |
10367 | ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes); | |
10368 | if (ret) | |
10369 | goto out_unlock; | |
10370 | ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes); | |
10371 | if (ret) | |
10372 | goto out_free_data_space; | |
d4135134 FM |
10373 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes, |
10374 | false); | |
7c0c7269 OS |
10375 | if (ret) |
10376 | goto out_qgroup_free_data; | |
10377 | ||
10378 | /* Try an inline extent first. */ | |
10379 | if (start == 0 && encoded->unencoded_len == encoded->len && | |
10380 | encoded->unencoded_offset == 0) { | |
10381 | ret = cow_file_range_inline(inode, encoded->len, orig_count, | |
10382 | compression, pages, true); | |
10383 | if (ret <= 0) { | |
10384 | if (ret == 0) | |
10385 | ret = orig_count; | |
10386 | goto out_delalloc_release; | |
10387 | } | |
10388 | } | |
10389 | ||
10390 | ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes, | |
10391 | disk_num_bytes, 0, 0, &ins, 1, 1); | |
10392 | if (ret) | |
10393 | goto out_delalloc_release; | |
10394 | extent_reserved = true; | |
10395 | ||
10396 | em = create_io_em(inode, start, num_bytes, | |
10397 | start - encoded->unencoded_offset, ins.objectid, | |
10398 | ins.offset, ins.offset, ram_bytes, compression, | |
10399 | BTRFS_ORDERED_COMPRESSED); | |
10400 | if (IS_ERR(em)) { | |
10401 | ret = PTR_ERR(em); | |
10402 | goto out_free_reserved; | |
10403 | } | |
10404 | free_extent_map(em); | |
10405 | ||
d611935b | 10406 | ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes, |
7c0c7269 OS |
10407 | ins.objectid, ins.offset, |
10408 | encoded->unencoded_offset, | |
10409 | (1 << BTRFS_ORDERED_ENCODED) | | |
10410 | (1 << BTRFS_ORDERED_COMPRESSED), | |
10411 | compression); | |
d611935b | 10412 | if (IS_ERR(ordered)) { |
4c0c8cfc | 10413 | btrfs_drop_extent_map_range(inode, start, end, false); |
d611935b | 10414 | ret = PTR_ERR(ordered); |
7c0c7269 OS |
10415 | goto out_free_reserved; |
10416 | } | |
10417 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10418 | ||
10419 | if (start + encoded->len > inode->vfs_inode.i_size) | |
10420 | i_size_write(&inode->vfs_inode, start + encoded->len); | |
10421 | ||
570eb97b | 10422 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10423 | |
10424 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10425 | ||
d611935b | 10426 | btrfs_submit_compressed_write(ordered, pages, nr_pages, 0, false); |
7c0c7269 OS |
10427 | ret = orig_count; |
10428 | goto out; | |
10429 | ||
10430 | out_free_reserved: | |
10431 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10432 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); | |
10433 | out_delalloc_release: | |
10434 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10435 | btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0); | |
10436 | out_qgroup_free_data: | |
10437 | if (ret < 0) | |
9e65bfca | 10438 | btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes, NULL); |
7c0c7269 OS |
10439 | out_free_data_space: |
10440 | /* | |
10441 | * If btrfs_reserve_extent() succeeded, then we already decremented | |
10442 | * bytes_may_use. | |
10443 | */ | |
10444 | if (!extent_reserved) | |
10445 | btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes); | |
10446 | out_unlock: | |
570eb97b | 10447 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10448 | out_pages: |
10449 | for (i = 0; i < nr_pages; i++) { | |
10450 | if (pages[i]) | |
10451 | __free_page(pages[i]); | |
10452 | } | |
10453 | kvfree(pages); | |
10454 | out: | |
10455 | if (ret >= 0) | |
10456 | iocb->ki_pos += encoded->len; | |
10457 | return ret; | |
10458 | } | |
10459 | ||
ed46ff3d OS |
10460 | #ifdef CONFIG_SWAP |
10461 | /* | |
10462 | * Add an entry indicating a block group or device which is pinned by a | |
10463 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10464 | * negative errno on failure. | |
10465 | */ | |
10466 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10467 | bool is_block_group) | |
10468 | { | |
10469 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10470 | struct btrfs_swapfile_pin *sp, *entry; | |
10471 | struct rb_node **p; | |
10472 | struct rb_node *parent = NULL; | |
10473 | ||
10474 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10475 | if (!sp) | |
10476 | return -ENOMEM; | |
10477 | sp->ptr = ptr; | |
10478 | sp->inode = inode; | |
10479 | sp->is_block_group = is_block_group; | |
195a49ea | 10480 | sp->bg_extent_count = 1; |
ed46ff3d OS |
10481 | |
10482 | spin_lock(&fs_info->swapfile_pins_lock); | |
10483 | p = &fs_info->swapfile_pins.rb_node; | |
10484 | while (*p) { | |
10485 | parent = *p; | |
10486 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10487 | if (sp->ptr < entry->ptr || | |
10488 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10489 | p = &(*p)->rb_left; | |
10490 | } else if (sp->ptr > entry->ptr || | |
10491 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10492 | p = &(*p)->rb_right; | |
10493 | } else { | |
195a49ea FM |
10494 | if (is_block_group) |
10495 | entry->bg_extent_count++; | |
ed46ff3d OS |
10496 | spin_unlock(&fs_info->swapfile_pins_lock); |
10497 | kfree(sp); | |
10498 | return 1; | |
10499 | } | |
10500 | } | |
10501 | rb_link_node(&sp->node, parent, p); | |
10502 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10503 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10504 | return 0; | |
10505 | } | |
10506 | ||
10507 | /* Free all of the entries pinned by this swapfile. */ | |
10508 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10509 | { | |
10510 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10511 | struct btrfs_swapfile_pin *sp; | |
10512 | struct rb_node *node, *next; | |
10513 | ||
10514 | spin_lock(&fs_info->swapfile_pins_lock); | |
10515 | node = rb_first(&fs_info->swapfile_pins); | |
10516 | while (node) { | |
10517 | next = rb_next(node); | |
10518 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10519 | if (sp->inode == inode) { | |
10520 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
195a49ea FM |
10521 | if (sp->is_block_group) { |
10522 | btrfs_dec_block_group_swap_extents(sp->ptr, | |
10523 | sp->bg_extent_count); | |
ed46ff3d | 10524 | btrfs_put_block_group(sp->ptr); |
195a49ea | 10525 | } |
ed46ff3d OS |
10526 | kfree(sp); |
10527 | } | |
10528 | node = next; | |
10529 | } | |
10530 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10531 | } | |
10532 | ||
10533 | struct btrfs_swap_info { | |
10534 | u64 start; | |
10535 | u64 block_start; | |
10536 | u64 block_len; | |
10537 | u64 lowest_ppage; | |
10538 | u64 highest_ppage; | |
10539 | unsigned long nr_pages; | |
10540 | int nr_extents; | |
10541 | }; | |
10542 | ||
10543 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10544 | struct btrfs_swap_info *bsi) | |
10545 | { | |
10546 | unsigned long nr_pages; | |
c2f82263 | 10547 | unsigned long max_pages; |
ed46ff3d OS |
10548 | u64 first_ppage, first_ppage_reported, next_ppage; |
10549 | int ret; | |
10550 | ||
c2f82263 FM |
10551 | /* |
10552 | * Our swapfile may have had its size extended after the swap header was | |
10553 | * written. In that case activating the swapfile should not go beyond | |
10554 | * the max size set in the swap header. | |
10555 | */ | |
10556 | if (bsi->nr_pages >= sis->max) | |
10557 | return 0; | |
10558 | ||
10559 | max_pages = sis->max - bsi->nr_pages; | |
ce394a7f YZ |
10560 | first_ppage = PAGE_ALIGN(bsi->block_start) >> PAGE_SHIFT; |
10561 | next_ppage = PAGE_ALIGN_DOWN(bsi->block_start + bsi->block_len) >> PAGE_SHIFT; | |
ed46ff3d OS |
10562 | |
10563 | if (first_ppage >= next_ppage) | |
10564 | return 0; | |
10565 | nr_pages = next_ppage - first_ppage; | |
c2f82263 | 10566 | nr_pages = min(nr_pages, max_pages); |
ed46ff3d OS |
10567 | |
10568 | first_ppage_reported = first_ppage; | |
10569 | if (bsi->start == 0) | |
10570 | first_ppage_reported++; | |
10571 | if (bsi->lowest_ppage > first_ppage_reported) | |
10572 | bsi->lowest_ppage = first_ppage_reported; | |
10573 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10574 | bsi->highest_ppage = next_ppage - 1; | |
10575 | ||
10576 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10577 | if (ret < 0) | |
10578 | return ret; | |
10579 | bsi->nr_extents += ret; | |
10580 | bsi->nr_pages += nr_pages; | |
10581 | return 0; | |
10582 | } | |
10583 | ||
10584 | static void btrfs_swap_deactivate(struct file *file) | |
10585 | { | |
10586 | struct inode *inode = file_inode(file); | |
10587 | ||
10588 | btrfs_free_swapfile_pins(inode); | |
10589 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10590 | } | |
10591 | ||
10592 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10593 | sector_t *span) | |
10594 | { | |
10595 | struct inode *inode = file_inode(file); | |
dd0734f2 FM |
10596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10597 | struct btrfs_fs_info *fs_info = root->fs_info; | |
ed46ff3d OS |
10598 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
10599 | struct extent_state *cached_state = NULL; | |
10600 | struct extent_map *em = NULL; | |
7dc66abb | 10601 | struct btrfs_chunk_map *map = NULL; |
ed46ff3d OS |
10602 | struct btrfs_device *device = NULL; |
10603 | struct btrfs_swap_info bsi = { | |
10604 | .lowest_ppage = (sector_t)-1ULL, | |
10605 | }; | |
10606 | int ret = 0; | |
10607 | u64 isize; | |
10608 | u64 start; | |
10609 | ||
10610 | /* | |
10611 | * If the swap file was just created, make sure delalloc is done. If the | |
10612 | * file changes again after this, the user is doing something stupid and | |
10613 | * we don't really care. | |
10614 | */ | |
10615 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10616 | if (ret) | |
10617 | return ret; | |
10618 | ||
10619 | /* | |
10620 | * The inode is locked, so these flags won't change after we check them. | |
10621 | */ | |
10622 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10623 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10624 | return -EINVAL; | |
10625 | } | |
10626 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10627 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10628 | return -EINVAL; | |
10629 | } | |
10630 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10631 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10632 | return -EINVAL; | |
10633 | } | |
10634 | ||
10635 | /* | |
10636 | * Balance or device remove/replace/resize can move stuff around from | |
c3e1f96c GR |
10637 | * under us. The exclop protection makes sure they aren't running/won't |
10638 | * run concurrently while we are mapping the swap extents, and | |
10639 | * fs_info->swapfile_pins prevents them from running while the swap | |
10640 | * file is active and moving the extents. Note that this also prevents | |
10641 | * a concurrent device add which isn't actually necessary, but it's not | |
ed46ff3d OS |
10642 | * really worth the trouble to allow it. |
10643 | */ | |
c3e1f96c | 10644 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
ed46ff3d OS |
10645 | btrfs_warn(fs_info, |
10646 | "cannot activate swapfile while exclusive operation is running"); | |
10647 | return -EBUSY; | |
10648 | } | |
dd0734f2 FM |
10649 | |
10650 | /* | |
10651 | * Prevent snapshot creation while we are activating the swap file. | |
10652 | * We do not want to race with snapshot creation. If snapshot creation | |
10653 | * already started before we bumped nr_swapfiles from 0 to 1 and | |
10654 | * completes before the first write into the swap file after it is | |
10655 | * activated, than that write would fallback to COW. | |
10656 | */ | |
10657 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { | |
10658 | btrfs_exclop_finish(fs_info); | |
10659 | btrfs_warn(fs_info, | |
10660 | "cannot activate swapfile because snapshot creation is in progress"); | |
10661 | return -EINVAL; | |
10662 | } | |
ed46ff3d OS |
10663 | /* |
10664 | * Snapshots can create extents which require COW even if NODATACOW is | |
10665 | * set. We use this counter to prevent snapshots. We must increment it | |
10666 | * before walking the extents because we don't want a concurrent | |
10667 | * snapshot to run after we've already checked the extents. | |
60021bd7 KH |
10668 | * |
10669 | * It is possible that subvolume is marked for deletion but still not | |
10670 | * removed yet. To prevent this race, we check the root status before | |
10671 | * activating the swapfile. | |
ed46ff3d | 10672 | */ |
60021bd7 KH |
10673 | spin_lock(&root->root_item_lock); |
10674 | if (btrfs_root_dead(root)) { | |
10675 | spin_unlock(&root->root_item_lock); | |
10676 | ||
10677 | btrfs_exclop_finish(fs_info); | |
10678 | btrfs_warn(fs_info, | |
10679 | "cannot activate swapfile because subvolume %llu is being deleted", | |
10680 | root->root_key.objectid); | |
10681 | return -EPERM; | |
10682 | } | |
dd0734f2 | 10683 | atomic_inc(&root->nr_swapfiles); |
60021bd7 | 10684 | spin_unlock(&root->root_item_lock); |
ed46ff3d OS |
10685 | |
10686 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10687 | ||
570eb97b | 10688 | lock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
10689 | start = 0; |
10690 | while (start < isize) { | |
10691 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10692 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10693 | u64 len = isize - start; |
10694 | ||
39b07b5d | 10695 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
10696 | if (IS_ERR(em)) { |
10697 | ret = PTR_ERR(em); | |
10698 | goto out; | |
10699 | } | |
10700 | ||
10701 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10702 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10703 | ret = -EINVAL; | |
10704 | goto out; | |
10705 | } | |
10706 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10707 | /* | |
10708 | * It's unlikely we'll ever actually find ourselves | |
10709 | * here, as a file small enough to fit inline won't be | |
10710 | * big enough to store more than the swap header, but in | |
10711 | * case something changes in the future, let's catch it | |
10712 | * here rather than later. | |
10713 | */ | |
10714 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10715 | ret = -EINVAL; | |
10716 | goto out; | |
10717 | } | |
f86f7a75 | 10718 | if (extent_map_is_compressed(em)) { |
ed46ff3d OS |
10719 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
10720 | ret = -EINVAL; | |
10721 | goto out; | |
10722 | } | |
10723 | ||
10724 | logical_block_start = em->block_start + (start - em->start); | |
10725 | len = min(len, em->len - (start - em->start)); | |
10726 | free_extent_map(em); | |
10727 | em = NULL; | |
10728 | ||
26ce9114 | 10729 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true); |
ed46ff3d OS |
10730 | if (ret < 0) { |
10731 | goto out; | |
10732 | } else if (ret) { | |
10733 | ret = 0; | |
10734 | } else { | |
10735 | btrfs_warn(fs_info, | |
10736 | "swapfile must not be copy-on-write"); | |
10737 | ret = -EINVAL; | |
10738 | goto out; | |
10739 | } | |
10740 | ||
7dc66abb FM |
10741 | map = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
10742 | if (IS_ERR(map)) { | |
10743 | ret = PTR_ERR(map); | |
ed46ff3d OS |
10744 | goto out; |
10745 | } | |
10746 | ||
7dc66abb | 10747 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
ed46ff3d OS |
10748 | btrfs_warn(fs_info, |
10749 | "swapfile must have single data profile"); | |
10750 | ret = -EINVAL; | |
10751 | goto out; | |
10752 | } | |
10753 | ||
10754 | if (device == NULL) { | |
7dc66abb | 10755 | device = map->stripes[0].dev; |
ed46ff3d OS |
10756 | ret = btrfs_add_swapfile_pin(inode, device, false); |
10757 | if (ret == 1) | |
10758 | ret = 0; | |
10759 | else if (ret) | |
10760 | goto out; | |
7dc66abb | 10761 | } else if (device != map->stripes[0].dev) { |
ed46ff3d OS |
10762 | btrfs_warn(fs_info, "swapfile must be on one device"); |
10763 | ret = -EINVAL; | |
10764 | goto out; | |
10765 | } | |
10766 | ||
7dc66abb FM |
10767 | physical_block_start = (map->stripes[0].physical + |
10768 | (logical_block_start - map->start)); | |
10769 | len = min(len, map->chunk_len - (logical_block_start - map->start)); | |
10770 | btrfs_free_chunk_map(map); | |
10771 | map = NULL; | |
ed46ff3d OS |
10772 | |
10773 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10774 | if (!bg) { | |
10775 | btrfs_warn(fs_info, | |
10776 | "could not find block group containing swapfile"); | |
10777 | ret = -EINVAL; | |
10778 | goto out; | |
10779 | } | |
10780 | ||
195a49ea FM |
10781 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
10782 | btrfs_warn(fs_info, | |
10783 | "block group for swapfile at %llu is read-only%s", | |
10784 | bg->start, | |
10785 | atomic_read(&fs_info->scrubs_running) ? | |
10786 | " (scrub running)" : ""); | |
10787 | btrfs_put_block_group(bg); | |
10788 | ret = -EINVAL; | |
10789 | goto out; | |
10790 | } | |
10791 | ||
ed46ff3d OS |
10792 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
10793 | if (ret) { | |
10794 | btrfs_put_block_group(bg); | |
10795 | if (ret == 1) | |
10796 | ret = 0; | |
10797 | else | |
10798 | goto out; | |
10799 | } | |
10800 | ||
10801 | if (bsi.block_len && | |
10802 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10803 | bsi.block_len += len; | |
10804 | } else { | |
10805 | if (bsi.block_len) { | |
10806 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10807 | if (ret) | |
10808 | goto out; | |
10809 | } | |
10810 | bsi.start = start; | |
10811 | bsi.block_start = physical_block_start; | |
10812 | bsi.block_len = len; | |
10813 | } | |
10814 | ||
10815 | start += len; | |
10816 | } | |
10817 | ||
10818 | if (bsi.block_len) | |
10819 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10820 | ||
10821 | out: | |
10822 | if (!IS_ERR_OR_NULL(em)) | |
10823 | free_extent_map(em); | |
7dc66abb FM |
10824 | if (!IS_ERR_OR_NULL(map)) |
10825 | btrfs_free_chunk_map(map); | |
ed46ff3d | 10826 | |
570eb97b | 10827 | unlock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
10828 | |
10829 | if (ret) | |
10830 | btrfs_swap_deactivate(file); | |
10831 | ||
dd0734f2 FM |
10832 | btrfs_drew_write_unlock(&root->snapshot_lock); |
10833 | ||
c3e1f96c | 10834 | btrfs_exclop_finish(fs_info); |
ed46ff3d OS |
10835 | |
10836 | if (ret) | |
10837 | return ret; | |
10838 | ||
10839 | if (device) | |
10840 | sis->bdev = device->bdev; | |
10841 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10842 | sis->max = bsi.nr_pages; | |
10843 | sis->pages = bsi.nr_pages - 1; | |
10844 | sis->highest_bit = bsi.nr_pages - 1; | |
10845 | return bsi.nr_extents; | |
10846 | } | |
10847 | #else | |
10848 | static void btrfs_swap_deactivate(struct file *file) | |
10849 | { | |
10850 | } | |
10851 | ||
10852 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10853 | sector_t *span) | |
10854 | { | |
10855 | return -EOPNOTSUPP; | |
10856 | } | |
10857 | #endif | |
10858 | ||
2766ff61 FM |
10859 | /* |
10860 | * Update the number of bytes used in the VFS' inode. When we replace extents in | |
10861 | * a range (clone, dedupe, fallocate's zero range), we must update the number of | |
10862 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls | |
10863 | * always get a correct value. | |
10864 | */ | |
10865 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, | |
10866 | const u64 add_bytes, | |
10867 | const u64 del_bytes) | |
10868 | { | |
10869 | if (add_bytes == del_bytes) | |
10870 | return; | |
10871 | ||
10872 | spin_lock(&inode->lock); | |
10873 | if (del_bytes > 0) | |
10874 | inode_sub_bytes(&inode->vfs_inode, del_bytes); | |
10875 | if (add_bytes > 0) | |
10876 | inode_add_bytes(&inode->vfs_inode, add_bytes); | |
10877 | spin_unlock(&inode->lock); | |
10878 | } | |
10879 | ||
43dd529a | 10880 | /* |
63c34cb4 FM |
10881 | * Verify that there are no ordered extents for a given file range. |
10882 | * | |
10883 | * @inode: The target inode. | |
10884 | * @start: Start offset of the file range, should be sector size aligned. | |
10885 | * @end: End offset (inclusive) of the file range, its value +1 should be | |
10886 | * sector size aligned. | |
10887 | * | |
10888 | * This should typically be used for cases where we locked an inode's VFS lock in | |
10889 | * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode, | |
10890 | * we have flushed all delalloc in the range, we have waited for all ordered | |
10891 | * extents in the range to complete and finally we have locked the file range in | |
10892 | * the inode's io_tree. | |
10893 | */ | |
10894 | void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end) | |
10895 | { | |
10896 | struct btrfs_root *root = inode->root; | |
10897 | struct btrfs_ordered_extent *ordered; | |
10898 | ||
10899 | if (!IS_ENABLED(CONFIG_BTRFS_ASSERT)) | |
10900 | return; | |
10901 | ||
10902 | ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start); | |
10903 | if (ordered) { | |
10904 | btrfs_err(root->fs_info, | |
10905 | "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])", | |
10906 | start, end, btrfs_ino(inode), root->root_key.objectid, | |
10907 | ordered->file_offset, | |
10908 | ordered->file_offset + ordered->num_bytes - 1); | |
10909 | btrfs_put_ordered_extent(ordered); | |
10910 | } | |
10911 | ||
10912 | ASSERT(ordered == NULL); | |
10913 | } | |
10914 | ||
6e1d5dcc | 10915 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10916 | .getattr = btrfs_getattr, |
39279cc3 CM |
10917 | .lookup = btrfs_lookup, |
10918 | .create = btrfs_create, | |
10919 | .unlink = btrfs_unlink, | |
10920 | .link = btrfs_link, | |
10921 | .mkdir = btrfs_mkdir, | |
10922 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10923 | .rename = btrfs_rename2, |
39279cc3 CM |
10924 | .symlink = btrfs_symlink, |
10925 | .setattr = btrfs_setattr, | |
618e21d5 | 10926 | .mknod = btrfs_mknod, |
5103e947 | 10927 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10928 | .permission = btrfs_permission, |
cac2f8b8 | 10929 | .get_inode_acl = btrfs_get_acl, |
996a710d | 10930 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10931 | .update_time = btrfs_update_time, |
ef3b9af5 | 10932 | .tmpfile = btrfs_tmpfile, |
97fc2977 MS |
10933 | .fileattr_get = btrfs_fileattr_get, |
10934 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 10935 | }; |
76dda93c | 10936 | |
828c0950 | 10937 | static const struct file_operations btrfs_dir_file_operations = { |
e60aa5da | 10938 | .llseek = btrfs_dir_llseek, |
39279cc3 | 10939 | .read = generic_read_dir, |
02dbfc99 | 10940 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10941 | .open = btrfs_opendir, |
34287aa3 | 10942 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10943 | #ifdef CONFIG_COMPAT |
4c63c245 | 10944 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10945 | #endif |
6bf13c0c | 10946 | .release = btrfs_release_file, |
e02119d5 | 10947 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10948 | }; |
10949 | ||
35054394 CM |
10950 | /* |
10951 | * btrfs doesn't support the bmap operation because swapfiles | |
10952 | * use bmap to make a mapping of extents in the file. They assume | |
10953 | * these extents won't change over the life of the file and they | |
10954 | * use the bmap result to do IO directly to the drive. | |
10955 | * | |
10956 | * the btrfs bmap call would return logical addresses that aren't | |
10957 | * suitable for IO and they also will change frequently as COW | |
10958 | * operations happen. So, swapfile + btrfs == corruption. | |
10959 | * | |
10960 | * For now we're avoiding this by dropping bmap. | |
10961 | */ | |
7f09410b | 10962 | static const struct address_space_operations btrfs_aops = { |
fb12489b | 10963 | .read_folio = btrfs_read_folio, |
b293f02e | 10964 | .writepages = btrfs_writepages, |
ba206a02 | 10965 | .readahead = btrfs_readahead, |
895586eb | 10966 | .invalidate_folio = btrfs_invalidate_folio, |
f913cff3 | 10967 | .release_folio = btrfs_release_folio, |
e7a60a17 | 10968 | .migrate_folio = btrfs_migrate_folio, |
187c82cb | 10969 | .dirty_folio = filemap_dirty_folio, |
465fdd97 | 10970 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10971 | .swap_activate = btrfs_swap_activate, |
10972 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10973 | }; |
10974 | ||
6e1d5dcc | 10975 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10976 | .getattr = btrfs_getattr, |
10977 | .setattr = btrfs_setattr, | |
5103e947 | 10978 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10979 | .permission = btrfs_permission, |
1506fcc8 | 10980 | .fiemap = btrfs_fiemap, |
cac2f8b8 | 10981 | .get_inode_acl = btrfs_get_acl, |
996a710d | 10982 | .set_acl = btrfs_set_acl, |
e41f941a | 10983 | .update_time = btrfs_update_time, |
97fc2977 MS |
10984 | .fileattr_get = btrfs_fileattr_get, |
10985 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 10986 | }; |
6e1d5dcc | 10987 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10988 | .getattr = btrfs_getattr, |
10989 | .setattr = btrfs_setattr, | |
fdebe2bd | 10990 | .permission = btrfs_permission, |
33268eaf | 10991 | .listxattr = btrfs_listxattr, |
cac2f8b8 | 10992 | .get_inode_acl = btrfs_get_acl, |
996a710d | 10993 | .set_acl = btrfs_set_acl, |
e41f941a | 10994 | .update_time = btrfs_update_time, |
618e21d5 | 10995 | }; |
6e1d5dcc | 10996 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10997 | .get_link = page_get_link, |
f209561a | 10998 | .getattr = btrfs_getattr, |
22c44fe6 | 10999 | .setattr = btrfs_setattr, |
fdebe2bd | 11000 | .permission = btrfs_permission, |
0279b4cd | 11001 | .listxattr = btrfs_listxattr, |
e41f941a | 11002 | .update_time = btrfs_update_time, |
39279cc3 | 11003 | }; |
76dda93c | 11004 | |
82d339d9 | 11005 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
11006 | .d_delete = btrfs_dentry_delete, |
11007 | }; |