]>
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" | |
e6dcd2dc | 42 | #include "ordered-data.h" |
95819c05 | 43 | #include "xattr.h" |
e02119d5 | 44 | #include "tree-log.h" |
103c1972 | 45 | #include "bio.h" |
c8b97818 | 46 | #include "compression.h" |
b4ce94de | 47 | #include "locking.h" |
63541927 | 48 | #include "props.h" |
31193213 | 49 | #include "qgroup.h" |
86736342 | 50 | #include "delalloc-space.h" |
aac0023c | 51 | #include "block-group.h" |
467dc47e | 52 | #include "space-info.h" |
d8e3fb10 | 53 | #include "zoned.h" |
b945a463 | 54 | #include "subpage.h" |
26c2c454 | 55 | #include "inode-item.h" |
c7f13d42 | 56 | #include "fs.h" |
ad1ac501 | 57 | #include "accessors.h" |
a0231804 | 58 | #include "extent-tree.h" |
45c40c8f | 59 | #include "root-tree.h" |
59b818e0 | 60 | #include "defrag.h" |
f2b39277 | 61 | #include "dir-item.h" |
7c8ede16 | 62 | #include "file-item.h" |
c7a03b52 | 63 | #include "uuid-tree.h" |
7572dec8 | 64 | #include "ioctl.h" |
af142b6f | 65 | #include "file.h" |
33cf97a7 | 66 | #include "acl.h" |
67707479 | 67 | #include "relocation.h" |
5c11adcc | 68 | #include "verity.h" |
7f0add25 | 69 | #include "super.h" |
aa5d3003 | 70 | #include "orphan.h" |
b9a9a850 | 71 | #include "backref.h" |
02c372e1 | 72 | #include "raid-stripe-tree.h" |
39279cc3 CM |
73 | |
74 | struct btrfs_iget_args { | |
0202e83f | 75 | u64 ino; |
39279cc3 CM |
76 | struct btrfs_root *root; |
77 | }; | |
78 | ||
f28a4928 | 79 | struct btrfs_dio_data { |
f85781fb GR |
80 | ssize_t submitted; |
81 | struct extent_changeset *data_reserved; | |
53f2c206 | 82 | struct btrfs_ordered_extent *ordered; |
f5585f4f FM |
83 | bool data_space_reserved; |
84 | bool nocow_done; | |
f28a4928 FM |
85 | }; |
86 | ||
a3e171a0 | 87 | struct btrfs_dio_private { |
67d66982 | 88 | /* Range of I/O */ |
a3e171a0 | 89 | u64 file_offset; |
a3e171a0 CH |
90 | u32 bytes; |
91 | ||
642c5d34 | 92 | /* This must be last */ |
67d66982 | 93 | struct btrfs_bio bbio; |
a3e171a0 CH |
94 | }; |
95 | ||
642c5d34 CH |
96 | static struct bio_set btrfs_dio_bioset; |
97 | ||
88d2beec FM |
98 | struct btrfs_rename_ctx { |
99 | /* Output field. Stores the index number of the old directory entry. */ | |
100 | u64 index; | |
101 | }; | |
102 | ||
b9a9a850 QW |
103 | /* |
104 | * Used by data_reloc_print_warning_inode() to pass needed info for filename | |
105 | * resolution and output of error message. | |
106 | */ | |
107 | struct data_reloc_warn { | |
108 | struct btrfs_path path; | |
109 | struct btrfs_fs_info *fs_info; | |
110 | u64 extent_item_size; | |
111 | u64 logical; | |
112 | int mirror_num; | |
113 | }; | |
114 | ||
516095cd DS |
115 | /* |
116 | * For the file_extent_tree, we want to hold the inode lock when we lookup and | |
117 | * update the disk_i_size, but lockdep will complain because our io_tree we hold | |
118 | * the tree lock and get the inode lock when setting delalloc. These two things | |
119 | * are unrelated, so make a class for the file_extent_tree so we don't get the | |
120 | * two locking patterns mixed up. | |
121 | */ | |
122 | static struct lock_class_key file_extent_tree_class; | |
123 | ||
6e1d5dcc AD |
124 | static const struct inode_operations btrfs_dir_inode_operations; |
125 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
126 | static const struct inode_operations btrfs_special_inode_operations; |
127 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 128 | static const struct address_space_operations btrfs_aops; |
828c0950 | 129 | static const struct file_operations btrfs_dir_file_operations; |
39279cc3 CM |
130 | |
131 | static struct kmem_cache *btrfs_inode_cachep; | |
39279cc3 | 132 | |
3972f260 | 133 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
d9dcae67 | 134 | static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback); |
ba9145ad | 135 | |
256b0cf9 CH |
136 | static noinline int run_delalloc_cow(struct btrfs_inode *inode, |
137 | struct page *locked_page, u64 start, | |
138 | u64 end, struct writeback_control *wbc, | |
139 | bool pages_dirty); | |
4b67c11d NB |
140 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
141 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
142 | u64 block_len, u64 orig_block_len, |
143 | u64 ram_bytes, int compress_type, | |
144 | int type); | |
7b128766 | 145 | |
b9a9a850 QW |
146 | static int data_reloc_print_warning_inode(u64 inum, u64 offset, u64 num_bytes, |
147 | u64 root, void *warn_ctx) | |
148 | { | |
149 | struct data_reloc_warn *warn = warn_ctx; | |
150 | struct btrfs_fs_info *fs_info = warn->fs_info; | |
151 | struct extent_buffer *eb; | |
152 | struct btrfs_inode_item *inode_item; | |
153 | struct inode_fs_paths *ipath = NULL; | |
154 | struct btrfs_root *local_root; | |
155 | struct btrfs_key key; | |
156 | unsigned int nofs_flag; | |
157 | u32 nlink; | |
158 | int ret; | |
159 | ||
160 | local_root = btrfs_get_fs_root(fs_info, root, true); | |
161 | if (IS_ERR(local_root)) { | |
162 | ret = PTR_ERR(local_root); | |
163 | goto err; | |
164 | } | |
165 | ||
166 | /* This makes the path point to (inum INODE_ITEM ioff). */ | |
167 | key.objectid = inum; | |
168 | key.type = BTRFS_INODE_ITEM_KEY; | |
169 | key.offset = 0; | |
170 | ||
171 | ret = btrfs_search_slot(NULL, local_root, &key, &warn->path, 0, 0); | |
172 | if (ret) { | |
173 | btrfs_put_root(local_root); | |
174 | btrfs_release_path(&warn->path); | |
175 | goto err; | |
176 | } | |
177 | ||
178 | eb = warn->path.nodes[0]; | |
179 | inode_item = btrfs_item_ptr(eb, warn->path.slots[0], struct btrfs_inode_item); | |
180 | nlink = btrfs_inode_nlink(eb, inode_item); | |
181 | btrfs_release_path(&warn->path); | |
182 | ||
183 | nofs_flag = memalloc_nofs_save(); | |
184 | ipath = init_ipath(4096, local_root, &warn->path); | |
185 | memalloc_nofs_restore(nofs_flag); | |
186 | if (IS_ERR(ipath)) { | |
187 | btrfs_put_root(local_root); | |
188 | ret = PTR_ERR(ipath); | |
189 | ipath = NULL; | |
190 | /* | |
191 | * -ENOMEM, not a critical error, just output an generic error | |
192 | * without filename. | |
193 | */ | |
194 | btrfs_warn(fs_info, | |
195 | "checksum error at logical %llu mirror %u root %llu, inode %llu offset %llu", | |
196 | warn->logical, warn->mirror_num, root, inum, offset); | |
197 | return ret; | |
198 | } | |
199 | ret = paths_from_inode(inum, ipath); | |
200 | if (ret < 0) | |
201 | goto err; | |
202 | ||
203 | /* | |
204 | * We deliberately ignore the bit ipath might have been too small to | |
205 | * hold all of the paths here | |
206 | */ | |
207 | for (int i = 0; i < ipath->fspath->elem_cnt; i++) { | |
208 | btrfs_warn(fs_info, | |
209 | "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu length %u links %u (path: %s)", | |
210 | warn->logical, warn->mirror_num, root, inum, offset, | |
211 | fs_info->sectorsize, nlink, | |
212 | (char *)(unsigned long)ipath->fspath->val[i]); | |
213 | } | |
214 | ||
215 | btrfs_put_root(local_root); | |
216 | free_ipath(ipath); | |
217 | return 0; | |
218 | ||
219 | err: | |
220 | btrfs_warn(fs_info, | |
221 | "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu, path resolving failed with ret=%d", | |
222 | warn->logical, warn->mirror_num, root, inum, offset, ret); | |
223 | ||
224 | free_ipath(ipath); | |
225 | return ret; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Do extra user-friendly error output (e.g. lookup all the affected files). | |
230 | * | |
231 | * Return true if we succeeded doing the backref lookup. | |
232 | * Return false if such lookup failed, and has to fallback to the old error message. | |
233 | */ | |
234 | static void print_data_reloc_error(const struct btrfs_inode *inode, u64 file_off, | |
235 | const u8 *csum, const u8 *csum_expected, | |
236 | int mirror_num) | |
237 | { | |
238 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
239 | struct btrfs_path path = { 0 }; | |
240 | struct btrfs_key found_key = { 0 }; | |
241 | struct extent_buffer *eb; | |
242 | struct btrfs_extent_item *ei; | |
243 | const u32 csum_size = fs_info->csum_size; | |
244 | u64 logical; | |
245 | u64 flags; | |
246 | u32 item_size; | |
247 | int ret; | |
248 | ||
249 | mutex_lock(&fs_info->reloc_mutex); | |
250 | logical = btrfs_get_reloc_bg_bytenr(fs_info); | |
251 | mutex_unlock(&fs_info->reloc_mutex); | |
252 | ||
253 | if (logical == U64_MAX) { | |
254 | btrfs_warn_rl(fs_info, "has data reloc tree but no running relocation"); | |
255 | btrfs_warn_rl(fs_info, | |
256 | "csum failed root %lld ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
257 | inode->root->root_key.objectid, btrfs_ino(inode), file_off, | |
258 | CSUM_FMT_VALUE(csum_size, csum), | |
259 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
260 | mirror_num); | |
261 | return; | |
262 | } | |
263 | ||
264 | logical += file_off; | |
265 | btrfs_warn_rl(fs_info, | |
266 | "csum failed root %lld ino %llu off %llu logical %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
267 | inode->root->root_key.objectid, | |
268 | btrfs_ino(inode), file_off, logical, | |
269 | CSUM_FMT_VALUE(csum_size, csum), | |
270 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
271 | mirror_num); | |
272 | ||
273 | ret = extent_from_logical(fs_info, logical, &path, &found_key, &flags); | |
274 | if (ret < 0) { | |
275 | btrfs_err_rl(fs_info, "failed to lookup extent item for logical %llu: %d", | |
276 | logical, ret); | |
277 | return; | |
278 | } | |
279 | eb = path.nodes[0]; | |
280 | ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item); | |
281 | item_size = btrfs_item_size(eb, path.slots[0]); | |
282 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
283 | unsigned long ptr = 0; | |
284 | u64 ref_root; | |
285 | u8 ref_level; | |
286 | ||
b7f9945a | 287 | while (true) { |
b9a9a850 QW |
288 | ret = tree_backref_for_extent(&ptr, eb, &found_key, ei, |
289 | item_size, &ref_root, | |
290 | &ref_level); | |
b7f9945a QW |
291 | if (ret < 0) { |
292 | btrfs_warn_rl(fs_info, | |
293 | "failed to resolve tree backref for logical %llu: %d", | |
294 | logical, ret); | |
295 | break; | |
296 | } | |
297 | if (ret > 0) | |
298 | break; | |
299 | ||
b9a9a850 QW |
300 | btrfs_warn_rl(fs_info, |
301 | "csum error at logical %llu mirror %u: metadata %s (level %d) in tree %llu", | |
302 | logical, mirror_num, | |
303 | (ref_level ? "node" : "leaf"), | |
b7f9945a QW |
304 | ref_level, ref_root); |
305 | } | |
b9a9a850 QW |
306 | btrfs_release_path(&path); |
307 | } else { | |
308 | struct btrfs_backref_walk_ctx ctx = { 0 }; | |
309 | struct data_reloc_warn reloc_warn = { 0 }; | |
310 | ||
311 | btrfs_release_path(&path); | |
312 | ||
313 | ctx.bytenr = found_key.objectid; | |
314 | ctx.extent_item_pos = logical - found_key.objectid; | |
315 | ctx.fs_info = fs_info; | |
316 | ||
317 | reloc_warn.logical = logical; | |
318 | reloc_warn.extent_item_size = found_key.offset; | |
319 | reloc_warn.mirror_num = mirror_num; | |
320 | reloc_warn.fs_info = fs_info; | |
321 | ||
322 | iterate_extent_inodes(&ctx, true, | |
323 | data_reloc_print_warning_inode, &reloc_warn); | |
324 | } | |
325 | } | |
326 | ||
f60acad3 JB |
327 | static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode, |
328 | u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num) | |
329 | { | |
330 | struct btrfs_root *root = inode->root; | |
331 | const u32 csum_size = root->fs_info->csum_size; | |
332 | ||
b9a9a850 QW |
333 | /* For data reloc tree, it's better to do a backref lookup instead. */ |
334 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
335 | return print_data_reloc_error(inode, logical_start, csum, | |
336 | csum_expected, mirror_num); | |
337 | ||
f60acad3 JB |
338 | /* Output without objectid, which is more meaningful */ |
339 | if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID) { | |
340 | btrfs_warn_rl(root->fs_info, | |
341 | "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
342 | root->root_key.objectid, btrfs_ino(inode), | |
343 | logical_start, | |
344 | CSUM_FMT_VALUE(csum_size, csum), | |
345 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
346 | mirror_num); | |
347 | } else { | |
348 | btrfs_warn_rl(root->fs_info, | |
349 | "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d", | |
350 | root->root_key.objectid, btrfs_ino(inode), | |
351 | logical_start, | |
352 | CSUM_FMT_VALUE(csum_size, csum), | |
353 | CSUM_FMT_VALUE(csum_size, csum_expected), | |
354 | mirror_num); | |
355 | } | |
356 | } | |
357 | ||
a14b78ad | 358 | /* |
9580503b | 359 | * Lock inode i_rwsem based on arguments passed. |
a14b78ad GR |
360 | * |
361 | * ilock_flags can have the following bit set: | |
362 | * | |
363 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode | |
364 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt | |
365 | * return -EAGAIN | |
8318ba79 | 366 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
a14b78ad | 367 | */ |
29b6352b | 368 | int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags) |
a14b78ad GR |
369 | { |
370 | if (ilock_flags & BTRFS_ILOCK_SHARED) { | |
371 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
29b6352b | 372 | if (!inode_trylock_shared(&inode->vfs_inode)) |
a14b78ad GR |
373 | return -EAGAIN; |
374 | else | |
375 | return 0; | |
376 | } | |
29b6352b | 377 | inode_lock_shared(&inode->vfs_inode); |
a14b78ad GR |
378 | } else { |
379 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
29b6352b | 380 | if (!inode_trylock(&inode->vfs_inode)) |
a14b78ad GR |
381 | return -EAGAIN; |
382 | else | |
383 | return 0; | |
384 | } | |
29b6352b | 385 | inode_lock(&inode->vfs_inode); |
a14b78ad | 386 | } |
8318ba79 | 387 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
29b6352b | 388 | down_write(&inode->i_mmap_lock); |
a14b78ad GR |
389 | return 0; |
390 | } | |
391 | ||
392 | /* | |
9580503b | 393 | * Unock inode i_rwsem. |
a14b78ad GR |
394 | * |
395 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() | |
396 | * to decide whether the lock acquired is shared or exclusive. | |
397 | */ | |
e5d4d75b | 398 | void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags) |
a14b78ad | 399 | { |
8318ba79 | 400 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
e5d4d75b | 401 | up_write(&inode->i_mmap_lock); |
a14b78ad | 402 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
e5d4d75b | 403 | inode_unlock_shared(&inode->vfs_inode); |
a14b78ad | 404 | else |
e5d4d75b | 405 | inode_unlock(&inode->vfs_inode); |
a14b78ad GR |
406 | } |
407 | ||
52427260 QW |
408 | /* |
409 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 410 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
411 | * |
412 | * NOTE: caller must ensure that when an error happens, it can not call | |
413 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
414 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
415 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 416 | * extent (btrfs_finish_ordered_io()). |
52427260 | 417 | */ |
64e1db56 | 418 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
419 | struct page *locked_page, |
420 | u64 offset, u64 bytes) | |
52427260 | 421 | { |
63d71450 NA |
422 | unsigned long index = offset >> PAGE_SHIFT; |
423 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
0e47b25c | 424 | u64 page_start = 0, page_end = 0; |
63d71450 NA |
425 | struct page *page; |
426 | ||
99826e4c NA |
427 | if (locked_page) { |
428 | page_start = page_offset(locked_page); | |
429 | page_end = page_start + PAGE_SIZE - 1; | |
430 | } | |
431 | ||
63d71450 | 432 | while (index <= end_index) { |
968f2566 | 433 | /* |
9783e4de CH |
434 | * For locked page, we will call btrfs_mark_ordered_io_finished |
435 | * through btrfs_mark_ordered_io_finished() on it | |
436 | * in run_delalloc_range() for the error handling, which will | |
437 | * clear page Ordered and run the ordered extent accounting. | |
968f2566 QW |
438 | * |
439 | * Here we can't just clear the Ordered bit, or | |
440 | * btrfs_mark_ordered_io_finished() would skip the accounting | |
441 | * for the page range, and the ordered extent will never finish. | |
442 | */ | |
99826e4c | 443 | if (locked_page && index == (page_start >> PAGE_SHIFT)) { |
968f2566 QW |
444 | index++; |
445 | continue; | |
446 | } | |
64e1db56 | 447 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
448 | index++; |
449 | if (!page) | |
450 | continue; | |
968f2566 QW |
451 | |
452 | /* | |
453 | * Here we just clear all Ordered bits for every page in the | |
711f447b | 454 | * range, then btrfs_mark_ordered_io_finished() will handle |
968f2566 QW |
455 | * the ordered extent accounting for the range. |
456 | */ | |
55151ea9 QW |
457 | btrfs_folio_clamp_clear_ordered(inode->root->fs_info, |
458 | page_folio(page), offset, bytes); | |
63d71450 NA |
459 | put_page(page); |
460 | } | |
d1051d6e | 461 | |
99826e4c NA |
462 | if (locked_page) { |
463 | /* The locked page covers the full range, nothing needs to be done */ | |
464 | if (bytes + offset <= page_start + PAGE_SIZE) | |
465 | return; | |
466 | /* | |
467 | * In case this page belongs to the delalloc range being | |
468 | * instantiated then skip it, since the first page of a range is | |
469 | * going to be properly cleaned up by the caller of | |
470 | * run_delalloc_range | |
471 | */ | |
472 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
473 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; | |
474 | offset = page_offset(locked_page) + PAGE_SIZE; | |
475 | } | |
d1051d6e NB |
476 | } |
477 | ||
711f447b | 478 | return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false); |
52427260 QW |
479 | } |
480 | ||
7152b425 | 481 | static int btrfs_dirty_inode(struct btrfs_inode *inode); |
7b128766 | 482 | |
f34f57a3 | 483 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
3538d68d | 484 | struct btrfs_new_inode_args *args) |
0279b4cd JO |
485 | { |
486 | int err; | |
487 | ||
3538d68d OS |
488 | if (args->default_acl) { |
489 | err = __btrfs_set_acl(trans, args->inode, args->default_acl, | |
490 | ACL_TYPE_DEFAULT); | |
491 | if (err) | |
492 | return err; | |
493 | } | |
494 | if (args->acl) { | |
495 | err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS); | |
496 | if (err) | |
497 | return err; | |
498 | } | |
499 | if (!args->default_acl && !args->acl) | |
500 | cache_no_acl(args->inode); | |
501 | return btrfs_xattr_security_init(trans, args->inode, args->dir, | |
502 | &args->dentry->d_name); | |
0279b4cd JO |
503 | } |
504 | ||
c8b97818 CM |
505 | /* |
506 | * this does all the hard work for inserting an inline extent into | |
507 | * the btree. The caller should have done a btrfs_drop_extents so that | |
508 | * no overlapping inline items exist in the btree | |
509 | */ | |
40f76580 | 510 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
8dd9872d OS |
511 | struct btrfs_path *path, |
512 | struct btrfs_inode *inode, bool extent_inserted, | |
513 | size_t size, size_t compressed_size, | |
fe3f566c | 514 | int compress_type, |
d9496e8a OS |
515 | struct page **compressed_pages, |
516 | bool update_i_size) | |
c8b97818 | 517 | { |
8dd9872d | 518 | struct btrfs_root *root = inode->root; |
c8b97818 CM |
519 | struct extent_buffer *leaf; |
520 | struct page *page = NULL; | |
521 | char *kaddr; | |
522 | unsigned long ptr; | |
523 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
524 | int ret; |
525 | size_t cur_size = size; | |
d9496e8a | 526 | u64 i_size; |
c8b97818 | 527 | |
982f1f5d JJB |
528 | ASSERT((compressed_size > 0 && compressed_pages) || |
529 | (compressed_size == 0 && !compressed_pages)); | |
530 | ||
fe3f566c | 531 | if (compressed_size && compressed_pages) |
c8b97818 | 532 | cur_size = compressed_size; |
c8b97818 | 533 | |
1acae57b FDBM |
534 | if (!extent_inserted) { |
535 | struct btrfs_key key; | |
536 | size_t datasize; | |
c8b97818 | 537 | |
8dd9872d OS |
538 | key.objectid = btrfs_ino(inode); |
539 | key.offset = 0; | |
962a298f | 540 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 541 | |
1acae57b | 542 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
1acae57b FDBM |
543 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
544 | datasize); | |
79b4f4c6 | 545 | if (ret) |
1acae57b | 546 | goto fail; |
c8b97818 CM |
547 | } |
548 | leaf = path->nodes[0]; | |
549 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
550 | struct btrfs_file_extent_item); | |
551 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
552 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
553 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
554 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
555 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
556 | ptr = btrfs_file_extent_inline_start(ei); | |
557 | ||
261507a0 | 558 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
559 | struct page *cpage; |
560 | int i = 0; | |
d397712b | 561 | while (compressed_size > 0) { |
c8b97818 | 562 | cpage = compressed_pages[i]; |
5b050f04 | 563 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 564 | PAGE_SIZE); |
c8b97818 | 565 | |
4cb2e5e8 | 566 | kaddr = kmap_local_page(cpage); |
c8b97818 | 567 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
4cb2e5e8 | 568 | kunmap_local(kaddr); |
c8b97818 CM |
569 | |
570 | i++; | |
571 | ptr += cur_size; | |
572 | compressed_size -= cur_size; | |
573 | } | |
574 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 575 | compress_type); |
c8b97818 | 576 | } else { |
8dd9872d | 577 | page = find_get_page(inode->vfs_inode.i_mapping, 0); |
c8b97818 | 578 | btrfs_set_file_extent_compression(leaf, ei, 0); |
4cb2e5e8 | 579 | kaddr = kmap_local_page(page); |
8dd9872d | 580 | write_extent_buffer(leaf, kaddr, ptr, size); |
4cb2e5e8 | 581 | kunmap_local(kaddr); |
09cbfeaf | 582 | put_page(page); |
c8b97818 | 583 | } |
50564b65 | 584 | btrfs_mark_buffer_dirty(trans, leaf); |
1acae57b | 585 | btrfs_release_path(path); |
c8b97818 | 586 | |
9ddc959e JB |
587 | /* |
588 | * We align size to sectorsize for inline extents just for simplicity | |
589 | * sake. | |
590 | */ | |
8dd9872d OS |
591 | ret = btrfs_inode_set_file_extent_range(inode, 0, |
592 | ALIGN(size, root->fs_info->sectorsize)); | |
9ddc959e JB |
593 | if (ret) |
594 | goto fail; | |
595 | ||
c2167754 | 596 | /* |
d9496e8a OS |
597 | * We're an inline extent, so nobody can extend the file past i_size |
598 | * without locking a page we already have locked. | |
c2167754 | 599 | * |
d9496e8a OS |
600 | * We must do any i_size and inode updates before we unlock the pages. |
601 | * Otherwise we could end up racing with unlink. | |
c2167754 | 602 | */ |
d9496e8a OS |
603 | i_size = i_size_read(&inode->vfs_inode); |
604 | if (update_i_size && size > i_size) { | |
605 | i_size_write(&inode->vfs_inode, size); | |
606 | i_size = size; | |
607 | } | |
608 | inode->disk_i_size = i_size; | |
8dd9872d | 609 | |
c8b97818 | 610 | fail: |
79b4f4c6 | 611 | return ret; |
c8b97818 CM |
612 | } |
613 | ||
614 | ||
615 | /* | |
616 | * conditionally insert an inline extent into the file. This | |
617 | * does the checks required to make sure the data is small enough | |
618 | * to fit as an inline extent. | |
619 | */ | |
8dd9872d OS |
620 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, |
621 | size_t compressed_size, | |
00361589 | 622 | int compress_type, |
d9496e8a OS |
623 | struct page **compressed_pages, |
624 | bool update_i_size) | |
c8b97818 | 625 | { |
5893dfb9 | 626 | struct btrfs_drop_extents_args drop_args = { 0 }; |
a0349401 | 627 | struct btrfs_root *root = inode->root; |
0b246afa | 628 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 629 | struct btrfs_trans_handle *trans; |
8dd9872d | 630 | u64 data_len = (compressed_size ?: size); |
c8b97818 | 631 | int ret; |
1acae57b | 632 | struct btrfs_path *path; |
c8b97818 | 633 | |
8dd9872d OS |
634 | /* |
635 | * We can create an inline extent if it ends at or beyond the current | |
636 | * i_size, is no larger than a sector (decompressed), and the (possibly | |
637 | * compressed) data fits in a leaf and the configured maximum inline | |
638 | * size. | |
639 | */ | |
640 | if (size < i_size_read(&inode->vfs_inode) || | |
641 | size > fs_info->sectorsize || | |
0b246afa | 642 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
8dd9872d | 643 | data_len > fs_info->max_inline) |
c8b97818 | 644 | return 1; |
c8b97818 | 645 | |
1acae57b FDBM |
646 | path = btrfs_alloc_path(); |
647 | if (!path) | |
648 | return -ENOMEM; | |
649 | ||
00361589 | 650 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
651 | if (IS_ERR(trans)) { |
652 | btrfs_free_path(path); | |
00361589 | 653 | return PTR_ERR(trans); |
1acae57b | 654 | } |
a0349401 | 655 | trans->block_rsv = &inode->block_rsv; |
00361589 | 656 | |
5893dfb9 | 657 | drop_args.path = path; |
8dd9872d OS |
658 | drop_args.start = 0; |
659 | drop_args.end = fs_info->sectorsize; | |
5893dfb9 FM |
660 | drop_args.drop_cache = true; |
661 | drop_args.replace_extent = true; | |
8dd9872d | 662 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len); |
5893dfb9 | 663 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
00361589 | 664 | if (ret) { |
66642832 | 665 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
666 | goto out; |
667 | } | |
c8b97818 | 668 | |
8dd9872d OS |
669 | ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted, |
670 | size, compressed_size, compress_type, | |
d9496e8a | 671 | compressed_pages, update_i_size); |
2adcac1a | 672 | if (ret && ret != -ENOSPC) { |
66642832 | 673 | btrfs_abort_transaction(trans, ret); |
00361589 | 674 | goto out; |
2adcac1a | 675 | } else if (ret == -ENOSPC) { |
00361589 JB |
676 | ret = 1; |
677 | goto out; | |
79787eaa | 678 | } |
2adcac1a | 679 | |
8dd9872d | 680 | btrfs_update_inode_bytes(inode, size, drop_args.bytes_found); |
8b9d0322 | 681 | ret = btrfs_update_inode(trans, inode); |
2766ff61 FM |
682 | if (ret && ret != -ENOSPC) { |
683 | btrfs_abort_transaction(trans, ret); | |
684 | goto out; | |
685 | } else if (ret == -ENOSPC) { | |
686 | ret = 1; | |
687 | goto out; | |
688 | } | |
689 | ||
23e3337f | 690 | btrfs_set_inode_full_sync(inode); |
00361589 | 691 | out: |
94ed938a QW |
692 | /* |
693 | * Don't forget to free the reserved space, as for inlined extent | |
694 | * it won't count as data extent, free them directly here. | |
695 | * And at reserve time, it's always aligned to page size, so | |
696 | * just free one page here. | |
697 | */ | |
9e65bfca | 698 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE, NULL); |
1acae57b | 699 | btrfs_free_path(path); |
3a45bb20 | 700 | btrfs_end_transaction(trans); |
00361589 | 701 | return ret; |
c8b97818 CM |
702 | } |
703 | ||
771ed689 CM |
704 | struct async_extent { |
705 | u64 start; | |
706 | u64 ram_size; | |
707 | u64 compressed_size; | |
708 | struct page **pages; | |
709 | unsigned long nr_pages; | |
261507a0 | 710 | int compress_type; |
771ed689 CM |
711 | struct list_head list; |
712 | }; | |
713 | ||
97db1204 | 714 | struct async_chunk { |
99a81a44 | 715 | struct btrfs_inode *inode; |
771ed689 CM |
716 | struct page *locked_page; |
717 | u64 start; | |
718 | u64 end; | |
bf9486d6 | 719 | blk_opf_t write_flags; |
771ed689 | 720 | struct list_head extents; |
ec39f769 | 721 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 722 | struct btrfs_work work; |
9e895a8f | 723 | struct async_cow *async_cow; |
771ed689 CM |
724 | }; |
725 | ||
97db1204 | 726 | struct async_cow { |
97db1204 NB |
727 | atomic_t num_chunks; |
728 | struct async_chunk chunks[]; | |
771ed689 CM |
729 | }; |
730 | ||
97db1204 | 731 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
732 | u64 start, u64 ram_size, |
733 | u64 compressed_size, | |
734 | struct page **pages, | |
261507a0 LZ |
735 | unsigned long nr_pages, |
736 | int compress_type) | |
771ed689 CM |
737 | { |
738 | struct async_extent *async_extent; | |
739 | ||
740 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 741 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
742 | async_extent->start = start; |
743 | async_extent->ram_size = ram_size; | |
744 | async_extent->compressed_size = compressed_size; | |
745 | async_extent->pages = pages; | |
746 | async_extent->nr_pages = nr_pages; | |
261507a0 | 747 | async_extent->compress_type = compress_type; |
771ed689 CM |
748 | list_add_tail(&async_extent->list, &cow->extents); |
749 | return 0; | |
750 | } | |
751 | ||
42c16da6 QW |
752 | /* |
753 | * Check if the inode needs to be submitted to compression, based on mount | |
754 | * options, defragmentation, properties or heuristics. | |
755 | */ | |
808a1292 NB |
756 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
757 | u64 end) | |
f79707b0 | 758 | { |
808a1292 | 759 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 760 | |
e6f9d696 | 761 | if (!btrfs_inode_can_compress(inode)) { |
42c16da6 QW |
762 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
763 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 764 | btrfs_ino(inode)); |
42c16da6 QW |
765 | return 0; |
766 | } | |
0cf9b244 QW |
767 | /* |
768 | * Special check for subpage. | |
769 | * | |
770 | * We lock the full page then run each delalloc range in the page, thus | |
771 | * for the following case, we will hit some subpage specific corner case: | |
772 | * | |
773 | * 0 32K 64K | |
774 | * | |///////| |///////| | |
775 | * \- A \- B | |
776 | * | |
777 | * In above case, both range A and range B will try to unlock the full | |
778 | * page [0, 64K), causing the one finished later will have page | |
779 | * unlocked already, triggering various page lock requirement BUG_ON()s. | |
780 | * | |
781 | * So here we add an artificial limit that subpage compression can only | |
782 | * if the range is fully page aligned. | |
783 | * | |
784 | * In theory we only need to ensure the first page is fully covered, but | |
785 | * the tailing partial page will be locked until the full compression | |
786 | * finishes, delaying the write of other range. | |
787 | * | |
788 | * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range | |
789 | * first to prevent any submitted async extent to unlock the full page. | |
790 | * By this, we can ensure for subpage case that only the last async_cow | |
791 | * will unlock the full page. | |
792 | */ | |
793 | if (fs_info->sectorsize < PAGE_SIZE) { | |
1280d2d1 FK |
794 | if (!PAGE_ALIGNED(start) || |
795 | !PAGE_ALIGNED(end + 1)) | |
0cf9b244 QW |
796 | return 0; |
797 | } | |
798 | ||
f79707b0 | 799 | /* force compress */ |
0b246afa | 800 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 801 | return 1; |
eec63c65 | 802 | /* defrag ioctl */ |
808a1292 | 803 | if (inode->defrag_compress) |
eec63c65 | 804 | return 1; |
f79707b0 | 805 | /* bad compression ratios */ |
808a1292 | 806 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 807 | return 0; |
0b246afa | 808 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
809 | inode->flags & BTRFS_INODE_COMPRESS || |
810 | inode->prop_compress) | |
811 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
812 | return 0; |
813 | } | |
814 | ||
6158e1ce | 815 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
558732df | 816 | u64 start, u64 end, u64 num_bytes, u32 small_write) |
26d30f85 AJ |
817 | { |
818 | /* If this is a small write inside eof, kick off a defrag */ | |
819 | if (num_bytes < small_write && | |
6158e1ce | 820 | (start > 0 || end + 1 < inode->disk_i_size)) |
558732df | 821 | btrfs_add_inode_defrag(NULL, inode, small_write); |
26d30f85 AJ |
822 | } |
823 | ||
d352ac68 | 824 | /* |
c15d8cf2 | 825 | * Work queue call back to started compression on a file and pages. |
c8b97818 | 826 | * |
c15d8cf2 CH |
827 | * This is done inside an ordered work queue, and the compression is spread |
828 | * across many cpus. The actual IO submission is step two, and the ordered work | |
829 | * queue takes care of making sure that happens in the same order things were | |
830 | * put onto the queue by writepages and friends. | |
c8b97818 | 831 | * |
c15d8cf2 CH |
832 | * If this code finds it can't get good compression, it puts an entry onto the |
833 | * work queue to write the uncompressed bytes. This makes sure that both | |
834 | * compressed inodes and uncompressed inodes are written in the same order that | |
835 | * the flusher thread sent them down. | |
d352ac68 | 836 | */ |
c15d8cf2 | 837 | static void compress_file_range(struct btrfs_work *work) |
b888db2b | 838 | { |
c15d8cf2 CH |
839 | struct async_chunk *async_chunk = |
840 | container_of(work, struct async_chunk, work); | |
99a01bd6 DS |
841 | struct btrfs_inode *inode = async_chunk->inode; |
842 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
a994310a | 843 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
0b246afa | 844 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
845 | u64 start = async_chunk->start; |
846 | u64 end = async_chunk->end; | |
c8b97818 | 847 | u64 actual_end; |
d98da499 | 848 | u64 i_size; |
e6dcd2dc | 849 | int ret = 0; |
e94e54e8 | 850 | struct page **pages; |
c8b97818 | 851 | unsigned long nr_pages; |
c8b97818 CM |
852 | unsigned long total_compressed = 0; |
853 | unsigned long total_in = 0; | |
e94e54e8 | 854 | unsigned int poff; |
c8b97818 | 855 | int i; |
0b246afa | 856 | int compress_type = fs_info->compress_type; |
b888db2b | 857 | |
99a01bd6 | 858 | inode_should_defrag(inode, start, end, end - start + 1, SZ_16K); |
4cb5300b | 859 | |
44962ca3 CH |
860 | /* |
861 | * We need to call clear_page_dirty_for_io on each page in the range. | |
862 | * Otherwise applications with the file mmap'd can wander in and change | |
863 | * the page contents while we are compressing them. | |
864 | */ | |
865 | extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end); | |
866 | ||
d98da499 JB |
867 | /* |
868 | * We need to save i_size before now because it could change in between | |
869 | * us evaluating the size and assigning it. This is because we lock and | |
870 | * unlock the page in truncate and fallocate, and then modify the i_size | |
871 | * later on. | |
872 | * | |
873 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
874 | * does that for us. | |
875 | */ | |
876 | barrier(); | |
99a01bd6 | 877 | i_size = i_size_read(&inode->vfs_inode); |
d98da499 JB |
878 | barrier(); |
879 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 | 880 | again: |
e94e54e8 | 881 | pages = NULL; |
09cbfeaf | 882 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
544fe4a9 | 883 | nr_pages = min_t(unsigned long, nr_pages, BTRFS_MAX_COMPRESSED_PAGES); |
be20aa9d | 884 | |
f03d9301 CM |
885 | /* |
886 | * we don't want to send crud past the end of i_size through | |
887 | * compression, that's just a waste of CPU time. So, if the | |
888 | * end of the file is before the start of our current | |
889 | * requested range of bytes, we bail out to the uncompressed | |
890 | * cleanup code that can deal with all of this. | |
891 | * | |
892 | * It isn't really the fastest way to fix things, but this is a | |
893 | * very uncommon corner. | |
894 | */ | |
895 | if (actual_end <= start) | |
896 | goto cleanup_and_bail_uncompressed; | |
897 | ||
c8b97818 CM |
898 | total_compressed = actual_end - start; |
899 | ||
4bcbb332 | 900 | /* |
0cf9b244 | 901 | * Skip compression for a small file range(<=blocksize) that |
01327610 | 902 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
903 | */ |
904 | if (total_compressed <= blocksize && | |
99a01bd6 | 905 | (start > 0 || end + 1 < inode->disk_i_size)) |
4bcbb332 SW |
906 | goto cleanup_and_bail_uncompressed; |
907 | ||
0cf9b244 QW |
908 | /* |
909 | * For subpage case, we require full page alignment for the sector | |
910 | * aligned range. | |
911 | * Thus we must also check against @actual_end, not just @end. | |
912 | */ | |
913 | if (blocksize < PAGE_SIZE) { | |
1280d2d1 FK |
914 | if (!PAGE_ALIGNED(start) || |
915 | !PAGE_ALIGNED(round_up(actual_end, blocksize))) | |
0cf9b244 QW |
916 | goto cleanup_and_bail_uncompressed; |
917 | } | |
918 | ||
069eac78 DS |
919 | total_compressed = min_t(unsigned long, total_compressed, |
920 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
921 | total_in = 0; |
922 | ret = 0; | |
db94535d | 923 | |
771ed689 | 924 | /* |
e94e54e8 CH |
925 | * We do compression for mount -o compress and when the inode has not |
926 | * been flagged as NOCOMPRESS. This flag can change at any time if we | |
927 | * discover bad compression ratios. | |
c8b97818 | 928 | */ |
e94e54e8 | 929 | if (!inode_need_compress(inode, start, end)) |
6a7167bf | 930 | goto cleanup_and_bail_uncompressed; |
261507a0 | 931 | |
e94e54e8 CH |
932 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
933 | if (!pages) { | |
4adaa611 | 934 | /* |
e94e54e8 CH |
935 | * Memory allocation failure is not a fatal error, we can fall |
936 | * back to uncompressed code. | |
4adaa611 | 937 | */ |
6a7167bf | 938 | goto cleanup_and_bail_uncompressed; |
e94e54e8 | 939 | } |
f51d2b59 | 940 | |
e94e54e8 CH |
941 | if (inode->defrag_compress) |
942 | compress_type = inode->defrag_compress; | |
943 | else if (inode->prop_compress) | |
944 | compress_type = inode->prop_compress; | |
945 | ||
e94e54e8 CH |
946 | /* Compression level is applied here. */ |
947 | ret = btrfs_compress_pages(compress_type | (fs_info->compress_level << 4), | |
948 | mapping, start, pages, &nr_pages, &total_in, | |
949 | &total_compressed); | |
950 | if (ret) | |
184aa1ff | 951 | goto mark_incompressible; |
c8b97818 | 952 | |
e94e54e8 CH |
953 | /* |
954 | * Zero the tail end of the last page, as we might be sending it down | |
955 | * to disk. | |
956 | */ | |
957 | poff = offset_in_page(total_compressed); | |
958 | if (poff) | |
959 | memzero_page(pages[nr_pages - 1], poff, PAGE_SIZE - poff); | |
c8b97818 | 960 | |
7367253a | 961 | /* |
6a7167bf CH |
962 | * Try to create an inline extent. |
963 | * | |
964 | * If we didn't compress the entire range, try to create an uncompressed | |
965 | * inline extent, else a compressed one. | |
966 | * | |
7367253a | 967 | * Check cow_file_range() for why we don't even try to create inline |
e94e54e8 | 968 | * extent for the subpage case. |
7367253a QW |
969 | */ |
970 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { | |
6a7167bf CH |
971 | if (total_in < actual_end) { |
972 | ret = cow_file_range_inline(inode, actual_end, 0, | |
973 | BTRFS_COMPRESS_NONE, NULL, | |
974 | false); | |
c8b97818 | 975 | } else { |
99a01bd6 | 976 | ret = cow_file_range_inline(inode, actual_end, |
fe3f566c | 977 | total_compressed, |
d9496e8a OS |
978 | compress_type, pages, |
979 | false); | |
c8b97818 | 980 | } |
79787eaa | 981 | if (ret <= 0) { |
151a41bc | 982 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
983 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
984 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 | 985 | |
a994310a CH |
986 | if (ret < 0) |
987 | mapping_set_error(mapping, -EIO); | |
151a41bc | 988 | |
771ed689 | 989 | /* |
79787eaa JM |
990 | * inline extent creation worked or returned error, |
991 | * we don't need to create any more async work items. | |
992 | * Unlock and free up our temp pages. | |
8b62f87b JB |
993 | * |
994 | * We use DO_ACCOUNTING here because we need the | |
995 | * delalloc_release_metadata to be done _after_ we drop | |
996 | * our outstanding extent for clearing delalloc for this | |
997 | * range. | |
771ed689 | 998 | */ |
99a01bd6 | 999 | extent_clear_unlock_delalloc(inode, start, end, |
ad7ff17b | 1000 | NULL, |
74e9194a | 1001 | clear_flags, |
ba8b04c1 | 1002 | PAGE_UNLOCK | |
6869b0a8 | 1003 | PAGE_START_WRITEBACK | |
c2790a2e | 1004 | PAGE_END_WRITEBACK); |
f778b6b8 | 1005 | goto free_pages; |
c8b97818 CM |
1006 | } |
1007 | } | |
1008 | ||
e94e54e8 CH |
1009 | /* |
1010 | * We aren't doing an inline extent. Round the compressed size up to a | |
1011 | * block size boundary so the allocator does sane things. | |
1012 | */ | |
1013 | total_compressed = ALIGN(total_compressed, blocksize); | |
c8b97818 | 1014 | |
e94e54e8 CH |
1015 | /* |
1016 | * One last check to make sure the compression is really a win, compare | |
1017 | * the page count read with the blocks on disk, compression must free at | |
1018 | * least one sector. | |
1019 | */ | |
1020 | total_in = round_up(total_in, fs_info->sectorsize); | |
1021 | if (total_compressed + blocksize > total_in) | |
184aa1ff | 1022 | goto mark_incompressible; |
c8bb0c8b | 1023 | |
e94e54e8 CH |
1024 | /* |
1025 | * The async work queues will take care of doing actual allocation on | |
1026 | * disk for these compressed pages, and will submit the bios. | |
1027 | */ | |
1028 | add_async_extent(async_chunk, start, total_in, total_compressed, pages, | |
1029 | nr_pages, compress_type); | |
1030 | if (start + total_in < end) { | |
1031 | start += total_in; | |
1032 | cond_resched(); | |
1033 | goto again; | |
c8b97818 | 1034 | } |
e94e54e8 CH |
1035 | return; |
1036 | ||
184aa1ff CH |
1037 | mark_incompressible: |
1038 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress) | |
1039 | inode->flags |= BTRFS_INODE_NOCOMPRESS; | |
e94e54e8 | 1040 | cleanup_and_bail_uncompressed: |
b5326271 | 1041 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 1042 | BTRFS_COMPRESS_NONE); |
f778b6b8 | 1043 | free_pages: |
c8bb0c8b | 1044 | if (pages) { |
4d3a800e | 1045 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 1046 | WARN_ON(pages[i]->mapping); |
9ba965dc | 1047 | btrfs_free_compr_page(pages[i]); |
c8b97818 CM |
1048 | } |
1049 | kfree(pages); | |
c8b97818 | 1050 | } |
771ed689 | 1051 | } |
771ed689 | 1052 | |
40ae837b FM |
1053 | static void free_async_extent_pages(struct async_extent *async_extent) |
1054 | { | |
1055 | int i; | |
1056 | ||
1057 | if (!async_extent->pages) | |
1058 | return; | |
1059 | ||
1060 | for (i = 0; i < async_extent->nr_pages; i++) { | |
1061 | WARN_ON(async_extent->pages[i]->mapping); | |
9ba965dc | 1062 | btrfs_free_compr_page(async_extent->pages[i]); |
40ae837b FM |
1063 | } |
1064 | kfree(async_extent->pages); | |
1065 | async_extent->nr_pages = 0; | |
1066 | async_extent->pages = NULL; | |
771ed689 CM |
1067 | } |
1068 | ||
ff20d6a4 CH |
1069 | static void submit_uncompressed_range(struct btrfs_inode *inode, |
1070 | struct async_extent *async_extent, | |
1071 | struct page *locked_page) | |
771ed689 | 1072 | { |
2b83a0ee QW |
1073 | u64 start = async_extent->start; |
1074 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
2b83a0ee | 1075 | int ret; |
7027f871 CH |
1076 | struct writeback_control wbc = { |
1077 | .sync_mode = WB_SYNC_ALL, | |
1078 | .range_start = start, | |
1079 | .range_end = end, | |
1080 | .no_cgroup_owner = 1, | |
1081 | }; | |
771ed689 | 1082 | |
256b0cf9 CH |
1083 | wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode); |
1084 | ret = run_delalloc_cow(inode, locked_page, start, end, &wbc, false); | |
1085 | wbc_detach_inode(&wbc); | |
2b83a0ee | 1086 | if (ret < 0) { |
71aa147b NA |
1087 | btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1); |
1088 | if (locked_page) { | |
1089 | const u64 page_start = page_offset(locked_page); | |
71aa147b | 1090 | |
71aa147b NA |
1091 | set_page_writeback(locked_page); |
1092 | end_page_writeback(locked_page); | |
9783e4de CH |
1093 | btrfs_mark_ordered_io_finished(inode, locked_page, |
1094 | page_start, PAGE_SIZE, | |
1095 | !ret); | |
9783e4de | 1096 | mapping_set_error(locked_page->mapping, ret); |
2b83a0ee | 1097 | unlock_page(locked_page); |
71aa147b | 1098 | } |
2b83a0ee | 1099 | } |
2b83a0ee | 1100 | } |
79787eaa | 1101 | |
84f262f0 CH |
1102 | static void submit_one_async_extent(struct async_chunk *async_chunk, |
1103 | struct async_extent *async_extent, | |
1104 | u64 *alloc_hint) | |
771ed689 | 1105 | { |
84f262f0 | 1106 | struct btrfs_inode *inode = async_chunk->inode; |
b4ccace8 QW |
1107 | struct extent_io_tree *io_tree = &inode->io_tree; |
1108 | struct btrfs_root *root = inode->root; | |
1109 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d611935b | 1110 | struct btrfs_ordered_extent *ordered; |
771ed689 | 1111 | struct btrfs_key ins; |
2b83a0ee | 1112 | struct page *locked_page = NULL; |
771ed689 | 1113 | struct extent_map *em; |
f5a84ee3 | 1114 | int ret = 0; |
b4ccace8 QW |
1115 | u64 start = async_extent->start; |
1116 | u64 end = async_extent->start + async_extent->ram_size - 1; | |
771ed689 | 1117 | |
896d7c1a CH |
1118 | if (async_chunk->blkcg_css) |
1119 | kthread_associate_blkcg(async_chunk->blkcg_css); | |
1120 | ||
2b83a0ee QW |
1121 | /* |
1122 | * If async_chunk->locked_page is in the async_extent range, we need to | |
1123 | * handle it. | |
1124 | */ | |
1125 | if (async_chunk->locked_page) { | |
1126 | u64 locked_page_start = page_offset(async_chunk->locked_page); | |
1127 | u64 locked_page_end = locked_page_start + PAGE_SIZE - 1; | |
3e04e7f1 | 1128 | |
2b83a0ee QW |
1129 | if (!(start >= locked_page_end || end <= locked_page_start)) |
1130 | locked_page = async_chunk->locked_page; | |
b4ccace8 | 1131 | } |
570eb97b | 1132 | lock_extent(io_tree, start, end, NULL); |
ce62003f | 1133 | |
67583468 | 1134 | if (async_extent->compress_type == BTRFS_COMPRESS_NONE) { |
ff20d6a4 | 1135 | submit_uncompressed_range(inode, async_extent, locked_page); |
e43a6210 CH |
1136 | goto done; |
1137 | } | |
ce62003f | 1138 | |
b4ccace8 QW |
1139 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
1140 | async_extent->compressed_size, | |
1141 | async_extent->compressed_size, | |
1142 | 0, *alloc_hint, &ins, 1, 1); | |
1143 | if (ret) { | |
c2167754 | 1144 | /* |
b4ccace8 QW |
1145 | * Here we used to try again by going back to non-compressed |
1146 | * path for ENOSPC. But we can't reserve space even for | |
1147 | * compressed size, how could it work for uncompressed size | |
1148 | * which requires larger size? So here we directly go error | |
1149 | * path. | |
c2167754 | 1150 | */ |
b4ccace8 QW |
1151 | goto out_free; |
1152 | } | |
1153 | ||
1154 | /* Here we're doing allocation and writeback of the compressed pages */ | |
1155 | em = create_io_em(inode, start, | |
1156 | async_extent->ram_size, /* len */ | |
1157 | start, /* orig_start */ | |
1158 | ins.objectid, /* block_start */ | |
1159 | ins.offset, /* block_len */ | |
1160 | ins.offset, /* orig_block_len */ | |
1161 | async_extent->ram_size, /* ram_bytes */ | |
1162 | async_extent->compress_type, | |
1163 | BTRFS_ORDERED_COMPRESSED); | |
1164 | if (IS_ERR(em)) { | |
1165 | ret = PTR_ERR(em); | |
1166 | goto out_free_reserve; | |
1167 | } | |
1168 | free_extent_map(em); | |
771ed689 | 1169 | |
d611935b | 1170 | ordered = btrfs_alloc_ordered_extent(inode, start, /* file_offset */ |
cb36a9bb OS |
1171 | async_extent->ram_size, /* num_bytes */ |
1172 | async_extent->ram_size, /* ram_bytes */ | |
1173 | ins.objectid, /* disk_bytenr */ | |
1174 | ins.offset, /* disk_num_bytes */ | |
1175 | 0, /* offset */ | |
1176 | 1 << BTRFS_ORDERED_COMPRESSED, | |
1177 | async_extent->compress_type); | |
d611935b | 1178 | if (IS_ERR(ordered)) { |
4c0c8cfc | 1179 | btrfs_drop_extent_map_range(inode, start, end, false); |
d611935b | 1180 | ret = PTR_ERR(ordered); |
b4ccace8 | 1181 | goto out_free_reserve; |
771ed689 | 1182 | } |
b4ccace8 QW |
1183 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
1184 | ||
1185 | /* Clear dirty, set writeback and unlock the pages. */ | |
1186 | extent_clear_unlock_delalloc(inode, start, end, | |
1187 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, | |
1188 | PAGE_UNLOCK | PAGE_START_WRITEBACK); | |
d611935b | 1189 | btrfs_submit_compressed_write(ordered, |
b4ccace8 QW |
1190 | async_extent->pages, /* compressed_pages */ |
1191 | async_extent->nr_pages, | |
05d06a5c | 1192 | async_chunk->write_flags, true); |
b4ccace8 | 1193 | *alloc_hint = ins.objectid + ins.offset; |
e43a6210 | 1194 | done: |
896d7c1a CH |
1195 | if (async_chunk->blkcg_css) |
1196 | kthread_associate_blkcg(NULL); | |
b4ccace8 | 1197 | kfree(async_extent); |
84f262f0 | 1198 | return; |
b4ccace8 | 1199 | |
3e04e7f1 | 1200 | out_free_reserve: |
0b246afa | 1201 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1202 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1203 | out_free: |
a994310a | 1204 | mapping_set_error(inode->vfs_inode.i_mapping, -EIO); |
b4ccace8 | 1205 | extent_clear_unlock_delalloc(inode, start, end, |
c2790a2e | 1206 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 1207 | EXTENT_DELALLOC_NEW | |
151a41bc | 1208 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
6869b0a8 | 1209 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
a994310a | 1210 | PAGE_END_WRITEBACK); |
40ae837b | 1211 | free_async_extent_pages(async_extent); |
84f262f0 CH |
1212 | if (async_chunk->blkcg_css) |
1213 | kthread_associate_blkcg(NULL); | |
1214 | btrfs_debug(fs_info, | |
b4ccace8 | 1215 | "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d", |
84f262f0 CH |
1216 | root->root_key.objectid, btrfs_ino(inode), start, |
1217 | async_extent->ram_size, ret); | |
1218 | kfree(async_extent); | |
771ed689 CM |
1219 | } |
1220 | ||
43c69849 | 1221 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
1222 | u64 num_bytes) |
1223 | { | |
43c69849 | 1224 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
1225 | struct extent_map *em; |
1226 | u64 alloc_hint = 0; | |
1227 | ||
1228 | read_lock(&em_tree->lock); | |
1229 | em = search_extent_mapping(em_tree, start, num_bytes); | |
1230 | if (em) { | |
1231 | /* | |
1232 | * if block start isn't an actual block number then find the | |
1233 | * first block in this inode and use that as a hint. If that | |
1234 | * block is also bogus then just don't worry about it. | |
1235 | */ | |
1236 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
1237 | free_extent_map(em); | |
1238 | em = search_extent_mapping(em_tree, 0, 0); | |
1239 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
1240 | alloc_hint = em->block_start; | |
1241 | if (em) | |
1242 | free_extent_map(em); | |
1243 | } else { | |
1244 | alloc_hint = em->block_start; | |
1245 | free_extent_map(em); | |
1246 | } | |
1247 | } | |
1248 | read_unlock(&em_tree->lock); | |
1249 | ||
1250 | return alloc_hint; | |
1251 | } | |
1252 | ||
771ed689 CM |
1253 | /* |
1254 | * when extent_io.c finds a delayed allocation range in the file, | |
1255 | * the call backs end up in this code. The basic idea is to | |
1256 | * allocate extents on disk for the range, and create ordered data structs | |
1257 | * in ram to track those extents. | |
1258 | * | |
1259 | * locked_page is the page that writepage had locked already. We use | |
1260 | * it to make sure we don't do extra locks or unlocks. | |
1261 | * | |
ba9145ad | 1262 | * When this function fails, it unlocks all pages except @locked_page. |
9ce7466f | 1263 | * |
c56cbe90 CH |
1264 | * When this function successfully creates an inline extent, it returns 1 and |
1265 | * unlocks all pages including locked_page and starts I/O on them. | |
ba9145ad CH |
1266 | * (In reality inline extents are limited to a single page, so locked_page is |
1267 | * the only page handled anyway). | |
9ce7466f | 1268 | * |
ba9145ad CH |
1269 | * When this function succeed and creates a normal extent, the page locking |
1270 | * status depends on the passed in flags: | |
9ce7466f | 1271 | * |
ba9145ad CH |
1272 | * - If @keep_locked is set, all pages are kept locked. |
1273 | * - Else all pages except for @locked_page are unlocked. | |
9ce7466f NA |
1274 | * |
1275 | * When a failure happens in the second or later iteration of the | |
1276 | * while-loop, the ordered extents created in previous iterations are kept | |
1277 | * intact. So, the caller must clean them up by calling | |
1278 | * btrfs_cleanup_ordered_extents(). See btrfs_run_delalloc_range() for | |
1279 | * example. | |
771ed689 | 1280 | */ |
6e26c442 | 1281 | static noinline int cow_file_range(struct btrfs_inode *inode, |
c56cbe90 CH |
1282 | struct page *locked_page, u64 start, u64 end, |
1283 | u64 *done_offset, | |
53ffb30a | 1284 | bool keep_locked, bool no_inline) |
771ed689 | 1285 | { |
6e26c442 NB |
1286 | struct btrfs_root *root = inode->root; |
1287 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 | 1288 | u64 alloc_hint = 0; |
9ce7466f | 1289 | u64 orig_start = start; |
771ed689 CM |
1290 | u64 num_bytes; |
1291 | unsigned long ram_size; | |
a315e68f | 1292 | u64 cur_alloc_size = 0; |
432cd2a1 | 1293 | u64 min_alloc_size; |
0b246afa | 1294 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
1295 | struct btrfs_key ins; |
1296 | struct extent_map *em; | |
a315e68f FM |
1297 | unsigned clear_bits; |
1298 | unsigned long page_ops; | |
1299 | bool extent_reserved = false; | |
771ed689 CM |
1300 | int ret = 0; |
1301 | ||
6e26c442 | 1302 | if (btrfs_is_free_space_inode(inode)) { |
29bce2f3 JB |
1303 | ret = -EINVAL; |
1304 | goto out_unlock; | |
02ecd2c2 | 1305 | } |
771ed689 | 1306 | |
fda2832f | 1307 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1308 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1309 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1310 | |
6e26c442 | 1311 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1312 | |
7367253a QW |
1313 | /* |
1314 | * Due to the page size limit, for subpage we can only trigger the | |
1315 | * writeback for the dirty sectors of page, that means data writeback | |
1316 | * is doing more writeback than what we want. | |
1317 | * | |
1318 | * This is especially unexpected for some call sites like fallocate, | |
1319 | * where we only increase i_size after everything is done. | |
1320 | * This means we can trigger inline extent even if we didn't want to. | |
1321 | * So here we skip inline extent creation completely. | |
1322 | */ | |
53ffb30a | 1323 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE && !no_inline) { |
8dd9872d OS |
1324 | u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode), |
1325 | end + 1); | |
1326 | ||
771ed689 | 1327 | /* lets try to make an inline extent */ |
8dd9872d | 1328 | ret = cow_file_range_inline(inode, actual_end, 0, |
d9496e8a | 1329 | BTRFS_COMPRESS_NONE, NULL, false); |
771ed689 | 1330 | if (ret == 0) { |
8b62f87b JB |
1331 | /* |
1332 | * We use DO_ACCOUNTING here because we need the | |
1333 | * delalloc_release_metadata to be run _after_ we drop | |
1334 | * our outstanding extent for clearing delalloc for this | |
1335 | * range. | |
1336 | */ | |
4750af3b QW |
1337 | extent_clear_unlock_delalloc(inode, start, end, |
1338 | locked_page, | |
c2790a2e | 1339 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1340 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1341 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 1342 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
4750af3b QW |
1343 | /* |
1344 | * locked_page is locked by the caller of | |
1345 | * writepage_delalloc(), not locked by | |
1346 | * __process_pages_contig(). | |
1347 | * | |
1348 | * We can't let __process_pages_contig() to unlock it, | |
1349 | * as it doesn't have any subpage::writers recorded. | |
1350 | * | |
1351 | * Here we manually unlock the page, since the caller | |
c56cbe90 CH |
1352 | * can't determine if it's an inline extent or a |
1353 | * compressed extent. | |
4750af3b QW |
1354 | */ |
1355 | unlock_page(locked_page); | |
6e144bf1 CH |
1356 | ret = 1; |
1357 | goto done; | |
79787eaa | 1358 | } else if (ret < 0) { |
79787eaa | 1359 | goto out_unlock; |
771ed689 CM |
1360 | } |
1361 | } | |
1362 | ||
6e26c442 | 1363 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 | 1364 | |
432cd2a1 FM |
1365 | /* |
1366 | * Relocation relies on the relocated extents to have exactly the same | |
1367 | * size as the original extents. Normally writeback for relocation data | |
1368 | * extents follows a NOCOW path because relocation preallocates the | |
1369 | * extents. However, due to an operation such as scrub turning a block | |
1370 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1371 | * an extent allocated during COW has exactly the requested size and can | |
1372 | * not be split into smaller extents, otherwise relocation breaks and | |
1373 | * fails during the stage where it updates the bytenr of file extent | |
1374 | * items. | |
1375 | */ | |
37f00a6d | 1376 | if (btrfs_is_data_reloc_root(root)) |
432cd2a1 FM |
1377 | min_alloc_size = num_bytes; |
1378 | else | |
1379 | min_alloc_size = fs_info->sectorsize; | |
1380 | ||
3752d22f | 1381 | while (num_bytes > 0) { |
34bfaf15 CH |
1382 | struct btrfs_ordered_extent *ordered; |
1383 | ||
3752d22f | 1384 | cur_alloc_size = num_bytes; |
18513091 | 1385 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1386 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1387 | &ins, 1, 1); |
6e144bf1 CH |
1388 | if (ret == -EAGAIN) { |
1389 | /* | |
1390 | * btrfs_reserve_extent only returns -EAGAIN for zoned | |
1391 | * file systems, which is an indication that there are | |
1392 | * no active zones to allocate from at the moment. | |
1393 | * | |
1394 | * If this is the first loop iteration, wait for at | |
1395 | * least one zone to finish before retrying the | |
1396 | * allocation. Otherwise ask the caller to write out | |
1397 | * the already allocated blocks before coming back to | |
1398 | * us, or return -ENOSPC if it can't handle retries. | |
1399 | */ | |
1400 | ASSERT(btrfs_is_zoned(fs_info)); | |
1401 | if (start == orig_start) { | |
1402 | wait_on_bit_io(&inode->root->fs_info->flags, | |
1403 | BTRFS_FS_NEED_ZONE_FINISH, | |
1404 | TASK_UNINTERRUPTIBLE); | |
1405 | continue; | |
1406 | } | |
1407 | if (done_offset) { | |
1408 | *done_offset = start - 1; | |
1409 | return 0; | |
1410 | } | |
1411 | ret = -ENOSPC; | |
1412 | } | |
00361589 | 1413 | if (ret < 0) |
79787eaa | 1414 | goto out_unlock; |
a315e68f FM |
1415 | cur_alloc_size = ins.offset; |
1416 | extent_reserved = true; | |
d397712b | 1417 | |
771ed689 | 1418 | ram_size = ins.offset; |
6e26c442 | 1419 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1420 | start, /* orig_start */ |
1421 | ins.objectid, /* block_start */ | |
1422 | ins.offset, /* block_len */ | |
1423 | ins.offset, /* orig_block_len */ | |
1424 | ram_size, /* ram_bytes */ | |
1425 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1426 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1427 | if (IS_ERR(em)) { |
1428 | ret = PTR_ERR(em); | |
ace68bac | 1429 | goto out_reserve; |
090a127a | 1430 | } |
6f9994db | 1431 | free_extent_map(em); |
e6dcd2dc | 1432 | |
34bfaf15 CH |
1433 | ordered = btrfs_alloc_ordered_extent(inode, start, ram_size, |
1434 | ram_size, ins.objectid, cur_alloc_size, | |
1435 | 0, 1 << BTRFS_ORDERED_REGULAR, | |
1436 | BTRFS_COMPRESS_NONE); | |
1437 | if (IS_ERR(ordered)) { | |
1438 | ret = PTR_ERR(ordered); | |
d9f85963 | 1439 | goto out_drop_extent_cache; |
34bfaf15 | 1440 | } |
c8b97818 | 1441 | |
37f00a6d | 1442 | if (btrfs_is_data_reloc_root(root)) { |
34bfaf15 CH |
1443 | ret = btrfs_reloc_clone_csums(ordered); |
1444 | ||
4dbd80fb QW |
1445 | /* |
1446 | * Only drop cache here, and process as normal. | |
1447 | * | |
1448 | * We must not allow extent_clear_unlock_delalloc() | |
1449 | * at out_unlock label to free meta of this ordered | |
1450 | * extent, as its meta should be freed by | |
1451 | * btrfs_finish_ordered_io(). | |
1452 | * | |
1453 | * So we must continue until @start is increased to | |
1454 | * skip current ordered extent. | |
1455 | */ | |
00361589 | 1456 | if (ret) |
4c0c8cfc FM |
1457 | btrfs_drop_extent_map_range(inode, start, |
1458 | start + ram_size - 1, | |
1459 | false); | |
17d217fe | 1460 | } |
34bfaf15 | 1461 | btrfs_put_ordered_extent(ordered); |
17d217fe | 1462 | |
0b246afa | 1463 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1464 | |
f57ad937 QW |
1465 | /* |
1466 | * We're not doing compressed IO, don't unlock the first page | |
1467 | * (which the caller expects to stay locked), don't clear any | |
1468 | * dirty bits and don't set any writeback bits | |
8b62b72b | 1469 | * |
f57ad937 QW |
1470 | * Do set the Ordered (Private2) bit so we know this page was |
1471 | * properly setup for writepage. | |
c8b97818 | 1472 | */ |
ba9145ad | 1473 | page_ops = (keep_locked ? 0 : PAGE_UNLOCK); |
f57ad937 | 1474 | page_ops |= PAGE_SET_ORDERED; |
a791e35e | 1475 | |
6e26c442 | 1476 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1477 | locked_page, |
c2790a2e | 1478 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1479 | page_ops); |
3752d22f AJ |
1480 | if (num_bytes < cur_alloc_size) |
1481 | num_bytes = 0; | |
4dbd80fb | 1482 | else |
3752d22f | 1483 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1484 | alloc_hint = ins.objectid + ins.offset; |
1485 | start += cur_alloc_size; | |
a315e68f | 1486 | extent_reserved = false; |
4dbd80fb QW |
1487 | |
1488 | /* | |
1489 | * btrfs_reloc_clone_csums() error, since start is increased | |
1490 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1491 | * free metadata of current ordered extent, we're OK to exit. | |
1492 | */ | |
1493 | if (ret) | |
1494 | goto out_unlock; | |
b888db2b | 1495 | } |
6e144bf1 CH |
1496 | done: |
1497 | if (done_offset) | |
1498 | *done_offset = end; | |
be20aa9d | 1499 | return ret; |
b7d5b0a8 | 1500 | |
d9f85963 | 1501 | out_drop_extent_cache: |
4c0c8cfc | 1502 | btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false); |
ace68bac | 1503 | out_reserve: |
0b246afa | 1504 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1505 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1506 | out_unlock: |
9ce7466f NA |
1507 | /* |
1508 | * Now, we have three regions to clean up: | |
1509 | * | |
1510 | * |-------(1)----|---(2)---|-------------(3)----------| | |
1511 | * `- orig_start `- start `- start + cur_alloc_size `- end | |
1512 | * | |
1513 | * We process each region below. | |
1514 | */ | |
1515 | ||
a7e3b975 FM |
1516 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1517 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
6869b0a8 | 1518 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
9ce7466f | 1519 | |
a315e68f | 1520 | /* |
9ce7466f NA |
1521 | * For the range (1). We have already instantiated the ordered extents |
1522 | * for this region. They are cleaned up by | |
1523 | * btrfs_cleanup_ordered_extents() in e.g, | |
1524 | * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are | |
1525 | * already cleared in the above loop. And, EXTENT_DELALLOC_NEW | | |
1526 | * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup | |
1527 | * function. | |
1528 | * | |
ba9145ad | 1529 | * However, in case of @keep_locked, we still need to unlock the pages |
9ce7466f NA |
1530 | * (except @locked_page) to ensure all the pages are unlocked. |
1531 | */ | |
ba9145ad | 1532 | if (keep_locked && orig_start < start) { |
71aa147b NA |
1533 | if (!locked_page) |
1534 | mapping_set_error(inode->vfs_inode.i_mapping, ret); | |
9ce7466f NA |
1535 | extent_clear_unlock_delalloc(inode, orig_start, start - 1, |
1536 | locked_page, 0, page_ops); | |
71aa147b | 1537 | } |
9ce7466f | 1538 | |
a315e68f | 1539 | /* |
9ce7466f NA |
1540 | * For the range (2). If we reserved an extent for our delalloc range |
1541 | * (or a subrange) and failed to create the respective ordered extent, | |
1542 | * then it means that when we reserved the extent we decremented the | |
1543 | * extent's size from the data space_info's bytes_may_use counter and | |
1544 | * incremented the space_info's bytes_reserved counter by the same | |
1545 | * amount. We must make sure extent_clear_unlock_delalloc() does not try | |
1546 | * to decrement again the data space_info's bytes_may_use counter, | |
1547 | * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV. | |
a315e68f FM |
1548 | */ |
1549 | if (extent_reserved) { | |
6e26c442 | 1550 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1551 | start + cur_alloc_size - 1, |
a315e68f FM |
1552 | locked_page, |
1553 | clear_bits, | |
1554 | page_ops); | |
1555 | start += cur_alloc_size; | |
a315e68f | 1556 | } |
9ce7466f NA |
1557 | |
1558 | /* | |
1559 | * For the range (3). We never touched the region. In addition to the | |
1560 | * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data | |
1561 | * space_info's bytes_may_use counter, reserved in | |
1562 | * btrfs_check_data_free_space(). | |
1563 | */ | |
12b2d64e CH |
1564 | if (start < end) { |
1565 | clear_bits |= EXTENT_CLEAR_DATA_RESV; | |
1566 | extent_clear_unlock_delalloc(inode, start, end, locked_page, | |
1567 | clear_bits, page_ops); | |
1568 | } | |
aaafa1eb | 1569 | return ret; |
771ed689 | 1570 | } |
c8b97818 | 1571 | |
771ed689 | 1572 | /* |
c15d8cf2 CH |
1573 | * Phase two of compressed writeback. This is the ordered portion of the code, |
1574 | * which only gets called in the order the work was queued. We walk all the | |
1575 | * async extents created by compress_file_range and send them down to the disk. | |
078b8b90 DS |
1576 | * |
1577 | * If called with @do_free == true then it'll try to finish the work and free | |
1578 | * the work struct eventually. | |
771ed689 | 1579 | */ |
078b8b90 | 1580 | static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_free) |
771ed689 | 1581 | { |
c5a68aec NB |
1582 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1583 | work); | |
1584 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
00d31d17 | 1585 | struct async_extent *async_extent; |
771ed689 | 1586 | unsigned long nr_pages; |
00d31d17 | 1587 | u64 alloc_hint = 0; |
771ed689 | 1588 | |
078b8b90 DS |
1589 | if (do_free) { |
1590 | struct async_chunk *async_chunk; | |
1591 | struct async_cow *async_cow; | |
1592 | ||
1593 | async_chunk = container_of(work, struct async_chunk, work); | |
1594 | btrfs_add_delayed_iput(async_chunk->inode); | |
1595 | if (async_chunk->blkcg_css) | |
1596 | css_put(async_chunk->blkcg_css); | |
1597 | ||
1598 | async_cow = async_chunk->async_cow; | |
1599 | if (atomic_dec_and_test(&async_cow->num_chunks)) | |
1600 | kvfree(async_cow); | |
1601 | return; | |
1602 | } | |
1603 | ||
b5326271 | 1604 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1605 | PAGE_SHIFT; |
771ed689 | 1606 | |
00d31d17 CH |
1607 | while (!list_empty(&async_chunk->extents)) { |
1608 | async_extent = list_entry(async_chunk->extents.next, | |
1609 | struct async_extent, list); | |
1610 | list_del(&async_extent->list); | |
1611 | submit_one_async_extent(async_chunk, async_extent, &alloc_hint); | |
1612 | } | |
ac98141d JB |
1613 | |
1614 | /* atomic_sub_return implies a barrier */ | |
1615 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < | |
1616 | 5 * SZ_1M) | |
1617 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1618 | } |
c8b97818 | 1619 | |
bb7b05fe | 1620 | static bool run_delalloc_compressed(struct btrfs_inode *inode, |
c56cbe90 CH |
1621 | struct page *locked_page, u64 start, |
1622 | u64 end, struct writeback_control *wbc) | |
771ed689 | 1623 | { |
751b6431 | 1624 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1625 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1626 | struct async_cow *ctx; |
1627 | struct async_chunk *async_chunk; | |
771ed689 | 1628 | unsigned long nr_pages; |
97db1204 NB |
1629 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1630 | int i; | |
b1c16ac9 | 1631 | unsigned nofs_flag; |
bf9486d6 | 1632 | const blk_opf_t write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1633 | |
b1c16ac9 NB |
1634 | nofs_flag = memalloc_nofs_save(); |
1635 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1636 | memalloc_nofs_restore(nofs_flag); | |
973fb26e CH |
1637 | if (!ctx) |
1638 | return false; | |
b1c16ac9 | 1639 | |
973fb26e CH |
1640 | unlock_extent(&inode->io_tree, start, end, NULL); |
1641 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); | |
97db1204 NB |
1642 | |
1643 | async_chunk = ctx->chunks; | |
1644 | atomic_set(&ctx->num_chunks, num_chunks); | |
1645 | ||
1646 | for (i = 0; i < num_chunks; i++) { | |
973fb26e | 1647 | u64 cur_end = min(end, start + SZ_512K - 1); |
771ed689 | 1648 | |
bd4691a0 NB |
1649 | /* |
1650 | * igrab is called higher up in the call chain, take only the | |
1651 | * lightweight reference for the callback lifetime | |
1652 | */ | |
751b6431 | 1653 | ihold(&inode->vfs_inode); |
9e895a8f | 1654 | async_chunk[i].async_cow = ctx; |
99a81a44 | 1655 | async_chunk[i].inode = inode; |
97db1204 NB |
1656 | async_chunk[i].start = start; |
1657 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1658 | async_chunk[i].write_flags = write_flags; |
1659 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1660 | ||
1d53c9e6 CM |
1661 | /* |
1662 | * The locked_page comes all the way from writepage and its | |
1663 | * the original page we were actually given. As we spread | |
1664 | * this large delalloc region across multiple async_chunk | |
1665 | * structs, only the first struct needs a pointer to locked_page | |
1666 | * | |
1667 | * This way we don't need racey decisions about who is supposed | |
1668 | * to unlock it. | |
1669 | */ | |
1670 | if (locked_page) { | |
ec39f769 CM |
1671 | /* |
1672 | * Depending on the compressibility, the pages might or | |
1673 | * might not go through async. We want all of them to | |
1674 | * be accounted against wbc once. Let's do it here | |
1675 | * before the paths diverge. wbc accounting is used | |
1676 | * only for foreign writeback detection and doesn't | |
1677 | * need full accuracy. Just account the whole thing | |
1678 | * against the first page. | |
1679 | */ | |
1680 | wbc_account_cgroup_owner(wbc, locked_page, | |
1681 | cur_end - start); | |
1d53c9e6 CM |
1682 | async_chunk[i].locked_page = locked_page; |
1683 | locked_page = NULL; | |
1684 | } else { | |
1685 | async_chunk[i].locked_page = NULL; | |
1686 | } | |
1687 | ||
ec39f769 CM |
1688 | if (blkcg_css != blkcg_root_css) { |
1689 | css_get(blkcg_css); | |
1690 | async_chunk[i].blkcg_css = blkcg_css; | |
3480373e | 1691 | async_chunk[i].write_flags |= REQ_BTRFS_CGROUP_PUNT; |
ec39f769 CM |
1692 | } else { |
1693 | async_chunk[i].blkcg_css = NULL; | |
1694 | } | |
1695 | ||
c15d8cf2 | 1696 | btrfs_init_work(&async_chunk[i].work, compress_file_range, |
078b8b90 | 1697 | submit_compressed_extents); |
771ed689 | 1698 | |
97db1204 | 1699 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1700 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1701 | |
97db1204 | 1702 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1703 | |
771ed689 CM |
1704 | start = cur_end + 1; |
1705 | } | |
973fb26e | 1706 | return true; |
be20aa9d CM |
1707 | } |
1708 | ||
256b0cf9 CH |
1709 | /* |
1710 | * Run the delalloc range from start to end, and write back any dirty pages | |
1711 | * covered by the range. | |
1712 | */ | |
1713 | static noinline int run_delalloc_cow(struct btrfs_inode *inode, | |
1714 | struct page *locked_page, u64 start, | |
1715 | u64 end, struct writeback_control *wbc, | |
1716 | bool pages_dirty) | |
42c01100 | 1717 | { |
898793d9 | 1718 | u64 done_offset = end; |
42c01100 NA |
1719 | int ret; |
1720 | ||
898793d9 | 1721 | while (start <= end) { |
c56cbe90 CH |
1722 | ret = cow_file_range(inode, locked_page, start, end, &done_offset, |
1723 | true, false); | |
6e144bf1 | 1724 | if (ret) |
898793d9 | 1725 | return ret; |
778b8785 | 1726 | extent_write_locked_range(&inode->vfs_inode, locked_page, start, |
256b0cf9 | 1727 | done_offset, wbc, pages_dirty); |
898793d9 NA |
1728 | start = done_offset + 1; |
1729 | } | |
42c01100 | 1730 | |
c56cbe90 | 1731 | return 1; |
42c01100 NA |
1732 | } |
1733 | ||
2ff7e61e | 1734 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
26ce9114 | 1735 | u64 bytenr, u64 num_bytes, bool nowait) |
17d217fe | 1736 | { |
fc28b25e | 1737 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); |
17d217fe | 1738 | struct btrfs_ordered_sum *sums; |
fc28b25e | 1739 | int ret; |
17d217fe YZ |
1740 | LIST_HEAD(list); |
1741 | ||
97e38239 QW |
1742 | ret = btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, |
1743 | &list, 0, nowait); | |
17d217fe YZ |
1744 | if (ret == 0 && list_empty(&list)) |
1745 | return 0; | |
1746 | ||
1747 | while (!list_empty(&list)) { | |
1748 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1749 | list_del(&sums->list); | |
1750 | kfree(sums); | |
1751 | } | |
58113753 LB |
1752 | if (ret < 0) |
1753 | return ret; | |
17d217fe YZ |
1754 | return 1; |
1755 | } | |
1756 | ||
8ba96f3d | 1757 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
53ffb30a | 1758 | const u64 start, const u64 end) |
467dc47e | 1759 | { |
8ba96f3d | 1760 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
37f00a6d | 1761 | const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); |
2166e5ed | 1762 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1763 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1764 | u64 range_start = start; |
1765 | u64 count; | |
53ffb30a | 1766 | int ret; |
467dc47e FM |
1767 | |
1768 | /* | |
1769 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1770 | * made we had not enough available data space and therefore we did not | |
1771 | * reserve data space for it, since we though we could do NOCOW for the | |
1772 | * respective file range (either there is prealloc extent or the inode | |
1773 | * has the NOCOW bit set). | |
1774 | * | |
1775 | * However when we need to fallback to COW mode (because for example the | |
1776 | * block group for the corresponding extent was turned to RO mode by a | |
1777 | * scrub or relocation) we need to do the following: | |
1778 | * | |
1779 | * 1) We increment the bytes_may_use counter of the data space info. | |
1780 | * If COW succeeds, it allocates a new data extent and after doing | |
1781 | * that it decrements the space info's bytes_may_use counter and | |
1782 | * increments its bytes_reserved counter by the same amount (we do | |
1783 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1784 | * bytes_may_use counter to compensate (when space is reserved at | |
1785 | * buffered write time, the bytes_may_use counter is incremented); | |
1786 | * | |
1787 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1788 | * that if the COW path fails for any reason, it decrements (through | |
1789 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1790 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1791 | * |
1792 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1793 | * space cache inode or an inode of the data relocation tree, we must |
1794 | * also increment bytes_may_use of the data space_info for the same | |
1795 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1796 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1797 | * group that contains that extent to RO mode and therefore force COW |
1798 | * when starting writeback. | |
467dc47e | 1799 | */ |
2166e5ed | 1800 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
8c6e53a7 | 1801 | EXTENT_NORESERVE, 0, NULL); |
6bd335b4 FM |
1802 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1803 | u64 bytes = count; | |
8ba96f3d | 1804 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1805 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1806 | ||
6bd335b4 FM |
1807 | if (is_space_ino || is_reloc_ino) |
1808 | bytes = range_bytes; | |
1809 | ||
467dc47e | 1810 | spin_lock(&sinfo->lock); |
2166e5ed | 1811 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1812 | spin_unlock(&sinfo->lock); |
1813 | ||
2166e5ed FM |
1814 | if (count > 0) |
1815 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
bd015294 | 1816 | NULL); |
467dc47e FM |
1817 | } |
1818 | ||
53ffb30a CH |
1819 | /* |
1820 | * Don't try to create inline extents, as a mix of inline extent that | |
1821 | * is written out and unlocked directly and a normal NOCOW extent | |
1822 | * doesn't work. | |
1823 | */ | |
c56cbe90 CH |
1824 | ret = cow_file_range(inode, locked_page, start, end, NULL, false, true); |
1825 | ASSERT(ret != 1); | |
53ffb30a | 1826 | return ret; |
467dc47e FM |
1827 | } |
1828 | ||
619104ba FM |
1829 | struct can_nocow_file_extent_args { |
1830 | /* Input fields. */ | |
1831 | ||
1832 | /* Start file offset of the range we want to NOCOW. */ | |
1833 | u64 start; | |
1834 | /* End file offset (inclusive) of the range we want to NOCOW. */ | |
1835 | u64 end; | |
1836 | bool writeback_path; | |
1837 | bool strict; | |
1838 | /* | |
1839 | * Free the path passed to can_nocow_file_extent() once it's not needed | |
1840 | * anymore. | |
1841 | */ | |
1842 | bool free_path; | |
1843 | ||
1844 | /* Output fields. Only set when can_nocow_file_extent() returns 1. */ | |
1845 | ||
1846 | u64 disk_bytenr; | |
1847 | u64 disk_num_bytes; | |
1848 | u64 extent_offset; | |
1849 | /* Number of bytes that can be written to in NOCOW mode. */ | |
1850 | u64 num_bytes; | |
1851 | }; | |
1852 | ||
1853 | /* | |
1854 | * Check if we can NOCOW the file extent that the path points to. | |
1855 | * This function may return with the path released, so the caller should check | |
1856 | * if path->nodes[0] is NULL or not if it needs to use the path afterwards. | |
1857 | * | |
1858 | * Returns: < 0 on error | |
1859 | * 0 if we can not NOCOW | |
1860 | * 1 if we can NOCOW | |
1861 | */ | |
1862 | static int can_nocow_file_extent(struct btrfs_path *path, | |
1863 | struct btrfs_key *key, | |
1864 | struct btrfs_inode *inode, | |
1865 | struct can_nocow_file_extent_args *args) | |
1866 | { | |
1867 | const bool is_freespace_inode = btrfs_is_free_space_inode(inode); | |
1868 | struct extent_buffer *leaf = path->nodes[0]; | |
1869 | struct btrfs_root *root = inode->root; | |
1870 | struct btrfs_file_extent_item *fi; | |
1871 | u64 extent_end; | |
1872 | u8 extent_type; | |
1873 | int can_nocow = 0; | |
1874 | int ret = 0; | |
26ce9114 | 1875 | bool nowait = path->nowait; |
619104ba FM |
1876 | |
1877 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
1878 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1879 | ||
1880 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) | |
1881 | goto out; | |
1882 | ||
1883 | /* Can't access these fields unless we know it's not an inline extent. */ | |
1884 | args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1885 | args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
1886 | args->extent_offset = btrfs_file_extent_offset(leaf, fi); | |
1887 | ||
1888 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && | |
1889 | extent_type == BTRFS_FILE_EXTENT_REG) | |
1890 | goto out; | |
1891 | ||
1892 | /* | |
1893 | * If the extent was created before the generation where the last snapshot | |
1894 | * for its subvolume was created, then this implies the extent is shared, | |
1895 | * hence we must COW. | |
1896 | */ | |
a7bb6bd4 | 1897 | if (!args->strict && |
619104ba FM |
1898 | btrfs_file_extent_generation(leaf, fi) <= |
1899 | btrfs_root_last_snapshot(&root->root_item)) | |
1900 | goto out; | |
1901 | ||
1902 | /* An explicit hole, must COW. */ | |
1903 | if (args->disk_bytenr == 0) | |
1904 | goto out; | |
1905 | ||
1906 | /* Compressed/encrypted/encoded extents must be COWed. */ | |
1907 | if (btrfs_file_extent_compression(leaf, fi) || | |
1908 | btrfs_file_extent_encryption(leaf, fi) || | |
1909 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1910 | goto out; | |
1911 | ||
1912 | extent_end = btrfs_file_extent_end(path); | |
1913 | ||
1914 | /* | |
1915 | * The following checks can be expensive, as they need to take other | |
1916 | * locks and do btree or rbtree searches, so release the path to avoid | |
1917 | * blocking other tasks for too long. | |
1918 | */ | |
1919 | btrfs_release_path(path); | |
1920 | ||
1921 | ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), | |
1922 | key->offset - args->extent_offset, | |
deccae40 | 1923 | args->disk_bytenr, args->strict, path); |
619104ba FM |
1924 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); |
1925 | if (ret != 0) | |
1926 | goto out; | |
1927 | ||
1928 | if (args->free_path) { | |
1929 | /* | |
1930 | * We don't need the path anymore, plus through the | |
1931 | * csum_exist_in_range() call below we will end up allocating | |
1932 | * another path. So free the path to avoid unnecessary extra | |
1933 | * memory usage. | |
1934 | */ | |
1935 | btrfs_free_path(path); | |
1936 | path = NULL; | |
1937 | } | |
1938 | ||
1939 | /* If there are pending snapshots for this root, we must COW. */ | |
1940 | if (args->writeback_path && !is_freespace_inode && | |
1941 | atomic_read(&root->snapshot_force_cow)) | |
1942 | goto out; | |
1943 | ||
1944 | args->disk_bytenr += args->extent_offset; | |
1945 | args->disk_bytenr += args->start - key->offset; | |
1946 | args->num_bytes = min(args->end + 1, extent_end) - args->start; | |
1947 | ||
1948 | /* | |
1949 | * Force COW if csums exist in the range. This ensures that csums for a | |
1950 | * given extent are either valid or do not exist. | |
1951 | */ | |
26ce9114 JB |
1952 | ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes, |
1953 | nowait); | |
619104ba FM |
1954 | WARN_ON_ONCE(ret > 0 && is_freespace_inode); |
1955 | if (ret != 0) | |
1956 | goto out; | |
1957 | ||
1958 | can_nocow = 1; | |
1959 | out: | |
1960 | if (args->free_path && path) | |
1961 | btrfs_free_path(path); | |
1962 | ||
1963 | return ret < 0 ? ret : can_nocow; | |
1964 | } | |
1965 | ||
d352ac68 CM |
1966 | /* |
1967 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1968 | * of the extents that exist in the file, and COWs the file as required. | |
1969 | * | |
1970 | * If no cow copies or snapshots exist, we write directly to the existing | |
1971 | * blocks on disk | |
1972 | */ | |
968322c8 | 1973 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1974 | struct page *locked_page, |
53ffb30a | 1975 | const u64 start, const u64 end) |
be20aa9d | 1976 | { |
968322c8 NB |
1977 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1978 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1979 | struct btrfs_path *path; |
3e024846 NB |
1980 | u64 cow_start = (u64)-1; |
1981 | u64 cur_offset = start; | |
8ecebf4d | 1982 | int ret; |
3e024846 | 1983 | bool check_prev = true; |
968322c8 | 1984 | u64 ino = btrfs_ino(inode); |
619104ba | 1985 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
be20aa9d | 1986 | |
76c5126e CH |
1987 | /* |
1988 | * Normally on a zoned device we're only doing COW writes, but in case | |
1989 | * of relocation on a zoned filesystem serializes I/O so that we're only | |
1990 | * writing sequentially and can end up here as well. | |
1991 | */ | |
1992 | ASSERT(!btrfs_is_zoned(fs_info) || btrfs_is_data_reloc_root(root)); | |
1993 | ||
be20aa9d | 1994 | path = btrfs_alloc_path(); |
17ca04af | 1995 | if (!path) { |
38dc8889 CH |
1996 | ret = -ENOMEM; |
1997 | goto error; | |
17ca04af | 1998 | } |
82d5902d | 1999 | |
619104ba FM |
2000 | nocow_args.end = end; |
2001 | nocow_args.writeback_path = true; | |
2002 | ||
80ff3856 | 2003 | while (1) { |
18f62b86 | 2004 | struct btrfs_block_group *nocow_bg = NULL; |
34bfaf15 | 2005 | struct btrfs_ordered_extent *ordered; |
3e024846 NB |
2006 | struct btrfs_key found_key; |
2007 | struct btrfs_file_extent_item *fi; | |
2008 | struct extent_buffer *leaf; | |
2009 | u64 extent_end; | |
3e024846 | 2010 | u64 ram_bytes; |
619104ba | 2011 | u64 nocow_end; |
3e024846 | 2012 | int extent_type; |
3daea5fd | 2013 | bool is_prealloc; |
762bf098 | 2014 | |
e4c3b2dc | 2015 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 2016 | cur_offset, 0); |
d788a349 | 2017 | if (ret < 0) |
79787eaa | 2018 | goto error; |
a6bd9cd1 NB |
2019 | |
2020 | /* | |
2021 | * If there is no extent for our range when doing the initial | |
2022 | * search, then go back to the previous slot as it will be the | |
2023 | * one containing the search offset | |
2024 | */ | |
80ff3856 YZ |
2025 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
2026 | leaf = path->nodes[0]; | |
2027 | btrfs_item_key_to_cpu(leaf, &found_key, | |
2028 | path->slots[0] - 1); | |
33345d01 | 2029 | if (found_key.objectid == ino && |
80ff3856 YZ |
2030 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
2031 | path->slots[0]--; | |
2032 | } | |
3e024846 | 2033 | check_prev = false; |
80ff3856 | 2034 | next_slot: |
a6bd9cd1 | 2035 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
2036 | leaf = path->nodes[0]; |
2037 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
2038 | ret = btrfs_next_leaf(root, path); | |
953fa5ce | 2039 | if (ret < 0) |
79787eaa | 2040 | goto error; |
80ff3856 YZ |
2041 | if (ret > 0) |
2042 | break; | |
2043 | leaf = path->nodes[0]; | |
2044 | } | |
be20aa9d | 2045 | |
80ff3856 YZ |
2046 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
2047 | ||
a6bd9cd1 | 2048 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
2049 | if (found_key.objectid > ino) |
2050 | break; | |
a6bd9cd1 NB |
2051 | /* |
2052 | * Keep searching until we find an EXTENT_ITEM or there are no | |
2053 | * more extents for this inode | |
2054 | */ | |
1d512cb7 FM |
2055 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
2056 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
2057 | path->slots[0]++; | |
2058 | goto next_slot; | |
2059 | } | |
a6bd9cd1 NB |
2060 | |
2061 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 2062 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
2063 | found_key.offset > end) |
2064 | break; | |
2065 | ||
a6bd9cd1 NB |
2066 | /* |
2067 | * If the found extent starts after requested offset, then | |
2068 | * adjust extent_end to be right before this extent begins | |
2069 | */ | |
80ff3856 YZ |
2070 | if (found_key.offset > cur_offset) { |
2071 | extent_end = found_key.offset; | |
e9061e21 | 2072 | extent_type = 0; |
18f62b86 | 2073 | goto must_cow; |
80ff3856 YZ |
2074 | } |
2075 | ||
a6bd9cd1 NB |
2076 | /* |
2077 | * Found extent which begins before our range and potentially | |
2078 | * intersect it | |
2079 | */ | |
80ff3856 YZ |
2080 | fi = btrfs_item_ptr(leaf, path->slots[0], |
2081 | struct btrfs_file_extent_item); | |
2082 | extent_type = btrfs_file_extent_type(leaf, fi); | |
619104ba FM |
2083 | /* If this is triggered then we have a memory corruption. */ |
2084 | ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES); | |
2085 | if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) { | |
2086 | ret = -EUCLEAN; | |
2087 | goto error; | |
2088 | } | |
cc95bef6 | 2089 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
619104ba | 2090 | extent_end = btrfs_file_extent_end(path); |
c65ca98f | 2091 | |
619104ba FM |
2092 | /* |
2093 | * If the extent we got ends before our current offset, skip to | |
2094 | * the next extent. | |
2095 | */ | |
2096 | if (extent_end <= cur_offset) { | |
2097 | path->slots[0]++; | |
2098 | goto next_slot; | |
2099 | } | |
c65ca98f | 2100 | |
619104ba FM |
2101 | nocow_args.start = cur_offset; |
2102 | ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args); | |
953fa5ce | 2103 | if (ret < 0) |
619104ba | 2104 | goto error; |
953fa5ce | 2105 | if (ret == 0) |
18f62b86 | 2106 | goto must_cow; |
58113753 | 2107 | |
619104ba | 2108 | ret = 0; |
18f62b86 CH |
2109 | nocow_bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); |
2110 | if (!nocow_bg) { | |
2111 | must_cow: | |
2112 | /* | |
2113 | * If we can't perform NOCOW writeback for the range, | |
2114 | * then record the beginning of the range that needs to | |
2115 | * be COWed. It will be written out before the next | |
2116 | * NOCOW range if we find one, or when exiting this | |
2117 | * loop. | |
2118 | */ | |
80ff3856 YZ |
2119 | if (cow_start == (u64)-1) |
2120 | cow_start = cur_offset; | |
2121 | cur_offset = extent_end; | |
2122 | if (cur_offset > end) | |
2123 | break; | |
c65ca98f FM |
2124 | if (!path->nodes[0]) |
2125 | continue; | |
80ff3856 YZ |
2126 | path->slots[0]++; |
2127 | goto next_slot; | |
7ea394f1 YZ |
2128 | } |
2129 | ||
a6bd9cd1 NB |
2130 | /* |
2131 | * COW range from cow_start to found_key.offset - 1. As the key | |
2132 | * will contain the beginning of the first extent that can be | |
2133 | * NOCOW, following one which needs to be COW'ed | |
2134 | */ | |
80ff3856 | 2135 | if (cow_start != (u64)-1) { |
968322c8 | 2136 | ret = fallback_to_cow(inode, locked_page, |
53ffb30a | 2137 | cow_start, found_key.offset - 1); |
80ff3856 | 2138 | cow_start = (u64)-1; |
18f62b86 CH |
2139 | if (ret) { |
2140 | btrfs_dec_nocow_writers(nocow_bg); | |
79787eaa | 2141 | goto error; |
18f62b86 | 2142 | } |
7ea394f1 | 2143 | } |
80ff3856 | 2144 | |
619104ba | 2145 | nocow_end = cur_offset + nocow_args.num_bytes - 1; |
3daea5fd CH |
2146 | is_prealloc = extent_type == BTRFS_FILE_EXTENT_PREALLOC; |
2147 | if (is_prealloc) { | |
619104ba | 2148 | u64 orig_start = found_key.offset - nocow_args.extent_offset; |
3e024846 | 2149 | struct extent_map *em; |
6f9994db | 2150 | |
619104ba | 2151 | em = create_io_em(inode, cur_offset, nocow_args.num_bytes, |
6f9994db | 2152 | orig_start, |
619104ba FM |
2153 | nocow_args.disk_bytenr, /* block_start */ |
2154 | nocow_args.num_bytes, /* block_len */ | |
2155 | nocow_args.disk_num_bytes, /* orig_block_len */ | |
6f9994db LB |
2156 | ram_bytes, BTRFS_COMPRESS_NONE, |
2157 | BTRFS_ORDERED_PREALLOC); | |
2158 | if (IS_ERR(em)) { | |
18f62b86 | 2159 | btrfs_dec_nocow_writers(nocow_bg); |
6f9994db LB |
2160 | ret = PTR_ERR(em); |
2161 | goto error; | |
d899e052 | 2162 | } |
6f9994db | 2163 | free_extent_map(em); |
3daea5fd CH |
2164 | } |
2165 | ||
34bfaf15 | 2166 | ordered = btrfs_alloc_ordered_extent(inode, cur_offset, |
3daea5fd CH |
2167 | nocow_args.num_bytes, nocow_args.num_bytes, |
2168 | nocow_args.disk_bytenr, nocow_args.num_bytes, 0, | |
2169 | is_prealloc | |
2170 | ? (1 << BTRFS_ORDERED_PREALLOC) | |
2171 | : (1 << BTRFS_ORDERED_NOCOW), | |
2172 | BTRFS_COMPRESS_NONE); | |
18f62b86 | 2173 | btrfs_dec_nocow_writers(nocow_bg); |
34bfaf15 | 2174 | if (IS_ERR(ordered)) { |
3daea5fd | 2175 | if (is_prealloc) { |
4c0c8cfc FM |
2176 | btrfs_drop_extent_map_range(inode, cur_offset, |
2177 | nocow_end, false); | |
762bf098 | 2178 | } |
34bfaf15 | 2179 | ret = PTR_ERR(ordered); |
3daea5fd | 2180 | goto error; |
d899e052 | 2181 | } |
80ff3856 | 2182 | |
37f00a6d | 2183 | if (btrfs_is_data_reloc_root(root)) |
4dbd80fb QW |
2184 | /* |
2185 | * Error handled later, as we must prevent | |
2186 | * extent_clear_unlock_delalloc() in error handler | |
2187 | * from freeing metadata of created ordered extent. | |
2188 | */ | |
34bfaf15 CH |
2189 | ret = btrfs_reloc_clone_csums(ordered); |
2190 | btrfs_put_ordered_extent(ordered); | |
efa56464 | 2191 | |
619104ba | 2192 | extent_clear_unlock_delalloc(inode, cur_offset, nocow_end, |
c2790a2e | 2193 | locked_page, EXTENT_LOCKED | |
18513091 WX |
2194 | EXTENT_DELALLOC | |
2195 | EXTENT_CLEAR_DATA_RESV, | |
f57ad937 | 2196 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
18513091 | 2197 | |
80ff3856 | 2198 | cur_offset = extent_end; |
4dbd80fb QW |
2199 | |
2200 | /* | |
2201 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
2202 | * handler, as metadata for created ordered extent will only | |
2203 | * be freed by btrfs_finish_ordered_io(). | |
2204 | */ | |
2205 | if (ret) | |
2206 | goto error; | |
80ff3856 YZ |
2207 | if (cur_offset > end) |
2208 | break; | |
be20aa9d | 2209 | } |
b3b4aa74 | 2210 | btrfs_release_path(path); |
80ff3856 | 2211 | |
506481b2 | 2212 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 2213 | cow_start = cur_offset; |
17ca04af | 2214 | |
80ff3856 | 2215 | if (cow_start != (u64)-1) { |
506481b2 | 2216 | cur_offset = end; |
53ffb30a | 2217 | ret = fallback_to_cow(inode, locked_page, cow_start, end); |
953fa5ce | 2218 | cow_start = (u64)-1; |
d788a349 | 2219 | if (ret) |
79787eaa | 2220 | goto error; |
80ff3856 YZ |
2221 | } |
2222 | ||
18f62b86 CH |
2223 | btrfs_free_path(path); |
2224 | return 0; | |
762bf098 | 2225 | |
18f62b86 | 2226 | error: |
953fa5ce CH |
2227 | /* |
2228 | * If an error happened while a COW region is outstanding, cur_offset | |
2229 | * needs to be reset to cow_start to ensure the COW region is unlocked | |
2230 | * as well. | |
2231 | */ | |
2232 | if (cow_start != (u64)-1) | |
2233 | cur_offset = cow_start; | |
18f62b86 | 2234 | if (cur_offset < end) |
968322c8 | 2235 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 2236 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
2237 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
2238 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
6869b0a8 | 2239 | PAGE_START_WRITEBACK | |
c2790a2e | 2240 | PAGE_END_WRITEBACK); |
7ea394f1 | 2241 | btrfs_free_path(path); |
79787eaa | 2242 | return ret; |
be20aa9d CM |
2243 | } |
2244 | ||
6e65ae76 | 2245 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 | 2246 | { |
6e65ae76 GR |
2247 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
2248 | if (inode->defrag_bytes && | |
99be1a66 | 2249 | test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG)) |
6e65ae76 GR |
2250 | return false; |
2251 | return true; | |
2252 | } | |
2253 | return false; | |
47059d93 WS |
2254 | } |
2255 | ||
d352ac68 | 2256 | /* |
5eaad97a NB |
2257 | * Function to process delayed allocation (create CoW) for ranges which are |
2258 | * being touched for the first time. | |
d352ac68 | 2259 | */ |
98456b9c | 2260 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
c56cbe90 | 2261 | u64 start, u64 end, struct writeback_control *wbc) |
be20aa9d | 2262 | { |
42c01100 | 2263 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
c56cbe90 | 2264 | int ret; |
a2135011 | 2265 | |
2749f7ef | 2266 | /* |
c56cbe90 CH |
2267 | * The range must cover part of the @locked_page, or a return of 1 |
2268 | * can confuse the caller. | |
2749f7ef QW |
2269 | */ |
2270 | ASSERT(!(end <= page_offset(locked_page) || | |
2271 | start >= page_offset(locked_page) + PAGE_SIZE)); | |
2272 | ||
6e65ae76 | 2273 | if (should_nocow(inode, start, end)) { |
53ffb30a | 2274 | ret = run_delalloc_nocow(inode, locked_page, start, end); |
973fb26e | 2275 | goto out; |
7ddf5a42 | 2276 | } |
973fb26e CH |
2277 | |
2278 | if (btrfs_inode_can_compress(inode) && | |
2279 | inode_need_compress(inode, start, end) && | |
c56cbe90 CH |
2280 | run_delalloc_compressed(inode, locked_page, start, end, wbc)) |
2281 | return 1; | |
973fb26e CH |
2282 | |
2283 | if (zoned) | |
256b0cf9 CH |
2284 | ret = run_delalloc_cow(inode, locked_page, start, end, wbc, |
2285 | true); | |
973fb26e | 2286 | else |
c56cbe90 CH |
2287 | ret = cow_file_range(inode, locked_page, start, end, NULL, |
2288 | false, false); | |
973fb26e CH |
2289 | |
2290 | out: | |
c56cbe90 | 2291 | if (ret < 0) |
98456b9c | 2292 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 2293 | end - start + 1); |
b888db2b CM |
2294 | return ret; |
2295 | } | |
2296 | ||
62798a49 | 2297 | void btrfs_split_delalloc_extent(struct btrfs_inode *inode, |
abbb55f4 | 2298 | struct extent_state *orig, u64 split) |
9ed74f2d | 2299 | { |
62798a49 | 2300 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
dcab6a3b JB |
2301 | u64 size; |
2302 | ||
0ca1f7ce | 2303 | /* not delalloc, ignore it */ |
9ed74f2d | 2304 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 2305 | return; |
9ed74f2d | 2306 | |
dcab6a3b | 2307 | size = orig->end - orig->start + 1; |
f7b12a62 | 2308 | if (size > fs_info->max_extent_size) { |
823bb20a | 2309 | u32 num_extents; |
dcab6a3b JB |
2310 | u64 new_size; |
2311 | ||
2312 | /* | |
5c848198 | 2313 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 2314 | * applies here, just in reverse. |
dcab6a3b JB |
2315 | */ |
2316 | new_size = orig->end - split + 1; | |
7d7672bc | 2317 | num_extents = count_max_extents(fs_info, new_size); |
ba117213 | 2318 | new_size = split - orig->start; |
7d7672bc NA |
2319 | num_extents += count_max_extents(fs_info, new_size); |
2320 | if (count_max_extents(fs_info, size) >= num_extents) | |
dcab6a3b JB |
2321 | return; |
2322 | } | |
2323 | ||
62798a49 DS |
2324 | spin_lock(&inode->lock); |
2325 | btrfs_mod_outstanding_extents(inode, 1); | |
2326 | spin_unlock(&inode->lock); | |
9ed74f2d JB |
2327 | } |
2328 | ||
2329 | /* | |
5c848198 NB |
2330 | * Handle merged delayed allocation extents so we can keep track of new extents |
2331 | * that are just merged onto old extents, such as when we are doing sequential | |
2332 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 2333 | */ |
2454151c | 2334 | void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new, |
5c848198 | 2335 | struct extent_state *other) |
9ed74f2d | 2336 | { |
2454151c | 2337 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
dcab6a3b | 2338 | u64 new_size, old_size; |
823bb20a | 2339 | u32 num_extents; |
dcab6a3b | 2340 | |
9ed74f2d JB |
2341 | /* not delalloc, ignore it */ |
2342 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 2343 | return; |
9ed74f2d | 2344 | |
8461a3de JB |
2345 | if (new->start > other->start) |
2346 | new_size = new->end - other->start + 1; | |
2347 | else | |
2348 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
2349 | |
2350 | /* we're not bigger than the max, unreserve the space and go */ | |
f7b12a62 | 2351 | if (new_size <= fs_info->max_extent_size) { |
2454151c DS |
2352 | spin_lock(&inode->lock); |
2353 | btrfs_mod_outstanding_extents(inode, -1); | |
2354 | spin_unlock(&inode->lock); | |
dcab6a3b JB |
2355 | return; |
2356 | } | |
2357 | ||
2358 | /* | |
ba117213 JB |
2359 | * We have to add up either side to figure out how many extents were |
2360 | * accounted for before we merged into one big extent. If the number of | |
2361 | * extents we accounted for is <= the amount we need for the new range | |
2362 | * then we can return, otherwise drop. Think of it like this | |
2363 | * | |
2364 | * [ 4k][MAX_SIZE] | |
2365 | * | |
2366 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
2367 | * need 2 outstanding extents, on one side we have 1 and the other side | |
2368 | * we have 1 so they are == and we can return. But in this case | |
2369 | * | |
2370 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
2371 | * | |
2372 | * Each range on their own accounts for 2 extents, but merged together | |
2373 | * they are only 3 extents worth of accounting, so we need to drop in | |
2374 | * this case. | |
dcab6a3b | 2375 | */ |
ba117213 | 2376 | old_size = other->end - other->start + 1; |
7d7672bc | 2377 | num_extents = count_max_extents(fs_info, old_size); |
ba117213 | 2378 | old_size = new->end - new->start + 1; |
7d7672bc NA |
2379 | num_extents += count_max_extents(fs_info, old_size); |
2380 | if (count_max_extents(fs_info, new_size) >= num_extents) | |
dcab6a3b JB |
2381 | return; |
2382 | ||
2454151c DS |
2383 | spin_lock(&inode->lock); |
2384 | btrfs_mod_outstanding_extents(inode, -1); | |
2385 | spin_unlock(&inode->lock); | |
9ed74f2d JB |
2386 | } |
2387 | ||
8a46e55a | 2388 | static void btrfs_add_delalloc_inodes(struct btrfs_inode *inode) |
eb73c1b7 | 2389 | { |
8a46e55a FM |
2390 | struct btrfs_root *root = inode->root; |
2391 | struct btrfs_fs_info *fs_info = root->fs_info; | |
0b246afa | 2392 | |
eb73c1b7 | 2393 | spin_lock(&root->delalloc_lock); |
82ca5a04 DS |
2394 | if (list_empty(&inode->delalloc_inodes)) { |
2395 | list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes); | |
2396 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, &inode->runtime_flags); | |
eb73c1b7 MX |
2397 | root->nr_delalloc_inodes++; |
2398 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 2399 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2400 | BUG_ON(!list_empty(&root->delalloc_root)); |
2401 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
2402 | &fs_info->delalloc_roots); |
2403 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
2404 | } |
2405 | } | |
2406 | spin_unlock(&root->delalloc_lock); | |
2407 | } | |
2408 | ||
2b877331 NB |
2409 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
2410 | struct btrfs_inode *inode) | |
eb73c1b7 | 2411 | { |
3ffbd68c | 2412 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 2413 | |
9e3e97f4 NB |
2414 | if (!list_empty(&inode->delalloc_inodes)) { |
2415 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 2416 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2417 | &inode->runtime_flags); |
eb73c1b7 MX |
2418 | root->nr_delalloc_inodes--; |
2419 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 2420 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 2421 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2422 | BUG_ON(list_empty(&root->delalloc_root)); |
2423 | list_del_init(&root->delalloc_root); | |
0b246afa | 2424 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2425 | } |
2426 | } | |
2b877331 NB |
2427 | } |
2428 | ||
2429 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2430 | struct btrfs_inode *inode) | |
2431 | { | |
2432 | spin_lock(&root->delalloc_lock); | |
2433 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
2434 | spin_unlock(&root->delalloc_lock); |
2435 | } | |
2436 | ||
d352ac68 | 2437 | /* |
e06a1fc9 NB |
2438 | * Properly track delayed allocation bytes in the inode and to maintain the |
2439 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 2440 | */ |
4c5d166f | 2441 | void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state, |
6d92b304 | 2442 | u32 bits) |
291d673e | 2443 | { |
4c5d166f | 2444 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
0b246afa | 2445 | |
6d92b304 | 2446 | if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC)) |
47059d93 | 2447 | WARN_ON(1); |
75eff68e CM |
2448 | /* |
2449 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2450 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2451 | * bit, which is only set or cleared with irqs on |
2452 | */ | |
6d92b304 | 2453 | if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
0ca1f7ce | 2454 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2455 | u32 num_extents = count_max_extents(fs_info, len); |
4c5d166f | 2456 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 2457 | |
4c5d166f DS |
2458 | spin_lock(&inode->lock); |
2459 | btrfs_mod_outstanding_extents(inode, num_extents); | |
2460 | spin_unlock(&inode->lock); | |
287a0ab9 | 2461 | |
6a3891c5 | 2462 | /* For sanity tests */ |
0b246afa | 2463 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2464 | return; |
2465 | ||
104b4e51 NB |
2466 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2467 | fs_info->delalloc_batch); | |
4c5d166f DS |
2468 | spin_lock(&inode->lock); |
2469 | inode->delalloc_bytes += len; | |
6d92b304 | 2470 | if (bits & EXTENT_DEFRAG) |
4c5d166f | 2471 | inode->defrag_bytes += len; |
df0af1a5 | 2472 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
4c5d166f | 2473 | &inode->runtime_flags)) |
8a46e55a | 2474 | btrfs_add_delalloc_inodes(inode); |
4c5d166f | 2475 | spin_unlock(&inode->lock); |
291d673e | 2476 | } |
a7e3b975 FM |
2477 | |
2478 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2479 | (bits & EXTENT_DELALLOC_NEW)) { |
4c5d166f DS |
2480 | spin_lock(&inode->lock); |
2481 | inode->new_delalloc_bytes += state->end + 1 - state->start; | |
2482 | spin_unlock(&inode->lock); | |
a7e3b975 | 2483 | } |
291d673e CM |
2484 | } |
2485 | ||
d352ac68 | 2486 | /* |
a36bb5f9 NB |
2487 | * Once a range is no longer delalloc this function ensures that proper |
2488 | * accounting happens. | |
d352ac68 | 2489 | */ |
bd54766e | 2490 | void btrfs_clear_delalloc_extent(struct btrfs_inode *inode, |
6d92b304 | 2491 | struct extent_state *state, u32 bits) |
291d673e | 2492 | { |
bd54766e | 2493 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
47059d93 | 2494 | u64 len = state->end + 1 - state->start; |
7d7672bc | 2495 | u32 num_extents = count_max_extents(fs_info, len); |
47059d93 | 2496 | |
6d92b304 | 2497 | if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) { |
4a4b964f | 2498 | spin_lock(&inode->lock); |
6fc0ef68 | 2499 | inode->defrag_bytes -= len; |
4a4b964f FM |
2500 | spin_unlock(&inode->lock); |
2501 | } | |
47059d93 | 2502 | |
75eff68e CM |
2503 | /* |
2504 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2505 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2506 | * bit, which is only set or cleared with irqs on |
2507 | */ | |
6d92b304 | 2508 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2509 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2510 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2511 | |
8b62f87b JB |
2512 | spin_lock(&inode->lock); |
2513 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2514 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2515 | |
b6d08f06 JB |
2516 | /* |
2517 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2518 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2519 | * error. |
2520 | */ | |
6d92b304 | 2521 | if (bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2522 | root != fs_info->tree_root) |
43b18595 | 2523 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2524 | |
6a3891c5 | 2525 | /* For sanity tests. */ |
0b246afa | 2526 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2527 | return; |
2528 | ||
37f00a6d | 2529 | if (!btrfs_is_data_reloc_root(root) && |
a315e68f | 2530 | do_list && !(state->state & EXTENT_NORESERVE) && |
6d92b304 | 2531 | (bits & EXTENT_CLEAR_DATA_RESV)) |
9db5d510 | 2532 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2533 | |
104b4e51 NB |
2534 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2535 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2536 | spin_lock(&inode->lock); |
2537 | inode->delalloc_bytes -= len; | |
2538 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2539 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2540 | &inode->runtime_flags)) |
eb73c1b7 | 2541 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2542 | spin_unlock(&inode->lock); |
291d673e | 2543 | } |
a7e3b975 FM |
2544 | |
2545 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
6d92b304 | 2546 | (bits & EXTENT_DELALLOC_NEW)) { |
a7e3b975 FM |
2547 | spin_lock(&inode->lock); |
2548 | ASSERT(inode->new_delalloc_bytes >= len); | |
2549 | inode->new_delalloc_bytes -= len; | |
6d92b304 | 2550 | if (bits & EXTENT_ADD_INODE_BYTES) |
2766ff61 | 2551 | inode_add_bytes(&inode->vfs_inode, len); |
a7e3b975 FM |
2552 | spin_unlock(&inode->lock); |
2553 | } | |
291d673e CM |
2554 | } |
2555 | ||
71df088c CH |
2556 | static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio, |
2557 | struct btrfs_ordered_extent *ordered) | |
d22002fd | 2558 | { |
69ccf3f4 CH |
2559 | u64 start = (u64)bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT; |
2560 | u64 len = bbio->bio.bi_iter.bi_size; | |
b0307e28 | 2561 | struct btrfs_ordered_extent *new; |
ebdb44a0 | 2562 | int ret; |
d22002fd | 2563 | |
11d33ab6 | 2564 | /* Must always be called for the beginning of an ordered extent. */ |
7edd339c CH |
2565 | if (WARN_ON_ONCE(start != ordered->disk_bytenr)) |
2566 | return -EINVAL; | |
d22002fd | 2567 | |
11d33ab6 | 2568 | /* No need to split if the ordered extent covers the entire bio. */ |
ec63b84d CH |
2569 | if (ordered->disk_num_bytes == len) { |
2570 | refcount_inc(&ordered->refs); | |
2571 | bbio->ordered = ordered; | |
7edd339c | 2572 | return 0; |
ec63b84d | 2573 | } |
d22002fd | 2574 | |
f0f5329a BB |
2575 | /* |
2576 | * Don't split the extent_map for NOCOW extents, as we're writing into | |
2577 | * a pre-existing one. | |
2578 | */ | |
ebdb44a0 CH |
2579 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
2580 | ret = split_extent_map(bbio->inode, bbio->file_offset, | |
f000bc6f CH |
2581 | ordered->num_bytes, len, |
2582 | ordered->disk_bytenr); | |
ebdb44a0 CH |
2583 | if (ret) |
2584 | return ret; | |
2585 | } | |
f0f5329a | 2586 | |
b0307e28 CH |
2587 | new = btrfs_split_ordered_extent(ordered, len); |
2588 | if (IS_ERR(new)) | |
2589 | return PTR_ERR(new); | |
ec63b84d | 2590 | bbio->ordered = new; |
b0307e28 | 2591 | return 0; |
d22002fd NA |
2592 | } |
2593 | ||
d352ac68 CM |
2594 | /* |
2595 | * given a list of ordered sums record them in the inode. This happens | |
2596 | * at IO completion time based on sums calculated at bio submission time. | |
2597 | */ | |
510f85ed NB |
2598 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
2599 | struct list_head *list) | |
e6dcd2dc | 2600 | { |
e6dcd2dc | 2601 | struct btrfs_ordered_sum *sum; |
fc28b25e | 2602 | struct btrfs_root *csum_root = NULL; |
ac01f26a | 2603 | int ret; |
e6dcd2dc | 2604 | |
c6e30871 | 2605 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2606 | trans->adding_csums = true; |
fc28b25e JB |
2607 | if (!csum_root) |
2608 | csum_root = btrfs_csum_root(trans->fs_info, | |
5cfe76f8 | 2609 | sum->logical); |
fc28b25e | 2610 | ret = btrfs_csum_file_blocks(trans, csum_root, sum); |
7c2871a2 | 2611 | trans->adding_csums = false; |
ac01f26a NB |
2612 | if (ret) |
2613 | return ret; | |
e6dcd2dc CM |
2614 | } |
2615 | return 0; | |
2616 | } | |
2617 | ||
c3347309 FM |
2618 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
2619 | const u64 start, | |
2620 | const u64 len, | |
2621 | struct extent_state **cached_state) | |
2622 | { | |
2623 | u64 search_start = start; | |
2624 | const u64 end = start + len - 1; | |
2625 | ||
2626 | while (search_start < end) { | |
2627 | const u64 search_len = end - search_start + 1; | |
2628 | struct extent_map *em; | |
2629 | u64 em_len; | |
2630 | int ret = 0; | |
2631 | ||
8bab0a30 | 2632 | em = btrfs_get_extent(inode, NULL, search_start, search_len); |
c3347309 FM |
2633 | if (IS_ERR(em)) |
2634 | return PTR_ERR(em); | |
2635 | ||
2636 | if (em->block_start != EXTENT_MAP_HOLE) | |
2637 | goto next; | |
2638 | ||
2639 | em_len = em->len; | |
2640 | if (em->start < search_start) | |
2641 | em_len -= search_start - em->start; | |
2642 | if (em_len > search_len) | |
2643 | em_len = search_len; | |
2644 | ||
2645 | ret = set_extent_bit(&inode->io_tree, search_start, | |
2646 | search_start + em_len - 1, | |
1d126800 | 2647 | EXTENT_DELALLOC_NEW, cached_state); |
c3347309 FM |
2648 | next: |
2649 | search_start = extent_map_end(em); | |
2650 | free_extent_map(em); | |
2651 | if (ret) | |
2652 | return ret; | |
2653 | } | |
2654 | return 0; | |
2655 | } | |
2656 | ||
c2566f22 | 2657 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2658 | unsigned int extra_bits, |
330a5827 | 2659 | struct extent_state **cached_state) |
ea8c2819 | 2660 | { |
fdb1e121 | 2661 | WARN_ON(PAGE_ALIGNED(end)); |
c3347309 FM |
2662 | |
2663 | if (start >= i_size_read(&inode->vfs_inode) && | |
2664 | !(inode->flags & BTRFS_INODE_PREALLOC)) { | |
2665 | /* | |
2666 | * There can't be any extents following eof in this case so just | |
2667 | * set the delalloc new bit for the range directly. | |
2668 | */ | |
2669 | extra_bits |= EXTENT_DELALLOC_NEW; | |
2670 | } else { | |
2671 | int ret; | |
2672 | ||
2673 | ret = btrfs_find_new_delalloc_bytes(inode, start, | |
2674 | end + 1 - start, | |
2675 | cached_state); | |
2676 | if (ret) | |
2677 | return ret; | |
2678 | } | |
2679 | ||
66240ab1 | 2680 | return set_extent_bit(&inode->io_tree, start, end, |
1d126800 | 2681 | EXTENT_DELALLOC | extra_bits, cached_state); |
ea8c2819 CM |
2682 | } |
2683 | ||
d352ac68 | 2684 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2685 | struct btrfs_writepage_fixup { |
2686 | struct page *page; | |
36eeaef5 | 2687 | struct btrfs_inode *inode; |
247e743c CM |
2688 | struct btrfs_work work; |
2689 | }; | |
2690 | ||
b2950863 | 2691 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c | 2692 | { |
9783e4de CH |
2693 | struct btrfs_writepage_fixup *fixup = |
2694 | container_of(work, struct btrfs_writepage_fixup, work); | |
247e743c | 2695 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 2696 | struct extent_state *cached_state = NULL; |
364ecf36 | 2697 | struct extent_changeset *data_reserved = NULL; |
9783e4de CH |
2698 | struct page *page = fixup->page; |
2699 | struct btrfs_inode *inode = fixup->inode; | |
2700 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
2701 | u64 page_start = page_offset(page); | |
2702 | u64 page_end = page_offset(page) + PAGE_SIZE - 1; | |
25f3c502 | 2703 | int ret = 0; |
f4b1363c | 2704 | bool free_delalloc_space = true; |
247e743c | 2705 | |
f4b1363c JB |
2706 | /* |
2707 | * This is similar to page_mkwrite, we need to reserve the space before | |
2708 | * we take the page lock. | |
2709 | */ | |
65d87f79 NB |
2710 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2711 | PAGE_SIZE); | |
4a096752 | 2712 | again: |
247e743c | 2713 | lock_page(page); |
25f3c502 CM |
2714 | |
2715 | /* | |
2716 | * Before we queued this fixup, we took a reference on the page. | |
2717 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2718 | * address space. | |
2719 | */ | |
f4b1363c JB |
2720 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2721 | /* | |
2722 | * Unfortunately this is a little tricky, either | |
2723 | * | |
2724 | * 1) We got here and our page had already been dealt with and | |
2725 | * we reserved our space, thus ret == 0, so we need to just | |
2726 | * drop our space reservation and bail. This can happen the | |
2727 | * first time we come into the fixup worker, or could happen | |
2728 | * while waiting for the ordered extent. | |
2729 | * 2) Our page was already dealt with, but we happened to get an | |
2730 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2731 | * this case we obviously don't have anything to release, but | |
2732 | * because the page was already dealt with we don't want to | |
2733 | * mark the page with an error, so make sure we're resetting | |
2734 | * ret to 0. This is why we have this check _before_ the ret | |
2735 | * check, because we do not want to have a surprise ENOSPC | |
2736 | * when the page was already properly dealt with. | |
2737 | */ | |
2738 | if (!ret) { | |
65d87f79 NB |
2739 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2740 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2741 | page_start, PAGE_SIZE, |
2742 | true); | |
2743 | } | |
2744 | ret = 0; | |
247e743c | 2745 | goto out_page; |
f4b1363c | 2746 | } |
247e743c | 2747 | |
25f3c502 | 2748 | /* |
f4b1363c JB |
2749 | * We can't mess with the page state unless it is locked, so now that |
2750 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2751 | */ |
f4b1363c JB |
2752 | if (ret) |
2753 | goto out_page; | |
247e743c | 2754 | |
570eb97b | 2755 | lock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2756 | |
2757 | /* already ordered? We're done */ | |
f57ad937 | 2758 | if (PageOrdered(page)) |
f4b1363c | 2759 | goto out_reserved; |
4a096752 | 2760 | |
65d87f79 | 2761 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2762 | if (ordered) { |
570eb97b JB |
2763 | unlock_extent(&inode->io_tree, page_start, page_end, |
2764 | &cached_state); | |
4a096752 | 2765 | unlock_page(page); |
36d45567 | 2766 | btrfs_start_ordered_extent(ordered); |
87826df0 | 2767 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2768 | goto again; |
2769 | } | |
247e743c | 2770 | |
65d87f79 | 2771 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2772 | &cached_state); |
25f3c502 | 2773 | if (ret) |
53687007 | 2774 | goto out_reserved; |
f3038ee3 | 2775 | |
25f3c502 CM |
2776 | /* |
2777 | * Everything went as planned, we're now the owner of a dirty page with | |
2778 | * delayed allocation bits set and space reserved for our COW | |
2779 | * destination. | |
2780 | * | |
2781 | * The page was dirty when we started, nothing should have cleaned it. | |
2782 | */ | |
2783 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2784 | free_delalloc_space = false; |
53687007 | 2785 | out_reserved: |
65d87f79 | 2786 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2787 | if (free_delalloc_space) |
65d87f79 NB |
2788 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2789 | PAGE_SIZE, true); | |
570eb97b | 2790 | unlock_extent(&inode->io_tree, page_start, page_end, &cached_state); |
247e743c | 2791 | out_page: |
25f3c502 CM |
2792 | if (ret) { |
2793 | /* | |
2794 | * We hit ENOSPC or other errors. Update the mapping and page | |
2795 | * to reflect the errors and clean the page. | |
2796 | */ | |
2797 | mapping_set_error(page->mapping, ret); | |
9783e4de CH |
2798 | btrfs_mark_ordered_io_finished(inode, page, page_start, |
2799 | PAGE_SIZE, !ret); | |
25f3c502 | 2800 | clear_page_dirty_for_io(page); |
25f3c502 | 2801 | } |
55151ea9 | 2802 | btrfs_folio_clear_checked(fs_info, page_folio(page), page_start, PAGE_SIZE); |
247e743c | 2803 | unlock_page(page); |
09cbfeaf | 2804 | put_page(page); |
b897abec | 2805 | kfree(fixup); |
364ecf36 | 2806 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2807 | /* |
2808 | * As a precaution, do a delayed iput in case it would be the last iput | |
2809 | * that could need flushing space. Recursing back to fixup worker would | |
2810 | * deadlock. | |
2811 | */ | |
e55cf7ca | 2812 | btrfs_add_delayed_iput(inode); |
247e743c CM |
2813 | } |
2814 | ||
2815 | /* | |
2816 | * There are a few paths in the higher layers of the kernel that directly | |
2817 | * set the page dirty bit without asking the filesystem if it is a | |
2818 | * good idea. This causes problems because we want to make sure COW | |
2819 | * properly happens and the data=ordered rules are followed. | |
2820 | * | |
c8b97818 | 2821 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2822 | * hasn't been properly setup for IO. We kick off an async process |
2823 | * to fix it up. The async helper will wait for ordered extents, set | |
2824 | * the delalloc bit and make it safe to write the page. | |
2825 | */ | |
a129ffb8 | 2826 | int btrfs_writepage_cow_fixup(struct page *page) |
247e743c CM |
2827 | { |
2828 | struct inode *inode = page->mapping->host; | |
41044b41 | 2829 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
247e743c | 2830 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2831 | |
f57ad937 QW |
2832 | /* This page has ordered extent covering it already */ |
2833 | if (PageOrdered(page)) | |
247e743c CM |
2834 | return 0; |
2835 | ||
25f3c502 CM |
2836 | /* |
2837 | * PageChecked is set below when we create a fixup worker for this page, | |
2838 | * don't try to create another one if we're already PageChecked() | |
2839 | * | |
2840 | * The extent_io writepage code will redirty the page if we send back | |
2841 | * EAGAIN. | |
2842 | */ | |
247e743c CM |
2843 | if (PageChecked(page)) |
2844 | return -EAGAIN; | |
2845 | ||
2846 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2847 | if (!fixup) | |
2848 | return -EAGAIN; | |
f421950f | 2849 | |
f4b1363c JB |
2850 | /* |
2851 | * We are already holding a reference to this inode from | |
2852 | * write_cache_pages. We need to hold it because the space reservation | |
2853 | * takes place outside of the page lock, and we can't trust | |
2854 | * page->mapping outside of the page lock. | |
2855 | */ | |
2856 | ihold(inode); | |
55151ea9 | 2857 | btrfs_folio_set_checked(fs_info, page_folio(page), page_offset(page), PAGE_SIZE); |
09cbfeaf | 2858 | get_page(page); |
078b8b90 | 2859 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL); |
247e743c | 2860 | fixup->page = page; |
36eeaef5 | 2861 | fixup->inode = BTRFS_I(inode); |
0b246afa | 2862 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2863 | |
2864 | return -EAGAIN; | |
247e743c CM |
2865 | } |
2866 | ||
d899e052 | 2867 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 2868 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a | 2869 | struct btrfs_file_extent_item *stack_fi, |
2766ff61 | 2870 | const bool update_inode_bytes, |
9729f10a | 2871 | u64 qgroup_reserved) |
d899e052 | 2872 | { |
c553f94d | 2873 | struct btrfs_root *root = inode->root; |
2766ff61 | 2874 | const u64 sectorsize = root->fs_info->sectorsize; |
d899e052 YZ |
2875 | struct btrfs_path *path; |
2876 | struct extent_buffer *leaf; | |
2877 | struct btrfs_key ins; | |
203f44c5 QW |
2878 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2879 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
cb36a9bb | 2880 | u64 offset = btrfs_stack_file_extent_offset(stack_fi); |
203f44c5 QW |
2881 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
2882 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
5893dfb9 | 2883 | struct btrfs_drop_extents_args drop_args = { 0 }; |
d899e052 YZ |
2884 | int ret; |
2885 | ||
2886 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2887 | if (!path) |
2888 | return -ENOMEM; | |
d899e052 | 2889 | |
a1ed835e CM |
2890 | /* |
2891 | * we may be replacing one extent in the tree with another. | |
2892 | * The new extent is pinned in the extent map, and we don't want | |
2893 | * to drop it from the cache until it is completely in the btree. | |
2894 | * | |
2895 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2896 | * the caller is expected to unpin it and allow it to be merged | |
2897 | * with the others. | |
2898 | */ | |
5893dfb9 FM |
2899 | drop_args.path = path; |
2900 | drop_args.start = file_pos; | |
2901 | drop_args.end = file_pos + num_bytes; | |
2902 | drop_args.replace_extent = true; | |
2903 | drop_args.extent_item_size = sizeof(*stack_fi); | |
2904 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); | |
79787eaa JM |
2905 | if (ret) |
2906 | goto out; | |
d899e052 | 2907 | |
5893dfb9 | 2908 | if (!drop_args.extent_inserted) { |
c553f94d | 2909 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
2910 | ins.offset = file_pos; |
2911 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2912 | ||
1acae57b | 2913 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
203f44c5 | 2914 | sizeof(*stack_fi)); |
1acae57b FDBM |
2915 | if (ret) |
2916 | goto out; | |
2917 | } | |
d899e052 | 2918 | leaf = path->nodes[0]; |
203f44c5 QW |
2919 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2920 | write_extent_buffer(leaf, stack_fi, | |
2921 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2922 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2923 | |
50564b65 | 2924 | btrfs_mark_buffer_dirty(trans, leaf); |
ce195332 | 2925 | btrfs_release_path(path); |
d899e052 | 2926 | |
2766ff61 FM |
2927 | /* |
2928 | * If we dropped an inline extent here, we know the range where it is | |
2929 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the | |
1a9fd417 | 2930 | * number of bytes only for that range containing the inline extent. |
2766ff61 FM |
2931 | * The remaining of the range will be processed when clearning the |
2932 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. | |
2933 | */ | |
2934 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { | |
2935 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); | |
2936 | ||
2937 | inline_size = drop_args.bytes_found - inline_size; | |
2938 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); | |
2939 | drop_args.bytes_found -= inline_size; | |
2940 | num_bytes -= sectorsize; | |
2941 | } | |
2942 | ||
2943 | if (update_inode_bytes) | |
2944 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); | |
d899e052 YZ |
2945 | |
2946 | ins.objectid = disk_bytenr; | |
2947 | ins.offset = disk_num_bytes; | |
2948 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2949 | |
c553f94d | 2950 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
2951 | if (ret) |
2952 | goto out; | |
2953 | ||
c553f94d | 2954 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
cb36a9bb OS |
2955 | file_pos - offset, |
2956 | qgroup_reserved, &ins); | |
79787eaa | 2957 | out: |
d899e052 | 2958 | btrfs_free_path(path); |
b9473439 | 2959 | |
79787eaa | 2960 | return ret; |
d899e052 YZ |
2961 | } |
2962 | ||
2ff7e61e | 2963 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2964 | u64 start, u64 len) |
2965 | { | |
32da5386 | 2966 | struct btrfs_block_group *cache; |
e570fd27 | 2967 | |
0b246afa | 2968 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2969 | ASSERT(cache); |
2970 | ||
2971 | spin_lock(&cache->lock); | |
2972 | cache->delalloc_bytes -= len; | |
2973 | spin_unlock(&cache->lock); | |
2974 | ||
2975 | btrfs_put_block_group(cache); | |
2976 | } | |
2977 | ||
203f44c5 | 2978 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
203f44c5 QW |
2979 | struct btrfs_ordered_extent *oe) |
2980 | { | |
2981 | struct btrfs_file_extent_item stack_fi; | |
2766ff61 | 2982 | bool update_inode_bytes; |
cb36a9bb OS |
2983 | u64 num_bytes = oe->num_bytes; |
2984 | u64 ram_bytes = oe->ram_bytes; | |
203f44c5 QW |
2985 | |
2986 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2987 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2988 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2989 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2990 | oe->disk_num_bytes); | |
cb36a9bb | 2991 | btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset); |
c1867eb3 DS |
2992 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) { |
2993 | num_bytes = oe->truncated_len; | |
2994 | ram_bytes = num_bytes; | |
2995 | } | |
cb36a9bb OS |
2996 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes); |
2997 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes); | |
203f44c5 QW |
2998 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
2999 | /* Encryption and other encoding is reserved and all 0 */ | |
3000 | ||
2766ff61 FM |
3001 | /* |
3002 | * For delalloc, when completing an ordered extent we update the inode's | |
3003 | * bytes when clearing the range in the inode's io tree, so pass false | |
3004 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), | |
3005 | * except if the ordered extent was truncated. | |
3006 | */ | |
3007 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || | |
7c0c7269 | 3008 | test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) || |
2766ff61 FM |
3009 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
3010 | ||
3c38c877 NB |
3011 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
3012 | oe->file_offset, &stack_fi, | |
2766ff61 | 3013 | update_inode_bytes, oe->qgroup_rsv); |
203f44c5 QW |
3014 | } |
3015 | ||
3016 | /* | |
3017 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
3018 | * an ordered extent if the range of bytes in the file it covers are |
3019 | * fully written. | |
3020 | */ | |
71df088c | 3021 | int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 3022 | { |
72e7e6ed NB |
3023 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
3024 | struct btrfs_root *root = inode->root; | |
3025 | struct btrfs_fs_info *fs_info = root->fs_info; | |
0ca1f7ce | 3026 | struct btrfs_trans_handle *trans = NULL; |
72e7e6ed | 3027 | struct extent_io_tree *io_tree = &inode->io_tree; |
2ac55d41 | 3028 | struct extent_state *cached_state = NULL; |
bffe633e | 3029 | u64 start, end; |
261507a0 | 3030 | int compress_type = 0; |
77cef2ec | 3031 | int ret = 0; |
bffe633e | 3032 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 3033 | bool freespace_inode; |
77cef2ec | 3034 | bool truncated = false; |
49940bdd | 3035 | bool clear_reserved_extent = true; |
2766ff61 | 3036 | unsigned int clear_bits = EXTENT_DEFRAG; |
a7e3b975 | 3037 | |
bffe633e OS |
3038 | start = ordered_extent->file_offset; |
3039 | end = start + ordered_extent->num_bytes - 1; | |
3040 | ||
a7e3b975 FM |
3041 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
3042 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
7c0c7269 OS |
3043 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) && |
3044 | !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags)) | |
2766ff61 | 3045 | clear_bits |= EXTENT_DELALLOC_NEW; |
e6dcd2dc | 3046 | |
72e7e6ed | 3047 | freespace_inode = btrfs_is_free_space_inode(inode); |
5f4403e1 IA |
3048 | if (!freespace_inode) |
3049 | btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent); | |
0cb59c99 | 3050 | |
5fd02043 JB |
3051 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
3052 | ret = -EIO; | |
3053 | goto out; | |
3054 | } | |
3055 | ||
71df088c | 3056 | if (btrfs_is_zoned(fs_info)) |
be1a1d7a NA |
3057 | btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, |
3058 | ordered_extent->disk_num_bytes); | |
d8e3fb10 | 3059 | |
77cef2ec JB |
3060 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
3061 | truncated = true; | |
3062 | logical_len = ordered_extent->truncated_len; | |
3063 | /* Truncated the entire extent, don't bother adding */ | |
3064 | if (!logical_len) | |
3065 | goto out; | |
3066 | } | |
3067 | ||
c2167754 | 3068 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 3069 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 3070 | |
72e7e6ed | 3071 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
3072 | if (freespace_inode) |
3073 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
3074 | else |
3075 | trans = btrfs_join_transaction(root); | |
3076 | if (IS_ERR(trans)) { | |
3077 | ret = PTR_ERR(trans); | |
3078 | trans = NULL; | |
3079 | goto out; | |
c2167754 | 3080 | } |
72e7e6ed | 3081 | trans->block_rsv = &inode->block_rsv; |
0a5d0dc5 | 3082 | ret = btrfs_update_inode_fallback(trans, inode); |
6c760c07 | 3083 | if (ret) /* -ENOMEM or corruption */ |
66642832 | 3084 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
3085 | goto out; |
3086 | } | |
e6dcd2dc | 3087 | |
2766ff61 | 3088 | clear_bits |= EXTENT_LOCKED; |
570eb97b | 3089 | lock_extent(io_tree, start, end, &cached_state); |
e6dcd2dc | 3090 | |
8d510121 NB |
3091 | if (freespace_inode) |
3092 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 3093 | else |
7a7eaa40 | 3094 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3095 | if (IS_ERR(trans)) { |
3096 | ret = PTR_ERR(trans); | |
3097 | trans = NULL; | |
a7e3b975 | 3098 | goto out; |
79787eaa | 3099 | } |
a79b7d4b | 3100 | |
72e7e6ed | 3101 | trans->block_rsv = &inode->block_rsv; |
c2167754 | 3102 | |
02c372e1 JT |
3103 | ret = btrfs_insert_raid_extent(trans, ordered_extent); |
3104 | if (ret) | |
3105 | goto out; | |
3106 | ||
c8b97818 | 3107 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3108 | compress_type = ordered_extent->compress_type; |
d899e052 | 3109 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3110 | BUG_ON(compress_type); |
72e7e6ed | 3111 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
3112 | ordered_extent->file_offset, |
3113 | ordered_extent->file_offset + | |
77cef2ec | 3114 | logical_len); |
343d8a30 NA |
3115 | btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr, |
3116 | ordered_extent->disk_num_bytes); | |
d899e052 | 3117 | } else { |
0b246afa | 3118 | BUG_ON(root == fs_info->tree_root); |
3c38c877 | 3119 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
49940bdd JB |
3120 | if (!ret) { |
3121 | clear_reserved_extent = false; | |
2ff7e61e | 3122 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
3123 | ordered_extent->disk_bytenr, |
3124 | ordered_extent->disk_num_bytes); | |
49940bdd | 3125 | } |
d899e052 | 3126 | } |
c03c89f8 DS |
3127 | if (ret < 0) { |
3128 | btrfs_abort_transaction(trans, ret); | |
3129 | goto out; | |
3130 | } | |
3131 | ||
3132 | ret = unpin_extent_cache(inode, ordered_extent->file_offset, | |
3133 | ordered_extent->num_bytes, trans->transid); | |
79787eaa | 3134 | if (ret < 0) { |
66642832 | 3135 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3136 | goto out; |
79787eaa | 3137 | } |
2ac55d41 | 3138 | |
510f85ed | 3139 | ret = add_pending_csums(trans, &ordered_extent->list); |
ac01f26a NB |
3140 | if (ret) { |
3141 | btrfs_abort_transaction(trans, ret); | |
3142 | goto out; | |
3143 | } | |
e6dcd2dc | 3144 | |
2766ff61 FM |
3145 | /* |
3146 | * If this is a new delalloc range, clear its new delalloc flag to | |
3147 | * update the inode's number of bytes. This needs to be done first | |
3148 | * before updating the inode item. | |
3149 | */ | |
3150 | if ((clear_bits & EXTENT_DELALLOC_NEW) && | |
3151 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) | |
72e7e6ed | 3152 | clear_extent_bit(&inode->io_tree, start, end, |
2766ff61 | 3153 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
bd015294 | 3154 | &cached_state); |
2766ff61 | 3155 | |
72e7e6ed | 3156 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
0a5d0dc5 | 3157 | ret = btrfs_update_inode_fallback(trans, inode); |
6c760c07 | 3158 | if (ret) { /* -ENOMEM or corruption */ |
66642832 | 3159 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3160 | goto out; |
1ef30be1 JB |
3161 | } |
3162 | ret = 0; | |
c2167754 | 3163 | out: |
bd015294 | 3164 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
313facc5 | 3165 | &cached_state); |
a7e3b975 | 3166 | |
a698d075 | 3167 | if (trans) |
3a45bb20 | 3168 | btrfs_end_transaction(trans); |
0cb59c99 | 3169 | |
77cef2ec | 3170 | if (ret || truncated) { |
bffe633e | 3171 | u64 unwritten_start = start; |
77cef2ec | 3172 | |
d61bec08 JB |
3173 | /* |
3174 | * If we failed to finish this ordered extent for any reason we | |
3175 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered | |
3176 | * extent, and mark the inode with the error if it wasn't | |
3177 | * already set. Any error during writeback would have already | |
3178 | * set the mapping error, so we need to set it if we're the ones | |
3179 | * marking this ordered extent as failed. | |
3180 | */ | |
3181 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, | |
3182 | &ordered_extent->flags)) | |
3183 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); | |
3184 | ||
77cef2ec | 3185 | if (truncated) |
bffe633e OS |
3186 | unwritten_start += logical_len; |
3187 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec | 3188 | |
5571e41e JB |
3189 | /* |
3190 | * Drop extent maps for the part of the extent we didn't write. | |
3191 | * | |
3192 | * We have an exception here for the free_space_inode, this is | |
3193 | * because when we do btrfs_get_extent() on the free space inode | |
3194 | * we will search the commit root. If this is a new block group | |
3195 | * we won't find anything, and we will trip over the assert in | |
3196 | * writepage where we do ASSERT(em->block_start != | |
3197 | * EXTENT_MAP_HOLE). | |
3198 | * | |
3199 | * Theoretically we could also skip this for any NOCOW extent as | |
3200 | * we don't mess with the extent map tree in the NOCOW case, but | |
3201 | * for now simply skip this if we are the free space inode. | |
3202 | */ | |
3203 | if (!btrfs_is_free_space_inode(inode)) | |
3204 | btrfs_drop_extent_map_range(inode, unwritten_start, | |
3205 | end, false); | |
5fd02043 | 3206 | |
0bec9ef5 JB |
3207 | /* |
3208 | * If the ordered extent had an IOERR or something else went | |
3209 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3210 | * back to the allocator. We only free the extent in the |
3211 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3212 | * |
3213 | * If we made it past insert_reserved_file_extent before we | |
3214 | * errored out then we don't need to do this as the accounting | |
3215 | * has already been done. | |
0bec9ef5 | 3216 | */ |
77cef2ec | 3217 | if ((ret || !logical_len) && |
49940bdd | 3218 | clear_reserved_extent && |
77cef2ec | 3219 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
3220 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
3221 | /* | |
3222 | * Discard the range before returning it back to the | |
3223 | * free space pool | |
3224 | */ | |
46b27f50 | 3225 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 3226 | btrfs_discard_extent(fs_info, |
bffe633e OS |
3227 | ordered_extent->disk_bytenr, |
3228 | ordered_extent->disk_num_bytes, | |
3229 | NULL); | |
2ff7e61e | 3230 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
3231 | ordered_extent->disk_bytenr, |
3232 | ordered_extent->disk_num_bytes, 1); | |
e28b0211 BB |
3233 | /* |
3234 | * Actually free the qgroup rsv which was released when | |
3235 | * the ordered extent was created. | |
3236 | */ | |
3237 | btrfs_qgroup_free_refroot(fs_info, inode->root->root_key.objectid, | |
3238 | ordered_extent->qgroup_rsv, | |
3239 | BTRFS_QGROUP_RSV_DATA); | |
4eaaec24 | 3240 | } |
0bec9ef5 JB |
3241 | } |
3242 | ||
5fd02043 | 3243 | /* |
8bad3c02 LB |
3244 | * This needs to be done to make sure anybody waiting knows we are done |
3245 | * updating everything for this ordered extent. | |
5fd02043 | 3246 | */ |
72e7e6ed | 3247 | btrfs_remove_ordered_extent(inode, ordered_extent); |
5fd02043 | 3248 | |
e6dcd2dc CM |
3249 | /* once for us */ |
3250 | btrfs_put_ordered_extent(ordered_extent); | |
3251 | /* once for the tree */ | |
3252 | btrfs_put_ordered_extent(ordered_extent); | |
3253 | ||
5fd02043 JB |
3254 | return ret; |
3255 | } | |
3256 | ||
71df088c CH |
3257 | int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered) |
3258 | { | |
41044b41 | 3259 | if (btrfs_is_zoned(inode_to_fs_info(ordered->inode)) && |
02c372e1 JT |
3260 | !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) && |
3261 | list_empty(&ordered->bioc_list)) | |
71df088c CH |
3262 | btrfs_finish_ordered_zoned(ordered); |
3263 | return btrfs_finish_one_ordered(ordered); | |
3264 | } | |
3265 | ||
ae643a74 QW |
3266 | /* |
3267 | * Verify the checksum for a single sector without any extra action that depend | |
3268 | * on the type of I/O. | |
3269 | */ | |
3270 | int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page, | |
3271 | u32 pgoff, u8 *csum, const u8 * const csum_expected) | |
3272 | { | |
3273 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
3274 | char *kaddr; | |
3275 | ||
3276 | ASSERT(pgoff + fs_info->sectorsize <= PAGE_SIZE); | |
3277 | ||
3278 | shash->tfm = fs_info->csum_shash; | |
3279 | ||
3280 | kaddr = kmap_local_page(page) + pgoff; | |
3281 | crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum); | |
3282 | kunmap_local(kaddr); | |
3283 | ||
3284 | if (memcmp(csum, csum_expected, fs_info->csum_size)) | |
3285 | return -EIO; | |
3286 | return 0; | |
211f90e6 CM |
3287 | } |
3288 | ||
265d4ac0 | 3289 | /* |
e5219044 CH |
3290 | * Verify the checksum of a single data sector. |
3291 | * | |
3292 | * @bbio: btrfs_io_bio which contains the csum | |
3293 | * @dev: device the sector is on | |
7ffd27e3 | 3294 | * @bio_offset: offset to the beginning of the bio (in bytes) |
e5219044 | 3295 | * @bv: bio_vec to check |
265d4ac0 | 3296 | * |
e5219044 CH |
3297 | * Check if the checksum on a data block is valid. When a checksum mismatch is |
3298 | * detected, report the error and fill the corrupted range with zero. | |
ae643a74 | 3299 | * |
e5219044 | 3300 | * Return %true if the sector is ok or had no checksum to start with, else %false. |
265d4ac0 | 3301 | */ |
e5219044 CH |
3302 | bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev, |
3303 | u32 bio_offset, struct bio_vec *bv) | |
dc380aea | 3304 | { |
e5219044 | 3305 | struct btrfs_inode *inode = bbio->inode; |
621af94a | 3306 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
e5219044 CH |
3307 | u64 file_offset = bbio->file_offset + bio_offset; |
3308 | u64 end = file_offset + bv->bv_len - 1; | |
d5178578 JT |
3309 | u8 *csum_expected; |
3310 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 3311 | |
3d49d0d3 | 3312 | ASSERT(bv->bv_len == fs_info->sectorsize); |
265d4ac0 | 3313 | |
e5219044 CH |
3314 | if (!bbio->csum) |
3315 | return true; | |
d5178578 | 3316 | |
e5219044 CH |
3317 | if (btrfs_is_data_reloc_root(inode->root) && |
3318 | test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM, | |
893fe243 | 3319 | NULL)) { |
e5219044 CH |
3320 | /* Skip the range without csum for data reloc inode */ |
3321 | clear_extent_bits(&inode->io_tree, file_offset, end, | |
3322 | EXTENT_NODATASUM); | |
3323 | return true; | |
3324 | } | |
3325 | ||
fa13661c JT |
3326 | csum_expected = bbio->csum + (bio_offset >> fs_info->sectorsize_bits) * |
3327 | fs_info->csum_size; | |
3d49d0d3 CH |
3328 | if (btrfs_check_sector_csum(fs_info, bv->bv_page, bv->bv_offset, csum, |
3329 | csum_expected)) | |
dc380aea | 3330 | goto zeroit; |
e5219044 | 3331 | return true; |
ae643a74 | 3332 | |
dc380aea | 3333 | zeroit: |
3d49d0d3 CH |
3334 | btrfs_print_data_csum_error(inode, file_offset, csum, csum_expected, |
3335 | bbio->mirror_num); | |
3336 | if (dev) | |
3337 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS); | |
3338 | memzero_bvec(bv); | |
3339 | return false; | |
07157aac | 3340 | } |
b888db2b | 3341 | |
c1c3fac2 | 3342 | /* |
9580503b | 3343 | * Perform a delayed iput on @inode. |
c1c3fac2 NB |
3344 | * |
3345 | * @inode: The inode we want to perform iput on | |
3346 | * | |
3347 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3348 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3349 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3350 | * transaction commit time/superblock commit/cleaner kthread. | |
3351 | */ | |
e55cf7ca | 3352 | void btrfs_add_delayed_iput(struct btrfs_inode *inode) |
24bbcf04 | 3353 | { |
e55cf7ca | 3354 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
866e98a4 | 3355 | unsigned long flags; |
24bbcf04 | 3356 | |
e55cf7ca | 3357 | if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1)) |
24bbcf04 YZ |
3358 | return; |
3359 | ||
034f784d | 3360 | atomic_inc(&fs_info->nr_delayed_iputs); |
866e98a4 FM |
3361 | /* |
3362 | * Need to be irq safe here because we can be called from either an irq | |
3363 | * context (see bio.c and btrfs_put_ordered_extent()) or a non-irq | |
3364 | * context. | |
3365 | */ | |
3366 | spin_lock_irqsave(&fs_info->delayed_iput_lock, flags); | |
e55cf7ca DS |
3367 | ASSERT(list_empty(&inode->delayed_iput)); |
3368 | list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs); | |
866e98a4 | 3369 | spin_unlock_irqrestore(&fs_info->delayed_iput_lock, flags); |
fd340d0f JB |
3370 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3371 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3372 | } |
3373 | ||
63611e73 JB |
3374 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3375 | struct btrfs_inode *inode) | |
3376 | { | |
3377 | list_del_init(&inode->delayed_iput); | |
866e98a4 | 3378 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3379 | iput(&inode->vfs_inode); |
3380 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3381 | wake_up(&fs_info->delayed_iputs_wait); | |
866e98a4 | 3382 | spin_lock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3383 | } |
3384 | ||
3385 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3386 | struct btrfs_inode *inode) | |
3387 | { | |
3388 | if (!list_empty(&inode->delayed_iput)) { | |
866e98a4 | 3389 | spin_lock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3390 | if (!list_empty(&inode->delayed_iput)) |
3391 | run_delayed_iput_locked(fs_info, inode); | |
866e98a4 | 3392 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
63611e73 JB |
3393 | } |
3394 | } | |
3395 | ||
2ff7e61e | 3396 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3397 | { |
866e98a4 FM |
3398 | /* |
3399 | * btrfs_put_ordered_extent() can run in irq context (see bio.c), which | |
3400 | * calls btrfs_add_delayed_iput() and that needs to lock | |
3401 | * fs_info->delayed_iput_lock. So we need to disable irqs here to | |
3402 | * prevent a deadlock. | |
3403 | */ | |
3404 | spin_lock_irq(&fs_info->delayed_iput_lock); | |
8089fe62 DS |
3405 | while (!list_empty(&fs_info->delayed_iputs)) { |
3406 | struct btrfs_inode *inode; | |
3407 | ||
3408 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3409 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3410 | run_delayed_iput_locked(fs_info, inode); |
866e98a4 FM |
3411 | if (need_resched()) { |
3412 | spin_unlock_irq(&fs_info->delayed_iput_lock); | |
3413 | cond_resched(); | |
3414 | spin_lock_irq(&fs_info->delayed_iput_lock); | |
3415 | } | |
24bbcf04 | 3416 | } |
866e98a4 | 3417 | spin_unlock_irq(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3418 | } |
3419 | ||
e43eec81 | 3420 | /* |
2639631d NB |
3421 | * Wait for flushing all delayed iputs |
3422 | * | |
3423 | * @fs_info: the filesystem | |
034f784d JB |
3424 | * |
3425 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3426 | * set. Once they are all done running we will return, unless we are killed in | |
3427 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3428 | * that might get blocked on the iputs. | |
2639631d NB |
3429 | * |
3430 | * Return EINTR if we were killed, 0 if nothing's pending | |
034f784d JB |
3431 | */ |
3432 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3433 | { | |
3434 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3435 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3436 | if (ret) | |
3437 | return -EINTR; | |
3438 | return 0; | |
3439 | } | |
3440 | ||
7b128766 | 3441 | /* |
f7e9e8fc OS |
3442 | * This creates an orphan entry for the given inode in case something goes wrong |
3443 | * in the middle of an unlink. | |
7b128766 | 3444 | */ |
73f2e545 | 3445 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3446 | struct btrfs_inode *inode) |
7b128766 | 3447 | { |
d68fc57b | 3448 | int ret; |
7b128766 | 3449 | |
27919067 OS |
3450 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3451 | if (ret && ret != -EEXIST) { | |
3452 | btrfs_abort_transaction(trans, ret); | |
3453 | return ret; | |
d68fc57b YZ |
3454 | } |
3455 | ||
d68fc57b | 3456 | return 0; |
7b128766 JB |
3457 | } |
3458 | ||
3459 | /* | |
f7e9e8fc OS |
3460 | * We have done the delete so we can go ahead and remove the orphan item for |
3461 | * this particular inode. | |
7b128766 | 3462 | */ |
48a3b636 | 3463 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3464 | struct btrfs_inode *inode) |
7b128766 | 3465 | { |
27919067 | 3466 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3467 | } |
3468 | ||
3469 | /* | |
3470 | * this cleans up any orphans that may be left on the list from the last use | |
3471 | * of this root. | |
3472 | */ | |
66b4ffd1 | 3473 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3474 | { |
0b246afa | 3475 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3476 | struct btrfs_path *path; |
3477 | struct extent_buffer *leaf; | |
7b128766 JB |
3478 | struct btrfs_key key, found_key; |
3479 | struct btrfs_trans_handle *trans; | |
3480 | struct inode *inode; | |
8f6d7f4f | 3481 | u64 last_objectid = 0; |
f7e9e8fc | 3482 | int ret = 0, nr_unlink = 0; |
7b128766 | 3483 | |
54230013 | 3484 | if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) |
66b4ffd1 | 3485 | return 0; |
c71bf099 YZ |
3486 | |
3487 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3488 | if (!path) { |
3489 | ret = -ENOMEM; | |
3490 | goto out; | |
3491 | } | |
e4058b54 | 3492 | path->reada = READA_BACK; |
7b128766 JB |
3493 | |
3494 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3495 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3496 | key.offset = (u64)-1; |
3497 | ||
7b128766 JB |
3498 | while (1) { |
3499 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3500 | if (ret < 0) |
3501 | goto out; | |
7b128766 JB |
3502 | |
3503 | /* | |
3504 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3505 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3506 | * find the key and see if we have stuff that matches |
3507 | */ | |
3508 | if (ret > 0) { | |
66b4ffd1 | 3509 | ret = 0; |
7b128766 JB |
3510 | if (path->slots[0] == 0) |
3511 | break; | |
3512 | path->slots[0]--; | |
3513 | } | |
3514 | ||
3515 | /* pull out the item */ | |
3516 | leaf = path->nodes[0]; | |
7b128766 JB |
3517 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3518 | ||
3519 | /* make sure the item matches what we want */ | |
3520 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3521 | break; | |
962a298f | 3522 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3523 | break; |
3524 | ||
3525 | /* release the path since we're done with it */ | |
b3b4aa74 | 3526 | btrfs_release_path(path); |
7b128766 JB |
3527 | |
3528 | /* | |
3529 | * this is where we are basically btrfs_lookup, without the | |
3530 | * crossing root thing. we store the inode number in the | |
3531 | * offset of the orphan item. | |
3532 | */ | |
8f6d7f4f JB |
3533 | |
3534 | if (found_key.offset == last_objectid) { | |
a7f8de50 FM |
3535 | /* |
3536 | * We found the same inode as before. This means we were | |
3537 | * not able to remove its items via eviction triggered | |
3538 | * by an iput(). A transaction abort may have happened, | |
3539 | * due to -ENOSPC for example, so try to grab the error | |
3540 | * that lead to a transaction abort, if any. | |
3541 | */ | |
0b246afa JM |
3542 | btrfs_err(fs_info, |
3543 | "Error removing orphan entry, stopping orphan cleanup"); | |
a7f8de50 | 3544 | ret = BTRFS_FS_ERROR(fs_info) ?: -EINVAL; |
8f6d7f4f JB |
3545 | goto out; |
3546 | } | |
3547 | ||
3548 | last_objectid = found_key.offset; | |
3549 | ||
5d4f98a2 YZ |
3550 | found_key.objectid = found_key.offset; |
3551 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3552 | found_key.offset = 0; | |
0202e83f | 3553 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
cbaee87f FM |
3554 | if (IS_ERR(inode)) { |
3555 | ret = PTR_ERR(inode); | |
3556 | inode = NULL; | |
3557 | if (ret != -ENOENT) | |
3558 | goto out; | |
3559 | } | |
7b128766 | 3560 | |
cbaee87f | 3561 | if (!inode && root == fs_info->tree_root) { |
f8e9e0b0 | 3562 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3563 | int is_dead_root = 0; |
3564 | ||
3565 | /* | |
0c0218e9 | 3566 | * This is an orphan in the tree root. Currently these |
f8e9e0b0 | 3567 | * could come from 2 sources: |
0c0218e9 | 3568 | * a) a root (snapshot/subvolume) deletion in progress |
f8e9e0b0 | 3569 | * b) a free space cache inode |
0c0218e9 FM |
3570 | * We need to distinguish those two, as the orphan item |
3571 | * for a root must not get deleted before the deletion | |
3572 | * of the snapshot/subvolume's tree completes. | |
3573 | * | |
3574 | * btrfs_find_orphan_roots() ran before us, which has | |
3575 | * found all deleted roots and loaded them into | |
fc7cbcd4 | 3576 | * fs_info->fs_roots_radix. So here we can find if an |
0c0218e9 | 3577 | * orphan item corresponds to a deleted root by looking |
fc7cbcd4 | 3578 | * up the root from that radix tree. |
f8e9e0b0 | 3579 | */ |
a619b3c7 | 3580 | |
fc7cbcd4 DS |
3581 | spin_lock(&fs_info->fs_roots_radix_lock); |
3582 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3583 | (unsigned long)found_key.objectid); | |
a619b3c7 RK |
3584 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
3585 | is_dead_root = 1; | |
fc7cbcd4 | 3586 | spin_unlock(&fs_info->fs_roots_radix_lock); |
a619b3c7 | 3587 | |
f8e9e0b0 AJ |
3588 | if (is_dead_root) { |
3589 | /* prevent this orphan from being found again */ | |
3590 | key.offset = found_key.objectid - 1; | |
3591 | continue; | |
3592 | } | |
f7e9e8fc | 3593 | |
f8e9e0b0 | 3594 | } |
f7e9e8fc | 3595 | |
7b128766 | 3596 | /* |
f7e9e8fc | 3597 | * If we have an inode with links, there are a couple of |
70524253 BB |
3598 | * possibilities: |
3599 | * | |
3600 | * 1. We were halfway through creating fsverity metadata for the | |
3601 | * file. In that case, the orphan item represents incomplete | |
3602 | * fsverity metadata which must be cleaned up with | |
3603 | * btrfs_drop_verity_items and deleting the orphan item. | |
3604 | ||
3605 | * 2. Old kernels (before v3.12) used to create an | |
f7e9e8fc OS |
3606 | * orphan item for truncate indicating that there were possibly |
3607 | * extent items past i_size that needed to be deleted. In v3.12, | |
3608 | * truncate was changed to update i_size in sync with the extent | |
3609 | * items, but the (useless) orphan item was still created. Since | |
3610 | * v4.18, we don't create the orphan item for truncate at all. | |
3611 | * | |
3612 | * So, this item could mean that we need to do a truncate, but | |
3613 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3614 | * and was not cleanly unmounted. The odds of that are quite | |
3615 | * slim, and it's a pain to do the truncate now, so just delete | |
3616 | * the orphan item. | |
3617 | * | |
3618 | * It's also possible that this orphan item was supposed to be | |
3619 | * deleted but wasn't. The inode number may have been reused, | |
3620 | * but either way, we can delete the orphan item. | |
7b128766 | 3621 | */ |
cbaee87f FM |
3622 | if (!inode || inode->i_nlink) { |
3623 | if (inode) { | |
70524253 | 3624 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); |
f7e9e8fc | 3625 | iput(inode); |
b777d279 | 3626 | inode = NULL; |
70524253 BB |
3627 | if (ret) |
3628 | goto out; | |
3629 | } | |
a8c9e576 | 3630 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3631 | if (IS_ERR(trans)) { |
3632 | ret = PTR_ERR(trans); | |
3633 | goto out; | |
3634 | } | |
0b246afa JM |
3635 | btrfs_debug(fs_info, "auto deleting %Lu", |
3636 | found_key.objectid); | |
a8c9e576 JB |
3637 | ret = btrfs_del_orphan_item(trans, root, |
3638 | found_key.objectid); | |
3a45bb20 | 3639 | btrfs_end_transaction(trans); |
cbaee87f | 3640 | if (ret) |
4ef31a45 | 3641 | goto out; |
7b128766 JB |
3642 | continue; |
3643 | } | |
3644 | ||
f7e9e8fc | 3645 | nr_unlink++; |
7b128766 JB |
3646 | |
3647 | /* this will do delete_inode and everything for us */ | |
3648 | iput(inode); | |
3649 | } | |
3254c876 MX |
3650 | /* release the path since we're done with it */ |
3651 | btrfs_release_path(path); | |
3652 | ||
a575ceeb | 3653 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3654 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3655 | if (!IS_ERR(trans)) |
3a45bb20 | 3656 | btrfs_end_transaction(trans); |
d68fc57b | 3657 | } |
7b128766 JB |
3658 | |
3659 | if (nr_unlink) | |
0b246afa | 3660 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3661 | |
3662 | out: | |
3663 | if (ret) | |
0b246afa | 3664 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3665 | btrfs_free_path(path); |
3666 | return ret; | |
7b128766 JB |
3667 | } |
3668 | ||
46a53cca CM |
3669 | /* |
3670 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3671 | * don't find any xattrs, we know there can't be any acls. | |
3672 | * | |
3673 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3674 | */ | |
3675 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3676 | int slot, u64 objectid, |
3677 | int *first_xattr_slot) | |
46a53cca CM |
3678 | { |
3679 | u32 nritems = btrfs_header_nritems(leaf); | |
3680 | struct btrfs_key found_key; | |
f23b5a59 JB |
3681 | static u64 xattr_access = 0; |
3682 | static u64 xattr_default = 0; | |
46a53cca CM |
3683 | int scanned = 0; |
3684 | ||
f23b5a59 | 3685 | if (!xattr_access) { |
97d79299 AG |
3686 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3687 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3688 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3689 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3690 | } |
3691 | ||
46a53cca | 3692 | slot++; |
63541927 | 3693 | *first_xattr_slot = -1; |
46a53cca CM |
3694 | while (slot < nritems) { |
3695 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3696 | ||
3697 | /* we found a different objectid, there must not be acls */ | |
3698 | if (found_key.objectid != objectid) | |
3699 | return 0; | |
3700 | ||
3701 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3702 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3703 | if (*first_xattr_slot == -1) |
3704 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3705 | if (found_key.offset == xattr_access || |
3706 | found_key.offset == xattr_default) | |
3707 | return 1; | |
3708 | } | |
46a53cca CM |
3709 | |
3710 | /* | |
3711 | * we found a key greater than an xattr key, there can't | |
3712 | * be any acls later on | |
3713 | */ | |
3714 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3715 | return 0; | |
3716 | ||
3717 | slot++; | |
3718 | scanned++; | |
3719 | ||
3720 | /* | |
3721 | * it goes inode, inode backrefs, xattrs, extents, | |
3722 | * so if there are a ton of hard links to an inode there can | |
3723 | * be a lot of backrefs. Don't waste time searching too hard, | |
3724 | * this is just an optimization | |
3725 | */ | |
3726 | if (scanned >= 8) | |
3727 | break; | |
3728 | } | |
3729 | /* we hit the end of the leaf before we found an xattr or | |
3730 | * something larger than an xattr. We have to assume the inode | |
3731 | * has acls | |
3732 | */ | |
63541927 FDBM |
3733 | if (*first_xattr_slot == -1) |
3734 | *first_xattr_slot = slot; | |
46a53cca CM |
3735 | return 1; |
3736 | } | |
3737 | ||
d352ac68 CM |
3738 | /* |
3739 | * read an inode from the btree into the in-memory inode | |
3740 | */ | |
4222ea71 FM |
3741 | static int btrfs_read_locked_inode(struct inode *inode, |
3742 | struct btrfs_path *in_path) | |
39279cc3 | 3743 | { |
41044b41 | 3744 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
4222ea71 | 3745 | struct btrfs_path *path = in_path; |
5f39d397 | 3746 | struct extent_buffer *leaf; |
39279cc3 CM |
3747 | struct btrfs_inode_item *inode_item; |
3748 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3749 | struct btrfs_key location; | |
67de1176 | 3750 | unsigned long ptr; |
46a53cca | 3751 | int maybe_acls; |
618e21d5 | 3752 | u32 rdev; |
39279cc3 | 3753 | int ret; |
2f7e33d4 | 3754 | bool filled = false; |
63541927 | 3755 | int first_xattr_slot; |
2f7e33d4 MX |
3756 | |
3757 | ret = btrfs_fill_inode(inode, &rdev); | |
3758 | if (!ret) | |
3759 | filled = true; | |
39279cc3 | 3760 | |
4222ea71 FM |
3761 | if (!path) { |
3762 | path = btrfs_alloc_path(); | |
3763 | if (!path) | |
3764 | return -ENOMEM; | |
3765 | } | |
1748f843 | 3766 | |
39279cc3 | 3767 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3768 | |
39279cc3 | 3769 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3770 | if (ret) { |
4222ea71 FM |
3771 | if (path != in_path) |
3772 | btrfs_free_path(path); | |
f5b3a417 | 3773 | return ret; |
67710892 | 3774 | } |
39279cc3 | 3775 | |
5f39d397 | 3776 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3777 | |
3778 | if (filled) | |
67de1176 | 3779 | goto cache_index; |
2f7e33d4 | 3780 | |
5f39d397 CM |
3781 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3782 | struct btrfs_inode_item); | |
5f39d397 | 3783 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3784 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3785 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3786 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3787 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3788 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3789 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3790 | |
b1c38a13 JL |
3791 | inode_set_atime(inode, btrfs_timespec_sec(leaf, &inode_item->atime), |
3792 | btrfs_timespec_nsec(leaf, &inode_item->atime)); | |
5f39d397 | 3793 | |
b1c38a13 JL |
3794 | inode_set_mtime(inode, btrfs_timespec_sec(leaf, &inode_item->mtime), |
3795 | btrfs_timespec_nsec(leaf, &inode_item->mtime)); | |
5f39d397 | 3796 | |
2a9462de JL |
3797 | inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime), |
3798 | btrfs_timespec_nsec(leaf, &inode_item->ctime)); | |
5f39d397 | 3799 | |
c6e8f898 DS |
3800 | BTRFS_I(inode)->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime); |
3801 | BTRFS_I(inode)->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3802 | |
a76a3cd4 | 3803 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3804 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3805 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3806 | ||
c7f88c4e JL |
3807 | inode_set_iversion_queried(inode, |
3808 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3809 | inode->i_generation = BTRFS_I(inode)->generation; |
3810 | inode->i_rdev = 0; | |
3811 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3812 | ||
3813 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
77eea05e BB |
3814 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
3815 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); | |
6e17d30b YD |
3816 | |
3817 | cache_index: | |
5dc562c5 JB |
3818 | /* |
3819 | * If we were modified in the current generation and evicted from memory | |
3820 | * and then re-read we need to do a full sync since we don't have any | |
3821 | * idea about which extents were modified before we were evicted from | |
3822 | * cache. | |
6e17d30b YD |
3823 | * |
3824 | * This is required for both inode re-read from disk and delayed inode | |
6140ba8a | 3825 | * in the delayed_nodes xarray. |
5dc562c5 | 3826 | */ |
4a4f8fe2 | 3827 | if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info)) |
5dc562c5 JB |
3828 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3829 | &BTRFS_I(inode)->runtime_flags); | |
3830 | ||
bde6c242 FM |
3831 | /* |
3832 | * We don't persist the id of the transaction where an unlink operation | |
3833 | * against the inode was last made. So here we assume the inode might | |
3834 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3835 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3836 | * between the inode and its parent if the inode is fsync'ed and the log | |
3837 | * replayed. For example, in the scenario: | |
3838 | * | |
3839 | * touch mydir/foo | |
3840 | * ln mydir/foo mydir/bar | |
3841 | * sync | |
3842 | * unlink mydir/bar | |
3843 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3844 | * xfs_io -c fsync mydir/foo | |
3845 | * <power failure> | |
3846 | * mount fs, triggers fsync log replay | |
3847 | * | |
3848 | * We must make sure that when we fsync our inode foo we also log its | |
3849 | * parent inode, otherwise after log replay the parent still has the | |
3850 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3851 | * and doesn't have an inode ref with the name "bar" anymore. | |
3852 | * | |
3853 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3854 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3855 | * transaction commits on fsync if our inode is a directory, or if our |
3856 | * inode is not a directory, logging its parent unnecessarily. | |
3857 | */ | |
3858 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3859 | ||
3ebac17c FM |
3860 | /* |
3861 | * Same logic as for last_unlink_trans. We don't persist the generation | |
3862 | * of the last transaction where this inode was used for a reflink | |
3863 | * operation, so after eviction and reloading the inode we must be | |
3864 | * pessimistic and assume the last transaction that modified the inode. | |
3865 | */ | |
3866 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
3867 | ||
67de1176 MX |
3868 | path->slots[0]++; |
3869 | if (inode->i_nlink != 1 || | |
3870 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3871 | goto cache_acl; | |
3872 | ||
3873 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3874 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3875 | goto cache_acl; |
3876 | ||
3877 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3878 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3879 | struct btrfs_inode_ref *ref; | |
3880 | ||
3881 | ref = (struct btrfs_inode_ref *)ptr; | |
3882 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3883 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3884 | struct btrfs_inode_extref *extref; | |
3885 | ||
3886 | extref = (struct btrfs_inode_extref *)ptr; | |
3887 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3888 | extref); | |
3889 | } | |
2f7e33d4 | 3890 | cache_acl: |
46a53cca CM |
3891 | /* |
3892 | * try to precache a NULL acl entry for files that don't have | |
3893 | * any xattrs or acls | |
3894 | */ | |
33345d01 | 3895 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3896 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3897 | if (first_xattr_slot != -1) { |
3898 | path->slots[0] = first_xattr_slot; | |
3899 | ret = btrfs_load_inode_props(inode, path); | |
3900 | if (ret) | |
0b246afa | 3901 | btrfs_err(fs_info, |
351fd353 | 3902 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3903 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3904 | root->root_key.objectid, ret); |
3905 | } | |
4222ea71 FM |
3906 | if (path != in_path) |
3907 | btrfs_free_path(path); | |
63541927 | 3908 | |
72c04902 AV |
3909 | if (!maybe_acls) |
3910 | cache_no_acl(inode); | |
46a53cca | 3911 | |
39279cc3 | 3912 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3913 | case S_IFREG: |
3914 | inode->i_mapping->a_ops = &btrfs_aops; | |
3915 | inode->i_fop = &btrfs_file_operations; | |
3916 | inode->i_op = &btrfs_file_inode_operations; | |
3917 | break; | |
3918 | case S_IFDIR: | |
3919 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3920 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3921 | break; |
3922 | case S_IFLNK: | |
3923 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3924 | inode_nohighmem(inode); |
4779cc04 | 3925 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3926 | break; |
618e21d5 | 3927 | default: |
0279b4cd | 3928 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3929 | init_special_inode(inode, inode->i_mode, rdev); |
3930 | break; | |
39279cc3 | 3931 | } |
6cbff00f | 3932 | |
7b6a221e | 3933 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3934 | return 0; |
39279cc3 CM |
3935 | } |
3936 | ||
d352ac68 CM |
3937 | /* |
3938 | * given a leaf and an inode, copy the inode fields into the leaf | |
3939 | */ | |
e02119d5 CM |
3940 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3941 | struct extent_buffer *leaf, | |
5f39d397 | 3942 | struct btrfs_inode_item *item, |
39279cc3 CM |
3943 | struct inode *inode) |
3944 | { | |
51fab693 | 3945 | struct btrfs_map_token token; |
77eea05e | 3946 | u64 flags; |
51fab693 | 3947 | |
c82f823c | 3948 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3949 | |
cc4c13d5 DS |
3950 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3951 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3952 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3953 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3954 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3955 | ||
3956 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
b1c38a13 | 3957 | inode_get_atime_sec(inode)); |
cc4c13d5 | 3958 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
b1c38a13 | 3959 | inode_get_atime_nsec(inode)); |
cc4c13d5 DS |
3960 | |
3961 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
b1c38a13 | 3962 | inode_get_mtime_sec(inode)); |
cc4c13d5 | 3963 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
b1c38a13 | 3964 | inode_get_mtime_nsec(inode)); |
cc4c13d5 DS |
3965 | |
3966 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
b1c38a13 | 3967 | inode_get_ctime_sec(inode)); |
cc4c13d5 | 3968 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
b1c38a13 | 3969 | inode_get_ctime_nsec(inode)); |
cc4c13d5 | 3970 | |
c6e8f898 DS |
3971 | btrfs_set_token_timespec_sec(&token, &item->otime, BTRFS_I(inode)->i_otime_sec); |
3972 | btrfs_set_token_timespec_nsec(&token, &item->otime, BTRFS_I(inode)->i_otime_nsec); | |
cc4c13d5 DS |
3973 | |
3974 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3975 | btrfs_set_token_inode_generation(&token, item, | |
3976 | BTRFS_I(inode)->generation); | |
3977 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3978 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3979 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
77eea05e BB |
3980 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
3981 | BTRFS_I(inode)->ro_flags); | |
3982 | btrfs_set_token_inode_flags(&token, item, flags); | |
cc4c13d5 | 3983 | btrfs_set_token_inode_block_group(&token, item, 0); |
39279cc3 CM |
3984 | } |
3985 | ||
d352ac68 CM |
3986 | /* |
3987 | * copy everything in the in-memory inode into the btree. | |
3988 | */ | |
2115133f | 3989 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
07a274a8 | 3990 | struct btrfs_inode *inode) |
39279cc3 CM |
3991 | { |
3992 | struct btrfs_inode_item *inode_item; | |
3993 | struct btrfs_path *path; | |
5f39d397 | 3994 | struct extent_buffer *leaf; |
39279cc3 CM |
3995 | int ret; |
3996 | ||
3997 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3998 | if (!path) |
3999 | return -ENOMEM; | |
4000 | ||
07a274a8 | 4001 | ret = btrfs_lookup_inode(trans, inode->root, path, &inode->location, 1); |
39279cc3 CM |
4002 | if (ret) { |
4003 | if (ret > 0) | |
4004 | ret = -ENOENT; | |
4005 | goto failed; | |
4006 | } | |
4007 | ||
5f39d397 CM |
4008 | leaf = path->nodes[0]; |
4009 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 4010 | struct btrfs_inode_item); |
39279cc3 | 4011 | |
dfeb9e7c | 4012 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
50564b65 | 4013 | btrfs_mark_buffer_dirty(trans, leaf); |
dfeb9e7c | 4014 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
4015 | ret = 0; |
4016 | failed: | |
39279cc3 CM |
4017 | btrfs_free_path(path); |
4018 | return ret; | |
4019 | } | |
4020 | ||
2115133f CM |
4021 | /* |
4022 | * copy everything in the in-memory inode into the btree. | |
4023 | */ | |
cddaaacc | 4024 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
cddaaacc | 4025 | struct btrfs_inode *inode) |
2115133f | 4026 | { |
8b9d0322 | 4027 | struct btrfs_root *root = inode->root; |
0b246afa | 4028 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
4029 | int ret; |
4030 | ||
4031 | /* | |
4032 | * If the inode is a free space inode, we can deadlock during commit | |
4033 | * if we put it into the delayed code. | |
4034 | * | |
4035 | * The data relocation inode should also be directly updated | |
4036 | * without delay | |
4037 | */ | |
9a56fcd1 | 4038 | if (!btrfs_is_free_space_inode(inode) |
37f00a6d | 4039 | && !btrfs_is_data_reloc_root(root) |
0b246afa | 4040 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4041 | btrfs_update_root_times(trans, root); |
4042 | ||
04bd8e94 | 4043 | ret = btrfs_delayed_update_inode(trans, inode); |
2115133f | 4044 | if (!ret) |
9a56fcd1 | 4045 | btrfs_set_inode_last_trans(trans, inode); |
2115133f CM |
4046 | return ret; |
4047 | } | |
4048 | ||
07a274a8 | 4049 | return btrfs_update_inode_item(trans, inode); |
2115133f CM |
4050 | } |
4051 | ||
729f7961 | 4052 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
0a5d0dc5 | 4053 | struct btrfs_inode *inode) |
2115133f CM |
4054 | { |
4055 | int ret; | |
4056 | ||
8b9d0322 | 4057 | ret = btrfs_update_inode(trans, inode); |
2115133f | 4058 | if (ret == -ENOSPC) |
07a274a8 | 4059 | return btrfs_update_inode_item(trans, inode); |
2115133f CM |
4060 | return ret; |
4061 | } | |
4062 | ||
d352ac68 CM |
4063 | /* |
4064 | * unlink helper that gets used here in inode.c and in the tree logging | |
4065 | * recovery code. It remove a link in a directory with a given name, and | |
4066 | * also drops the back refs in the inode to the directory | |
4067 | */ | |
92986796 | 4068 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e NB |
4069 | struct btrfs_inode *dir, |
4070 | struct btrfs_inode *inode, | |
6db75318 | 4071 | const struct fscrypt_str *name, |
88d2beec | 4072 | struct btrfs_rename_ctx *rename_ctx) |
39279cc3 | 4073 | { |
4467af88 | 4074 | struct btrfs_root *root = dir->root; |
0b246afa | 4075 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4076 | struct btrfs_path *path; |
39279cc3 | 4077 | int ret = 0; |
39279cc3 | 4078 | struct btrfs_dir_item *di; |
aec7477b | 4079 | u64 index; |
33345d01 LZ |
4080 | u64 ino = btrfs_ino(inode); |
4081 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4082 | |
4083 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4084 | if (!path) { |
4085 | ret = -ENOMEM; | |
554233a6 | 4086 | goto out; |
54aa1f4d CM |
4087 | } |
4088 | ||
e43eec81 | 4089 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1); |
3cf5068f LB |
4090 | if (IS_ERR_OR_NULL(di)) { |
4091 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
4092 | goto err; |
4093 | } | |
39279cc3 | 4094 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4095 | if (ret) |
4096 | goto err; | |
b3b4aa74 | 4097 | btrfs_release_path(path); |
39279cc3 | 4098 | |
67de1176 MX |
4099 | /* |
4100 | * If we don't have dir index, we have to get it by looking up | |
4101 | * the inode ref, since we get the inode ref, remove it directly, | |
4102 | * it is unnecessary to do delayed deletion. | |
4103 | * | |
4104 | * But if we have dir index, needn't search inode ref to get it. | |
4105 | * Since the inode ref is close to the inode item, it is better | |
4106 | * that we delay to delete it, and just do this deletion when | |
4107 | * we update the inode item. | |
4108 | */ | |
4ec5934e | 4109 | if (inode->dir_index) { |
67de1176 MX |
4110 | ret = btrfs_delayed_delete_inode_ref(inode); |
4111 | if (!ret) { | |
4ec5934e | 4112 | index = inode->dir_index; |
67de1176 MX |
4113 | goto skip_backref; |
4114 | } | |
4115 | } | |
4116 | ||
e43eec81 | 4117 | ret = btrfs_del_inode_ref(trans, root, name, ino, dir_ino, &index); |
aec7477b | 4118 | if (ret) { |
0b246afa | 4119 | btrfs_info(fs_info, |
c2cf52eb | 4120 | "failed to delete reference to %.*s, inode %llu parent %llu", |
e43eec81 | 4121 | name->len, name->name, ino, dir_ino); |
66642832 | 4122 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4123 | goto err; |
4124 | } | |
67de1176 | 4125 | skip_backref: |
88d2beec FM |
4126 | if (rename_ctx) |
4127 | rename_ctx->index = index; | |
4128 | ||
9add2945 | 4129 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4130 | if (ret) { |
66642832 | 4131 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4132 | goto err; |
79787eaa | 4133 | } |
39279cc3 | 4134 | |
259c4b96 FM |
4135 | /* |
4136 | * If we are in a rename context, we don't need to update anything in the | |
4137 | * log. That will be done later during the rename by btrfs_log_new_name(). | |
143823cf | 4138 | * Besides that, doing it here would only cause extra unnecessary btree |
259c4b96 FM |
4139 | * operations on the log tree, increasing latency for applications. |
4140 | */ | |
4141 | if (!rename_ctx) { | |
e43eec81 STD |
4142 | btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino); |
4143 | btrfs_del_dir_entries_in_log(trans, root, name, dir, index); | |
259c4b96 | 4144 | } |
63611e73 JB |
4145 | |
4146 | /* | |
4147 | * If we have a pending delayed iput we could end up with the final iput | |
4148 | * being run in btrfs-cleaner context. If we have enough of these built | |
4149 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
4150 | * way to throttle the unlinks. Since we're currently holding a ref on | |
4151 | * the inode we can run the delayed iput here without any issues as the | |
4152 | * final iput won't be done until after we drop the ref we're currently | |
4153 | * holding. | |
4154 | */ | |
4155 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
4156 | err: |
4157 | btrfs_free_path(path); | |
e02119d5 CM |
4158 | if (ret) |
4159 | goto out; | |
4160 | ||
e43eec81 | 4161 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2); |
4ec5934e NB |
4162 | inode_inc_iversion(&inode->vfs_inode); |
4163 | inode_inc_iversion(&dir->vfs_inode); | |
b1c38a13 | 4164 | inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode)); |
8b9d0322 | 4165 | ret = btrfs_update_inode(trans, dir); |
e02119d5 | 4166 | out: |
39279cc3 CM |
4167 | return ret; |
4168 | } | |
4169 | ||
92986796 | 4170 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4ec5934e | 4171 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
6db75318 | 4172 | const struct fscrypt_str *name) |
92986796 AV |
4173 | { |
4174 | int ret; | |
e43eec81 STD |
4175 | |
4176 | ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL); | |
92986796 | 4177 | if (!ret) { |
4ec5934e | 4178 | drop_nlink(&inode->vfs_inode); |
8b9d0322 | 4179 | ret = btrfs_update_inode(trans, inode); |
92986796 AV |
4180 | } |
4181 | return ret; | |
4182 | } | |
39279cc3 | 4183 | |
a22285a6 YZ |
4184 | /* |
4185 | * helper to start transaction for unlink and rmdir. | |
4186 | * | |
d52be818 JB |
4187 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4188 | * if we cannot make our reservations the normal way try and see if there is | |
4189 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4190 | * allow the unlink to occur. | |
a22285a6 | 4191 | */ |
e569b1d5 | 4192 | static struct btrfs_trans_handle *__unlink_start_trans(struct btrfs_inode *dir) |
4df27c4d | 4193 | { |
e569b1d5 | 4194 | struct btrfs_root *root = dir->root; |
4df27c4d | 4195 | |
5630e2bc FM |
4196 | return btrfs_start_transaction_fallback_global_rsv(root, |
4197 | BTRFS_UNLINK_METADATA_UNITS); | |
a22285a6 YZ |
4198 | } |
4199 | ||
4200 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4201 | { | |
a22285a6 | 4202 | struct btrfs_trans_handle *trans; |
2b0143b5 | 4203 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4204 | int ret; |
ab3c5c18 | 4205 | struct fscrypt_name fname; |
a22285a6 | 4206 | |
ab3c5c18 STD |
4207 | ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
4208 | if (ret) | |
4209 | return ret; | |
ab3c5c18 STD |
4210 | |
4211 | /* This needs to handle no-key deletions later on */ | |
a22285a6 | 4212 | |
e569b1d5 | 4213 | trans = __unlink_start_trans(BTRFS_I(dir)); |
ab3c5c18 STD |
4214 | if (IS_ERR(trans)) { |
4215 | ret = PTR_ERR(trans); | |
4216 | goto fscrypt_free; | |
4217 | } | |
5f39d397 | 4218 | |
4ec5934e | 4219 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
59fcf388 | 4220 | false); |
12fcfd22 | 4221 | |
e43eec81 | 4222 | ret = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4223 | &fname.disk_name); |
b532402e | 4224 | if (ret) |
ab3c5c18 | 4225 | goto end_trans; |
7b128766 | 4226 | |
a22285a6 | 4227 | if (inode->i_nlink == 0) { |
73f2e545 | 4228 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e | 4229 | if (ret) |
ab3c5c18 | 4230 | goto end_trans; |
a22285a6 | 4231 | } |
7b128766 | 4232 | |
ab3c5c18 | 4233 | end_trans: |
3a45bb20 | 4234 | btrfs_end_transaction(trans); |
4467af88 | 4235 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
ab3c5c18 STD |
4236 | fscrypt_free: |
4237 | fscrypt_free_filename(&fname); | |
39279cc3 CM |
4238 | return ret; |
4239 | } | |
4240 | ||
f60a2364 | 4241 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
5b7544cb | 4242 | struct btrfs_inode *dir, struct dentry *dentry) |
4df27c4d | 4243 | { |
5b7544cb | 4244 | struct btrfs_root *root = dir->root; |
045d3967 | 4245 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
4246 | struct btrfs_path *path; |
4247 | struct extent_buffer *leaf; | |
4248 | struct btrfs_dir_item *di; | |
4249 | struct btrfs_key key; | |
4250 | u64 index; | |
4251 | int ret; | |
045d3967 | 4252 | u64 objectid; |
5b7544cb | 4253 | u64 dir_ino = btrfs_ino(dir); |
ab3c5c18 STD |
4254 | struct fscrypt_name fname; |
4255 | ||
5b7544cb | 4256 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); |
ab3c5c18 STD |
4257 | if (ret) |
4258 | return ret; | |
ab3c5c18 STD |
4259 | |
4260 | /* This needs to handle no-key deletions later on */ | |
4df27c4d | 4261 | |
045d3967 JB |
4262 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
4263 | objectid = inode->root->root_key.objectid; | |
4264 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
4265 | objectid = inode->location.objectid; | |
4266 | } else { | |
4267 | WARN_ON(1); | |
ab3c5c18 | 4268 | fscrypt_free_filename(&fname); |
045d3967 JB |
4269 | return -EINVAL; |
4270 | } | |
4271 | ||
4df27c4d | 4272 | path = btrfs_alloc_path(); |
ab3c5c18 STD |
4273 | if (!path) { |
4274 | ret = -ENOMEM; | |
4275 | goto out; | |
4276 | } | |
4df27c4d | 4277 | |
33345d01 | 4278 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
6db75318 | 4279 | &fname.disk_name, -1); |
79787eaa | 4280 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4281 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4282 | goto out; |
4283 | } | |
4df27c4d YZ |
4284 | |
4285 | leaf = path->nodes[0]; | |
4286 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4287 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4288 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4289 | if (ret) { |
66642832 | 4290 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4291 | goto out; |
4292 | } | |
b3b4aa74 | 4293 | btrfs_release_path(path); |
4df27c4d | 4294 | |
d49d3287 JB |
4295 | /* |
4296 | * This is a placeholder inode for a subvolume we didn't have a | |
4297 | * reference to at the time of the snapshot creation. In the meantime | |
4298 | * we could have renamed the real subvol link into our snapshot, so | |
1a9fd417 | 4299 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
d49d3287 JB |
4300 | * Instead simply lookup the dir_index_item for this entry so we can |
4301 | * remove it. Otherwise we know we have a ref to the root and we can | |
4302 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
4303 | */ | |
4304 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
6db75318 | 4305 | di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name); |
79787eaa JM |
4306 | if (IS_ERR_OR_NULL(di)) { |
4307 | if (!di) | |
4308 | ret = -ENOENT; | |
4309 | else | |
4310 | ret = PTR_ERR(di); | |
66642832 | 4311 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4312 | goto out; |
4313 | } | |
4df27c4d YZ |
4314 | |
4315 | leaf = path->nodes[0]; | |
4316 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 4317 | index = key.offset; |
d49d3287 JB |
4318 | btrfs_release_path(path); |
4319 | } else { | |
4320 | ret = btrfs_del_root_ref(trans, objectid, | |
4321 | root->root_key.objectid, dir_ino, | |
6db75318 | 4322 | &index, &fname.disk_name); |
d49d3287 JB |
4323 | if (ret) { |
4324 | btrfs_abort_transaction(trans, ret); | |
4325 | goto out; | |
4326 | } | |
4df27c4d YZ |
4327 | } |
4328 | ||
5b7544cb | 4329 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4330 | if (ret) { |
66642832 | 4331 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4332 | goto out; |
4333 | } | |
4df27c4d | 4334 | |
5b7544cb DS |
4335 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2); |
4336 | inode_inc_iversion(&dir->vfs_inode); | |
b1c38a13 | 4337 | inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode)); |
0a5d0dc5 | 4338 | ret = btrfs_update_inode_fallback(trans, dir); |
79787eaa | 4339 | if (ret) |
66642832 | 4340 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4341 | out: |
71d7aed0 | 4342 | btrfs_free_path(path); |
ab3c5c18 | 4343 | fscrypt_free_filename(&fname); |
79787eaa | 4344 | return ret; |
4df27c4d YZ |
4345 | } |
4346 | ||
ec42f167 MT |
4347 | /* |
4348 | * Helper to check if the subvolume references other subvolumes or if it's | |
4349 | * default. | |
4350 | */ | |
f60a2364 | 4351 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4352 | { |
4353 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4354 | struct btrfs_path *path; | |
4355 | struct btrfs_dir_item *di; | |
4356 | struct btrfs_key key; | |
6db75318 | 4357 | struct fscrypt_str name = FSTR_INIT("default", 7); |
ec42f167 MT |
4358 | u64 dir_id; |
4359 | int ret; | |
4360 | ||
4361 | path = btrfs_alloc_path(); | |
4362 | if (!path) | |
4363 | return -ENOMEM; | |
4364 | ||
4365 | /* Make sure this root isn't set as the default subvol */ | |
4366 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4367 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
e43eec81 | 4368 | dir_id, &name, 0); |
ec42f167 MT |
4369 | if (di && !IS_ERR(di)) { |
4370 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4371 | if (key.objectid == root->root_key.objectid) { | |
4372 | ret = -EPERM; | |
4373 | btrfs_err(fs_info, | |
4374 | "deleting default subvolume %llu is not allowed", | |
4375 | key.objectid); | |
4376 | goto out; | |
4377 | } | |
4378 | btrfs_release_path(path); | |
4379 | } | |
4380 | ||
4381 | key.objectid = root->root_key.objectid; | |
4382 | key.type = BTRFS_ROOT_REF_KEY; | |
4383 | key.offset = (u64)-1; | |
4384 | ||
4385 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4386 | if (ret < 0) | |
4387 | goto out; | |
4388 | BUG_ON(ret == 0); | |
4389 | ||
4390 | ret = 0; | |
4391 | if (path->slots[0] > 0) { | |
4392 | path->slots[0]--; | |
4393 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4394 | if (key.objectid == root->root_key.objectid && | |
4395 | key.type == BTRFS_ROOT_REF_KEY) | |
4396 | ret = -ENOTEMPTY; | |
4397 | } | |
4398 | out: | |
4399 | btrfs_free_path(path); | |
4400 | return ret; | |
4401 | } | |
4402 | ||
20a68004 NB |
4403 | /* Delete all dentries for inodes belonging to the root */ |
4404 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4405 | { | |
4406 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4407 | struct rb_node *node; | |
4408 | struct rb_node *prev; | |
4409 | struct btrfs_inode *entry; | |
4410 | struct inode *inode; | |
4411 | u64 objectid = 0; | |
4412 | ||
84961539 | 4413 | if (!BTRFS_FS_ERROR(fs_info)) |
20a68004 NB |
4414 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
4415 | ||
4416 | spin_lock(&root->inode_lock); | |
4417 | again: | |
4418 | node = root->inode_tree.rb_node; | |
4419 | prev = NULL; | |
4420 | while (node) { | |
4421 | prev = node; | |
4422 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4423 | ||
37508515 | 4424 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4425 | node = node->rb_left; |
37508515 | 4426 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4427 | node = node->rb_right; |
4428 | else | |
4429 | break; | |
4430 | } | |
4431 | if (!node) { | |
4432 | while (prev) { | |
4433 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4434 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4435 | node = prev; |
4436 | break; | |
4437 | } | |
4438 | prev = rb_next(prev); | |
4439 | } | |
4440 | } | |
4441 | while (node) { | |
4442 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4443 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4444 | inode = igrab(&entry->vfs_inode); |
4445 | if (inode) { | |
4446 | spin_unlock(&root->inode_lock); | |
4447 | if (atomic_read(&inode->i_count) > 1) | |
4448 | d_prune_aliases(inode); | |
4449 | /* | |
4450 | * btrfs_drop_inode will have it removed from the inode | |
4451 | * cache when its usage count hits zero. | |
4452 | */ | |
4453 | iput(inode); | |
4454 | cond_resched(); | |
4455 | spin_lock(&root->inode_lock); | |
4456 | goto again; | |
4457 | } | |
4458 | ||
4459 | if (cond_resched_lock(&root->inode_lock)) | |
4460 | goto again; | |
4461 | ||
4462 | node = rb_next(node); | |
4463 | } | |
4464 | spin_unlock(&root->inode_lock); | |
4465 | } | |
4466 | ||
3c4f91e2 | 4467 | int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry) |
f60a2364 | 4468 | { |
3c4f91e2 | 4469 | struct btrfs_root *root = dir->root; |
41044b41 | 4470 | struct btrfs_fs_info *fs_info = root->fs_info; |
f60a2364 MT |
4471 | struct inode *inode = d_inode(dentry); |
4472 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4473 | struct btrfs_trans_handle *trans; | |
4474 | struct btrfs_block_rsv block_rsv; | |
4475 | u64 root_flags; | |
f60a2364 | 4476 | int ret; |
f60a2364 | 4477 | |
3324d054 OS |
4478 | down_write(&fs_info->subvol_sem); |
4479 | ||
f60a2364 MT |
4480 | /* |
4481 | * Don't allow to delete a subvolume with send in progress. This is | |
4482 | * inside the inode lock so the error handling that has to drop the bit | |
4483 | * again is not run concurrently. | |
4484 | */ | |
4485 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4486 | if (dest->send_in_progress) { |
f60a2364 MT |
4487 | spin_unlock(&dest->root_item_lock); |
4488 | btrfs_warn(fs_info, | |
4489 | "attempt to delete subvolume %llu during send", | |
4490 | dest->root_key.objectid); | |
3324d054 OS |
4491 | ret = -EPERM; |
4492 | goto out_up_write; | |
f60a2364 | 4493 | } |
60021bd7 KH |
4494 | if (atomic_read(&dest->nr_swapfiles)) { |
4495 | spin_unlock(&dest->root_item_lock); | |
4496 | btrfs_warn(fs_info, | |
4497 | "attempt to delete subvolume %llu with active swapfile", | |
4498 | root->root_key.objectid); | |
3324d054 OS |
4499 | ret = -EPERM; |
4500 | goto out_up_write; | |
60021bd7 | 4501 | } |
a7176f74 LF |
4502 | root_flags = btrfs_root_flags(&dest->root_item); |
4503 | btrfs_set_root_flags(&dest->root_item, | |
4504 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4505 | spin_unlock(&dest->root_item_lock); | |
f60a2364 | 4506 | |
ee0d904f NB |
4507 | ret = may_destroy_subvol(dest); |
4508 | if (ret) | |
3324d054 | 4509 | goto out_undead; |
f60a2364 MT |
4510 | |
4511 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4512 | /* | |
4513 | * One for dir inode, | |
4514 | * two for dir entries, | |
4515 | * two for root ref/backref. | |
4516 | */ | |
ee0d904f NB |
4517 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
4518 | if (ret) | |
3324d054 | 4519 | goto out_undead; |
f60a2364 MT |
4520 | |
4521 | trans = btrfs_start_transaction(root, 0); | |
4522 | if (IS_ERR(trans)) { | |
ee0d904f | 4523 | ret = PTR_ERR(trans); |
f60a2364 MT |
4524 | goto out_release; |
4525 | } | |
4526 | trans->block_rsv = &block_rsv; | |
4527 | trans->bytes_reserved = block_rsv.size; | |
4528 | ||
3c4f91e2 | 4529 | btrfs_record_snapshot_destroy(trans, dir); |
f60a2364 | 4530 | |
045d3967 | 4531 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 | 4532 | if (ret) { |
f60a2364 MT |
4533 | btrfs_abort_transaction(trans, ret); |
4534 | goto out_end_trans; | |
4535 | } | |
4536 | ||
2731f518 JB |
4537 | ret = btrfs_record_root_in_trans(trans, dest); |
4538 | if (ret) { | |
4539 | btrfs_abort_transaction(trans, ret); | |
4540 | goto out_end_trans; | |
4541 | } | |
f60a2364 MT |
4542 | |
4543 | memset(&dest->root_item.drop_progress, 0, | |
4544 | sizeof(dest->root_item.drop_progress)); | |
c8422684 | 4545 | btrfs_set_root_drop_level(&dest->root_item, 0); |
f60a2364 MT |
4546 | btrfs_set_root_refs(&dest->root_item, 0); |
4547 | ||
4548 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4549 | ret = btrfs_insert_orphan_item(trans, | |
4550 | fs_info->tree_root, | |
4551 | dest->root_key.objectid); | |
4552 | if (ret) { | |
4553 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4554 | goto out_end_trans; |
4555 | } | |
4556 | } | |
4557 | ||
d1957791 | 4558 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4559 | BTRFS_UUID_KEY_SUBVOL, |
4560 | dest->root_key.objectid); | |
4561 | if (ret && ret != -ENOENT) { | |
4562 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4563 | goto out_end_trans; |
4564 | } | |
4565 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4566 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4567 | dest->root_item.received_uuid, |
4568 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4569 | dest->root_key.objectid); | |
4570 | if (ret && ret != -ENOENT) { | |
4571 | btrfs_abort_transaction(trans, ret); | |
f60a2364 MT |
4572 | goto out_end_trans; |
4573 | } | |
4574 | } | |
4575 | ||
082b6c97 QW |
4576 | free_anon_bdev(dest->anon_dev); |
4577 | dest->anon_dev = 0; | |
f60a2364 MT |
4578 | out_end_trans: |
4579 | trans->block_rsv = NULL; | |
4580 | trans->bytes_reserved = 0; | |
4581 | ret = btrfs_end_transaction(trans); | |
f60a2364 MT |
4582 | inode->i_flags |= S_DEAD; |
4583 | out_release: | |
e85fde51 | 4584 | btrfs_subvolume_release_metadata(root, &block_rsv); |
3324d054 | 4585 | out_undead: |
ee0d904f | 4586 | if (ret) { |
f60a2364 MT |
4587 | spin_lock(&dest->root_item_lock); |
4588 | root_flags = btrfs_root_flags(&dest->root_item); | |
4589 | btrfs_set_root_flags(&dest->root_item, | |
4590 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4591 | spin_unlock(&dest->root_item_lock); | |
3324d054 OS |
4592 | } |
4593 | out_up_write: | |
4594 | up_write(&fs_info->subvol_sem); | |
4595 | if (!ret) { | |
f60a2364 | 4596 | d_invalidate(dentry); |
20a68004 | 4597 | btrfs_prune_dentries(dest); |
f60a2364 | 4598 | ASSERT(dest->send_in_progress == 0); |
f60a2364 MT |
4599 | } |
4600 | ||
ee0d904f | 4601 | return ret; |
f60a2364 MT |
4602 | } |
4603 | ||
39279cc3 CM |
4604 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4605 | { | |
2b0143b5 | 4606 | struct inode *inode = d_inode(dentry); |
813febdb | 4607 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
1832a6d5 | 4608 | int err = 0; |
39279cc3 | 4609 | struct btrfs_trans_handle *trans; |
44f714da | 4610 | u64 last_unlink_trans; |
ab3c5c18 | 4611 | struct fscrypt_name fname; |
39279cc3 | 4612 | |
b3ae244e | 4613 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4614 | return -ENOTEMPTY; |
813febdb JB |
4615 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) { |
4616 | if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) { | |
4617 | btrfs_err(fs_info, | |
4618 | "extent tree v2 doesn't support snapshot deletion yet"); | |
4619 | return -EOPNOTSUPP; | |
4620 | } | |
3c4f91e2 | 4621 | return btrfs_delete_subvolume(BTRFS_I(dir), dentry); |
813febdb | 4622 | } |
134d4512 | 4623 | |
ab3c5c18 STD |
4624 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); |
4625 | if (err) | |
4626 | return err; | |
ab3c5c18 STD |
4627 | |
4628 | /* This needs to handle no-key deletions later on */ | |
4629 | ||
e569b1d5 | 4630 | trans = __unlink_start_trans(BTRFS_I(dir)); |
ab3c5c18 STD |
4631 | if (IS_ERR(trans)) { |
4632 | err = PTR_ERR(trans); | |
4633 | goto out_notrans; | |
4634 | } | |
5df6a9f6 | 4635 | |
4a0cc7ca | 4636 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
5b7544cb | 4637 | err = btrfs_unlink_subvol(trans, BTRFS_I(dir), dentry); |
4df27c4d YZ |
4638 | goto out; |
4639 | } | |
4640 | ||
73f2e545 | 4641 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4642 | if (err) |
4df27c4d | 4643 | goto out; |
7b128766 | 4644 | |
44f714da FM |
4645 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4646 | ||
39279cc3 | 4647 | /* now the directory is empty */ |
e43eec81 | 4648 | err = btrfs_unlink_inode(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
6db75318 | 4649 | &fname.disk_name); |
44f714da | 4650 | if (!err) { |
6ef06d27 | 4651 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4652 | /* |
4653 | * Propagate the last_unlink_trans value of the deleted dir to | |
4654 | * its parent directory. This is to prevent an unrecoverable | |
4655 | * log tree in the case we do something like this: | |
4656 | * 1) create dir foo | |
4657 | * 2) create snapshot under dir foo | |
4658 | * 3) delete the snapshot | |
4659 | * 4) rmdir foo | |
4660 | * 5) mkdir foo | |
4661 | * 6) fsync foo or some file inside foo | |
4662 | */ | |
4663 | if (last_unlink_trans >= trans->transid) | |
4664 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4665 | } | |
4df27c4d | 4666 | out: |
3a45bb20 | 4667 | btrfs_end_transaction(trans); |
ab3c5c18 | 4668 | out_notrans: |
813febdb | 4669 | btrfs_btree_balance_dirty(fs_info); |
ab3c5c18 | 4670 | fscrypt_free_filename(&fname); |
3954401f | 4671 | |
39279cc3 CM |
4672 | return err; |
4673 | } | |
4674 | ||
39279cc3 | 4675 | /* |
9580503b DS |
4676 | * Read, zero a chunk and write a block. |
4677 | * | |
2aaa6655 JB |
4678 | * @inode - inode that we're zeroing |
4679 | * @from - the offset to start zeroing | |
4680 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4681 | * offset | |
4682 | * @front - zero up to the offset instead of from the offset on | |
4683 | * | |
9703fefe | 4684 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4685 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4686 | */ |
217f42eb NB |
4687 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
4688 | int front) | |
39279cc3 | 4689 | { |
217f42eb NB |
4690 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4691 | struct address_space *mapping = inode->vfs_inode.i_mapping; | |
4692 | struct extent_io_tree *io_tree = &inode->io_tree; | |
e6dcd2dc | 4693 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 4694 | struct extent_state *cached_state = NULL; |
364ecf36 | 4695 | struct extent_changeset *data_reserved = NULL; |
6d4572a9 | 4696 | bool only_release_metadata = false; |
0b246afa | 4697 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4698 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4699 | unsigned offset = from & (blocksize - 1); |
df055afe | 4700 | struct folio *folio; |
3b16a4e3 | 4701 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4702 | size_t write_bytes = blocksize; |
39279cc3 | 4703 | int ret = 0; |
9703fefe CR |
4704 | u64 block_start; |
4705 | u64 block_end; | |
39279cc3 | 4706 | |
b03ebd99 NB |
4707 | if (IS_ALIGNED(offset, blocksize) && |
4708 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4709 | goto out; |
9703fefe | 4710 | |
8b62f87b JB |
4711 | block_start = round_down(from, blocksize); |
4712 | block_end = block_start + blocksize - 1; | |
4713 | ||
217f42eb | 4714 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
1daedb1d | 4715 | blocksize, false); |
6d4572a9 | 4716 | if (ret < 0) { |
80f9d241 | 4717 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) { |
6d4572a9 QW |
4718 | /* For nocow case, no need to reserve data space */ |
4719 | only_release_metadata = true; | |
4720 | } else { | |
4721 | goto out; | |
4722 | } | |
4723 | } | |
d4135134 | 4724 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false); |
6d4572a9 QW |
4725 | if (ret < 0) { |
4726 | if (!only_release_metadata) | |
217f42eb NB |
4727 | btrfs_free_reserved_data_space(inode, data_reserved, |
4728 | block_start, blocksize); | |
6d4572a9 QW |
4729 | goto out; |
4730 | } | |
211c17f5 | 4731 | again: |
df055afe GR |
4732 | folio = __filemap_get_folio(mapping, index, |
4733 | FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mask); | |
4734 | if (IS_ERR(folio)) { | |
217f42eb NB |
4735 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
4736 | blocksize, true); | |
4737 | btrfs_delalloc_release_extents(inode, blocksize); | |
ac6a2b36 | 4738 | ret = -ENOMEM; |
39279cc3 | 4739 | goto out; |
5d5e103a | 4740 | } |
e6dcd2dc | 4741 | |
df055afe GR |
4742 | if (!folio_test_uptodate(folio)) { |
4743 | ret = btrfs_read_folio(NULL, folio); | |
4744 | folio_lock(folio); | |
4745 | if (folio->mapping != mapping) { | |
4746 | folio_unlock(folio); | |
4747 | folio_put(folio); | |
211c17f5 CM |
4748 | goto again; |
4749 | } | |
df055afe | 4750 | if (!folio_test_uptodate(folio)) { |
39279cc3 | 4751 | ret = -EIO; |
89642229 | 4752 | goto out_unlock; |
39279cc3 CM |
4753 | } |
4754 | } | |
17b17fcd JB |
4755 | |
4756 | /* | |
4757 | * We unlock the page after the io is completed and then re-lock it | |
4758 | * above. release_folio() could have come in between that and cleared | |
cfbf07e2 | 4759 | * folio private, but left the page in the mapping. Set the page mapped |
17b17fcd JB |
4760 | * here to make sure it's properly set for the subpage stuff. |
4761 | */ | |
df055afe | 4762 | ret = set_folio_extent_mapped(folio); |
17b17fcd JB |
4763 | if (ret < 0) |
4764 | goto out_unlock; | |
4765 | ||
df055afe | 4766 | folio_wait_writeback(folio); |
e6dcd2dc | 4767 | |
570eb97b | 4768 | lock_extent(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc | 4769 | |
217f42eb | 4770 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4771 | if (ordered) { |
570eb97b | 4772 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
df055afe GR |
4773 | folio_unlock(folio); |
4774 | folio_put(folio); | |
36d45567 | 4775 | btrfs_start_ordered_extent(ordered); |
e6dcd2dc CM |
4776 | btrfs_put_ordered_extent(ordered); |
4777 | goto again; | |
4778 | } | |
4779 | ||
217f42eb | 4780 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
e182163d | 4781 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
bd015294 | 4782 | &cached_state); |
5d5e103a | 4783 | |
217f42eb | 4784 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 4785 | &cached_state); |
9ed74f2d | 4786 | if (ret) { |
570eb97b | 4787 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
9ed74f2d JB |
4788 | goto out_unlock; |
4789 | } | |
4790 | ||
9703fefe | 4791 | if (offset != blocksize) { |
2aaa6655 | 4792 | if (!len) |
9703fefe | 4793 | len = blocksize - offset; |
2aaa6655 | 4794 | if (front) |
df055afe GR |
4795 | folio_zero_range(folio, block_start - folio_pos(folio), |
4796 | offset); | |
2aaa6655 | 4797 | else |
df055afe GR |
4798 | folio_zero_range(folio, |
4799 | (block_start - folio_pos(folio)) + offset, | |
4800 | len); | |
e6dcd2dc | 4801 | } |
df055afe | 4802 | btrfs_folio_clear_checked(fs_info, folio, block_start, |
55151ea9 | 4803 | block_end + 1 - block_start); |
df055afe | 4804 | btrfs_folio_set_dirty(fs_info, folio, block_start, |
55151ea9 | 4805 | block_end + 1 - block_start); |
570eb97b | 4806 | unlock_extent(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4807 | |
6d4572a9 | 4808 | if (only_release_metadata) |
217f42eb | 4809 | set_extent_bit(&inode->io_tree, block_start, block_end, |
1d126800 | 4810 | EXTENT_NORESERVE, NULL); |
6d4572a9 | 4811 | |
89642229 | 4812 | out_unlock: |
6d4572a9 QW |
4813 | if (ret) { |
4814 | if (only_release_metadata) | |
217f42eb | 4815 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
6d4572a9 | 4816 | else |
217f42eb | 4817 | btrfs_delalloc_release_space(inode, data_reserved, |
6d4572a9 QW |
4818 | block_start, blocksize, true); |
4819 | } | |
217f42eb | 4820 | btrfs_delalloc_release_extents(inode, blocksize); |
df055afe GR |
4821 | folio_unlock(folio); |
4822 | folio_put(folio); | |
39279cc3 | 4823 | out: |
6d4572a9 | 4824 | if (only_release_metadata) |
217f42eb | 4825 | btrfs_check_nocow_unlock(inode); |
364ecf36 | 4826 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4827 | return ret; |
4828 | } | |
4829 | ||
0a325e62 | 4830 | static int maybe_insert_hole(struct btrfs_inode *inode, u64 offset, u64 len) |
16e7549f | 4831 | { |
0a325e62 | 4832 | struct btrfs_root *root = inode->root; |
a4ba6cc0 | 4833 | struct btrfs_fs_info *fs_info = root->fs_info; |
16e7549f | 4834 | struct btrfs_trans_handle *trans; |
5893dfb9 | 4835 | struct btrfs_drop_extents_args drop_args = { 0 }; |
16e7549f JB |
4836 | int ret; |
4837 | ||
4838 | /* | |
cceaa89f FM |
4839 | * If NO_HOLES is enabled, we don't need to do anything. |
4840 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() | |
4841 | * or btrfs_update_inode() will be called, which guarantee that the next | |
4842 | * fsync will know this inode was changed and needs to be logged. | |
16e7549f | 4843 | */ |
cceaa89f | 4844 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f | 4845 | return 0; |
16e7549f JB |
4846 | |
4847 | /* | |
4848 | * 1 - for the one we're dropping | |
4849 | * 1 - for the one we're adding | |
4850 | * 1 - for updating the inode. | |
4851 | */ | |
4852 | trans = btrfs_start_transaction(root, 3); | |
4853 | if (IS_ERR(trans)) | |
4854 | return PTR_ERR(trans); | |
4855 | ||
5893dfb9 FM |
4856 | drop_args.start = offset; |
4857 | drop_args.end = offset + len; | |
4858 | drop_args.drop_cache = true; | |
4859 | ||
a4ba6cc0 | 4860 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
16e7549f | 4861 | if (ret) { |
66642832 | 4862 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4863 | btrfs_end_transaction(trans); |
16e7549f JB |
4864 | return ret; |
4865 | } | |
4866 | ||
d1f68ba0 | 4867 | ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len); |
2766ff61 | 4868 | if (ret) { |
66642832 | 4869 | btrfs_abort_transaction(trans, ret); |
2766ff61 | 4870 | } else { |
a4ba6cc0 | 4871 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
8b9d0322 | 4872 | btrfs_update_inode(trans, inode); |
2766ff61 | 4873 | } |
3a45bb20 | 4874 | btrfs_end_transaction(trans); |
16e7549f JB |
4875 | return ret; |
4876 | } | |
4877 | ||
695a0d0d JB |
4878 | /* |
4879 | * This function puts in dummy file extents for the area we're creating a hole | |
4880 | * for. So if we are truncating this file to a larger size we need to insert | |
4881 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4882 | * the range between oldsize and size | |
4883 | */ | |
b06359a3 | 4884 | int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4885 | { |
b06359a3 NB |
4886 | struct btrfs_root *root = inode->root; |
4887 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4888 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a22285a6 | 4889 | struct extent_map *em = NULL; |
2ac55d41 | 4890 | struct extent_state *cached_state = NULL; |
0b246afa JM |
4891 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4892 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4893 | u64 last_byte; |
4894 | u64 cur_offset; | |
4895 | u64 hole_size; | |
9ed74f2d | 4896 | int err = 0; |
39279cc3 | 4897 | |
a71754fc | 4898 | /* |
9703fefe CR |
4899 | * If our size started in the middle of a block we need to zero out the |
4900 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4901 | * expose stale data. |
4902 | */ | |
b06359a3 | 4903 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4904 | if (err) |
4905 | return err; | |
4906 | ||
9036c102 YZ |
4907 | if (size <= hole_start) |
4908 | return 0; | |
4909 | ||
b06359a3 NB |
4910 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
4911 | &cached_state); | |
9036c102 YZ |
4912 | cur_offset = hole_start; |
4913 | while (1) { | |
8bab0a30 | 4914 | em = btrfs_get_extent(inode, NULL, cur_offset, block_end - cur_offset); |
79787eaa JM |
4915 | if (IS_ERR(em)) { |
4916 | err = PTR_ERR(em); | |
f2767956 | 4917 | em = NULL; |
79787eaa JM |
4918 | break; |
4919 | } | |
9036c102 | 4920 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4921 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4922 | hole_size = last_byte - cur_offset; |
4923 | ||
f86f7a75 | 4924 | if (!(em->flags & EXTENT_FLAG_PREALLOC)) { |
5dc562c5 | 4925 | struct extent_map *hole_em; |
9ed74f2d | 4926 | |
0a325e62 | 4927 | err = maybe_insert_hole(inode, cur_offset, hole_size); |
16e7549f | 4928 | if (err) |
3893e33b | 4929 | break; |
9ddc959e | 4930 | |
b06359a3 | 4931 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4932 | cur_offset, hole_size); |
4933 | if (err) | |
4934 | break; | |
4935 | ||
5dc562c5 JB |
4936 | hole_em = alloc_extent_map(); |
4937 | if (!hole_em) { | |
a1ba4c08 FM |
4938 | btrfs_drop_extent_map_range(inode, cur_offset, |
4939 | cur_offset + hole_size - 1, | |
4940 | false); | |
23e3337f | 4941 | btrfs_set_inode_full_sync(inode); |
5dc562c5 JB |
4942 | goto next; |
4943 | } | |
4944 | hole_em->start = cur_offset; | |
4945 | hole_em->len = hole_size; | |
4946 | hole_em->orig_start = cur_offset; | |
8082510e | 4947 | |
5dc562c5 JB |
4948 | hole_em->block_start = EXTENT_MAP_HOLE; |
4949 | hole_em->block_len = 0; | |
b4939680 | 4950 | hole_em->orig_block_len = 0; |
cc95bef6 | 4951 | hole_em->ram_bytes = hole_size; |
4a4f8fe2 | 4952 | hole_em->generation = btrfs_get_fs_generation(fs_info); |
8082510e | 4953 | |
a1ba4c08 | 4954 | err = btrfs_replace_extent_map_range(inode, hole_em, true); |
5dc562c5 | 4955 | free_extent_map(hole_em); |
9ddc959e | 4956 | } else { |
b06359a3 | 4957 | err = btrfs_inode_set_file_extent_range(inode, |
9ddc959e JB |
4958 | cur_offset, hole_size); |
4959 | if (err) | |
4960 | break; | |
9036c102 | 4961 | } |
16e7549f | 4962 | next: |
9036c102 | 4963 | free_extent_map(em); |
a22285a6 | 4964 | em = NULL; |
9036c102 | 4965 | cur_offset = last_byte; |
8082510e | 4966 | if (cur_offset >= block_end) |
9036c102 YZ |
4967 | break; |
4968 | } | |
a22285a6 | 4969 | free_extent_map(em); |
570eb97b | 4970 | unlock_extent(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4971 | return err; |
4972 | } | |
39279cc3 | 4973 | |
3972f260 | 4974 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4975 | { |
f4a2f4c5 MX |
4976 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4977 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4978 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4979 | loff_t newsize = attr->ia_size; |
4980 | int mask = attr->ia_valid; | |
8082510e YZ |
4981 | int ret; |
4982 | ||
3972f260 ES |
4983 | /* |
4984 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4985 | * special case where we need to update the times despite not having | |
4986 | * these flags set. For all other operations the VFS set these flags | |
4987 | * explicitly if it wants a timestamp update. | |
4988 | */ | |
dff6efc3 CH |
4989 | if (newsize != oldsize) { |
4990 | inode_inc_iversion(inode); | |
c1867eb3 | 4991 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) { |
b1c38a13 JL |
4992 | inode_set_mtime_to_ts(inode, |
4993 | inode_set_ctime_current(inode)); | |
c1867eb3 | 4994 | } |
dff6efc3 | 4995 | } |
3972f260 | 4996 | |
a41ad394 | 4997 | if (newsize > oldsize) { |
9ea24bbe | 4998 | /* |
ea14b57f | 4999 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5000 | * This is to ensure the snapshot captures a fully consistent |
5001 | * state of this file - if the snapshot captures this expanding | |
5002 | * truncation, it must capture all writes that happened before | |
5003 | * this truncation. | |
5004 | */ | |
dcc3eb96 | 5005 | btrfs_drew_write_lock(&root->snapshot_lock); |
b06359a3 | 5006 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
9ea24bbe | 5007 | if (ret) { |
dcc3eb96 | 5008 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 5009 | return ret; |
9ea24bbe | 5010 | } |
8082510e | 5011 | |
f4a2f4c5 | 5012 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5013 | if (IS_ERR(trans)) { |
dcc3eb96 | 5014 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 5015 | return PTR_ERR(trans); |
9ea24bbe | 5016 | } |
f4a2f4c5 MX |
5017 | |
5018 | i_size_write(inode, newsize); | |
76aea537 | 5019 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
27772b68 | 5020 | pagecache_isize_extended(inode, oldsize, newsize); |
8b9d0322 | 5021 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
dcc3eb96 | 5022 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 5023 | btrfs_end_transaction(trans); |
a41ad394 | 5024 | } else { |
41044b41 | 5025 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
24c0a722 NA |
5026 | |
5027 | if (btrfs_is_zoned(fs_info)) { | |
5028 | ret = btrfs_wait_ordered_range(inode, | |
5029 | ALIGN(newsize, fs_info->sectorsize), | |
5030 | (u64)-1); | |
5031 | if (ret) | |
5032 | return ret; | |
5033 | } | |
8082510e | 5034 | |
a41ad394 JB |
5035 | /* |
5036 | * We're truncating a file that used to have good data down to | |
1fd4033d NB |
5037 | * zero. Make sure any new writes to the file get on disk |
5038 | * on close. | |
a41ad394 JB |
5039 | */ |
5040 | if (newsize == 0) | |
1fd4033d | 5041 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
72ac3c0d | 5042 | &BTRFS_I(inode)->runtime_flags); |
8082510e | 5043 | |
a41ad394 | 5044 | truncate_setsize(inode, newsize); |
2e60a51e | 5045 | |
2e60a51e | 5046 | inode_dio_wait(inode); |
2e60a51e | 5047 | |
d9dcae67 | 5048 | ret = btrfs_truncate(BTRFS_I(inode), newsize == oldsize); |
7f4f6e0a JB |
5049 | if (ret && inode->i_nlink) { |
5050 | int err; | |
5051 | ||
5052 | /* | |
f7e9e8fc OS |
5053 | * Truncate failed, so fix up the in-memory size. We |
5054 | * adjusted disk_i_size down as we removed extents, so | |
5055 | * wait for disk_i_size to be stable and then update the | |
5056 | * in-memory size to match. | |
7f4f6e0a | 5057 | */ |
f7e9e8fc | 5058 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5059 | if (err) |
f7e9e8fc OS |
5060 | return err; |
5061 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5062 | } |
8082510e YZ |
5063 | } |
5064 | ||
a41ad394 | 5065 | return ret; |
8082510e YZ |
5066 | } |
5067 | ||
c1632a0f | 5068 | static int btrfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 5069 | struct iattr *attr) |
9036c102 | 5070 | { |
2b0143b5 | 5071 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5072 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5073 | int err; |
39279cc3 | 5074 | |
b83cc969 LZ |
5075 | if (btrfs_root_readonly(root)) |
5076 | return -EROFS; | |
5077 | ||
c1632a0f | 5078 | err = setattr_prepare(idmap, dentry, attr); |
9036c102 YZ |
5079 | if (err) |
5080 | return err; | |
2bf5a725 | 5081 | |
5a3f23d5 | 5082 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5083 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5084 | if (err) |
5085 | return err; | |
39279cc3 | 5086 | } |
9036c102 | 5087 | |
1025774c | 5088 | if (attr->ia_valid) { |
c1632a0f | 5089 | setattr_copy(idmap, inode, attr); |
0c4d2d95 | 5090 | inode_inc_iversion(inode); |
7152b425 | 5091 | err = btrfs_dirty_inode(BTRFS_I(inode)); |
1025774c | 5092 | |
22c44fe6 | 5093 | if (!err && attr->ia_valid & ATTR_MODE) |
13e83a49 | 5094 | err = posix_acl_chmod(idmap, dentry, inode->i_mode); |
1025774c | 5095 | } |
33268eaf | 5096 | |
39279cc3 CM |
5097 | return err; |
5098 | } | |
61295eb8 | 5099 | |
131e404a | 5100 | /* |
895586eb MWO |
5101 | * While truncating the inode pages during eviction, we get the VFS |
5102 | * calling btrfs_invalidate_folio() against each folio of the inode. This | |
5103 | * is slow because the calls to btrfs_invalidate_folio() result in a | |
570eb97b | 5104 | * huge amount of calls to lock_extent() and clear_extent_bit(), |
895586eb MWO |
5105 | * which keep merging and splitting extent_state structures over and over, |
5106 | * wasting lots of time. | |
131e404a | 5107 | * |
895586eb MWO |
5108 | * Therefore if the inode is being evicted, let btrfs_invalidate_folio() |
5109 | * skip all those expensive operations on a per folio basis and do only | |
5110 | * the ordered io finishing, while we release here the extent_map and | |
5111 | * extent_state structures, without the excessive merging and splitting. | |
131e404a FDBM |
5112 | */ |
5113 | static void evict_inode_truncate_pages(struct inode *inode) | |
5114 | { | |
5115 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
131e404a FDBM |
5116 | struct rb_node *node; |
5117 | ||
5118 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5119 | truncate_inode_pages_final(&inode->i_data); |
131e404a | 5120 | |
9c9d1b4f | 5121 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
131e404a | 5122 | |
6ca07097 FM |
5123 | /* |
5124 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
5125 | * We can have ongoing bios started by readahead that have |
5126 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
5127 | * still in progress (unlocked the pages in the bio but did not yet |
5128 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5129 | * ranges can still be locked and eviction started because before |
5130 | * submitting those bios, which are executed by a separate task (work | |
5131 | * queue kthread), inode references (inode->i_count) were not taken | |
5132 | * (which would be dropped in the end io callback of each bio). | |
5133 | * Therefore here we effectively end up waiting for those bios and | |
5134 | * anyone else holding locked ranges without having bumped the inode's | |
5135 | * reference count - if we don't do it, when they access the inode's | |
5136 | * io_tree to unlock a range it may be too late, leading to an | |
5137 | * use-after-free issue. | |
5138 | */ | |
131e404a FDBM |
5139 | spin_lock(&io_tree->lock); |
5140 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5141 | struct extent_state *state; | |
5142 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5143 | u64 start; |
5144 | u64 end; | |
421f0922 | 5145 | unsigned state_flags; |
131e404a FDBM |
5146 | |
5147 | node = rb_first(&io_tree->state); | |
5148 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5149 | start = state->start; |
5150 | end = state->end; | |
421f0922 | 5151 | state_flags = state->state; |
131e404a FDBM |
5152 | spin_unlock(&io_tree->lock); |
5153 | ||
570eb97b | 5154 | lock_extent(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5155 | |
5156 | /* | |
5157 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5158 | * and its reserved space won't be freed by delayed_ref. | |
5159 | * So we need to free its reserved space here. | |
895586eb | 5160 | * (Refer to comment in btrfs_invalidate_folio, case 2) |
b9d0b389 QW |
5161 | * |
5162 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5163 | */ | |
421f0922 | 5164 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f | 5165 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
9e65bfca | 5166 | end - start + 1, NULL); |
b9d0b389 | 5167 | |
6ca07097 | 5168 | clear_extent_bit(io_tree, start, end, |
bd015294 | 5169 | EXTENT_CLEAR_ALL_BITS | EXTENT_DO_ACCOUNTING, |
e182163d | 5170 | &cached_state); |
131e404a | 5171 | |
7064dd5c | 5172 | cond_resched(); |
131e404a FDBM |
5173 | spin_lock(&io_tree->lock); |
5174 | } | |
5175 | spin_unlock(&io_tree->lock); | |
5176 | } | |
5177 | ||
4b9d7b59 | 5178 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5179 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5180 | { |
5181 | struct btrfs_fs_info *fs_info = root->fs_info; | |
d3984c90 | 5182 | struct btrfs_trans_handle *trans; |
b13d57db | 5183 | u64 delayed_refs_extra = btrfs_calc_delayed_ref_bytes(fs_info, 1); |
d3984c90 | 5184 | int ret; |
4b9d7b59 | 5185 | |
d3984c90 JB |
5186 | /* |
5187 | * Eviction should be taking place at some place safe because of our | |
5188 | * delayed iputs. However the normal flushing code will run delayed | |
5189 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5190 | * | |
5191 | * We reserve the delayed_refs_extra here again because we can't use | |
5192 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5193 | * above. We reserve our extra bit here because we generate a ton of | |
5194 | * delayed refs activity by truncating. | |
5195 | * | |
ee6adbfd JB |
5196 | * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can, |
5197 | * if we fail to make this reservation we can re-try without the | |
5198 | * delayed_refs_extra so we can make some forward progress. | |
d3984c90 | 5199 | */ |
9270501c | 5200 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, |
d3984c90 JB |
5201 | BTRFS_RESERVE_FLUSH_EVICT); |
5202 | if (ret) { | |
9270501c | 5203 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, |
ee6adbfd JB |
5204 | BTRFS_RESERVE_FLUSH_EVICT); |
5205 | if (ret) { | |
d3984c90 JB |
5206 | btrfs_warn(fs_info, |
5207 | "could not allocate space for delete; will truncate on mount"); | |
5208 | return ERR_PTR(-ENOSPC); | |
5209 | } | |
5210 | delayed_refs_extra = 0; | |
5211 | } | |
4b9d7b59 | 5212 | |
d3984c90 JB |
5213 | trans = btrfs_join_transaction(root); |
5214 | if (IS_ERR(trans)) | |
5215 | return trans; | |
5216 | ||
5217 | if (delayed_refs_extra) { | |
5218 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5219 | trans->bytes_reserved = delayed_refs_extra; | |
5220 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
4e0527de | 5221 | delayed_refs_extra, true); |
4b9d7b59 | 5222 | } |
d3984c90 | 5223 | return trans; |
4b9d7b59 OS |
5224 | } |
5225 | ||
bd555975 | 5226 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5227 | { |
41044b41 | 5228 | struct btrfs_fs_info *fs_info; |
39279cc3 CM |
5229 | struct btrfs_trans_handle *trans; |
5230 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
b7b1167c | 5231 | struct btrfs_block_rsv *rsv = NULL; |
39279cc3 CM |
5232 | int ret; |
5233 | ||
1abe9b8a | 5234 | trace_btrfs_inode_evict(inode); |
5235 | ||
3d48d981 | 5236 | if (!root) { |
14605409 | 5237 | fsverity_cleanup_inode(inode); |
e8f1bc14 | 5238 | clear_inode(inode); |
3d48d981 NB |
5239 | return; |
5240 | } | |
5241 | ||
41044b41 | 5242 | fs_info = inode_to_fs_info(inode); |
131e404a FDBM |
5243 | evict_inode_truncate_pages(inode); |
5244 | ||
69e9c6c6 SB |
5245 | if (inode->i_nlink && |
5246 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5247 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5248 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
b7b1167c | 5249 | goto out; |
bd555975 | 5250 | |
27919067 | 5251 | if (is_bad_inode(inode)) |
b7b1167c | 5252 | goto out; |
5f39d397 | 5253 | |
7b40b695 | 5254 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
b7b1167c | 5255 | goto out; |
c71bf099 | 5256 | |
76dda93c | 5257 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5258 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5259 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
b7b1167c | 5260 | goto out; |
76dda93c YZ |
5261 | } |
5262 | ||
2adc75d6 JB |
5263 | /* |
5264 | * This makes sure the inode item in tree is uptodate and the space for | |
5265 | * the inode update is released. | |
5266 | */ | |
aa79021f | 5267 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5268 | if (ret) |
b7b1167c | 5269 | goto out; |
0e8c36a9 | 5270 | |
2adc75d6 JB |
5271 | /* |
5272 | * This drops any pending insert or delete operations we have for this | |
5273 | * inode. We could have a delayed dir index deletion queued up, but | |
5274 | * we're removing the inode completely so that'll be taken care of in | |
5275 | * the truncate. | |
5276 | */ | |
5277 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); | |
5278 | ||
2ff7e61e | 5279 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5280 | if (!rsv) |
b7b1167c | 5281 | goto out; |
2bd36e7b | 5282 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
710d5921 | 5283 | rsv->failfast = true; |
4289a667 | 5284 | |
6ef06d27 | 5285 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5286 | |
8082510e | 5287 | while (1) { |
d9ac19c3 | 5288 | struct btrfs_truncate_control control = { |
71d18b53 | 5289 | .inode = BTRFS_I(inode), |
487e81d2 | 5290 | .ino = btrfs_ino(BTRFS_I(inode)), |
d9ac19c3 JB |
5291 | .new_size = 0, |
5292 | .min_type = 0, | |
5293 | }; | |
5294 | ||
ad80cf50 | 5295 | trans = evict_refill_and_join(root, rsv); |
27919067 | 5296 | if (IS_ERR(trans)) |
b7b1167c | 5297 | goto out; |
7b128766 | 5298 | |
4289a667 JB |
5299 | trans->block_rsv = rsv; |
5300 | ||
71d18b53 | 5301 | ret = btrfs_truncate_inode_items(trans, root, &control); |
27919067 OS |
5302 | trans->block_rsv = &fs_info->trans_block_rsv; |
5303 | btrfs_end_transaction(trans); | |
afa4b0af FM |
5304 | /* |
5305 | * We have not added new delayed items for our inode after we | |
5306 | * have flushed its delayed items, so no need to throttle on | |
5307 | * delayed items. However we have modified extent buffers. | |
5308 | */ | |
5309 | btrfs_btree_balance_dirty_nodelay(fs_info); | |
27919067 | 5310 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
b7b1167c | 5311 | goto out; |
27919067 | 5312 | else if (!ret) |
8082510e | 5313 | break; |
8082510e | 5314 | } |
5f39d397 | 5315 | |
4ef31a45 | 5316 | /* |
27919067 OS |
5317 | * Errors here aren't a big deal, it just means we leave orphan items in |
5318 | * the tree. They will be cleaned up on the next mount. If the inode | |
5319 | * number gets reused, cleanup deletes the orphan item without doing | |
5320 | * anything, and unlink reuses the existing orphan item. | |
5321 | * | |
5322 | * If it turns out that we are dropping too many of these, we might want | |
5323 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5324 | */ |
ad80cf50 | 5325 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5326 | if (!IS_ERR(trans)) { |
5327 | trans->block_rsv = rsv; | |
5328 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5329 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5330 | btrfs_end_transaction(trans); | |
5331 | } | |
54aa1f4d | 5332 | |
b7b1167c | 5333 | out: |
27919067 | 5334 | btrfs_free_block_rsv(fs_info, rsv); |
27919067 OS |
5335 | /* |
5336 | * If we didn't successfully delete, the orphan item will still be in | |
5337 | * the tree and we'll retry on the next mount. Again, we might also want | |
5338 | * to retry these periodically in the future. | |
5339 | */ | |
f48d1cf5 | 5340 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
14605409 | 5341 | fsverity_cleanup_inode(inode); |
dbd5768f | 5342 | clear_inode(inode); |
39279cc3 CM |
5343 | } |
5344 | ||
5345 | /* | |
6bf9e4bd QW |
5346 | * Return the key found in the dir entry in the location pointer, fill @type |
5347 | * with BTRFS_FT_*, and return 0. | |
5348 | * | |
005d6712 SY |
5349 | * If no dir entries were found, returns -ENOENT. |
5350 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 | 5351 | */ |
d1de429b | 5352 | static int btrfs_inode_by_name(struct btrfs_inode *dir, struct dentry *dentry, |
6bf9e4bd | 5353 | struct btrfs_key *location, u8 *type) |
39279cc3 | 5354 | { |
39279cc3 CM |
5355 | struct btrfs_dir_item *di; |
5356 | struct btrfs_path *path; | |
d1de429b | 5357 | struct btrfs_root *root = dir->root; |
0d9f7f3e | 5358 | int ret = 0; |
ab3c5c18 | 5359 | struct fscrypt_name fname; |
39279cc3 CM |
5360 | |
5361 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5362 | if (!path) |
5363 | return -ENOMEM; | |
3954401f | 5364 | |
d1de429b | 5365 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); |
10a8857a | 5366 | if (ret < 0) |
ab3c5c18 | 5367 | goto out; |
10a8857a STD |
5368 | /* |
5369 | * fscrypt_setup_filename() should never return a positive value, but | |
5370 | * gcc on sparc/parisc thinks it can, so assert that doesn't happen. | |
5371 | */ | |
5372 | ASSERT(ret == 0); | |
ab3c5c18 | 5373 | |
ab3c5c18 STD |
5374 | /* This needs to handle no-key deletions later on */ |
5375 | ||
d1de429b | 5376 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), |
6db75318 | 5377 | &fname.disk_name, 0); |
3cf5068f LB |
5378 | if (IS_ERR_OR_NULL(di)) { |
5379 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5380 | goto out; |
5381 | } | |
d397712b | 5382 | |
5f39d397 | 5383 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5384 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5385 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5386 | ret = -EUCLEAN; |
56a0e706 LB |
5387 | btrfs_warn(root->fs_info, |
5388 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
d1de429b | 5389 | __func__, fname.disk_name.name, btrfs_ino(dir), |
56a0e706 | 5390 | location->objectid, location->type, location->offset); |
56a0e706 | 5391 | } |
6bf9e4bd | 5392 | if (!ret) |
94a48aef | 5393 | *type = btrfs_dir_ftype(path->nodes[0], di); |
39279cc3 | 5394 | out: |
ab3c5c18 | 5395 | fscrypt_free_filename(&fname); |
39279cc3 CM |
5396 | btrfs_free_path(path); |
5397 | return ret; | |
5398 | } | |
5399 | ||
5400 | /* | |
5401 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5402 | * needs to be changed to reflect the root directory of the tree root. This | |
5403 | * is kind of like crossing a mount point. | |
5404 | */ | |
2ff7e61e | 5405 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
3c1b1c4c | 5406 | struct btrfs_inode *dir, |
4df27c4d YZ |
5407 | struct dentry *dentry, |
5408 | struct btrfs_key *location, | |
5409 | struct btrfs_root **sub_root) | |
39279cc3 | 5410 | { |
4df27c4d YZ |
5411 | struct btrfs_path *path; |
5412 | struct btrfs_root *new_root; | |
5413 | struct btrfs_root_ref *ref; | |
5414 | struct extent_buffer *leaf; | |
1d4c08e0 | 5415 | struct btrfs_key key; |
4df27c4d YZ |
5416 | int ret; |
5417 | int err = 0; | |
ab3c5c18 | 5418 | struct fscrypt_name fname; |
ab3c5c18 | 5419 | |
3c1b1c4c | 5420 | ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 0, &fname); |
ab3c5c18 STD |
5421 | if (ret) |
5422 | return ret; | |
39279cc3 | 5423 | |
4df27c4d YZ |
5424 | path = btrfs_alloc_path(); |
5425 | if (!path) { | |
5426 | err = -ENOMEM; | |
5427 | goto out; | |
5428 | } | |
39279cc3 | 5429 | |
4df27c4d | 5430 | err = -ENOENT; |
3c1b1c4c | 5431 | key.objectid = dir->root->root_key.objectid; |
1d4c08e0 DS |
5432 | key.type = BTRFS_ROOT_REF_KEY; |
5433 | key.offset = location->objectid; | |
5434 | ||
0b246afa | 5435 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5436 | if (ret) { |
5437 | if (ret < 0) | |
5438 | err = ret; | |
5439 | goto out; | |
5440 | } | |
39279cc3 | 5441 | |
4df27c4d YZ |
5442 | leaf = path->nodes[0]; |
5443 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
3c1b1c4c | 5444 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
6db75318 | 5445 | btrfs_root_ref_name_len(leaf, ref) != fname.disk_name.len) |
4df27c4d | 5446 | goto out; |
39279cc3 | 5447 | |
6db75318 STD |
5448 | ret = memcmp_extent_buffer(leaf, fname.disk_name.name, |
5449 | (unsigned long)(ref + 1), fname.disk_name.len); | |
4df27c4d YZ |
5450 | if (ret) |
5451 | goto out; | |
5452 | ||
b3b4aa74 | 5453 | btrfs_release_path(path); |
4df27c4d | 5454 | |
56e9357a | 5455 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5456 | if (IS_ERR(new_root)) { |
5457 | err = PTR_ERR(new_root); | |
5458 | goto out; | |
5459 | } | |
5460 | ||
4df27c4d YZ |
5461 | *sub_root = new_root; |
5462 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5463 | location->type = BTRFS_INODE_ITEM_KEY; | |
5464 | location->offset = 0; | |
5465 | err = 0; | |
5466 | out: | |
5467 | btrfs_free_path(path); | |
ab3c5c18 | 5468 | fscrypt_free_filename(&fname); |
4df27c4d | 5469 | return err; |
39279cc3 CM |
5470 | } |
5471 | ||
4c45a4f4 | 5472 | static void inode_tree_add(struct btrfs_inode *inode) |
5d4f98a2 | 5473 | { |
4c45a4f4 | 5474 | struct btrfs_root *root = inode->root; |
5d4f98a2 | 5475 | struct btrfs_inode *entry; |
03e860bd NP |
5476 | struct rb_node **p; |
5477 | struct rb_node *parent; | |
4c45a4f4 DS |
5478 | struct rb_node *new = &inode->rb_node; |
5479 | u64 ino = btrfs_ino(inode); | |
5d4f98a2 | 5480 | |
4c45a4f4 | 5481 | if (inode_unhashed(&inode->vfs_inode)) |
76dda93c | 5482 | return; |
e1409cef | 5483 | parent = NULL; |
5d4f98a2 | 5484 | spin_lock(&root->inode_lock); |
e1409cef | 5485 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5486 | while (*p) { |
5487 | parent = *p; | |
5488 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5489 | ||
37508515 | 5490 | if (ino < btrfs_ino(entry)) |
03e860bd | 5491 | p = &parent->rb_left; |
37508515 | 5492 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5493 | p = &parent->rb_right; |
5d4f98a2 YZ |
5494 | else { |
5495 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5496 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5497 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5498 | RB_CLEAR_NODE(parent); |
5499 | spin_unlock(&root->inode_lock); | |
cef21937 | 5500 | return; |
5d4f98a2 YZ |
5501 | } |
5502 | } | |
cef21937 FDBM |
5503 | rb_link_node(new, parent, p); |
5504 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5505 | spin_unlock(&root->inode_lock); |
5506 | } | |
5507 | ||
b79b7249 | 5508 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5509 | { |
b79b7249 | 5510 | struct btrfs_root *root = inode->root; |
76dda93c | 5511 | int empty = 0; |
5d4f98a2 | 5512 | |
03e860bd | 5513 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5514 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5515 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5516 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5517 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5518 | } |
03e860bd | 5519 | spin_unlock(&root->inode_lock); |
76dda93c | 5520 | |
69e9c6c6 | 5521 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5522 | spin_lock(&root->inode_lock); |
5523 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5524 | spin_unlock(&root->inode_lock); | |
5525 | if (empty) | |
5526 | btrfs_add_dead_root(root); | |
5527 | } | |
5528 | } | |
5529 | ||
5d4f98a2 | 5530 | |
e02119d5 CM |
5531 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5532 | { | |
5533 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5534 | |
5535 | inode->i_ino = args->ino; | |
5536 | BTRFS_I(inode)->location.objectid = args->ino; | |
5537 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5538 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5539 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5540 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
9b9b8854 JB |
5541 | |
5542 | if (args->root && args->root == args->root->fs_info->tree_root && | |
5543 | args->ino != BTRFS_BTREE_INODE_OBJECTID) | |
5544 | set_bit(BTRFS_INODE_FREE_SPACE_INODE, | |
5545 | &BTRFS_I(inode)->runtime_flags); | |
39279cc3 CM |
5546 | return 0; |
5547 | } | |
5548 | ||
5549 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5550 | { | |
5551 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5552 | |
5553 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5554 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5555 | } |
5556 | ||
0202e83f | 5557 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5558 | struct btrfs_root *root) |
39279cc3 CM |
5559 | { |
5560 | struct inode *inode; | |
5561 | struct btrfs_iget_args args; | |
0202e83f | 5562 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5563 | |
0202e83f | 5564 | args.ino = ino; |
39279cc3 CM |
5565 | args.root = root; |
5566 | ||
778ba82b | 5567 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5568 | btrfs_init_locked_inode, |
5569 | (void *)&args); | |
5570 | return inode; | |
5571 | } | |
5572 | ||
4c66e0d4 | 5573 | /* |
0202e83f | 5574 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5575 | * Path can be preallocated to prevent recursing back to iget through |
5576 | * allocator. NULL is also valid but may require an additional allocation | |
5577 | * later. | |
1a54ef8c | 5578 | */ |
0202e83f | 5579 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5580 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5581 | { |
5582 | struct inode *inode; | |
5583 | ||
0202e83f | 5584 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5585 | if (!inode) |
5d4f98a2 | 5586 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5587 | |
5588 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5589 | int ret; |
5590 | ||
4222ea71 | 5591 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5592 | if (!ret) { |
4c45a4f4 | 5593 | inode_tree_add(BTRFS_I(inode)); |
1748f843 | 5594 | unlock_new_inode(inode); |
1748f843 | 5595 | } else { |
f5b3a417 AV |
5596 | iget_failed(inode); |
5597 | /* | |
5598 | * ret > 0 can come from btrfs_search_slot called by | |
5599 | * btrfs_read_locked_inode, this means the inode item | |
5600 | * was not found. | |
5601 | */ | |
5602 | if (ret > 0) | |
5603 | ret = -ENOENT; | |
5604 | inode = ERR_PTR(ret); | |
1748f843 MF |
5605 | } |
5606 | } | |
5607 | ||
1a54ef8c BR |
5608 | return inode; |
5609 | } | |
5610 | ||
0202e83f | 5611 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5612 | { |
0202e83f | 5613 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5614 | } |
5615 | ||
94628ad9 | 5616 | static struct inode *new_simple_dir(struct inode *dir, |
4df27c4d YZ |
5617 | struct btrfs_key *key, |
5618 | struct btrfs_root *root) | |
5619 | { | |
d5acbc60 | 5620 | struct timespec64 ts; |
94628ad9 | 5621 | struct inode *inode = new_inode(dir->i_sb); |
4df27c4d YZ |
5622 | |
5623 | if (!inode) | |
5624 | return ERR_PTR(-ENOMEM); | |
5625 | ||
5c8fd99f | 5626 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5627 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5628 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5629 | |
5630 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5631 | /* |
5632 | * We only need lookup, the rest is read-only and there's no inode | |
5633 | * associated with the dentry | |
5634 | */ | |
5635 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5636 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5637 | inode->i_fop = &simple_dir_operations; |
5638 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
d5acbc60 LT |
5639 | |
5640 | ts = inode_set_ctime_current(inode); | |
5641 | inode_set_mtime_to_ts(inode, ts); | |
b1c38a13 | 5642 | inode_set_atime_to_ts(inode, inode_get_atime(dir)); |
d5acbc60 LT |
5643 | BTRFS_I(inode)->i_otime_sec = ts.tv_sec; |
5644 | BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec; | |
5645 | ||
94628ad9 LT |
5646 | inode->i_uid = dir->i_uid; |
5647 | inode->i_gid = dir->i_gid; | |
4df27c4d YZ |
5648 | |
5649 | return inode; | |
5650 | } | |
5651 | ||
a55e65b8 DS |
5652 | static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN); |
5653 | static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE); | |
5654 | static_assert(BTRFS_FT_DIR == FT_DIR); | |
5655 | static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV); | |
5656 | static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV); | |
5657 | static_assert(BTRFS_FT_FIFO == FT_FIFO); | |
5658 | static_assert(BTRFS_FT_SOCK == FT_SOCK); | |
5659 | static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK); | |
5660 | ||
6bf9e4bd QW |
5661 | static inline u8 btrfs_inode_type(struct inode *inode) |
5662 | { | |
6bf9e4bd QW |
5663 | return fs_umode_to_ftype(inode->i_mode); |
5664 | } | |
5665 | ||
3de4586c | 5666 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5667 | { |
41044b41 | 5668 | struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); |
d397712b | 5669 | struct inode *inode; |
4df27c4d | 5670 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5671 | struct btrfs_root *sub_root = root; |
5672 | struct btrfs_key location; | |
6bf9e4bd | 5673 | u8 di_type = 0; |
b4aff1f8 | 5674 | int ret = 0; |
39279cc3 CM |
5675 | |
5676 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5677 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5678 | |
d1de429b | 5679 | ret = btrfs_inode_by_name(BTRFS_I(dir), dentry, &location, &di_type); |
39279cc3 CM |
5680 | if (ret < 0) |
5681 | return ERR_PTR(ret); | |
5f39d397 | 5682 | |
4df27c4d | 5683 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5684 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5685 | if (IS_ERR(inode)) |
5686 | return inode; | |
5687 | ||
5688 | /* Do extra check against inode mode with di_type */ | |
5689 | if (btrfs_inode_type(inode) != di_type) { | |
5690 | btrfs_crit(fs_info, | |
5691 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5692 | inode->i_mode, btrfs_inode_type(inode), | |
5693 | di_type); | |
5694 | iput(inode); | |
5695 | return ERR_PTR(-EUCLEAN); | |
5696 | } | |
4df27c4d YZ |
5697 | return inode; |
5698 | } | |
5699 | ||
3c1b1c4c | 5700 | ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry, |
4df27c4d YZ |
5701 | &location, &sub_root); |
5702 | if (ret < 0) { | |
5703 | if (ret != -ENOENT) | |
5704 | inode = ERR_PTR(ret); | |
5705 | else | |
94628ad9 | 5706 | inode = new_simple_dir(dir, &location, root); |
4df27c4d | 5707 | } else { |
0202e83f | 5708 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
00246528 | 5709 | btrfs_put_root(sub_root); |
76dda93c | 5710 | |
fc8b235f NB |
5711 | if (IS_ERR(inode)) |
5712 | return inode; | |
5713 | ||
0b246afa | 5714 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5715 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5716 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5717 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5718 | if (ret) { |
5719 | iput(inode); | |
66b4ffd1 | 5720 | inode = ERR_PTR(ret); |
01cd3367 | 5721 | } |
c71bf099 YZ |
5722 | } |
5723 | ||
3de4586c CM |
5724 | return inode; |
5725 | } | |
5726 | ||
fe15ce44 | 5727 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5728 | { |
5729 | struct btrfs_root *root; | |
2b0143b5 | 5730 | struct inode *inode = d_inode(dentry); |
76dda93c | 5731 | |
848cce0d | 5732 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5733 | inode = d_inode(dentry->d_parent); |
76dda93c | 5734 | |
848cce0d LZ |
5735 | if (inode) { |
5736 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5737 | if (btrfs_root_refs(&root->root_item) == 0) |
5738 | return 1; | |
848cce0d | 5739 | |
4a0cc7ca | 5740 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5741 | return 1; |
efefb143 | 5742 | } |
76dda93c YZ |
5743 | return 0; |
5744 | } | |
5745 | ||
3de4586c | 5746 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5747 | unsigned int flags) |
3de4586c | 5748 | { |
3837d208 | 5749 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5750 | |
3837d208 AV |
5751 | if (inode == ERR_PTR(-ENOENT)) |
5752 | inode = NULL; | |
41d28bca | 5753 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5754 | } |
5755 | ||
9b378f6a FM |
5756 | /* |
5757 | * Find the highest existing sequence number in a directory and then set the | |
5758 | * in-memory index_cnt variable to the first free sequence number. | |
5759 | */ | |
5760 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) | |
5761 | { | |
5762 | struct btrfs_root *root = inode->root; | |
5763 | struct btrfs_key key, found_key; | |
5764 | struct btrfs_path *path; | |
5765 | struct extent_buffer *leaf; | |
5766 | int ret; | |
5767 | ||
5768 | key.objectid = btrfs_ino(inode); | |
5769 | key.type = BTRFS_DIR_INDEX_KEY; | |
5770 | key.offset = (u64)-1; | |
5771 | ||
5772 | path = btrfs_alloc_path(); | |
5773 | if (!path) | |
5774 | return -ENOMEM; | |
5775 | ||
5776 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5777 | if (ret < 0) | |
5778 | goto out; | |
5779 | /* FIXME: we should be able to handle this */ | |
5780 | if (ret == 0) | |
5781 | goto out; | |
5782 | ret = 0; | |
5783 | ||
5784 | if (path->slots[0] == 0) { | |
5785 | inode->index_cnt = BTRFS_DIR_START_INDEX; | |
5786 | goto out; | |
5787 | } | |
5788 | ||
5789 | path->slots[0]--; | |
5790 | ||
5791 | leaf = path->nodes[0]; | |
5792 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5793 | ||
5794 | if (found_key.objectid != btrfs_ino(inode) || | |
5795 | found_key.type != BTRFS_DIR_INDEX_KEY) { | |
5796 | inode->index_cnt = BTRFS_DIR_START_INDEX; | |
5797 | goto out; | |
5798 | } | |
5799 | ||
5800 | inode->index_cnt = found_key.offset + 1; | |
5801 | out: | |
5802 | btrfs_free_path(path); | |
5803 | return ret; | |
5804 | } | |
5805 | ||
5806 | static int btrfs_get_dir_last_index(struct btrfs_inode *dir, u64 *index) | |
5807 | { | |
8e7f82de | 5808 | int ret = 0; |
9b378f6a | 5809 | |
8e7f82de FM |
5810 | btrfs_inode_lock(dir, 0); |
5811 | if (dir->index_cnt == (u64)-1) { | |
9b378f6a FM |
5812 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5813 | if (ret) { | |
5814 | ret = btrfs_set_inode_index_count(dir); | |
5815 | if (ret) | |
8e7f82de | 5816 | goto out; |
9b378f6a FM |
5817 | } |
5818 | } | |
5819 | ||
35795036 FM |
5820 | /* index_cnt is the index number of next new entry, so decrement it. */ |
5821 | *index = dir->index_cnt - 1; | |
8e7f82de FM |
5822 | out: |
5823 | btrfs_inode_unlock(dir, 0); | |
9b378f6a | 5824 | |
8e7f82de | 5825 | return ret; |
9b378f6a FM |
5826 | } |
5827 | ||
23b5ec74 JB |
5828 | /* |
5829 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5830 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5831 | * our information into that, and then dir_emit from the buffer. This is | |
5832 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5833 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5834 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5835 | * tree lock. | |
5836 | */ | |
5837 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5838 | { | |
5839 | struct btrfs_file_private *private; | |
9b378f6a FM |
5840 | u64 last_index; |
5841 | int ret; | |
5842 | ||
5843 | ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index); | |
5844 | if (ret) | |
5845 | return ret; | |
23b5ec74 JB |
5846 | |
5847 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5848 | if (!private) | |
5849 | return -ENOMEM; | |
9b378f6a | 5850 | private->last_index = last_index; |
23b5ec74 JB |
5851 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
5852 | if (!private->filldir_buf) { | |
5853 | kfree(private); | |
5854 | return -ENOMEM; | |
5855 | } | |
5856 | file->private_data = private; | |
5857 | return 0; | |
5858 | } | |
5859 | ||
e60aa5da FM |
5860 | static loff_t btrfs_dir_llseek(struct file *file, loff_t offset, int whence) |
5861 | { | |
5862 | struct btrfs_file_private *private = file->private_data; | |
5863 | int ret; | |
5864 | ||
5865 | ret = btrfs_get_dir_last_index(BTRFS_I(file_inode(file)), | |
5866 | &private->last_index); | |
5867 | if (ret) | |
5868 | return ret; | |
5869 | ||
5870 | return generic_file_llseek(file, offset, whence); | |
5871 | } | |
5872 | ||
23b5ec74 JB |
5873 | struct dir_entry { |
5874 | u64 ino; | |
5875 | u64 offset; | |
5876 | unsigned type; | |
5877 | int name_len; | |
5878 | }; | |
5879 | ||
5880 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5881 | { | |
5882 | while (entries--) { | |
5883 | struct dir_entry *entry = addr; | |
5884 | char *name = (char *)(entry + 1); | |
5885 | ||
92d32170 DS |
5886 | ctx->pos = get_unaligned(&entry->offset); |
5887 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5888 | get_unaligned(&entry->ino), | |
5889 | get_unaligned(&entry->type))) | |
23b5ec74 | 5890 | return 1; |
92d32170 DS |
5891 | addr += sizeof(struct dir_entry) + |
5892 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5893 | ctx->pos++; |
5894 | } | |
5895 | return 0; | |
5896 | } | |
5897 | ||
9cdda8d3 | 5898 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5899 | { |
9cdda8d3 | 5900 | struct inode *inode = file_inode(file); |
39279cc3 | 5901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5902 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5903 | struct btrfs_dir_item *di; |
5904 | struct btrfs_key key; | |
5f39d397 | 5905 | struct btrfs_key found_key; |
39279cc3 | 5906 | struct btrfs_path *path; |
23b5ec74 | 5907 | void *addr; |
84af994b RJ |
5908 | LIST_HEAD(ins_list); |
5909 | LIST_HEAD(del_list); | |
39279cc3 | 5910 | int ret; |
5f39d397 CM |
5911 | char *name_ptr; |
5912 | int name_len; | |
23b5ec74 JB |
5913 | int entries = 0; |
5914 | int total_len = 0; | |
02dbfc99 | 5915 | bool put = false; |
c2951f32 | 5916 | struct btrfs_key location; |
5f39d397 | 5917 | |
9cdda8d3 AV |
5918 | if (!dir_emit_dots(file, ctx)) |
5919 | return 0; | |
5920 | ||
49593bfa | 5921 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5922 | if (!path) |
5923 | return -ENOMEM; | |
ff5714cc | 5924 | |
23b5ec74 | 5925 | addr = private->filldir_buf; |
e4058b54 | 5926 | path->reada = READA_FORWARD; |
49593bfa | 5927 | |
9b378f6a FM |
5928 | put = btrfs_readdir_get_delayed_items(inode, private->last_index, |
5929 | &ins_list, &del_list); | |
16cdcec7 | 5930 | |
23b5ec74 | 5931 | again: |
c2951f32 | 5932 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5933 | key.offset = ctx->pos; |
4a0cc7ca | 5934 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5935 | |
a8ce68fd | 5936 | btrfs_for_each_slot(root, &key, &found_key, path, ret) { |
23b5ec74 | 5937 | struct dir_entry *entry; |
a8ce68fd | 5938 | struct extent_buffer *leaf = path->nodes[0]; |
94a48aef | 5939 | u8 ftype; |
5f39d397 CM |
5940 | |
5941 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5942 | break; |
c2951f32 | 5943 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5944 | break; |
9cdda8d3 | 5945 | if (found_key.offset < ctx->pos) |
a8ce68fd | 5946 | continue; |
9b378f6a FM |
5947 | if (found_key.offset > private->last_index) |
5948 | break; | |
c2951f32 | 5949 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
a8ce68fd GN |
5950 | continue; |
5951 | di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); | |
c2951f32 | 5952 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5953 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5954 | PAGE_SIZE) { | |
5955 | btrfs_release_path(path); | |
5956 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5957 | if (ret) | |
5958 | goto nopos; | |
5959 | addr = private->filldir_buf; | |
5960 | entries = 0; | |
5961 | total_len = 0; | |
5962 | goto again; | |
c2951f32 | 5963 | } |
23b5ec74 | 5964 | |
94a48aef | 5965 | ftype = btrfs_dir_flags_to_ftype(btrfs_dir_flags(leaf, di)); |
23b5ec74 | 5966 | entry = addr; |
23b5ec74 | 5967 | name_ptr = (char *)(entry + 1); |
94a48aef OS |
5968 | read_extent_buffer(leaf, name_ptr, |
5969 | (unsigned long)(di + 1), name_len); | |
5970 | put_unaligned(name_len, &entry->name_len); | |
5971 | put_unaligned(fs_ftype_to_dtype(ftype), &entry->type); | |
c2951f32 | 5972 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5973 | put_unaligned(location.objectid, &entry->ino); |
5974 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5975 | entries++; |
5976 | addr += sizeof(struct dir_entry) + name_len; | |
5977 | total_len += sizeof(struct dir_entry) + name_len; | |
39279cc3 | 5978 | } |
a8ce68fd GN |
5979 | /* Catch error encountered during iteration */ |
5980 | if (ret < 0) | |
5981 | goto err; | |
5982 | ||
23b5ec74 JB |
5983 | btrfs_release_path(path); |
5984 | ||
5985 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5986 | if (ret) | |
5987 | goto nopos; | |
49593bfa | 5988 | |
d2fbb2b5 | 5989 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5990 | if (ret) |
bc4ef759 DS |
5991 | goto nopos; |
5992 | ||
db62efbb ZB |
5993 | /* |
5994 | * Stop new entries from being returned after we return the last | |
5995 | * entry. | |
5996 | * | |
5997 | * New directory entries are assigned a strictly increasing | |
5998 | * offset. This means that new entries created during readdir | |
5999 | * are *guaranteed* to be seen in the future by that readdir. | |
6000 | * This has broken buggy programs which operate on names as | |
6001 | * they're returned by readdir. Until we re-use freed offsets | |
6002 | * we have this hack to stop new entries from being returned | |
6003 | * under the assumption that they'll never reach this huge | |
6004 | * offset. | |
6005 | * | |
6006 | * This is being careful not to overflow 32bit loff_t unless the | |
6007 | * last entry requires it because doing so has broken 32bit apps | |
6008 | * in the past. | |
6009 | */ | |
c2951f32 JM |
6010 | if (ctx->pos >= INT_MAX) |
6011 | ctx->pos = LLONG_MAX; | |
6012 | else | |
6013 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6014 | nopos: |
6015 | ret = 0; | |
6016 | err: | |
02dbfc99 OS |
6017 | if (put) |
6018 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6019 | btrfs_free_path(path); |
39279cc3 CM |
6020 | return ret; |
6021 | } | |
6022 | ||
39279cc3 | 6023 | /* |
54aa1f4d | 6024 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6025 | * inode changes. But, it is most likely to find the inode in cache. |
6026 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6027 | * to keep or drop this code. | |
6028 | */ | |
7152b425 | 6029 | static int btrfs_dirty_inode(struct btrfs_inode *inode) |
39279cc3 | 6030 | { |
7152b425 DS |
6031 | struct btrfs_root *root = inode->root; |
6032 | struct btrfs_fs_info *fs_info = root->fs_info; | |
39279cc3 | 6033 | struct btrfs_trans_handle *trans; |
8929ecfa YZ |
6034 | int ret; |
6035 | ||
7152b425 | 6036 | if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags)) |
22c44fe6 | 6037 | return 0; |
39279cc3 | 6038 | |
7a7eaa40 | 6039 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6040 | if (IS_ERR(trans)) |
6041 | return PTR_ERR(trans); | |
8929ecfa | 6042 | |
8b9d0322 | 6043 | ret = btrfs_update_inode(trans, inode); |
2199cb0f | 6044 | if (ret == -ENOSPC || ret == -EDQUOT) { |
94b60442 | 6045 | /* whoops, lets try again with the full transaction */ |
3a45bb20 | 6046 | btrfs_end_transaction(trans); |
94b60442 | 6047 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6048 | if (IS_ERR(trans)) |
6049 | return PTR_ERR(trans); | |
8929ecfa | 6050 | |
8b9d0322 | 6051 | ret = btrfs_update_inode(trans, inode); |
94b60442 | 6052 | } |
3a45bb20 | 6053 | btrfs_end_transaction(trans); |
7152b425 | 6054 | if (inode->delayed_node) |
2ff7e61e | 6055 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6056 | |
6057 | return ret; | |
6058 | } | |
6059 | ||
6060 | /* | |
6061 | * This is a copy of file_update_time. We need this so we can return error on | |
6062 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6063 | */ | |
913e9928 | 6064 | static int btrfs_update_time(struct inode *inode, int flags) |
22c44fe6 | 6065 | { |
2bc55652 | 6066 | struct btrfs_root *root = BTRFS_I(inode)->root; |
a666ce9b | 6067 | bool dirty; |
2bc55652 AB |
6068 | |
6069 | if (btrfs_root_readonly(root)) | |
6070 | return -EROFS; | |
6071 | ||
bb7cc0a6 | 6072 | dirty = inode_update_timestamps(inode, flags); |
7152b425 | 6073 | return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0; |
39279cc3 CM |
6074 | } |
6075 | ||
d352ac68 CM |
6076 | /* |
6077 | * helper to find a free sequence number in a given directory. This current | |
6078 | * code is very simple, later versions will do smarter things in the btree | |
6079 | */ | |
877574e2 | 6080 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6081 | { |
6082 | int ret = 0; | |
6083 | ||
877574e2 NB |
6084 | if (dir->index_cnt == (u64)-1) { |
6085 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6086 | if (ret) { |
6087 | ret = btrfs_set_inode_index_count(dir); | |
6088 | if (ret) | |
6089 | return ret; | |
6090 | } | |
aec7477b JB |
6091 | } |
6092 | ||
877574e2 NB |
6093 | *index = dir->index_cnt; |
6094 | dir->index_cnt++; | |
aec7477b JB |
6095 | |
6096 | return ret; | |
6097 | } | |
6098 | ||
b0d5d10f CM |
6099 | static int btrfs_insert_inode_locked(struct inode *inode) |
6100 | { | |
6101 | struct btrfs_iget_args args; | |
0202e83f DS |
6102 | |
6103 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
6104 | args.root = BTRFS_I(inode)->root; |
6105 | ||
6106 | return insert_inode_locked4(inode, | |
6107 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6108 | btrfs_find_actor, &args); | |
6109 | } | |
6110 | ||
3538d68d OS |
6111 | int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args, |
6112 | unsigned int *trans_num_items) | |
6113 | { | |
6114 | struct inode *dir = args->dir; | |
6115 | struct inode *inode = args->inode; | |
6116 | int ret; | |
6117 | ||
ab3c5c18 STD |
6118 | if (!args->orphan) { |
6119 | ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0, | |
6120 | &args->fname); | |
6121 | if (ret) | |
6122 | return ret; | |
ab3c5c18 STD |
6123 | } |
6124 | ||
3538d68d | 6125 | ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl); |
ab3c5c18 STD |
6126 | if (ret) { |
6127 | fscrypt_free_filename(&args->fname); | |
3538d68d | 6128 | return ret; |
ab3c5c18 | 6129 | } |
3538d68d OS |
6130 | |
6131 | /* 1 to add inode item */ | |
6132 | *trans_num_items = 1; | |
6133 | /* 1 to add compression property */ | |
6134 | if (BTRFS_I(dir)->prop_compress) | |
6135 | (*trans_num_items)++; | |
6136 | /* 1 to add default ACL xattr */ | |
6137 | if (args->default_acl) | |
6138 | (*trans_num_items)++; | |
6139 | /* 1 to add access ACL xattr */ | |
6140 | if (args->acl) | |
6141 | (*trans_num_items)++; | |
6142 | #ifdef CONFIG_SECURITY | |
6143 | /* 1 to add LSM xattr */ | |
6144 | if (dir->i_security) | |
6145 | (*trans_num_items)++; | |
6146 | #endif | |
6147 | if (args->orphan) { | |
6148 | /* 1 to add orphan item */ | |
6149 | (*trans_num_items)++; | |
6150 | } else { | |
6151 | /* | |
3538d68d OS |
6152 | * 1 to add dir item |
6153 | * 1 to add dir index | |
6154 | * 1 to update parent inode item | |
97bdf1a9 FM |
6155 | * |
6156 | * No need for 1 unit for the inode ref item because it is | |
6157 | * inserted in a batch together with the inode item at | |
6158 | * btrfs_create_new_inode(). | |
3538d68d | 6159 | */ |
97bdf1a9 | 6160 | *trans_num_items += 3; |
3538d68d OS |
6161 | } |
6162 | return 0; | |
6163 | } | |
6164 | ||
6165 | void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args) | |
6166 | { | |
6167 | posix_acl_release(args->acl); | |
6168 | posix_acl_release(args->default_acl); | |
ab3c5c18 | 6169 | fscrypt_free_filename(&args->fname); |
3538d68d OS |
6170 | } |
6171 | ||
19aee8de AJ |
6172 | /* |
6173 | * Inherit flags from the parent inode. | |
6174 | * | |
6175 | * Currently only the compression flags and the cow flags are inherited. | |
6176 | */ | |
7a0443f0 | 6177 | static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode *dir) |
19aee8de AJ |
6178 | { |
6179 | unsigned int flags; | |
6180 | ||
7a0443f0 | 6181 | flags = dir->flags; |
19aee8de AJ |
6182 | |
6183 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
7a0443f0 DS |
6184 | inode->flags &= ~BTRFS_INODE_COMPRESS; |
6185 | inode->flags |= BTRFS_INODE_NOCOMPRESS; | |
19aee8de | 6186 | } else if (flags & BTRFS_INODE_COMPRESS) { |
7a0443f0 DS |
6187 | inode->flags &= ~BTRFS_INODE_NOCOMPRESS; |
6188 | inode->flags |= BTRFS_INODE_COMPRESS; | |
19aee8de AJ |
6189 | } |
6190 | ||
6191 | if (flags & BTRFS_INODE_NODATACOW) { | |
7a0443f0 DS |
6192 | inode->flags |= BTRFS_INODE_NODATACOW; |
6193 | if (S_ISREG(inode->vfs_inode.i_mode)) | |
6194 | inode->flags |= BTRFS_INODE_NODATASUM; | |
19aee8de AJ |
6195 | } |
6196 | ||
7a0443f0 | 6197 | btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode); |
19aee8de AJ |
6198 | } |
6199 | ||
3538d68d | 6200 | int btrfs_create_new_inode(struct btrfs_trans_handle *trans, |
caae78e0 | 6201 | struct btrfs_new_inode_args *args) |
39279cc3 | 6202 | { |
d5acbc60 | 6203 | struct timespec64 ts; |
caae78e0 | 6204 | struct inode *dir = args->dir; |
3538d68d | 6205 | struct inode *inode = args->inode; |
6db75318 | 6206 | const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name; |
41044b41 | 6207 | struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); |
3538d68d | 6208 | struct btrfs_root *root; |
5f39d397 | 6209 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6210 | struct btrfs_key *location; |
5f39d397 | 6211 | struct btrfs_path *path; |
6437d458 | 6212 | u64 objectid; |
9c58309d CM |
6213 | struct btrfs_inode_ref *ref; |
6214 | struct btrfs_key key[2]; | |
6215 | u32 sizes[2]; | |
b7ef5f3a | 6216 | struct btrfs_item_batch batch; |
9c58309d | 6217 | unsigned long ptr; |
39279cc3 | 6218 | int ret; |
39279cc3 | 6219 | |
5f39d397 | 6220 | path = btrfs_alloc_path(); |
d8926bb3 | 6221 | if (!path) |
a1fd0c35 | 6222 | return -ENOMEM; |
39279cc3 | 6223 | |
3538d68d OS |
6224 | if (!args->subvol) |
6225 | BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root); | |
6226 | root = BTRFS_I(inode)->root; | |
6227 | ||
6437d458 | 6228 | ret = btrfs_get_free_objectid(root, &objectid); |
caae78e0 OS |
6229 | if (ret) |
6230 | goto out; | |
581bb050 LZ |
6231 | inode->i_ino = objectid; |
6232 | ||
caae78e0 OS |
6233 | if (args->orphan) { |
6234 | /* | |
6235 | * O_TMPFILE, set link count to 0, so that after this point, we | |
6236 | * fill in an inode item with the correct link count. | |
6237 | */ | |
6238 | set_nlink(inode, 0); | |
6239 | } else { | |
1abe9b8a | 6240 | trace_btrfs_inode_request(dir); |
6241 | ||
caae78e0 OS |
6242 | ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index); |
6243 | if (ret) | |
6244 | goto out; | |
aec7477b | 6245 | } |
49024388 FM |
6246 | /* index_cnt is ignored for everything but a dir. */ |
6247 | BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX; | |
e02119d5 | 6248 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6249 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6250 | |
ed9b50a1 JB |
6251 | /* |
6252 | * We don't have any capability xattrs set here yet, shortcut any | |
6253 | * queries for the xattrs here. If we add them later via the inode | |
6254 | * security init path or any other path this flag will be cleared. | |
6255 | */ | |
6256 | set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); | |
6257 | ||
caae78e0 OS |
6258 | /* |
6259 | * Subvolumes don't inherit flags from their parent directory. | |
6260 | * Originally this was probably by accident, but we probably can't | |
6261 | * change it now without compatibility issues. | |
6262 | */ | |
6263 | if (!args->subvol) | |
7a0443f0 | 6264 | btrfs_inherit_iflags(BTRFS_I(inode), BTRFS_I(dir)); |
305eaac0 | 6265 | |
a1fd0c35 | 6266 | if (S_ISREG(inode->i_mode)) { |
305eaac0 OS |
6267 | if (btrfs_test_opt(fs_info, NODATASUM)) |
6268 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6269 | if (btrfs_test_opt(fs_info, NODATACOW)) | |
6270 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | | |
6271 | BTRFS_INODE_NODATASUM; | |
6272 | } | |
6273 | ||
caae78e0 OS |
6274 | location = &BTRFS_I(inode)->location; |
6275 | location->objectid = objectid; | |
6276 | location->offset = 0; | |
6277 | location->type = BTRFS_INODE_ITEM_KEY; | |
6278 | ||
6279 | ret = btrfs_insert_inode_locked(inode); | |
6280 | if (ret < 0) { | |
6281 | if (!args->orphan) | |
6282 | BTRFS_I(dir)->index_cnt--; | |
6283 | goto out; | |
6284 | } | |
6285 | ||
5dc562c5 JB |
6286 | /* |
6287 | * We could have gotten an inode number from somebody who was fsynced | |
6288 | * and then removed in this same transaction, so let's just set full | |
6289 | * sync since it will be a full sync anyway and this will blow away the | |
6290 | * old info in the log. | |
6291 | */ | |
23e3337f | 6292 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 | 6293 | |
9c58309d | 6294 | key[0].objectid = objectid; |
962a298f | 6295 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6296 | key[0].offset = 0; |
6297 | ||
9c58309d | 6298 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 | 6299 | |
caae78e0 | 6300 | if (!args->orphan) { |
ef3b9af5 FM |
6301 | /* |
6302 | * Start new inodes with an inode_ref. This is slightly more | |
6303 | * efficient for small numbers of hard links since they will | |
6304 | * be packed into one item. Extended refs will kick in if we | |
6305 | * add more hard links than can fit in the ref item. | |
6306 | */ | |
6307 | key[1].objectid = objectid; | |
962a298f | 6308 | key[1].type = BTRFS_INODE_REF_KEY; |
caae78e0 | 6309 | if (args->subvol) { |
23c24ef8 | 6310 | key[1].offset = objectid; |
caae78e0 OS |
6311 | sizes[1] = 2 + sizeof(*ref); |
6312 | } else { | |
6313 | key[1].offset = btrfs_ino(BTRFS_I(dir)); | |
e43eec81 | 6314 | sizes[1] = name->len + sizeof(*ref); |
caae78e0 | 6315 | } |
ef3b9af5 | 6316 | } |
9c58309d | 6317 | |
b7ef5f3a FM |
6318 | batch.keys = &key[0]; |
6319 | batch.data_sizes = &sizes[0]; | |
caae78e0 OS |
6320 | batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]); |
6321 | batch.nr = args->orphan ? 1 : 2; | |
b7ef5f3a | 6322 | ret = btrfs_insert_empty_items(trans, root, path, &batch); |
caae78e0 OS |
6323 | if (ret != 0) { |
6324 | btrfs_abort_transaction(trans, ret); | |
6325 | goto discard; | |
6326 | } | |
5f39d397 | 6327 | |
d5acbc60 LT |
6328 | ts = simple_inode_init_ts(inode); |
6329 | BTRFS_I(inode)->i_otime_sec = ts.tv_sec; | |
6330 | BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec; | |
9cc97d64 | 6331 | |
caae78e0 OS |
6332 | /* |
6333 | * We're going to fill the inode item now, so at this point the inode | |
6334 | * must be fully initialized. | |
6335 | */ | |
6336 | ||
5f39d397 CM |
6337 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6338 | struct btrfs_inode_item); | |
b159fa28 | 6339 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6340 | sizeof(*inode_item)); |
e02119d5 | 6341 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6342 | |
caae78e0 | 6343 | if (!args->orphan) { |
ef3b9af5 FM |
6344 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
6345 | struct btrfs_inode_ref); | |
ef3b9af5 | 6346 | ptr = (unsigned long)(ref + 1); |
caae78e0 OS |
6347 | if (args->subvol) { |
6348 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2); | |
6349 | btrfs_set_inode_ref_index(path->nodes[0], ref, 0); | |
6350 | write_extent_buffer(path->nodes[0], "..", ptr, 2); | |
6351 | } else { | |
e43eec81 STD |
6352 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, |
6353 | name->len); | |
caae78e0 OS |
6354 | btrfs_set_inode_ref_index(path->nodes[0], ref, |
6355 | BTRFS_I(inode)->dir_index); | |
e43eec81 STD |
6356 | write_extent_buffer(path->nodes[0], name->name, ptr, |
6357 | name->len); | |
caae78e0 | 6358 | } |
ef3b9af5 | 6359 | } |
9c58309d | 6360 | |
50564b65 | 6361 | btrfs_mark_buffer_dirty(trans, path->nodes[0]); |
814e7718 FM |
6362 | /* |
6363 | * We don't need the path anymore, plus inheriting properties, adding | |
6364 | * ACLs, security xattrs, orphan item or adding the link, will result in | |
6365 | * allocating yet another path. So just free our path. | |
6366 | */ | |
6367 | btrfs_free_path(path); | |
6368 | path = NULL; | |
5f39d397 | 6369 | |
6c3636eb STD |
6370 | if (args->subvol) { |
6371 | struct inode *parent; | |
6372 | ||
6373 | /* | |
6374 | * Subvolumes inherit properties from their parent subvolume, | |
6375 | * not the directory they were created in. | |
6376 | */ | |
6377 | parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID, | |
6378 | BTRFS_I(dir)->root); | |
6379 | if (IS_ERR(parent)) { | |
6380 | ret = PTR_ERR(parent); | |
6381 | } else { | |
6382 | ret = btrfs_inode_inherit_props(trans, inode, parent); | |
6383 | iput(parent); | |
6384 | } | |
6385 | } else { | |
6386 | ret = btrfs_inode_inherit_props(trans, inode, dir); | |
6387 | } | |
6388 | if (ret) { | |
6389 | btrfs_err(fs_info, | |
6390 | "error inheriting props for ino %llu (root %llu): %d", | |
6391 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, | |
6392 | ret); | |
6393 | } | |
6394 | ||
6395 | /* | |
6396 | * Subvolumes don't inherit ACLs or get passed to the LSM. This is | |
6397 | * probably a bug. | |
6398 | */ | |
6399 | if (!args->subvol) { | |
6400 | ret = btrfs_init_inode_security(trans, args); | |
6401 | if (ret) { | |
6402 | btrfs_abort_transaction(trans, ret); | |
6403 | goto discard; | |
6404 | } | |
6405 | } | |
6406 | ||
4c45a4f4 | 6407 | inode_tree_add(BTRFS_I(inode)); |
1abe9b8a | 6408 | |
6409 | trace_btrfs_inode_new(inode); | |
d9094414 | 6410 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6411 | |
8ea05e3a AB |
6412 | btrfs_update_root_times(trans, root); |
6413 | ||
caae78e0 OS |
6414 | if (args->orphan) { |
6415 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
6416 | } else { | |
6417 | ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name, | |
e43eec81 | 6418 | 0, BTRFS_I(inode)->dir_index); |
caae78e0 OS |
6419 | } |
6420 | if (ret) { | |
6421 | btrfs_abort_transaction(trans, ret); | |
6422 | goto discard; | |
6423 | } | |
63541927 | 6424 | |
814e7718 | 6425 | return 0; |
b0d5d10f | 6426 | |
caae78e0 | 6427 | discard: |
a1fd0c35 OS |
6428 | /* |
6429 | * discard_new_inode() calls iput(), but the caller owns the reference | |
6430 | * to the inode. | |
6431 | */ | |
6432 | ihold(inode); | |
32955c54 | 6433 | discard_new_inode(inode); |
caae78e0 | 6434 | out: |
5f39d397 | 6435 | btrfs_free_path(path); |
a1fd0c35 | 6436 | return ret; |
39279cc3 CM |
6437 | } |
6438 | ||
d352ac68 CM |
6439 | /* |
6440 | * utility function to add 'inode' into 'parent_inode' with | |
6441 | * a give name and a given sequence number. | |
6442 | * if 'add_backref' is true, also insert a backref from the | |
6443 | * inode to the parent directory. | |
6444 | */ | |
e02119d5 | 6445 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6446 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
6db75318 | 6447 | const struct fscrypt_str *name, int add_backref, u64 index) |
39279cc3 | 6448 | { |
4df27c4d | 6449 | int ret = 0; |
39279cc3 | 6450 | struct btrfs_key key; |
db0a669f NB |
6451 | struct btrfs_root *root = parent_inode->root; |
6452 | u64 ino = btrfs_ino(inode); | |
6453 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6454 | |
33345d01 | 6455 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6456 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6457 | } else { |
33345d01 | 6458 | key.objectid = ino; |
962a298f | 6459 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6460 | key.offset = 0; |
6461 | } | |
6462 | ||
33345d01 | 6463 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6464 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa | 6465 | root->root_key.objectid, parent_ino, |
e43eec81 | 6466 | index, name); |
4df27c4d | 6467 | } else if (add_backref) { |
e43eec81 STD |
6468 | ret = btrfs_insert_inode_ref(trans, root, name, |
6469 | ino, parent_ino, index); | |
4df27c4d | 6470 | } |
39279cc3 | 6471 | |
79787eaa JM |
6472 | /* Nothing to clean up yet */ |
6473 | if (ret) | |
6474 | return ret; | |
4df27c4d | 6475 | |
e43eec81 | 6476 | ret = btrfs_insert_dir_item(trans, name, parent_inode, &key, |
db0a669f | 6477 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6478 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6479 | goto fail_dir_item; |
6480 | else if (ret) { | |
66642832 | 6481 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6482 | return ret; |
39279cc3 | 6483 | } |
79787eaa | 6484 | |
db0a669f | 6485 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
e43eec81 | 6486 | name->len * 2); |
db0a669f | 6487 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6488 | /* |
6489 | * If we are replaying a log tree, we do not want to update the mtime | |
6490 | * and ctime of the parent directory with the current time, since the | |
6491 | * log replay procedure is responsible for setting them to their correct | |
6492 | * values (the ones it had when the fsync was done). | |
6493 | */ | |
2a9462de | 6494 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) |
b1c38a13 JL |
6495 | inode_set_mtime_to_ts(&parent_inode->vfs_inode, |
6496 | inode_set_ctime_current(&parent_inode->vfs_inode)); | |
5338e43a | 6497 | |
8b9d0322 | 6498 | ret = btrfs_update_inode(trans, parent_inode); |
79787eaa | 6499 | if (ret) |
66642832 | 6500 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6501 | return ret; |
fe66a05a CM |
6502 | |
6503 | fail_dir_item: | |
6504 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6505 | u64 local_index; | |
6506 | int err; | |
3ee1c553 | 6507 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa | 6508 | root->root_key.objectid, parent_ino, |
e43eec81 | 6509 | &local_index, name); |
1690dd41 JT |
6510 | if (err) |
6511 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6512 | } else if (add_backref) { |
6513 | u64 local_index; | |
6514 | int err; | |
6515 | ||
e43eec81 STD |
6516 | err = btrfs_del_inode_ref(trans, root, name, ino, parent_ino, |
6517 | &local_index); | |
1690dd41 JT |
6518 | if (err) |
6519 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6520 | } |
1690dd41 JT |
6521 | |
6522 | /* Return the original error code */ | |
fe66a05a | 6523 | return ret; |
39279cc3 CM |
6524 | } |
6525 | ||
5f465bf1 OS |
6526 | static int btrfs_create_common(struct inode *dir, struct dentry *dentry, |
6527 | struct inode *inode) | |
618e21d5 | 6528 | { |
41044b41 | 6529 | struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); |
618e21d5 | 6530 | struct btrfs_root *root = BTRFS_I(dir)->root; |
3538d68d OS |
6531 | struct btrfs_new_inode_args new_inode_args = { |
6532 | .dir = dir, | |
6533 | .dentry = dentry, | |
6534 | .inode = inode, | |
6535 | }; | |
6536 | unsigned int trans_num_items; | |
5f465bf1 | 6537 | struct btrfs_trans_handle *trans; |
618e21d5 | 6538 | int err; |
618e21d5 | 6539 | |
3538d68d | 6540 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); |
caae78e0 OS |
6541 | if (err) |
6542 | goto out_inode; | |
3538d68d OS |
6543 | |
6544 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 6545 | if (IS_ERR(trans)) { |
3538d68d OS |
6546 | err = PTR_ERR(trans); |
6547 | goto out_new_inode_args; | |
a1fd0c35 | 6548 | } |
1832a6d5 | 6549 | |
caae78e0 OS |
6550 | err = btrfs_create_new_inode(trans, &new_inode_args); |
6551 | if (!err) | |
6552 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6553 | |
3a45bb20 | 6554 | btrfs_end_transaction(trans); |
5f465bf1 | 6555 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
6556 | out_new_inode_args: |
6557 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
6558 | out_inode: |
6559 | if (err) | |
6560 | iput(inode); | |
618e21d5 JB |
6561 | return err; |
6562 | } | |
6563 | ||
5ebb29be | 6564 | static int btrfs_mknod(struct mnt_idmap *idmap, struct inode *dir, |
5f465bf1 OS |
6565 | struct dentry *dentry, umode_t mode, dev_t rdev) |
6566 | { | |
6567 | struct inode *inode; | |
6568 | ||
6569 | inode = new_inode(dir->i_sb); | |
6570 | if (!inode) | |
6571 | return -ENOMEM; | |
f2d40141 | 6572 | inode_init_owner(idmap, inode, dir, mode); |
5f465bf1 OS |
6573 | inode->i_op = &btrfs_special_inode_operations; |
6574 | init_special_inode(inode, inode->i_mode, rdev); | |
6575 | return btrfs_create_common(dir, dentry, inode); | |
6576 | } | |
6577 | ||
6c960e68 | 6578 | static int btrfs_create(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 6579 | struct dentry *dentry, umode_t mode, bool excl) |
39279cc3 | 6580 | { |
a1fd0c35 | 6581 | struct inode *inode; |
39279cc3 | 6582 | |
a1fd0c35 OS |
6583 | inode = new_inode(dir->i_sb); |
6584 | if (!inode) | |
6585 | return -ENOMEM; | |
f2d40141 | 6586 | inode_init_owner(idmap, inode, dir, mode); |
a1fd0c35 OS |
6587 | inode->i_fop = &btrfs_file_operations; |
6588 | inode->i_op = &btrfs_file_inode_operations; | |
6589 | inode->i_mapping->a_ops = &btrfs_aops; | |
5f465bf1 | 6590 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6591 | } |
6592 | ||
6593 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6594 | struct dentry *dentry) | |
6595 | { | |
271dba45 | 6596 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6597 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6598 | struct inode *inode = d_inode(old_dentry); |
41044b41 | 6599 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
ab3c5c18 | 6600 | struct fscrypt_name fname; |
00e4e6b3 | 6601 | u64 index; |
39279cc3 CM |
6602 | int err; |
6603 | int drop_inode = 0; | |
6604 | ||
4a8be425 | 6605 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6606 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6607 | return -EXDEV; |
4a8be425 | 6608 | |
f186373f | 6609 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6610 | return -EMLINK; |
4a8be425 | 6611 | |
ab3c5c18 STD |
6612 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname); |
6613 | if (err) | |
6614 | goto fail; | |
6615 | ||
877574e2 | 6616 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6617 | if (err) |
6618 | goto fail; | |
6619 | ||
a22285a6 | 6620 | /* |
7e6b6465 | 6621 | * 2 items for inode and inode ref |
a22285a6 | 6622 | * 2 items for dir items |
7e6b6465 | 6623 | * 1 item for parent inode |
399b0bbf | 6624 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6625 | */ |
399b0bbf | 6626 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6627 | if (IS_ERR(trans)) { |
6628 | err = PTR_ERR(trans); | |
271dba45 | 6629 | trans = NULL; |
a22285a6 YZ |
6630 | goto fail; |
6631 | } | |
5f39d397 | 6632 | |
67de1176 MX |
6633 | /* There are several dir indexes for this inode, clear the cache. */ |
6634 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6635 | inc_nlink(inode); |
0c4d2d95 | 6636 | inode_inc_iversion(inode); |
2a9462de | 6637 | inode_set_ctime_current(inode); |
7de9c6ee | 6638 | ihold(inode); |
e9976151 | 6639 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6640 | |
81512e89 | 6641 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6db75318 | 6642 | &fname.disk_name, 1, index); |
5f39d397 | 6643 | |
a5719521 | 6644 | if (err) { |
54aa1f4d | 6645 | drop_inode = 1; |
a5719521 | 6646 | } else { |
10d9f309 | 6647 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6648 | |
8b9d0322 | 6649 | err = btrfs_update_inode(trans, BTRFS_I(inode)); |
79787eaa JM |
6650 | if (err) |
6651 | goto fail; | |
ef3b9af5 FM |
6652 | if (inode->i_nlink == 1) { |
6653 | /* | |
6654 | * If new hard link count is 1, it's a file created | |
6655 | * with open(2) O_TMPFILE flag. | |
6656 | */ | |
3d6ae7bb | 6657 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6658 | if (err) |
6659 | goto fail; | |
6660 | } | |
08c422c2 | 6661 | d_instantiate(dentry, inode); |
88d2beec | 6662 | btrfs_log_new_name(trans, old_dentry, NULL, 0, parent); |
a5719521 | 6663 | } |
39279cc3 | 6664 | |
1832a6d5 | 6665 | fail: |
ab3c5c18 | 6666 | fscrypt_free_filename(&fname); |
271dba45 | 6667 | if (trans) |
3a45bb20 | 6668 | btrfs_end_transaction(trans); |
39279cc3 CM |
6669 | if (drop_inode) { |
6670 | inode_dec_link_count(inode); | |
6671 | iput(inode); | |
6672 | } | |
2ff7e61e | 6673 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6674 | return err; |
6675 | } | |
6676 | ||
c54bd91e | 6677 | static int btrfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 6678 | struct dentry *dentry, umode_t mode) |
39279cc3 | 6679 | { |
a1fd0c35 | 6680 | struct inode *inode; |
39279cc3 | 6681 | |
a1fd0c35 OS |
6682 | inode = new_inode(dir->i_sb); |
6683 | if (!inode) | |
6684 | return -ENOMEM; | |
f2d40141 | 6685 | inode_init_owner(idmap, inode, dir, S_IFDIR | mode); |
a1fd0c35 OS |
6686 | inode->i_op = &btrfs_dir_inode_operations; |
6687 | inode->i_fop = &btrfs_dir_file_operations; | |
5f465bf1 | 6688 | return btrfs_create_common(dir, dentry, inode); |
39279cc3 CM |
6689 | } |
6690 | ||
c8b97818 | 6691 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6692 | struct page *page, |
c8b97818 CM |
6693 | struct btrfs_file_extent_item *item) |
6694 | { | |
6695 | int ret; | |
6696 | struct extent_buffer *leaf = path->nodes[0]; | |
6697 | char *tmp; | |
6698 | size_t max_size; | |
6699 | unsigned long inline_size; | |
6700 | unsigned long ptr; | |
261507a0 | 6701 | int compress_type; |
c8b97818 | 6702 | |
261507a0 | 6703 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 | 6704 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
437bd07e | 6705 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); |
c8b97818 | 6706 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6707 | if (!tmp) |
6708 | return -ENOMEM; | |
c8b97818 CM |
6709 | ptr = btrfs_file_extent_inline_start(item); |
6710 | ||
6711 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6712 | ||
09cbfeaf | 6713 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
a982fc82 | 6714 | ret = btrfs_decompress(compress_type, tmp, page, 0, inline_size, max_size); |
e1699d2d ZB |
6715 | |
6716 | /* | |
6717 | * decompression code contains a memset to fill in any space between the end | |
6718 | * of the uncompressed data and the end of max_size in case the decompressed | |
6719 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6720 | * the end of an inline extent and the beginning of the next block, so we | |
6721 | * cover that region here. | |
6722 | */ | |
6723 | ||
a982fc82 QW |
6724 | if (max_size < PAGE_SIZE) |
6725 | memzero_page(page, max_size, PAGE_SIZE - max_size); | |
c8b97818 | 6726 | kfree(tmp); |
166ae5a4 | 6727 | return ret; |
c8b97818 CM |
6728 | } |
6729 | ||
a982fc82 QW |
6730 | static int read_inline_extent(struct btrfs_inode *inode, struct btrfs_path *path, |
6731 | struct page *page) | |
6732 | { | |
6733 | struct btrfs_file_extent_item *fi; | |
6734 | void *kaddr; | |
6735 | size_t copy_size; | |
6736 | ||
6737 | if (!page || PageUptodate(page)) | |
6738 | return 0; | |
6739 | ||
6740 | ASSERT(page_offset(page) == 0); | |
6741 | ||
6742 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
6743 | struct btrfs_file_extent_item); | |
6744 | if (btrfs_file_extent_compression(path->nodes[0], fi) != BTRFS_COMPRESS_NONE) | |
6745 | return uncompress_inline(path, page, fi); | |
6746 | ||
6747 | copy_size = min_t(u64, PAGE_SIZE, | |
6748 | btrfs_file_extent_ram_bytes(path->nodes[0], fi)); | |
6749 | kaddr = kmap_local_page(page); | |
6750 | read_extent_buffer(path->nodes[0], kaddr, | |
6751 | btrfs_file_extent_inline_start(fi), copy_size); | |
6752 | kunmap_local(kaddr); | |
6753 | if (copy_size < PAGE_SIZE) | |
6754 | memzero_page(page, copy_size, PAGE_SIZE - copy_size); | |
6755 | return 0; | |
6756 | } | |
6757 | ||
43dd529a DS |
6758 | /* |
6759 | * Lookup the first extent overlapping a range in a file. | |
6760 | * | |
39b07b5d OS |
6761 | * @inode: file to search in |
6762 | * @page: page to read extent data into if the extent is inline | |
39b07b5d OS |
6763 | * @start: file offset |
6764 | * @len: length of range starting at @start | |
6765 | * | |
43dd529a DS |
6766 | * Return the first &struct extent_map which overlaps the given range, reading |
6767 | * it from the B-tree and caching it if necessary. Note that there may be more | |
6768 | * extents which overlap the given range after the returned extent_map. | |
d352ac68 | 6769 | * |
39b07b5d OS |
6770 | * If @page is not NULL and the extent is inline, this also reads the extent |
6771 | * data directly into the page and marks the extent up to date in the io_tree. | |
6772 | * | |
6773 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6774 | */ |
fc4f21b1 | 6775 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
8bab0a30 | 6776 | struct page *page, u64 start, u64 len) |
a52d9a80 | 6777 | { |
3ffbd68c | 6778 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6779 | int ret = 0; |
a52d9a80 CM |
6780 | u64 extent_start = 0; |
6781 | u64 extent_end = 0; | |
fc4f21b1 | 6782 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6783 | int extent_type = -1; |
f421950f | 6784 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6785 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6786 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6787 | struct extent_buffer *leaf; |
6788 | struct btrfs_key found_key; | |
a52d9a80 | 6789 | struct extent_map *em = NULL; |
fc4f21b1 | 6790 | struct extent_map_tree *em_tree = &inode->extent_tree; |
a52d9a80 | 6791 | |
890871be | 6792 | read_lock(&em_tree->lock); |
d1310b2e | 6793 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6794 | read_unlock(&em_tree->lock); |
d1310b2e | 6795 | |
a52d9a80 | 6796 | if (em) { |
e1c4b745 CM |
6797 | if (em->start > start || em->start + em->len <= start) |
6798 | free_extent_map(em); | |
6799 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6800 | free_extent_map(em); |
6801 | else | |
6802 | goto out; | |
a52d9a80 | 6803 | } |
172ddd60 | 6804 | em = alloc_extent_map(); |
a52d9a80 | 6805 | if (!em) { |
1028d1c4 | 6806 | ret = -ENOMEM; |
d1310b2e | 6807 | goto out; |
a52d9a80 | 6808 | } |
d1310b2e | 6809 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6810 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6811 | em->len = (u64)-1; |
c8b97818 | 6812 | em->block_len = (u64)-1; |
f421950f | 6813 | |
bee6ec82 | 6814 | path = btrfs_alloc_path(); |
f421950f | 6815 | if (!path) { |
1028d1c4 | 6816 | ret = -ENOMEM; |
bee6ec82 | 6817 | goto out; |
f421950f CM |
6818 | } |
6819 | ||
bee6ec82 LB |
6820 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6821 | path->reada = READA_FORWARD; | |
4d7240f0 JB |
6822 | |
6823 | /* | |
6824 | * The same explanation in load_free_space_cache applies here as well, | |
6825 | * we only read when we're loading the free space cache, and at that | |
6826 | * point the commit_root has everything we need. | |
6827 | */ | |
6828 | if (btrfs_is_free_space_inode(inode)) { | |
6829 | path->search_commit_root = 1; | |
6830 | path->skip_locking = 1; | |
6831 | } | |
51899412 | 6832 | |
5c9a702e | 6833 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6834 | if (ret < 0) { |
a52d9a80 | 6835 | goto out; |
b8eeab7f | 6836 | } else if (ret > 0) { |
a52d9a80 CM |
6837 | if (path->slots[0] == 0) |
6838 | goto not_found; | |
6839 | path->slots[0]--; | |
1028d1c4 | 6840 | ret = 0; |
a52d9a80 CM |
6841 | } |
6842 | ||
5f39d397 CM |
6843 | leaf = path->nodes[0]; |
6844 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6845 | struct btrfs_file_extent_item); |
5f39d397 | 6846 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6847 | if (found_key.objectid != objectid || |
694c12ed | 6848 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6849 | /* |
6850 | * If we backup past the first extent we want to move forward | |
6851 | * and see if there is an extent in front of us, otherwise we'll | |
6852 | * say there is a hole for our whole search range which can | |
6853 | * cause problems. | |
6854 | */ | |
6855 | extent_end = start; | |
6856 | goto next; | |
a52d9a80 CM |
6857 | } |
6858 | ||
694c12ed | 6859 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6860 | extent_start = found_key.offset; |
a5eeb3d1 | 6861 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6862 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6863 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6864 | /* Only regular file could have regular/prealloc extent */ |
6865 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 6866 | ret = -EUCLEAN; |
6bf9e4bd QW |
6867 | btrfs_crit(fs_info, |
6868 | "regular/prealloc extent found for non-regular inode %llu", | |
6869 | btrfs_ino(inode)); | |
6870 | goto out; | |
6871 | } | |
09ed2f16 LB |
6872 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6873 | extent_start); | |
694c12ed | 6874 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6875 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6876 | path->slots[0], | |
6877 | extent_start); | |
9036c102 | 6878 | } |
25a50341 | 6879 | next: |
9036c102 YZ |
6880 | if (start >= extent_end) { |
6881 | path->slots[0]++; | |
6882 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6883 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 6884 | if (ret < 0) |
9036c102 | 6885 | goto out; |
1028d1c4 | 6886 | else if (ret > 0) |
9036c102 | 6887 | goto not_found; |
1028d1c4 | 6888 | |
9036c102 | 6889 | leaf = path->nodes[0]; |
a52d9a80 | 6890 | } |
9036c102 YZ |
6891 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6892 | if (found_key.objectid != objectid || | |
6893 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6894 | goto not_found; | |
6895 | if (start + len <= found_key.offset) | |
6896 | goto not_found; | |
e2eca69d WS |
6897 | if (start > found_key.offset) |
6898 | goto next; | |
02a033df NB |
6899 | |
6900 | /* New extent overlaps with existing one */ | |
9036c102 | 6901 | em->start = start; |
70c8a91c | 6902 | em->orig_start = start; |
9036c102 | 6903 | em->len = found_key.offset - start; |
02a033df NB |
6904 | em->block_start = EXTENT_MAP_HOLE; |
6905 | goto insert; | |
9036c102 YZ |
6906 | } |
6907 | ||
280f15cb | 6908 | btrfs_extent_item_to_extent_map(inode, path, item, em); |
7ffbb598 | 6909 | |
694c12ed NB |
6910 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6911 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6912 | goto insert; |
694c12ed | 6913 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
affc5424 QW |
6914 | /* |
6915 | * Inline extent can only exist at file offset 0. This is | |
6916 | * ensured by tree-checker and inline extent creation path. | |
6917 | * Thus all members representing file offsets should be zero. | |
6918 | */ | |
affc5424 QW |
6919 | ASSERT(extent_start == 0); |
6920 | ASSERT(em->start == 0); | |
5f39d397 | 6921 | |
a196a894 QW |
6922 | /* |
6923 | * btrfs_extent_item_to_extent_map() should have properly | |
6924 | * initialized em members already. | |
6925 | * | |
6926 | * Other members are not utilized for inline extents. | |
6927 | */ | |
6928 | ASSERT(em->block_start == EXTENT_MAP_INLINE); | |
946c2923 | 6929 | ASSERT(em->len == fs_info->sectorsize); |
e49aabd9 | 6930 | |
a982fc82 QW |
6931 | ret = read_inline_extent(inode, path, page); |
6932 | if (ret < 0) | |
6933 | goto out; | |
a52d9a80 | 6934 | goto insert; |
a52d9a80 CM |
6935 | } |
6936 | not_found: | |
6937 | em->start = start; | |
70c8a91c | 6938 | em->orig_start = start; |
d1310b2e | 6939 | em->len = len; |
5f39d397 | 6940 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6941 | insert: |
1028d1c4 | 6942 | ret = 0; |
b3b4aa74 | 6943 | btrfs_release_path(path); |
d1310b2e | 6944 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6945 | btrfs_err(fs_info, |
5d163e0e JM |
6946 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6947 | em->start, em->len, start, len); | |
1028d1c4 | 6948 | ret = -EIO; |
a52d9a80 CM |
6949 | goto out; |
6950 | } | |
d1310b2e | 6951 | |
890871be | 6952 | write_lock(&em_tree->lock); |
1028d1c4 | 6953 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6954 | write_unlock(&em_tree->lock); |
a52d9a80 | 6955 | out: |
c6414280 | 6956 | btrfs_free_path(path); |
1abe9b8a | 6957 | |
fc4f21b1 | 6958 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6959 | |
1028d1c4 | 6960 | if (ret) { |
a52d9a80 | 6961 | free_extent_map(em); |
1028d1c4 | 6962 | return ERR_PTR(ret); |
a52d9a80 CM |
6963 | } |
6964 | return em; | |
6965 | } | |
6966 | ||
64f54188 | 6967 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
53f2c206 | 6968 | struct btrfs_dio_data *dio_data, |
5f9a8a51 FM |
6969 | const u64 start, |
6970 | const u64 len, | |
6971 | const u64 orig_start, | |
6972 | const u64 block_start, | |
6973 | const u64 block_len, | |
6974 | const u64 orig_block_len, | |
6975 | const u64 ram_bytes, | |
6976 | const int type) | |
6977 | { | |
6978 | struct extent_map *em = NULL; | |
53f2c206 | 6979 | struct btrfs_ordered_extent *ordered; |
5f9a8a51 | 6980 | |
5f9a8a51 | 6981 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
6982 | em = create_io_em(inode, start, len, orig_start, block_start, |
6983 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
6984 | BTRFS_COMPRESS_NONE, /* compress_type */ |
6985 | type); | |
5f9a8a51 FM |
6986 | if (IS_ERR(em)) |
6987 | goto out; | |
6988 | } | |
53f2c206 BB |
6989 | ordered = btrfs_alloc_ordered_extent(inode, start, len, len, |
6990 | block_start, block_len, 0, | |
6991 | (1 << type) | | |
6992 | (1 << BTRFS_ORDERED_DIRECT), | |
6993 | BTRFS_COMPRESS_NONE); | |
6994 | if (IS_ERR(ordered)) { | |
5f9a8a51 FM |
6995 | if (em) { |
6996 | free_extent_map(em); | |
4c0c8cfc FM |
6997 | btrfs_drop_extent_map_range(inode, start, |
6998 | start + len - 1, false); | |
5f9a8a51 | 6999 | } |
53f2c206 BB |
7000 | em = ERR_CAST(ordered); |
7001 | } else { | |
7002 | ASSERT(!dio_data->ordered); | |
7003 | dio_data->ordered = ordered; | |
5f9a8a51 FM |
7004 | } |
7005 | out: | |
5f9a8a51 FM |
7006 | |
7007 | return em; | |
7008 | } | |
7009 | ||
9fc6f911 | 7010 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
53f2c206 | 7011 | struct btrfs_dio_data *dio_data, |
4b46fce2 JB |
7012 | u64 start, u64 len) |
7013 | { | |
9fc6f911 NB |
7014 | struct btrfs_root *root = inode->root; |
7015 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 7016 | struct extent_map *em; |
4b46fce2 JB |
7017 | struct btrfs_key ins; |
7018 | u64 alloc_hint; | |
7019 | int ret; | |
4b46fce2 | 7020 | |
9fc6f911 | 7021 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
776a838f | 7022 | again: |
0b246afa | 7023 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7024 | 0, alloc_hint, &ins, 1, 1); |
776a838f NA |
7025 | if (ret == -EAGAIN) { |
7026 | ASSERT(btrfs_is_zoned(fs_info)); | |
7027 | wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH, | |
7028 | TASK_UNINTERRUPTIBLE); | |
7029 | goto again; | |
7030 | } | |
00361589 JB |
7031 | if (ret) |
7032 | return ERR_PTR(ret); | |
4b46fce2 | 7033 | |
53f2c206 | 7034 | em = btrfs_create_dio_extent(inode, dio_data, start, ins.offset, start, |
5f9a8a51 | 7035 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 7036 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7037 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7038 | if (IS_ERR(em)) |
9fc6f911 NB |
7039 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
7040 | 1); | |
de0ee0ed | 7041 | |
4b46fce2 JB |
7042 | return em; |
7043 | } | |
7044 | ||
f4639636 | 7045 | static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
05947ae1 AJ |
7046 | { |
7047 | struct btrfs_block_group *block_group; | |
f4639636 | 7048 | bool readonly = false; |
05947ae1 AJ |
7049 | |
7050 | block_group = btrfs_lookup_block_group(fs_info, bytenr); | |
7051 | if (!block_group || block_group->ro) | |
f4639636 | 7052 | readonly = true; |
05947ae1 AJ |
7053 | if (block_group) |
7054 | btrfs_put_block_group(block_group); | |
7055 | return readonly; | |
7056 | } | |
7057 | ||
46bfbb5c | 7058 | /* |
e4ecaf90 QW |
7059 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
7060 | * | |
7061 | * @offset: File offset | |
7062 | * @len: The length to write, will be updated to the nocow writeable | |
7063 | * range | |
7064 | * @orig_start: (optional) Return the original file offset of the file extent | |
7065 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
7066 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
7067 | * @strict: if true, omit optimizations that might force us into unnecessary |
7068 | * cow. e.g., don't trust generation number. | |
e4ecaf90 | 7069 | * |
e4ecaf90 QW |
7070 | * Return: |
7071 | * >0 and update @len if we can do nocow write | |
7072 | * 0 if we can't do nocow write | |
7073 | * <0 if error happened | |
7074 | * | |
7075 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
7076 | * any ordered extents. | |
46bfbb5c | 7077 | */ |
00361589 | 7078 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 7079 | u64 *orig_start, u64 *orig_block_len, |
26ce9114 | 7080 | u64 *ram_bytes, bool nowait, bool strict) |
46bfbb5c | 7081 | { |
41044b41 | 7082 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
619104ba | 7083 | struct can_nocow_file_extent_args nocow_args = { 0 }; |
46bfbb5c CM |
7084 | struct btrfs_path *path; |
7085 | int ret; | |
7086 | struct extent_buffer *leaf; | |
7087 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7088 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7089 | struct btrfs_file_extent_item *fi; |
7090 | struct btrfs_key key; | |
46bfbb5c | 7091 | int found_type; |
e77751aa | 7092 | |
46bfbb5c CM |
7093 | path = btrfs_alloc_path(); |
7094 | if (!path) | |
7095 | return -ENOMEM; | |
26ce9114 | 7096 | path->nowait = nowait; |
46bfbb5c | 7097 | |
f85b7379 DS |
7098 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7099 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7100 | if (ret < 0) |
7101 | goto out; | |
7102 | ||
46bfbb5c | 7103 | if (ret == 1) { |
619104ba | 7104 | if (path->slots[0] == 0) { |
46bfbb5c CM |
7105 | /* can't find the item, must cow */ |
7106 | ret = 0; | |
7107 | goto out; | |
7108 | } | |
619104ba | 7109 | path->slots[0]--; |
46bfbb5c CM |
7110 | } |
7111 | ret = 0; | |
7112 | leaf = path->nodes[0]; | |
619104ba | 7113 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
4a0cc7ca | 7114 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7115 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7116 | /* not our file or wrong item type, must cow */ | |
7117 | goto out; | |
7118 | } | |
7119 | ||
7120 | if (key.offset > offset) { | |
7121 | /* Wrong offset, must cow */ | |
7122 | goto out; | |
7123 | } | |
7124 | ||
619104ba | 7125 | if (btrfs_file_extent_end(path) <= offset) |
7ee9e440 JB |
7126 | goto out; |
7127 | ||
619104ba FM |
7128 | fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); |
7129 | found_type = btrfs_file_extent_type(leaf, fi); | |
7130 | if (ram_bytes) | |
7131 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
e77751aa | 7132 | |
619104ba FM |
7133 | nocow_args.start = offset; |
7134 | nocow_args.end = offset + *len - 1; | |
7135 | nocow_args.strict = strict; | |
7136 | nocow_args.free_path = true; | |
7ee9e440 | 7137 | |
619104ba FM |
7138 | ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args); |
7139 | /* can_nocow_file_extent() has freed the path. */ | |
7140 | path = NULL; | |
7ee9e440 | 7141 | |
619104ba FM |
7142 | if (ret != 1) { |
7143 | /* Treat errors as not being able to NOCOW. */ | |
7144 | ret = 0; | |
78d4295b | 7145 | goto out; |
7ee9e440 | 7146 | } |
eb384b55 | 7147 | |
619104ba FM |
7148 | ret = 0; |
7149 | if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr)) | |
46bfbb5c | 7150 | goto out; |
7b2b7085 | 7151 | |
619104ba FM |
7152 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
7153 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7b2b7085 MX |
7154 | u64 range_end; |
7155 | ||
619104ba | 7156 | range_end = round_up(offset + nocow_args.num_bytes, |
da17066c | 7157 | root->fs_info->sectorsize) - 1; |
99be1a66 | 7158 | ret = test_range_bit_exists(io_tree, offset, range_end, EXTENT_DELALLOC); |
7b2b7085 MX |
7159 | if (ret) { |
7160 | ret = -EAGAIN; | |
7161 | goto out; | |
7162 | } | |
7163 | } | |
7164 | ||
619104ba FM |
7165 | if (orig_start) |
7166 | *orig_start = key.offset - nocow_args.extent_offset; | |
7167 | if (orig_block_len) | |
7168 | *orig_block_len = nocow_args.disk_num_bytes; | |
00361589 | 7169 | |
619104ba | 7170 | *len = nocow_args.num_bytes; |
46bfbb5c CM |
7171 | ret = 1; |
7172 | out: | |
7173 | btrfs_free_path(path); | |
7174 | return ret; | |
7175 | } | |
7176 | ||
eb838e73 | 7177 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
59094403 FM |
7178 | struct extent_state **cached_state, |
7179 | unsigned int iomap_flags) | |
eb838e73 | 7180 | { |
59094403 FM |
7181 | const bool writing = (iomap_flags & IOMAP_WRITE); |
7182 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); | |
7183 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
eb838e73 JB |
7184 | struct btrfs_ordered_extent *ordered; |
7185 | int ret = 0; | |
7186 | ||
7187 | while (1) { | |
59094403 | 7188 | if (nowait) { |
83ae4133 JB |
7189 | if (!try_lock_extent(io_tree, lockstart, lockend, |
7190 | cached_state)) | |
59094403 FM |
7191 | return -EAGAIN; |
7192 | } else { | |
570eb97b | 7193 | lock_extent(io_tree, lockstart, lockend, cached_state); |
59094403 | 7194 | } |
eb838e73 JB |
7195 | /* |
7196 | * We're concerned with the entire range that we're going to be | |
01327610 | 7197 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7198 | * extents in this range. |
7199 | */ | |
a776c6fa | 7200 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7201 | lockend - lockstart + 1); |
7202 | ||
7203 | /* | |
7204 | * We need to make sure there are no buffered pages in this | |
7205 | * range either, we could have raced between the invalidate in | |
7206 | * generic_file_direct_write and locking the extent. The | |
7207 | * invalidate needs to happen so that reads after a write do not | |
7208 | * get stale data. | |
7209 | */ | |
fc4adbff | 7210 | if (!ordered && |
051c98eb DS |
7211 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7212 | lockstart, lockend))) | |
eb838e73 JB |
7213 | break; |
7214 | ||
570eb97b | 7215 | unlock_extent(io_tree, lockstart, lockend, cached_state); |
eb838e73 JB |
7216 | |
7217 | if (ordered) { | |
59094403 FM |
7218 | if (nowait) { |
7219 | btrfs_put_ordered_extent(ordered); | |
7220 | ret = -EAGAIN; | |
7221 | break; | |
7222 | } | |
ade77029 FM |
7223 | /* |
7224 | * If we are doing a DIO read and the ordered extent we | |
7225 | * found is for a buffered write, we can not wait for it | |
7226 | * to complete and retry, because if we do so we can | |
7227 | * deadlock with concurrent buffered writes on page | |
7228 | * locks. This happens only if our DIO read covers more | |
7229 | * than one extent map, if at this point has already | |
7230 | * created an ordered extent for a previous extent map | |
7231 | * and locked its range in the inode's io tree, and a | |
7232 | * concurrent write against that previous extent map's | |
7233 | * range and this range started (we unlock the ranges | |
7234 | * in the io tree only when the bios complete and | |
7235 | * buffered writes always lock pages before attempting | |
7236 | * to lock range in the io tree). | |
7237 | */ | |
7238 | if (writing || | |
7239 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
36d45567 | 7240 | btrfs_start_ordered_extent(ordered); |
ade77029 | 7241 | else |
59094403 | 7242 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7243 | btrfs_put_ordered_extent(ordered); |
7244 | } else { | |
eb838e73 | 7245 | /* |
b850ae14 FM |
7246 | * We could trigger writeback for this range (and wait |
7247 | * for it to complete) and then invalidate the pages for | |
7248 | * this range (through invalidate_inode_pages2_range()), | |
7249 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7250 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7251 | * triggered a readahead) on a page lock due to an |
7252 | * ordered dio extent we created before but did not have | |
7253 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7254 | * complete), which makes readahead wait for that |
b850ae14 FM |
7255 | * ordered extent to complete while holding a lock on |
7256 | * that page. | |
eb838e73 | 7257 | */ |
59094403 | 7258 | ret = nowait ? -EAGAIN : -ENOTBLK; |
eb838e73 JB |
7259 | } |
7260 | ||
ade77029 FM |
7261 | if (ret) |
7262 | break; | |
7263 | ||
eb838e73 JB |
7264 | cond_resched(); |
7265 | } | |
7266 | ||
7267 | return ret; | |
7268 | } | |
7269 | ||
6f9994db | 7270 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7271 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7272 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7273 | u64 block_len, u64 orig_block_len, |
7274 | u64 ram_bytes, int compress_type, | |
7275 | int type) | |
69ffb543 | 7276 | { |
69ffb543 | 7277 | struct extent_map *em; |
69ffb543 JB |
7278 | int ret; |
7279 | ||
6f9994db LB |
7280 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7281 | type == BTRFS_ORDERED_COMPRESSED || | |
7282 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7283 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7284 | |
69ffb543 JB |
7285 | em = alloc_extent_map(); |
7286 | if (!em) | |
7287 | return ERR_PTR(-ENOMEM); | |
7288 | ||
7289 | em->start = start; | |
7290 | em->orig_start = orig_start; | |
7291 | em->len = len; | |
7292 | em->block_len = block_len; | |
7293 | em->block_start = block_start; | |
b4939680 | 7294 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7295 | em->ram_bytes = ram_bytes; |
70c8a91c | 7296 | em->generation = -1; |
f86f7a75 FM |
7297 | em->flags |= EXTENT_FLAG_PINNED; |
7298 | if (type == BTRFS_ORDERED_PREALLOC) | |
7299 | em->flags |= EXTENT_FLAG_FILLING; | |
7300 | else if (type == BTRFS_ORDERED_COMPRESSED) | |
7301 | extent_map_set_compression(em, compress_type); | |
69ffb543 | 7302 | |
a1ba4c08 | 7303 | ret = btrfs_replace_extent_map_range(inode, em, true); |
69ffb543 JB |
7304 | if (ret) { |
7305 | free_extent_map(em); | |
7306 | return ERR_PTR(ret); | |
7307 | } | |
7308 | ||
6f9994db | 7309 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7310 | return em; |
7311 | } | |
7312 | ||
1c8d0175 | 7313 | |
c5794e51 | 7314 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7315 | struct inode *inode, |
7316 | struct btrfs_dio_data *dio_data, | |
7833b865 | 7317 | u64 start, u64 *lenp, |
d7a8ab4e | 7318 | unsigned int iomap_flags) |
c5794e51 | 7319 | { |
d4135134 | 7320 | const bool nowait = (iomap_flags & IOMAP_NOWAIT); |
41044b41 | 7321 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
c5794e51 | 7322 | struct extent_map *em = *map; |
f0bfa76a FM |
7323 | int type; |
7324 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
2306e83e | 7325 | struct btrfs_block_group *bg; |
f0bfa76a FM |
7326 | bool can_nocow = false; |
7327 | bool space_reserved = false; | |
7833b865 | 7328 | u64 len = *lenp; |
6d82ad13 | 7329 | u64 prev_len; |
c5794e51 NB |
7330 | int ret = 0; |
7331 | ||
7332 | /* | |
7333 | * We don't allocate a new extent in the following cases | |
7334 | * | |
7335 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7336 | * existing extent. | |
7337 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7338 | * just use the extent. | |
7339 | * | |
7340 | */ | |
f86f7a75 | 7341 | if ((em->flags & EXTENT_FLAG_PREALLOC) || |
c5794e51 NB |
7342 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
7343 | em->block_start != EXTENT_MAP_HOLE)) { | |
f86f7a75 | 7344 | if (em->flags & EXTENT_FLAG_PREALLOC) |
c5794e51 NB |
7345 | type = BTRFS_ORDERED_PREALLOC; |
7346 | else | |
7347 | type = BTRFS_ORDERED_NOCOW; | |
7348 | len = min(len, em->len - (start - em->start)); | |
7349 | block_start = em->block_start + (start - em->start); | |
7350 | ||
7351 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
26ce9114 | 7352 | &orig_block_len, &ram_bytes, false, false) == 1) { |
2306e83e FM |
7353 | bg = btrfs_inc_nocow_writers(fs_info, block_start); |
7354 | if (bg) | |
7355 | can_nocow = true; | |
7356 | } | |
f0bfa76a | 7357 | } |
c5794e51 | 7358 | |
6d82ad13 | 7359 | prev_len = len; |
f0bfa76a FM |
7360 | if (can_nocow) { |
7361 | struct extent_map *em2; | |
7362 | ||
7363 | /* We can NOCOW, so only need to reserve metadata space. */ | |
d4135134 FM |
7364 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, |
7365 | nowait); | |
f0bfa76a FM |
7366 | if (ret < 0) { |
7367 | /* Our caller expects us to free the input extent map. */ | |
7368 | free_extent_map(em); | |
7369 | *map = NULL; | |
2306e83e | 7370 | btrfs_dec_nocow_writers(bg); |
d4135134 FM |
7371 | if (nowait && (ret == -ENOSPC || ret == -EDQUOT)) |
7372 | ret = -EAGAIN; | |
f0bfa76a FM |
7373 | goto out; |
7374 | } | |
7375 | space_reserved = true; | |
7376 | ||
53f2c206 | 7377 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), dio_data, start, len, |
f0bfa76a FM |
7378 | orig_start, block_start, |
7379 | len, orig_block_len, | |
7380 | ram_bytes, type); | |
2306e83e | 7381 | btrfs_dec_nocow_writers(bg); |
f0bfa76a FM |
7382 | if (type == BTRFS_ORDERED_PREALLOC) { |
7383 | free_extent_map(em); | |
c1867eb3 DS |
7384 | *map = em2; |
7385 | em = em2; | |
f0bfa76a | 7386 | } |
c5794e51 | 7387 | |
f0bfa76a FM |
7388 | if (IS_ERR(em2)) { |
7389 | ret = PTR_ERR(em2); | |
7390 | goto out; | |
c5794e51 | 7391 | } |
f5585f4f FM |
7392 | |
7393 | dio_data->nocow_done = true; | |
f0bfa76a | 7394 | } else { |
f0bfa76a FM |
7395 | /* Our caller expects us to free the input extent map. */ |
7396 | free_extent_map(em); | |
7397 | *map = NULL; | |
7398 | ||
7833b865 CH |
7399 | if (nowait) { |
7400 | ret = -EAGAIN; | |
7401 | goto out; | |
7402 | } | |
d7a8ab4e | 7403 | |
f5585f4f FM |
7404 | /* |
7405 | * If we could not allocate data space before locking the file | |
7406 | * range and we can't do a NOCOW write, then we have to fail. | |
7407 | */ | |
7833b865 CH |
7408 | if (!dio_data->data_space_reserved) { |
7409 | ret = -ENOSPC; | |
7410 | goto out; | |
7411 | } | |
f5585f4f FM |
7412 | |
7413 | /* | |
7414 | * We have to COW and we have already reserved data space before, | |
7415 | * so now we reserve only metadata. | |
7416 | */ | |
7417 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len, | |
7418 | false); | |
f0bfa76a FM |
7419 | if (ret < 0) |
7420 | goto out; | |
7421 | space_reserved = true; | |
7422 | ||
53f2c206 | 7423 | em = btrfs_new_extent_direct(BTRFS_I(inode), dio_data, start, len); |
f0bfa76a FM |
7424 | if (IS_ERR(em)) { |
7425 | ret = PTR_ERR(em); | |
7426 | goto out; | |
7427 | } | |
7428 | *map = em; | |
7429 | len = min(len, em->len - (start - em->start)); | |
7430 | if (len < prev_len) | |
f5585f4f FM |
7431 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
7432 | prev_len - len, true); | |
c5794e51 NB |
7433 | } |
7434 | ||
f0bfa76a FM |
7435 | /* |
7436 | * We have created our ordered extent, so we can now release our reservation | |
7437 | * for an outstanding extent. | |
7438 | */ | |
6d82ad13 | 7439 | btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len); |
c5794e51 | 7440 | |
c5794e51 NB |
7441 | /* |
7442 | * Need to update the i_size under the extent lock so buffered | |
7443 | * readers will get the updated i_size when we unlock. | |
7444 | */ | |
f85781fb | 7445 | if (start + len > i_size_read(inode)) |
c5794e51 | 7446 | i_size_write(inode, start + len); |
c5794e51 | 7447 | out: |
f0bfa76a FM |
7448 | if (ret && space_reserved) { |
7449 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); | |
f5585f4f | 7450 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true); |
f0bfa76a | 7451 | } |
7833b865 | 7452 | *lenp = len; |
c5794e51 NB |
7453 | return ret; |
7454 | } | |
7455 | ||
f85781fb GR |
7456 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7457 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7458 | struct iomap *srcmap) | |
4b46fce2 | 7459 | { |
491a6d01 | 7460 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
41044b41 | 7461 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
4b46fce2 | 7462 | struct extent_map *em; |
eb838e73 | 7463 | struct extent_state *cached_state = NULL; |
491a6d01 | 7464 | struct btrfs_dio_data *dio_data = iter->private; |
eb838e73 | 7465 | u64 lockstart, lockend; |
f85781fb | 7466 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7467 | int ret = 0; |
f85781fb | 7468 | u64 len = length; |
f5585f4f | 7469 | const u64 data_alloc_len = length; |
f85781fb | 7470 | bool unlock_extents = false; |
eb838e73 | 7471 | |
79d3d1d1 JB |
7472 | /* |
7473 | * We could potentially fault if we have a buffer > PAGE_SIZE, and if | |
7474 | * we're NOWAIT we may submit a bio for a partial range and return | |
7475 | * EIOCBQUEUED, which would result in an errant short read. | |
7476 | * | |
7477 | * The best way to handle this would be to allow for partial completions | |
7478 | * of iocb's, so we could submit the partial bio, return and fault in | |
7479 | * the rest of the pages, and then submit the io for the rest of the | |
7480 | * range. However we don't have that currently, so simply return | |
7481 | * -EAGAIN at this point so that the normal path is used. | |
7482 | */ | |
7483 | if (!write && (flags & IOMAP_NOWAIT) && length > PAGE_SIZE) | |
7484 | return -EAGAIN; | |
7485 | ||
ee5b46a3 CH |
7486 | /* |
7487 | * Cap the size of reads to that usually seen in buffered I/O as we need | |
7488 | * to allocate a contiguous array for the checksums. | |
7489 | */ | |
f85781fb | 7490 | if (!write) |
ee5b46a3 | 7491 | len = min_t(u64, len, fs_info->sectorsize * BTRFS_MAX_BIO_SECTORS); |
eb838e73 | 7492 | |
c329861d JB |
7493 | lockstart = start; |
7494 | lockend = start + len - 1; | |
7495 | ||
f85781fb | 7496 | /* |
b023e675 FM |
7497 | * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't |
7498 | * enough if we've written compressed pages to this area, so we need to | |
7499 | * flush the dirty pages again to make absolutely sure that any | |
7500 | * outstanding dirty pages are on disk - the first flush only starts | |
7501 | * compression on the data, while keeping the pages locked, so by the | |
7502 | * time the second flush returns we know bios for the compressed pages | |
7503 | * were submitted and finished, and the pages no longer under writeback. | |
7504 | * | |
7505 | * If we have a NOWAIT request and we have any pages in the range that | |
7506 | * are locked, likely due to compression still in progress, we don't want | |
7507 | * to block on page locks. We also don't want to block on pages marked as | |
7508 | * dirty or under writeback (same as for the non-compression case). | |
7509 | * iomap_dio_rw() did the same check, but after that and before we got | |
7510 | * here, mmap'ed writes may have happened or buffered reads started | |
7511 | * (readpage() and readahead(), which lock pages), as we haven't locked | |
7512 | * the file range yet. | |
f85781fb GR |
7513 | */ |
7514 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7515 | &BTRFS_I(inode)->runtime_flags)) { | |
b023e675 FM |
7516 | if (flags & IOMAP_NOWAIT) { |
7517 | if (filemap_range_needs_writeback(inode->i_mapping, | |
7518 | lockstart, lockend)) | |
7519 | return -EAGAIN; | |
7520 | } else { | |
7521 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7522 | start + length - 1); | |
7523 | if (ret) | |
7524 | return ret; | |
7525 | } | |
f85781fb GR |
7526 | } |
7527 | ||
491a6d01 | 7528 | memset(dio_data, 0, sizeof(*dio_data)); |
f85781fb | 7529 | |
f5585f4f FM |
7530 | /* |
7531 | * We always try to allocate data space and must do it before locking | |
7532 | * the file range, to avoid deadlocks with concurrent writes to the same | |
7533 | * range if the range has several extents and the writes don't expand the | |
7534 | * current i_size (the inode lock is taken in shared mode). If we fail to | |
7535 | * allocate data space here we continue and later, after locking the | |
7536 | * file range, we fail with ENOSPC only if we figure out we can not do a | |
7537 | * NOCOW write. | |
7538 | */ | |
7539 | if (write && !(flags & IOMAP_NOWAIT)) { | |
7540 | ret = btrfs_check_data_free_space(BTRFS_I(inode), | |
7541 | &dio_data->data_reserved, | |
1daedb1d | 7542 | start, data_alloc_len, false); |
f5585f4f FM |
7543 | if (!ret) |
7544 | dio_data->data_space_reserved = true; | |
7545 | else if (ret && !(BTRFS_I(inode)->flags & | |
7546 | (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) | |
7547 | goto err; | |
7548 | } | |
e1cbbfa5 | 7549 | |
eb838e73 JB |
7550 | /* |
7551 | * If this errors out it's because we couldn't invalidate pagecache for | |
59094403 FM |
7552 | * this range and we need to fallback to buffered IO, or we are doing a |
7553 | * NOWAIT read/write and we need to block. | |
eb838e73 | 7554 | */ |
59094403 FM |
7555 | ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags); |
7556 | if (ret < 0) | |
9c9464cc | 7557 | goto err; |
eb838e73 | 7558 | |
8bab0a30 | 7559 | em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len); |
eb838e73 JB |
7560 | if (IS_ERR(em)) { |
7561 | ret = PTR_ERR(em); | |
7562 | goto unlock_err; | |
7563 | } | |
4b46fce2 JB |
7564 | |
7565 | /* | |
7566 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7567 | * io. INLINE is special, and we could probably kludge it in here, but | |
7568 | * it's still buffered so for safety lets just fall back to the generic | |
7569 | * buffered path. | |
7570 | * | |
7571 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7572 | * decompress it, so there will be buffering required no matter what we | |
7573 | * do, so go ahead and fallback to buffered. | |
7574 | * | |
01327610 | 7575 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7576 | * to buffered IO. Don't blame me, this is the price we pay for using |
7577 | * the generic code. | |
7578 | */ | |
f86f7a75 | 7579 | if (extent_map_is_compressed(em) || |
4b46fce2 JB |
7580 | em->block_start == EXTENT_MAP_INLINE) { |
7581 | free_extent_map(em); | |
a4527e18 FM |
7582 | /* |
7583 | * If we are in a NOWAIT context, return -EAGAIN in order to | |
7584 | * fallback to buffered IO. This is not only because we can | |
7585 | * block with buffered IO (no support for NOWAIT semantics at | |
7586 | * the moment) but also to avoid returning short reads to user | |
7587 | * space - this happens if we were able to read some data from | |
7588 | * previous non-compressed extents and then when we fallback to | |
7589 | * buffered IO, at btrfs_file_read_iter() by calling | |
7590 | * filemap_read(), we fail to fault in pages for the read buffer, | |
7591 | * in which case filemap_read() returns a short read (the number | |
7592 | * of bytes previously read is > 0, so it does not return -EFAULT). | |
7593 | */ | |
7594 | ret = (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK; | |
eb838e73 | 7595 | goto unlock_err; |
4b46fce2 JB |
7596 | } |
7597 | ||
f85781fb | 7598 | len = min(len, em->len - (start - em->start)); |
ca93e44b FM |
7599 | |
7600 | /* | |
7601 | * If we have a NOWAIT request and the range contains multiple extents | |
7602 | * (or a mix of extents and holes), then we return -EAGAIN to make the | |
7603 | * caller fallback to a context where it can do a blocking (without | |
7604 | * NOWAIT) request. This way we avoid doing partial IO and returning | |
7605 | * success to the caller, which is not optimal for writes and for reads | |
7606 | * it can result in unexpected behaviour for an application. | |
7607 | * | |
7608 | * When doing a read, because we use IOMAP_DIO_PARTIAL when calling | |
7609 | * iomap_dio_rw(), we can end up returning less data then what the caller | |
7610 | * asked for, resulting in an unexpected, and incorrect, short read. | |
7611 | * That is, the caller asked to read N bytes and we return less than that, | |
7612 | * which is wrong unless we are crossing EOF. This happens if we get a | |
7613 | * page fault error when trying to fault in pages for the buffer that is | |
7614 | * associated to the struct iov_iter passed to iomap_dio_rw(), and we | |
7615 | * have previously submitted bios for other extents in the range, in | |
7616 | * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of | |
7617 | * those bios have completed by the time we get the page fault error, | |
7618 | * which we return back to our caller - we should only return EIOCBQUEUED | |
7619 | * after we have submitted bios for all the extents in the range. | |
7620 | */ | |
7621 | if ((flags & IOMAP_NOWAIT) && len < length) { | |
7622 | free_extent_map(em); | |
7623 | ret = -EAGAIN; | |
7624 | goto unlock_err; | |
7625 | } | |
7626 | ||
f85781fb GR |
7627 | if (write) { |
7628 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
7833b865 | 7629 | start, &len, flags); |
c5794e51 NB |
7630 | if (ret < 0) |
7631 | goto unlock_err; | |
f85781fb GR |
7632 | unlock_extents = true; |
7633 | /* Recalc len in case the new em is smaller than requested */ | |
7634 | len = min(len, em->len - (start - em->start)); | |
f5585f4f FM |
7635 | if (dio_data->data_space_reserved) { |
7636 | u64 release_offset; | |
7637 | u64 release_len = 0; | |
7638 | ||
7639 | if (dio_data->nocow_done) { | |
7640 | release_offset = start; | |
7641 | release_len = data_alloc_len; | |
7642 | } else if (len < data_alloc_len) { | |
7643 | release_offset = start + len; | |
7644 | release_len = data_alloc_len - len; | |
7645 | } | |
7646 | ||
7647 | if (release_len > 0) | |
7648 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7649 | dio_data->data_reserved, | |
7650 | release_offset, | |
7651 | release_len); | |
7652 | } | |
c5794e51 | 7653 | } else { |
1c8d0175 NB |
7654 | /* |
7655 | * We need to unlock only the end area that we aren't using. | |
7656 | * The rest is going to be unlocked by the endio routine. | |
7657 | */ | |
f85781fb GR |
7658 | lockstart = start + len; |
7659 | if (lockstart < lockend) | |
7660 | unlock_extents = true; | |
7661 | } | |
7662 | ||
7663 | if (unlock_extents) | |
570eb97b JB |
7664 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7665 | &cached_state); | |
f85781fb GR |
7666 | else |
7667 | free_extent_state(cached_state); | |
7668 | ||
7669 | /* | |
7670 | * Translate extent map information to iomap. | |
7671 | * We trim the extents (and move the addr) even though iomap code does | |
7672 | * that, since we have locked only the parts we are performing I/O in. | |
7673 | */ | |
7674 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
f86f7a75 | 7675 | ((em->flags & EXTENT_FLAG_PREALLOC) && !write)) { |
f85781fb GR |
7676 | iomap->addr = IOMAP_NULL_ADDR; |
7677 | iomap->type = IOMAP_HOLE; | |
7678 | } else { | |
7679 | iomap->addr = em->block_start + (start - em->start); | |
7680 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7681 | } |
f85781fb | 7682 | iomap->offset = start; |
d24fa5c1 | 7683 | iomap->bdev = fs_info->fs_devices->latest_dev->bdev; |
f85781fb | 7684 | iomap->length = len; |
4b46fce2 JB |
7685 | free_extent_map(em); |
7686 | ||
7687 | return 0; | |
eb838e73 JB |
7688 | |
7689 | unlock_err: | |
570eb97b JB |
7690 | unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7691 | &cached_state); | |
9c9464cc | 7692 | err: |
f5585f4f FM |
7693 | if (dio_data->data_space_reserved) { |
7694 | btrfs_free_reserved_data_space(BTRFS_I(inode), | |
7695 | dio_data->data_reserved, | |
7696 | start, data_alloc_len); | |
7697 | extent_changeset_free(dio_data->data_reserved); | |
7698 | } | |
7699 | ||
f85781fb GR |
7700 | return ret; |
7701 | } | |
7702 | ||
7703 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7704 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7705 | { | |
491a6d01 CH |
7706 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap); |
7707 | struct btrfs_dio_data *dio_data = iter->private; | |
f85781fb GR |
7708 | size_t submitted = dio_data->submitted; |
7709 | const bool write = !!(flags & IOMAP_WRITE); | |
491a6d01 | 7710 | int ret = 0; |
f85781fb GR |
7711 | |
7712 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7713 | /* If reading from a hole, unlock and return */ | |
570eb97b JB |
7714 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1, |
7715 | NULL); | |
491a6d01 | 7716 | return 0; |
f85781fb GR |
7717 | } |
7718 | ||
7719 | if (submitted < length) { | |
7720 | pos += submitted; | |
7721 | length -= submitted; | |
7722 | if (write) | |
b41b6f69 CH |
7723 | btrfs_finish_ordered_extent(dio_data->ordered, NULL, |
7724 | pos, length, false); | |
f85781fb GR |
7725 | else |
7726 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
570eb97b | 7727 | pos + length - 1, NULL); |
f85781fb GR |
7728 | ret = -ENOTBLK; |
7729 | } | |
53f2c206 BB |
7730 | if (write) { |
7731 | btrfs_put_ordered_extent(dio_data->ordered); | |
7732 | dio_data->ordered = NULL; | |
7733 | } | |
f85781fb | 7734 | |
f0bfa76a | 7735 | if (write) |
f85781fb | 7736 | extent_changeset_free(dio_data->data_reserved); |
8b110e39 MX |
7737 | return ret; |
7738 | } | |
7739 | ||
67d66982 | 7740 | static void btrfs_dio_end_io(struct btrfs_bio *bbio) |
8b110e39 | 7741 | { |
67d66982 CH |
7742 | struct btrfs_dio_private *dip = |
7743 | container_of(bbio, struct btrfs_dio_private, bbio); | |
7744 | struct btrfs_inode *inode = bbio->inode; | |
917f32a2 | 7745 | struct bio *bio = &bbio->bio; |
e65e1535 | 7746 | |
67d66982 CH |
7747 | if (bio->bi_status) { |
7748 | btrfs_warn(inode->root->fs_info, | |
7749 | "direct IO failed ino %llu op 0x%0x offset %#llx len %u err no %d", | |
7750 | btrfs_ino(inode), bio->bi_opf, | |
7751 | dip->file_offset, dip->bytes, bio->bi_status); | |
7609afac | 7752 | } |
1ae39938 | 7753 | |
b41b6f69 CH |
7754 | if (btrfs_op(bio) == BTRFS_MAP_WRITE) { |
7755 | btrfs_finish_ordered_extent(bbio->ordered, NULL, | |
7756 | dip->file_offset, dip->bytes, | |
7757 | !bio->bi_status); | |
7758 | } else { | |
67d66982 CH |
7759 | unlock_extent(&inode->io_tree, dip->file_offset, |
7760 | dip->file_offset + dip->bytes - 1, NULL); | |
b41b6f69 | 7761 | } |
ea1f0ced | 7762 | |
67d66982 CH |
7763 | bbio->bio.bi_private = bbio->private; |
7764 | iomap_dio_bio_end_io(bio); | |
e65e1535 MX |
7765 | } |
7766 | ||
67d66982 CH |
7767 | static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio, |
7768 | loff_t file_offset) | |
c36cac28 | 7769 | { |
67d66982 | 7770 | struct btrfs_bio *bbio = btrfs_bio(bio); |
642c5d34 | 7771 | struct btrfs_dio_private *dip = |
67d66982 | 7772 | container_of(bbio, struct btrfs_dio_private, bbio); |
491a6d01 | 7773 | struct btrfs_dio_data *dio_data = iter->private; |
544d24f9 | 7774 | |
4317ff00 QW |
7775 | btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info, |
7776 | btrfs_dio_end_io, bio->bi_private); | |
7777 | bbio->inode = BTRFS_I(iter->inode); | |
67d66982 | 7778 | bbio->file_offset = file_offset; |
e65e1535 | 7779 | |
67d66982 CH |
7780 | dip->file_offset = file_offset; |
7781 | dip->bytes = bio->bi_iter.bi_size; | |
e65e1535 | 7782 | |
67d66982 | 7783 | dio_data->submitted += bio->bi_iter.bi_size; |
b73a6fd1 BB |
7784 | |
7785 | /* | |
7786 | * Check if we are doing a partial write. If we are, we need to split | |
7787 | * the ordered extent to match the submitted bio. Hang on to the | |
7788 | * remaining unfinishable ordered_extent in dio_data so that it can be | |
7789 | * cancelled in iomap_end to avoid a deadlock wherein faulting the | |
7790 | * remaining pages is blocked on the outstanding ordered extent. | |
7791 | */ | |
7792 | if (iter->flags & IOMAP_WRITE) { | |
7793 | int ret; | |
7794 | ||
7795 | ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered); | |
7796 | if (ret) { | |
7cad645e CH |
7797 | btrfs_finish_ordered_extent(dio_data->ordered, NULL, |
7798 | file_offset, dip->bytes, | |
7799 | !ret); | |
7800 | bio->bi_status = errno_to_blk_status(ret); | |
7801 | iomap_dio_bio_end_io(bio); | |
b73a6fd1 BB |
7802 | return; |
7803 | } | |
7804 | } | |
7805 | ||
ae42a154 | 7806 | btrfs_submit_bio(bbio, 0); |
4b46fce2 JB |
7807 | } |
7808 | ||
36e8c622 | 7809 | static const struct iomap_ops btrfs_dio_iomap_ops = { |
f85781fb GR |
7810 | .iomap_begin = btrfs_dio_iomap_begin, |
7811 | .iomap_end = btrfs_dio_iomap_end, | |
7812 | }; | |
7813 | ||
36e8c622 | 7814 | static const struct iomap_dio_ops btrfs_dio_ops = { |
67d66982 | 7815 | .submit_io = btrfs_dio_submit_io, |
642c5d34 | 7816 | .bio_set = &btrfs_dio_bioset, |
f85781fb GR |
7817 | }; |
7818 | ||
8184620a | 7819 | ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter, size_t done_before) |
36e8c622 | 7820 | { |
53f2c206 | 7821 | struct btrfs_dio_data data = { 0 }; |
491a6d01 | 7822 | |
36e8c622 | 7823 | return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, |
8184620a FM |
7824 | IOMAP_DIO_PARTIAL, &data, done_before); |
7825 | } | |
7826 | ||
7827 | struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter, | |
7828 | size_t done_before) | |
7829 | { | |
53f2c206 | 7830 | struct btrfs_dio_data data = { 0 }; |
8184620a FM |
7831 | |
7832 | return __iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops, | |
7833 | IOMAP_DIO_PARTIAL, &data, done_before); | |
36e8c622 CH |
7834 | } |
7835 | ||
1506fcc8 | 7836 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 7837 | u64 start, u64 len) |
1506fcc8 | 7838 | { |
418b0902 | 7839 | struct btrfs_inode *btrfs_inode = BTRFS_I(inode); |
05dadc09 TI |
7840 | int ret; |
7841 | ||
45dd052e | 7842 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
7843 | if (ret) |
7844 | return ret; | |
7845 | ||
33a86cfa FM |
7846 | /* |
7847 | * fiemap_prep() called filemap_write_and_wait() for the whole possible | |
7848 | * file range (0 to LLONG_MAX), but that is not enough if we have | |
7849 | * compression enabled. The first filemap_fdatawrite_range() only kicks | |
7850 | * in the compression of data (in an async thread) and will return | |
7851 | * before the compression is done and writeback is started. A second | |
7852 | * filemap_fdatawrite_range() is needed to wait for the compression to | |
ac3c0d36 FM |
7853 | * complete and writeback to start. We also need to wait for ordered |
7854 | * extents to complete, because our fiemap implementation uses mainly | |
7855 | * file extent items to list the extents, searching for extent maps | |
7856 | * only for file ranges with holes or prealloc extents to figure out | |
7857 | * if we have delalloc in those ranges. | |
33a86cfa FM |
7858 | */ |
7859 | if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) { | |
ac3c0d36 | 7860 | ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX); |
33a86cfa FM |
7861 | if (ret) |
7862 | return ret; | |
7863 | } | |
7864 | ||
418b0902 FM |
7865 | btrfs_inode_lock(btrfs_inode, BTRFS_ILOCK_SHARED); |
7866 | ||
7867 | /* | |
7868 | * We did an initial flush to avoid holding the inode's lock while | |
7869 | * triggering writeback and waiting for the completion of IO and ordered | |
7870 | * extents. Now after we locked the inode we do it again, because it's | |
7871 | * possible a new write may have happened in between those two steps. | |
7872 | */ | |
7873 | if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) { | |
7874 | ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX); | |
7875 | if (ret) { | |
7876 | btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED); | |
7877 | return ret; | |
7878 | } | |
7879 | } | |
7880 | ||
7881 | ret = extent_fiemap(btrfs_inode, fieinfo, start, len); | |
7882 | btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED); | |
7883 | ||
7884 | return ret; | |
1506fcc8 YS |
7885 | } |
7886 | ||
48a3b636 ES |
7887 | static int btrfs_writepages(struct address_space *mapping, |
7888 | struct writeback_control *wbc) | |
b293f02e | 7889 | { |
8ae225a8 | 7890 | return extent_writepages(mapping, wbc); |
b293f02e CM |
7891 | } |
7892 | ||
ba206a02 | 7893 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 7894 | { |
ba206a02 | 7895 | extent_readahead(rac); |
3ab2fb5a | 7896 | } |
2a3ff0ad | 7897 | |
7c11d0ae | 7898 | /* |
f913cff3 | 7899 | * For release_folio() and invalidate_folio() we have a race window where |
895586eb | 7900 | * folio_end_writeback() is called but the subpage spinlock is not yet released. |
7c11d0ae QW |
7901 | * If we continue to release/invalidate the page, we could cause use-after-free |
7902 | * for subpage spinlock. So this function is to spin and wait for subpage | |
7903 | * spinlock. | |
7904 | */ | |
7905 | static void wait_subpage_spinlock(struct page *page) | |
7906 | { | |
b33d2e53 | 7907 | struct btrfs_fs_info *fs_info = page_to_fs_info(page); |
cfbf07e2 | 7908 | struct folio *folio = page_folio(page); |
7c11d0ae QW |
7909 | struct btrfs_subpage *subpage; |
7910 | ||
13df3775 | 7911 | if (!btrfs_is_subpage(fs_info, page->mapping)) |
7c11d0ae QW |
7912 | return; |
7913 | ||
cfbf07e2 QW |
7914 | ASSERT(folio_test_private(folio) && folio_get_private(folio)); |
7915 | subpage = folio_get_private(folio); | |
7c11d0ae QW |
7916 | |
7917 | /* | |
7918 | * This may look insane as we just acquire the spinlock and release it, | |
7919 | * without doing anything. But we just want to make sure no one is | |
7920 | * still holding the subpage spinlock. | |
7921 | * And since the page is not dirty nor writeback, and we have page | |
7922 | * locked, the only possible way to hold a spinlock is from the endio | |
7923 | * function to clear page writeback. | |
7924 | * | |
7925 | * Here we just acquire the spinlock so that all existing callers | |
7926 | * should exit and we're safe to release/invalidate the page. | |
7927 | */ | |
7928 | spin_lock_irq(&subpage->lock); | |
7929 | spin_unlock_irq(&subpage->lock); | |
7930 | } | |
7931 | ||
f913cff3 | 7932 | static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
9ebefb18 | 7933 | { |
f913cff3 | 7934 | int ret = try_release_extent_mapping(&folio->page, gfp_flags); |
7c11d0ae QW |
7935 | |
7936 | if (ret == 1) { | |
f913cff3 MWO |
7937 | wait_subpage_spinlock(&folio->page); |
7938 | clear_page_extent_mapped(&folio->page); | |
7c11d0ae | 7939 | } |
a52d9a80 | 7940 | return ret; |
39279cc3 CM |
7941 | } |
7942 | ||
f913cff3 | 7943 | static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags) |
e6dcd2dc | 7944 | { |
f913cff3 MWO |
7945 | if (folio_test_writeback(folio) || folio_test_dirty(folio)) |
7946 | return false; | |
7947 | return __btrfs_release_folio(folio, gfp_flags); | |
e6dcd2dc CM |
7948 | } |
7949 | ||
f8e66081 | 7950 | #ifdef CONFIG_MIGRATION |
e7a60a17 MWO |
7951 | static int btrfs_migrate_folio(struct address_space *mapping, |
7952 | struct folio *dst, struct folio *src, | |
f8e66081 RG |
7953 | enum migrate_mode mode) |
7954 | { | |
e7a60a17 | 7955 | int ret = filemap_migrate_folio(mapping, dst, src, mode); |
f8e66081 | 7956 | |
f8e66081 RG |
7957 | if (ret != MIGRATEPAGE_SUCCESS) |
7958 | return ret; | |
7959 | ||
e7a60a17 MWO |
7960 | if (folio_test_ordered(src)) { |
7961 | folio_clear_ordered(src); | |
7962 | folio_set_ordered(dst); | |
f8e66081 RG |
7963 | } |
7964 | ||
f8e66081 RG |
7965 | return MIGRATEPAGE_SUCCESS; |
7966 | } | |
e7a60a17 MWO |
7967 | #else |
7968 | #define btrfs_migrate_folio NULL | |
f8e66081 RG |
7969 | #endif |
7970 | ||
895586eb MWO |
7971 | static void btrfs_invalidate_folio(struct folio *folio, size_t offset, |
7972 | size_t length) | |
39279cc3 | 7973 | { |
c8293894 | 7974 | struct btrfs_inode *inode = folio_to_inode(folio); |
b945a463 | 7975 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
53ac7ead | 7976 | struct extent_io_tree *tree = &inode->io_tree; |
2ac55d41 | 7977 | struct extent_state *cached_state = NULL; |
895586eb MWO |
7978 | u64 page_start = folio_pos(folio); |
7979 | u64 page_end = page_start + folio_size(folio) - 1; | |
3b835840 | 7980 | u64 cur; |
53ac7ead | 7981 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 7982 | |
8b62b72b | 7983 | /* |
895586eb MWO |
7984 | * We have folio locked so no new ordered extent can be created on this |
7985 | * page, nor bio can be submitted for this folio. | |
8b62b72b | 7986 | * |
895586eb MWO |
7987 | * But already submitted bio can still be finished on this folio. |
7988 | * Furthermore, endio function won't skip folio which has Ordered | |
f57ad937 | 7989 | * (Private2) already cleared, so it's possible for endio and |
895586eb MWO |
7990 | * invalidate_folio to do the same ordered extent accounting twice |
7991 | * on one folio. | |
266a2586 QW |
7992 | * |
7993 | * So here we wait for any submitted bios to finish, so that we won't | |
895586eb | 7994 | * do double ordered extent accounting on the same folio. |
8b62b72b | 7995 | */ |
895586eb MWO |
7996 | folio_wait_writeback(folio); |
7997 | wait_subpage_spinlock(&folio->page); | |
8b62b72b | 7998 | |
bcd77455 QW |
7999 | /* |
8000 | * For subpage case, we have call sites like | |
8001 | * btrfs_punch_hole_lock_range() which passes range not aligned to | |
8002 | * sectorsize. | |
895586eb MWO |
8003 | * If the range doesn't cover the full folio, we don't need to and |
8004 | * shouldn't clear page extent mapped, as folio->private can still | |
bcd77455 QW |
8005 | * record subpage dirty bits for other part of the range. |
8006 | * | |
895586eb MWO |
8007 | * For cases that invalidate the full folio even the range doesn't |
8008 | * cover the full folio, like invalidating the last folio, we're | |
bcd77455 QW |
8009 | * still safe to wait for ordered extent to finish. |
8010 | */ | |
5a60542c | 8011 | if (!(offset == 0 && length == folio_size(folio))) { |
f913cff3 | 8012 | btrfs_release_folio(folio, GFP_NOFS); |
e6dcd2dc CM |
8013 | return; |
8014 | } | |
131e404a FDBM |
8015 | |
8016 | if (!inode_evicting) | |
570eb97b | 8017 | lock_extent(tree, page_start, page_end, &cached_state); |
951c80f8 | 8018 | |
3b835840 QW |
8019 | cur = page_start; |
8020 | while (cur < page_end) { | |
8021 | struct btrfs_ordered_extent *ordered; | |
3b835840 | 8022 | u64 range_end; |
b945a463 | 8023 | u32 range_len; |
bd015294 | 8024 | u32 extra_flags = 0; |
3b835840 QW |
8025 | |
8026 | ordered = btrfs_lookup_first_ordered_range(inode, cur, | |
8027 | page_end + 1 - cur); | |
8028 | if (!ordered) { | |
8029 | range_end = page_end; | |
8030 | /* | |
8031 | * No ordered extent covering this range, we are safe | |
8032 | * to delete all extent states in the range. | |
8033 | */ | |
bd015294 | 8034 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8035 | goto next; |
8036 | } | |
8037 | if (ordered->file_offset > cur) { | |
8038 | /* | |
8039 | * There is a range between [cur, oe->file_offset) not | |
8040 | * covered by any ordered extent. | |
8041 | * We are safe to delete all extent states, and handle | |
8042 | * the ordered extent in the next iteration. | |
8043 | */ | |
8044 | range_end = ordered->file_offset - 1; | |
bd015294 | 8045 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8046 | goto next; |
8047 | } | |
8048 | ||
8049 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, | |
8050 | page_end); | |
b945a463 QW |
8051 | ASSERT(range_end + 1 - cur < U32_MAX); |
8052 | range_len = range_end + 1 - cur; | |
55151ea9 | 8053 | if (!btrfs_folio_test_ordered(fs_info, folio, cur, range_len)) { |
3b835840 | 8054 | /* |
f57ad937 QW |
8055 | * If Ordered (Private2) is cleared, it means endio has |
8056 | * already been executed for the range. | |
3b835840 QW |
8057 | * We can't delete the extent states as |
8058 | * btrfs_finish_ordered_io() may still use some of them. | |
8059 | */ | |
3b835840 QW |
8060 | goto next; |
8061 | } | |
55151ea9 | 8062 | btrfs_folio_clear_ordered(fs_info, folio, cur, range_len); |
3b835840 | 8063 | |
eb84ae03 | 8064 | /* |
2766ff61 FM |
8065 | * IO on this page will never be started, so we need to account |
8066 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW | |
8067 | * here, must leave that up for the ordered extent completion. | |
3b835840 QW |
8068 | * |
8069 | * This will also unlock the range for incoming | |
8070 | * btrfs_finish_ordered_io(). | |
eb84ae03 | 8071 | */ |
131e404a | 8072 | if (!inode_evicting) |
3b835840 | 8073 | clear_extent_bit(tree, cur, range_end, |
2766ff61 | 8074 | EXTENT_DELALLOC | |
131e404a | 8075 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
bd015294 | 8076 | EXTENT_DEFRAG, &cached_state); |
3b835840 | 8077 | |
54c65371 | 8078 | spin_lock_irq(&inode->ordered_tree_lock); |
3b835840 QW |
8079 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
8080 | ordered->truncated_len = min(ordered->truncated_len, | |
8081 | cur - ordered->file_offset); | |
54c65371 | 8082 | spin_unlock_irq(&inode->ordered_tree_lock); |
3b835840 | 8083 | |
bd015294 JB |
8084 | /* |
8085 | * If the ordered extent has finished, we're safe to delete all | |
8086 | * the extent states of the range, otherwise | |
8087 | * btrfs_finish_ordered_io() will get executed by endio for | |
8088 | * other pages, so we can't delete extent states. | |
8089 | */ | |
3b835840 | 8090 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
f41b6ba9 | 8091 | cur, range_end + 1 - cur)) { |
3b835840 QW |
8092 | btrfs_finish_ordered_io(ordered); |
8093 | /* | |
8094 | * The ordered extent has finished, now we're again | |
8095 | * safe to delete all extent states of the range. | |
8096 | */ | |
bd015294 | 8097 | extra_flags = EXTENT_CLEAR_ALL_BITS; |
3b835840 QW |
8098 | } |
8099 | next: | |
8100 | if (ordered) | |
8101 | btrfs_put_ordered_extent(ordered); | |
8b62b72b | 8102 | /* |
3b835840 QW |
8103 | * Qgroup reserved space handler |
8104 | * Sector(s) here will be either: | |
266a2586 | 8105 | * |
3b835840 QW |
8106 | * 1) Already written to disk or bio already finished |
8107 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. | |
8108 | * Qgroup will be handled by its qgroup_record then. | |
8109 | * btrfs_qgroup_free_data() call will do nothing here. | |
8110 | * | |
8111 | * 2) Not written to disk yet | |
8112 | * Then btrfs_qgroup_free_data() call will clear the | |
8113 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup | |
8114 | * reserved data space. | |
8115 | * Since the IO will never happen for this page. | |
8b62b72b | 8116 | */ |
9e65bfca | 8117 | btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL); |
131e404a | 8118 | if (!inode_evicting) { |
3b835840 QW |
8119 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
8120 | EXTENT_DELALLOC | EXTENT_UPTODATE | | |
bd015294 JB |
8121 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG | |
8122 | extra_flags, &cached_state); | |
131e404a | 8123 | } |
3b835840 | 8124 | cur = range_end + 1; |
131e404a | 8125 | } |
b9d0b389 | 8126 | /* |
3b835840 | 8127 | * We have iterated through all ordered extents of the page, the page |
f57ad937 QW |
8128 | * should not have Ordered (Private2) anymore, or the above iteration |
8129 | * did something wrong. | |
b9d0b389 | 8130 | */ |
895586eb | 8131 | ASSERT(!folio_test_ordered(folio)); |
55151ea9 | 8132 | btrfs_folio_clear_checked(fs_info, folio, folio_pos(folio), folio_size(folio)); |
3b835840 | 8133 | if (!inode_evicting) |
f913cff3 | 8134 | __btrfs_release_folio(folio, GFP_NOFS); |
895586eb | 8135 | clear_page_extent_mapped(&folio->page); |
39279cc3 CM |
8136 | } |
8137 | ||
9ebefb18 CM |
8138 | /* |
8139 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8140 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8141 | * be careful to check for EOF conditions here. We set the page up correctly | |
8142 | * for a written page which means we get ENOSPC checking when writing into | |
8143 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8144 | * support these features. | |
8145 | * | |
8146 | * We are not allowed to take the i_mutex here so we have to play games to | |
8147 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8148 | * truncate_setsize() writes the inode size before removing pages, once we have |
8149 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8150 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8151 | * unlock the page. | |
8152 | */ | |
a528a241 | 8153 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8154 | { |
c2ec175c | 8155 | struct page *page = vmf->page; |
55151ea9 | 8156 | struct folio *folio = page_folio(page); |
11bac800 | 8157 | struct inode *inode = file_inode(vmf->vma->vm_file); |
41044b41 | 8158 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
e6dcd2dc CM |
8159 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8160 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8161 | struct extent_state *cached_state = NULL; |
364ecf36 | 8162 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 8163 | unsigned long zero_start; |
9ebefb18 | 8164 | loff_t size; |
a528a241 SJ |
8165 | vm_fault_t ret; |
8166 | int ret2; | |
9998eb70 | 8167 | int reserved = 0; |
d0b7da88 | 8168 | u64 reserved_space; |
a52d9a80 | 8169 | u64 page_start; |
e6dcd2dc | 8170 | u64 page_end; |
d0b7da88 CR |
8171 | u64 end; |
8172 | ||
55151ea9 QW |
8173 | ASSERT(folio_order(folio) == 0); |
8174 | ||
09cbfeaf | 8175 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8176 | |
b2b5ef5c | 8177 | sb_start_pagefault(inode->i_sb); |
df480633 | 8178 | page_start = page_offset(page); |
09cbfeaf | 8179 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8180 | end = page_end; |
df480633 | 8181 | |
d0b7da88 CR |
8182 | /* |
8183 | * Reserving delalloc space after obtaining the page lock can lead to | |
8184 | * deadlock. For example, if a dirty page is locked by this function | |
8185 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
f3e90c1c | 8186 | * dirty page write out, then the btrfs_writepages() function could |
d0b7da88 CR |
8187 | * end up waiting indefinitely to get a lock on the page currently |
8188 | * being processed by btrfs_page_mkwrite() function. | |
8189 | */ | |
e5b7231e NB |
8190 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8191 | page_start, reserved_space); | |
a528a241 SJ |
8192 | if (!ret2) { |
8193 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8194 | reserved = 1; |
8195 | } | |
a528a241 SJ |
8196 | if (ret2) { |
8197 | ret = vmf_error(ret2); | |
9998eb70 CM |
8198 | if (reserved) |
8199 | goto out; | |
8200 | goto out_noreserve; | |
56a76f82 | 8201 | } |
1832a6d5 | 8202 | |
56a76f82 | 8203 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8204 | again: |
8318ba79 | 8205 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8206 | lock_page(page); |
9ebefb18 | 8207 | size = i_size_read(inode); |
a52d9a80 | 8208 | |
9ebefb18 | 8209 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8210 | (page_start >= size)) { |
9ebefb18 CM |
8211 | /* page got truncated out from underneath us */ |
8212 | goto out_unlock; | |
8213 | } | |
e6dcd2dc CM |
8214 | wait_on_page_writeback(page); |
8215 | ||
570eb97b | 8216 | lock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8217 | ret2 = set_page_extent_mapped(page); |
8218 | if (ret2 < 0) { | |
8219 | ret = vmf_error(ret2); | |
570eb97b | 8220 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
32443de3 QW |
8221 | goto out_unlock; |
8222 | } | |
e6dcd2dc | 8223 | |
eb84ae03 CM |
8224 | /* |
8225 | * we can't set the delalloc bits if there are pending ordered | |
8226 | * extents. Drop our locks and wait for them to finish | |
8227 | */ | |
a776c6fa NB |
8228 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8229 | PAGE_SIZE); | |
e6dcd2dc | 8230 | if (ordered) { |
570eb97b | 8231 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc | 8232 | unlock_page(page); |
8318ba79 | 8233 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
36d45567 | 8234 | btrfs_start_ordered_extent(ordered); |
e6dcd2dc CM |
8235 | btrfs_put_ordered_extent(ordered); |
8236 | goto again; | |
8237 | } | |
8238 | ||
09cbfeaf | 8239 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8240 | reserved_space = round_up(size - page_start, |
0b246afa | 8241 | fs_info->sectorsize); |
09cbfeaf | 8242 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8243 | end = page_start + reserved_space - 1; |
86d52921 NB |
8244 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8245 | data_reserved, page_start, | |
8246 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8247 | } |
8248 | } | |
8249 | ||
fbf19087 | 8250 | /* |
5416034f LB |
8251 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8252 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8253 | * bits, thus in this case for space account reason, we still need to | |
8254 | * clear any delalloc bits within this page range since we have to | |
8255 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8256 | */ |
d0b7da88 | 8257 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d | 8258 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
bd015294 | 8259 | EXTENT_DEFRAG, &cached_state); |
fbf19087 | 8260 | |
c2566f22 | 8261 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8262 | &cached_state); |
a528a241 | 8263 | if (ret2) { |
570eb97b | 8264 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
9ed74f2d JB |
8265 | ret = VM_FAULT_SIGBUS; |
8266 | goto out_unlock; | |
8267 | } | |
9ebefb18 CM |
8268 | |
8269 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8270 | if (page_start + PAGE_SIZE > size) |
7073017a | 8271 | zero_start = offset_in_page(size); |
9ebefb18 | 8272 | else |
09cbfeaf | 8273 | zero_start = PAGE_SIZE; |
9ebefb18 | 8274 | |
21a8935e | 8275 | if (zero_start != PAGE_SIZE) |
d048b9c2 | 8276 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
21a8935e | 8277 | |
55151ea9 QW |
8278 | btrfs_folio_clear_checked(fs_info, folio, page_start, PAGE_SIZE); |
8279 | btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start); | |
8280 | btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start); | |
5a3f23d5 | 8281 | |
bc0939fc | 8282 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
257c62e1 | 8283 | |
570eb97b | 8284 | unlock_extent(io_tree, page_start, page_end, &cached_state); |
8318ba79 | 8285 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
9ebefb18 | 8286 | |
76de60ed YY |
8287 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8288 | sb_end_pagefault(inode->i_sb); | |
8289 | extent_changeset_free(data_reserved); | |
8290 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8291 | |
8292 | out_unlock: | |
9ebefb18 | 8293 | unlock_page(page); |
8318ba79 | 8294 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
1832a6d5 | 8295 | out: |
8702ba93 | 8296 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8297 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8298 | reserved_space, (ret != 0)); |
9998eb70 | 8299 | out_noreserve: |
b2b5ef5c | 8300 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8301 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8302 | return ret; |
8303 | } | |
8304 | ||
d9dcae67 | 8305 | static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) |
39279cc3 | 8306 | { |
d9ac19c3 | 8307 | struct btrfs_truncate_control control = { |
d9dcae67 DS |
8308 | .inode = inode, |
8309 | .ino = btrfs_ino(inode), | |
d9ac19c3 | 8310 | .min_type = BTRFS_EXTENT_DATA_KEY, |
655807b8 | 8311 | .clear_extent_range = true, |
d9ac19c3 | 8312 | }; |
d9dcae67 DS |
8313 | struct btrfs_root *root = inode->root; |
8314 | struct btrfs_fs_info *fs_info = root->fs_info; | |
fcb80c2a | 8315 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8316 | int ret; |
39279cc3 | 8317 | struct btrfs_trans_handle *trans; |
0b246afa | 8318 | u64 mask = fs_info->sectorsize - 1; |
6822b3f7 | 8319 | const u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8320 | |
213e8c55 | 8321 | if (!skip_writeback) { |
d9dcae67 DS |
8322 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, |
8323 | inode->vfs_inode.i_size & (~mask), | |
213e8c55 FM |
8324 | (u64)-1); |
8325 | if (ret) | |
8326 | return ret; | |
8327 | } | |
39279cc3 | 8328 | |
fcb80c2a | 8329 | /* |
f7e9e8fc OS |
8330 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8331 | * things going on here: | |
fcb80c2a | 8332 | * |
f7e9e8fc | 8333 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8334 | * |
f7e9e8fc | 8335 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8336 | * be free'd up by the truncate operation, but also have some slack |
8337 | * space reserved in case it uses space during the truncate (thank you | |
8338 | * very much snapshotting). | |
8339 | * | |
f7e9e8fc | 8340 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8341 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8342 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8343 | * doesn't end up using space reserved for updating the inode. We also |
8344 | * need to be able to stop the transaction and start a new one, which | |
8345 | * means we need to be able to update the inode several times, and we | |
8346 | * have no idea of knowing how many times that will be, so we can't just | |
8347 | * reserve 1 item for the entirety of the operation, so that has to be | |
8348 | * done separately as well. | |
fcb80c2a JB |
8349 | * |
8350 | * So that leaves us with | |
8351 | * | |
f7e9e8fc | 8352 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8353 | * transaction reservation. |
f7e9e8fc | 8354 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8355 | * updating the inode. |
8356 | */ | |
2ff7e61e | 8357 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8358 | if (!rsv) |
8359 | return -ENOMEM; | |
4a338542 | 8360 | rsv->size = min_size; |
710d5921 | 8361 | rsv->failfast = true; |
f0cd846e | 8362 | |
907cbceb | 8363 | /* |
07127184 | 8364 | * 1 for the truncate slack space |
907cbceb JB |
8365 | * 1 for updating the inode. |
8366 | */ | |
f3fe820c | 8367 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8368 | if (IS_ERR(trans)) { |
ad7e1a74 | 8369 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8370 | goto out; |
8371 | } | |
f0cd846e | 8372 | |
907cbceb | 8373 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8374 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8375 | min_size, false); |
6822b3f7 FM |
8376 | /* |
8377 | * We have reserved 2 metadata units when we started the transaction and | |
8378 | * min_size matches 1 unit, so this should never fail, but if it does, | |
8379 | * it's not critical we just fail truncation. | |
8380 | */ | |
8381 | if (WARN_ON(ret)) { | |
8382 | btrfs_end_transaction(trans); | |
8383 | goto out; | |
8384 | } | |
f0cd846e | 8385 | |
ca7e70f5 | 8386 | trans->block_rsv = rsv; |
907cbceb | 8387 | |
8082510e | 8388 | while (1) { |
9a4a1429 | 8389 | struct extent_state *cached_state = NULL; |
d9dcae67 | 8390 | const u64 new_size = inode->vfs_inode.i_size; |
9a4a1429 JB |
8391 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
8392 | ||
d9ac19c3 | 8393 | control.new_size = new_size; |
d9dcae67 | 8394 | lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); |
9a4a1429 JB |
8395 | /* |
8396 | * We want to drop from the next block forward in case this new | |
8397 | * size is not block aligned since we will be keeping the last | |
8398 | * block of the extent just the way it is. | |
8399 | */ | |
d9dcae67 | 8400 | btrfs_drop_extent_map_range(inode, |
4c0c8cfc FM |
8401 | ALIGN(new_size, fs_info->sectorsize), |
8402 | (u64)-1, false); | |
9a4a1429 | 8403 | |
71d18b53 | 8404 | ret = btrfs_truncate_inode_items(trans, root, &control); |
c2ddb612 | 8405 | |
d9dcae67 DS |
8406 | inode_sub_bytes(&inode->vfs_inode, control.sub_bytes); |
8407 | btrfs_inode_safe_disk_i_size_write(inode, control.last_size); | |
c2ddb612 | 8408 | |
d9dcae67 | 8409 | unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); |
9a4a1429 | 8410 | |
ddfae63c | 8411 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8412 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8413 | break; |
39279cc3 | 8414 | |
8b9d0322 | 8415 | ret = btrfs_update_inode(trans, inode); |
ad7e1a74 | 8416 | if (ret) |
3893e33b | 8417 | break; |
ca7e70f5 | 8418 | |
3a45bb20 | 8419 | btrfs_end_transaction(trans); |
2ff7e61e | 8420 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8421 | |
8422 | trans = btrfs_start_transaction(root, 2); | |
8423 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8424 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8425 | trans = NULL; |
8426 | break; | |
8427 | } | |
8428 | ||
63f018be | 8429 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8430 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8431 | rsv, min_size, false); |
6822b3f7 FM |
8432 | /* |
8433 | * We have reserved 2 metadata units when we started the | |
8434 | * transaction and min_size matches 1 unit, so this should never | |
8435 | * fail, but if it does, it's not critical we just fail truncation. | |
8436 | */ | |
8437 | if (WARN_ON(ret)) | |
8438 | break; | |
8439 | ||
ca7e70f5 | 8440 | trans->block_rsv = rsv; |
8082510e YZ |
8441 | } |
8442 | ||
ddfae63c JB |
8443 | /* |
8444 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
54f03ab1 JB |
8445 | * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we |
8446 | * know we've truncated everything except the last little bit, and can | |
8447 | * do btrfs_truncate_block and then update the disk_i_size. | |
ddfae63c | 8448 | */ |
54f03ab1 | 8449 | if (ret == BTRFS_NEED_TRUNCATE_BLOCK) { |
ddfae63c JB |
8450 | btrfs_end_transaction(trans); |
8451 | btrfs_btree_balance_dirty(fs_info); | |
8452 | ||
d9dcae67 | 8453 | ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, 0, 0); |
ddfae63c JB |
8454 | if (ret) |
8455 | goto out; | |
8456 | trans = btrfs_start_transaction(root, 1); | |
8457 | if (IS_ERR(trans)) { | |
8458 | ret = PTR_ERR(trans); | |
8459 | goto out; | |
8460 | } | |
d9dcae67 | 8461 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
ddfae63c JB |
8462 | } |
8463 | ||
917c16b2 | 8464 | if (trans) { |
ad7e1a74 OS |
8465 | int ret2; |
8466 | ||
0b246afa | 8467 | trans->block_rsv = &fs_info->trans_block_rsv; |
8b9d0322 | 8468 | ret2 = btrfs_update_inode(trans, inode); |
ad7e1a74 OS |
8469 | if (ret2 && !ret) |
8470 | ret = ret2; | |
7b128766 | 8471 | |
ad7e1a74 OS |
8472 | ret2 = btrfs_end_transaction(trans); |
8473 | if (ret2 && !ret) | |
8474 | ret = ret2; | |
2ff7e61e | 8475 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8476 | } |
fcb80c2a | 8477 | out: |
2ff7e61e | 8478 | btrfs_free_block_rsv(fs_info, rsv); |
0d7d3165 FM |
8479 | /* |
8480 | * So if we truncate and then write and fsync we normally would just | |
8481 | * write the extents that changed, which is a problem if we need to | |
8482 | * first truncate that entire inode. So set this flag so we write out | |
8483 | * all of the extents in the inode to the sync log so we're completely | |
8484 | * safe. | |
8485 | * | |
8486 | * If no extents were dropped or trimmed we don't need to force the next | |
8487 | * fsync to truncate all the inode's items from the log and re-log them | |
8488 | * all. This means the truncate operation did not change the file size, | |
8489 | * or changed it to a smaller size but there was only an implicit hole | |
8490 | * between the old i_size and the new i_size, and there were no prealloc | |
8491 | * extents beyond i_size to drop. | |
8492 | */ | |
d9ac19c3 | 8493 | if (control.extents_found > 0) |
d9dcae67 | 8494 | btrfs_set_inode_full_sync(inode); |
fcb80c2a | 8495 | |
ad7e1a74 | 8496 | return ret; |
39279cc3 CM |
8497 | } |
8498 | ||
f2d40141 | 8499 | struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap, |
a1fd0c35 OS |
8500 | struct inode *dir) |
8501 | { | |
8502 | struct inode *inode; | |
8503 | ||
8504 | inode = new_inode(dir->i_sb); | |
8505 | if (inode) { | |
8506 | /* | |
8507 | * Subvolumes don't inherit the sgid bit or the parent's gid if | |
8508 | * the parent's sgid bit is set. This is probably a bug. | |
8509 | */ | |
f2d40141 | 8510 | inode_init_owner(idmap, inode, NULL, |
a1fd0c35 OS |
8511 | S_IFDIR | (~current_umask() & S_IRWXUGO)); |
8512 | inode->i_op = &btrfs_dir_inode_operations; | |
8513 | inode->i_fop = &btrfs_dir_file_operations; | |
8514 | } | |
8515 | return inode; | |
8516 | } | |
8517 | ||
39279cc3 CM |
8518 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8519 | { | |
69fe2d75 | 8520 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8521 | struct btrfs_inode *ei; |
2ead6ae7 | 8522 | struct inode *inode; |
637e6e0f DS |
8523 | struct extent_io_tree *file_extent_tree = NULL; |
8524 | ||
8525 | /* Self tests may pass a NULL fs_info. */ | |
8526 | if (fs_info && !btrfs_fs_incompat(fs_info, NO_HOLES)) { | |
8527 | file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL); | |
8528 | if (!file_extent_tree) | |
8529 | return NULL; | |
8530 | } | |
39279cc3 | 8531 | |
fd60b288 | 8532 | ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL); |
637e6e0f DS |
8533 | if (!ei) { |
8534 | kfree(file_extent_tree); | |
39279cc3 | 8535 | return NULL; |
637e6e0f | 8536 | } |
2ead6ae7 YZ |
8537 | |
8538 | ei->root = NULL; | |
2ead6ae7 | 8539 | ei->generation = 0; |
15ee9bc7 | 8540 | ei->last_trans = 0; |
257c62e1 | 8541 | ei->last_sub_trans = 0; |
e02119d5 | 8542 | ei->logged_trans = 0; |
2ead6ae7 | 8543 | ei->delalloc_bytes = 0; |
a7e3b975 | 8544 | ei->new_delalloc_bytes = 0; |
47059d93 | 8545 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8546 | ei->disk_i_size = 0; |
8547 | ei->flags = 0; | |
77eea05e | 8548 | ei->ro_flags = 0; |
7709cde3 | 8549 | ei->csum_bytes = 0; |
2ead6ae7 | 8550 | ei->index_cnt = (u64)-1; |
67de1176 | 8551 | ei->dir_index = 0; |
2ead6ae7 | 8552 | ei->last_unlink_trans = 0; |
3ebac17c | 8553 | ei->last_reflink_trans = 0; |
46d8bc34 | 8554 | ei->last_log_commit = 0; |
2ead6ae7 | 8555 | |
9e0baf60 JB |
8556 | spin_lock_init(&ei->lock); |
8557 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8558 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8559 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8560 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8561 | ei->runtime_flags = 0; |
b52aa8c9 | 8562 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8563 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8564 | |
16cdcec7 MX |
8565 | ei->delayed_node = NULL; |
8566 | ||
c6e8f898 DS |
8567 | ei->i_otime_sec = 0; |
8568 | ei->i_otime_nsec = 0; | |
9cc97d64 | 8569 | |
2ead6ae7 | 8570 | inode = &ei->vfs_inode; |
a8067e02 | 8571 | extent_map_tree_init(&ei->extent_tree); |
738290c0 DS |
8572 | |
8573 | /* This io tree sets the valid inode. */ | |
35da5a7e | 8574 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO); |
0988fc7b | 8575 | ei->io_tree.inode = ei; |
738290c0 | 8576 | |
637e6e0f DS |
8577 | ei->file_extent_tree = file_extent_tree; |
8578 | if (file_extent_tree) { | |
8579 | extent_io_tree_init(fs_info, ei->file_extent_tree, | |
8580 | IO_TREE_INODE_FILE_EXTENT); | |
8581 | /* Lockdep class is set only for the file extent tree. */ | |
8582 | lockdep_set_class(&ei->file_extent_tree->lock, &file_extent_tree_class); | |
8583 | } | |
2ead6ae7 | 8584 | mutex_init(&ei->log_mutex); |
54c65371 DS |
8585 | spin_lock_init(&ei->ordered_tree_lock); |
8586 | ei->ordered_tree = RB_ROOT; | |
8587 | ei->ordered_tree_last = NULL; | |
2ead6ae7 | 8588 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8589 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8590 | RB_CLEAR_NODE(&ei->rb_node); |
8318ba79 | 8591 | init_rwsem(&ei->i_mmap_lock); |
2ead6ae7 YZ |
8592 | |
8593 | return inode; | |
39279cc3 CM |
8594 | } |
8595 | ||
aaedb55b JB |
8596 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8597 | void btrfs_test_destroy_inode(struct inode *inode) | |
8598 | { | |
4c0c8cfc | 8599 | btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); |
637e6e0f | 8600 | kfree(BTRFS_I(inode)->file_extent_tree); |
aaedb55b JB |
8601 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8602 | } | |
8603 | #endif | |
8604 | ||
26602cab | 8605 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8606 | { |
637e6e0f | 8607 | kfree(BTRFS_I(inode)->file_extent_tree); |
fa0d7e3d NP |
8608 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8609 | } | |
8610 | ||
633cc816 | 8611 | void btrfs_destroy_inode(struct inode *vfs_inode) |
39279cc3 | 8612 | { |
e6dcd2dc | 8613 | struct btrfs_ordered_extent *ordered; |
633cc816 NB |
8614 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
8615 | struct btrfs_root *root = inode->root; | |
5f4403e1 | 8616 | bool freespace_inode; |
5a3f23d5 | 8617 | |
633cc816 NB |
8618 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
8619 | WARN_ON(vfs_inode->i_data.nrpages); | |
8620 | WARN_ON(inode->block_rsv.reserved); | |
8621 | WARN_ON(inode->block_rsv.size); | |
8622 | WARN_ON(inode->outstanding_extents); | |
dc287224 FM |
8623 | if (!S_ISDIR(vfs_inode->i_mode)) { |
8624 | WARN_ON(inode->delalloc_bytes); | |
8625 | WARN_ON(inode->new_delalloc_bytes); | |
8626 | } | |
633cc816 NB |
8627 | WARN_ON(inode->csum_bytes); |
8628 | WARN_ON(inode->defrag_bytes); | |
39279cc3 | 8629 | |
a6dbd429 JB |
8630 | /* |
8631 | * This can happen where we create an inode, but somebody else also | |
8632 | * created the same inode and we need to destroy the one we already | |
8633 | * created. | |
8634 | */ | |
8635 | if (!root) | |
26602cab | 8636 | return; |
a6dbd429 | 8637 | |
5f4403e1 IA |
8638 | /* |
8639 | * If this is a free space inode do not take the ordered extents lockdep | |
8640 | * map. | |
8641 | */ | |
8642 | freespace_inode = btrfs_is_free_space_inode(inode); | |
8643 | ||
d397712b | 8644 | while (1) { |
633cc816 | 8645 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
e6dcd2dc CM |
8646 | if (!ordered) |
8647 | break; | |
8648 | else { | |
633cc816 | 8649 | btrfs_err(root->fs_info, |
5d163e0e | 8650 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8651 | ordered->file_offset, ordered->num_bytes); |
5f4403e1 IA |
8652 | |
8653 | if (!freespace_inode) | |
8654 | btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent); | |
8655 | ||
71fe0a55 | 8656 | btrfs_remove_ordered_extent(inode, ordered); |
e6dcd2dc CM |
8657 | btrfs_put_ordered_extent(ordered); |
8658 | btrfs_put_ordered_extent(ordered); | |
8659 | } | |
8660 | } | |
633cc816 NB |
8661 | btrfs_qgroup_check_reserved_leak(inode); |
8662 | inode_tree_del(inode); | |
4c0c8cfc | 8663 | btrfs_drop_extent_map_range(inode, 0, (u64)-1, false); |
633cc816 NB |
8664 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
8665 | btrfs_put_root(inode->root); | |
39279cc3 CM |
8666 | } |
8667 | ||
45321ac5 | 8668 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8669 | { |
8670 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8671 | |
6379ef9f NA |
8672 | if (root == NULL) |
8673 | return 1; | |
8674 | ||
fa6ac876 | 8675 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8676 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8677 | return 1; |
76dda93c | 8678 | else |
45321ac5 | 8679 | return generic_drop_inode(inode); |
76dda93c YZ |
8680 | } |
8681 | ||
0ee0fda0 | 8682 | static void init_once(void *foo) |
39279cc3 | 8683 | { |
0d031dc4 | 8684 | struct btrfs_inode *ei = foo; |
39279cc3 CM |
8685 | |
8686 | inode_init_once(&ei->vfs_inode); | |
8687 | } | |
8688 | ||
e67c718b | 8689 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8690 | { |
8c0a8537 KS |
8691 | /* |
8692 | * Make sure all delayed rcu free inodes are flushed before we | |
8693 | * destroy cache. | |
8694 | */ | |
8695 | rcu_barrier(); | |
642c5d34 | 8696 | bioset_exit(&btrfs_dio_bioset); |
5598e900 | 8697 | kmem_cache_destroy(btrfs_inode_cachep); |
39279cc3 CM |
8698 | } |
8699 | ||
f5c29bd9 | 8700 | int __init btrfs_init_cachep(void) |
39279cc3 | 8701 | { |
837e1972 | 8702 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8703 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8704 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8705 | init_once); | |
39279cc3 CM |
8706 | if (!btrfs_inode_cachep) |
8707 | goto fail; | |
9601e3f6 | 8708 | |
642c5d34 | 8709 | if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE, |
67d66982 | 8710 | offsetof(struct btrfs_dio_private, bbio.bio), |
642c5d34 CH |
8711 | BIOSET_NEED_BVECS)) |
8712 | goto fail; | |
8713 | ||
39279cc3 CM |
8714 | return 0; |
8715 | fail: | |
8716 | btrfs_destroy_cachep(); | |
8717 | return -ENOMEM; | |
8718 | } | |
8719 | ||
b74d24f7 | 8720 | static int btrfs_getattr(struct mnt_idmap *idmap, |
549c7297 | 8721 | const struct path *path, struct kstat *stat, |
a528d35e | 8722 | u32 request_mask, unsigned int flags) |
39279cc3 | 8723 | { |
df0af1a5 | 8724 | u64 delalloc_bytes; |
2766ff61 | 8725 | u64 inode_bytes; |
a528d35e | 8726 | struct inode *inode = d_inode(path->dentry); |
4e00422e | 8727 | u32 blocksize = btrfs_sb(inode->i_sb)->sectorsize; |
04a87e34 | 8728 | u32 bi_flags = BTRFS_I(inode)->flags; |
14605409 | 8729 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
04a87e34 YS |
8730 | |
8731 | stat->result_mask |= STATX_BTIME; | |
c6e8f898 DS |
8732 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec; |
8733 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec; | |
04a87e34 YS |
8734 | if (bi_flags & BTRFS_INODE_APPEND) |
8735 | stat->attributes |= STATX_ATTR_APPEND; | |
8736 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8737 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8738 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8739 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8740 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8741 | stat->attributes |= STATX_ATTR_NODUMP; | |
14605409 BB |
8742 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
8743 | stat->attributes |= STATX_ATTR_VERITY; | |
04a87e34 YS |
8744 | |
8745 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8746 | STATX_ATTR_COMPRESSED | | |
8747 | STATX_ATTR_IMMUTABLE | | |
8748 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8749 | |
0d72b928 | 8750 | generic_fillattr(idmap, request_mask, inode, stat); |
0ee5dc67 | 8751 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8752 | |
8753 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8754 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
2766ff61 | 8755 | inode_bytes = inode_get_bytes(inode); |
df0af1a5 | 8756 | spin_unlock(&BTRFS_I(inode)->lock); |
2766ff61 | 8757 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
29e70be2 | 8758 | ALIGN(delalloc_bytes, blocksize)) >> SECTOR_SHIFT; |
39279cc3 CM |
8759 | return 0; |
8760 | } | |
8761 | ||
cdd1fedf DF |
8762 | static int btrfs_rename_exchange(struct inode *old_dir, |
8763 | struct dentry *old_dentry, | |
8764 | struct inode *new_dir, | |
8765 | struct dentry *new_dentry) | |
8766 | { | |
41044b41 | 8767 | struct btrfs_fs_info *fs_info = inode_to_fs_info(old_dir); |
cdd1fedf | 8768 | struct btrfs_trans_handle *trans; |
c1621871 | 8769 | unsigned int trans_num_items; |
cdd1fedf DF |
8770 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
8771 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
8772 | struct inode *new_inode = new_dentry->d_inode; | |
8773 | struct inode *old_inode = old_dentry->d_inode; | |
88d2beec FM |
8774 | struct btrfs_rename_ctx old_rename_ctx; |
8775 | struct btrfs_rename_ctx new_rename_ctx; | |
4a0cc7ca NB |
8776 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
8777 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
8778 | u64 old_idx = 0; |
8779 | u64 new_idx = 0; | |
cdd1fedf | 8780 | int ret; |
75b463d2 | 8781 | int ret2; |
dc09ef35 | 8782 | bool need_abort = false; |
ab3c5c18 | 8783 | struct fscrypt_name old_fname, new_fname; |
6db75318 | 8784 | struct fscrypt_str *old_name, *new_name; |
cdd1fedf | 8785 | |
3f79f6f6 N |
8786 | /* |
8787 | * For non-subvolumes allow exchange only within one subvolume, in the | |
8788 | * same inode namespace. Two subvolumes (represented as directory) can | |
8789 | * be exchanged as they're a logical link and have a fixed inode number. | |
8790 | */ | |
8791 | if (root != dest && | |
8792 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || | |
8793 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) | |
cdd1fedf DF |
8794 | return -EXDEV; |
8795 | ||
ab3c5c18 STD |
8796 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
8797 | if (ret) | |
8798 | return ret; | |
8799 | ||
8800 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); | |
8801 | if (ret) { | |
8802 | fscrypt_free_filename(&old_fname); | |
8803 | return ret; | |
8804 | } | |
8805 | ||
6db75318 STD |
8806 | old_name = &old_fname.disk_name; |
8807 | new_name = &new_fname.disk_name; | |
ab3c5c18 | 8808 | |
cdd1fedf | 8809 | /* close the race window with snapshot create/destroy ioctl */ |
943eb3bf JB |
8810 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
8811 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8812 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8813 | |
8814 | /* | |
c1621871 OS |
8815 | * For each inode: |
8816 | * 1 to remove old dir item | |
8817 | * 1 to remove old dir index | |
8818 | * 1 to add new dir item | |
8819 | * 1 to add new dir index | |
8820 | * 1 to update parent inode | |
8821 | * | |
8822 | * If the parents are the same, we only need to account for one | |
cdd1fedf | 8823 | */ |
c1621871 OS |
8824 | trans_num_items = (old_dir == new_dir ? 9 : 10); |
8825 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8826 | /* | |
8827 | * 1 to remove old root ref | |
8828 | * 1 to remove old root backref | |
8829 | * 1 to add new root ref | |
8830 | * 1 to add new root backref | |
8831 | */ | |
8832 | trans_num_items += 4; | |
8833 | } else { | |
8834 | /* | |
8835 | * 1 to update inode item | |
8836 | * 1 to remove old inode ref | |
8837 | * 1 to add new inode ref | |
8838 | */ | |
8839 | trans_num_items += 3; | |
8840 | } | |
8841 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
8842 | trans_num_items += 4; | |
8843 | else | |
8844 | trans_num_items += 3; | |
8845 | trans = btrfs_start_transaction(root, trans_num_items); | |
cdd1fedf DF |
8846 | if (IS_ERR(trans)) { |
8847 | ret = PTR_ERR(trans); | |
8848 | goto out_notrans; | |
8849 | } | |
8850 | ||
00aa8e87 JB |
8851 | if (dest != root) { |
8852 | ret = btrfs_record_root_in_trans(trans, dest); | |
8853 | if (ret) | |
8854 | goto out_fail; | |
8855 | } | |
3e174099 | 8856 | |
cdd1fedf DF |
8857 | /* |
8858 | * We need to find a free sequence number both in the source and | |
8859 | * in the destination directory for the exchange. | |
8860 | */ | |
877574e2 | 8861 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
8862 | if (ret) |
8863 | goto out_fail; | |
877574e2 | 8864 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
8865 | if (ret) |
8866 | goto out_fail; | |
8867 | ||
8868 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
8869 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
8870 | ||
8871 | /* Reference for the source. */ | |
8872 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8873 | /* force full log commit if subvolume involved. */ | |
90787766 | 8874 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8875 | } else { |
6db75318 | 8876 | ret = btrfs_insert_inode_ref(trans, dest, new_name, old_ino, |
f85b7379 DS |
8877 | btrfs_ino(BTRFS_I(new_dir)), |
8878 | old_idx); | |
cdd1fedf DF |
8879 | if (ret) |
8880 | goto out_fail; | |
dc09ef35 | 8881 | need_abort = true; |
cdd1fedf DF |
8882 | } |
8883 | ||
8884 | /* And now for the dest. */ | |
8885 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8886 | /* force full log commit if subvolume involved. */ | |
90787766 | 8887 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8888 | } else { |
6db75318 | 8889 | ret = btrfs_insert_inode_ref(trans, root, old_name, new_ino, |
f85b7379 DS |
8890 | btrfs_ino(BTRFS_I(old_dir)), |
8891 | new_idx); | |
dc09ef35 JB |
8892 | if (ret) { |
8893 | if (need_abort) | |
8894 | btrfs_abort_transaction(trans, ret); | |
cdd1fedf | 8895 | goto out_fail; |
dc09ef35 | 8896 | } |
cdd1fedf DF |
8897 | } |
8898 | ||
8899 | /* Update inode version and ctime/mtime. */ | |
8900 | inode_inc_iversion(old_dir); | |
8901 | inode_inc_iversion(new_dir); | |
8902 | inode_inc_iversion(old_inode); | |
8903 | inode_inc_iversion(new_inode); | |
130f1eca | 8904 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); |
cdd1fedf DF |
8905 | |
8906 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 | 8907 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
59fcf388 | 8908 | BTRFS_I(old_inode), true); |
f85b7379 | 8909 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
59fcf388 | 8910 | BTRFS_I(new_inode), true); |
cdd1fedf DF |
8911 | } |
8912 | ||
8913 | /* src is a subvolume */ | |
8914 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
5b7544cb | 8915 | ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); |
cdd1fedf | 8916 | } else { /* src is an inode */ |
4467af88 | 8917 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
4ec5934e | 8918 | BTRFS_I(old_dentry->d_inode), |
6db75318 | 8919 | old_name, &old_rename_ctx); |
cdd1fedf | 8920 | if (!ret) |
8b9d0322 | 8921 | ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); |
cdd1fedf DF |
8922 | } |
8923 | if (ret) { | |
66642832 | 8924 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8925 | goto out_fail; |
8926 | } | |
8927 | ||
8928 | /* dest is a subvolume */ | |
8929 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
5b7544cb | 8930 | ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); |
cdd1fedf | 8931 | } else { /* dest is an inode */ |
4467af88 | 8932 | ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 8933 | BTRFS_I(new_dentry->d_inode), |
6db75318 | 8934 | new_name, &new_rename_ctx); |
cdd1fedf | 8935 | if (!ret) |
8b9d0322 | 8936 | ret = btrfs_update_inode(trans, BTRFS_I(new_inode)); |
cdd1fedf DF |
8937 | } |
8938 | if (ret) { | |
66642832 | 8939 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8940 | goto out_fail; |
8941 | } | |
8942 | ||
db0a669f | 8943 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 8944 | new_name, 0, old_idx); |
cdd1fedf | 8945 | if (ret) { |
66642832 | 8946 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8947 | goto out_fail; |
8948 | } | |
8949 | ||
db0a669f | 8950 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
6db75318 | 8951 | old_name, 0, new_idx); |
cdd1fedf | 8952 | if (ret) { |
66642832 | 8953 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8954 | goto out_fail; |
8955 | } | |
8956 | ||
8957 | if (old_inode->i_nlink == 1) | |
8958 | BTRFS_I(old_inode)->dir_index = old_idx; | |
8959 | if (new_inode->i_nlink == 1) | |
8960 | BTRFS_I(new_inode)->dir_index = new_idx; | |
8961 | ||
259c4b96 FM |
8962 | /* |
8963 | * Now pin the logs of the roots. We do it to ensure that no other task | |
8964 | * can sync the logs while we are in progress with the rename, because | |
8965 | * that could result in an inconsistency in case any of the inodes that | |
8966 | * are part of this rename operation were logged before. | |
8967 | */ | |
8968 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8969 | btrfs_pin_log_trans(root); | |
8970 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8971 | btrfs_pin_log_trans(dest); | |
8972 | ||
8973 | /* Do the log updates for all inodes. */ | |
8974 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
d5f5bd54 | 8975 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 8976 | old_rename_ctx.index, new_dentry->d_parent); |
259c4b96 | 8977 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 8978 | btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir), |
88d2beec | 8979 | new_rename_ctx.index, old_dentry->d_parent); |
259c4b96 FM |
8980 | |
8981 | /* Now unpin the logs. */ | |
8982 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) | |
8983 | btrfs_end_log_trans(root); | |
8984 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) | |
cdd1fedf | 8985 | btrfs_end_log_trans(dest); |
cdd1fedf | 8986 | out_fail: |
75b463d2 FM |
8987 | ret2 = btrfs_end_transaction(trans); |
8988 | ret = ret ? ret : ret2; | |
cdd1fedf | 8989 | out_notrans: |
943eb3bf JB |
8990 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
8991 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8992 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 8993 | |
ab3c5c18 STD |
8994 | fscrypt_free_filename(&new_fname); |
8995 | fscrypt_free_filename(&old_fname); | |
cdd1fedf DF |
8996 | return ret; |
8997 | } | |
8998 | ||
f2d40141 | 8999 | static struct inode *new_whiteout_inode(struct mnt_idmap *idmap, |
a1fd0c35 OS |
9000 | struct inode *dir) |
9001 | { | |
9002 | struct inode *inode; | |
9003 | ||
9004 | inode = new_inode(dir->i_sb); | |
9005 | if (inode) { | |
f2d40141 | 9006 | inode_init_owner(idmap, inode, dir, |
a1fd0c35 OS |
9007 | S_IFCHR | WHITEOUT_MODE); |
9008 | inode->i_op = &btrfs_special_inode_operations; | |
9009 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
9010 | } | |
9011 | return inode; | |
9012 | } | |
9013 | ||
f2d40141 | 9014 | static int btrfs_rename(struct mnt_idmap *idmap, |
ca07274c CB |
9015 | struct inode *old_dir, struct dentry *old_dentry, |
9016 | struct inode *new_dir, struct dentry *new_dentry, | |
9017 | unsigned int flags) | |
39279cc3 | 9018 | { |
41044b41 | 9019 | struct btrfs_fs_info *fs_info = inode_to_fs_info(old_dir); |
3538d68d OS |
9020 | struct btrfs_new_inode_args whiteout_args = { |
9021 | .dir = old_dir, | |
9022 | .dentry = old_dentry, | |
9023 | }; | |
39279cc3 | 9024 | struct btrfs_trans_handle *trans; |
5062af35 | 9025 | unsigned int trans_num_items; |
39279cc3 | 9026 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9027 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9028 | struct inode *new_inode = d_inode(new_dentry); |
9029 | struct inode *old_inode = d_inode(old_dentry); | |
88d2beec | 9030 | struct btrfs_rename_ctx rename_ctx; |
00e4e6b3 | 9031 | u64 index = 0; |
39279cc3 | 9032 | int ret; |
75b463d2 | 9033 | int ret2; |
4a0cc7ca | 9034 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
ab3c5c18 | 9035 | struct fscrypt_name old_fname, new_fname; |
39279cc3 | 9036 | |
4a0cc7ca | 9037 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9038 | return -EPERM; |
9039 | ||
4df27c4d | 9040 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9041 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9042 | return -EXDEV; |
9043 | ||
33345d01 | 9044 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9045 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9046 | return -ENOTEMPTY; |
5f39d397 | 9047 | |
4df27c4d YZ |
9048 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9049 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9050 | return -ENOTEMPTY; | |
9c52057c | 9051 | |
ab3c5c18 STD |
9052 | ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); |
9053 | if (ret) | |
9054 | return ret; | |
9c52057c | 9055 | |
ab3c5c18 STD |
9056 | ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); |
9057 | if (ret) { | |
9058 | fscrypt_free_filename(&old_fname); | |
9059 | return ret; | |
9060 | } | |
9c52057c | 9061 | |
9c52057c | 9062 | /* check for collisions, even if the name isn't there */ |
6db75318 | 9063 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name); |
9c52057c CM |
9064 | if (ret) { |
9065 | if (ret == -EEXIST) { | |
9066 | /* we shouldn't get | |
9067 | * eexist without a new_inode */ | |
fae7f21c | 9068 | if (WARN_ON(!new_inode)) { |
ab3c5c18 | 9069 | goto out_fscrypt_names; |
9c52057c CM |
9070 | } |
9071 | } else { | |
9072 | /* maybe -EOVERFLOW */ | |
ab3c5c18 | 9073 | goto out_fscrypt_names; |
9c52057c CM |
9074 | } |
9075 | } | |
9076 | ret = 0; | |
9077 | ||
5a3f23d5 | 9078 | /* |
8d875f95 CM |
9079 | * we're using rename to replace one file with another. Start IO on it |
9080 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9081 | */ |
8d875f95 | 9082 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9083 | filemap_flush(old_inode->i_mapping); |
9084 | ||
a1fd0c35 | 9085 | if (flags & RENAME_WHITEOUT) { |
f2d40141 | 9086 | whiteout_args.inode = new_whiteout_inode(idmap, old_dir); |
abe3bf74 CJ |
9087 | if (!whiteout_args.inode) { |
9088 | ret = -ENOMEM; | |
9089 | goto out_fscrypt_names; | |
9090 | } | |
3538d68d OS |
9091 | ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items); |
9092 | if (ret) | |
9093 | goto out_whiteout_inode; | |
9094 | } else { | |
9095 | /* 1 to update the old parent inode. */ | |
9096 | trans_num_items = 1; | |
a1fd0c35 OS |
9097 | } |
9098 | ||
c1621871 OS |
9099 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
9100 | /* Close the race window with snapshot create/destroy ioctl */ | |
0b246afa | 9101 | down_read(&fs_info->subvol_sem); |
c1621871 OS |
9102 | /* |
9103 | * 1 to remove old root ref | |
9104 | * 1 to remove old root backref | |
9105 | * 1 to add new root ref | |
9106 | * 1 to add new root backref | |
9107 | */ | |
3538d68d | 9108 | trans_num_items += 4; |
c1621871 OS |
9109 | } else { |
9110 | /* | |
9111 | * 1 to update inode | |
9112 | * 1 to remove old inode ref | |
9113 | * 1 to add new inode ref | |
9114 | */ | |
3538d68d | 9115 | trans_num_items += 3; |
c1621871 | 9116 | } |
a22285a6 | 9117 | /* |
c1621871 OS |
9118 | * 1 to remove old dir item |
9119 | * 1 to remove old dir index | |
c1621871 OS |
9120 | * 1 to add new dir item |
9121 | * 1 to add new dir index | |
a22285a6 | 9122 | */ |
3538d68d OS |
9123 | trans_num_items += 4; |
9124 | /* 1 to update new parent inode if it's not the same as the old parent */ | |
c1621871 OS |
9125 | if (new_dir != old_dir) |
9126 | trans_num_items++; | |
9127 | if (new_inode) { | |
9128 | /* | |
9129 | * 1 to update inode | |
9130 | * 1 to remove inode ref | |
9131 | * 1 to remove dir item | |
9132 | * 1 to remove dir index | |
9133 | * 1 to possibly add orphan item | |
9134 | */ | |
9135 | trans_num_items += 5; | |
9136 | } | |
5062af35 | 9137 | trans = btrfs_start_transaction(root, trans_num_items); |
b44c59a8 | 9138 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9139 | ret = PTR_ERR(trans); |
9140 | goto out_notrans; | |
9141 | } | |
76dda93c | 9142 | |
b0fec6fd JB |
9143 | if (dest != root) { |
9144 | ret = btrfs_record_root_in_trans(trans, dest); | |
9145 | if (ret) | |
9146 | goto out_fail; | |
9147 | } | |
5f39d397 | 9148 | |
877574e2 | 9149 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9150 | if (ret) |
9151 | goto out_fail; | |
5a3f23d5 | 9152 | |
67de1176 | 9153 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9154 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9155 | /* force full log commit if subvolume involved. */ |
90787766 | 9156 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9157 | } else { |
6db75318 STD |
9158 | ret = btrfs_insert_inode_ref(trans, dest, &new_fname.disk_name, |
9159 | old_ino, btrfs_ino(BTRFS_I(new_dir)), | |
9160 | index); | |
a5719521 YZ |
9161 | if (ret) |
9162 | goto out_fail; | |
4df27c4d | 9163 | } |
5a3f23d5 | 9164 | |
0c4d2d95 JB |
9165 | inode_inc_iversion(old_dir); |
9166 | inode_inc_iversion(new_dir); | |
9167 | inode_inc_iversion(old_inode); | |
130f1eca | 9168 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); |
5f39d397 | 9169 | |
12fcfd22 | 9170 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 | 9171 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
59fcf388 | 9172 | BTRFS_I(old_inode), true); |
12fcfd22 | 9173 | |
33345d01 | 9174 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
5b7544cb | 9175 | ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); |
4df27c4d | 9176 | } else { |
4467af88 | 9177 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
e43eec81 | 9178 | BTRFS_I(d_inode(old_dentry)), |
6db75318 | 9179 | &old_fname.disk_name, &rename_ctx); |
92986796 | 9180 | if (!ret) |
8b9d0322 | 9181 | ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); |
4df27c4d | 9182 | } |
79787eaa | 9183 | if (ret) { |
66642832 | 9184 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9185 | goto out_fail; |
9186 | } | |
39279cc3 CM |
9187 | |
9188 | if (new_inode) { | |
0c4d2d95 | 9189 | inode_inc_iversion(new_inode); |
4a0cc7ca | 9190 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9191 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
5b7544cb | 9192 | ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); |
4df27c4d YZ |
9193 | BUG_ON(new_inode->i_nlink == 0); |
9194 | } else { | |
4467af88 | 9195 | ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
4ec5934e | 9196 | BTRFS_I(d_inode(new_dentry)), |
6db75318 | 9197 | &new_fname.disk_name); |
4df27c4d | 9198 | } |
4ef31a45 | 9199 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9200 | ret = btrfs_orphan_add(trans, |
9201 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9202 | if (ret) { |
66642832 | 9203 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9204 | goto out_fail; |
9205 | } | |
39279cc3 | 9206 | } |
aec7477b | 9207 | |
db0a669f | 9208 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
6db75318 | 9209 | &new_fname.disk_name, 0, index); |
79787eaa | 9210 | if (ret) { |
66642832 | 9211 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9212 | goto out_fail; |
9213 | } | |
39279cc3 | 9214 | |
67de1176 MX |
9215 | if (old_inode->i_nlink == 1) |
9216 | BTRFS_I(old_inode)->dir_index = index; | |
9217 | ||
259c4b96 | 9218 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) |
d5f5bd54 | 9219 | btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir), |
88d2beec | 9220 | rename_ctx.index, new_dentry->d_parent); |
cdd1fedf DF |
9221 | |
9222 | if (flags & RENAME_WHITEOUT) { | |
caae78e0 | 9223 | ret = btrfs_create_new_inode(trans, &whiteout_args); |
cdd1fedf | 9224 | if (ret) { |
66642832 | 9225 | btrfs_abort_transaction(trans, ret); |
cdd1fedf | 9226 | goto out_fail; |
caae78e0 OS |
9227 | } else { |
9228 | unlock_new_inode(whiteout_args.inode); | |
9229 | iput(whiteout_args.inode); | |
9230 | whiteout_args.inode = NULL; | |
cdd1fedf | 9231 | } |
4df27c4d | 9232 | } |
39279cc3 | 9233 | out_fail: |
75b463d2 FM |
9234 | ret2 = btrfs_end_transaction(trans); |
9235 | ret = ret ? ret : ret2; | |
b44c59a8 | 9236 | out_notrans: |
33345d01 | 9237 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9238 | up_read(&fs_info->subvol_sem); |
a1fd0c35 | 9239 | if (flags & RENAME_WHITEOUT) |
3538d68d OS |
9240 | btrfs_new_inode_args_destroy(&whiteout_args); |
9241 | out_whiteout_inode: | |
9242 | if (flags & RENAME_WHITEOUT) | |
9243 | iput(whiteout_args.inode); | |
ab3c5c18 STD |
9244 | out_fscrypt_names: |
9245 | fscrypt_free_filename(&old_fname); | |
9246 | fscrypt_free_filename(&new_fname); | |
39279cc3 CM |
9247 | return ret; |
9248 | } | |
9249 | ||
e18275ae | 9250 | static int btrfs_rename2(struct mnt_idmap *idmap, struct inode *old_dir, |
549c7297 CB |
9251 | struct dentry *old_dentry, struct inode *new_dir, |
9252 | struct dentry *new_dentry, unsigned int flags) | |
80ace85c | 9253 | { |
ca6dee6b FM |
9254 | int ret; |
9255 | ||
cdd1fedf | 9256 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9257 | return -EINVAL; |
9258 | ||
cdd1fedf | 9259 | if (flags & RENAME_EXCHANGE) |
ca6dee6b FM |
9260 | ret = btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
9261 | new_dentry); | |
9262 | else | |
f2d40141 | 9263 | ret = btrfs_rename(idmap, old_dir, old_dentry, new_dir, |
ca6dee6b | 9264 | new_dentry, flags); |
cdd1fedf | 9265 | |
ca6dee6b FM |
9266 | btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info); |
9267 | ||
9268 | return ret; | |
80ace85c MS |
9269 | } |
9270 | ||
3a2f8c07 NB |
9271 | struct btrfs_delalloc_work { |
9272 | struct inode *inode; | |
9273 | struct completion completion; | |
9274 | struct list_head list; | |
9275 | struct btrfs_work work; | |
9276 | }; | |
9277 | ||
8ccf6f19 MX |
9278 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9279 | { | |
9280 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9281 | struct inode *inode; |
8ccf6f19 MX |
9282 | |
9283 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9284 | work); | |
9f23e289 | 9285 | inode = delalloc_work->inode; |
30424601 DS |
9286 | filemap_flush(inode->i_mapping); |
9287 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9288 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9289 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9290 | |
076da91c | 9291 | iput(inode); |
8ccf6f19 MX |
9292 | complete(&delalloc_work->completion); |
9293 | } | |
9294 | ||
3a2f8c07 | 9295 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9296 | { |
9297 | struct btrfs_delalloc_work *work; | |
9298 | ||
100d5702 | 9299 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9300 | if (!work) |
9301 | return NULL; | |
9302 | ||
9303 | init_completion(&work->completion); | |
9304 | INIT_LIST_HEAD(&work->list); | |
9305 | work->inode = inode; | |
078b8b90 | 9306 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL); |
8ccf6f19 MX |
9307 | |
9308 | return work; | |
9309 | } | |
9310 | ||
d352ac68 CM |
9311 | /* |
9312 | * some fairly slow code that needs optimization. This walks the list | |
9313 | * of all the inodes with pending delalloc and forces them to disk. | |
9314 | */ | |
e076ab2a JB |
9315 | static int start_delalloc_inodes(struct btrfs_root *root, |
9316 | struct writeback_control *wbc, bool snapshot, | |
3d45f221 | 9317 | bool in_reclaim_context) |
ea8c2819 | 9318 | { |
ea8c2819 | 9319 | struct btrfs_inode *binode; |
5b21f2ed | 9320 | struct inode *inode; |
8ccf6f19 | 9321 | struct btrfs_delalloc_work *work, *next; |
84af994b RJ |
9322 | LIST_HEAD(works); |
9323 | LIST_HEAD(splice); | |
8ccf6f19 | 9324 | int ret = 0; |
e076ab2a | 9325 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
ea8c2819 | 9326 | |
573bfb72 | 9327 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9328 | spin_lock(&root->delalloc_lock); |
9329 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9330 | while (!list_empty(&splice)) { |
9331 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9332 | delalloc_inodes); |
1eafa6c7 | 9333 | |
eb73c1b7 MX |
9334 | list_move_tail(&binode->delalloc_inodes, |
9335 | &root->delalloc_inodes); | |
3d45f221 FM |
9336 | |
9337 | if (in_reclaim_context && | |
9338 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) | |
9339 | continue; | |
9340 | ||
5b21f2ed | 9341 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9342 | if (!inode) { |
eb73c1b7 | 9343 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9344 | continue; |
df0af1a5 | 9345 | } |
eb73c1b7 | 9346 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9347 | |
3cd24c69 EL |
9348 | if (snapshot) |
9349 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9350 | &binode->runtime_flags); | |
e076ab2a JB |
9351 | if (full_flush) { |
9352 | work = btrfs_alloc_delalloc_work(inode); | |
9353 | if (!work) { | |
9354 | iput(inode); | |
9355 | ret = -ENOMEM; | |
9356 | goto out; | |
9357 | } | |
9358 | list_add_tail(&work->list, &works); | |
9359 | btrfs_queue_work(root->fs_info->flush_workers, | |
9360 | &work->work); | |
9361 | } else { | |
b3776305 | 9362 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
e55cf7ca | 9363 | btrfs_add_delayed_iput(BTRFS_I(inode)); |
e076ab2a | 9364 | if (ret || wbc->nr_to_write <= 0) |
b4912139 JB |
9365 | goto out; |
9366 | } | |
5b21f2ed | 9367 | cond_resched(); |
eb73c1b7 | 9368 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9369 | } |
eb73c1b7 | 9370 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9371 | |
a1ecaabb | 9372 | out: |
eb73c1b7 MX |
9373 | list_for_each_entry_safe(work, next, &works, list) { |
9374 | list_del_init(&work->list); | |
40012f96 NB |
9375 | wait_for_completion(&work->completion); |
9376 | kfree(work); | |
eb73c1b7 MX |
9377 | } |
9378 | ||
81f1d390 | 9379 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9380 | spin_lock(&root->delalloc_lock); |
9381 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9382 | spin_unlock(&root->delalloc_lock); | |
9383 | } | |
573bfb72 | 9384 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9385 | return ret; |
9386 | } | |
1eafa6c7 | 9387 | |
f9baa501 | 9388 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context) |
eb73c1b7 | 9389 | { |
e076ab2a JB |
9390 | struct writeback_control wbc = { |
9391 | .nr_to_write = LONG_MAX, | |
9392 | .sync_mode = WB_SYNC_NONE, | |
9393 | .range_start = 0, | |
9394 | .range_end = LLONG_MAX, | |
9395 | }; | |
0b246afa | 9396 | struct btrfs_fs_info *fs_info = root->fs_info; |
1eafa6c7 | 9397 | |
84961539 | 9398 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9399 | return -EROFS; |
9400 | ||
f9baa501 | 9401 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
eb73c1b7 MX |
9402 | } |
9403 | ||
9db4dc24 | 9404 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr, |
3d45f221 | 9405 | bool in_reclaim_context) |
eb73c1b7 | 9406 | { |
e076ab2a | 9407 | struct writeback_control wbc = { |
9db4dc24 | 9408 | .nr_to_write = nr, |
e076ab2a JB |
9409 | .sync_mode = WB_SYNC_NONE, |
9410 | .range_start = 0, | |
9411 | .range_end = LLONG_MAX, | |
9412 | }; | |
eb73c1b7 | 9413 | struct btrfs_root *root; |
84af994b | 9414 | LIST_HEAD(splice); |
eb73c1b7 MX |
9415 | int ret; |
9416 | ||
84961539 | 9417 | if (BTRFS_FS_ERROR(fs_info)) |
eb73c1b7 MX |
9418 | return -EROFS; |
9419 | ||
573bfb72 | 9420 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9421 | spin_lock(&fs_info->delalloc_root_lock); |
9422 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
d7830b71 | 9423 | while (!list_empty(&splice)) { |
e076ab2a JB |
9424 | /* |
9425 | * Reset nr_to_write here so we know that we're doing a full | |
9426 | * flush. | |
9427 | */ | |
9db4dc24 | 9428 | if (nr == LONG_MAX) |
e076ab2a JB |
9429 | wbc.nr_to_write = LONG_MAX; |
9430 | ||
eb73c1b7 MX |
9431 | root = list_first_entry(&splice, struct btrfs_root, |
9432 | delalloc_root); | |
00246528 | 9433 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9434 | BUG_ON(!root); |
9435 | list_move_tail(&root->delalloc_root, | |
9436 | &fs_info->delalloc_roots); | |
9437 | spin_unlock(&fs_info->delalloc_root_lock); | |
9438 | ||
e076ab2a | 9439 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
00246528 | 9440 | btrfs_put_root(root); |
e076ab2a | 9441 | if (ret < 0 || wbc.nr_to_write <= 0) |
eb73c1b7 | 9442 | goto out; |
eb73c1b7 | 9443 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9444 | } |
eb73c1b7 | 9445 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9446 | |
6c255e67 | 9447 | ret = 0; |
eb73c1b7 | 9448 | out: |
81f1d390 | 9449 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9450 | spin_lock(&fs_info->delalloc_root_lock); |
9451 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9452 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9453 | } |
573bfb72 | 9454 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9455 | return ret; |
ea8c2819 CM |
9456 | } |
9457 | ||
7a77db95 | 9458 | static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 9459 | struct dentry *dentry, const char *symname) |
39279cc3 | 9460 | { |
41044b41 | 9461 | struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); |
39279cc3 CM |
9462 | struct btrfs_trans_handle *trans; |
9463 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9464 | struct btrfs_path *path; | |
9465 | struct btrfs_key key; | |
a1fd0c35 | 9466 | struct inode *inode; |
3538d68d OS |
9467 | struct btrfs_new_inode_args new_inode_args = { |
9468 | .dir = dir, | |
9469 | .dentry = dentry, | |
9470 | }; | |
9471 | unsigned int trans_num_items; | |
39279cc3 | 9472 | int err; |
39279cc3 CM |
9473 | int name_len; |
9474 | int datasize; | |
5f39d397 | 9475 | unsigned long ptr; |
39279cc3 | 9476 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9477 | struct extent_buffer *leaf; |
39279cc3 | 9478 | |
f06becc4 | 9479 | name_len = strlen(symname); |
0b246afa | 9480 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9481 | return -ENAMETOOLONG; |
1832a6d5 | 9482 | |
a1fd0c35 OS |
9483 | inode = new_inode(dir->i_sb); |
9484 | if (!inode) | |
9485 | return -ENOMEM; | |
f2d40141 | 9486 | inode_init_owner(idmap, inode, dir, S_IFLNK | S_IRWXUGO); |
a1fd0c35 OS |
9487 | inode->i_op = &btrfs_symlink_inode_operations; |
9488 | inode_nohighmem(inode); | |
9489 | inode->i_mapping->a_ops = &btrfs_aops; | |
caae78e0 OS |
9490 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
9491 | inode_set_bytes(inode, name_len); | |
a1fd0c35 | 9492 | |
3538d68d OS |
9493 | new_inode_args.inode = inode; |
9494 | err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
9495 | if (err) |
9496 | goto out_inode; | |
3538d68d OS |
9497 | /* 1 additional item for the inline extent */ |
9498 | trans_num_items++; | |
9499 | ||
9500 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 9501 | if (IS_ERR(trans)) { |
3538d68d OS |
9502 | err = PTR_ERR(trans); |
9503 | goto out_new_inode_args; | |
a1fd0c35 | 9504 | } |
1832a6d5 | 9505 | |
caae78e0 | 9506 | err = btrfs_create_new_inode(trans, &new_inode_args); |
b0d5d10f | 9507 | if (err) |
caae78e0 | 9508 | goto out; |
ad19db71 | 9509 | |
39279cc3 | 9510 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9511 | if (!path) { |
9512 | err = -ENOMEM; | |
caae78e0 OS |
9513 | btrfs_abort_transaction(trans, err); |
9514 | discard_new_inode(inode); | |
9515 | inode = NULL; | |
9516 | goto out; | |
d8926bb3 | 9517 | } |
4a0cc7ca | 9518 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9519 | key.offset = 0; |
962a298f | 9520 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9521 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9522 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9523 | datasize); | |
54aa1f4d | 9524 | if (err) { |
caae78e0 | 9525 | btrfs_abort_transaction(trans, err); |
b0839166 | 9526 | btrfs_free_path(path); |
caae78e0 OS |
9527 | discard_new_inode(inode); |
9528 | inode = NULL; | |
9529 | goto out; | |
54aa1f4d | 9530 | } |
5f39d397 CM |
9531 | leaf = path->nodes[0]; |
9532 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9533 | struct btrfs_file_extent_item); | |
9534 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9535 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9536 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9537 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9538 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9539 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9540 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9541 | ||
39279cc3 | 9542 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 | 9543 | write_extent_buffer(leaf, symname, ptr, name_len); |
50564b65 | 9544 | btrfs_mark_buffer_dirty(trans, leaf); |
39279cc3 | 9545 | btrfs_free_path(path); |
5f39d397 | 9546 | |
1e2e547a | 9547 | d_instantiate_new(dentry, inode); |
caae78e0 OS |
9548 | err = 0; |
9549 | out: | |
3a45bb20 | 9550 | btrfs_end_transaction(trans); |
2ff7e61e | 9551 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
9552 | out_new_inode_args: |
9553 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
9554 | out_inode: |
9555 | if (err) | |
9556 | iput(inode); | |
39279cc3 CM |
9557 | return err; |
9558 | } | |
16432985 | 9559 | |
8fccebfa FM |
9560 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
9561 | struct btrfs_trans_handle *trans_in, | |
90dffd0c NB |
9562 | struct btrfs_inode *inode, |
9563 | struct btrfs_key *ins, | |
203f44c5 QW |
9564 | u64 file_offset) |
9565 | { | |
9566 | struct btrfs_file_extent_item stack_fi; | |
bf385648 | 9567 | struct btrfs_replace_extent_info extent_info; |
8fccebfa FM |
9568 | struct btrfs_trans_handle *trans = trans_in; |
9569 | struct btrfs_path *path; | |
203f44c5 QW |
9570 | u64 start = ins->objectid; |
9571 | u64 len = ins->offset; | |
9e65bfca | 9572 | u64 qgroup_released = 0; |
9729f10a | 9573 | int ret; |
203f44c5 QW |
9574 | |
9575 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9576 | ||
9577 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9578 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9579 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9580 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9581 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9582 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9583 | /* Encryption and other encoding is reserved and all 0 */ | |
9584 | ||
9e65bfca BB |
9585 | ret = btrfs_qgroup_release_data(inode, file_offset, len, &qgroup_released); |
9586 | if (ret < 0) | |
9587 | return ERR_PTR(ret); | |
8fccebfa FM |
9588 | |
9589 | if (trans) { | |
90dffd0c | 9590 | ret = insert_reserved_file_extent(trans, inode, |
2766ff61 | 9591 | file_offset, &stack_fi, |
fbf48bb0 | 9592 | true, qgroup_released); |
8fccebfa | 9593 | if (ret) |
a3ee79bd | 9594 | goto free_qgroup; |
8fccebfa FM |
9595 | return trans; |
9596 | } | |
9597 | ||
9598 | extent_info.disk_offset = start; | |
9599 | extent_info.disk_len = len; | |
9600 | extent_info.data_offset = 0; | |
9601 | extent_info.data_len = len; | |
9602 | extent_info.file_offset = file_offset; | |
9603 | extent_info.extent_buf = (char *)&stack_fi; | |
8fccebfa | 9604 | extent_info.is_new_extent = true; |
983d8209 | 9605 | extent_info.update_times = true; |
fbf48bb0 | 9606 | extent_info.qgroup_reserved = qgroup_released; |
8fccebfa FM |
9607 | extent_info.insertions = 0; |
9608 | ||
9609 | path = btrfs_alloc_path(); | |
a3ee79bd QW |
9610 | if (!path) { |
9611 | ret = -ENOMEM; | |
9612 | goto free_qgroup; | |
9613 | } | |
8fccebfa | 9614 | |
bfc78479 | 9615 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
8fccebfa FM |
9616 | file_offset + len - 1, &extent_info, |
9617 | &trans); | |
9618 | btrfs_free_path(path); | |
9619 | if (ret) | |
a3ee79bd | 9620 | goto free_qgroup; |
8fccebfa | 9621 | return trans; |
a3ee79bd QW |
9622 | |
9623 | free_qgroup: | |
9624 | /* | |
9625 | * We have released qgroup data range at the beginning of the function, | |
9626 | * and normally qgroup_released bytes will be freed when committing | |
9627 | * transaction. | |
9628 | * But if we error out early, we have to free what we have released | |
9629 | * or we leak qgroup data reservation. | |
9630 | */ | |
9631 | btrfs_qgroup_free_refroot(inode->root->fs_info, | |
9632 | inode->root->root_key.objectid, qgroup_released, | |
9633 | BTRFS_QGROUP_RSV_DATA); | |
9634 | return ERR_PTR(ret); | |
203f44c5 | 9635 | } |
8fccebfa | 9636 | |
0af3d00b JB |
9637 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9638 | u64 start, u64 num_bytes, u64 min_size, | |
9639 | loff_t actual_len, u64 *alloc_hint, | |
9640 | struct btrfs_trans_handle *trans) | |
d899e052 | 9641 | { |
41044b41 | 9642 | struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
5dc562c5 | 9643 | struct extent_map *em; |
d899e052 YZ |
9644 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9645 | struct btrfs_key ins; | |
d899e052 | 9646 | u64 cur_offset = start; |
b778cf96 | 9647 | u64 clear_offset = start; |
55a61d1d | 9648 | u64 i_size; |
154ea289 | 9649 | u64 cur_bytes; |
0b670dc4 | 9650 | u64 last_alloc = (u64)-1; |
d899e052 | 9651 | int ret = 0; |
0af3d00b | 9652 | bool own_trans = true; |
18513091 | 9653 | u64 end = start + num_bytes - 1; |
d899e052 | 9654 | |
0af3d00b JB |
9655 | if (trans) |
9656 | own_trans = false; | |
d899e052 | 9657 | while (num_bytes > 0) { |
ee22184b | 9658 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9659 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9660 | /* |
9661 | * If we are severely fragmented we could end up with really | |
9662 | * small allocations, so if the allocator is returning small | |
9663 | * chunks lets make its job easier by only searching for those | |
9664 | * sized chunks. | |
9665 | */ | |
9666 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9667 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9668 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
8fccebfa | 9669 | if (ret) |
a22285a6 | 9670 | break; |
b778cf96 JB |
9671 | |
9672 | /* | |
9673 | * We've reserved this space, and thus converted it from | |
9674 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9675 | * from here on out we will only need to clear our reservation | |
9676 | * for the remaining unreserved area, so advance our | |
9677 | * clear_offset by our extent size. | |
9678 | */ | |
9679 | clear_offset += ins.offset; | |
5a303d5d | 9680 | |
0b670dc4 | 9681 | last_alloc = ins.offset; |
90dffd0c NB |
9682 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
9683 | &ins, cur_offset); | |
1afc708d FM |
9684 | /* |
9685 | * Now that we inserted the prealloc extent we can finally | |
9686 | * decrement the number of reservations in the block group. | |
9687 | * If we did it before, we could race with relocation and have | |
9688 | * relocation miss the reserved extent, making it fail later. | |
9689 | */ | |
9690 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
8fccebfa FM |
9691 | if (IS_ERR(trans)) { |
9692 | ret = PTR_ERR(trans); | |
2ff7e61e | 9693 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9694 | ins.offset, 0); |
79787eaa JM |
9695 | break; |
9696 | } | |
31193213 | 9697 | |
5dc562c5 JB |
9698 | em = alloc_extent_map(); |
9699 | if (!em) { | |
a1ba4c08 FM |
9700 | btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset, |
9701 | cur_offset + ins.offset - 1, false); | |
23e3337f | 9702 | btrfs_set_inode_full_sync(BTRFS_I(inode)); |
5dc562c5 JB |
9703 | goto next; |
9704 | } | |
9705 | ||
9706 | em->start = cur_offset; | |
9707 | em->orig_start = cur_offset; | |
9708 | em->len = ins.offset; | |
9709 | em->block_start = ins.objectid; | |
9710 | em->block_len = ins.offset; | |
b4939680 | 9711 | em->orig_block_len = ins.offset; |
cc95bef6 | 9712 | em->ram_bytes = ins.offset; |
f86f7a75 | 9713 | em->flags |= EXTENT_FLAG_PREALLOC; |
5dc562c5 JB |
9714 | em->generation = trans->transid; |
9715 | ||
a1ba4c08 | 9716 | ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true); |
5dc562c5 JB |
9717 | free_extent_map(em); |
9718 | next: | |
d899e052 YZ |
9719 | num_bytes -= ins.offset; |
9720 | cur_offset += ins.offset; | |
efa56464 | 9721 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9722 | |
0c4d2d95 | 9723 | inode_inc_iversion(inode); |
2a9462de | 9724 | inode_set_ctime_current(inode); |
6cbff00f | 9725 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9726 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9727 | (actual_len > inode->i_size) && |
9728 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9729 | if (cur_offset > actual_len) |
55a61d1d | 9730 | i_size = actual_len; |
d1ea6a61 | 9731 | else |
55a61d1d JB |
9732 | i_size = cur_offset; |
9733 | i_size_write(inode, i_size); | |
76aea537 | 9734 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
5a303d5d YZ |
9735 | } |
9736 | ||
8b9d0322 | 9737 | ret = btrfs_update_inode(trans, BTRFS_I(inode)); |
79787eaa JM |
9738 | |
9739 | if (ret) { | |
66642832 | 9740 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9741 | if (own_trans) |
3a45bb20 | 9742 | btrfs_end_transaction(trans); |
79787eaa JM |
9743 | break; |
9744 | } | |
d899e052 | 9745 | |
8fccebfa | 9746 | if (own_trans) { |
3a45bb20 | 9747 | btrfs_end_transaction(trans); |
8fccebfa FM |
9748 | trans = NULL; |
9749 | } | |
5a303d5d | 9750 | } |
b778cf96 | 9751 | if (clear_offset < end) |
25ce28ca | 9752 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 9753 | end - clear_offset + 1); |
d899e052 YZ |
9754 | return ret; |
9755 | } | |
9756 | ||
0af3d00b JB |
9757 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9758 | u64 start, u64 num_bytes, u64 min_size, | |
9759 | loff_t actual_len, u64 *alloc_hint) | |
9760 | { | |
9761 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9762 | min_size, actual_len, alloc_hint, | |
9763 | NULL); | |
9764 | } | |
9765 | ||
9766 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9767 | struct btrfs_trans_handle *trans, int mode, | |
9768 | u64 start, u64 num_bytes, u64 min_size, | |
9769 | loff_t actual_len, u64 *alloc_hint) | |
9770 | { | |
9771 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9772 | min_size, actual_len, alloc_hint, trans); | |
9773 | } | |
9774 | ||
4609e1f1 | 9775 | static int btrfs_permission(struct mnt_idmap *idmap, |
549c7297 | 9776 | struct inode *inode, int mask) |
fdebe2bd | 9777 | { |
b83cc969 | 9778 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9779 | umode_t mode = inode->i_mode; |
b83cc969 | 9780 | |
cb6db4e5 JM |
9781 | if (mask & MAY_WRITE && |
9782 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9783 | if (btrfs_root_readonly(root)) | |
9784 | return -EROFS; | |
9785 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9786 | return -EACCES; | |
9787 | } | |
4609e1f1 | 9788 | return generic_permission(idmap, inode, mask); |
fdebe2bd | 9789 | } |
39279cc3 | 9790 | |
011e2b71 | 9791 | static int btrfs_tmpfile(struct mnt_idmap *idmap, struct inode *dir, |
863f144f | 9792 | struct file *file, umode_t mode) |
ef3b9af5 | 9793 | { |
41044b41 | 9794 | struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); |
ef3b9af5 FM |
9795 | struct btrfs_trans_handle *trans; |
9796 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
a1fd0c35 | 9797 | struct inode *inode; |
3538d68d OS |
9798 | struct btrfs_new_inode_args new_inode_args = { |
9799 | .dir = dir, | |
863f144f | 9800 | .dentry = file->f_path.dentry, |
3538d68d OS |
9801 | .orphan = true, |
9802 | }; | |
9803 | unsigned int trans_num_items; | |
a1fd0c35 OS |
9804 | int ret; |
9805 | ||
9806 | inode = new_inode(dir->i_sb); | |
9807 | if (!inode) | |
9808 | return -ENOMEM; | |
f2d40141 | 9809 | inode_init_owner(idmap, inode, dir, mode); |
a1fd0c35 OS |
9810 | inode->i_fop = &btrfs_file_operations; |
9811 | inode->i_op = &btrfs_file_inode_operations; | |
9812 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 | 9813 | |
3538d68d OS |
9814 | new_inode_args.inode = inode; |
9815 | ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); | |
caae78e0 OS |
9816 | if (ret) |
9817 | goto out_inode; | |
3538d68d OS |
9818 | |
9819 | trans = btrfs_start_transaction(root, trans_num_items); | |
a1fd0c35 | 9820 | if (IS_ERR(trans)) { |
3538d68d OS |
9821 | ret = PTR_ERR(trans); |
9822 | goto out_new_inode_args; | |
a1fd0c35 | 9823 | } |
ef3b9af5 | 9824 | |
caae78e0 | 9825 | ret = btrfs_create_new_inode(trans, &new_inode_args); |
ef3b9af5 | 9826 | |
5762b5c9 | 9827 | /* |
3538d68d OS |
9828 | * We set number of links to 0 in btrfs_create_new_inode(), and here we |
9829 | * set it to 1 because d_tmpfile() will issue a warning if the count is | |
9830 | * 0, through: | |
5762b5c9 FM |
9831 | * |
9832 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9833 | */ | |
9834 | set_nlink(inode, 1); | |
caae78e0 OS |
9835 | |
9836 | if (!ret) { | |
863f144f | 9837 | d_tmpfile(file, inode); |
caae78e0 OS |
9838 | unlock_new_inode(inode); |
9839 | mark_inode_dirty(inode); | |
9840 | } | |
9841 | ||
3a45bb20 | 9842 | btrfs_end_transaction(trans); |
2ff7e61e | 9843 | btrfs_btree_balance_dirty(fs_info); |
3538d68d OS |
9844 | out_new_inode_args: |
9845 | btrfs_new_inode_args_destroy(&new_inode_args); | |
caae78e0 OS |
9846 | out_inode: |
9847 | if (ret) | |
9848 | iput(inode); | |
863f144f | 9849 | return finish_open_simple(file, ret); |
ef3b9af5 FM |
9850 | } |
9851 | ||
d2a91064 | 9852 | void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) |
c6100a4b | 9853 | { |
d2a91064 | 9854 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
c6100a4b JB |
9855 | unsigned long index = start >> PAGE_SHIFT; |
9856 | unsigned long end_index = end >> PAGE_SHIFT; | |
9857 | struct page *page; | |
d2a91064 | 9858 | u32 len; |
c6100a4b | 9859 | |
d2a91064 QW |
9860 | ASSERT(end + 1 - start <= U32_MAX); |
9861 | len = end + 1 - start; | |
c6100a4b | 9862 | while (index <= end_index) { |
d2a91064 | 9863 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
c6100a4b | 9864 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
d2a91064 | 9865 | |
55151ea9 QW |
9866 | /* This is for data, which doesn't yet support larger folio. */ |
9867 | ASSERT(folio_order(page_folio(page)) == 0); | |
9868 | btrfs_folio_set_writeback(fs_info, page_folio(page), start, len); | |
c6100a4b JB |
9869 | put_page(page); |
9870 | index++; | |
9871 | } | |
9872 | } | |
9873 | ||
3ea4dc5b OS |
9874 | int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info, |
9875 | int compress_type) | |
1881fba8 OS |
9876 | { |
9877 | switch (compress_type) { | |
9878 | case BTRFS_COMPRESS_NONE: | |
9879 | return BTRFS_ENCODED_IO_COMPRESSION_NONE; | |
9880 | case BTRFS_COMPRESS_ZLIB: | |
9881 | return BTRFS_ENCODED_IO_COMPRESSION_ZLIB; | |
9882 | case BTRFS_COMPRESS_LZO: | |
9883 | /* | |
9884 | * The LZO format depends on the sector size. 64K is the maximum | |
9885 | * sector size that we support. | |
9886 | */ | |
9887 | if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K) | |
9888 | return -EINVAL; | |
9889 | return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + | |
9890 | (fs_info->sectorsize_bits - 12); | |
9891 | case BTRFS_COMPRESS_ZSTD: | |
9892 | return BTRFS_ENCODED_IO_COMPRESSION_ZSTD; | |
9893 | default: | |
9894 | return -EUCLEAN; | |
9895 | } | |
9896 | } | |
9897 | ||
9898 | static ssize_t btrfs_encoded_read_inline( | |
9899 | struct kiocb *iocb, | |
9900 | struct iov_iter *iter, u64 start, | |
9901 | u64 lockend, | |
9902 | struct extent_state **cached_state, | |
9903 | u64 extent_start, size_t count, | |
9904 | struct btrfs_ioctl_encoded_io_args *encoded, | |
9905 | bool *unlocked) | |
9906 | { | |
9907 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
9908 | struct btrfs_root *root = inode->root; | |
9909 | struct btrfs_fs_info *fs_info = root->fs_info; | |
9910 | struct extent_io_tree *io_tree = &inode->io_tree; | |
9911 | struct btrfs_path *path; | |
9912 | struct extent_buffer *leaf; | |
9913 | struct btrfs_file_extent_item *item; | |
9914 | u64 ram_bytes; | |
9915 | unsigned long ptr; | |
9916 | void *tmp; | |
9917 | ssize_t ret; | |
9918 | ||
9919 | path = btrfs_alloc_path(); | |
9920 | if (!path) { | |
9921 | ret = -ENOMEM; | |
9922 | goto out; | |
9923 | } | |
9924 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), | |
9925 | extent_start, 0); | |
9926 | if (ret) { | |
9927 | if (ret > 0) { | |
9928 | /* The extent item disappeared? */ | |
9929 | ret = -EIO; | |
9930 | } | |
9931 | goto out; | |
9932 | } | |
9933 | leaf = path->nodes[0]; | |
9934 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
9935 | ||
9936 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); | |
9937 | ptr = btrfs_file_extent_inline_start(item); | |
9938 | ||
9939 | encoded->len = min_t(u64, extent_start + ram_bytes, | |
9940 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
9941 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
9942 | btrfs_file_extent_compression(leaf, item)); | |
9943 | if (ret < 0) | |
9944 | goto out; | |
9945 | encoded->compression = ret; | |
9946 | if (encoded->compression) { | |
9947 | size_t inline_size; | |
9948 | ||
9949 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
9950 | path->slots[0]); | |
9951 | if (inline_size > count) { | |
9952 | ret = -ENOBUFS; | |
9953 | goto out; | |
9954 | } | |
9955 | count = inline_size; | |
9956 | encoded->unencoded_len = ram_bytes; | |
9957 | encoded->unencoded_offset = iocb->ki_pos - extent_start; | |
9958 | } else { | |
9959 | count = min_t(u64, count, encoded->len); | |
9960 | encoded->len = count; | |
9961 | encoded->unencoded_len = count; | |
9962 | ptr += iocb->ki_pos - extent_start; | |
9963 | } | |
9964 | ||
9965 | tmp = kmalloc(count, GFP_NOFS); | |
9966 | if (!tmp) { | |
9967 | ret = -ENOMEM; | |
9968 | goto out; | |
9969 | } | |
9970 | read_extent_buffer(leaf, tmp, ptr, count); | |
9971 | btrfs_release_path(path); | |
570eb97b | 9972 | unlock_extent(io_tree, start, lockend, cached_state); |
e5d4d75b | 9973 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
9974 | *unlocked = true; |
9975 | ||
9976 | ret = copy_to_iter(tmp, count, iter); | |
9977 | if (ret != count) | |
9978 | ret = -EFAULT; | |
9979 | kfree(tmp); | |
9980 | out: | |
9981 | btrfs_free_path(path); | |
9982 | return ret; | |
9983 | } | |
9984 | ||
9985 | struct btrfs_encoded_read_private { | |
1881fba8 OS |
9986 | wait_queue_head_t wait; |
9987 | atomic_t pending; | |
9988 | blk_status_t status; | |
1881fba8 OS |
9989 | }; |
9990 | ||
917f32a2 | 9991 | static void btrfs_encoded_read_endio(struct btrfs_bio *bbio) |
1881fba8 | 9992 | { |
917f32a2 | 9993 | struct btrfs_encoded_read_private *priv = bbio->private; |
1881fba8 | 9994 | |
7609afac | 9995 | if (bbio->bio.bi_status) { |
1881fba8 OS |
9996 | /* |
9997 | * The memory barrier implied by the atomic_dec_return() here | |
9998 | * pairs with the memory barrier implied by the | |
9999 | * atomic_dec_return() or io_wait_event() in | |
10000 | * btrfs_encoded_read_regular_fill_pages() to ensure that this | |
10001 | * write is observed before the load of status in | |
10002 | * btrfs_encoded_read_regular_fill_pages(). | |
10003 | */ | |
7609afac | 10004 | WRITE_ONCE(priv->status, bbio->bio.bi_status); |
1881fba8 OS |
10005 | } |
10006 | if (!atomic_dec_return(&priv->pending)) | |
10007 | wake_up(&priv->wait); | |
917f32a2 | 10008 | bio_put(&bbio->bio); |
1881fba8 OS |
10009 | } |
10010 | ||
3ea4dc5b OS |
10011 | int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, |
10012 | u64 file_offset, u64 disk_bytenr, | |
10013 | u64 disk_io_size, struct page **pages) | |
1881fba8 | 10014 | { |
4317ff00 | 10015 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1881fba8 | 10016 | struct btrfs_encoded_read_private priv = { |
1881fba8 | 10017 | .pending = ATOMIC_INIT(1), |
1881fba8 OS |
10018 | }; |
10019 | unsigned long i = 0; | |
b41bbd29 | 10020 | struct btrfs_bio *bbio; |
1881fba8 OS |
10021 | |
10022 | init_waitqueue_head(&priv.wait); | |
1881fba8 | 10023 | |
4317ff00 QW |
10024 | bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info, |
10025 | btrfs_encoded_read_endio, &priv); | |
b41bbd29 | 10026 | bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; |
4317ff00 | 10027 | bbio->inode = inode; |
1881fba8 | 10028 | |
34f888ce CH |
10029 | do { |
10030 | size_t bytes = min_t(u64, disk_io_size, PAGE_SIZE); | |
10031 | ||
b41bbd29 | 10032 | if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) { |
34f888ce | 10033 | atomic_inc(&priv.pending); |
b41bbd29 | 10034 | btrfs_submit_bio(bbio, 0); |
34f888ce | 10035 | |
4317ff00 | 10036 | bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info, |
b41bbd29 CH |
10037 | btrfs_encoded_read_endio, &priv); |
10038 | bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; | |
4317ff00 | 10039 | bbio->inode = inode; |
34f888ce | 10040 | continue; |
1881fba8 | 10041 | } |
34f888ce CH |
10042 | |
10043 | i++; | |
10044 | disk_bytenr += bytes; | |
10045 | disk_io_size -= bytes; | |
10046 | } while (disk_io_size); | |
10047 | ||
10048 | atomic_inc(&priv.pending); | |
b41bbd29 | 10049 | btrfs_submit_bio(bbio, 0); |
1881fba8 | 10050 | |
1881fba8 OS |
10051 | if (atomic_dec_return(&priv.pending)) |
10052 | io_wait_event(priv.wait, !atomic_read(&priv.pending)); | |
10053 | /* See btrfs_encoded_read_endio() for ordering. */ | |
10054 | return blk_status_to_errno(READ_ONCE(priv.status)); | |
10055 | } | |
10056 | ||
10057 | static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb, | |
10058 | struct iov_iter *iter, | |
10059 | u64 start, u64 lockend, | |
10060 | struct extent_state **cached_state, | |
10061 | u64 disk_bytenr, u64 disk_io_size, | |
10062 | size_t count, bool compressed, | |
10063 | bool *unlocked) | |
10064 | { | |
10065 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10066 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10067 | struct page **pages; | |
10068 | unsigned long nr_pages, i; | |
10069 | u64 cur; | |
10070 | size_t page_offset; | |
10071 | ssize_t ret; | |
10072 | ||
10073 | nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE); | |
10074 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); | |
10075 | if (!pages) | |
10076 | return -ENOMEM; | |
09e6cef1 | 10077 | ret = btrfs_alloc_page_array(nr_pages, pages, 0); |
dd137dd1 STD |
10078 | if (ret) { |
10079 | ret = -ENOMEM; | |
10080 | goto out; | |
1881fba8 | 10081 | } |
1881fba8 OS |
10082 | |
10083 | ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr, | |
10084 | disk_io_size, pages); | |
10085 | if (ret) | |
10086 | goto out; | |
10087 | ||
570eb97b | 10088 | unlock_extent(io_tree, start, lockend, cached_state); |
e5d4d75b | 10089 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10090 | *unlocked = true; |
10091 | ||
10092 | if (compressed) { | |
10093 | i = 0; | |
10094 | page_offset = 0; | |
10095 | } else { | |
10096 | i = (iocb->ki_pos - start) >> PAGE_SHIFT; | |
10097 | page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1); | |
10098 | } | |
10099 | cur = 0; | |
10100 | while (cur < count) { | |
10101 | size_t bytes = min_t(size_t, count - cur, | |
10102 | PAGE_SIZE - page_offset); | |
10103 | ||
10104 | if (copy_page_to_iter(pages[i], page_offset, bytes, | |
10105 | iter) != bytes) { | |
10106 | ret = -EFAULT; | |
10107 | goto out; | |
10108 | } | |
10109 | i++; | |
10110 | cur += bytes; | |
10111 | page_offset = 0; | |
10112 | } | |
10113 | ret = count; | |
10114 | out: | |
10115 | for (i = 0; i < nr_pages; i++) { | |
10116 | if (pages[i]) | |
10117 | __free_page(pages[i]); | |
10118 | } | |
10119 | kfree(pages); | |
10120 | return ret; | |
10121 | } | |
10122 | ||
10123 | ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, | |
10124 | struct btrfs_ioctl_encoded_io_args *encoded) | |
10125 | { | |
10126 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10127 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
10128 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10129 | ssize_t ret; | |
10130 | size_t count = iov_iter_count(iter); | |
10131 | u64 start, lockend, disk_bytenr, disk_io_size; | |
10132 | struct extent_state *cached_state = NULL; | |
10133 | struct extent_map *em; | |
10134 | bool unlocked = false; | |
10135 | ||
10136 | file_accessed(iocb->ki_filp); | |
10137 | ||
29b6352b | 10138 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10139 | |
10140 | if (iocb->ki_pos >= inode->vfs_inode.i_size) { | |
e5d4d75b | 10141 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10142 | return 0; |
10143 | } | |
10144 | start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize); | |
10145 | /* | |
10146 | * We don't know how long the extent containing iocb->ki_pos is, but if | |
10147 | * it's compressed we know that it won't be longer than this. | |
10148 | */ | |
10149 | lockend = start + BTRFS_MAX_UNCOMPRESSED - 1; | |
10150 | ||
10151 | for (;;) { | |
10152 | struct btrfs_ordered_extent *ordered; | |
10153 | ||
10154 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, | |
10155 | lockend - start + 1); | |
10156 | if (ret) | |
10157 | goto out_unlock_inode; | |
570eb97b | 10158 | lock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10159 | ordered = btrfs_lookup_ordered_range(inode, start, |
10160 | lockend - start + 1); | |
10161 | if (!ordered) | |
10162 | break; | |
10163 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10164 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10165 | cond_resched(); |
10166 | } | |
10167 | ||
8bab0a30 | 10168 | em = btrfs_get_extent(inode, NULL, start, lockend - start + 1); |
1881fba8 OS |
10169 | if (IS_ERR(em)) { |
10170 | ret = PTR_ERR(em); | |
10171 | goto out_unlock_extent; | |
10172 | } | |
10173 | ||
10174 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10175 | u64 extent_start = em->start; | |
10176 | ||
10177 | /* | |
10178 | * For inline extents we get everything we need out of the | |
10179 | * extent item. | |
10180 | */ | |
10181 | free_extent_map(em); | |
10182 | em = NULL; | |
10183 | ret = btrfs_encoded_read_inline(iocb, iter, start, lockend, | |
10184 | &cached_state, extent_start, | |
10185 | count, encoded, &unlocked); | |
10186 | goto out; | |
10187 | } | |
10188 | ||
10189 | /* | |
10190 | * We only want to return up to EOF even if the extent extends beyond | |
10191 | * that. | |
10192 | */ | |
10193 | encoded->len = min_t(u64, extent_map_end(em), | |
10194 | inode->vfs_inode.i_size) - iocb->ki_pos; | |
10195 | if (em->block_start == EXTENT_MAP_HOLE || | |
f86f7a75 | 10196 | (em->flags & EXTENT_FLAG_PREALLOC)) { |
1881fba8 OS |
10197 | disk_bytenr = EXTENT_MAP_HOLE; |
10198 | count = min_t(u64, count, encoded->len); | |
10199 | encoded->len = count; | |
10200 | encoded->unencoded_len = count; | |
f86f7a75 | 10201 | } else if (extent_map_is_compressed(em)) { |
1881fba8 OS |
10202 | disk_bytenr = em->block_start; |
10203 | /* | |
10204 | * Bail if the buffer isn't large enough to return the whole | |
10205 | * compressed extent. | |
10206 | */ | |
10207 | if (em->block_len > count) { | |
10208 | ret = -ENOBUFS; | |
10209 | goto out_em; | |
10210 | } | |
c1867eb3 DS |
10211 | disk_io_size = em->block_len; |
10212 | count = em->block_len; | |
1881fba8 OS |
10213 | encoded->unencoded_len = em->ram_bytes; |
10214 | encoded->unencoded_offset = iocb->ki_pos - em->orig_start; | |
10215 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
f86f7a75 | 10216 | extent_map_compression(em)); |
1881fba8 OS |
10217 | if (ret < 0) |
10218 | goto out_em; | |
10219 | encoded->compression = ret; | |
10220 | } else { | |
10221 | disk_bytenr = em->block_start + (start - em->start); | |
10222 | if (encoded->len > count) | |
10223 | encoded->len = count; | |
10224 | /* | |
10225 | * Don't read beyond what we locked. This also limits the page | |
10226 | * allocations that we'll do. | |
10227 | */ | |
10228 | disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start; | |
10229 | count = start + disk_io_size - iocb->ki_pos; | |
10230 | encoded->len = count; | |
10231 | encoded->unencoded_len = count; | |
10232 | disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize); | |
10233 | } | |
10234 | free_extent_map(em); | |
10235 | em = NULL; | |
10236 | ||
10237 | if (disk_bytenr == EXTENT_MAP_HOLE) { | |
570eb97b | 10238 | unlock_extent(io_tree, start, lockend, &cached_state); |
e5d4d75b | 10239 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10240 | unlocked = true; |
10241 | ret = iov_iter_zero(count, iter); | |
10242 | if (ret != count) | |
10243 | ret = -EFAULT; | |
10244 | } else { | |
10245 | ret = btrfs_encoded_read_regular(iocb, iter, start, lockend, | |
10246 | &cached_state, disk_bytenr, | |
10247 | disk_io_size, count, | |
10248 | encoded->compression, | |
10249 | &unlocked); | |
10250 | } | |
10251 | ||
10252 | out: | |
10253 | if (ret >= 0) | |
10254 | iocb->ki_pos += encoded->len; | |
10255 | out_em: | |
10256 | free_extent_map(em); | |
10257 | out_unlock_extent: | |
10258 | if (!unlocked) | |
570eb97b | 10259 | unlock_extent(io_tree, start, lockend, &cached_state); |
1881fba8 OS |
10260 | out_unlock_inode: |
10261 | if (!unlocked) | |
e5d4d75b | 10262 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
1881fba8 OS |
10263 | return ret; |
10264 | } | |
10265 | ||
7c0c7269 OS |
10266 | ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from, |
10267 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
10268 | { | |
10269 | struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); | |
10270 | struct btrfs_root *root = inode->root; | |
10271 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10272 | struct extent_io_tree *io_tree = &inode->io_tree; | |
10273 | struct extent_changeset *data_reserved = NULL; | |
10274 | struct extent_state *cached_state = NULL; | |
d611935b | 10275 | struct btrfs_ordered_extent *ordered; |
7c0c7269 OS |
10276 | int compression; |
10277 | size_t orig_count; | |
10278 | u64 start, end; | |
10279 | u64 num_bytes, ram_bytes, disk_num_bytes; | |
10280 | unsigned long nr_pages, i; | |
10281 | struct page **pages; | |
10282 | struct btrfs_key ins; | |
10283 | bool extent_reserved = false; | |
10284 | struct extent_map *em; | |
10285 | ssize_t ret; | |
10286 | ||
10287 | switch (encoded->compression) { | |
10288 | case BTRFS_ENCODED_IO_COMPRESSION_ZLIB: | |
10289 | compression = BTRFS_COMPRESS_ZLIB; | |
10290 | break; | |
10291 | case BTRFS_ENCODED_IO_COMPRESSION_ZSTD: | |
10292 | compression = BTRFS_COMPRESS_ZSTD; | |
10293 | break; | |
10294 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K: | |
10295 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K: | |
10296 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K: | |
10297 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K: | |
10298 | case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K: | |
10299 | /* The sector size must match for LZO. */ | |
10300 | if (encoded->compression - | |
10301 | BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 != | |
10302 | fs_info->sectorsize_bits) | |
10303 | return -EINVAL; | |
10304 | compression = BTRFS_COMPRESS_LZO; | |
10305 | break; | |
10306 | default: | |
10307 | return -EINVAL; | |
10308 | } | |
10309 | if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE) | |
10310 | return -EINVAL; | |
10311 | ||
1bd96c92 FM |
10312 | /* |
10313 | * Compressed extents should always have checksums, so error out if we | |
10314 | * have a NOCOW file or inode was created while mounted with NODATASUM. | |
10315 | */ | |
10316 | if (inode->flags & BTRFS_INODE_NODATASUM) | |
10317 | return -EINVAL; | |
10318 | ||
7c0c7269 OS |
10319 | orig_count = iov_iter_count(from); |
10320 | ||
10321 | /* The extent size must be sane. */ | |
10322 | if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED || | |
10323 | orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0) | |
10324 | return -EINVAL; | |
10325 | ||
10326 | /* | |
10327 | * The compressed data must be smaller than the decompressed data. | |
10328 | * | |
10329 | * It's of course possible for data to compress to larger or the same | |
10330 | * size, but the buffered I/O path falls back to no compression for such | |
10331 | * data, and we don't want to break any assumptions by creating these | |
10332 | * extents. | |
10333 | * | |
10334 | * Note that this is less strict than the current check we have that the | |
10335 | * compressed data must be at least one sector smaller than the | |
10336 | * decompressed data. We only want to enforce the weaker requirement | |
10337 | * from old kernels that it is at least one byte smaller. | |
10338 | */ | |
10339 | if (orig_count >= encoded->unencoded_len) | |
10340 | return -EINVAL; | |
10341 | ||
10342 | /* The extent must start on a sector boundary. */ | |
10343 | start = iocb->ki_pos; | |
10344 | if (!IS_ALIGNED(start, fs_info->sectorsize)) | |
10345 | return -EINVAL; | |
10346 | ||
10347 | /* | |
10348 | * The extent must end on a sector boundary. However, we allow a write | |
10349 | * which ends at or extends i_size to have an unaligned length; we round | |
10350 | * up the extent size and set i_size to the unaligned end. | |
10351 | */ | |
10352 | if (start + encoded->len < inode->vfs_inode.i_size && | |
10353 | !IS_ALIGNED(start + encoded->len, fs_info->sectorsize)) | |
10354 | return -EINVAL; | |
10355 | ||
10356 | /* Finally, the offset in the unencoded data must be sector-aligned. */ | |
10357 | if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize)) | |
10358 | return -EINVAL; | |
10359 | ||
10360 | num_bytes = ALIGN(encoded->len, fs_info->sectorsize); | |
10361 | ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize); | |
10362 | end = start + num_bytes - 1; | |
10363 | ||
10364 | /* | |
10365 | * If the extent cannot be inline, the compressed data on disk must be | |
10366 | * sector-aligned. For convenience, we extend it with zeroes if it | |
10367 | * isn't. | |
10368 | */ | |
10369 | disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize); | |
10370 | nr_pages = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE); | |
10371 | pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT); | |
10372 | if (!pages) | |
10373 | return -ENOMEM; | |
10374 | for (i = 0; i < nr_pages; i++) { | |
10375 | size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from)); | |
10376 | char *kaddr; | |
10377 | ||
10378 | pages[i] = alloc_page(GFP_KERNEL_ACCOUNT); | |
10379 | if (!pages[i]) { | |
10380 | ret = -ENOMEM; | |
10381 | goto out_pages; | |
10382 | } | |
70826b6b | 10383 | kaddr = kmap_local_page(pages[i]); |
7c0c7269 | 10384 | if (copy_from_iter(kaddr, bytes, from) != bytes) { |
70826b6b | 10385 | kunmap_local(kaddr); |
7c0c7269 OS |
10386 | ret = -EFAULT; |
10387 | goto out_pages; | |
10388 | } | |
10389 | if (bytes < PAGE_SIZE) | |
10390 | memset(kaddr + bytes, 0, PAGE_SIZE - bytes); | |
70826b6b | 10391 | kunmap_local(kaddr); |
7c0c7269 OS |
10392 | } |
10393 | ||
10394 | for (;;) { | |
10395 | struct btrfs_ordered_extent *ordered; | |
10396 | ||
10397 | ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes); | |
10398 | if (ret) | |
10399 | goto out_pages; | |
10400 | ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping, | |
10401 | start >> PAGE_SHIFT, | |
10402 | end >> PAGE_SHIFT); | |
10403 | if (ret) | |
10404 | goto out_pages; | |
570eb97b | 10405 | lock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10406 | ordered = btrfs_lookup_ordered_range(inode, start, num_bytes); |
10407 | if (!ordered && | |
10408 | !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end)) | |
10409 | break; | |
10410 | if (ordered) | |
10411 | btrfs_put_ordered_extent(ordered); | |
570eb97b | 10412 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10413 | cond_resched(); |
10414 | } | |
10415 | ||
10416 | /* | |
10417 | * We don't use the higher-level delalloc space functions because our | |
10418 | * num_bytes and disk_num_bytes are different. | |
10419 | */ | |
10420 | ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes); | |
10421 | if (ret) | |
10422 | goto out_unlock; | |
10423 | ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes); | |
10424 | if (ret) | |
10425 | goto out_free_data_space; | |
d4135134 FM |
10426 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes, |
10427 | false); | |
7c0c7269 OS |
10428 | if (ret) |
10429 | goto out_qgroup_free_data; | |
10430 | ||
10431 | /* Try an inline extent first. */ | |
10432 | if (start == 0 && encoded->unencoded_len == encoded->len && | |
10433 | encoded->unencoded_offset == 0) { | |
10434 | ret = cow_file_range_inline(inode, encoded->len, orig_count, | |
10435 | compression, pages, true); | |
10436 | if (ret <= 0) { | |
10437 | if (ret == 0) | |
10438 | ret = orig_count; | |
10439 | goto out_delalloc_release; | |
10440 | } | |
10441 | } | |
10442 | ||
10443 | ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes, | |
10444 | disk_num_bytes, 0, 0, &ins, 1, 1); | |
10445 | if (ret) | |
10446 | goto out_delalloc_release; | |
10447 | extent_reserved = true; | |
10448 | ||
10449 | em = create_io_em(inode, start, num_bytes, | |
10450 | start - encoded->unencoded_offset, ins.objectid, | |
10451 | ins.offset, ins.offset, ram_bytes, compression, | |
10452 | BTRFS_ORDERED_COMPRESSED); | |
10453 | if (IS_ERR(em)) { | |
10454 | ret = PTR_ERR(em); | |
10455 | goto out_free_reserved; | |
10456 | } | |
10457 | free_extent_map(em); | |
10458 | ||
d611935b | 10459 | ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes, |
7c0c7269 OS |
10460 | ins.objectid, ins.offset, |
10461 | encoded->unencoded_offset, | |
10462 | (1 << BTRFS_ORDERED_ENCODED) | | |
10463 | (1 << BTRFS_ORDERED_COMPRESSED), | |
10464 | compression); | |
d611935b | 10465 | if (IS_ERR(ordered)) { |
4c0c8cfc | 10466 | btrfs_drop_extent_map_range(inode, start, end, false); |
d611935b | 10467 | ret = PTR_ERR(ordered); |
7c0c7269 OS |
10468 | goto out_free_reserved; |
10469 | } | |
10470 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10471 | ||
10472 | if (start + encoded->len > inode->vfs_inode.i_size) | |
10473 | i_size_write(&inode->vfs_inode, start + encoded->len); | |
10474 | ||
570eb97b | 10475 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10476 | |
10477 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10478 | ||
d611935b | 10479 | btrfs_submit_compressed_write(ordered, pages, nr_pages, 0, false); |
7c0c7269 OS |
10480 | ret = orig_count; |
10481 | goto out; | |
10482 | ||
10483 | out_free_reserved: | |
10484 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
10485 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); | |
10486 | out_delalloc_release: | |
10487 | btrfs_delalloc_release_extents(inode, num_bytes); | |
10488 | btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0); | |
10489 | out_qgroup_free_data: | |
10490 | if (ret < 0) | |
9e65bfca | 10491 | btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes, NULL); |
7c0c7269 OS |
10492 | out_free_data_space: |
10493 | /* | |
10494 | * If btrfs_reserve_extent() succeeded, then we already decremented | |
10495 | * bytes_may_use. | |
10496 | */ | |
10497 | if (!extent_reserved) | |
10498 | btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes); | |
10499 | out_unlock: | |
570eb97b | 10500 | unlock_extent(io_tree, start, end, &cached_state); |
7c0c7269 OS |
10501 | out_pages: |
10502 | for (i = 0; i < nr_pages; i++) { | |
10503 | if (pages[i]) | |
10504 | __free_page(pages[i]); | |
10505 | } | |
10506 | kvfree(pages); | |
10507 | out: | |
10508 | if (ret >= 0) | |
10509 | iocb->ki_pos += encoded->len; | |
10510 | return ret; | |
10511 | } | |
10512 | ||
ed46ff3d OS |
10513 | #ifdef CONFIG_SWAP |
10514 | /* | |
10515 | * Add an entry indicating a block group or device which is pinned by a | |
10516 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10517 | * negative errno on failure. | |
10518 | */ | |
10519 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10520 | bool is_block_group) | |
10521 | { | |
10522 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10523 | struct btrfs_swapfile_pin *sp, *entry; | |
10524 | struct rb_node **p; | |
10525 | struct rb_node *parent = NULL; | |
10526 | ||
10527 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10528 | if (!sp) | |
10529 | return -ENOMEM; | |
10530 | sp->ptr = ptr; | |
10531 | sp->inode = inode; | |
10532 | sp->is_block_group = is_block_group; | |
195a49ea | 10533 | sp->bg_extent_count = 1; |
ed46ff3d OS |
10534 | |
10535 | spin_lock(&fs_info->swapfile_pins_lock); | |
10536 | p = &fs_info->swapfile_pins.rb_node; | |
10537 | while (*p) { | |
10538 | parent = *p; | |
10539 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10540 | if (sp->ptr < entry->ptr || | |
10541 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10542 | p = &(*p)->rb_left; | |
10543 | } else if (sp->ptr > entry->ptr || | |
10544 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10545 | p = &(*p)->rb_right; | |
10546 | } else { | |
195a49ea FM |
10547 | if (is_block_group) |
10548 | entry->bg_extent_count++; | |
ed46ff3d OS |
10549 | spin_unlock(&fs_info->swapfile_pins_lock); |
10550 | kfree(sp); | |
10551 | return 1; | |
10552 | } | |
10553 | } | |
10554 | rb_link_node(&sp->node, parent, p); | |
10555 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10556 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10557 | return 0; | |
10558 | } | |
10559 | ||
10560 | /* Free all of the entries pinned by this swapfile. */ | |
10561 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10562 | { | |
10563 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10564 | struct btrfs_swapfile_pin *sp; | |
10565 | struct rb_node *node, *next; | |
10566 | ||
10567 | spin_lock(&fs_info->swapfile_pins_lock); | |
10568 | node = rb_first(&fs_info->swapfile_pins); | |
10569 | while (node) { | |
10570 | next = rb_next(node); | |
10571 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10572 | if (sp->inode == inode) { | |
10573 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
195a49ea FM |
10574 | if (sp->is_block_group) { |
10575 | btrfs_dec_block_group_swap_extents(sp->ptr, | |
10576 | sp->bg_extent_count); | |
ed46ff3d | 10577 | btrfs_put_block_group(sp->ptr); |
195a49ea | 10578 | } |
ed46ff3d OS |
10579 | kfree(sp); |
10580 | } | |
10581 | node = next; | |
10582 | } | |
10583 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10584 | } | |
10585 | ||
10586 | struct btrfs_swap_info { | |
10587 | u64 start; | |
10588 | u64 block_start; | |
10589 | u64 block_len; | |
10590 | u64 lowest_ppage; | |
10591 | u64 highest_ppage; | |
10592 | unsigned long nr_pages; | |
10593 | int nr_extents; | |
10594 | }; | |
10595 | ||
10596 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10597 | struct btrfs_swap_info *bsi) | |
10598 | { | |
10599 | unsigned long nr_pages; | |
c2f82263 | 10600 | unsigned long max_pages; |
ed46ff3d OS |
10601 | u64 first_ppage, first_ppage_reported, next_ppage; |
10602 | int ret; | |
10603 | ||
c2f82263 FM |
10604 | /* |
10605 | * Our swapfile may have had its size extended after the swap header was | |
10606 | * written. In that case activating the swapfile should not go beyond | |
10607 | * the max size set in the swap header. | |
10608 | */ | |
10609 | if (bsi->nr_pages >= sis->max) | |
10610 | return 0; | |
10611 | ||
10612 | max_pages = sis->max - bsi->nr_pages; | |
ce394a7f YZ |
10613 | first_ppage = PAGE_ALIGN(bsi->block_start) >> PAGE_SHIFT; |
10614 | next_ppage = PAGE_ALIGN_DOWN(bsi->block_start + bsi->block_len) >> PAGE_SHIFT; | |
ed46ff3d OS |
10615 | |
10616 | if (first_ppage >= next_ppage) | |
10617 | return 0; | |
10618 | nr_pages = next_ppage - first_ppage; | |
c2f82263 | 10619 | nr_pages = min(nr_pages, max_pages); |
ed46ff3d OS |
10620 | |
10621 | first_ppage_reported = first_ppage; | |
10622 | if (bsi->start == 0) | |
10623 | first_ppage_reported++; | |
10624 | if (bsi->lowest_ppage > first_ppage_reported) | |
10625 | bsi->lowest_ppage = first_ppage_reported; | |
10626 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10627 | bsi->highest_ppage = next_ppage - 1; | |
10628 | ||
10629 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10630 | if (ret < 0) | |
10631 | return ret; | |
10632 | bsi->nr_extents += ret; | |
10633 | bsi->nr_pages += nr_pages; | |
10634 | return 0; | |
10635 | } | |
10636 | ||
10637 | static void btrfs_swap_deactivate(struct file *file) | |
10638 | { | |
10639 | struct inode *inode = file_inode(file); | |
10640 | ||
10641 | btrfs_free_swapfile_pins(inode); | |
10642 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10643 | } | |
10644 | ||
10645 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10646 | sector_t *span) | |
10647 | { | |
10648 | struct inode *inode = file_inode(file); | |
dd0734f2 FM |
10649 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10650 | struct btrfs_fs_info *fs_info = root->fs_info; | |
ed46ff3d OS |
10651 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
10652 | struct extent_state *cached_state = NULL; | |
10653 | struct extent_map *em = NULL; | |
7dc66abb | 10654 | struct btrfs_chunk_map *map = NULL; |
ed46ff3d OS |
10655 | struct btrfs_device *device = NULL; |
10656 | struct btrfs_swap_info bsi = { | |
10657 | .lowest_ppage = (sector_t)-1ULL, | |
10658 | }; | |
10659 | int ret = 0; | |
10660 | u64 isize; | |
10661 | u64 start; | |
10662 | ||
10663 | /* | |
10664 | * If the swap file was just created, make sure delalloc is done. If the | |
10665 | * file changes again after this, the user is doing something stupid and | |
10666 | * we don't really care. | |
10667 | */ | |
10668 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10669 | if (ret) | |
10670 | return ret; | |
10671 | ||
10672 | /* | |
10673 | * The inode is locked, so these flags won't change after we check them. | |
10674 | */ | |
10675 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10676 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10677 | return -EINVAL; | |
10678 | } | |
10679 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10680 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10681 | return -EINVAL; | |
10682 | } | |
10683 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10684 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10685 | return -EINVAL; | |
10686 | } | |
10687 | ||
10688 | /* | |
10689 | * Balance or device remove/replace/resize can move stuff around from | |
c3e1f96c GR |
10690 | * under us. The exclop protection makes sure they aren't running/won't |
10691 | * run concurrently while we are mapping the swap extents, and | |
10692 | * fs_info->swapfile_pins prevents them from running while the swap | |
10693 | * file is active and moving the extents. Note that this also prevents | |
10694 | * a concurrent device add which isn't actually necessary, but it's not | |
ed46ff3d OS |
10695 | * really worth the trouble to allow it. |
10696 | */ | |
c3e1f96c | 10697 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
ed46ff3d OS |
10698 | btrfs_warn(fs_info, |
10699 | "cannot activate swapfile while exclusive operation is running"); | |
10700 | return -EBUSY; | |
10701 | } | |
dd0734f2 FM |
10702 | |
10703 | /* | |
10704 | * Prevent snapshot creation while we are activating the swap file. | |
10705 | * We do not want to race with snapshot creation. If snapshot creation | |
10706 | * already started before we bumped nr_swapfiles from 0 to 1 and | |
10707 | * completes before the first write into the swap file after it is | |
10708 | * activated, than that write would fallback to COW. | |
10709 | */ | |
10710 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { | |
10711 | btrfs_exclop_finish(fs_info); | |
10712 | btrfs_warn(fs_info, | |
10713 | "cannot activate swapfile because snapshot creation is in progress"); | |
10714 | return -EINVAL; | |
10715 | } | |
ed46ff3d OS |
10716 | /* |
10717 | * Snapshots can create extents which require COW even if NODATACOW is | |
10718 | * set. We use this counter to prevent snapshots. We must increment it | |
10719 | * before walking the extents because we don't want a concurrent | |
10720 | * snapshot to run after we've already checked the extents. | |
60021bd7 KH |
10721 | * |
10722 | * It is possible that subvolume is marked for deletion but still not | |
10723 | * removed yet. To prevent this race, we check the root status before | |
10724 | * activating the swapfile. | |
ed46ff3d | 10725 | */ |
60021bd7 KH |
10726 | spin_lock(&root->root_item_lock); |
10727 | if (btrfs_root_dead(root)) { | |
10728 | spin_unlock(&root->root_item_lock); | |
10729 | ||
10730 | btrfs_exclop_finish(fs_info); | |
10731 | btrfs_warn(fs_info, | |
10732 | "cannot activate swapfile because subvolume %llu is being deleted", | |
10733 | root->root_key.objectid); | |
10734 | return -EPERM; | |
10735 | } | |
dd0734f2 | 10736 | atomic_inc(&root->nr_swapfiles); |
60021bd7 | 10737 | spin_unlock(&root->root_item_lock); |
ed46ff3d OS |
10738 | |
10739 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10740 | ||
570eb97b | 10741 | lock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
10742 | start = 0; |
10743 | while (start < isize) { | |
10744 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10745 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10746 | u64 len = isize - start; |
10747 | ||
8bab0a30 | 10748 | em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len); |
ed46ff3d OS |
10749 | if (IS_ERR(em)) { |
10750 | ret = PTR_ERR(em); | |
10751 | goto out; | |
10752 | } | |
10753 | ||
10754 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10755 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10756 | ret = -EINVAL; | |
10757 | goto out; | |
10758 | } | |
10759 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10760 | /* | |
10761 | * It's unlikely we'll ever actually find ourselves | |
10762 | * here, as a file small enough to fit inline won't be | |
10763 | * big enough to store more than the swap header, but in | |
10764 | * case something changes in the future, let's catch it | |
10765 | * here rather than later. | |
10766 | */ | |
10767 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10768 | ret = -EINVAL; | |
10769 | goto out; | |
10770 | } | |
f86f7a75 | 10771 | if (extent_map_is_compressed(em)) { |
ed46ff3d OS |
10772 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
10773 | ret = -EINVAL; | |
10774 | goto out; | |
10775 | } | |
10776 | ||
10777 | logical_block_start = em->block_start + (start - em->start); | |
10778 | len = min(len, em->len - (start - em->start)); | |
10779 | free_extent_map(em); | |
10780 | em = NULL; | |
10781 | ||
26ce9114 | 10782 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true); |
ed46ff3d OS |
10783 | if (ret < 0) { |
10784 | goto out; | |
10785 | } else if (ret) { | |
10786 | ret = 0; | |
10787 | } else { | |
10788 | btrfs_warn(fs_info, | |
10789 | "swapfile must not be copy-on-write"); | |
10790 | ret = -EINVAL; | |
10791 | goto out; | |
10792 | } | |
10793 | ||
7dc66abb FM |
10794 | map = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
10795 | if (IS_ERR(map)) { | |
10796 | ret = PTR_ERR(map); | |
ed46ff3d OS |
10797 | goto out; |
10798 | } | |
10799 | ||
7dc66abb | 10800 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
ed46ff3d OS |
10801 | btrfs_warn(fs_info, |
10802 | "swapfile must have single data profile"); | |
10803 | ret = -EINVAL; | |
10804 | goto out; | |
10805 | } | |
10806 | ||
10807 | if (device == NULL) { | |
7dc66abb | 10808 | device = map->stripes[0].dev; |
ed46ff3d OS |
10809 | ret = btrfs_add_swapfile_pin(inode, device, false); |
10810 | if (ret == 1) | |
10811 | ret = 0; | |
10812 | else if (ret) | |
10813 | goto out; | |
7dc66abb | 10814 | } else if (device != map->stripes[0].dev) { |
ed46ff3d OS |
10815 | btrfs_warn(fs_info, "swapfile must be on one device"); |
10816 | ret = -EINVAL; | |
10817 | goto out; | |
10818 | } | |
10819 | ||
7dc66abb FM |
10820 | physical_block_start = (map->stripes[0].physical + |
10821 | (logical_block_start - map->start)); | |
10822 | len = min(len, map->chunk_len - (logical_block_start - map->start)); | |
10823 | btrfs_free_chunk_map(map); | |
10824 | map = NULL; | |
ed46ff3d OS |
10825 | |
10826 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10827 | if (!bg) { | |
10828 | btrfs_warn(fs_info, | |
10829 | "could not find block group containing swapfile"); | |
10830 | ret = -EINVAL; | |
10831 | goto out; | |
10832 | } | |
10833 | ||
195a49ea FM |
10834 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
10835 | btrfs_warn(fs_info, | |
10836 | "block group for swapfile at %llu is read-only%s", | |
10837 | bg->start, | |
10838 | atomic_read(&fs_info->scrubs_running) ? | |
10839 | " (scrub running)" : ""); | |
10840 | btrfs_put_block_group(bg); | |
10841 | ret = -EINVAL; | |
10842 | goto out; | |
10843 | } | |
10844 | ||
ed46ff3d OS |
10845 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
10846 | if (ret) { | |
10847 | btrfs_put_block_group(bg); | |
10848 | if (ret == 1) | |
10849 | ret = 0; | |
10850 | else | |
10851 | goto out; | |
10852 | } | |
10853 | ||
10854 | if (bsi.block_len && | |
10855 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10856 | bsi.block_len += len; | |
10857 | } else { | |
10858 | if (bsi.block_len) { | |
10859 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10860 | if (ret) | |
10861 | goto out; | |
10862 | } | |
10863 | bsi.start = start; | |
10864 | bsi.block_start = physical_block_start; | |
10865 | bsi.block_len = len; | |
10866 | } | |
10867 | ||
10868 | start += len; | |
10869 | } | |
10870 | ||
10871 | if (bsi.block_len) | |
10872 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10873 | ||
10874 | out: | |
10875 | if (!IS_ERR_OR_NULL(em)) | |
10876 | free_extent_map(em); | |
7dc66abb FM |
10877 | if (!IS_ERR_OR_NULL(map)) |
10878 | btrfs_free_chunk_map(map); | |
ed46ff3d | 10879 | |
570eb97b | 10880 | unlock_extent(io_tree, 0, isize - 1, &cached_state); |
ed46ff3d OS |
10881 | |
10882 | if (ret) | |
10883 | btrfs_swap_deactivate(file); | |
10884 | ||
dd0734f2 FM |
10885 | btrfs_drew_write_unlock(&root->snapshot_lock); |
10886 | ||
c3e1f96c | 10887 | btrfs_exclop_finish(fs_info); |
ed46ff3d OS |
10888 | |
10889 | if (ret) | |
10890 | return ret; | |
10891 | ||
10892 | if (device) | |
10893 | sis->bdev = device->bdev; | |
10894 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10895 | sis->max = bsi.nr_pages; | |
10896 | sis->pages = bsi.nr_pages - 1; | |
10897 | sis->highest_bit = bsi.nr_pages - 1; | |
10898 | return bsi.nr_extents; | |
10899 | } | |
10900 | #else | |
10901 | static void btrfs_swap_deactivate(struct file *file) | |
10902 | { | |
10903 | } | |
10904 | ||
10905 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10906 | sector_t *span) | |
10907 | { | |
10908 | return -EOPNOTSUPP; | |
10909 | } | |
10910 | #endif | |
10911 | ||
2766ff61 FM |
10912 | /* |
10913 | * Update the number of bytes used in the VFS' inode. When we replace extents in | |
10914 | * a range (clone, dedupe, fallocate's zero range), we must update the number of | |
10915 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls | |
10916 | * always get a correct value. | |
10917 | */ | |
10918 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, | |
10919 | const u64 add_bytes, | |
10920 | const u64 del_bytes) | |
10921 | { | |
10922 | if (add_bytes == del_bytes) | |
10923 | return; | |
10924 | ||
10925 | spin_lock(&inode->lock); | |
10926 | if (del_bytes > 0) | |
10927 | inode_sub_bytes(&inode->vfs_inode, del_bytes); | |
10928 | if (add_bytes > 0) | |
10929 | inode_add_bytes(&inode->vfs_inode, add_bytes); | |
10930 | spin_unlock(&inode->lock); | |
10931 | } | |
10932 | ||
43dd529a | 10933 | /* |
63c34cb4 FM |
10934 | * Verify that there are no ordered extents for a given file range. |
10935 | * | |
10936 | * @inode: The target inode. | |
10937 | * @start: Start offset of the file range, should be sector size aligned. | |
10938 | * @end: End offset (inclusive) of the file range, its value +1 should be | |
10939 | * sector size aligned. | |
10940 | * | |
10941 | * This should typically be used for cases where we locked an inode's VFS lock in | |
10942 | * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode, | |
10943 | * we have flushed all delalloc in the range, we have waited for all ordered | |
10944 | * extents in the range to complete and finally we have locked the file range in | |
10945 | * the inode's io_tree. | |
10946 | */ | |
10947 | void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end) | |
10948 | { | |
10949 | struct btrfs_root *root = inode->root; | |
10950 | struct btrfs_ordered_extent *ordered; | |
10951 | ||
10952 | if (!IS_ENABLED(CONFIG_BTRFS_ASSERT)) | |
10953 | return; | |
10954 | ||
10955 | ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start); | |
10956 | if (ordered) { | |
10957 | btrfs_err(root->fs_info, | |
10958 | "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])", | |
10959 | start, end, btrfs_ino(inode), root->root_key.objectid, | |
10960 | ordered->file_offset, | |
10961 | ordered->file_offset + ordered->num_bytes - 1); | |
10962 | btrfs_put_ordered_extent(ordered); | |
10963 | } | |
10964 | ||
10965 | ASSERT(ordered == NULL); | |
10966 | } | |
10967 | ||
6e1d5dcc | 10968 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10969 | .getattr = btrfs_getattr, |
39279cc3 CM |
10970 | .lookup = btrfs_lookup, |
10971 | .create = btrfs_create, | |
10972 | .unlink = btrfs_unlink, | |
10973 | .link = btrfs_link, | |
10974 | .mkdir = btrfs_mkdir, | |
10975 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10976 | .rename = btrfs_rename2, |
39279cc3 CM |
10977 | .symlink = btrfs_symlink, |
10978 | .setattr = btrfs_setattr, | |
618e21d5 | 10979 | .mknod = btrfs_mknod, |
5103e947 | 10980 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10981 | .permission = btrfs_permission, |
cac2f8b8 | 10982 | .get_inode_acl = btrfs_get_acl, |
996a710d | 10983 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10984 | .update_time = btrfs_update_time, |
ef3b9af5 | 10985 | .tmpfile = btrfs_tmpfile, |
97fc2977 MS |
10986 | .fileattr_get = btrfs_fileattr_get, |
10987 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 10988 | }; |
76dda93c | 10989 | |
828c0950 | 10990 | static const struct file_operations btrfs_dir_file_operations = { |
e60aa5da | 10991 | .llseek = btrfs_dir_llseek, |
39279cc3 | 10992 | .read = generic_read_dir, |
02dbfc99 | 10993 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10994 | .open = btrfs_opendir, |
34287aa3 | 10995 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10996 | #ifdef CONFIG_COMPAT |
4c63c245 | 10997 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10998 | #endif |
6bf13c0c | 10999 | .release = btrfs_release_file, |
e02119d5 | 11000 | .fsync = btrfs_sync_file, |
39279cc3 CM |
11001 | }; |
11002 | ||
35054394 CM |
11003 | /* |
11004 | * btrfs doesn't support the bmap operation because swapfiles | |
11005 | * use bmap to make a mapping of extents in the file. They assume | |
11006 | * these extents won't change over the life of the file and they | |
11007 | * use the bmap result to do IO directly to the drive. | |
11008 | * | |
11009 | * the btrfs bmap call would return logical addresses that aren't | |
11010 | * suitable for IO and they also will change frequently as COW | |
11011 | * operations happen. So, swapfile + btrfs == corruption. | |
11012 | * | |
11013 | * For now we're avoiding this by dropping bmap. | |
11014 | */ | |
7f09410b | 11015 | static const struct address_space_operations btrfs_aops = { |
fb12489b | 11016 | .read_folio = btrfs_read_folio, |
b293f02e | 11017 | .writepages = btrfs_writepages, |
ba206a02 | 11018 | .readahead = btrfs_readahead, |
895586eb | 11019 | .invalidate_folio = btrfs_invalidate_folio, |
f913cff3 | 11020 | .release_folio = btrfs_release_folio, |
e7a60a17 | 11021 | .migrate_folio = btrfs_migrate_folio, |
187c82cb | 11022 | .dirty_folio = filemap_dirty_folio, |
af7628d6 | 11023 | .error_remove_folio = generic_error_remove_folio, |
ed46ff3d OS |
11024 | .swap_activate = btrfs_swap_activate, |
11025 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
11026 | }; |
11027 | ||
6e1d5dcc | 11028 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
11029 | .getattr = btrfs_getattr, |
11030 | .setattr = btrfs_setattr, | |
5103e947 | 11031 | .listxattr = btrfs_listxattr, |
fdebe2bd | 11032 | .permission = btrfs_permission, |
1506fcc8 | 11033 | .fiemap = btrfs_fiemap, |
cac2f8b8 | 11034 | .get_inode_acl = btrfs_get_acl, |
996a710d | 11035 | .set_acl = btrfs_set_acl, |
e41f941a | 11036 | .update_time = btrfs_update_time, |
97fc2977 MS |
11037 | .fileattr_get = btrfs_fileattr_get, |
11038 | .fileattr_set = btrfs_fileattr_set, | |
39279cc3 | 11039 | }; |
6e1d5dcc | 11040 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
11041 | .getattr = btrfs_getattr, |
11042 | .setattr = btrfs_setattr, | |
fdebe2bd | 11043 | .permission = btrfs_permission, |
33268eaf | 11044 | .listxattr = btrfs_listxattr, |
cac2f8b8 | 11045 | .get_inode_acl = btrfs_get_acl, |
996a710d | 11046 | .set_acl = btrfs_set_acl, |
e41f941a | 11047 | .update_time = btrfs_update_time, |
618e21d5 | 11048 | }; |
6e1d5dcc | 11049 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 11050 | .get_link = page_get_link, |
f209561a | 11051 | .getattr = btrfs_getattr, |
22c44fe6 | 11052 | .setattr = btrfs_setattr, |
fdebe2bd | 11053 | .permission = btrfs_permission, |
0279b4cd | 11054 | .listxattr = btrfs_listxattr, |
e41f941a | 11055 | .update_time = btrfs_update_time, |
39279cc3 | 11056 | }; |
76dda93c | 11057 | |
82d339d9 | 11058 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
11059 | .d_delete = btrfs_dentry_delete, |
11060 | }; |