]>
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; | |
0eb79294 | 65 | bool sync; |
f28a4928 FM |
66 | }; |
67 | ||
6e1d5dcc AD |
68 | static const struct inode_operations btrfs_dir_inode_operations; |
69 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
70 | static const struct inode_operations btrfs_special_inode_operations; |
71 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 72 | static const struct address_space_operations btrfs_aops; |
828c0950 | 73 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 74 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
75 | |
76 | static struct kmem_cache *btrfs_inode_cachep; | |
77 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 78 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 79 | struct kmem_cache *btrfs_free_space_cachep; |
3acd4850 | 80 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
39279cc3 | 81 | |
3972f260 | 82 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 83 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
5fd02043 | 84 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
6e26c442 | 85 | static noinline int cow_file_range(struct btrfs_inode *inode, |
771ed689 | 86 | struct page *locked_page, |
74e9194a | 87 | u64 start, u64 end, int *page_started, |
330a5827 | 88 | unsigned long *nr_written, int unlock); |
4b67c11d NB |
89 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
90 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
91 | u64 block_len, u64 orig_block_len, |
92 | u64 ram_bytes, int compress_type, | |
93 | int type); | |
7b128766 | 94 | |
b672b5c1 | 95 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
96 | const u64 offset, const u64 bytes, |
97 | const bool uptodate); | |
98 | ||
99 | /* | |
100 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 101 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
102 | * |
103 | * NOTE: caller must ensure that when an error happens, it can not call | |
104 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
105 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
106 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 107 | * extent (btrfs_finish_ordered_io()). |
52427260 | 108 | */ |
64e1db56 | 109 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
d1051d6e NB |
110 | struct page *locked_page, |
111 | u64 offset, u64 bytes) | |
52427260 | 112 | { |
63d71450 NA |
113 | unsigned long index = offset >> PAGE_SHIFT; |
114 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
d1051d6e NB |
115 | u64 page_start = page_offset(locked_page); |
116 | u64 page_end = page_start + PAGE_SIZE - 1; | |
117 | ||
63d71450 NA |
118 | struct page *page; |
119 | ||
120 | while (index <= end_index) { | |
64e1db56 | 121 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
63d71450 NA |
122 | index++; |
123 | if (!page) | |
124 | continue; | |
125 | ClearPagePrivate2(page); | |
126 | put_page(page); | |
127 | } | |
d1051d6e NB |
128 | |
129 | /* | |
130 | * In case this page belongs to the delalloc range being instantiated | |
131 | * then skip it, since the first page of a range is going to be | |
132 | * properly cleaned up by the caller of run_delalloc_range | |
133 | */ | |
134 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
135 | offset += PAGE_SIZE; | |
136 | bytes -= PAGE_SIZE; | |
137 | } | |
138 | ||
64e1db56 | 139 | return __endio_write_update_ordered(inode, offset, bytes, false); |
52427260 QW |
140 | } |
141 | ||
48a3b636 | 142 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 143 | |
6a3891c5 JB |
144 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
145 | void btrfs_test_inode_set_ops(struct inode *inode) | |
146 | { | |
147 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
148 | } | |
149 | #endif | |
150 | ||
f34f57a3 | 151 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
152 | struct inode *inode, struct inode *dir, |
153 | const struct qstr *qstr) | |
0279b4cd JO |
154 | { |
155 | int err; | |
156 | ||
f34f57a3 | 157 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 158 | if (!err) |
2a7dba39 | 159 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
160 | return err; |
161 | } | |
162 | ||
c8b97818 CM |
163 | /* |
164 | * this does all the hard work for inserting an inline extent into | |
165 | * the btree. The caller should have done a btrfs_drop_extents so that | |
166 | * no overlapping inline items exist in the btree | |
167 | */ | |
40f76580 | 168 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 169 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
170 | struct btrfs_root *root, struct inode *inode, |
171 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 172 | int compress_type, |
c8b97818 CM |
173 | struct page **compressed_pages) |
174 | { | |
c8b97818 CM |
175 | struct extent_buffer *leaf; |
176 | struct page *page = NULL; | |
177 | char *kaddr; | |
178 | unsigned long ptr; | |
179 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
180 | int ret; |
181 | size_t cur_size = size; | |
c8b97818 | 182 | unsigned long offset; |
c8b97818 | 183 | |
982f1f5d JJB |
184 | ASSERT((compressed_size > 0 && compressed_pages) || |
185 | (compressed_size == 0 && !compressed_pages)); | |
186 | ||
fe3f566c | 187 | if (compressed_size && compressed_pages) |
c8b97818 | 188 | cur_size = compressed_size; |
c8b97818 | 189 | |
1acae57b | 190 | inode_add_bytes(inode, size); |
c8b97818 | 191 | |
1acae57b FDBM |
192 | if (!extent_inserted) { |
193 | struct btrfs_key key; | |
194 | size_t datasize; | |
c8b97818 | 195 | |
4a0cc7ca | 196 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 197 | key.offset = start; |
962a298f | 198 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 199 | |
1acae57b FDBM |
200 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
201 | path->leave_spinning = 1; | |
202 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
203 | datasize); | |
79b4f4c6 | 204 | if (ret) |
1acae57b | 205 | goto fail; |
c8b97818 CM |
206 | } |
207 | leaf = path->nodes[0]; | |
208 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
209 | struct btrfs_file_extent_item); | |
210 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
211 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
212 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
213 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
214 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
215 | ptr = btrfs_file_extent_inline_start(ei); | |
216 | ||
261507a0 | 217 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
218 | struct page *cpage; |
219 | int i = 0; | |
d397712b | 220 | while (compressed_size > 0) { |
c8b97818 | 221 | cpage = compressed_pages[i]; |
5b050f04 | 222 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 223 | PAGE_SIZE); |
c8b97818 | 224 | |
7ac687d9 | 225 | kaddr = kmap_atomic(cpage); |
c8b97818 | 226 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 227 | kunmap_atomic(kaddr); |
c8b97818 CM |
228 | |
229 | i++; | |
230 | ptr += cur_size; | |
231 | compressed_size -= cur_size; | |
232 | } | |
233 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 234 | compress_type); |
c8b97818 CM |
235 | } else { |
236 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 237 | start >> PAGE_SHIFT); |
c8b97818 | 238 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 239 | kaddr = kmap_atomic(page); |
7073017a | 240 | offset = offset_in_page(start); |
c8b97818 | 241 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 242 | kunmap_atomic(kaddr); |
09cbfeaf | 243 | put_page(page); |
c8b97818 CM |
244 | } |
245 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 246 | btrfs_release_path(path); |
c8b97818 | 247 | |
9ddc959e JB |
248 | /* |
249 | * We align size to sectorsize for inline extents just for simplicity | |
250 | * sake. | |
251 | */ | |
252 | size = ALIGN(size, root->fs_info->sectorsize); | |
253 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); | |
254 | if (ret) | |
255 | goto fail; | |
256 | ||
c2167754 YZ |
257 | /* |
258 | * we're an inline extent, so nobody can | |
259 | * extend the file past i_size without locking | |
260 | * a page we already have locked. | |
261 | * | |
262 | * We must do any isize and inode updates | |
263 | * before we unlock the pages. Otherwise we | |
264 | * could end up racing with unlink. | |
265 | */ | |
c8b97818 | 266 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 267 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 268 | |
c8b97818 | 269 | fail: |
79b4f4c6 | 270 | return ret; |
c8b97818 CM |
271 | } |
272 | ||
273 | ||
274 | /* | |
275 | * conditionally insert an inline extent into the file. This | |
276 | * does the checks required to make sure the data is small enough | |
277 | * to fit as an inline extent. | |
278 | */ | |
a0349401 | 279 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, |
00361589 JB |
280 | u64 end, size_t compressed_size, |
281 | int compress_type, | |
282 | struct page **compressed_pages) | |
c8b97818 | 283 | { |
a0349401 | 284 | struct btrfs_root *root = inode->root; |
0b246afa | 285 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 286 | struct btrfs_trans_handle *trans; |
a0349401 | 287 | u64 isize = i_size_read(&inode->vfs_inode); |
c8b97818 CM |
288 | u64 actual_end = min(end + 1, isize); |
289 | u64 inline_len = actual_end - start; | |
0b246afa | 290 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
291 | u64 data_len = inline_len; |
292 | int ret; | |
1acae57b FDBM |
293 | struct btrfs_path *path; |
294 | int extent_inserted = 0; | |
295 | u32 extent_item_size; | |
c8b97818 CM |
296 | |
297 | if (compressed_size) | |
298 | data_len = compressed_size; | |
299 | ||
300 | if (start > 0 || | |
0b246afa JM |
301 | actual_end > fs_info->sectorsize || |
302 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 303 | (!compressed_size && |
0b246afa | 304 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 305 | end + 1 < isize || |
0b246afa | 306 | data_len > fs_info->max_inline) { |
c8b97818 CM |
307 | return 1; |
308 | } | |
309 | ||
1acae57b FDBM |
310 | path = btrfs_alloc_path(); |
311 | if (!path) | |
312 | return -ENOMEM; | |
313 | ||
00361589 | 314 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
315 | if (IS_ERR(trans)) { |
316 | btrfs_free_path(path); | |
00361589 | 317 | return PTR_ERR(trans); |
1acae57b | 318 | } |
a0349401 | 319 | trans->block_rsv = &inode->block_rsv; |
00361589 | 320 | |
1acae57b FDBM |
321 | if (compressed_size && compressed_pages) |
322 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
323 | compressed_size); | |
324 | else | |
325 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
326 | inline_len); | |
327 | ||
a0349401 NB |
328 | ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end, |
329 | NULL, 1, 1, extent_item_size, | |
330 | &extent_inserted); | |
00361589 | 331 | if (ret) { |
66642832 | 332 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
333 | goto out; |
334 | } | |
c8b97818 CM |
335 | |
336 | if (isize > actual_end) | |
337 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b | 338 | ret = insert_inline_extent(trans, path, extent_inserted, |
a0349401 | 339 | root, &inode->vfs_inode, start, |
c8b97818 | 340 | inline_len, compressed_size, |
fe3f566c | 341 | compress_type, compressed_pages); |
2adcac1a | 342 | if (ret && ret != -ENOSPC) { |
66642832 | 343 | btrfs_abort_transaction(trans, ret); |
00361589 | 344 | goto out; |
2adcac1a | 345 | } else if (ret == -ENOSPC) { |
00361589 JB |
346 | ret = 1; |
347 | goto out; | |
79787eaa | 348 | } |
2adcac1a | 349 | |
a0349401 NB |
350 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
351 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); | |
00361589 | 352 | out: |
94ed938a QW |
353 | /* |
354 | * Don't forget to free the reserved space, as for inlined extent | |
355 | * it won't count as data extent, free them directly here. | |
356 | * And at reserve time, it's always aligned to page size, so | |
357 | * just free one page here. | |
358 | */ | |
a0349401 | 359 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 360 | btrfs_free_path(path); |
3a45bb20 | 361 | btrfs_end_transaction(trans); |
00361589 | 362 | return ret; |
c8b97818 CM |
363 | } |
364 | ||
771ed689 CM |
365 | struct async_extent { |
366 | u64 start; | |
367 | u64 ram_size; | |
368 | u64 compressed_size; | |
369 | struct page **pages; | |
370 | unsigned long nr_pages; | |
261507a0 | 371 | int compress_type; |
771ed689 CM |
372 | struct list_head list; |
373 | }; | |
374 | ||
97db1204 | 375 | struct async_chunk { |
771ed689 | 376 | struct inode *inode; |
771ed689 CM |
377 | struct page *locked_page; |
378 | u64 start; | |
379 | u64 end; | |
f82b7359 | 380 | unsigned int write_flags; |
771ed689 | 381 | struct list_head extents; |
ec39f769 | 382 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 383 | struct btrfs_work work; |
97db1204 | 384 | atomic_t *pending; |
771ed689 CM |
385 | }; |
386 | ||
97db1204 NB |
387 | struct async_cow { |
388 | /* Number of chunks in flight; must be first in the structure */ | |
389 | atomic_t num_chunks; | |
390 | struct async_chunk chunks[]; | |
771ed689 CM |
391 | }; |
392 | ||
97db1204 | 393 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
394 | u64 start, u64 ram_size, |
395 | u64 compressed_size, | |
396 | struct page **pages, | |
261507a0 LZ |
397 | unsigned long nr_pages, |
398 | int compress_type) | |
771ed689 CM |
399 | { |
400 | struct async_extent *async_extent; | |
401 | ||
402 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 403 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
404 | async_extent->start = start; |
405 | async_extent->ram_size = ram_size; | |
406 | async_extent->compressed_size = compressed_size; | |
407 | async_extent->pages = pages; | |
408 | async_extent->nr_pages = nr_pages; | |
261507a0 | 409 | async_extent->compress_type = compress_type; |
771ed689 CM |
410 | list_add_tail(&async_extent->list, &cow->extents); |
411 | return 0; | |
412 | } | |
413 | ||
42c16da6 QW |
414 | /* |
415 | * Check if the inode has flags compatible with compression | |
416 | */ | |
99c88dc7 | 417 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
42c16da6 | 418 | { |
99c88dc7 NB |
419 | if (inode->flags & BTRFS_INODE_NODATACOW || |
420 | inode->flags & BTRFS_INODE_NODATASUM) | |
42c16da6 QW |
421 | return false; |
422 | return true; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Check if the inode needs to be submitted to compression, based on mount | |
427 | * options, defragmentation, properties or heuristics. | |
428 | */ | |
808a1292 NB |
429 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
430 | u64 end) | |
f79707b0 | 431 | { |
808a1292 | 432 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
f79707b0 | 433 | |
808a1292 | 434 | if (!inode_can_compress(inode)) { |
42c16da6 QW |
435 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
436 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
808a1292 | 437 | btrfs_ino(inode)); |
42c16da6 QW |
438 | return 0; |
439 | } | |
f79707b0 | 440 | /* force compress */ |
0b246afa | 441 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 442 | return 1; |
eec63c65 | 443 | /* defrag ioctl */ |
808a1292 | 444 | if (inode->defrag_compress) |
eec63c65 | 445 | return 1; |
f79707b0 | 446 | /* bad compression ratios */ |
808a1292 | 447 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
f79707b0 | 448 | return 0; |
0b246afa | 449 | if (btrfs_test_opt(fs_info, COMPRESS) || |
808a1292 NB |
450 | inode->flags & BTRFS_INODE_COMPRESS || |
451 | inode->prop_compress) | |
452 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); | |
f79707b0 WS |
453 | return 0; |
454 | } | |
455 | ||
6158e1ce | 456 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
457 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
458 | { | |
459 | /* If this is a small write inside eof, kick off a defrag */ | |
460 | if (num_bytes < small_write && | |
6158e1ce | 461 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
462 | btrfs_add_inode_defrag(NULL, inode); |
463 | } | |
464 | ||
d352ac68 | 465 | /* |
771ed689 CM |
466 | * we create compressed extents in two phases. The first |
467 | * phase compresses a range of pages that have already been | |
468 | * locked (both pages and state bits are locked). | |
c8b97818 | 469 | * |
771ed689 CM |
470 | * This is done inside an ordered work queue, and the compression |
471 | * is spread across many cpus. The actual IO submission is step | |
472 | * two, and the ordered work queue takes care of making sure that | |
473 | * happens in the same order things were put onto the queue by | |
474 | * writepages and friends. | |
c8b97818 | 475 | * |
771ed689 CM |
476 | * If this code finds it can't get good compression, it puts an |
477 | * entry onto the work queue to write the uncompressed bytes. This | |
478 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
479 | * are written in the same order that the flusher thread sent them |
480 | * down. | |
d352ac68 | 481 | */ |
ac3e9933 | 482 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 483 | { |
1368c6da | 484 | struct inode *inode = async_chunk->inode; |
0b246afa | 485 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 486 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
487 | u64 start = async_chunk->start; |
488 | u64 end = async_chunk->end; | |
c8b97818 | 489 | u64 actual_end; |
d98da499 | 490 | u64 i_size; |
e6dcd2dc | 491 | int ret = 0; |
c8b97818 CM |
492 | struct page **pages = NULL; |
493 | unsigned long nr_pages; | |
c8b97818 CM |
494 | unsigned long total_compressed = 0; |
495 | unsigned long total_in = 0; | |
c8b97818 CM |
496 | int i; |
497 | int will_compress; | |
0b246afa | 498 | int compress_type = fs_info->compress_type; |
ac3e9933 | 499 | int compressed_extents = 0; |
4adaa611 | 500 | int redirty = 0; |
b888db2b | 501 | |
6158e1ce NB |
502 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
503 | SZ_16K); | |
4cb5300b | 504 | |
d98da499 JB |
505 | /* |
506 | * We need to save i_size before now because it could change in between | |
507 | * us evaluating the size and assigning it. This is because we lock and | |
508 | * unlock the page in truncate and fallocate, and then modify the i_size | |
509 | * later on. | |
510 | * | |
511 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
512 | * does that for us. | |
513 | */ | |
514 | barrier(); | |
515 | i_size = i_size_read(inode); | |
516 | barrier(); | |
517 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
518 | again: |
519 | will_compress = 0; | |
09cbfeaf | 520 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
521 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
522 | nr_pages = min_t(unsigned long, nr_pages, | |
523 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 524 | |
f03d9301 CM |
525 | /* |
526 | * we don't want to send crud past the end of i_size through | |
527 | * compression, that's just a waste of CPU time. So, if the | |
528 | * end of the file is before the start of our current | |
529 | * requested range of bytes, we bail out to the uncompressed | |
530 | * cleanup code that can deal with all of this. | |
531 | * | |
532 | * It isn't really the fastest way to fix things, but this is a | |
533 | * very uncommon corner. | |
534 | */ | |
535 | if (actual_end <= start) | |
536 | goto cleanup_and_bail_uncompressed; | |
537 | ||
c8b97818 CM |
538 | total_compressed = actual_end - start; |
539 | ||
4bcbb332 SW |
540 | /* |
541 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 542 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
543 | */ |
544 | if (total_compressed <= blocksize && | |
545 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
546 | goto cleanup_and_bail_uncompressed; | |
547 | ||
069eac78 DS |
548 | total_compressed = min_t(unsigned long, total_compressed, |
549 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
550 | total_in = 0; |
551 | ret = 0; | |
db94535d | 552 | |
771ed689 CM |
553 | /* |
554 | * we do compression for mount -o compress and when the | |
555 | * inode has not been flagged as nocompress. This flag can | |
556 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 557 | */ |
808a1292 | 558 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
c8b97818 | 559 | WARN_ON(pages); |
31e818fe | 560 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
561 | if (!pages) { |
562 | /* just bail out to the uncompressed code */ | |
3527a018 | 563 | nr_pages = 0; |
560f7d75 LZ |
564 | goto cont; |
565 | } | |
c8b97818 | 566 | |
eec63c65 DS |
567 | if (BTRFS_I(inode)->defrag_compress) |
568 | compress_type = BTRFS_I(inode)->defrag_compress; | |
569 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 570 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 571 | |
4adaa611 CM |
572 | /* |
573 | * we need to call clear_page_dirty_for_io on each | |
574 | * page in the range. Otherwise applications with the file | |
575 | * mmap'd can wander in and change the page contents while | |
576 | * we are compressing them. | |
577 | * | |
578 | * If the compression fails for any reason, we set the pages | |
579 | * dirty again later on. | |
e9679de3 TT |
580 | * |
581 | * Note that the remaining part is redirtied, the start pointer | |
582 | * has moved, the end is the original one. | |
4adaa611 | 583 | */ |
e9679de3 TT |
584 | if (!redirty) { |
585 | extent_range_clear_dirty_for_io(inode, start, end); | |
586 | redirty = 1; | |
587 | } | |
f51d2b59 DS |
588 | |
589 | /* Compression level is applied here and only here */ | |
590 | ret = btrfs_compress_pages( | |
591 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 592 | inode->i_mapping, start, |
38c31464 | 593 | pages, |
4d3a800e | 594 | &nr_pages, |
261507a0 | 595 | &total_in, |
e5d74902 | 596 | &total_compressed); |
c8b97818 CM |
597 | |
598 | if (!ret) { | |
7073017a | 599 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 600 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
601 | char *kaddr; |
602 | ||
603 | /* zero the tail end of the last page, we might be | |
604 | * sending it down to disk | |
605 | */ | |
606 | if (offset) { | |
7ac687d9 | 607 | kaddr = kmap_atomic(page); |
c8b97818 | 608 | memset(kaddr + offset, 0, |
09cbfeaf | 609 | PAGE_SIZE - offset); |
7ac687d9 | 610 | kunmap_atomic(kaddr); |
c8b97818 CM |
611 | } |
612 | will_compress = 1; | |
613 | } | |
614 | } | |
560f7d75 | 615 | cont: |
c8b97818 CM |
616 | if (start == 0) { |
617 | /* lets try to make an inline extent */ | |
6018ba0a | 618 | if (ret || total_in < actual_end) { |
c8b97818 | 619 | /* we didn't compress the entire range, try |
771ed689 | 620 | * to make an uncompressed inline extent. |
c8b97818 | 621 | */ |
a0349401 NB |
622 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
623 | 0, BTRFS_COMPRESS_NONE, | |
624 | NULL); | |
c8b97818 | 625 | } else { |
771ed689 | 626 | /* try making a compressed inline extent */ |
a0349401 | 627 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
fe3f566c LZ |
628 | total_compressed, |
629 | compress_type, pages); | |
c8b97818 | 630 | } |
79787eaa | 631 | if (ret <= 0) { |
151a41bc | 632 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
633 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
634 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
635 | unsigned long page_error_op; |
636 | ||
e6eb4314 | 637 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 638 | |
771ed689 | 639 | /* |
79787eaa JM |
640 | * inline extent creation worked or returned error, |
641 | * we don't need to create any more async work items. | |
642 | * Unlock and free up our temp pages. | |
8b62f87b JB |
643 | * |
644 | * We use DO_ACCOUNTING here because we need the | |
645 | * delalloc_release_metadata to be done _after_ we drop | |
646 | * our outstanding extent for clearing delalloc for this | |
647 | * range. | |
771ed689 | 648 | */ |
ad7ff17b NB |
649 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
650 | NULL, | |
74e9194a | 651 | clear_flags, |
ba8b04c1 | 652 | PAGE_UNLOCK | |
c2790a2e JB |
653 | PAGE_CLEAR_DIRTY | |
654 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 655 | page_error_op | |
c2790a2e | 656 | PAGE_END_WRITEBACK); |
cecc8d90 | 657 | |
1e6e238c QW |
658 | /* |
659 | * Ensure we only free the compressed pages if we have | |
660 | * them allocated, as we can still reach here with | |
661 | * inode_need_compress() == false. | |
662 | */ | |
663 | if (pages) { | |
664 | for (i = 0; i < nr_pages; i++) { | |
665 | WARN_ON(pages[i]->mapping); | |
666 | put_page(pages[i]); | |
667 | } | |
668 | kfree(pages); | |
cecc8d90 | 669 | } |
cecc8d90 | 670 | return 0; |
c8b97818 CM |
671 | } |
672 | } | |
673 | ||
674 | if (will_compress) { | |
675 | /* | |
676 | * we aren't doing an inline extent round the compressed size | |
677 | * up to a block size boundary so the allocator does sane | |
678 | * things | |
679 | */ | |
fda2832f | 680 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
681 | |
682 | /* | |
683 | * one last check to make sure the compression is really a | |
170607eb TT |
684 | * win, compare the page count read with the blocks on disk, |
685 | * compression must free at least one sector size | |
c8b97818 | 686 | */ |
09cbfeaf | 687 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 688 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 689 | compressed_extents++; |
c8bb0c8b AS |
690 | |
691 | /* | |
692 | * The async work queues will take care of doing actual | |
693 | * allocation on disk for these compressed pages, and | |
694 | * will submit them to the elevator. | |
695 | */ | |
b5326271 | 696 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 697 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
698 | compress_type); |
699 | ||
1170862d TT |
700 | if (start + total_in < end) { |
701 | start += total_in; | |
c8bb0c8b AS |
702 | pages = NULL; |
703 | cond_resched(); | |
704 | goto again; | |
705 | } | |
ac3e9933 | 706 | return compressed_extents; |
c8b97818 CM |
707 | } |
708 | } | |
c8bb0c8b | 709 | if (pages) { |
c8b97818 CM |
710 | /* |
711 | * the compression code ran but failed to make things smaller, | |
712 | * free any pages it allocated and our page pointer array | |
713 | */ | |
4d3a800e | 714 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 715 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 716 | put_page(pages[i]); |
c8b97818 CM |
717 | } |
718 | kfree(pages); | |
719 | pages = NULL; | |
720 | total_compressed = 0; | |
4d3a800e | 721 | nr_pages = 0; |
c8b97818 CM |
722 | |
723 | /* flag the file so we don't compress in the future */ | |
0b246afa | 724 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 725 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 726 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 727 | } |
c8b97818 | 728 | } |
f03d9301 | 729 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
730 | /* |
731 | * No compression, but we still need to write the pages in the file | |
732 | * we've been given so far. redirty the locked page if it corresponds | |
733 | * to our extent and set things up for the async work queue to run | |
734 | * cow_file_range to do the normal delalloc dance. | |
735 | */ | |
1d53c9e6 CM |
736 | if (async_chunk->locked_page && |
737 | (page_offset(async_chunk->locked_page) >= start && | |
738 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 739 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 740 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 741 | } |
c8bb0c8b AS |
742 | |
743 | if (redirty) | |
744 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 745 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 746 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 747 | compressed_extents++; |
3b951516 | 748 | |
ac3e9933 | 749 | return compressed_extents; |
771ed689 | 750 | } |
771ed689 | 751 | |
40ae837b FM |
752 | static void free_async_extent_pages(struct async_extent *async_extent) |
753 | { | |
754 | int i; | |
755 | ||
756 | if (!async_extent->pages) | |
757 | return; | |
758 | ||
759 | for (i = 0; i < async_extent->nr_pages; i++) { | |
760 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 761 | put_page(async_extent->pages[i]); |
40ae837b FM |
762 | } |
763 | kfree(async_extent->pages); | |
764 | async_extent->nr_pages = 0; | |
765 | async_extent->pages = NULL; | |
771ed689 CM |
766 | } |
767 | ||
768 | /* | |
769 | * phase two of compressed writeback. This is the ordered portion | |
770 | * of the code, which only gets called in the order the work was | |
771 | * queued. We walk all the async extents created by compress_file_range | |
772 | * and send them down to the disk. | |
773 | */ | |
b5326271 | 774 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
771ed689 | 775 | { |
a0ff10dc NB |
776 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
777 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
771ed689 CM |
778 | struct async_extent *async_extent; |
779 | u64 alloc_hint = 0; | |
771ed689 CM |
780 | struct btrfs_key ins; |
781 | struct extent_map *em; | |
a0ff10dc NB |
782 | struct btrfs_root *root = inode->root; |
783 | struct extent_io_tree *io_tree = &inode->io_tree; | |
f5a84ee3 | 784 | int ret = 0; |
771ed689 | 785 | |
3e04e7f1 | 786 | again: |
b5326271 NB |
787 | while (!list_empty(&async_chunk->extents)) { |
788 | async_extent = list_entry(async_chunk->extents.next, | |
771ed689 CM |
789 | struct async_extent, list); |
790 | list_del(&async_extent->list); | |
c8b97818 | 791 | |
f5a84ee3 | 792 | retry: |
7447555f NB |
793 | lock_extent(io_tree, async_extent->start, |
794 | async_extent->start + async_extent->ram_size - 1); | |
771ed689 CM |
795 | /* did the compression code fall back to uncompressed IO? */ |
796 | if (!async_extent->pages) { | |
797 | int page_started = 0; | |
798 | unsigned long nr_written = 0; | |
799 | ||
771ed689 | 800 | /* allocate blocks */ |
a0ff10dc | 801 | ret = cow_file_range(inode, async_chunk->locked_page, |
f5a84ee3 JB |
802 | async_extent->start, |
803 | async_extent->start + | |
804 | async_extent->ram_size - 1, | |
330a5827 | 805 | &page_started, &nr_written, 0); |
771ed689 | 806 | |
79787eaa JM |
807 | /* JDM XXX */ |
808 | ||
771ed689 CM |
809 | /* |
810 | * if page_started, cow_file_range inserted an | |
811 | * inline extent and took care of all the unlocking | |
812 | * and IO for us. Otherwise, we need to submit | |
813 | * all those pages down to the drive. | |
814 | */ | |
f5a84ee3 | 815 | if (!page_started && !ret) |
a0ff10dc | 816 | extent_write_locked_range(&inode->vfs_inode, |
5e3ee236 | 817 | async_extent->start, |
d397712b | 818 | async_extent->start + |
771ed689 | 819 | async_extent->ram_size - 1, |
771ed689 | 820 | WB_SYNC_ALL); |
1d53c9e6 | 821 | else if (ret && async_chunk->locked_page) |
b5326271 | 822 | unlock_page(async_chunk->locked_page); |
771ed689 CM |
823 | kfree(async_extent); |
824 | cond_resched(); | |
825 | continue; | |
826 | } | |
827 | ||
18513091 | 828 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
829 | async_extent->compressed_size, |
830 | async_extent->compressed_size, | |
e570fd27 | 831 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 832 | if (ret) { |
40ae837b | 833 | free_async_extent_pages(async_extent); |
3e04e7f1 | 834 | |
fdf8e2ea JB |
835 | if (ret == -ENOSPC) { |
836 | unlock_extent(io_tree, async_extent->start, | |
837 | async_extent->start + | |
838 | async_extent->ram_size - 1); | |
ce62003f LB |
839 | |
840 | /* | |
841 | * we need to redirty the pages if we decide to | |
842 | * fallback to uncompressed IO, otherwise we | |
843 | * will not submit these pages down to lower | |
844 | * layers. | |
845 | */ | |
a0ff10dc | 846 | extent_range_redirty_for_io(&inode->vfs_inode, |
ce62003f LB |
847 | async_extent->start, |
848 | async_extent->start + | |
849 | async_extent->ram_size - 1); | |
850 | ||
79787eaa | 851 | goto retry; |
fdf8e2ea | 852 | } |
3e04e7f1 | 853 | goto out_free; |
f5a84ee3 | 854 | } |
c2167754 YZ |
855 | /* |
856 | * here we're doing allocation and writeback of the | |
857 | * compressed pages | |
858 | */ | |
a0ff10dc | 859 | em = create_io_em(inode, async_extent->start, |
6f9994db LB |
860 | async_extent->ram_size, /* len */ |
861 | async_extent->start, /* orig_start */ | |
862 | ins.objectid, /* block_start */ | |
863 | ins.offset, /* block_len */ | |
864 | ins.offset, /* orig_block_len */ | |
865 | async_extent->ram_size, /* ram_bytes */ | |
866 | async_extent->compress_type, | |
867 | BTRFS_ORDERED_COMPRESSED); | |
868 | if (IS_ERR(em)) | |
869 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 870 | goto out_free_reserve; |
6f9994db | 871 | free_extent_map(em); |
3e04e7f1 | 872 | |
a0ff10dc | 873 | ret = btrfs_add_ordered_extent_compress(inode, |
261507a0 LZ |
874 | async_extent->start, |
875 | ins.objectid, | |
876 | async_extent->ram_size, | |
877 | ins.offset, | |
878 | BTRFS_ORDERED_COMPRESSED, | |
879 | async_extent->compress_type); | |
d9f85963 | 880 | if (ret) { |
a0ff10dc | 881 | btrfs_drop_extent_cache(inode, async_extent->start, |
d9f85963 FM |
882 | async_extent->start + |
883 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 884 | goto out_free_reserve; |
d9f85963 | 885 | } |
0b246afa | 886 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 887 | |
771ed689 CM |
888 | /* |
889 | * clear dirty, set writeback and unlock the pages. | |
890 | */ | |
a0ff10dc | 891 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
892 | async_extent->start + |
893 | async_extent->ram_size - 1, | |
151a41bc JB |
894 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
895 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 896 | PAGE_SET_WRITEBACK); |
a0ff10dc | 897 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
d397712b CM |
898 | async_extent->ram_size, |
899 | ins.objectid, | |
900 | ins.offset, async_extent->pages, | |
f82b7359 | 901 | async_extent->nr_pages, |
ec39f769 CM |
902 | async_chunk->write_flags, |
903 | async_chunk->blkcg_css)) { | |
fce2a4e6 FM |
904 | struct page *p = async_extent->pages[0]; |
905 | const u64 start = async_extent->start; | |
906 | const u64 end = start + async_extent->ram_size - 1; | |
907 | ||
a0ff10dc | 908 | p->mapping = inode->vfs_inode.i_mapping; |
c629732d | 909 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
7087a9d8 | 910 | |
fce2a4e6 | 911 | p->mapping = NULL; |
a0ff10dc | 912 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
fce2a4e6 FM |
913 | PAGE_END_WRITEBACK | |
914 | PAGE_SET_ERROR); | |
40ae837b | 915 | free_async_extent_pages(async_extent); |
fce2a4e6 | 916 | } |
771ed689 CM |
917 | alloc_hint = ins.objectid + ins.offset; |
918 | kfree(async_extent); | |
919 | cond_resched(); | |
920 | } | |
dec8f175 | 921 | return; |
3e04e7f1 | 922 | out_free_reserve: |
0b246afa | 923 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 924 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 925 | out_free: |
a0ff10dc | 926 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
927 | async_extent->start + |
928 | async_extent->ram_size - 1, | |
c2790a2e | 929 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 930 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
931 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
932 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
933 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
934 | PAGE_SET_ERROR); | |
40ae837b | 935 | free_async_extent_pages(async_extent); |
79787eaa | 936 | kfree(async_extent); |
3e04e7f1 | 937 | goto again; |
771ed689 CM |
938 | } |
939 | ||
43c69849 | 940 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
941 | u64 num_bytes) |
942 | { | |
43c69849 | 943 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
944 | struct extent_map *em; |
945 | u64 alloc_hint = 0; | |
946 | ||
947 | read_lock(&em_tree->lock); | |
948 | em = search_extent_mapping(em_tree, start, num_bytes); | |
949 | if (em) { | |
950 | /* | |
951 | * if block start isn't an actual block number then find the | |
952 | * first block in this inode and use that as a hint. If that | |
953 | * block is also bogus then just don't worry about it. | |
954 | */ | |
955 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
956 | free_extent_map(em); | |
957 | em = search_extent_mapping(em_tree, 0, 0); | |
958 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
959 | alloc_hint = em->block_start; | |
960 | if (em) | |
961 | free_extent_map(em); | |
962 | } else { | |
963 | alloc_hint = em->block_start; | |
964 | free_extent_map(em); | |
965 | } | |
966 | } | |
967 | read_unlock(&em_tree->lock); | |
968 | ||
969 | return alloc_hint; | |
970 | } | |
971 | ||
771ed689 CM |
972 | /* |
973 | * when extent_io.c finds a delayed allocation range in the file, | |
974 | * the call backs end up in this code. The basic idea is to | |
975 | * allocate extents on disk for the range, and create ordered data structs | |
976 | * in ram to track those extents. | |
977 | * | |
978 | * locked_page is the page that writepage had locked already. We use | |
979 | * it to make sure we don't do extra locks or unlocks. | |
980 | * | |
981 | * *page_started is set to one if we unlock locked_page and do everything | |
982 | * required to start IO on it. It may be clean and already done with | |
983 | * IO when we return. | |
984 | */ | |
6e26c442 | 985 | static noinline int cow_file_range(struct btrfs_inode *inode, |
00361589 | 986 | struct page *locked_page, |
74e9194a | 987 | u64 start, u64 end, int *page_started, |
330a5827 | 988 | unsigned long *nr_written, int unlock) |
771ed689 | 989 | { |
6e26c442 NB |
990 | struct btrfs_root *root = inode->root; |
991 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 CM |
992 | u64 alloc_hint = 0; |
993 | u64 num_bytes; | |
994 | unsigned long ram_size; | |
a315e68f | 995 | u64 cur_alloc_size = 0; |
432cd2a1 | 996 | u64 min_alloc_size; |
0b246afa | 997 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
998 | struct btrfs_key ins; |
999 | struct extent_map *em; | |
a315e68f FM |
1000 | unsigned clear_bits; |
1001 | unsigned long page_ops; | |
1002 | bool extent_reserved = false; | |
771ed689 CM |
1003 | int ret = 0; |
1004 | ||
6e26c442 | 1005 | if (btrfs_is_free_space_inode(inode)) { |
02ecd2c2 | 1006 | WARN_ON_ONCE(1); |
29bce2f3 JB |
1007 | ret = -EINVAL; |
1008 | goto out_unlock; | |
02ecd2c2 | 1009 | } |
771ed689 | 1010 | |
fda2832f | 1011 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1012 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1013 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1014 | |
6e26c442 | 1015 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1016 | |
771ed689 CM |
1017 | if (start == 0) { |
1018 | /* lets try to make an inline extent */ | |
6e26c442 | 1019 | ret = cow_file_range_inline(inode, start, end, 0, |
d02c0e20 | 1020 | BTRFS_COMPRESS_NONE, NULL); |
771ed689 | 1021 | if (ret == 0) { |
8b62f87b JB |
1022 | /* |
1023 | * We use DO_ACCOUNTING here because we need the | |
1024 | * delalloc_release_metadata to be run _after_ we drop | |
1025 | * our outstanding extent for clearing delalloc for this | |
1026 | * range. | |
1027 | */ | |
6e26c442 | 1028 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
c2790a2e | 1029 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1030 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1031 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
1032 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1033 | PAGE_END_WRITEBACK); | |
771ed689 | 1034 | *nr_written = *nr_written + |
09cbfeaf | 1035 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1036 | *page_started = 1; |
771ed689 | 1037 | goto out; |
79787eaa | 1038 | } else if (ret < 0) { |
79787eaa | 1039 | goto out_unlock; |
771ed689 CM |
1040 | } |
1041 | } | |
1042 | ||
6e26c442 NB |
1043 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
1044 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | |
771ed689 | 1045 | |
432cd2a1 FM |
1046 | /* |
1047 | * Relocation relies on the relocated extents to have exactly the same | |
1048 | * size as the original extents. Normally writeback for relocation data | |
1049 | * extents follows a NOCOW path because relocation preallocates the | |
1050 | * extents. However, due to an operation such as scrub turning a block | |
1051 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1052 | * an extent allocated during COW has exactly the requested size and can | |
1053 | * not be split into smaller extents, otherwise relocation breaks and | |
1054 | * fails during the stage where it updates the bytenr of file extent | |
1055 | * items. | |
1056 | */ | |
1057 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1058 | min_alloc_size = num_bytes; | |
1059 | else | |
1060 | min_alloc_size = fs_info->sectorsize; | |
1061 | ||
3752d22f AJ |
1062 | while (num_bytes > 0) { |
1063 | cur_alloc_size = num_bytes; | |
18513091 | 1064 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1065 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1066 | &ins, 1, 1); |
00361589 | 1067 | if (ret < 0) |
79787eaa | 1068 | goto out_unlock; |
a315e68f FM |
1069 | cur_alloc_size = ins.offset; |
1070 | extent_reserved = true; | |
d397712b | 1071 | |
771ed689 | 1072 | ram_size = ins.offset; |
6e26c442 | 1073 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1074 | start, /* orig_start */ |
1075 | ins.objectid, /* block_start */ | |
1076 | ins.offset, /* block_len */ | |
1077 | ins.offset, /* orig_block_len */ | |
1078 | ram_size, /* ram_bytes */ | |
1079 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1080 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1081 | if (IS_ERR(em)) { |
1082 | ret = PTR_ERR(em); | |
ace68bac | 1083 | goto out_reserve; |
090a127a | 1084 | } |
6f9994db | 1085 | free_extent_map(em); |
e6dcd2dc | 1086 | |
6e26c442 NB |
1087 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
1088 | ram_size, cur_alloc_size, 0); | |
ace68bac | 1089 | if (ret) |
d9f85963 | 1090 | goto out_drop_extent_cache; |
c8b97818 | 1091 | |
17d217fe YZ |
1092 | if (root->root_key.objectid == |
1093 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
6e26c442 | 1094 | ret = btrfs_reloc_clone_csums(inode, start, |
17d217fe | 1095 | cur_alloc_size); |
4dbd80fb QW |
1096 | /* |
1097 | * Only drop cache here, and process as normal. | |
1098 | * | |
1099 | * We must not allow extent_clear_unlock_delalloc() | |
1100 | * at out_unlock label to free meta of this ordered | |
1101 | * extent, as its meta should be freed by | |
1102 | * btrfs_finish_ordered_io(). | |
1103 | * | |
1104 | * So we must continue until @start is increased to | |
1105 | * skip current ordered extent. | |
1106 | */ | |
00361589 | 1107 | if (ret) |
6e26c442 | 1108 | btrfs_drop_extent_cache(inode, start, |
4dbd80fb | 1109 | start + ram_size - 1, 0); |
17d217fe YZ |
1110 | } |
1111 | ||
0b246afa | 1112 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1113 | |
c8b97818 CM |
1114 | /* we're not doing compressed IO, don't unlock the first |
1115 | * page (which the caller expects to stay locked), don't | |
1116 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1117 | * |
1118 | * Do set the Private2 bit so we know this page was properly | |
1119 | * setup for writepage | |
c8b97818 | 1120 | */ |
a315e68f FM |
1121 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1122 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1123 | |
6e26c442 | 1124 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1125 | locked_page, |
c2790a2e | 1126 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1127 | page_ops); |
3752d22f AJ |
1128 | if (num_bytes < cur_alloc_size) |
1129 | num_bytes = 0; | |
4dbd80fb | 1130 | else |
3752d22f | 1131 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1132 | alloc_hint = ins.objectid + ins.offset; |
1133 | start += cur_alloc_size; | |
a315e68f | 1134 | extent_reserved = false; |
4dbd80fb QW |
1135 | |
1136 | /* | |
1137 | * btrfs_reloc_clone_csums() error, since start is increased | |
1138 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1139 | * free metadata of current ordered extent, we're OK to exit. | |
1140 | */ | |
1141 | if (ret) | |
1142 | goto out_unlock; | |
b888db2b | 1143 | } |
79787eaa | 1144 | out: |
be20aa9d | 1145 | return ret; |
b7d5b0a8 | 1146 | |
d9f85963 | 1147 | out_drop_extent_cache: |
6e26c442 | 1148 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
ace68bac | 1149 | out_reserve: |
0b246afa | 1150 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1151 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1152 | out_unlock: |
a7e3b975 FM |
1153 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1154 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1155 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1156 | PAGE_END_WRITEBACK; | |
1157 | /* | |
1158 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1159 | * failed to create the respective ordered extent, then it means that | |
1160 | * when we reserved the extent we decremented the extent's size from | |
1161 | * the data space_info's bytes_may_use counter and incremented the | |
1162 | * space_info's bytes_reserved counter by the same amount. We must make | |
1163 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1164 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1165 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1166 | */ | |
1167 | if (extent_reserved) { | |
6e26c442 | 1168 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1169 | start + cur_alloc_size - 1, |
a315e68f FM |
1170 | locked_page, |
1171 | clear_bits, | |
1172 | page_ops); | |
1173 | start += cur_alloc_size; | |
1174 | if (start >= end) | |
1175 | goto out; | |
1176 | } | |
6e26c442 | 1177 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1178 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1179 | page_ops); | |
79787eaa | 1180 | goto out; |
771ed689 | 1181 | } |
c8b97818 | 1182 | |
771ed689 CM |
1183 | /* |
1184 | * work queue call back to started compression on a file and pages | |
1185 | */ | |
1186 | static noinline void async_cow_start(struct btrfs_work *work) | |
1187 | { | |
b5326271 | 1188 | struct async_chunk *async_chunk; |
ac3e9933 | 1189 | int compressed_extents; |
771ed689 | 1190 | |
b5326271 | 1191 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1192 | |
ac3e9933 NB |
1193 | compressed_extents = compress_file_range(async_chunk); |
1194 | if (compressed_extents == 0) { | |
b5326271 NB |
1195 | btrfs_add_delayed_iput(async_chunk->inode); |
1196 | async_chunk->inode = NULL; | |
8180ef88 | 1197 | } |
771ed689 CM |
1198 | } |
1199 | ||
1200 | /* | |
1201 | * work queue call back to submit previously compressed pages | |
1202 | */ | |
1203 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1204 | { | |
c5a68aec NB |
1205 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1206 | work); | |
1207 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1208 | unsigned long nr_pages; |
1209 | ||
b5326271 | 1210 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1211 | PAGE_SHIFT; |
771ed689 | 1212 | |
093258e6 | 1213 | /* atomic_sub_return implies a barrier */ |
0b246afa | 1214 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
093258e6 DS |
1215 | 5 * SZ_1M) |
1216 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1217 | |
4546d178 | 1218 | /* |
b5326271 | 1219 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1220 | * in which case we don't have anything to submit, yet we need to |
1221 | * always adjust ->async_delalloc_pages as its paired with the init | |
1222 | * happening in cow_file_range_async | |
1223 | */ | |
b5326271 NB |
1224 | if (async_chunk->inode) |
1225 | submit_compressed_extents(async_chunk); | |
771ed689 | 1226 | } |
c8b97818 | 1227 | |
771ed689 CM |
1228 | static noinline void async_cow_free(struct btrfs_work *work) |
1229 | { | |
b5326271 | 1230 | struct async_chunk *async_chunk; |
97db1204 | 1231 | |
b5326271 NB |
1232 | async_chunk = container_of(work, struct async_chunk, work); |
1233 | if (async_chunk->inode) | |
1234 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1235 | if (async_chunk->blkcg_css) |
1236 | css_put(async_chunk->blkcg_css); | |
97db1204 NB |
1237 | /* |
1238 | * Since the pointer to 'pending' is at the beginning of the array of | |
b5326271 | 1239 | * async_chunk's, freeing it ensures the whole array has been freed. |
97db1204 | 1240 | */ |
b5326271 | 1241 | if (atomic_dec_and_test(async_chunk->pending)) |
b1c16ac9 | 1242 | kvfree(async_chunk->pending); |
771ed689 CM |
1243 | } |
1244 | ||
751b6431 | 1245 | static int cow_file_range_async(struct btrfs_inode *inode, |
ec39f769 CM |
1246 | struct writeback_control *wbc, |
1247 | struct page *locked_page, | |
771ed689 | 1248 | u64 start, u64 end, int *page_started, |
fac07d2b | 1249 | unsigned long *nr_written) |
771ed689 | 1250 | { |
751b6431 | 1251 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
ec39f769 | 1252 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1253 | struct async_cow *ctx; |
1254 | struct async_chunk *async_chunk; | |
771ed689 CM |
1255 | unsigned long nr_pages; |
1256 | u64 cur_end; | |
97db1204 NB |
1257 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1258 | int i; | |
1259 | bool should_compress; | |
b1c16ac9 | 1260 | unsigned nofs_flag; |
fac07d2b | 1261 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1262 | |
751b6431 | 1263 | unlock_extent(&inode->io_tree, start, end); |
97db1204 | 1264 | |
751b6431 | 1265 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
97db1204 NB |
1266 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
1267 | num_chunks = 1; | |
1268 | should_compress = false; | |
1269 | } else { | |
1270 | should_compress = true; | |
1271 | } | |
1272 | ||
b1c16ac9 NB |
1273 | nofs_flag = memalloc_nofs_save(); |
1274 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1275 | memalloc_nofs_restore(nofs_flag); | |
1276 | ||
97db1204 NB |
1277 | if (!ctx) { |
1278 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1279 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1280 | EXTENT_DO_ACCOUNTING; | |
1281 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1282 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | | |
1283 | PAGE_SET_ERROR; | |
1284 | ||
751b6431 NB |
1285 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1286 | clear_bits, page_ops); | |
97db1204 NB |
1287 | return -ENOMEM; |
1288 | } | |
1289 | ||
1290 | async_chunk = ctx->chunks; | |
1291 | atomic_set(&ctx->num_chunks, num_chunks); | |
1292 | ||
1293 | for (i = 0; i < num_chunks; i++) { | |
1294 | if (should_compress) | |
1295 | cur_end = min(end, start + SZ_512K - 1); | |
1296 | else | |
1297 | cur_end = end; | |
771ed689 | 1298 | |
bd4691a0 NB |
1299 | /* |
1300 | * igrab is called higher up in the call chain, take only the | |
1301 | * lightweight reference for the callback lifetime | |
1302 | */ | |
751b6431 | 1303 | ihold(&inode->vfs_inode); |
97db1204 | 1304 | async_chunk[i].pending = &ctx->num_chunks; |
751b6431 | 1305 | async_chunk[i].inode = &inode->vfs_inode; |
97db1204 NB |
1306 | async_chunk[i].start = start; |
1307 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1308 | async_chunk[i].write_flags = write_flags; |
1309 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1310 | ||
1d53c9e6 CM |
1311 | /* |
1312 | * The locked_page comes all the way from writepage and its | |
1313 | * the original page we were actually given. As we spread | |
1314 | * this large delalloc region across multiple async_chunk | |
1315 | * structs, only the first struct needs a pointer to locked_page | |
1316 | * | |
1317 | * This way we don't need racey decisions about who is supposed | |
1318 | * to unlock it. | |
1319 | */ | |
1320 | if (locked_page) { | |
ec39f769 CM |
1321 | /* |
1322 | * Depending on the compressibility, the pages might or | |
1323 | * might not go through async. We want all of them to | |
1324 | * be accounted against wbc once. Let's do it here | |
1325 | * before the paths diverge. wbc accounting is used | |
1326 | * only for foreign writeback detection and doesn't | |
1327 | * need full accuracy. Just account the whole thing | |
1328 | * against the first page. | |
1329 | */ | |
1330 | wbc_account_cgroup_owner(wbc, locked_page, | |
1331 | cur_end - start); | |
1d53c9e6 CM |
1332 | async_chunk[i].locked_page = locked_page; |
1333 | locked_page = NULL; | |
1334 | } else { | |
1335 | async_chunk[i].locked_page = NULL; | |
1336 | } | |
1337 | ||
ec39f769 CM |
1338 | if (blkcg_css != blkcg_root_css) { |
1339 | css_get(blkcg_css); | |
1340 | async_chunk[i].blkcg_css = blkcg_css; | |
1341 | } else { | |
1342 | async_chunk[i].blkcg_css = NULL; | |
1343 | } | |
1344 | ||
a0cac0ec OS |
1345 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1346 | async_cow_submit, async_cow_free); | |
771ed689 | 1347 | |
97db1204 | 1348 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1349 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1350 | |
97db1204 | 1351 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1352 | |
771ed689 CM |
1353 | *nr_written += nr_pages; |
1354 | start = cur_end + 1; | |
1355 | } | |
1356 | *page_started = 1; | |
1357 | return 0; | |
be20aa9d CM |
1358 | } |
1359 | ||
2ff7e61e | 1360 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1361 | u64 bytenr, u64 num_bytes) |
1362 | { | |
1363 | int ret; | |
1364 | struct btrfs_ordered_sum *sums; | |
1365 | LIST_HEAD(list); | |
1366 | ||
0b246afa | 1367 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1368 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1369 | if (ret == 0 && list_empty(&list)) |
1370 | return 0; | |
1371 | ||
1372 | while (!list_empty(&list)) { | |
1373 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1374 | list_del(&sums->list); | |
1375 | kfree(sums); | |
1376 | } | |
58113753 LB |
1377 | if (ret < 0) |
1378 | return ret; | |
17d217fe YZ |
1379 | return 1; |
1380 | } | |
1381 | ||
8ba96f3d | 1382 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
467dc47e FM |
1383 | const u64 start, const u64 end, |
1384 | int *page_started, unsigned long *nr_written) | |
1385 | { | |
8ba96f3d NB |
1386 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
1387 | const bool is_reloc_ino = (inode->root->root_key.objectid == | |
6bd335b4 | 1388 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
2166e5ed | 1389 | const u64 range_bytes = end + 1 - start; |
8ba96f3d | 1390 | struct extent_io_tree *io_tree = &inode->io_tree; |
467dc47e FM |
1391 | u64 range_start = start; |
1392 | u64 count; | |
1393 | ||
1394 | /* | |
1395 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1396 | * made we had not enough available data space and therefore we did not | |
1397 | * reserve data space for it, since we though we could do NOCOW for the | |
1398 | * respective file range (either there is prealloc extent or the inode | |
1399 | * has the NOCOW bit set). | |
1400 | * | |
1401 | * However when we need to fallback to COW mode (because for example the | |
1402 | * block group for the corresponding extent was turned to RO mode by a | |
1403 | * scrub or relocation) we need to do the following: | |
1404 | * | |
1405 | * 1) We increment the bytes_may_use counter of the data space info. | |
1406 | * If COW succeeds, it allocates a new data extent and after doing | |
1407 | * that it decrements the space info's bytes_may_use counter and | |
1408 | * increments its bytes_reserved counter by the same amount (we do | |
1409 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1410 | * bytes_may_use counter to compensate (when space is reserved at | |
1411 | * buffered write time, the bytes_may_use counter is incremented); | |
1412 | * | |
1413 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1414 | * that if the COW path fails for any reason, it decrements (through | |
1415 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1416 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1417 | * |
1418 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1419 | * space cache inode or an inode of the data relocation tree, we must |
1420 | * also increment bytes_may_use of the data space_info for the same | |
1421 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1422 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1423 | * group that contains that extent to RO mode and therefore force COW |
1424 | * when starting writeback. | |
467dc47e | 1425 | */ |
2166e5ed | 1426 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
467dc47e | 1427 | EXTENT_NORESERVE, 0); |
6bd335b4 FM |
1428 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1429 | u64 bytes = count; | |
8ba96f3d | 1430 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
467dc47e FM |
1431 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
1432 | ||
6bd335b4 FM |
1433 | if (is_space_ino || is_reloc_ino) |
1434 | bytes = range_bytes; | |
1435 | ||
467dc47e | 1436 | spin_lock(&sinfo->lock); |
2166e5ed | 1437 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1438 | spin_unlock(&sinfo->lock); |
1439 | ||
2166e5ed FM |
1440 | if (count > 0) |
1441 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
1442 | 0, 0, NULL); | |
467dc47e FM |
1443 | } |
1444 | ||
8ba96f3d NB |
1445 | return cow_file_range(inode, locked_page, start, end, page_started, |
1446 | nr_written, 1); | |
467dc47e FM |
1447 | } |
1448 | ||
d352ac68 CM |
1449 | /* |
1450 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1451 | * of the extents that exist in the file, and COWs the file as required. | |
1452 | * | |
1453 | * If no cow copies or snapshots exist, we write directly to the existing | |
1454 | * blocks on disk | |
1455 | */ | |
968322c8 | 1456 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
7f366cfe | 1457 | struct page *locked_page, |
3e024846 NB |
1458 | const u64 start, const u64 end, |
1459 | int *page_started, int force, | |
1460 | unsigned long *nr_written) | |
be20aa9d | 1461 | { |
968322c8 NB |
1462 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1463 | struct btrfs_root *root = inode->root; | |
be20aa9d | 1464 | struct btrfs_path *path; |
3e024846 NB |
1465 | u64 cow_start = (u64)-1; |
1466 | u64 cur_offset = start; | |
8ecebf4d | 1467 | int ret; |
3e024846 | 1468 | bool check_prev = true; |
968322c8 NB |
1469 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
1470 | u64 ino = btrfs_ino(inode); | |
762bf098 NB |
1471 | bool nocow = false; |
1472 | u64 disk_bytenr = 0; | |
be20aa9d CM |
1473 | |
1474 | path = btrfs_alloc_path(); | |
17ca04af | 1475 | if (!path) { |
968322c8 | 1476 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
c2790a2e | 1477 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1478 | EXTENT_DO_ACCOUNTING | |
1479 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1480 | PAGE_CLEAR_DIRTY | |
1481 | PAGE_SET_WRITEBACK | | |
1482 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1483 | return -ENOMEM; |
17ca04af | 1484 | } |
82d5902d | 1485 | |
80ff3856 | 1486 | while (1) { |
3e024846 NB |
1487 | struct btrfs_key found_key; |
1488 | struct btrfs_file_extent_item *fi; | |
1489 | struct extent_buffer *leaf; | |
1490 | u64 extent_end; | |
1491 | u64 extent_offset; | |
3e024846 NB |
1492 | u64 num_bytes = 0; |
1493 | u64 disk_num_bytes; | |
3e024846 NB |
1494 | u64 ram_bytes; |
1495 | int extent_type; | |
762bf098 NB |
1496 | |
1497 | nocow = false; | |
3e024846 | 1498 | |
e4c3b2dc | 1499 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1500 | cur_offset, 0); |
d788a349 | 1501 | if (ret < 0) |
79787eaa | 1502 | goto error; |
a6bd9cd1 NB |
1503 | |
1504 | /* | |
1505 | * If there is no extent for our range when doing the initial | |
1506 | * search, then go back to the previous slot as it will be the | |
1507 | * one containing the search offset | |
1508 | */ | |
80ff3856 YZ |
1509 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1510 | leaf = path->nodes[0]; | |
1511 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1512 | path->slots[0] - 1); | |
33345d01 | 1513 | if (found_key.objectid == ino && |
80ff3856 YZ |
1514 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1515 | path->slots[0]--; | |
1516 | } | |
3e024846 | 1517 | check_prev = false; |
80ff3856 | 1518 | next_slot: |
a6bd9cd1 | 1519 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1520 | leaf = path->nodes[0]; |
1521 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1522 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1523 | if (ret < 0) { |
1524 | if (cow_start != (u64)-1) | |
1525 | cur_offset = cow_start; | |
79787eaa | 1526 | goto error; |
e8916699 | 1527 | } |
80ff3856 YZ |
1528 | if (ret > 0) |
1529 | break; | |
1530 | leaf = path->nodes[0]; | |
1531 | } | |
be20aa9d | 1532 | |
80ff3856 YZ |
1533 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1534 | ||
a6bd9cd1 | 1535 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
1536 | if (found_key.objectid > ino) |
1537 | break; | |
a6bd9cd1 NB |
1538 | /* |
1539 | * Keep searching until we find an EXTENT_ITEM or there are no | |
1540 | * more extents for this inode | |
1541 | */ | |
1d512cb7 FM |
1542 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
1543 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1544 | path->slots[0]++; | |
1545 | goto next_slot; | |
1546 | } | |
a6bd9cd1 NB |
1547 | |
1548 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 1549 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
1550 | found_key.offset > end) |
1551 | break; | |
1552 | ||
a6bd9cd1 NB |
1553 | /* |
1554 | * If the found extent starts after requested offset, then | |
1555 | * adjust extent_end to be right before this extent begins | |
1556 | */ | |
80ff3856 YZ |
1557 | if (found_key.offset > cur_offset) { |
1558 | extent_end = found_key.offset; | |
e9061e21 | 1559 | extent_type = 0; |
80ff3856 YZ |
1560 | goto out_check; |
1561 | } | |
1562 | ||
a6bd9cd1 NB |
1563 | /* |
1564 | * Found extent which begins before our range and potentially | |
1565 | * intersect it | |
1566 | */ | |
80ff3856 YZ |
1567 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1568 | struct btrfs_file_extent_item); | |
1569 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1570 | ||
cc95bef6 | 1571 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1572 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1573 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1574 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1575 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1576 | extent_end = found_key.offset + |
1577 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1578 | disk_num_bytes = |
1579 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
a6bd9cd1 | 1580 | /* |
de7999af FM |
1581 | * If the extent we got ends before our current offset, |
1582 | * skip to the next extent. | |
a6bd9cd1 | 1583 | */ |
de7999af | 1584 | if (extent_end <= cur_offset) { |
80ff3856 YZ |
1585 | path->slots[0]++; |
1586 | goto next_slot; | |
1587 | } | |
a6bd9cd1 | 1588 | /* Skip holes */ |
17d217fe YZ |
1589 | if (disk_bytenr == 0) |
1590 | goto out_check; | |
a6bd9cd1 | 1591 | /* Skip compressed/encrypted/encoded extents */ |
80ff3856 YZ |
1592 | if (btrfs_file_extent_compression(leaf, fi) || |
1593 | btrfs_file_extent_encryption(leaf, fi) || | |
1594 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1595 | goto out_check; | |
78d4295b | 1596 | /* |
a6bd9cd1 NB |
1597 | * If extent is created before the last volume's snapshot |
1598 | * this implies the extent is shared, hence we can't do | |
1599 | * nocow. This is the same check as in | |
1600 | * btrfs_cross_ref_exist but without calling | |
1601 | * btrfs_search_slot. | |
78d4295b | 1602 | */ |
3e024846 | 1603 | if (!freespace_inode && |
27a7ff55 | 1604 | btrfs_file_extent_generation(leaf, fi) <= |
78d4295b EL |
1605 | btrfs_root_last_snapshot(&root->root_item)) |
1606 | goto out_check; | |
d899e052 YZ |
1607 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1608 | goto out_check; | |
a6bd9cd1 | 1609 | /* If extent is RO, we must COW it */ |
2ff7e61e | 1610 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1611 | goto out_check; |
58113753 LB |
1612 | ret = btrfs_cross_ref_exist(root, ino, |
1613 | found_key.offset - | |
a84d5d42 | 1614 | extent_offset, disk_bytenr, false); |
58113753 LB |
1615 | if (ret) { |
1616 | /* | |
1617 | * ret could be -EIO if the above fails to read | |
1618 | * metadata. | |
1619 | */ | |
1620 | if (ret < 0) { | |
1621 | if (cow_start != (u64)-1) | |
1622 | cur_offset = cow_start; | |
1623 | goto error; | |
1624 | } | |
1625 | ||
3e024846 | 1626 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1627 | goto out_check; |
58113753 | 1628 | } |
5d4f98a2 | 1629 | disk_bytenr += extent_offset; |
17d217fe YZ |
1630 | disk_bytenr += cur_offset - found_key.offset; |
1631 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 | 1632 | /* |
a6bd9cd1 NB |
1633 | * If there are pending snapshots for this root, we |
1634 | * fall into common COW way | |
e9894fd3 | 1635 | */ |
3e024846 | 1636 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
8ecebf4d | 1637 | goto out_check; |
17d217fe YZ |
1638 | /* |
1639 | * force cow if csum exists in the range. | |
1640 | * this ensure that csum for a given extent are | |
1641 | * either valid or do not exist. | |
1642 | */ | |
58113753 LB |
1643 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1644 | num_bytes); | |
1645 | if (ret) { | |
58113753 LB |
1646 | /* |
1647 | * ret could be -EIO if the above fails to read | |
1648 | * metadata. | |
1649 | */ | |
1650 | if (ret < 0) { | |
1651 | if (cow_start != (u64)-1) | |
1652 | cur_offset = cow_start; | |
1653 | goto error; | |
1654 | } | |
3e024846 | 1655 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1656 | goto out_check; |
91e1f56a | 1657 | } |
8ecebf4d | 1658 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1659 | goto out_check; |
3e024846 | 1660 | nocow = true; |
80ff3856 | 1661 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e8e21007 NB |
1662 | extent_end = found_key.offset + ram_bytes; |
1663 | extent_end = ALIGN(extent_end, fs_info->sectorsize); | |
922f0518 NB |
1664 | /* Skip extents outside of our requested range */ |
1665 | if (extent_end <= start) { | |
1666 | path->slots[0]++; | |
1667 | goto next_slot; | |
1668 | } | |
80ff3856 | 1669 | } else { |
e8e21007 | 1670 | /* If this triggers then we have a memory corruption */ |
290342f6 | 1671 | BUG(); |
80ff3856 YZ |
1672 | } |
1673 | out_check: | |
a6bd9cd1 NB |
1674 | /* |
1675 | * If nocow is false then record the beginning of the range | |
1676 | * that needs to be COWed | |
1677 | */ | |
80ff3856 YZ |
1678 | if (!nocow) { |
1679 | if (cow_start == (u64)-1) | |
1680 | cow_start = cur_offset; | |
1681 | cur_offset = extent_end; | |
1682 | if (cur_offset > end) | |
1683 | break; | |
1684 | path->slots[0]++; | |
1685 | goto next_slot; | |
7ea394f1 YZ |
1686 | } |
1687 | ||
b3b4aa74 | 1688 | btrfs_release_path(path); |
a6bd9cd1 NB |
1689 | |
1690 | /* | |
1691 | * COW range from cow_start to found_key.offset - 1. As the key | |
1692 | * will contain the beginning of the first extent that can be | |
1693 | * NOCOW, following one which needs to be COW'ed | |
1694 | */ | |
80ff3856 | 1695 | if (cow_start != (u64)-1) { |
968322c8 | 1696 | ret = fallback_to_cow(inode, locked_page, |
8ba96f3d | 1697 | cow_start, found_key.offset - 1, |
467dc47e | 1698 | page_started, nr_written); |
230ed397 | 1699 | if (ret) |
79787eaa | 1700 | goto error; |
80ff3856 | 1701 | cow_start = (u64)-1; |
7ea394f1 | 1702 | } |
80ff3856 | 1703 | |
d899e052 | 1704 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db | 1705 | u64 orig_start = found_key.offset - extent_offset; |
3e024846 | 1706 | struct extent_map *em; |
6f9994db | 1707 | |
968322c8 | 1708 | em = create_io_em(inode, cur_offset, num_bytes, |
6f9994db LB |
1709 | orig_start, |
1710 | disk_bytenr, /* block_start */ | |
1711 | num_bytes, /* block_len */ | |
1712 | disk_num_bytes, /* orig_block_len */ | |
1713 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1714 | BTRFS_ORDERED_PREALLOC); | |
1715 | if (IS_ERR(em)) { | |
6f9994db LB |
1716 | ret = PTR_ERR(em); |
1717 | goto error; | |
d899e052 | 1718 | } |
6f9994db | 1719 | free_extent_map(em); |
968322c8 | 1720 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1721 | disk_bytenr, num_bytes, |
1722 | num_bytes, | |
1723 | BTRFS_ORDERED_PREALLOC); | |
762bf098 | 1724 | if (ret) { |
968322c8 | 1725 | btrfs_drop_extent_cache(inode, cur_offset, |
762bf098 NB |
1726 | cur_offset + num_bytes - 1, |
1727 | 0); | |
1728 | goto error; | |
1729 | } | |
d899e052 | 1730 | } else { |
968322c8 | 1731 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
bb55f626 NB |
1732 | disk_bytenr, num_bytes, |
1733 | num_bytes, | |
1734 | BTRFS_ORDERED_NOCOW); | |
762bf098 NB |
1735 | if (ret) |
1736 | goto error; | |
d899e052 | 1737 | } |
80ff3856 | 1738 | |
f78c436c | 1739 | if (nocow) |
0b246afa | 1740 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
762bf098 | 1741 | nocow = false; |
771ed689 | 1742 | |
efa56464 | 1743 | if (root->root_key.objectid == |
4dbd80fb QW |
1744 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1745 | /* | |
1746 | * Error handled later, as we must prevent | |
1747 | * extent_clear_unlock_delalloc() in error handler | |
1748 | * from freeing metadata of created ordered extent. | |
1749 | */ | |
968322c8 | 1750 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
efa56464 | 1751 | num_bytes); |
efa56464 | 1752 | |
968322c8 | 1753 | extent_clear_unlock_delalloc(inode, cur_offset, |
74e9194a | 1754 | cur_offset + num_bytes - 1, |
c2790a2e | 1755 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1756 | EXTENT_DELALLOC | |
1757 | EXTENT_CLEAR_DATA_RESV, | |
1758 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1759 | ||
80ff3856 | 1760 | cur_offset = extent_end; |
4dbd80fb QW |
1761 | |
1762 | /* | |
1763 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1764 | * handler, as metadata for created ordered extent will only | |
1765 | * be freed by btrfs_finish_ordered_io(). | |
1766 | */ | |
1767 | if (ret) | |
1768 | goto error; | |
80ff3856 YZ |
1769 | if (cur_offset > end) |
1770 | break; | |
be20aa9d | 1771 | } |
b3b4aa74 | 1772 | btrfs_release_path(path); |
80ff3856 | 1773 | |
506481b2 | 1774 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1775 | cow_start = cur_offset; |
17ca04af | 1776 | |
80ff3856 | 1777 | if (cow_start != (u64)-1) { |
506481b2 | 1778 | cur_offset = end; |
968322c8 NB |
1779 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
1780 | page_started, nr_written); | |
d788a349 | 1781 | if (ret) |
79787eaa | 1782 | goto error; |
80ff3856 YZ |
1783 | } |
1784 | ||
79787eaa | 1785 | error: |
762bf098 NB |
1786 | if (nocow) |
1787 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1788 | ||
17ca04af | 1789 | if (ret && cur_offset < end) |
968322c8 | 1790 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 1791 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1792 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1793 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1794 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1795 | PAGE_SET_WRITEBACK | |
1796 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1797 | btrfs_free_path(path); |
79787eaa | 1798 | return ret; |
be20aa9d CM |
1799 | } |
1800 | ||
0c494225 | 1801 | static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end) |
47059d93 WS |
1802 | { |
1803 | ||
0c494225 NB |
1804 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
1805 | !(inode->flags & BTRFS_INODE_PREALLOC)) | |
47059d93 WS |
1806 | return 0; |
1807 | ||
1808 | /* | |
1809 | * @defrag_bytes is a hint value, no spinlock held here, | |
1810 | * if is not zero, it means the file is defragging. | |
1811 | * Force cow if given extent needs to be defragged. | |
1812 | */ | |
0c494225 NB |
1813 | if (inode->defrag_bytes && |
1814 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) | |
47059d93 WS |
1815 | return 1; |
1816 | ||
1817 | return 0; | |
1818 | } | |
1819 | ||
d352ac68 | 1820 | /* |
5eaad97a NB |
1821 | * Function to process delayed allocation (create CoW) for ranges which are |
1822 | * being touched for the first time. | |
d352ac68 | 1823 | */ |
98456b9c | 1824 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
5eaad97a NB |
1825 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1826 | struct writeback_control *wbc) | |
be20aa9d | 1827 | { |
be20aa9d | 1828 | int ret; |
98456b9c | 1829 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1830 | |
98456b9c NB |
1831 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
1832 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
d397712b | 1833 | page_started, 1, nr_written); |
98456b9c NB |
1834 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
1835 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
d397712b | 1836 | page_started, 0, nr_written); |
98456b9c NB |
1837 | } else if (!inode_can_compress(inode) || |
1838 | !inode_need_compress(inode, start, end)) { | |
1839 | ret = cow_file_range(inode, locked_page, start, end, | |
1840 | page_started, nr_written, 1); | |
7ddf5a42 | 1841 | } else { |
98456b9c NB |
1842 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
1843 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, | |
fac07d2b | 1844 | page_started, nr_written); |
7ddf5a42 | 1845 | } |
52427260 | 1846 | if (ret) |
98456b9c | 1847 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
d1051d6e | 1848 | end - start + 1); |
b888db2b CM |
1849 | return ret; |
1850 | } | |
1851 | ||
abbb55f4 NB |
1852 | void btrfs_split_delalloc_extent(struct inode *inode, |
1853 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1854 | { |
dcab6a3b JB |
1855 | u64 size; |
1856 | ||
0ca1f7ce | 1857 | /* not delalloc, ignore it */ |
9ed74f2d | 1858 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1859 | return; |
9ed74f2d | 1860 | |
dcab6a3b JB |
1861 | size = orig->end - orig->start + 1; |
1862 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1863 | u32 num_extents; |
dcab6a3b JB |
1864 | u64 new_size; |
1865 | ||
1866 | /* | |
5c848198 | 1867 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 1868 | * applies here, just in reverse. |
dcab6a3b JB |
1869 | */ |
1870 | new_size = orig->end - split + 1; | |
823bb20a | 1871 | num_extents = count_max_extents(new_size); |
ba117213 | 1872 | new_size = split - orig->start; |
823bb20a DS |
1873 | num_extents += count_max_extents(new_size); |
1874 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1875 | return; |
1876 | } | |
1877 | ||
9e0baf60 | 1878 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1879 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1880 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1881 | } |
1882 | ||
1883 | /* | |
5c848198 NB |
1884 | * Handle merged delayed allocation extents so we can keep track of new extents |
1885 | * that are just merged onto old extents, such as when we are doing sequential | |
1886 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 1887 | */ |
5c848198 NB |
1888 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
1889 | struct extent_state *other) | |
9ed74f2d | 1890 | { |
dcab6a3b | 1891 | u64 new_size, old_size; |
823bb20a | 1892 | u32 num_extents; |
dcab6a3b | 1893 | |
9ed74f2d JB |
1894 | /* not delalloc, ignore it */ |
1895 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1896 | return; |
9ed74f2d | 1897 | |
8461a3de JB |
1898 | if (new->start > other->start) |
1899 | new_size = new->end - other->start + 1; | |
1900 | else | |
1901 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1902 | |
1903 | /* we're not bigger than the max, unreserve the space and go */ | |
1904 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1905 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1906 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1907 | spin_unlock(&BTRFS_I(inode)->lock); |
1908 | return; | |
1909 | } | |
1910 | ||
1911 | /* | |
ba117213 JB |
1912 | * We have to add up either side to figure out how many extents were |
1913 | * accounted for before we merged into one big extent. If the number of | |
1914 | * extents we accounted for is <= the amount we need for the new range | |
1915 | * then we can return, otherwise drop. Think of it like this | |
1916 | * | |
1917 | * [ 4k][MAX_SIZE] | |
1918 | * | |
1919 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1920 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1921 | * we have 1 so they are == and we can return. But in this case | |
1922 | * | |
1923 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1924 | * | |
1925 | * Each range on their own accounts for 2 extents, but merged together | |
1926 | * they are only 3 extents worth of accounting, so we need to drop in | |
1927 | * this case. | |
dcab6a3b | 1928 | */ |
ba117213 | 1929 | old_size = other->end - other->start + 1; |
823bb20a | 1930 | num_extents = count_max_extents(old_size); |
ba117213 | 1931 | old_size = new->end - new->start + 1; |
823bb20a DS |
1932 | num_extents += count_max_extents(old_size); |
1933 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1934 | return; |
1935 | ||
9e0baf60 | 1936 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1937 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1938 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1939 | } |
1940 | ||
eb73c1b7 MX |
1941 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1942 | struct inode *inode) | |
1943 | { | |
0b246afa JM |
1944 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1945 | ||
eb73c1b7 MX |
1946 | spin_lock(&root->delalloc_lock); |
1947 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1948 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1949 | &root->delalloc_inodes); | |
1950 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1951 | &BTRFS_I(inode)->runtime_flags); | |
1952 | root->nr_delalloc_inodes++; | |
1953 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1954 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1955 | BUG_ON(!list_empty(&root->delalloc_root)); |
1956 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1957 | &fs_info->delalloc_roots); |
1958 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1959 | } |
1960 | } | |
1961 | spin_unlock(&root->delalloc_lock); | |
1962 | } | |
1963 | ||
2b877331 NB |
1964 | |
1965 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1966 | struct btrfs_inode *inode) | |
eb73c1b7 | 1967 | { |
3ffbd68c | 1968 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 1969 | |
9e3e97f4 NB |
1970 | if (!list_empty(&inode->delalloc_inodes)) { |
1971 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1972 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1973 | &inode->runtime_flags); |
eb73c1b7 MX |
1974 | root->nr_delalloc_inodes--; |
1975 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 1976 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 1977 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1978 | BUG_ON(list_empty(&root->delalloc_root)); |
1979 | list_del_init(&root->delalloc_root); | |
0b246afa | 1980 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1981 | } |
1982 | } | |
2b877331 NB |
1983 | } |
1984 | ||
1985 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1986 | struct btrfs_inode *inode) | |
1987 | { | |
1988 | spin_lock(&root->delalloc_lock); | |
1989 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
1990 | spin_unlock(&root->delalloc_lock); |
1991 | } | |
1992 | ||
d352ac68 | 1993 | /* |
e06a1fc9 NB |
1994 | * Properly track delayed allocation bytes in the inode and to maintain the |
1995 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 1996 | */ |
e06a1fc9 NB |
1997 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
1998 | unsigned *bits) | |
291d673e | 1999 | { |
0b246afa JM |
2000 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2001 | ||
47059d93 WS |
2002 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
2003 | WARN_ON(1); | |
75eff68e CM |
2004 | /* |
2005 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2006 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2007 | * bit, which is only set or cleared with irqs on |
2008 | */ | |
0ca1f7ce | 2009 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 2010 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2011 | u64 len = state->end + 1 - state->start; |
8b62f87b | 2012 | u32 num_extents = count_max_extents(len); |
70ddc553 | 2013 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 2014 | |
8b62f87b JB |
2015 | spin_lock(&BTRFS_I(inode)->lock); |
2016 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
2017 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 2018 | |
6a3891c5 | 2019 | /* For sanity tests */ |
0b246afa | 2020 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2021 | return; |
2022 | ||
104b4e51 NB |
2023 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2024 | fs_info->delalloc_batch); | |
df0af1a5 | 2025 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 2026 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
2027 | if (*bits & EXTENT_DEFRAG) |
2028 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 2029 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
2030 | &BTRFS_I(inode)->runtime_flags)) |
2031 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 2032 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 2033 | } |
a7e3b975 FM |
2034 | |
2035 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
2036 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2037 | spin_lock(&BTRFS_I(inode)->lock); | |
2038 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
2039 | state->start; | |
2040 | spin_unlock(&BTRFS_I(inode)->lock); | |
2041 | } | |
291d673e CM |
2042 | } |
2043 | ||
d352ac68 | 2044 | /* |
a36bb5f9 NB |
2045 | * Once a range is no longer delalloc this function ensures that proper |
2046 | * accounting happens. | |
d352ac68 | 2047 | */ |
a36bb5f9 NB |
2048 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
2049 | struct extent_state *state, unsigned *bits) | |
291d673e | 2050 | { |
a36bb5f9 NB |
2051 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
2052 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 2053 | u64 len = state->end + 1 - state->start; |
823bb20a | 2054 | u32 num_extents = count_max_extents(len); |
47059d93 | 2055 | |
4a4b964f FM |
2056 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
2057 | spin_lock(&inode->lock); | |
6fc0ef68 | 2058 | inode->defrag_bytes -= len; |
4a4b964f FM |
2059 | spin_unlock(&inode->lock); |
2060 | } | |
47059d93 | 2061 | |
75eff68e CM |
2062 | /* |
2063 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2064 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2065 | * bit, which is only set or cleared with irqs on |
2066 | */ | |
0ca1f7ce | 2067 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2068 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2069 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2070 | |
8b62f87b JB |
2071 | spin_lock(&inode->lock); |
2072 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2073 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2074 | |
b6d08f06 JB |
2075 | /* |
2076 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2077 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2078 | * error. |
2079 | */ | |
a315e68f | 2080 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2081 | root != fs_info->tree_root) |
43b18595 | 2082 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2083 | |
6a3891c5 | 2084 | /* For sanity tests. */ |
0b246afa | 2085 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2086 | return; |
2087 | ||
a315e68f FM |
2088 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
2089 | do_list && !(state->state & EXTENT_NORESERVE) && | |
2090 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
9db5d510 | 2091 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
9ed74f2d | 2092 | |
104b4e51 NB |
2093 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2094 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2095 | spin_lock(&inode->lock); |
2096 | inode->delalloc_bytes -= len; | |
2097 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2098 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2099 | &inode->runtime_flags)) |
eb73c1b7 | 2100 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2101 | spin_unlock(&inode->lock); |
291d673e | 2102 | } |
a7e3b975 FM |
2103 | |
2104 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
2105 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2106 | spin_lock(&inode->lock); | |
2107 | ASSERT(inode->new_delalloc_bytes >= len); | |
2108 | inode->new_delalloc_bytes -= len; | |
2109 | spin_unlock(&inode->lock); | |
2110 | } | |
291d673e CM |
2111 | } |
2112 | ||
d352ac68 | 2113 | /* |
da12fe54 NB |
2114 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
2115 | * in a chunk's stripe. This function ensures that bios do not span a | |
2116 | * stripe/chunk | |
6f034ece | 2117 | * |
da12fe54 NB |
2118 | * @page - The page we are about to add to the bio |
2119 | * @size - size we want to add to the bio | |
2120 | * @bio - bio we want to ensure is smaller than a stripe | |
2121 | * @bio_flags - flags of the bio | |
2122 | * | |
2123 | * return 1 if page cannot be added to the bio | |
2124 | * return 0 if page can be added to the bio | |
6f034ece | 2125 | * return error otherwise |
d352ac68 | 2126 | */ |
da12fe54 NB |
2127 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
2128 | unsigned long bio_flags) | |
239b14b3 | 2129 | { |
0b246afa JM |
2130 | struct inode *inode = page->mapping->host; |
2131 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 2132 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
2133 | u64 length = 0; |
2134 | u64 map_length; | |
239b14b3 | 2135 | int ret; |
89b798ad | 2136 | struct btrfs_io_geometry geom; |
239b14b3 | 2137 | |
771ed689 CM |
2138 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
2139 | return 0; | |
2140 | ||
4f024f37 | 2141 | length = bio->bi_iter.bi_size; |
239b14b3 | 2142 | map_length = length; |
89b798ad NB |
2143 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
2144 | &geom); | |
6f034ece LB |
2145 | if (ret < 0) |
2146 | return ret; | |
89b798ad NB |
2147 | |
2148 | if (geom.len < length + size) | |
239b14b3 | 2149 | return 1; |
3444a972 | 2150 | return 0; |
239b14b3 CM |
2151 | } |
2152 | ||
d352ac68 CM |
2153 | /* |
2154 | * in order to insert checksums into the metadata in large chunks, | |
2155 | * we wait until bio submission time. All the pages in the bio are | |
2156 | * checksummed and sums are attached onto the ordered extent record. | |
2157 | * | |
2158 | * At IO completion time the cums attached on the ordered extent record | |
2159 | * are inserted into the btree | |
2160 | */ | |
d0ee3934 | 2161 | static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, |
eaf25d93 | 2162 | u64 bio_offset) |
065631f6 | 2163 | { |
c6100a4b | 2164 | struct inode *inode = private_data; |
e015640f | 2165 | |
c965d640 | 2166 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
4a69a410 | 2167 | } |
e015640f | 2168 | |
d352ac68 | 2169 | /* |
cad321ad | 2170 | * extent_io.c submission hook. This does the right thing for csum calculation |
4c274bc6 LB |
2171 | * on write, or reading the csums from the tree before a read. |
2172 | * | |
2173 | * Rules about async/sync submit, | |
2174 | * a) read: sync submit | |
2175 | * | |
2176 | * b) write without checksum: sync submit | |
2177 | * | |
2178 | * c) write with checksum: | |
2179 | * c-1) if bio is issued by fsync: sync submit | |
2180 | * (sync_writers != 0) | |
2181 | * | |
2182 | * c-2) if root is reloc root: sync submit | |
2183 | * (only in case of buffered IO) | |
2184 | * | |
2185 | * c-3) otherwise: async submit | |
d352ac68 | 2186 | */ |
a56b1c7b | 2187 | static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
50489a57 NB |
2188 | int mirror_num, |
2189 | unsigned long bio_flags) | |
2190 | ||
44b8bd7e | 2191 | { |
0b246afa | 2192 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 2193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 2194 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 2195 | blk_status_t ret = 0; |
19b9bdb0 | 2196 | int skip_sum; |
b812ce28 | 2197 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 2198 | |
6cbff00f | 2199 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 2200 | |
70ddc553 | 2201 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 2202 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 2203 | |
37226b21 | 2204 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 2205 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 2206 | if (ret) |
61891923 | 2207 | goto out; |
5fd02043 | 2208 | |
d20f7043 | 2209 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
2210 | ret = btrfs_submit_compressed_read(inode, bio, |
2211 | mirror_num, | |
2212 | bio_flags); | |
2213 | goto out; | |
c2db1073 | 2214 | } else if (!skip_sum) { |
db72e47f | 2215 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
c2db1073 | 2216 | if (ret) |
61891923 | 2217 | goto out; |
c2db1073 | 2218 | } |
4d1b5fb4 | 2219 | goto mapit; |
b812ce28 | 2220 | } else if (async && !skip_sum) { |
17d217fe YZ |
2221 | /* csum items have already been cloned */ |
2222 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
2223 | goto mapit; | |
19b9bdb0 | 2224 | /* we're doing a write, do the async checksumming */ |
c6100a4b | 2225 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
e7681167 | 2226 | 0, inode, btrfs_submit_bio_start); |
61891923 | 2227 | goto out; |
b812ce28 | 2228 | } else if (!skip_sum) { |
bd242a08 | 2229 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
b812ce28 JB |
2230 | if (ret) |
2231 | goto out; | |
19b9bdb0 CM |
2232 | } |
2233 | ||
0b86a832 | 2234 | mapit: |
08635bae | 2235 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
61891923 SB |
2236 | |
2237 | out: | |
4e4cbee9 CH |
2238 | if (ret) { |
2239 | bio->bi_status = ret; | |
4246a0b6 CH |
2240 | bio_endio(bio); |
2241 | } | |
61891923 | 2242 | return ret; |
065631f6 | 2243 | } |
6885f308 | 2244 | |
d352ac68 CM |
2245 | /* |
2246 | * given a list of ordered sums record them in the inode. This happens | |
2247 | * at IO completion time based on sums calculated at bio submission time. | |
2248 | */ | |
ba1da2f4 | 2249 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2250 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2251 | { |
e6dcd2dc | 2252 | struct btrfs_ordered_sum *sum; |
ac01f26a | 2253 | int ret; |
e6dcd2dc | 2254 | |
c6e30871 | 2255 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2256 | trans->adding_csums = true; |
ac01f26a | 2257 | ret = btrfs_csum_file_blocks(trans, |
d20f7043 | 2258 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
7c2871a2 | 2259 | trans->adding_csums = false; |
ac01f26a NB |
2260 | if (ret) |
2261 | return ret; | |
e6dcd2dc CM |
2262 | } |
2263 | return 0; | |
2264 | } | |
2265 | ||
c2566f22 | 2266 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
e3b8a485 | 2267 | unsigned int extra_bits, |
330a5827 | 2268 | struct extent_state **cached_state) |
ea8c2819 | 2269 | { |
fdb1e121 | 2270 | WARN_ON(PAGE_ALIGNED(end)); |
c2566f22 NB |
2271 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
2272 | cached_state); | |
ea8c2819 CM |
2273 | } |
2274 | ||
d352ac68 | 2275 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2276 | struct btrfs_writepage_fixup { |
2277 | struct page *page; | |
f4b1363c | 2278 | struct inode *inode; |
247e743c CM |
2279 | struct btrfs_work work; |
2280 | }; | |
2281 | ||
b2950863 | 2282 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2283 | { |
2284 | struct btrfs_writepage_fixup *fixup; | |
2285 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2286 | struct extent_state *cached_state = NULL; |
364ecf36 | 2287 | struct extent_changeset *data_reserved = NULL; |
247e743c | 2288 | struct page *page; |
65d87f79 | 2289 | struct btrfs_inode *inode; |
247e743c CM |
2290 | u64 page_start; |
2291 | u64 page_end; | |
25f3c502 | 2292 | int ret = 0; |
f4b1363c | 2293 | bool free_delalloc_space = true; |
247e743c CM |
2294 | |
2295 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2296 | page = fixup->page; | |
65d87f79 | 2297 | inode = BTRFS_I(fixup->inode); |
f4b1363c JB |
2298 | page_start = page_offset(page); |
2299 | page_end = page_offset(page) + PAGE_SIZE - 1; | |
2300 | ||
2301 | /* | |
2302 | * This is similar to page_mkwrite, we need to reserve the space before | |
2303 | * we take the page lock. | |
2304 | */ | |
65d87f79 NB |
2305 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
2306 | PAGE_SIZE); | |
4a096752 | 2307 | again: |
247e743c | 2308 | lock_page(page); |
25f3c502 CM |
2309 | |
2310 | /* | |
2311 | * Before we queued this fixup, we took a reference on the page. | |
2312 | * page->mapping may go NULL, but it shouldn't be moved to a different | |
2313 | * address space. | |
2314 | */ | |
f4b1363c JB |
2315 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
2316 | /* | |
2317 | * Unfortunately this is a little tricky, either | |
2318 | * | |
2319 | * 1) We got here and our page had already been dealt with and | |
2320 | * we reserved our space, thus ret == 0, so we need to just | |
2321 | * drop our space reservation and bail. This can happen the | |
2322 | * first time we come into the fixup worker, or could happen | |
2323 | * while waiting for the ordered extent. | |
2324 | * 2) Our page was already dealt with, but we happened to get an | |
2325 | * ENOSPC above from the btrfs_delalloc_reserve_space. In | |
2326 | * this case we obviously don't have anything to release, but | |
2327 | * because the page was already dealt with we don't want to | |
2328 | * mark the page with an error, so make sure we're resetting | |
2329 | * ret to 0. This is why we have this check _before_ the ret | |
2330 | * check, because we do not want to have a surprise ENOSPC | |
2331 | * when the page was already properly dealt with. | |
2332 | */ | |
2333 | if (!ret) { | |
65d87f79 NB |
2334 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
2335 | btrfs_delalloc_release_space(inode, data_reserved, | |
f4b1363c JB |
2336 | page_start, PAGE_SIZE, |
2337 | true); | |
2338 | } | |
2339 | ret = 0; | |
247e743c | 2340 | goto out_page; |
f4b1363c | 2341 | } |
247e743c | 2342 | |
25f3c502 | 2343 | /* |
f4b1363c JB |
2344 | * We can't mess with the page state unless it is locked, so now that |
2345 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2346 | */ |
f4b1363c JB |
2347 | if (ret) |
2348 | goto out_page; | |
247e743c | 2349 | |
65d87f79 | 2350 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
4a096752 CM |
2351 | |
2352 | /* already ordered? We're done */ | |
8b62b72b | 2353 | if (PagePrivate2(page)) |
f4b1363c | 2354 | goto out_reserved; |
4a096752 | 2355 | |
65d87f79 | 2356 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
4a096752 | 2357 | if (ordered) { |
65d87f79 NB |
2358 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
2359 | &cached_state); | |
4a096752 | 2360 | unlock_page(page); |
65d87f79 | 2361 | btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1); |
87826df0 | 2362 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2363 | goto again; |
2364 | } | |
247e743c | 2365 | |
65d87f79 | 2366 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2367 | &cached_state); |
25f3c502 | 2368 | if (ret) |
53687007 | 2369 | goto out_reserved; |
f3038ee3 | 2370 | |
25f3c502 CM |
2371 | /* |
2372 | * Everything went as planned, we're now the owner of a dirty page with | |
2373 | * delayed allocation bits set and space reserved for our COW | |
2374 | * destination. | |
2375 | * | |
2376 | * The page was dirty when we started, nothing should have cleaned it. | |
2377 | */ | |
2378 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2379 | free_delalloc_space = false; |
53687007 | 2380 | out_reserved: |
65d87f79 | 2381 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
f4b1363c | 2382 | if (free_delalloc_space) |
65d87f79 NB |
2383 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2384 | PAGE_SIZE, true); | |
2385 | unlock_extent_cached(&inode->io_tree, page_start, page_end, | |
e43bbe5e | 2386 | &cached_state); |
247e743c | 2387 | out_page: |
25f3c502 CM |
2388 | if (ret) { |
2389 | /* | |
2390 | * We hit ENOSPC or other errors. Update the mapping and page | |
2391 | * to reflect the errors and clean the page. | |
2392 | */ | |
2393 | mapping_set_error(page->mapping, ret); | |
2394 | end_extent_writepage(page, ret, page_start, page_end); | |
2395 | clear_page_dirty_for_io(page); | |
2396 | SetPageError(page); | |
2397 | } | |
2398 | ClearPageChecked(page); | |
247e743c | 2399 | unlock_page(page); |
09cbfeaf | 2400 | put_page(page); |
b897abec | 2401 | kfree(fixup); |
364ecf36 | 2402 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2403 | /* |
2404 | * As a precaution, do a delayed iput in case it would be the last iput | |
2405 | * that could need flushing space. Recursing back to fixup worker would | |
2406 | * deadlock. | |
2407 | */ | |
65d87f79 | 2408 | btrfs_add_delayed_iput(&inode->vfs_inode); |
247e743c CM |
2409 | } |
2410 | ||
2411 | /* | |
2412 | * There are a few paths in the higher layers of the kernel that directly | |
2413 | * set the page dirty bit without asking the filesystem if it is a | |
2414 | * good idea. This causes problems because we want to make sure COW | |
2415 | * properly happens and the data=ordered rules are followed. | |
2416 | * | |
c8b97818 | 2417 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2418 | * hasn't been properly setup for IO. We kick off an async process |
2419 | * to fix it up. The async helper will wait for ordered extents, set | |
2420 | * the delalloc bit and make it safe to write the page. | |
2421 | */ | |
d75855b4 | 2422 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
247e743c CM |
2423 | { |
2424 | struct inode *inode = page->mapping->host; | |
0b246afa | 2425 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2426 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2427 | |
8b62b72b CM |
2428 | /* this page is properly in the ordered list */ |
2429 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2430 | return 0; |
2431 | ||
25f3c502 CM |
2432 | /* |
2433 | * PageChecked is set below when we create a fixup worker for this page, | |
2434 | * don't try to create another one if we're already PageChecked() | |
2435 | * | |
2436 | * The extent_io writepage code will redirty the page if we send back | |
2437 | * EAGAIN. | |
2438 | */ | |
247e743c CM |
2439 | if (PageChecked(page)) |
2440 | return -EAGAIN; | |
2441 | ||
2442 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2443 | if (!fixup) | |
2444 | return -EAGAIN; | |
f421950f | 2445 | |
f4b1363c JB |
2446 | /* |
2447 | * We are already holding a reference to this inode from | |
2448 | * write_cache_pages. We need to hold it because the space reservation | |
2449 | * takes place outside of the page lock, and we can't trust | |
2450 | * page->mapping outside of the page lock. | |
2451 | */ | |
2452 | ihold(inode); | |
247e743c | 2453 | SetPageChecked(page); |
09cbfeaf | 2454 | get_page(page); |
a0cac0ec | 2455 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 2456 | fixup->page = page; |
f4b1363c | 2457 | fixup->inode = inode; |
0b246afa | 2458 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2459 | |
2460 | return -EAGAIN; | |
247e743c CM |
2461 | } |
2462 | ||
d899e052 | 2463 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
c553f94d | 2464 | struct btrfs_inode *inode, u64 file_pos, |
9729f10a QW |
2465 | struct btrfs_file_extent_item *stack_fi, |
2466 | u64 qgroup_reserved) | |
d899e052 | 2467 | { |
c553f94d | 2468 | struct btrfs_root *root = inode->root; |
d899e052 YZ |
2469 | struct btrfs_path *path; |
2470 | struct extent_buffer *leaf; | |
2471 | struct btrfs_key ins; | |
203f44c5 QW |
2472 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2473 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
2474 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); | |
2475 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
1acae57b | 2476 | int extent_inserted = 0; |
d899e052 YZ |
2477 | int ret; |
2478 | ||
2479 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2480 | if (!path) |
2481 | return -ENOMEM; | |
d899e052 | 2482 | |
a1ed835e CM |
2483 | /* |
2484 | * we may be replacing one extent in the tree with another. | |
2485 | * The new extent is pinned in the extent map, and we don't want | |
2486 | * to drop it from the cache until it is completely in the btree. | |
2487 | * | |
2488 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2489 | * the caller is expected to unpin it and allow it to be merged | |
2490 | * with the others. | |
2491 | */ | |
c553f94d | 2492 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
1acae57b | 2493 | file_pos + num_bytes, NULL, 0, |
203f44c5 | 2494 | 1, sizeof(*stack_fi), &extent_inserted); |
79787eaa JM |
2495 | if (ret) |
2496 | goto out; | |
d899e052 | 2497 | |
1acae57b | 2498 | if (!extent_inserted) { |
c553f94d | 2499 | ins.objectid = btrfs_ino(inode); |
1acae57b FDBM |
2500 | ins.offset = file_pos; |
2501 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2502 | ||
2503 | path->leave_spinning = 1; | |
2504 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
203f44c5 | 2505 | sizeof(*stack_fi)); |
1acae57b FDBM |
2506 | if (ret) |
2507 | goto out; | |
2508 | } | |
d899e052 | 2509 | leaf = path->nodes[0]; |
203f44c5 QW |
2510 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2511 | write_extent_buffer(leaf, stack_fi, | |
2512 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2513 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2514 | |
d899e052 | 2515 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2516 | btrfs_release_path(path); |
d899e052 | 2517 | |
c553f94d | 2518 | inode_add_bytes(&inode->vfs_inode, num_bytes); |
d899e052 YZ |
2519 | |
2520 | ins.objectid = disk_bytenr; | |
2521 | ins.offset = disk_num_bytes; | |
2522 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2523 | |
c553f94d | 2524 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
9ddc959e JB |
2525 | if (ret) |
2526 | goto out; | |
2527 | ||
c553f94d | 2528 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
9729f10a | 2529 | file_pos, qgroup_reserved, &ins); |
79787eaa | 2530 | out: |
d899e052 | 2531 | btrfs_free_path(path); |
b9473439 | 2532 | |
79787eaa | 2533 | return ret; |
d899e052 YZ |
2534 | } |
2535 | ||
2ff7e61e | 2536 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2537 | u64 start, u64 len) |
2538 | { | |
32da5386 | 2539 | struct btrfs_block_group *cache; |
e570fd27 | 2540 | |
0b246afa | 2541 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2542 | ASSERT(cache); |
2543 | ||
2544 | spin_lock(&cache->lock); | |
2545 | cache->delalloc_bytes -= len; | |
2546 | spin_unlock(&cache->lock); | |
2547 | ||
2548 | btrfs_put_block_group(cache); | |
2549 | } | |
2550 | ||
203f44c5 QW |
2551 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
2552 | struct inode *inode, | |
2553 | struct btrfs_ordered_extent *oe) | |
2554 | { | |
2555 | struct btrfs_file_extent_item stack_fi; | |
2556 | u64 logical_len; | |
2557 | ||
2558 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2559 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2560 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2561 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2562 | oe->disk_num_bytes); | |
2563 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) | |
2564 | logical_len = oe->truncated_len; | |
2565 | else | |
2566 | logical_len = oe->num_bytes; | |
2567 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); | |
2568 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); | |
2569 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); | |
2570 | /* Encryption and other encoding is reserved and all 0 */ | |
2571 | ||
c553f94d | 2572 | return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset, |
7dbeaad0 | 2573 | &stack_fi, oe->qgroup_rsv); |
203f44c5 QW |
2574 | } |
2575 | ||
2576 | /* | |
2577 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
2578 | * an ordered extent if the range of bytes in the file it covers are |
2579 | * fully written. | |
2580 | */ | |
5fd02043 | 2581 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2582 | { |
5fd02043 | 2583 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2584 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2585 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2586 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2587 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2588 | struct extent_state *cached_state = NULL; |
bffe633e | 2589 | u64 start, end; |
261507a0 | 2590 | int compress_type = 0; |
77cef2ec | 2591 | int ret = 0; |
bffe633e | 2592 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 2593 | bool freespace_inode; |
77cef2ec | 2594 | bool truncated = false; |
a7e3b975 FM |
2595 | bool range_locked = false; |
2596 | bool clear_new_delalloc_bytes = false; | |
49940bdd | 2597 | bool clear_reserved_extent = true; |
313facc5 | 2598 | unsigned int clear_bits; |
a7e3b975 | 2599 | |
bffe633e OS |
2600 | start = ordered_extent->file_offset; |
2601 | end = start + ordered_extent->num_bytes - 1; | |
2602 | ||
a7e3b975 FM |
2603 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
2604 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2605 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2606 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2607 | |
8d510121 | 2608 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2609 | |
5fd02043 JB |
2610 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2611 | ret = -EIO; | |
2612 | goto out; | |
2613 | } | |
2614 | ||
bffe633e | 2615 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
f612496b | 2616 | |
77cef2ec JB |
2617 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2618 | truncated = true; | |
2619 | logical_len = ordered_extent->truncated_len; | |
2620 | /* Truncated the entire extent, don't bother adding */ | |
2621 | if (!logical_len) | |
2622 | goto out; | |
2623 | } | |
2624 | ||
c2167754 | 2625 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2626 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 2627 | |
d923afe9 | 2628 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
2629 | if (freespace_inode) |
2630 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
2631 | else |
2632 | trans = btrfs_join_transaction(root); | |
2633 | if (IS_ERR(trans)) { | |
2634 | ret = PTR_ERR(trans); | |
2635 | trans = NULL; | |
2636 | goto out; | |
c2167754 | 2637 | } |
69fe2d75 | 2638 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
2639 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2640 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2641 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2642 | goto out; |
2643 | } | |
e6dcd2dc | 2644 | |
a7e3b975 | 2645 | range_locked = true; |
bffe633e | 2646 | lock_extent_bits(io_tree, start, end, &cached_state); |
e6dcd2dc | 2647 | |
8d510121 NB |
2648 | if (freespace_inode) |
2649 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 2650 | else |
7a7eaa40 | 2651 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2652 | if (IS_ERR(trans)) { |
2653 | ret = PTR_ERR(trans); | |
2654 | trans = NULL; | |
a7e3b975 | 2655 | goto out; |
79787eaa | 2656 | } |
a79b7d4b | 2657 | |
69fe2d75 | 2658 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 2659 | |
c8b97818 | 2660 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2661 | compress_type = ordered_extent->compress_type; |
d899e052 | 2662 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2663 | BUG_ON(compress_type); |
7a6d7067 | 2664 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2665 | ordered_extent->file_offset, |
2666 | ordered_extent->file_offset + | |
77cef2ec | 2667 | logical_len); |
d899e052 | 2668 | } else { |
0b246afa | 2669 | BUG_ON(root == fs_info->tree_root); |
203f44c5 QW |
2670 | ret = insert_ordered_extent_file_extent(trans, inode, |
2671 | ordered_extent); | |
49940bdd JB |
2672 | if (!ret) { |
2673 | clear_reserved_extent = false; | |
2ff7e61e | 2674 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
2675 | ordered_extent->disk_bytenr, |
2676 | ordered_extent->disk_num_bytes); | |
49940bdd | 2677 | } |
d899e052 | 2678 | } |
5dc562c5 | 2679 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
bffe633e OS |
2680 | ordered_extent->file_offset, |
2681 | ordered_extent->num_bytes, trans->transid); | |
79787eaa | 2682 | if (ret < 0) { |
66642832 | 2683 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2684 | goto out; |
79787eaa | 2685 | } |
2ac55d41 | 2686 | |
ac01f26a NB |
2687 | ret = add_pending_csums(trans, inode, &ordered_extent->list); |
2688 | if (ret) { | |
2689 | btrfs_abort_transaction(trans, ret); | |
2690 | goto out; | |
2691 | } | |
e6dcd2dc | 2692 | |
d923afe9 | 2693 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
6c760c07 JB |
2694 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2695 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 2696 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2697 | goto out; |
1ef30be1 JB |
2698 | } |
2699 | ret = 0; | |
c2167754 | 2700 | out: |
313facc5 OS |
2701 | clear_bits = EXTENT_DEFRAG; |
2702 | if (range_locked) | |
2703 | clear_bits |= EXTENT_LOCKED; | |
2704 | if (clear_new_delalloc_bytes) | |
2705 | clear_bits |= EXTENT_DELALLOC_NEW; | |
bffe633e OS |
2706 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
2707 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, | |
313facc5 | 2708 | &cached_state); |
a7e3b975 | 2709 | |
a698d075 | 2710 | if (trans) |
3a45bb20 | 2711 | btrfs_end_transaction(trans); |
0cb59c99 | 2712 | |
77cef2ec | 2713 | if (ret || truncated) { |
bffe633e | 2714 | u64 unwritten_start = start; |
77cef2ec JB |
2715 | |
2716 | if (truncated) | |
bffe633e OS |
2717 | unwritten_start += logical_len; |
2718 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec JB |
2719 | |
2720 | /* Drop the cache for the part of the extent we didn't write. */ | |
bffe633e | 2721 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
5fd02043 | 2722 | |
0bec9ef5 JB |
2723 | /* |
2724 | * If the ordered extent had an IOERR or something else went | |
2725 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2726 | * back to the allocator. We only free the extent in the |
2727 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
2728 | * |
2729 | * If we made it past insert_reserved_file_extent before we | |
2730 | * errored out then we don't need to do this as the accounting | |
2731 | * has already been done. | |
0bec9ef5 | 2732 | */ |
77cef2ec | 2733 | if ((ret || !logical_len) && |
49940bdd | 2734 | clear_reserved_extent && |
77cef2ec | 2735 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
2736 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
2737 | /* | |
2738 | * Discard the range before returning it back to the | |
2739 | * free space pool | |
2740 | */ | |
46b27f50 | 2741 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 2742 | btrfs_discard_extent(fs_info, |
bffe633e OS |
2743 | ordered_extent->disk_bytenr, |
2744 | ordered_extent->disk_num_bytes, | |
2745 | NULL); | |
2ff7e61e | 2746 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
2747 | ordered_extent->disk_bytenr, |
2748 | ordered_extent->disk_num_bytes, 1); | |
4eaaec24 | 2749 | } |
0bec9ef5 JB |
2750 | } |
2751 | ||
5fd02043 | 2752 | /* |
8bad3c02 LB |
2753 | * This needs to be done to make sure anybody waiting knows we are done |
2754 | * updating everything for this ordered extent. | |
5fd02043 JB |
2755 | */ |
2756 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2757 | ||
e6dcd2dc CM |
2758 | /* once for us */ |
2759 | btrfs_put_ordered_extent(ordered_extent); | |
2760 | /* once for the tree */ | |
2761 | btrfs_put_ordered_extent(ordered_extent); | |
2762 | ||
5fd02043 JB |
2763 | return ret; |
2764 | } | |
2765 | ||
2766 | static void finish_ordered_fn(struct btrfs_work *work) | |
2767 | { | |
2768 | struct btrfs_ordered_extent *ordered_extent; | |
2769 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2770 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2771 | } |
2772 | ||
c629732d NB |
2773 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
2774 | u64 end, int uptodate) | |
211f90e6 | 2775 | { |
3347c48f NB |
2776 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
2777 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
5fd02043 | 2778 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 | 2779 | struct btrfs_workqueue *wq; |
5fd02043 | 2780 | |
1abe9b8a | 2781 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2782 | ||
8b62b72b | 2783 | ClearPagePrivate2(page); |
3347c48f NB |
2784 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2785 | end - start + 1, uptodate)) | |
c3988d63 | 2786 | return; |
5fd02043 | 2787 | |
3347c48f | 2788 | if (btrfs_is_free_space_inode(inode)) |
0b246afa | 2789 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 2790 | else |
0b246afa | 2791 | wq = fs_info->endio_write_workers; |
5fd02043 | 2792 | |
a0cac0ec | 2793 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
9e0af237 | 2794 | btrfs_queue_work(wq, &ordered_extent->work); |
211f90e6 CM |
2795 | } |
2796 | ||
47df7765 OS |
2797 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
2798 | int icsum, struct page *page, int pgoff, u64 start, | |
2799 | size_t len) | |
dc380aea | 2800 | { |
d5178578 JT |
2801 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2802 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
dc380aea | 2803 | char *kaddr; |
d5178578 JT |
2804 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
2805 | u8 *csum_expected; | |
2806 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 2807 | |
d5178578 | 2808 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
dc380aea MX |
2809 | |
2810 | kaddr = kmap_atomic(page); | |
d5178578 JT |
2811 | shash->tfm = fs_info->csum_shash; |
2812 | ||
fd08001f | 2813 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
d5178578 JT |
2814 | |
2815 | if (memcmp(csum, csum_expected, csum_size)) | |
dc380aea MX |
2816 | goto zeroit; |
2817 | ||
2818 | kunmap_atomic(kaddr); | |
2819 | return 0; | |
2820 | zeroit: | |
ea41d6b2 JT |
2821 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
2822 | io_bio->mirror_num); | |
814723e0 NB |
2823 | if (io_bio->device) |
2824 | btrfs_dev_stat_inc_and_print(io_bio->device, | |
2825 | BTRFS_DEV_STAT_CORRUPTION_ERRS); | |
dc380aea MX |
2826 | memset(kaddr + pgoff, 1, len); |
2827 | flush_dcache_page(page); | |
2828 | kunmap_atomic(kaddr); | |
dc380aea MX |
2829 | return -EIO; |
2830 | } | |
2831 | ||
d352ac68 CM |
2832 | /* |
2833 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
2834 | * if there's a match, we allow the bio to finish. If not, the code in |
2835 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 2836 | */ |
facc8a22 MX |
2837 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
2838 | u64 phy_offset, struct page *page, | |
2839 | u64 start, u64 end, int mirror) | |
07157aac | 2840 | { |
4eee4fa4 | 2841 | size_t offset = start - page_offset(page); |
07157aac | 2842 | struct inode *inode = page->mapping->host; |
d1310b2e | 2843 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 2844 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 2845 | |
d20f7043 CM |
2846 | if (PageChecked(page)) { |
2847 | ClearPageChecked(page); | |
dc380aea | 2848 | return 0; |
d20f7043 | 2849 | } |
6cbff00f CH |
2850 | |
2851 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 2852 | return 0; |
17d217fe YZ |
2853 | |
2854 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 2855 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 2856 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 2857 | return 0; |
17d217fe | 2858 | } |
d20f7043 | 2859 | |
facc8a22 | 2860 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
47df7765 OS |
2861 | return check_data_csum(inode, io_bio, phy_offset, page, offset, start, |
2862 | (size_t)(end - start + 1)); | |
07157aac | 2863 | } |
b888db2b | 2864 | |
c1c3fac2 NB |
2865 | /* |
2866 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
2867 | * | |
2868 | * @inode: The inode we want to perform iput on | |
2869 | * | |
2870 | * This function uses the generic vfs_inode::i_count to track whether we should | |
2871 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
2872 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
2873 | * transaction commit time/superblock commit/cleaner kthread. | |
2874 | */ | |
24bbcf04 YZ |
2875 | void btrfs_add_delayed_iput(struct inode *inode) |
2876 | { | |
0b246afa | 2877 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 2878 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
2879 | |
2880 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
2881 | return; | |
2882 | ||
034f784d | 2883 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 2884 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
2885 | ASSERT(list_empty(&binode->delayed_iput)); |
2886 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 2887 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
2888 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
2889 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
2890 | } |
2891 | ||
63611e73 JB |
2892 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
2893 | struct btrfs_inode *inode) | |
2894 | { | |
2895 | list_del_init(&inode->delayed_iput); | |
2896 | spin_unlock(&fs_info->delayed_iput_lock); | |
2897 | iput(&inode->vfs_inode); | |
2898 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
2899 | wake_up(&fs_info->delayed_iputs_wait); | |
2900 | spin_lock(&fs_info->delayed_iput_lock); | |
2901 | } | |
2902 | ||
2903 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
2904 | struct btrfs_inode *inode) | |
2905 | { | |
2906 | if (!list_empty(&inode->delayed_iput)) { | |
2907 | spin_lock(&fs_info->delayed_iput_lock); | |
2908 | if (!list_empty(&inode->delayed_iput)) | |
2909 | run_delayed_iput_locked(fs_info, inode); | |
2910 | spin_unlock(&fs_info->delayed_iput_lock); | |
2911 | } | |
2912 | } | |
2913 | ||
2ff7e61e | 2914 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 2915 | { |
24bbcf04 | 2916 | |
24bbcf04 | 2917 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
2918 | while (!list_empty(&fs_info->delayed_iputs)) { |
2919 | struct btrfs_inode *inode; | |
2920 | ||
2921 | inode = list_first_entry(&fs_info->delayed_iputs, | |
2922 | struct btrfs_inode, delayed_iput); | |
63611e73 | 2923 | run_delayed_iput_locked(fs_info, inode); |
24bbcf04 | 2924 | } |
8089fe62 | 2925 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
2926 | } |
2927 | ||
034f784d JB |
2928 | /** |
2929 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running | |
2930 | * @fs_info - the fs_info for this fs | |
2931 | * @return - EINTR if we were killed, 0 if nothing's pending | |
2932 | * | |
2933 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
2934 | * set. Once they are all done running we will return, unless we are killed in | |
2935 | * which case we return EINTR. This helps in user operations like fallocate etc | |
2936 | * that might get blocked on the iputs. | |
2937 | */ | |
2938 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
2939 | { | |
2940 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
2941 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
2942 | if (ret) | |
2943 | return -EINTR; | |
2944 | return 0; | |
2945 | } | |
2946 | ||
7b128766 | 2947 | /* |
f7e9e8fc OS |
2948 | * This creates an orphan entry for the given inode in case something goes wrong |
2949 | * in the middle of an unlink. | |
7b128766 | 2950 | */ |
73f2e545 | 2951 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 2952 | struct btrfs_inode *inode) |
7b128766 | 2953 | { |
d68fc57b | 2954 | int ret; |
7b128766 | 2955 | |
27919067 OS |
2956 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
2957 | if (ret && ret != -EEXIST) { | |
2958 | btrfs_abort_transaction(trans, ret); | |
2959 | return ret; | |
d68fc57b YZ |
2960 | } |
2961 | ||
d68fc57b | 2962 | return 0; |
7b128766 JB |
2963 | } |
2964 | ||
2965 | /* | |
f7e9e8fc OS |
2966 | * We have done the delete so we can go ahead and remove the orphan item for |
2967 | * this particular inode. | |
7b128766 | 2968 | */ |
48a3b636 | 2969 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 2970 | struct btrfs_inode *inode) |
7b128766 | 2971 | { |
27919067 | 2972 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
2973 | } |
2974 | ||
2975 | /* | |
2976 | * this cleans up any orphans that may be left on the list from the last use | |
2977 | * of this root. | |
2978 | */ | |
66b4ffd1 | 2979 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 2980 | { |
0b246afa | 2981 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
2982 | struct btrfs_path *path; |
2983 | struct extent_buffer *leaf; | |
7b128766 JB |
2984 | struct btrfs_key key, found_key; |
2985 | struct btrfs_trans_handle *trans; | |
2986 | struct inode *inode; | |
8f6d7f4f | 2987 | u64 last_objectid = 0; |
f7e9e8fc | 2988 | int ret = 0, nr_unlink = 0; |
7b128766 | 2989 | |
d68fc57b | 2990 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 2991 | return 0; |
c71bf099 YZ |
2992 | |
2993 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
2994 | if (!path) { |
2995 | ret = -ENOMEM; | |
2996 | goto out; | |
2997 | } | |
e4058b54 | 2998 | path->reada = READA_BACK; |
7b128766 JB |
2999 | |
3000 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3001 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3002 | key.offset = (u64)-1; |
3003 | ||
7b128766 JB |
3004 | while (1) { |
3005 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3006 | if (ret < 0) |
3007 | goto out; | |
7b128766 JB |
3008 | |
3009 | /* | |
3010 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3011 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3012 | * find the key and see if we have stuff that matches |
3013 | */ | |
3014 | if (ret > 0) { | |
66b4ffd1 | 3015 | ret = 0; |
7b128766 JB |
3016 | if (path->slots[0] == 0) |
3017 | break; | |
3018 | path->slots[0]--; | |
3019 | } | |
3020 | ||
3021 | /* pull out the item */ | |
3022 | leaf = path->nodes[0]; | |
7b128766 JB |
3023 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3024 | ||
3025 | /* make sure the item matches what we want */ | |
3026 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3027 | break; | |
962a298f | 3028 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3029 | break; |
3030 | ||
3031 | /* release the path since we're done with it */ | |
b3b4aa74 | 3032 | btrfs_release_path(path); |
7b128766 JB |
3033 | |
3034 | /* | |
3035 | * this is where we are basically btrfs_lookup, without the | |
3036 | * crossing root thing. we store the inode number in the | |
3037 | * offset of the orphan item. | |
3038 | */ | |
8f6d7f4f JB |
3039 | |
3040 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3041 | btrfs_err(fs_info, |
3042 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3043 | ret = -EINVAL; |
3044 | goto out; | |
3045 | } | |
3046 | ||
3047 | last_objectid = found_key.offset; | |
3048 | ||
5d4f98a2 YZ |
3049 | found_key.objectid = found_key.offset; |
3050 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3051 | found_key.offset = 0; | |
0202e83f | 3052 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
8c6ffba0 | 3053 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3054 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3055 | goto out; |
7b128766 | 3056 | |
0b246afa | 3057 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 | 3058 | struct btrfs_root *dead_root; |
f8e9e0b0 AJ |
3059 | int is_dead_root = 0; |
3060 | ||
3061 | /* | |
3062 | * this is an orphan in the tree root. Currently these | |
3063 | * could come from 2 sources: | |
3064 | * a) a snapshot deletion in progress | |
3065 | * b) a free space cache inode | |
3066 | * We need to distinguish those two, as the snapshot | |
3067 | * orphan must not get deleted. | |
3068 | * find_dead_roots already ran before us, so if this | |
3069 | * is a snapshot deletion, we should find the root | |
a619b3c7 | 3070 | * in the fs_roots radix tree. |
f8e9e0b0 | 3071 | */ |
a619b3c7 RK |
3072 | |
3073 | spin_lock(&fs_info->fs_roots_radix_lock); | |
3074 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3075 | (unsigned long)found_key.objectid); | |
3076 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) | |
3077 | is_dead_root = 1; | |
3078 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
3079 | ||
f8e9e0b0 AJ |
3080 | if (is_dead_root) { |
3081 | /* prevent this orphan from being found again */ | |
3082 | key.offset = found_key.objectid - 1; | |
3083 | continue; | |
3084 | } | |
f7e9e8fc | 3085 | |
f8e9e0b0 | 3086 | } |
f7e9e8fc | 3087 | |
7b128766 | 3088 | /* |
f7e9e8fc OS |
3089 | * If we have an inode with links, there are a couple of |
3090 | * possibilities. Old kernels (before v3.12) used to create an | |
3091 | * orphan item for truncate indicating that there were possibly | |
3092 | * extent items past i_size that needed to be deleted. In v3.12, | |
3093 | * truncate was changed to update i_size in sync with the extent | |
3094 | * items, but the (useless) orphan item was still created. Since | |
3095 | * v4.18, we don't create the orphan item for truncate at all. | |
3096 | * | |
3097 | * So, this item could mean that we need to do a truncate, but | |
3098 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3099 | * and was not cleanly unmounted. The odds of that are quite | |
3100 | * slim, and it's a pain to do the truncate now, so just delete | |
3101 | * the orphan item. | |
3102 | * | |
3103 | * It's also possible that this orphan item was supposed to be | |
3104 | * deleted but wasn't. The inode number may have been reused, | |
3105 | * but either way, we can delete the orphan item. | |
7b128766 | 3106 | */ |
f7e9e8fc OS |
3107 | if (ret == -ENOENT || inode->i_nlink) { |
3108 | if (!ret) | |
3109 | iput(inode); | |
a8c9e576 | 3110 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3111 | if (IS_ERR(trans)) { |
3112 | ret = PTR_ERR(trans); | |
3113 | goto out; | |
3114 | } | |
0b246afa JM |
3115 | btrfs_debug(fs_info, "auto deleting %Lu", |
3116 | found_key.objectid); | |
a8c9e576 JB |
3117 | ret = btrfs_del_orphan_item(trans, root, |
3118 | found_key.objectid); | |
3a45bb20 | 3119 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3120 | if (ret) |
3121 | goto out; | |
7b128766 JB |
3122 | continue; |
3123 | } | |
3124 | ||
f7e9e8fc | 3125 | nr_unlink++; |
7b128766 JB |
3126 | |
3127 | /* this will do delete_inode and everything for us */ | |
3128 | iput(inode); | |
3129 | } | |
3254c876 MX |
3130 | /* release the path since we're done with it */ |
3131 | btrfs_release_path(path); | |
3132 | ||
d68fc57b YZ |
3133 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3134 | ||
a575ceeb | 3135 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3136 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3137 | if (!IS_ERR(trans)) |
3a45bb20 | 3138 | btrfs_end_transaction(trans); |
d68fc57b | 3139 | } |
7b128766 JB |
3140 | |
3141 | if (nr_unlink) | |
0b246afa | 3142 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3143 | |
3144 | out: | |
3145 | if (ret) | |
0b246afa | 3146 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3147 | btrfs_free_path(path); |
3148 | return ret; | |
7b128766 JB |
3149 | } |
3150 | ||
46a53cca CM |
3151 | /* |
3152 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3153 | * don't find any xattrs, we know there can't be any acls. | |
3154 | * | |
3155 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3156 | */ | |
3157 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3158 | int slot, u64 objectid, |
3159 | int *first_xattr_slot) | |
46a53cca CM |
3160 | { |
3161 | u32 nritems = btrfs_header_nritems(leaf); | |
3162 | struct btrfs_key found_key; | |
f23b5a59 JB |
3163 | static u64 xattr_access = 0; |
3164 | static u64 xattr_default = 0; | |
46a53cca CM |
3165 | int scanned = 0; |
3166 | ||
f23b5a59 | 3167 | if (!xattr_access) { |
97d79299 AG |
3168 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3169 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3170 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3171 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3172 | } |
3173 | ||
46a53cca | 3174 | slot++; |
63541927 | 3175 | *first_xattr_slot = -1; |
46a53cca CM |
3176 | while (slot < nritems) { |
3177 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3178 | ||
3179 | /* we found a different objectid, there must not be acls */ | |
3180 | if (found_key.objectid != objectid) | |
3181 | return 0; | |
3182 | ||
3183 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3184 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3185 | if (*first_xattr_slot == -1) |
3186 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3187 | if (found_key.offset == xattr_access || |
3188 | found_key.offset == xattr_default) | |
3189 | return 1; | |
3190 | } | |
46a53cca CM |
3191 | |
3192 | /* | |
3193 | * we found a key greater than an xattr key, there can't | |
3194 | * be any acls later on | |
3195 | */ | |
3196 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3197 | return 0; | |
3198 | ||
3199 | slot++; | |
3200 | scanned++; | |
3201 | ||
3202 | /* | |
3203 | * it goes inode, inode backrefs, xattrs, extents, | |
3204 | * so if there are a ton of hard links to an inode there can | |
3205 | * be a lot of backrefs. Don't waste time searching too hard, | |
3206 | * this is just an optimization | |
3207 | */ | |
3208 | if (scanned >= 8) | |
3209 | break; | |
3210 | } | |
3211 | /* we hit the end of the leaf before we found an xattr or | |
3212 | * something larger than an xattr. We have to assume the inode | |
3213 | * has acls | |
3214 | */ | |
63541927 FDBM |
3215 | if (*first_xattr_slot == -1) |
3216 | *first_xattr_slot = slot; | |
46a53cca CM |
3217 | return 1; |
3218 | } | |
3219 | ||
d352ac68 CM |
3220 | /* |
3221 | * read an inode from the btree into the in-memory inode | |
3222 | */ | |
4222ea71 FM |
3223 | static int btrfs_read_locked_inode(struct inode *inode, |
3224 | struct btrfs_path *in_path) | |
39279cc3 | 3225 | { |
0b246afa | 3226 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3227 | struct btrfs_path *path = in_path; |
5f39d397 | 3228 | struct extent_buffer *leaf; |
39279cc3 CM |
3229 | struct btrfs_inode_item *inode_item; |
3230 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3231 | struct btrfs_key location; | |
67de1176 | 3232 | unsigned long ptr; |
46a53cca | 3233 | int maybe_acls; |
618e21d5 | 3234 | u32 rdev; |
39279cc3 | 3235 | int ret; |
2f7e33d4 | 3236 | bool filled = false; |
63541927 | 3237 | int first_xattr_slot; |
2f7e33d4 MX |
3238 | |
3239 | ret = btrfs_fill_inode(inode, &rdev); | |
3240 | if (!ret) | |
3241 | filled = true; | |
39279cc3 | 3242 | |
4222ea71 FM |
3243 | if (!path) { |
3244 | path = btrfs_alloc_path(); | |
3245 | if (!path) | |
3246 | return -ENOMEM; | |
3247 | } | |
1748f843 | 3248 | |
39279cc3 | 3249 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3250 | |
39279cc3 | 3251 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3252 | if (ret) { |
4222ea71 FM |
3253 | if (path != in_path) |
3254 | btrfs_free_path(path); | |
f5b3a417 | 3255 | return ret; |
67710892 | 3256 | } |
39279cc3 | 3257 | |
5f39d397 | 3258 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3259 | |
3260 | if (filled) | |
67de1176 | 3261 | goto cache_index; |
2f7e33d4 | 3262 | |
5f39d397 CM |
3263 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3264 | struct btrfs_inode_item); | |
5f39d397 | 3265 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3266 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3267 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3268 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3269 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3270 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3271 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3272 | |
a937b979 DS |
3273 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3274 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3275 | |
a937b979 DS |
3276 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3277 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3278 | |
a937b979 DS |
3279 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3280 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3281 | |
9cc97d64 | 3282 | BTRFS_I(inode)->i_otime.tv_sec = |
3283 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3284 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3285 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3286 | |
a76a3cd4 | 3287 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3288 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3289 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3290 | ||
c7f88c4e JL |
3291 | inode_set_iversion_queried(inode, |
3292 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3293 | inode->i_generation = BTRFS_I(inode)->generation; |
3294 | inode->i_rdev = 0; | |
3295 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3296 | ||
3297 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3298 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3299 | ||
3300 | cache_index: | |
5dc562c5 JB |
3301 | /* |
3302 | * If we were modified in the current generation and evicted from memory | |
3303 | * and then re-read we need to do a full sync since we don't have any | |
3304 | * idea about which extents were modified before we were evicted from | |
3305 | * cache. | |
6e17d30b YD |
3306 | * |
3307 | * This is required for both inode re-read from disk and delayed inode | |
3308 | * in delayed_nodes_tree. | |
5dc562c5 | 3309 | */ |
0b246afa | 3310 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3311 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3312 | &BTRFS_I(inode)->runtime_flags); | |
3313 | ||
bde6c242 FM |
3314 | /* |
3315 | * We don't persist the id of the transaction where an unlink operation | |
3316 | * against the inode was last made. So here we assume the inode might | |
3317 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3318 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3319 | * between the inode and its parent if the inode is fsync'ed and the log | |
3320 | * replayed. For example, in the scenario: | |
3321 | * | |
3322 | * touch mydir/foo | |
3323 | * ln mydir/foo mydir/bar | |
3324 | * sync | |
3325 | * unlink mydir/bar | |
3326 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3327 | * xfs_io -c fsync mydir/foo | |
3328 | * <power failure> | |
3329 | * mount fs, triggers fsync log replay | |
3330 | * | |
3331 | * We must make sure that when we fsync our inode foo we also log its | |
3332 | * parent inode, otherwise after log replay the parent still has the | |
3333 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3334 | * and doesn't have an inode ref with the name "bar" anymore. | |
3335 | * | |
3336 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3337 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3338 | * transaction commits on fsync if our inode is a directory, or if our |
3339 | * inode is not a directory, logging its parent unnecessarily. | |
3340 | */ | |
3341 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3342 | ||
3ebac17c FM |
3343 | /* |
3344 | * Same logic as for last_unlink_trans. We don't persist the generation | |
3345 | * of the last transaction where this inode was used for a reflink | |
3346 | * operation, so after eviction and reloading the inode we must be | |
3347 | * pessimistic and assume the last transaction that modified the inode. | |
3348 | */ | |
3349 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; | |
3350 | ||
67de1176 MX |
3351 | path->slots[0]++; |
3352 | if (inode->i_nlink != 1 || | |
3353 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3354 | goto cache_acl; | |
3355 | ||
3356 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3357 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3358 | goto cache_acl; |
3359 | ||
3360 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3361 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3362 | struct btrfs_inode_ref *ref; | |
3363 | ||
3364 | ref = (struct btrfs_inode_ref *)ptr; | |
3365 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3366 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3367 | struct btrfs_inode_extref *extref; | |
3368 | ||
3369 | extref = (struct btrfs_inode_extref *)ptr; | |
3370 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3371 | extref); | |
3372 | } | |
2f7e33d4 | 3373 | cache_acl: |
46a53cca CM |
3374 | /* |
3375 | * try to precache a NULL acl entry for files that don't have | |
3376 | * any xattrs or acls | |
3377 | */ | |
33345d01 | 3378 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3379 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3380 | if (first_xattr_slot != -1) { |
3381 | path->slots[0] = first_xattr_slot; | |
3382 | ret = btrfs_load_inode_props(inode, path); | |
3383 | if (ret) | |
0b246afa | 3384 | btrfs_err(fs_info, |
351fd353 | 3385 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3386 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3387 | root->root_key.objectid, ret); |
3388 | } | |
4222ea71 FM |
3389 | if (path != in_path) |
3390 | btrfs_free_path(path); | |
63541927 | 3391 | |
72c04902 AV |
3392 | if (!maybe_acls) |
3393 | cache_no_acl(inode); | |
46a53cca | 3394 | |
39279cc3 | 3395 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3396 | case S_IFREG: |
3397 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3398 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3399 | inode->i_fop = &btrfs_file_operations; |
3400 | inode->i_op = &btrfs_file_inode_operations; | |
3401 | break; | |
3402 | case S_IFDIR: | |
3403 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3404 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3405 | break; |
3406 | case S_IFLNK: | |
3407 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3408 | inode_nohighmem(inode); |
4779cc04 | 3409 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3410 | break; |
618e21d5 | 3411 | default: |
0279b4cd | 3412 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3413 | init_special_inode(inode, inode->i_mode, rdev); |
3414 | break; | |
39279cc3 | 3415 | } |
6cbff00f | 3416 | |
7b6a221e | 3417 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3418 | return 0; |
39279cc3 CM |
3419 | } |
3420 | ||
d352ac68 CM |
3421 | /* |
3422 | * given a leaf and an inode, copy the inode fields into the leaf | |
3423 | */ | |
e02119d5 CM |
3424 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3425 | struct extent_buffer *leaf, | |
5f39d397 | 3426 | struct btrfs_inode_item *item, |
39279cc3 CM |
3427 | struct inode *inode) |
3428 | { | |
51fab693 LB |
3429 | struct btrfs_map_token token; |
3430 | ||
c82f823c | 3431 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3432 | |
cc4c13d5 DS |
3433 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3434 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3435 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3436 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3437 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3438 | ||
3439 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
3440 | inode->i_atime.tv_sec); | |
3441 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
3442 | inode->i_atime.tv_nsec); | |
3443 | ||
3444 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
3445 | inode->i_mtime.tv_sec); | |
3446 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
3447 | inode->i_mtime.tv_nsec); | |
3448 | ||
3449 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
3450 | inode->i_ctime.tv_sec); | |
3451 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
3452 | inode->i_ctime.tv_nsec); | |
3453 | ||
3454 | btrfs_set_token_timespec_sec(&token, &item->otime, | |
3455 | BTRFS_I(inode)->i_otime.tv_sec); | |
3456 | btrfs_set_token_timespec_nsec(&token, &item->otime, | |
3457 | BTRFS_I(inode)->i_otime.tv_nsec); | |
3458 | ||
3459 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3460 | btrfs_set_token_inode_generation(&token, item, | |
3461 | BTRFS_I(inode)->generation); | |
3462 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3463 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3464 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
3465 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); | |
3466 | btrfs_set_token_inode_block_group(&token, item, 0); | |
39279cc3 CM |
3467 | } |
3468 | ||
d352ac68 CM |
3469 | /* |
3470 | * copy everything in the in-memory inode into the btree. | |
3471 | */ | |
2115133f | 3472 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3473 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3474 | { |
3475 | struct btrfs_inode_item *inode_item; | |
3476 | struct btrfs_path *path; | |
5f39d397 | 3477 | struct extent_buffer *leaf; |
39279cc3 CM |
3478 | int ret; |
3479 | ||
3480 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3481 | if (!path) |
3482 | return -ENOMEM; | |
3483 | ||
b9473439 | 3484 | path->leave_spinning = 1; |
16cdcec7 MX |
3485 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3486 | 1); | |
39279cc3 CM |
3487 | if (ret) { |
3488 | if (ret > 0) | |
3489 | ret = -ENOENT; | |
3490 | goto failed; | |
3491 | } | |
3492 | ||
5f39d397 CM |
3493 | leaf = path->nodes[0]; |
3494 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3495 | struct btrfs_inode_item); |
39279cc3 | 3496 | |
e02119d5 | 3497 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3498 | btrfs_mark_buffer_dirty(leaf); |
d9094414 | 3499 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
39279cc3 CM |
3500 | ret = 0; |
3501 | failed: | |
39279cc3 CM |
3502 | btrfs_free_path(path); |
3503 | return ret; | |
3504 | } | |
3505 | ||
2115133f CM |
3506 | /* |
3507 | * copy everything in the in-memory inode into the btree. | |
3508 | */ | |
3509 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3510 | struct btrfs_root *root, struct inode *inode) | |
3511 | { | |
0b246afa | 3512 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3513 | int ret; |
3514 | ||
3515 | /* | |
3516 | * If the inode is a free space inode, we can deadlock during commit | |
3517 | * if we put it into the delayed code. | |
3518 | * | |
3519 | * The data relocation inode should also be directly updated | |
3520 | * without delay | |
3521 | */ | |
70ddc553 | 3522 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3523 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3524 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3525 | btrfs_update_root_times(trans, root); |
3526 | ||
2115133f CM |
3527 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3528 | if (!ret) | |
d9094414 | 3529 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
2115133f CM |
3530 | return ret; |
3531 | } | |
3532 | ||
3533 | return btrfs_update_inode_item(trans, root, inode); | |
3534 | } | |
3535 | ||
be6aef60 JB |
3536 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3537 | struct btrfs_root *root, | |
3538 | struct inode *inode) | |
2115133f CM |
3539 | { |
3540 | int ret; | |
3541 | ||
3542 | ret = btrfs_update_inode(trans, root, inode); | |
3543 | if (ret == -ENOSPC) | |
3544 | return btrfs_update_inode_item(trans, root, inode); | |
3545 | return ret; | |
3546 | } | |
3547 | ||
d352ac68 CM |
3548 | /* |
3549 | * unlink helper that gets used here in inode.c and in the tree logging | |
3550 | * recovery code. It remove a link in a directory with a given name, and | |
3551 | * also drops the back refs in the inode to the directory | |
3552 | */ | |
92986796 AV |
3553 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3554 | struct btrfs_root *root, | |
4ec5934e NB |
3555 | struct btrfs_inode *dir, |
3556 | struct btrfs_inode *inode, | |
92986796 | 3557 | const char *name, int name_len) |
39279cc3 | 3558 | { |
0b246afa | 3559 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 3560 | struct btrfs_path *path; |
39279cc3 | 3561 | int ret = 0; |
39279cc3 | 3562 | struct btrfs_dir_item *di; |
aec7477b | 3563 | u64 index; |
33345d01 LZ |
3564 | u64 ino = btrfs_ino(inode); |
3565 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3566 | |
3567 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3568 | if (!path) { |
3569 | ret = -ENOMEM; | |
554233a6 | 3570 | goto out; |
54aa1f4d CM |
3571 | } |
3572 | ||
b9473439 | 3573 | path->leave_spinning = 1; |
33345d01 | 3574 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 3575 | name, name_len, -1); |
3cf5068f LB |
3576 | if (IS_ERR_OR_NULL(di)) { |
3577 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
3578 | goto err; |
3579 | } | |
39279cc3 | 3580 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3581 | if (ret) |
3582 | goto err; | |
b3b4aa74 | 3583 | btrfs_release_path(path); |
39279cc3 | 3584 | |
67de1176 MX |
3585 | /* |
3586 | * If we don't have dir index, we have to get it by looking up | |
3587 | * the inode ref, since we get the inode ref, remove it directly, | |
3588 | * it is unnecessary to do delayed deletion. | |
3589 | * | |
3590 | * But if we have dir index, needn't search inode ref to get it. | |
3591 | * Since the inode ref is close to the inode item, it is better | |
3592 | * that we delay to delete it, and just do this deletion when | |
3593 | * we update the inode item. | |
3594 | */ | |
4ec5934e | 3595 | if (inode->dir_index) { |
67de1176 MX |
3596 | ret = btrfs_delayed_delete_inode_ref(inode); |
3597 | if (!ret) { | |
4ec5934e | 3598 | index = inode->dir_index; |
67de1176 MX |
3599 | goto skip_backref; |
3600 | } | |
3601 | } | |
3602 | ||
33345d01 LZ |
3603 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3604 | dir_ino, &index); | |
aec7477b | 3605 | if (ret) { |
0b246afa | 3606 | btrfs_info(fs_info, |
c2cf52eb | 3607 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 3608 | name_len, name, ino, dir_ino); |
66642832 | 3609 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
3610 | goto err; |
3611 | } | |
67de1176 | 3612 | skip_backref: |
9add2945 | 3613 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 3614 | if (ret) { |
66642832 | 3615 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 3616 | goto err; |
79787eaa | 3617 | } |
39279cc3 | 3618 | |
4ec5934e NB |
3619 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
3620 | dir_ino); | |
79787eaa | 3621 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 3622 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3623 | goto err; |
3624 | } | |
e02119d5 | 3625 | |
4ec5934e NB |
3626 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
3627 | index); | |
6418c961 CM |
3628 | if (ret == -ENOENT) |
3629 | ret = 0; | |
d4e3991b | 3630 | else if (ret) |
66642832 | 3631 | btrfs_abort_transaction(trans, ret); |
63611e73 JB |
3632 | |
3633 | /* | |
3634 | * If we have a pending delayed iput we could end up with the final iput | |
3635 | * being run in btrfs-cleaner context. If we have enough of these built | |
3636 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
3637 | * way to throttle the unlinks. Since we're currently holding a ref on | |
3638 | * the inode we can run the delayed iput here without any issues as the | |
3639 | * final iput won't be done until after we drop the ref we're currently | |
3640 | * holding. | |
3641 | */ | |
3642 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
3643 | err: |
3644 | btrfs_free_path(path); | |
e02119d5 CM |
3645 | if (ret) |
3646 | goto out; | |
3647 | ||
6ef06d27 | 3648 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
3649 | inode_inc_iversion(&inode->vfs_inode); |
3650 | inode_inc_iversion(&dir->vfs_inode); | |
3651 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
3652 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
3653 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 3654 | out: |
39279cc3 CM |
3655 | return ret; |
3656 | } | |
3657 | ||
92986796 AV |
3658 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3659 | struct btrfs_root *root, | |
4ec5934e | 3660 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
3661 | const char *name, int name_len) |
3662 | { | |
3663 | int ret; | |
3664 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
3665 | if (!ret) { | |
4ec5934e NB |
3666 | drop_nlink(&inode->vfs_inode); |
3667 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
3668 | } |
3669 | return ret; | |
3670 | } | |
39279cc3 | 3671 | |
a22285a6 YZ |
3672 | /* |
3673 | * helper to start transaction for unlink and rmdir. | |
3674 | * | |
d52be818 JB |
3675 | * unlink and rmdir are special in btrfs, they do not always free space, so |
3676 | * if we cannot make our reservations the normal way try and see if there is | |
3677 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
3678 | * allow the unlink to occur. | |
a22285a6 | 3679 | */ |
d52be818 | 3680 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 3681 | { |
a22285a6 | 3682 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 3683 | |
e70bea5f JB |
3684 | /* |
3685 | * 1 for the possible orphan item | |
3686 | * 1 for the dir item | |
3687 | * 1 for the dir index | |
3688 | * 1 for the inode ref | |
e70bea5f JB |
3689 | * 1 for the inode |
3690 | */ | |
7f9fe614 | 3691 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
a22285a6 YZ |
3692 | } |
3693 | ||
3694 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
3695 | { | |
3696 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3697 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 3698 | struct inode *inode = d_inode(dentry); |
a22285a6 | 3699 | int ret; |
a22285a6 | 3700 | |
d52be818 | 3701 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
3702 | if (IS_ERR(trans)) |
3703 | return PTR_ERR(trans); | |
5f39d397 | 3704 | |
4ec5934e NB |
3705 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
3706 | 0); | |
12fcfd22 | 3707 | |
4ec5934e NB |
3708 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
3709 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
3710 | dentry->d_name.len); | |
b532402e TI |
3711 | if (ret) |
3712 | goto out; | |
7b128766 | 3713 | |
a22285a6 | 3714 | if (inode->i_nlink == 0) { |
73f2e545 | 3715 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
3716 | if (ret) |
3717 | goto out; | |
a22285a6 | 3718 | } |
7b128766 | 3719 | |
b532402e | 3720 | out: |
3a45bb20 | 3721 | btrfs_end_transaction(trans); |
2ff7e61e | 3722 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
3723 | return ret; |
3724 | } | |
3725 | ||
f60a2364 | 3726 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
045d3967 | 3727 | struct inode *dir, struct dentry *dentry) |
4df27c4d | 3728 | { |
401b3b19 | 3729 | struct btrfs_root *root = BTRFS_I(dir)->root; |
045d3967 | 3730 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
3731 | struct btrfs_path *path; |
3732 | struct extent_buffer *leaf; | |
3733 | struct btrfs_dir_item *di; | |
3734 | struct btrfs_key key; | |
045d3967 JB |
3735 | const char *name = dentry->d_name.name; |
3736 | int name_len = dentry->d_name.len; | |
4df27c4d YZ |
3737 | u64 index; |
3738 | int ret; | |
045d3967 | 3739 | u64 objectid; |
4a0cc7ca | 3740 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d | 3741 | |
045d3967 JB |
3742 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
3743 | objectid = inode->root->root_key.objectid; | |
3744 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
3745 | objectid = inode->location.objectid; | |
3746 | } else { | |
3747 | WARN_ON(1); | |
3748 | return -EINVAL; | |
3749 | } | |
3750 | ||
4df27c4d YZ |
3751 | path = btrfs_alloc_path(); |
3752 | if (!path) | |
3753 | return -ENOMEM; | |
3754 | ||
33345d01 | 3755 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 3756 | name, name_len, -1); |
79787eaa | 3757 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 3758 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
3759 | goto out; |
3760 | } | |
4df27c4d YZ |
3761 | |
3762 | leaf = path->nodes[0]; | |
3763 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
3764 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
3765 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 3766 | if (ret) { |
66642832 | 3767 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3768 | goto out; |
3769 | } | |
b3b4aa74 | 3770 | btrfs_release_path(path); |
4df27c4d | 3771 | |
d49d3287 JB |
3772 | /* |
3773 | * This is a placeholder inode for a subvolume we didn't have a | |
3774 | * reference to at the time of the snapshot creation. In the meantime | |
3775 | * we could have renamed the real subvol link into our snapshot, so | |
3776 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. | |
3777 | * Instead simply lookup the dir_index_item for this entry so we can | |
3778 | * remove it. Otherwise we know we have a ref to the root and we can | |
3779 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
3780 | */ | |
3781 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
33345d01 | 3782 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 3783 | name, name_len); |
79787eaa JM |
3784 | if (IS_ERR_OR_NULL(di)) { |
3785 | if (!di) | |
3786 | ret = -ENOENT; | |
3787 | else | |
3788 | ret = PTR_ERR(di); | |
66642832 | 3789 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3790 | goto out; |
3791 | } | |
4df27c4d YZ |
3792 | |
3793 | leaf = path->nodes[0]; | |
3794 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 3795 | index = key.offset; |
d49d3287 JB |
3796 | btrfs_release_path(path); |
3797 | } else { | |
3798 | ret = btrfs_del_root_ref(trans, objectid, | |
3799 | root->root_key.objectid, dir_ino, | |
3800 | &index, name, name_len); | |
3801 | if (ret) { | |
3802 | btrfs_abort_transaction(trans, ret); | |
3803 | goto out; | |
3804 | } | |
4df27c4d YZ |
3805 | } |
3806 | ||
9add2945 | 3807 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 3808 | if (ret) { |
66642832 | 3809 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3810 | goto out; |
3811 | } | |
4df27c4d | 3812 | |
6ef06d27 | 3813 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 3814 | inode_inc_iversion(dir); |
c2050a45 | 3815 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 3816 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 3817 | if (ret) |
66642832 | 3818 | btrfs_abort_transaction(trans, ret); |
79787eaa | 3819 | out: |
71d7aed0 | 3820 | btrfs_free_path(path); |
79787eaa | 3821 | return ret; |
4df27c4d YZ |
3822 | } |
3823 | ||
ec42f167 MT |
3824 | /* |
3825 | * Helper to check if the subvolume references other subvolumes or if it's | |
3826 | * default. | |
3827 | */ | |
f60a2364 | 3828 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
3829 | { |
3830 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3831 | struct btrfs_path *path; | |
3832 | struct btrfs_dir_item *di; | |
3833 | struct btrfs_key key; | |
3834 | u64 dir_id; | |
3835 | int ret; | |
3836 | ||
3837 | path = btrfs_alloc_path(); | |
3838 | if (!path) | |
3839 | return -ENOMEM; | |
3840 | ||
3841 | /* Make sure this root isn't set as the default subvol */ | |
3842 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
3843 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
3844 | dir_id, "default", 7, 0); | |
3845 | if (di && !IS_ERR(di)) { | |
3846 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
3847 | if (key.objectid == root->root_key.objectid) { | |
3848 | ret = -EPERM; | |
3849 | btrfs_err(fs_info, | |
3850 | "deleting default subvolume %llu is not allowed", | |
3851 | key.objectid); | |
3852 | goto out; | |
3853 | } | |
3854 | btrfs_release_path(path); | |
3855 | } | |
3856 | ||
3857 | key.objectid = root->root_key.objectid; | |
3858 | key.type = BTRFS_ROOT_REF_KEY; | |
3859 | key.offset = (u64)-1; | |
3860 | ||
3861 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
3862 | if (ret < 0) | |
3863 | goto out; | |
3864 | BUG_ON(ret == 0); | |
3865 | ||
3866 | ret = 0; | |
3867 | if (path->slots[0] > 0) { | |
3868 | path->slots[0]--; | |
3869 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
3870 | if (key.objectid == root->root_key.objectid && | |
3871 | key.type == BTRFS_ROOT_REF_KEY) | |
3872 | ret = -ENOTEMPTY; | |
3873 | } | |
3874 | out: | |
3875 | btrfs_free_path(path); | |
3876 | return ret; | |
3877 | } | |
3878 | ||
20a68004 NB |
3879 | /* Delete all dentries for inodes belonging to the root */ |
3880 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
3881 | { | |
3882 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3883 | struct rb_node *node; | |
3884 | struct rb_node *prev; | |
3885 | struct btrfs_inode *entry; | |
3886 | struct inode *inode; | |
3887 | u64 objectid = 0; | |
3888 | ||
3889 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
3890 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
3891 | ||
3892 | spin_lock(&root->inode_lock); | |
3893 | again: | |
3894 | node = root->inode_tree.rb_node; | |
3895 | prev = NULL; | |
3896 | while (node) { | |
3897 | prev = node; | |
3898 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
3899 | ||
37508515 | 3900 | if (objectid < btrfs_ino(entry)) |
20a68004 | 3901 | node = node->rb_left; |
37508515 | 3902 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
3903 | node = node->rb_right; |
3904 | else | |
3905 | break; | |
3906 | } | |
3907 | if (!node) { | |
3908 | while (prev) { | |
3909 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 3910 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
3911 | node = prev; |
3912 | break; | |
3913 | } | |
3914 | prev = rb_next(prev); | |
3915 | } | |
3916 | } | |
3917 | while (node) { | |
3918 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 3919 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
3920 | inode = igrab(&entry->vfs_inode); |
3921 | if (inode) { | |
3922 | spin_unlock(&root->inode_lock); | |
3923 | if (atomic_read(&inode->i_count) > 1) | |
3924 | d_prune_aliases(inode); | |
3925 | /* | |
3926 | * btrfs_drop_inode will have it removed from the inode | |
3927 | * cache when its usage count hits zero. | |
3928 | */ | |
3929 | iput(inode); | |
3930 | cond_resched(); | |
3931 | spin_lock(&root->inode_lock); | |
3932 | goto again; | |
3933 | } | |
3934 | ||
3935 | if (cond_resched_lock(&root->inode_lock)) | |
3936 | goto again; | |
3937 | ||
3938 | node = rb_next(node); | |
3939 | } | |
3940 | spin_unlock(&root->inode_lock); | |
3941 | } | |
3942 | ||
f60a2364 MT |
3943 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
3944 | { | |
3945 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
3946 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3947 | struct inode *inode = d_inode(dentry); | |
3948 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
3949 | struct btrfs_trans_handle *trans; | |
3950 | struct btrfs_block_rsv block_rsv; | |
3951 | u64 root_flags; | |
f60a2364 MT |
3952 | int ret; |
3953 | int err; | |
3954 | ||
3955 | /* | |
3956 | * Don't allow to delete a subvolume with send in progress. This is | |
3957 | * inside the inode lock so the error handling that has to drop the bit | |
3958 | * again is not run concurrently. | |
3959 | */ | |
3960 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 3961 | if (dest->send_in_progress) { |
f60a2364 MT |
3962 | spin_unlock(&dest->root_item_lock); |
3963 | btrfs_warn(fs_info, | |
3964 | "attempt to delete subvolume %llu during send", | |
3965 | dest->root_key.objectid); | |
3966 | return -EPERM; | |
3967 | } | |
a7176f74 LF |
3968 | root_flags = btrfs_root_flags(&dest->root_item); |
3969 | btrfs_set_root_flags(&dest->root_item, | |
3970 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
3971 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
3972 | |
3973 | down_write(&fs_info->subvol_sem); | |
3974 | ||
3975 | err = may_destroy_subvol(dest); | |
3976 | if (err) | |
3977 | goto out_up_write; | |
3978 | ||
3979 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
3980 | /* | |
3981 | * One for dir inode, | |
3982 | * two for dir entries, | |
3983 | * two for root ref/backref. | |
3984 | */ | |
c4c129db | 3985 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
f60a2364 MT |
3986 | if (err) |
3987 | goto out_up_write; | |
3988 | ||
3989 | trans = btrfs_start_transaction(root, 0); | |
3990 | if (IS_ERR(trans)) { | |
3991 | err = PTR_ERR(trans); | |
3992 | goto out_release; | |
3993 | } | |
3994 | trans->block_rsv = &block_rsv; | |
3995 | trans->bytes_reserved = block_rsv.size; | |
3996 | ||
3997 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
3998 | ||
045d3967 | 3999 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 MT |
4000 | if (ret) { |
4001 | err = ret; | |
4002 | btrfs_abort_transaction(trans, ret); | |
4003 | goto out_end_trans; | |
4004 | } | |
4005 | ||
4006 | btrfs_record_root_in_trans(trans, dest); | |
4007 | ||
4008 | memset(&dest->root_item.drop_progress, 0, | |
4009 | sizeof(dest->root_item.drop_progress)); | |
4010 | dest->root_item.drop_level = 0; | |
4011 | btrfs_set_root_refs(&dest->root_item, 0); | |
4012 | ||
4013 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4014 | ret = btrfs_insert_orphan_item(trans, | |
4015 | fs_info->tree_root, | |
4016 | dest->root_key.objectid); | |
4017 | if (ret) { | |
4018 | btrfs_abort_transaction(trans, ret); | |
4019 | err = ret; | |
4020 | goto out_end_trans; | |
4021 | } | |
4022 | } | |
4023 | ||
d1957791 | 4024 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4025 | BTRFS_UUID_KEY_SUBVOL, |
4026 | dest->root_key.objectid); | |
4027 | if (ret && ret != -ENOENT) { | |
4028 | btrfs_abort_transaction(trans, ret); | |
4029 | err = ret; | |
4030 | goto out_end_trans; | |
4031 | } | |
4032 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4033 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4034 | dest->root_item.received_uuid, |
4035 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4036 | dest->root_key.objectid); | |
4037 | if (ret && ret != -ENOENT) { | |
4038 | btrfs_abort_transaction(trans, ret); | |
4039 | err = ret; | |
4040 | goto out_end_trans; | |
4041 | } | |
4042 | } | |
4043 | ||
082b6c97 QW |
4044 | free_anon_bdev(dest->anon_dev); |
4045 | dest->anon_dev = 0; | |
f60a2364 MT |
4046 | out_end_trans: |
4047 | trans->block_rsv = NULL; | |
4048 | trans->bytes_reserved = 0; | |
4049 | ret = btrfs_end_transaction(trans); | |
4050 | if (ret && !err) | |
4051 | err = ret; | |
4052 | inode->i_flags |= S_DEAD; | |
4053 | out_release: | |
e85fde51 | 4054 | btrfs_subvolume_release_metadata(root, &block_rsv); |
f60a2364 MT |
4055 | out_up_write: |
4056 | up_write(&fs_info->subvol_sem); | |
4057 | if (err) { | |
4058 | spin_lock(&dest->root_item_lock); | |
4059 | root_flags = btrfs_root_flags(&dest->root_item); | |
4060 | btrfs_set_root_flags(&dest->root_item, | |
4061 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4062 | spin_unlock(&dest->root_item_lock); | |
4063 | } else { | |
4064 | d_invalidate(dentry); | |
20a68004 | 4065 | btrfs_prune_dentries(dest); |
f60a2364 MT |
4066 | ASSERT(dest->send_in_progress == 0); |
4067 | ||
4068 | /* the last ref */ | |
4069 | if (dest->ino_cache_inode) { | |
4070 | iput(dest->ino_cache_inode); | |
4071 | dest->ino_cache_inode = NULL; | |
4072 | } | |
4073 | } | |
4074 | ||
4075 | return err; | |
4076 | } | |
4077 | ||
39279cc3 CM |
4078 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4079 | { | |
2b0143b5 | 4080 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4081 | int err = 0; |
39279cc3 | 4082 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4083 | struct btrfs_trans_handle *trans; |
44f714da | 4084 | u64 last_unlink_trans; |
39279cc3 | 4085 | |
b3ae244e | 4086 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4087 | return -ENOTEMPTY; |
4a0cc7ca | 4088 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4089 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4090 | |
d52be818 | 4091 | trans = __unlink_start_trans(dir); |
a22285a6 | 4092 | if (IS_ERR(trans)) |
5df6a9f6 | 4093 | return PTR_ERR(trans); |
5df6a9f6 | 4094 | |
4a0cc7ca | 4095 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 4096 | err = btrfs_unlink_subvol(trans, dir, dentry); |
4df27c4d YZ |
4097 | goto out; |
4098 | } | |
4099 | ||
73f2e545 | 4100 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4101 | if (err) |
4df27c4d | 4102 | goto out; |
7b128766 | 4103 | |
44f714da FM |
4104 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4105 | ||
39279cc3 | 4106 | /* now the directory is empty */ |
4ec5934e NB |
4107 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4108 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4109 | dentry->d_name.len); | |
44f714da | 4110 | if (!err) { |
6ef06d27 | 4111 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4112 | /* |
4113 | * Propagate the last_unlink_trans value of the deleted dir to | |
4114 | * its parent directory. This is to prevent an unrecoverable | |
4115 | * log tree in the case we do something like this: | |
4116 | * 1) create dir foo | |
4117 | * 2) create snapshot under dir foo | |
4118 | * 3) delete the snapshot | |
4119 | * 4) rmdir foo | |
4120 | * 5) mkdir foo | |
4121 | * 6) fsync foo or some file inside foo | |
4122 | */ | |
4123 | if (last_unlink_trans >= trans->transid) | |
4124 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4125 | } | |
4df27c4d | 4126 | out: |
3a45bb20 | 4127 | btrfs_end_transaction(trans); |
2ff7e61e | 4128 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4129 | |
39279cc3 CM |
4130 | return err; |
4131 | } | |
4132 | ||
ddfae63c JB |
4133 | /* |
4134 | * Return this if we need to call truncate_block for the last bit of the | |
4135 | * truncate. | |
4136 | */ | |
4137 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4138 | |
39279cc3 CM |
4139 | /* |
4140 | * this can truncate away extent items, csum items and directory items. | |
4141 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4142 | * any higher than new_size |
39279cc3 CM |
4143 | * |
4144 | * csum items that cross the new i_size are truncated to the new size | |
4145 | * as well. | |
7b128766 JB |
4146 | * |
4147 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4148 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4149 | */ |
8082510e YZ |
4150 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4151 | struct btrfs_root *root, | |
4152 | struct inode *inode, | |
4153 | u64 new_size, u32 min_type) | |
39279cc3 | 4154 | { |
0b246afa | 4155 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4156 | struct btrfs_path *path; |
5f39d397 | 4157 | struct extent_buffer *leaf; |
39279cc3 | 4158 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4159 | struct btrfs_key key; |
4160 | struct btrfs_key found_key; | |
39279cc3 | 4161 | u64 extent_start = 0; |
db94535d | 4162 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4163 | u64 extent_offset = 0; |
39279cc3 | 4164 | u64 item_end = 0; |
c1aa4575 | 4165 | u64 last_size = new_size; |
8082510e | 4166 | u32 found_type = (u8)-1; |
39279cc3 CM |
4167 | int found_extent; |
4168 | int del_item; | |
85e21bac CM |
4169 | int pending_del_nr = 0; |
4170 | int pending_del_slot = 0; | |
179e29e4 | 4171 | int extent_type = -1; |
8082510e | 4172 | int ret; |
4a0cc7ca | 4173 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4174 | u64 bytes_deleted = 0; |
897ca819 TM |
4175 | bool be_nice = false; |
4176 | bool should_throttle = false; | |
28553fa9 FM |
4177 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
4178 | struct extent_state *cached_state = NULL; | |
8082510e YZ |
4179 | |
4180 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4181 | |
28ed1345 | 4182 | /* |
92a7cc42 QW |
4183 | * For non-free space inodes and non-shareable roots, we want to back |
4184 | * off from time to time. This means all inodes in subvolume roots, | |
4185 | * reloc roots, and data reloc roots. | |
28ed1345 | 4186 | */ |
70ddc553 | 4187 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
92a7cc42 | 4188 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
897ca819 | 4189 | be_nice = true; |
28ed1345 | 4190 | |
0eb0e19c MF |
4191 | path = btrfs_alloc_path(); |
4192 | if (!path) | |
4193 | return -ENOMEM; | |
e4058b54 | 4194 | path->reada = READA_BACK; |
0eb0e19c | 4195 | |
82028e0a | 4196 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
a5ae50de FM |
4197 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
4198 | &cached_state); | |
28553fa9 | 4199 | |
82028e0a QW |
4200 | /* |
4201 | * We want to drop from the next block forward in case this | |
4202 | * new size is not block aligned since we will be keeping the | |
4203 | * last block of the extent just the way it is. | |
4204 | */ | |
dcdbc059 | 4205 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4206 | fs_info->sectorsize), |
da17066c | 4207 | (u64)-1, 0); |
82028e0a | 4208 | } |
8082510e | 4209 | |
16cdcec7 MX |
4210 | /* |
4211 | * This function is also used to drop the items in the log tree before | |
4212 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
52042d8e | 4213 | * it is used to drop the logged items. So we shouldn't kill the delayed |
16cdcec7 MX |
4214 | * items. |
4215 | */ | |
4216 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4217 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4218 | |
33345d01 | 4219 | key.objectid = ino; |
39279cc3 | 4220 | key.offset = (u64)-1; |
5f39d397 CM |
4221 | key.type = (u8)-1; |
4222 | ||
85e21bac | 4223 | search_again: |
28ed1345 CM |
4224 | /* |
4225 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4226 | * up a huge file in a single leaf. Most of the time that | |
4227 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4228 | */ | |
fd86a3a3 OS |
4229 | if (be_nice && bytes_deleted > SZ_32M && |
4230 | btrfs_should_end_transaction(trans)) { | |
4231 | ret = -EAGAIN; | |
4232 | goto out; | |
28ed1345 CM |
4233 | } |
4234 | ||
85e21bac | 4235 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
fd86a3a3 | 4236 | if (ret < 0) |
8082510e | 4237 | goto out; |
d397712b | 4238 | |
85e21bac | 4239 | if (ret > 0) { |
fd86a3a3 | 4240 | ret = 0; |
e02119d5 CM |
4241 | /* there are no items in the tree for us to truncate, we're |
4242 | * done | |
4243 | */ | |
8082510e YZ |
4244 | if (path->slots[0] == 0) |
4245 | goto out; | |
85e21bac CM |
4246 | path->slots[0]--; |
4247 | } | |
4248 | ||
d397712b | 4249 | while (1) { |
9ddc959e JB |
4250 | u64 clear_start = 0, clear_len = 0; |
4251 | ||
39279cc3 | 4252 | fi = NULL; |
5f39d397 CM |
4253 | leaf = path->nodes[0]; |
4254 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4255 | found_type = found_key.type; |
39279cc3 | 4256 | |
33345d01 | 4257 | if (found_key.objectid != ino) |
39279cc3 | 4258 | break; |
5f39d397 | 4259 | |
85e21bac | 4260 | if (found_type < min_type) |
39279cc3 CM |
4261 | break; |
4262 | ||
5f39d397 | 4263 | item_end = found_key.offset; |
39279cc3 | 4264 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4265 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4266 | struct btrfs_file_extent_item); |
179e29e4 CM |
4267 | extent_type = btrfs_file_extent_type(leaf, fi); |
4268 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4269 | item_end += |
db94535d | 4270 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4271 | |
4272 | trace_btrfs_truncate_show_fi_regular( | |
4273 | BTRFS_I(inode), leaf, fi, | |
4274 | found_key.offset); | |
179e29e4 | 4275 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 QW |
4276 | item_end += btrfs_file_extent_ram_bytes(leaf, |
4277 | fi); | |
09ed2f16 LB |
4278 | |
4279 | trace_btrfs_truncate_show_fi_inline( | |
4280 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4281 | found_key.offset); | |
39279cc3 | 4282 | } |
008630c1 | 4283 | item_end--; |
39279cc3 | 4284 | } |
8082510e YZ |
4285 | if (found_type > min_type) { |
4286 | del_item = 1; | |
4287 | } else { | |
76b42abb | 4288 | if (item_end < new_size) |
b888db2b | 4289 | break; |
8082510e YZ |
4290 | if (found_key.offset >= new_size) |
4291 | del_item = 1; | |
4292 | else | |
4293 | del_item = 0; | |
39279cc3 | 4294 | } |
39279cc3 | 4295 | found_extent = 0; |
39279cc3 | 4296 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4297 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4298 | goto delete; | |
4299 | ||
4300 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4301 | u64 num_dec; |
9ddc959e JB |
4302 | |
4303 | clear_start = found_key.offset; | |
db94535d | 4304 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4305 | if (!del_item) { |
db94535d CM |
4306 | u64 orig_num_bytes = |
4307 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4308 | extent_num_bytes = ALIGN(new_size - |
4309 | found_key.offset, | |
0b246afa | 4310 | fs_info->sectorsize); |
9ddc959e | 4311 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
db94535d CM |
4312 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4313 | extent_num_bytes); | |
4314 | num_dec = (orig_num_bytes - | |
9069218d | 4315 | extent_num_bytes); |
92a7cc42 | 4316 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 MX |
4317 | &root->state) && |
4318 | extent_start != 0) | |
a76a3cd4 | 4319 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4320 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4321 | } else { |
db94535d CM |
4322 | extent_num_bytes = |
4323 | btrfs_file_extent_disk_num_bytes(leaf, | |
4324 | fi); | |
5d4f98a2 YZ |
4325 | extent_offset = found_key.offset - |
4326 | btrfs_file_extent_offset(leaf, fi); | |
4327 | ||
39279cc3 | 4328 | /* FIXME blocksize != 4096 */ |
9069218d | 4329 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4330 | if (extent_start != 0) { |
4331 | found_extent = 1; | |
92a7cc42 | 4332 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 | 4333 | &root->state)) |
a76a3cd4 | 4334 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4335 | } |
39279cc3 | 4336 | } |
9ddc959e | 4337 | clear_len = num_dec; |
9069218d | 4338 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4339 | /* |
4340 | * we can't truncate inline items that have had | |
4341 | * special encodings | |
4342 | */ | |
4343 | if (!del_item && | |
c8b97818 | 4344 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4345 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4346 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4347 | u32 size = (u32)(new_size - found_key.offset); | |
4348 | ||
4349 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4350 | size = btrfs_file_extent_calc_inline_size(size); | |
78ac4f9e | 4351 | btrfs_truncate_item(path, size, 1); |
ddfae63c | 4352 | } else if (!del_item) { |
514ac8ad | 4353 | /* |
ddfae63c JB |
4354 | * We have to bail so the last_size is set to |
4355 | * just before this extent. | |
514ac8ad | 4356 | */ |
fd86a3a3 | 4357 | ret = NEED_TRUNCATE_BLOCK; |
ddfae63c | 4358 | break; |
9ddc959e JB |
4359 | } else { |
4360 | /* | |
4361 | * Inline extents are special, we just treat | |
4362 | * them as a full sector worth in the file | |
4363 | * extent tree just for simplicity sake. | |
4364 | */ | |
4365 | clear_len = fs_info->sectorsize; | |
ddfae63c | 4366 | } |
0305cd5f | 4367 | |
92a7cc42 | 4368 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
0305cd5f | 4369 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4370 | } |
179e29e4 | 4371 | delete: |
9ddc959e JB |
4372 | /* |
4373 | * We use btrfs_truncate_inode_items() to clean up log trees for | |
4374 | * multiple fsyncs, and in this case we don't want to clear the | |
4375 | * file extent range because it's just the log. | |
4376 | */ | |
4377 | if (root == BTRFS_I(inode)->root) { | |
4378 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), | |
4379 | clear_start, clear_len); | |
4380 | if (ret) { | |
4381 | btrfs_abort_transaction(trans, ret); | |
4382 | break; | |
4383 | } | |
4384 | } | |
4385 | ||
ddfae63c JB |
4386 | if (del_item) |
4387 | last_size = found_key.offset; | |
4388 | else | |
4389 | last_size = new_size; | |
39279cc3 | 4390 | if (del_item) { |
85e21bac CM |
4391 | if (!pending_del_nr) { |
4392 | /* no pending yet, add ourselves */ | |
4393 | pending_del_slot = path->slots[0]; | |
4394 | pending_del_nr = 1; | |
4395 | } else if (pending_del_nr && | |
4396 | path->slots[0] + 1 == pending_del_slot) { | |
4397 | /* hop on the pending chunk */ | |
4398 | pending_del_nr++; | |
4399 | pending_del_slot = path->slots[0]; | |
4400 | } else { | |
d397712b | 4401 | BUG(); |
85e21bac | 4402 | } |
39279cc3 CM |
4403 | } else { |
4404 | break; | |
4405 | } | |
897ca819 | 4406 | should_throttle = false; |
28f75a0e | 4407 | |
27cdeb70 | 4408 | if (found_extent && |
82028e0a | 4409 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
ffd4bb2a QW |
4410 | struct btrfs_ref ref = { 0 }; |
4411 | ||
28ed1345 | 4412 | bytes_deleted += extent_num_bytes; |
ffd4bb2a QW |
4413 | |
4414 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, | |
4415 | extent_start, extent_num_bytes, 0); | |
4416 | ref.real_root = root->root_key.objectid; | |
4417 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
4418 | ino, extent_offset); | |
4419 | ret = btrfs_free_extent(trans, &ref); | |
05522109 OS |
4420 | if (ret) { |
4421 | btrfs_abort_transaction(trans, ret); | |
4422 | break; | |
4423 | } | |
28f75a0e | 4424 | if (be_nice) { |
7c861627 | 4425 | if (btrfs_should_throttle_delayed_refs(trans)) |
897ca819 | 4426 | should_throttle = true; |
28f75a0e | 4427 | } |
39279cc3 | 4428 | } |
85e21bac | 4429 | |
8082510e YZ |
4430 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4431 | break; | |
4432 | ||
4433 | if (path->slots[0] == 0 || | |
1262133b | 4434 | path->slots[0] != pending_del_slot || |
28bad212 | 4435 | should_throttle) { |
8082510e YZ |
4436 | if (pending_del_nr) { |
4437 | ret = btrfs_del_items(trans, root, path, | |
4438 | pending_del_slot, | |
4439 | pending_del_nr); | |
79787eaa | 4440 | if (ret) { |
66642832 | 4441 | btrfs_abort_transaction(trans, ret); |
fd86a3a3 | 4442 | break; |
79787eaa | 4443 | } |
8082510e YZ |
4444 | pending_del_nr = 0; |
4445 | } | |
b3b4aa74 | 4446 | btrfs_release_path(path); |
28bad212 | 4447 | |
28f75a0e | 4448 | /* |
28bad212 JB |
4449 | * We can generate a lot of delayed refs, so we need to |
4450 | * throttle every once and a while and make sure we're | |
4451 | * adding enough space to keep up with the work we are | |
4452 | * generating. Since we hold a transaction here we | |
4453 | * can't flush, and we don't want to FLUSH_LIMIT because | |
4454 | * we could have generated too many delayed refs to | |
4455 | * actually allocate, so just bail if we're short and | |
4456 | * let the normal reservation dance happen higher up. | |
28f75a0e | 4457 | */ |
28bad212 JB |
4458 | if (should_throttle) { |
4459 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
4460 | BTRFS_RESERVE_NO_FLUSH); | |
4461 | if (ret) { | |
4462 | ret = -EAGAIN; | |
4463 | break; | |
4464 | } | |
28f75a0e | 4465 | } |
85e21bac | 4466 | goto search_again; |
8082510e YZ |
4467 | } else { |
4468 | path->slots[0]--; | |
85e21bac | 4469 | } |
39279cc3 | 4470 | } |
8082510e | 4471 | out: |
fd86a3a3 OS |
4472 | if (ret >= 0 && pending_del_nr) { |
4473 | int err; | |
4474 | ||
4475 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
85e21bac | 4476 | pending_del_nr); |
fd86a3a3 OS |
4477 | if (err) { |
4478 | btrfs_abort_transaction(trans, err); | |
4479 | ret = err; | |
4480 | } | |
85e21bac | 4481 | } |
76b42abb FM |
4482 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4483 | ASSERT(last_size >= new_size); | |
fd86a3a3 | 4484 | if (!ret && last_size > new_size) |
76b42abb | 4485 | last_size = new_size; |
d923afe9 | 4486 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
a5ae50de FM |
4487 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
4488 | (u64)-1, &cached_state); | |
76b42abb | 4489 | } |
28ed1345 | 4490 | |
39279cc3 | 4491 | btrfs_free_path(path); |
fd86a3a3 | 4492 | return ret; |
39279cc3 CM |
4493 | } |
4494 | ||
4495 | /* | |
9703fefe | 4496 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4497 | * @inode - inode that we're zeroing |
4498 | * @from - the offset to start zeroing | |
4499 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4500 | * offset | |
4501 | * @front - zero up to the offset instead of from the offset on | |
4502 | * | |
9703fefe | 4503 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4504 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4505 | */ |
9703fefe | 4506 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4507 | int front) |
39279cc3 | 4508 | { |
0b246afa | 4509 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4510 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4511 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4512 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4513 | struct extent_state *cached_state = NULL; |
364ecf36 | 4514 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4515 | char *kaddr; |
6d4572a9 | 4516 | bool only_release_metadata = false; |
0b246afa | 4517 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4518 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4519 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4520 | struct page *page; |
3b16a4e3 | 4521 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4522 | size_t write_bytes = blocksize; |
39279cc3 | 4523 | int ret = 0; |
9703fefe CR |
4524 | u64 block_start; |
4525 | u64 block_end; | |
39279cc3 | 4526 | |
b03ebd99 NB |
4527 | if (IS_ALIGNED(offset, blocksize) && |
4528 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4529 | goto out; |
9703fefe | 4530 | |
8b62f87b JB |
4531 | block_start = round_down(from, blocksize); |
4532 | block_end = block_start + blocksize - 1; | |
4533 | ||
36ea6f3e NB |
4534 | ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, |
4535 | block_start, blocksize); | |
6d4572a9 | 4536 | if (ret < 0) { |
38d37aa9 QW |
4537 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
4538 | &write_bytes) > 0) { | |
6d4572a9 QW |
4539 | /* For nocow case, no need to reserve data space */ |
4540 | only_release_metadata = true; | |
4541 | } else { | |
4542 | goto out; | |
4543 | } | |
4544 | } | |
4545 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); | |
4546 | if (ret < 0) { | |
4547 | if (!only_release_metadata) | |
25ce28ca NB |
4548 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
4549 | data_reserved, block_start, blocksize); | |
6d4572a9 QW |
4550 | goto out; |
4551 | } | |
211c17f5 | 4552 | again: |
3b16a4e3 | 4553 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4554 | if (!page) { |
86d52921 | 4555 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
43b18595 | 4556 | block_start, blocksize, true); |
8702ba93 | 4557 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
ac6a2b36 | 4558 | ret = -ENOMEM; |
39279cc3 | 4559 | goto out; |
5d5e103a | 4560 | } |
e6dcd2dc | 4561 | |
39279cc3 | 4562 | if (!PageUptodate(page)) { |
9ebefb18 | 4563 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4564 | lock_page(page); |
211c17f5 CM |
4565 | if (page->mapping != mapping) { |
4566 | unlock_page(page); | |
09cbfeaf | 4567 | put_page(page); |
211c17f5 CM |
4568 | goto again; |
4569 | } | |
39279cc3 CM |
4570 | if (!PageUptodate(page)) { |
4571 | ret = -EIO; | |
89642229 | 4572 | goto out_unlock; |
39279cc3 CM |
4573 | } |
4574 | } | |
211c17f5 | 4575 | wait_on_page_writeback(page); |
e6dcd2dc | 4576 | |
9703fefe | 4577 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4578 | set_page_extent_mapped(page); |
4579 | ||
c3504372 | 4580 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
e6dcd2dc | 4581 | if (ordered) { |
9703fefe | 4582 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4583 | &cached_state); |
e6dcd2dc | 4584 | unlock_page(page); |
09cbfeaf | 4585 | put_page(page); |
eb84ae03 | 4586 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4587 | btrfs_put_ordered_extent(ordered); |
4588 | goto again; | |
4589 | } | |
4590 | ||
9703fefe | 4591 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
e182163d OS |
4592 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
4593 | 0, 0, &cached_state); | |
5d5e103a | 4594 | |
c2566f22 | 4595 | ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0, |
330a5827 | 4596 | &cached_state); |
9ed74f2d | 4597 | if (ret) { |
9703fefe | 4598 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4599 | &cached_state); |
9ed74f2d JB |
4600 | goto out_unlock; |
4601 | } | |
4602 | ||
9703fefe | 4603 | if (offset != blocksize) { |
2aaa6655 | 4604 | if (!len) |
9703fefe | 4605 | len = blocksize - offset; |
e6dcd2dc | 4606 | kaddr = kmap(page); |
2aaa6655 | 4607 | if (front) |
9703fefe CR |
4608 | memset(kaddr + (block_start - page_offset(page)), |
4609 | 0, offset); | |
2aaa6655 | 4610 | else |
9703fefe CR |
4611 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4612 | 0, len); | |
e6dcd2dc CM |
4613 | flush_dcache_page(page); |
4614 | kunmap(page); | |
4615 | } | |
247e743c | 4616 | ClearPageChecked(page); |
e6dcd2dc | 4617 | set_page_dirty(page); |
e43bbe5e | 4618 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4619 | |
6d4572a9 QW |
4620 | if (only_release_metadata) |
4621 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, | |
4622 | block_end, EXTENT_NORESERVE, NULL, NULL, | |
4623 | GFP_NOFS); | |
4624 | ||
89642229 | 4625 | out_unlock: |
6d4572a9 QW |
4626 | if (ret) { |
4627 | if (only_release_metadata) | |
4628 | btrfs_delalloc_release_metadata(BTRFS_I(inode), | |
4629 | blocksize, true); | |
4630 | else | |
86d52921 | 4631 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
6d4572a9 QW |
4632 | block_start, blocksize, true); |
4633 | } | |
8702ba93 | 4634 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
39279cc3 | 4635 | unlock_page(page); |
09cbfeaf | 4636 | put_page(page); |
39279cc3 | 4637 | out: |
6d4572a9 | 4638 | if (only_release_metadata) |
38d37aa9 | 4639 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
364ecf36 | 4640 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4641 | return ret; |
4642 | } | |
4643 | ||
16e7549f JB |
4644 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4645 | u64 offset, u64 len) | |
4646 | { | |
0b246afa | 4647 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4648 | struct btrfs_trans_handle *trans; |
4649 | int ret; | |
4650 | ||
4651 | /* | |
4652 | * Still need to make sure the inode looks like it's been updated so | |
4653 | * that any holes get logged if we fsync. | |
4654 | */ | |
0b246afa JM |
4655 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4656 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4657 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4658 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4659 | return 0; | |
4660 | } | |
4661 | ||
4662 | /* | |
4663 | * 1 - for the one we're dropping | |
4664 | * 1 - for the one we're adding | |
4665 | * 1 - for updating the inode. | |
4666 | */ | |
4667 | trans = btrfs_start_transaction(root, 3); | |
4668 | if (IS_ERR(trans)) | |
4669 | return PTR_ERR(trans); | |
4670 | ||
4671 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4672 | if (ret) { | |
66642832 | 4673 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4674 | btrfs_end_transaction(trans); |
16e7549f JB |
4675 | return ret; |
4676 | } | |
4677 | ||
f85b7379 DS |
4678 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4679 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4680 | if (ret) |
66642832 | 4681 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4682 | else |
4683 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4684 | btrfs_end_transaction(trans); |
16e7549f JB |
4685 | return ret; |
4686 | } | |
4687 | ||
695a0d0d JB |
4688 | /* |
4689 | * This function puts in dummy file extents for the area we're creating a hole | |
4690 | * for. So if we are truncating this file to a larger size we need to insert | |
4691 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4692 | * the range between oldsize and size | |
4693 | */ | |
a41ad394 | 4694 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4695 | { |
0b246afa | 4696 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4697 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4698 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4699 | struct extent_map *em = NULL; |
2ac55d41 | 4700 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4701 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4702 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4703 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4704 | u64 last_byte; |
4705 | u64 cur_offset; | |
4706 | u64 hole_size; | |
9ed74f2d | 4707 | int err = 0; |
39279cc3 | 4708 | |
a71754fc | 4709 | /* |
9703fefe CR |
4710 | * If our size started in the middle of a block we need to zero out the |
4711 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4712 | * expose stale data. |
4713 | */ | |
9703fefe | 4714 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4715 | if (err) |
4716 | return err; | |
4717 | ||
9036c102 YZ |
4718 | if (size <= hole_start) |
4719 | return 0; | |
4720 | ||
b272ae22 | 4721 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
23d31bd4 | 4722 | block_end - 1, &cached_state); |
9036c102 YZ |
4723 | cur_offset = hole_start; |
4724 | while (1) { | |
fc4f21b1 | 4725 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
39b07b5d | 4726 | block_end - cur_offset); |
79787eaa JM |
4727 | if (IS_ERR(em)) { |
4728 | err = PTR_ERR(em); | |
f2767956 | 4729 | em = NULL; |
79787eaa JM |
4730 | break; |
4731 | } | |
9036c102 | 4732 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4733 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4734 | hole_size = last_byte - cur_offset; |
4735 | ||
8082510e | 4736 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4737 | struct extent_map *hole_em; |
9ed74f2d | 4738 | |
16e7549f JB |
4739 | err = maybe_insert_hole(root, inode, cur_offset, |
4740 | hole_size); | |
4741 | if (err) | |
3893e33b | 4742 | break; |
9ddc959e JB |
4743 | |
4744 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4745 | cur_offset, hole_size); | |
4746 | if (err) | |
4747 | break; | |
4748 | ||
dcdbc059 | 4749 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4750 | cur_offset + hole_size - 1, 0); |
4751 | hole_em = alloc_extent_map(); | |
4752 | if (!hole_em) { | |
4753 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4754 | &BTRFS_I(inode)->runtime_flags); | |
4755 | goto next; | |
4756 | } | |
4757 | hole_em->start = cur_offset; | |
4758 | hole_em->len = hole_size; | |
4759 | hole_em->orig_start = cur_offset; | |
8082510e | 4760 | |
5dc562c5 JB |
4761 | hole_em->block_start = EXTENT_MAP_HOLE; |
4762 | hole_em->block_len = 0; | |
b4939680 | 4763 | hole_em->orig_block_len = 0; |
cc95bef6 | 4764 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 4765 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4766 | hole_em->generation = fs_info->generation; |
8082510e | 4767 | |
5dc562c5 JB |
4768 | while (1) { |
4769 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4770 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4771 | write_unlock(&em_tree->lock); |
4772 | if (err != -EEXIST) | |
4773 | break; | |
dcdbc059 NB |
4774 | btrfs_drop_extent_cache(BTRFS_I(inode), |
4775 | cur_offset, | |
5dc562c5 JB |
4776 | cur_offset + |
4777 | hole_size - 1, 0); | |
4778 | } | |
4779 | free_extent_map(hole_em); | |
9ddc959e JB |
4780 | } else { |
4781 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4782 | cur_offset, hole_size); | |
4783 | if (err) | |
4784 | break; | |
9036c102 | 4785 | } |
16e7549f | 4786 | next: |
9036c102 | 4787 | free_extent_map(em); |
a22285a6 | 4788 | em = NULL; |
9036c102 | 4789 | cur_offset = last_byte; |
8082510e | 4790 | if (cur_offset >= block_end) |
9036c102 YZ |
4791 | break; |
4792 | } | |
a22285a6 | 4793 | free_extent_map(em); |
e43bbe5e | 4794 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4795 | return err; |
4796 | } | |
39279cc3 | 4797 | |
3972f260 | 4798 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4799 | { |
f4a2f4c5 MX |
4800 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4801 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4802 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4803 | loff_t newsize = attr->ia_size; |
4804 | int mask = attr->ia_valid; | |
8082510e YZ |
4805 | int ret; |
4806 | ||
3972f260 ES |
4807 | /* |
4808 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4809 | * special case where we need to update the times despite not having | |
4810 | * these flags set. For all other operations the VFS set these flags | |
4811 | * explicitly if it wants a timestamp update. | |
4812 | */ | |
dff6efc3 CH |
4813 | if (newsize != oldsize) { |
4814 | inode_inc_iversion(inode); | |
4815 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4816 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 4817 | current_time(inode); |
dff6efc3 | 4818 | } |
3972f260 | 4819 | |
a41ad394 | 4820 | if (newsize > oldsize) { |
9ea24bbe | 4821 | /* |
ea14b57f | 4822 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
4823 | * This is to ensure the snapshot captures a fully consistent |
4824 | * state of this file - if the snapshot captures this expanding | |
4825 | * truncation, it must capture all writes that happened before | |
4826 | * this truncation. | |
4827 | */ | |
dcc3eb96 | 4828 | btrfs_drew_write_lock(&root->snapshot_lock); |
a41ad394 | 4829 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 4830 | if (ret) { |
dcc3eb96 | 4831 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 4832 | return ret; |
9ea24bbe | 4833 | } |
8082510e | 4834 | |
f4a2f4c5 | 4835 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 4836 | if (IS_ERR(trans)) { |
dcc3eb96 | 4837 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 4838 | return PTR_ERR(trans); |
9ea24bbe | 4839 | } |
f4a2f4c5 MX |
4840 | |
4841 | i_size_write(inode, newsize); | |
d923afe9 | 4842 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
27772b68 | 4843 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 4844 | ret = btrfs_update_inode(trans, root, inode); |
dcc3eb96 | 4845 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 4846 | btrfs_end_transaction(trans); |
a41ad394 | 4847 | } else { |
8082510e | 4848 | |
a41ad394 JB |
4849 | /* |
4850 | * We're truncating a file that used to have good data down to | |
4851 | * zero. Make sure it gets into the ordered flush list so that | |
4852 | * any new writes get down to disk quickly. | |
4853 | */ | |
4854 | if (newsize == 0) | |
72ac3c0d JB |
4855 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4856 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4857 | |
a41ad394 | 4858 | truncate_setsize(inode, newsize); |
2e60a51e | 4859 | |
8e0fa5d7 DS |
4860 | /* Disable nonlocked read DIO to avoid the endless truncate */ |
4861 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); | |
2e60a51e | 4862 | inode_dio_wait(inode); |
8e0fa5d7 | 4863 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 4864 | |
213e8c55 | 4865 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
4866 | if (ret && inode->i_nlink) { |
4867 | int err; | |
4868 | ||
4869 | /* | |
f7e9e8fc OS |
4870 | * Truncate failed, so fix up the in-memory size. We |
4871 | * adjusted disk_i_size down as we removed extents, so | |
4872 | * wait for disk_i_size to be stable and then update the | |
4873 | * in-memory size to match. | |
7f4f6e0a | 4874 | */ |
f7e9e8fc | 4875 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 4876 | if (err) |
f7e9e8fc OS |
4877 | return err; |
4878 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 4879 | } |
8082510e YZ |
4880 | } |
4881 | ||
a41ad394 | 4882 | return ret; |
8082510e YZ |
4883 | } |
4884 | ||
9036c102 YZ |
4885 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4886 | { | |
2b0143b5 | 4887 | struct inode *inode = d_inode(dentry); |
b83cc969 | 4888 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4889 | int err; |
39279cc3 | 4890 | |
b83cc969 LZ |
4891 | if (btrfs_root_readonly(root)) |
4892 | return -EROFS; | |
4893 | ||
31051c85 | 4894 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
4895 | if (err) |
4896 | return err; | |
2bf5a725 | 4897 | |
5a3f23d5 | 4898 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4899 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4900 | if (err) |
4901 | return err; | |
39279cc3 | 4902 | } |
9036c102 | 4903 | |
1025774c CH |
4904 | if (attr->ia_valid) { |
4905 | setattr_copy(inode, attr); | |
0c4d2d95 | 4906 | inode_inc_iversion(inode); |
22c44fe6 | 4907 | err = btrfs_dirty_inode(inode); |
1025774c | 4908 | |
22c44fe6 | 4909 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 4910 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 4911 | } |
33268eaf | 4912 | |
39279cc3 CM |
4913 | return err; |
4914 | } | |
61295eb8 | 4915 | |
131e404a FDBM |
4916 | /* |
4917 | * While truncating the inode pages during eviction, we get the VFS calling | |
4918 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4919 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4920 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4921 | * extent_state structures over and over, wasting lots of time. | |
4922 | * | |
4923 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4924 | * those expensive operations on a per page basis and do only the ordered io | |
4925 | * finishing, while we release here the extent_map and extent_state structures, | |
4926 | * without the excessive merging and splitting. | |
4927 | */ | |
4928 | static void evict_inode_truncate_pages(struct inode *inode) | |
4929 | { | |
4930 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4931 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4932 | struct rb_node *node; | |
4933 | ||
4934 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 4935 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
4936 | |
4937 | write_lock(&map_tree->lock); | |
07e1ce09 | 4938 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
4939 | struct extent_map *em; |
4940 | ||
07e1ce09 | 4941 | node = rb_first_cached(&map_tree->map); |
131e404a | 4942 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
4943 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
4944 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
4945 | remove_extent_mapping(map_tree, em); |
4946 | free_extent_map(em); | |
7064dd5c FM |
4947 | if (need_resched()) { |
4948 | write_unlock(&map_tree->lock); | |
4949 | cond_resched(); | |
4950 | write_lock(&map_tree->lock); | |
4951 | } | |
131e404a FDBM |
4952 | } |
4953 | write_unlock(&map_tree->lock); | |
4954 | ||
6ca07097 FM |
4955 | /* |
4956 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
4957 | * We can have ongoing bios started by readahead that have |
4958 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
4959 | * still in progress (unlocked the pages in the bio but did not yet |
4960 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
4961 | * ranges can still be locked and eviction started because before |
4962 | * submitting those bios, which are executed by a separate task (work | |
4963 | * queue kthread), inode references (inode->i_count) were not taken | |
4964 | * (which would be dropped in the end io callback of each bio). | |
4965 | * Therefore here we effectively end up waiting for those bios and | |
4966 | * anyone else holding locked ranges without having bumped the inode's | |
4967 | * reference count - if we don't do it, when they access the inode's | |
4968 | * io_tree to unlock a range it may be too late, leading to an | |
4969 | * use-after-free issue. | |
4970 | */ | |
131e404a FDBM |
4971 | spin_lock(&io_tree->lock); |
4972 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
4973 | struct extent_state *state; | |
4974 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
4975 | u64 start; |
4976 | u64 end; | |
421f0922 | 4977 | unsigned state_flags; |
131e404a FDBM |
4978 | |
4979 | node = rb_first(&io_tree->state); | |
4980 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
4981 | start = state->start; |
4982 | end = state->end; | |
421f0922 | 4983 | state_flags = state->state; |
131e404a FDBM |
4984 | spin_unlock(&io_tree->lock); |
4985 | ||
ff13db41 | 4986 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
4987 | |
4988 | /* | |
4989 | * If still has DELALLOC flag, the extent didn't reach disk, | |
4990 | * and its reserved space won't be freed by delayed_ref. | |
4991 | * So we need to free its reserved space here. | |
4992 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
4993 | * | |
4994 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
4995 | */ | |
421f0922 | 4996 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f NB |
4997 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
4998 | end - start + 1); | |
b9d0b389 | 4999 | |
6ca07097 | 5000 | clear_extent_bit(io_tree, start, end, |
e182163d OS |
5001 | EXTENT_LOCKED | EXTENT_DELALLOC | |
5002 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
5003 | &cached_state); | |
131e404a | 5004 | |
7064dd5c | 5005 | cond_resched(); |
131e404a FDBM |
5006 | spin_lock(&io_tree->lock); |
5007 | } | |
5008 | spin_unlock(&io_tree->lock); | |
5009 | } | |
5010 | ||
4b9d7b59 | 5011 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5012 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5013 | { |
5014 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5015 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
d3984c90 | 5016 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5017 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5018 | int ret; |
4b9d7b59 | 5019 | |
d3984c90 JB |
5020 | /* |
5021 | * Eviction should be taking place at some place safe because of our | |
5022 | * delayed iputs. However the normal flushing code will run delayed | |
5023 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5024 | * | |
5025 | * We reserve the delayed_refs_extra here again because we can't use | |
5026 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5027 | * above. We reserve our extra bit here because we generate a ton of | |
5028 | * delayed refs activity by truncating. | |
5029 | * | |
5030 | * If we cannot make our reservation we'll attempt to steal from the | |
5031 | * global reserve, because we really want to be able to free up space. | |
5032 | */ | |
5033 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, | |
5034 | BTRFS_RESERVE_FLUSH_EVICT); | |
5035 | if (ret) { | |
4b9d7b59 OS |
5036 | /* |
5037 | * Try to steal from the global reserve if there is space for | |
5038 | * it. | |
5039 | */ | |
d3984c90 JB |
5040 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
5041 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { | |
5042 | btrfs_warn(fs_info, | |
5043 | "could not allocate space for delete; will truncate on mount"); | |
5044 | return ERR_PTR(-ENOSPC); | |
5045 | } | |
5046 | delayed_refs_extra = 0; | |
5047 | } | |
4b9d7b59 | 5048 | |
d3984c90 JB |
5049 | trans = btrfs_join_transaction(root); |
5050 | if (IS_ERR(trans)) | |
5051 | return trans; | |
5052 | ||
5053 | if (delayed_refs_extra) { | |
5054 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5055 | trans->bytes_reserved = delayed_refs_extra; | |
5056 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5057 | delayed_refs_extra, 1); | |
4b9d7b59 | 5058 | } |
d3984c90 | 5059 | return trans; |
4b9d7b59 OS |
5060 | } |
5061 | ||
bd555975 | 5062 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5063 | { |
0b246afa | 5064 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5065 | struct btrfs_trans_handle *trans; |
5066 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5067 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5068 | int ret; |
5069 | ||
1abe9b8a | 5070 | trace_btrfs_inode_evict(inode); |
5071 | ||
3d48d981 | 5072 | if (!root) { |
e8f1bc14 | 5073 | clear_inode(inode); |
3d48d981 NB |
5074 | return; |
5075 | } | |
5076 | ||
131e404a FDBM |
5077 | evict_inode_truncate_pages(inode); |
5078 | ||
69e9c6c6 SB |
5079 | if (inode->i_nlink && |
5080 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5081 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5082 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5083 | goto no_delete; |
5084 | ||
27919067 | 5085 | if (is_bad_inode(inode)) |
39279cc3 | 5086 | goto no_delete; |
5f39d397 | 5087 | |
7ab7956e | 5088 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5089 | |
7b40b695 | 5090 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5091 | goto no_delete; |
c71bf099 | 5092 | |
76dda93c | 5093 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5094 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5095 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5096 | goto no_delete; |
5097 | } | |
5098 | ||
aa79021f | 5099 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5100 | if (ret) |
0e8c36a9 | 5101 | goto no_delete; |
0e8c36a9 | 5102 | |
2ff7e61e | 5103 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5104 | if (!rsv) |
4289a667 | 5105 | goto no_delete; |
2bd36e7b | 5106 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
ca7e70f5 | 5107 | rsv->failfast = 1; |
4289a667 | 5108 | |
6ef06d27 | 5109 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5110 | |
8082510e | 5111 | while (1) { |
ad80cf50 | 5112 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5113 | if (IS_ERR(trans)) |
5114 | goto free_rsv; | |
7b128766 | 5115 | |
4289a667 JB |
5116 | trans->block_rsv = rsv; |
5117 | ||
d68fc57b | 5118 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
27919067 OS |
5119 | trans->block_rsv = &fs_info->trans_block_rsv; |
5120 | btrfs_end_transaction(trans); | |
5121 | btrfs_btree_balance_dirty(fs_info); | |
5122 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5123 | goto free_rsv; | |
5124 | else if (!ret) | |
8082510e | 5125 | break; |
8082510e | 5126 | } |
5f39d397 | 5127 | |
4ef31a45 | 5128 | /* |
27919067 OS |
5129 | * Errors here aren't a big deal, it just means we leave orphan items in |
5130 | * the tree. They will be cleaned up on the next mount. If the inode | |
5131 | * number gets reused, cleanup deletes the orphan item without doing | |
5132 | * anything, and unlink reuses the existing orphan item. | |
5133 | * | |
5134 | * If it turns out that we are dropping too many of these, we might want | |
5135 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5136 | */ |
ad80cf50 | 5137 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5138 | if (!IS_ERR(trans)) { |
5139 | trans->block_rsv = rsv; | |
5140 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5141 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5142 | btrfs_end_transaction(trans); | |
5143 | } | |
54aa1f4d | 5144 | |
0b246afa | 5145 | if (!(root == fs_info->tree_root || |
581bb050 | 5146 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5147 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5148 | |
27919067 OS |
5149 | free_rsv: |
5150 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5151 | no_delete: |
27919067 OS |
5152 | /* |
5153 | * If we didn't successfully delete, the orphan item will still be in | |
5154 | * the tree and we'll retry on the next mount. Again, we might also want | |
5155 | * to retry these periodically in the future. | |
5156 | */ | |
f48d1cf5 | 5157 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5158 | clear_inode(inode); |
39279cc3 CM |
5159 | } |
5160 | ||
5161 | /* | |
6bf9e4bd QW |
5162 | * Return the key found in the dir entry in the location pointer, fill @type |
5163 | * with BTRFS_FT_*, and return 0. | |
5164 | * | |
005d6712 SY |
5165 | * If no dir entries were found, returns -ENOENT. |
5166 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5167 | */ |
5168 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5169 | struct btrfs_key *location, u8 *type) |
39279cc3 CM |
5170 | { |
5171 | const char *name = dentry->d_name.name; | |
5172 | int namelen = dentry->d_name.len; | |
5173 | struct btrfs_dir_item *di; | |
5174 | struct btrfs_path *path; | |
5175 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5176 | int ret = 0; |
39279cc3 CM |
5177 | |
5178 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5179 | if (!path) |
5180 | return -ENOMEM; | |
3954401f | 5181 | |
f85b7379 DS |
5182 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5183 | name, namelen, 0); | |
3cf5068f LB |
5184 | if (IS_ERR_OR_NULL(di)) { |
5185 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5186 | goto out; |
5187 | } | |
d397712b | 5188 | |
5f39d397 | 5189 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5190 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5191 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5192 | ret = -EUCLEAN; |
56a0e706 LB |
5193 | btrfs_warn(root->fs_info, |
5194 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5195 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5196 | location->objectid, location->type, location->offset); | |
56a0e706 | 5197 | } |
6bf9e4bd QW |
5198 | if (!ret) |
5199 | *type = btrfs_dir_type(path->nodes[0], di); | |
39279cc3 | 5200 | out: |
39279cc3 CM |
5201 | btrfs_free_path(path); |
5202 | return ret; | |
5203 | } | |
5204 | ||
5205 | /* | |
5206 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5207 | * needs to be changed to reflect the root directory of the tree root. This | |
5208 | * is kind of like crossing a mount point. | |
5209 | */ | |
2ff7e61e | 5210 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5211 | struct inode *dir, |
5212 | struct dentry *dentry, | |
5213 | struct btrfs_key *location, | |
5214 | struct btrfs_root **sub_root) | |
39279cc3 | 5215 | { |
4df27c4d YZ |
5216 | struct btrfs_path *path; |
5217 | struct btrfs_root *new_root; | |
5218 | struct btrfs_root_ref *ref; | |
5219 | struct extent_buffer *leaf; | |
1d4c08e0 | 5220 | struct btrfs_key key; |
4df27c4d YZ |
5221 | int ret; |
5222 | int err = 0; | |
39279cc3 | 5223 | |
4df27c4d YZ |
5224 | path = btrfs_alloc_path(); |
5225 | if (!path) { | |
5226 | err = -ENOMEM; | |
5227 | goto out; | |
5228 | } | |
39279cc3 | 5229 | |
4df27c4d | 5230 | err = -ENOENT; |
1d4c08e0 DS |
5231 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5232 | key.type = BTRFS_ROOT_REF_KEY; | |
5233 | key.offset = location->objectid; | |
5234 | ||
0b246afa | 5235 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5236 | if (ret) { |
5237 | if (ret < 0) | |
5238 | err = ret; | |
5239 | goto out; | |
5240 | } | |
39279cc3 | 5241 | |
4df27c4d YZ |
5242 | leaf = path->nodes[0]; |
5243 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5244 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5245 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5246 | goto out; | |
39279cc3 | 5247 | |
4df27c4d YZ |
5248 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5249 | (unsigned long)(ref + 1), | |
5250 | dentry->d_name.len); | |
5251 | if (ret) | |
5252 | goto out; | |
5253 | ||
b3b4aa74 | 5254 | btrfs_release_path(path); |
4df27c4d | 5255 | |
56e9357a | 5256 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5257 | if (IS_ERR(new_root)) { |
5258 | err = PTR_ERR(new_root); | |
5259 | goto out; | |
5260 | } | |
5261 | ||
4df27c4d YZ |
5262 | *sub_root = new_root; |
5263 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5264 | location->type = BTRFS_INODE_ITEM_KEY; | |
5265 | location->offset = 0; | |
5266 | err = 0; | |
5267 | out: | |
5268 | btrfs_free_path(path); | |
5269 | return err; | |
39279cc3 CM |
5270 | } |
5271 | ||
5d4f98a2 YZ |
5272 | static void inode_tree_add(struct inode *inode) |
5273 | { | |
5274 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5275 | struct btrfs_inode *entry; | |
03e860bd NP |
5276 | struct rb_node **p; |
5277 | struct rb_node *parent; | |
cef21937 | 5278 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5279 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5280 | |
1d3382cb | 5281 | if (inode_unhashed(inode)) |
76dda93c | 5282 | return; |
e1409cef | 5283 | parent = NULL; |
5d4f98a2 | 5284 | spin_lock(&root->inode_lock); |
e1409cef | 5285 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5286 | while (*p) { |
5287 | parent = *p; | |
5288 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5289 | ||
37508515 | 5290 | if (ino < btrfs_ino(entry)) |
03e860bd | 5291 | p = &parent->rb_left; |
37508515 | 5292 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5293 | p = &parent->rb_right; |
5d4f98a2 YZ |
5294 | else { |
5295 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5296 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5297 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5298 | RB_CLEAR_NODE(parent); |
5299 | spin_unlock(&root->inode_lock); | |
cef21937 | 5300 | return; |
5d4f98a2 YZ |
5301 | } |
5302 | } | |
cef21937 FDBM |
5303 | rb_link_node(new, parent, p); |
5304 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5305 | spin_unlock(&root->inode_lock); |
5306 | } | |
5307 | ||
b79b7249 | 5308 | static void inode_tree_del(struct btrfs_inode *inode) |
5d4f98a2 | 5309 | { |
b79b7249 | 5310 | struct btrfs_root *root = inode->root; |
76dda93c | 5311 | int empty = 0; |
5d4f98a2 | 5312 | |
03e860bd | 5313 | spin_lock(&root->inode_lock); |
b79b7249 NB |
5314 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
5315 | rb_erase(&inode->rb_node, &root->inode_tree); | |
5316 | RB_CLEAR_NODE(&inode->rb_node); | |
76dda93c | 5317 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5318 | } |
03e860bd | 5319 | spin_unlock(&root->inode_lock); |
76dda93c | 5320 | |
69e9c6c6 | 5321 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5322 | spin_lock(&root->inode_lock); |
5323 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5324 | spin_unlock(&root->inode_lock); | |
5325 | if (empty) | |
5326 | btrfs_add_dead_root(root); | |
5327 | } | |
5328 | } | |
5329 | ||
5d4f98a2 | 5330 | |
e02119d5 CM |
5331 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5332 | { | |
5333 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5334 | |
5335 | inode->i_ino = args->ino; | |
5336 | BTRFS_I(inode)->location.objectid = args->ino; | |
5337 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5338 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5339 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5340 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
39279cc3 CM |
5341 | return 0; |
5342 | } | |
5343 | ||
5344 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5345 | { | |
5346 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5347 | |
5348 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5349 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5350 | } |
5351 | ||
0202e83f | 5352 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5353 | struct btrfs_root *root) |
39279cc3 CM |
5354 | { |
5355 | struct inode *inode; | |
5356 | struct btrfs_iget_args args; | |
0202e83f | 5357 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5358 | |
0202e83f | 5359 | args.ino = ino; |
39279cc3 CM |
5360 | args.root = root; |
5361 | ||
778ba82b | 5362 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5363 | btrfs_init_locked_inode, |
5364 | (void *)&args); | |
5365 | return inode; | |
5366 | } | |
5367 | ||
4c66e0d4 | 5368 | /* |
0202e83f | 5369 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5370 | * Path can be preallocated to prevent recursing back to iget through |
5371 | * allocator. NULL is also valid but may require an additional allocation | |
5372 | * later. | |
1a54ef8c | 5373 | */ |
0202e83f | 5374 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5375 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5376 | { |
5377 | struct inode *inode; | |
5378 | ||
0202e83f | 5379 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5380 | if (!inode) |
5d4f98a2 | 5381 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5382 | |
5383 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5384 | int ret; |
5385 | ||
4222ea71 | 5386 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5387 | if (!ret) { |
1748f843 MF |
5388 | inode_tree_add(inode); |
5389 | unlock_new_inode(inode); | |
1748f843 | 5390 | } else { |
f5b3a417 AV |
5391 | iget_failed(inode); |
5392 | /* | |
5393 | * ret > 0 can come from btrfs_search_slot called by | |
5394 | * btrfs_read_locked_inode, this means the inode item | |
5395 | * was not found. | |
5396 | */ | |
5397 | if (ret > 0) | |
5398 | ret = -ENOENT; | |
5399 | inode = ERR_PTR(ret); | |
1748f843 MF |
5400 | } |
5401 | } | |
5402 | ||
1a54ef8c BR |
5403 | return inode; |
5404 | } | |
5405 | ||
0202e83f | 5406 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5407 | { |
0202e83f | 5408 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5409 | } |
5410 | ||
4df27c4d YZ |
5411 | static struct inode *new_simple_dir(struct super_block *s, |
5412 | struct btrfs_key *key, | |
5413 | struct btrfs_root *root) | |
5414 | { | |
5415 | struct inode *inode = new_inode(s); | |
5416 | ||
5417 | if (!inode) | |
5418 | return ERR_PTR(-ENOMEM); | |
5419 | ||
5c8fd99f | 5420 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5421 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5422 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5423 | |
5424 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5425 | /* |
5426 | * We only need lookup, the rest is read-only and there's no inode | |
5427 | * associated with the dentry | |
5428 | */ | |
5429 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5430 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5431 | inode->i_fop = &simple_dir_operations; |
5432 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5433 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5434 | inode->i_atime = inode->i_mtime; |
5435 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5436 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5437 | |
5438 | return inode; | |
5439 | } | |
5440 | ||
6bf9e4bd QW |
5441 | static inline u8 btrfs_inode_type(struct inode *inode) |
5442 | { | |
5443 | /* | |
5444 | * Compile-time asserts that generic FT_* types still match | |
5445 | * BTRFS_FT_* types | |
5446 | */ | |
5447 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); | |
5448 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); | |
5449 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); | |
5450 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); | |
5451 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); | |
5452 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); | |
5453 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); | |
5454 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); | |
5455 | ||
5456 | return fs_umode_to_ftype(inode->i_mode); | |
5457 | } | |
5458 | ||
3de4586c | 5459 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5460 | { |
0b246afa | 5461 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5462 | struct inode *inode; |
4df27c4d | 5463 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5464 | struct btrfs_root *sub_root = root; |
5465 | struct btrfs_key location; | |
6bf9e4bd | 5466 | u8 di_type = 0; |
b4aff1f8 | 5467 | int ret = 0; |
39279cc3 CM |
5468 | |
5469 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5470 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5471 | |
6bf9e4bd | 5472 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5473 | if (ret < 0) |
5474 | return ERR_PTR(ret); | |
5f39d397 | 5475 | |
4df27c4d | 5476 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5477 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5478 | if (IS_ERR(inode)) |
5479 | return inode; | |
5480 | ||
5481 | /* Do extra check against inode mode with di_type */ | |
5482 | if (btrfs_inode_type(inode) != di_type) { | |
5483 | btrfs_crit(fs_info, | |
5484 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5485 | inode->i_mode, btrfs_inode_type(inode), | |
5486 | di_type); | |
5487 | iput(inode); | |
5488 | return ERR_PTR(-EUCLEAN); | |
5489 | } | |
4df27c4d YZ |
5490 | return inode; |
5491 | } | |
5492 | ||
2ff7e61e | 5493 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5494 | &location, &sub_root); |
5495 | if (ret < 0) { | |
5496 | if (ret != -ENOENT) | |
5497 | inode = ERR_PTR(ret); | |
5498 | else | |
5499 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5500 | } else { | |
0202e83f | 5501 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
39279cc3 | 5502 | } |
8727002f | 5503 | if (root != sub_root) |
00246528 | 5504 | btrfs_put_root(sub_root); |
76dda93c | 5505 | |
34d19bad | 5506 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5507 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5508 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5509 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5510 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5511 | if (ret) { |
5512 | iput(inode); | |
66b4ffd1 | 5513 | inode = ERR_PTR(ret); |
01cd3367 | 5514 | } |
c71bf099 YZ |
5515 | } |
5516 | ||
3de4586c CM |
5517 | return inode; |
5518 | } | |
5519 | ||
fe15ce44 | 5520 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5521 | { |
5522 | struct btrfs_root *root; | |
2b0143b5 | 5523 | struct inode *inode = d_inode(dentry); |
76dda93c | 5524 | |
848cce0d | 5525 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5526 | inode = d_inode(dentry->d_parent); |
76dda93c | 5527 | |
848cce0d LZ |
5528 | if (inode) { |
5529 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5530 | if (btrfs_root_refs(&root->root_item) == 0) |
5531 | return 1; | |
848cce0d | 5532 | |
4a0cc7ca | 5533 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5534 | return 1; |
efefb143 | 5535 | } |
76dda93c YZ |
5536 | return 0; |
5537 | } | |
5538 | ||
3de4586c | 5539 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5540 | unsigned int flags) |
3de4586c | 5541 | { |
3837d208 | 5542 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5543 | |
3837d208 AV |
5544 | if (inode == ERR_PTR(-ENOENT)) |
5545 | inode = NULL; | |
41d28bca | 5546 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5547 | } |
5548 | ||
23b5ec74 JB |
5549 | /* |
5550 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5551 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5552 | * our information into that, and then dir_emit from the buffer. This is | |
5553 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5554 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5555 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5556 | * tree lock. | |
5557 | */ | |
5558 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5559 | { | |
5560 | struct btrfs_file_private *private; | |
5561 | ||
5562 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5563 | if (!private) | |
5564 | return -ENOMEM; | |
5565 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5566 | if (!private->filldir_buf) { | |
5567 | kfree(private); | |
5568 | return -ENOMEM; | |
5569 | } | |
5570 | file->private_data = private; | |
5571 | return 0; | |
5572 | } | |
5573 | ||
5574 | struct dir_entry { | |
5575 | u64 ino; | |
5576 | u64 offset; | |
5577 | unsigned type; | |
5578 | int name_len; | |
5579 | }; | |
5580 | ||
5581 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5582 | { | |
5583 | while (entries--) { | |
5584 | struct dir_entry *entry = addr; | |
5585 | char *name = (char *)(entry + 1); | |
5586 | ||
92d32170 DS |
5587 | ctx->pos = get_unaligned(&entry->offset); |
5588 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5589 | get_unaligned(&entry->ino), | |
5590 | get_unaligned(&entry->type))) | |
23b5ec74 | 5591 | return 1; |
92d32170 DS |
5592 | addr += sizeof(struct dir_entry) + |
5593 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5594 | ctx->pos++; |
5595 | } | |
5596 | return 0; | |
5597 | } | |
5598 | ||
9cdda8d3 | 5599 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5600 | { |
9cdda8d3 | 5601 | struct inode *inode = file_inode(file); |
39279cc3 | 5602 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5603 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5604 | struct btrfs_dir_item *di; |
5605 | struct btrfs_key key; | |
5f39d397 | 5606 | struct btrfs_key found_key; |
39279cc3 | 5607 | struct btrfs_path *path; |
23b5ec74 | 5608 | void *addr; |
16cdcec7 MX |
5609 | struct list_head ins_list; |
5610 | struct list_head del_list; | |
39279cc3 | 5611 | int ret; |
5f39d397 | 5612 | struct extent_buffer *leaf; |
39279cc3 | 5613 | int slot; |
5f39d397 CM |
5614 | char *name_ptr; |
5615 | int name_len; | |
23b5ec74 JB |
5616 | int entries = 0; |
5617 | int total_len = 0; | |
02dbfc99 | 5618 | bool put = false; |
c2951f32 | 5619 | struct btrfs_key location; |
5f39d397 | 5620 | |
9cdda8d3 AV |
5621 | if (!dir_emit_dots(file, ctx)) |
5622 | return 0; | |
5623 | ||
49593bfa | 5624 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5625 | if (!path) |
5626 | return -ENOMEM; | |
ff5714cc | 5627 | |
23b5ec74 | 5628 | addr = private->filldir_buf; |
e4058b54 | 5629 | path->reada = READA_FORWARD; |
49593bfa | 5630 | |
c2951f32 JM |
5631 | INIT_LIST_HEAD(&ins_list); |
5632 | INIT_LIST_HEAD(&del_list); | |
5633 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5634 | |
23b5ec74 | 5635 | again: |
c2951f32 | 5636 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5637 | key.offset = ctx->pos; |
4a0cc7ca | 5638 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5639 | |
39279cc3 CM |
5640 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5641 | if (ret < 0) | |
5642 | goto err; | |
49593bfa DW |
5643 | |
5644 | while (1) { | |
23b5ec74 JB |
5645 | struct dir_entry *entry; |
5646 | ||
5f39d397 | 5647 | leaf = path->nodes[0]; |
39279cc3 | 5648 | slot = path->slots[0]; |
b9e03af0 LZ |
5649 | if (slot >= btrfs_header_nritems(leaf)) { |
5650 | ret = btrfs_next_leaf(root, path); | |
5651 | if (ret < 0) | |
5652 | goto err; | |
5653 | else if (ret > 0) | |
5654 | break; | |
5655 | continue; | |
39279cc3 | 5656 | } |
3de4586c | 5657 | |
5f39d397 CM |
5658 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5659 | ||
5660 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5661 | break; |
c2951f32 | 5662 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5663 | break; |
9cdda8d3 | 5664 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5665 | goto next; |
c2951f32 | 5666 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5667 | goto next; |
39279cc3 | 5668 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 5669 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5670 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5671 | PAGE_SIZE) { | |
5672 | btrfs_release_path(path); | |
5673 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5674 | if (ret) | |
5675 | goto nopos; | |
5676 | addr = private->filldir_buf; | |
5677 | entries = 0; | |
5678 | total_len = 0; | |
5679 | goto again; | |
c2951f32 | 5680 | } |
23b5ec74 JB |
5681 | |
5682 | entry = addr; | |
92d32170 | 5683 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 5684 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
5685 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5686 | name_len); | |
7d157c3d | 5687 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
92d32170 | 5688 | &entry->type); |
c2951f32 | 5689 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5690 | put_unaligned(location.objectid, &entry->ino); |
5691 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5692 | entries++; |
5693 | addr += sizeof(struct dir_entry) + name_len; | |
5694 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5695 | next: |
5696 | path->slots[0]++; | |
39279cc3 | 5697 | } |
23b5ec74 JB |
5698 | btrfs_release_path(path); |
5699 | ||
5700 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5701 | if (ret) | |
5702 | goto nopos; | |
49593bfa | 5703 | |
d2fbb2b5 | 5704 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5705 | if (ret) |
bc4ef759 DS |
5706 | goto nopos; |
5707 | ||
db62efbb ZB |
5708 | /* |
5709 | * Stop new entries from being returned after we return the last | |
5710 | * entry. | |
5711 | * | |
5712 | * New directory entries are assigned a strictly increasing | |
5713 | * offset. This means that new entries created during readdir | |
5714 | * are *guaranteed* to be seen in the future by that readdir. | |
5715 | * This has broken buggy programs which operate on names as | |
5716 | * they're returned by readdir. Until we re-use freed offsets | |
5717 | * we have this hack to stop new entries from being returned | |
5718 | * under the assumption that they'll never reach this huge | |
5719 | * offset. | |
5720 | * | |
5721 | * This is being careful not to overflow 32bit loff_t unless the | |
5722 | * last entry requires it because doing so has broken 32bit apps | |
5723 | * in the past. | |
5724 | */ | |
c2951f32 JM |
5725 | if (ctx->pos >= INT_MAX) |
5726 | ctx->pos = LLONG_MAX; | |
5727 | else | |
5728 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5729 | nopos: |
5730 | ret = 0; | |
5731 | err: | |
02dbfc99 OS |
5732 | if (put) |
5733 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5734 | btrfs_free_path(path); |
39279cc3 CM |
5735 | return ret; |
5736 | } | |
5737 | ||
39279cc3 | 5738 | /* |
54aa1f4d | 5739 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5740 | * inode changes. But, it is most likely to find the inode in cache. |
5741 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5742 | * to keep or drop this code. | |
5743 | */ | |
48a3b636 | 5744 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 5745 | { |
2ff7e61e | 5746 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5747 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5748 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5749 | int ret; |
5750 | ||
72ac3c0d | 5751 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5752 | return 0; |
39279cc3 | 5753 | |
7a7eaa40 | 5754 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5755 | if (IS_ERR(trans)) |
5756 | return PTR_ERR(trans); | |
8929ecfa YZ |
5757 | |
5758 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5759 | if (ret && ret == -ENOSPC) { |
5760 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 5761 | btrfs_end_transaction(trans); |
94b60442 | 5762 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
5763 | if (IS_ERR(trans)) |
5764 | return PTR_ERR(trans); | |
8929ecfa | 5765 | |
94b60442 | 5766 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5767 | } |
3a45bb20 | 5768 | btrfs_end_transaction(trans); |
16cdcec7 | 5769 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 5770 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
5771 | |
5772 | return ret; | |
5773 | } | |
5774 | ||
5775 | /* | |
5776 | * This is a copy of file_update_time. We need this so we can return error on | |
5777 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5778 | */ | |
95582b00 | 5779 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 5780 | int flags) |
22c44fe6 | 5781 | { |
2bc55652 | 5782 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 5783 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
5784 | |
5785 | if (btrfs_root_readonly(root)) | |
5786 | return -EROFS; | |
5787 | ||
e41f941a | 5788 | if (flags & S_VERSION) |
3a8c7231 | 5789 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
5790 | if (flags & S_CTIME) |
5791 | inode->i_ctime = *now; | |
5792 | if (flags & S_MTIME) | |
5793 | inode->i_mtime = *now; | |
5794 | if (flags & S_ATIME) | |
5795 | inode->i_atime = *now; | |
3a8c7231 | 5796 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
5797 | } |
5798 | ||
d352ac68 CM |
5799 | /* |
5800 | * find the highest existing sequence number in a directory | |
5801 | * and then set the in-memory index_cnt variable to reflect | |
5802 | * free sequence numbers | |
5803 | */ | |
4c570655 | 5804 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 5805 | { |
4c570655 | 5806 | struct btrfs_root *root = inode->root; |
aec7477b JB |
5807 | struct btrfs_key key, found_key; |
5808 | struct btrfs_path *path; | |
5809 | struct extent_buffer *leaf; | |
5810 | int ret; | |
5811 | ||
4c570655 | 5812 | key.objectid = btrfs_ino(inode); |
962a298f | 5813 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5814 | key.offset = (u64)-1; |
5815 | ||
5816 | path = btrfs_alloc_path(); | |
5817 | if (!path) | |
5818 | return -ENOMEM; | |
5819 | ||
5820 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5821 | if (ret < 0) | |
5822 | goto out; | |
5823 | /* FIXME: we should be able to handle this */ | |
5824 | if (ret == 0) | |
5825 | goto out; | |
5826 | ret = 0; | |
5827 | ||
5828 | /* | |
5829 | * MAGIC NUMBER EXPLANATION: | |
5830 | * since we search a directory based on f_pos we have to start at 2 | |
5831 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5832 | * else has to start at 2 | |
5833 | */ | |
5834 | if (path->slots[0] == 0) { | |
4c570655 | 5835 | inode->index_cnt = 2; |
aec7477b JB |
5836 | goto out; |
5837 | } | |
5838 | ||
5839 | path->slots[0]--; | |
5840 | ||
5841 | leaf = path->nodes[0]; | |
5842 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5843 | ||
4c570655 | 5844 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5845 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 5846 | inode->index_cnt = 2; |
aec7477b JB |
5847 | goto out; |
5848 | } | |
5849 | ||
4c570655 | 5850 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
5851 | out: |
5852 | btrfs_free_path(path); | |
5853 | return ret; | |
5854 | } | |
5855 | ||
d352ac68 CM |
5856 | /* |
5857 | * helper to find a free sequence number in a given directory. This current | |
5858 | * code is very simple, later versions will do smarter things in the btree | |
5859 | */ | |
877574e2 | 5860 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
5861 | { |
5862 | int ret = 0; | |
5863 | ||
877574e2 NB |
5864 | if (dir->index_cnt == (u64)-1) { |
5865 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
5866 | if (ret) { |
5867 | ret = btrfs_set_inode_index_count(dir); | |
5868 | if (ret) | |
5869 | return ret; | |
5870 | } | |
aec7477b JB |
5871 | } |
5872 | ||
877574e2 NB |
5873 | *index = dir->index_cnt; |
5874 | dir->index_cnt++; | |
aec7477b JB |
5875 | |
5876 | return ret; | |
5877 | } | |
5878 | ||
b0d5d10f CM |
5879 | static int btrfs_insert_inode_locked(struct inode *inode) |
5880 | { | |
5881 | struct btrfs_iget_args args; | |
0202e83f DS |
5882 | |
5883 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
5884 | args.root = BTRFS_I(inode)->root; |
5885 | ||
5886 | return insert_inode_locked4(inode, | |
5887 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5888 | btrfs_find_actor, &args); | |
5889 | } | |
5890 | ||
19aee8de AJ |
5891 | /* |
5892 | * Inherit flags from the parent inode. | |
5893 | * | |
5894 | * Currently only the compression flags and the cow flags are inherited. | |
5895 | */ | |
5896 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
5897 | { | |
5898 | unsigned int flags; | |
5899 | ||
5900 | if (!dir) | |
5901 | return; | |
5902 | ||
5903 | flags = BTRFS_I(dir)->flags; | |
5904 | ||
5905 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
5906 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
5907 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
5908 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
5909 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
5910 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
5911 | } | |
5912 | ||
5913 | if (flags & BTRFS_INODE_NODATACOW) { | |
5914 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
5915 | if (S_ISREG(inode->i_mode)) | |
5916 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
5917 | } | |
5918 | ||
7b6a221e | 5919 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
5920 | } |
5921 | ||
39279cc3 CM |
5922 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
5923 | struct btrfs_root *root, | |
aec7477b | 5924 | struct inode *dir, |
9c58309d | 5925 | const char *name, int name_len, |
175a4eb7 AV |
5926 | u64 ref_objectid, u64 objectid, |
5927 | umode_t mode, u64 *index) | |
39279cc3 | 5928 | { |
0b246afa | 5929 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 5930 | struct inode *inode; |
5f39d397 | 5931 | struct btrfs_inode_item *inode_item; |
39279cc3 | 5932 | struct btrfs_key *location; |
5f39d397 | 5933 | struct btrfs_path *path; |
9c58309d CM |
5934 | struct btrfs_inode_ref *ref; |
5935 | struct btrfs_key key[2]; | |
5936 | u32 sizes[2]; | |
ef3b9af5 | 5937 | int nitems = name ? 2 : 1; |
9c58309d | 5938 | unsigned long ptr; |
11a19a90 | 5939 | unsigned int nofs_flag; |
39279cc3 | 5940 | int ret; |
39279cc3 | 5941 | |
5f39d397 | 5942 | path = btrfs_alloc_path(); |
d8926bb3 MF |
5943 | if (!path) |
5944 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 5945 | |
11a19a90 | 5946 | nofs_flag = memalloc_nofs_save(); |
0b246afa | 5947 | inode = new_inode(fs_info->sb); |
11a19a90 | 5948 | memalloc_nofs_restore(nofs_flag); |
8fb27640 YS |
5949 | if (!inode) { |
5950 | btrfs_free_path(path); | |
39279cc3 | 5951 | return ERR_PTR(-ENOMEM); |
8fb27640 | 5952 | } |
39279cc3 | 5953 | |
5762b5c9 FM |
5954 | /* |
5955 | * O_TMPFILE, set link count to 0, so that after this point, | |
5956 | * we fill in an inode item with the correct link count. | |
5957 | */ | |
5958 | if (!name) | |
5959 | set_nlink(inode, 0); | |
5960 | ||
581bb050 LZ |
5961 | /* |
5962 | * we have to initialize this early, so we can reclaim the inode | |
5963 | * number if we fail afterwards in this function. | |
5964 | */ | |
5965 | inode->i_ino = objectid; | |
5966 | ||
ef3b9af5 | 5967 | if (dir && name) { |
1abe9b8a | 5968 | trace_btrfs_inode_request(dir); |
5969 | ||
877574e2 | 5970 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 5971 | if (ret) { |
8fb27640 | 5972 | btrfs_free_path(path); |
09771430 | 5973 | iput(inode); |
aec7477b | 5974 | return ERR_PTR(ret); |
09771430 | 5975 | } |
ef3b9af5 FM |
5976 | } else if (dir) { |
5977 | *index = 0; | |
aec7477b JB |
5978 | } |
5979 | /* | |
5980 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 5981 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
5982 | * number |
5983 | */ | |
5984 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 5985 | BTRFS_I(inode)->dir_index = *index; |
5c8fd99f | 5986 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
e02119d5 | 5987 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 5988 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 5989 | |
5dc562c5 JB |
5990 | /* |
5991 | * We could have gotten an inode number from somebody who was fsynced | |
5992 | * and then removed in this same transaction, so let's just set full | |
5993 | * sync since it will be a full sync anyway and this will blow away the | |
5994 | * old info in the log. | |
5995 | */ | |
5996 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
5997 | ||
9c58309d | 5998 | key[0].objectid = objectid; |
962a298f | 5999 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6000 | key[0].offset = 0; |
6001 | ||
9c58309d | 6002 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6003 | |
6004 | if (name) { | |
6005 | /* | |
6006 | * Start new inodes with an inode_ref. This is slightly more | |
6007 | * efficient for small numbers of hard links since they will | |
6008 | * be packed into one item. Extended refs will kick in if we | |
6009 | * add more hard links than can fit in the ref item. | |
6010 | */ | |
6011 | key[1].objectid = objectid; | |
962a298f | 6012 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6013 | key[1].offset = ref_objectid; |
6014 | ||
6015 | sizes[1] = name_len + sizeof(*ref); | |
6016 | } | |
9c58309d | 6017 | |
b0d5d10f CM |
6018 | location = &BTRFS_I(inode)->location; |
6019 | location->objectid = objectid; | |
6020 | location->offset = 0; | |
962a298f | 6021 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6022 | |
6023 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6024 | if (ret < 0) { |
6025 | iput(inode); | |
b0d5d10f | 6026 | goto fail; |
32955c54 | 6027 | } |
b0d5d10f | 6028 | |
b9473439 | 6029 | path->leave_spinning = 1; |
ef3b9af5 | 6030 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6031 | if (ret != 0) |
b0d5d10f | 6032 | goto fail_unlock; |
5f39d397 | 6033 | |
ecc11fab | 6034 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6035 | inode_set_bytes(inode, 0); |
9cc97d64 | 6036 | |
c2050a45 | 6037 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6038 | inode->i_atime = inode->i_mtime; |
6039 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6040 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6041 | |
5f39d397 CM |
6042 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6043 | struct btrfs_inode_item); | |
b159fa28 | 6044 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6045 | sizeof(*inode_item)); |
e02119d5 | 6046 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6047 | |
ef3b9af5 FM |
6048 | if (name) { |
6049 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6050 | struct btrfs_inode_ref); | |
6051 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6052 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6053 | ptr = (unsigned long)(ref + 1); | |
6054 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6055 | } | |
9c58309d | 6056 | |
5f39d397 CM |
6057 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6058 | btrfs_free_path(path); | |
6059 | ||
6cbff00f CH |
6060 | btrfs_inherit_iflags(inode, dir); |
6061 | ||
569254b0 | 6062 | if (S_ISREG(mode)) { |
0b246afa | 6063 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6064 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6065 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6066 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6067 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6068 | } |
6069 | ||
5d4f98a2 | 6070 | inode_tree_add(inode); |
1abe9b8a | 6071 | |
6072 | trace_btrfs_inode_new(inode); | |
d9094414 | 6073 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
1abe9b8a | 6074 | |
8ea05e3a AB |
6075 | btrfs_update_root_times(trans, root); |
6076 | ||
63541927 FDBM |
6077 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6078 | if (ret) | |
0b246afa | 6079 | btrfs_err(fs_info, |
63541927 | 6080 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6081 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6082 | |
39279cc3 | 6083 | return inode; |
b0d5d10f CM |
6084 | |
6085 | fail_unlock: | |
32955c54 | 6086 | discard_new_inode(inode); |
5f39d397 | 6087 | fail: |
ef3b9af5 | 6088 | if (dir && name) |
aec7477b | 6089 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6090 | btrfs_free_path(path); |
6091 | return ERR_PTR(ret); | |
39279cc3 CM |
6092 | } |
6093 | ||
d352ac68 CM |
6094 | /* |
6095 | * utility function to add 'inode' into 'parent_inode' with | |
6096 | * a give name and a given sequence number. | |
6097 | * if 'add_backref' is true, also insert a backref from the | |
6098 | * inode to the parent directory. | |
6099 | */ | |
e02119d5 | 6100 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6101 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6102 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6103 | { |
4df27c4d | 6104 | int ret = 0; |
39279cc3 | 6105 | struct btrfs_key key; |
db0a669f NB |
6106 | struct btrfs_root *root = parent_inode->root; |
6107 | u64 ino = btrfs_ino(inode); | |
6108 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6109 | |
33345d01 | 6110 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6111 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6112 | } else { |
33345d01 | 6113 | key.objectid = ino; |
962a298f | 6114 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6115 | key.offset = 0; |
6116 | } | |
6117 | ||
33345d01 | 6118 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6119 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6120 | root->root_key.objectid, parent_ino, |
6121 | index, name, name_len); | |
4df27c4d | 6122 | } else if (add_backref) { |
33345d01 LZ |
6123 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6124 | parent_ino, index); | |
4df27c4d | 6125 | } |
39279cc3 | 6126 | |
79787eaa JM |
6127 | /* Nothing to clean up yet */ |
6128 | if (ret) | |
6129 | return ret; | |
4df27c4d | 6130 | |
684572df | 6131 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6132 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6133 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6134 | goto fail_dir_item; |
6135 | else if (ret) { | |
66642832 | 6136 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6137 | return ret; |
39279cc3 | 6138 | } |
79787eaa | 6139 | |
db0a669f | 6140 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6141 | name_len * 2); |
db0a669f | 6142 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6143 | /* |
6144 | * If we are replaying a log tree, we do not want to update the mtime | |
6145 | * and ctime of the parent directory with the current time, since the | |
6146 | * log replay procedure is responsible for setting them to their correct | |
6147 | * values (the ones it had when the fsync was done). | |
6148 | */ | |
6149 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6150 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6151 | ||
6152 | parent_inode->vfs_inode.i_mtime = now; | |
6153 | parent_inode->vfs_inode.i_ctime = now; | |
6154 | } | |
db0a669f | 6155 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
79787eaa | 6156 | if (ret) |
66642832 | 6157 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6158 | return ret; |
fe66a05a CM |
6159 | |
6160 | fail_dir_item: | |
6161 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6162 | u64 local_index; | |
6163 | int err; | |
3ee1c553 | 6164 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6165 | root->root_key.objectid, parent_ino, |
6166 | &local_index, name, name_len); | |
1690dd41 JT |
6167 | if (err) |
6168 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6169 | } else if (add_backref) { |
6170 | u64 local_index; | |
6171 | int err; | |
6172 | ||
6173 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6174 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6175 | if (err) |
6176 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6177 | } |
1690dd41 JT |
6178 | |
6179 | /* Return the original error code */ | |
fe66a05a | 6180 | return ret; |
39279cc3 CM |
6181 | } |
6182 | ||
6183 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6184 | struct btrfs_inode *dir, struct dentry *dentry, |
6185 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6186 | { |
a1b075d2 JB |
6187 | int err = btrfs_add_link(trans, dir, inode, |
6188 | dentry->d_name.name, dentry->d_name.len, | |
6189 | backref, index); | |
39279cc3 CM |
6190 | if (err > 0) |
6191 | err = -EEXIST; | |
6192 | return err; | |
6193 | } | |
6194 | ||
618e21d5 | 6195 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6196 | umode_t mode, dev_t rdev) |
618e21d5 | 6197 | { |
2ff7e61e | 6198 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6199 | struct btrfs_trans_handle *trans; |
6200 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6201 | struct inode *inode = NULL; |
618e21d5 | 6202 | int err; |
618e21d5 | 6203 | u64 objectid; |
00e4e6b3 | 6204 | u64 index = 0; |
618e21d5 | 6205 | |
9ed74f2d JB |
6206 | /* |
6207 | * 2 for inode item and ref | |
6208 | * 2 for dir items | |
6209 | * 1 for xattr if selinux is on | |
6210 | */ | |
a22285a6 YZ |
6211 | trans = btrfs_start_transaction(root, 5); |
6212 | if (IS_ERR(trans)) | |
6213 | return PTR_ERR(trans); | |
1832a6d5 | 6214 | |
581bb050 LZ |
6215 | err = btrfs_find_free_ino(root, &objectid); |
6216 | if (err) | |
6217 | goto out_unlock; | |
6218 | ||
aec7477b | 6219 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6220 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6221 | mode, &index); | |
7cf96da3 TI |
6222 | if (IS_ERR(inode)) { |
6223 | err = PTR_ERR(inode); | |
32955c54 | 6224 | inode = NULL; |
618e21d5 | 6225 | goto out_unlock; |
7cf96da3 | 6226 | } |
618e21d5 | 6227 | |
ad19db71 CS |
6228 | /* |
6229 | * If the active LSM wants to access the inode during | |
6230 | * d_instantiate it needs these. Smack checks to see | |
6231 | * if the filesystem supports xattrs by looking at the | |
6232 | * ops vector. | |
6233 | */ | |
ad19db71 | 6234 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6235 | init_special_inode(inode, inode->i_mode, rdev); |
6236 | ||
6237 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6238 | if (err) |
32955c54 | 6239 | goto out_unlock; |
b0d5d10f | 6240 | |
cef415af NB |
6241 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6242 | 0, index); | |
32955c54 AV |
6243 | if (err) |
6244 | goto out_unlock; | |
6245 | ||
6246 | btrfs_update_inode(trans, root, inode); | |
6247 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6248 | |
618e21d5 | 6249 | out_unlock: |
3a45bb20 | 6250 | btrfs_end_transaction(trans); |
2ff7e61e | 6251 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6252 | if (err && inode) { |
618e21d5 | 6253 | inode_dec_link_count(inode); |
32955c54 | 6254 | discard_new_inode(inode); |
618e21d5 | 6255 | } |
618e21d5 JB |
6256 | return err; |
6257 | } | |
6258 | ||
39279cc3 | 6259 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6260 | umode_t mode, bool excl) |
39279cc3 | 6261 | { |
2ff7e61e | 6262 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6263 | struct btrfs_trans_handle *trans; |
6264 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6265 | struct inode *inode = NULL; |
a22285a6 | 6266 | int err; |
39279cc3 | 6267 | u64 objectid; |
00e4e6b3 | 6268 | u64 index = 0; |
39279cc3 | 6269 | |
9ed74f2d JB |
6270 | /* |
6271 | * 2 for inode item and ref | |
6272 | * 2 for dir items | |
6273 | * 1 for xattr if selinux is on | |
6274 | */ | |
a22285a6 YZ |
6275 | trans = btrfs_start_transaction(root, 5); |
6276 | if (IS_ERR(trans)) | |
6277 | return PTR_ERR(trans); | |
9ed74f2d | 6278 | |
581bb050 LZ |
6279 | err = btrfs_find_free_ino(root, &objectid); |
6280 | if (err) | |
6281 | goto out_unlock; | |
6282 | ||
aec7477b | 6283 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6284 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6285 | mode, &index); | |
7cf96da3 TI |
6286 | if (IS_ERR(inode)) { |
6287 | err = PTR_ERR(inode); | |
32955c54 | 6288 | inode = NULL; |
39279cc3 | 6289 | goto out_unlock; |
7cf96da3 | 6290 | } |
ad19db71 CS |
6291 | /* |
6292 | * If the active LSM wants to access the inode during | |
6293 | * d_instantiate it needs these. Smack checks to see | |
6294 | * if the filesystem supports xattrs by looking at the | |
6295 | * ops vector. | |
6296 | */ | |
6297 | inode->i_fop = &btrfs_file_operations; | |
6298 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6299 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6300 | |
6301 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6302 | if (err) | |
32955c54 | 6303 | goto out_unlock; |
b0d5d10f CM |
6304 | |
6305 | err = btrfs_update_inode(trans, root, inode); | |
6306 | if (err) | |
32955c54 | 6307 | goto out_unlock; |
ad19db71 | 6308 | |
cef415af NB |
6309 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6310 | 0, index); | |
39279cc3 | 6311 | if (err) |
32955c54 | 6312 | goto out_unlock; |
43baa579 | 6313 | |
43baa579 | 6314 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
1e2e547a | 6315 | d_instantiate_new(dentry, inode); |
43baa579 | 6316 | |
39279cc3 | 6317 | out_unlock: |
3a45bb20 | 6318 | btrfs_end_transaction(trans); |
32955c54 | 6319 | if (err && inode) { |
39279cc3 | 6320 | inode_dec_link_count(inode); |
32955c54 | 6321 | discard_new_inode(inode); |
39279cc3 | 6322 | } |
2ff7e61e | 6323 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6324 | return err; |
6325 | } | |
6326 | ||
6327 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6328 | struct dentry *dentry) | |
6329 | { | |
271dba45 | 6330 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6331 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6332 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6333 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6334 | u64 index; |
39279cc3 CM |
6335 | int err; |
6336 | int drop_inode = 0; | |
6337 | ||
4a8be425 | 6338 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6339 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6340 | return -EXDEV; |
4a8be425 | 6341 | |
f186373f | 6342 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6343 | return -EMLINK; |
4a8be425 | 6344 | |
877574e2 | 6345 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6346 | if (err) |
6347 | goto fail; | |
6348 | ||
a22285a6 | 6349 | /* |
7e6b6465 | 6350 | * 2 items for inode and inode ref |
a22285a6 | 6351 | * 2 items for dir items |
7e6b6465 | 6352 | * 1 item for parent inode |
399b0bbf | 6353 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6354 | */ |
399b0bbf | 6355 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6356 | if (IS_ERR(trans)) { |
6357 | err = PTR_ERR(trans); | |
271dba45 | 6358 | trans = NULL; |
a22285a6 YZ |
6359 | goto fail; |
6360 | } | |
5f39d397 | 6361 | |
67de1176 MX |
6362 | /* There are several dir indexes for this inode, clear the cache. */ |
6363 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6364 | inc_nlink(inode); |
0c4d2d95 | 6365 | inode_inc_iversion(inode); |
c2050a45 | 6366 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6367 | ihold(inode); |
e9976151 | 6368 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6369 | |
cef415af NB |
6370 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6371 | 1, index); | |
5f39d397 | 6372 | |
a5719521 | 6373 | if (err) { |
54aa1f4d | 6374 | drop_inode = 1; |
a5719521 | 6375 | } else { |
10d9f309 | 6376 | struct dentry *parent = dentry->d_parent; |
d4682ba0 | 6377 | |
a5719521 | 6378 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6379 | if (err) |
6380 | goto fail; | |
ef3b9af5 FM |
6381 | if (inode->i_nlink == 1) { |
6382 | /* | |
6383 | * If new hard link count is 1, it's a file created | |
6384 | * with open(2) O_TMPFILE flag. | |
6385 | */ | |
3d6ae7bb | 6386 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6387 | if (err) |
6388 | goto fail; | |
6389 | } | |
08c422c2 | 6390 | d_instantiate(dentry, inode); |
75b463d2 | 6391 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6392 | } |
39279cc3 | 6393 | |
1832a6d5 | 6394 | fail: |
271dba45 | 6395 | if (trans) |
3a45bb20 | 6396 | btrfs_end_transaction(trans); |
39279cc3 CM |
6397 | if (drop_inode) { |
6398 | inode_dec_link_count(inode); | |
6399 | iput(inode); | |
6400 | } | |
2ff7e61e | 6401 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6402 | return err; |
6403 | } | |
6404 | ||
18bb1db3 | 6405 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6406 | { |
2ff7e61e | 6407 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6408 | struct inode *inode = NULL; |
39279cc3 CM |
6409 | struct btrfs_trans_handle *trans; |
6410 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6411 | int err = 0; | |
b9d86667 | 6412 | u64 objectid = 0; |
00e4e6b3 | 6413 | u64 index = 0; |
39279cc3 | 6414 | |
9ed74f2d JB |
6415 | /* |
6416 | * 2 items for inode and ref | |
6417 | * 2 items for dir items | |
6418 | * 1 for xattr if selinux is on | |
6419 | */ | |
a22285a6 YZ |
6420 | trans = btrfs_start_transaction(root, 5); |
6421 | if (IS_ERR(trans)) | |
6422 | return PTR_ERR(trans); | |
39279cc3 | 6423 | |
581bb050 LZ |
6424 | err = btrfs_find_free_ino(root, &objectid); |
6425 | if (err) | |
6426 | goto out_fail; | |
6427 | ||
aec7477b | 6428 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6429 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6430 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6431 | if (IS_ERR(inode)) { |
6432 | err = PTR_ERR(inode); | |
32955c54 | 6433 | inode = NULL; |
39279cc3 CM |
6434 | goto out_fail; |
6435 | } | |
5f39d397 | 6436 | |
b0d5d10f CM |
6437 | /* these must be set before we unlock the inode */ |
6438 | inode->i_op = &btrfs_dir_inode_operations; | |
6439 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6440 | |
2a7dba39 | 6441 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6442 | if (err) |
32955c54 | 6443 | goto out_fail; |
39279cc3 | 6444 | |
6ef06d27 | 6445 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6446 | err = btrfs_update_inode(trans, root, inode); |
6447 | if (err) | |
32955c54 | 6448 | goto out_fail; |
5f39d397 | 6449 | |
db0a669f NB |
6450 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6451 | dentry->d_name.name, | |
6452 | dentry->d_name.len, 0, index); | |
39279cc3 | 6453 | if (err) |
32955c54 | 6454 | goto out_fail; |
5f39d397 | 6455 | |
1e2e547a | 6456 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
6457 | |
6458 | out_fail: | |
3a45bb20 | 6459 | btrfs_end_transaction(trans); |
32955c54 | 6460 | if (err && inode) { |
c7cfb8a5 | 6461 | inode_dec_link_count(inode); |
32955c54 | 6462 | discard_new_inode(inode); |
c7cfb8a5 | 6463 | } |
2ff7e61e | 6464 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6465 | return err; |
6466 | } | |
6467 | ||
c8b97818 | 6468 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6469 | struct page *page, |
c8b97818 CM |
6470 | size_t pg_offset, u64 extent_offset, |
6471 | struct btrfs_file_extent_item *item) | |
6472 | { | |
6473 | int ret; | |
6474 | struct extent_buffer *leaf = path->nodes[0]; | |
6475 | char *tmp; | |
6476 | size_t max_size; | |
6477 | unsigned long inline_size; | |
6478 | unsigned long ptr; | |
261507a0 | 6479 | int compress_type; |
c8b97818 CM |
6480 | |
6481 | WARN_ON(pg_offset != 0); | |
261507a0 | 6482 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6483 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6484 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6485 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6486 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6487 | if (!tmp) |
6488 | return -ENOMEM; | |
c8b97818 CM |
6489 | ptr = btrfs_file_extent_inline_start(item); |
6490 | ||
6491 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6492 | ||
09cbfeaf | 6493 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6494 | ret = btrfs_decompress(compress_type, tmp, page, |
6495 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6496 | |
6497 | /* | |
6498 | * decompression code contains a memset to fill in any space between the end | |
6499 | * of the uncompressed data and the end of max_size in case the decompressed | |
6500 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6501 | * the end of an inline extent and the beginning of the next block, so we | |
6502 | * cover that region here. | |
6503 | */ | |
6504 | ||
6505 | if (max_size + pg_offset < PAGE_SIZE) { | |
6506 | char *map = kmap(page); | |
6507 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6508 | kunmap(page); | |
6509 | } | |
c8b97818 | 6510 | kfree(tmp); |
166ae5a4 | 6511 | return ret; |
c8b97818 CM |
6512 | } |
6513 | ||
39b07b5d OS |
6514 | /** |
6515 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. | |
6516 | * @inode: file to search in | |
6517 | * @page: page to read extent data into if the extent is inline | |
6518 | * @pg_offset: offset into @page to copy to | |
6519 | * @start: file offset | |
6520 | * @len: length of range starting at @start | |
6521 | * | |
6522 | * This returns the first &struct extent_map which overlaps with the given | |
6523 | * range, reading it from the B-tree and caching it if necessary. Note that | |
6524 | * there may be more extents which overlap the given range after the returned | |
6525 | * extent_map. | |
d352ac68 | 6526 | * |
39b07b5d OS |
6527 | * If @page is not NULL and the extent is inline, this also reads the extent |
6528 | * data directly into the page and marks the extent up to date in the io_tree. | |
6529 | * | |
6530 | * Return: ERR_PTR on error, non-NULL extent_map on success. | |
d352ac68 | 6531 | */ |
fc4f21b1 | 6532 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
39b07b5d OS |
6533 | struct page *page, size_t pg_offset, |
6534 | u64 start, u64 len) | |
a52d9a80 | 6535 | { |
3ffbd68c | 6536 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
1028d1c4 | 6537 | int ret = 0; |
a52d9a80 CM |
6538 | u64 extent_start = 0; |
6539 | u64 extent_end = 0; | |
fc4f21b1 | 6540 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6541 | int extent_type = -1; |
f421950f | 6542 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6543 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6544 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6545 | struct extent_buffer *leaf; |
6546 | struct btrfs_key found_key; | |
a52d9a80 | 6547 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6548 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6549 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6550 | |
890871be | 6551 | read_lock(&em_tree->lock); |
d1310b2e | 6552 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6553 | read_unlock(&em_tree->lock); |
d1310b2e | 6554 | |
a52d9a80 | 6555 | if (em) { |
e1c4b745 CM |
6556 | if (em->start > start || em->start + em->len <= start) |
6557 | free_extent_map(em); | |
6558 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6559 | free_extent_map(em); |
6560 | else | |
6561 | goto out; | |
a52d9a80 | 6562 | } |
172ddd60 | 6563 | em = alloc_extent_map(); |
a52d9a80 | 6564 | if (!em) { |
1028d1c4 | 6565 | ret = -ENOMEM; |
d1310b2e | 6566 | goto out; |
a52d9a80 | 6567 | } |
d1310b2e | 6568 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6569 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6570 | em->len = (u64)-1; |
c8b97818 | 6571 | em->block_len = (u64)-1; |
f421950f | 6572 | |
bee6ec82 | 6573 | path = btrfs_alloc_path(); |
f421950f | 6574 | if (!path) { |
1028d1c4 | 6575 | ret = -ENOMEM; |
bee6ec82 | 6576 | goto out; |
f421950f CM |
6577 | } |
6578 | ||
bee6ec82 LB |
6579 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6580 | path->reada = READA_FORWARD; | |
6581 | ||
e49aabd9 LB |
6582 | /* |
6583 | * Unless we're going to uncompress the inline extent, no sleep would | |
6584 | * happen. | |
6585 | */ | |
6586 | path->leave_spinning = 1; | |
6587 | ||
51899412 JB |
6588 | path->recurse = btrfs_is_free_space_inode(inode); |
6589 | ||
5c9a702e | 6590 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 | 6591 | if (ret < 0) { |
a52d9a80 | 6592 | goto out; |
b8eeab7f | 6593 | } else if (ret > 0) { |
a52d9a80 CM |
6594 | if (path->slots[0] == 0) |
6595 | goto not_found; | |
6596 | path->slots[0]--; | |
1028d1c4 | 6597 | ret = 0; |
a52d9a80 CM |
6598 | } |
6599 | ||
5f39d397 CM |
6600 | leaf = path->nodes[0]; |
6601 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6602 | struct btrfs_file_extent_item); |
5f39d397 | 6603 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6604 | if (found_key.objectid != objectid || |
694c12ed | 6605 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6606 | /* |
6607 | * If we backup past the first extent we want to move forward | |
6608 | * and see if there is an extent in front of us, otherwise we'll | |
6609 | * say there is a hole for our whole search range which can | |
6610 | * cause problems. | |
6611 | */ | |
6612 | extent_end = start; | |
6613 | goto next; | |
a52d9a80 CM |
6614 | } |
6615 | ||
694c12ed | 6616 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6617 | extent_start = found_key.offset; |
a5eeb3d1 | 6618 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6619 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6620 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6621 | /* Only regular file could have regular/prealloc extent */ |
6622 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
1028d1c4 | 6623 | ret = -EUCLEAN; |
6bf9e4bd QW |
6624 | btrfs_crit(fs_info, |
6625 | "regular/prealloc extent found for non-regular inode %llu", | |
6626 | btrfs_ino(inode)); | |
6627 | goto out; | |
6628 | } | |
09ed2f16 LB |
6629 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6630 | extent_start); | |
694c12ed | 6631 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6632 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6633 | path->slots[0], | |
6634 | extent_start); | |
9036c102 | 6635 | } |
25a50341 | 6636 | next: |
9036c102 YZ |
6637 | if (start >= extent_end) { |
6638 | path->slots[0]++; | |
6639 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6640 | ret = btrfs_next_leaf(root, path); | |
1028d1c4 | 6641 | if (ret < 0) |
9036c102 | 6642 | goto out; |
1028d1c4 | 6643 | else if (ret > 0) |
9036c102 | 6644 | goto not_found; |
1028d1c4 | 6645 | |
9036c102 | 6646 | leaf = path->nodes[0]; |
a52d9a80 | 6647 | } |
9036c102 YZ |
6648 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6649 | if (found_key.objectid != objectid || | |
6650 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6651 | goto not_found; | |
6652 | if (start + len <= found_key.offset) | |
6653 | goto not_found; | |
e2eca69d WS |
6654 | if (start > found_key.offset) |
6655 | goto next; | |
02a033df NB |
6656 | |
6657 | /* New extent overlaps with existing one */ | |
9036c102 | 6658 | em->start = start; |
70c8a91c | 6659 | em->orig_start = start; |
9036c102 | 6660 | em->len = found_key.offset - start; |
02a033df NB |
6661 | em->block_start = EXTENT_MAP_HOLE; |
6662 | goto insert; | |
9036c102 YZ |
6663 | } |
6664 | ||
39b07b5d | 6665 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
7ffbb598 | 6666 | |
694c12ed NB |
6667 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6668 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6669 | goto insert; |
694c12ed | 6670 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 6671 | unsigned long ptr; |
a52d9a80 | 6672 | char *map; |
3326d1b0 CM |
6673 | size_t size; |
6674 | size_t extent_offset; | |
6675 | size_t copy_size; | |
a52d9a80 | 6676 | |
39b07b5d | 6677 | if (!page) |
689f9346 | 6678 | goto out; |
5f39d397 | 6679 | |
e41ca589 | 6680 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 6681 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6682 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6683 | size - extent_offset); | |
3326d1b0 | 6684 | em->start = extent_start + extent_offset; |
0b246afa | 6685 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 6686 | em->orig_block_len = em->len; |
70c8a91c | 6687 | em->orig_start = em->start; |
689f9346 | 6688 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 LB |
6689 | |
6690 | btrfs_set_path_blocking(path); | |
bf46f52d | 6691 | if (!PageUptodate(page)) { |
261507a0 LZ |
6692 | if (btrfs_file_extent_compression(leaf, item) != |
6693 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6694 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6695 | extent_offset, item); |
1028d1c4 | 6696 | if (ret) |
166ae5a4 | 6697 | goto out; |
c8b97818 CM |
6698 | } else { |
6699 | map = kmap(page); | |
6700 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6701 | copy_size); | |
09cbfeaf | 6702 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6703 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6704 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6705 | copy_size); |
6706 | } | |
c8b97818 CM |
6707 | kunmap(page); |
6708 | } | |
179e29e4 | 6709 | flush_dcache_page(page); |
a52d9a80 | 6710 | } |
d1310b2e | 6711 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6712 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6713 | goto insert; |
a52d9a80 CM |
6714 | } |
6715 | not_found: | |
6716 | em->start = start; | |
70c8a91c | 6717 | em->orig_start = start; |
d1310b2e | 6718 | em->len = len; |
5f39d397 | 6719 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6720 | insert: |
1028d1c4 | 6721 | ret = 0; |
b3b4aa74 | 6722 | btrfs_release_path(path); |
d1310b2e | 6723 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6724 | btrfs_err(fs_info, |
5d163e0e JM |
6725 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6726 | em->start, em->len, start, len); | |
1028d1c4 | 6727 | ret = -EIO; |
a52d9a80 CM |
6728 | goto out; |
6729 | } | |
d1310b2e | 6730 | |
890871be | 6731 | write_lock(&em_tree->lock); |
1028d1c4 | 6732 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6733 | write_unlock(&em_tree->lock); |
a52d9a80 | 6734 | out: |
c6414280 | 6735 | btrfs_free_path(path); |
1abe9b8a | 6736 | |
fc4f21b1 | 6737 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6738 | |
1028d1c4 | 6739 | if (ret) { |
a52d9a80 | 6740 | free_extent_map(em); |
1028d1c4 | 6741 | return ERR_PTR(ret); |
a52d9a80 CM |
6742 | } |
6743 | return em; | |
6744 | } | |
6745 | ||
fc4f21b1 | 6746 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 6747 | u64 start, u64 len) |
ec29ed5b CM |
6748 | { |
6749 | struct extent_map *em; | |
6750 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 6751 | u64 delalloc_start = start; |
ec29ed5b | 6752 | u64 end; |
f3714ef4 NB |
6753 | u64 delalloc_len; |
6754 | u64 delalloc_end; | |
ec29ed5b CM |
6755 | int err = 0; |
6756 | ||
39b07b5d | 6757 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
ec29ed5b CM |
6758 | if (IS_ERR(em)) |
6759 | return em; | |
9986277e DC |
6760 | /* |
6761 | * If our em maps to: | |
6762 | * - a hole or | |
6763 | * - a pre-alloc extent, | |
6764 | * there might actually be delalloc bytes behind it. | |
6765 | */ | |
6766 | if (em->block_start != EXTENT_MAP_HOLE && | |
6767 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6768 | return em; | |
6769 | else | |
6770 | hole_em = em; | |
ec29ed5b CM |
6771 | |
6772 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6773 | end = start + len; | |
6774 | if (end < start) | |
6775 | end = (u64)-1; | |
6776 | else | |
6777 | end -= 1; | |
6778 | ||
6779 | em = NULL; | |
6780 | ||
6781 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 6782 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 6783 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
6784 | delalloc_end = delalloc_start + delalloc_len; |
6785 | if (delalloc_end < delalloc_start) | |
6786 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
6787 | |
6788 | /* | |
f3714ef4 NB |
6789 | * We didn't find anything useful, return the original results from |
6790 | * get_extent() | |
ec29ed5b | 6791 | */ |
f3714ef4 | 6792 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
6793 | em = hole_em; |
6794 | hole_em = NULL; | |
6795 | goto out; | |
6796 | } | |
6797 | ||
f3714ef4 NB |
6798 | /* |
6799 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
6800 | * the start they passed in | |
ec29ed5b | 6801 | */ |
f3714ef4 NB |
6802 | delalloc_start = max(start, delalloc_start); |
6803 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 6804 | |
f3714ef4 NB |
6805 | if (delalloc_len > 0) { |
6806 | u64 hole_start; | |
02950af4 | 6807 | u64 hole_len; |
f3714ef4 | 6808 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 6809 | |
172ddd60 | 6810 | em = alloc_extent_map(); |
ec29ed5b CM |
6811 | if (!em) { |
6812 | err = -ENOMEM; | |
6813 | goto out; | |
6814 | } | |
f3714ef4 NB |
6815 | |
6816 | ASSERT(hole_em); | |
ec29ed5b | 6817 | /* |
f3714ef4 NB |
6818 | * When btrfs_get_extent can't find anything it returns one |
6819 | * huge hole | |
ec29ed5b | 6820 | * |
f3714ef4 NB |
6821 | * Make sure what it found really fits our range, and adjust to |
6822 | * make sure it is based on the start from the caller | |
ec29ed5b | 6823 | */ |
f3714ef4 NB |
6824 | if (hole_end <= start || hole_em->start > end) { |
6825 | free_extent_map(hole_em); | |
6826 | hole_em = NULL; | |
6827 | } else { | |
6828 | hole_start = max(hole_em->start, start); | |
6829 | hole_len = hole_end - hole_start; | |
ec29ed5b | 6830 | } |
f3714ef4 NB |
6831 | |
6832 | if (hole_em && delalloc_start > hole_start) { | |
6833 | /* | |
6834 | * Our hole starts before our delalloc, so we have to | |
6835 | * return just the parts of the hole that go until the | |
6836 | * delalloc starts | |
ec29ed5b | 6837 | */ |
f3714ef4 | 6838 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
6839 | em->start = hole_start; |
6840 | em->orig_start = hole_start; | |
6841 | /* | |
f3714ef4 NB |
6842 | * Don't adjust block start at all, it is fixed at |
6843 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
6844 | */ |
6845 | em->block_start = hole_em->block_start; | |
6846 | em->block_len = hole_len; | |
f9e4fb53 LB |
6847 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6848 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 6849 | } else { |
f3714ef4 NB |
6850 | /* |
6851 | * Hole is out of passed range or it starts after | |
6852 | * delalloc range | |
6853 | */ | |
6854 | em->start = delalloc_start; | |
6855 | em->len = delalloc_len; | |
6856 | em->orig_start = delalloc_start; | |
ec29ed5b | 6857 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 6858 | em->block_len = delalloc_len; |
ec29ed5b | 6859 | } |
bf8d32b9 | 6860 | } else { |
ec29ed5b CM |
6861 | return hole_em; |
6862 | } | |
6863 | out: | |
6864 | ||
6865 | free_extent_map(hole_em); | |
6866 | if (err) { | |
6867 | free_extent_map(em); | |
6868 | return ERR_PTR(err); | |
6869 | } | |
6870 | return em; | |
6871 | } | |
6872 | ||
64f54188 | 6873 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
5f9a8a51 FM |
6874 | const u64 start, |
6875 | const u64 len, | |
6876 | const u64 orig_start, | |
6877 | const u64 block_start, | |
6878 | const u64 block_len, | |
6879 | const u64 orig_block_len, | |
6880 | const u64 ram_bytes, | |
6881 | const int type) | |
6882 | { | |
6883 | struct extent_map *em = NULL; | |
6884 | int ret; | |
6885 | ||
5f9a8a51 | 6886 | if (type != BTRFS_ORDERED_NOCOW) { |
64f54188 NB |
6887 | em = create_io_em(inode, start, len, orig_start, block_start, |
6888 | block_len, orig_block_len, ram_bytes, | |
6f9994db LB |
6889 | BTRFS_COMPRESS_NONE, /* compress_type */ |
6890 | type); | |
5f9a8a51 FM |
6891 | if (IS_ERR(em)) |
6892 | goto out; | |
6893 | } | |
64f54188 NB |
6894 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
6895 | block_len, type); | |
5f9a8a51 FM |
6896 | if (ret) { |
6897 | if (em) { | |
6898 | free_extent_map(em); | |
64f54188 | 6899 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
5f9a8a51 FM |
6900 | } |
6901 | em = ERR_PTR(ret); | |
6902 | } | |
6903 | out: | |
5f9a8a51 FM |
6904 | |
6905 | return em; | |
6906 | } | |
6907 | ||
9fc6f911 | 6908 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
4b46fce2 JB |
6909 | u64 start, u64 len) |
6910 | { | |
9fc6f911 NB |
6911 | struct btrfs_root *root = inode->root; |
6912 | struct btrfs_fs_info *fs_info = root->fs_info; | |
70c8a91c | 6913 | struct extent_map *em; |
4b46fce2 JB |
6914 | struct btrfs_key ins; |
6915 | u64 alloc_hint; | |
6916 | int ret; | |
4b46fce2 | 6917 | |
9fc6f911 | 6918 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 6919 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 6920 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
6921 | if (ret) |
6922 | return ERR_PTR(ret); | |
4b46fce2 | 6923 | |
9fc6f911 | 6924 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
5f9a8a51 | 6925 | ins.objectid, ins.offset, ins.offset, |
6288d6ea | 6926 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 6927 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 6928 | if (IS_ERR(em)) |
9fc6f911 NB |
6929 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
6930 | 1); | |
de0ee0ed | 6931 | |
4b46fce2 JB |
6932 | return em; |
6933 | } | |
6934 | ||
46bfbb5c | 6935 | /* |
e4ecaf90 QW |
6936 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
6937 | * | |
6938 | * @offset: File offset | |
6939 | * @len: The length to write, will be updated to the nocow writeable | |
6940 | * range | |
6941 | * @orig_start: (optional) Return the original file offset of the file extent | |
6942 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
6943 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
a84d5d42 BB |
6944 | * @strict: if true, omit optimizations that might force us into unnecessary |
6945 | * cow. e.g., don't trust generation number. | |
e4ecaf90 QW |
6946 | * |
6947 | * This function will flush ordered extents in the range to ensure proper | |
6948 | * nocow checks for (nowait == false) case. | |
6949 | * | |
6950 | * Return: | |
6951 | * >0 and update @len if we can do nocow write | |
6952 | * 0 if we can't do nocow write | |
6953 | * <0 if error happened | |
6954 | * | |
6955 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
6956 | * any ordered extents. | |
46bfbb5c | 6957 | */ |
00361589 | 6958 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 | 6959 | u64 *orig_start, u64 *orig_block_len, |
a84d5d42 | 6960 | u64 *ram_bytes, bool strict) |
46bfbb5c | 6961 | { |
2ff7e61e | 6962 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
6963 | struct btrfs_path *path; |
6964 | int ret; | |
6965 | struct extent_buffer *leaf; | |
6966 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 6967 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
6968 | struct btrfs_file_extent_item *fi; |
6969 | struct btrfs_key key; | |
6970 | u64 disk_bytenr; | |
6971 | u64 backref_offset; | |
6972 | u64 extent_end; | |
6973 | u64 num_bytes; | |
6974 | int slot; | |
6975 | int found_type; | |
7ee9e440 | 6976 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 6977 | |
46bfbb5c CM |
6978 | path = btrfs_alloc_path(); |
6979 | if (!path) | |
6980 | return -ENOMEM; | |
6981 | ||
f85b7379 DS |
6982 | ret = btrfs_lookup_file_extent(NULL, root, path, |
6983 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
6984 | if (ret < 0) |
6985 | goto out; | |
6986 | ||
6987 | slot = path->slots[0]; | |
6988 | if (ret == 1) { | |
6989 | if (slot == 0) { | |
6990 | /* can't find the item, must cow */ | |
6991 | ret = 0; | |
6992 | goto out; | |
6993 | } | |
6994 | slot--; | |
6995 | } | |
6996 | ret = 0; | |
6997 | leaf = path->nodes[0]; | |
6998 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 6999 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7000 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7001 | /* not our file or wrong item type, must cow */ | |
7002 | goto out; | |
7003 | } | |
7004 | ||
7005 | if (key.offset > offset) { | |
7006 | /* Wrong offset, must cow */ | |
7007 | goto out; | |
7008 | } | |
7009 | ||
7010 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7011 | found_type = btrfs_file_extent_type(leaf, fi); | |
7012 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7013 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7014 | /* not a regular extent, must cow */ | |
7015 | goto out; | |
7016 | } | |
7ee9e440 JB |
7017 | |
7018 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7019 | goto out; | |
7020 | ||
e77751aa MX |
7021 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7022 | if (extent_end <= offset) | |
7023 | goto out; | |
7024 | ||
46bfbb5c | 7025 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7026 | if (disk_bytenr == 0) |
7027 | goto out; | |
7028 | ||
7029 | if (btrfs_file_extent_compression(leaf, fi) || | |
7030 | btrfs_file_extent_encryption(leaf, fi) || | |
7031 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7032 | goto out; | |
7033 | ||
78d4295b EL |
7034 | /* |
7035 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7036 | * unnecessary search. | |
7037 | */ | |
a84d5d42 BB |
7038 | if (!strict && |
7039 | (btrfs_file_extent_generation(leaf, fi) <= | |
7040 | btrfs_root_last_snapshot(&root->root_item))) | |
78d4295b EL |
7041 | goto out; |
7042 | ||
46bfbb5c CM |
7043 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7044 | ||
7ee9e440 JB |
7045 | if (orig_start) { |
7046 | *orig_start = key.offset - backref_offset; | |
7047 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7048 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7049 | } | |
eb384b55 | 7050 | |
2ff7e61e | 7051 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7052 | goto out; |
7b2b7085 MX |
7053 | |
7054 | num_bytes = min(offset + *len, extent_end) - offset; | |
7055 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7056 | u64 range_end; | |
7057 | ||
da17066c JM |
7058 | range_end = round_up(offset + num_bytes, |
7059 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7060 | ret = test_range_bit(io_tree, offset, range_end, |
7061 | EXTENT_DELALLOC, 0, NULL); | |
7062 | if (ret) { | |
7063 | ret = -EAGAIN; | |
7064 | goto out; | |
7065 | } | |
7066 | } | |
7067 | ||
1bda19eb | 7068 | btrfs_release_path(path); |
46bfbb5c CM |
7069 | |
7070 | /* | |
7071 | * look for other files referencing this extent, if we | |
7072 | * find any we must cow | |
7073 | */ | |
00361589 | 7074 | |
e4c3b2dc | 7075 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
a84d5d42 BB |
7076 | key.offset - backref_offset, disk_bytenr, |
7077 | strict); | |
00361589 JB |
7078 | if (ret) { |
7079 | ret = 0; | |
7080 | goto out; | |
7081 | } | |
46bfbb5c CM |
7082 | |
7083 | /* | |
7084 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7085 | * in this extent we are about to write. If there | |
7086 | * are any csums in that range we have to cow in order | |
7087 | * to keep the csums correct | |
7088 | */ | |
7089 | disk_bytenr += backref_offset; | |
7090 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7091 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7092 | goto out; | |
46bfbb5c CM |
7093 | /* |
7094 | * all of the above have passed, it is safe to overwrite this extent | |
7095 | * without cow | |
7096 | */ | |
eb384b55 | 7097 | *len = num_bytes; |
46bfbb5c CM |
7098 | ret = 1; |
7099 | out: | |
7100 | btrfs_free_path(path); | |
7101 | return ret; | |
7102 | } | |
7103 | ||
eb838e73 | 7104 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
f85781fb | 7105 | struct extent_state **cached_state, bool writing) |
eb838e73 JB |
7106 | { |
7107 | struct btrfs_ordered_extent *ordered; | |
7108 | int ret = 0; | |
7109 | ||
7110 | while (1) { | |
7111 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7112 | cached_state); |
eb838e73 JB |
7113 | /* |
7114 | * We're concerned with the entire range that we're going to be | |
01327610 | 7115 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7116 | * extents in this range. |
7117 | */ | |
a776c6fa | 7118 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7119 | lockend - lockstart + 1); |
7120 | ||
7121 | /* | |
7122 | * We need to make sure there are no buffered pages in this | |
7123 | * range either, we could have raced between the invalidate in | |
7124 | * generic_file_direct_write and locking the extent. The | |
7125 | * invalidate needs to happen so that reads after a write do not | |
7126 | * get stale data. | |
7127 | */ | |
fc4adbff | 7128 | if (!ordered && |
051c98eb DS |
7129 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7130 | lockstart, lockend))) | |
eb838e73 JB |
7131 | break; |
7132 | ||
7133 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7134 | cached_state); |
eb838e73 JB |
7135 | |
7136 | if (ordered) { | |
ade77029 FM |
7137 | /* |
7138 | * If we are doing a DIO read and the ordered extent we | |
7139 | * found is for a buffered write, we can not wait for it | |
7140 | * to complete and retry, because if we do so we can | |
7141 | * deadlock with concurrent buffered writes on page | |
7142 | * locks. This happens only if our DIO read covers more | |
7143 | * than one extent map, if at this point has already | |
7144 | * created an ordered extent for a previous extent map | |
7145 | * and locked its range in the inode's io tree, and a | |
7146 | * concurrent write against that previous extent map's | |
7147 | * range and this range started (we unlock the ranges | |
7148 | * in the io tree only when the bios complete and | |
7149 | * buffered writes always lock pages before attempting | |
7150 | * to lock range in the io tree). | |
7151 | */ | |
7152 | if (writing || | |
7153 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7154 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7155 | else | |
7156 | ret = -ENOTBLK; | |
eb838e73 JB |
7157 | btrfs_put_ordered_extent(ordered); |
7158 | } else { | |
eb838e73 | 7159 | /* |
b850ae14 FM |
7160 | * We could trigger writeback for this range (and wait |
7161 | * for it to complete) and then invalidate the pages for | |
7162 | * this range (through invalidate_inode_pages2_range()), | |
7163 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7164 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7165 | * triggered a readahead) on a page lock due to an |
7166 | * ordered dio extent we created before but did not have | |
7167 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7168 | * complete), which makes readahead wait for that |
b850ae14 FM |
7169 | * ordered extent to complete while holding a lock on |
7170 | * that page. | |
eb838e73 | 7171 | */ |
b850ae14 | 7172 | ret = -ENOTBLK; |
eb838e73 JB |
7173 | } |
7174 | ||
ade77029 FM |
7175 | if (ret) |
7176 | break; | |
7177 | ||
eb838e73 JB |
7178 | cond_resched(); |
7179 | } | |
7180 | ||
7181 | return ret; | |
7182 | } | |
7183 | ||
6f9994db | 7184 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7185 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7186 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7187 | u64 block_len, u64 orig_block_len, |
7188 | u64 ram_bytes, int compress_type, | |
7189 | int type) | |
69ffb543 JB |
7190 | { |
7191 | struct extent_map_tree *em_tree; | |
7192 | struct extent_map *em; | |
69ffb543 JB |
7193 | int ret; |
7194 | ||
6f9994db LB |
7195 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7196 | type == BTRFS_ORDERED_COMPRESSED || | |
7197 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7198 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7199 | |
4b67c11d | 7200 | em_tree = &inode->extent_tree; |
69ffb543 JB |
7201 | em = alloc_extent_map(); |
7202 | if (!em) | |
7203 | return ERR_PTR(-ENOMEM); | |
7204 | ||
7205 | em->start = start; | |
7206 | em->orig_start = orig_start; | |
7207 | em->len = len; | |
7208 | em->block_len = block_len; | |
7209 | em->block_start = block_start; | |
b4939680 | 7210 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7211 | em->ram_bytes = ram_bytes; |
70c8a91c | 7212 | em->generation = -1; |
69ffb543 | 7213 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7214 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7215 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7216 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7217 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7218 | em->compress_type = compress_type; | |
7219 | } | |
69ffb543 JB |
7220 | |
7221 | do { | |
4b67c11d NB |
7222 | btrfs_drop_extent_cache(inode, em->start, |
7223 | em->start + em->len - 1, 0); | |
69ffb543 | 7224 | write_lock(&em_tree->lock); |
09a2a8f9 | 7225 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7226 | write_unlock(&em_tree->lock); |
6f9994db LB |
7227 | /* |
7228 | * The caller has taken lock_extent(), who could race with us | |
7229 | * to add em? | |
7230 | */ | |
69ffb543 JB |
7231 | } while (ret == -EEXIST); |
7232 | ||
7233 | if (ret) { | |
7234 | free_extent_map(em); | |
7235 | return ERR_PTR(ret); | |
7236 | } | |
7237 | ||
6f9994db | 7238 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7239 | return em; |
7240 | } | |
7241 | ||
1c8d0175 | 7242 | |
c5794e51 | 7243 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
c5794e51 NB |
7244 | struct inode *inode, |
7245 | struct btrfs_dio_data *dio_data, | |
7246 | u64 start, u64 len) | |
7247 | { | |
7248 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7249 | struct extent_map *em = *map; | |
7250 | int ret = 0; | |
7251 | ||
7252 | /* | |
7253 | * We don't allocate a new extent in the following cases | |
7254 | * | |
7255 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7256 | * existing extent. | |
7257 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7258 | * just use the extent. | |
7259 | * | |
7260 | */ | |
7261 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7262 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7263 | em->block_start != EXTENT_MAP_HOLE)) { | |
7264 | int type; | |
7265 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7266 | ||
7267 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7268 | type = BTRFS_ORDERED_PREALLOC; | |
7269 | else | |
7270 | type = BTRFS_ORDERED_NOCOW; | |
7271 | len = min(len, em->len - (start - em->start)); | |
7272 | block_start = em->block_start + (start - em->start); | |
7273 | ||
7274 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
a84d5d42 | 7275 | &orig_block_len, &ram_bytes, false) == 1 && |
c5794e51 NB |
7276 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
7277 | struct extent_map *em2; | |
7278 | ||
64f54188 | 7279 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
c5794e51 NB |
7280 | orig_start, block_start, |
7281 | len, orig_block_len, | |
7282 | ram_bytes, type); | |
7283 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7284 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7285 | free_extent_map(em); | |
7286 | *map = em = em2; | |
7287 | } | |
7288 | ||
7289 | if (em2 && IS_ERR(em2)) { | |
7290 | ret = PTR_ERR(em2); | |
7291 | goto out; | |
7292 | } | |
7293 | /* | |
7294 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7295 | * use the existing or preallocated extent, so does not | |
7296 | * need to adjust btrfs_space_info's bytes_may_use. | |
7297 | */ | |
9db5d510 | 7298 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
c5794e51 NB |
7299 | goto skip_cow; |
7300 | } | |
7301 | } | |
7302 | ||
7303 | /* this will cow the extent */ | |
c5794e51 | 7304 | free_extent_map(em); |
9fc6f911 | 7305 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
c5794e51 NB |
7306 | if (IS_ERR(em)) { |
7307 | ret = PTR_ERR(em); | |
7308 | goto out; | |
7309 | } | |
7310 | ||
7311 | len = min(len, em->len - (start - em->start)); | |
7312 | ||
7313 | skip_cow: | |
c5794e51 NB |
7314 | /* |
7315 | * Need to update the i_size under the extent lock so buffered | |
7316 | * readers will get the updated i_size when we unlock. | |
7317 | */ | |
f85781fb | 7318 | if (start + len > i_size_read(inode)) |
c5794e51 NB |
7319 | i_size_write(inode, start + len); |
7320 | ||
c5794e51 | 7321 | dio_data->reserve -= len; |
c5794e51 NB |
7322 | out: |
7323 | return ret; | |
7324 | } | |
7325 | ||
f85781fb GR |
7326 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
7327 | loff_t length, unsigned int flags, struct iomap *iomap, | |
7328 | struct iomap *srcmap) | |
4b46fce2 | 7329 | { |
0b246afa | 7330 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7331 | struct extent_map *em; |
eb838e73 | 7332 | struct extent_state *cached_state = NULL; |
50745b0a | 7333 | struct btrfs_dio_data *dio_data = NULL; |
eb838e73 | 7334 | u64 lockstart, lockend; |
f85781fb | 7335 | const bool write = !!(flags & IOMAP_WRITE); |
0934856d | 7336 | int ret = 0; |
f85781fb GR |
7337 | u64 len = length; |
7338 | bool unlock_extents = false; | |
0eb79294 JB |
7339 | bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB); |
7340 | ||
7341 | /* | |
7342 | * We used current->journal_info here to see if we were sync, but | |
7343 | * there's a lot of tests in the enospc machinery to not do flushing if | |
7344 | * we have a journal_info set, so we need to clear this out and re-set | |
7345 | * it in iomap_end. | |
7346 | */ | |
7347 | ASSERT(current->journal_info == NULL || | |
7348 | current->journal_info == BTRFS_DIO_SYNC_STUB); | |
7349 | current->journal_info = NULL; | |
eb838e73 | 7350 | |
f85781fb | 7351 | if (!write) |
0b246afa | 7352 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7353 | |
c329861d JB |
7354 | lockstart = start; |
7355 | lockend = start + len - 1; | |
7356 | ||
f85781fb GR |
7357 | /* |
7358 | * The generic stuff only does filemap_write_and_wait_range, which | |
7359 | * isn't enough if we've written compressed pages to this area, so we | |
7360 | * need to flush the dirty pages again to make absolutely sure that any | |
7361 | * outstanding dirty pages are on disk. | |
7362 | */ | |
7363 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
7364 | &BTRFS_I(inode)->runtime_flags)) { | |
7365 | ret = filemap_fdatawrite_range(inode->i_mapping, start, | |
7366 | start + length - 1); | |
7367 | if (ret) | |
7368 | return ret; | |
7369 | } | |
7370 | ||
7371 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); | |
7372 | if (!dio_data) | |
7373 | return -ENOMEM; | |
7374 | ||
0eb79294 | 7375 | dio_data->sync = sync; |
f85781fb GR |
7376 | dio_data->length = length; |
7377 | if (write) { | |
7378 | dio_data->reserve = round_up(length, fs_info->sectorsize); | |
7379 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), | |
7380 | &dio_data->data_reserved, | |
7381 | start, dio_data->reserve); | |
7382 | if (ret) { | |
7383 | extent_changeset_free(dio_data->data_reserved); | |
7384 | kfree(dio_data); | |
7385 | return ret; | |
7386 | } | |
e1cbbfa5 | 7387 | } |
f85781fb GR |
7388 | iomap->private = dio_data; |
7389 | ||
e1cbbfa5 | 7390 | |
eb838e73 JB |
7391 | /* |
7392 | * If this errors out it's because we couldn't invalidate pagecache for | |
7393 | * this range and we need to fallback to buffered. | |
7394 | */ | |
f85781fb | 7395 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
9c9464cc FM |
7396 | ret = -ENOTBLK; |
7397 | goto err; | |
7398 | } | |
eb838e73 | 7399 | |
39b07b5d | 7400 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7401 | if (IS_ERR(em)) { |
7402 | ret = PTR_ERR(em); | |
7403 | goto unlock_err; | |
7404 | } | |
4b46fce2 JB |
7405 | |
7406 | /* | |
7407 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7408 | * io. INLINE is special, and we could probably kludge it in here, but | |
7409 | * it's still buffered so for safety lets just fall back to the generic | |
7410 | * buffered path. | |
7411 | * | |
7412 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7413 | * decompress it, so there will be buffering required no matter what we | |
7414 | * do, so go ahead and fallback to buffered. | |
7415 | * | |
01327610 | 7416 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7417 | * to buffered IO. Don't blame me, this is the price we pay for using |
7418 | * the generic code. | |
7419 | */ | |
7420 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7421 | em->block_start == EXTENT_MAP_INLINE) { | |
7422 | free_extent_map(em); | |
eb838e73 JB |
7423 | ret = -ENOTBLK; |
7424 | goto unlock_err; | |
4b46fce2 JB |
7425 | } |
7426 | ||
f85781fb GR |
7427 | len = min(len, em->len - (start - em->start)); |
7428 | if (write) { | |
7429 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, | |
7430 | start, len); | |
c5794e51 NB |
7431 | if (ret < 0) |
7432 | goto unlock_err; | |
f85781fb GR |
7433 | unlock_extents = true; |
7434 | /* Recalc len in case the new em is smaller than requested */ | |
7435 | len = min(len, em->len - (start - em->start)); | |
c5794e51 | 7436 | } else { |
1c8d0175 NB |
7437 | /* |
7438 | * We need to unlock only the end area that we aren't using. | |
7439 | * The rest is going to be unlocked by the endio routine. | |
7440 | */ | |
f85781fb GR |
7441 | lockstart = start + len; |
7442 | if (lockstart < lockend) | |
7443 | unlock_extents = true; | |
7444 | } | |
7445 | ||
7446 | if (unlock_extents) | |
7447 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
7448 | lockstart, lockend, &cached_state); | |
7449 | else | |
7450 | free_extent_state(cached_state); | |
7451 | ||
7452 | /* | |
7453 | * Translate extent map information to iomap. | |
7454 | * We trim the extents (and move the addr) even though iomap code does | |
7455 | * that, since we have locked only the parts we are performing I/O in. | |
7456 | */ | |
7457 | if ((em->block_start == EXTENT_MAP_HOLE) || | |
7458 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { | |
7459 | iomap->addr = IOMAP_NULL_ADDR; | |
7460 | iomap->type = IOMAP_HOLE; | |
7461 | } else { | |
7462 | iomap->addr = em->block_start + (start - em->start); | |
7463 | iomap->type = IOMAP_MAPPED; | |
a43a67a2 | 7464 | } |
f85781fb GR |
7465 | iomap->offset = start; |
7466 | iomap->bdev = fs_info->fs_devices->latest_bdev; | |
7467 | iomap->length = len; | |
a43a67a2 | 7468 | |
4b46fce2 JB |
7469 | free_extent_map(em); |
7470 | ||
7471 | return 0; | |
eb838e73 JB |
7472 | |
7473 | unlock_err: | |
e182163d OS |
7474 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7475 | &cached_state); | |
9c9464cc | 7476 | err: |
f85781fb GR |
7477 | if (dio_data) { |
7478 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7479 | dio_data->data_reserved, start, | |
7480 | dio_data->reserve, true); | |
7481 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); | |
7482 | extent_changeset_free(dio_data->data_reserved); | |
7483 | kfree(dio_data); | |
7484 | } | |
7485 | return ret; | |
7486 | } | |
7487 | ||
7488 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, | |
7489 | ssize_t written, unsigned int flags, struct iomap *iomap) | |
7490 | { | |
7491 | int ret = 0; | |
7492 | struct btrfs_dio_data *dio_data = iomap->private; | |
7493 | size_t submitted = dio_data->submitted; | |
7494 | const bool write = !!(flags & IOMAP_WRITE); | |
7495 | ||
7496 | if (!write && (iomap->type == IOMAP_HOLE)) { | |
7497 | /* If reading from a hole, unlock and return */ | |
7498 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); | |
7499 | goto out; | |
7500 | } | |
7501 | ||
7502 | if (submitted < length) { | |
7503 | pos += submitted; | |
7504 | length -= submitted; | |
7505 | if (write) | |
7506 | __endio_write_update_ordered(BTRFS_I(inode), pos, | |
7507 | length, false); | |
7508 | else | |
7509 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, | |
7510 | pos + length - 1); | |
7511 | ret = -ENOTBLK; | |
7512 | } | |
7513 | ||
7514 | if (write) { | |
7515 | if (dio_data->reserve) | |
7516 | btrfs_delalloc_release_space(BTRFS_I(inode), | |
7517 | dio_data->data_reserved, pos, | |
7518 | dio_data->reserve, true); | |
7519 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); | |
7520 | extent_changeset_free(dio_data->data_reserved); | |
7521 | } | |
7522 | out: | |
0eb79294 JB |
7523 | /* |
7524 | * We're all done, we can re-set the current->journal_info now safely | |
7525 | * for our endio. | |
7526 | */ | |
7527 | if (dio_data->sync) { | |
7528 | ASSERT(current->journal_info == NULL); | |
7529 | current->journal_info = BTRFS_DIO_SYNC_STUB; | |
7530 | } | |
f85781fb GR |
7531 | kfree(dio_data); |
7532 | iomap->private = NULL; | |
7533 | ||
8b110e39 MX |
7534 | return ret; |
7535 | } | |
7536 | ||
769b4f24 | 7537 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
8b110e39 | 7538 | { |
769b4f24 OS |
7539 | /* |
7540 | * This implies a barrier so that stores to dio_bio->bi_status before | |
7541 | * this and loads of dio_bio->bi_status after this are fully ordered. | |
7542 | */ | |
7543 | if (!refcount_dec_and_test(&dip->refs)) | |
7544 | return; | |
8b110e39 | 7545 | |
769b4f24 | 7546 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
b672b5c1 NB |
7547 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
7548 | dip->logical_offset, | |
769b4f24 OS |
7549 | dip->bytes, |
7550 | !dip->dio_bio->bi_status); | |
7551 | } else { | |
7552 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, | |
7553 | dip->logical_offset, | |
7554 | dip->logical_offset + dip->bytes - 1); | |
8b110e39 MX |
7555 | } |
7556 | ||
f85781fb | 7557 | bio_endio(dip->dio_bio); |
769b4f24 | 7558 | kfree(dip); |
8b110e39 MX |
7559 | } |
7560 | ||
77d5d689 OS |
7561 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7562 | int mirror_num, | |
7563 | unsigned long bio_flags) | |
8b110e39 | 7564 | { |
77d5d689 | 7565 | struct btrfs_dio_private *dip = bio->bi_private; |
2ff7e61e | 7566 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7567 | blk_status_t ret; |
8b110e39 | 7568 | |
37226b21 | 7569 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7570 | |
5c047a69 | 7571 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
8b110e39 | 7572 | if (ret) |
ea057f6d | 7573 | return ret; |
8b110e39 | 7574 | |
77d5d689 | 7575 | refcount_inc(&dip->refs); |
08635bae | 7576 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
8b110e39 | 7577 | if (ret) |
fd9d6670 | 7578 | refcount_dec(&dip->refs); |
77d5d689 | 7579 | return ret; |
8b110e39 MX |
7580 | } |
7581 | ||
fd9d6670 OS |
7582 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
7583 | struct btrfs_io_bio *io_bio, | |
7584 | const bool uptodate) | |
4b46fce2 | 7585 | { |
fd9d6670 OS |
7586 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
7587 | const u32 sectorsize = fs_info->sectorsize; | |
7588 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
7589 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
7590 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); | |
17347cec LB |
7591 | struct bio_vec bvec; |
7592 | struct bvec_iter iter; | |
fd9d6670 OS |
7593 | u64 start = io_bio->logical; |
7594 | int icsum = 0; | |
58efbc9f | 7595 | blk_status_t err = BLK_STS_OK; |
4b46fce2 | 7596 | |
fd9d6670 OS |
7597 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
7598 | unsigned int i, nr_sectors, pgoff; | |
8b110e39 | 7599 | |
17347cec LB |
7600 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
7601 | pgoff = bvec.bv_offset; | |
fd9d6670 | 7602 | for (i = 0; i < nr_sectors; i++) { |
97bf5a55 | 7603 | ASSERT(pgoff < PAGE_SIZE); |
fd9d6670 OS |
7604 | if (uptodate && |
7605 | (!csum || !check_data_csum(inode, io_bio, icsum, | |
7606 | bvec.bv_page, pgoff, | |
7607 | start, sectorsize))) { | |
7608 | clean_io_failure(fs_info, failure_tree, io_tree, | |
7609 | start, bvec.bv_page, | |
7610 | btrfs_ino(BTRFS_I(inode)), | |
7611 | pgoff); | |
7612 | } else { | |
7613 | blk_status_t status; | |
7614 | ||
77d5d689 OS |
7615 | status = btrfs_submit_read_repair(inode, |
7616 | &io_bio->bio, | |
7617 | start - io_bio->logical, | |
fd9d6670 OS |
7618 | bvec.bv_page, pgoff, |
7619 | start, | |
7620 | start + sectorsize - 1, | |
77d5d689 OS |
7621 | io_bio->mirror_num, |
7622 | submit_dio_repair_bio); | |
fd9d6670 OS |
7623 | if (status) |
7624 | err = status; | |
7625 | } | |
7626 | start += sectorsize; | |
7627 | icsum++; | |
2dabb324 | 7628 | pgoff += sectorsize; |
2dabb324 | 7629 | } |
2c30c71b | 7630 | } |
c1dc0896 MX |
7631 | return err; |
7632 | } | |
7633 | ||
b672b5c1 | 7634 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
52427260 QW |
7635 | const u64 offset, const u64 bytes, |
7636 | const bool uptodate) | |
4b46fce2 | 7637 | { |
b672b5c1 | 7638 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4b46fce2 | 7639 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 | 7640 | struct btrfs_workqueue *wq; |
14543774 FM |
7641 | u64 ordered_offset = offset; |
7642 | u64 ordered_bytes = bytes; | |
67c003f9 | 7643 | u64 last_offset; |
4b46fce2 | 7644 | |
b672b5c1 | 7645 | if (btrfs_is_free_space_inode(inode)) |
52427260 | 7646 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 7647 | else |
52427260 | 7648 | wq = fs_info->endio_write_workers; |
52427260 | 7649 | |
b25f0d00 NB |
7650 | while (ordered_offset < offset + bytes) { |
7651 | last_offset = ordered_offset; | |
b672b5c1 | 7652 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
7095821e NB |
7653 | &ordered_offset, |
7654 | ordered_bytes, | |
7655 | uptodate)) { | |
a0cac0ec OS |
7656 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
7657 | NULL); | |
b25f0d00 NB |
7658 | btrfs_queue_work(wq, &ordered->work); |
7659 | } | |
7660 | /* | |
7661 | * If btrfs_dec_test_ordered_pending does not find any ordered | |
7662 | * extent in the range, we can exit. | |
7663 | */ | |
7664 | if (ordered_offset == last_offset) | |
7665 | return; | |
7666 | /* | |
7667 | * Our bio might span multiple ordered extents. In this case | |
52042d8e | 7668 | * we keep going until we have accounted the whole dio. |
b25f0d00 NB |
7669 | */ |
7670 | if (ordered_offset < offset + bytes) { | |
7671 | ordered_bytes = offset + bytes - ordered_offset; | |
7672 | ordered = NULL; | |
7673 | } | |
163cf09c | 7674 | } |
14543774 FM |
7675 | } |
7676 | ||
d0ee3934 | 7677 | static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, |
d0779291 | 7678 | struct bio *bio, u64 offset) |
eaf25d93 | 7679 | { |
c6100a4b | 7680 | struct inode *inode = private_data; |
c965d640 JT |
7681 | |
7682 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); | |
eaf25d93 CM |
7683 | } |
7684 | ||
4246a0b6 | 7685 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
7686 | { |
7687 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 7688 | blk_status_t err = bio->bi_status; |
e65e1535 | 7689 | |
8b110e39 MX |
7690 | if (err) |
7691 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 7692 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
7693 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
7694 | bio->bi_opf, | |
8b110e39 MX |
7695 | (unsigned long long)bio->bi_iter.bi_sector, |
7696 | bio->bi_iter.bi_size, err); | |
7697 | ||
769b4f24 OS |
7698 | if (bio_op(bio) == REQ_OP_READ) { |
7699 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), | |
fd9d6670 | 7700 | !err); |
e65e1535 MX |
7701 | } |
7702 | ||
769b4f24 OS |
7703 | if (err) |
7704 | dip->dio_bio->bi_status = err; | |
e65e1535 | 7705 | |
e65e1535 | 7706 | bio_put(bio); |
769b4f24 | 7707 | btrfs_dio_private_put(dip); |
c1dc0896 MX |
7708 | } |
7709 | ||
d0ee3934 DS |
7710 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
7711 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 7712 | { |
0b246afa | 7713 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 7714 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 7715 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 7716 | blk_status_t ret; |
e65e1535 | 7717 | |
4c274bc6 | 7718 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
7719 | if (async_submit) |
7720 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
7721 | ||
5fd02043 | 7722 | if (!write) { |
0b246afa | 7723 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
7724 | if (ret) |
7725 | goto err; | |
7726 | } | |
e65e1535 | 7727 | |
e6961cac | 7728 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
7729 | goto map; |
7730 | ||
7731 | if (write && async_submit) { | |
c6100a4b JB |
7732 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
7733 | file_offset, inode, | |
e288c080 | 7734 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 7735 | goto err; |
1ae39938 JB |
7736 | } else if (write) { |
7737 | /* | |
7738 | * If we aren't doing async submit, calculate the csum of the | |
7739 | * bio now. | |
7740 | */ | |
bd242a08 | 7741 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
1ae39938 JB |
7742 | if (ret) |
7743 | goto err; | |
23ea8e5a | 7744 | } else { |
85879573 OS |
7745 | u64 csum_offset; |
7746 | ||
7747 | csum_offset = file_offset - dip->logical_offset; | |
7748 | csum_offset >>= inode->i_sb->s_blocksize_bits; | |
7749 | csum_offset *= btrfs_super_csum_size(fs_info->super_copy); | |
7750 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; | |
c2db1073 | 7751 | } |
1ae39938 | 7752 | map: |
08635bae | 7753 | ret = btrfs_map_bio(fs_info, bio, 0); |
e65e1535 | 7754 | err: |
e65e1535 MX |
7755 | return ret; |
7756 | } | |
7757 | ||
c36cac28 OS |
7758 | /* |
7759 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked | |
7760 | * or ordered extents whether or not we submit any bios. | |
7761 | */ | |
7762 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, | |
7763 | struct inode *inode, | |
7764 | loff_t file_offset) | |
e65e1535 | 7765 | { |
c36cac28 | 7766 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
85879573 OS |
7767 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
7768 | size_t dip_size; | |
c36cac28 | 7769 | struct btrfs_dio_private *dip; |
c36cac28 | 7770 | |
85879573 OS |
7771 | dip_size = sizeof(*dip); |
7772 | if (!write && csum) { | |
7773 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7774 | const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); | |
7775 | size_t nblocks; | |
7776 | ||
7777 | nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; | |
7778 | dip_size += csum_size * nblocks; | |
7779 | } | |
7780 | ||
7781 | dip = kzalloc(dip_size, GFP_NOFS); | |
c36cac28 OS |
7782 | if (!dip) |
7783 | return NULL; | |
7784 | ||
c36cac28 OS |
7785 | dip->inode = inode; |
7786 | dip->logical_offset = file_offset; | |
7787 | dip->bytes = dio_bio->bi_iter.bi_size; | |
7788 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
c36cac28 | 7789 | dip->dio_bio = dio_bio; |
e3b318d1 | 7790 | refcount_set(&dip->refs, 1); |
c36cac28 OS |
7791 | return dip; |
7792 | } | |
7793 | ||
f85781fb GR |
7794 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
7795 | struct bio *dio_bio, loff_t file_offset) | |
c36cac28 OS |
7796 | { |
7797 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); | |
85879573 | 7798 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
0b246afa | 7799 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
769b4f24 OS |
7800 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
7801 | BTRFS_BLOCK_GROUP_RAID56_MASK); | |
c36cac28 | 7802 | struct btrfs_dio_private *dip; |
e65e1535 | 7803 | struct bio *bio; |
c36cac28 | 7804 | u64 start_sector; |
1ae39938 | 7805 | int async_submit = 0; |
725130ba LB |
7806 | u64 submit_len; |
7807 | int clone_offset = 0; | |
7808 | int clone_len; | |
5f4dc8fc | 7809 | int ret; |
58efbc9f | 7810 | blk_status_t status; |
89b798ad | 7811 | struct btrfs_io_geometry geom; |
f85781fb | 7812 | struct btrfs_dio_data *dio_data = iomap->private; |
e65e1535 | 7813 | |
c36cac28 OS |
7814 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
7815 | if (!dip) { | |
7816 | if (!write) { | |
7817 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
7818 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
7819 | } | |
7820 | dio_bio->bi_status = BLK_STS_RESOURCE; | |
f85781fb GR |
7821 | bio_endio(dio_bio); |
7822 | return BLK_QC_T_NONE; | |
c36cac28 | 7823 | } |
facc8a22 | 7824 | |
85879573 OS |
7825 | if (!write && csum) { |
7826 | /* | |
7827 | * Load the csums up front to reduce csum tree searches and | |
7828 | * contention when submitting bios. | |
7829 | */ | |
7830 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, | |
7831 | dip->csums); | |
7832 | if (status != BLK_STS_OK) | |
7833 | goto out_err; | |
02f57c7a JB |
7834 | } |
7835 | ||
769b4f24 OS |
7836 | start_sector = dio_bio->bi_iter.bi_sector; |
7837 | submit_len = dio_bio->bi_iter.bi_size; | |
53b381b3 | 7838 | |
3c91ee69 | 7839 | do { |
769b4f24 OS |
7840 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
7841 | start_sector << 9, submit_len, | |
7842 | &geom); | |
7843 | if (ret) { | |
7844 | status = errno_to_blk_status(ret); | |
7845 | goto out_err; | |
7846 | } | |
7847 | ASSERT(geom.len <= INT_MAX); | |
7848 | ||
89b798ad | 7849 | clone_len = min_t(int, submit_len, geom.len); |
02f57c7a | 7850 | |
725130ba LB |
7851 | /* |
7852 | * This will never fail as it's passing GPF_NOFS and | |
7853 | * the allocation is backed by btrfs_bioset. | |
7854 | */ | |
769b4f24 | 7855 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
725130ba LB |
7856 | bio->bi_private = dip; |
7857 | bio->bi_end_io = btrfs_end_dio_bio; | |
7858 | btrfs_io_bio(bio)->logical = file_offset; | |
7859 | ||
7860 | ASSERT(submit_len >= clone_len); | |
7861 | submit_len -= clone_len; | |
e65e1535 | 7862 | |
725130ba LB |
7863 | /* |
7864 | * Increase the count before we submit the bio so we know | |
7865 | * the end IO handler won't happen before we increase the | |
7866 | * count. Otherwise, the dip might get freed before we're | |
7867 | * done setting it up. | |
769b4f24 OS |
7868 | * |
7869 | * We transfer the initial reference to the last bio, so we | |
7870 | * don't need to increment the reference count for the last one. | |
725130ba | 7871 | */ |
769b4f24 OS |
7872 | if (submit_len > 0) { |
7873 | refcount_inc(&dip->refs); | |
7874 | /* | |
7875 | * If we are submitting more than one bio, submit them | |
7876 | * all asynchronously. The exception is RAID 5 or 6, as | |
7877 | * asynchronous checksums make it difficult to collect | |
7878 | * full stripe writes. | |
7879 | */ | |
7880 | if (!raid56) | |
7881 | async_submit = 1; | |
7882 | } | |
e65e1535 | 7883 | |
d0ee3934 | 7884 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
7885 | async_submit); |
7886 | if (status) { | |
725130ba | 7887 | bio_put(bio); |
769b4f24 OS |
7888 | if (submit_len > 0) |
7889 | refcount_dec(&dip->refs); | |
725130ba LB |
7890 | goto out_err; |
7891 | } | |
e65e1535 | 7892 | |
f85781fb | 7893 | dio_data->submitted += clone_len; |
725130ba LB |
7894 | clone_offset += clone_len; |
7895 | start_sector += clone_len >> 9; | |
7896 | file_offset += clone_len; | |
3c91ee69 | 7897 | } while (submit_len > 0); |
f85781fb | 7898 | return BLK_QC_T_NONE; |
e65e1535 | 7899 | |
e65e1535 | 7900 | out_err: |
769b4f24 OS |
7901 | dip->dio_bio->bi_status = status; |
7902 | btrfs_dio_private_put(dip); | |
f85781fb | 7903 | return BLK_QC_T_NONE; |
4b46fce2 JB |
7904 | } |
7905 | ||
f4c48b44 DS |
7906 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
7907 | const struct iov_iter *iter, loff_t offset) | |
7908 | { | |
7909 | int seg; | |
7910 | int i; | |
7911 | unsigned int blocksize_mask = fs_info->sectorsize - 1; | |
7912 | ssize_t retval = -EINVAL; | |
0934856d | 7913 | |
f4c48b44 DS |
7914 | if (offset & blocksize_mask) |
7915 | goto out; | |
7916 | ||
7917 | if (iov_iter_alignment(iter) & blocksize_mask) | |
7918 | goto out; | |
7919 | ||
7920 | /* If this is a write we don't need to check anymore */ | |
7921 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) | |
7922 | return 0; | |
7923 | /* | |
7924 | * Check to make sure we don't have duplicate iov_base's in this | |
7925 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
7926 | * when reading back. | |
7927 | */ | |
7928 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
7929 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
7930 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
7931 | goto out; | |
7932 | } | |
7933 | } | |
7934 | retval = 0; | |
7935 | out: | |
7936 | return retval; | |
7937 | } | |
7938 | ||
0eb79294 JB |
7939 | static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size, |
7940 | int error, unsigned flags) | |
7941 | { | |
7942 | /* | |
7943 | * Now if we're still in the context of our submitter we know we can't | |
7944 | * safely run generic_write_sync(), so clear our flag here so that the | |
7945 | * caller knows to follow up with a sync. | |
7946 | */ | |
7947 | if (current->journal_info == BTRFS_DIO_SYNC_STUB) { | |
7948 | current->journal_info = NULL; | |
7949 | return error; | |
7950 | } | |
7951 | ||
7952 | if (error) | |
7953 | return error; | |
7954 | ||
7955 | if (size) { | |
7956 | iocb->ki_flags |= IOCB_DSYNC; | |
7957 | return generic_write_sync(iocb, size); | |
7958 | } | |
7959 | ||
7960 | return 0; | |
7961 | } | |
7962 | ||
f85781fb GR |
7963 | static const struct iomap_ops btrfs_dio_iomap_ops = { |
7964 | .iomap_begin = btrfs_dio_iomap_begin, | |
7965 | .iomap_end = btrfs_dio_iomap_end, | |
7966 | }; | |
7967 | ||
7968 | static const struct iomap_dio_ops btrfs_dio_ops = { | |
7969 | .submit_io = btrfs_submit_direct, | |
7970 | }; | |
7971 | ||
0eb79294 JB |
7972 | static const struct iomap_dio_ops btrfs_sync_dops = { |
7973 | .submit_io = btrfs_submit_direct, | |
7974 | .end_io = btrfs_maybe_fsync_end_io, | |
7975 | }; | |
7976 | ||
f85781fb | 7977 | ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
f4c48b44 DS |
7978 | { |
7979 | struct file *file = iocb->ki_filp; | |
7980 | struct inode *inode = file->f_mapping->host; | |
7981 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7982 | struct extent_changeset *data_reserved = NULL; | |
7983 | loff_t offset = iocb->ki_pos; | |
7984 | size_t count = 0; | |
7985 | bool relock = false; | |
7986 | ssize_t ret; | |
7987 | ||
7988 | if (check_direct_IO(fs_info, iter, offset)) | |
7989 | return 0; | |
7990 | ||
7991 | count = iov_iter_count(iter); | |
7992 | if (iov_iter_rw(iter) == WRITE) { | |
7993 | /* | |
7994 | * If the write DIO is beyond the EOF, we need update | |
7995 | * the isize, but it is protected by i_mutex. So we can | |
7996 | * not unlock the i_mutex at this case. | |
7997 | */ | |
7998 | if (offset + count <= inode->i_size) { | |
7999 | inode_unlock(inode); | |
8000 | relock = true; | |
f4c48b44 DS |
8001 | } |
8002 | down_read(&BTRFS_I(inode)->dio_sem); | |
8003 | } | |
8004 | ||
0eb79294 JB |
8005 | /* |
8006 | * We have are actually a sync iocb, so we need our fancy endio to know | |
8007 | * if we need to sync. | |
8008 | */ | |
8009 | if (current->journal_info) | |
8010 | ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, | |
8011 | &btrfs_sync_dops, is_sync_kiocb(iocb)); | |
8012 | else | |
8013 | ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, | |
8014 | &btrfs_dio_ops, is_sync_kiocb(iocb)); | |
f85781fb GR |
8015 | |
8016 | if (ret == -ENOTBLK) | |
8017 | ret = 0; | |
8018 | ||
8019 | if (iov_iter_rw(iter) == WRITE) | |
f4c48b44 | 8020 | up_read(&BTRFS_I(inode)->dio_sem); |
f85781fb | 8021 | |
f4c48b44 DS |
8022 | if (relock) |
8023 | inode_lock(inode); | |
55e20bd1 | 8024 | |
f4c48b44 DS |
8025 | extent_changeset_free(data_reserved); |
8026 | return ret; | |
8027 | } | |
16432985 | 8028 | |
1506fcc8 | 8029 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 8030 | u64 start, u64 len) |
1506fcc8 | 8031 | { |
05dadc09 TI |
8032 | int ret; |
8033 | ||
45dd052e | 8034 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
8035 | if (ret) |
8036 | return ret; | |
8037 | ||
facee0a0 | 8038 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
1506fcc8 YS |
8039 | } |
8040 | ||
a52d9a80 | 8041 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8042 | { |
71ad38b4 | 8043 | return extent_read_full_page(page, btrfs_get_extent, 0); |
9ebefb18 | 8044 | } |
1832a6d5 | 8045 | |
a52d9a80 | 8046 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8047 | { |
be7bd730 JB |
8048 | struct inode *inode = page->mapping->host; |
8049 | int ret; | |
b888db2b CM |
8050 | |
8051 | if (current->flags & PF_MEMALLOC) { | |
8052 | redirty_page_for_writepage(wbc, page); | |
8053 | unlock_page(page); | |
8054 | return 0; | |
8055 | } | |
be7bd730 JB |
8056 | |
8057 | /* | |
8058 | * If we are under memory pressure we will call this directly from the | |
8059 | * VM, we need to make sure we have the inode referenced for the ordered | |
8060 | * extent. If not just return like we didn't do anything. | |
8061 | */ | |
8062 | if (!igrab(inode)) { | |
8063 | redirty_page_for_writepage(wbc, page); | |
8064 | return AOP_WRITEPAGE_ACTIVATE; | |
8065 | } | |
0a9b0e53 | 8066 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8067 | btrfs_add_delayed_iput(inode); |
8068 | return ret; | |
9ebefb18 CM |
8069 | } |
8070 | ||
48a3b636 ES |
8071 | static int btrfs_writepages(struct address_space *mapping, |
8072 | struct writeback_control *wbc) | |
b293f02e | 8073 | { |
8ae225a8 | 8074 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8075 | } |
8076 | ||
ba206a02 | 8077 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 8078 | { |
ba206a02 | 8079 | extent_readahead(rac); |
3ab2fb5a | 8080 | } |
2a3ff0ad | 8081 | |
e6dcd2dc | 8082 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8083 | { |
477a30ba | 8084 | int ret = try_release_extent_mapping(page, gfp_flags); |
d1b89bc0 GJ |
8085 | if (ret == 1) |
8086 | detach_page_private(page); | |
a52d9a80 | 8087 | return ret; |
39279cc3 CM |
8088 | } |
8089 | ||
e6dcd2dc CM |
8090 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8091 | { | |
98509cfc CM |
8092 | if (PageWriteback(page) || PageDirty(page)) |
8093 | return 0; | |
3ba7ab22 | 8094 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8095 | } |
8096 | ||
f8e66081 RG |
8097 | #ifdef CONFIG_MIGRATION |
8098 | static int btrfs_migratepage(struct address_space *mapping, | |
8099 | struct page *newpage, struct page *page, | |
8100 | enum migrate_mode mode) | |
8101 | { | |
8102 | int ret; | |
8103 | ||
8104 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); | |
8105 | if (ret != MIGRATEPAGE_SUCCESS) | |
8106 | return ret; | |
8107 | ||
d1b89bc0 GJ |
8108 | if (page_has_private(page)) |
8109 | attach_page_private(newpage, detach_page_private(page)); | |
f8e66081 RG |
8110 | |
8111 | if (PagePrivate2(page)) { | |
8112 | ClearPagePrivate2(page); | |
8113 | SetPagePrivate2(newpage); | |
8114 | } | |
8115 | ||
8116 | if (mode != MIGRATE_SYNC_NO_COPY) | |
8117 | migrate_page_copy(newpage, page); | |
8118 | else | |
8119 | migrate_page_states(newpage, page); | |
8120 | return MIGRATEPAGE_SUCCESS; | |
8121 | } | |
8122 | #endif | |
8123 | ||
d47992f8 LC |
8124 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8125 | unsigned int length) | |
39279cc3 | 8126 | { |
53ac7ead NB |
8127 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
8128 | struct extent_io_tree *tree = &inode->io_tree; | |
e6dcd2dc | 8129 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8130 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8131 | u64 page_start = page_offset(page); |
09cbfeaf | 8132 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8133 | u64 start; |
8134 | u64 end; | |
53ac7ead | 8135 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
39279cc3 | 8136 | |
8b62b72b CM |
8137 | /* |
8138 | * we have the page locked, so new writeback can't start, | |
8139 | * and the dirty bit won't be cleared while we are here. | |
8140 | * | |
8141 | * Wait for IO on this page so that we can safely clear | |
8142 | * the PagePrivate2 bit and do ordered accounting | |
8143 | */ | |
e6dcd2dc | 8144 | wait_on_page_writeback(page); |
8b62b72b | 8145 | |
e6dcd2dc CM |
8146 | if (offset) { |
8147 | btrfs_releasepage(page, GFP_NOFS); | |
8148 | return; | |
8149 | } | |
131e404a FDBM |
8150 | |
8151 | if (!inode_evicting) | |
ff13db41 | 8152 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8153 | again: |
8154 | start = page_start; | |
53ac7ead | 8155 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
e6dcd2dc | 8156 | if (ordered) { |
bffe633e OS |
8157 | end = min(page_end, |
8158 | ordered->file_offset + ordered->num_bytes - 1); | |
eb84ae03 CM |
8159 | /* |
8160 | * IO on this page will never be started, so we need | |
8161 | * to account for any ordered extents now | |
8162 | */ | |
131e404a | 8163 | if (!inode_evicting) |
dbfdb6d1 | 8164 | clear_extent_bit(tree, start, end, |
e182163d | 8165 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
131e404a | 8166 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8167 | EXTENT_DEFRAG, 1, 0, &cached_state); |
8b62b72b CM |
8168 | /* |
8169 | * whoever cleared the private bit is responsible | |
8170 | * for the finish_ordered_io | |
8171 | */ | |
77cef2ec JB |
8172 | if (TestClearPagePrivate2(page)) { |
8173 | struct btrfs_ordered_inode_tree *tree; | |
8174 | u64 new_len; | |
8175 | ||
53ac7ead | 8176 | tree = &inode->ordered_tree; |
77cef2ec JB |
8177 | |
8178 | spin_lock_irq(&tree->lock); | |
8179 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8180 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8181 | if (new_len < ordered->truncated_len) |
8182 | ordered->truncated_len = new_len; | |
8183 | spin_unlock_irq(&tree->lock); | |
8184 | ||
53ac7ead NB |
8185 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
8186 | start, | |
dbfdb6d1 | 8187 | end - start + 1, 1)) |
77cef2ec | 8188 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8189 | } |
e6dcd2dc | 8190 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8191 | if (!inode_evicting) { |
8192 | cached_state = NULL; | |
dbfdb6d1 | 8193 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8194 | &cached_state); |
8195 | } | |
dbfdb6d1 CR |
8196 | |
8197 | start = end + 1; | |
8198 | if (start < page_end) | |
8199 | goto again; | |
131e404a FDBM |
8200 | } |
8201 | ||
b9d0b389 QW |
8202 | /* |
8203 | * Qgroup reserved space handler | |
8204 | * Page here will be either | |
fa91e4aa QW |
8205 | * 1) Already written to disk or ordered extent already submitted |
8206 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. | |
8207 | * Qgroup will be handled by its qgroup_record then. | |
8208 | * btrfs_qgroup_free_data() call will do nothing here. | |
8209 | * | |
8210 | * 2) Not written to disk yet | |
8211 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED | |
8212 | * bit of its io_tree, and free the qgroup reserved data space. | |
8213 | * Since the IO will never happen for this page. | |
b9d0b389 | 8214 | */ |
53ac7ead | 8215 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a | 8216 | if (!inode_evicting) { |
e182163d | 8217 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
a7e3b975 FM |
8218 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
8219 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
ae0f1625 | 8220 | &cached_state); |
131e404a FDBM |
8221 | |
8222 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8223 | } |
e6dcd2dc | 8224 | |
4a096752 | 8225 | ClearPageChecked(page); |
d1b89bc0 | 8226 | detach_page_private(page); |
39279cc3 CM |
8227 | } |
8228 | ||
9ebefb18 CM |
8229 | /* |
8230 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8231 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8232 | * be careful to check for EOF conditions here. We set the page up correctly | |
8233 | * for a written page which means we get ENOSPC checking when writing into | |
8234 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8235 | * support these features. | |
8236 | * | |
8237 | * We are not allowed to take the i_mutex here so we have to play games to | |
8238 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8239 | * truncate_setsize() writes the inode size before removing pages, once we have |
8240 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8241 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8242 | * unlock the page. | |
8243 | */ | |
a528a241 | 8244 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8245 | { |
c2ec175c | 8246 | struct page *page = vmf->page; |
11bac800 | 8247 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8248 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8249 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8250 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8251 | struct extent_state *cached_state = NULL; |
364ecf36 | 8252 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
8253 | char *kaddr; |
8254 | unsigned long zero_start; | |
9ebefb18 | 8255 | loff_t size; |
a528a241 SJ |
8256 | vm_fault_t ret; |
8257 | int ret2; | |
9998eb70 | 8258 | int reserved = 0; |
d0b7da88 | 8259 | u64 reserved_space; |
a52d9a80 | 8260 | u64 page_start; |
e6dcd2dc | 8261 | u64 page_end; |
d0b7da88 CR |
8262 | u64 end; |
8263 | ||
09cbfeaf | 8264 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8265 | |
b2b5ef5c | 8266 | sb_start_pagefault(inode->i_sb); |
df480633 | 8267 | page_start = page_offset(page); |
09cbfeaf | 8268 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8269 | end = page_end; |
df480633 | 8270 | |
d0b7da88 CR |
8271 | /* |
8272 | * Reserving delalloc space after obtaining the page lock can lead to | |
8273 | * deadlock. For example, if a dirty page is locked by this function | |
8274 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8275 | * dirty page write out, then the btrfs_writepage() function could | |
8276 | * end up waiting indefinitely to get a lock on the page currently | |
8277 | * being processed by btrfs_page_mkwrite() function. | |
8278 | */ | |
e5b7231e NB |
8279 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
8280 | page_start, reserved_space); | |
a528a241 SJ |
8281 | if (!ret2) { |
8282 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8283 | reserved = 1; |
8284 | } | |
a528a241 SJ |
8285 | if (ret2) { |
8286 | ret = vmf_error(ret2); | |
9998eb70 CM |
8287 | if (reserved) |
8288 | goto out; | |
8289 | goto out_noreserve; | |
56a76f82 | 8290 | } |
1832a6d5 | 8291 | |
56a76f82 | 8292 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8293 | again: |
9ebefb18 | 8294 | lock_page(page); |
9ebefb18 | 8295 | size = i_size_read(inode); |
a52d9a80 | 8296 | |
9ebefb18 | 8297 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8298 | (page_start >= size)) { |
9ebefb18 CM |
8299 | /* page got truncated out from underneath us */ |
8300 | goto out_unlock; | |
8301 | } | |
e6dcd2dc CM |
8302 | wait_on_page_writeback(page); |
8303 | ||
ff13db41 | 8304 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
8305 | set_page_extent_mapped(page); |
8306 | ||
eb84ae03 CM |
8307 | /* |
8308 | * we can't set the delalloc bits if there are pending ordered | |
8309 | * extents. Drop our locks and wait for them to finish | |
8310 | */ | |
a776c6fa NB |
8311 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8312 | PAGE_SIZE); | |
e6dcd2dc | 8313 | if (ordered) { |
2ac55d41 | 8314 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8315 | &cached_state); |
e6dcd2dc | 8316 | unlock_page(page); |
eb84ae03 | 8317 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8318 | btrfs_put_ordered_extent(ordered); |
8319 | goto again; | |
8320 | } | |
8321 | ||
09cbfeaf | 8322 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8323 | reserved_space = round_up(size - page_start, |
0b246afa | 8324 | fs_info->sectorsize); |
09cbfeaf | 8325 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8326 | end = page_start + reserved_space - 1; |
86d52921 NB |
8327 | btrfs_delalloc_release_space(BTRFS_I(inode), |
8328 | data_reserved, page_start, | |
8329 | PAGE_SIZE - reserved_space, true); | |
d0b7da88 CR |
8330 | } |
8331 | } | |
8332 | ||
fbf19087 | 8333 | /* |
5416034f LB |
8334 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8335 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8336 | * bits, thus in this case for space account reason, we still need to | |
8337 | * clear any delalloc bits within this page range since we have to | |
8338 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8339 | */ |
d0b7da88 | 8340 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d OS |
8341 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
8342 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
fbf19087 | 8343 | |
c2566f22 | 8344 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
330a5827 | 8345 | &cached_state); |
a528a241 | 8346 | if (ret2) { |
2ac55d41 | 8347 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8348 | &cached_state); |
9ed74f2d JB |
8349 | ret = VM_FAULT_SIGBUS; |
8350 | goto out_unlock; | |
8351 | } | |
9ebefb18 CM |
8352 | |
8353 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8354 | if (page_start + PAGE_SIZE > size) |
7073017a | 8355 | zero_start = offset_in_page(size); |
9ebefb18 | 8356 | else |
09cbfeaf | 8357 | zero_start = PAGE_SIZE; |
9ebefb18 | 8358 | |
09cbfeaf | 8359 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 8360 | kaddr = kmap(page); |
09cbfeaf | 8361 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
8362 | flush_dcache_page(page); |
8363 | kunmap(page); | |
8364 | } | |
247e743c | 8365 | ClearPageChecked(page); |
e6dcd2dc | 8366 | set_page_dirty(page); |
50a9b214 | 8367 | SetPageUptodate(page); |
5a3f23d5 | 8368 | |
0b246afa | 8369 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 8370 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 8371 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8372 | |
e43bbe5e | 8373 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
9ebefb18 | 8374 | |
76de60ed YY |
8375 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8376 | sb_end_pagefault(inode->i_sb); | |
8377 | extent_changeset_free(data_reserved); | |
8378 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8379 | |
8380 | out_unlock: | |
9ebefb18 | 8381 | unlock_page(page); |
1832a6d5 | 8382 | out: |
8702ba93 | 8383 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
86d52921 | 8384 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
43b18595 | 8385 | reserved_space, (ret != 0)); |
9998eb70 | 8386 | out_noreserve: |
b2b5ef5c | 8387 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8388 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8389 | return ret; |
8390 | } | |
8391 | ||
213e8c55 | 8392 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8393 | { |
0b246afa | 8394 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8395 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8396 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8397 | int ret; |
39279cc3 | 8398 | struct btrfs_trans_handle *trans; |
0b246afa | 8399 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 8400 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8401 | |
213e8c55 FM |
8402 | if (!skip_writeback) { |
8403 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8404 | (u64)-1); | |
8405 | if (ret) | |
8406 | return ret; | |
8407 | } | |
39279cc3 | 8408 | |
fcb80c2a | 8409 | /* |
f7e9e8fc OS |
8410 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8411 | * things going on here: | |
fcb80c2a | 8412 | * |
f7e9e8fc | 8413 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8414 | * |
f7e9e8fc | 8415 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8416 | * be free'd up by the truncate operation, but also have some slack |
8417 | * space reserved in case it uses space during the truncate (thank you | |
8418 | * very much snapshotting). | |
8419 | * | |
f7e9e8fc | 8420 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8421 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8422 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8423 | * doesn't end up using space reserved for updating the inode. We also |
8424 | * need to be able to stop the transaction and start a new one, which | |
8425 | * means we need to be able to update the inode several times, and we | |
8426 | * have no idea of knowing how many times that will be, so we can't just | |
8427 | * reserve 1 item for the entirety of the operation, so that has to be | |
8428 | * done separately as well. | |
fcb80c2a JB |
8429 | * |
8430 | * So that leaves us with | |
8431 | * | |
f7e9e8fc | 8432 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8433 | * transaction reservation. |
f7e9e8fc | 8434 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8435 | * updating the inode. |
8436 | */ | |
2ff7e61e | 8437 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8438 | if (!rsv) |
8439 | return -ENOMEM; | |
4a338542 | 8440 | rsv->size = min_size; |
ca7e70f5 | 8441 | rsv->failfast = 1; |
f0cd846e | 8442 | |
907cbceb | 8443 | /* |
07127184 | 8444 | * 1 for the truncate slack space |
907cbceb JB |
8445 | * 1 for updating the inode. |
8446 | */ | |
f3fe820c | 8447 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8448 | if (IS_ERR(trans)) { |
ad7e1a74 | 8449 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8450 | goto out; |
8451 | } | |
f0cd846e | 8452 | |
907cbceb | 8453 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8454 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8455 | min_size, false); |
fcb80c2a | 8456 | BUG_ON(ret); |
f0cd846e | 8457 | |
5dc562c5 JB |
8458 | /* |
8459 | * So if we truncate and then write and fsync we normally would just | |
8460 | * write the extents that changed, which is a problem if we need to | |
8461 | * first truncate that entire inode. So set this flag so we write out | |
8462 | * all of the extents in the inode to the sync log so we're completely | |
8463 | * safe. | |
8464 | */ | |
8465 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8466 | trans->block_rsv = rsv; |
907cbceb | 8467 | |
8082510e YZ |
8468 | while (1) { |
8469 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8470 | inode->i_size, | |
8471 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 8472 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8473 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8474 | break; |
39279cc3 | 8475 | |
8082510e | 8476 | ret = btrfs_update_inode(trans, root, inode); |
ad7e1a74 | 8477 | if (ret) |
3893e33b | 8478 | break; |
ca7e70f5 | 8479 | |
3a45bb20 | 8480 | btrfs_end_transaction(trans); |
2ff7e61e | 8481 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8482 | |
8483 | trans = btrfs_start_transaction(root, 2); | |
8484 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8485 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8486 | trans = NULL; |
8487 | break; | |
8488 | } | |
8489 | ||
63f018be | 8490 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8491 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8492 | rsv, min_size, false); |
ca7e70f5 JB |
8493 | BUG_ON(ret); /* shouldn't happen */ |
8494 | trans->block_rsv = rsv; | |
8082510e YZ |
8495 | } |
8496 | ||
ddfae63c JB |
8497 | /* |
8498 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
8499 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
8500 | * we've truncated everything except the last little bit, and can do | |
8501 | * btrfs_truncate_block and then update the disk_i_size. | |
8502 | */ | |
8503 | if (ret == NEED_TRUNCATE_BLOCK) { | |
8504 | btrfs_end_transaction(trans); | |
8505 | btrfs_btree_balance_dirty(fs_info); | |
8506 | ||
8507 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
8508 | if (ret) | |
8509 | goto out; | |
8510 | trans = btrfs_start_transaction(root, 1); | |
8511 | if (IS_ERR(trans)) { | |
8512 | ret = PTR_ERR(trans); | |
8513 | goto out; | |
8514 | } | |
d923afe9 | 8515 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
ddfae63c JB |
8516 | } |
8517 | ||
917c16b2 | 8518 | if (trans) { |
ad7e1a74 OS |
8519 | int ret2; |
8520 | ||
0b246afa | 8521 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 OS |
8522 | ret2 = btrfs_update_inode(trans, root, inode); |
8523 | if (ret2 && !ret) | |
8524 | ret = ret2; | |
7b128766 | 8525 | |
ad7e1a74 OS |
8526 | ret2 = btrfs_end_transaction(trans); |
8527 | if (ret2 && !ret) | |
8528 | ret = ret2; | |
2ff7e61e | 8529 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8530 | } |
fcb80c2a | 8531 | out: |
2ff7e61e | 8532 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 8533 | |
ad7e1a74 | 8534 | return ret; |
39279cc3 CM |
8535 | } |
8536 | ||
d352ac68 CM |
8537 | /* |
8538 | * create a new subvolume directory/inode (helper for the ioctl). | |
8539 | */ | |
d2fb3437 | 8540 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8541 | struct btrfs_root *new_root, |
8542 | struct btrfs_root *parent_root, | |
8543 | u64 new_dirid) | |
39279cc3 | 8544 | { |
39279cc3 | 8545 | struct inode *inode; |
76dda93c | 8546 | int err; |
00e4e6b3 | 8547 | u64 index = 0; |
39279cc3 | 8548 | |
12fc9d09 FA |
8549 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8550 | new_dirid, new_dirid, | |
8551 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8552 | &index); | |
54aa1f4d | 8553 | if (IS_ERR(inode)) |
f46b5a66 | 8554 | return PTR_ERR(inode); |
39279cc3 CM |
8555 | inode->i_op = &btrfs_dir_inode_operations; |
8556 | inode->i_fop = &btrfs_dir_file_operations; | |
8557 | ||
bfe86848 | 8558 | set_nlink(inode, 1); |
6ef06d27 | 8559 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 8560 | unlock_new_inode(inode); |
3b96362c | 8561 | |
63541927 FDBM |
8562 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8563 | if (err) | |
8564 | btrfs_err(new_root->fs_info, | |
351fd353 | 8565 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8566 | new_root->root_key.objectid, err); |
8567 | ||
76dda93c | 8568 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8569 | |
76dda93c | 8570 | iput(inode); |
ce598979 | 8571 | return err; |
39279cc3 CM |
8572 | } |
8573 | ||
39279cc3 CM |
8574 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8575 | { | |
69fe2d75 | 8576 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8577 | struct btrfs_inode *ei; |
2ead6ae7 | 8578 | struct inode *inode; |
39279cc3 | 8579 | |
712e36c5 | 8580 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
8581 | if (!ei) |
8582 | return NULL; | |
2ead6ae7 YZ |
8583 | |
8584 | ei->root = NULL; | |
2ead6ae7 | 8585 | ei->generation = 0; |
15ee9bc7 | 8586 | ei->last_trans = 0; |
257c62e1 | 8587 | ei->last_sub_trans = 0; |
e02119d5 | 8588 | ei->logged_trans = 0; |
2ead6ae7 | 8589 | ei->delalloc_bytes = 0; |
a7e3b975 | 8590 | ei->new_delalloc_bytes = 0; |
47059d93 | 8591 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8592 | ei->disk_i_size = 0; |
8593 | ei->flags = 0; | |
7709cde3 | 8594 | ei->csum_bytes = 0; |
2ead6ae7 | 8595 | ei->index_cnt = (u64)-1; |
67de1176 | 8596 | ei->dir_index = 0; |
2ead6ae7 | 8597 | ei->last_unlink_trans = 0; |
3ebac17c | 8598 | ei->last_reflink_trans = 0; |
46d8bc34 | 8599 | ei->last_log_commit = 0; |
2ead6ae7 | 8600 | |
9e0baf60 JB |
8601 | spin_lock_init(&ei->lock); |
8602 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8603 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8604 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8605 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8606 | ei->runtime_flags = 0; |
b52aa8c9 | 8607 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8608 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8609 | |
16cdcec7 MX |
8610 | ei->delayed_node = NULL; |
8611 | ||
9cc97d64 | 8612 | ei->i_otime.tv_sec = 0; |
8613 | ei->i_otime.tv_nsec = 0; | |
8614 | ||
2ead6ae7 | 8615 | inode = &ei->vfs_inode; |
a8067e02 | 8616 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 QW |
8617 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
8618 | extent_io_tree_init(fs_info, &ei->io_failure_tree, | |
8619 | IO_TREE_INODE_IO_FAILURE, inode); | |
41a2ee75 JB |
8620 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
8621 | IO_TREE_INODE_FILE_EXTENT, inode); | |
7b439738 DS |
8622 | ei->io_tree.track_uptodate = true; |
8623 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 8624 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8625 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 8626 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8627 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8628 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8629 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 8630 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
8631 | |
8632 | return inode; | |
39279cc3 CM |
8633 | } |
8634 | ||
aaedb55b JB |
8635 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8636 | void btrfs_test_destroy_inode(struct inode *inode) | |
8637 | { | |
dcdbc059 | 8638 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
8639 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8640 | } | |
8641 | #endif | |
8642 | ||
26602cab | 8643 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8644 | { |
fa0d7e3d NP |
8645 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8646 | } | |
8647 | ||
39279cc3 CM |
8648 | void btrfs_destroy_inode(struct inode *inode) |
8649 | { | |
0b246afa | 8650 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 8651 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
8652 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8653 | ||
b3d9b7a3 | 8654 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 8655 | WARN_ON(inode->i_data.nrpages); |
69fe2d75 JB |
8656 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
8657 | WARN_ON(BTRFS_I(inode)->block_rsv.size); | |
9e0baf60 | 8658 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7709cde3 | 8659 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 8660 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 8661 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 8662 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 8663 | |
a6dbd429 JB |
8664 | /* |
8665 | * This can happen where we create an inode, but somebody else also | |
8666 | * created the same inode and we need to destroy the one we already | |
8667 | * created. | |
8668 | */ | |
8669 | if (!root) | |
26602cab | 8670 | return; |
a6dbd429 | 8671 | |
d397712b | 8672 | while (1) { |
6d072c8e NB |
8673 | ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), |
8674 | (u64)-1); | |
e6dcd2dc CM |
8675 | if (!ordered) |
8676 | break; | |
8677 | else { | |
0b246afa | 8678 | btrfs_err(fs_info, |
5d163e0e | 8679 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8680 | ordered->file_offset, ordered->num_bytes); |
e6dcd2dc CM |
8681 | btrfs_remove_ordered_extent(inode, ordered); |
8682 | btrfs_put_ordered_extent(ordered); | |
8683 | btrfs_put_ordered_extent(ordered); | |
8684 | } | |
8685 | } | |
cfdd4592 | 8686 | btrfs_qgroup_check_reserved_leak(BTRFS_I(inode)); |
b79b7249 | 8687 | inode_tree_del(BTRFS_I(inode)); |
dcdbc059 | 8688 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
41a2ee75 | 8689 | btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1); |
5c8fd99f | 8690 | btrfs_put_root(BTRFS_I(inode)->root); |
39279cc3 CM |
8691 | } |
8692 | ||
45321ac5 | 8693 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8694 | { |
8695 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8696 | |
6379ef9f NA |
8697 | if (root == NULL) |
8698 | return 1; | |
8699 | ||
fa6ac876 | 8700 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8701 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8702 | return 1; |
76dda93c | 8703 | else |
45321ac5 | 8704 | return generic_drop_inode(inode); |
76dda93c YZ |
8705 | } |
8706 | ||
0ee0fda0 | 8707 | static void init_once(void *foo) |
39279cc3 CM |
8708 | { |
8709 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8710 | ||
8711 | inode_init_once(&ei->vfs_inode); | |
8712 | } | |
8713 | ||
e67c718b | 8714 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8715 | { |
8c0a8537 KS |
8716 | /* |
8717 | * Make sure all delayed rcu free inodes are flushed before we | |
8718 | * destroy cache. | |
8719 | */ | |
8720 | rcu_barrier(); | |
5598e900 KM |
8721 | kmem_cache_destroy(btrfs_inode_cachep); |
8722 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
8723 | kmem_cache_destroy(btrfs_path_cachep); |
8724 | kmem_cache_destroy(btrfs_free_space_cachep); | |
3acd4850 | 8725 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
39279cc3 CM |
8726 | } |
8727 | ||
f5c29bd9 | 8728 | int __init btrfs_init_cachep(void) |
39279cc3 | 8729 | { |
837e1972 | 8730 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8731 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8732 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8733 | init_once); | |
39279cc3 CM |
8734 | if (!btrfs_inode_cachep) |
8735 | goto fail; | |
9601e3f6 | 8736 | |
837e1972 | 8737 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 8738 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 8739 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8740 | if (!btrfs_trans_handle_cachep) |
8741 | goto fail; | |
9601e3f6 | 8742 | |
837e1972 | 8743 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 8744 | sizeof(struct btrfs_path), 0, |
fba4b697 | 8745 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8746 | if (!btrfs_path_cachep) |
8747 | goto fail; | |
9601e3f6 | 8748 | |
837e1972 | 8749 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 8750 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 8751 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
8752 | if (!btrfs_free_space_cachep) |
8753 | goto fail; | |
8754 | ||
3acd4850 CL |
8755 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
8756 | PAGE_SIZE, PAGE_SIZE, | |
8757 | SLAB_RED_ZONE, NULL); | |
8758 | if (!btrfs_free_space_bitmap_cachep) | |
8759 | goto fail; | |
8760 | ||
39279cc3 CM |
8761 | return 0; |
8762 | fail: | |
8763 | btrfs_destroy_cachep(); | |
8764 | return -ENOMEM; | |
8765 | } | |
8766 | ||
a528d35e DH |
8767 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
8768 | u32 request_mask, unsigned int flags) | |
39279cc3 | 8769 | { |
df0af1a5 | 8770 | u64 delalloc_bytes; |
a528d35e | 8771 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 8772 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
8773 | u32 bi_flags = BTRFS_I(inode)->flags; |
8774 | ||
8775 | stat->result_mask |= STATX_BTIME; | |
8776 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
8777 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
8778 | if (bi_flags & BTRFS_INODE_APPEND) | |
8779 | stat->attributes |= STATX_ATTR_APPEND; | |
8780 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8781 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8782 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8783 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8784 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8785 | stat->attributes |= STATX_ATTR_NODUMP; | |
8786 | ||
8787 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8788 | STATX_ATTR_COMPRESSED | | |
8789 | STATX_ATTR_IMMUTABLE | | |
8790 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8791 | |
39279cc3 | 8792 | generic_fillattr(inode, stat); |
0ee5dc67 | 8793 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8794 | |
8795 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8796 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 8797 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 8798 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 8799 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
8800 | return 0; |
8801 | } | |
8802 | ||
cdd1fedf DF |
8803 | static int btrfs_rename_exchange(struct inode *old_dir, |
8804 | struct dentry *old_dentry, | |
8805 | struct inode *new_dir, | |
8806 | struct dentry *new_dentry) | |
8807 | { | |
0b246afa | 8808 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
8809 | struct btrfs_trans_handle *trans; |
8810 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
8811 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
8812 | struct inode *new_inode = new_dentry->d_inode; | |
8813 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 8814 | struct timespec64 ctime = current_time(old_inode); |
4a0cc7ca NB |
8815 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
8816 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
8817 | u64 old_idx = 0; |
8818 | u64 new_idx = 0; | |
cdd1fedf | 8819 | int ret; |
75b463d2 | 8820 | int ret2; |
86e8aa0e FM |
8821 | bool root_log_pinned = false; |
8822 | bool dest_log_pinned = false; | |
cdd1fedf DF |
8823 | |
8824 | /* we only allow rename subvolume link between subvolumes */ | |
8825 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
8826 | return -EXDEV; | |
8827 | ||
8828 | /* close the race window with snapshot create/destroy ioctl */ | |
943eb3bf JB |
8829 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
8830 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8831 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8832 | |
8833 | /* | |
8834 | * We want to reserve the absolute worst case amount of items. So if | |
8835 | * both inodes are subvols and we need to unlink them then that would | |
8836 | * require 4 item modifications, but if they are both normal inodes it | |
8837 | * would require 5 item modifications, so we'll assume their normal | |
8838 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
8839 | * should cover the worst case number of items we'll modify. | |
8840 | */ | |
8841 | trans = btrfs_start_transaction(root, 12); | |
8842 | if (IS_ERR(trans)) { | |
8843 | ret = PTR_ERR(trans); | |
8844 | goto out_notrans; | |
8845 | } | |
8846 | ||
3e174099 JB |
8847 | if (dest != root) |
8848 | btrfs_record_root_in_trans(trans, dest); | |
8849 | ||
cdd1fedf DF |
8850 | /* |
8851 | * We need to find a free sequence number both in the source and | |
8852 | * in the destination directory for the exchange. | |
8853 | */ | |
877574e2 | 8854 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
8855 | if (ret) |
8856 | goto out_fail; | |
877574e2 | 8857 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
8858 | if (ret) |
8859 | goto out_fail; | |
8860 | ||
8861 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
8862 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
8863 | ||
8864 | /* Reference for the source. */ | |
8865 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8866 | /* force full log commit if subvolume involved. */ | |
90787766 | 8867 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8868 | } else { |
376e5a57 FM |
8869 | btrfs_pin_log_trans(root); |
8870 | root_log_pinned = true; | |
cdd1fedf DF |
8871 | ret = btrfs_insert_inode_ref(trans, dest, |
8872 | new_dentry->d_name.name, | |
8873 | new_dentry->d_name.len, | |
8874 | old_ino, | |
f85b7379 DS |
8875 | btrfs_ino(BTRFS_I(new_dir)), |
8876 | old_idx); | |
cdd1fedf DF |
8877 | if (ret) |
8878 | goto out_fail; | |
cdd1fedf DF |
8879 | } |
8880 | ||
8881 | /* And now for the dest. */ | |
8882 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8883 | /* force full log commit if subvolume involved. */ | |
90787766 | 8884 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8885 | } else { |
376e5a57 FM |
8886 | btrfs_pin_log_trans(dest); |
8887 | dest_log_pinned = true; | |
cdd1fedf DF |
8888 | ret = btrfs_insert_inode_ref(trans, root, |
8889 | old_dentry->d_name.name, | |
8890 | old_dentry->d_name.len, | |
8891 | new_ino, | |
f85b7379 DS |
8892 | btrfs_ino(BTRFS_I(old_dir)), |
8893 | new_idx); | |
cdd1fedf DF |
8894 | if (ret) |
8895 | goto out_fail; | |
cdd1fedf DF |
8896 | } |
8897 | ||
8898 | /* Update inode version and ctime/mtime. */ | |
8899 | inode_inc_iversion(old_dir); | |
8900 | inode_inc_iversion(new_dir); | |
8901 | inode_inc_iversion(old_inode); | |
8902 | inode_inc_iversion(new_inode); | |
8903 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
8904 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
8905 | old_inode->i_ctime = ctime; | |
8906 | new_inode->i_ctime = ctime; | |
8907 | ||
8908 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
8909 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
8910 | BTRFS_I(old_inode), 1); | |
8911 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
8912 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
8913 | } |
8914 | ||
8915 | /* src is a subvolume */ | |
8916 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8917 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
cdd1fedf | 8918 | } else { /* src is an inode */ |
4ec5934e NB |
8919 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
8920 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
8921 | old_dentry->d_name.name, |
8922 | old_dentry->d_name.len); | |
8923 | if (!ret) | |
8924 | ret = btrfs_update_inode(trans, root, old_inode); | |
8925 | } | |
8926 | if (ret) { | |
66642832 | 8927 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8928 | goto out_fail; |
8929 | } | |
8930 | ||
8931 | /* dest is a subvolume */ | |
8932 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8933 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
cdd1fedf | 8934 | } else { /* dest is an inode */ |
4ec5934e NB |
8935 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
8936 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
8937 | new_dentry->d_name.name, |
8938 | new_dentry->d_name.len); | |
8939 | if (!ret) | |
8940 | ret = btrfs_update_inode(trans, dest, new_inode); | |
8941 | } | |
8942 | if (ret) { | |
66642832 | 8943 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8944 | goto out_fail; |
8945 | } | |
8946 | ||
db0a669f | 8947 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
8948 | new_dentry->d_name.name, |
8949 | new_dentry->d_name.len, 0, old_idx); | |
8950 | if (ret) { | |
66642832 | 8951 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8952 | goto out_fail; |
8953 | } | |
8954 | ||
db0a669f | 8955 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
8956 | old_dentry->d_name.name, |
8957 | old_dentry->d_name.len, 0, new_idx); | |
8958 | if (ret) { | |
66642832 | 8959 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8960 | goto out_fail; |
8961 | } | |
8962 | ||
8963 | if (old_inode->i_nlink == 1) | |
8964 | BTRFS_I(old_inode)->dir_index = old_idx; | |
8965 | if (new_inode->i_nlink == 1) | |
8966 | BTRFS_I(new_inode)->dir_index = new_idx; | |
8967 | ||
86e8aa0e | 8968 | if (root_log_pinned) { |
75b463d2 FM |
8969 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
8970 | new_dentry->d_parent); | |
cdd1fedf | 8971 | btrfs_end_log_trans(root); |
86e8aa0e | 8972 | root_log_pinned = false; |
cdd1fedf | 8973 | } |
86e8aa0e | 8974 | if (dest_log_pinned) { |
75b463d2 FM |
8975 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
8976 | old_dentry->d_parent); | |
cdd1fedf | 8977 | btrfs_end_log_trans(dest); |
86e8aa0e | 8978 | dest_log_pinned = false; |
cdd1fedf DF |
8979 | } |
8980 | out_fail: | |
86e8aa0e FM |
8981 | /* |
8982 | * If we have pinned a log and an error happened, we unpin tasks | |
8983 | * trying to sync the log and force them to fallback to a transaction | |
8984 | * commit if the log currently contains any of the inodes involved in | |
8985 | * this rename operation (to ensure we do not persist a log with an | |
8986 | * inconsistent state for any of these inodes or leading to any | |
8987 | * inconsistencies when replayed). If the transaction was aborted, the | |
8988 | * abortion reason is propagated to userspace when attempting to commit | |
8989 | * the transaction. If the log does not contain any of these inodes, we | |
8990 | * allow the tasks to sync it. | |
8991 | */ | |
8992 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
8993 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
8994 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
8995 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 8996 | (new_inode && |
0f8939b8 | 8997 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 8998 | btrfs_set_log_full_commit(trans); |
86e8aa0e FM |
8999 | |
9000 | if (root_log_pinned) { | |
9001 | btrfs_end_log_trans(root); | |
9002 | root_log_pinned = false; | |
9003 | } | |
9004 | if (dest_log_pinned) { | |
9005 | btrfs_end_log_trans(dest); | |
9006 | dest_log_pinned = false; | |
9007 | } | |
9008 | } | |
75b463d2 FM |
9009 | ret2 = btrfs_end_transaction(trans); |
9010 | ret = ret ? ret : ret2; | |
cdd1fedf | 9011 | out_notrans: |
943eb3bf JB |
9012 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
9013 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9014 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9015 | |
9016 | return ret; | |
9017 | } | |
9018 | ||
9019 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9020 | struct btrfs_root *root, | |
9021 | struct inode *dir, | |
9022 | struct dentry *dentry) | |
9023 | { | |
9024 | int ret; | |
9025 | struct inode *inode; | |
9026 | u64 objectid; | |
9027 | u64 index; | |
9028 | ||
9029 | ret = btrfs_find_free_ino(root, &objectid); | |
9030 | if (ret) | |
9031 | return ret; | |
9032 | ||
9033 | inode = btrfs_new_inode(trans, root, dir, | |
9034 | dentry->d_name.name, | |
9035 | dentry->d_name.len, | |
4a0cc7ca | 9036 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9037 | objectid, |
9038 | S_IFCHR | WHITEOUT_MODE, | |
9039 | &index); | |
9040 | ||
9041 | if (IS_ERR(inode)) { | |
9042 | ret = PTR_ERR(inode); | |
9043 | return ret; | |
9044 | } | |
9045 | ||
9046 | inode->i_op = &btrfs_special_inode_operations; | |
9047 | init_special_inode(inode, inode->i_mode, | |
9048 | WHITEOUT_DEV); | |
9049 | ||
9050 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9051 | &dentry->d_name); | |
9052 | if (ret) | |
c9901618 | 9053 | goto out; |
cdd1fedf | 9054 | |
cef415af NB |
9055 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9056 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9057 | if (ret) |
c9901618 | 9058 | goto out; |
cdd1fedf DF |
9059 | |
9060 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9061 | out: |
cdd1fedf | 9062 | unlock_new_inode(inode); |
c9901618 FM |
9063 | if (ret) |
9064 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9065 | iput(inode); |
9066 | ||
c9901618 | 9067 | return ret; |
cdd1fedf DF |
9068 | } |
9069 | ||
d397712b | 9070 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9071 | struct inode *new_dir, struct dentry *new_dentry, |
9072 | unsigned int flags) | |
39279cc3 | 9073 | { |
0b246afa | 9074 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9075 | struct btrfs_trans_handle *trans; |
5062af35 | 9076 | unsigned int trans_num_items; |
39279cc3 | 9077 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9078 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9079 | struct inode *new_inode = d_inode(new_dentry); |
9080 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9081 | u64 index = 0; |
39279cc3 | 9082 | int ret; |
75b463d2 | 9083 | int ret2; |
4a0cc7ca | 9084 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9085 | bool log_pinned = false; |
39279cc3 | 9086 | |
4a0cc7ca | 9087 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9088 | return -EPERM; |
9089 | ||
4df27c4d | 9090 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9091 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9092 | return -EXDEV; |
9093 | ||
33345d01 | 9094 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9095 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9096 | return -ENOTEMPTY; |
5f39d397 | 9097 | |
4df27c4d YZ |
9098 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9099 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9100 | return -ENOTEMPTY; | |
9c52057c CM |
9101 | |
9102 | ||
9103 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9104 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9105 | new_dentry->d_name.name, |
9106 | new_dentry->d_name.len); | |
9107 | ||
9108 | if (ret) { | |
9109 | if (ret == -EEXIST) { | |
9110 | /* we shouldn't get | |
9111 | * eexist without a new_inode */ | |
fae7f21c | 9112 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9113 | return ret; |
9114 | } | |
9115 | } else { | |
9116 | /* maybe -EOVERFLOW */ | |
9117 | return ret; | |
9118 | } | |
9119 | } | |
9120 | ret = 0; | |
9121 | ||
5a3f23d5 | 9122 | /* |
8d875f95 CM |
9123 | * we're using rename to replace one file with another. Start IO on it |
9124 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9125 | */ |
8d875f95 | 9126 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9127 | filemap_flush(old_inode->i_mapping); |
9128 | ||
76dda93c | 9129 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9130 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9131 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9132 | /* |
9133 | * We want to reserve the absolute worst case amount of items. So if | |
9134 | * both inodes are subvols and we need to unlink them then that would | |
9135 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9136 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9137 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9138 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9139 | * If our rename has the whiteout flag, we need more 5 units for the |
9140 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9141 | * when selinux is enabled). | |
a22285a6 | 9142 | */ |
5062af35 FM |
9143 | trans_num_items = 11; |
9144 | if (flags & RENAME_WHITEOUT) | |
9145 | trans_num_items += 5; | |
9146 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9147 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9148 | ret = PTR_ERR(trans); |
9149 | goto out_notrans; | |
9150 | } | |
76dda93c | 9151 | |
4df27c4d YZ |
9152 | if (dest != root) |
9153 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9154 | |
877574e2 | 9155 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9156 | if (ret) |
9157 | goto out_fail; | |
5a3f23d5 | 9158 | |
67de1176 | 9159 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9160 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9161 | /* force full log commit if subvolume involved. */ |
90787766 | 9162 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9163 | } else { |
c4aba954 FM |
9164 | btrfs_pin_log_trans(root); |
9165 | log_pinned = true; | |
a5719521 YZ |
9166 | ret = btrfs_insert_inode_ref(trans, dest, |
9167 | new_dentry->d_name.name, | |
9168 | new_dentry->d_name.len, | |
33345d01 | 9169 | old_ino, |
4a0cc7ca | 9170 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9171 | if (ret) |
9172 | goto out_fail; | |
4df27c4d | 9173 | } |
5a3f23d5 | 9174 | |
0c4d2d95 JB |
9175 | inode_inc_iversion(old_dir); |
9176 | inode_inc_iversion(new_dir); | |
9177 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9178 | old_dir->i_ctime = old_dir->i_mtime = |
9179 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9180 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9181 | |
12fcfd22 | 9182 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9183 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9184 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9185 | |
33345d01 | 9186 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
045d3967 | 9187 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
4df27c4d | 9188 | } else { |
4ec5934e NB |
9189 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9190 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9191 | old_dentry->d_name.name, |
9192 | old_dentry->d_name.len); | |
9193 | if (!ret) | |
9194 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9195 | } |
79787eaa | 9196 | if (ret) { |
66642832 | 9197 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9198 | goto out_fail; |
9199 | } | |
39279cc3 CM |
9200 | |
9201 | if (new_inode) { | |
0c4d2d95 | 9202 | inode_inc_iversion(new_inode); |
c2050a45 | 9203 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9204 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9205 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 9206 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
4df27c4d YZ |
9207 | BUG_ON(new_inode->i_nlink == 0); |
9208 | } else { | |
4ec5934e NB |
9209 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9210 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
9211 | new_dentry->d_name.name, |
9212 | new_dentry->d_name.len); | |
9213 | } | |
4ef31a45 | 9214 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9215 | ret = btrfs_orphan_add(trans, |
9216 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9217 | if (ret) { |
66642832 | 9218 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9219 | goto out_fail; |
9220 | } | |
39279cc3 | 9221 | } |
aec7477b | 9222 | |
db0a669f | 9223 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 9224 | new_dentry->d_name.name, |
a5719521 | 9225 | new_dentry->d_name.len, 0, index); |
79787eaa | 9226 | if (ret) { |
66642832 | 9227 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9228 | goto out_fail; |
9229 | } | |
39279cc3 | 9230 | |
67de1176 MX |
9231 | if (old_inode->i_nlink == 1) |
9232 | BTRFS_I(old_inode)->dir_index = index; | |
9233 | ||
3dc9e8f7 | 9234 | if (log_pinned) { |
75b463d2 FM |
9235 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9236 | new_dentry->d_parent); | |
4df27c4d | 9237 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9238 | log_pinned = false; |
4df27c4d | 9239 | } |
cdd1fedf DF |
9240 | |
9241 | if (flags & RENAME_WHITEOUT) { | |
9242 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9243 | old_dentry); | |
9244 | ||
9245 | if (ret) { | |
66642832 | 9246 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9247 | goto out_fail; |
9248 | } | |
4df27c4d | 9249 | } |
39279cc3 | 9250 | out_fail: |
3dc9e8f7 FM |
9251 | /* |
9252 | * If we have pinned the log and an error happened, we unpin tasks | |
9253 | * trying to sync the log and force them to fallback to a transaction | |
9254 | * commit if the log currently contains any of the inodes involved in | |
9255 | * this rename operation (to ensure we do not persist a log with an | |
9256 | * inconsistent state for any of these inodes or leading to any | |
9257 | * inconsistencies when replayed). If the transaction was aborted, the | |
9258 | * abortion reason is propagated to userspace when attempting to commit | |
9259 | * the transaction. If the log does not contain any of these inodes, we | |
9260 | * allow the tasks to sync it. | |
9261 | */ | |
9262 | if (ret && log_pinned) { | |
0f8939b8 NB |
9263 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9264 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9265 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9266 | (new_inode && |
0f8939b8 | 9267 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 9268 | btrfs_set_log_full_commit(trans); |
3dc9e8f7 FM |
9269 | |
9270 | btrfs_end_log_trans(root); | |
9271 | log_pinned = false; | |
9272 | } | |
75b463d2 FM |
9273 | ret2 = btrfs_end_transaction(trans); |
9274 | ret = ret ? ret : ret2; | |
b44c59a8 | 9275 | out_notrans: |
33345d01 | 9276 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9277 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9278 | |
39279cc3 CM |
9279 | return ret; |
9280 | } | |
9281 | ||
80ace85c MS |
9282 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9283 | struct inode *new_dir, struct dentry *new_dentry, | |
9284 | unsigned int flags) | |
9285 | { | |
cdd1fedf | 9286 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9287 | return -EINVAL; |
9288 | ||
cdd1fedf DF |
9289 | if (flags & RENAME_EXCHANGE) |
9290 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9291 | new_dentry); | |
9292 | ||
9293 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9294 | } |
9295 | ||
3a2f8c07 NB |
9296 | struct btrfs_delalloc_work { |
9297 | struct inode *inode; | |
9298 | struct completion completion; | |
9299 | struct list_head list; | |
9300 | struct btrfs_work work; | |
9301 | }; | |
9302 | ||
8ccf6f19 MX |
9303 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9304 | { | |
9305 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9306 | struct inode *inode; |
8ccf6f19 MX |
9307 | |
9308 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9309 | work); | |
9f23e289 | 9310 | inode = delalloc_work->inode; |
30424601 DS |
9311 | filemap_flush(inode->i_mapping); |
9312 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9313 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9314 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9315 | |
076da91c | 9316 | iput(inode); |
8ccf6f19 MX |
9317 | complete(&delalloc_work->completion); |
9318 | } | |
9319 | ||
3a2f8c07 | 9320 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9321 | { |
9322 | struct btrfs_delalloc_work *work; | |
9323 | ||
100d5702 | 9324 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9325 | if (!work) |
9326 | return NULL; | |
9327 | ||
9328 | init_completion(&work->completion); | |
9329 | INIT_LIST_HEAD(&work->list); | |
9330 | work->inode = inode; | |
a0cac0ec | 9331 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
9332 | |
9333 | return work; | |
9334 | } | |
9335 | ||
d352ac68 CM |
9336 | /* |
9337 | * some fairly slow code that needs optimization. This walks the list | |
9338 | * of all the inodes with pending delalloc and forces them to disk. | |
9339 | */ | |
b4912139 | 9340 | static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot) |
ea8c2819 | 9341 | { |
ea8c2819 | 9342 | struct btrfs_inode *binode; |
5b21f2ed | 9343 | struct inode *inode; |
8ccf6f19 MX |
9344 | struct btrfs_delalloc_work *work, *next; |
9345 | struct list_head works; | |
1eafa6c7 | 9346 | struct list_head splice; |
8ccf6f19 | 9347 | int ret = 0; |
ea8c2819 | 9348 | |
8ccf6f19 | 9349 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9350 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9351 | |
573bfb72 | 9352 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9353 | spin_lock(&root->delalloc_lock); |
9354 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9355 | while (!list_empty(&splice)) { |
9356 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9357 | delalloc_inodes); |
1eafa6c7 | 9358 | |
eb73c1b7 MX |
9359 | list_move_tail(&binode->delalloc_inodes, |
9360 | &root->delalloc_inodes); | |
5b21f2ed | 9361 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9362 | if (!inode) { |
eb73c1b7 | 9363 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9364 | continue; |
df0af1a5 | 9365 | } |
eb73c1b7 | 9366 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9367 | |
3cd24c69 EL |
9368 | if (snapshot) |
9369 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9370 | &binode->runtime_flags); | |
076da91c | 9371 | work = btrfs_alloc_delalloc_work(inode); |
5d99a998 | 9372 | if (!work) { |
4fbb5147 | 9373 | iput(inode); |
1eafa6c7 | 9374 | ret = -ENOMEM; |
a1ecaabb | 9375 | goto out; |
5b21f2ed | 9376 | } |
1eafa6c7 | 9377 | list_add_tail(&work->list, &works); |
a44903ab QW |
9378 | btrfs_queue_work(root->fs_info->flush_workers, |
9379 | &work->work); | |
b4912139 JB |
9380 | if (*nr != U64_MAX) { |
9381 | (*nr)--; | |
9382 | if (*nr == 0) | |
9383 | goto out; | |
9384 | } | |
5b21f2ed | 9385 | cond_resched(); |
eb73c1b7 | 9386 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9387 | } |
eb73c1b7 | 9388 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9389 | |
a1ecaabb | 9390 | out: |
eb73c1b7 MX |
9391 | list_for_each_entry_safe(work, next, &works, list) { |
9392 | list_del_init(&work->list); | |
40012f96 NB |
9393 | wait_for_completion(&work->completion); |
9394 | kfree(work); | |
eb73c1b7 MX |
9395 | } |
9396 | ||
81f1d390 | 9397 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9398 | spin_lock(&root->delalloc_lock); |
9399 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9400 | spin_unlock(&root->delalloc_lock); | |
9401 | } | |
573bfb72 | 9402 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9403 | return ret; |
9404 | } | |
1eafa6c7 | 9405 | |
3cd24c69 | 9406 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
eb73c1b7 | 9407 | { |
0b246afa | 9408 | struct btrfs_fs_info *fs_info = root->fs_info; |
b4912139 | 9409 | u64 nr = U64_MAX; |
1eafa6c7 | 9410 | |
0b246afa | 9411 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9412 | return -EROFS; |
9413 | ||
b4912139 | 9414 | return start_delalloc_inodes(root, &nr, true); |
eb73c1b7 MX |
9415 | } |
9416 | ||
b4912139 | 9417 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr) |
eb73c1b7 MX |
9418 | { |
9419 | struct btrfs_root *root; | |
9420 | struct list_head splice; | |
9421 | int ret; | |
9422 | ||
2c21b4d7 | 9423 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9424 | return -EROFS; |
9425 | ||
9426 | INIT_LIST_HEAD(&splice); | |
9427 | ||
573bfb72 | 9428 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9429 | spin_lock(&fs_info->delalloc_root_lock); |
9430 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9431 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9432 | root = list_first_entry(&splice, struct btrfs_root, |
9433 | delalloc_root); | |
00246528 | 9434 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9435 | BUG_ON(!root); |
9436 | list_move_tail(&root->delalloc_root, | |
9437 | &fs_info->delalloc_roots); | |
9438 | spin_unlock(&fs_info->delalloc_root_lock); | |
9439 | ||
b4912139 | 9440 | ret = start_delalloc_inodes(root, &nr, false); |
00246528 | 9441 | btrfs_put_root(root); |
6c255e67 | 9442 | if (ret < 0) |
eb73c1b7 | 9443 | goto out; |
eb73c1b7 | 9444 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9445 | } |
eb73c1b7 | 9446 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9447 | |
6c255e67 | 9448 | ret = 0; |
eb73c1b7 | 9449 | out: |
81f1d390 | 9450 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9451 | spin_lock(&fs_info->delalloc_root_lock); |
9452 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9453 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9454 | } |
573bfb72 | 9455 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9456 | return ret; |
ea8c2819 CM |
9457 | } |
9458 | ||
39279cc3 CM |
9459 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9460 | const char *symname) | |
9461 | { | |
0b246afa | 9462 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9463 | struct btrfs_trans_handle *trans; |
9464 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9465 | struct btrfs_path *path; | |
9466 | struct btrfs_key key; | |
1832a6d5 | 9467 | struct inode *inode = NULL; |
39279cc3 | 9468 | int err; |
39279cc3 | 9469 | u64 objectid; |
67871254 | 9470 | u64 index = 0; |
39279cc3 CM |
9471 | int name_len; |
9472 | int datasize; | |
5f39d397 | 9473 | unsigned long ptr; |
39279cc3 | 9474 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9475 | struct extent_buffer *leaf; |
39279cc3 | 9476 | |
f06becc4 | 9477 | name_len = strlen(symname); |
0b246afa | 9478 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9479 | return -ENAMETOOLONG; |
1832a6d5 | 9480 | |
9ed74f2d JB |
9481 | /* |
9482 | * 2 items for inode item and ref | |
9483 | * 2 items for dir items | |
9269d12b FM |
9484 | * 1 item for updating parent inode item |
9485 | * 1 item for the inline extent item | |
9ed74f2d JB |
9486 | * 1 item for xattr if selinux is on |
9487 | */ | |
9269d12b | 9488 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
9489 | if (IS_ERR(trans)) |
9490 | return PTR_ERR(trans); | |
1832a6d5 | 9491 | |
581bb050 LZ |
9492 | err = btrfs_find_free_ino(root, &objectid); |
9493 | if (err) | |
9494 | goto out_unlock; | |
9495 | ||
aec7477b | 9496 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
9497 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
9498 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
9499 | if (IS_ERR(inode)) { |
9500 | err = PTR_ERR(inode); | |
32955c54 | 9501 | inode = NULL; |
39279cc3 | 9502 | goto out_unlock; |
7cf96da3 | 9503 | } |
39279cc3 | 9504 | |
ad19db71 CS |
9505 | /* |
9506 | * If the active LSM wants to access the inode during | |
9507 | * d_instantiate it needs these. Smack checks to see | |
9508 | * if the filesystem supports xattrs by looking at the | |
9509 | * ops vector. | |
9510 | */ | |
9511 | inode->i_fop = &btrfs_file_operations; | |
9512 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9513 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9514 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9515 | ||
9516 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9517 | if (err) | |
32955c54 | 9518 | goto out_unlock; |
ad19db71 | 9519 | |
39279cc3 | 9520 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9521 | if (!path) { |
9522 | err = -ENOMEM; | |
32955c54 | 9523 | goto out_unlock; |
d8926bb3 | 9524 | } |
4a0cc7ca | 9525 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9526 | key.offset = 0; |
962a298f | 9527 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9528 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9529 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9530 | datasize); | |
54aa1f4d | 9531 | if (err) { |
b0839166 | 9532 | btrfs_free_path(path); |
32955c54 | 9533 | goto out_unlock; |
54aa1f4d | 9534 | } |
5f39d397 CM |
9535 | leaf = path->nodes[0]; |
9536 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9537 | struct btrfs_file_extent_item); | |
9538 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9539 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9540 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9541 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9542 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9543 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9544 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9545 | ||
39279cc3 | 9546 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9547 | write_extent_buffer(leaf, symname, ptr, name_len); |
9548 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9549 | btrfs_free_path(path); |
5f39d397 | 9550 | |
39279cc3 | 9551 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 9552 | inode_nohighmem(inode); |
d899e052 | 9553 | inode_set_bytes(inode, name_len); |
6ef06d27 | 9554 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 9555 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
9556 | /* |
9557 | * Last step, add directory indexes for our symlink inode. This is the | |
9558 | * last step to avoid extra cleanup of these indexes if an error happens | |
9559 | * elsewhere above. | |
9560 | */ | |
9561 | if (!err) | |
cef415af NB |
9562 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9563 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
9564 | if (err) |
9565 | goto out_unlock; | |
b0d5d10f | 9566 | |
1e2e547a | 9567 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
9568 | |
9569 | out_unlock: | |
3a45bb20 | 9570 | btrfs_end_transaction(trans); |
32955c54 | 9571 | if (err && inode) { |
39279cc3 | 9572 | inode_dec_link_count(inode); |
32955c54 | 9573 | discard_new_inode(inode); |
39279cc3 | 9574 | } |
2ff7e61e | 9575 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
9576 | return err; |
9577 | } | |
16432985 | 9578 | |
203f44c5 QW |
9579 | static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans, |
9580 | struct inode *inode, struct btrfs_key *ins, | |
9581 | u64 file_offset) | |
9582 | { | |
9583 | struct btrfs_file_extent_item stack_fi; | |
9584 | u64 start = ins->objectid; | |
9585 | u64 len = ins->offset; | |
9729f10a | 9586 | int ret; |
203f44c5 QW |
9587 | |
9588 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9589 | ||
9590 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9591 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9592 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9593 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9594 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9595 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9596 | /* Encryption and other encoding is reserved and all 0 */ | |
9597 | ||
72b7d15b | 9598 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
9729f10a QW |
9599 | if (ret < 0) |
9600 | return ret; | |
c553f94d | 9601 | return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset, |
9729f10a | 9602 | &stack_fi, ret); |
203f44c5 | 9603 | } |
0af3d00b JB |
9604 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9605 | u64 start, u64 num_bytes, u64 min_size, | |
9606 | loff_t actual_len, u64 *alloc_hint, | |
9607 | struct btrfs_trans_handle *trans) | |
d899e052 | 9608 | { |
0b246afa | 9609 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
9610 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9611 | struct extent_map *em; | |
d899e052 YZ |
9612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9613 | struct btrfs_key ins; | |
d899e052 | 9614 | u64 cur_offset = start; |
b778cf96 | 9615 | u64 clear_offset = start; |
55a61d1d | 9616 | u64 i_size; |
154ea289 | 9617 | u64 cur_bytes; |
0b670dc4 | 9618 | u64 last_alloc = (u64)-1; |
d899e052 | 9619 | int ret = 0; |
0af3d00b | 9620 | bool own_trans = true; |
18513091 | 9621 | u64 end = start + num_bytes - 1; |
d899e052 | 9622 | |
0af3d00b JB |
9623 | if (trans) |
9624 | own_trans = false; | |
d899e052 | 9625 | while (num_bytes > 0) { |
0af3d00b JB |
9626 | if (own_trans) { |
9627 | trans = btrfs_start_transaction(root, 3); | |
9628 | if (IS_ERR(trans)) { | |
9629 | ret = PTR_ERR(trans); | |
9630 | break; | |
9631 | } | |
5a303d5d YZ |
9632 | } |
9633 | ||
ee22184b | 9634 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9635 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9636 | /* |
9637 | * If we are severely fragmented we could end up with really | |
9638 | * small allocations, so if the allocator is returning small | |
9639 | * chunks lets make its job easier by only searching for those | |
9640 | * sized chunks. | |
9641 | */ | |
9642 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9643 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9644 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 9645 | if (ret) { |
0af3d00b | 9646 | if (own_trans) |
3a45bb20 | 9647 | btrfs_end_transaction(trans); |
a22285a6 | 9648 | break; |
d899e052 | 9649 | } |
b778cf96 JB |
9650 | |
9651 | /* | |
9652 | * We've reserved this space, and thus converted it from | |
9653 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9654 | * from here on out we will only need to clear our reservation | |
9655 | * for the remaining unreserved area, so advance our | |
9656 | * clear_offset by our extent size. | |
9657 | */ | |
9658 | clear_offset += ins.offset; | |
0b246afa | 9659 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 9660 | |
0b670dc4 | 9661 | last_alloc = ins.offset; |
c553f94d | 9662 | ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset); |
79787eaa | 9663 | if (ret) { |
2ff7e61e | 9664 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9665 | ins.offset, 0); |
66642832 | 9666 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9667 | if (own_trans) |
3a45bb20 | 9668 | btrfs_end_transaction(trans); |
79787eaa JM |
9669 | break; |
9670 | } | |
31193213 | 9671 | |
dcdbc059 | 9672 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 9673 | cur_offset + ins.offset -1, 0); |
5a303d5d | 9674 | |
5dc562c5 JB |
9675 | em = alloc_extent_map(); |
9676 | if (!em) { | |
9677 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9678 | &BTRFS_I(inode)->runtime_flags); | |
9679 | goto next; | |
9680 | } | |
9681 | ||
9682 | em->start = cur_offset; | |
9683 | em->orig_start = cur_offset; | |
9684 | em->len = ins.offset; | |
9685 | em->block_start = ins.objectid; | |
9686 | em->block_len = ins.offset; | |
b4939680 | 9687 | em->orig_block_len = ins.offset; |
cc95bef6 | 9688 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9689 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
9690 | em->generation = trans->transid; | |
9691 | ||
9692 | while (1) { | |
9693 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9694 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9695 | write_unlock(&em_tree->lock); |
9696 | if (ret != -EEXIST) | |
9697 | break; | |
dcdbc059 | 9698 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
9699 | cur_offset + ins.offset - 1, |
9700 | 0); | |
9701 | } | |
9702 | free_extent_map(em); | |
9703 | next: | |
d899e052 YZ |
9704 | num_bytes -= ins.offset; |
9705 | cur_offset += ins.offset; | |
efa56464 | 9706 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9707 | |
0c4d2d95 | 9708 | inode_inc_iversion(inode); |
c2050a45 | 9709 | inode->i_ctime = current_time(inode); |
6cbff00f | 9710 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9711 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9712 | (actual_len > inode->i_size) && |
9713 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9714 | if (cur_offset > actual_len) |
55a61d1d | 9715 | i_size = actual_len; |
d1ea6a61 | 9716 | else |
55a61d1d JB |
9717 | i_size = cur_offset; |
9718 | i_size_write(inode, i_size); | |
d923afe9 | 9719 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
5a303d5d YZ |
9720 | } |
9721 | ||
d899e052 | 9722 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9723 | |
9724 | if (ret) { | |
66642832 | 9725 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9726 | if (own_trans) |
3a45bb20 | 9727 | btrfs_end_transaction(trans); |
79787eaa JM |
9728 | break; |
9729 | } | |
d899e052 | 9730 | |
0af3d00b | 9731 | if (own_trans) |
3a45bb20 | 9732 | btrfs_end_transaction(trans); |
5a303d5d | 9733 | } |
b778cf96 | 9734 | if (clear_offset < end) |
25ce28ca | 9735 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
b778cf96 | 9736 | end - clear_offset + 1); |
d899e052 YZ |
9737 | return ret; |
9738 | } | |
9739 | ||
0af3d00b JB |
9740 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9741 | u64 start, u64 num_bytes, u64 min_size, | |
9742 | loff_t actual_len, u64 *alloc_hint) | |
9743 | { | |
9744 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9745 | min_size, actual_len, alloc_hint, | |
9746 | NULL); | |
9747 | } | |
9748 | ||
9749 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9750 | struct btrfs_trans_handle *trans, int mode, | |
9751 | u64 start, u64 num_bytes, u64 min_size, | |
9752 | loff_t actual_len, u64 *alloc_hint) | |
9753 | { | |
9754 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9755 | min_size, actual_len, alloc_hint, trans); | |
9756 | } | |
9757 | ||
e6dcd2dc CM |
9758 | static int btrfs_set_page_dirty(struct page *page) |
9759 | { | |
e6dcd2dc CM |
9760 | return __set_page_dirty_nobuffers(page); |
9761 | } | |
9762 | ||
10556cb2 | 9763 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9764 | { |
b83cc969 | 9765 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9766 | umode_t mode = inode->i_mode; |
b83cc969 | 9767 | |
cb6db4e5 JM |
9768 | if (mask & MAY_WRITE && |
9769 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9770 | if (btrfs_root_readonly(root)) | |
9771 | return -EROFS; | |
9772 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9773 | return -EACCES; | |
9774 | } | |
2830ba7f | 9775 | return generic_permission(inode, mask); |
fdebe2bd | 9776 | } |
39279cc3 | 9777 | |
ef3b9af5 FM |
9778 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9779 | { | |
2ff7e61e | 9780 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
9781 | struct btrfs_trans_handle *trans; |
9782 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9783 | struct inode *inode = NULL; | |
9784 | u64 objectid; | |
9785 | u64 index; | |
9786 | int ret = 0; | |
9787 | ||
9788 | /* | |
9789 | * 5 units required for adding orphan entry | |
9790 | */ | |
9791 | trans = btrfs_start_transaction(root, 5); | |
9792 | if (IS_ERR(trans)) | |
9793 | return PTR_ERR(trans); | |
9794 | ||
9795 | ret = btrfs_find_free_ino(root, &objectid); | |
9796 | if (ret) | |
9797 | goto out; | |
9798 | ||
9799 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 9800 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
9801 | if (IS_ERR(inode)) { |
9802 | ret = PTR_ERR(inode); | |
9803 | inode = NULL; | |
9804 | goto out; | |
9805 | } | |
9806 | ||
ef3b9af5 FM |
9807 | inode->i_fop = &btrfs_file_operations; |
9808 | inode->i_op = &btrfs_file_inode_operations; | |
9809 | ||
9810 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9811 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9812 | ||
b0d5d10f CM |
9813 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9814 | if (ret) | |
32955c54 | 9815 | goto out; |
b0d5d10f CM |
9816 | |
9817 | ret = btrfs_update_inode(trans, root, inode); | |
9818 | if (ret) | |
32955c54 | 9819 | goto out; |
73f2e545 | 9820 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 9821 | if (ret) |
32955c54 | 9822 | goto out; |
ef3b9af5 | 9823 | |
5762b5c9 FM |
9824 | /* |
9825 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9826 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9827 | * through: | |
9828 | * | |
9829 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9830 | */ | |
9831 | set_nlink(inode, 1); | |
ef3b9af5 | 9832 | d_tmpfile(dentry, inode); |
32955c54 | 9833 | unlock_new_inode(inode); |
ef3b9af5 | 9834 | mark_inode_dirty(inode); |
ef3b9af5 | 9835 | out: |
3a45bb20 | 9836 | btrfs_end_transaction(trans); |
32955c54 AV |
9837 | if (ret && inode) |
9838 | discard_new_inode(inode); | |
2ff7e61e | 9839 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
9840 | return ret; |
9841 | } | |
9842 | ||
5cdc84bf | 9843 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
c6100a4b | 9844 | { |
5cdc84bf | 9845 | struct inode *inode = tree->private_data; |
c6100a4b JB |
9846 | unsigned long index = start >> PAGE_SHIFT; |
9847 | unsigned long end_index = end >> PAGE_SHIFT; | |
9848 | struct page *page; | |
9849 | ||
9850 | while (index <= end_index) { | |
9851 | page = find_get_page(inode->i_mapping, index); | |
9852 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
9853 | set_page_writeback(page); | |
9854 | put_page(page); | |
9855 | index++; | |
9856 | } | |
9857 | } | |
9858 | ||
ed46ff3d OS |
9859 | #ifdef CONFIG_SWAP |
9860 | /* | |
9861 | * Add an entry indicating a block group or device which is pinned by a | |
9862 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
9863 | * negative errno on failure. | |
9864 | */ | |
9865 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
9866 | bool is_block_group) | |
9867 | { | |
9868 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9869 | struct btrfs_swapfile_pin *sp, *entry; | |
9870 | struct rb_node **p; | |
9871 | struct rb_node *parent = NULL; | |
9872 | ||
9873 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
9874 | if (!sp) | |
9875 | return -ENOMEM; | |
9876 | sp->ptr = ptr; | |
9877 | sp->inode = inode; | |
9878 | sp->is_block_group = is_block_group; | |
9879 | ||
9880 | spin_lock(&fs_info->swapfile_pins_lock); | |
9881 | p = &fs_info->swapfile_pins.rb_node; | |
9882 | while (*p) { | |
9883 | parent = *p; | |
9884 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
9885 | if (sp->ptr < entry->ptr || | |
9886 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
9887 | p = &(*p)->rb_left; | |
9888 | } else if (sp->ptr > entry->ptr || | |
9889 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
9890 | p = &(*p)->rb_right; | |
9891 | } else { | |
9892 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9893 | kfree(sp); | |
9894 | return 1; | |
9895 | } | |
9896 | } | |
9897 | rb_link_node(&sp->node, parent, p); | |
9898 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
9899 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9900 | return 0; | |
9901 | } | |
9902 | ||
9903 | /* Free all of the entries pinned by this swapfile. */ | |
9904 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
9905 | { | |
9906 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9907 | struct btrfs_swapfile_pin *sp; | |
9908 | struct rb_node *node, *next; | |
9909 | ||
9910 | spin_lock(&fs_info->swapfile_pins_lock); | |
9911 | node = rb_first(&fs_info->swapfile_pins); | |
9912 | while (node) { | |
9913 | next = rb_next(node); | |
9914 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
9915 | if (sp->inode == inode) { | |
9916 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
9917 | if (sp->is_block_group) | |
9918 | btrfs_put_block_group(sp->ptr); | |
9919 | kfree(sp); | |
9920 | } | |
9921 | node = next; | |
9922 | } | |
9923 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9924 | } | |
9925 | ||
9926 | struct btrfs_swap_info { | |
9927 | u64 start; | |
9928 | u64 block_start; | |
9929 | u64 block_len; | |
9930 | u64 lowest_ppage; | |
9931 | u64 highest_ppage; | |
9932 | unsigned long nr_pages; | |
9933 | int nr_extents; | |
9934 | }; | |
9935 | ||
9936 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
9937 | struct btrfs_swap_info *bsi) | |
9938 | { | |
9939 | unsigned long nr_pages; | |
9940 | u64 first_ppage, first_ppage_reported, next_ppage; | |
9941 | int ret; | |
9942 | ||
9943 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
9944 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
9945 | PAGE_SIZE) >> PAGE_SHIFT; | |
9946 | ||
9947 | if (first_ppage >= next_ppage) | |
9948 | return 0; | |
9949 | nr_pages = next_ppage - first_ppage; | |
9950 | ||
9951 | first_ppage_reported = first_ppage; | |
9952 | if (bsi->start == 0) | |
9953 | first_ppage_reported++; | |
9954 | if (bsi->lowest_ppage > first_ppage_reported) | |
9955 | bsi->lowest_ppage = first_ppage_reported; | |
9956 | if (bsi->highest_ppage < (next_ppage - 1)) | |
9957 | bsi->highest_ppage = next_ppage - 1; | |
9958 | ||
9959 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
9960 | if (ret < 0) | |
9961 | return ret; | |
9962 | bsi->nr_extents += ret; | |
9963 | bsi->nr_pages += nr_pages; | |
9964 | return 0; | |
9965 | } | |
9966 | ||
9967 | static void btrfs_swap_deactivate(struct file *file) | |
9968 | { | |
9969 | struct inode *inode = file_inode(file); | |
9970 | ||
9971 | btrfs_free_swapfile_pins(inode); | |
9972 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
9973 | } | |
9974 | ||
9975 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
9976 | sector_t *span) | |
9977 | { | |
9978 | struct inode *inode = file_inode(file); | |
9979 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9980 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
9981 | struct extent_state *cached_state = NULL; | |
9982 | struct extent_map *em = NULL; | |
9983 | struct btrfs_device *device = NULL; | |
9984 | struct btrfs_swap_info bsi = { | |
9985 | .lowest_ppage = (sector_t)-1ULL, | |
9986 | }; | |
9987 | int ret = 0; | |
9988 | u64 isize; | |
9989 | u64 start; | |
9990 | ||
9991 | /* | |
9992 | * If the swap file was just created, make sure delalloc is done. If the | |
9993 | * file changes again after this, the user is doing something stupid and | |
9994 | * we don't really care. | |
9995 | */ | |
9996 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
9997 | if (ret) | |
9998 | return ret; | |
9999 | ||
10000 | /* | |
10001 | * The inode is locked, so these flags won't change after we check them. | |
10002 | */ | |
10003 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10004 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10005 | return -EINVAL; | |
10006 | } | |
10007 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10008 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10009 | return -EINVAL; | |
10010 | } | |
10011 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10012 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10013 | return -EINVAL; | |
10014 | } | |
10015 | ||
10016 | /* | |
10017 | * Balance or device remove/replace/resize can move stuff around from | |
10018 | * under us. The EXCL_OP flag makes sure they aren't running/won't run | |
10019 | * concurrently while we are mapping the swap extents, and | |
10020 | * fs_info->swapfile_pins prevents them from running while the swap file | |
10021 | * is active and moving the extents. Note that this also prevents a | |
10022 | * concurrent device add which isn't actually necessary, but it's not | |
10023 | * really worth the trouble to allow it. | |
10024 | */ | |
10025 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { | |
10026 | btrfs_warn(fs_info, | |
10027 | "cannot activate swapfile while exclusive operation is running"); | |
10028 | return -EBUSY; | |
10029 | } | |
10030 | /* | |
10031 | * Snapshots can create extents which require COW even if NODATACOW is | |
10032 | * set. We use this counter to prevent snapshots. We must increment it | |
10033 | * before walking the extents because we don't want a concurrent | |
10034 | * snapshot to run after we've already checked the extents. | |
10035 | */ | |
10036 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); | |
10037 | ||
10038 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10039 | ||
10040 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10041 | start = 0; | |
10042 | while (start < isize) { | |
10043 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10044 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10045 | u64 len = isize - start; |
10046 | ||
39b07b5d | 10047 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
10048 | if (IS_ERR(em)) { |
10049 | ret = PTR_ERR(em); | |
10050 | goto out; | |
10051 | } | |
10052 | ||
10053 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10054 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10055 | ret = -EINVAL; | |
10056 | goto out; | |
10057 | } | |
10058 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10059 | /* | |
10060 | * It's unlikely we'll ever actually find ourselves | |
10061 | * here, as a file small enough to fit inline won't be | |
10062 | * big enough to store more than the swap header, but in | |
10063 | * case something changes in the future, let's catch it | |
10064 | * here rather than later. | |
10065 | */ | |
10066 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10067 | ret = -EINVAL; | |
10068 | goto out; | |
10069 | } | |
10070 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10071 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10072 | ret = -EINVAL; | |
10073 | goto out; | |
10074 | } | |
10075 | ||
10076 | logical_block_start = em->block_start + (start - em->start); | |
10077 | len = min(len, em->len - (start - em->start)); | |
10078 | free_extent_map(em); | |
10079 | em = NULL; | |
10080 | ||
a84d5d42 | 10081 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
ed46ff3d OS |
10082 | if (ret < 0) { |
10083 | goto out; | |
10084 | } else if (ret) { | |
10085 | ret = 0; | |
10086 | } else { | |
10087 | btrfs_warn(fs_info, | |
10088 | "swapfile must not be copy-on-write"); | |
10089 | ret = -EINVAL; | |
10090 | goto out; | |
10091 | } | |
10092 | ||
10093 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10094 | if (IS_ERR(em)) { | |
10095 | ret = PTR_ERR(em); | |
10096 | goto out; | |
10097 | } | |
10098 | ||
10099 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10100 | btrfs_warn(fs_info, | |
10101 | "swapfile must have single data profile"); | |
10102 | ret = -EINVAL; | |
10103 | goto out; | |
10104 | } | |
10105 | ||
10106 | if (device == NULL) { | |
10107 | device = em->map_lookup->stripes[0].dev; | |
10108 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10109 | if (ret == 1) | |
10110 | ret = 0; | |
10111 | else if (ret) | |
10112 | goto out; | |
10113 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10114 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10115 | ret = -EINVAL; | |
10116 | goto out; | |
10117 | } | |
10118 | ||
10119 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10120 | (logical_block_start - em->start)); | |
10121 | len = min(len, em->len - (logical_block_start - em->start)); | |
10122 | free_extent_map(em); | |
10123 | em = NULL; | |
10124 | ||
10125 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10126 | if (!bg) { | |
10127 | btrfs_warn(fs_info, | |
10128 | "could not find block group containing swapfile"); | |
10129 | ret = -EINVAL; | |
10130 | goto out; | |
10131 | } | |
10132 | ||
10133 | ret = btrfs_add_swapfile_pin(inode, bg, true); | |
10134 | if (ret) { | |
10135 | btrfs_put_block_group(bg); | |
10136 | if (ret == 1) | |
10137 | ret = 0; | |
10138 | else | |
10139 | goto out; | |
10140 | } | |
10141 | ||
10142 | if (bsi.block_len && | |
10143 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10144 | bsi.block_len += len; | |
10145 | } else { | |
10146 | if (bsi.block_len) { | |
10147 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10148 | if (ret) | |
10149 | goto out; | |
10150 | } | |
10151 | bsi.start = start; | |
10152 | bsi.block_start = physical_block_start; | |
10153 | bsi.block_len = len; | |
10154 | } | |
10155 | ||
10156 | start += len; | |
10157 | } | |
10158 | ||
10159 | if (bsi.block_len) | |
10160 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10161 | ||
10162 | out: | |
10163 | if (!IS_ERR_OR_NULL(em)) | |
10164 | free_extent_map(em); | |
10165 | ||
10166 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10167 | ||
10168 | if (ret) | |
10169 | btrfs_swap_deactivate(file); | |
10170 | ||
10171 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); | |
10172 | ||
10173 | if (ret) | |
10174 | return ret; | |
10175 | ||
10176 | if (device) | |
10177 | sis->bdev = device->bdev; | |
10178 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10179 | sis->max = bsi.nr_pages; | |
10180 | sis->pages = bsi.nr_pages - 1; | |
10181 | sis->highest_bit = bsi.nr_pages - 1; | |
10182 | return bsi.nr_extents; | |
10183 | } | |
10184 | #else | |
10185 | static void btrfs_swap_deactivate(struct file *file) | |
10186 | { | |
10187 | } | |
10188 | ||
10189 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10190 | sector_t *span) | |
10191 | { | |
10192 | return -EOPNOTSUPP; | |
10193 | } | |
10194 | #endif | |
10195 | ||
6e1d5dcc | 10196 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10197 | .getattr = btrfs_getattr, |
39279cc3 CM |
10198 | .lookup = btrfs_lookup, |
10199 | .create = btrfs_create, | |
10200 | .unlink = btrfs_unlink, | |
10201 | .link = btrfs_link, | |
10202 | .mkdir = btrfs_mkdir, | |
10203 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10204 | .rename = btrfs_rename2, |
39279cc3 CM |
10205 | .symlink = btrfs_symlink, |
10206 | .setattr = btrfs_setattr, | |
618e21d5 | 10207 | .mknod = btrfs_mknod, |
5103e947 | 10208 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10209 | .permission = btrfs_permission, |
4e34e719 | 10210 | .get_acl = btrfs_get_acl, |
996a710d | 10211 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10212 | .update_time = btrfs_update_time, |
ef3b9af5 | 10213 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10214 | }; |
76dda93c | 10215 | |
828c0950 | 10216 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10217 | .llseek = generic_file_llseek, |
10218 | .read = generic_read_dir, | |
02dbfc99 | 10219 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10220 | .open = btrfs_opendir, |
34287aa3 | 10221 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10222 | #ifdef CONFIG_COMPAT |
4c63c245 | 10223 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10224 | #endif |
6bf13c0c | 10225 | .release = btrfs_release_file, |
e02119d5 | 10226 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10227 | }; |
10228 | ||
20e5506b | 10229 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10230 | /* mandatory callbacks */ |
065631f6 | 10231 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac CM |
10232 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
10233 | }; | |
10234 | ||
35054394 CM |
10235 | /* |
10236 | * btrfs doesn't support the bmap operation because swapfiles | |
10237 | * use bmap to make a mapping of extents in the file. They assume | |
10238 | * these extents won't change over the life of the file and they | |
10239 | * use the bmap result to do IO directly to the drive. | |
10240 | * | |
10241 | * the btrfs bmap call would return logical addresses that aren't | |
10242 | * suitable for IO and they also will change frequently as COW | |
10243 | * operations happen. So, swapfile + btrfs == corruption. | |
10244 | * | |
10245 | * For now we're avoiding this by dropping bmap. | |
10246 | */ | |
7f09410b | 10247 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10248 | .readpage = btrfs_readpage, |
10249 | .writepage = btrfs_writepage, | |
b293f02e | 10250 | .writepages = btrfs_writepages, |
ba206a02 | 10251 | .readahead = btrfs_readahead, |
f85781fb | 10252 | .direct_IO = noop_direct_IO, |
a52d9a80 CM |
10253 | .invalidatepage = btrfs_invalidatepage, |
10254 | .releasepage = btrfs_releasepage, | |
f8e66081 RG |
10255 | #ifdef CONFIG_MIGRATION |
10256 | .migratepage = btrfs_migratepage, | |
10257 | #endif | |
e6dcd2dc | 10258 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10259 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10260 | .swap_activate = btrfs_swap_activate, |
10261 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10262 | }; |
10263 | ||
6e1d5dcc | 10264 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10265 | .getattr = btrfs_getattr, |
10266 | .setattr = btrfs_setattr, | |
5103e947 | 10267 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10268 | .permission = btrfs_permission, |
1506fcc8 | 10269 | .fiemap = btrfs_fiemap, |
4e34e719 | 10270 | .get_acl = btrfs_get_acl, |
996a710d | 10271 | .set_acl = btrfs_set_acl, |
e41f941a | 10272 | .update_time = btrfs_update_time, |
39279cc3 | 10273 | }; |
6e1d5dcc | 10274 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10275 | .getattr = btrfs_getattr, |
10276 | .setattr = btrfs_setattr, | |
fdebe2bd | 10277 | .permission = btrfs_permission, |
33268eaf | 10278 | .listxattr = btrfs_listxattr, |
4e34e719 | 10279 | .get_acl = btrfs_get_acl, |
996a710d | 10280 | .set_acl = btrfs_set_acl, |
e41f941a | 10281 | .update_time = btrfs_update_time, |
618e21d5 | 10282 | }; |
6e1d5dcc | 10283 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10284 | .get_link = page_get_link, |
f209561a | 10285 | .getattr = btrfs_getattr, |
22c44fe6 | 10286 | .setattr = btrfs_setattr, |
fdebe2bd | 10287 | .permission = btrfs_permission, |
0279b4cd | 10288 | .listxattr = btrfs_listxattr, |
e41f941a | 10289 | .update_time = btrfs_update_time, |
39279cc3 | 10290 | }; |
76dda93c | 10291 | |
82d339d9 | 10292 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
10293 | .d_delete = btrfs_dentry_delete, |
10294 | }; |