]>
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> |
f2eb0a24 | 9 | #include <linux/file.h> |
39279cc3 CM |
10 | #include <linux/fs.h> |
11 | #include <linux/pagemap.h> | |
12 | #include <linux/highmem.h> | |
13 | #include <linux/time.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/string.h> | |
39279cc3 | 16 | #include <linux/backing-dev.h> |
39279cc3 | 17 | #include <linux/writeback.h> |
39279cc3 | 18 | #include <linux/compat.h> |
5103e947 | 19 | #include <linux/xattr.h> |
33268eaf | 20 | #include <linux/posix_acl.h> |
d899e052 | 21 | #include <linux/falloc.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
7a36ddec | 23 | #include <linux/ratelimit.h> |
55e301fd | 24 | #include <linux/btrfs.h> |
53b381b3 | 25 | #include <linux/blkdev.h> |
f23b5a59 | 26 | #include <linux/posix_acl_xattr.h> |
e2e40f2c | 27 | #include <linux/uio.h> |
69fe2d75 | 28 | #include <linux/magic.h> |
ae5e165d | 29 | #include <linux/iversion.h> |
ed46ff3d | 30 | #include <linux/swap.h> |
f8e66081 | 31 | #include <linux/migrate.h> |
b1c16ac9 | 32 | #include <linux/sched/mm.h> |
f85781fb | 33 | #include <linux/iomap.h> |
92d32170 | 34 | #include <asm/unaligned.h> |
602cbe91 | 35 | #include "misc.h" |
39279cc3 CM |
36 | #include "ctree.h" |
37 | #include "disk-io.h" | |
38 | #include "transaction.h" | |
39 | #include "btrfs_inode.h" | |
39279cc3 | 40 | #include "print-tree.h" |
e6dcd2dc | 41 | #include "ordered-data.h" |
95819c05 | 42 | #include "xattr.h" |
e02119d5 | 43 | #include "tree-log.h" |
4a54c8c1 | 44 | #include "volumes.h" |
c8b97818 | 45 | #include "compression.h" |
b4ce94de | 46 | #include "locking.h" |
dc89e982 | 47 | #include "free-space-cache.h" |
581bb050 | 48 | #include "inode-map.h" |
63541927 | 49 | #include "props.h" |
31193213 | 50 | #include "qgroup.h" |
86736342 | 51 | #include "delalloc-space.h" |
aac0023c | 52 | #include "block-group.h" |
467dc47e | 53 | #include "space-info.h" |
39279cc3 CM |
54 | |
55 | struct btrfs_iget_args { | |
0202e83f | 56 | u64 ino; |
39279cc3 CM |
57 | struct btrfs_root *root; |
58 | }; | |
59 | ||
f28a4928 | 60 | struct btrfs_dio_data { |
f28a4928 | 61 | u64 reserve; |
f85781fb GR |
62 | loff_t length; |
63 | ssize_t submitted; | |
64 | struct extent_changeset *data_reserved; | |
f28a4928 FM |
65 | }; |
66 | ||
6e1d5dcc AD |
67 | static const struct inode_operations btrfs_dir_inode_operations; |
68 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
69 | static const struct inode_operations btrfs_special_inode_operations; |
70 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 71 | static const struct address_space_operations btrfs_aops; |
828c0950 | 72 | static const struct file_operations btrfs_dir_file_operations; |
39279cc3 CM |
73 | |
74 | static struct kmem_cache *btrfs_inode_cachep; | |
75 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 76 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 77 | struct kmem_cache *btrfs_free_space_cachep; |
3acd4850 | 78 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
39279cc3 | 79 | |
3972f260 | 80 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 81 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
5fd02043 | 82 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
6e26c442 | 83 | static noinline int cow_file_range(struct btrfs_inode *inode, |
771ed689 | 84 | struct page *locked_page, |
74e9194a | 85 | u64 start, u64 end, int *page_started, |
330a5827 | 86 | unsigned long *nr_written, int unlock); |
4b67c11d NB |
87 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
88 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
89 | u64 block_len, u64 orig_block_len, |
90 | u64 ram_bytes, int compress_type, | |
91 | int type); | |
7b128766 | 92 | |
b672b5c1 | 93 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
94 | const u64 offset, const u64 bytes, |
95 | const bool uptodate); | |
96 | ||
a14b78ad GR |
97 | /* |
98 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed | |
99 | * | |
100 | * ilock_flags can have the following bit set: | |
101 | * | |
102 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode | |
103 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt | |
104 | * return -EAGAIN | |
105 | */ | |
106 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) | |
107 | { | |
108 | if (ilock_flags & BTRFS_ILOCK_SHARED) { | |
109 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
110 | if (!inode_trylock_shared(inode)) | |
111 | return -EAGAIN; | |
112 | else | |
113 | return 0; | |
114 | } | |
115 | inode_lock_shared(inode); | |
116 | } else { | |
117 | if (ilock_flags & BTRFS_ILOCK_TRY) { | |
118 | if (!inode_trylock(inode)) | |
119 | return -EAGAIN; | |
120 | else | |
121 | return 0; | |
122 | } | |
123 | inode_lock(inode); | |
124 | } | |
125 | return 0; | |
126 | } | |
127 | ||
128 | /* | |
129 | * btrfs_inode_unlock - unock inode i_rwsem | |
130 | * | |
131 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() | |
132 | * to decide whether the lock acquired is shared or exclusive. | |
133 | */ | |
134 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) | |
135 | { | |
136 | if (ilock_flags & BTRFS_ILOCK_SHARED) | |
137 | inode_unlock_shared(inode); | |
138 | else | |
139 | inode_unlock(inode); | |
140 | } | |
141 | ||
52427260 QW |
142 | /* |
143 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 144 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
145 | * |
146 | * NOTE: caller must ensure that when an error happens, it can not call | |
147 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
148 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
149 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 150 | * extent (btrfs_finish_ordered_io()). |
52427260 | 151 | */ |
64e1db56 | 152 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
153 | struct page *locked_page, |
154 | u64 offset, u64 bytes) | |
52427260 | 155 | { |
63d71450 NA |
156 | unsigned long index = offset >> PAGE_SHIFT; |
157 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
d1051d6e NB |
158 | u64 page_start = page_offset(locked_page); |
159 | u64 page_end = page_start + PAGE_SIZE - 1; | |
160 | ||
63d71450 NA |
161 | struct page *page; |
162 | ||
163 | while (index <= end_index) { | |
64e1db56 | 164 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
165 | index++; |
166 | if (!page) | |
167 | continue; | |
168 | ClearPagePrivate2(page); | |
169 | put_page(page); | |
170 | } | |
d1051d6e NB |
171 | |
172 | /* | |
173 | * In case this page belongs to the delalloc range being instantiated | |
174 | * then skip it, since the first page of a range is going to be | |
175 | * properly cleaned up by the caller of run_delalloc_range | |
176 | */ | |
177 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
178 | offset += PAGE_SIZE; | |
179 | bytes -= PAGE_SIZE; | |
180 | } | |
181 | ||
64e1db56 | 182 | return __endio_write_update_ordered(inode, offset, bytes, false); |
52427260 QW |
183 | } |
184 | ||
48a3b636 | 185 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 186 | |
f34f57a3 | 187 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
188 | struct inode *inode, struct inode *dir, |
189 | const struct qstr *qstr) | |
0279b4cd JO |
190 | { |
191 | int err; | |
192 | ||
f34f57a3 | 193 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 194 | if (!err) |
2a7dba39 | 195 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
196 | return err; |
197 | } | |
198 | ||
c8b97818 CM |
199 | /* |
200 | * this does all the hard work for inserting an inline extent into | |
201 | * the btree. The caller should have done a btrfs_drop_extents so that | |
202 | * no overlapping inline items exist in the btree | |
203 | */ | |
40f76580 | 204 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 205 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
206 | struct btrfs_root *root, struct inode *inode, |
207 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 208 | int compress_type, |
c8b97818 CM |
209 | struct page **compressed_pages) |
210 | { | |
c8b97818 CM |
211 | struct extent_buffer *leaf; |
212 | struct page *page = NULL; | |
213 | char *kaddr; | |
214 | unsigned long ptr; | |
215 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
216 | int ret; |
217 | size_t cur_size = size; | |
c8b97818 | 218 | unsigned long offset; |
c8b97818 | 219 | |
982f1f5d JJB |
220 | ASSERT((compressed_size > 0 && compressed_pages) || |
221 | (compressed_size == 0 && !compressed_pages)); | |
222 | ||
fe3f566c | 223 | if (compressed_size && compressed_pages) |
c8b97818 | 224 | cur_size = compressed_size; |
c8b97818 | 225 | |
1acae57b | 226 | inode_add_bytes(inode, size); |
c8b97818 | 227 | |
1acae57b FDBM |
228 | if (!extent_inserted) { |
229 | struct btrfs_key key; | |
230 | size_t datasize; | |
c8b97818 | 231 | |
4a0cc7ca | 232 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 233 | key.offset = start; |
962a298f | 234 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 235 | |
1acae57b FDBM |
236 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
237 | path->leave_spinning = 1; | |
238 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
239 | datasize); | |
79b4f4c6 | 240 | if (ret) |
1acae57b | 241 | goto fail; |
c8b97818 CM |
242 | } |
243 | leaf = path->nodes[0]; | |
244 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
245 | struct btrfs_file_extent_item); | |
246 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
247 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
248 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
249 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
250 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
251 | ptr = btrfs_file_extent_inline_start(ei); | |
252 | ||
261507a0 | 253 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
254 | struct page *cpage; |
255 | int i = 0; | |
d397712b | 256 | while (compressed_size > 0) { |
c8b97818 | 257 | cpage = compressed_pages[i]; |
5b050f04 | 258 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 259 | PAGE_SIZE); |
c8b97818 | 260 | |
7ac687d9 | 261 | kaddr = kmap_atomic(cpage); |
c8b97818 | 262 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 263 | kunmap_atomic(kaddr); |
c8b97818 CM |
264 | |
265 | i++; | |
266 | ptr += cur_size; | |
267 | compressed_size -= cur_size; | |
268 | } | |
269 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 270 | compress_type); |
c8b97818 CM |
271 | } else { |
272 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 273 | start >> PAGE_SHIFT); |
c8b97818 | 274 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 275 | kaddr = kmap_atomic(page); |
7073017a | 276 | offset = offset_in_page(start); |
c8b97818 | 277 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 278 | kunmap_atomic(kaddr); |
09cbfeaf | 279 | put_page(page); |
c8b97818 CM |
280 | } |
281 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 282 | btrfs_release_path(path); |
c8b97818 | 283 | |
9ddc959e JB |
284 | /* |
285 | * We align size to sectorsize for inline extents just for simplicity | |
286 | * sake. | |
287 | */ | |
288 | size = ALIGN(size, root->fs_info->sectorsize); | |
289 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); | |
290 | if (ret) | |
291 | goto fail; | |
292 | ||
c2167754 YZ |
293 | /* |
294 | * we're an inline extent, so nobody can | |
295 | * extend the file past i_size without locking | |
296 | * a page we already have locked. | |
297 | * | |
298 | * We must do any isize and inode updates | |
299 | * before we unlock the pages. Otherwise we | |
300 | * could end up racing with unlink. | |
301 | */ | |
c8b97818 | 302 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 303 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 304 | |
c8b97818 | 305 | fail: |
79b4f4c6 | 306 | return ret; |
c8b97818 CM |
307 | } |
308 | ||
309 | ||
310 | /* | |
311 | * conditionally insert an inline extent into the file. This | |
312 | * does the checks required to make sure the data is small enough | |
313 | * to fit as an inline extent. | |
314 | */ | |
a0349401 | 315 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, |
00361589 JB |
316 | u64 end, size_t compressed_size, |
317 | int compress_type, | |
318 | struct page **compressed_pages) | |
c8b97818 | 319 | { |
a0349401 | 320 | struct btrfs_root *root = inode->root; |
0b246afa | 321 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 322 | struct btrfs_trans_handle *trans; |
a0349401 | 323 | u64 isize = i_size_read(&inode->vfs_inode); |
c8b97818 CM |
324 | u64 actual_end = min(end + 1, isize); |
325 | u64 inline_len = actual_end - start; | |
0b246afa | 326 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
327 | u64 data_len = inline_len; |
328 | int ret; | |
1acae57b FDBM |
329 | struct btrfs_path *path; |
330 | int extent_inserted = 0; | |
331 | u32 extent_item_size; | |
c8b97818 CM |
332 | |
333 | if (compressed_size) | |
334 | data_len = compressed_size; | |
335 | ||
336 | if (start > 0 || | |
0b246afa JM |
337 | actual_end > fs_info->sectorsize || |
338 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 339 | (!compressed_size && |
0b246afa | 340 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 341 | end + 1 < isize || |
0b246afa | 342 | data_len > fs_info->max_inline) { |
c8b97818 CM |
343 | return 1; |
344 | } | |
345 | ||
1acae57b FDBM |
346 | path = btrfs_alloc_path(); |
347 | if (!path) | |
348 | return -ENOMEM; | |
349 | ||
00361589 | 350 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
351 | if (IS_ERR(trans)) { |
352 | btrfs_free_path(path); | |
00361589 | 353 | return PTR_ERR(trans); |
1acae57b | 354 | } |
a0349401 | 355 | trans->block_rsv = &inode->block_rsv; |
00361589 | 356 | |
1acae57b FDBM |
357 | if (compressed_size && compressed_pages) |
358 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
359 | compressed_size); | |
360 | else | |
361 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
362 | inline_len); | |
363 | ||
a0349401 NB |
364 | ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end, |
365 | NULL, 1, 1, extent_item_size, | |
366 | &extent_inserted); | |
00361589 | 367 | if (ret) { |
66642832 | 368 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
369 | goto out; |
370 | } | |
c8b97818 CM |
371 | |
372 | if (isize > actual_end) | |
373 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b | 374 | ret = insert_inline_extent(trans, path, extent_inserted, |
a0349401 | 375 | root, &inode->vfs_inode, start, |
c8b97818 | 376 | inline_len, compressed_size, |
fe3f566c | 377 | compress_type, compressed_pages); |
2adcac1a | 378 | if (ret && ret != -ENOSPC) { |
66642832 | 379 | btrfs_abort_transaction(trans, ret); |
00361589 | 380 | goto out; |
2adcac1a | 381 | } else if (ret == -ENOSPC) { |
00361589 JB |
382 | ret = 1; |
383 | goto out; | |
79787eaa | 384 | } |
2adcac1a | 385 | |
a0349401 NB |
386 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
387 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); | |
00361589 | 388 | out: |
94ed938a QW |
389 | /* |
390 | * Don't forget to free the reserved space, as for inlined extent | |
391 | * it won't count as data extent, free them directly here. | |
392 | * And at reserve time, it's always aligned to page size, so | |
393 | * just free one page here. | |
394 | */ | |
a0349401 | 395 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 396 | btrfs_free_path(path); |
3a45bb20 | 397 | btrfs_end_transaction(trans); |
00361589 | 398 | return ret; |
c8b97818 CM |
399 | } |
400 | ||
771ed689 CM |
401 | struct async_extent { |
402 | u64 start; | |
403 | u64 ram_size; | |
404 | u64 compressed_size; | |
405 | struct page **pages; | |
406 | unsigned long nr_pages; | |
261507a0 | 407 | int compress_type; |
771ed689 CM |
408 | struct list_head list; |
409 | }; | |
410 | ||
97db1204 | 411 | struct async_chunk { |
771ed689 | 412 | struct inode *inode; |
771ed689 CM |
413 | struct page *locked_page; |
414 | u64 start; | |
415 | u64 end; | |
f82b7359 | 416 | unsigned int write_flags; |
771ed689 | 417 | struct list_head extents; |
ec39f769 | 418 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 419 | struct btrfs_work work; |
97db1204 | 420 | atomic_t *pending; |
771ed689 CM |
421 | }; |
422 | ||
97db1204 NB |
423 | struct async_cow { |
424 | /* Number of chunks in flight; must be first in the structure */ | |
425 | atomic_t num_chunks; | |
426 | struct async_chunk chunks[]; | |
771ed689 CM |
427 | }; |
428 | ||
97db1204 | 429 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
430 | u64 start, u64 ram_size, |
431 | u64 compressed_size, | |
432 | struct page **pages, | |
261507a0 LZ |
433 | unsigned long nr_pages, |
434 | int compress_type) | |
771ed689 CM |
435 | { |
436 | struct async_extent *async_extent; | |
437 | ||
438 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 439 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
440 | async_extent->start = start; |
441 | async_extent->ram_size = ram_size; | |
442 | async_extent->compressed_size = compressed_size; | |
443 | async_extent->pages = pages; | |
444 | async_extent->nr_pages = nr_pages; | |
261507a0 | 445 | async_extent->compress_type = compress_type; |
771ed689 CM |
446 | list_add_tail(&async_extent->list, &cow->extents); |
447 | return 0; | |
448 | } | |
449 | ||
42c16da6 QW |
450 | /* |
451 | * Check if the inode has flags compatible with compression | |
452 | */ | |
99c88dc7 | 453 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
42c16da6 | 454 | { |
99c88dc7 NB |
455 | if (inode->flags & BTRFS_INODE_NODATACOW || |
456 | inode->flags & BTRFS_INODE_NODATASUM) | |
42c16da6 QW |
457 | return false; |
458 | return true; | |
459 | } | |
460 | ||
461 | /* | |
462 | * Check if the inode needs to be submitted to compression, based on mount | |
463 | * options, defragmentation, properties or heuristics. | |
464 | */ | |
808a1292 NB |
465 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
466 | u64 end) | |
f79707b0 | 467 | { |
808a1292 | 468 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 469 | |
808a1292 | 470 | if (!inode_can_compress(inode)) { |
42c16da6 QW |
471 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
472 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 473 | btrfs_ino(inode)); |
42c16da6 QW |
474 | return 0; |
475 | } | |
f79707b0 | 476 | /* force compress */ |
0b246afa | 477 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 478 | return 1; |
eec63c65 | 479 | /* defrag ioctl */ |
808a1292 | 480 | if (inode->defrag_compress) |
eec63c65 | 481 | return 1; |
f79707b0 | 482 | /* bad compression ratios */ |
808a1292 | 483 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 484 | return 0; |
0b246afa | 485 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
486 | inode->flags & BTRFS_INODE_COMPRESS || |
487 | inode->prop_compress) | |
488 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
489 | return 0; |
490 | } | |
491 | ||
6158e1ce | 492 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
493 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
494 | { | |
495 | /* If this is a small write inside eof, kick off a defrag */ | |
496 | if (num_bytes < small_write && | |
6158e1ce | 497 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
498 | btrfs_add_inode_defrag(NULL, inode); |
499 | } | |
500 | ||
d352ac68 | 501 | /* |
771ed689 CM |
502 | * we create compressed extents in two phases. The first |
503 | * phase compresses a range of pages that have already been | |
504 | * locked (both pages and state bits are locked). | |
c8b97818 | 505 | * |
771ed689 CM |
506 | * This is done inside an ordered work queue, and the compression |
507 | * is spread across many cpus. The actual IO submission is step | |
508 | * two, and the ordered work queue takes care of making sure that | |
509 | * happens in the same order things were put onto the queue by | |
510 | * writepages and friends. | |
c8b97818 | 511 | * |
771ed689 CM |
512 | * If this code finds it can't get good compression, it puts an |
513 | * entry onto the work queue to write the uncompressed bytes. This | |
514 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
515 | * are written in the same order that the flusher thread sent them |
516 | * down. | |
d352ac68 | 517 | */ |
ac3e9933 | 518 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 519 | { |
1368c6da | 520 | struct inode *inode = async_chunk->inode; |
0b246afa | 521 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 522 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
523 | u64 start = async_chunk->start; |
524 | u64 end = async_chunk->end; | |
c8b97818 | 525 | u64 actual_end; |
d98da499 | 526 | u64 i_size; |
e6dcd2dc | 527 | int ret = 0; |
c8b97818 CM |
528 | struct page **pages = NULL; |
529 | unsigned long nr_pages; | |
c8b97818 CM |
530 | unsigned long total_compressed = 0; |
531 | unsigned long total_in = 0; | |
c8b97818 CM |
532 | int i; |
533 | int will_compress; | |
0b246afa | 534 | int compress_type = fs_info->compress_type; |
ac3e9933 | 535 | int compressed_extents = 0; |
4adaa611 | 536 | int redirty = 0; |
b888db2b | 537 | |
6158e1ce NB |
538 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
539 | SZ_16K); | |
4cb5300b | 540 | |
d98da499 JB |
541 | /* |
542 | * We need to save i_size before now because it could change in between | |
543 | * us evaluating the size and assigning it. This is because we lock and | |
544 | * unlock the page in truncate and fallocate, and then modify the i_size | |
545 | * later on. | |
546 | * | |
547 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
548 | * does that for us. | |
549 | */ | |
550 | barrier(); | |
551 | i_size = i_size_read(inode); | |
552 | barrier(); | |
553 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
554 | again: |
555 | will_compress = 0; | |
09cbfeaf | 556 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
557 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
558 | nr_pages = min_t(unsigned long, nr_pages, | |
559 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 560 | |
f03d9301 CM |
561 | /* |
562 | * we don't want to send crud past the end of i_size through | |
563 | * compression, that's just a waste of CPU time. So, if the | |
564 | * end of the file is before the start of our current | |
565 | * requested range of bytes, we bail out to the uncompressed | |
566 | * cleanup code that can deal with all of this. | |
567 | * | |
568 | * It isn't really the fastest way to fix things, but this is a | |
569 | * very uncommon corner. | |
570 | */ | |
571 | if (actual_end <= start) | |
572 | goto cleanup_and_bail_uncompressed; | |
573 | ||
c8b97818 CM |
574 | total_compressed = actual_end - start; |
575 | ||
4bcbb332 SW |
576 | /* |
577 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 578 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
579 | */ |
580 | if (total_compressed <= blocksize && | |
581 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
582 | goto cleanup_and_bail_uncompressed; | |
583 | ||
069eac78 DS |
584 | total_compressed = min_t(unsigned long, total_compressed, |
585 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
586 | total_in = 0; |
587 | ret = 0; | |
db94535d | 588 | |
771ed689 CM |
589 | /* |
590 | * we do compression for mount -o compress and when the | |
591 | * inode has not been flagged as nocompress. This flag can | |
592 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 593 | */ |
808a1292 | 594 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
c8b97818 | 595 | WARN_ON(pages); |
31e818fe | 596 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
597 | if (!pages) { |
598 | /* just bail out to the uncompressed code */ | |
3527a018 | 599 | nr_pages = 0; |
560f7d75 LZ |
600 | goto cont; |
601 | } | |
c8b97818 | 602 | |
eec63c65 DS |
603 | if (BTRFS_I(inode)->defrag_compress) |
604 | compress_type = BTRFS_I(inode)->defrag_compress; | |
605 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 606 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 607 | |
4adaa611 CM |
608 | /* |
609 | * we need to call clear_page_dirty_for_io on each | |
610 | * page in the range. Otherwise applications with the file | |
611 | * mmap'd can wander in and change the page contents while | |
612 | * we are compressing them. | |
613 | * | |
614 | * If the compression fails for any reason, we set the pages | |
615 | * dirty again later on. | |
e9679de3 TT |
616 | * |
617 | * Note that the remaining part is redirtied, the start pointer | |
618 | * has moved, the end is the original one. | |
4adaa611 | 619 | */ |
e9679de3 TT |
620 | if (!redirty) { |
621 | extent_range_clear_dirty_for_io(inode, start, end); | |
622 | redirty = 1; | |
623 | } | |
f51d2b59 DS |
624 | |
625 | /* Compression level is applied here and only here */ | |
626 | ret = btrfs_compress_pages( | |
627 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 628 | inode->i_mapping, start, |
38c31464 | 629 | pages, |
4d3a800e | 630 | &nr_pages, |
261507a0 | 631 | &total_in, |
e5d74902 | 632 | &total_compressed); |
c8b97818 CM |
633 | |
634 | if (!ret) { | |
7073017a | 635 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 636 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
637 | char *kaddr; |
638 | ||
639 | /* zero the tail end of the last page, we might be | |
640 | * sending it down to disk | |
641 | */ | |
642 | if (offset) { | |
7ac687d9 | 643 | kaddr = kmap_atomic(page); |
c8b97818 | 644 | memset(kaddr + offset, 0, |
09cbfeaf | 645 | PAGE_SIZE - offset); |
7ac687d9 | 646 | kunmap_atomic(kaddr); |
c8b97818 CM |
647 | } |
648 | will_compress = 1; | |
649 | } | |
650 | } | |
560f7d75 | 651 | cont: |
c8b97818 CM |
652 | if (start == 0) { |
653 | /* lets try to make an inline extent */ | |
6018ba0a | 654 | if (ret || total_in < actual_end) { |
c8b97818 | 655 | /* we didn't compress the entire range, try |
771ed689 | 656 | * to make an uncompressed inline extent. |
c8b97818 | 657 | */ |
a0349401 NB |
658 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
659 | 0, BTRFS_COMPRESS_NONE, | |
660 | NULL); | |
c8b97818 | 661 | } else { |
771ed689 | 662 | /* try making a compressed inline extent */ |
a0349401 | 663 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
fe3f566c LZ |
664 | total_compressed, |
665 | compress_type, pages); | |
c8b97818 | 666 | } |
79787eaa | 667 | if (ret <= 0) { |
151a41bc | 668 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
669 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
670 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
671 | unsigned long page_error_op; |
672 | ||
e6eb4314 | 673 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 674 | |
771ed689 | 675 | /* |
79787eaa JM |
676 | * inline extent creation worked or returned error, |
677 | * we don't need to create any more async work items. | |
678 | * Unlock and free up our temp pages. | |
8b62f87b JB |
679 | * |
680 | * We use DO_ACCOUNTING here because we need the | |
681 | * delalloc_release_metadata to be done _after_ we drop | |
682 | * our outstanding extent for clearing delalloc for this | |
683 | * range. | |
771ed689 | 684 | */ |
ad7ff17b NB |
685 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
686 | NULL, | |
74e9194a | 687 | clear_flags, |
ba8b04c1 | 688 | PAGE_UNLOCK | |
c2790a2e JB |
689 | PAGE_CLEAR_DIRTY | |
690 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 691 | page_error_op | |
c2790a2e | 692 | PAGE_END_WRITEBACK); |
cecc8d90 | 693 | |
1e6e238c QW |
694 | /* |
695 | * Ensure we only free the compressed pages if we have | |
696 | * them allocated, as we can still reach here with | |
697 | * inode_need_compress() == false. | |
698 | */ | |
699 | if (pages) { | |
700 | for (i = 0; i < nr_pages; i++) { | |
701 | WARN_ON(pages[i]->mapping); | |
702 | put_page(pages[i]); | |
703 | } | |
704 | kfree(pages); | |
cecc8d90 | 705 | } |
cecc8d90 | 706 | return 0; |
c8b97818 CM |
707 | } |
708 | } | |
709 | ||
710 | if (will_compress) { | |
711 | /* | |
712 | * we aren't doing an inline extent round the compressed size | |
713 | * up to a block size boundary so the allocator does sane | |
714 | * things | |
715 | */ | |
fda2832f | 716 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
717 | |
718 | /* | |
719 | * one last check to make sure the compression is really a | |
170607eb TT |
720 | * win, compare the page count read with the blocks on disk, |
721 | * compression must free at least one sector size | |
c8b97818 | 722 | */ |
09cbfeaf | 723 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 724 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 725 | compressed_extents++; |
c8bb0c8b AS |
726 | |
727 | /* | |
728 | * The async work queues will take care of doing actual | |
729 | * allocation on disk for these compressed pages, and | |
730 | * will submit them to the elevator. | |
731 | */ | |
b5326271 | 732 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 733 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
734 | compress_type); |
735 | ||
1170862d TT |
736 | if (start + total_in < end) { |
737 | start += total_in; | |
c8bb0c8b AS |
738 | pages = NULL; |
739 | cond_resched(); | |
740 | goto again; | |
741 | } | |
ac3e9933 | 742 | return compressed_extents; |
c8b97818 CM |
743 | } |
744 | } | |
c8bb0c8b | 745 | if (pages) { |
c8b97818 CM |
746 | /* |
747 | * the compression code ran but failed to make things smaller, | |
748 | * free any pages it allocated and our page pointer array | |
749 | */ | |
4d3a800e | 750 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 751 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 752 | put_page(pages[i]); |
c8b97818 CM |
753 | } |
754 | kfree(pages); | |
755 | pages = NULL; | |
756 | total_compressed = 0; | |
4d3a800e | 757 | nr_pages = 0; |
c8b97818 CM |
758 | |
759 | /* flag the file so we don't compress in the future */ | |
0b246afa | 760 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 761 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 762 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 763 | } |
c8b97818 | 764 | } |
f03d9301 | 765 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
766 | /* |
767 | * No compression, but we still need to write the pages in the file | |
768 | * we've been given so far. redirty the locked page if it corresponds | |
769 | * to our extent and set things up for the async work queue to run | |
770 | * cow_file_range to do the normal delalloc dance. | |
771 | */ | |
1d53c9e6 CM |
772 | if (async_chunk->locked_page && |
773 | (page_offset(async_chunk->locked_page) >= start && | |
774 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 775 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 776 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 777 | } |
c8bb0c8b AS |
778 | |
779 | if (redirty) | |
780 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 781 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 782 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 783 | compressed_extents++; |
3b951516 | 784 | |
ac3e9933 | 785 | return compressed_extents; |
771ed689 | 786 | } |
771ed689 | 787 | |
40ae837b FM |
788 | static void free_async_extent_pages(struct async_extent *async_extent) |
789 | { | |
790 | int i; | |
791 | ||
792 | if (!async_extent->pages) | |
793 | return; | |
794 | ||
795 | for (i = 0; i < async_extent->nr_pages; i++) { | |
796 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 797 | put_page(async_extent->pages[i]); |
40ae837b FM |
798 | } |
799 | kfree(async_extent->pages); | |
800 | async_extent->nr_pages = 0; | |
801 | async_extent->pages = NULL; | |
771ed689 CM |
802 | } |
803 | ||
804 | /* | |
805 | * phase two of compressed writeback. This is the ordered portion | |
806 | * of the code, which only gets called in the order the work was | |
807 | * queued. We walk all the async extents created by compress_file_range | |
808 | * and send them down to the disk. | |
809 | */ | |
b5326271 | 810 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
771ed689 | 811 | { |
a0ff10dc NB |
812 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
813 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
771ed689 CM |
814 | struct async_extent *async_extent; |
815 | u64 alloc_hint = 0; | |
771ed689 CM |
816 | struct btrfs_key ins; |
817 | struct extent_map *em; | |
a0ff10dc NB |
818 | struct btrfs_root *root = inode->root; |
819 | struct extent_io_tree *io_tree = &inode->io_tree; | |
f5a84ee3 | 820 | int ret = 0; |
771ed689 | 821 | |
3e04e7f1 | 822 | again: |
b5326271 NB |
823 | while (!list_empty(&async_chunk->extents)) { |
824 | async_extent = list_entry(async_chunk->extents.next, | |
771ed689 CM |
825 | struct async_extent, list); |
826 | list_del(&async_extent->list); | |
c8b97818 | 827 | |
f5a84ee3 | 828 | retry: |
7447555f NB |
829 | lock_extent(io_tree, async_extent->start, |
830 | async_extent->start + async_extent->ram_size - 1); | |
771ed689 CM |
831 | /* did the compression code fall back to uncompressed IO? */ |
832 | if (!async_extent->pages) { | |
833 | int page_started = 0; | |
834 | unsigned long nr_written = 0; | |
835 | ||
771ed689 | 836 | /* allocate blocks */ |
a0ff10dc | 837 | ret = cow_file_range(inode, async_chunk->locked_page, |
f5a84ee3 JB |
838 | async_extent->start, |
839 | async_extent->start + | |
840 | async_extent->ram_size - 1, | |
330a5827 | 841 | &page_started, &nr_written, 0); |
771ed689 | 842 | |
79787eaa JM |
843 | /* JDM XXX */ |
844 | ||
771ed689 CM |
845 | /* |
846 | * if page_started, cow_file_range inserted an | |
847 | * inline extent and took care of all the unlocking | |
848 | * and IO for us. Otherwise, we need to submit | |
849 | * all those pages down to the drive. | |
850 | */ | |
f5a84ee3 | 851 | if (!page_started && !ret) |
a0ff10dc | 852 | extent_write_locked_range(&inode->vfs_inode, |
5e3ee236 | 853 | async_extent->start, |
d397712b | 854 | async_extent->start + |
771ed689 | 855 | async_extent->ram_size - 1, |
771ed689 | 856 | WB_SYNC_ALL); |
1d53c9e6 | 857 | else if (ret && async_chunk->locked_page) |
b5326271 | 858 | unlock_page(async_chunk->locked_page); |
771ed689 CM |
859 | kfree(async_extent); |
860 | cond_resched(); | |
861 | continue; | |
862 | } | |
863 | ||
18513091 | 864 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
865 | async_extent->compressed_size, |
866 | async_extent->compressed_size, | |
e570fd27 | 867 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 868 | if (ret) { |
40ae837b | 869 | free_async_extent_pages(async_extent); |
3e04e7f1 | 870 | |
fdf8e2ea JB |
871 | if (ret == -ENOSPC) { |
872 | unlock_extent(io_tree, async_extent->start, | |
873 | async_extent->start + | |
874 | async_extent->ram_size - 1); | |
ce62003f LB |
875 | |
876 | /* | |
877 | * we need to redirty the pages if we decide to | |
878 | * fallback to uncompressed IO, otherwise we | |
879 | * will not submit these pages down to lower | |
880 | * layers. | |
881 | */ | |
a0ff10dc | 882 | extent_range_redirty_for_io(&inode->vfs_inode, |
ce62003f LB |
883 | async_extent->start, |
884 | async_extent->start + | |
885 | async_extent->ram_size - 1); | |
886 | ||
79787eaa | 887 | goto retry; |
fdf8e2ea | 888 | } |
3e04e7f1 | 889 | goto out_free; |
f5a84ee3 | 890 | } |
c2167754 YZ |
891 | /* |
892 | * here we're doing allocation and writeback of the | |
893 | * compressed pages | |
894 | */ | |
a0ff10dc | 895 | em = create_io_em(inode, async_extent->start, |
6f9994db LB |
896 | async_extent->ram_size, /* len */ |
897 | async_extent->start, /* orig_start */ | |
898 | ins.objectid, /* block_start */ | |
899 | ins.offset, /* block_len */ | |
900 | ins.offset, /* orig_block_len */ | |
901 | async_extent->ram_size, /* ram_bytes */ | |
902 | async_extent->compress_type, | |
903 | BTRFS_ORDERED_COMPRESSED); | |
904 | if (IS_ERR(em)) | |
905 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 906 | goto out_free_reserve; |
6f9994db | 907 | free_extent_map(em); |
3e04e7f1 | 908 | |
a0ff10dc | 909 | ret = btrfs_add_ordered_extent_compress(inode, |
261507a0 LZ |
910 | async_extent->start, |
911 | ins.objectid, | |
912 | async_extent->ram_size, | |
913 | ins.offset, | |
914 | BTRFS_ORDERED_COMPRESSED, | |
915 | async_extent->compress_type); | |
d9f85963 | 916 | if (ret) { |
a0ff10dc | 917 | btrfs_drop_extent_cache(inode, async_extent->start, |
d9f85963 FM |
918 | async_extent->start + |
919 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 920 | goto out_free_reserve; |
d9f85963 | 921 | } |
0b246afa | 922 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 923 | |
771ed689 CM |
924 | /* |
925 | * clear dirty, set writeback and unlock the pages. | |
926 | */ | |
a0ff10dc | 927 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
928 | async_extent->start + |
929 | async_extent->ram_size - 1, | |
151a41bc JB |
930 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
931 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 932 | PAGE_SET_WRITEBACK); |
a0ff10dc | 933 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
d397712b CM |
934 | async_extent->ram_size, |
935 | ins.objectid, | |
936 | ins.offset, async_extent->pages, | |
f82b7359 | 937 | async_extent->nr_pages, |
ec39f769 CM |
938 | async_chunk->write_flags, |
939 | async_chunk->blkcg_css)) { | |
fce2a4e6 FM |
940 | struct page *p = async_extent->pages[0]; |
941 | const u64 start = async_extent->start; | |
942 | const u64 end = start + async_extent->ram_size - 1; | |
943 | ||
a0ff10dc | 944 | p->mapping = inode->vfs_inode.i_mapping; |
c629732d | 945 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
7087a9d8 | 946 | |
fce2a4e6 | 947 | p->mapping = NULL; |
a0ff10dc | 948 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
fce2a4e6 FM |
949 | PAGE_END_WRITEBACK | |
950 | PAGE_SET_ERROR); | |
40ae837b | 951 | free_async_extent_pages(async_extent); |
fce2a4e6 | 952 | } |
771ed689 CM |
953 | alloc_hint = ins.objectid + ins.offset; |
954 | kfree(async_extent); | |
955 | cond_resched(); | |
956 | } | |
dec8f175 | 957 | return; |
3e04e7f1 | 958 | out_free_reserve: |
0b246afa | 959 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 960 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 961 | out_free: |
a0ff10dc | 962 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
963 | async_extent->start + |
964 | async_extent->ram_size - 1, | |
c2790a2e | 965 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 966 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
967 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
968 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
969 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
970 | PAGE_SET_ERROR); | |
40ae837b | 971 | free_async_extent_pages(async_extent); |
79787eaa | 972 | kfree(async_extent); |
3e04e7f1 | 973 | goto again; |
771ed689 CM |
974 | } |
975 | ||
43c69849 | 976 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
977 | u64 num_bytes) |
978 | { | |
43c69849 | 979 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
980 | struct extent_map *em; |
981 | u64 alloc_hint = 0; | |
982 | ||
983 | read_lock(&em_tree->lock); | |
984 | em = search_extent_mapping(em_tree, start, num_bytes); | |
985 | if (em) { | |
986 | /* | |
987 | * if block start isn't an actual block number then find the | |
988 | * first block in this inode and use that as a hint. If that | |
989 | * block is also bogus then just don't worry about it. | |
990 | */ | |
991 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
992 | free_extent_map(em); | |
993 | em = search_extent_mapping(em_tree, 0, 0); | |
994 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
995 | alloc_hint = em->block_start; | |
996 | if (em) | |
997 | free_extent_map(em); | |
998 | } else { | |
999 | alloc_hint = em->block_start; | |
1000 | free_extent_map(em); | |
1001 | } | |
1002 | } | |
1003 | read_unlock(&em_tree->lock); | |
1004 | ||
1005 | return alloc_hint; | |
1006 | } | |
1007 | ||
771ed689 CM |
1008 | /* |
1009 | * when extent_io.c finds a delayed allocation range in the file, | |
1010 | * the call backs end up in this code. The basic idea is to | |
1011 | * allocate extents on disk for the range, and create ordered data structs | |
1012 | * in ram to track those extents. | |
1013 | * | |
1014 | * locked_page is the page that writepage had locked already. We use | |
1015 | * it to make sure we don't do extra locks or unlocks. | |
1016 | * | |
1017 | * *page_started is set to one if we unlock locked_page and do everything | |
1018 | * required to start IO on it. It may be clean and already done with | |
1019 | * IO when we return. | |
1020 | */ | |
6e26c442 | 1021 | static noinline int cow_file_range(struct btrfs_inode *inode, |
00361589 | 1022 | struct page *locked_page, |
74e9194a | 1023 | u64 start, u64 end, int *page_started, |
330a5827 | 1024 | unsigned long *nr_written, int unlock) |
771ed689 | 1025 | { |
6e26c442 NB |
1026 | struct btrfs_root *root = inode->root; |
1027 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 CM |
1028 | u64 alloc_hint = 0; |
1029 | u64 num_bytes; | |
1030 | unsigned long ram_size; | |
a315e68f | 1031 | u64 cur_alloc_size = 0; |
432cd2a1 | 1032 | u64 min_alloc_size; |
0b246afa | 1033 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
1034 | struct btrfs_key ins; |
1035 | struct extent_map *em; | |
a315e68f FM |
1036 | unsigned clear_bits; |
1037 | unsigned long page_ops; | |
1038 | bool extent_reserved = false; | |
771ed689 CM |
1039 | int ret = 0; |
1040 | ||
6e26c442 | 1041 | if (btrfs_is_free_space_inode(inode)) { |
02ecd2c2 | 1042 | WARN_ON_ONCE(1); |
29bce2f3 JB |
1043 | ret = -EINVAL; |
1044 | goto out_unlock; | |
02ecd2c2 | 1045 | } |
771ed689 | 1046 | |
fda2832f | 1047 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1048 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1049 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1050 | |
6e26c442 | 1051 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1052 | |
771ed689 CM |
1053 | if (start == 0) { |
1054 | /* lets try to make an inline extent */ | |
6e26c442 | 1055 | ret = cow_file_range_inline(inode, start, end, 0, |
d02c0e20 | 1056 | BTRFS_COMPRESS_NONE, NULL); |
771ed689 | 1057 | if (ret == 0) { |
8b62f87b JB |
1058 | /* |
1059 | * We use DO_ACCOUNTING here because we need the | |
1060 | * delalloc_release_metadata to be run _after_ we drop | |
1061 | * our outstanding extent for clearing delalloc for this | |
1062 | * range. | |
1063 | */ | |
6e26c442 | 1064 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
c2790a2e | 1065 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1066 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1067 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
1068 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1069 | PAGE_END_WRITEBACK); | |
771ed689 | 1070 | *nr_written = *nr_written + |
09cbfeaf | 1071 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1072 | *page_started = 1; |
771ed689 | 1073 | goto out; |
79787eaa | 1074 | } else if (ret < 0) { |
79787eaa | 1075 | goto out_unlock; |
771ed689 CM |
1076 | } |
1077 | } | |
1078 | ||
6e26c442 NB |
1079 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
1080 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | |
771ed689 | 1081 | |
432cd2a1 FM |
1082 | /* |
1083 | * Relocation relies on the relocated extents to have exactly the same | |
1084 | * size as the original extents. Normally writeback for relocation data | |
1085 | * extents follows a NOCOW path because relocation preallocates the | |
1086 | * extents. However, due to an operation such as scrub turning a block | |
1087 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1088 | * an extent allocated during COW has exactly the requested size and can | |
1089 | * not be split into smaller extents, otherwise relocation breaks and | |
1090 | * fails during the stage where it updates the bytenr of file extent | |
1091 | * items. | |
1092 | */ | |
1093 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1094 | min_alloc_size = num_bytes; | |
1095 | else | |
1096 | min_alloc_size = fs_info->sectorsize; | |
1097 | ||
3752d22f AJ |
1098 | while (num_bytes > 0) { |
1099 | cur_alloc_size = num_bytes; | |
18513091 | 1100 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1101 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1102 | &ins, 1, 1); |
00361589 | 1103 | if (ret < 0) |
79787eaa | 1104 | goto out_unlock; |
a315e68f FM |
1105 | cur_alloc_size = ins.offset; |
1106 | extent_reserved = true; | |
d397712b | 1107 | |
771ed689 | 1108 | ram_size = ins.offset; |
6e26c442 | 1109 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1110 | start, /* orig_start */ |
1111 | ins.objectid, /* block_start */ | |
1112 | ins.offset, /* block_len */ | |
1113 | ins.offset, /* orig_block_len */ | |
1114 | ram_size, /* ram_bytes */ | |
1115 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1116 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1117 | if (IS_ERR(em)) { |
1118 | ret = PTR_ERR(em); | |
ace68bac | 1119 | goto out_reserve; |
090a127a | 1120 | } |
6f9994db | 1121 | free_extent_map(em); |
e6dcd2dc | 1122 | |
6e26c442 NB |
1123 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
1124 | ram_size, cur_alloc_size, 0); | |
ace68bac | 1125 | if (ret) |
d9f85963 | 1126 | goto out_drop_extent_cache; |
c8b97818 | 1127 | |
17d217fe YZ |
1128 | if (root->root_key.objectid == |
1129 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
6e26c442 | 1130 | ret = btrfs_reloc_clone_csums(inode, start, |
17d217fe | 1131 | cur_alloc_size); |
4dbd80fb QW |
1132 | /* |
1133 | * Only drop cache here, and process as normal. | |
1134 | * | |
1135 | * We must not allow extent_clear_unlock_delalloc() | |
1136 | * at out_unlock label to free meta of this ordered | |
1137 | * extent, as its meta should be freed by | |
1138 | * btrfs_finish_ordered_io(). | |
1139 | * | |
1140 | * So we must continue until @start is increased to | |
1141 | * skip current ordered extent. | |
1142 | */ | |
00361589 | 1143 | if (ret) |
6e26c442 | 1144 | btrfs_drop_extent_cache(inode, start, |
4dbd80fb | 1145 | start + ram_size - 1, 0); |
17d217fe YZ |
1146 | } |
1147 | ||
0b246afa | 1148 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1149 | |
c8b97818 CM |
1150 | /* we're not doing compressed IO, don't unlock the first |
1151 | * page (which the caller expects to stay locked), don't | |
1152 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1153 | * |
1154 | * Do set the Private2 bit so we know this page was properly | |
1155 | * setup for writepage | |
c8b97818 | 1156 | */ |
a315e68f FM |
1157 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1158 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1159 | |
6e26c442 | 1160 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1161 | locked_page, |
c2790a2e | 1162 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1163 | page_ops); |
3752d22f AJ |
1164 | if (num_bytes < cur_alloc_size) |
1165 | num_bytes = 0; | |
4dbd80fb | 1166 | else |
3752d22f | 1167 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1168 | alloc_hint = ins.objectid + ins.offset; |
1169 | start += cur_alloc_size; | |
a315e68f | 1170 | extent_reserved = false; |
4dbd80fb QW |
1171 | |
1172 | /* | |
1173 | * btrfs_reloc_clone_csums() error, since start is increased | |
1174 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1175 | * free metadata of current ordered extent, we're OK to exit. | |
1176 | */ | |
1177 | if (ret) | |
1178 | goto out_unlock; | |
b888db2b | 1179 | } |
79787eaa | 1180 | out: |
be20aa9d | 1181 | return ret; |
b7d5b0a8 | 1182 | |
d9f85963 | 1183 | out_drop_extent_cache: |
6e26c442 | 1184 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
ace68bac | 1185 | out_reserve: |
0b246afa | 1186 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1187 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1188 | out_unlock: |
a7e3b975 FM |
1189 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1190 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1191 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1192 | PAGE_END_WRITEBACK; | |
1193 | /* | |
1194 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1195 | * failed to create the respective ordered extent, then it means that | |
1196 | * when we reserved the extent we decremented the extent's size from | |
1197 | * the data space_info's bytes_may_use counter and incremented the | |
1198 | * space_info's bytes_reserved counter by the same amount. We must make | |
1199 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1200 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1201 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1202 | */ | |
1203 | if (extent_reserved) { | |
6e26c442 | 1204 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1205 | start + cur_alloc_size - 1, |
a315e68f FM |
1206 | locked_page, |
1207 | clear_bits, | |
1208 | page_ops); | |
1209 | start += cur_alloc_size; | |
1210 | if (start >= end) | |
1211 | goto out; | |
1212 | } | |
6e26c442 | 1213 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1214 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1215 | page_ops); | |
79787eaa | 1216 | goto out; |
771ed689 | 1217 | } |
c8b97818 | 1218 | |
771ed689 CM |
1219 | /* |
1220 | * work queue call back to started compression on a file and pages | |
1221 | */ | |
1222 | static noinline void async_cow_start(struct btrfs_work *work) | |
1223 | { | |
b5326271 | 1224 | struct async_chunk *async_chunk; |
ac3e9933 | 1225 | int compressed_extents; |
771ed689 | 1226 | |
b5326271 | 1227 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1228 | |
ac3e9933 NB |
1229 | compressed_extents = compress_file_range(async_chunk); |
1230 | if (compressed_extents == 0) { | |
b5326271 NB |
1231 | btrfs_add_delayed_iput(async_chunk->inode); |
1232 | async_chunk->inode = NULL; | |
8180ef88 | 1233 | } |
771ed689 CM |
1234 | } |
1235 | ||
1236 | /* | |
1237 | * work queue call back to submit previously compressed pages | |
1238 | */ | |
1239 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1240 | { | |
c5a68aec NB |
1241 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1242 | work); | |
1243 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1244 | unsigned long nr_pages; |
1245 | ||
b5326271 | 1246 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1247 | PAGE_SHIFT; |
771ed689 | 1248 | |
093258e6 | 1249 | /* atomic_sub_return implies a barrier */ |
0b246afa | 1250 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
093258e6 DS |
1251 | 5 * SZ_1M) |
1252 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1253 | |
4546d178 | 1254 | /* |
b5326271 | 1255 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1256 | * in which case we don't have anything to submit, yet we need to |
1257 | * always adjust ->async_delalloc_pages as its paired with the init | |
1258 | * happening in cow_file_range_async | |
1259 | */ | |
b5326271 NB |
1260 | if (async_chunk->inode) |
1261 | submit_compressed_extents(async_chunk); | |
771ed689 | 1262 | } |
c8b97818 | 1263 | |
771ed689 CM |
1264 | static noinline void async_cow_free(struct btrfs_work *work) |
1265 | { | |
b5326271 | 1266 | struct async_chunk *async_chunk; |
97db1204 | 1267 | |
b5326271 NB |
1268 | async_chunk = container_of(work, struct async_chunk, work); |
1269 | if (async_chunk->inode) | |
1270 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1271 | if (async_chunk->blkcg_css) |
1272 | css_put(async_chunk->blkcg_css); | |
97db1204 NB |
1273 | /* |
1274 | * Since the pointer to 'pending' is at the beginning of the array of | |
b5326271 | 1275 | * async_chunk's, freeing it ensures the whole array has been freed. |
97db1204 | 1276 | */ |
b5326271 | 1277 | if (atomic_dec_and_test(async_chunk->pending)) |
b1c16ac9 | 1278 | kvfree(async_chunk->pending); |
771ed689 CM |
1279 | } |
1280 | ||
751b6431 | 1281 | static int cow_file_range_async(struct btrfs_inode *inode, |
ec39f769 CM |
1282 | struct writeback_control *wbc, |
1283 | struct page *locked_page, | |
771ed689 | 1284 | u64 start, u64 end, int *page_started, |
fac07d2b | 1285 | unsigned long *nr_written) |
771ed689 | 1286 | { |
751b6431 | 1287 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1288 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1289 | struct async_cow *ctx; |
1290 | struct async_chunk *async_chunk; | |
771ed689 CM |
1291 | unsigned long nr_pages; |
1292 | u64 cur_end; | |
97db1204 NB |
1293 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1294 | int i; | |
1295 | bool should_compress; | |
b1c16ac9 | 1296 | unsigned nofs_flag; |
fac07d2b | 1297 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1298 | |
751b6431 | 1299 | unlock_extent(&inode->io_tree, start, end); |
97db1204 | 1300 | |
751b6431 | 1301 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
97db1204 NB |
1302 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
1303 | num_chunks = 1; | |
1304 | should_compress = false; | |
1305 | } else { | |
1306 | should_compress = true; | |
1307 | } | |
1308 | ||
b1c16ac9 NB |
1309 | nofs_flag = memalloc_nofs_save(); |
1310 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1311 | memalloc_nofs_restore(nofs_flag); | |
1312 | ||
97db1204 NB |
1313 | if (!ctx) { |
1314 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1315 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1316 | EXTENT_DO_ACCOUNTING; | |
1317 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1318 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | | |
1319 | PAGE_SET_ERROR; | |
1320 | ||
751b6431 NB |
1321 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1322 | clear_bits, page_ops); | |
97db1204 NB |
1323 | return -ENOMEM; |
1324 | } | |
1325 | ||
1326 | async_chunk = ctx->chunks; | |
1327 | atomic_set(&ctx->num_chunks, num_chunks); | |
1328 | ||
1329 | for (i = 0; i < num_chunks; i++) { | |
1330 | if (should_compress) | |
1331 | cur_end = min(end, start + SZ_512K - 1); | |
1332 | else | |
1333 | cur_end = end; | |
771ed689 | 1334 | |
bd4691a0 NB |
1335 | /* |
1336 | * igrab is called higher up in the call chain, take only the | |
1337 | * lightweight reference for the callback lifetime | |
1338 | */ | |
751b6431 | 1339 | ihold(&inode->vfs_inode); |
97db1204 | 1340 | async_chunk[i].pending = &ctx->num_chunks; |
751b6431 | 1341 | async_chunk[i].inode = &inode->vfs_inode; |
97db1204 NB |
1342 | async_chunk[i].start = start; |
1343 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1344 | async_chunk[i].write_flags = write_flags; |
1345 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1346 | ||
1d53c9e6 CM |
1347 | /* |
1348 | * The locked_page comes all the way from writepage and its | |
1349 | * the original page we were actually given. As we spread | |
1350 | * this large delalloc region across multiple async_chunk | |
1351 | * structs, only the first struct needs a pointer to locked_page | |
1352 | * | |
1353 | * This way we don't need racey decisions about who is supposed | |
1354 | * to unlock it. | |
1355 | */ | |
1356 | if (locked_page) { | |
ec39f769 CM |
1357 | /* |
1358 | * Depending on the compressibility, the pages might or | |
1359 | * might not go through async. We want all of them to | |
1360 | * be accounted against wbc once. Let's do it here | |
1361 | * before the paths diverge. wbc accounting is used | |
1362 | * only for foreign writeback detection and doesn't | |
1363 | * need full accuracy. Just account the whole thing | |
1364 | * against the first page. | |
1365 | */ | |
1366 | wbc_account_cgroup_owner(wbc, locked_page, | |
1367 | cur_end - start); | |
1d53c9e6 CM |
1368 | async_chunk[i].locked_page = locked_page; |
1369 | locked_page = NULL; | |
1370 | } else { | |
1371 | async_chunk[i].locked_page = NULL; | |
1372 | } | |
1373 | ||
ec39f769 CM |
1374 | if (blkcg_css != blkcg_root_css) { |
1375 | css_get(blkcg_css); | |
1376 | async_chunk[i].blkcg_css = blkcg_css; | |
1377 | } else { | |
1378 | async_chunk[i].blkcg_css = NULL; | |
1379 | } | |
1380 | ||
a0cac0ec OS |
1381 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1382 | async_cow_submit, async_cow_free); | |
771ed689 | 1383 | |
97db1204 | 1384 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1385 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1386 | |
97db1204 | 1387 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1388 | |
771ed689 CM |
1389 | *nr_written += nr_pages; |
1390 | start = cur_end + 1; | |
1391 | } | |
1392 | *page_started = 1; | |
1393 | return 0; | |
be20aa9d CM |
1394 | } |
1395 | ||
2ff7e61e | 1396 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1397 | u64 bytenr, u64 num_bytes) |
1398 | { | |
1399 | int ret; | |
1400 | struct btrfs_ordered_sum *sums; | |
1401 | LIST_HEAD(list); | |
1402 | ||
0b246afa | 1403 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1404 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1405 | if (ret == 0 && list_empty(&list)) |
1406 | return 0; | |
1407 | ||
1408 | while (!list_empty(&list)) { | |
1409 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1410 | list_del(&sums->list); | |
1411 | kfree(sums); | |
1412 | } | |
58113753 LB |
1413 | if (ret < 0) |
1414 | return ret; | |
17d217fe YZ |
1415 | return 1; |
1416 | } | |
1417 | ||
8ba96f3d | 1418 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
467dc47e FM |
1419 | const u64 start, const u64 end, |
1420 | int *page_started, unsigned long *nr_written) | |
1421 | { | |
8ba96f3d NB |
1422 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
1423 | const bool is_reloc_ino = (inode->root->root_key.objectid == | |
6bd335b4 | 1424 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
2166e5ed | 1425 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1426 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1427 | u64 range_start = start; |
1428 | u64 count; | |
1429 | ||
1430 | /* | |
1431 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1432 | * made we had not enough available data space and therefore we did not | |
1433 | * reserve data space for it, since we though we could do NOCOW for the | |
1434 | * respective file range (either there is prealloc extent or the inode | |
1435 | * has the NOCOW bit set). | |
1436 | * | |
1437 | * However when we need to fallback to COW mode (because for example the | |
1438 | * block group for the corresponding extent was turned to RO mode by a | |
1439 | * scrub or relocation) we need to do the following: | |
1440 | * | |
1441 | * 1) We increment the bytes_may_use counter of the data space info. | |
1442 | * If COW succeeds, it allocates a new data extent and after doing | |
1443 | * that it decrements the space info's bytes_may_use counter and | |
1444 | * increments its bytes_reserved counter by the same amount (we do | |
1445 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1446 | * bytes_may_use counter to compensate (when space is reserved at | |
1447 | * buffered write time, the bytes_may_use counter is incremented); | |
1448 | * | |
1449 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1450 | * that if the COW path fails for any reason, it decrements (through | |
1451 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1452 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1453 | * |
1454 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1455 | * space cache inode or an inode of the data relocation tree, we must |
1456 | * also increment bytes_may_use of the data space_info for the same | |
1457 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1458 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1459 | * group that contains that extent to RO mode and therefore force COW |
1460 | * when starting writeback. | |
467dc47e | 1461 | */ |
2166e5ed | 1462 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
467dc47e | 1463 | EXTENT_NORESERVE, 0); |
6bd335b4 FM |
1464 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1465 | u64 bytes = count; | |
8ba96f3d | 1466 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1467 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1468 | ||
6bd335b4 FM |
1469 | if (is_space_ino || is_reloc_ino) |
1470 | bytes = range_bytes; | |
1471 | ||
467dc47e | 1472 | spin_lock(&sinfo->lock); |
2166e5ed | 1473 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1474 | spin_unlock(&sinfo->lock); |
1475 | ||
2166e5ed FM |
1476 | if (count > 0) |
1477 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
1478 | 0, 0, NULL); | |
467dc47e FM |
1479 | } |
1480 | ||
8ba96f3d NB |
1481 | return cow_file_range(inode, locked_page, start, end, page_started, |
1482 | nr_written, 1); | |
467dc47e FM |
1483 | } |
1484 | ||
d352ac68 CM |
1485 | /* |
1486 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1487 | * of the extents that exist in the file, and COWs the file as required. | |
1488 | * | |
1489 | * If no cow copies or snapshots exist, we write directly to the existing | |
1490 | * blocks on disk | |
1491 | */ | |
968322c8 | 1492 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1493 | struct page *locked_page, |
3e024846 NB |
1494 | const u64 start, const u64 end, |
1495 | int *page_started, int force, | |
1496 | unsigned long *nr_written) | |
be20aa9d | 1497 | { |
968322c8 NB |
1498 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1499 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1500 | struct btrfs_path *path; |
3e024846 NB |
1501 | u64 cow_start = (u64)-1; |
1502 | u64 cur_offset = start; | |
8ecebf4d | 1503 | int ret; |
3e024846 | 1504 | bool check_prev = true; |
968322c8 NB |
1505 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
1506 | u64 ino = btrfs_ino(inode); | |
762bf098 NB |
1507 | bool nocow = false; |
1508 | u64 disk_bytenr = 0; | |
be20aa9d CM |
1509 | |
1510 | path = btrfs_alloc_path(); | |
17ca04af | 1511 | if (!path) { |
968322c8 | 1512 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
c2790a2e | 1513 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1514 | EXTENT_DO_ACCOUNTING | |
1515 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1516 | PAGE_CLEAR_DIRTY | |
1517 | PAGE_SET_WRITEBACK | | |
1518 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1519 | return -ENOMEM; |
17ca04af | 1520 | } |
82d5902d | 1521 | |
80ff3856 | 1522 | while (1) { |
3e024846 NB |
1523 | struct btrfs_key found_key; |
1524 | struct btrfs_file_extent_item *fi; | |
1525 | struct extent_buffer *leaf; | |
1526 | u64 extent_end; | |
1527 | u64 extent_offset; | |
3e024846 NB |
1528 | u64 num_bytes = 0; |
1529 | u64 disk_num_bytes; | |
3e024846 NB |
1530 | u64 ram_bytes; |
1531 | int extent_type; | |
762bf098 NB |
1532 | |
1533 | nocow = false; | |
3e024846 | 1534 | |
e4c3b2dc | 1535 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1536 | cur_offset, 0); |
d788a349 | 1537 | if (ret < 0) |
79787eaa | 1538 | goto error; |
a6bd9cd1 NB |
1539 | |
1540 | /* | |
1541 | * If there is no extent for our range when doing the initial | |
1542 | * search, then go back to the previous slot as it will be the | |
1543 | * one containing the search offset | |
1544 | */ | |
80ff3856 YZ |
1545 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1546 | leaf = path->nodes[0]; | |
1547 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1548 | path->slots[0] - 1); | |
33345d01 | 1549 | if (found_key.objectid == ino && |
80ff3856 YZ |
1550 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1551 | path->slots[0]--; | |
1552 | } | |
3e024846 | 1553 | check_prev = false; |
80ff3856 | 1554 | next_slot: |
a6bd9cd1 | 1555 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1556 | leaf = path->nodes[0]; |
1557 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1558 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1559 | if (ret < 0) { |
1560 | if (cow_start != (u64)-1) | |
1561 | cur_offset = cow_start; | |
79787eaa | 1562 | goto error; |
e8916699 | 1563 | } |
80ff3856 YZ |
1564 | if (ret > 0) |
1565 | break; | |
1566 | leaf = path->nodes[0]; | |
1567 | } | |
be20aa9d | 1568 | |
80ff3856 YZ |
1569 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1570 | ||
a6bd9cd1 | 1571 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
1572 | if (found_key.objectid > ino) |
1573 | break; | |
a6bd9cd1 NB |
1574 | /* |
1575 | * Keep searching until we find an EXTENT_ITEM or there are no | |
1576 | * more extents for this inode | |
1577 | */ | |
1d512cb7 FM |
1578 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
1579 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1580 | path->slots[0]++; | |
1581 | goto next_slot; | |
1582 | } | |
a6bd9cd1 NB |
1583 | |
1584 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 1585 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
1586 | found_key.offset > end) |
1587 | break; | |
1588 | ||
a6bd9cd1 NB |
1589 | /* |
1590 | * If the found extent starts after requested offset, then | |
1591 | * adjust extent_end to be right before this extent begins | |
1592 | */ | |
80ff3856 YZ |
1593 | if (found_key.offset > cur_offset) { |
1594 | extent_end = found_key.offset; | |
e9061e21 | 1595 | extent_type = 0; |
80ff3856 YZ |
1596 | goto out_check; |
1597 | } | |
1598 | ||
a6bd9cd1 NB |
1599 | /* |
1600 | * Found extent which begins before our range and potentially | |
1601 | * intersect it | |
1602 | */ | |
80ff3856 YZ |
1603 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1604 | struct btrfs_file_extent_item); | |
1605 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1606 | ||
cc95bef6 | 1607 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1608 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1609 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1610 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1611 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1612 | extent_end = found_key.offset + |
1613 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1614 | disk_num_bytes = |
1615 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
a6bd9cd1 | 1616 | /* |
de7999af FM |
1617 | * If the extent we got ends before our current offset, |
1618 | * skip to the next extent. | |
a6bd9cd1 | 1619 | */ |
de7999af | 1620 | if (extent_end <= cur_offset) { |
80ff3856 YZ |
1621 | path->slots[0]++; |
1622 | goto next_slot; | |
1623 | } | |
a6bd9cd1 | 1624 | /* Skip holes */ |
17d217fe YZ |
1625 | if (disk_bytenr == 0) |
1626 | goto out_check; | |
a6bd9cd1 | 1627 | /* Skip compressed/encrypted/encoded extents */ |
80ff3856 YZ |
1628 | if (btrfs_file_extent_compression(leaf, fi) || |
1629 | btrfs_file_extent_encryption(leaf, fi) || | |
1630 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1631 | goto out_check; | |
78d4295b | 1632 | /* |
a6bd9cd1 NB |
1633 | * If extent is created before the last volume's snapshot |
1634 | * this implies the extent is shared, hence we can't do | |
1635 | * nocow. This is the same check as in | |
1636 | * btrfs_cross_ref_exist but without calling | |
1637 | * btrfs_search_slot. | |
78d4295b | 1638 | */ |
3e024846 | 1639 | if (!freespace_inode && |
27a7ff55 | 1640 | btrfs_file_extent_generation(leaf, fi) <= |
78d4295b EL |
1641 | btrfs_root_last_snapshot(&root->root_item)) |
1642 | goto out_check; | |
d899e052 YZ |
1643 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1644 | goto out_check; | |
a6bd9cd1 | 1645 | /* If extent is RO, we must COW it */ |
2ff7e61e | 1646 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1647 | goto out_check; |
58113753 LB |
1648 | ret = btrfs_cross_ref_exist(root, ino, |
1649 | found_key.offset - | |
a84d5d42 | 1650 | extent_offset, disk_bytenr, false); |
58113753 LB |
1651 | if (ret) { |
1652 | /* | |
1653 | * ret could be -EIO if the above fails to read | |
1654 | * metadata. | |
1655 | */ | |
1656 | if (ret < 0) { | |
1657 | if (cow_start != (u64)-1) | |
1658 | cur_offset = cow_start; | |
1659 | goto error; | |
1660 | } | |
1661 | ||
3e024846 | 1662 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1663 | goto out_check; |
58113753 | 1664 | } |
5d4f98a2 | 1665 | disk_bytenr += extent_offset; |
17d217fe YZ |
1666 | disk_bytenr += cur_offset - found_key.offset; |
1667 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 | 1668 | /* |
a6bd9cd1 NB |
1669 | * If there are pending snapshots for this root, we |
1670 | * fall into common COW way | |
e9894fd3 | 1671 | */ |
3e024846 | 1672 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
8ecebf4d | 1673 | goto out_check; |
17d217fe YZ |
1674 | /* |
1675 | * force cow if csum exists in the range. | |
1676 | * this ensure that csum for a given extent are | |
1677 | * either valid or do not exist. | |
1678 | */ | |
58113753 LB |
1679 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1680 | num_bytes); | |
1681 | if (ret) { | |
58113753 LB |
1682 | /* |
1683 | * ret could be -EIO if the above fails to read | |
1684 | * metadata. | |
1685 | */ | |
1686 | if (ret < 0) { | |
1687 | if (cow_start != (u64)-1) | |
1688 | cur_offset = cow_start; | |
1689 | goto error; | |
1690 | } | |
3e024846 | 1691 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1692 | goto out_check; |
91e1f56a | 1693 | } |
8ecebf4d | 1694 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1695 | goto out_check; |
3e024846 | 1696 | nocow = true; |
80ff3856 | 1697 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e8e21007 NB |
1698 | extent_end = found_key.offset + ram_bytes; |
1699 | extent_end = ALIGN(extent_end, fs_info->sectorsize); | |
922f0518 NB |
1700 | /* Skip extents outside of our requested range */ |
1701 | if (extent_end <= start) { | |
1702 | path->slots[0]++; | |
1703 | goto next_slot; | |
1704 | } | |
80ff3856 | 1705 | } else { |
e8e21007 | 1706 | /* If this triggers then we have a memory corruption */ |
290342f6 | 1707 | BUG(); |
80ff3856 YZ |
1708 | } |
1709 | out_check: | |
a6bd9cd1 NB |
1710 | /* |
1711 | * If nocow is false then record the beginning of the range | |
1712 | * that needs to be COWed | |
1713 | */ | |
80ff3856 YZ |
1714 | if (!nocow) { |
1715 | if (cow_start == (u64)-1) | |
1716 | cow_start = cur_offset; | |
1717 | cur_offset = extent_end; | |
1718 | if (cur_offset > end) | |
1719 | break; | |
1720 | path->slots[0]++; | |
1721 | goto next_slot; | |
7ea394f1 YZ |
1722 | } |
1723 | ||
b3b4aa74 | 1724 | btrfs_release_path(path); |
a6bd9cd1 NB |
1725 | |
1726 | /* | |
1727 | * COW range from cow_start to found_key.offset - 1. As the key | |
1728 | * will contain the beginning of the first extent that can be | |
1729 | * NOCOW, following one which needs to be COW'ed | |
1730 | */ | |
80ff3856 | 1731 | if (cow_start != (u64)-1) { |
968322c8 | 1732 | ret = fallback_to_cow(inode, locked_page, |
8ba96f3d | 1733 | cow_start, found_key.offset - 1, |
467dc47e | 1734 | page_started, nr_written); |
230ed397 | 1735 | if (ret) |
79787eaa | 1736 | goto error; |
80ff3856 | 1737 | cow_start = (u64)-1; |
7ea394f1 | 1738 | } |
80ff3856 | 1739 | |
d899e052 | 1740 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db | 1741 | u64 orig_start = found_key.offset - extent_offset; |
3e024846 | 1742 | struct extent_map *em; |
6f9994db | 1743 | |
968322c8 | 1744 | em = create_io_em(inode, cur_offset, num_bytes, |
6f9994db LB |
1745 | orig_start, |
1746 | disk_bytenr, /* block_start */ | |
1747 | num_bytes, /* block_len */ | |
1748 | disk_num_bytes, /* orig_block_len */ | |
1749 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1750 | BTRFS_ORDERED_PREALLOC); | |
1751 | if (IS_ERR(em)) { | |
6f9994db LB |
1752 | ret = PTR_ERR(em); |
1753 | goto error; | |
d899e052 | 1754 | } |
6f9994db | 1755 | free_extent_map(em); |
968322c8 | 1756 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1757 | disk_bytenr, num_bytes, |
1758 | num_bytes, | |
1759 | BTRFS_ORDERED_PREALLOC); | |
762bf098 | 1760 | if (ret) { |
968322c8 | 1761 | btrfs_drop_extent_cache(inode, cur_offset, |
762bf098 NB |
1762 | cur_offset + num_bytes - 1, |
1763 | 0); | |
1764 | goto error; | |
1765 | } | |
d899e052 | 1766 | } else { |
968322c8 | 1767 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1768 | disk_bytenr, num_bytes, |
1769 | num_bytes, | |
1770 | BTRFS_ORDERED_NOCOW); | |
762bf098 NB |
1771 | if (ret) |
1772 | goto error; | |
d899e052 | 1773 | } |
80ff3856 | 1774 | |
f78c436c | 1775 | if (nocow) |
0b246afa | 1776 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
762bf098 | 1777 | nocow = false; |
771ed689 | 1778 | |
efa56464 | 1779 | if (root->root_key.objectid == |
4dbd80fb QW |
1780 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1781 | /* | |
1782 | * Error handled later, as we must prevent | |
1783 | * extent_clear_unlock_delalloc() in error handler | |
1784 | * from freeing metadata of created ordered extent. | |
1785 | */ | |
968322c8 | 1786 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
efa56464 | 1787 | num_bytes); |
efa56464 | 1788 | |
968322c8 | 1789 | extent_clear_unlock_delalloc(inode, cur_offset, |
74e9194a | 1790 | cur_offset + num_bytes - 1, |
c2790a2e | 1791 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1792 | EXTENT_DELALLOC | |
1793 | EXTENT_CLEAR_DATA_RESV, | |
1794 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1795 | ||
80ff3856 | 1796 | cur_offset = extent_end; |
4dbd80fb QW |
1797 | |
1798 | /* | |
1799 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1800 | * handler, as metadata for created ordered extent will only | |
1801 | * be freed by btrfs_finish_ordered_io(). | |
1802 | */ | |
1803 | if (ret) | |
1804 | goto error; | |
80ff3856 YZ |
1805 | if (cur_offset > end) |
1806 | break; | |
be20aa9d | 1807 | } |
b3b4aa74 | 1808 | btrfs_release_path(path); |
80ff3856 | 1809 | |
506481b2 | 1810 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1811 | cow_start = cur_offset; |
17ca04af | 1812 | |
80ff3856 | 1813 | if (cow_start != (u64)-1) { |
506481b2 | 1814 | cur_offset = end; |
968322c8 NB |
1815 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
1816 | page_started, nr_written); | |
d788a349 | 1817 | if (ret) |
79787eaa | 1818 | goto error; |
80ff3856 YZ |
1819 | } |
1820 | ||
79787eaa | 1821 | error: |
762bf098 NB |
1822 | if (nocow) |
1823 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1824 | ||
17ca04af | 1825 | if (ret && cur_offset < end) |
968322c8 | 1826 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 1827 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1828 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1829 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1830 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1831 | PAGE_SET_WRITEBACK | |
1832 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1833 | btrfs_free_path(path); |
79787eaa | 1834 | return ret; |
be20aa9d CM |
1835 | } |
1836 | ||
0c494225 | 1837 | static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 WS |
1838 | { |
1839 | ||
0c494225 NB |
1840 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
1841 | !(inode->flags & BTRFS_INODE_PREALLOC)) | |
47059d93 WS |
1842 | return 0; |
1843 | ||
1844 | /* | |
1845 | * @defrag_bytes is a hint value, no spinlock held here, | |
1846 | * if is not zero, it means the file is defragging. | |
1847 | * Force cow if given extent needs to be defragged. | |
1848 | */ | |
0c494225 NB |
1849 | if (inode->defrag_bytes && |
1850 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) | |
47059d93 WS |
1851 | return 1; |
1852 | ||
1853 | return 0; | |
1854 | } | |
1855 | ||
d352ac68 | 1856 | /* |
5eaad97a NB |
1857 | * Function to process delayed allocation (create CoW) for ranges which are |
1858 | * being touched for the first time. | |
d352ac68 | 1859 | */ |
98456b9c | 1860 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
5eaad97a NB |
1861 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1862 | struct writeback_control *wbc) | |
be20aa9d | 1863 | { |
be20aa9d | 1864 | int ret; |
98456b9c | 1865 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1866 | |
98456b9c NB |
1867 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
1868 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
d397712b | 1869 | page_started, 1, nr_written); |
98456b9c NB |
1870 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
1871 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
d397712b | 1872 | page_started, 0, nr_written); |
98456b9c NB |
1873 | } else if (!inode_can_compress(inode) || |
1874 | !inode_need_compress(inode, start, end)) { | |
1875 | ret = cow_file_range(inode, locked_page, start, end, | |
1876 | page_started, nr_written, 1); | |
7ddf5a42 | 1877 | } else { |
98456b9c NB |
1878 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
1879 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, | |
fac07d2b | 1880 | page_started, nr_written); |
7ddf5a42 | 1881 | } |
52427260 | 1882 | if (ret) |
98456b9c | 1883 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 1884 | end - start + 1); |
b888db2b CM |
1885 | return ret; |
1886 | } | |
1887 | ||
abbb55f4 NB |
1888 | void btrfs_split_delalloc_extent(struct inode *inode, |
1889 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1890 | { |
dcab6a3b JB |
1891 | u64 size; |
1892 | ||
0ca1f7ce | 1893 | /* not delalloc, ignore it */ |
9ed74f2d | 1894 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1895 | return; |
9ed74f2d | 1896 | |
dcab6a3b JB |
1897 | size = orig->end - orig->start + 1; |
1898 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1899 | u32 num_extents; |
dcab6a3b JB |
1900 | u64 new_size; |
1901 | ||
1902 | /* | |
5c848198 | 1903 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 1904 | * applies here, just in reverse. |
dcab6a3b JB |
1905 | */ |
1906 | new_size = orig->end - split + 1; | |
823bb20a | 1907 | num_extents = count_max_extents(new_size); |
ba117213 | 1908 | new_size = split - orig->start; |
823bb20a DS |
1909 | num_extents += count_max_extents(new_size); |
1910 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1911 | return; |
1912 | } | |
1913 | ||
9e0baf60 | 1914 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1915 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1916 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1917 | } |
1918 | ||
1919 | /* | |
5c848198 NB |
1920 | * Handle merged delayed allocation extents so we can keep track of new extents |
1921 | * that are just merged onto old extents, such as when we are doing sequential | |
1922 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 1923 | */ |
5c848198 NB |
1924 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
1925 | struct extent_state *other) | |
9ed74f2d | 1926 | { |
dcab6a3b | 1927 | u64 new_size, old_size; |
823bb20a | 1928 | u32 num_extents; |
dcab6a3b | 1929 | |
9ed74f2d JB |
1930 | /* not delalloc, ignore it */ |
1931 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1932 | return; |
9ed74f2d | 1933 | |
8461a3de JB |
1934 | if (new->start > other->start) |
1935 | new_size = new->end - other->start + 1; | |
1936 | else | |
1937 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1938 | |
1939 | /* we're not bigger than the max, unreserve the space and go */ | |
1940 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1941 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1942 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1943 | spin_unlock(&BTRFS_I(inode)->lock); |
1944 | return; | |
1945 | } | |
1946 | ||
1947 | /* | |
ba117213 JB |
1948 | * We have to add up either side to figure out how many extents were |
1949 | * accounted for before we merged into one big extent. If the number of | |
1950 | * extents we accounted for is <= the amount we need for the new range | |
1951 | * then we can return, otherwise drop. Think of it like this | |
1952 | * | |
1953 | * [ 4k][MAX_SIZE] | |
1954 | * | |
1955 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1956 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1957 | * we have 1 so they are == and we can return. But in this case | |
1958 | * | |
1959 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1960 | * | |
1961 | * Each range on their own accounts for 2 extents, but merged together | |
1962 | * they are only 3 extents worth of accounting, so we need to drop in | |
1963 | * this case. | |
dcab6a3b | 1964 | */ |
ba117213 | 1965 | old_size = other->end - other->start + 1; |
823bb20a | 1966 | num_extents = count_max_extents(old_size); |
ba117213 | 1967 | old_size = new->end - new->start + 1; |
823bb20a DS |
1968 | num_extents += count_max_extents(old_size); |
1969 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1970 | return; |
1971 | ||
9e0baf60 | 1972 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1973 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1974 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1975 | } |
1976 | ||
eb73c1b7 MX |
1977 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1978 | struct inode *inode) | |
1979 | { | |
0b246afa JM |
1980 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1981 | ||
eb73c1b7 MX |
1982 | spin_lock(&root->delalloc_lock); |
1983 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1984 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1985 | &root->delalloc_inodes); | |
1986 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1987 | &BTRFS_I(inode)->runtime_flags); | |
1988 | root->nr_delalloc_inodes++; | |
1989 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1990 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1991 | BUG_ON(!list_empty(&root->delalloc_root)); |
1992 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1993 | &fs_info->delalloc_roots); |
1994 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1995 | } |
1996 | } | |
1997 | spin_unlock(&root->delalloc_lock); | |
1998 | } | |
1999 | ||
2b877331 NB |
2000 | |
2001 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2002 | struct btrfs_inode *inode) | |
eb73c1b7 | 2003 | { |
3ffbd68c | 2004 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 2005 | |
9e3e97f4 NB |
2006 | if (!list_empty(&inode->delalloc_inodes)) { |
2007 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 2008 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2009 | &inode->runtime_flags); |
eb73c1b7 MX |
2010 | root->nr_delalloc_inodes--; |
2011 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 2012 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 2013 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2014 | BUG_ON(list_empty(&root->delalloc_root)); |
2015 | list_del_init(&root->delalloc_root); | |
0b246afa | 2016 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
2017 | } |
2018 | } | |
2b877331 NB |
2019 | } |
2020 | ||
2021 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
2022 | struct btrfs_inode *inode) | |
2023 | { | |
2024 | spin_lock(&root->delalloc_lock); | |
2025 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
2026 | spin_unlock(&root->delalloc_lock); |
2027 | } | |
2028 | ||
d352ac68 | 2029 | /* |
e06a1fc9 NB |
2030 | * Properly track delayed allocation bytes in the inode and to maintain the |
2031 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 2032 | */ |
e06a1fc9 NB |
2033 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
2034 | unsigned *bits) | |
291d673e | 2035 | { |
0b246afa JM |
2036 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2037 | ||
47059d93 WS |
2038 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
2039 | WARN_ON(1); | |
75eff68e CM |
2040 | /* |
2041 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2042 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2043 | * bit, which is only set or cleared with irqs on |
2044 | */ | |
0ca1f7ce | 2045 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 2046 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2047 | u64 len = state->end + 1 - state->start; |
8b62f87b | 2048 | u32 num_extents = count_max_extents(len); |
70ddc553 | 2049 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 2050 | |
8b62f87b JB |
2051 | spin_lock(&BTRFS_I(inode)->lock); |
2052 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
2053 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 2054 | |
6a3891c5 | 2055 | /* For sanity tests */ |
0b246afa | 2056 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2057 | return; |
2058 | ||
104b4e51 NB |
2059 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2060 | fs_info->delalloc_batch); | |
df0af1a5 | 2061 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 2062 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
2063 | if (*bits & EXTENT_DEFRAG) |
2064 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 2065 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
2066 | &BTRFS_I(inode)->runtime_flags)) |
2067 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 2068 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 2069 | } |
a7e3b975 FM |
2070 | |
2071 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
2072 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2073 | spin_lock(&BTRFS_I(inode)->lock); | |
2074 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
2075 | state->start; | |
2076 | spin_unlock(&BTRFS_I(inode)->lock); | |
2077 | } | |
291d673e CM |
2078 | } |
2079 | ||
d352ac68 | 2080 | /* |
a36bb5f9 NB |
2081 | * Once a range is no longer delalloc this function ensures that proper |
2082 | * accounting happens. | |
d352ac68 | 2083 | */ |
a36bb5f9 NB |
2084 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
2085 | struct extent_state *state, unsigned *bits) | |
291d673e | 2086 | { |
a36bb5f9 NB |
2087 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
2088 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 2089 | u64 len = state->end + 1 - state->start; |
823bb20a | 2090 | u32 num_extents = count_max_extents(len); |
47059d93 | 2091 | |
4a4b964f FM |
2092 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
2093 | spin_lock(&inode->lock); | |
6fc0ef68 | 2094 | inode->defrag_bytes -= len; |
4a4b964f FM |
2095 | spin_unlock(&inode->lock); |
2096 | } | |
47059d93 | 2097 | |
75eff68e CM |
2098 | /* |
2099 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2100 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2101 | * bit, which is only set or cleared with irqs on |
2102 | */ | |
0ca1f7ce | 2103 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2104 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2105 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2106 | |
8b62f87b JB |
2107 | spin_lock(&inode->lock); |
2108 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2109 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2110 | |
b6d08f06 JB |
2111 | /* |
2112 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2113 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2114 | * error. |
2115 | */ | |
a315e68f | 2116 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2117 | root != fs_info->tree_root) |
43b18595 | 2118 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2119 | |
6a3891c5 | 2120 | /* For sanity tests. */ |
0b246afa | 2121 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2122 | return; |
2123 | ||
a315e68f FM |
2124 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
2125 | do_list && !(state->state & EXTENT_NORESERVE) && | |
2126 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
9db5d510 | 2127 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2128 | |
104b4e51 NB |
2129 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2130 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2131 | spin_lock(&inode->lock); |
2132 | inode->delalloc_bytes -= len; | |
2133 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2134 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2135 | &inode->runtime_flags)) |
eb73c1b7 | 2136 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2137 | spin_unlock(&inode->lock); |
291d673e | 2138 | } |
a7e3b975 FM |
2139 | |
2140 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
2141 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2142 | spin_lock(&inode->lock); | |
2143 | ASSERT(inode->new_delalloc_bytes >= len); | |
2144 | inode->new_delalloc_bytes -= len; | |
2145 | spin_unlock(&inode->lock); | |
2146 | } | |
291d673e CM |
2147 | } |
2148 | ||
d352ac68 | 2149 | /* |
da12fe54 NB |
2150 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
2151 | * in a chunk's stripe. This function ensures that bios do not span a | |
2152 | * stripe/chunk | |
6f034ece | 2153 | * |
da12fe54 NB |
2154 | * @page - The page we are about to add to the bio |
2155 | * @size - size we want to add to the bio | |
2156 | * @bio - bio we want to ensure is smaller than a stripe | |
2157 | * @bio_flags - flags of the bio | |
2158 | * | |
2159 | * return 1 if page cannot be added to the bio | |
2160 | * return 0 if page can be added to the bio | |
6f034ece | 2161 | * return error otherwise |
d352ac68 | 2162 | */ |
da12fe54 NB |
2163 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
2164 | unsigned long bio_flags) | |
239b14b3 | 2165 | { |
0b246afa JM |
2166 | struct inode *inode = page->mapping->host; |
2167 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 2168 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
2169 | u64 length = 0; |
2170 | u64 map_length; | |
239b14b3 | 2171 | int ret; |
89b798ad | 2172 | struct btrfs_io_geometry geom; |
239b14b3 | 2173 | |
771ed689 CM |
2174 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
2175 | return 0; | |
2176 | ||
4f024f37 | 2177 | length = bio->bi_iter.bi_size; |
239b14b3 | 2178 | map_length = length; |
89b798ad NB |
2179 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
2180 | &geom); | |
6f034ece LB |
2181 | if (ret < 0) |
2182 | return ret; | |
89b798ad NB |
2183 | |
2184 | if (geom.len < length + size) | |
239b14b3 | 2185 | return 1; |
3444a972 | 2186 | return 0; |
239b14b3 CM |
2187 | } |
2188 | ||
d352ac68 CM |
2189 | /* |
2190 | * in order to insert checksums into the metadata in large chunks, | |
2191 | * we wait until bio submission time. All the pages in the bio are | |
2192 | * checksummed and sums are attached onto the ordered extent record. | |
2193 | * | |
2194 | * At IO completion time the cums attached on the ordered extent record | |
2195 | * are inserted into the btree | |
2196 | */ | |
8896a08d QW |
2197 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
2198 | u64 bio_offset) | |
065631f6 | 2199 | { |
c965d640 | 2200 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
4a69a410 | 2201 | } |
e015640f | 2202 | |
d352ac68 | 2203 | /* |
cad321ad | 2204 | * extent_io.c submission hook. This does the right thing for csum calculation |
4c274bc6 LB |
2205 | * on write, or reading the csums from the tree before a read. |
2206 | * | |
2207 | * Rules about async/sync submit, | |
2208 | * a) read: sync submit | |
2209 | * | |
2210 | * b) write without checksum: sync submit | |
2211 | * | |
2212 | * c) write with checksum: | |
2213 | * c-1) if bio is issued by fsync: sync submit | |
2214 | * (sync_writers != 0) | |
2215 | * | |
2216 | * c-2) if root is reloc root: sync submit | |
2217 | * (only in case of buffered IO) | |
2218 | * | |
2219 | * c-3) otherwise: async submit | |
d352ac68 | 2220 | */ |
908930f3 NB |
2221 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
2222 | int mirror_num, unsigned long bio_flags) | |
50489a57 | 2223 | |
44b8bd7e | 2224 | { |
0b246afa | 2225 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 2226 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 2227 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 2228 | blk_status_t ret = 0; |
19b9bdb0 | 2229 | int skip_sum; |
b812ce28 | 2230 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 2231 | |
42437a63 JB |
2232 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
2233 | !fs_info->csum_root; | |
cad321ad | 2234 | |
70ddc553 | 2235 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 2236 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 2237 | |
37226b21 | 2238 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 2239 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 2240 | if (ret) |
61891923 | 2241 | goto out; |
5fd02043 | 2242 | |
d20f7043 | 2243 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
2244 | ret = btrfs_submit_compressed_read(inode, bio, |
2245 | mirror_num, | |
2246 | bio_flags); | |
2247 | goto out; | |
334c16d8 JB |
2248 | } else { |
2249 | /* | |
2250 | * Lookup bio sums does extra checks around whether we | |
2251 | * need to csum or not, which is why we ignore skip_sum | |
2252 | * here. | |
2253 | */ | |
db72e47f | 2254 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
c2db1073 | 2255 | if (ret) |
61891923 | 2256 | goto out; |
c2db1073 | 2257 | } |
4d1b5fb4 | 2258 | goto mapit; |
b812ce28 | 2259 | } else if (async && !skip_sum) { |
17d217fe YZ |
2260 | /* csum items have already been cloned */ |
2261 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
2262 | goto mapit; | |
19b9bdb0 | 2263 | /* we're doing a write, do the async checksumming */ |
8896a08d QW |
2264 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
2265 | 0, btrfs_submit_bio_start); | |
61891923 | 2266 | goto out; |
b812ce28 | 2267 | } else if (!skip_sum) { |
bd242a08 | 2268 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
b812ce28 JB |
2269 | if (ret) |
2270 | goto out; | |
19b9bdb0 CM |
2271 | } |
2272 | ||
0b86a832 | 2273 | mapit: |
08635bae | 2274 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
61891923 SB |
2275 | |
2276 | out: | |
4e4cbee9 CH |
2277 | if (ret) { |
2278 | bio->bi_status = ret; | |
4246a0b6 CH |
2279 | bio_endio(bio); |
2280 | } | |
61891923 | 2281 | return ret; |
065631f6 | 2282 | } |
6885f308 | 2283 | |
d352ac68 CM |
2284 | /* |
2285 | * given a list of ordered sums record them in the inode. This happens | |
2286 | * at IO completion time based on sums calculated at bio submission time. | |
2287 | */ | |
510f85ed NB |
2288 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
2289 | struct list_head *list) | |
e6dcd2dc | 2290 | { |
e6dcd2dc | 2291 | struct btrfs_ordered_sum *sum; |
ac01f26a | 2292 | int ret; |
e6dcd2dc | 2293 | |
c6e30871 | 2294 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2295 | trans->adding_csums = true; |
510f85ed | 2296 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
7c2871a2 | 2297 | trans->adding_csums = false; |
ac01f26a NB |
2298 | if (ret) |
2299 | return ret; | |
e6dcd2dc CM |
2300 | } |
2301 | return 0; | |
2302 | } | |
2303 | ||
c3347309 FM |
2304 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
2305 | const u64 start, | |
2306 | const u64 len, | |
2307 | struct extent_state **cached_state) | |
2308 | { | |
2309 | u64 search_start = start; | |
2310 | const u64 end = start + len - 1; | |
2311 | ||
2312 | while (search_start < end) { | |
2313 | const u64 search_len = end - search_start + 1; | |
2314 | struct extent_map *em; | |
2315 | u64 em_len; | |
2316 | int ret = 0; | |
2317 | ||
2318 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); | |
2319 | if (IS_ERR(em)) | |
2320 | return PTR_ERR(em); | |
2321 | ||
2322 | if (em->block_start != EXTENT_MAP_HOLE) | |
2323 | goto next; | |
2324 | ||
2325 | em_len = em->len; | |
2326 | if (em->start < search_start) | |
2327 | em_len -= search_start - em->start; | |
2328 | if (em_len > search_len) | |
2329 | em_len = search_len; | |
2330 | ||
2331 | ret = set_extent_bit(&inode->io_tree, search_start, | |
2332 | search_start + em_len - 1, | |
2333 | EXTENT_DELALLOC_NEW, | |
2334 | NULL, cached_state, GFP_NOFS); | |
2335 | next: | |
2336 | search_start = extent_map_end(em); | |
2337 | free_extent_map(em); | |
2338 | if (ret) | |
2339 | return ret; | |
2340 | } | |
2341 | return 0; | |
2342 | } | |
2343 | ||
c2566f22 | 2344 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2345 | unsigned int extra_bits, |
330a5827 | 2346 | struct extent_state **cached_state) |
ea8c2819 | 2347 | { |
fdb1e121 | 2348 | WARN_ON(PAGE_ALIGNED(end)); |
c3347309 FM |
2349 | |
2350 | if (start >= i_size_read(&inode->vfs_inode) && | |
2351 | !(inode->flags & BTRFS_INODE_PREALLOC)) { | |
2352 | /* | |
2353 | * There can't be any extents following eof in this case so just | |
2354 | * set the delalloc new bit for the range directly. | |
2355 | */ | |
2356 | extra_bits |= EXTENT_DELALLOC_NEW; | |
2357 | } else { | |
2358 | int ret; | |
2359 | ||
2360 | ret = btrfs_find_new_delalloc_bytes(inode, start, | |
2361 | end + 1 - start, | |
2362 | cached_state); | |
2363 | if (ret) | |
2364 | return ret; | |
2365 | } | |
2366 | ||
c2566f22 NB |
2367 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
2368 | cached_state); | |
ea8c2819 CM |
2369 | } |
2370 | ||
d352ac68 | 2371 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2372 | struct btrfs_writepage_fixup { |
2373 | struct page *page; | |
f4b1363c | 2374 | struct inode *inode; |
247e743c CM |
2375 | struct btrfs_work work; |
2376 | }; | |
2377 | ||
b2950863 | 2378 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2379 | { |
2380 | struct btrfs_writepage_fixup *fixup; | |
2381 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2382 | struct extent_state *cached_state = NULL; |
364ecf36 | 2383 | struct extent_changeset *data_reserved = NULL; |
247e743c | 2384 | struct page *page; |
65d87f79 | 2385 | struct btrfs_inode *inode; |
247e743c CM |
2386 | u64 page_start; |
2387 | u64 page_end; | |
25f3c502 | 2388 | int ret = 0; |
f4b1363c | 2389 | bool free_delalloc_space = true; |
247e743c CM |
2390 | |
2391 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2392 | page = fixup->page; | |
65d87f79 | 2393 | inode = BTRFS_I(fixup->inode); |
f4b1363c JB |
2394 | page_start = page_offset(page); |
2395 | page_end = page_offset(page) + PAGE_SIZE - 1; | |
2396 | ||
2397 | /* | |
2398 | * This is similar to page_mkwrite, we need to reserve the space before | |
2399 | * we take the page lock. | |
2400 | */ | |
65d87f79 NB |
2401 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2402 | PAGE_SIZE); | |
4a096752 | 2403 | again: |
247e743c | 2404 | lock_page(page); |
25f3c502 CM |
2405 | |
2406 | /* | |
2407 | * Before we queued this fixup, we took a reference on the page. | |
2408 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2409 | * address space. | |
2410 | */ | |
f4b1363c JB |
2411 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2412 | /* | |
2413 | * Unfortunately this is a little tricky, either | |
2414 | * | |
2415 | * 1) We got here and our page had already been dealt with and | |
2416 | * we reserved our space, thus ret == 0, so we need to just | |
2417 | * drop our space reservation and bail. This can happen the | |
2418 | * first time we come into the fixup worker, or could happen | |
2419 | * while waiting for the ordered extent. | |
2420 | * 2) Our page was already dealt with, but we happened to get an | |
2421 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2422 | * this case we obviously don't have anything to release, but | |
2423 | * because the page was already dealt with we don't want to | |
2424 | * mark the page with an error, so make sure we're resetting | |
2425 | * ret to 0. This is why we have this check _before_ the ret | |
2426 | * check, because we do not want to have a surprise ENOSPC | |
2427 | * when the page was already properly dealt with. | |
2428 | */ | |
2429 | if (!ret) { | |
65d87f79 NB |
2430 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2431 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2432 | page_start, PAGE_SIZE, |
2433 | true); | |
2434 | } | |
2435 | ret = 0; | |
247e743c | 2436 | goto out_page; |
f4b1363c | 2437 | } |
247e743c | 2438 | |
25f3c502 | 2439 | /* |
f4b1363c JB |
2440 | * We can't mess with the page state unless it is locked, so now that |
2441 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2442 | */ |
f4b1363c JB |
2443 | if (ret) |
2444 | goto out_page; | |
247e743c | 2445 | |
65d87f79 | 2446 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2447 | |
2448 | /* already ordered? We're done */ | |
8b62b72b | 2449 | if (PagePrivate2(page)) |
f4b1363c | 2450 | goto out_reserved; |
4a096752 | 2451 | |
65d87f79 | 2452 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2453 | if (ordered) { |
65d87f79 NB |
2454 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
2455 | &cached_state); | |
4a096752 | 2456 | unlock_page(page); |
c0a43603 | 2457 | btrfs_start_ordered_extent(ordered, 1); |
87826df0 | 2458 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2459 | goto again; |
2460 | } | |
247e743c | 2461 | |
65d87f79 | 2462 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2463 | &cached_state); |
25f3c502 | 2464 | if (ret) |
53687007 | 2465 | goto out_reserved; |
f3038ee3 | 2466 | |
25f3c502 CM |
2467 | /* |
2468 | * Everything went as planned, we're now the owner of a dirty page with | |
2469 | * delayed allocation bits set and space reserved for our COW | |
2470 | * destination. | |
2471 | * | |
2472 | * The page was dirty when we started, nothing should have cleaned it. | |
2473 | */ | |
2474 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2475 | free_delalloc_space = false; |
53687007 | 2476 | out_reserved: |
65d87f79 | 2477 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2478 | if (free_delalloc_space) |
65d87f79 NB |
2479 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2480 | PAGE_SIZE, true); | |
2481 | unlock_extent_cached(&inode->io_tree, page_start, page_end, | |
e43bbe5e | 2482 | &cached_state); |
247e743c | 2483 | out_page: |
25f3c502 CM |
2484 | if (ret) { |
2485 | /* | |
2486 | * We hit ENOSPC or other errors. Update the mapping and page | |
2487 | * to reflect the errors and clean the page. | |
2488 | */ | |
2489 | mapping_set_error(page->mapping, ret); | |
2490 | end_extent_writepage(page, ret, page_start, page_end); | |
2491 | clear_page_dirty_for_io(page); | |
2492 | SetPageError(page); | |
2493 | } | |
2494 | ClearPageChecked(page); | |
247e743c | 2495 | unlock_page(page); |
09cbfeaf | 2496 | put_page(page); |
b897abec | 2497 | kfree(fixup); |
364ecf36 | 2498 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2499 | /* |
2500 | * As a precaution, do a delayed iput in case it would be the last iput | |
2501 | * that could need flushing space. Recursing back to fixup worker would | |
2502 | * deadlock. | |
2503 | */ | |
65d87f79 | 2504 | btrfs_add_delayed_iput(&inode->vfs_inode); |
247e743c CM |
2505 | } |
2506 | ||
2507 | /* | |
2508 | * There are a few paths in the higher layers of the kernel that directly | |
2509 | * set the page dirty bit without asking the filesystem if it is a | |
2510 | * good idea. This causes problems because we want to make sure COW | |
2511 | * properly happens and the data=ordered rules are followed. | |
2512 | * | |
c8b97818 | 2513 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2514 | * hasn't been properly setup for IO. We kick off an async process |
2515 | * to fix it up. The async helper will wait for ordered extents, set | |
2516 | * the delalloc bit and make it safe to write the page. | |
2517 | */ | |
d75855b4 | 2518 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
247e743c CM |
2519 | { |
2520 | struct inode *inode = page->mapping->host; | |
0b246afa | 2521 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2522 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2523 | |
8b62b72b CM |
2524 | /* this page is properly in the ordered list */ |
2525 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2526 | return 0; |
2527 | ||
25f3c502 CM |
2528 | /* |
2529 | * PageChecked is set below when we create a fixup worker for this page, | |
2530 | * don't try to create another one if we're already PageChecked() | |
2531 | * | |
2532 | * The extent_io writepage code will redirty the page if we send back | |
2533 | * EAGAIN. | |
2534 | */ | |
247e743c CM |
2535 | if (PageChecked(page)) |
2536 | return -EAGAIN; | |
2537 | ||
2538 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2539 | if (!fixup) | |
2540 | return -EAGAIN; | |
f421950f | 2541 | |
f4b1363c JB |
2542 | /* |
2543 | * We are already holding a reference to this inode from | |
2544 | * write_cache_pages. We need to hold it because the space reservation | |
2545 | * takes place outside of the page lock, and we can't trust | |
2546 | * page->mapping outside of the page lock. | |
2547 | */ | |
2548 | ihold(inode); | |
247e743c | 2549 | SetPageChecked(page); |
09cbfeaf | 2550 | get_page(page); |
a0cac0ec | 2551 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 2552 | fixup->page = page; |
f4b1363c | 2553 | fixup->inode = inode; |
0b246afa | 2554 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2555 | |
2556 | return -EAGAIN; | |
247e743c CM |
2557 | } |
2558 | ||
d899e052 | 2559 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 2560 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a QW |
2561 | struct btrfs_file_extent_item *stack_fi, |
2562 | u64 qgroup_reserved) | |
d899e052 | 2563 | { |
c553f94d | 2564 | struct btrfs_root *root = inode->root; |
d899e052 YZ |
2565 | struct btrfs_path *path; |
2566 | struct extent_buffer *leaf; | |
2567 | struct btrfs_key ins; | |
203f44c5 QW |
2568 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2569 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
2570 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); | |
2571 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
1acae57b | 2572 | int extent_inserted = 0; |
d899e052 YZ |
2573 | int ret; |
2574 | ||
2575 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2576 | if (!path) |
2577 | return -ENOMEM; | |
d899e052 | 2578 | |
a1ed835e CM |
2579 | /* |
2580 | * we may be replacing one extent in the tree with another. | |
2581 | * The new extent is pinned in the extent map, and we don't want | |
2582 | * to drop it from the cache until it is completely in the btree. | |
2583 | * | |
2584 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2585 | * the caller is expected to unpin it and allow it to be merged | |
2586 | * with the others. | |
2587 | */ | |
c553f94d | 2588 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
1acae57b | 2589 | file_pos + num_bytes, NULL, 0, |
203f44c5 | 2590 | 1, sizeof(*stack_fi), &extent_inserted); |
79787eaa JM |
2591 | if (ret) |
2592 | goto out; | |
d899e052 | 2593 | |
1acae57b | 2594 | if (!extent_inserted) { |
c553f94d | 2595 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
2596 | ins.offset = file_pos; |
2597 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2598 | ||
2599 | path->leave_spinning = 1; | |
2600 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
203f44c5 | 2601 | sizeof(*stack_fi)); |
1acae57b FDBM |
2602 | if (ret) |
2603 | goto out; | |
2604 | } | |
d899e052 | 2605 | leaf = path->nodes[0]; |
203f44c5 QW |
2606 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2607 | write_extent_buffer(leaf, stack_fi, | |
2608 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2609 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2610 | |
d899e052 | 2611 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2612 | btrfs_release_path(path); |
d899e052 | 2613 | |
c553f94d | 2614 | inode_add_bytes(&inode->vfs_inode, num_bytes); |
d899e052 YZ |
2615 | |
2616 | ins.objectid = disk_bytenr; | |
2617 | ins.offset = disk_num_bytes; | |
2618 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2619 | |
c553f94d | 2620 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
2621 | if (ret) |
2622 | goto out; | |
2623 | ||
c553f94d | 2624 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
9729f10a | 2625 | file_pos, qgroup_reserved, &ins); |
79787eaa | 2626 | out: |
d899e052 | 2627 | btrfs_free_path(path); |
b9473439 | 2628 | |
79787eaa | 2629 | return ret; |
d899e052 YZ |
2630 | } |
2631 | ||
2ff7e61e | 2632 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2633 | u64 start, u64 len) |
2634 | { | |
32da5386 | 2635 | struct btrfs_block_group *cache; |
e570fd27 | 2636 | |
0b246afa | 2637 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2638 | ASSERT(cache); |
2639 | ||
2640 | spin_lock(&cache->lock); | |
2641 | cache->delalloc_bytes -= len; | |
2642 | spin_unlock(&cache->lock); | |
2643 | ||
2644 | btrfs_put_block_group(cache); | |
2645 | } | |
2646 | ||
203f44c5 | 2647 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
203f44c5 QW |
2648 | struct btrfs_ordered_extent *oe) |
2649 | { | |
2650 | struct btrfs_file_extent_item stack_fi; | |
2651 | u64 logical_len; | |
2652 | ||
2653 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2654 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2655 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2656 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2657 | oe->disk_num_bytes); | |
2658 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) | |
2659 | logical_len = oe->truncated_len; | |
2660 | else | |
2661 | logical_len = oe->num_bytes; | |
2662 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); | |
2663 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); | |
2664 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); | |
2665 | /* Encryption and other encoding is reserved and all 0 */ | |
2666 | ||
3c38c877 NB |
2667 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
2668 | oe->file_offset, &stack_fi, | |
2669 | oe->qgroup_rsv); | |
203f44c5 QW |
2670 | } |
2671 | ||
2672 | /* | |
2673 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
2674 | * an ordered extent if the range of bytes in the file it covers are |
2675 | * fully written. | |
2676 | */ | |
5fd02043 | 2677 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2678 | { |
5fd02043 | 2679 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2680 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2681 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2682 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2683 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2684 | struct extent_state *cached_state = NULL; |
bffe633e | 2685 | u64 start, end; |
261507a0 | 2686 | int compress_type = 0; |
77cef2ec | 2687 | int ret = 0; |
bffe633e | 2688 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 2689 | bool freespace_inode; |
77cef2ec | 2690 | bool truncated = false; |
a7e3b975 FM |
2691 | bool range_locked = false; |
2692 | bool clear_new_delalloc_bytes = false; | |
49940bdd | 2693 | bool clear_reserved_extent = true; |
313facc5 | 2694 | unsigned int clear_bits; |
a7e3b975 | 2695 | |
bffe633e OS |
2696 | start = ordered_extent->file_offset; |
2697 | end = start + ordered_extent->num_bytes - 1; | |
2698 | ||
a7e3b975 FM |
2699 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
2700 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2701 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2702 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2703 | |
8d510121 | 2704 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2705 | |
5fd02043 JB |
2706 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2707 | ret = -EIO; | |
2708 | goto out; | |
2709 | } | |
2710 | ||
bffe633e | 2711 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
f612496b | 2712 | |
77cef2ec JB |
2713 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2714 | truncated = true; | |
2715 | logical_len = ordered_extent->truncated_len; | |
2716 | /* Truncated the entire extent, don't bother adding */ | |
2717 | if (!logical_len) | |
2718 | goto out; | |
2719 | } | |
2720 | ||
c2167754 | 2721 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2722 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 2723 | |
d923afe9 | 2724 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
2725 | if (freespace_inode) |
2726 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
2727 | else |
2728 | trans = btrfs_join_transaction(root); | |
2729 | if (IS_ERR(trans)) { | |
2730 | ret = PTR_ERR(trans); | |
2731 | trans = NULL; | |
2732 | goto out; | |
c2167754 | 2733 | } |
69fe2d75 | 2734 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
2735 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2736 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2737 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2738 | goto out; |
2739 | } | |
e6dcd2dc | 2740 | |
a7e3b975 | 2741 | range_locked = true; |
bffe633e | 2742 | lock_extent_bits(io_tree, start, end, &cached_state); |
e6dcd2dc | 2743 | |
8d510121 NB |
2744 | if (freespace_inode) |
2745 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 2746 | else |
7a7eaa40 | 2747 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2748 | if (IS_ERR(trans)) { |
2749 | ret = PTR_ERR(trans); | |
2750 | trans = NULL; | |
a7e3b975 | 2751 | goto out; |
79787eaa | 2752 | } |
a79b7d4b | 2753 | |
69fe2d75 | 2754 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 2755 | |
c8b97818 | 2756 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2757 | compress_type = ordered_extent->compress_type; |
d899e052 | 2758 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2759 | BUG_ON(compress_type); |
7a6d7067 | 2760 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2761 | ordered_extent->file_offset, |
2762 | ordered_extent->file_offset + | |
77cef2ec | 2763 | logical_len); |
d899e052 | 2764 | } else { |
0b246afa | 2765 | BUG_ON(root == fs_info->tree_root); |
3c38c877 | 2766 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
49940bdd JB |
2767 | if (!ret) { |
2768 | clear_reserved_extent = false; | |
2ff7e61e | 2769 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
2770 | ordered_extent->disk_bytenr, |
2771 | ordered_extent->disk_num_bytes); | |
49940bdd | 2772 | } |
d899e052 | 2773 | } |
5dc562c5 | 2774 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
bffe633e OS |
2775 | ordered_extent->file_offset, |
2776 | ordered_extent->num_bytes, trans->transid); | |
79787eaa | 2777 | if (ret < 0) { |
66642832 | 2778 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2779 | goto out; |
79787eaa | 2780 | } |
2ac55d41 | 2781 | |
510f85ed | 2782 | ret = add_pending_csums(trans, &ordered_extent->list); |
ac01f26a NB |
2783 | if (ret) { |
2784 | btrfs_abort_transaction(trans, ret); | |
2785 | goto out; | |
2786 | } | |
e6dcd2dc | 2787 | |
d923afe9 | 2788 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
6c760c07 JB |
2789 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2790 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 2791 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2792 | goto out; |
1ef30be1 JB |
2793 | } |
2794 | ret = 0; | |
c2167754 | 2795 | out: |
313facc5 OS |
2796 | clear_bits = EXTENT_DEFRAG; |
2797 | if (range_locked) | |
2798 | clear_bits |= EXTENT_LOCKED; | |
2799 | if (clear_new_delalloc_bytes) | |
2800 | clear_bits |= EXTENT_DELALLOC_NEW; | |
bffe633e OS |
2801 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
2802 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, | |
313facc5 | 2803 | &cached_state); |
a7e3b975 | 2804 | |
a698d075 | 2805 | if (trans) |
3a45bb20 | 2806 | btrfs_end_transaction(trans); |
0cb59c99 | 2807 | |
77cef2ec | 2808 | if (ret || truncated) { |
bffe633e | 2809 | u64 unwritten_start = start; |
77cef2ec JB |
2810 | |
2811 | if (truncated) | |
bffe633e OS |
2812 | unwritten_start += logical_len; |
2813 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec JB |
2814 | |
2815 | /* Drop the cache for the part of the extent we didn't write. */ | |
bffe633e | 2816 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
5fd02043 | 2817 | |
0bec9ef5 JB |
2818 | /* |
2819 | * If the ordered extent had an IOERR or something else went | |
2820 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2821 | * back to the allocator. We only free the extent in the |
2822 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
2823 | * |
2824 | * If we made it past insert_reserved_file_extent before we | |
2825 | * errored out then we don't need to do this as the accounting | |
2826 | * has already been done. | |
0bec9ef5 | 2827 | */ |
77cef2ec | 2828 | if ((ret || !logical_len) && |
49940bdd | 2829 | clear_reserved_extent && |
77cef2ec | 2830 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
2831 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
2832 | /* | |
2833 | * Discard the range before returning it back to the | |
2834 | * free space pool | |
2835 | */ | |
46b27f50 | 2836 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 2837 | btrfs_discard_extent(fs_info, |
bffe633e OS |
2838 | ordered_extent->disk_bytenr, |
2839 | ordered_extent->disk_num_bytes, | |
2840 | NULL); | |
2ff7e61e | 2841 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
2842 | ordered_extent->disk_bytenr, |
2843 | ordered_extent->disk_num_bytes, 1); | |
4eaaec24 | 2844 | } |
0bec9ef5 JB |
2845 | } |
2846 | ||
5fd02043 | 2847 | /* |
8bad3c02 LB |
2848 | * This needs to be done to make sure anybody waiting knows we are done |
2849 | * updating everything for this ordered extent. | |
5fd02043 | 2850 | */ |
71fe0a55 | 2851 | btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent); |
5fd02043 | 2852 | |
e6dcd2dc CM |
2853 | /* once for us */ |
2854 | btrfs_put_ordered_extent(ordered_extent); | |
2855 | /* once for the tree */ | |
2856 | btrfs_put_ordered_extent(ordered_extent); | |
2857 | ||
5fd02043 JB |
2858 | return ret; |
2859 | } | |
2860 | ||
2861 | static void finish_ordered_fn(struct btrfs_work *work) | |
2862 | { | |
2863 | struct btrfs_ordered_extent *ordered_extent; | |
2864 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2865 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2866 | } |
2867 | ||
c629732d NB |
2868 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
2869 | u64 end, int uptodate) | |
211f90e6 | 2870 | { |
3347c48f NB |
2871 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
2872 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
5fd02043 | 2873 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 | 2874 | struct btrfs_workqueue *wq; |
5fd02043 | 2875 | |
1abe9b8a | 2876 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2877 | ||
8b62b72b | 2878 | ClearPagePrivate2(page); |
3347c48f NB |
2879 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2880 | end - start + 1, uptodate)) | |
c3988d63 | 2881 | return; |
5fd02043 | 2882 | |
3347c48f | 2883 | if (btrfs_is_free_space_inode(inode)) |
0b246afa | 2884 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 2885 | else |
0b246afa | 2886 | wq = fs_info->endio_write_workers; |
5fd02043 | 2887 | |
a0cac0ec | 2888 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
9e0af237 | 2889 | btrfs_queue_work(wq, &ordered_extent->work); |
211f90e6 CM |
2890 | } |
2891 | ||
265d4ac0 QW |
2892 | /* |
2893 | * check_data_csum - verify checksum of one sector of uncompressed data | |
2894 | * @inode: the inode | |
2895 | * @io_bio: btrfs_io_bio which contains the csum | |
2896 | * @icsum: checksum index in the io_bio->csum array, size of csum_size | |
2897 | * @page: page where is the data to be verified | |
2898 | * @pgoff: offset inside the page | |
2899 | * | |
2900 | * The length of such check is always one sector size. | |
2901 | */ | |
47df7765 | 2902 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
265d4ac0 | 2903 | int icsum, struct page *page, int pgoff) |
dc380aea | 2904 | { |
d5178578 JT |
2905 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2906 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
dc380aea | 2907 | char *kaddr; |
265d4ac0 | 2908 | u32 len = fs_info->sectorsize; |
d5178578 JT |
2909 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
2910 | u8 *csum_expected; | |
2911 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 2912 | |
265d4ac0 QW |
2913 | ASSERT(pgoff + len <= PAGE_SIZE); |
2914 | ||
d5178578 | 2915 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
dc380aea MX |
2916 | |
2917 | kaddr = kmap_atomic(page); | |
d5178578 JT |
2918 | shash->tfm = fs_info->csum_shash; |
2919 | ||
fd08001f | 2920 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
d5178578 JT |
2921 | |
2922 | if (memcmp(csum, csum_expected, csum_size)) | |
dc380aea MX |
2923 | goto zeroit; |
2924 | ||
2925 | kunmap_atomic(kaddr); | |
2926 | return 0; | |
2927 | zeroit: | |
265d4ac0 QW |
2928 | btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff, |
2929 | csum, csum_expected, io_bio->mirror_num); | |
814723e0 NB |
2930 | if (io_bio->device) |
2931 | btrfs_dev_stat_inc_and_print(io_bio->device, | |
2932 | BTRFS_DEV_STAT_CORRUPTION_ERRS); | |
dc380aea MX |
2933 | memset(kaddr + pgoff, 1, len); |
2934 | flush_dcache_page(page); | |
2935 | kunmap_atomic(kaddr); | |
dc380aea MX |
2936 | return -EIO; |
2937 | } | |
2938 | ||
d352ac68 CM |
2939 | /* |
2940 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
2941 | * if there's a match, we allow the bio to finish. If not, the code in |
2942 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 2943 | */ |
9a446d6a NB |
2944 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset, |
2945 | struct page *page, u64 start, u64 end, int mirror) | |
07157aac | 2946 | { |
4eee4fa4 | 2947 | size_t offset = start - page_offset(page); |
07157aac | 2948 | struct inode *inode = page->mapping->host; |
d1310b2e | 2949 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 2950 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 2951 | |
d20f7043 CM |
2952 | if (PageChecked(page)) { |
2953 | ClearPageChecked(page); | |
dc380aea | 2954 | return 0; |
d20f7043 | 2955 | } |
6cbff00f CH |
2956 | |
2957 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 2958 | return 0; |
17d217fe | 2959 | |
42437a63 JB |
2960 | if (!root->fs_info->csum_root) |
2961 | return 0; | |
2962 | ||
17d217fe | 2963 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
9655d298 | 2964 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 2965 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 2966 | return 0; |
17d217fe | 2967 | } |
d20f7043 | 2968 | |
facc8a22 | 2969 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
265d4ac0 | 2970 | return check_data_csum(inode, io_bio, phy_offset, page, offset); |
07157aac | 2971 | } |
b888db2b | 2972 | |
c1c3fac2 NB |
2973 | /* |
2974 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
2975 | * | |
2976 | * @inode: The inode we want to perform iput on | |
2977 | * | |
2978 | * This function uses the generic vfs_inode::i_count to track whether we should | |
2979 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
2980 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
2981 | * transaction commit time/superblock commit/cleaner kthread. | |
2982 | */ | |
24bbcf04 YZ |
2983 | void btrfs_add_delayed_iput(struct inode *inode) |
2984 | { | |
0b246afa | 2985 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 2986 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
2987 | |
2988 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
2989 | return; | |
2990 | ||
034f784d | 2991 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 2992 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
2993 | ASSERT(list_empty(&binode->delayed_iput)); |
2994 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 2995 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
2996 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
2997 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
2998 | } |
2999 | ||
63611e73 JB |
3000 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3001 | struct btrfs_inode *inode) | |
3002 | { | |
3003 | list_del_init(&inode->delayed_iput); | |
3004 | spin_unlock(&fs_info->delayed_iput_lock); | |
3005 | iput(&inode->vfs_inode); | |
3006 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3007 | wake_up(&fs_info->delayed_iputs_wait); | |
3008 | spin_lock(&fs_info->delayed_iput_lock); | |
3009 | } | |
3010 | ||
3011 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3012 | struct btrfs_inode *inode) | |
3013 | { | |
3014 | if (!list_empty(&inode->delayed_iput)) { | |
3015 | spin_lock(&fs_info->delayed_iput_lock); | |
3016 | if (!list_empty(&inode->delayed_iput)) | |
3017 | run_delayed_iput_locked(fs_info, inode); | |
3018 | spin_unlock(&fs_info->delayed_iput_lock); | |
3019 | } | |
3020 | } | |
3021 | ||
2ff7e61e | 3022 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3023 | { |
24bbcf04 | 3024 | |
24bbcf04 | 3025 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3026 | while (!list_empty(&fs_info->delayed_iputs)) { |
3027 | struct btrfs_inode *inode; | |
3028 | ||
3029 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3030 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3031 | run_delayed_iput_locked(fs_info, inode); |
24bbcf04 | 3032 | } |
8089fe62 | 3033 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3034 | } |
3035 | ||
034f784d JB |
3036 | /** |
3037 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running | |
3038 | * @fs_info - the fs_info for this fs | |
3039 | * @return - EINTR if we were killed, 0 if nothing's pending | |
3040 | * | |
3041 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3042 | * set. Once they are all done running we will return, unless we are killed in | |
3043 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3044 | * that might get blocked on the iputs. | |
3045 | */ | |
3046 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3047 | { | |
3048 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3049 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3050 | if (ret) | |
3051 | return -EINTR; | |
3052 | return 0; | |
3053 | } | |
3054 | ||
7b128766 | 3055 | /* |
f7e9e8fc OS |
3056 | * This creates an orphan entry for the given inode in case something goes wrong |
3057 | * in the middle of an unlink. | |
7b128766 | 3058 | */ |
73f2e545 | 3059 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3060 | struct btrfs_inode *inode) |
7b128766 | 3061 | { |
d68fc57b | 3062 | int ret; |
7b128766 | 3063 | |
27919067 OS |
3064 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3065 | if (ret && ret != -EEXIST) { | |
3066 | btrfs_abort_transaction(trans, ret); | |
3067 | return ret; | |
d68fc57b YZ |
3068 | } |
3069 | ||
d68fc57b | 3070 | return 0; |
7b128766 JB |
3071 | } |
3072 | ||
3073 | /* | |
f7e9e8fc OS |
3074 | * We have done the delete so we can go ahead and remove the orphan item for |
3075 | * this particular inode. | |
7b128766 | 3076 | */ |
48a3b636 | 3077 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3078 | struct btrfs_inode *inode) |
7b128766 | 3079 | { |
27919067 | 3080 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3081 | } |
3082 | ||
3083 | /* | |
3084 | * this cleans up any orphans that may be left on the list from the last use | |
3085 | * of this root. | |
3086 | */ | |
66b4ffd1 | 3087 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3088 | { |
0b246afa | 3089 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3090 | struct btrfs_path *path; |
3091 | struct extent_buffer *leaf; | |
7b128766 JB |
3092 | struct btrfs_key key, found_key; |
3093 | struct btrfs_trans_handle *trans; | |
3094 | struct inode *inode; | |
8f6d7f4f | 3095 | u64 last_objectid = 0; |
f7e9e8fc | 3096 | int ret = 0, nr_unlink = 0; |
7b128766 | 3097 | |
d68fc57b | 3098 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3099 | return 0; |
c71bf099 YZ |
3100 | |
3101 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3102 | if (!path) { |
3103 | ret = -ENOMEM; | |
3104 | goto out; | |
3105 | } | |
e4058b54 | 3106 | path->reada = READA_BACK; |
7b128766 JB |
3107 | |
3108 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3109 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3110 | key.offset = (u64)-1; |
3111 | ||
7b128766 JB |
3112 | while (1) { |
3113 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3114 | if (ret < 0) |
3115 | goto out; | |
7b128766 JB |
3116 | |
3117 | /* | |
3118 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3119 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3120 | * find the key and see if we have stuff that matches |
3121 | */ | |
3122 | if (ret > 0) { | |
66b4ffd1 | 3123 | ret = 0; |
7b128766 JB |
3124 | if (path->slots[0] == 0) |
3125 | break; | |
3126 | path->slots[0]--; | |
3127 | } | |
3128 | ||
3129 | /* pull out the item */ | |
3130 | leaf = path->nodes[0]; | |
7b128766 JB |
3131 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3132 | ||
3133 | /* make sure the item matches what we want */ | |
3134 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3135 | break; | |
962a298f | 3136 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3137 | break; |
3138 | ||
3139 | /* release the path since we're done with it */ | |
b3b4aa74 | 3140 | btrfs_release_path(path); |
7b128766 JB |
3141 | |
3142 | /* | |
3143 | * this is where we are basically btrfs_lookup, without the | |
3144 | * crossing root thing. we store the inode number in the | |
3145 | * offset of the orphan item. | |
3146 | */ | |
8f6d7f4f JB |
3147 | |
3148 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3149 | btrfs_err(fs_info, |
3150 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3151 | ret = -EINVAL; |
3152 | goto out; | |
3153 | } | |
3154 | ||
3155 | last_objectid = found_key.offset; | |
3156 | ||
5d4f98a2 YZ |
3157 | found_key.objectid = found_key.offset; |
3158 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3159 | found_key.offset = 0; | |
0202e83f | 3160 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
8c6ffba0 | 3161 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3162 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3163 | goto out; |
7b128766 | 3164 | |
0b246afa | 3165 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 | 3166 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3167 | int is_dead_root = 0; |
3168 | ||
3169 | /* | |
3170 | * this is an orphan in the tree root. Currently these | |
3171 | * could come from 2 sources: | |
3172 | * a) a snapshot deletion in progress | |
3173 | * b) a free space cache inode | |
3174 | * We need to distinguish those two, as the snapshot | |
3175 | * orphan must not get deleted. | |
3176 | * find_dead_roots already ran before us, so if this | |
3177 | * is a snapshot deletion, we should find the root | |
a619b3c7 | 3178 | * in the fs_roots radix tree. |
f8e9e0b0 | 3179 | */ |
a619b3c7 RK |
3180 | |
3181 | spin_lock(&fs_info->fs_roots_radix_lock); | |
3182 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3183 | (unsigned long)found_key.objectid); | |
3184 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) | |
3185 | is_dead_root = 1; | |
3186 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
3187 | ||
f8e9e0b0 AJ |
3188 | if (is_dead_root) { |
3189 | /* prevent this orphan from being found again */ | |
3190 | key.offset = found_key.objectid - 1; | |
3191 | continue; | |
3192 | } | |
f7e9e8fc | 3193 | |
f8e9e0b0 | 3194 | } |
f7e9e8fc | 3195 | |
7b128766 | 3196 | /* |
f7e9e8fc OS |
3197 | * If we have an inode with links, there are a couple of |
3198 | * possibilities. Old kernels (before v3.12) used to create an | |
3199 | * orphan item for truncate indicating that there were possibly | |
3200 | * extent items past i_size that needed to be deleted. In v3.12, | |
3201 | * truncate was changed to update i_size in sync with the extent | |
3202 | * items, but the (useless) orphan item was still created. Since | |
3203 | * v4.18, we don't create the orphan item for truncate at all. | |
3204 | * | |
3205 | * So, this item could mean that we need to do a truncate, but | |
3206 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3207 | * and was not cleanly unmounted. The odds of that are quite | |
3208 | * slim, and it's a pain to do the truncate now, so just delete | |
3209 | * the orphan item. | |
3210 | * | |
3211 | * It's also possible that this orphan item was supposed to be | |
3212 | * deleted but wasn't. The inode number may have been reused, | |
3213 | * but either way, we can delete the orphan item. | |
7b128766 | 3214 | */ |
f7e9e8fc OS |
3215 | if (ret == -ENOENT || inode->i_nlink) { |
3216 | if (!ret) | |
3217 | iput(inode); | |
a8c9e576 | 3218 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3219 | if (IS_ERR(trans)) { |
3220 | ret = PTR_ERR(trans); | |
3221 | goto out; | |
3222 | } | |
0b246afa JM |
3223 | btrfs_debug(fs_info, "auto deleting %Lu", |
3224 | found_key.objectid); | |
a8c9e576 JB |
3225 | ret = btrfs_del_orphan_item(trans, root, |
3226 | found_key.objectid); | |
3a45bb20 | 3227 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3228 | if (ret) |
3229 | goto out; | |
7b128766 JB |
3230 | continue; |
3231 | } | |
3232 | ||
f7e9e8fc | 3233 | nr_unlink++; |
7b128766 JB |
3234 | |
3235 | /* this will do delete_inode and everything for us */ | |
3236 | iput(inode); | |
3237 | } | |
3254c876 MX |
3238 | /* release the path since we're done with it */ |
3239 | btrfs_release_path(path); | |
3240 | ||
d68fc57b YZ |
3241 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3242 | ||
a575ceeb | 3243 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3244 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3245 | if (!IS_ERR(trans)) |
3a45bb20 | 3246 | btrfs_end_transaction(trans); |
d68fc57b | 3247 | } |
7b128766 JB |
3248 | |
3249 | if (nr_unlink) | |
0b246afa | 3250 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3251 | |
3252 | out: | |
3253 | if (ret) | |
0b246afa | 3254 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3255 | btrfs_free_path(path); |
3256 | return ret; | |
7b128766 JB |
3257 | } |
3258 | ||
46a53cca CM |
3259 | /* |
3260 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3261 | * don't find any xattrs, we know there can't be any acls. | |
3262 | * | |
3263 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3264 | */ | |
3265 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3266 | int slot, u64 objectid, |
3267 | int *first_xattr_slot) | |
46a53cca CM |
3268 | { |
3269 | u32 nritems = btrfs_header_nritems(leaf); | |
3270 | struct btrfs_key found_key; | |
f23b5a59 JB |
3271 | static u64 xattr_access = 0; |
3272 | static u64 xattr_default = 0; | |
46a53cca CM |
3273 | int scanned = 0; |
3274 | ||
f23b5a59 | 3275 | if (!xattr_access) { |
97d79299 AG |
3276 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3277 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3278 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3279 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3280 | } |
3281 | ||
46a53cca | 3282 | slot++; |
63541927 | 3283 | *first_xattr_slot = -1; |
46a53cca CM |
3284 | while (slot < nritems) { |
3285 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3286 | ||
3287 | /* we found a different objectid, there must not be acls */ | |
3288 | if (found_key.objectid != objectid) | |
3289 | return 0; | |
3290 | ||
3291 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3292 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3293 | if (*first_xattr_slot == -1) |
3294 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3295 | if (found_key.offset == xattr_access || |
3296 | found_key.offset == xattr_default) | |
3297 | return 1; | |
3298 | } | |
46a53cca CM |
3299 | |
3300 | /* | |
3301 | * we found a key greater than an xattr key, there can't | |
3302 | * be any acls later on | |
3303 | */ | |
3304 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3305 | return 0; | |
3306 | ||
3307 | slot++; | |
3308 | scanned++; | |
3309 | ||
3310 | /* | |
3311 | * it goes inode, inode backrefs, xattrs, extents, | |
3312 | * so if there are a ton of hard links to an inode there can | |
3313 | * be a lot of backrefs. Don't waste time searching too hard, | |
3314 | * this is just an optimization | |
3315 | */ | |
3316 | if (scanned >= 8) | |
3317 | break; | |
3318 | } | |
3319 | /* we hit the end of the leaf before we found an xattr or | |
3320 | * something larger than an xattr. We have to assume the inode | |
3321 | * has acls | |
3322 | */ | |
63541927 FDBM |
3323 | if (*first_xattr_slot == -1) |
3324 | *first_xattr_slot = slot; | |
46a53cca CM |
3325 | return 1; |
3326 | } | |
3327 | ||
d352ac68 CM |
3328 | /* |
3329 | * read an inode from the btree into the in-memory inode | |
3330 | */ | |
4222ea71 FM |
3331 | static int btrfs_read_locked_inode(struct inode *inode, |
3332 | struct btrfs_path *in_path) | |
39279cc3 | 3333 | { |
0b246afa | 3334 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3335 | struct btrfs_path *path = in_path; |
5f39d397 | 3336 | struct extent_buffer *leaf; |
39279cc3 CM |
3337 | struct btrfs_inode_item *inode_item; |
3338 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3339 | struct btrfs_key location; | |
67de1176 | 3340 | unsigned long ptr; |
46a53cca | 3341 | int maybe_acls; |
618e21d5 | 3342 | u32 rdev; |
39279cc3 | 3343 | int ret; |
2f7e33d4 | 3344 | bool filled = false; |
63541927 | 3345 | int first_xattr_slot; |
2f7e33d4 MX |
3346 | |
3347 | ret = btrfs_fill_inode(inode, &rdev); | |
3348 | if (!ret) | |
3349 | filled = true; | |
39279cc3 | 3350 | |
4222ea71 FM |
3351 | if (!path) { |
3352 | path = btrfs_alloc_path(); | |
3353 | if (!path) | |
3354 | return -ENOMEM; | |
3355 | } | |
1748f843 | 3356 | |
39279cc3 | 3357 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3358 | |
39279cc3 | 3359 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3360 | if (ret) { |
4222ea71 FM |
3361 | if (path != in_path) |
3362 | btrfs_free_path(path); | |
f5b3a417 | 3363 | return ret; |
67710892 | 3364 | } |
39279cc3 | 3365 | |
5f39d397 | 3366 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3367 | |
3368 | if (filled) | |
67de1176 | 3369 | goto cache_index; |
2f7e33d4 | 3370 | |
5f39d397 CM |
3371 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3372 | struct btrfs_inode_item); | |
5f39d397 | 3373 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3374 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3375 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3376 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3377 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3378 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3379 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3380 | |
a937b979 DS |
3381 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3382 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3383 | |
a937b979 DS |
3384 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3385 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3386 | |
a937b979 DS |
3387 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3388 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3389 | |
9cc97d64 | 3390 | BTRFS_I(inode)->i_otime.tv_sec = |
3391 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3392 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3393 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3394 | |
a76a3cd4 | 3395 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3396 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3397 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3398 | ||
c7f88c4e JL |
3399 | inode_set_iversion_queried(inode, |
3400 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3401 | inode->i_generation = BTRFS_I(inode)->generation; |
3402 | inode->i_rdev = 0; | |
3403 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3404 | ||
3405 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3406 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3407 | ||
3408 | cache_index: | |
5dc562c5 JB |
3409 | /* |
3410 | * If we were modified in the current generation and evicted from memory | |
3411 | * and then re-read we need to do a full sync since we don't have any | |
3412 | * idea about which extents were modified before we were evicted from | |
3413 | * cache. | |
6e17d30b YD |
3414 | * |
3415 | * This is required for both inode re-read from disk and delayed inode | |
3416 | * in delayed_nodes_tree. | |
5dc562c5 | 3417 | */ |
0b246afa | 3418 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3419 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3420 | &BTRFS_I(inode)->runtime_flags); | |
3421 | ||
bde6c242 FM |
3422 | /* |
3423 | * We don't persist the id of the transaction where an unlink operation | |
3424 | * against the inode was last made. So here we assume the inode might | |
3425 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3426 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3427 | * between the inode and its parent if the inode is fsync'ed and the log | |
3428 | * replayed. For example, in the scenario: | |
3429 | * | |
3430 | * touch mydir/foo | |
3431 | * ln mydir/foo mydir/bar | |
3432 | * sync | |
3433 | * unlink mydir/bar | |
3434 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3435 | * xfs_io -c fsync mydir/foo | |
3436 | * <power failure> | |
3437 | * mount fs, triggers fsync log replay | |
3438 | * | |
3439 | * We must make sure that when we fsync our inode foo we also log its | |
3440 | * parent inode, otherwise after log replay the parent still has the | |
3441 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3442 | * and doesn't have an inode ref with the name "bar" anymore. | |
3443 | * | |
3444 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3445 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3446 | * transaction commits on fsync if our inode is a directory, or if our |
3447 | * inode is not a directory, logging its parent unnecessarily. | |
3448 | */ | |
3449 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3450 | ||
3ebac17c FM |
3451 | /* |
3452 | * Same logic as for last_unlink_trans. We don't persist the generation | |
3453 | * of the last transaction where this inode was used for a reflink | |
3454 | * operation, so after eviction and reloading the inode we must be | |
3455 | * pessimistic and assume the last transaction that modified the inode. | |
3456 | */ | |
3457 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
3458 | ||
67de1176 MX |
3459 | path->slots[0]++; |
3460 | if (inode->i_nlink != 1 || | |
3461 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3462 | goto cache_acl; | |
3463 | ||
3464 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3465 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3466 | goto cache_acl; |
3467 | ||
3468 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3469 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3470 | struct btrfs_inode_ref *ref; | |
3471 | ||
3472 | ref = (struct btrfs_inode_ref *)ptr; | |
3473 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3474 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3475 | struct btrfs_inode_extref *extref; | |
3476 | ||
3477 | extref = (struct btrfs_inode_extref *)ptr; | |
3478 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3479 | extref); | |
3480 | } | |
2f7e33d4 | 3481 | cache_acl: |
46a53cca CM |
3482 | /* |
3483 | * try to precache a NULL acl entry for files that don't have | |
3484 | * any xattrs or acls | |
3485 | */ | |
33345d01 | 3486 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3487 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3488 | if (first_xattr_slot != -1) { |
3489 | path->slots[0] = first_xattr_slot; | |
3490 | ret = btrfs_load_inode_props(inode, path); | |
3491 | if (ret) | |
0b246afa | 3492 | btrfs_err(fs_info, |
351fd353 | 3493 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3494 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3495 | root->root_key.objectid, ret); |
3496 | } | |
4222ea71 FM |
3497 | if (path != in_path) |
3498 | btrfs_free_path(path); | |
63541927 | 3499 | |
72c04902 AV |
3500 | if (!maybe_acls) |
3501 | cache_no_acl(inode); | |
46a53cca | 3502 | |
39279cc3 | 3503 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3504 | case S_IFREG: |
3505 | inode->i_mapping->a_ops = &btrfs_aops; | |
3506 | inode->i_fop = &btrfs_file_operations; | |
3507 | inode->i_op = &btrfs_file_inode_operations; | |
3508 | break; | |
3509 | case S_IFDIR: | |
3510 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3511 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3512 | break; |
3513 | case S_IFLNK: | |
3514 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3515 | inode_nohighmem(inode); |
4779cc04 | 3516 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3517 | break; |
618e21d5 | 3518 | default: |
0279b4cd | 3519 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3520 | init_special_inode(inode, inode->i_mode, rdev); |
3521 | break; | |
39279cc3 | 3522 | } |
6cbff00f | 3523 | |
7b6a221e | 3524 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3525 | return 0; |
39279cc3 CM |
3526 | } |
3527 | ||
d352ac68 CM |
3528 | /* |
3529 | * given a leaf and an inode, copy the inode fields into the leaf | |
3530 | */ | |
e02119d5 CM |
3531 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3532 | struct extent_buffer *leaf, | |
5f39d397 | 3533 | struct btrfs_inode_item *item, |
39279cc3 CM |
3534 | struct inode *inode) |
3535 | { | |
51fab693 LB |
3536 | struct btrfs_map_token token; |
3537 | ||
c82f823c | 3538 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3539 | |
cc4c13d5 DS |
3540 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3541 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3542 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3543 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3544 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3545 | ||
3546 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
3547 | inode->i_atime.tv_sec); | |
3548 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
3549 | inode->i_atime.tv_nsec); | |
3550 | ||
3551 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
3552 | inode->i_mtime.tv_sec); | |
3553 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
3554 | inode->i_mtime.tv_nsec); | |
3555 | ||
3556 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
3557 | inode->i_ctime.tv_sec); | |
3558 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
3559 | inode->i_ctime.tv_nsec); | |
3560 | ||
3561 | btrfs_set_token_timespec_sec(&token, &item->otime, | |
3562 | BTRFS_I(inode)->i_otime.tv_sec); | |
3563 | btrfs_set_token_timespec_nsec(&token, &item->otime, | |
3564 | BTRFS_I(inode)->i_otime.tv_nsec); | |
3565 | ||
3566 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3567 | btrfs_set_token_inode_generation(&token, item, | |
3568 | BTRFS_I(inode)->generation); | |
3569 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3570 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3571 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
3572 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); | |
3573 | btrfs_set_token_inode_block_group(&token, item, 0); | |
39279cc3 CM |
3574 | } |
3575 | ||
d352ac68 CM |
3576 | /* |
3577 | * copy everything in the in-memory inode into the btree. | |
3578 | */ | |
2115133f | 3579 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3580 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3581 | { |
3582 | struct btrfs_inode_item *inode_item; | |
3583 | struct btrfs_path *path; | |
5f39d397 | 3584 | struct extent_buffer *leaf; |
39279cc3 CM |
3585 | int ret; |
3586 | ||
3587 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3588 | if (!path) |
3589 | return -ENOMEM; | |
3590 | ||
b9473439 | 3591 | path->leave_spinning = 1; |
16cdcec7 MX |
3592 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3593 | 1); | |
39279cc3 CM |
3594 | if (ret) { |
3595 | if (ret > 0) | |
3596 | ret = -ENOENT; | |
3597 | goto failed; | |
3598 | } | |
3599 | ||
5f39d397 CM |
3600 | leaf = path->nodes[0]; |
3601 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3602 | struct btrfs_inode_item); |
39279cc3 | 3603 | |
e02119d5 | 3604 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3605 | btrfs_mark_buffer_dirty(leaf); |
d9094414 | 3606 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
39279cc3 CM |
3607 | ret = 0; |
3608 | failed: | |
39279cc3 CM |
3609 | btrfs_free_path(path); |
3610 | return ret; | |
3611 | } | |
3612 | ||
2115133f CM |
3613 | /* |
3614 | * copy everything in the in-memory inode into the btree. | |
3615 | */ | |
3616 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3617 | struct btrfs_root *root, struct inode *inode) | |
3618 | { | |
0b246afa | 3619 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3620 | int ret; |
3621 | ||
3622 | /* | |
3623 | * If the inode is a free space inode, we can deadlock during commit | |
3624 | * if we put it into the delayed code. | |
3625 | * | |
3626 | * The data relocation inode should also be directly updated | |
3627 | * without delay | |
3628 | */ | |
70ddc553 | 3629 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3630 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3631 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3632 | btrfs_update_root_times(trans, root); |
3633 | ||
2115133f CM |
3634 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3635 | if (!ret) | |
d9094414 | 3636 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
2115133f CM |
3637 | return ret; |
3638 | } | |
3639 | ||
3640 | return btrfs_update_inode_item(trans, root, inode); | |
3641 | } | |
3642 | ||
be6aef60 JB |
3643 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3644 | struct btrfs_root *root, | |
3645 | struct inode *inode) | |
2115133f CM |
3646 | { |
3647 | int ret; | |
3648 | ||
3649 | ret = btrfs_update_inode(trans, root, inode); | |
3650 | if (ret == -ENOSPC) | |
3651 | return btrfs_update_inode_item(trans, root, inode); | |
3652 | return ret; | |
3653 | } | |
3654 | ||
d352ac68 CM |
3655 | /* |
3656 | * unlink helper that gets used here in inode.c and in the tree logging | |
3657 | * recovery code. It remove a link in a directory with a given name, and | |
3658 | * also drops the back refs in the inode to the directory | |
3659 | */ | |
92986796 AV |
3660 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3661 | struct btrfs_root *root, | |
4ec5934e NB |
3662 | struct btrfs_inode *dir, |
3663 | struct btrfs_inode *inode, | |
92986796 | 3664 | const char *name, int name_len) |
39279cc3 | 3665 | { |
0b246afa | 3666 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 3667 | struct btrfs_path *path; |
39279cc3 | 3668 | int ret = 0; |
39279cc3 | 3669 | struct btrfs_dir_item *di; |
aec7477b | 3670 | u64 index; |
33345d01 LZ |
3671 | u64 ino = btrfs_ino(inode); |
3672 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3673 | |
3674 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3675 | if (!path) { |
3676 | ret = -ENOMEM; | |
554233a6 | 3677 | goto out; |
54aa1f4d CM |
3678 | } |
3679 | ||
b9473439 | 3680 | path->leave_spinning = 1; |
33345d01 | 3681 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 3682 | name, name_len, -1); |
3cf5068f LB |
3683 | if (IS_ERR_OR_NULL(di)) { |
3684 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
3685 | goto err; |
3686 | } | |
39279cc3 | 3687 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3688 | if (ret) |
3689 | goto err; | |
b3b4aa74 | 3690 | btrfs_release_path(path); |
39279cc3 | 3691 | |
67de1176 MX |
3692 | /* |
3693 | * If we don't have dir index, we have to get it by looking up | |
3694 | * the inode ref, since we get the inode ref, remove it directly, | |
3695 | * it is unnecessary to do delayed deletion. | |
3696 | * | |
3697 | * But if we have dir index, needn't search inode ref to get it. | |
3698 | * Since the inode ref is close to the inode item, it is better | |
3699 | * that we delay to delete it, and just do this deletion when | |
3700 | * we update the inode item. | |
3701 | */ | |
4ec5934e | 3702 | if (inode->dir_index) { |
67de1176 MX |
3703 | ret = btrfs_delayed_delete_inode_ref(inode); |
3704 | if (!ret) { | |
4ec5934e | 3705 | index = inode->dir_index; |
67de1176 MX |
3706 | goto skip_backref; |
3707 | } | |
3708 | } | |
3709 | ||
33345d01 LZ |
3710 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3711 | dir_ino, &index); | |
aec7477b | 3712 | if (ret) { |
0b246afa | 3713 | btrfs_info(fs_info, |
c2cf52eb | 3714 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 3715 | name_len, name, ino, dir_ino); |
66642832 | 3716 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
3717 | goto err; |
3718 | } | |
67de1176 | 3719 | skip_backref: |
9add2945 | 3720 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 3721 | if (ret) { |
66642832 | 3722 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 3723 | goto err; |
79787eaa | 3724 | } |
39279cc3 | 3725 | |
4ec5934e NB |
3726 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
3727 | dir_ino); | |
79787eaa | 3728 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 3729 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3730 | goto err; |
3731 | } | |
e02119d5 | 3732 | |
4ec5934e NB |
3733 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
3734 | index); | |
6418c961 CM |
3735 | if (ret == -ENOENT) |
3736 | ret = 0; | |
d4e3991b | 3737 | else if (ret) |
66642832 | 3738 | btrfs_abort_transaction(trans, ret); |
63611e73 JB |
3739 | |
3740 | /* | |
3741 | * If we have a pending delayed iput we could end up with the final iput | |
3742 | * being run in btrfs-cleaner context. If we have enough of these built | |
3743 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
3744 | * way to throttle the unlinks. Since we're currently holding a ref on | |
3745 | * the inode we can run the delayed iput here without any issues as the | |
3746 | * final iput won't be done until after we drop the ref we're currently | |
3747 | * holding. | |
3748 | */ | |
3749 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
3750 | err: |
3751 | btrfs_free_path(path); | |
e02119d5 CM |
3752 | if (ret) |
3753 | goto out; | |
3754 | ||
6ef06d27 | 3755 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
3756 | inode_inc_iversion(&inode->vfs_inode); |
3757 | inode_inc_iversion(&dir->vfs_inode); | |
3758 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
3759 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
3760 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 3761 | out: |
39279cc3 CM |
3762 | return ret; |
3763 | } | |
3764 | ||
92986796 AV |
3765 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3766 | struct btrfs_root *root, | |
4ec5934e | 3767 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
3768 | const char *name, int name_len) |
3769 | { | |
3770 | int ret; | |
3771 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
3772 | if (!ret) { | |
4ec5934e NB |
3773 | drop_nlink(&inode->vfs_inode); |
3774 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
3775 | } |
3776 | return ret; | |
3777 | } | |
39279cc3 | 3778 | |
a22285a6 YZ |
3779 | /* |
3780 | * helper to start transaction for unlink and rmdir. | |
3781 | * | |
d52be818 JB |
3782 | * unlink and rmdir are special in btrfs, they do not always free space, so |
3783 | * if we cannot make our reservations the normal way try and see if there is | |
3784 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
3785 | * allow the unlink to occur. | |
a22285a6 | 3786 | */ |
d52be818 | 3787 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 3788 | { |
a22285a6 | 3789 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 3790 | |
e70bea5f JB |
3791 | /* |
3792 | * 1 for the possible orphan item | |
3793 | * 1 for the dir item | |
3794 | * 1 for the dir index | |
3795 | * 1 for the inode ref | |
e70bea5f JB |
3796 | * 1 for the inode |
3797 | */ | |
7f9fe614 | 3798 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
a22285a6 YZ |
3799 | } |
3800 | ||
3801 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
3802 | { | |
3803 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3804 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 3805 | struct inode *inode = d_inode(dentry); |
a22285a6 | 3806 | int ret; |
a22285a6 | 3807 | |
d52be818 | 3808 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
3809 | if (IS_ERR(trans)) |
3810 | return PTR_ERR(trans); | |
5f39d397 | 3811 | |
4ec5934e NB |
3812 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
3813 | 0); | |
12fcfd22 | 3814 | |
4ec5934e NB |
3815 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
3816 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
3817 | dentry->d_name.len); | |
b532402e TI |
3818 | if (ret) |
3819 | goto out; | |
7b128766 | 3820 | |
a22285a6 | 3821 | if (inode->i_nlink == 0) { |
73f2e545 | 3822 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
3823 | if (ret) |
3824 | goto out; | |
a22285a6 | 3825 | } |
7b128766 | 3826 | |
b532402e | 3827 | out: |
3a45bb20 | 3828 | btrfs_end_transaction(trans); |
2ff7e61e | 3829 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
3830 | return ret; |
3831 | } | |
3832 | ||
f60a2364 | 3833 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
045d3967 | 3834 | struct inode *dir, struct dentry *dentry) |
4df27c4d | 3835 | { |
401b3b19 | 3836 | struct btrfs_root *root = BTRFS_I(dir)->root; |
045d3967 | 3837 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
3838 | struct btrfs_path *path; |
3839 | struct extent_buffer *leaf; | |
3840 | struct btrfs_dir_item *di; | |
3841 | struct btrfs_key key; | |
045d3967 JB |
3842 | const char *name = dentry->d_name.name; |
3843 | int name_len = dentry->d_name.len; | |
4df27c4d YZ |
3844 | u64 index; |
3845 | int ret; | |
045d3967 | 3846 | u64 objectid; |
4a0cc7ca | 3847 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d | 3848 | |
045d3967 JB |
3849 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
3850 | objectid = inode->root->root_key.objectid; | |
3851 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
3852 | objectid = inode->location.objectid; | |
3853 | } else { | |
3854 | WARN_ON(1); | |
3855 | return -EINVAL; | |
3856 | } | |
3857 | ||
4df27c4d YZ |
3858 | path = btrfs_alloc_path(); |
3859 | if (!path) | |
3860 | return -ENOMEM; | |
3861 | ||
33345d01 | 3862 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 3863 | name, name_len, -1); |
79787eaa | 3864 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 3865 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
3866 | goto out; |
3867 | } | |
4df27c4d YZ |
3868 | |
3869 | leaf = path->nodes[0]; | |
3870 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
3871 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
3872 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 3873 | if (ret) { |
66642832 | 3874 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3875 | goto out; |
3876 | } | |
b3b4aa74 | 3877 | btrfs_release_path(path); |
4df27c4d | 3878 | |
d49d3287 JB |
3879 | /* |
3880 | * This is a placeholder inode for a subvolume we didn't have a | |
3881 | * reference to at the time of the snapshot creation. In the meantime | |
3882 | * we could have renamed the real subvol link into our snapshot, so | |
3883 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. | |
3884 | * Instead simply lookup the dir_index_item for this entry so we can | |
3885 | * remove it. Otherwise we know we have a ref to the root and we can | |
3886 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
3887 | */ | |
3888 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
33345d01 | 3889 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 3890 | name, name_len); |
79787eaa JM |
3891 | if (IS_ERR_OR_NULL(di)) { |
3892 | if (!di) | |
3893 | ret = -ENOENT; | |
3894 | else | |
3895 | ret = PTR_ERR(di); | |
66642832 | 3896 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3897 | goto out; |
3898 | } | |
4df27c4d YZ |
3899 | |
3900 | leaf = path->nodes[0]; | |
3901 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 3902 | index = key.offset; |
d49d3287 JB |
3903 | btrfs_release_path(path); |
3904 | } else { | |
3905 | ret = btrfs_del_root_ref(trans, objectid, | |
3906 | root->root_key.objectid, dir_ino, | |
3907 | &index, name, name_len); | |
3908 | if (ret) { | |
3909 | btrfs_abort_transaction(trans, ret); | |
3910 | goto out; | |
3911 | } | |
4df27c4d YZ |
3912 | } |
3913 | ||
9add2945 | 3914 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 3915 | if (ret) { |
66642832 | 3916 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3917 | goto out; |
3918 | } | |
4df27c4d | 3919 | |
6ef06d27 | 3920 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 3921 | inode_inc_iversion(dir); |
c2050a45 | 3922 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 3923 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 3924 | if (ret) |
66642832 | 3925 | btrfs_abort_transaction(trans, ret); |
79787eaa | 3926 | out: |
71d7aed0 | 3927 | btrfs_free_path(path); |
79787eaa | 3928 | return ret; |
4df27c4d YZ |
3929 | } |
3930 | ||
ec42f167 MT |
3931 | /* |
3932 | * Helper to check if the subvolume references other subvolumes or if it's | |
3933 | * default. | |
3934 | */ | |
f60a2364 | 3935 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
3936 | { |
3937 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3938 | struct btrfs_path *path; | |
3939 | struct btrfs_dir_item *di; | |
3940 | struct btrfs_key key; | |
3941 | u64 dir_id; | |
3942 | int ret; | |
3943 | ||
3944 | path = btrfs_alloc_path(); | |
3945 | if (!path) | |
3946 | return -ENOMEM; | |
3947 | ||
3948 | /* Make sure this root isn't set as the default subvol */ | |
3949 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
3950 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
3951 | dir_id, "default", 7, 0); | |
3952 | if (di && !IS_ERR(di)) { | |
3953 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
3954 | if (key.objectid == root->root_key.objectid) { | |
3955 | ret = -EPERM; | |
3956 | btrfs_err(fs_info, | |
3957 | "deleting default subvolume %llu is not allowed", | |
3958 | key.objectid); | |
3959 | goto out; | |
3960 | } | |
3961 | btrfs_release_path(path); | |
3962 | } | |
3963 | ||
3964 | key.objectid = root->root_key.objectid; | |
3965 | key.type = BTRFS_ROOT_REF_KEY; | |
3966 | key.offset = (u64)-1; | |
3967 | ||
3968 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
3969 | if (ret < 0) | |
3970 | goto out; | |
3971 | BUG_ON(ret == 0); | |
3972 | ||
3973 | ret = 0; | |
3974 | if (path->slots[0] > 0) { | |
3975 | path->slots[0]--; | |
3976 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
3977 | if (key.objectid == root->root_key.objectid && | |
3978 | key.type == BTRFS_ROOT_REF_KEY) | |
3979 | ret = -ENOTEMPTY; | |
3980 | } | |
3981 | out: | |
3982 | btrfs_free_path(path); | |
3983 | return ret; | |
3984 | } | |
3985 | ||
20a68004 NB |
3986 | /* Delete all dentries for inodes belonging to the root */ |
3987 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
3988 | { | |
3989 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3990 | struct rb_node *node; | |
3991 | struct rb_node *prev; | |
3992 | struct btrfs_inode *entry; | |
3993 | struct inode *inode; | |
3994 | u64 objectid = 0; | |
3995 | ||
3996 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
3997 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
3998 | ||
3999 | spin_lock(&root->inode_lock); | |
4000 | again: | |
4001 | node = root->inode_tree.rb_node; | |
4002 | prev = NULL; | |
4003 | while (node) { | |
4004 | prev = node; | |
4005 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4006 | ||
37508515 | 4007 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4008 | node = node->rb_left; |
37508515 | 4009 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4010 | node = node->rb_right; |
4011 | else | |
4012 | break; | |
4013 | } | |
4014 | if (!node) { | |
4015 | while (prev) { | |
4016 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4017 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4018 | node = prev; |
4019 | break; | |
4020 | } | |
4021 | prev = rb_next(prev); | |
4022 | } | |
4023 | } | |
4024 | while (node) { | |
4025 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4026 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4027 | inode = igrab(&entry->vfs_inode); |
4028 | if (inode) { | |
4029 | spin_unlock(&root->inode_lock); | |
4030 | if (atomic_read(&inode->i_count) > 1) | |
4031 | d_prune_aliases(inode); | |
4032 | /* | |
4033 | * btrfs_drop_inode will have it removed from the inode | |
4034 | * cache when its usage count hits zero. | |
4035 | */ | |
4036 | iput(inode); | |
4037 | cond_resched(); | |
4038 | spin_lock(&root->inode_lock); | |
4039 | goto again; | |
4040 | } | |
4041 | ||
4042 | if (cond_resched_lock(&root->inode_lock)) | |
4043 | goto again; | |
4044 | ||
4045 | node = rb_next(node); | |
4046 | } | |
4047 | spin_unlock(&root->inode_lock); | |
4048 | } | |
4049 | ||
f60a2364 MT |
4050 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
4051 | { | |
4052 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
4053 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4054 | struct inode *inode = d_inode(dentry); | |
4055 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4056 | struct btrfs_trans_handle *trans; | |
4057 | struct btrfs_block_rsv block_rsv; | |
4058 | u64 root_flags; | |
f60a2364 MT |
4059 | int ret; |
4060 | int err; | |
4061 | ||
4062 | /* | |
4063 | * Don't allow to delete a subvolume with send in progress. This is | |
4064 | * inside the inode lock so the error handling that has to drop the bit | |
4065 | * again is not run concurrently. | |
4066 | */ | |
4067 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4068 | if (dest->send_in_progress) { |
f60a2364 MT |
4069 | spin_unlock(&dest->root_item_lock); |
4070 | btrfs_warn(fs_info, | |
4071 | "attempt to delete subvolume %llu during send", | |
4072 | dest->root_key.objectid); | |
4073 | return -EPERM; | |
4074 | } | |
a7176f74 LF |
4075 | root_flags = btrfs_root_flags(&dest->root_item); |
4076 | btrfs_set_root_flags(&dest->root_item, | |
4077 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4078 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4079 | |
4080 | down_write(&fs_info->subvol_sem); | |
4081 | ||
4082 | err = may_destroy_subvol(dest); | |
4083 | if (err) | |
4084 | goto out_up_write; | |
4085 | ||
4086 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4087 | /* | |
4088 | * One for dir inode, | |
4089 | * two for dir entries, | |
4090 | * two for root ref/backref. | |
4091 | */ | |
c4c129db | 4092 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
f60a2364 MT |
4093 | if (err) |
4094 | goto out_up_write; | |
4095 | ||
4096 | trans = btrfs_start_transaction(root, 0); | |
4097 | if (IS_ERR(trans)) { | |
4098 | err = PTR_ERR(trans); | |
4099 | goto out_release; | |
4100 | } | |
4101 | trans->block_rsv = &block_rsv; | |
4102 | trans->bytes_reserved = block_rsv.size; | |
4103 | ||
4104 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
4105 | ||
045d3967 | 4106 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 MT |
4107 | if (ret) { |
4108 | err = ret; | |
4109 | btrfs_abort_transaction(trans, ret); | |
4110 | goto out_end_trans; | |
4111 | } | |
4112 | ||
4113 | btrfs_record_root_in_trans(trans, dest); | |
4114 | ||
4115 | memset(&dest->root_item.drop_progress, 0, | |
4116 | sizeof(dest->root_item.drop_progress)); | |
c8422684 | 4117 | btrfs_set_root_drop_level(&dest->root_item, 0); |
f60a2364 MT |
4118 | btrfs_set_root_refs(&dest->root_item, 0); |
4119 | ||
4120 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4121 | ret = btrfs_insert_orphan_item(trans, | |
4122 | fs_info->tree_root, | |
4123 | dest->root_key.objectid); | |
4124 | if (ret) { | |
4125 | btrfs_abort_transaction(trans, ret); | |
4126 | err = ret; | |
4127 | goto out_end_trans; | |
4128 | } | |
4129 | } | |
4130 | ||
d1957791 | 4131 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4132 | BTRFS_UUID_KEY_SUBVOL, |
4133 | dest->root_key.objectid); | |
4134 | if (ret && ret != -ENOENT) { | |
4135 | btrfs_abort_transaction(trans, ret); | |
4136 | err = ret; | |
4137 | goto out_end_trans; | |
4138 | } | |
4139 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4140 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4141 | dest->root_item.received_uuid, |
4142 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4143 | dest->root_key.objectid); | |
4144 | if (ret && ret != -ENOENT) { | |
4145 | btrfs_abort_transaction(trans, ret); | |
4146 | err = ret; | |
4147 | goto out_end_trans; | |
4148 | } | |
4149 | } | |
4150 | ||
082b6c97 QW |
4151 | free_anon_bdev(dest->anon_dev); |
4152 | dest->anon_dev = 0; | |
f60a2364 MT |
4153 | out_end_trans: |
4154 | trans->block_rsv = NULL; | |
4155 | trans->bytes_reserved = 0; | |
4156 | ret = btrfs_end_transaction(trans); | |
4157 | if (ret && !err) | |
4158 | err = ret; | |
4159 | inode->i_flags |= S_DEAD; | |
4160 | out_release: | |
e85fde51 | 4161 | btrfs_subvolume_release_metadata(root, &block_rsv); |
f60a2364 MT |
4162 | out_up_write: |
4163 | up_write(&fs_info->subvol_sem); | |
4164 | if (err) { | |
4165 | spin_lock(&dest->root_item_lock); | |
4166 | root_flags = btrfs_root_flags(&dest->root_item); | |
4167 | btrfs_set_root_flags(&dest->root_item, | |
4168 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4169 | spin_unlock(&dest->root_item_lock); | |
4170 | } else { | |
4171 | d_invalidate(dentry); | |
20a68004 | 4172 | btrfs_prune_dentries(dest); |
f60a2364 MT |
4173 | ASSERT(dest->send_in_progress == 0); |
4174 | ||
4175 | /* the last ref */ | |
4176 | if (dest->ino_cache_inode) { | |
4177 | iput(dest->ino_cache_inode); | |
4178 | dest->ino_cache_inode = NULL; | |
4179 | } | |
4180 | } | |
4181 | ||
4182 | return err; | |
4183 | } | |
4184 | ||
39279cc3 CM |
4185 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4186 | { | |
2b0143b5 | 4187 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4188 | int err = 0; |
39279cc3 | 4189 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4190 | struct btrfs_trans_handle *trans; |
44f714da | 4191 | u64 last_unlink_trans; |
39279cc3 | 4192 | |
b3ae244e | 4193 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4194 | return -ENOTEMPTY; |
4a0cc7ca | 4195 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4196 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4197 | |
d52be818 | 4198 | trans = __unlink_start_trans(dir); |
a22285a6 | 4199 | if (IS_ERR(trans)) |
5df6a9f6 | 4200 | return PTR_ERR(trans); |
5df6a9f6 | 4201 | |
4a0cc7ca | 4202 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 4203 | err = btrfs_unlink_subvol(trans, dir, dentry); |
4df27c4d YZ |
4204 | goto out; |
4205 | } | |
4206 | ||
73f2e545 | 4207 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4208 | if (err) |
4df27c4d | 4209 | goto out; |
7b128766 | 4210 | |
44f714da FM |
4211 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4212 | ||
39279cc3 | 4213 | /* now the directory is empty */ |
4ec5934e NB |
4214 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4215 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4216 | dentry->d_name.len); | |
44f714da | 4217 | if (!err) { |
6ef06d27 | 4218 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4219 | /* |
4220 | * Propagate the last_unlink_trans value of the deleted dir to | |
4221 | * its parent directory. This is to prevent an unrecoverable | |
4222 | * log tree in the case we do something like this: | |
4223 | * 1) create dir foo | |
4224 | * 2) create snapshot under dir foo | |
4225 | * 3) delete the snapshot | |
4226 | * 4) rmdir foo | |
4227 | * 5) mkdir foo | |
4228 | * 6) fsync foo or some file inside foo | |
4229 | */ | |
4230 | if (last_unlink_trans >= trans->transid) | |
4231 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4232 | } | |
4df27c4d | 4233 | out: |
3a45bb20 | 4234 | btrfs_end_transaction(trans); |
2ff7e61e | 4235 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4236 | |
39279cc3 CM |
4237 | return err; |
4238 | } | |
4239 | ||
ddfae63c JB |
4240 | /* |
4241 | * Return this if we need to call truncate_block for the last bit of the | |
4242 | * truncate. | |
4243 | */ | |
4244 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4245 | |
39279cc3 CM |
4246 | /* |
4247 | * this can truncate away extent items, csum items and directory items. | |
4248 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4249 | * any higher than new_size |
39279cc3 CM |
4250 | * |
4251 | * csum items that cross the new i_size are truncated to the new size | |
4252 | * as well. | |
7b128766 JB |
4253 | * |
4254 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4255 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4256 | */ |
8082510e YZ |
4257 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4258 | struct btrfs_root *root, | |
4259 | struct inode *inode, | |
4260 | u64 new_size, u32 min_type) | |
39279cc3 | 4261 | { |
0b246afa | 4262 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4263 | struct btrfs_path *path; |
5f39d397 | 4264 | struct extent_buffer *leaf; |
39279cc3 | 4265 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4266 | struct btrfs_key key; |
4267 | struct btrfs_key found_key; | |
39279cc3 | 4268 | u64 extent_start = 0; |
db94535d | 4269 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4270 | u64 extent_offset = 0; |
39279cc3 | 4271 | u64 item_end = 0; |
c1aa4575 | 4272 | u64 last_size = new_size; |
8082510e | 4273 | u32 found_type = (u8)-1; |
39279cc3 CM |
4274 | int found_extent; |
4275 | int del_item; | |
85e21bac CM |
4276 | int pending_del_nr = 0; |
4277 | int pending_del_slot = 0; | |
179e29e4 | 4278 | int extent_type = -1; |
8082510e | 4279 | int ret; |
4a0cc7ca | 4280 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4281 | u64 bytes_deleted = 0; |
897ca819 TM |
4282 | bool be_nice = false; |
4283 | bool should_throttle = false; | |
28553fa9 FM |
4284 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
4285 | struct extent_state *cached_state = NULL; | |
8082510e YZ |
4286 | |
4287 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4288 | |
28ed1345 | 4289 | /* |
92a7cc42 QW |
4290 | * For non-free space inodes and non-shareable roots, we want to back |
4291 | * off from time to time. This means all inodes in subvolume roots, | |
4292 | * reloc roots, and data reloc roots. | |
28ed1345 | 4293 | */ |
70ddc553 | 4294 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
92a7cc42 | 4295 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
897ca819 | 4296 | be_nice = true; |
28ed1345 | 4297 | |
0eb0e19c MF |
4298 | path = btrfs_alloc_path(); |
4299 | if (!path) | |
4300 | return -ENOMEM; | |
e4058b54 | 4301 | path->reada = READA_BACK; |
0eb0e19c | 4302 | |
82028e0a | 4303 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
a5ae50de FM |
4304 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
4305 | &cached_state); | |
28553fa9 | 4306 | |
82028e0a QW |
4307 | /* |
4308 | * We want to drop from the next block forward in case this | |
4309 | * new size is not block aligned since we will be keeping the | |
4310 | * last block of the extent just the way it is. | |
4311 | */ | |
dcdbc059 | 4312 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4313 | fs_info->sectorsize), |
da17066c | 4314 | (u64)-1, 0); |
82028e0a | 4315 | } |
8082510e | 4316 | |
16cdcec7 MX |
4317 | /* |
4318 | * This function is also used to drop the items in the log tree before | |
4319 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
52042d8e | 4320 | * it is used to drop the logged items. So we shouldn't kill the delayed |
16cdcec7 MX |
4321 | * items. |
4322 | */ | |
4323 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4324 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4325 | |
33345d01 | 4326 | key.objectid = ino; |
39279cc3 | 4327 | key.offset = (u64)-1; |
5f39d397 CM |
4328 | key.type = (u8)-1; |
4329 | ||
85e21bac | 4330 | search_again: |
28ed1345 CM |
4331 | /* |
4332 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4333 | * up a huge file in a single leaf. Most of the time that | |
4334 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4335 | */ | |
fd86a3a3 OS |
4336 | if (be_nice && bytes_deleted > SZ_32M && |
4337 | btrfs_should_end_transaction(trans)) { | |
4338 | ret = -EAGAIN; | |
4339 | goto out; | |
28ed1345 CM |
4340 | } |
4341 | ||
85e21bac | 4342 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
fd86a3a3 | 4343 | if (ret < 0) |
8082510e | 4344 | goto out; |
d397712b | 4345 | |
85e21bac | 4346 | if (ret > 0) { |
fd86a3a3 | 4347 | ret = 0; |
e02119d5 CM |
4348 | /* there are no items in the tree for us to truncate, we're |
4349 | * done | |
4350 | */ | |
8082510e YZ |
4351 | if (path->slots[0] == 0) |
4352 | goto out; | |
85e21bac CM |
4353 | path->slots[0]--; |
4354 | } | |
4355 | ||
d397712b | 4356 | while (1) { |
9ddc959e JB |
4357 | u64 clear_start = 0, clear_len = 0; |
4358 | ||
39279cc3 | 4359 | fi = NULL; |
5f39d397 CM |
4360 | leaf = path->nodes[0]; |
4361 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4362 | found_type = found_key.type; |
39279cc3 | 4363 | |
33345d01 | 4364 | if (found_key.objectid != ino) |
39279cc3 | 4365 | break; |
5f39d397 | 4366 | |
85e21bac | 4367 | if (found_type < min_type) |
39279cc3 CM |
4368 | break; |
4369 | ||
5f39d397 | 4370 | item_end = found_key.offset; |
39279cc3 | 4371 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4372 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4373 | struct btrfs_file_extent_item); |
179e29e4 CM |
4374 | extent_type = btrfs_file_extent_type(leaf, fi); |
4375 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4376 | item_end += |
db94535d | 4377 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4378 | |
4379 | trace_btrfs_truncate_show_fi_regular( | |
4380 | BTRFS_I(inode), leaf, fi, | |
4381 | found_key.offset); | |
179e29e4 | 4382 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 QW |
4383 | item_end += btrfs_file_extent_ram_bytes(leaf, |
4384 | fi); | |
09ed2f16 LB |
4385 | |
4386 | trace_btrfs_truncate_show_fi_inline( | |
4387 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4388 | found_key.offset); | |
39279cc3 | 4389 | } |
008630c1 | 4390 | item_end--; |
39279cc3 | 4391 | } |
8082510e YZ |
4392 | if (found_type > min_type) { |
4393 | del_item = 1; | |
4394 | } else { | |
76b42abb | 4395 | if (item_end < new_size) |
b888db2b | 4396 | break; |
8082510e YZ |
4397 | if (found_key.offset >= new_size) |
4398 | del_item = 1; | |
4399 | else | |
4400 | del_item = 0; | |
39279cc3 | 4401 | } |
39279cc3 | 4402 | found_extent = 0; |
39279cc3 | 4403 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4404 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4405 | goto delete; | |
4406 | ||
4407 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4408 | u64 num_dec; |
9ddc959e JB |
4409 | |
4410 | clear_start = found_key.offset; | |
db94535d | 4411 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4412 | if (!del_item) { |
db94535d CM |
4413 | u64 orig_num_bytes = |
4414 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4415 | extent_num_bytes = ALIGN(new_size - |
4416 | found_key.offset, | |
0b246afa | 4417 | fs_info->sectorsize); |
9ddc959e | 4418 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
db94535d CM |
4419 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4420 | extent_num_bytes); | |
4421 | num_dec = (orig_num_bytes - | |
9069218d | 4422 | extent_num_bytes); |
92a7cc42 | 4423 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 MX |
4424 | &root->state) && |
4425 | extent_start != 0) | |
a76a3cd4 | 4426 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4427 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4428 | } else { |
db94535d CM |
4429 | extent_num_bytes = |
4430 | btrfs_file_extent_disk_num_bytes(leaf, | |
4431 | fi); | |
5d4f98a2 YZ |
4432 | extent_offset = found_key.offset - |
4433 | btrfs_file_extent_offset(leaf, fi); | |
4434 | ||
39279cc3 | 4435 | /* FIXME blocksize != 4096 */ |
9069218d | 4436 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4437 | if (extent_start != 0) { |
4438 | found_extent = 1; | |
92a7cc42 | 4439 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 | 4440 | &root->state)) |
a76a3cd4 | 4441 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4442 | } |
39279cc3 | 4443 | } |
9ddc959e | 4444 | clear_len = num_dec; |
9069218d | 4445 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4446 | /* |
4447 | * we can't truncate inline items that have had | |
4448 | * special encodings | |
4449 | */ | |
4450 | if (!del_item && | |
c8b97818 | 4451 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4452 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4453 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4454 | u32 size = (u32)(new_size - found_key.offset); | |
4455 | ||
4456 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4457 | size = btrfs_file_extent_calc_inline_size(size); | |
78ac4f9e | 4458 | btrfs_truncate_item(path, size, 1); |
ddfae63c | 4459 | } else if (!del_item) { |
514ac8ad | 4460 | /* |
ddfae63c JB |
4461 | * We have to bail so the last_size is set to |
4462 | * just before this extent. | |
514ac8ad | 4463 | */ |
fd86a3a3 | 4464 | ret = NEED_TRUNCATE_BLOCK; |
ddfae63c | 4465 | break; |
9ddc959e JB |
4466 | } else { |
4467 | /* | |
4468 | * Inline extents are special, we just treat | |
4469 | * them as a full sector worth in the file | |
4470 | * extent tree just for simplicity sake. | |
4471 | */ | |
4472 | clear_len = fs_info->sectorsize; | |
ddfae63c | 4473 | } |
0305cd5f | 4474 | |
92a7cc42 | 4475 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
0305cd5f | 4476 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4477 | } |
179e29e4 | 4478 | delete: |
9ddc959e JB |
4479 | /* |
4480 | * We use btrfs_truncate_inode_items() to clean up log trees for | |
4481 | * multiple fsyncs, and in this case we don't want to clear the | |
4482 | * file extent range because it's just the log. | |
4483 | */ | |
4484 | if (root == BTRFS_I(inode)->root) { | |
4485 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), | |
4486 | clear_start, clear_len); | |
4487 | if (ret) { | |
4488 | btrfs_abort_transaction(trans, ret); | |
4489 | break; | |
4490 | } | |
4491 | } | |
4492 | ||
ddfae63c JB |
4493 | if (del_item) |
4494 | last_size = found_key.offset; | |
4495 | else | |
4496 | last_size = new_size; | |
39279cc3 | 4497 | if (del_item) { |
85e21bac CM |
4498 | if (!pending_del_nr) { |
4499 | /* no pending yet, add ourselves */ | |
4500 | pending_del_slot = path->slots[0]; | |
4501 | pending_del_nr = 1; | |
4502 | } else if (pending_del_nr && | |
4503 | path->slots[0] + 1 == pending_del_slot) { | |
4504 | /* hop on the pending chunk */ | |
4505 | pending_del_nr++; | |
4506 | pending_del_slot = path->slots[0]; | |
4507 | } else { | |
d397712b | 4508 | BUG(); |
85e21bac | 4509 | } |
39279cc3 CM |
4510 | } else { |
4511 | break; | |
4512 | } | |
897ca819 | 4513 | should_throttle = false; |
28f75a0e | 4514 | |
27cdeb70 | 4515 | if (found_extent && |
82028e0a | 4516 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
ffd4bb2a QW |
4517 | struct btrfs_ref ref = { 0 }; |
4518 | ||
28ed1345 | 4519 | bytes_deleted += extent_num_bytes; |
ffd4bb2a QW |
4520 | |
4521 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, | |
4522 | extent_start, extent_num_bytes, 0); | |
4523 | ref.real_root = root->root_key.objectid; | |
4524 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
4525 | ino, extent_offset); | |
4526 | ret = btrfs_free_extent(trans, &ref); | |
05522109 OS |
4527 | if (ret) { |
4528 | btrfs_abort_transaction(trans, ret); | |
4529 | break; | |
4530 | } | |
28f75a0e | 4531 | if (be_nice) { |
7c861627 | 4532 | if (btrfs_should_throttle_delayed_refs(trans)) |
897ca819 | 4533 | should_throttle = true; |
28f75a0e | 4534 | } |
39279cc3 | 4535 | } |
85e21bac | 4536 | |
8082510e YZ |
4537 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4538 | break; | |
4539 | ||
4540 | if (path->slots[0] == 0 || | |
1262133b | 4541 | path->slots[0] != pending_del_slot || |
28bad212 | 4542 | should_throttle) { |
8082510e YZ |
4543 | if (pending_del_nr) { |
4544 | ret = btrfs_del_items(trans, root, path, | |
4545 | pending_del_slot, | |
4546 | pending_del_nr); | |
79787eaa | 4547 | if (ret) { |
66642832 | 4548 | btrfs_abort_transaction(trans, ret); |
fd86a3a3 | 4549 | break; |
79787eaa | 4550 | } |
8082510e YZ |
4551 | pending_del_nr = 0; |
4552 | } | |
b3b4aa74 | 4553 | btrfs_release_path(path); |
28bad212 | 4554 | |
28f75a0e | 4555 | /* |
28bad212 JB |
4556 | * We can generate a lot of delayed refs, so we need to |
4557 | * throttle every once and a while and make sure we're | |
4558 | * adding enough space to keep up with the work we are | |
4559 | * generating. Since we hold a transaction here we | |
4560 | * can't flush, and we don't want to FLUSH_LIMIT because | |
4561 | * we could have generated too many delayed refs to | |
4562 | * actually allocate, so just bail if we're short and | |
4563 | * let the normal reservation dance happen higher up. | |
28f75a0e | 4564 | */ |
28bad212 JB |
4565 | if (should_throttle) { |
4566 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
4567 | BTRFS_RESERVE_NO_FLUSH); | |
4568 | if (ret) { | |
4569 | ret = -EAGAIN; | |
4570 | break; | |
4571 | } | |
28f75a0e | 4572 | } |
85e21bac | 4573 | goto search_again; |
8082510e YZ |
4574 | } else { |
4575 | path->slots[0]--; | |
85e21bac | 4576 | } |
39279cc3 | 4577 | } |
8082510e | 4578 | out: |
fd86a3a3 OS |
4579 | if (ret >= 0 && pending_del_nr) { |
4580 | int err; | |
4581 | ||
4582 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
85e21bac | 4583 | pending_del_nr); |
fd86a3a3 OS |
4584 | if (err) { |
4585 | btrfs_abort_transaction(trans, err); | |
4586 | ret = err; | |
4587 | } | |
85e21bac | 4588 | } |
76b42abb FM |
4589 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4590 | ASSERT(last_size >= new_size); | |
fd86a3a3 | 4591 | if (!ret && last_size > new_size) |
76b42abb | 4592 | last_size = new_size; |
d923afe9 | 4593 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
a5ae50de FM |
4594 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
4595 | (u64)-1, &cached_state); | |
76b42abb | 4596 | } |
28ed1345 | 4597 | |
39279cc3 | 4598 | btrfs_free_path(path); |
fd86a3a3 | 4599 | return ret; |
39279cc3 CM |
4600 | } |
4601 | ||
4602 | /* | |
9703fefe | 4603 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4604 | * @inode - inode that we're zeroing |
4605 | * @from - the offset to start zeroing | |
4606 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4607 | * offset | |
4608 | * @front - zero up to the offset instead of from the offset on | |
4609 | * | |
9703fefe | 4610 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4611 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4612 | */ |
9703fefe | 4613 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4614 | int front) |
39279cc3 | 4615 | { |
0b246afa | 4616 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4617 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4618 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4619 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4620 | struct extent_state *cached_state = NULL; |
364ecf36 | 4621 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4622 | char *kaddr; |
6d4572a9 | 4623 | bool only_release_metadata = false; |
0b246afa | 4624 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4625 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4626 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4627 | struct page *page; |
3b16a4e3 | 4628 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4629 | size_t write_bytes = blocksize; |
39279cc3 | 4630 | int ret = 0; |
9703fefe CR |
4631 | u64 block_start; |
4632 | u64 block_end; | |
39279cc3 | 4633 | |
b03ebd99 NB |
4634 | if (IS_ALIGNED(offset, blocksize) && |
4635 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4636 | goto out; |
9703fefe | 4637 | |
8b62f87b JB |
4638 | block_start = round_down(from, blocksize); |
4639 | block_end = block_start + blocksize - 1; | |
4640 | ||
36ea6f3e NB |
4641 | ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, |
4642 | block_start, blocksize); | |
6d4572a9 | 4643 | if (ret < 0) { |
38d37aa9 QW |
4644 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
4645 | &write_bytes) > 0) { | |
6d4572a9 QW |
4646 | /* For nocow case, no need to reserve data space */ |
4647 | only_release_metadata = true; | |
4648 | } else { | |
4649 | goto out; | |
4650 | } | |
4651 | } | |
4652 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); | |
4653 | if (ret < 0) { | |
4654 | if (!only_release_metadata) | |
25ce28ca NB |
4655 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
4656 | data_reserved, block_start, blocksize); | |
6d4572a9 QW |
4657 | goto out; |
4658 | } | |
211c17f5 | 4659 | again: |
3b16a4e3 | 4660 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4661 | if (!page) { |
86d52921 | 4662 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
43b18595 | 4663 | block_start, blocksize, true); |
8702ba93 | 4664 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
ac6a2b36 | 4665 | ret = -ENOMEM; |
39279cc3 | 4666 | goto out; |
5d5e103a | 4667 | } |
e6dcd2dc | 4668 | |
39279cc3 | 4669 | if (!PageUptodate(page)) { |
9ebefb18 | 4670 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4671 | lock_page(page); |
211c17f5 CM |
4672 | if (page->mapping != mapping) { |
4673 | unlock_page(page); | |
09cbfeaf | 4674 | put_page(page); |
211c17f5 CM |
4675 | goto again; |
4676 | } | |
39279cc3 CM |
4677 | if (!PageUptodate(page)) { |
4678 | ret = -EIO; | |
89642229 | 4679 | goto out_unlock; |
39279cc3 CM |
4680 | } |
4681 | } | |
211c17f5 | 4682 | wait_on_page_writeback(page); |
e6dcd2dc | 4683 | |
9703fefe | 4684 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4685 | set_page_extent_mapped(page); |
4686 | ||
c3504372 | 4687 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
e6dcd2dc | 4688 | if (ordered) { |
9703fefe | 4689 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4690 | &cached_state); |
e6dcd2dc | 4691 | unlock_page(page); |
09cbfeaf | 4692 | put_page(page); |
c0a43603 | 4693 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
4694 | btrfs_put_ordered_extent(ordered); |
4695 | goto again; | |
4696 | } | |
4697 | ||
9703fefe | 4698 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
e182163d OS |
4699 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
4700 | 0, 0, &cached_state); | |
5d5e103a | 4701 | |
c2566f22 | 4702 | ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0, |
330a5827 | 4703 | &cached_state); |
9ed74f2d | 4704 | if (ret) { |
9703fefe | 4705 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4706 | &cached_state); |
9ed74f2d JB |
4707 | goto out_unlock; |
4708 | } | |
4709 | ||
9703fefe | 4710 | if (offset != blocksize) { |
2aaa6655 | 4711 | if (!len) |
9703fefe | 4712 | len = blocksize - offset; |
e6dcd2dc | 4713 | kaddr = kmap(page); |
2aaa6655 | 4714 | if (front) |
9703fefe CR |
4715 | memset(kaddr + (block_start - page_offset(page)), |
4716 | 0, offset); | |
2aaa6655 | 4717 | else |
9703fefe CR |
4718 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4719 | 0, len); | |
e6dcd2dc CM |
4720 | flush_dcache_page(page); |
4721 | kunmap(page); | |
4722 | } | |
247e743c | 4723 | ClearPageChecked(page); |
e6dcd2dc | 4724 | set_page_dirty(page); |
e43bbe5e | 4725 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4726 | |
6d4572a9 QW |
4727 | if (only_release_metadata) |
4728 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, | |
3f6bb4ae | 4729 | block_end, EXTENT_NORESERVE, NULL, GFP_NOFS); |
6d4572a9 | 4730 | |
89642229 | 4731 | out_unlock: |
6d4572a9 QW |
4732 | if (ret) { |
4733 | if (only_release_metadata) | |
4734 | btrfs_delalloc_release_metadata(BTRFS_I(inode), | |
4735 | blocksize, true); | |
4736 | else | |
86d52921 | 4737 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
6d4572a9 QW |
4738 | block_start, blocksize, true); |
4739 | } | |
8702ba93 | 4740 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
39279cc3 | 4741 | unlock_page(page); |
09cbfeaf | 4742 | put_page(page); |
39279cc3 | 4743 | out: |
6d4572a9 | 4744 | if (only_release_metadata) |
38d37aa9 | 4745 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
364ecf36 | 4746 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4747 | return ret; |
4748 | } | |
4749 | ||
16e7549f JB |
4750 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4751 | u64 offset, u64 len) | |
4752 | { | |
0b246afa | 4753 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4754 | struct btrfs_trans_handle *trans; |
4755 | int ret; | |
4756 | ||
4757 | /* | |
4758 | * Still need to make sure the inode looks like it's been updated so | |
4759 | * that any holes get logged if we fsync. | |
4760 | */ | |
0b246afa JM |
4761 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4762 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4763 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4764 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4765 | return 0; | |
4766 | } | |
4767 | ||
4768 | /* | |
4769 | * 1 - for the one we're dropping | |
4770 | * 1 - for the one we're adding | |
4771 | * 1 - for updating the inode. | |
4772 | */ | |
4773 | trans = btrfs_start_transaction(root, 3); | |
4774 | if (IS_ERR(trans)) | |
4775 | return PTR_ERR(trans); | |
4776 | ||
4777 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4778 | if (ret) { | |
66642832 | 4779 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4780 | btrfs_end_transaction(trans); |
16e7549f JB |
4781 | return ret; |
4782 | } | |
4783 | ||
f85b7379 DS |
4784 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4785 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4786 | if (ret) |
66642832 | 4787 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4788 | else |
4789 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4790 | btrfs_end_transaction(trans); |
16e7549f JB |
4791 | return ret; |
4792 | } | |
4793 | ||
695a0d0d JB |
4794 | /* |
4795 | * This function puts in dummy file extents for the area we're creating a hole | |
4796 | * for. So if we are truncating this file to a larger size we need to insert | |
4797 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4798 | * the range between oldsize and size | |
4799 | */ | |
a41ad394 | 4800 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4801 | { |
0b246afa | 4802 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4803 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4804 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4805 | struct extent_map *em = NULL; |
2ac55d41 | 4806 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4807 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4808 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4809 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4810 | u64 last_byte; |
4811 | u64 cur_offset; | |
4812 | u64 hole_size; | |
9ed74f2d | 4813 | int err = 0; |
39279cc3 | 4814 | |
a71754fc | 4815 | /* |
9703fefe CR |
4816 | * If our size started in the middle of a block we need to zero out the |
4817 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4818 | * expose stale data. |
4819 | */ | |
9703fefe | 4820 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4821 | if (err) |
4822 | return err; | |
4823 | ||
9036c102 YZ |
4824 | if (size <= hole_start) |
4825 | return 0; | |
4826 | ||
b272ae22 | 4827 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
23d31bd4 | 4828 | block_end - 1, &cached_state); |
9036c102 YZ |
4829 | cur_offset = hole_start; |
4830 | while (1) { | |
fc4f21b1 | 4831 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
39b07b5d | 4832 | block_end - cur_offset); |
79787eaa JM |
4833 | if (IS_ERR(em)) { |
4834 | err = PTR_ERR(em); | |
f2767956 | 4835 | em = NULL; |
79787eaa JM |
4836 | break; |
4837 | } | |
9036c102 | 4838 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4839 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4840 | hole_size = last_byte - cur_offset; |
4841 | ||
8082510e | 4842 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4843 | struct extent_map *hole_em; |
9ed74f2d | 4844 | |
16e7549f JB |
4845 | err = maybe_insert_hole(root, inode, cur_offset, |
4846 | hole_size); | |
4847 | if (err) | |
3893e33b | 4848 | break; |
9ddc959e JB |
4849 | |
4850 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4851 | cur_offset, hole_size); | |
4852 | if (err) | |
4853 | break; | |
4854 | ||
dcdbc059 | 4855 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4856 | cur_offset + hole_size - 1, 0); |
4857 | hole_em = alloc_extent_map(); | |
4858 | if (!hole_em) { | |
4859 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4860 | &BTRFS_I(inode)->runtime_flags); | |
4861 | goto next; | |
4862 | } | |
4863 | hole_em->start = cur_offset; | |
4864 | hole_em->len = hole_size; | |
4865 | hole_em->orig_start = cur_offset; | |
8082510e | 4866 | |
5dc562c5 JB |
4867 | hole_em->block_start = EXTENT_MAP_HOLE; |
4868 | hole_em->block_len = 0; | |
b4939680 | 4869 | hole_em->orig_block_len = 0; |
cc95bef6 | 4870 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 4871 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4872 | hole_em->generation = fs_info->generation; |
8082510e | 4873 | |
5dc562c5 JB |
4874 | while (1) { |
4875 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4876 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4877 | write_unlock(&em_tree->lock); |
4878 | if (err != -EEXIST) | |
4879 | break; | |
dcdbc059 NB |
4880 | btrfs_drop_extent_cache(BTRFS_I(inode), |
4881 | cur_offset, | |
5dc562c5 JB |
4882 | cur_offset + |
4883 | hole_size - 1, 0); | |
4884 | } | |
4885 | free_extent_map(hole_em); | |
9ddc959e JB |
4886 | } else { |
4887 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4888 | cur_offset, hole_size); | |
4889 | if (err) | |
4890 | break; | |
9036c102 | 4891 | } |
16e7549f | 4892 | next: |
9036c102 | 4893 | free_extent_map(em); |
a22285a6 | 4894 | em = NULL; |
9036c102 | 4895 | cur_offset = last_byte; |
8082510e | 4896 | if (cur_offset >= block_end) |
9036c102 YZ |
4897 | break; |
4898 | } | |
a22285a6 | 4899 | free_extent_map(em); |
e43bbe5e | 4900 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4901 | return err; |
4902 | } | |
39279cc3 | 4903 | |
3972f260 | 4904 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4905 | { |
f4a2f4c5 MX |
4906 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4907 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4908 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4909 | loff_t newsize = attr->ia_size; |
4910 | int mask = attr->ia_valid; | |
8082510e YZ |
4911 | int ret; |
4912 | ||
3972f260 ES |
4913 | /* |
4914 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4915 | * special case where we need to update the times despite not having | |
4916 | * these flags set. For all other operations the VFS set these flags | |
4917 | * explicitly if it wants a timestamp update. | |
4918 | */ | |
dff6efc3 CH |
4919 | if (newsize != oldsize) { |
4920 | inode_inc_iversion(inode); | |
4921 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4922 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 4923 | current_time(inode); |
dff6efc3 | 4924 | } |
3972f260 | 4925 | |
a41ad394 | 4926 | if (newsize > oldsize) { |
9ea24bbe | 4927 | /* |
ea14b57f | 4928 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
4929 | * This is to ensure the snapshot captures a fully consistent |
4930 | * state of this file - if the snapshot captures this expanding | |
4931 | * truncation, it must capture all writes that happened before | |
4932 | * this truncation. | |
4933 | */ | |
dcc3eb96 | 4934 | btrfs_drew_write_lock(&root->snapshot_lock); |
a41ad394 | 4935 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 4936 | if (ret) { |
dcc3eb96 | 4937 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 4938 | return ret; |
9ea24bbe | 4939 | } |
8082510e | 4940 | |
f4a2f4c5 | 4941 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 4942 | if (IS_ERR(trans)) { |
dcc3eb96 | 4943 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 4944 | return PTR_ERR(trans); |
9ea24bbe | 4945 | } |
f4a2f4c5 MX |
4946 | |
4947 | i_size_write(inode, newsize); | |
d923afe9 | 4948 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
27772b68 | 4949 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 4950 | ret = btrfs_update_inode(trans, root, inode); |
dcc3eb96 | 4951 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 4952 | btrfs_end_transaction(trans); |
a41ad394 | 4953 | } else { |
8082510e | 4954 | |
a41ad394 JB |
4955 | /* |
4956 | * We're truncating a file that used to have good data down to | |
1fd4033d NB |
4957 | * zero. Make sure any new writes to the file get on disk |
4958 | * on close. | |
a41ad394 JB |
4959 | */ |
4960 | if (newsize == 0) | |
1fd4033d | 4961 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
72ac3c0d | 4962 | &BTRFS_I(inode)->runtime_flags); |
8082510e | 4963 | |
a41ad394 | 4964 | truncate_setsize(inode, newsize); |
2e60a51e | 4965 | |
2e60a51e | 4966 | inode_dio_wait(inode); |
2e60a51e | 4967 | |
213e8c55 | 4968 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
4969 | if (ret && inode->i_nlink) { |
4970 | int err; | |
4971 | ||
4972 | /* | |
f7e9e8fc OS |
4973 | * Truncate failed, so fix up the in-memory size. We |
4974 | * adjusted disk_i_size down as we removed extents, so | |
4975 | * wait for disk_i_size to be stable and then update the | |
4976 | * in-memory size to match. | |
7f4f6e0a | 4977 | */ |
f7e9e8fc | 4978 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 4979 | if (err) |
f7e9e8fc OS |
4980 | return err; |
4981 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 4982 | } |
8082510e YZ |
4983 | } |
4984 | ||
a41ad394 | 4985 | return ret; |
8082510e YZ |
4986 | } |
4987 | ||
9036c102 YZ |
4988 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4989 | { | |
2b0143b5 | 4990 | struct inode *inode = d_inode(dentry); |
b83cc969 | 4991 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4992 | int err; |
39279cc3 | 4993 | |
b83cc969 LZ |
4994 | if (btrfs_root_readonly(root)) |
4995 | return -EROFS; | |
4996 | ||
31051c85 | 4997 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
4998 | if (err) |
4999 | return err; | |
2bf5a725 | 5000 | |
5a3f23d5 | 5001 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5002 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5003 | if (err) |
5004 | return err; | |
39279cc3 | 5005 | } |
9036c102 | 5006 | |
1025774c CH |
5007 | if (attr->ia_valid) { |
5008 | setattr_copy(inode, attr); | |
0c4d2d95 | 5009 | inode_inc_iversion(inode); |
22c44fe6 | 5010 | err = btrfs_dirty_inode(inode); |
1025774c | 5011 | |
22c44fe6 | 5012 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5013 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5014 | } |
33268eaf | 5015 | |
39279cc3 CM |
5016 | return err; |
5017 | } | |
61295eb8 | 5018 | |
131e404a FDBM |
5019 | /* |
5020 | * While truncating the inode pages during eviction, we get the VFS calling | |
5021 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5022 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5023 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5024 | * extent_state structures over and over, wasting lots of time. | |
5025 | * | |
5026 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5027 | * those expensive operations on a per page basis and do only the ordered io | |
5028 | * finishing, while we release here the extent_map and extent_state structures, | |
5029 | * without the excessive merging and splitting. | |
5030 | */ | |
5031 | static void evict_inode_truncate_pages(struct inode *inode) | |
5032 | { | |
5033 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5034 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5035 | struct rb_node *node; | |
5036 | ||
5037 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5038 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5039 | |
5040 | write_lock(&map_tree->lock); | |
07e1ce09 | 5041 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
5042 | struct extent_map *em; |
5043 | ||
07e1ce09 | 5044 | node = rb_first_cached(&map_tree->map); |
131e404a | 5045 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
5046 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5047 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5048 | remove_extent_mapping(map_tree, em); |
5049 | free_extent_map(em); | |
7064dd5c FM |
5050 | if (need_resched()) { |
5051 | write_unlock(&map_tree->lock); | |
5052 | cond_resched(); | |
5053 | write_lock(&map_tree->lock); | |
5054 | } | |
131e404a FDBM |
5055 | } |
5056 | write_unlock(&map_tree->lock); | |
5057 | ||
6ca07097 FM |
5058 | /* |
5059 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
5060 | * We can have ongoing bios started by readahead that have |
5061 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
5062 | * still in progress (unlocked the pages in the bio but did not yet |
5063 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5064 | * ranges can still be locked and eviction started because before |
5065 | * submitting those bios, which are executed by a separate task (work | |
5066 | * queue kthread), inode references (inode->i_count) were not taken | |
5067 | * (which would be dropped in the end io callback of each bio). | |
5068 | * Therefore here we effectively end up waiting for those bios and | |
5069 | * anyone else holding locked ranges without having bumped the inode's | |
5070 | * reference count - if we don't do it, when they access the inode's | |
5071 | * io_tree to unlock a range it may be too late, leading to an | |
5072 | * use-after-free issue. | |
5073 | */ | |
131e404a FDBM |
5074 | spin_lock(&io_tree->lock); |
5075 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5076 | struct extent_state *state; | |
5077 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5078 | u64 start; |
5079 | u64 end; | |
421f0922 | 5080 | unsigned state_flags; |
131e404a FDBM |
5081 | |
5082 | node = rb_first(&io_tree->state); | |
5083 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5084 | start = state->start; |
5085 | end = state->end; | |
421f0922 | 5086 | state_flags = state->state; |
131e404a FDBM |
5087 | spin_unlock(&io_tree->lock); |
5088 | ||
ff13db41 | 5089 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5090 | |
5091 | /* | |
5092 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5093 | * and its reserved space won't be freed by delayed_ref. | |
5094 | * So we need to free its reserved space here. | |
5095 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5096 | * | |
5097 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5098 | */ | |
421f0922 | 5099 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f NB |
5100 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
5101 | end - start + 1); | |
b9d0b389 | 5102 | |
6ca07097 | 5103 | clear_extent_bit(io_tree, start, end, |
e182163d OS |
5104 | EXTENT_LOCKED | EXTENT_DELALLOC | |
5105 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
5106 | &cached_state); | |
131e404a | 5107 | |
7064dd5c | 5108 | cond_resched(); |
131e404a FDBM |
5109 | spin_lock(&io_tree->lock); |
5110 | } | |
5111 | spin_unlock(&io_tree->lock); | |
5112 | } | |
5113 | ||
4b9d7b59 | 5114 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5115 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5116 | { |
5117 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5118 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
d3984c90 | 5119 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5120 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5121 | int ret; |
4b9d7b59 | 5122 | |
d3984c90 JB |
5123 | /* |
5124 | * Eviction should be taking place at some place safe because of our | |
5125 | * delayed iputs. However the normal flushing code will run delayed | |
5126 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5127 | * | |
5128 | * We reserve the delayed_refs_extra here again because we can't use | |
5129 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5130 | * above. We reserve our extra bit here because we generate a ton of | |
5131 | * delayed refs activity by truncating. | |
5132 | * | |
5133 | * If we cannot make our reservation we'll attempt to steal from the | |
5134 | * global reserve, because we really want to be able to free up space. | |
5135 | */ | |
5136 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, | |
5137 | BTRFS_RESERVE_FLUSH_EVICT); | |
5138 | if (ret) { | |
4b9d7b59 OS |
5139 | /* |
5140 | * Try to steal from the global reserve if there is space for | |
5141 | * it. | |
5142 | */ | |
d3984c90 JB |
5143 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
5144 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { | |
5145 | btrfs_warn(fs_info, | |
5146 | "could not allocate space for delete; will truncate on mount"); | |
5147 | return ERR_PTR(-ENOSPC); | |
5148 | } | |
5149 | delayed_refs_extra = 0; | |
5150 | } | |
4b9d7b59 | 5151 | |
d3984c90 JB |
5152 | trans = btrfs_join_transaction(root); |
5153 | if (IS_ERR(trans)) | |
5154 | return trans; | |
5155 | ||
5156 | if (delayed_refs_extra) { | |
5157 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5158 | trans->bytes_reserved = delayed_refs_extra; | |
5159 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5160 | delayed_refs_extra, 1); | |
4b9d7b59 | 5161 | } |
d3984c90 | 5162 | return trans; |
4b9d7b59 OS |
5163 | } |
5164 | ||
bd555975 | 5165 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5166 | { |
0b246afa | 5167 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5168 | struct btrfs_trans_handle *trans; |
5169 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5170 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5171 | int ret; |
5172 | ||
1abe9b8a | 5173 | trace_btrfs_inode_evict(inode); |
5174 | ||
3d48d981 | 5175 | if (!root) { |
e8f1bc14 | 5176 | clear_inode(inode); |
3d48d981 NB |
5177 | return; |
5178 | } | |
5179 | ||
131e404a FDBM |
5180 | evict_inode_truncate_pages(inode); |
5181 | ||
69e9c6c6 SB |
5182 | if (inode->i_nlink && |
5183 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5184 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5185 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5186 | goto no_delete; |
5187 | ||
27919067 | 5188 | if (is_bad_inode(inode)) |
39279cc3 | 5189 | goto no_delete; |
5f39d397 | 5190 | |
7ab7956e | 5191 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5192 | |
7b40b695 | 5193 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5194 | goto no_delete; |
c71bf099 | 5195 | |
76dda93c | 5196 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5197 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5198 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5199 | goto no_delete; |
5200 | } | |
5201 | ||
aa79021f | 5202 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5203 | if (ret) |
0e8c36a9 | 5204 | goto no_delete; |
0e8c36a9 | 5205 | |
2ff7e61e | 5206 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5207 | if (!rsv) |
4289a667 | 5208 | goto no_delete; |
2bd36e7b | 5209 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
ca7e70f5 | 5210 | rsv->failfast = 1; |
4289a667 | 5211 | |
6ef06d27 | 5212 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5213 | |
8082510e | 5214 | while (1) { |
ad80cf50 | 5215 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5216 | if (IS_ERR(trans)) |
5217 | goto free_rsv; | |
7b128766 | 5218 | |
4289a667 JB |
5219 | trans->block_rsv = rsv; |
5220 | ||
d68fc57b | 5221 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
27919067 OS |
5222 | trans->block_rsv = &fs_info->trans_block_rsv; |
5223 | btrfs_end_transaction(trans); | |
5224 | btrfs_btree_balance_dirty(fs_info); | |
5225 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5226 | goto free_rsv; | |
5227 | else if (!ret) | |
8082510e | 5228 | break; |
8082510e | 5229 | } |
5f39d397 | 5230 | |
4ef31a45 | 5231 | /* |
27919067 OS |
5232 | * Errors here aren't a big deal, it just means we leave orphan items in |
5233 | * the tree. They will be cleaned up on the next mount. If the inode | |
5234 | * number gets reused, cleanup deletes the orphan item without doing | |
5235 | * anything, and unlink reuses the existing orphan item. | |
5236 | * | |
5237 | * If it turns out that we are dropping too many of these, we might want | |
5238 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5239 | */ |
ad80cf50 | 5240 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5241 | if (!IS_ERR(trans)) { |
5242 | trans->block_rsv = rsv; | |
5243 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5244 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5245 | btrfs_end_transaction(trans); | |
5246 | } | |
54aa1f4d | 5247 | |
0b246afa | 5248 | if (!(root == fs_info->tree_root || |
581bb050 | 5249 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5250 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5251 | |
27919067 OS |
5252 | free_rsv: |
5253 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5254 | no_delete: |
27919067 OS |
5255 | /* |
5256 | * If we didn't successfully delete, the orphan item will still be in | |
5257 | * the tree and we'll retry on the next mount. Again, we might also want | |
5258 | * to retry these periodically in the future. | |
5259 | */ | |
f48d1cf5 | 5260 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5261 | clear_inode(inode); |
39279cc3 CM |
5262 | } |
5263 | ||
5264 | /* | |
6bf9e4bd QW |
5265 | * Return the key found in the dir entry in the location pointer, fill @type |
5266 | * with BTRFS_FT_*, and return 0. | |
5267 | * | |
005d6712 SY |
5268 | * If no dir entries were found, returns -ENOENT. |
5269 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5270 | */ |
5271 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5272 | struct btrfs_key *location, u8 *type) |
39279cc3 CM |
5273 | { |
5274 | const char *name = dentry->d_name.name; | |
5275 | int namelen = dentry->d_name.len; | |
5276 | struct btrfs_dir_item *di; | |
5277 | struct btrfs_path *path; | |
5278 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5279 | int ret = 0; |
39279cc3 CM |
5280 | |
5281 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5282 | if (!path) |
5283 | return -ENOMEM; | |
3954401f | 5284 | |
f85b7379 DS |
5285 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5286 | name, namelen, 0); | |
3cf5068f LB |
5287 | if (IS_ERR_OR_NULL(di)) { |
5288 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5289 | goto out; |
5290 | } | |
d397712b | 5291 | |
5f39d397 | 5292 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5293 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5294 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5295 | ret = -EUCLEAN; |
56a0e706 LB |
5296 | btrfs_warn(root->fs_info, |
5297 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5298 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5299 | location->objectid, location->type, location->offset); | |
56a0e706 | 5300 | } |
6bf9e4bd QW |
5301 | if (!ret) |
5302 | *type = btrfs_dir_type(path->nodes[0], di); | |
39279cc3 | 5303 | out: |
39279cc3 CM |
5304 | btrfs_free_path(path); |
5305 | return ret; | |
5306 | } | |
5307 | ||
5308 | /* | |
5309 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5310 | * needs to be changed to reflect the root directory of the tree root. This | |
5311 | * is kind of like crossing a mount point. | |
5312 | */ | |
2ff7e61e | 5313 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5314 | struct inode *dir, |
5315 | struct dentry *dentry, | |
5316 | struct btrfs_key *location, | |
5317 | struct btrfs_root **sub_root) | |
39279cc3 | 5318 | { |
4df27c4d YZ |
5319 | struct btrfs_path *path; |
5320 | struct btrfs_root *new_root; | |
5321 | struct btrfs_root_ref *ref; | |
5322 | struct extent_buffer *leaf; | |
1d4c08e0 | 5323 | struct btrfs_key key; |
4df27c4d YZ |
5324 | int ret; |
5325 | int err = 0; | |
39279cc3 | 5326 | |
4df27c4d YZ |
5327 | path = btrfs_alloc_path(); |
5328 | if (!path) { | |
5329 | err = -ENOMEM; | |
5330 | goto out; | |
5331 | } | |
39279cc3 | 5332 | |
4df27c4d | 5333 | err = -ENOENT; |
1d4c08e0 DS |
5334 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5335 | key.type = BTRFS_ROOT_REF_KEY; | |
5336 | key.offset = location->objectid; | |
5337 | ||
0b246afa | 5338 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5339 | if (ret) { |
5340 | if (ret < 0) | |
5341 | err = ret; | |
5342 | goto out; | |
5343 | } | |
39279cc3 | 5344 | |
4df27c4d YZ |
5345 | leaf = path->nodes[0]; |
5346 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5347 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5348 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5349 | goto out; | |
39279cc3 | 5350 | |
4df27c4d YZ |
5351 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5352 | (unsigned long)(ref + 1), | |
5353 | dentry->d_name.len); | |
5354 | if (ret) | |
5355 | goto out; | |
5356 | ||
b3b4aa74 | 5357 | btrfs_release_path(path); |
4df27c4d | 5358 | |
56e9357a | 5359 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5360 | if (IS_ERR(new_root)) { |
5361 | err = PTR_ERR(new_root); | |
5362 | goto out; | |
5363 | } | |
5364 | ||
4df27c4d YZ |
5365 | *sub_root = new_root; |
5366 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5367 | location->type = BTRFS_INODE_ITEM_KEY; | |
5368 | location->offset = 0; | |
5369 | err = 0; | |
5370 | out: | |
5371 | btrfs_free_path(path); | |
5372 | return err; | |
39279cc3 CM |
5373 | } |
5374 | ||
5d4f98a2 YZ |
5375 | static void inode_tree_add(struct inode *inode) |
5376 | { | |
5377 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5378 | struct btrfs_inode *entry; | |
03e860bd FNP |
5379 | struct rb_node **p; |
5380 | struct rb_node *parent; | |
cef21937 | 5381 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5382 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5383 | |
1d3382cb | 5384 | if (inode_unhashed(inode)) |
76dda93c | 5385 | return; |
e1409cef | 5386 | parent = NULL; |
5d4f98a2 | 5387 | spin_lock(&root->inode_lock); |
e1409cef | 5388 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5389 | while (*p) { |
5390 | parent = *p; | |
5391 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5392 | ||
37508515 | 5393 | if (ino < btrfs_ino(entry)) |
03e860bd | 5394 | p = &parent->rb_left; |
37508515 | 5395 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5396 | p = &parent->rb_right; |
5d4f98a2 YZ |
5397 | else { |
5398 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5399 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5400 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5401 | RB_CLEAR_NODE(parent); |
5402 | spin_unlock(&root->inode_lock); | |
cef21937 | 5403 | return; |
5d4f98a2 YZ |
5404 | } |
5405 | } | |
cef21937 FDBM |
5406 | rb_link_node(new, parent, p); |
5407 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5408 | spin_unlock(&root->inode_lock); |
5409 | } | |
5410 | ||
b79b7249 | 5411 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5412 | { |
b79b7249 | 5413 | struct btrfs_root *root = inode->root; |
76dda93c | 5414 | int empty = 0; |
5d4f98a2 | 5415 | |
03e860bd | 5416 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5417 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5418 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5419 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5420 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5421 | } |
03e860bd | 5422 | spin_unlock(&root->inode_lock); |
76dda93c | 5423 | |
69e9c6c6 | 5424 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5425 | spin_lock(&root->inode_lock); |
5426 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5427 | spin_unlock(&root->inode_lock); | |
5428 | if (empty) | |
5429 | btrfs_add_dead_root(root); | |
5430 | } | |
5431 | } | |
5432 | ||
5d4f98a2 | 5433 | |
e02119d5 CM |
5434 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5435 | { | |
5436 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5437 | |
5438 | inode->i_ino = args->ino; | |
5439 | BTRFS_I(inode)->location.objectid = args->ino; | |
5440 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5441 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5442 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5443 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
39279cc3 CM |
5444 | return 0; |
5445 | } | |
5446 | ||
5447 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5448 | { | |
5449 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5450 | |
5451 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5452 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5453 | } |
5454 | ||
0202e83f | 5455 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5456 | struct btrfs_root *root) |
39279cc3 CM |
5457 | { |
5458 | struct inode *inode; | |
5459 | struct btrfs_iget_args args; | |
0202e83f | 5460 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5461 | |
0202e83f | 5462 | args.ino = ino; |
39279cc3 CM |
5463 | args.root = root; |
5464 | ||
778ba82b | 5465 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5466 | btrfs_init_locked_inode, |
5467 | (void *)&args); | |
5468 | return inode; | |
5469 | } | |
5470 | ||
4c66e0d4 | 5471 | /* |
0202e83f | 5472 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5473 | * Path can be preallocated to prevent recursing back to iget through |
5474 | * allocator. NULL is also valid but may require an additional allocation | |
5475 | * later. | |
1a54ef8c | 5476 | */ |
0202e83f | 5477 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5478 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5479 | { |
5480 | struct inode *inode; | |
5481 | ||
0202e83f | 5482 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5483 | if (!inode) |
5d4f98a2 | 5484 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5485 | |
5486 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5487 | int ret; |
5488 | ||
4222ea71 | 5489 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5490 | if (!ret) { |
1748f843 MF |
5491 | inode_tree_add(inode); |
5492 | unlock_new_inode(inode); | |
1748f843 | 5493 | } else { |
f5b3a417 AV |
5494 | iget_failed(inode); |
5495 | /* | |
5496 | * ret > 0 can come from btrfs_search_slot called by | |
5497 | * btrfs_read_locked_inode, this means the inode item | |
5498 | * was not found. | |
5499 | */ | |
5500 | if (ret > 0) | |
5501 | ret = -ENOENT; | |
5502 | inode = ERR_PTR(ret); | |
1748f843 MF |
5503 | } |
5504 | } | |
5505 | ||
1a54ef8c BR |
5506 | return inode; |
5507 | } | |
5508 | ||
0202e83f | 5509 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5510 | { |
0202e83f | 5511 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5512 | } |
5513 | ||
4df27c4d YZ |
5514 | static struct inode *new_simple_dir(struct super_block *s, |
5515 | struct btrfs_key *key, | |
5516 | struct btrfs_root *root) | |
5517 | { | |
5518 | struct inode *inode = new_inode(s); | |
5519 | ||
5520 | if (!inode) | |
5521 | return ERR_PTR(-ENOMEM); | |
5522 | ||
5c8fd99f | 5523 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5524 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5525 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5526 | |
5527 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5528 | /* |
5529 | * We only need lookup, the rest is read-only and there's no inode | |
5530 | * associated with the dentry | |
5531 | */ | |
5532 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5533 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5534 | inode->i_fop = &simple_dir_operations; |
5535 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5536 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5537 | inode->i_atime = inode->i_mtime; |
5538 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5539 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5540 | |
5541 | return inode; | |
5542 | } | |
5543 | ||
6bf9e4bd QW |
5544 | static inline u8 btrfs_inode_type(struct inode *inode) |
5545 | { | |
5546 | /* | |
5547 | * Compile-time asserts that generic FT_* types still match | |
5548 | * BTRFS_FT_* types | |
5549 | */ | |
5550 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); | |
5551 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); | |
5552 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); | |
5553 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); | |
5554 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); | |
5555 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); | |
5556 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); | |
5557 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); | |
5558 | ||
5559 | return fs_umode_to_ftype(inode->i_mode); | |
5560 | } | |
5561 | ||
3de4586c | 5562 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5563 | { |
0b246afa | 5564 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5565 | struct inode *inode; |
4df27c4d | 5566 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5567 | struct btrfs_root *sub_root = root; |
5568 | struct btrfs_key location; | |
6bf9e4bd | 5569 | u8 di_type = 0; |
b4aff1f8 | 5570 | int ret = 0; |
39279cc3 CM |
5571 | |
5572 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5573 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5574 | |
6bf9e4bd | 5575 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5576 | if (ret < 0) |
5577 | return ERR_PTR(ret); | |
5f39d397 | 5578 | |
4df27c4d | 5579 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5580 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5581 | if (IS_ERR(inode)) |
5582 | return inode; | |
5583 | ||
5584 | /* Do extra check against inode mode with di_type */ | |
5585 | if (btrfs_inode_type(inode) != di_type) { | |
5586 | btrfs_crit(fs_info, | |
5587 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5588 | inode->i_mode, btrfs_inode_type(inode), | |
5589 | di_type); | |
5590 | iput(inode); | |
5591 | return ERR_PTR(-EUCLEAN); | |
5592 | } | |
4df27c4d YZ |
5593 | return inode; |
5594 | } | |
5595 | ||
2ff7e61e | 5596 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5597 | &location, &sub_root); |
5598 | if (ret < 0) { | |
5599 | if (ret != -ENOENT) | |
5600 | inode = ERR_PTR(ret); | |
5601 | else | |
5602 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5603 | } else { | |
0202e83f | 5604 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
39279cc3 | 5605 | } |
8727002f | 5606 | if (root != sub_root) |
00246528 | 5607 | btrfs_put_root(sub_root); |
76dda93c | 5608 | |
34d19bad | 5609 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5610 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5611 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5612 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5613 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5614 | if (ret) { |
5615 | iput(inode); | |
66b4ffd1 | 5616 | inode = ERR_PTR(ret); |
01cd3367 | 5617 | } |
c71bf099 YZ |
5618 | } |
5619 | ||
3de4586c CM |
5620 | return inode; |
5621 | } | |
5622 | ||
fe15ce44 | 5623 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5624 | { |
5625 | struct btrfs_root *root; | |
2b0143b5 | 5626 | struct inode *inode = d_inode(dentry); |
76dda93c | 5627 | |
848cce0d | 5628 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5629 | inode = d_inode(dentry->d_parent); |
76dda93c | 5630 | |
848cce0d LZ |
5631 | if (inode) { |
5632 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5633 | if (btrfs_root_refs(&root->root_item) == 0) |
5634 | return 1; | |
848cce0d | 5635 | |
4a0cc7ca | 5636 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5637 | return 1; |
efefb143 | 5638 | } |
76dda93c YZ |
5639 | return 0; |
5640 | } | |
5641 | ||
3de4586c | 5642 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5643 | unsigned int flags) |
3de4586c | 5644 | { |
3837d208 | 5645 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5646 | |
3837d208 AV |
5647 | if (inode == ERR_PTR(-ENOENT)) |
5648 | inode = NULL; | |
41d28bca | 5649 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5650 | } |
5651 | ||
23b5ec74 JB |
5652 | /* |
5653 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5654 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5655 | * our information into that, and then dir_emit from the buffer. This is | |
5656 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5657 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5658 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5659 | * tree lock. | |
5660 | */ | |
5661 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5662 | { | |
5663 | struct btrfs_file_private *private; | |
5664 | ||
5665 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5666 | if (!private) | |
5667 | return -ENOMEM; | |
5668 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5669 | if (!private->filldir_buf) { | |
5670 | kfree(private); | |
5671 | return -ENOMEM; | |
5672 | } | |
5673 | file->private_data = private; | |
5674 | return 0; | |
5675 | } | |
5676 | ||
5677 | struct dir_entry { | |
5678 | u64 ino; | |
5679 | u64 offset; | |
5680 | unsigned type; | |
5681 | int name_len; | |
5682 | }; | |
5683 | ||
5684 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5685 | { | |
5686 | while (entries--) { | |
5687 | struct dir_entry *entry = addr; | |
5688 | char *name = (char *)(entry + 1); | |
5689 | ||
92d32170 DS |
5690 | ctx->pos = get_unaligned(&entry->offset); |
5691 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5692 | get_unaligned(&entry->ino), | |
5693 | get_unaligned(&entry->type))) | |
23b5ec74 | 5694 | return 1; |
92d32170 DS |
5695 | addr += sizeof(struct dir_entry) + |
5696 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5697 | ctx->pos++; |
5698 | } | |
5699 | return 0; | |
5700 | } | |
5701 | ||
9cdda8d3 | 5702 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5703 | { |
9cdda8d3 | 5704 | struct inode *inode = file_inode(file); |
39279cc3 | 5705 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5706 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5707 | struct btrfs_dir_item *di; |
5708 | struct btrfs_key key; | |
5f39d397 | 5709 | struct btrfs_key found_key; |
39279cc3 | 5710 | struct btrfs_path *path; |
23b5ec74 | 5711 | void *addr; |
16cdcec7 MX |
5712 | struct list_head ins_list; |
5713 | struct list_head del_list; | |
39279cc3 | 5714 | int ret; |
5f39d397 | 5715 | struct extent_buffer *leaf; |
39279cc3 | 5716 | int slot; |
5f39d397 CM |
5717 | char *name_ptr; |
5718 | int name_len; | |
23b5ec74 JB |
5719 | int entries = 0; |
5720 | int total_len = 0; | |
02dbfc99 | 5721 | bool put = false; |
c2951f32 | 5722 | struct btrfs_key location; |
5f39d397 | 5723 | |
9cdda8d3 AV |
5724 | if (!dir_emit_dots(file, ctx)) |
5725 | return 0; | |
5726 | ||
49593bfa | 5727 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5728 | if (!path) |
5729 | return -ENOMEM; | |
ff5714cc | 5730 | |
23b5ec74 | 5731 | addr = private->filldir_buf; |
e4058b54 | 5732 | path->reada = READA_FORWARD; |
49593bfa | 5733 | |
c2951f32 JM |
5734 | INIT_LIST_HEAD(&ins_list); |
5735 | INIT_LIST_HEAD(&del_list); | |
5736 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5737 | |
23b5ec74 | 5738 | again: |
c2951f32 | 5739 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5740 | key.offset = ctx->pos; |
4a0cc7ca | 5741 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5742 | |
39279cc3 CM |
5743 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5744 | if (ret < 0) | |
5745 | goto err; | |
49593bfa DW |
5746 | |
5747 | while (1) { | |
23b5ec74 JB |
5748 | struct dir_entry *entry; |
5749 | ||
5f39d397 | 5750 | leaf = path->nodes[0]; |
39279cc3 | 5751 | slot = path->slots[0]; |
b9e03af0 LZ |
5752 | if (slot >= btrfs_header_nritems(leaf)) { |
5753 | ret = btrfs_next_leaf(root, path); | |
5754 | if (ret < 0) | |
5755 | goto err; | |
5756 | else if (ret > 0) | |
5757 | break; | |
5758 | continue; | |
39279cc3 | 5759 | } |
3de4586c | 5760 | |
5f39d397 CM |
5761 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5762 | ||
5763 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5764 | break; |
c2951f32 | 5765 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5766 | break; |
9cdda8d3 | 5767 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5768 | goto next; |
c2951f32 | 5769 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5770 | goto next; |
39279cc3 | 5771 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 5772 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5773 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5774 | PAGE_SIZE) { | |
5775 | btrfs_release_path(path); | |
5776 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5777 | if (ret) | |
5778 | goto nopos; | |
5779 | addr = private->filldir_buf; | |
5780 | entries = 0; | |
5781 | total_len = 0; | |
5782 | goto again; | |
c2951f32 | 5783 | } |
23b5ec74 JB |
5784 | |
5785 | entry = addr; | |
92d32170 | 5786 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 5787 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
5788 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5789 | name_len); | |
7d157c3d | 5790 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
92d32170 | 5791 | &entry->type); |
c2951f32 | 5792 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5793 | put_unaligned(location.objectid, &entry->ino); |
5794 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5795 | entries++; |
5796 | addr += sizeof(struct dir_entry) + name_len; | |
5797 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5798 | next: |
5799 | path->slots[0]++; | |
39279cc3 | 5800 | } |
23b5ec74 JB |
5801 | btrfs_release_path(path); |
5802 | ||
5803 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5804 | if (ret) | |
5805 | goto nopos; | |
49593bfa | 5806 | |
d2fbb2b5 | 5807 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5808 | if (ret) |
bc4ef759 DS |
5809 | goto nopos; |
5810 | ||
db62efbb ZB |
5811 | /* |
5812 | * Stop new entries from being returned after we return the last | |
5813 | * entry. | |
5814 | * | |
5815 | * New directory entries are assigned a strictly increasing | |
5816 | * offset. This means that new entries created during readdir | |
5817 | * are *guaranteed* to be seen in the future by that readdir. | |
5818 | * This has broken buggy programs which operate on names as | |
5819 | * they're returned by readdir. Until we re-use freed offsets | |
5820 | * we have this hack to stop new entries from being returned | |
5821 | * under the assumption that they'll never reach this huge | |
5822 | * offset. | |
5823 | * | |
5824 | * This is being careful not to overflow 32bit loff_t unless the | |
5825 | * last entry requires it because doing so has broken 32bit apps | |
5826 | * in the past. | |
5827 | */ | |
c2951f32 JM |
5828 | if (ctx->pos >= INT_MAX) |
5829 | ctx->pos = LLONG_MAX; | |
5830 | else | |
5831 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5832 | nopos: |
5833 | ret = 0; | |
5834 | err: | |
02dbfc99 OS |
5835 | if (put) |
5836 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5837 | btrfs_free_path(path); |
39279cc3 CM |
5838 | return ret; |
5839 | } | |
5840 | ||
39279cc3 | 5841 | /* |
54aa1f4d | 5842 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5843 | * inode changes. But, it is most likely to find the inode in cache. |
5844 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5845 | * to keep or drop this code. | |
5846 | */ | |
48a3b636 | 5847 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 5848 | { |
2ff7e61e | 5849 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5850 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5851 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5852 | int ret; |
5853 | ||
72ac3c0d | 5854 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5855 | return 0; |
39279cc3 | 5856 | |
7a7eaa40 | 5857 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5858 | if (IS_ERR(trans)) |
5859 | return PTR_ERR(trans); | |
8929ecfa YZ |
5860 | |
5861 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5862 | if (ret && ret == -ENOSPC) { |
5863 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 5864 | btrfs_end_transaction(trans); |
94b60442 | 5865 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
5866 | if (IS_ERR(trans)) |
5867 | return PTR_ERR(trans); | |
8929ecfa | 5868 | |
94b60442 | 5869 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5870 | } |
3a45bb20 | 5871 | btrfs_end_transaction(trans); |
16cdcec7 | 5872 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 5873 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
5874 | |
5875 | return ret; | |
5876 | } | |
5877 | ||
5878 | /* | |
5879 | * This is a copy of file_update_time. We need this so we can return error on | |
5880 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5881 | */ | |
95582b00 | 5882 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 5883 | int flags) |
22c44fe6 | 5884 | { |
2bc55652 | 5885 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 5886 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
5887 | |
5888 | if (btrfs_root_readonly(root)) | |
5889 | return -EROFS; | |
5890 | ||
e41f941a | 5891 | if (flags & S_VERSION) |
3a8c7231 | 5892 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
5893 | if (flags & S_CTIME) |
5894 | inode->i_ctime = *now; | |
5895 | if (flags & S_MTIME) | |
5896 | inode->i_mtime = *now; | |
5897 | if (flags & S_ATIME) | |
5898 | inode->i_atime = *now; | |
3a8c7231 | 5899 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
5900 | } |
5901 | ||
d352ac68 CM |
5902 | /* |
5903 | * find the highest existing sequence number in a directory | |
5904 | * and then set the in-memory index_cnt variable to reflect | |
5905 | * free sequence numbers | |
5906 | */ | |
4c570655 | 5907 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 5908 | { |
4c570655 | 5909 | struct btrfs_root *root = inode->root; |
aec7477b JB |
5910 | struct btrfs_key key, found_key; |
5911 | struct btrfs_path *path; | |
5912 | struct extent_buffer *leaf; | |
5913 | int ret; | |
5914 | ||
4c570655 | 5915 | key.objectid = btrfs_ino(inode); |
962a298f | 5916 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5917 | key.offset = (u64)-1; |
5918 | ||
5919 | path = btrfs_alloc_path(); | |
5920 | if (!path) | |
5921 | return -ENOMEM; | |
5922 | ||
5923 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5924 | if (ret < 0) | |
5925 | goto out; | |
5926 | /* FIXME: we should be able to handle this */ | |
5927 | if (ret == 0) | |
5928 | goto out; | |
5929 | ret = 0; | |
5930 | ||
5931 | /* | |
5932 | * MAGIC NUMBER EXPLANATION: | |
5933 | * since we search a directory based on f_pos we have to start at 2 | |
5934 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5935 | * else has to start at 2 | |
5936 | */ | |
5937 | if (path->slots[0] == 0) { | |
4c570655 | 5938 | inode->index_cnt = 2; |
aec7477b JB |
5939 | goto out; |
5940 | } | |
5941 | ||
5942 | path->slots[0]--; | |
5943 | ||
5944 | leaf = path->nodes[0]; | |
5945 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5946 | ||
4c570655 | 5947 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5948 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 5949 | inode->index_cnt = 2; |
aec7477b JB |
5950 | goto out; |
5951 | } | |
5952 | ||
4c570655 | 5953 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
5954 | out: |
5955 | btrfs_free_path(path); | |
5956 | return ret; | |
5957 | } | |
5958 | ||
d352ac68 CM |
5959 | /* |
5960 | * helper to find a free sequence number in a given directory. This current | |
5961 | * code is very simple, later versions will do smarter things in the btree | |
5962 | */ | |
877574e2 | 5963 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
5964 | { |
5965 | int ret = 0; | |
5966 | ||
877574e2 NB |
5967 | if (dir->index_cnt == (u64)-1) { |
5968 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
5969 | if (ret) { |
5970 | ret = btrfs_set_inode_index_count(dir); | |
5971 | if (ret) | |
5972 | return ret; | |
5973 | } | |
aec7477b JB |
5974 | } |
5975 | ||
877574e2 NB |
5976 | *index = dir->index_cnt; |
5977 | dir->index_cnt++; | |
aec7477b JB |
5978 | |
5979 | return ret; | |
5980 | } | |
5981 | ||
b0d5d10f CM |
5982 | static int btrfs_insert_inode_locked(struct inode *inode) |
5983 | { | |
5984 | struct btrfs_iget_args args; | |
0202e83f DS |
5985 | |
5986 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
5987 | args.root = BTRFS_I(inode)->root; |
5988 | ||
5989 | return insert_inode_locked4(inode, | |
5990 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5991 | btrfs_find_actor, &args); | |
5992 | } | |
5993 | ||
19aee8de AJ |
5994 | /* |
5995 | * Inherit flags from the parent inode. | |
5996 | * | |
5997 | * Currently only the compression flags and the cow flags are inherited. | |
5998 | */ | |
5999 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6000 | { | |
6001 | unsigned int flags; | |
6002 | ||
6003 | if (!dir) | |
6004 | return; | |
6005 | ||
6006 | flags = BTRFS_I(dir)->flags; | |
6007 | ||
6008 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6009 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6010 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6011 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6012 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6013 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6014 | } | |
6015 | ||
6016 | if (flags & BTRFS_INODE_NODATACOW) { | |
6017 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6018 | if (S_ISREG(inode->i_mode)) | |
6019 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6020 | } | |
6021 | ||
7b6a221e | 6022 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
6023 | } |
6024 | ||
39279cc3 CM |
6025 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6026 | struct btrfs_root *root, | |
aec7477b | 6027 | struct inode *dir, |
9c58309d | 6028 | const char *name, int name_len, |
175a4eb7 AV |
6029 | u64 ref_objectid, u64 objectid, |
6030 | umode_t mode, u64 *index) | |
39279cc3 | 6031 | { |
0b246afa | 6032 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6033 | struct inode *inode; |
5f39d397 | 6034 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6035 | struct btrfs_key *location; |
5f39d397 | 6036 | struct btrfs_path *path; |
9c58309d CM |
6037 | struct btrfs_inode_ref *ref; |
6038 | struct btrfs_key key[2]; | |
6039 | u32 sizes[2]; | |
ef3b9af5 | 6040 | int nitems = name ? 2 : 1; |
9c58309d | 6041 | unsigned long ptr; |
11a19a90 | 6042 | unsigned int nofs_flag; |
39279cc3 | 6043 | int ret; |
39279cc3 | 6044 | |
5f39d397 | 6045 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6046 | if (!path) |
6047 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6048 | |
11a19a90 | 6049 | nofs_flag = memalloc_nofs_save(); |
0b246afa | 6050 | inode = new_inode(fs_info->sb); |
11a19a90 | 6051 | memalloc_nofs_restore(nofs_flag); |
8fb27640 YS |
6052 | if (!inode) { |
6053 | btrfs_free_path(path); | |
39279cc3 | 6054 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6055 | } |
39279cc3 | 6056 | |
5762b5c9 FM |
6057 | /* |
6058 | * O_TMPFILE, set link count to 0, so that after this point, | |
6059 | * we fill in an inode item with the correct link count. | |
6060 | */ | |
6061 | if (!name) | |
6062 | set_nlink(inode, 0); | |
6063 | ||
581bb050 LZ |
6064 | /* |
6065 | * we have to initialize this early, so we can reclaim the inode | |
6066 | * number if we fail afterwards in this function. | |
6067 | */ | |
6068 | inode->i_ino = objectid; | |
6069 | ||
ef3b9af5 | 6070 | if (dir && name) { |
1abe9b8a | 6071 | trace_btrfs_inode_request(dir); |
6072 | ||
877574e2 | 6073 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6074 | if (ret) { |
8fb27640 | 6075 | btrfs_free_path(path); |
09771430 | 6076 | iput(inode); |
aec7477b | 6077 | return ERR_PTR(ret); |
09771430 | 6078 | } |
ef3b9af5 FM |
6079 | } else if (dir) { |
6080 | *index = 0; | |
aec7477b JB |
6081 | } |
6082 | /* | |
6083 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 6084 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
6085 | * number |
6086 | */ | |
6087 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6088 | BTRFS_I(inode)->dir_index = *index; |
5c8fd99f | 6089 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
e02119d5 | 6090 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6091 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6092 | |
5dc562c5 JB |
6093 | /* |
6094 | * We could have gotten an inode number from somebody who was fsynced | |
6095 | * and then removed in this same transaction, so let's just set full | |
6096 | * sync since it will be a full sync anyway and this will blow away the | |
6097 | * old info in the log. | |
6098 | */ | |
6099 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6100 | ||
9c58309d | 6101 | key[0].objectid = objectid; |
962a298f | 6102 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6103 | key[0].offset = 0; |
6104 | ||
9c58309d | 6105 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6106 | |
6107 | if (name) { | |
6108 | /* | |
6109 | * Start new inodes with an inode_ref. This is slightly more | |
6110 | * efficient for small numbers of hard links since they will | |
6111 | * be packed into one item. Extended refs will kick in if we | |
6112 | * add more hard links than can fit in the ref item. | |
6113 | */ | |
6114 | key[1].objectid = objectid; | |
962a298f | 6115 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6116 | key[1].offset = ref_objectid; |
6117 | ||
6118 | sizes[1] = name_len + sizeof(*ref); | |
6119 | } | |
9c58309d | 6120 | |
b0d5d10f CM |
6121 | location = &BTRFS_I(inode)->location; |
6122 | location->objectid = objectid; | |
6123 | location->offset = 0; | |
962a298f | 6124 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6125 | |
6126 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6127 | if (ret < 0) { |
6128 | iput(inode); | |
b0d5d10f | 6129 | goto fail; |
32955c54 | 6130 | } |
b0d5d10f | 6131 | |
b9473439 | 6132 | path->leave_spinning = 1; |
ef3b9af5 | 6133 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6134 | if (ret != 0) |
b0d5d10f | 6135 | goto fail_unlock; |
5f39d397 | 6136 | |
ecc11fab | 6137 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6138 | inode_set_bytes(inode, 0); |
9cc97d64 | 6139 | |
c2050a45 | 6140 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6141 | inode->i_atime = inode->i_mtime; |
6142 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6143 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6144 | |
5f39d397 CM |
6145 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6146 | struct btrfs_inode_item); | |
b159fa28 | 6147 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6148 | sizeof(*inode_item)); |
e02119d5 | 6149 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6150 | |
ef3b9af5 FM |
6151 | if (name) { |
6152 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6153 | struct btrfs_inode_ref); | |
6154 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6155 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6156 | ptr = (unsigned long)(ref + 1); | |
6157 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6158 | } | |
9c58309d | 6159 | |
5f39d397 CM |
6160 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6161 | btrfs_free_path(path); | |
6162 | ||
6cbff00f CH |
6163 | btrfs_inherit_iflags(inode, dir); |
6164 | ||
569254b0 | 6165 | if (S_ISREG(mode)) { |
0b246afa | 6166 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6167 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6168 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6169 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6170 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6171 | } |
6172 | ||
5d4f98a2 | 6173 | inode_tree_add(inode); |
1abe9b8a | 6174 | |
6175 | trace_btrfs_inode_new(inode); | |
d9094414 | 6176 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6177 | |
8ea05e3a AB |
6178 | btrfs_update_root_times(trans, root); |
6179 | ||
63541927 FDBM |
6180 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6181 | if (ret) | |
0b246afa | 6182 | btrfs_err(fs_info, |
63541927 | 6183 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6184 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6185 | |
39279cc3 | 6186 | return inode; |
b0d5d10f CM |
6187 | |
6188 | fail_unlock: | |
32955c54 | 6189 | discard_new_inode(inode); |
5f39d397 | 6190 | fail: |
ef3b9af5 | 6191 | if (dir && name) |
aec7477b | 6192 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6193 | btrfs_free_path(path); |
6194 | return ERR_PTR(ret); | |
39279cc3 CM |
6195 | } |
6196 | ||
d352ac68 CM |
6197 | /* |
6198 | * utility function to add 'inode' into 'parent_inode' with | |
6199 | * a give name and a given sequence number. | |
6200 | * if 'add_backref' is true, also insert a backref from the | |
6201 | * inode to the parent directory. | |
6202 | */ | |
e02119d5 | 6203 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6204 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6205 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6206 | { |
4df27c4d | 6207 | int ret = 0; |
39279cc3 | 6208 | struct btrfs_key key; |
db0a669f NB |
6209 | struct btrfs_root *root = parent_inode->root; |
6210 | u64 ino = btrfs_ino(inode); | |
6211 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6212 | |
33345d01 | 6213 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6214 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6215 | } else { |
33345d01 | 6216 | key.objectid = ino; |
962a298f | 6217 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6218 | key.offset = 0; |
6219 | } | |
6220 | ||
33345d01 | 6221 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6222 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6223 | root->root_key.objectid, parent_ino, |
6224 | index, name, name_len); | |
4df27c4d | 6225 | } else if (add_backref) { |
33345d01 LZ |
6226 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6227 | parent_ino, index); | |
4df27c4d | 6228 | } |
39279cc3 | 6229 | |
79787eaa JM |
6230 | /* Nothing to clean up yet */ |
6231 | if (ret) | |
6232 | return ret; | |
4df27c4d | 6233 | |
684572df | 6234 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6235 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6236 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6237 | goto fail_dir_item; |
6238 | else if (ret) { | |
66642832 | 6239 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6240 | return ret; |
39279cc3 | 6241 | } |
79787eaa | 6242 | |
db0a669f | 6243 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6244 | name_len * 2); |
db0a669f | 6245 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6246 | /* |
6247 | * If we are replaying a log tree, we do not want to update the mtime | |
6248 | * and ctime of the parent directory with the current time, since the | |
6249 | * log replay procedure is responsible for setting them to their correct | |
6250 | * values (the ones it had when the fsync was done). | |
6251 | */ | |
6252 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6253 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6254 | ||
6255 | parent_inode->vfs_inode.i_mtime = now; | |
6256 | parent_inode->vfs_inode.i_ctime = now; | |
6257 | } | |
db0a669f | 6258 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
79787eaa | 6259 | if (ret) |
66642832 | 6260 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6261 | return ret; |
fe66a05a CM |
6262 | |
6263 | fail_dir_item: | |
6264 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6265 | u64 local_index; | |
6266 | int err; | |
3ee1c553 | 6267 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6268 | root->root_key.objectid, parent_ino, |
6269 | &local_index, name, name_len); | |
1690dd41 JT |
6270 | if (err) |
6271 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6272 | } else if (add_backref) { |
6273 | u64 local_index; | |
6274 | int err; | |
6275 | ||
6276 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6277 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6278 | if (err) |
6279 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6280 | } |
1690dd41 JT |
6281 | |
6282 | /* Return the original error code */ | |
fe66a05a | 6283 | return ret; |
39279cc3 CM |
6284 | } |
6285 | ||
6286 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6287 | struct btrfs_inode *dir, struct dentry *dentry, |
6288 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6289 | { |
a1b075d2 JB |
6290 | int err = btrfs_add_link(trans, dir, inode, |
6291 | dentry->d_name.name, dentry->d_name.len, | |
6292 | backref, index); | |
39279cc3 CM |
6293 | if (err > 0) |
6294 | err = -EEXIST; | |
6295 | return err; | |
6296 | } | |
6297 | ||
618e21d5 | 6298 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6299 | umode_t mode, dev_t rdev) |
618e21d5 | 6300 | { |
2ff7e61e | 6301 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6302 | struct btrfs_trans_handle *trans; |
6303 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6304 | struct inode *inode = NULL; |
618e21d5 | 6305 | int err; |
618e21d5 | 6306 | u64 objectid; |
00e4e6b3 | 6307 | u64 index = 0; |
618e21d5 | 6308 | |
9ed74f2d JB |
6309 | /* |
6310 | * 2 for inode item and ref | |
6311 | * 2 for dir items | |
6312 | * 1 for xattr if selinux is on | |
6313 | */ | |
a22285a6 YZ |
6314 | trans = btrfs_start_transaction(root, 5); |
6315 | if (IS_ERR(trans)) | |
6316 | return PTR_ERR(trans); | |
1832a6d5 | 6317 | |
581bb050 LZ |
6318 | err = btrfs_find_free_ino(root, &objectid); |
6319 | if (err) | |
6320 | goto out_unlock; | |
6321 | ||
aec7477b | 6322 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6323 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6324 | mode, &index); | |
7cf96da3 TI |
6325 | if (IS_ERR(inode)) { |
6326 | err = PTR_ERR(inode); | |
32955c54 | 6327 | inode = NULL; |
618e21d5 | 6328 | goto out_unlock; |
7cf96da3 | 6329 | } |
618e21d5 | 6330 | |
ad19db71 CS |
6331 | /* |
6332 | * If the active LSM wants to access the inode during | |
6333 | * d_instantiate it needs these. Smack checks to see | |
6334 | * if the filesystem supports xattrs by looking at the | |
6335 | * ops vector. | |
6336 | */ | |
ad19db71 | 6337 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6338 | init_special_inode(inode, inode->i_mode, rdev); |
6339 | ||
6340 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6341 | if (err) |
32955c54 | 6342 | goto out_unlock; |
b0d5d10f | 6343 | |
cef415af NB |
6344 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6345 | 0, index); | |
32955c54 AV |
6346 | if (err) |
6347 | goto out_unlock; | |
6348 | ||
6349 | btrfs_update_inode(trans, root, inode); | |
6350 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6351 | |
618e21d5 | 6352 | out_unlock: |
3a45bb20 | 6353 | btrfs_end_transaction(trans); |
2ff7e61e | 6354 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6355 | if (err && inode) { |
618e21d5 | 6356 | inode_dec_link_count(inode); |
32955c54 | 6357 | discard_new_inode(inode); |
618e21d5 | 6358 | } |
618e21d5 JB |
6359 | return err; |
6360 | } | |
6361 | ||
39279cc3 | 6362 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6363 | umode_t mode, bool excl) |
39279cc3 | 6364 | { |
2ff7e61e | 6365 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6366 | struct btrfs_trans_handle *trans; |
6367 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6368 | struct inode *inode = NULL; |
a22285a6 | 6369 | int err; |
39279cc3 | 6370 | u64 objectid; |
00e4e6b3 | 6371 | u64 index = 0; |
39279cc3 | 6372 | |
9ed74f2d JB |
6373 | /* |
6374 | * 2 for inode item and ref | |
6375 | * 2 for dir items | |
6376 | * 1 for xattr if selinux is on | |
6377 | */ | |
a22285a6 YZ |
6378 | trans = btrfs_start_transaction(root, 5); |
6379 | if (IS_ERR(trans)) | |
6380 | return PTR_ERR(trans); | |
9ed74f2d | 6381 | |
581bb050 LZ |
6382 | err = btrfs_find_free_ino(root, &objectid); |
6383 | if (err) | |
6384 | goto out_unlock; | |
6385 | ||
aec7477b | 6386 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6387 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6388 | mode, &index); | |
7cf96da3 TI |
6389 | if (IS_ERR(inode)) { |
6390 | err = PTR_ERR(inode); | |
32955c54 | 6391 | inode = NULL; |
39279cc3 | 6392 | goto out_unlock; |
7cf96da3 | 6393 | } |
ad19db71 CS |
6394 | /* |
6395 | * If the active LSM wants to access the inode during | |
6396 | * d_instantiate it needs these. Smack checks to see | |
6397 | * if the filesystem supports xattrs by looking at the | |
6398 | * ops vector. | |
6399 | */ | |
6400 | inode->i_fop = &btrfs_file_operations; | |
6401 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6402 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6403 | |
6404 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6405 | if (err) | |
32955c54 | 6406 | goto out_unlock; |
b0d5d10f CM |
6407 | |
6408 | err = btrfs_update_inode(trans, root, inode); | |
6409 | if (err) | |
32955c54 | 6410 | goto out_unlock; |
ad19db71 | 6411 | |
cef415af NB |
6412 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6413 | 0, index); | |
39279cc3 | 6414 | if (err) |
32955c54 | 6415 | goto out_unlock; |
43baa579 | 6416 | |
1e2e547a | 6417 | d_instantiate_new(dentry, inode); |
43baa579 | 6418 | |
39279cc3 | 6419 | out_unlock: |
3a45bb20 | 6420 | btrfs_end_transaction(trans); |
32955c54 | 6421 | if (err && inode) { |
39279cc3 | 6422 | inode_dec_link_count(inode); |
32955c54 | 6423 | discard_new_inode(inode); |
39279cc3 | 6424 | } |
2ff7e61e | 6425 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6426 | return err; |
6427 | } | |
6428 | ||
6429 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6430 | struct dentry *dentry) | |
6431 | { | |
271dba45 | 6432 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6433 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6434 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6435 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6436 | u64 index; |
39279cc3 CM |
6437 | int err; |
6438 | int drop_inode = 0; | |
6439 | ||
4a8be425 | 6440 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6441 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6442 | return -EXDEV; |
4a8be425 | 6443 | |
f186373f | 6444 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6445 | return -EMLINK; |
4a8be425 | 6446 | |
877574e2 | 6447 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6448 | if (err) |
6449 | goto fail; | |
6450 | ||
a22285a6 | 6451 | /* |
7e6b6465 | 6452 | * 2 items for inode and inode ref |
a22285a6 | 6453 | * 2 items for dir items |
7e6b6465 | 6454 | * 1 item for parent inode |
399b0bbf | 6455 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6456 | */ |
399b0bbf | 6457 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6458 | if (IS_ERR(trans)) { |
6459 | err = PTR_ERR(trans); | |
271dba45 | 6460 | trans = NULL; |
a22285a6 YZ |
6461 | goto fail; |
6462 | } | |
5f39d397 | 6463 | |
67de1176 MX |
6464 | /* There are several dir indexes for this inode, clear the cache. */ |
6465 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6466 | inc_nlink(inode); |
0c4d2d95 | 6467 | inode_inc_iversion(inode); |
c2050a45 | 6468 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6469 | ihold(inode); |
e9976151 | 6470 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6471 | |
cef415af NB |
6472 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6473 | 1, index); | |
5f39d397 | 6474 | |
a5719521 | 6475 | if (err) { |
54aa1f4d | 6476 | drop_inode = 1; |
a5719521 | 6477 | } else { |
10d9f309 | 6478 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6479 | |
a5719521 | 6480 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6481 | if (err) |
6482 | goto fail; | |
ef3b9af5 FM |
6483 | if (inode->i_nlink == 1) { |
6484 | /* | |
6485 | * If new hard link count is 1, it's a file created | |
6486 | * with open(2) O_TMPFILE flag. | |
6487 | */ | |
3d6ae7bb | 6488 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6489 | if (err) |
6490 | goto fail; | |
6491 | } | |
08c422c2 | 6492 | d_instantiate(dentry, inode); |
75b463d2 | 6493 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6494 | } |
39279cc3 | 6495 | |
1832a6d5 | 6496 | fail: |
271dba45 | 6497 | if (trans) |
3a45bb20 | 6498 | btrfs_end_transaction(trans); |
39279cc3 CM |
6499 | if (drop_inode) { |
6500 | inode_dec_link_count(inode); | |
6501 | iput(inode); | |
6502 | } | |
2ff7e61e | 6503 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6504 | return err; |
6505 | } | |
6506 | ||
18bb1db3 | 6507 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6508 | { |
2ff7e61e | 6509 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6510 | struct inode *inode = NULL; |
39279cc3 CM |
6511 | struct btrfs_trans_handle *trans; |
6512 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6513 | int err = 0; | |
b9d86667 | 6514 | u64 objectid = 0; |
00e4e6b3 | 6515 | u64 index = 0; |
39279cc3 | 6516 | |
9ed74f2d JB |
6517 | /* |
6518 | * 2 items for inode and ref | |
6519 | * 2 items for dir items | |
6520 | * 1 for xattr if selinux is on | |
6521 | */ | |
a22285a6 YZ |
6522 | trans = btrfs_start_transaction(root, 5); |
6523 | if (IS_ERR(trans)) | |
6524 | return PTR_ERR(trans); | |
39279cc3 | 6525 | |
581bb050 LZ |
6526 | err = btrfs_find_free_ino(root, &objectid); |
6527 | if (err) | |
6528 | goto out_fail; | |
6529 | ||
aec7477b | 6530 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6531 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6532 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6533 | if (IS_ERR(inode)) { |
6534 | err = PTR_ERR(inode); | |
32955c54 | 6535 | inode = NULL; |
39279cc3 CM |
6536 | goto out_fail; |
6537 | } | |
5f39d397 | 6538 | |
b0d5d10f CM |
6539 | /* these must be set before we unlock the inode */ |
6540 | inode->i_op = &btrfs_dir_inode_operations; | |
6541 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6542 | |
2a7dba39 | 6543 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6544 | if (err) |
32955c54 | 6545 | goto out_fail; |
39279cc3 | 6546 | |
6ef06d27 | 6547 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6548 | err = btrfs_update_inode(trans, root, inode); |
6549 | if (err) | |
32955c54 | 6550 | goto out_fail; |
5f39d397 | 6551 | |
db0a669f NB |
6552 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6553 | dentry->d_name.name, | |
6554 | dentry->d_name.len, 0, index); | |
39279cc3 | 6555 | if (err) |
32955c54 | 6556 | goto out_fail; |
5f39d397 | 6557 | |
1e2e547a | 6558 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
6559 | |
6560 | out_fail: | |
3a45bb20 | 6561 | btrfs_end_transaction(trans); |
32955c54 | 6562 | if (err && inode) { |
c7cfb8a5 | 6563 | inode_dec_link_count(inode); |
32955c54 | 6564 | discard_new_inode(inode); |
c7cfb8a5 | 6565 | } |
2ff7e61e | 6566 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6567 | return err; |
6568 | } | |
6569 | ||
c8b97818 | 6570 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6571 | struct page *page, |
c8b97818 CM |
6572 | size_t pg_offset, u64 extent_offset, |
6573 | struct btrfs_file_extent_item *item) | |
6574 | { | |
6575 | int ret; | |
6576 | struct extent_buffer *leaf = path->nodes[0]; | |
6577 | char *tmp; | |
6578 | size_t max_size; | |
6579 | unsigned long inline_size; | |
6580 | unsigned long ptr; | |
261507a0 | 6581 | int compress_type; |
c8b97818 CM |
6582 | |
6583 | WARN_ON(pg_offset != 0); | |
261507a0 | 6584 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6585 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6586 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6587 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6588 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6589 | if (!tmp) |
6590 | return -ENOMEM; | |
c8b97818 CM |
6591 | ptr = btrfs_file_extent_inline_start(item); |
6592 | ||
6593 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6594 | ||
09cbfeaf | 6595 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6596 | ret = btrfs_decompress(compress_type, tmp, page, |
6597 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6598 | |
6599 | /* | |
6600 | * decompression code contains a memset to fill in any space between the end | |
6601 | * of the uncompressed data and the end of max_size in case the decompressed | |
6602 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6603 | * the end of an inline extent and the beginning of the next block, so we | |
6604 | * cover that region here. | |
6605 | */ | |
6606 | ||
6607 | if (max_size + pg_offset < PAGE_SIZE) { | |
6608 | char *map = kmap(page); | |
6609 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6610 | kunmap(page); | |
6611 | } | |
c8b97818 | 6612 | kfree(tmp); |
166ae5a4 | 6613 | return ret; |
c8b97818 CM |
6614 | } |
6615 | ||
39b07b5d OS |
6616 | /** |
6617 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. | |
6618 | * @inode: file to search in | |
6619 | * @page: page to read extent data into if the extent is inline | |
6620 | * @pg_offset: offset into @page to copy to | |
6621 | * @start: file offset | |
6622 | * @len: length of range starting at @start | |
6623 | * | |
6624 | * This returns the first &struct extent_map which overlaps with the given | |
6625 | * range, reading it from the B-tree and caching it if necessary. Note that | |
6626 | * there may be more extents which overlap the given range after the returned | |
6627 | * extent_map. | |
d352ac68 | 6628 | * |
39b07b5d OS |
6629 | * If @page is not NULL and the extent is inline, this also reads the extent |
6630 | * data directly into the page and marks the extent up to date in the io_tree. | |
6631 | * | |
6632 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6633 | */ |
fc4f21b1 | 6634 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
39b07b5d OS |
6635 | struct page *page, size_t pg_offset, |
6636 | u64 start, u64 len) | |
a52d9a80 | 6637 | { |
3ffbd68c | 6638 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6639 | int ret = 0; |
a52d9a80 CM |
6640 | u64 extent_start = 0; |
6641 | u64 extent_end = 0; | |
fc4f21b1 | 6642 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6643 | int extent_type = -1; |
f421950f | 6644 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6645 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6646 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6647 | struct extent_buffer *leaf; |
6648 | struct btrfs_key found_key; | |
a52d9a80 | 6649 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6650 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6651 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6652 | |
890871be | 6653 | read_lock(&em_tree->lock); |
d1310b2e | 6654 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6655 | read_unlock(&em_tree->lock); |
d1310b2e | 6656 | |
a52d9a80 | 6657 | if (em) { |
e1c4b745 CM |
6658 | if (em->start > start || em->start + em->len <= start) |
6659 | free_extent_map(em); | |
6660 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6661 | free_extent_map(em); |
6662 | else | |
6663 | goto out; | |
a52d9a80 | 6664 | } |
172ddd60 | 6665 | em = alloc_extent_map(); |
a52d9a80 | 6666 | if (!em) { |
1028d1c4 | 6667 | ret = -ENOMEM; |
d1310b2e | 6668 | goto out; |
a52d9a80 | 6669 | } |
d1310b2e | 6670 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6671 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6672 | em->len = (u64)-1; |
c8b97818 | 6673 | em->block_len = (u64)-1; |
f421950f | 6674 | |
bee6ec82 | 6675 | path = btrfs_alloc_path(); |
f421950f | 6676 | if (!path) { |
1028d1c4 | 6677 | ret = -ENOMEM; |
bee6ec82 | 6678 | goto out; |
f421950f CM |
6679 | } |
6680 | ||
bee6ec82 LB |
6681 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6682 | path->reada = READA_FORWARD; | |
6683 | ||
e49aabd9 LB |
6684 | /* |
6685 | * Unless we're going to uncompress the inline extent, no sleep would | |
6686 | * happen. | |
6687 | */ | |
6688 | path->leave_spinning = 1; | |
6689 | ||
51899412 JB |
6690 | path->recurse = btrfs_is_free_space_inode(inode); |
6691 | ||
5c9a702e | 6692 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6693 | if (ret < 0) { |
a52d9a80 | 6694 | goto out; |
b8eeab7f | 6695 | } else if (ret > 0) { |
a52d9a80 CM |
6696 | if (path->slots[0] == 0) |
6697 | goto not_found; | |
6698 | path->slots[0]--; | |
1028d1c4 | 6699 | ret = 0; |
a52d9a80 CM |
6700 | } |
6701 | ||
5f39d397 CM |
6702 | leaf = path->nodes[0]; |
6703 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6704 | struct btrfs_file_extent_item); |
5f39d397 | 6705 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6706 | if (found_key.objectid != objectid || |
694c12ed | 6707 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6708 | /* |
6709 | * If we backup past the first extent we want to move forward | |
6710 | * and see if there is an extent in front of us, otherwise we'll | |
6711 | * say there is a hole for our whole search range which can | |
6712 | * cause problems. | |
6713 | */ | |
6714 | extent_end = start; | |
6715 | goto next; | |
a52d9a80 CM |
6716 | } |
6717 | ||
694c12ed | 6718 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6719 | extent_start = found_key.offset; |
a5eeb3d1 | 6720 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6721 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6722 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6723 | /* Only regular file could have regular/prealloc extent */ |
6724 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 6725 | ret = -EUCLEAN; |
6bf9e4bd QW |
6726 | btrfs_crit(fs_info, |
6727 | "regular/prealloc extent found for non-regular inode %llu", | |
6728 | btrfs_ino(inode)); | |
6729 | goto out; | |
6730 | } | |
09ed2f16 LB |
6731 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6732 | extent_start); | |
694c12ed | 6733 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6734 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6735 | path->slots[0], | |
6736 | extent_start); | |
9036c102 | 6737 | } |
25a50341 | 6738 | next: |
9036c102 YZ |
6739 | if (start >= extent_end) { |
6740 | path->slots[0]++; | |
6741 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6742 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 6743 | if (ret < 0) |
9036c102 | 6744 | goto out; |
1028d1c4 | 6745 | else if (ret > 0) |
9036c102 | 6746 | goto not_found; |
1028d1c4 | 6747 | |
9036c102 | 6748 | leaf = path->nodes[0]; |
a52d9a80 | 6749 | } |
9036c102 YZ |
6750 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6751 | if (found_key.objectid != objectid || | |
6752 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6753 | goto not_found; | |
6754 | if (start + len <= found_key.offset) | |
6755 | goto not_found; | |
e2eca69d WS |
6756 | if (start > found_key.offset) |
6757 | goto next; | |
02a033df NB |
6758 | |
6759 | /* New extent overlaps with existing one */ | |
9036c102 | 6760 | em->start = start; |
70c8a91c | 6761 | em->orig_start = start; |
9036c102 | 6762 | em->len = found_key.offset - start; |
02a033df NB |
6763 | em->block_start = EXTENT_MAP_HOLE; |
6764 | goto insert; | |
9036c102 YZ |
6765 | } |
6766 | ||
39b07b5d | 6767 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
7ffbb598 | 6768 | |
694c12ed NB |
6769 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6770 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6771 | goto insert; |
694c12ed | 6772 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 6773 | unsigned long ptr; |
a52d9a80 | 6774 | char *map; |
3326d1b0 CM |
6775 | size_t size; |
6776 | size_t extent_offset; | |
6777 | size_t copy_size; | |
a52d9a80 | 6778 | |
39b07b5d | 6779 | if (!page) |
689f9346 | 6780 | goto out; |
5f39d397 | 6781 | |
e41ca589 | 6782 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 6783 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6784 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6785 | size - extent_offset); | |
3326d1b0 | 6786 | em->start = extent_start + extent_offset; |
0b246afa | 6787 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 6788 | em->orig_block_len = em->len; |
70c8a91c | 6789 | em->orig_start = em->start; |
689f9346 | 6790 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 LB |
6791 | |
6792 | btrfs_set_path_blocking(path); | |
bf46f52d | 6793 | if (!PageUptodate(page)) { |
261507a0 LZ |
6794 | if (btrfs_file_extent_compression(leaf, item) != |
6795 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6796 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6797 | extent_offset, item); |
1028d1c4 | 6798 | if (ret) |
166ae5a4 | 6799 | goto out; |
c8b97818 CM |
6800 | } else { |
6801 | map = kmap(page); | |
6802 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6803 | copy_size); | |
09cbfeaf | 6804 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6805 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6806 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6807 | copy_size); |
6808 | } | |
c8b97818 CM |
6809 | kunmap(page); |
6810 | } | |
179e29e4 | 6811 | flush_dcache_page(page); |
a52d9a80 | 6812 | } |
d1310b2e | 6813 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6814 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6815 | goto insert; |
a52d9a80 CM |
6816 | } |
6817 | not_found: | |
6818 | em->start = start; | |
70c8a91c | 6819 | em->orig_start = start; |
d1310b2e | 6820 | em->len = len; |
5f39d397 | 6821 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6822 | insert: |
1028d1c4 | 6823 | ret = 0; |
b3b4aa74 | 6824 | btrfs_release_path(path); |
d1310b2e | 6825 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6826 | btrfs_err(fs_info, |
5d163e0e JM |
6827 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6828 | em->start, em->len, start, len); | |
1028d1c4 | 6829 | ret = -EIO; |
a52d9a80 CM |
6830 | goto out; |
6831 | } | |
d1310b2e | 6832 | |
890871be | 6833 | write_lock(&em_tree->lock); |
1028d1c4 | 6834 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6835 | write_unlock(&em_tree->lock); |
a52d9a80 | 6836 | out: |
c6414280 | 6837 | btrfs_free_path(path); |
1abe9b8a | 6838 | |
fc4f21b1 | 6839 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6840 | |
1028d1c4 | 6841 | if (ret) { |
a52d9a80 | 6842 | free_extent_map(em); |
1028d1c4 | 6843 | return ERR_PTR(ret); |
a52d9a80 CM |
6844 | } |
6845 | return em; | |
6846 | } | |
6847 | ||
fc4f21b1 | 6848 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 6849 | u64 start, u64 len) |
ec29ed5b CM |
6850 | { |
6851 | struct extent_map *em; | |
6852 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 6853 | u64 delalloc_start = start; |
ec29ed5b | 6854 | u64 end; |
f3714ef4 NB |
6855 | u64 delalloc_len; |
6856 | u64 delalloc_end; | |
ec29ed5b CM |
6857 | int err = 0; |
6858 | ||
39b07b5d | 6859 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
ec29ed5b CM |
6860 | if (IS_ERR(em)) |
6861 | return em; | |
9986277e DC |
6862 | /* |
6863 | * If our em maps to: | |
6864 | * - a hole or | |
6865 | * - a pre-alloc extent, | |
6866 | * there might actually be delalloc bytes behind it. | |
6867 | */ | |
6868 | if (em->block_start != EXTENT_MAP_HOLE && | |
6869 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6870 | return em; | |
6871 | else | |
6872 | hole_em = em; | |
ec29ed5b CM |
6873 | |
6874 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6875 | end = start + len; | |
6876 | if (end < start) | |
6877 | end = (u64)-1; | |
6878 | else | |
6879 | end -= 1; | |
6880 | ||
6881 | em = NULL; | |
6882 | ||
6883 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 6884 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 6885 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
6886 | delalloc_end = delalloc_start + delalloc_len; |
6887 | if (delalloc_end < delalloc_start) | |
6888 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
6889 | |
6890 | /* | |
f3714ef4 NB |
6891 | * We didn't find anything useful, return the original results from |
6892 | * get_extent() | |
ec29ed5b | 6893 | */ |
f3714ef4 | 6894 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
6895 | em = hole_em; |
6896 | hole_em = NULL; | |
6897 | goto out; | |
6898 | } | |
6899 | ||
f3714ef4 NB |
6900 | /* |
6901 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
6902 | * the start they passed in | |
ec29ed5b | 6903 | */ |
f3714ef4 NB |
6904 | delalloc_start = max(start, delalloc_start); |
6905 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 6906 | |
f3714ef4 NB |
6907 | if (delalloc_len > 0) { |
6908 | u64 hole_start; | |
02950af4 | 6909 | u64 hole_len; |
f3714ef4 | 6910 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 6911 | |
172ddd60 | 6912 | em = alloc_extent_map(); |
ec29ed5b CM |
6913 | if (!em) { |
6914 | err = -ENOMEM; | |
6915 | goto out; | |
6916 | } | |
f3714ef4 NB |
6917 | |
6918 | ASSERT(hole_em); | |
ec29ed5b | 6919 | /* |
f3714ef4 NB |
6920 | * When btrfs_get_extent can't find anything it returns one |
6921 | * huge hole | |
ec29ed5b | 6922 | * |
f3714ef4 NB |
6923 | * Make sure what it found really fits our range, and adjust to |
6924 | * make sure it is based on the start from the caller | |
ec29ed5b | 6925 | */ |
f3714ef4 NB |
6926 | if (hole_end <= start || hole_em->start > end) { |
6927 | free_extent_map(hole_em); | |
6928 | hole_em = NULL; | |
6929 | } else { | |
6930 | hole_start = max(hole_em->start, start); | |
6931 | hole_len = hole_end - hole_start; | |
ec29ed5b | 6932 | } |
f3714ef4 NB |
6933 | |
6934 | if (hole_em && delalloc_start > hole_start) { | |
6935 | /* | |
6936 | * Our hole starts before our delalloc, so we have to | |
6937 | * return just the parts of the hole that go until the | |
6938 | * delalloc starts | |
ec29ed5b | 6939 | */ |
f3714ef4 | 6940 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
6941 | em->start = hole_start; |
6942 | em->orig_start = hole_start; | |
6943 | /* | |
f3714ef4 NB |
6944 | * Don't adjust block start at all, it is fixed at |
6945 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
6946 | */ |
6947 | em->block_start = hole_em->block_start; | |
6948 | em->block_len = hole_len; | |
f9e4fb53 LB |
6949 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6950 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 6951 | } else { |
f3714ef4 NB |
6952 | /* |
6953 | * Hole is out of passed range or it starts after | |
6954 | * delalloc range | |
6955 | */ | |
6956 | em->start = delalloc_start; | |
6957 | em->len = delalloc_len; | |
6958 | em->orig_start = delalloc_start; | |
ec29ed5b | 6959 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 6960 | em->block_len = delalloc_len; |
ec29ed5b | 6961 | } |
bf8d32b9 | 6962 | } else { |
ec29ed5b CM |
6963 | return hole_em; |
6964 | } | |
6965 | out: | |
6966 | ||
6967 | free_extent_map(hole_em); | |
6968 | if (err) { | |
6969 | free_extent_map(em); | |
6970 | return ERR_PTR(err); | |
6971 | } | |
6972 | return em; | |
6973 | } | |
6974 | ||
64f54188 | 6975 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
5f9a8a51 FM |
6976 | const u64 start, |
6977 | const u64 len, | |
6978 | const u64 orig_start, | |
6979 | const u64 block_start, | |
6980 | const u64 block_len, | |
6981 | const u64 orig_block_len, | |
6982 | const u64 ram_bytes, | |
6983 | const int type) | |
6984 | { | |
6985 | struct extent_map *em = NULL; | |
6986 | int ret; | |
6987 | ||
5f9a8a51 | 6988 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
6989 | em = create_io_em(inode, start, len, orig_start, block_start, |
6990 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
6991 | BTRFS_COMPRESS_NONE, /* compress_type */ |
6992 | type); | |
5f9a8a51 FM |
6993 | if (IS_ERR(em)) |
6994 | goto out; | |
6995 | } | |
64f54188 NB |
6996 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
6997 | block_len, type); | |
5f9a8a51 FM |
6998 | if (ret) { |
6999 | if (em) { | |
7000 | free_extent_map(em); | |
64f54188 | 7001 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
5f9a8a51 FM |
7002 | } |
7003 | em = ERR_PTR(ret); | |
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, |
4b46fce2 JB |
7011 | u64 start, u64 len) |
7012 | { | |
9fc6f911 NB |
7013 | struct btrfs_root *root = inode->root; |
7014 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 7015 | struct extent_map *em; |
4b46fce2 JB |
7016 | struct btrfs_key ins; |
7017 | u64 alloc_hint; | |
7018 | int ret; | |
4b46fce2 | 7019 | |
9fc6f911 | 7020 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7021 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7022 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7023 | if (ret) |
7024 | return ERR_PTR(ret); | |
4b46fce2 | 7025 | |
9fc6f911 | 7026 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
5f9a8a51 | 7027 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 7028 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7029 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7030 | if (IS_ERR(em)) |
9fc6f911 NB |
7031 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
7032 | 1); | |
de0ee0ed | 7033 | |
4b46fce2 JB |
7034 | return em; |
7035 | } | |
7036 | ||
46bfbb5c | 7037 | /* |
e4ecaf90 QW |
7038 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
7039 | * | |
7040 | * @offset: File offset | |
7041 | * @len: The length to write, will be updated to the nocow writeable | |
7042 | * range | |
7043 | * @orig_start: (optional) Return the original file offset of the file extent | |
7044 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
7045 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
7046 | * @strict: if true, omit optimizations that might force us into unnecessary |
7047 | * cow. e.g., don't trust generation number. | |
e4ecaf90 QW |
7048 | * |
7049 | * This function will flush ordered extents in the range to ensure proper | |
7050 | * nocow checks for (nowait == false) case. | |
7051 | * | |
7052 | * Return: | |
7053 | * >0 and update @len if we can do nocow write | |
7054 | * 0 if we can't do nocow write | |
7055 | * <0 if error happened | |
7056 | * | |
7057 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
7058 | * any ordered extents. | |
46bfbb5c | 7059 | */ |
00361589 | 7060 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 7061 | u64 *orig_start, u64 *orig_block_len, |
a84d5d42 | 7062 | u64 *ram_bytes, bool strict) |
46bfbb5c | 7063 | { |
2ff7e61e | 7064 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7065 | struct btrfs_path *path; |
7066 | int ret; | |
7067 | struct extent_buffer *leaf; | |
7068 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7069 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7070 | struct btrfs_file_extent_item *fi; |
7071 | struct btrfs_key key; | |
7072 | u64 disk_bytenr; | |
7073 | u64 backref_offset; | |
7074 | u64 extent_end; | |
7075 | u64 num_bytes; | |
7076 | int slot; | |
7077 | int found_type; | |
7ee9e440 | 7078 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7079 | |
46bfbb5c CM |
7080 | path = btrfs_alloc_path(); |
7081 | if (!path) | |
7082 | return -ENOMEM; | |
7083 | ||
f85b7379 DS |
7084 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7085 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7086 | if (ret < 0) |
7087 | goto out; | |
7088 | ||
7089 | slot = path->slots[0]; | |
7090 | if (ret == 1) { | |
7091 | if (slot == 0) { | |
7092 | /* can't find the item, must cow */ | |
7093 | ret = 0; | |
7094 | goto out; | |
7095 | } | |
7096 | slot--; | |
7097 | } | |
7098 | ret = 0; | |
7099 | leaf = path->nodes[0]; | |
7100 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7101 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7102 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7103 | /* not our file or wrong item type, must cow */ | |
7104 | goto out; | |
7105 | } | |
7106 | ||
7107 | if (key.offset > offset) { | |
7108 | /* Wrong offset, must cow */ | |
7109 | goto out; | |
7110 | } | |
7111 | ||
7112 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7113 | found_type = btrfs_file_extent_type(leaf, fi); | |
7114 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7115 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7116 | /* not a regular extent, must cow */ | |
7117 | goto out; | |
7118 | } | |
7ee9e440 JB |
7119 | |
7120 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7121 | goto out; | |
7122 | ||
e77751aa MX |
7123 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7124 | if (extent_end <= offset) | |
7125 | goto out; | |
7126 | ||
46bfbb5c | 7127 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7128 | if (disk_bytenr == 0) |
7129 | goto out; | |
7130 | ||
7131 | if (btrfs_file_extent_compression(leaf, fi) || | |
7132 | btrfs_file_extent_encryption(leaf, fi) || | |
7133 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7134 | goto out; | |
7135 | ||
78d4295b EL |
7136 | /* |
7137 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7138 | * unnecessary search. | |
7139 | */ | |
a84d5d42 BB |
7140 | if (!strict && |
7141 | (btrfs_file_extent_generation(leaf, fi) <= | |
7142 | btrfs_root_last_snapshot(&root->root_item))) | |
78d4295b EL |
7143 | goto out; |
7144 | ||
46bfbb5c CM |
7145 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7146 | ||
7ee9e440 JB |
7147 | if (orig_start) { |
7148 | *orig_start = key.offset - backref_offset; | |
7149 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7150 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7151 | } | |
eb384b55 | 7152 | |
2ff7e61e | 7153 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7154 | goto out; |
7b2b7085 MX |
7155 | |
7156 | num_bytes = min(offset + *len, extent_end) - offset; | |
7157 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7158 | u64 range_end; | |
7159 | ||
da17066c JM |
7160 | range_end = round_up(offset + num_bytes, |
7161 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7162 | ret = test_range_bit(io_tree, offset, range_end, |
7163 | EXTENT_DELALLOC, 0, NULL); | |
7164 | if (ret) { | |
7165 | ret = -EAGAIN; | |
7166 | goto out; | |
7167 | } | |
7168 | } | |
7169 | ||
1bda19eb | 7170 | btrfs_release_path(path); |
46bfbb5c CM |
7171 | |
7172 | /* | |
7173 | * look for other files referencing this extent, if we | |
7174 | * find any we must cow | |
7175 | */ | |
00361589 | 7176 | |
e4c3b2dc | 7177 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
a84d5d42 BB |
7178 | key.offset - backref_offset, disk_bytenr, |
7179 | strict); | |
00361589 JB |
7180 | if (ret) { |
7181 | ret = 0; | |
7182 | goto out; | |
7183 | } | |
46bfbb5c CM |
7184 | |
7185 | /* | |
7186 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7187 | * in this extent we are about to write. If there | |
7188 | * are any csums in that range we have to cow in order | |
7189 | * to keep the csums correct | |
7190 | */ | |
7191 | disk_bytenr += backref_offset; | |
7192 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7193 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7194 | goto out; | |
46bfbb5c CM |
7195 | /* |
7196 | * all of the above have passed, it is safe to overwrite this extent | |
7197 | * without cow | |
7198 | */ | |
eb384b55 | 7199 | *len = num_bytes; |
46bfbb5c CM |
7200 | ret = 1; |
7201 | out: | |
7202 | btrfs_free_path(path); | |
7203 | return ret; | |
7204 | } | |
7205 | ||
eb838e73 | 7206 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
f85781fb | 7207 | struct extent_state **cached_state, bool writing) |
eb838e73 JB |
7208 | { |
7209 | struct btrfs_ordered_extent *ordered; | |
7210 | int ret = 0; | |
7211 | ||
7212 | while (1) { | |
7213 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7214 | cached_state); |
eb838e73 JB |
7215 | /* |
7216 | * We're concerned with the entire range that we're going to be | |
01327610 | 7217 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7218 | * extents in this range. |
7219 | */ | |
a776c6fa | 7220 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7221 | lockend - lockstart + 1); |
7222 | ||
7223 | /* | |
7224 | * We need to make sure there are no buffered pages in this | |
7225 | * range either, we could have raced between the invalidate in | |
7226 | * generic_file_direct_write and locking the extent. The | |
7227 | * invalidate needs to happen so that reads after a write do not | |
7228 | * get stale data. | |
7229 | */ | |
fc4adbff | 7230 | if (!ordered && |
051c98eb DS |
7231 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7232 | lockstart, lockend))) | |
eb838e73 JB |
7233 | break; |
7234 | ||
7235 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7236 | cached_state); |
eb838e73 JB |
7237 | |
7238 | if (ordered) { | |
ade77029 FM |
7239 | /* |
7240 | * If we are doing a DIO read and the ordered extent we | |
7241 | * found is for a buffered write, we can not wait for it | |
7242 | * to complete and retry, because if we do so we can | |
7243 | * deadlock with concurrent buffered writes on page | |
7244 | * locks. This happens only if our DIO read covers more | |
7245 | * than one extent map, if at this point has already | |
7246 | * created an ordered extent for a previous extent map | |
7247 | * and locked its range in the inode's io tree, and a | |
7248 | * concurrent write against that previous extent map's | |
7249 | * range and this range started (we unlock the ranges | |
7250 | * in the io tree only when the bios complete and | |
7251 | * buffered writes always lock pages before attempting | |
7252 | * to lock range in the io tree). | |
7253 | */ | |
7254 | if (writing || | |
7255 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
c0a43603 | 7256 | btrfs_start_ordered_extent(ordered, 1); |
ade77029 FM |
7257 | else |
7258 | ret = -ENOTBLK; | |
eb838e73 JB |
7259 | btrfs_put_ordered_extent(ordered); |
7260 | } else { | |
eb838e73 | 7261 | /* |
b850ae14 FM |
7262 | * We could trigger writeback for this range (and wait |
7263 | * for it to complete) and then invalidate the pages for | |
7264 | * this range (through invalidate_inode_pages2_range()), | |
7265 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7266 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7267 | * triggered a readahead) on a page lock due to an |
7268 | * ordered dio extent we created before but did not have | |
7269 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7270 | * complete), which makes readahead wait for that |
b850ae14 FM |
7271 | * ordered extent to complete while holding a lock on |
7272 | * that page. | |
eb838e73 | 7273 | */ |
b850ae14 | 7274 | ret = -ENOTBLK; |
eb838e73 JB |
7275 | } |
7276 | ||
ade77029 FM |
7277 | if (ret) |
7278 | break; | |
7279 | ||
eb838e73 JB |
7280 | cond_resched(); |
7281 | } | |
7282 | ||
7283 | return ret; | |
7284 | } | |
7285 | ||
6f9994db | 7286 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7287 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7288 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7289 | u64 block_len, u64 orig_block_len, |
7290 | u64 ram_bytes, int compress_type, | |
7291 | int type) | |
69ffb543 JB |
7292 | { |
7293 | struct extent_map_tree *em_tree; | |
7294 | struct extent_map *em; | |
69ffb543 JB |
7295 | int ret; |
7296 | ||
6f9994db LB |
7297 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7298 | type == BTRFS_ORDERED_COMPRESSED || | |
7299 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7300 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7301 | |
4b67c11d | 7302 | em_tree = &inode->extent_tree; |
69ffb543 JB |
7303 | em = alloc_extent_map(); |
7304 | if (!em) | |
7305 | return ERR_PTR(-ENOMEM); | |
7306 | ||
7307 | em->start = start; | |
7308 | em->orig_start = orig_start; | |
7309 | em->len = len; | |
7310 | em->block_len = block_len; | |
7311 | em->block_start = block_start; | |
b4939680 | 7312 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7313 | em->ram_bytes = ram_bytes; |
70c8a91c | 7314 | em->generation = -1; |
69ffb543 | 7315 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7316 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7317 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7318 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7319 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7320 | em->compress_type = compress_type; | |
7321 | } | |
69ffb543 JB |
7322 | |
7323 | do { | |
4b67c11d NB |
7324 | btrfs_drop_extent_cache(inode, em->start, |
7325 | em->start + em->len - 1, 0); | |
69ffb543 | 7326 | write_lock(&em_tree->lock); |
09a2a8f9 | 7327 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7328 | write_unlock(&em_tree->lock); |
6f9994db LB |
7329 | /* |
7330 | * The caller has taken lock_extent(), who could race with us | |
7331 | * to add em? | |
7332 | */ | |
69ffb543 JB |
7333 | } while (ret == -EEXIST); |
7334 | ||
7335 | if (ret) { | |
7336 | free_extent_map(em); | |
7337 | return ERR_PTR(ret); | |
7338 | } | |
7339 | ||
6f9994db | 7340 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7341 | return em; |
7342 | } | |
7343 | ||
1c8d0175 | 7344 | |
c5794e51 | 7345 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7346 | struct inode *inode, |
7347 | struct btrfs_dio_data *dio_data, | |
7348 | u64 start, u64 len) | |
7349 | { | |
7350 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7351 | struct extent_map *em = *map; | |
7352 | int ret = 0; | |
7353 | ||
7354 | /* | |
7355 | * We don't allocate a new extent in the following cases | |
7356 | * | |
7357 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7358 | * existing extent. | |
7359 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7360 | * just use the extent. | |
7361 | * | |
7362 | */ | |
7363 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7364 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7365 | em->block_start != EXTENT_MAP_HOLE)) { | |
7366 | int type; | |
7367 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7368 | ||
7369 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7370 | type = BTRFS_ORDERED_PREALLOC; | |
7371 | else | |
7372 | type = BTRFS_ORDERED_NOCOW; | |
7373 | len = min(len, em->len - (start - em->start)); | |
7374 | block_start = em->block_start + (start - em->start); | |
7375 | ||
7376 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
a84d5d42 | 7377 | &orig_block_len, &ram_bytes, false) == 1 && |
c5794e51 NB |
7378 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
7379 | struct extent_map *em2; | |
7380 | ||
64f54188 | 7381 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
c5794e51 NB |
7382 | orig_start, block_start, |
7383 | len, orig_block_len, | |
7384 | ram_bytes, type); | |
7385 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7386 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7387 | free_extent_map(em); | |
7388 | *map = em = em2; | |
7389 | } | |
7390 | ||
7391 | if (em2 && IS_ERR(em2)) { | |
7392 | ret = PTR_ERR(em2); | |
7393 | goto out; | |
7394 | } | |
7395 | /* | |
7396 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7397 | * use the existing or preallocated extent, so does not | |
7398 | * need to adjust btrfs_space_info's bytes_may_use. | |
7399 | */ | |
9db5d510 | 7400 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
c5794e51 NB |
7401 | goto skip_cow; |
7402 | } | |
7403 | } | |
7404 | ||
7405 | /* this will cow the extent */ | |
c5794e51 | 7406 | free_extent_map(em); |
9fc6f911 | 7407 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
c5794e51 NB |
7408 | if (IS_ERR(em)) { |
7409 | ret = PTR_ERR(em); | |
7410 | goto out; | |
7411 | } | |
7412 | ||
7413 | len = min(len, em->len - (start - em->start)); | |
7414 | ||
7415 | skip_cow: | |
c5794e51 NB |
7416 | /* |
7417 | * Need to update the i_size under the extent lock so buffered | |
7418 | * readers will get the updated i_size when we unlock. | |
7419 | */ | |
f85781fb | 7420 | if (start + len > i_size_read(inode)) |
c5794e51 NB |
7421 | i_size_write(inode, start + len); |
7422 | ||
c5794e51 | 7423 | dio_data->reserve -= len; |
c5794e51 NB |
7424 | out: |
7425 | return ret; | |
7426 | } | |
7427 | ||
f85781fb GR |
7428 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7429 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7430 | struct iomap *srcmap) | |
4b46fce2 | 7431 | { |
0b246afa | 7432 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7433 | struct extent_map *em; |
eb838e73 | 7434 | struct extent_state *cached_state = NULL; |
50745b0a | 7435 | struct btrfs_dio_data *dio_data = NULL; |
eb838e73 | 7436 | u64 lockstart, lockend; |
f85781fb | 7437 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7438 | int ret = 0; |
f85781fb GR |
7439 | u64 len = length; |
7440 | bool unlock_extents = false; | |
eb838e73 | 7441 | |
f85781fb | 7442 | if (!write) |
0b246afa | 7443 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7444 | |
c329861d JB |
7445 | lockstart = start; |
7446 | lockend = start + len - 1; | |
7447 | ||
f85781fb GR |
7448 | /* |
7449 | * The generic stuff only does filemap_write_and_wait_range, which | |
7450 | * isn't enough if we've written compressed pages to this area, so we | |
7451 | * need to flush the dirty pages again to make absolutely sure that any | |
7452 | * outstanding dirty pages are on disk. | |
7453 | */ | |
7454 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7455 | &BTRFS_I(inode)->runtime_flags)) { | |
7456 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7457 | start + length - 1); | |
7458 | if (ret) | |
7459 | return ret; | |
7460 | } | |
7461 | ||
7462 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); | |
7463 | if (!dio_data) | |
7464 | return -ENOMEM; | |
7465 | ||
7466 | dio_data->length = length; | |
7467 | if (write) { | |
7468 | dio_data->reserve = round_up(length, fs_info->sectorsize); | |
7469 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), | |
7470 | &dio_data->data_reserved, | |
7471 | start, dio_data->reserve); | |
7472 | if (ret) { | |
7473 | extent_changeset_free(dio_data->data_reserved); | |
7474 | kfree(dio_data); | |
7475 | return ret; | |
7476 | } | |
e1cbbfa5 | 7477 | } |
f85781fb GR |
7478 | iomap->private = dio_data; |
7479 | ||
e1cbbfa5 | 7480 | |
eb838e73 JB |
7481 | /* |
7482 | * If this errors out it's because we couldn't invalidate pagecache for | |
7483 | * this range and we need to fallback to buffered. | |
7484 | */ | |
f85781fb | 7485 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
9c9464cc FM |
7486 | ret = -ENOTBLK; |
7487 | goto err; | |
7488 | } | |
eb838e73 | 7489 | |
39b07b5d | 7490 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7491 | if (IS_ERR(em)) { |
7492 | ret = PTR_ERR(em); | |
7493 | goto unlock_err; | |
7494 | } | |
4b46fce2 JB |
7495 | |
7496 | /* | |
7497 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7498 | * io. INLINE is special, and we could probably kludge it in here, but | |
7499 | * it's still buffered so for safety lets just fall back to the generic | |
7500 | * buffered path. | |
7501 | * | |
7502 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7503 | * decompress it, so there will be buffering required no matter what we | |
7504 | * do, so go ahead and fallback to buffered. | |
7505 | * | |
01327610 | 7506 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7507 | * to buffered IO. Don't blame me, this is the price we pay for using |
7508 | * the generic code. | |
7509 | */ | |
7510 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7511 | em->block_start == EXTENT_MAP_INLINE) { | |
7512 | free_extent_map(em); | |
eb838e73 JB |
7513 | ret = -ENOTBLK; |
7514 | goto unlock_err; | |
4b46fce2 JB |
7515 | } |
7516 | ||
f85781fb GR |
7517 | len = min(len, em->len - (start - em->start)); |
7518 | if (write) { | |
7519 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
7520 | start, len); | |
c5794e51 NB |
7521 | if (ret < 0) |
7522 | goto unlock_err; | |
f85781fb GR |
7523 | unlock_extents = true; |
7524 | /* Recalc len in case the new em is smaller than requested */ | |
7525 | len = min(len, em->len - (start - em->start)); | |
c5794e51 | 7526 | } else { |
1c8d0175 NB |
7527 | /* |
7528 | * We need to unlock only the end area that we aren't using. | |
7529 | * The rest is going to be unlocked by the endio routine. | |
7530 | */ | |
f85781fb GR |
7531 | lockstart = start + len; |
7532 | if (lockstart < lockend) | |
7533 | unlock_extents = true; | |
7534 | } | |
7535 | ||
7536 | if (unlock_extents) | |
7537 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
7538 | lockstart, lockend, &cached_state); | |
7539 | else | |
7540 | free_extent_state(cached_state); | |
7541 | ||
7542 | /* | |
7543 | * Translate extent map information to iomap. | |
7544 | * We trim the extents (and move the addr) even though iomap code does | |
7545 | * that, since we have locked only the parts we are performing I/O in. | |
7546 | */ | |
7547 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
7548 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { | |
7549 | iomap->addr = IOMAP_NULL_ADDR; | |
7550 | iomap->type = IOMAP_HOLE; | |
7551 | } else { | |
7552 | iomap->addr = em->block_start + (start - em->start); | |
7553 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7554 | } |
f85781fb GR |
7555 | iomap->offset = start; |
7556 | iomap->bdev = fs_info->fs_devices->latest_bdev; | |
7557 | iomap->length = len; | |
a43a67a2 | 7558 | |
4b46fce2 JB |
7559 | free_extent_map(em); |
7560 | ||
7561 | return 0; | |
eb838e73 JB |
7562 | |
7563 | unlock_err: | |
e182163d OS |
7564 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7565 | &cached_state); | |
9c9464cc | 7566 | err: |
f85781fb GR |
7567 | if (dio_data) { |
7568 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7569 | dio_data->data_reserved, start, | |
7570 | dio_data->reserve, true); | |
7571 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); | |
7572 | extent_changeset_free(dio_data->data_reserved); | |
7573 | kfree(dio_data); | |
7574 | } | |
7575 | return ret; | |
7576 | } | |
7577 | ||
7578 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7579 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7580 | { | |
7581 | int ret = 0; | |
7582 | struct btrfs_dio_data *dio_data = iomap->private; | |
7583 | size_t submitted = dio_data->submitted; | |
7584 | const bool write = !!(flags & IOMAP_WRITE); | |
7585 | ||
7586 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7587 | /* If reading from a hole, unlock and return */ | |
7588 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); | |
7589 | goto out; | |
7590 | } | |
7591 | ||
7592 | if (submitted < length) { | |
7593 | pos += submitted; | |
7594 | length -= submitted; | |
7595 | if (write) | |
7596 | __endio_write_update_ordered(BTRFS_I(inode), pos, | |
7597 | length, false); | |
7598 | else | |
7599 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
7600 | pos + length - 1); | |
7601 | ret = -ENOTBLK; | |
7602 | } | |
7603 | ||
7604 | if (write) { | |
7605 | if (dio_data->reserve) | |
7606 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7607 | dio_data->data_reserved, pos, | |
7608 | dio_data->reserve, true); | |
7609 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); | |
7610 | extent_changeset_free(dio_data->data_reserved); | |
7611 | } | |
7612 | out: | |
7613 | kfree(dio_data); | |
7614 | iomap->private = NULL; | |
7615 | ||
8b110e39 MX |
7616 | return ret; |
7617 | } | |
7618 | ||
769b4f24 | 7619 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
8b110e39 | 7620 | { |
769b4f24 OS |
7621 | /* |
7622 | * This implies a barrier so that stores to dio_bio->bi_status before | |
7623 | * this and loads of dio_bio->bi_status after this are fully ordered. | |
7624 | */ | |
7625 | if (!refcount_dec_and_test(&dip->refs)) | |
7626 | return; | |
8b110e39 | 7627 | |
769b4f24 | 7628 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
b672b5c1 NB |
7629 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
7630 | dip->logical_offset, | |
769b4f24 OS |
7631 | dip->bytes, |
7632 | !dip->dio_bio->bi_status); | |
7633 | } else { | |
7634 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, | |
7635 | dip->logical_offset, | |
7636 | dip->logical_offset + dip->bytes - 1); | |
8b110e39 MX |
7637 | } |
7638 | ||
f85781fb | 7639 | bio_endio(dip->dio_bio); |
769b4f24 | 7640 | kfree(dip); |
8b110e39 MX |
7641 | } |
7642 | ||
77d5d689 OS |
7643 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7644 | int mirror_num, | |
7645 | unsigned long bio_flags) | |
8b110e39 | 7646 | { |
77d5d689 | 7647 | struct btrfs_dio_private *dip = bio->bi_private; |
2ff7e61e | 7648 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7649 | blk_status_t ret; |
8b110e39 | 7650 | |
37226b21 | 7651 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7652 | |
5c047a69 | 7653 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
8b110e39 | 7654 | if (ret) |
ea057f6d | 7655 | return ret; |
8b110e39 | 7656 | |
77d5d689 | 7657 | refcount_inc(&dip->refs); |
08635bae | 7658 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
8b110e39 | 7659 | if (ret) |
fd9d6670 | 7660 | refcount_dec(&dip->refs); |
77d5d689 | 7661 | return ret; |
8b110e39 MX |
7662 | } |
7663 | ||
fd9d6670 OS |
7664 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
7665 | struct btrfs_io_bio *io_bio, | |
7666 | const bool uptodate) | |
4b46fce2 | 7667 | { |
fd9d6670 OS |
7668 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
7669 | const u32 sectorsize = fs_info->sectorsize; | |
7670 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
7671 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
7672 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); | |
17347cec LB |
7673 | struct bio_vec bvec; |
7674 | struct bvec_iter iter; | |
fd9d6670 OS |
7675 | u64 start = io_bio->logical; |
7676 | int icsum = 0; | |
58efbc9f | 7677 | blk_status_t err = BLK_STS_OK; |
4b46fce2 | 7678 | |
fd9d6670 OS |
7679 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
7680 | unsigned int i, nr_sectors, pgoff; | |
8b110e39 | 7681 | |
17347cec LB |
7682 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
7683 | pgoff = bvec.bv_offset; | |
fd9d6670 | 7684 | for (i = 0; i < nr_sectors; i++) { |
97bf5a55 | 7685 | ASSERT(pgoff < PAGE_SIZE); |
fd9d6670 OS |
7686 | if (uptodate && |
7687 | (!csum || !check_data_csum(inode, io_bio, icsum, | |
265d4ac0 | 7688 | bvec.bv_page, pgoff))) { |
fd9d6670 OS |
7689 | clean_io_failure(fs_info, failure_tree, io_tree, |
7690 | start, bvec.bv_page, | |
7691 | btrfs_ino(BTRFS_I(inode)), | |
7692 | pgoff); | |
7693 | } else { | |
7694 | blk_status_t status; | |
7695 | ||
77d5d689 OS |
7696 | status = btrfs_submit_read_repair(inode, |
7697 | &io_bio->bio, | |
7698 | start - io_bio->logical, | |
fd9d6670 OS |
7699 | bvec.bv_page, pgoff, |
7700 | start, | |
7701 | start + sectorsize - 1, | |
77d5d689 OS |
7702 | io_bio->mirror_num, |
7703 | submit_dio_repair_bio); | |
fd9d6670 OS |
7704 | if (status) |
7705 | err = status; | |
7706 | } | |
7707 | start += sectorsize; | |
7708 | icsum++; | |
2dabb324 | 7709 | pgoff += sectorsize; |
2dabb324 | 7710 | } |
2c30c71b | 7711 | } |
c1dc0896 MX |
7712 | return err; |
7713 | } | |
7714 | ||
b672b5c1 | 7715 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
7716 | const u64 offset, const u64 bytes, |
7717 | const bool uptodate) | |
4b46fce2 | 7718 | { |
b672b5c1 | 7719 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4b46fce2 | 7720 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 | 7721 | struct btrfs_workqueue *wq; |
14543774 FM |
7722 | u64 ordered_offset = offset; |
7723 | u64 ordered_bytes = bytes; | |
67c003f9 | 7724 | u64 last_offset; |
4b46fce2 | 7725 | |
b672b5c1 | 7726 | if (btrfs_is_free_space_inode(inode)) |
52427260 | 7727 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 7728 | else |
52427260 | 7729 | wq = fs_info->endio_write_workers; |
52427260 | 7730 | |
b25f0d00 NB |
7731 | while (ordered_offset < offset + bytes) { |
7732 | last_offset = ordered_offset; | |
b672b5c1 | 7733 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
7095821e NB |
7734 | &ordered_offset, |
7735 | ordered_bytes, | |
7736 | uptodate)) { | |
a0cac0ec OS |
7737 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
7738 | NULL); | |
b25f0d00 NB |
7739 | btrfs_queue_work(wq, &ordered->work); |
7740 | } | |
7741 | /* | |
7742 | * If btrfs_dec_test_ordered_pending does not find any ordered | |
7743 | * extent in the range, we can exit. | |
7744 | */ | |
7745 | if (ordered_offset == last_offset) | |
7746 | return; | |
7747 | /* | |
7748 | * Our bio might span multiple ordered extents. In this case | |
52042d8e | 7749 | * we keep going until we have accounted the whole dio. |
b25f0d00 NB |
7750 | */ |
7751 | if (ordered_offset < offset + bytes) { | |
7752 | ordered_bytes = offset + bytes - ordered_offset; | |
7753 | ordered = NULL; | |
7754 | } | |
163cf09c | 7755 | } |
14543774 FM |
7756 | } |
7757 | ||
8896a08d QW |
7758 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
7759 | struct bio *bio, u64 offset) | |
eaf25d93 | 7760 | { |
c965d640 | 7761 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
eaf25d93 CM |
7762 | } |
7763 | ||
4246a0b6 | 7764 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
7765 | { |
7766 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 7767 | blk_status_t err = bio->bi_status; |
e65e1535 | 7768 | |
8b110e39 MX |
7769 | if (err) |
7770 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 7771 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
7772 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
7773 | bio->bi_opf, | |
8b110e39 MX |
7774 | (unsigned long long)bio->bi_iter.bi_sector, |
7775 | bio->bi_iter.bi_size, err); | |
7776 | ||
769b4f24 OS |
7777 | if (bio_op(bio) == REQ_OP_READ) { |
7778 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), | |
fd9d6670 | 7779 | !err); |
e65e1535 MX |
7780 | } |
7781 | ||
769b4f24 OS |
7782 | if (err) |
7783 | dip->dio_bio->bi_status = err; | |
e65e1535 | 7784 | |
e65e1535 | 7785 | bio_put(bio); |
769b4f24 | 7786 | btrfs_dio_private_put(dip); |
c1dc0896 MX |
7787 | } |
7788 | ||
d0ee3934 DS |
7789 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
7790 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 7791 | { |
0b246afa | 7792 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 7793 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 7794 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 7795 | blk_status_t ret; |
e65e1535 | 7796 | |
4c274bc6 | 7797 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
7798 | if (async_submit) |
7799 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
7800 | ||
5fd02043 | 7801 | if (!write) { |
0b246afa | 7802 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
7803 | if (ret) |
7804 | goto err; | |
7805 | } | |
e65e1535 | 7806 | |
e6961cac | 7807 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
7808 | goto map; |
7809 | ||
7810 | if (write && async_submit) { | |
8896a08d QW |
7811 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, |
7812 | file_offset, | |
e288c080 | 7813 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 7814 | goto err; |
1ae39938 JB |
7815 | } else if (write) { |
7816 | /* | |
7817 | * If we aren't doing async submit, calculate the csum of the | |
7818 | * bio now. | |
7819 | */ | |
bd242a08 | 7820 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
1ae39938 JB |
7821 | if (ret) |
7822 | goto err; | |
23ea8e5a | 7823 | } else { |
85879573 OS |
7824 | u64 csum_offset; |
7825 | ||
7826 | csum_offset = file_offset - dip->logical_offset; | |
7827 | csum_offset >>= inode->i_sb->s_blocksize_bits; | |
7828 | csum_offset *= btrfs_super_csum_size(fs_info->super_copy); | |
7829 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; | |
c2db1073 | 7830 | } |
1ae39938 | 7831 | map: |
08635bae | 7832 | ret = btrfs_map_bio(fs_info, bio, 0); |
e65e1535 | 7833 | err: |
e65e1535 MX |
7834 | return ret; |
7835 | } | |
7836 | ||
c36cac28 OS |
7837 | /* |
7838 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked | |
7839 | * or ordered extents whether or not we submit any bios. | |
7840 | */ | |
7841 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, | |
7842 | struct inode *inode, | |
7843 | loff_t file_offset) | |
e65e1535 | 7844 | { |
c36cac28 | 7845 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
85879573 OS |
7846 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
7847 | size_t dip_size; | |
c36cac28 | 7848 | struct btrfs_dio_private *dip; |
c36cac28 | 7849 | |
85879573 OS |
7850 | dip_size = sizeof(*dip); |
7851 | if (!write && csum) { | |
7852 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7853 | const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); | |
7854 | size_t nblocks; | |
7855 | ||
7856 | nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; | |
7857 | dip_size += csum_size * nblocks; | |
7858 | } | |
7859 | ||
7860 | dip = kzalloc(dip_size, GFP_NOFS); | |
c36cac28 OS |
7861 | if (!dip) |
7862 | return NULL; | |
7863 | ||
c36cac28 OS |
7864 | dip->inode = inode; |
7865 | dip->logical_offset = file_offset; | |
7866 | dip->bytes = dio_bio->bi_iter.bi_size; | |
7867 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
c36cac28 | 7868 | dip->dio_bio = dio_bio; |
e3b318d1 | 7869 | refcount_set(&dip->refs, 1); |
c36cac28 OS |
7870 | return dip; |
7871 | } | |
7872 | ||
f85781fb GR |
7873 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
7874 | struct bio *dio_bio, loff_t file_offset) | |
c36cac28 OS |
7875 | { |
7876 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); | |
0b246afa | 7877 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
769b4f24 OS |
7878 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
7879 | BTRFS_BLOCK_GROUP_RAID56_MASK); | |
c36cac28 | 7880 | struct btrfs_dio_private *dip; |
e65e1535 | 7881 | struct bio *bio; |
c36cac28 | 7882 | u64 start_sector; |
1ae39938 | 7883 | int async_submit = 0; |
725130ba LB |
7884 | u64 submit_len; |
7885 | int clone_offset = 0; | |
7886 | int clone_len; | |
5f4dc8fc | 7887 | int ret; |
58efbc9f | 7888 | blk_status_t status; |
89b798ad | 7889 | struct btrfs_io_geometry geom; |
f85781fb | 7890 | struct btrfs_dio_data *dio_data = iomap->private; |
e65e1535 | 7891 | |
c36cac28 OS |
7892 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
7893 | if (!dip) { | |
7894 | if (!write) { | |
7895 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
7896 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
7897 | } | |
7898 | dio_bio->bi_status = BLK_STS_RESOURCE; | |
f85781fb GR |
7899 | bio_endio(dio_bio); |
7900 | return BLK_QC_T_NONE; | |
c36cac28 | 7901 | } |
facc8a22 | 7902 | |
334c16d8 | 7903 | if (!write) { |
85879573 OS |
7904 | /* |
7905 | * Load the csums up front to reduce csum tree searches and | |
7906 | * contention when submitting bios. | |
334c16d8 JB |
7907 | * |
7908 | * If we have csums disabled this will do nothing. | |
85879573 OS |
7909 | */ |
7910 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, | |
7911 | dip->csums); | |
7912 | if (status != BLK_STS_OK) | |
7913 | goto out_err; | |
02f57c7a JB |
7914 | } |
7915 | ||
769b4f24 OS |
7916 | start_sector = dio_bio->bi_iter.bi_sector; |
7917 | submit_len = dio_bio->bi_iter.bi_size; | |
53b381b3 | 7918 | |
3c91ee69 | 7919 | do { |
769b4f24 OS |
7920 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
7921 | start_sector << 9, submit_len, | |
7922 | &geom); | |
7923 | if (ret) { | |
7924 | status = errno_to_blk_status(ret); | |
7925 | goto out_err; | |
7926 | } | |
7927 | ASSERT(geom.len <= INT_MAX); | |
7928 | ||
89b798ad | 7929 | clone_len = min_t(int, submit_len, geom.len); |
02f57c7a | 7930 | |
725130ba LB |
7931 | /* |
7932 | * This will never fail as it's passing GPF_NOFS and | |
7933 | * the allocation is backed by btrfs_bioset. | |
7934 | */ | |
769b4f24 | 7935 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
725130ba LB |
7936 | bio->bi_private = dip; |
7937 | bio->bi_end_io = btrfs_end_dio_bio; | |
7938 | btrfs_io_bio(bio)->logical = file_offset; | |
7939 | ||
7940 | ASSERT(submit_len >= clone_len); | |
7941 | submit_len -= clone_len; | |
e65e1535 | 7942 | |
725130ba LB |
7943 | /* |
7944 | * Increase the count before we submit the bio so we know | |
7945 | * the end IO handler won't happen before we increase the | |
7946 | * count. Otherwise, the dip might get freed before we're | |
7947 | * done setting it up. | |
769b4f24 OS |
7948 | * |
7949 | * We transfer the initial reference to the last bio, so we | |
7950 | * don't need to increment the reference count for the last one. | |
725130ba | 7951 | */ |
769b4f24 OS |
7952 | if (submit_len > 0) { |
7953 | refcount_inc(&dip->refs); | |
7954 | /* | |
7955 | * If we are submitting more than one bio, submit them | |
7956 | * all asynchronously. The exception is RAID 5 or 6, as | |
7957 | * asynchronous checksums make it difficult to collect | |
7958 | * full stripe writes. | |
7959 | */ | |
7960 | if (!raid56) | |
7961 | async_submit = 1; | |
7962 | } | |
e65e1535 | 7963 | |
d0ee3934 | 7964 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
7965 | async_submit); |
7966 | if (status) { | |
725130ba | 7967 | bio_put(bio); |
769b4f24 OS |
7968 | if (submit_len > 0) |
7969 | refcount_dec(&dip->refs); | |
725130ba LB |
7970 | goto out_err; |
7971 | } | |
e65e1535 | 7972 | |
f85781fb | 7973 | dio_data->submitted += clone_len; |
725130ba LB |
7974 | clone_offset += clone_len; |
7975 | start_sector += clone_len >> 9; | |
7976 | file_offset += clone_len; | |
3c91ee69 | 7977 | } while (submit_len > 0); |
f85781fb | 7978 | return BLK_QC_T_NONE; |
e65e1535 | 7979 | |
e65e1535 | 7980 | out_err: |
769b4f24 OS |
7981 | dip->dio_bio->bi_status = status; |
7982 | btrfs_dio_private_put(dip); | |
f85781fb | 7983 | return BLK_QC_T_NONE; |
4b46fce2 JB |
7984 | } |
7985 | ||
4e4cabec | 7986 | const struct iomap_ops btrfs_dio_iomap_ops = { |
f85781fb GR |
7987 | .iomap_begin = btrfs_dio_iomap_begin, |
7988 | .iomap_end = btrfs_dio_iomap_end, | |
7989 | }; | |
7990 | ||
4e4cabec | 7991 | const struct iomap_dio_ops btrfs_dio_ops = { |
f85781fb GR |
7992 | .submit_io = btrfs_submit_direct, |
7993 | }; | |
7994 | ||
1506fcc8 | 7995 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 7996 | u64 start, u64 len) |
1506fcc8 | 7997 | { |
05dadc09 TI |
7998 | int ret; |
7999 | ||
45dd052e | 8000 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
8001 | if (ret) |
8002 | return ret; | |
8003 | ||
facee0a0 | 8004 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
1506fcc8 YS |
8005 | } |
8006 | ||
a52d9a80 | 8007 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8008 | { |
0f208812 NB |
8009 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
8010 | u64 start = page_offset(page); | |
8011 | u64 end = start + PAGE_SIZE - 1; | |
c1be9c1a | 8012 | unsigned long bio_flags = 0; |
0f208812 | 8013 | struct bio *bio = NULL; |
c1be9c1a NB |
8014 | int ret; |
8015 | ||
0f208812 NB |
8016 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
8017 | ||
8018 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); | |
c1be9c1a NB |
8019 | if (bio) |
8020 | ret = submit_one_bio(bio, 0, bio_flags); | |
8021 | return ret; | |
9ebefb18 | 8022 | } |
1832a6d5 | 8023 | |
a52d9a80 | 8024 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8025 | { |
be7bd730 JB |
8026 | struct inode *inode = page->mapping->host; |
8027 | int ret; | |
b888db2b CM |
8028 | |
8029 | if (current->flags & PF_MEMALLOC) { | |
8030 | redirty_page_for_writepage(wbc, page); | |
8031 | unlock_page(page); | |
8032 | return 0; | |
8033 | } | |
be7bd730 JB |
8034 | |
8035 | /* | |
8036 | * If we are under memory pressure we will call this directly from the | |
8037 | * VM, we need to make sure we have the inode referenced for the ordered | |
8038 | * extent. If not just return like we didn't do anything. | |
8039 | */ | |
8040 | if (!igrab(inode)) { | |
8041 | redirty_page_for_writepage(wbc, page); | |
8042 | return AOP_WRITEPAGE_ACTIVATE; | |
8043 | } | |
0a9b0e53 | 8044 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8045 | btrfs_add_delayed_iput(inode); |
8046 | return ret; | |
9ebefb18 CM |
8047 | } |
8048 | ||
48a3b636 ES |
8049 | static int btrfs_writepages(struct address_space *mapping, |
8050 | struct writeback_control *wbc) | |
b293f02e | 8051 | { |
8ae225a8 | 8052 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8053 | } |
8054 | ||
ba206a02 | 8055 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 8056 | { |
ba206a02 | 8057 | extent_readahead(rac); |
3ab2fb5a | 8058 | } |
2a3ff0ad | 8059 | |
e6dcd2dc | 8060 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8061 | { |
477a30ba | 8062 | int ret = try_release_extent_mapping(page, gfp_flags); |
d1b89bc0 GJ |
8063 | if (ret == 1) |
8064 | detach_page_private(page); | |
a52d9a80 | 8065 | return ret; |
39279cc3 CM |
8066 | } |
8067 | ||
e6dcd2dc CM |
8068 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8069 | { | |
98509cfc CM |
8070 | if (PageWriteback(page) || PageDirty(page)) |
8071 | return 0; | |
3ba7ab22 | 8072 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8073 | } |
8074 | ||
f8e66081 RG |
8075 | #ifdef CONFIG_MIGRATION |
8076 | static int btrfs_migratepage(struct address_space *mapping, | |
8077 | struct page *newpage, struct page *page, | |
8078 | enum migrate_mode mode) | |
8079 | { | |
8080 | int ret; | |
8081 | ||
8082 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); | |
8083 | if (ret != MIGRATEPAGE_SUCCESS) | |
8084 | return ret; | |
8085 | ||
d1b89bc0 GJ |
8086 | if (page_has_private(page)) |
8087 | attach_page_private(newpage, detach_page_private(page)); | |
f8e66081 RG |
8088 | |
8089 | if (PagePrivate2(page)) { | |
8090 | ClearPagePrivate2(page); | |
8091 | SetPagePrivate2(newpage); | |
8092 | } | |
8093 | ||
8094 | if (mode != MIGRATE_SYNC_NO_COPY) | |
8095 | migrate_page_copy(newpage, page); | |
8096 | else | |
8097 | migrate_page_states(newpage, page); | |
8098 | return MIGRATEPAGE_SUCCESS; | |
8099 | } | |
8100 | #endif | |
8101 | ||
d47992f8 LC |
8102 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8103 | unsigned int length) | |
39279cc3 | 8104 | { |
53ac7ead NB |
8105 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
8106 | struct extent_io_tree *tree = &inode->io_tree; | |
e6dcd2dc | 8107 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8108 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8109 | u64 page_start = page_offset(page); |
09cbfeaf | 8110 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8111 | u64 start; |
8112 | u64 end; | |
53ac7ead | 8113 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 8114 | |
8b62b72b CM |
8115 | /* |
8116 | * we have the page locked, so new writeback can't start, | |
8117 | * and the dirty bit won't be cleared while we are here. | |
8118 | * | |
8119 | * Wait for IO on this page so that we can safely clear | |
8120 | * the PagePrivate2 bit and do ordered accounting | |
8121 | */ | |
e6dcd2dc | 8122 | wait_on_page_writeback(page); |
8b62b72b | 8123 | |
e6dcd2dc CM |
8124 | if (offset) { |
8125 | btrfs_releasepage(page, GFP_NOFS); | |
8126 | return; | |
8127 | } | |
131e404a FDBM |
8128 | |
8129 | if (!inode_evicting) | |
ff13db41 | 8130 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8131 | again: |
8132 | start = page_start; | |
53ac7ead | 8133 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
e6dcd2dc | 8134 | if (ordered) { |
bffe633e OS |
8135 | end = min(page_end, |
8136 | ordered->file_offset + ordered->num_bytes - 1); | |
eb84ae03 CM |
8137 | /* |
8138 | * IO on this page will never be started, so we need | |
8139 | * to account for any ordered extents now | |
8140 | */ | |
131e404a | 8141 | if (!inode_evicting) |
dbfdb6d1 | 8142 | clear_extent_bit(tree, start, end, |
e182163d | 8143 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
131e404a | 8144 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8145 | EXTENT_DEFRAG, 1, 0, &cached_state); |
8b62b72b CM |
8146 | /* |
8147 | * whoever cleared the private bit is responsible | |
8148 | * for the finish_ordered_io | |
8149 | */ | |
77cef2ec JB |
8150 | if (TestClearPagePrivate2(page)) { |
8151 | struct btrfs_ordered_inode_tree *tree; | |
8152 | u64 new_len; | |
8153 | ||
53ac7ead | 8154 | tree = &inode->ordered_tree; |
77cef2ec JB |
8155 | |
8156 | spin_lock_irq(&tree->lock); | |
8157 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8158 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8159 | if (new_len < ordered->truncated_len) |
8160 | ordered->truncated_len = new_len; | |
8161 | spin_unlock_irq(&tree->lock); | |
8162 | ||
53ac7ead NB |
8163 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
8164 | start, | |
dbfdb6d1 | 8165 | end - start + 1, 1)) |
77cef2ec | 8166 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8167 | } |
e6dcd2dc | 8168 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8169 | if (!inode_evicting) { |
8170 | cached_state = NULL; | |
dbfdb6d1 | 8171 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8172 | &cached_state); |
8173 | } | |
dbfdb6d1 CR |
8174 | |
8175 | start = end + 1; | |
8176 | if (start < page_end) | |
8177 | goto again; | |
131e404a FDBM |
8178 | } |
8179 | ||
b9d0b389 QW |
8180 | /* |
8181 | * Qgroup reserved space handler | |
8182 | * Page here will be either | |
fa91e4aa QW |
8183 | * 1) Already written to disk or ordered extent already submitted |
8184 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. | |
8185 | * Qgroup will be handled by its qgroup_record then. | |
8186 | * btrfs_qgroup_free_data() call will do nothing here. | |
8187 | * | |
8188 | * 2) Not written to disk yet | |
8189 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED | |
8190 | * bit of its io_tree, and free the qgroup reserved data space. | |
8191 | * Since the IO will never happen for this page. | |
b9d0b389 | 8192 | */ |
53ac7ead | 8193 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a | 8194 | if (!inode_evicting) { |
e182163d | 8195 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
a7e3b975 FM |
8196 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
8197 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
ae0f1625 | 8198 | &cached_state); |
131e404a FDBM |
8199 | |
8200 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8201 | } |
e6dcd2dc | 8202 | |
4a096752 | 8203 | ClearPageChecked(page); |
d1b89bc0 | 8204 | detach_page_private(page); |
39279cc3 CM |
8205 | } |
8206 | ||
9ebefb18 CM |
8207 | /* |
8208 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8209 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8210 | * be careful to check for EOF conditions here. We set the page up correctly | |
8211 | * for a written page which means we get ENOSPC checking when writing into | |
8212 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8213 | * support these features. | |
8214 | * | |
8215 | * We are not allowed to take the i_mutex here so we have to play games to | |
8216 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8217 | * truncate_setsize() writes the inode size before removing pages, once we have |
8218 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8219 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8220 | * unlock the page. | |
8221 | */ | |
a528a241 | 8222 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8223 | { |
c2ec175c | 8224 | struct page *page = vmf->page; |
11bac800 | 8225 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8226 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8227 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8228 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8229 | struct extent_state *cached_state = NULL; |
364ecf36 | 8230 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
8231 | char *kaddr; |
8232 | unsigned long zero_start; | |
9ebefb18 | 8233 | loff_t size; |
a528a241 SJ |
8234 | vm_fault_t ret; |
8235 | int ret2; | |
9998eb70 | 8236 | int reserved = 0; |
d0b7da88 | 8237 | u64 reserved_space; |
a52d9a80 | 8238 | u64 page_start; |
e6dcd2dc | 8239 | u64 page_end; |
d0b7da88 CR |
8240 | u64 end; |
8241 | ||
09cbfeaf | 8242 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8243 | |
b2b5ef5c | 8244 | sb_start_pagefault(inode->i_sb); |
df480633 | 8245 | page_start = page_offset(page); |
09cbfeaf | 8246 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8247 | end = page_end; |
df480633 | 8248 | |
d0b7da88 CR |
8249 | /* |
8250 | * Reserving delalloc space after obtaining the page lock can lead to | |
8251 | * deadlock. For example, if a dirty page is locked by this function | |
8252 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8253 | * dirty page write out, then the btrfs_writepage() function could | |
8254 | * end up waiting indefinitely to get a lock on the page currently | |
8255 | * being processed by btrfs_page_mkwrite() function. | |
8256 | */ | |
e5b7231e NB |
8257 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8258 | page_start, reserved_space); | |
a528a241 SJ |
8259 | if (!ret2) { |
8260 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8261 | reserved = 1; |
8262 | } | |
a528a241 SJ |
8263 | if (ret2) { |
8264 | ret = vmf_error(ret2); | |
9998eb70 CM |
8265 | if (reserved) |
8266 | goto out; | |
8267 | goto out_noreserve; | |
56a76f82 | 8268 | } |
1832a6d5 | 8269 | |
56a76f82 | 8270 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8271 | again: |
9ebefb18 | 8272 | lock_page(page); |
9ebefb18 | 8273 | size = i_size_read(inode); |
a52d9a80 | 8274 | |
9ebefb18 | 8275 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8276 | (page_start >= size)) { |
9ebefb18 CM |
8277 | /* page got truncated out from underneath us */ |
8278 | goto out_unlock; | |
8279 | } | |
e6dcd2dc CM |
8280 | wait_on_page_writeback(page); |
8281 | ||
ff13db41 | 8282 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
8283 | set_page_extent_mapped(page); |
8284 | ||
eb84ae03 CM |
8285 | /* |
8286 | * we can't set the delalloc bits if there are pending ordered | |
8287 | * extents. Drop our locks and wait for them to finish | |
8288 | */ | |
a776c6fa NB |
8289 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8290 | PAGE_SIZE); | |
e6dcd2dc | 8291 | if (ordered) { |
2ac55d41 | 8292 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8293 | &cached_state); |
e6dcd2dc | 8294 | unlock_page(page); |
c0a43603 | 8295 | btrfs_start_ordered_extent(ordered, 1); |
e6dcd2dc CM |
8296 | btrfs_put_ordered_extent(ordered); |
8297 | goto again; | |
8298 | } | |
8299 | ||
09cbfeaf | 8300 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8301 | reserved_space = round_up(size - page_start, |
0b246afa | 8302 | fs_info->sectorsize); |
09cbfeaf | 8303 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8304 | end = page_start + reserved_space - 1; |
86d52921 NB |
8305 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8306 | data_reserved, page_start, | |
8307 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8308 | } |
8309 | } | |
8310 | ||
fbf19087 | 8311 | /* |
5416034f LB |
8312 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8313 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8314 | * bits, thus in this case for space account reason, we still need to | |
8315 | * clear any delalloc bits within this page range since we have to | |
8316 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8317 | */ |
d0b7da88 | 8318 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d OS |
8319 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
8320 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
fbf19087 | 8321 | |
c2566f22 | 8322 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8323 | &cached_state); |
a528a241 | 8324 | if (ret2) { |
2ac55d41 | 8325 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8326 | &cached_state); |
9ed74f2d JB |
8327 | ret = VM_FAULT_SIGBUS; |
8328 | goto out_unlock; | |
8329 | } | |
9ebefb18 CM |
8330 | |
8331 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8332 | if (page_start + PAGE_SIZE > size) |
7073017a | 8333 | zero_start = offset_in_page(size); |
9ebefb18 | 8334 | else |
09cbfeaf | 8335 | zero_start = PAGE_SIZE; |
9ebefb18 | 8336 | |
09cbfeaf | 8337 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 8338 | kaddr = kmap(page); |
09cbfeaf | 8339 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
8340 | flush_dcache_page(page); |
8341 | kunmap(page); | |
8342 | } | |
247e743c | 8343 | ClearPageChecked(page); |
e6dcd2dc | 8344 | set_page_dirty(page); |
50a9b214 | 8345 | SetPageUptodate(page); |
5a3f23d5 | 8346 | |
0b246afa | 8347 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 8348 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 8349 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8350 | |
e43bbe5e | 8351 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
9ebefb18 | 8352 | |
76de60ed YY |
8353 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8354 | sb_end_pagefault(inode->i_sb); | |
8355 | extent_changeset_free(data_reserved); | |
8356 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8357 | |
8358 | out_unlock: | |
9ebefb18 | 8359 | unlock_page(page); |
1832a6d5 | 8360 | out: |
8702ba93 | 8361 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8362 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8363 | reserved_space, (ret != 0)); |
9998eb70 | 8364 | out_noreserve: |
b2b5ef5c | 8365 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8366 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8367 | return ret; |
8368 | } | |
8369 | ||
213e8c55 | 8370 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8371 | { |
0b246afa | 8372 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8373 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8374 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8375 | int ret; |
39279cc3 | 8376 | struct btrfs_trans_handle *trans; |
0b246afa | 8377 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 8378 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8379 | |
213e8c55 FM |
8380 | if (!skip_writeback) { |
8381 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8382 | (u64)-1); | |
8383 | if (ret) | |
8384 | return ret; | |
8385 | } | |
39279cc3 | 8386 | |
fcb80c2a | 8387 | /* |
f7e9e8fc OS |
8388 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8389 | * things going on here: | |
fcb80c2a | 8390 | * |
f7e9e8fc | 8391 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8392 | * |
f7e9e8fc | 8393 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8394 | * be free'd up by the truncate operation, but also have some slack |
8395 | * space reserved in case it uses space during the truncate (thank you | |
8396 | * very much snapshotting). | |
8397 | * | |
f7e9e8fc | 8398 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8399 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8400 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8401 | * doesn't end up using space reserved for updating the inode. We also |
8402 | * need to be able to stop the transaction and start a new one, which | |
8403 | * means we need to be able to update the inode several times, and we | |
8404 | * have no idea of knowing how many times that will be, so we can't just | |
8405 | * reserve 1 item for the entirety of the operation, so that has to be | |
8406 | * done separately as well. | |
fcb80c2a JB |
8407 | * |
8408 | * So that leaves us with | |
8409 | * | |
f7e9e8fc | 8410 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8411 | * transaction reservation. |
f7e9e8fc | 8412 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8413 | * updating the inode. |
8414 | */ | |
2ff7e61e | 8415 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8416 | if (!rsv) |
8417 | return -ENOMEM; | |
4a338542 | 8418 | rsv->size = min_size; |
ca7e70f5 | 8419 | rsv->failfast = 1; |
f0cd846e | 8420 | |
907cbceb | 8421 | /* |
07127184 | 8422 | * 1 for the truncate slack space |
907cbceb JB |
8423 | * 1 for updating the inode. |
8424 | */ | |
f3fe820c | 8425 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8426 | if (IS_ERR(trans)) { |
ad7e1a74 | 8427 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8428 | goto out; |
8429 | } | |
f0cd846e | 8430 | |
907cbceb | 8431 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8432 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8433 | min_size, false); |
fcb80c2a | 8434 | BUG_ON(ret); |
f0cd846e | 8435 | |
5dc562c5 JB |
8436 | /* |
8437 | * So if we truncate and then write and fsync we normally would just | |
8438 | * write the extents that changed, which is a problem if we need to | |
8439 | * first truncate that entire inode. So set this flag so we write out | |
8440 | * all of the extents in the inode to the sync log so we're completely | |
8441 | * safe. | |
8442 | */ | |
8443 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8444 | trans->block_rsv = rsv; |
907cbceb | 8445 | |
8082510e YZ |
8446 | while (1) { |
8447 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8448 | inode->i_size, | |
8449 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 8450 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8451 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8452 | break; |
39279cc3 | 8453 | |
8082510e | 8454 | ret = btrfs_update_inode(trans, root, inode); |
ad7e1a74 | 8455 | if (ret) |
3893e33b | 8456 | break; |
ca7e70f5 | 8457 | |
3a45bb20 | 8458 | btrfs_end_transaction(trans); |
2ff7e61e | 8459 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8460 | |
8461 | trans = btrfs_start_transaction(root, 2); | |
8462 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8463 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8464 | trans = NULL; |
8465 | break; | |
8466 | } | |
8467 | ||
63f018be | 8468 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8469 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8470 | rsv, min_size, false); |
ca7e70f5 JB |
8471 | BUG_ON(ret); /* shouldn't happen */ |
8472 | trans->block_rsv = rsv; | |
8082510e YZ |
8473 | } |
8474 | ||
ddfae63c JB |
8475 | /* |
8476 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
8477 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
8478 | * we've truncated everything except the last little bit, and can do | |
8479 | * btrfs_truncate_block and then update the disk_i_size. | |
8480 | */ | |
8481 | if (ret == NEED_TRUNCATE_BLOCK) { | |
8482 | btrfs_end_transaction(trans); | |
8483 | btrfs_btree_balance_dirty(fs_info); | |
8484 | ||
8485 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
8486 | if (ret) | |
8487 | goto out; | |
8488 | trans = btrfs_start_transaction(root, 1); | |
8489 | if (IS_ERR(trans)) { | |
8490 | ret = PTR_ERR(trans); | |
8491 | goto out; | |
8492 | } | |
d923afe9 | 8493 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
ddfae63c JB |
8494 | } |
8495 | ||
917c16b2 | 8496 | if (trans) { |
ad7e1a74 OS |
8497 | int ret2; |
8498 | ||
0b246afa | 8499 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 OS |
8500 | ret2 = btrfs_update_inode(trans, root, inode); |
8501 | if (ret2 && !ret) | |
8502 | ret = ret2; | |
7b128766 | 8503 | |
ad7e1a74 OS |
8504 | ret2 = btrfs_end_transaction(trans); |
8505 | if (ret2 && !ret) | |
8506 | ret = ret2; | |
2ff7e61e | 8507 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8508 | } |
fcb80c2a | 8509 | out: |
2ff7e61e | 8510 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 8511 | |
ad7e1a74 | 8512 | return ret; |
39279cc3 CM |
8513 | } |
8514 | ||
d352ac68 CM |
8515 | /* |
8516 | * create a new subvolume directory/inode (helper for the ioctl). | |
8517 | */ | |
d2fb3437 | 8518 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8519 | struct btrfs_root *new_root, |
8520 | struct btrfs_root *parent_root, | |
8521 | u64 new_dirid) | |
39279cc3 | 8522 | { |
39279cc3 | 8523 | struct inode *inode; |
76dda93c | 8524 | int err; |
00e4e6b3 | 8525 | u64 index = 0; |
39279cc3 | 8526 | |
12fc9d09 FA |
8527 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8528 | new_dirid, new_dirid, | |
8529 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8530 | &index); | |
54aa1f4d | 8531 | if (IS_ERR(inode)) |
f46b5a66 | 8532 | return PTR_ERR(inode); |
39279cc3 CM |
8533 | inode->i_op = &btrfs_dir_inode_operations; |
8534 | inode->i_fop = &btrfs_dir_file_operations; | |
8535 | ||
bfe86848 | 8536 | set_nlink(inode, 1); |
6ef06d27 | 8537 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 8538 | unlock_new_inode(inode); |
3b96362c | 8539 | |
63541927 FDBM |
8540 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8541 | if (err) | |
8542 | btrfs_err(new_root->fs_info, | |
351fd353 | 8543 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8544 | new_root->root_key.objectid, err); |
8545 | ||
76dda93c | 8546 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8547 | |
76dda93c | 8548 | iput(inode); |
ce598979 | 8549 | return err; |
39279cc3 CM |
8550 | } |
8551 | ||
39279cc3 CM |
8552 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8553 | { | |
69fe2d75 | 8554 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8555 | struct btrfs_inode *ei; |
2ead6ae7 | 8556 | struct inode *inode; |
39279cc3 | 8557 | |
712e36c5 | 8558 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
8559 | if (!ei) |
8560 | return NULL; | |
2ead6ae7 YZ |
8561 | |
8562 | ei->root = NULL; | |
2ead6ae7 | 8563 | ei->generation = 0; |
15ee9bc7 | 8564 | ei->last_trans = 0; |
257c62e1 | 8565 | ei->last_sub_trans = 0; |
e02119d5 | 8566 | ei->logged_trans = 0; |
2ead6ae7 | 8567 | ei->delalloc_bytes = 0; |
a7e3b975 | 8568 | ei->new_delalloc_bytes = 0; |
47059d93 | 8569 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8570 | ei->disk_i_size = 0; |
8571 | ei->flags = 0; | |
7709cde3 | 8572 | ei->csum_bytes = 0; |
2ead6ae7 | 8573 | ei->index_cnt = (u64)-1; |
67de1176 | 8574 | ei->dir_index = 0; |
2ead6ae7 | 8575 | ei->last_unlink_trans = 0; |
3ebac17c | 8576 | ei->last_reflink_trans = 0; |
46d8bc34 | 8577 | ei->last_log_commit = 0; |
2ead6ae7 | 8578 | |
9e0baf60 JB |
8579 | spin_lock_init(&ei->lock); |
8580 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8581 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8582 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8583 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8584 | ei->runtime_flags = 0; |
b52aa8c9 | 8585 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8586 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8587 | |
16cdcec7 MX |
8588 | ei->delayed_node = NULL; |
8589 | ||
9cc97d64 | 8590 | ei->i_otime.tv_sec = 0; |
8591 | ei->i_otime.tv_nsec = 0; | |
8592 | ||
2ead6ae7 | 8593 | inode = &ei->vfs_inode; |
a8067e02 | 8594 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 QW |
8595 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
8596 | extent_io_tree_init(fs_info, &ei->io_failure_tree, | |
8597 | IO_TREE_INODE_IO_FAILURE, inode); | |
41a2ee75 JB |
8598 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
8599 | IO_TREE_INODE_FILE_EXTENT, inode); | |
7b439738 DS |
8600 | ei->io_tree.track_uptodate = true; |
8601 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 8602 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8603 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 8604 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8605 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8606 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 YZ |
8607 | RB_CLEAR_NODE(&ei->rb_node); |
8608 | ||
8609 | return inode; | |
39279cc3 CM |
8610 | } |
8611 | ||
aaedb55b JB |
8612 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8613 | void btrfs_test_destroy_inode(struct inode *inode) | |
8614 | { | |
dcdbc059 | 8615 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
8616 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8617 | } | |
8618 | #endif | |
8619 | ||
26602cab | 8620 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8621 | { |
fa0d7e3d NP |
8622 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8623 | } | |
8624 | ||
633cc816 | 8625 | void btrfs_destroy_inode(struct inode *vfs_inode) |
39279cc3 | 8626 | { |
e6dcd2dc | 8627 | struct btrfs_ordered_extent *ordered; |
633cc816 NB |
8628 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
8629 | struct btrfs_root *root = inode->root; | |
5a3f23d5 | 8630 | |
633cc816 NB |
8631 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
8632 | WARN_ON(vfs_inode->i_data.nrpages); | |
8633 | WARN_ON(inode->block_rsv.reserved); | |
8634 | WARN_ON(inode->block_rsv.size); | |
8635 | WARN_ON(inode->outstanding_extents); | |
8636 | WARN_ON(inode->delalloc_bytes); | |
8637 | WARN_ON(inode->new_delalloc_bytes); | |
8638 | WARN_ON(inode->csum_bytes); | |
8639 | WARN_ON(inode->defrag_bytes); | |
39279cc3 | 8640 | |
a6dbd429 JB |
8641 | /* |
8642 | * This can happen where we create an inode, but somebody else also | |
8643 | * created the same inode and we need to destroy the one we already | |
8644 | * created. | |
8645 | */ | |
8646 | if (!root) | |
26602cab | 8647 | return; |
a6dbd429 | 8648 | |
d397712b | 8649 | while (1) { |
633cc816 | 8650 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
e6dcd2dc CM |
8651 | if (!ordered) |
8652 | break; | |
8653 | else { | |
633cc816 | 8654 | btrfs_err(root->fs_info, |
5d163e0e | 8655 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8656 | ordered->file_offset, ordered->num_bytes); |
71fe0a55 | 8657 | btrfs_remove_ordered_extent(inode, ordered); |
e6dcd2dc CM |
8658 | btrfs_put_ordered_extent(ordered); |
8659 | btrfs_put_ordered_extent(ordered); | |
8660 | } | |
8661 | } | |
633cc816 NB |
8662 | btrfs_qgroup_check_reserved_leak(inode); |
8663 | inode_tree_del(inode); | |
8664 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
8665 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); | |
8666 | btrfs_put_root(inode->root); | |
39279cc3 CM |
8667 | } |
8668 | ||
45321ac5 | 8669 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8670 | { |
8671 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8672 | |
6379ef9f NA |
8673 | if (root == NULL) |
8674 | return 1; | |
8675 | ||
fa6ac876 | 8676 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8677 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8678 | return 1; |
76dda93c | 8679 | else |
45321ac5 | 8680 | return generic_drop_inode(inode); |
76dda93c YZ |
8681 | } |
8682 | ||
0ee0fda0 | 8683 | static void init_once(void *foo) |
39279cc3 CM |
8684 | { |
8685 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8686 | ||
8687 | inode_init_once(&ei->vfs_inode); | |
8688 | } | |
8689 | ||
e67c718b | 8690 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8691 | { |
8c0a8537 KS |
8692 | /* |
8693 | * Make sure all delayed rcu free inodes are flushed before we | |
8694 | * destroy cache. | |
8695 | */ | |
8696 | rcu_barrier(); | |
5598e900 KM |
8697 | kmem_cache_destroy(btrfs_inode_cachep); |
8698 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
8699 | kmem_cache_destroy(btrfs_path_cachep); |
8700 | kmem_cache_destroy(btrfs_free_space_cachep); | |
3acd4850 | 8701 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
39279cc3 CM |
8702 | } |
8703 | ||
f5c29bd9 | 8704 | int __init btrfs_init_cachep(void) |
39279cc3 | 8705 | { |
837e1972 | 8706 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8707 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8708 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8709 | init_once); | |
39279cc3 CM |
8710 | if (!btrfs_inode_cachep) |
8711 | goto fail; | |
9601e3f6 | 8712 | |
837e1972 | 8713 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 8714 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 8715 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8716 | if (!btrfs_trans_handle_cachep) |
8717 | goto fail; | |
9601e3f6 | 8718 | |
837e1972 | 8719 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 8720 | sizeof(struct btrfs_path), 0, |
fba4b697 | 8721 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8722 | if (!btrfs_path_cachep) |
8723 | goto fail; | |
9601e3f6 | 8724 | |
837e1972 | 8725 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 8726 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 8727 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
8728 | if (!btrfs_free_space_cachep) |
8729 | goto fail; | |
8730 | ||
3acd4850 CL |
8731 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
8732 | PAGE_SIZE, PAGE_SIZE, | |
8733 | SLAB_RED_ZONE, NULL); | |
8734 | if (!btrfs_free_space_bitmap_cachep) | |
8735 | goto fail; | |
8736 | ||
39279cc3 CM |
8737 | return 0; |
8738 | fail: | |
8739 | btrfs_destroy_cachep(); | |
8740 | return -ENOMEM; | |
8741 | } | |
8742 | ||
a528d35e DH |
8743 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
8744 | u32 request_mask, unsigned int flags) | |
39279cc3 | 8745 | { |
df0af1a5 | 8746 | u64 delalloc_bytes; |
a528d35e | 8747 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 8748 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
8749 | u32 bi_flags = BTRFS_I(inode)->flags; |
8750 | ||
8751 | stat->result_mask |= STATX_BTIME; | |
8752 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
8753 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
8754 | if (bi_flags & BTRFS_INODE_APPEND) | |
8755 | stat->attributes |= STATX_ATTR_APPEND; | |
8756 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8757 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8758 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8759 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8760 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8761 | stat->attributes |= STATX_ATTR_NODUMP; | |
8762 | ||
8763 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8764 | STATX_ATTR_COMPRESSED | | |
8765 | STATX_ATTR_IMMUTABLE | | |
8766 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8767 | |
39279cc3 | 8768 | generic_fillattr(inode, stat); |
0ee5dc67 | 8769 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8770 | |
8771 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8772 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 8773 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 8774 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 8775 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
8776 | return 0; |
8777 | } | |
8778 | ||
cdd1fedf DF |
8779 | static int btrfs_rename_exchange(struct inode *old_dir, |
8780 | struct dentry *old_dentry, | |
8781 | struct inode *new_dir, | |
8782 | struct dentry *new_dentry) | |
8783 | { | |
0b246afa | 8784 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
8785 | struct btrfs_trans_handle *trans; |
8786 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
8787 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
8788 | struct inode *new_inode = new_dentry->d_inode; | |
8789 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 8790 | struct timespec64 ctime = current_time(old_inode); |
4a0cc7ca NB |
8791 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
8792 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
8793 | u64 old_idx = 0; |
8794 | u64 new_idx = 0; | |
cdd1fedf | 8795 | int ret; |
75b463d2 | 8796 | int ret2; |
86e8aa0e FM |
8797 | bool root_log_pinned = false; |
8798 | bool dest_log_pinned = false; | |
cdd1fedf DF |
8799 | |
8800 | /* we only allow rename subvolume link between subvolumes */ | |
8801 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
8802 | return -EXDEV; | |
8803 | ||
8804 | /* close the race window with snapshot create/destroy ioctl */ | |
943eb3bf JB |
8805 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
8806 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8807 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8808 | |
8809 | /* | |
8810 | * We want to reserve the absolute worst case amount of items. So if | |
8811 | * both inodes are subvols and we need to unlink them then that would | |
8812 | * require 4 item modifications, but if they are both normal inodes it | |
8813 | * would require 5 item modifications, so we'll assume their normal | |
8814 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
8815 | * should cover the worst case number of items we'll modify. | |
8816 | */ | |
8817 | trans = btrfs_start_transaction(root, 12); | |
8818 | if (IS_ERR(trans)) { | |
8819 | ret = PTR_ERR(trans); | |
8820 | goto out_notrans; | |
8821 | } | |
8822 | ||
3e174099 JB |
8823 | if (dest != root) |
8824 | btrfs_record_root_in_trans(trans, dest); | |
8825 | ||
cdd1fedf DF |
8826 | /* |
8827 | * We need to find a free sequence number both in the source and | |
8828 | * in the destination directory for the exchange. | |
8829 | */ | |
877574e2 | 8830 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
8831 | if (ret) |
8832 | goto out_fail; | |
877574e2 | 8833 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
8834 | if (ret) |
8835 | goto out_fail; | |
8836 | ||
8837 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
8838 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
8839 | ||
8840 | /* Reference for the source. */ | |
8841 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8842 | /* force full log commit if subvolume involved. */ | |
90787766 | 8843 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8844 | } else { |
376e5a57 FM |
8845 | btrfs_pin_log_trans(root); |
8846 | root_log_pinned = true; | |
cdd1fedf DF |
8847 | ret = btrfs_insert_inode_ref(trans, dest, |
8848 | new_dentry->d_name.name, | |
8849 | new_dentry->d_name.len, | |
8850 | old_ino, | |
f85b7379 DS |
8851 | btrfs_ino(BTRFS_I(new_dir)), |
8852 | old_idx); | |
cdd1fedf DF |
8853 | if (ret) |
8854 | goto out_fail; | |
cdd1fedf DF |
8855 | } |
8856 | ||
8857 | /* And now for the dest. */ | |
8858 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8859 | /* force full log commit if subvolume involved. */ | |
90787766 | 8860 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8861 | } else { |
376e5a57 FM |
8862 | btrfs_pin_log_trans(dest); |
8863 | dest_log_pinned = true; | |
cdd1fedf DF |
8864 | ret = btrfs_insert_inode_ref(trans, root, |
8865 | old_dentry->d_name.name, | |
8866 | old_dentry->d_name.len, | |
8867 | new_ino, | |
f85b7379 DS |
8868 | btrfs_ino(BTRFS_I(old_dir)), |
8869 | new_idx); | |
cdd1fedf DF |
8870 | if (ret) |
8871 | goto out_fail; | |
cdd1fedf DF |
8872 | } |
8873 | ||
8874 | /* Update inode version and ctime/mtime. */ | |
8875 | inode_inc_iversion(old_dir); | |
8876 | inode_inc_iversion(new_dir); | |
8877 | inode_inc_iversion(old_inode); | |
8878 | inode_inc_iversion(new_inode); | |
8879 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
8880 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
8881 | old_inode->i_ctime = ctime; | |
8882 | new_inode->i_ctime = ctime; | |
8883 | ||
8884 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
8885 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
8886 | BTRFS_I(old_inode), 1); | |
8887 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
8888 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
8889 | } |
8890 | ||
8891 | /* src is a subvolume */ | |
8892 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8893 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
cdd1fedf | 8894 | } else { /* src is an inode */ |
4ec5934e NB |
8895 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
8896 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
8897 | old_dentry->d_name.name, |
8898 | old_dentry->d_name.len); | |
8899 | if (!ret) | |
8900 | ret = btrfs_update_inode(trans, root, old_inode); | |
8901 | } | |
8902 | if (ret) { | |
66642832 | 8903 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8904 | goto out_fail; |
8905 | } | |
8906 | ||
8907 | /* dest is a subvolume */ | |
8908 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8909 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
cdd1fedf | 8910 | } else { /* dest is an inode */ |
4ec5934e NB |
8911 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
8912 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
8913 | new_dentry->d_name.name, |
8914 | new_dentry->d_name.len); | |
8915 | if (!ret) | |
8916 | ret = btrfs_update_inode(trans, dest, new_inode); | |
8917 | } | |
8918 | if (ret) { | |
66642832 | 8919 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8920 | goto out_fail; |
8921 | } | |
8922 | ||
db0a669f | 8923 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
8924 | new_dentry->d_name.name, |
8925 | new_dentry->d_name.len, 0, old_idx); | |
8926 | if (ret) { | |
66642832 | 8927 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8928 | goto out_fail; |
8929 | } | |
8930 | ||
db0a669f | 8931 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
8932 | old_dentry->d_name.name, |
8933 | old_dentry->d_name.len, 0, new_idx); | |
8934 | if (ret) { | |
66642832 | 8935 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8936 | goto out_fail; |
8937 | } | |
8938 | ||
8939 | if (old_inode->i_nlink == 1) | |
8940 | BTRFS_I(old_inode)->dir_index = old_idx; | |
8941 | if (new_inode->i_nlink == 1) | |
8942 | BTRFS_I(new_inode)->dir_index = new_idx; | |
8943 | ||
86e8aa0e | 8944 | if (root_log_pinned) { |
75b463d2 FM |
8945 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
8946 | new_dentry->d_parent); | |
cdd1fedf | 8947 | btrfs_end_log_trans(root); |
86e8aa0e | 8948 | root_log_pinned = false; |
cdd1fedf | 8949 | } |
86e8aa0e | 8950 | if (dest_log_pinned) { |
75b463d2 FM |
8951 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
8952 | old_dentry->d_parent); | |
cdd1fedf | 8953 | btrfs_end_log_trans(dest); |
86e8aa0e | 8954 | dest_log_pinned = false; |
cdd1fedf DF |
8955 | } |
8956 | out_fail: | |
86e8aa0e FM |
8957 | /* |
8958 | * If we have pinned a log and an error happened, we unpin tasks | |
8959 | * trying to sync the log and force them to fallback to a transaction | |
8960 | * commit if the log currently contains any of the inodes involved in | |
8961 | * this rename operation (to ensure we do not persist a log with an | |
8962 | * inconsistent state for any of these inodes or leading to any | |
8963 | * inconsistencies when replayed). If the transaction was aborted, the | |
8964 | * abortion reason is propagated to userspace when attempting to commit | |
8965 | * the transaction. If the log does not contain any of these inodes, we | |
8966 | * allow the tasks to sync it. | |
8967 | */ | |
8968 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
8969 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
8970 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
8971 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 8972 | (new_inode && |
0f8939b8 | 8973 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 8974 | btrfs_set_log_full_commit(trans); |
86e8aa0e FM |
8975 | |
8976 | if (root_log_pinned) { | |
8977 | btrfs_end_log_trans(root); | |
8978 | root_log_pinned = false; | |
8979 | } | |
8980 | if (dest_log_pinned) { | |
8981 | btrfs_end_log_trans(dest); | |
8982 | dest_log_pinned = false; | |
8983 | } | |
8984 | } | |
75b463d2 FM |
8985 | ret2 = btrfs_end_transaction(trans); |
8986 | ret = ret ? ret : ret2; | |
cdd1fedf | 8987 | out_notrans: |
943eb3bf JB |
8988 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
8989 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8990 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8991 | |
8992 | return ret; | |
8993 | } | |
8994 | ||
8995 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
8996 | struct btrfs_root *root, | |
8997 | struct inode *dir, | |
8998 | struct dentry *dentry) | |
8999 | { | |
9000 | int ret; | |
9001 | struct inode *inode; | |
9002 | u64 objectid; | |
9003 | u64 index; | |
9004 | ||
9005 | ret = btrfs_find_free_ino(root, &objectid); | |
9006 | if (ret) | |
9007 | return ret; | |
9008 | ||
9009 | inode = btrfs_new_inode(trans, root, dir, | |
9010 | dentry->d_name.name, | |
9011 | dentry->d_name.len, | |
4a0cc7ca | 9012 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9013 | objectid, |
9014 | S_IFCHR | WHITEOUT_MODE, | |
9015 | &index); | |
9016 | ||
9017 | if (IS_ERR(inode)) { | |
9018 | ret = PTR_ERR(inode); | |
9019 | return ret; | |
9020 | } | |
9021 | ||
9022 | inode->i_op = &btrfs_special_inode_operations; | |
9023 | init_special_inode(inode, inode->i_mode, | |
9024 | WHITEOUT_DEV); | |
9025 | ||
9026 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9027 | &dentry->d_name); | |
9028 | if (ret) | |
c9901618 | 9029 | goto out; |
cdd1fedf | 9030 | |
cef415af NB |
9031 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9032 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9033 | if (ret) |
c9901618 | 9034 | goto out; |
cdd1fedf DF |
9035 | |
9036 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9037 | out: |
cdd1fedf | 9038 | unlock_new_inode(inode); |
c9901618 FM |
9039 | if (ret) |
9040 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9041 | iput(inode); |
9042 | ||
c9901618 | 9043 | return ret; |
cdd1fedf DF |
9044 | } |
9045 | ||
d397712b | 9046 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9047 | struct inode *new_dir, struct dentry *new_dentry, |
9048 | unsigned int flags) | |
39279cc3 | 9049 | { |
0b246afa | 9050 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9051 | struct btrfs_trans_handle *trans; |
5062af35 | 9052 | unsigned int trans_num_items; |
39279cc3 | 9053 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9054 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9055 | struct inode *new_inode = d_inode(new_dentry); |
9056 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9057 | u64 index = 0; |
39279cc3 | 9058 | int ret; |
75b463d2 | 9059 | int ret2; |
4a0cc7ca | 9060 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9061 | bool log_pinned = false; |
39279cc3 | 9062 | |
4a0cc7ca | 9063 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9064 | return -EPERM; |
9065 | ||
4df27c4d | 9066 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9067 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9068 | return -EXDEV; |
9069 | ||
33345d01 | 9070 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9071 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9072 | return -ENOTEMPTY; |
5f39d397 | 9073 | |
4df27c4d YZ |
9074 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9075 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9076 | return -ENOTEMPTY; | |
9c52057c CM |
9077 | |
9078 | ||
9079 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9080 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9081 | new_dentry->d_name.name, |
9082 | new_dentry->d_name.len); | |
9083 | ||
9084 | if (ret) { | |
9085 | if (ret == -EEXIST) { | |
9086 | /* we shouldn't get | |
9087 | * eexist without a new_inode */ | |
fae7f21c | 9088 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9089 | return ret; |
9090 | } | |
9091 | } else { | |
9092 | /* maybe -EOVERFLOW */ | |
9093 | return ret; | |
9094 | } | |
9095 | } | |
9096 | ret = 0; | |
9097 | ||
5a3f23d5 | 9098 | /* |
8d875f95 CM |
9099 | * we're using rename to replace one file with another. Start IO on it |
9100 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9101 | */ |
8d875f95 | 9102 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9103 | filemap_flush(old_inode->i_mapping); |
9104 | ||
76dda93c | 9105 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9106 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9107 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9108 | /* |
9109 | * We want to reserve the absolute worst case amount of items. So if | |
9110 | * both inodes are subvols and we need to unlink them then that would | |
9111 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9112 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9113 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9114 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9115 | * If our rename has the whiteout flag, we need more 5 units for the |
9116 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9117 | * when selinux is enabled). | |
a22285a6 | 9118 | */ |
5062af35 FM |
9119 | trans_num_items = 11; |
9120 | if (flags & RENAME_WHITEOUT) | |
9121 | trans_num_items += 5; | |
9122 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9123 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9124 | ret = PTR_ERR(trans); |
9125 | goto out_notrans; | |
9126 | } | |
76dda93c | 9127 | |
4df27c4d YZ |
9128 | if (dest != root) |
9129 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9130 | |
877574e2 | 9131 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9132 | if (ret) |
9133 | goto out_fail; | |
5a3f23d5 | 9134 | |
67de1176 | 9135 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9136 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9137 | /* force full log commit if subvolume involved. */ |
90787766 | 9138 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9139 | } else { |
c4aba954 FM |
9140 | btrfs_pin_log_trans(root); |
9141 | log_pinned = true; | |
a5719521 YZ |
9142 | ret = btrfs_insert_inode_ref(trans, dest, |
9143 | new_dentry->d_name.name, | |
9144 | new_dentry->d_name.len, | |
33345d01 | 9145 | old_ino, |
4a0cc7ca | 9146 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9147 | if (ret) |
9148 | goto out_fail; | |
4df27c4d | 9149 | } |
5a3f23d5 | 9150 | |
0c4d2d95 JB |
9151 | inode_inc_iversion(old_dir); |
9152 | inode_inc_iversion(new_dir); | |
9153 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9154 | old_dir->i_ctime = old_dir->i_mtime = |
9155 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9156 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9157 | |
12fcfd22 | 9158 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9159 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9160 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9161 | |
33345d01 | 9162 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
045d3967 | 9163 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
4df27c4d | 9164 | } else { |
4ec5934e NB |
9165 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9166 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9167 | old_dentry->d_name.name, |
9168 | old_dentry->d_name.len); | |
9169 | if (!ret) | |
9170 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9171 | } |
79787eaa | 9172 | if (ret) { |
66642832 | 9173 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9174 | goto out_fail; |
9175 | } | |
39279cc3 CM |
9176 | |
9177 | if (new_inode) { | |
0c4d2d95 | 9178 | inode_inc_iversion(new_inode); |
c2050a45 | 9179 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9180 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9181 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 9182 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
4df27c4d YZ |
9183 | BUG_ON(new_inode->i_nlink == 0); |
9184 | } else { | |
4ec5934e NB |
9185 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9186 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
9187 | new_dentry->d_name.name, |
9188 | new_dentry->d_name.len); | |
9189 | } | |
4ef31a45 | 9190 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9191 | ret = btrfs_orphan_add(trans, |
9192 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9193 | if (ret) { |
66642832 | 9194 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9195 | goto out_fail; |
9196 | } | |
39279cc3 | 9197 | } |
aec7477b | 9198 | |
db0a669f | 9199 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 9200 | new_dentry->d_name.name, |
a5719521 | 9201 | new_dentry->d_name.len, 0, index); |
79787eaa | 9202 | if (ret) { |
66642832 | 9203 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9204 | goto out_fail; |
9205 | } | |
39279cc3 | 9206 | |
67de1176 MX |
9207 | if (old_inode->i_nlink == 1) |
9208 | BTRFS_I(old_inode)->dir_index = index; | |
9209 | ||
3dc9e8f7 | 9210 | if (log_pinned) { |
75b463d2 FM |
9211 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9212 | new_dentry->d_parent); | |
4df27c4d | 9213 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9214 | log_pinned = false; |
4df27c4d | 9215 | } |
cdd1fedf DF |
9216 | |
9217 | if (flags & RENAME_WHITEOUT) { | |
9218 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9219 | old_dentry); | |
9220 | ||
9221 | if (ret) { | |
66642832 | 9222 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9223 | goto out_fail; |
9224 | } | |
4df27c4d | 9225 | } |
39279cc3 | 9226 | out_fail: |
3dc9e8f7 FM |
9227 | /* |
9228 | * If we have pinned the log and an error happened, we unpin tasks | |
9229 | * trying to sync the log and force them to fallback to a transaction | |
9230 | * commit if the log currently contains any of the inodes involved in | |
9231 | * this rename operation (to ensure we do not persist a log with an | |
9232 | * inconsistent state for any of these inodes or leading to any | |
9233 | * inconsistencies when replayed). If the transaction was aborted, the | |
9234 | * abortion reason is propagated to userspace when attempting to commit | |
9235 | * the transaction. If the log does not contain any of these inodes, we | |
9236 | * allow the tasks to sync it. | |
9237 | */ | |
9238 | if (ret && log_pinned) { | |
0f8939b8 NB |
9239 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9240 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9241 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9242 | (new_inode && |
0f8939b8 | 9243 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 9244 | btrfs_set_log_full_commit(trans); |
3dc9e8f7 FM |
9245 | |
9246 | btrfs_end_log_trans(root); | |
9247 | log_pinned = false; | |
9248 | } | |
75b463d2 FM |
9249 | ret2 = btrfs_end_transaction(trans); |
9250 | ret = ret ? ret : ret2; | |
b44c59a8 | 9251 | out_notrans: |
33345d01 | 9252 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9253 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9254 | |
39279cc3 CM |
9255 | return ret; |
9256 | } | |
9257 | ||
80ace85c MS |
9258 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9259 | struct inode *new_dir, struct dentry *new_dentry, | |
9260 | unsigned int flags) | |
9261 | { | |
cdd1fedf | 9262 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9263 | return -EINVAL; |
9264 | ||
cdd1fedf DF |
9265 | if (flags & RENAME_EXCHANGE) |
9266 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9267 | new_dentry); | |
9268 | ||
9269 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9270 | } |
9271 | ||
3a2f8c07 NB |
9272 | struct btrfs_delalloc_work { |
9273 | struct inode *inode; | |
9274 | struct completion completion; | |
9275 | struct list_head list; | |
9276 | struct btrfs_work work; | |
9277 | }; | |
9278 | ||
8ccf6f19 MX |
9279 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9280 | { | |
9281 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9282 | struct inode *inode; |
8ccf6f19 MX |
9283 | |
9284 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9285 | work); | |
9f23e289 | 9286 | inode = delalloc_work->inode; |
30424601 DS |
9287 | filemap_flush(inode->i_mapping); |
9288 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9289 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9290 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9291 | |
076da91c | 9292 | iput(inode); |
8ccf6f19 MX |
9293 | complete(&delalloc_work->completion); |
9294 | } | |
9295 | ||
3a2f8c07 | 9296 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9297 | { |
9298 | struct btrfs_delalloc_work *work; | |
9299 | ||
100d5702 | 9300 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9301 | if (!work) |
9302 | return NULL; | |
9303 | ||
9304 | init_completion(&work->completion); | |
9305 | INIT_LIST_HEAD(&work->list); | |
9306 | work->inode = inode; | |
a0cac0ec | 9307 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
9308 | |
9309 | return work; | |
9310 | } | |
9311 | ||
d352ac68 CM |
9312 | /* |
9313 | * some fairly slow code that needs optimization. This walks the list | |
9314 | * of all the inodes with pending delalloc and forces them to disk. | |
9315 | */ | |
b4912139 | 9316 | static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot) |
ea8c2819 | 9317 | { |
ea8c2819 | 9318 | struct btrfs_inode *binode; |
5b21f2ed | 9319 | struct inode *inode; |
8ccf6f19 MX |
9320 | struct btrfs_delalloc_work *work, *next; |
9321 | struct list_head works; | |
1eafa6c7 | 9322 | struct list_head splice; |
8ccf6f19 | 9323 | int ret = 0; |
ea8c2819 | 9324 | |
8ccf6f19 | 9325 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9326 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9327 | |
573bfb72 | 9328 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9329 | spin_lock(&root->delalloc_lock); |
9330 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9331 | while (!list_empty(&splice)) { |
9332 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9333 | delalloc_inodes); |
1eafa6c7 | 9334 | |
eb73c1b7 MX |
9335 | list_move_tail(&binode->delalloc_inodes, |
9336 | &root->delalloc_inodes); | |
5b21f2ed | 9337 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9338 | if (!inode) { |
eb73c1b7 | 9339 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9340 | continue; |
df0af1a5 | 9341 | } |
eb73c1b7 | 9342 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9343 | |
3cd24c69 EL |
9344 | if (snapshot) |
9345 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9346 | &binode->runtime_flags); | |
076da91c | 9347 | work = btrfs_alloc_delalloc_work(inode); |
5d99a998 | 9348 | if (!work) { |
4fbb5147 | 9349 | iput(inode); |
1eafa6c7 | 9350 | ret = -ENOMEM; |
a1ecaabb | 9351 | goto out; |
5b21f2ed | 9352 | } |
1eafa6c7 | 9353 | list_add_tail(&work->list, &works); |
a44903ab QW |
9354 | btrfs_queue_work(root->fs_info->flush_workers, |
9355 | &work->work); | |
b4912139 JB |
9356 | if (*nr != U64_MAX) { |
9357 | (*nr)--; | |
9358 | if (*nr == 0) | |
9359 | goto out; | |
9360 | } | |
5b21f2ed | 9361 | cond_resched(); |
eb73c1b7 | 9362 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9363 | } |
eb73c1b7 | 9364 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9365 | |
a1ecaabb | 9366 | out: |
eb73c1b7 MX |
9367 | list_for_each_entry_safe(work, next, &works, list) { |
9368 | list_del_init(&work->list); | |
40012f96 NB |
9369 | wait_for_completion(&work->completion); |
9370 | kfree(work); | |
eb73c1b7 MX |
9371 | } |
9372 | ||
81f1d390 | 9373 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9374 | spin_lock(&root->delalloc_lock); |
9375 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9376 | spin_unlock(&root->delalloc_lock); | |
9377 | } | |
573bfb72 | 9378 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9379 | return ret; |
9380 | } | |
1eafa6c7 | 9381 | |
3cd24c69 | 9382 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
eb73c1b7 | 9383 | { |
0b246afa | 9384 | struct btrfs_fs_info *fs_info = root->fs_info; |
b4912139 | 9385 | u64 nr = U64_MAX; |
1eafa6c7 | 9386 | |
0b246afa | 9387 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9388 | return -EROFS; |
9389 | ||
b4912139 | 9390 | return start_delalloc_inodes(root, &nr, true); |
eb73c1b7 MX |
9391 | } |
9392 | ||
b4912139 | 9393 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr) |
eb73c1b7 MX |
9394 | { |
9395 | struct btrfs_root *root; | |
9396 | struct list_head splice; | |
9397 | int ret; | |
9398 | ||
2c21b4d7 | 9399 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9400 | return -EROFS; |
9401 | ||
9402 | INIT_LIST_HEAD(&splice); | |
9403 | ||
573bfb72 | 9404 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9405 | spin_lock(&fs_info->delalloc_root_lock); |
9406 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9407 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9408 | root = list_first_entry(&splice, struct btrfs_root, |
9409 | delalloc_root); | |
00246528 | 9410 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9411 | BUG_ON(!root); |
9412 | list_move_tail(&root->delalloc_root, | |
9413 | &fs_info->delalloc_roots); | |
9414 | spin_unlock(&fs_info->delalloc_root_lock); | |
9415 | ||
b4912139 | 9416 | ret = start_delalloc_inodes(root, &nr, false); |
00246528 | 9417 | btrfs_put_root(root); |
6c255e67 | 9418 | if (ret < 0) |
eb73c1b7 | 9419 | goto out; |
eb73c1b7 | 9420 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9421 | } |
eb73c1b7 | 9422 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9423 | |
6c255e67 | 9424 | ret = 0; |
eb73c1b7 | 9425 | out: |
81f1d390 | 9426 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9427 | spin_lock(&fs_info->delalloc_root_lock); |
9428 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9429 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9430 | } |
573bfb72 | 9431 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9432 | return ret; |
ea8c2819 CM |
9433 | } |
9434 | ||
39279cc3 CM |
9435 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9436 | const char *symname) | |
9437 | { | |
0b246afa | 9438 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9439 | struct btrfs_trans_handle *trans; |
9440 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9441 | struct btrfs_path *path; | |
9442 | struct btrfs_key key; | |
1832a6d5 | 9443 | struct inode *inode = NULL; |
39279cc3 | 9444 | int err; |
39279cc3 | 9445 | u64 objectid; |
67871254 | 9446 | u64 index = 0; |
39279cc3 CM |
9447 | int name_len; |
9448 | int datasize; | |
5f39d397 | 9449 | unsigned long ptr; |
39279cc3 | 9450 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9451 | struct extent_buffer *leaf; |
39279cc3 | 9452 | |
f06becc4 | 9453 | name_len = strlen(symname); |
0b246afa | 9454 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9455 | return -ENAMETOOLONG; |
1832a6d5 | 9456 | |
9ed74f2d JB |
9457 | /* |
9458 | * 2 items for inode item and ref | |
9459 | * 2 items for dir items | |
9269d12b FM |
9460 | * 1 item for updating parent inode item |
9461 | * 1 item for the inline extent item | |
9ed74f2d JB |
9462 | * 1 item for xattr if selinux is on |
9463 | */ | |
9269d12b | 9464 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
9465 | if (IS_ERR(trans)) |
9466 | return PTR_ERR(trans); | |
1832a6d5 | 9467 | |
581bb050 LZ |
9468 | err = btrfs_find_free_ino(root, &objectid); |
9469 | if (err) | |
9470 | goto out_unlock; | |
9471 | ||
aec7477b | 9472 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
9473 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
9474 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
9475 | if (IS_ERR(inode)) { |
9476 | err = PTR_ERR(inode); | |
32955c54 | 9477 | inode = NULL; |
39279cc3 | 9478 | goto out_unlock; |
7cf96da3 | 9479 | } |
39279cc3 | 9480 | |
ad19db71 CS |
9481 | /* |
9482 | * If the active LSM wants to access the inode during | |
9483 | * d_instantiate it needs these. Smack checks to see | |
9484 | * if the filesystem supports xattrs by looking at the | |
9485 | * ops vector. | |
9486 | */ | |
9487 | inode->i_fop = &btrfs_file_operations; | |
9488 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9489 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9490 | |
9491 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9492 | if (err) | |
32955c54 | 9493 | goto out_unlock; |
ad19db71 | 9494 | |
39279cc3 | 9495 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9496 | if (!path) { |
9497 | err = -ENOMEM; | |
32955c54 | 9498 | goto out_unlock; |
d8926bb3 | 9499 | } |
4a0cc7ca | 9500 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9501 | key.offset = 0; |
962a298f | 9502 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9503 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9504 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9505 | datasize); | |
54aa1f4d | 9506 | if (err) { |
b0839166 | 9507 | btrfs_free_path(path); |
32955c54 | 9508 | goto out_unlock; |
54aa1f4d | 9509 | } |
5f39d397 CM |
9510 | leaf = path->nodes[0]; |
9511 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9512 | struct btrfs_file_extent_item); | |
9513 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9514 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9515 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9516 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9517 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9518 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9519 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9520 | ||
39279cc3 | 9521 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9522 | write_extent_buffer(leaf, symname, ptr, name_len); |
9523 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9524 | btrfs_free_path(path); |
5f39d397 | 9525 | |
39279cc3 | 9526 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 9527 | inode_nohighmem(inode); |
d899e052 | 9528 | inode_set_bytes(inode, name_len); |
6ef06d27 | 9529 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 9530 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
9531 | /* |
9532 | * Last step, add directory indexes for our symlink inode. This is the | |
9533 | * last step to avoid extra cleanup of these indexes if an error happens | |
9534 | * elsewhere above. | |
9535 | */ | |
9536 | if (!err) | |
cef415af NB |
9537 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9538 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
9539 | if (err) |
9540 | goto out_unlock; | |
b0d5d10f | 9541 | |
1e2e547a | 9542 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
9543 | |
9544 | out_unlock: | |
3a45bb20 | 9545 | btrfs_end_transaction(trans); |
32955c54 | 9546 | if (err && inode) { |
39279cc3 | 9547 | inode_dec_link_count(inode); |
32955c54 | 9548 | discard_new_inode(inode); |
39279cc3 | 9549 | } |
2ff7e61e | 9550 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
9551 | return err; |
9552 | } | |
16432985 | 9553 | |
8fccebfa FM |
9554 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
9555 | struct btrfs_trans_handle *trans_in, | |
203f44c5 QW |
9556 | struct inode *inode, struct btrfs_key *ins, |
9557 | u64 file_offset) | |
9558 | { | |
9559 | struct btrfs_file_extent_item stack_fi; | |
bf385648 | 9560 | struct btrfs_replace_extent_info extent_info; |
8fccebfa FM |
9561 | struct btrfs_trans_handle *trans = trans_in; |
9562 | struct btrfs_path *path; | |
203f44c5 QW |
9563 | u64 start = ins->objectid; |
9564 | u64 len = ins->offset; | |
9729f10a | 9565 | int ret; |
203f44c5 QW |
9566 | |
9567 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9568 | ||
9569 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9570 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9571 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9572 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9573 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9574 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9575 | /* Encryption and other encoding is reserved and all 0 */ | |
9576 | ||
72b7d15b | 9577 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
9729f10a | 9578 | if (ret < 0) |
8fccebfa FM |
9579 | return ERR_PTR(ret); |
9580 | ||
9581 | if (trans) { | |
9582 | ret = insert_reserved_file_extent(trans, BTRFS_I(inode), | |
9583 | file_offset, &stack_fi, ret); | |
9584 | if (ret) | |
9585 | return ERR_PTR(ret); | |
9586 | return trans; | |
9587 | } | |
9588 | ||
9589 | extent_info.disk_offset = start; | |
9590 | extent_info.disk_len = len; | |
9591 | extent_info.data_offset = 0; | |
9592 | extent_info.data_len = len; | |
9593 | extent_info.file_offset = file_offset; | |
9594 | extent_info.extent_buf = (char *)&stack_fi; | |
8fccebfa FM |
9595 | extent_info.is_new_extent = true; |
9596 | extent_info.qgroup_reserved = ret; | |
9597 | extent_info.insertions = 0; | |
9598 | ||
9599 | path = btrfs_alloc_path(); | |
9600 | if (!path) | |
9601 | return ERR_PTR(-ENOMEM); | |
9602 | ||
306bfec0 | 9603 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
8fccebfa FM |
9604 | file_offset + len - 1, &extent_info, |
9605 | &trans); | |
9606 | btrfs_free_path(path); | |
9607 | if (ret) | |
9608 | return ERR_PTR(ret); | |
9609 | ||
9610 | return trans; | |
203f44c5 | 9611 | } |
8fccebfa | 9612 | |
0af3d00b JB |
9613 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9614 | u64 start, u64 num_bytes, u64 min_size, | |
9615 | loff_t actual_len, u64 *alloc_hint, | |
9616 | struct btrfs_trans_handle *trans) | |
d899e052 | 9617 | { |
0b246afa | 9618 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
9619 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9620 | struct extent_map *em; | |
d899e052 YZ |
9621 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9622 | struct btrfs_key ins; | |
d899e052 | 9623 | u64 cur_offset = start; |
b778cf96 | 9624 | u64 clear_offset = start; |
55a61d1d | 9625 | u64 i_size; |
154ea289 | 9626 | u64 cur_bytes; |
0b670dc4 | 9627 | u64 last_alloc = (u64)-1; |
d899e052 | 9628 | int ret = 0; |
0af3d00b | 9629 | bool own_trans = true; |
18513091 | 9630 | u64 end = start + num_bytes - 1; |
d899e052 | 9631 | |
0af3d00b JB |
9632 | if (trans) |
9633 | own_trans = false; | |
d899e052 | 9634 | while (num_bytes > 0) { |
ee22184b | 9635 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9636 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9637 | /* |
9638 | * If we are severely fragmented we could end up with really | |
9639 | * small allocations, so if the allocator is returning small | |
9640 | * chunks lets make its job easier by only searching for those | |
9641 | * sized chunks. | |
9642 | */ | |
9643 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9644 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9645 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
8fccebfa | 9646 | if (ret) |
a22285a6 | 9647 | break; |
b778cf96 JB |
9648 | |
9649 | /* | |
9650 | * We've reserved this space, and thus converted it from | |
9651 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9652 | * from here on out we will only need to clear our reservation | |
9653 | * for the remaining unreserved area, so advance our | |
9654 | * clear_offset by our extent size. | |
9655 | */ | |
9656 | clear_offset += ins.offset; | |
5a303d5d | 9657 | |
0b670dc4 | 9658 | last_alloc = ins.offset; |
8fccebfa | 9659 | trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset); |
1afc708d FM |
9660 | /* |
9661 | * Now that we inserted the prealloc extent we can finally | |
9662 | * decrement the number of reservations in the block group. | |
9663 | * If we did it before, we could race with relocation and have | |
9664 | * relocation miss the reserved extent, making it fail later. | |
9665 | */ | |
9666 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); | |
8fccebfa FM |
9667 | if (IS_ERR(trans)) { |
9668 | ret = PTR_ERR(trans); | |
2ff7e61e | 9669 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9670 | ins.offset, 0); |
79787eaa JM |
9671 | break; |
9672 | } | |
31193213 | 9673 | |
dcdbc059 | 9674 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 9675 | cur_offset + ins.offset -1, 0); |
5a303d5d | 9676 | |
5dc562c5 JB |
9677 | em = alloc_extent_map(); |
9678 | if (!em) { | |
9679 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9680 | &BTRFS_I(inode)->runtime_flags); | |
9681 | goto next; | |
9682 | } | |
9683 | ||
9684 | em->start = cur_offset; | |
9685 | em->orig_start = cur_offset; | |
9686 | em->len = ins.offset; | |
9687 | em->block_start = ins.objectid; | |
9688 | em->block_len = ins.offset; | |
b4939680 | 9689 | em->orig_block_len = ins.offset; |
cc95bef6 | 9690 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9691 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
9692 | em->generation = trans->transid; | |
9693 | ||
9694 | while (1) { | |
9695 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9696 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9697 | write_unlock(&em_tree->lock); |
9698 | if (ret != -EEXIST) | |
9699 | break; | |
dcdbc059 | 9700 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
9701 | cur_offset + ins.offset - 1, |
9702 | 0); | |
9703 | } | |
9704 | free_extent_map(em); | |
9705 | next: | |
d899e052 YZ |
9706 | num_bytes -= ins.offset; |
9707 | cur_offset += ins.offset; | |
efa56464 | 9708 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9709 | |
0c4d2d95 | 9710 | inode_inc_iversion(inode); |
c2050a45 | 9711 | inode->i_ctime = current_time(inode); |
6cbff00f | 9712 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9713 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9714 | (actual_len > inode->i_size) && |
9715 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9716 | if (cur_offset > actual_len) |
55a61d1d | 9717 | i_size = actual_len; |
d1ea6a61 | 9718 | else |
55a61d1d JB |
9719 | i_size = cur_offset; |
9720 | i_size_write(inode, i_size); | |
d923afe9 | 9721 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
5a303d5d YZ |
9722 | } |
9723 | ||
d899e052 | 9724 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9725 | |
9726 | if (ret) { | |
66642832 | 9727 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9728 | if (own_trans) |
3a45bb20 | 9729 | btrfs_end_transaction(trans); |
79787eaa JM |
9730 | break; |
9731 | } | |
d899e052 | 9732 | |
8fccebfa | 9733 | if (own_trans) { |
3a45bb20 | 9734 | btrfs_end_transaction(trans); |
8fccebfa FM |
9735 | trans = NULL; |
9736 | } | |
5a303d5d | 9737 | } |
b778cf96 | 9738 | if (clear_offset < end) |
25ce28ca | 9739 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 9740 | end - clear_offset + 1); |
d899e052 YZ |
9741 | return ret; |
9742 | } | |
9743 | ||
0af3d00b JB |
9744 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9745 | u64 start, u64 num_bytes, u64 min_size, | |
9746 | loff_t actual_len, u64 *alloc_hint) | |
9747 | { | |
9748 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9749 | min_size, actual_len, alloc_hint, | |
9750 | NULL); | |
9751 | } | |
9752 | ||
9753 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9754 | struct btrfs_trans_handle *trans, int mode, | |
9755 | u64 start, u64 num_bytes, u64 min_size, | |
9756 | loff_t actual_len, u64 *alloc_hint) | |
9757 | { | |
9758 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9759 | min_size, actual_len, alloc_hint, trans); | |
9760 | } | |
9761 | ||
e6dcd2dc CM |
9762 | static int btrfs_set_page_dirty(struct page *page) |
9763 | { | |
e6dcd2dc CM |
9764 | return __set_page_dirty_nobuffers(page); |
9765 | } | |
9766 | ||
10556cb2 | 9767 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9768 | { |
b83cc969 | 9769 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9770 | umode_t mode = inode->i_mode; |
b83cc969 | 9771 | |
cb6db4e5 JM |
9772 | if (mask & MAY_WRITE && |
9773 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9774 | if (btrfs_root_readonly(root)) | |
9775 | return -EROFS; | |
9776 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9777 | return -EACCES; | |
9778 | } | |
2830ba7f | 9779 | return generic_permission(inode, mask); |
fdebe2bd | 9780 | } |
39279cc3 | 9781 | |
ef3b9af5 FM |
9782 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9783 | { | |
2ff7e61e | 9784 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
9785 | struct btrfs_trans_handle *trans; |
9786 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9787 | struct inode *inode = NULL; | |
9788 | u64 objectid; | |
9789 | u64 index; | |
9790 | int ret = 0; | |
9791 | ||
9792 | /* | |
9793 | * 5 units required for adding orphan entry | |
9794 | */ | |
9795 | trans = btrfs_start_transaction(root, 5); | |
9796 | if (IS_ERR(trans)) | |
9797 | return PTR_ERR(trans); | |
9798 | ||
9799 | ret = btrfs_find_free_ino(root, &objectid); | |
9800 | if (ret) | |
9801 | goto out; | |
9802 | ||
9803 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 9804 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
9805 | if (IS_ERR(inode)) { |
9806 | ret = PTR_ERR(inode); | |
9807 | inode = NULL; | |
9808 | goto out; | |
9809 | } | |
9810 | ||
ef3b9af5 FM |
9811 | inode->i_fop = &btrfs_file_operations; |
9812 | inode->i_op = &btrfs_file_inode_operations; | |
9813 | ||
9814 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 | 9815 | |
b0d5d10f CM |
9816 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9817 | if (ret) | |
32955c54 | 9818 | goto out; |
b0d5d10f CM |
9819 | |
9820 | ret = btrfs_update_inode(trans, root, inode); | |
9821 | if (ret) | |
32955c54 | 9822 | goto out; |
73f2e545 | 9823 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 9824 | if (ret) |
32955c54 | 9825 | goto out; |
ef3b9af5 | 9826 | |
5762b5c9 FM |
9827 | /* |
9828 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9829 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9830 | * through: | |
9831 | * | |
9832 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9833 | */ | |
9834 | set_nlink(inode, 1); | |
ef3b9af5 | 9835 | d_tmpfile(dentry, inode); |
32955c54 | 9836 | unlock_new_inode(inode); |
ef3b9af5 | 9837 | mark_inode_dirty(inode); |
ef3b9af5 | 9838 | out: |
3a45bb20 | 9839 | btrfs_end_transaction(trans); |
32955c54 AV |
9840 | if (ret && inode) |
9841 | discard_new_inode(inode); | |
2ff7e61e | 9842 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
9843 | return ret; |
9844 | } | |
9845 | ||
5cdc84bf | 9846 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
c6100a4b | 9847 | { |
5cdc84bf | 9848 | struct inode *inode = tree->private_data; |
c6100a4b JB |
9849 | unsigned long index = start >> PAGE_SHIFT; |
9850 | unsigned long end_index = end >> PAGE_SHIFT; | |
9851 | struct page *page; | |
9852 | ||
9853 | while (index <= end_index) { | |
9854 | page = find_get_page(inode->i_mapping, index); | |
9855 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
9856 | set_page_writeback(page); | |
9857 | put_page(page); | |
9858 | index++; | |
9859 | } | |
9860 | } | |
9861 | ||
ed46ff3d OS |
9862 | #ifdef CONFIG_SWAP |
9863 | /* | |
9864 | * Add an entry indicating a block group or device which is pinned by a | |
9865 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
9866 | * negative errno on failure. | |
9867 | */ | |
9868 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
9869 | bool is_block_group) | |
9870 | { | |
9871 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9872 | struct btrfs_swapfile_pin *sp, *entry; | |
9873 | struct rb_node **p; | |
9874 | struct rb_node *parent = NULL; | |
9875 | ||
9876 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
9877 | if (!sp) | |
9878 | return -ENOMEM; | |
9879 | sp->ptr = ptr; | |
9880 | sp->inode = inode; | |
9881 | sp->is_block_group = is_block_group; | |
9882 | ||
9883 | spin_lock(&fs_info->swapfile_pins_lock); | |
9884 | p = &fs_info->swapfile_pins.rb_node; | |
9885 | while (*p) { | |
9886 | parent = *p; | |
9887 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
9888 | if (sp->ptr < entry->ptr || | |
9889 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
9890 | p = &(*p)->rb_left; | |
9891 | } else if (sp->ptr > entry->ptr || | |
9892 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
9893 | p = &(*p)->rb_right; | |
9894 | } else { | |
9895 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9896 | kfree(sp); | |
9897 | return 1; | |
9898 | } | |
9899 | } | |
9900 | rb_link_node(&sp->node, parent, p); | |
9901 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
9902 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9903 | return 0; | |
9904 | } | |
9905 | ||
9906 | /* Free all of the entries pinned by this swapfile. */ | |
9907 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
9908 | { | |
9909 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9910 | struct btrfs_swapfile_pin *sp; | |
9911 | struct rb_node *node, *next; | |
9912 | ||
9913 | spin_lock(&fs_info->swapfile_pins_lock); | |
9914 | node = rb_first(&fs_info->swapfile_pins); | |
9915 | while (node) { | |
9916 | next = rb_next(node); | |
9917 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
9918 | if (sp->inode == inode) { | |
9919 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
9920 | if (sp->is_block_group) | |
9921 | btrfs_put_block_group(sp->ptr); | |
9922 | kfree(sp); | |
9923 | } | |
9924 | node = next; | |
9925 | } | |
9926 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9927 | } | |
9928 | ||
9929 | struct btrfs_swap_info { | |
9930 | u64 start; | |
9931 | u64 block_start; | |
9932 | u64 block_len; | |
9933 | u64 lowest_ppage; | |
9934 | u64 highest_ppage; | |
9935 | unsigned long nr_pages; | |
9936 | int nr_extents; | |
9937 | }; | |
9938 | ||
9939 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
9940 | struct btrfs_swap_info *bsi) | |
9941 | { | |
9942 | unsigned long nr_pages; | |
9943 | u64 first_ppage, first_ppage_reported, next_ppage; | |
9944 | int ret; | |
9945 | ||
9946 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
9947 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
9948 | PAGE_SIZE) >> PAGE_SHIFT; | |
9949 | ||
9950 | if (first_ppage >= next_ppage) | |
9951 | return 0; | |
9952 | nr_pages = next_ppage - first_ppage; | |
9953 | ||
9954 | first_ppage_reported = first_ppage; | |
9955 | if (bsi->start == 0) | |
9956 | first_ppage_reported++; | |
9957 | if (bsi->lowest_ppage > first_ppage_reported) | |
9958 | bsi->lowest_ppage = first_ppage_reported; | |
9959 | if (bsi->highest_ppage < (next_ppage - 1)) | |
9960 | bsi->highest_ppage = next_ppage - 1; | |
9961 | ||
9962 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
9963 | if (ret < 0) | |
9964 | return ret; | |
9965 | bsi->nr_extents += ret; | |
9966 | bsi->nr_pages += nr_pages; | |
9967 | return 0; | |
9968 | } | |
9969 | ||
9970 | static void btrfs_swap_deactivate(struct file *file) | |
9971 | { | |
9972 | struct inode *inode = file_inode(file); | |
9973 | ||
9974 | btrfs_free_swapfile_pins(inode); | |
9975 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
9976 | } | |
9977 | ||
9978 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
9979 | sector_t *span) | |
9980 | { | |
9981 | struct inode *inode = file_inode(file); | |
9982 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9983 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
9984 | struct extent_state *cached_state = NULL; | |
9985 | struct extent_map *em = NULL; | |
9986 | struct btrfs_device *device = NULL; | |
9987 | struct btrfs_swap_info bsi = { | |
9988 | .lowest_ppage = (sector_t)-1ULL, | |
9989 | }; | |
9990 | int ret = 0; | |
9991 | u64 isize; | |
9992 | u64 start; | |
9993 | ||
9994 | /* | |
9995 | * If the swap file was just created, make sure delalloc is done. If the | |
9996 | * file changes again after this, the user is doing something stupid and | |
9997 | * we don't really care. | |
9998 | */ | |
9999 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10000 | if (ret) | |
10001 | return ret; | |
10002 | ||
10003 | /* | |
10004 | * The inode is locked, so these flags won't change after we check them. | |
10005 | */ | |
10006 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10007 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10008 | return -EINVAL; | |
10009 | } | |
10010 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10011 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10012 | return -EINVAL; | |
10013 | } | |
10014 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10015 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10016 | return -EINVAL; | |
10017 | } | |
10018 | ||
10019 | /* | |
10020 | * Balance or device remove/replace/resize can move stuff around from | |
c3e1f96c GR |
10021 | * under us. The exclop protection makes sure they aren't running/won't |
10022 | * run concurrently while we are mapping the swap extents, and | |
10023 | * fs_info->swapfile_pins prevents them from running while the swap | |
10024 | * file is active and moving the extents. Note that this also prevents | |
10025 | * a concurrent device add which isn't actually necessary, but it's not | |
ed46ff3d OS |
10026 | * really worth the trouble to allow it. |
10027 | */ | |
c3e1f96c | 10028 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
ed46ff3d OS |
10029 | btrfs_warn(fs_info, |
10030 | "cannot activate swapfile while exclusive operation is running"); | |
10031 | return -EBUSY; | |
10032 | } | |
10033 | /* | |
10034 | * Snapshots can create extents which require COW even if NODATACOW is | |
10035 | * set. We use this counter to prevent snapshots. We must increment it | |
10036 | * before walking the extents because we don't want a concurrent | |
10037 | * snapshot to run after we've already checked the extents. | |
10038 | */ | |
10039 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); | |
10040 | ||
10041 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10042 | ||
10043 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10044 | start = 0; | |
10045 | while (start < isize) { | |
10046 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10047 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10048 | u64 len = isize - start; |
10049 | ||
39b07b5d | 10050 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
10051 | if (IS_ERR(em)) { |
10052 | ret = PTR_ERR(em); | |
10053 | goto out; | |
10054 | } | |
10055 | ||
10056 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10057 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10058 | ret = -EINVAL; | |
10059 | goto out; | |
10060 | } | |
10061 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10062 | /* | |
10063 | * It's unlikely we'll ever actually find ourselves | |
10064 | * here, as a file small enough to fit inline won't be | |
10065 | * big enough to store more than the swap header, but in | |
10066 | * case something changes in the future, let's catch it | |
10067 | * here rather than later. | |
10068 | */ | |
10069 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10070 | ret = -EINVAL; | |
10071 | goto out; | |
10072 | } | |
10073 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10074 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10075 | ret = -EINVAL; | |
10076 | goto out; | |
10077 | } | |
10078 | ||
10079 | logical_block_start = em->block_start + (start - em->start); | |
10080 | len = min(len, em->len - (start - em->start)); | |
10081 | free_extent_map(em); | |
10082 | em = NULL; | |
10083 | ||
a84d5d42 | 10084 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
ed46ff3d OS |
10085 | if (ret < 0) { |
10086 | goto out; | |
10087 | } else if (ret) { | |
10088 | ret = 0; | |
10089 | } else { | |
10090 | btrfs_warn(fs_info, | |
10091 | "swapfile must not be copy-on-write"); | |
10092 | ret = -EINVAL; | |
10093 | goto out; | |
10094 | } | |
10095 | ||
10096 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10097 | if (IS_ERR(em)) { | |
10098 | ret = PTR_ERR(em); | |
10099 | goto out; | |
10100 | } | |
10101 | ||
10102 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10103 | btrfs_warn(fs_info, | |
10104 | "swapfile must have single data profile"); | |
10105 | ret = -EINVAL; | |
10106 | goto out; | |
10107 | } | |
10108 | ||
10109 | if (device == NULL) { | |
10110 | device = em->map_lookup->stripes[0].dev; | |
10111 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10112 | if (ret == 1) | |
10113 | ret = 0; | |
10114 | else if (ret) | |
10115 | goto out; | |
10116 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10117 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10118 | ret = -EINVAL; | |
10119 | goto out; | |
10120 | } | |
10121 | ||
10122 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10123 | (logical_block_start - em->start)); | |
10124 | len = min(len, em->len - (logical_block_start - em->start)); | |
10125 | free_extent_map(em); | |
10126 | em = NULL; | |
10127 | ||
10128 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10129 | if (!bg) { | |
10130 | btrfs_warn(fs_info, | |
10131 | "could not find block group containing swapfile"); | |
10132 | ret = -EINVAL; | |
10133 | goto out; | |
10134 | } | |
10135 | ||
10136 | ret = btrfs_add_swapfile_pin(inode, bg, true); | |
10137 | if (ret) { | |
10138 | btrfs_put_block_group(bg); | |
10139 | if (ret == 1) | |
10140 | ret = 0; | |
10141 | else | |
10142 | goto out; | |
10143 | } | |
10144 | ||
10145 | if (bsi.block_len && | |
10146 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10147 | bsi.block_len += len; | |
10148 | } else { | |
10149 | if (bsi.block_len) { | |
10150 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10151 | if (ret) | |
10152 | goto out; | |
10153 | } | |
10154 | bsi.start = start; | |
10155 | bsi.block_start = physical_block_start; | |
10156 | bsi.block_len = len; | |
10157 | } | |
10158 | ||
10159 | start += len; | |
10160 | } | |
10161 | ||
10162 | if (bsi.block_len) | |
10163 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10164 | ||
10165 | out: | |
10166 | if (!IS_ERR_OR_NULL(em)) | |
10167 | free_extent_map(em); | |
10168 | ||
10169 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10170 | ||
10171 | if (ret) | |
10172 | btrfs_swap_deactivate(file); | |
10173 | ||
c3e1f96c | 10174 | btrfs_exclop_finish(fs_info); |
ed46ff3d OS |
10175 | |
10176 | if (ret) | |
10177 | return ret; | |
10178 | ||
10179 | if (device) | |
10180 | sis->bdev = device->bdev; | |
10181 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10182 | sis->max = bsi.nr_pages; | |
10183 | sis->pages = bsi.nr_pages - 1; | |
10184 | sis->highest_bit = bsi.nr_pages - 1; | |
10185 | return bsi.nr_extents; | |
10186 | } | |
10187 | #else | |
10188 | static void btrfs_swap_deactivate(struct file *file) | |
10189 | { | |
10190 | } | |
10191 | ||
10192 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10193 | sector_t *span) | |
10194 | { | |
10195 | return -EOPNOTSUPP; | |
10196 | } | |
10197 | #endif | |
10198 | ||
6e1d5dcc | 10199 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10200 | .getattr = btrfs_getattr, |
39279cc3 CM |
10201 | .lookup = btrfs_lookup, |
10202 | .create = btrfs_create, | |
10203 | .unlink = btrfs_unlink, | |
10204 | .link = btrfs_link, | |
10205 | .mkdir = btrfs_mkdir, | |
10206 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10207 | .rename = btrfs_rename2, |
39279cc3 CM |
10208 | .symlink = btrfs_symlink, |
10209 | .setattr = btrfs_setattr, | |
618e21d5 | 10210 | .mknod = btrfs_mknod, |
5103e947 | 10211 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10212 | .permission = btrfs_permission, |
4e34e719 | 10213 | .get_acl = btrfs_get_acl, |
996a710d | 10214 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10215 | .update_time = btrfs_update_time, |
ef3b9af5 | 10216 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10217 | }; |
76dda93c | 10218 | |
828c0950 | 10219 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10220 | .llseek = generic_file_llseek, |
10221 | .read = generic_read_dir, | |
02dbfc99 | 10222 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10223 | .open = btrfs_opendir, |
34287aa3 | 10224 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10225 | #ifdef CONFIG_COMPAT |
4c63c245 | 10226 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10227 | #endif |
6bf13c0c | 10228 | .release = btrfs_release_file, |
e02119d5 | 10229 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10230 | }; |
10231 | ||
35054394 CM |
10232 | /* |
10233 | * btrfs doesn't support the bmap operation because swapfiles | |
10234 | * use bmap to make a mapping of extents in the file. They assume | |
10235 | * these extents won't change over the life of the file and they | |
10236 | * use the bmap result to do IO directly to the drive. | |
10237 | * | |
10238 | * the btrfs bmap call would return logical addresses that aren't | |
10239 | * suitable for IO and they also will change frequently as COW | |
10240 | * operations happen. So, swapfile + btrfs == corruption. | |
10241 | * | |
10242 | * For now we're avoiding this by dropping bmap. | |
10243 | */ | |
7f09410b | 10244 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10245 | .readpage = btrfs_readpage, |
10246 | .writepage = btrfs_writepage, | |
b293f02e | 10247 | .writepages = btrfs_writepages, |
ba206a02 | 10248 | .readahead = btrfs_readahead, |
f85781fb | 10249 | .direct_IO = noop_direct_IO, |
a52d9a80 CM |
10250 | .invalidatepage = btrfs_invalidatepage, |
10251 | .releasepage = btrfs_releasepage, | |
f8e66081 RG |
10252 | #ifdef CONFIG_MIGRATION |
10253 | .migratepage = btrfs_migratepage, | |
10254 | #endif | |
e6dcd2dc | 10255 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10256 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10257 | .swap_activate = btrfs_swap_activate, |
10258 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10259 | }; |
10260 | ||
6e1d5dcc | 10261 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10262 | .getattr = btrfs_getattr, |
10263 | .setattr = btrfs_setattr, | |
5103e947 | 10264 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10265 | .permission = btrfs_permission, |
1506fcc8 | 10266 | .fiemap = btrfs_fiemap, |
4e34e719 | 10267 | .get_acl = btrfs_get_acl, |
996a710d | 10268 | .set_acl = btrfs_set_acl, |
e41f941a | 10269 | .update_time = btrfs_update_time, |
39279cc3 | 10270 | }; |
6e1d5dcc | 10271 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10272 | .getattr = btrfs_getattr, |
10273 | .setattr = btrfs_setattr, | |
fdebe2bd | 10274 | .permission = btrfs_permission, |
33268eaf | 10275 | .listxattr = btrfs_listxattr, |
4e34e719 | 10276 | .get_acl = btrfs_get_acl, |
996a710d | 10277 | .set_acl = btrfs_set_acl, |
e41f941a | 10278 | .update_time = btrfs_update_time, |
618e21d5 | 10279 | }; |
6e1d5dcc | 10280 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10281 | .get_link = page_get_link, |
f209561a | 10282 | .getattr = btrfs_getattr, |
22c44fe6 | 10283 | .setattr = btrfs_setattr, |
fdebe2bd | 10284 | .permission = btrfs_permission, |
0279b4cd | 10285 | .listxattr = btrfs_listxattr, |
e41f941a | 10286 | .update_time = btrfs_update_time, |
39279cc3 | 10287 | }; |
76dda93c | 10288 | |
82d339d9 | 10289 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
10290 | .d_delete = btrfs_dentry_delete, |
10291 | }; |