]>
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 | ||
8f18cf13 | 6 | #include <linux/kernel.h> |
065631f6 | 7 | #include <linux/bio.h> |
55e20bd1 | 8 | #include <linux/buffer_head.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> |
92d32170 | 33 | #include <asm/unaligned.h> |
602cbe91 | 34 | #include "misc.h" |
39279cc3 CM |
35 | #include "ctree.h" |
36 | #include "disk-io.h" | |
37 | #include "transaction.h" | |
38 | #include "btrfs_inode.h" | |
39279cc3 | 39 | #include "print-tree.h" |
e6dcd2dc | 40 | #include "ordered-data.h" |
95819c05 | 41 | #include "xattr.h" |
e02119d5 | 42 | #include "tree-log.h" |
4a54c8c1 | 43 | #include "volumes.h" |
c8b97818 | 44 | #include "compression.h" |
b4ce94de | 45 | #include "locking.h" |
dc89e982 | 46 | #include "free-space-cache.h" |
581bb050 | 47 | #include "inode-map.h" |
63541927 | 48 | #include "props.h" |
31193213 | 49 | #include "qgroup.h" |
86736342 | 50 | #include "delalloc-space.h" |
aac0023c | 51 | #include "block-group.h" |
467dc47e | 52 | #include "space-info.h" |
39279cc3 CM |
53 | |
54 | struct btrfs_iget_args { | |
0202e83f | 55 | u64 ino; |
39279cc3 CM |
56 | struct btrfs_root *root; |
57 | }; | |
58 | ||
f28a4928 | 59 | struct btrfs_dio_data { |
f28a4928 | 60 | u64 reserve; |
55e20bd1 DS |
61 | u64 unsubmitted_oe_range_start; |
62 | u64 unsubmitted_oe_range_end; | |
63 | int overwrite; | |
f28a4928 FM |
64 | }; |
65 | ||
6e1d5dcc AD |
66 | static const struct inode_operations btrfs_dir_inode_operations; |
67 | static const struct inode_operations btrfs_symlink_inode_operations; | |
6e1d5dcc AD |
68 | static const struct inode_operations btrfs_special_inode_operations; |
69 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 70 | static const struct address_space_operations btrfs_aops; |
828c0950 | 71 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 72 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
73 | |
74 | static struct kmem_cache *btrfs_inode_cachep; | |
75 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 76 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 77 | struct kmem_cache *btrfs_free_space_cachep; |
3acd4850 | 78 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
39279cc3 | 79 | |
3972f260 | 80 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 81 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
5fd02043 | 82 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
6e26c442 | 83 | static noinline int cow_file_range(struct btrfs_inode *inode, |
771ed689 | 84 | struct page *locked_page, |
74e9194a | 85 | u64 start, u64 end, int *page_started, |
330a5827 | 86 | unsigned long *nr_written, int unlock); |
4b67c11d NB |
87 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
88 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
89 | u64 block_len, u64 orig_block_len, |
90 | u64 ram_bytes, int compress_type, | |
91 | int type); | |
7b128766 | 92 | |
52427260 QW |
93 | static void __endio_write_update_ordered(struct inode *inode, |
94 | const u64 offset, const u64 bytes, | |
95 | const bool uptodate); | |
96 | ||
97 | /* | |
98 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 99 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
100 | * |
101 | * NOTE: caller must ensure that when an error happens, it can not call | |
102 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
103 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
104 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 105 | * extent (btrfs_finish_ordered_io()). |
52427260 QW |
106 | */ |
107 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, | |
d1051d6e NB |
108 | struct page *locked_page, |
109 | u64 offset, u64 bytes) | |
52427260 | 110 | { |
63d71450 NA |
111 | unsigned long index = offset >> PAGE_SHIFT; |
112 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
d1051d6e NB |
113 | u64 page_start = page_offset(locked_page); |
114 | u64 page_end = page_start + PAGE_SIZE - 1; | |
115 | ||
63d71450 NA |
116 | struct page *page; |
117 | ||
118 | while (index <= end_index) { | |
119 | page = find_get_page(inode->i_mapping, index); | |
120 | index++; | |
121 | if (!page) | |
122 | continue; | |
123 | ClearPagePrivate2(page); | |
124 | put_page(page); | |
125 | } | |
d1051d6e NB |
126 | |
127 | /* | |
128 | * In case this page belongs to the delalloc range being instantiated | |
129 | * then skip it, since the first page of a range is going to be | |
130 | * properly cleaned up by the caller of run_delalloc_range | |
131 | */ | |
132 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
133 | offset += PAGE_SIZE; | |
134 | bytes -= PAGE_SIZE; | |
135 | } | |
136 | ||
137 | return __endio_write_update_ordered(inode, offset, bytes, false); | |
52427260 QW |
138 | } |
139 | ||
48a3b636 | 140 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 141 | |
6a3891c5 JB |
142 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
143 | void btrfs_test_inode_set_ops(struct inode *inode) | |
144 | { | |
145 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
146 | } | |
147 | #endif | |
148 | ||
f34f57a3 | 149 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
150 | struct inode *inode, struct inode *dir, |
151 | const struct qstr *qstr) | |
0279b4cd JO |
152 | { |
153 | int err; | |
154 | ||
f34f57a3 | 155 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 156 | if (!err) |
2a7dba39 | 157 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
158 | return err; |
159 | } | |
160 | ||
c8b97818 CM |
161 | /* |
162 | * this does all the hard work for inserting an inline extent into | |
163 | * the btree. The caller should have done a btrfs_drop_extents so that | |
164 | * no overlapping inline items exist in the btree | |
165 | */ | |
40f76580 | 166 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 167 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
168 | struct btrfs_root *root, struct inode *inode, |
169 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 170 | int compress_type, |
c8b97818 CM |
171 | struct page **compressed_pages) |
172 | { | |
c8b97818 CM |
173 | struct extent_buffer *leaf; |
174 | struct page *page = NULL; | |
175 | char *kaddr; | |
176 | unsigned long ptr; | |
177 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
178 | int ret; |
179 | size_t cur_size = size; | |
c8b97818 | 180 | unsigned long offset; |
c8b97818 | 181 | |
982f1f5d JJB |
182 | ASSERT((compressed_size > 0 && compressed_pages) || |
183 | (compressed_size == 0 && !compressed_pages)); | |
184 | ||
fe3f566c | 185 | if (compressed_size && compressed_pages) |
c8b97818 | 186 | cur_size = compressed_size; |
c8b97818 | 187 | |
1acae57b | 188 | inode_add_bytes(inode, size); |
c8b97818 | 189 | |
1acae57b FDBM |
190 | if (!extent_inserted) { |
191 | struct btrfs_key key; | |
192 | size_t datasize; | |
c8b97818 | 193 | |
4a0cc7ca | 194 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 195 | key.offset = start; |
962a298f | 196 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 197 | |
1acae57b FDBM |
198 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
199 | path->leave_spinning = 1; | |
200 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
201 | datasize); | |
79b4f4c6 | 202 | if (ret) |
1acae57b | 203 | goto fail; |
c8b97818 CM |
204 | } |
205 | leaf = path->nodes[0]; | |
206 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
207 | struct btrfs_file_extent_item); | |
208 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
209 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
210 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
211 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
212 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
213 | ptr = btrfs_file_extent_inline_start(ei); | |
214 | ||
261507a0 | 215 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
216 | struct page *cpage; |
217 | int i = 0; | |
d397712b | 218 | while (compressed_size > 0) { |
c8b97818 | 219 | cpage = compressed_pages[i]; |
5b050f04 | 220 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 221 | PAGE_SIZE); |
c8b97818 | 222 | |
7ac687d9 | 223 | kaddr = kmap_atomic(cpage); |
c8b97818 | 224 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 225 | kunmap_atomic(kaddr); |
c8b97818 CM |
226 | |
227 | i++; | |
228 | ptr += cur_size; | |
229 | compressed_size -= cur_size; | |
230 | } | |
231 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 232 | compress_type); |
c8b97818 CM |
233 | } else { |
234 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 235 | start >> PAGE_SHIFT); |
c8b97818 | 236 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 237 | kaddr = kmap_atomic(page); |
7073017a | 238 | offset = offset_in_page(start); |
c8b97818 | 239 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 240 | kunmap_atomic(kaddr); |
09cbfeaf | 241 | put_page(page); |
c8b97818 CM |
242 | } |
243 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 244 | btrfs_release_path(path); |
c8b97818 | 245 | |
9ddc959e JB |
246 | /* |
247 | * We align size to sectorsize for inline extents just for simplicity | |
248 | * sake. | |
249 | */ | |
250 | size = ALIGN(size, root->fs_info->sectorsize); | |
251 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); | |
252 | if (ret) | |
253 | goto fail; | |
254 | ||
c2167754 YZ |
255 | /* |
256 | * we're an inline extent, so nobody can | |
257 | * extend the file past i_size without locking | |
258 | * a page we already have locked. | |
259 | * | |
260 | * We must do any isize and inode updates | |
261 | * before we unlock the pages. Otherwise we | |
262 | * could end up racing with unlink. | |
263 | */ | |
c8b97818 | 264 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 265 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 266 | |
c8b97818 | 267 | fail: |
79b4f4c6 | 268 | return ret; |
c8b97818 CM |
269 | } |
270 | ||
271 | ||
272 | /* | |
273 | * conditionally insert an inline extent into the file. This | |
274 | * does the checks required to make sure the data is small enough | |
275 | * to fit as an inline extent. | |
276 | */ | |
a0349401 | 277 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, |
00361589 JB |
278 | u64 end, size_t compressed_size, |
279 | int compress_type, | |
280 | struct page **compressed_pages) | |
c8b97818 | 281 | { |
a0349401 | 282 | struct btrfs_root *root = inode->root; |
0b246afa | 283 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 284 | struct btrfs_trans_handle *trans; |
a0349401 | 285 | u64 isize = i_size_read(&inode->vfs_inode); |
c8b97818 CM |
286 | u64 actual_end = min(end + 1, isize); |
287 | u64 inline_len = actual_end - start; | |
0b246afa | 288 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
289 | u64 data_len = inline_len; |
290 | int ret; | |
1acae57b FDBM |
291 | struct btrfs_path *path; |
292 | int extent_inserted = 0; | |
293 | u32 extent_item_size; | |
c8b97818 CM |
294 | |
295 | if (compressed_size) | |
296 | data_len = compressed_size; | |
297 | ||
298 | if (start > 0 || | |
0b246afa JM |
299 | actual_end > fs_info->sectorsize || |
300 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 301 | (!compressed_size && |
0b246afa | 302 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 303 | end + 1 < isize || |
0b246afa | 304 | data_len > fs_info->max_inline) { |
c8b97818 CM |
305 | return 1; |
306 | } | |
307 | ||
1acae57b FDBM |
308 | path = btrfs_alloc_path(); |
309 | if (!path) | |
310 | return -ENOMEM; | |
311 | ||
00361589 | 312 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
313 | if (IS_ERR(trans)) { |
314 | btrfs_free_path(path); | |
00361589 | 315 | return PTR_ERR(trans); |
1acae57b | 316 | } |
a0349401 | 317 | trans->block_rsv = &inode->block_rsv; |
00361589 | 318 | |
1acae57b FDBM |
319 | if (compressed_size && compressed_pages) |
320 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
321 | compressed_size); | |
322 | else | |
323 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
324 | inline_len); | |
325 | ||
a0349401 NB |
326 | ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end, |
327 | NULL, 1, 1, extent_item_size, | |
328 | &extent_inserted); | |
00361589 | 329 | if (ret) { |
66642832 | 330 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
331 | goto out; |
332 | } | |
c8b97818 CM |
333 | |
334 | if (isize > actual_end) | |
335 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b | 336 | ret = insert_inline_extent(trans, path, extent_inserted, |
a0349401 | 337 | root, &inode->vfs_inode, start, |
c8b97818 | 338 | inline_len, compressed_size, |
fe3f566c | 339 | compress_type, compressed_pages); |
2adcac1a | 340 | if (ret && ret != -ENOSPC) { |
66642832 | 341 | btrfs_abort_transaction(trans, ret); |
00361589 | 342 | goto out; |
2adcac1a | 343 | } else if (ret == -ENOSPC) { |
00361589 JB |
344 | ret = 1; |
345 | goto out; | |
79787eaa | 346 | } |
2adcac1a | 347 | |
a0349401 NB |
348 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
349 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); | |
00361589 | 350 | out: |
94ed938a QW |
351 | /* |
352 | * Don't forget to free the reserved space, as for inlined extent | |
353 | * it won't count as data extent, free them directly here. | |
354 | * And at reserve time, it's always aligned to page size, so | |
355 | * just free one page here. | |
356 | */ | |
a0349401 | 357 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 358 | btrfs_free_path(path); |
3a45bb20 | 359 | btrfs_end_transaction(trans); |
00361589 | 360 | return ret; |
c8b97818 CM |
361 | } |
362 | ||
771ed689 CM |
363 | struct async_extent { |
364 | u64 start; | |
365 | u64 ram_size; | |
366 | u64 compressed_size; | |
367 | struct page **pages; | |
368 | unsigned long nr_pages; | |
261507a0 | 369 | int compress_type; |
771ed689 CM |
370 | struct list_head list; |
371 | }; | |
372 | ||
97db1204 | 373 | struct async_chunk { |
771ed689 | 374 | struct inode *inode; |
771ed689 CM |
375 | struct page *locked_page; |
376 | u64 start; | |
377 | u64 end; | |
f82b7359 | 378 | unsigned int write_flags; |
771ed689 | 379 | struct list_head extents; |
ec39f769 | 380 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 381 | struct btrfs_work work; |
97db1204 | 382 | atomic_t *pending; |
771ed689 CM |
383 | }; |
384 | ||
97db1204 NB |
385 | struct async_cow { |
386 | /* Number of chunks in flight; must be first in the structure */ | |
387 | atomic_t num_chunks; | |
388 | struct async_chunk chunks[]; | |
771ed689 CM |
389 | }; |
390 | ||
97db1204 | 391 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
392 | u64 start, u64 ram_size, |
393 | u64 compressed_size, | |
394 | struct page **pages, | |
261507a0 LZ |
395 | unsigned long nr_pages, |
396 | int compress_type) | |
771ed689 CM |
397 | { |
398 | struct async_extent *async_extent; | |
399 | ||
400 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 401 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
402 | async_extent->start = start; |
403 | async_extent->ram_size = ram_size; | |
404 | async_extent->compressed_size = compressed_size; | |
405 | async_extent->pages = pages; | |
406 | async_extent->nr_pages = nr_pages; | |
261507a0 | 407 | async_extent->compress_type = compress_type; |
771ed689 CM |
408 | list_add_tail(&async_extent->list, &cow->extents); |
409 | return 0; | |
410 | } | |
411 | ||
42c16da6 QW |
412 | /* |
413 | * Check if the inode has flags compatible with compression | |
414 | */ | |
415 | static inline bool inode_can_compress(struct inode *inode) | |
416 | { | |
417 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW || | |
418 | BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
419 | return false; | |
420 | return true; | |
421 | } | |
422 | ||
423 | /* | |
424 | * Check if the inode needs to be submitted to compression, based on mount | |
425 | * options, defragmentation, properties or heuristics. | |
426 | */ | |
c2fcdcdf | 427 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
f79707b0 | 428 | { |
0b246afa | 429 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 | 430 | |
42c16da6 QW |
431 | if (!inode_can_compress(inode)) { |
432 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), | |
433 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
434 | btrfs_ino(BTRFS_I(inode))); | |
435 | return 0; | |
436 | } | |
f79707b0 | 437 | /* force compress */ |
0b246afa | 438 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 439 | return 1; |
eec63c65 DS |
440 | /* defrag ioctl */ |
441 | if (BTRFS_I(inode)->defrag_compress) | |
442 | return 1; | |
f79707b0 WS |
443 | /* bad compression ratios */ |
444 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
445 | return 0; | |
0b246afa | 446 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 | 447 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
b52aa8c9 | 448 | BTRFS_I(inode)->prop_compress) |
c2fcdcdf | 449 | return btrfs_compress_heuristic(inode, start, end); |
f79707b0 WS |
450 | return 0; |
451 | } | |
452 | ||
6158e1ce | 453 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
454 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
455 | { | |
456 | /* If this is a small write inside eof, kick off a defrag */ | |
457 | if (num_bytes < small_write && | |
6158e1ce | 458 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
459 | btrfs_add_inode_defrag(NULL, inode); |
460 | } | |
461 | ||
d352ac68 | 462 | /* |
771ed689 CM |
463 | * we create compressed extents in two phases. The first |
464 | * phase compresses a range of pages that have already been | |
465 | * locked (both pages and state bits are locked). | |
c8b97818 | 466 | * |
771ed689 CM |
467 | * This is done inside an ordered work queue, and the compression |
468 | * is spread across many cpus. The actual IO submission is step | |
469 | * two, and the ordered work queue takes care of making sure that | |
470 | * happens in the same order things were put onto the queue by | |
471 | * writepages and friends. | |
c8b97818 | 472 | * |
771ed689 CM |
473 | * If this code finds it can't get good compression, it puts an |
474 | * entry onto the work queue to write the uncompressed bytes. This | |
475 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
476 | * are written in the same order that the flusher thread sent them |
477 | * down. | |
d352ac68 | 478 | */ |
ac3e9933 | 479 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 480 | { |
1368c6da | 481 | struct inode *inode = async_chunk->inode; |
0b246afa | 482 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 483 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
484 | u64 start = async_chunk->start; |
485 | u64 end = async_chunk->end; | |
c8b97818 | 486 | u64 actual_end; |
d98da499 | 487 | u64 i_size; |
e6dcd2dc | 488 | int ret = 0; |
c8b97818 CM |
489 | struct page **pages = NULL; |
490 | unsigned long nr_pages; | |
c8b97818 CM |
491 | unsigned long total_compressed = 0; |
492 | unsigned long total_in = 0; | |
c8b97818 CM |
493 | int i; |
494 | int will_compress; | |
0b246afa | 495 | int compress_type = fs_info->compress_type; |
ac3e9933 | 496 | int compressed_extents = 0; |
4adaa611 | 497 | int redirty = 0; |
b888db2b | 498 | |
6158e1ce NB |
499 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
500 | SZ_16K); | |
4cb5300b | 501 | |
d98da499 JB |
502 | /* |
503 | * We need to save i_size before now because it could change in between | |
504 | * us evaluating the size and assigning it. This is because we lock and | |
505 | * unlock the page in truncate and fallocate, and then modify the i_size | |
506 | * later on. | |
507 | * | |
508 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
509 | * does that for us. | |
510 | */ | |
511 | barrier(); | |
512 | i_size = i_size_read(inode); | |
513 | barrier(); | |
514 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
515 | again: |
516 | will_compress = 0; | |
09cbfeaf | 517 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
518 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
519 | nr_pages = min_t(unsigned long, nr_pages, | |
520 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 521 | |
f03d9301 CM |
522 | /* |
523 | * we don't want to send crud past the end of i_size through | |
524 | * compression, that's just a waste of CPU time. So, if the | |
525 | * end of the file is before the start of our current | |
526 | * requested range of bytes, we bail out to the uncompressed | |
527 | * cleanup code that can deal with all of this. | |
528 | * | |
529 | * It isn't really the fastest way to fix things, but this is a | |
530 | * very uncommon corner. | |
531 | */ | |
532 | if (actual_end <= start) | |
533 | goto cleanup_and_bail_uncompressed; | |
534 | ||
c8b97818 CM |
535 | total_compressed = actual_end - start; |
536 | ||
4bcbb332 SW |
537 | /* |
538 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 539 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
540 | */ |
541 | if (total_compressed <= blocksize && | |
542 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
543 | goto cleanup_and_bail_uncompressed; | |
544 | ||
069eac78 DS |
545 | total_compressed = min_t(unsigned long, total_compressed, |
546 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
547 | total_in = 0; |
548 | ret = 0; | |
db94535d | 549 | |
771ed689 CM |
550 | /* |
551 | * we do compression for mount -o compress and when the | |
552 | * inode has not been flagged as nocompress. This flag can | |
553 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 554 | */ |
c2fcdcdf | 555 | if (inode_need_compress(inode, start, end)) { |
c8b97818 | 556 | WARN_ON(pages); |
31e818fe | 557 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
558 | if (!pages) { |
559 | /* just bail out to the uncompressed code */ | |
3527a018 | 560 | nr_pages = 0; |
560f7d75 LZ |
561 | goto cont; |
562 | } | |
c8b97818 | 563 | |
eec63c65 DS |
564 | if (BTRFS_I(inode)->defrag_compress) |
565 | compress_type = BTRFS_I(inode)->defrag_compress; | |
566 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 567 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 568 | |
4adaa611 CM |
569 | /* |
570 | * we need to call clear_page_dirty_for_io on each | |
571 | * page in the range. Otherwise applications with the file | |
572 | * mmap'd can wander in and change the page contents while | |
573 | * we are compressing them. | |
574 | * | |
575 | * If the compression fails for any reason, we set the pages | |
576 | * dirty again later on. | |
e9679de3 TT |
577 | * |
578 | * Note that the remaining part is redirtied, the start pointer | |
579 | * has moved, the end is the original one. | |
4adaa611 | 580 | */ |
e9679de3 TT |
581 | if (!redirty) { |
582 | extent_range_clear_dirty_for_io(inode, start, end); | |
583 | redirty = 1; | |
584 | } | |
f51d2b59 DS |
585 | |
586 | /* Compression level is applied here and only here */ | |
587 | ret = btrfs_compress_pages( | |
588 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 589 | inode->i_mapping, start, |
38c31464 | 590 | pages, |
4d3a800e | 591 | &nr_pages, |
261507a0 | 592 | &total_in, |
e5d74902 | 593 | &total_compressed); |
c8b97818 CM |
594 | |
595 | if (!ret) { | |
7073017a | 596 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 597 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
598 | char *kaddr; |
599 | ||
600 | /* zero the tail end of the last page, we might be | |
601 | * sending it down to disk | |
602 | */ | |
603 | if (offset) { | |
7ac687d9 | 604 | kaddr = kmap_atomic(page); |
c8b97818 | 605 | memset(kaddr + offset, 0, |
09cbfeaf | 606 | PAGE_SIZE - offset); |
7ac687d9 | 607 | kunmap_atomic(kaddr); |
c8b97818 CM |
608 | } |
609 | will_compress = 1; | |
610 | } | |
611 | } | |
560f7d75 | 612 | cont: |
c8b97818 CM |
613 | if (start == 0) { |
614 | /* lets try to make an inline extent */ | |
6018ba0a | 615 | if (ret || total_in < actual_end) { |
c8b97818 | 616 | /* we didn't compress the entire range, try |
771ed689 | 617 | * to make an uncompressed inline extent. |
c8b97818 | 618 | */ |
a0349401 NB |
619 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
620 | 0, BTRFS_COMPRESS_NONE, | |
621 | NULL); | |
c8b97818 | 622 | } else { |
771ed689 | 623 | /* try making a compressed inline extent */ |
a0349401 | 624 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
fe3f566c LZ |
625 | total_compressed, |
626 | compress_type, pages); | |
c8b97818 | 627 | } |
79787eaa | 628 | if (ret <= 0) { |
151a41bc | 629 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
630 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
631 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
632 | unsigned long page_error_op; |
633 | ||
e6eb4314 | 634 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 635 | |
771ed689 | 636 | /* |
79787eaa JM |
637 | * inline extent creation worked or returned error, |
638 | * we don't need to create any more async work items. | |
639 | * Unlock and free up our temp pages. | |
8b62f87b JB |
640 | * |
641 | * We use DO_ACCOUNTING here because we need the | |
642 | * delalloc_release_metadata to be done _after_ we drop | |
643 | * our outstanding extent for clearing delalloc for this | |
644 | * range. | |
771ed689 | 645 | */ |
ad7ff17b NB |
646 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
647 | NULL, | |
74e9194a | 648 | clear_flags, |
ba8b04c1 | 649 | PAGE_UNLOCK | |
c2790a2e JB |
650 | PAGE_CLEAR_DIRTY | |
651 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 652 | page_error_op | |
c2790a2e | 653 | PAGE_END_WRITEBACK); |
cecc8d90 NB |
654 | |
655 | for (i = 0; i < nr_pages; i++) { | |
656 | WARN_ON(pages[i]->mapping); | |
657 | put_page(pages[i]); | |
658 | } | |
659 | kfree(pages); | |
660 | ||
661 | return 0; | |
c8b97818 CM |
662 | } |
663 | } | |
664 | ||
665 | if (will_compress) { | |
666 | /* | |
667 | * we aren't doing an inline extent round the compressed size | |
668 | * up to a block size boundary so the allocator does sane | |
669 | * things | |
670 | */ | |
fda2832f | 671 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
672 | |
673 | /* | |
674 | * one last check to make sure the compression is really a | |
170607eb TT |
675 | * win, compare the page count read with the blocks on disk, |
676 | * compression must free at least one sector size | |
c8b97818 | 677 | */ |
09cbfeaf | 678 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 679 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 680 | compressed_extents++; |
c8bb0c8b AS |
681 | |
682 | /* | |
683 | * The async work queues will take care of doing actual | |
684 | * allocation on disk for these compressed pages, and | |
685 | * will submit them to the elevator. | |
686 | */ | |
b5326271 | 687 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 688 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
689 | compress_type); |
690 | ||
1170862d TT |
691 | if (start + total_in < end) { |
692 | start += total_in; | |
c8bb0c8b AS |
693 | pages = NULL; |
694 | cond_resched(); | |
695 | goto again; | |
696 | } | |
ac3e9933 | 697 | return compressed_extents; |
c8b97818 CM |
698 | } |
699 | } | |
c8bb0c8b | 700 | if (pages) { |
c8b97818 CM |
701 | /* |
702 | * the compression code ran but failed to make things smaller, | |
703 | * free any pages it allocated and our page pointer array | |
704 | */ | |
4d3a800e | 705 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 706 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 707 | put_page(pages[i]); |
c8b97818 CM |
708 | } |
709 | kfree(pages); | |
710 | pages = NULL; | |
711 | total_compressed = 0; | |
4d3a800e | 712 | nr_pages = 0; |
c8b97818 CM |
713 | |
714 | /* flag the file so we don't compress in the future */ | |
0b246afa | 715 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 716 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 717 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 718 | } |
c8b97818 | 719 | } |
f03d9301 | 720 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
721 | /* |
722 | * No compression, but we still need to write the pages in the file | |
723 | * we've been given so far. redirty the locked page if it corresponds | |
724 | * to our extent and set things up for the async work queue to run | |
725 | * cow_file_range to do the normal delalloc dance. | |
726 | */ | |
1d53c9e6 CM |
727 | if (async_chunk->locked_page && |
728 | (page_offset(async_chunk->locked_page) >= start && | |
729 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 730 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 731 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 732 | } |
c8bb0c8b AS |
733 | |
734 | if (redirty) | |
735 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 736 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 737 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 738 | compressed_extents++; |
3b951516 | 739 | |
ac3e9933 | 740 | return compressed_extents; |
771ed689 | 741 | } |
771ed689 | 742 | |
40ae837b FM |
743 | static void free_async_extent_pages(struct async_extent *async_extent) |
744 | { | |
745 | int i; | |
746 | ||
747 | if (!async_extent->pages) | |
748 | return; | |
749 | ||
750 | for (i = 0; i < async_extent->nr_pages; i++) { | |
751 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 752 | put_page(async_extent->pages[i]); |
40ae837b FM |
753 | } |
754 | kfree(async_extent->pages); | |
755 | async_extent->nr_pages = 0; | |
756 | async_extent->pages = NULL; | |
771ed689 CM |
757 | } |
758 | ||
759 | /* | |
760 | * phase two of compressed writeback. This is the ordered portion | |
761 | * of the code, which only gets called in the order the work was | |
762 | * queued. We walk all the async extents created by compress_file_range | |
763 | * and send them down to the disk. | |
764 | */ | |
b5326271 | 765 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
771ed689 | 766 | { |
a0ff10dc NB |
767 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
768 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
771ed689 CM |
769 | struct async_extent *async_extent; |
770 | u64 alloc_hint = 0; | |
771ed689 CM |
771 | struct btrfs_key ins; |
772 | struct extent_map *em; | |
a0ff10dc NB |
773 | struct btrfs_root *root = inode->root; |
774 | struct extent_io_tree *io_tree = &inode->io_tree; | |
f5a84ee3 | 775 | int ret = 0; |
771ed689 | 776 | |
3e04e7f1 | 777 | again: |
b5326271 NB |
778 | while (!list_empty(&async_chunk->extents)) { |
779 | async_extent = list_entry(async_chunk->extents.next, | |
771ed689 CM |
780 | struct async_extent, list); |
781 | list_del(&async_extent->list); | |
c8b97818 | 782 | |
f5a84ee3 | 783 | retry: |
7447555f NB |
784 | lock_extent(io_tree, async_extent->start, |
785 | async_extent->start + async_extent->ram_size - 1); | |
771ed689 CM |
786 | /* did the compression code fall back to uncompressed IO? */ |
787 | if (!async_extent->pages) { | |
788 | int page_started = 0; | |
789 | unsigned long nr_written = 0; | |
790 | ||
771ed689 | 791 | /* allocate blocks */ |
a0ff10dc | 792 | ret = cow_file_range(inode, async_chunk->locked_page, |
f5a84ee3 JB |
793 | async_extent->start, |
794 | async_extent->start + | |
795 | async_extent->ram_size - 1, | |
330a5827 | 796 | &page_started, &nr_written, 0); |
771ed689 | 797 | |
79787eaa JM |
798 | /* JDM XXX */ |
799 | ||
771ed689 CM |
800 | /* |
801 | * if page_started, cow_file_range inserted an | |
802 | * inline extent and took care of all the unlocking | |
803 | * and IO for us. Otherwise, we need to submit | |
804 | * all those pages down to the drive. | |
805 | */ | |
f5a84ee3 | 806 | if (!page_started && !ret) |
a0ff10dc | 807 | extent_write_locked_range(&inode->vfs_inode, |
5e3ee236 | 808 | async_extent->start, |
d397712b | 809 | async_extent->start + |
771ed689 | 810 | async_extent->ram_size - 1, |
771ed689 | 811 | WB_SYNC_ALL); |
1d53c9e6 | 812 | else if (ret && async_chunk->locked_page) |
b5326271 | 813 | unlock_page(async_chunk->locked_page); |
771ed689 CM |
814 | kfree(async_extent); |
815 | cond_resched(); | |
816 | continue; | |
817 | } | |
818 | ||
18513091 | 819 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
820 | async_extent->compressed_size, |
821 | async_extent->compressed_size, | |
e570fd27 | 822 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 823 | if (ret) { |
40ae837b | 824 | free_async_extent_pages(async_extent); |
3e04e7f1 | 825 | |
fdf8e2ea JB |
826 | if (ret == -ENOSPC) { |
827 | unlock_extent(io_tree, async_extent->start, | |
828 | async_extent->start + | |
829 | async_extent->ram_size - 1); | |
ce62003f LB |
830 | |
831 | /* | |
832 | * we need to redirty the pages if we decide to | |
833 | * fallback to uncompressed IO, otherwise we | |
834 | * will not submit these pages down to lower | |
835 | * layers. | |
836 | */ | |
a0ff10dc | 837 | extent_range_redirty_for_io(&inode->vfs_inode, |
ce62003f LB |
838 | async_extent->start, |
839 | async_extent->start + | |
840 | async_extent->ram_size - 1); | |
841 | ||
79787eaa | 842 | goto retry; |
fdf8e2ea | 843 | } |
3e04e7f1 | 844 | goto out_free; |
f5a84ee3 | 845 | } |
c2167754 YZ |
846 | /* |
847 | * here we're doing allocation and writeback of the | |
848 | * compressed pages | |
849 | */ | |
a0ff10dc | 850 | em = create_io_em(inode, async_extent->start, |
6f9994db LB |
851 | async_extent->ram_size, /* len */ |
852 | async_extent->start, /* orig_start */ | |
853 | ins.objectid, /* block_start */ | |
854 | ins.offset, /* block_len */ | |
855 | ins.offset, /* orig_block_len */ | |
856 | async_extent->ram_size, /* ram_bytes */ | |
857 | async_extent->compress_type, | |
858 | BTRFS_ORDERED_COMPRESSED); | |
859 | if (IS_ERR(em)) | |
860 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 861 | goto out_free_reserve; |
6f9994db | 862 | free_extent_map(em); |
3e04e7f1 | 863 | |
a0ff10dc | 864 | ret = btrfs_add_ordered_extent_compress(inode, |
261507a0 LZ |
865 | async_extent->start, |
866 | ins.objectid, | |
867 | async_extent->ram_size, | |
868 | ins.offset, | |
869 | BTRFS_ORDERED_COMPRESSED, | |
870 | async_extent->compress_type); | |
d9f85963 | 871 | if (ret) { |
a0ff10dc | 872 | btrfs_drop_extent_cache(inode, async_extent->start, |
d9f85963 FM |
873 | async_extent->start + |
874 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 875 | goto out_free_reserve; |
d9f85963 | 876 | } |
0b246afa | 877 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 878 | |
771ed689 CM |
879 | /* |
880 | * clear dirty, set writeback and unlock the pages. | |
881 | */ | |
a0ff10dc | 882 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
883 | async_extent->start + |
884 | async_extent->ram_size - 1, | |
151a41bc JB |
885 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
886 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 887 | PAGE_SET_WRITEBACK); |
a0ff10dc | 888 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
d397712b CM |
889 | async_extent->ram_size, |
890 | ins.objectid, | |
891 | ins.offset, async_extent->pages, | |
f82b7359 | 892 | async_extent->nr_pages, |
ec39f769 CM |
893 | async_chunk->write_flags, |
894 | async_chunk->blkcg_css)) { | |
fce2a4e6 FM |
895 | struct page *p = async_extent->pages[0]; |
896 | const u64 start = async_extent->start; | |
897 | const u64 end = start + async_extent->ram_size - 1; | |
898 | ||
a0ff10dc | 899 | p->mapping = inode->vfs_inode.i_mapping; |
c629732d | 900 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
7087a9d8 | 901 | |
fce2a4e6 | 902 | p->mapping = NULL; |
a0ff10dc | 903 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
fce2a4e6 FM |
904 | PAGE_END_WRITEBACK | |
905 | PAGE_SET_ERROR); | |
40ae837b | 906 | free_async_extent_pages(async_extent); |
fce2a4e6 | 907 | } |
771ed689 CM |
908 | alloc_hint = ins.objectid + ins.offset; |
909 | kfree(async_extent); | |
910 | cond_resched(); | |
911 | } | |
dec8f175 | 912 | return; |
3e04e7f1 | 913 | out_free_reserve: |
0b246afa | 914 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 915 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 916 | out_free: |
a0ff10dc | 917 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
918 | async_extent->start + |
919 | async_extent->ram_size - 1, | |
c2790a2e | 920 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 921 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
922 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
923 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
924 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
925 | PAGE_SET_ERROR); | |
40ae837b | 926 | free_async_extent_pages(async_extent); |
79787eaa | 927 | kfree(async_extent); |
3e04e7f1 | 928 | goto again; |
771ed689 CM |
929 | } |
930 | ||
43c69849 | 931 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
4b46fce2 JB |
932 | u64 num_bytes) |
933 | { | |
43c69849 | 934 | struct extent_map_tree *em_tree = &inode->extent_tree; |
4b46fce2 JB |
935 | struct extent_map *em; |
936 | u64 alloc_hint = 0; | |
937 | ||
938 | read_lock(&em_tree->lock); | |
939 | em = search_extent_mapping(em_tree, start, num_bytes); | |
940 | if (em) { | |
941 | /* | |
942 | * if block start isn't an actual block number then find the | |
943 | * first block in this inode and use that as a hint. If that | |
944 | * block is also bogus then just don't worry about it. | |
945 | */ | |
946 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
947 | free_extent_map(em); | |
948 | em = search_extent_mapping(em_tree, 0, 0); | |
949 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
950 | alloc_hint = em->block_start; | |
951 | if (em) | |
952 | free_extent_map(em); | |
953 | } else { | |
954 | alloc_hint = em->block_start; | |
955 | free_extent_map(em); | |
956 | } | |
957 | } | |
958 | read_unlock(&em_tree->lock); | |
959 | ||
960 | return alloc_hint; | |
961 | } | |
962 | ||
771ed689 CM |
963 | /* |
964 | * when extent_io.c finds a delayed allocation range in the file, | |
965 | * the call backs end up in this code. The basic idea is to | |
966 | * allocate extents on disk for the range, and create ordered data structs | |
967 | * in ram to track those extents. | |
968 | * | |
969 | * locked_page is the page that writepage had locked already. We use | |
970 | * it to make sure we don't do extra locks or unlocks. | |
971 | * | |
972 | * *page_started is set to one if we unlock locked_page and do everything | |
973 | * required to start IO on it. It may be clean and already done with | |
974 | * IO when we return. | |
975 | */ | |
6e26c442 | 976 | static noinline int cow_file_range(struct btrfs_inode *inode, |
00361589 | 977 | struct page *locked_page, |
74e9194a | 978 | u64 start, u64 end, int *page_started, |
330a5827 | 979 | unsigned long *nr_written, int unlock) |
771ed689 | 980 | { |
6e26c442 NB |
981 | struct btrfs_root *root = inode->root; |
982 | struct btrfs_fs_info *fs_info = root->fs_info; | |
771ed689 CM |
983 | u64 alloc_hint = 0; |
984 | u64 num_bytes; | |
985 | unsigned long ram_size; | |
a315e68f | 986 | u64 cur_alloc_size = 0; |
432cd2a1 | 987 | u64 min_alloc_size; |
0b246afa | 988 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
989 | struct btrfs_key ins; |
990 | struct extent_map *em; | |
a315e68f FM |
991 | unsigned clear_bits; |
992 | unsigned long page_ops; | |
993 | bool extent_reserved = false; | |
771ed689 CM |
994 | int ret = 0; |
995 | ||
6e26c442 | 996 | if (btrfs_is_free_space_inode(inode)) { |
02ecd2c2 | 997 | WARN_ON_ONCE(1); |
29bce2f3 JB |
998 | ret = -EINVAL; |
999 | goto out_unlock; | |
02ecd2c2 | 1000 | } |
771ed689 | 1001 | |
fda2832f | 1002 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 1003 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 1004 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1005 | |
6e26c442 | 1006 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 1007 | |
771ed689 CM |
1008 | if (start == 0) { |
1009 | /* lets try to make an inline extent */ | |
6e26c442 | 1010 | ret = cow_file_range_inline(inode, start, end, 0, |
d02c0e20 | 1011 | BTRFS_COMPRESS_NONE, NULL); |
771ed689 | 1012 | if (ret == 0) { |
8b62f87b JB |
1013 | /* |
1014 | * We use DO_ACCOUNTING here because we need the | |
1015 | * delalloc_release_metadata to be run _after_ we drop | |
1016 | * our outstanding extent for clearing delalloc for this | |
1017 | * range. | |
1018 | */ | |
6e26c442 | 1019 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
c2790a2e | 1020 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1021 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1022 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
1023 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1024 | PAGE_END_WRITEBACK); | |
771ed689 | 1025 | *nr_written = *nr_written + |
09cbfeaf | 1026 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1027 | *page_started = 1; |
771ed689 | 1028 | goto out; |
79787eaa | 1029 | } else if (ret < 0) { |
79787eaa | 1030 | goto out_unlock; |
771ed689 CM |
1031 | } |
1032 | } | |
1033 | ||
6e26c442 NB |
1034 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
1035 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | |
771ed689 | 1036 | |
432cd2a1 FM |
1037 | /* |
1038 | * Relocation relies on the relocated extents to have exactly the same | |
1039 | * size as the original extents. Normally writeback for relocation data | |
1040 | * extents follows a NOCOW path because relocation preallocates the | |
1041 | * extents. However, due to an operation such as scrub turning a block | |
1042 | * group to RO mode, it may fallback to COW mode, so we must make sure | |
1043 | * an extent allocated during COW has exactly the requested size and can | |
1044 | * not be split into smaller extents, otherwise relocation breaks and | |
1045 | * fails during the stage where it updates the bytenr of file extent | |
1046 | * items. | |
1047 | */ | |
1048 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1049 | min_alloc_size = num_bytes; | |
1050 | else | |
1051 | min_alloc_size = fs_info->sectorsize; | |
1052 | ||
3752d22f AJ |
1053 | while (num_bytes > 0) { |
1054 | cur_alloc_size = num_bytes; | |
18513091 | 1055 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
432cd2a1 | 1056 | min_alloc_size, 0, alloc_hint, |
e570fd27 | 1057 | &ins, 1, 1); |
00361589 | 1058 | if (ret < 0) |
79787eaa | 1059 | goto out_unlock; |
a315e68f FM |
1060 | cur_alloc_size = ins.offset; |
1061 | extent_reserved = true; | |
d397712b | 1062 | |
771ed689 | 1063 | ram_size = ins.offset; |
6e26c442 | 1064 | em = create_io_em(inode, start, ins.offset, /* len */ |
6f9994db LB |
1065 | start, /* orig_start */ |
1066 | ins.objectid, /* block_start */ | |
1067 | ins.offset, /* block_len */ | |
1068 | ins.offset, /* orig_block_len */ | |
1069 | ram_size, /* ram_bytes */ | |
1070 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1071 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1072 | if (IS_ERR(em)) { |
1073 | ret = PTR_ERR(em); | |
ace68bac | 1074 | goto out_reserve; |
090a127a | 1075 | } |
6f9994db | 1076 | free_extent_map(em); |
e6dcd2dc | 1077 | |
6e26c442 NB |
1078 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
1079 | ram_size, cur_alloc_size, 0); | |
ace68bac | 1080 | if (ret) |
d9f85963 | 1081 | goto out_drop_extent_cache; |
c8b97818 | 1082 | |
17d217fe YZ |
1083 | if (root->root_key.objectid == |
1084 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
6e26c442 | 1085 | ret = btrfs_reloc_clone_csums(inode, start, |
17d217fe | 1086 | cur_alloc_size); |
4dbd80fb QW |
1087 | /* |
1088 | * Only drop cache here, and process as normal. | |
1089 | * | |
1090 | * We must not allow extent_clear_unlock_delalloc() | |
1091 | * at out_unlock label to free meta of this ordered | |
1092 | * extent, as its meta should be freed by | |
1093 | * btrfs_finish_ordered_io(). | |
1094 | * | |
1095 | * So we must continue until @start is increased to | |
1096 | * skip current ordered extent. | |
1097 | */ | |
00361589 | 1098 | if (ret) |
6e26c442 | 1099 | btrfs_drop_extent_cache(inode, start, |
4dbd80fb | 1100 | start + ram_size - 1, 0); |
17d217fe YZ |
1101 | } |
1102 | ||
0b246afa | 1103 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1104 | |
c8b97818 CM |
1105 | /* we're not doing compressed IO, don't unlock the first |
1106 | * page (which the caller expects to stay locked), don't | |
1107 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1108 | * |
1109 | * Do set the Private2 bit so we know this page was properly | |
1110 | * setup for writepage | |
c8b97818 | 1111 | */ |
a315e68f FM |
1112 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1113 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1114 | |
6e26c442 | 1115 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
74e9194a | 1116 | locked_page, |
c2790a2e | 1117 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1118 | page_ops); |
3752d22f AJ |
1119 | if (num_bytes < cur_alloc_size) |
1120 | num_bytes = 0; | |
4dbd80fb | 1121 | else |
3752d22f | 1122 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1123 | alloc_hint = ins.objectid + ins.offset; |
1124 | start += cur_alloc_size; | |
a315e68f | 1125 | extent_reserved = false; |
4dbd80fb QW |
1126 | |
1127 | /* | |
1128 | * btrfs_reloc_clone_csums() error, since start is increased | |
1129 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1130 | * free metadata of current ordered extent, we're OK to exit. | |
1131 | */ | |
1132 | if (ret) | |
1133 | goto out_unlock; | |
b888db2b | 1134 | } |
79787eaa | 1135 | out: |
be20aa9d | 1136 | return ret; |
b7d5b0a8 | 1137 | |
d9f85963 | 1138 | out_drop_extent_cache: |
6e26c442 | 1139 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
ace68bac | 1140 | out_reserve: |
0b246afa | 1141 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1142 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1143 | out_unlock: |
a7e3b975 FM |
1144 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1145 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1146 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1147 | PAGE_END_WRITEBACK; | |
1148 | /* | |
1149 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1150 | * failed to create the respective ordered extent, then it means that | |
1151 | * when we reserved the extent we decremented the extent's size from | |
1152 | * the data space_info's bytes_may_use counter and incremented the | |
1153 | * space_info's bytes_reserved counter by the same amount. We must make | |
1154 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1155 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1156 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1157 | */ | |
1158 | if (extent_reserved) { | |
6e26c442 | 1159 | extent_clear_unlock_delalloc(inode, start, |
e2c8e92d | 1160 | start + cur_alloc_size - 1, |
a315e68f FM |
1161 | locked_page, |
1162 | clear_bits, | |
1163 | page_ops); | |
1164 | start += cur_alloc_size; | |
1165 | if (start >= end) | |
1166 | goto out; | |
1167 | } | |
6e26c442 | 1168 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1169 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1170 | page_ops); | |
79787eaa | 1171 | goto out; |
771ed689 | 1172 | } |
c8b97818 | 1173 | |
771ed689 CM |
1174 | /* |
1175 | * work queue call back to started compression on a file and pages | |
1176 | */ | |
1177 | static noinline void async_cow_start(struct btrfs_work *work) | |
1178 | { | |
b5326271 | 1179 | struct async_chunk *async_chunk; |
ac3e9933 | 1180 | int compressed_extents; |
771ed689 | 1181 | |
b5326271 | 1182 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1183 | |
ac3e9933 NB |
1184 | compressed_extents = compress_file_range(async_chunk); |
1185 | if (compressed_extents == 0) { | |
b5326271 NB |
1186 | btrfs_add_delayed_iput(async_chunk->inode); |
1187 | async_chunk->inode = NULL; | |
8180ef88 | 1188 | } |
771ed689 CM |
1189 | } |
1190 | ||
1191 | /* | |
1192 | * work queue call back to submit previously compressed pages | |
1193 | */ | |
1194 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1195 | { | |
c5a68aec NB |
1196 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1197 | work); | |
1198 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1199 | unsigned long nr_pages; |
1200 | ||
b5326271 | 1201 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1202 | PAGE_SHIFT; |
771ed689 | 1203 | |
093258e6 | 1204 | /* atomic_sub_return implies a barrier */ |
0b246afa | 1205 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
093258e6 DS |
1206 | 5 * SZ_1M) |
1207 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1208 | |
4546d178 | 1209 | /* |
b5326271 | 1210 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1211 | * in which case we don't have anything to submit, yet we need to |
1212 | * always adjust ->async_delalloc_pages as its paired with the init | |
1213 | * happening in cow_file_range_async | |
1214 | */ | |
b5326271 NB |
1215 | if (async_chunk->inode) |
1216 | submit_compressed_extents(async_chunk); | |
771ed689 | 1217 | } |
c8b97818 | 1218 | |
771ed689 CM |
1219 | static noinline void async_cow_free(struct btrfs_work *work) |
1220 | { | |
b5326271 | 1221 | struct async_chunk *async_chunk; |
97db1204 | 1222 | |
b5326271 NB |
1223 | async_chunk = container_of(work, struct async_chunk, work); |
1224 | if (async_chunk->inode) | |
1225 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1226 | if (async_chunk->blkcg_css) |
1227 | css_put(async_chunk->blkcg_css); | |
97db1204 NB |
1228 | /* |
1229 | * Since the pointer to 'pending' is at the beginning of the array of | |
b5326271 | 1230 | * async_chunk's, freeing it ensures the whole array has been freed. |
97db1204 | 1231 | */ |
b5326271 | 1232 | if (atomic_dec_and_test(async_chunk->pending)) |
b1c16ac9 | 1233 | kvfree(async_chunk->pending); |
771ed689 CM |
1234 | } |
1235 | ||
ec39f769 CM |
1236 | static int cow_file_range_async(struct inode *inode, |
1237 | struct writeback_control *wbc, | |
1238 | struct page *locked_page, | |
771ed689 | 1239 | u64 start, u64 end, int *page_started, |
fac07d2b | 1240 | unsigned long *nr_written) |
771ed689 | 1241 | { |
0b246afa | 1242 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
ec39f769 | 1243 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1244 | struct async_cow *ctx; |
1245 | struct async_chunk *async_chunk; | |
771ed689 CM |
1246 | unsigned long nr_pages; |
1247 | u64 cur_end; | |
97db1204 NB |
1248 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1249 | int i; | |
1250 | bool should_compress; | |
b1c16ac9 | 1251 | unsigned nofs_flag; |
fac07d2b | 1252 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1253 | |
69684c5a | 1254 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
97db1204 NB |
1255 | |
1256 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && | |
1257 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { | |
1258 | num_chunks = 1; | |
1259 | should_compress = false; | |
1260 | } else { | |
1261 | should_compress = true; | |
1262 | } | |
1263 | ||
b1c16ac9 NB |
1264 | nofs_flag = memalloc_nofs_save(); |
1265 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1266 | memalloc_nofs_restore(nofs_flag); | |
1267 | ||
97db1204 NB |
1268 | if (!ctx) { |
1269 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1270 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1271 | EXTENT_DO_ACCOUNTING; | |
1272 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1273 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | | |
1274 | PAGE_SET_ERROR; | |
1275 | ||
ad7ff17b NB |
1276 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
1277 | locked_page, clear_bits, page_ops); | |
97db1204 NB |
1278 | return -ENOMEM; |
1279 | } | |
1280 | ||
1281 | async_chunk = ctx->chunks; | |
1282 | atomic_set(&ctx->num_chunks, num_chunks); | |
1283 | ||
1284 | for (i = 0; i < num_chunks; i++) { | |
1285 | if (should_compress) | |
1286 | cur_end = min(end, start + SZ_512K - 1); | |
1287 | else | |
1288 | cur_end = end; | |
771ed689 | 1289 | |
bd4691a0 NB |
1290 | /* |
1291 | * igrab is called higher up in the call chain, take only the | |
1292 | * lightweight reference for the callback lifetime | |
1293 | */ | |
1294 | ihold(inode); | |
97db1204 NB |
1295 | async_chunk[i].pending = &ctx->num_chunks; |
1296 | async_chunk[i].inode = inode; | |
1297 | async_chunk[i].start = start; | |
1298 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1299 | async_chunk[i].write_flags = write_flags; |
1300 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1301 | ||
1d53c9e6 CM |
1302 | /* |
1303 | * The locked_page comes all the way from writepage and its | |
1304 | * the original page we were actually given. As we spread | |
1305 | * this large delalloc region across multiple async_chunk | |
1306 | * structs, only the first struct needs a pointer to locked_page | |
1307 | * | |
1308 | * This way we don't need racey decisions about who is supposed | |
1309 | * to unlock it. | |
1310 | */ | |
1311 | if (locked_page) { | |
ec39f769 CM |
1312 | /* |
1313 | * Depending on the compressibility, the pages might or | |
1314 | * might not go through async. We want all of them to | |
1315 | * be accounted against wbc once. Let's do it here | |
1316 | * before the paths diverge. wbc accounting is used | |
1317 | * only for foreign writeback detection and doesn't | |
1318 | * need full accuracy. Just account the whole thing | |
1319 | * against the first page. | |
1320 | */ | |
1321 | wbc_account_cgroup_owner(wbc, locked_page, | |
1322 | cur_end - start); | |
1d53c9e6 CM |
1323 | async_chunk[i].locked_page = locked_page; |
1324 | locked_page = NULL; | |
1325 | } else { | |
1326 | async_chunk[i].locked_page = NULL; | |
1327 | } | |
1328 | ||
ec39f769 CM |
1329 | if (blkcg_css != blkcg_root_css) { |
1330 | css_get(blkcg_css); | |
1331 | async_chunk[i].blkcg_css = blkcg_css; | |
1332 | } else { | |
1333 | async_chunk[i].blkcg_css = NULL; | |
1334 | } | |
1335 | ||
a0cac0ec OS |
1336 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1337 | async_cow_submit, async_cow_free); | |
771ed689 | 1338 | |
97db1204 | 1339 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1340 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1341 | |
97db1204 | 1342 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1343 | |
771ed689 CM |
1344 | *nr_written += nr_pages; |
1345 | start = cur_end + 1; | |
1346 | } | |
1347 | *page_started = 1; | |
1348 | return 0; | |
be20aa9d CM |
1349 | } |
1350 | ||
2ff7e61e | 1351 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1352 | u64 bytenr, u64 num_bytes) |
1353 | { | |
1354 | int ret; | |
1355 | struct btrfs_ordered_sum *sums; | |
1356 | LIST_HEAD(list); | |
1357 | ||
0b246afa | 1358 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1359 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1360 | if (ret == 0 && list_empty(&list)) |
1361 | return 0; | |
1362 | ||
1363 | while (!list_empty(&list)) { | |
1364 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1365 | list_del(&sums->list); | |
1366 | kfree(sums); | |
1367 | } | |
58113753 LB |
1368 | if (ret < 0) |
1369 | return ret; | |
17d217fe YZ |
1370 | return 1; |
1371 | } | |
1372 | ||
467dc47e FM |
1373 | static int fallback_to_cow(struct inode *inode, struct page *locked_page, |
1374 | const u64 start, const u64 end, | |
1375 | int *page_started, unsigned long *nr_written) | |
1376 | { | |
2166e5ed | 1377 | const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode)); |
6bd335b4 FM |
1378 | const bool is_reloc_ino = (BTRFS_I(inode)->root->root_key.objectid == |
1379 | BTRFS_DATA_RELOC_TREE_OBJECTID); | |
2166e5ed | 1380 | const u64 range_bytes = end + 1 - start; |
467dc47e FM |
1381 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
1382 | u64 range_start = start; | |
1383 | u64 count; | |
1384 | ||
1385 | /* | |
1386 | * If EXTENT_NORESERVE is set it means that when the buffered write was | |
1387 | * made we had not enough available data space and therefore we did not | |
1388 | * reserve data space for it, since we though we could do NOCOW for the | |
1389 | * respective file range (either there is prealloc extent or the inode | |
1390 | * has the NOCOW bit set). | |
1391 | * | |
1392 | * However when we need to fallback to COW mode (because for example the | |
1393 | * block group for the corresponding extent was turned to RO mode by a | |
1394 | * scrub or relocation) we need to do the following: | |
1395 | * | |
1396 | * 1) We increment the bytes_may_use counter of the data space info. | |
1397 | * If COW succeeds, it allocates a new data extent and after doing | |
1398 | * that it decrements the space info's bytes_may_use counter and | |
1399 | * increments its bytes_reserved counter by the same amount (we do | |
1400 | * this at btrfs_add_reserved_bytes()). So we need to increment the | |
1401 | * bytes_may_use counter to compensate (when space is reserved at | |
1402 | * buffered write time, the bytes_may_use counter is incremented); | |
1403 | * | |
1404 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so | |
1405 | * that if the COW path fails for any reason, it decrements (through | |
1406 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the | |
1407 | * data space info, which we incremented in the step above. | |
2166e5ed FM |
1408 | * |
1409 | * If we need to fallback to cow and the inode corresponds to a free | |
6bd335b4 FM |
1410 | * space cache inode or an inode of the data relocation tree, we must |
1411 | * also increment bytes_may_use of the data space_info for the same | |
1412 | * reason. Space caches and relocated data extents always get a prealloc | |
2166e5ed | 1413 | * extent for them, however scrub or balance may have set the block |
6bd335b4 FM |
1414 | * group that contains that extent to RO mode and therefore force COW |
1415 | * when starting writeback. | |
467dc47e | 1416 | */ |
2166e5ed | 1417 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
467dc47e | 1418 | EXTENT_NORESERVE, 0); |
6bd335b4 FM |
1419 | if (count > 0 || is_space_ino || is_reloc_ino) { |
1420 | u64 bytes = count; | |
467dc47e FM |
1421 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
1422 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; | |
1423 | ||
6bd335b4 FM |
1424 | if (is_space_ino || is_reloc_ino) |
1425 | bytes = range_bytes; | |
1426 | ||
467dc47e | 1427 | spin_lock(&sinfo->lock); |
2166e5ed | 1428 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
467dc47e FM |
1429 | spin_unlock(&sinfo->lock); |
1430 | ||
2166e5ed FM |
1431 | if (count > 0) |
1432 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, | |
1433 | 0, 0, NULL); | |
467dc47e FM |
1434 | } |
1435 | ||
6e26c442 NB |
1436 | return cow_file_range(BTRFS_I(inode), locked_page, start, end, |
1437 | page_started, nr_written, 1); | |
467dc47e FM |
1438 | } |
1439 | ||
d352ac68 CM |
1440 | /* |
1441 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1442 | * of the extents that exist in the file, and COWs the file as required. | |
1443 | * | |
1444 | * If no cow copies or snapshots exist, we write directly to the existing | |
1445 | * blocks on disk | |
1446 | */ | |
7f366cfe CM |
1447 | static noinline int run_delalloc_nocow(struct inode *inode, |
1448 | struct page *locked_page, | |
3e024846 NB |
1449 | const u64 start, const u64 end, |
1450 | int *page_started, int force, | |
1451 | unsigned long *nr_written) | |
be20aa9d | 1452 | { |
0b246afa | 1453 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d | 1454 | struct btrfs_root *root = BTRFS_I(inode)->root; |
be20aa9d | 1455 | struct btrfs_path *path; |
3e024846 NB |
1456 | u64 cow_start = (u64)-1; |
1457 | u64 cur_offset = start; | |
8ecebf4d | 1458 | int ret; |
3e024846 NB |
1459 | bool check_prev = true; |
1460 | const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); | |
4a0cc7ca | 1461 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
762bf098 NB |
1462 | bool nocow = false; |
1463 | u64 disk_bytenr = 0; | |
be20aa9d CM |
1464 | |
1465 | path = btrfs_alloc_path(); | |
17ca04af | 1466 | if (!path) { |
ad7ff17b NB |
1467 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
1468 | locked_page, | |
c2790a2e | 1469 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1470 | EXTENT_DO_ACCOUNTING | |
1471 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1472 | PAGE_CLEAR_DIRTY | |
1473 | PAGE_SET_WRITEBACK | | |
1474 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1475 | return -ENOMEM; |
17ca04af | 1476 | } |
82d5902d | 1477 | |
80ff3856 | 1478 | while (1) { |
3e024846 NB |
1479 | struct btrfs_key found_key; |
1480 | struct btrfs_file_extent_item *fi; | |
1481 | struct extent_buffer *leaf; | |
1482 | u64 extent_end; | |
1483 | u64 extent_offset; | |
3e024846 NB |
1484 | u64 num_bytes = 0; |
1485 | u64 disk_num_bytes; | |
3e024846 NB |
1486 | u64 ram_bytes; |
1487 | int extent_type; | |
762bf098 NB |
1488 | |
1489 | nocow = false; | |
3e024846 | 1490 | |
e4c3b2dc | 1491 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1492 | cur_offset, 0); |
d788a349 | 1493 | if (ret < 0) |
79787eaa | 1494 | goto error; |
a6bd9cd1 NB |
1495 | |
1496 | /* | |
1497 | * If there is no extent for our range when doing the initial | |
1498 | * search, then go back to the previous slot as it will be the | |
1499 | * one containing the search offset | |
1500 | */ | |
80ff3856 YZ |
1501 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1502 | leaf = path->nodes[0]; | |
1503 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1504 | path->slots[0] - 1); | |
33345d01 | 1505 | if (found_key.objectid == ino && |
80ff3856 YZ |
1506 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1507 | path->slots[0]--; | |
1508 | } | |
3e024846 | 1509 | check_prev = false; |
80ff3856 | 1510 | next_slot: |
a6bd9cd1 | 1511 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1512 | leaf = path->nodes[0]; |
1513 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1514 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1515 | if (ret < 0) { |
1516 | if (cow_start != (u64)-1) | |
1517 | cur_offset = cow_start; | |
79787eaa | 1518 | goto error; |
e8916699 | 1519 | } |
80ff3856 YZ |
1520 | if (ret > 0) |
1521 | break; | |
1522 | leaf = path->nodes[0]; | |
1523 | } | |
be20aa9d | 1524 | |
80ff3856 YZ |
1525 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1526 | ||
a6bd9cd1 | 1527 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
1528 | if (found_key.objectid > ino) |
1529 | break; | |
a6bd9cd1 NB |
1530 | /* |
1531 | * Keep searching until we find an EXTENT_ITEM or there are no | |
1532 | * more extents for this inode | |
1533 | */ | |
1d512cb7 FM |
1534 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
1535 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1536 | path->slots[0]++; | |
1537 | goto next_slot; | |
1538 | } | |
a6bd9cd1 NB |
1539 | |
1540 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 1541 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
1542 | found_key.offset > end) |
1543 | break; | |
1544 | ||
a6bd9cd1 NB |
1545 | /* |
1546 | * If the found extent starts after requested offset, then | |
1547 | * adjust extent_end to be right before this extent begins | |
1548 | */ | |
80ff3856 YZ |
1549 | if (found_key.offset > cur_offset) { |
1550 | extent_end = found_key.offset; | |
e9061e21 | 1551 | extent_type = 0; |
80ff3856 YZ |
1552 | goto out_check; |
1553 | } | |
1554 | ||
a6bd9cd1 NB |
1555 | /* |
1556 | * Found extent which begins before our range and potentially | |
1557 | * intersect it | |
1558 | */ | |
80ff3856 YZ |
1559 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1560 | struct btrfs_file_extent_item); | |
1561 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1562 | ||
cc95bef6 | 1563 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1564 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1565 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1566 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1567 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1568 | extent_end = found_key.offset + |
1569 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1570 | disk_num_bytes = |
1571 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
a6bd9cd1 | 1572 | /* |
de7999af FM |
1573 | * If the extent we got ends before our current offset, |
1574 | * skip to the next extent. | |
a6bd9cd1 | 1575 | */ |
de7999af | 1576 | if (extent_end <= cur_offset) { |
80ff3856 YZ |
1577 | path->slots[0]++; |
1578 | goto next_slot; | |
1579 | } | |
a6bd9cd1 | 1580 | /* Skip holes */ |
17d217fe YZ |
1581 | if (disk_bytenr == 0) |
1582 | goto out_check; | |
a6bd9cd1 | 1583 | /* Skip compressed/encrypted/encoded extents */ |
80ff3856 YZ |
1584 | if (btrfs_file_extent_compression(leaf, fi) || |
1585 | btrfs_file_extent_encryption(leaf, fi) || | |
1586 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1587 | goto out_check; | |
78d4295b | 1588 | /* |
a6bd9cd1 NB |
1589 | * If extent is created before the last volume's snapshot |
1590 | * this implies the extent is shared, hence we can't do | |
1591 | * nocow. This is the same check as in | |
1592 | * btrfs_cross_ref_exist but without calling | |
1593 | * btrfs_search_slot. | |
78d4295b | 1594 | */ |
3e024846 | 1595 | if (!freespace_inode && |
27a7ff55 | 1596 | btrfs_file_extent_generation(leaf, fi) <= |
78d4295b EL |
1597 | btrfs_root_last_snapshot(&root->root_item)) |
1598 | goto out_check; | |
d899e052 YZ |
1599 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1600 | goto out_check; | |
a6bd9cd1 | 1601 | /* If extent is RO, we must COW it */ |
2ff7e61e | 1602 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1603 | goto out_check; |
58113753 LB |
1604 | ret = btrfs_cross_ref_exist(root, ino, |
1605 | found_key.offset - | |
1606 | extent_offset, disk_bytenr); | |
1607 | if (ret) { | |
1608 | /* | |
1609 | * ret could be -EIO if the above fails to read | |
1610 | * metadata. | |
1611 | */ | |
1612 | if (ret < 0) { | |
1613 | if (cow_start != (u64)-1) | |
1614 | cur_offset = cow_start; | |
1615 | goto error; | |
1616 | } | |
1617 | ||
3e024846 | 1618 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1619 | goto out_check; |
58113753 | 1620 | } |
5d4f98a2 | 1621 | disk_bytenr += extent_offset; |
17d217fe YZ |
1622 | disk_bytenr += cur_offset - found_key.offset; |
1623 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 | 1624 | /* |
a6bd9cd1 NB |
1625 | * If there are pending snapshots for this root, we |
1626 | * fall into common COW way | |
e9894fd3 | 1627 | */ |
3e024846 | 1628 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
8ecebf4d | 1629 | goto out_check; |
17d217fe YZ |
1630 | /* |
1631 | * force cow if csum exists in the range. | |
1632 | * this ensure that csum for a given extent are | |
1633 | * either valid or do not exist. | |
1634 | */ | |
58113753 LB |
1635 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1636 | num_bytes); | |
1637 | if (ret) { | |
58113753 LB |
1638 | /* |
1639 | * ret could be -EIO if the above fails to read | |
1640 | * metadata. | |
1641 | */ | |
1642 | if (ret < 0) { | |
1643 | if (cow_start != (u64)-1) | |
1644 | cur_offset = cow_start; | |
1645 | goto error; | |
1646 | } | |
3e024846 | 1647 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1648 | goto out_check; |
91e1f56a | 1649 | } |
8ecebf4d | 1650 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1651 | goto out_check; |
3e024846 | 1652 | nocow = true; |
80ff3856 | 1653 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e8e21007 NB |
1654 | extent_end = found_key.offset + ram_bytes; |
1655 | extent_end = ALIGN(extent_end, fs_info->sectorsize); | |
922f0518 NB |
1656 | /* Skip extents outside of our requested range */ |
1657 | if (extent_end <= start) { | |
1658 | path->slots[0]++; | |
1659 | goto next_slot; | |
1660 | } | |
80ff3856 | 1661 | } else { |
e8e21007 | 1662 | /* If this triggers then we have a memory corruption */ |
290342f6 | 1663 | BUG(); |
80ff3856 YZ |
1664 | } |
1665 | out_check: | |
a6bd9cd1 NB |
1666 | /* |
1667 | * If nocow is false then record the beginning of the range | |
1668 | * that needs to be COWed | |
1669 | */ | |
80ff3856 YZ |
1670 | if (!nocow) { |
1671 | if (cow_start == (u64)-1) | |
1672 | cow_start = cur_offset; | |
1673 | cur_offset = extent_end; | |
1674 | if (cur_offset > end) | |
1675 | break; | |
1676 | path->slots[0]++; | |
1677 | goto next_slot; | |
7ea394f1 YZ |
1678 | } |
1679 | ||
b3b4aa74 | 1680 | btrfs_release_path(path); |
a6bd9cd1 NB |
1681 | |
1682 | /* | |
1683 | * COW range from cow_start to found_key.offset - 1. As the key | |
1684 | * will contain the beginning of the first extent that can be | |
1685 | * NOCOW, following one which needs to be COW'ed | |
1686 | */ | |
80ff3856 | 1687 | if (cow_start != (u64)-1) { |
467dc47e FM |
1688 | ret = fallback_to_cow(inode, locked_page, cow_start, |
1689 | found_key.offset - 1, | |
1690 | page_started, nr_written); | |
230ed397 | 1691 | if (ret) |
79787eaa | 1692 | goto error; |
80ff3856 | 1693 | cow_start = (u64)-1; |
7ea394f1 | 1694 | } |
80ff3856 | 1695 | |
d899e052 | 1696 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db | 1697 | u64 orig_start = found_key.offset - extent_offset; |
3e024846 | 1698 | struct extent_map *em; |
6f9994db | 1699 | |
4b67c11d | 1700 | em = create_io_em(BTRFS_I(inode), cur_offset, num_bytes, |
6f9994db LB |
1701 | orig_start, |
1702 | disk_bytenr, /* block_start */ | |
1703 | num_bytes, /* block_len */ | |
1704 | disk_num_bytes, /* orig_block_len */ | |
1705 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1706 | BTRFS_ORDERED_PREALLOC); | |
1707 | if (IS_ERR(em)) { | |
6f9994db LB |
1708 | ret = PTR_ERR(em); |
1709 | goto error; | |
d899e052 | 1710 | } |
6f9994db | 1711 | free_extent_map(em); |
e7fbf604 | 1712 | ret = btrfs_add_ordered_extent(BTRFS_I(inode), cur_offset, |
bb55f626 NB |
1713 | disk_bytenr, num_bytes, |
1714 | num_bytes, | |
1715 | BTRFS_ORDERED_PREALLOC); | |
762bf098 NB |
1716 | if (ret) { |
1717 | btrfs_drop_extent_cache(BTRFS_I(inode), | |
1718 | cur_offset, | |
1719 | cur_offset + num_bytes - 1, | |
1720 | 0); | |
1721 | goto error; | |
1722 | } | |
d899e052 | 1723 | } else { |
e7fbf604 | 1724 | ret = btrfs_add_ordered_extent(BTRFS_I(inode), cur_offset, |
bb55f626 NB |
1725 | disk_bytenr, num_bytes, |
1726 | num_bytes, | |
1727 | BTRFS_ORDERED_NOCOW); | |
762bf098 NB |
1728 | if (ret) |
1729 | goto error; | |
d899e052 | 1730 | } |
80ff3856 | 1731 | |
f78c436c | 1732 | if (nocow) |
0b246afa | 1733 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
762bf098 | 1734 | nocow = false; |
771ed689 | 1735 | |
efa56464 | 1736 | if (root->root_key.objectid == |
4dbd80fb QW |
1737 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1738 | /* | |
1739 | * Error handled later, as we must prevent | |
1740 | * extent_clear_unlock_delalloc() in error handler | |
1741 | * from freeing metadata of created ordered extent. | |
1742 | */ | |
7bfa9535 | 1743 | ret = btrfs_reloc_clone_csums(BTRFS_I(inode), cur_offset, |
efa56464 | 1744 | num_bytes); |
efa56464 | 1745 | |
ad7ff17b | 1746 | extent_clear_unlock_delalloc(BTRFS_I(inode), cur_offset, |
74e9194a | 1747 | cur_offset + num_bytes - 1, |
c2790a2e | 1748 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1749 | EXTENT_DELALLOC | |
1750 | EXTENT_CLEAR_DATA_RESV, | |
1751 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1752 | ||
80ff3856 | 1753 | cur_offset = extent_end; |
4dbd80fb QW |
1754 | |
1755 | /* | |
1756 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1757 | * handler, as metadata for created ordered extent will only | |
1758 | * be freed by btrfs_finish_ordered_io(). | |
1759 | */ | |
1760 | if (ret) | |
1761 | goto error; | |
80ff3856 YZ |
1762 | if (cur_offset > end) |
1763 | break; | |
be20aa9d | 1764 | } |
b3b4aa74 | 1765 | btrfs_release_path(path); |
80ff3856 | 1766 | |
506481b2 | 1767 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1768 | cow_start = cur_offset; |
17ca04af | 1769 | |
80ff3856 | 1770 | if (cow_start != (u64)-1) { |
506481b2 | 1771 | cur_offset = end; |
467dc47e FM |
1772 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
1773 | page_started, nr_written); | |
d788a349 | 1774 | if (ret) |
79787eaa | 1775 | goto error; |
80ff3856 YZ |
1776 | } |
1777 | ||
79787eaa | 1778 | error: |
762bf098 NB |
1779 | if (nocow) |
1780 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1781 | ||
17ca04af | 1782 | if (ret && cur_offset < end) |
ad7ff17b | 1783 | extent_clear_unlock_delalloc(BTRFS_I(inode), cur_offset, end, |
c2790a2e | 1784 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1785 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1786 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1787 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1788 | PAGE_SET_WRITEBACK | |
1789 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1790 | btrfs_free_path(path); |
79787eaa | 1791 | return ret; |
be20aa9d CM |
1792 | } |
1793 | ||
47059d93 WS |
1794 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1795 | { | |
1796 | ||
1797 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1798 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1799 | return 0; | |
1800 | ||
1801 | /* | |
1802 | * @defrag_bytes is a hint value, no spinlock held here, | |
1803 | * if is not zero, it means the file is defragging. | |
1804 | * Force cow if given extent needs to be defragged. | |
1805 | */ | |
1806 | if (BTRFS_I(inode)->defrag_bytes && | |
1807 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1808 | EXTENT_DEFRAG, 0, NULL)) | |
1809 | return 1; | |
1810 | ||
1811 | return 0; | |
1812 | } | |
1813 | ||
d352ac68 | 1814 | /* |
5eaad97a NB |
1815 | * Function to process delayed allocation (create CoW) for ranges which are |
1816 | * being touched for the first time. | |
d352ac68 | 1817 | */ |
bc9a8bf7 | 1818 | int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page, |
5eaad97a NB |
1819 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1820 | struct writeback_control *wbc) | |
be20aa9d | 1821 | { |
be20aa9d | 1822 | int ret; |
47059d93 | 1823 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1824 | |
47059d93 | 1825 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1826 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1827 | page_started, 1, nr_written); |
47059d93 | 1828 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1829 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1830 | page_started, 0, nr_written); |
42c16da6 QW |
1831 | } else if (!inode_can_compress(inode) || |
1832 | !inode_need_compress(inode, start, end)) { | |
6e26c442 | 1833 | ret = cow_file_range(BTRFS_I(inode), locked_page, start, end, |
330a5827 | 1834 | page_started, nr_written, 1); |
7ddf5a42 JB |
1835 | } else { |
1836 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1837 | &BTRFS_I(inode)->runtime_flags); | |
ec39f769 | 1838 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
fac07d2b | 1839 | page_started, nr_written); |
7ddf5a42 | 1840 | } |
52427260 | 1841 | if (ret) |
d1051d6e NB |
1842 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
1843 | end - start + 1); | |
b888db2b CM |
1844 | return ret; |
1845 | } | |
1846 | ||
abbb55f4 NB |
1847 | void btrfs_split_delalloc_extent(struct inode *inode, |
1848 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1849 | { |
dcab6a3b JB |
1850 | u64 size; |
1851 | ||
0ca1f7ce | 1852 | /* not delalloc, ignore it */ |
9ed74f2d | 1853 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1854 | return; |
9ed74f2d | 1855 | |
dcab6a3b JB |
1856 | size = orig->end - orig->start + 1; |
1857 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1858 | u32 num_extents; |
dcab6a3b JB |
1859 | u64 new_size; |
1860 | ||
1861 | /* | |
5c848198 | 1862 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 1863 | * applies here, just in reverse. |
dcab6a3b JB |
1864 | */ |
1865 | new_size = orig->end - split + 1; | |
823bb20a | 1866 | num_extents = count_max_extents(new_size); |
ba117213 | 1867 | new_size = split - orig->start; |
823bb20a DS |
1868 | num_extents += count_max_extents(new_size); |
1869 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1870 | return; |
1871 | } | |
1872 | ||
9e0baf60 | 1873 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1874 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1875 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1876 | } |
1877 | ||
1878 | /* | |
5c848198 NB |
1879 | * Handle merged delayed allocation extents so we can keep track of new extents |
1880 | * that are just merged onto old extents, such as when we are doing sequential | |
1881 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 1882 | */ |
5c848198 NB |
1883 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
1884 | struct extent_state *other) | |
9ed74f2d | 1885 | { |
dcab6a3b | 1886 | u64 new_size, old_size; |
823bb20a | 1887 | u32 num_extents; |
dcab6a3b | 1888 | |
9ed74f2d JB |
1889 | /* not delalloc, ignore it */ |
1890 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1891 | return; |
9ed74f2d | 1892 | |
8461a3de JB |
1893 | if (new->start > other->start) |
1894 | new_size = new->end - other->start + 1; | |
1895 | else | |
1896 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1897 | |
1898 | /* we're not bigger than the max, unreserve the space and go */ | |
1899 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1900 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1901 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1902 | spin_unlock(&BTRFS_I(inode)->lock); |
1903 | return; | |
1904 | } | |
1905 | ||
1906 | /* | |
ba117213 JB |
1907 | * We have to add up either side to figure out how many extents were |
1908 | * accounted for before we merged into one big extent. If the number of | |
1909 | * extents we accounted for is <= the amount we need for the new range | |
1910 | * then we can return, otherwise drop. Think of it like this | |
1911 | * | |
1912 | * [ 4k][MAX_SIZE] | |
1913 | * | |
1914 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1915 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1916 | * we have 1 so they are == and we can return. But in this case | |
1917 | * | |
1918 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1919 | * | |
1920 | * Each range on their own accounts for 2 extents, but merged together | |
1921 | * they are only 3 extents worth of accounting, so we need to drop in | |
1922 | * this case. | |
dcab6a3b | 1923 | */ |
ba117213 | 1924 | old_size = other->end - other->start + 1; |
823bb20a | 1925 | num_extents = count_max_extents(old_size); |
ba117213 | 1926 | old_size = new->end - new->start + 1; |
823bb20a DS |
1927 | num_extents += count_max_extents(old_size); |
1928 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1929 | return; |
1930 | ||
9e0baf60 | 1931 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1932 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1933 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1934 | } |
1935 | ||
eb73c1b7 MX |
1936 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1937 | struct inode *inode) | |
1938 | { | |
0b246afa JM |
1939 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1940 | ||
eb73c1b7 MX |
1941 | spin_lock(&root->delalloc_lock); |
1942 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1943 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1944 | &root->delalloc_inodes); | |
1945 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1946 | &BTRFS_I(inode)->runtime_flags); | |
1947 | root->nr_delalloc_inodes++; | |
1948 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1949 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1950 | BUG_ON(!list_empty(&root->delalloc_root)); |
1951 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1952 | &fs_info->delalloc_roots); |
1953 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1954 | } |
1955 | } | |
1956 | spin_unlock(&root->delalloc_lock); | |
1957 | } | |
1958 | ||
2b877331 NB |
1959 | |
1960 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1961 | struct btrfs_inode *inode) | |
eb73c1b7 | 1962 | { |
3ffbd68c | 1963 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 1964 | |
9e3e97f4 NB |
1965 | if (!list_empty(&inode->delalloc_inodes)) { |
1966 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1967 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1968 | &inode->runtime_flags); |
eb73c1b7 MX |
1969 | root->nr_delalloc_inodes--; |
1970 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 1971 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 1972 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1973 | BUG_ON(list_empty(&root->delalloc_root)); |
1974 | list_del_init(&root->delalloc_root); | |
0b246afa | 1975 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1976 | } |
1977 | } | |
2b877331 NB |
1978 | } |
1979 | ||
1980 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1981 | struct btrfs_inode *inode) | |
1982 | { | |
1983 | spin_lock(&root->delalloc_lock); | |
1984 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
1985 | spin_unlock(&root->delalloc_lock); |
1986 | } | |
1987 | ||
d352ac68 | 1988 | /* |
e06a1fc9 NB |
1989 | * Properly track delayed allocation bytes in the inode and to maintain the |
1990 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 1991 | */ |
e06a1fc9 NB |
1992 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
1993 | unsigned *bits) | |
291d673e | 1994 | { |
0b246afa JM |
1995 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1996 | ||
47059d93 WS |
1997 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1998 | WARN_ON(1); | |
75eff68e CM |
1999 | /* |
2000 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2001 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2002 | * bit, which is only set or cleared with irqs on |
2003 | */ | |
0ca1f7ce | 2004 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 2005 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2006 | u64 len = state->end + 1 - state->start; |
8b62f87b | 2007 | u32 num_extents = count_max_extents(len); |
70ddc553 | 2008 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 2009 | |
8b62f87b JB |
2010 | spin_lock(&BTRFS_I(inode)->lock); |
2011 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
2012 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 2013 | |
6a3891c5 | 2014 | /* For sanity tests */ |
0b246afa | 2015 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2016 | return; |
2017 | ||
104b4e51 NB |
2018 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
2019 | fs_info->delalloc_batch); | |
df0af1a5 | 2020 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 2021 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
2022 | if (*bits & EXTENT_DEFRAG) |
2023 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 2024 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
2025 | &BTRFS_I(inode)->runtime_flags)) |
2026 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 2027 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 2028 | } |
a7e3b975 FM |
2029 | |
2030 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
2031 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2032 | spin_lock(&BTRFS_I(inode)->lock); | |
2033 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
2034 | state->start; | |
2035 | spin_unlock(&BTRFS_I(inode)->lock); | |
2036 | } | |
291d673e CM |
2037 | } |
2038 | ||
d352ac68 | 2039 | /* |
a36bb5f9 NB |
2040 | * Once a range is no longer delalloc this function ensures that proper |
2041 | * accounting happens. | |
d352ac68 | 2042 | */ |
a36bb5f9 NB |
2043 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
2044 | struct extent_state *state, unsigned *bits) | |
291d673e | 2045 | { |
a36bb5f9 NB |
2046 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
2047 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 2048 | u64 len = state->end + 1 - state->start; |
823bb20a | 2049 | u32 num_extents = count_max_extents(len); |
47059d93 | 2050 | |
4a4b964f FM |
2051 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
2052 | spin_lock(&inode->lock); | |
6fc0ef68 | 2053 | inode->defrag_bytes -= len; |
4a4b964f FM |
2054 | spin_unlock(&inode->lock); |
2055 | } | |
47059d93 | 2056 | |
75eff68e CM |
2057 | /* |
2058 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 2059 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
2060 | * bit, which is only set or cleared with irqs on |
2061 | */ | |
0ca1f7ce | 2062 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 2063 | struct btrfs_root *root = inode->root; |
83eea1f1 | 2064 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 2065 | |
8b62f87b JB |
2066 | spin_lock(&inode->lock); |
2067 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
2068 | spin_unlock(&inode->lock); | |
0ca1f7ce | 2069 | |
b6d08f06 JB |
2070 | /* |
2071 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 2072 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
2073 | * error. |
2074 | */ | |
a315e68f | 2075 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 2076 | root != fs_info->tree_root) |
43b18595 | 2077 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 2078 | |
6a3891c5 | 2079 | /* For sanity tests. */ |
0b246afa | 2080 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
2081 | return; |
2082 | ||
a315e68f FM |
2083 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
2084 | do_list && !(state->state & EXTENT_NORESERVE) && | |
2085 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
2086 | btrfs_free_reserved_data_space_noquota( |
2087 | &inode->vfs_inode, | |
46d4dac8 | 2088 | len); |
9ed74f2d | 2089 | |
104b4e51 NB |
2090 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2091 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2092 | spin_lock(&inode->lock); |
2093 | inode->delalloc_bytes -= len; | |
2094 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2095 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2096 | &inode->runtime_flags)) |
eb73c1b7 | 2097 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2098 | spin_unlock(&inode->lock); |
291d673e | 2099 | } |
a7e3b975 FM |
2100 | |
2101 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
2102 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2103 | spin_lock(&inode->lock); | |
2104 | ASSERT(inode->new_delalloc_bytes >= len); | |
2105 | inode->new_delalloc_bytes -= len; | |
2106 | spin_unlock(&inode->lock); | |
2107 | } | |
291d673e CM |
2108 | } |
2109 | ||
d352ac68 | 2110 | /* |
da12fe54 NB |
2111 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
2112 | * in a chunk's stripe. This function ensures that bios do not span a | |
2113 | * stripe/chunk | |
6f034ece | 2114 | * |
da12fe54 NB |
2115 | * @page - The page we are about to add to the bio |
2116 | * @size - size we want to add to the bio | |
2117 | * @bio - bio we want to ensure is smaller than a stripe | |
2118 | * @bio_flags - flags of the bio | |
2119 | * | |
2120 | * return 1 if page cannot be added to the bio | |
2121 | * return 0 if page can be added to the bio | |
6f034ece | 2122 | * return error otherwise |
d352ac68 | 2123 | */ |
da12fe54 NB |
2124 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
2125 | unsigned long bio_flags) | |
239b14b3 | 2126 | { |
0b246afa JM |
2127 | struct inode *inode = page->mapping->host; |
2128 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 2129 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
2130 | u64 length = 0; |
2131 | u64 map_length; | |
239b14b3 | 2132 | int ret; |
89b798ad | 2133 | struct btrfs_io_geometry geom; |
239b14b3 | 2134 | |
771ed689 CM |
2135 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
2136 | return 0; | |
2137 | ||
4f024f37 | 2138 | length = bio->bi_iter.bi_size; |
239b14b3 | 2139 | map_length = length; |
89b798ad NB |
2140 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
2141 | &geom); | |
6f034ece LB |
2142 | if (ret < 0) |
2143 | return ret; | |
89b798ad NB |
2144 | |
2145 | if (geom.len < length + size) | |
239b14b3 | 2146 | return 1; |
3444a972 | 2147 | return 0; |
239b14b3 CM |
2148 | } |
2149 | ||
d352ac68 CM |
2150 | /* |
2151 | * in order to insert checksums into the metadata in large chunks, | |
2152 | * we wait until bio submission time. All the pages in the bio are | |
2153 | * checksummed and sums are attached onto the ordered extent record. | |
2154 | * | |
2155 | * At IO completion time the cums attached on the ordered extent record | |
2156 | * are inserted into the btree | |
2157 | */ | |
d0ee3934 | 2158 | static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, |
eaf25d93 | 2159 | u64 bio_offset) |
065631f6 | 2160 | { |
c6100a4b | 2161 | struct inode *inode = private_data; |
4e4cbee9 | 2162 | blk_status_t ret = 0; |
e015640f | 2163 | |
bd242a08 | 2164 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
79787eaa | 2165 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
2166 | return 0; |
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 | ||
2ac55d41 | 2266 | int btrfs_set_extent_delalloc(struct 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)); |
ea8c2819 | 2271 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
e3b8a485 | 2272 | extra_bits, 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 CM |
2288 | struct page *page; |
2289 | struct inode *inode; | |
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; | |
f4b1363c JB |
2297 | inode = fixup->inode; |
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 | */ | |
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) { | |
2334 | btrfs_delalloc_release_extents(BTRFS_I(inode), | |
2335 | PAGE_SIZE); | |
2336 | btrfs_delalloc_release_space(inode, data_reserved, | |
2337 | page_start, PAGE_SIZE, | |
2338 | true); | |
2339 | } | |
2340 | ret = 0; | |
247e743c | 2341 | goto out_page; |
f4b1363c | 2342 | } |
247e743c | 2343 | |
25f3c502 | 2344 | /* |
f4b1363c JB |
2345 | * We can't mess with the page state unless it is locked, so now that |
2346 | * it is locked bail if we failed to make our space reservation. | |
25f3c502 | 2347 | */ |
f4b1363c JB |
2348 | if (ret) |
2349 | goto out_page; | |
247e743c | 2350 | |
ff13db41 | 2351 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2352 | &cached_state); |
4a096752 CM |
2353 | |
2354 | /* already ordered? We're done */ | |
8b62b72b | 2355 | if (PagePrivate2(page)) |
f4b1363c | 2356 | goto out_reserved; |
4a096752 | 2357 | |
a776c6fa | 2358 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2359 | PAGE_SIZE); |
4a096752 | 2360 | if (ordered) { |
2ac55d41 | 2361 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
e43bbe5e | 2362 | page_end, &cached_state); |
4a096752 CM |
2363 | unlock_page(page); |
2364 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2365 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2366 | goto again; |
2367 | } | |
247e743c | 2368 | |
f3038ee3 | 2369 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2370 | &cached_state); |
25f3c502 | 2371 | if (ret) |
53687007 | 2372 | goto out_reserved; |
f3038ee3 | 2373 | |
25f3c502 CM |
2374 | /* |
2375 | * Everything went as planned, we're now the owner of a dirty page with | |
2376 | * delayed allocation bits set and space reserved for our COW | |
2377 | * destination. | |
2378 | * | |
2379 | * The page was dirty when we started, nothing should have cleaned it. | |
2380 | */ | |
2381 | BUG_ON(!PageDirty(page)); | |
f4b1363c | 2382 | free_delalloc_space = false; |
53687007 | 2383 | out_reserved: |
8702ba93 | 2384 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
f4b1363c | 2385 | if (free_delalloc_space) |
53687007 FM |
2386 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
2387 | PAGE_SIZE, true); | |
2ac55d41 | 2388 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
e43bbe5e | 2389 | &cached_state); |
247e743c | 2390 | out_page: |
25f3c502 CM |
2391 | if (ret) { |
2392 | /* | |
2393 | * We hit ENOSPC or other errors. Update the mapping and page | |
2394 | * to reflect the errors and clean the page. | |
2395 | */ | |
2396 | mapping_set_error(page->mapping, ret); | |
2397 | end_extent_writepage(page, ret, page_start, page_end); | |
2398 | clear_page_dirty_for_io(page); | |
2399 | SetPageError(page); | |
2400 | } | |
2401 | ClearPageChecked(page); | |
247e743c | 2402 | unlock_page(page); |
09cbfeaf | 2403 | put_page(page); |
b897abec | 2404 | kfree(fixup); |
364ecf36 | 2405 | extent_changeset_free(data_reserved); |
f4b1363c JB |
2406 | /* |
2407 | * As a precaution, do a delayed iput in case it would be the last iput | |
2408 | * that could need flushing space. Recursing back to fixup worker would | |
2409 | * deadlock. | |
2410 | */ | |
2411 | btrfs_add_delayed_iput(inode); | |
247e743c CM |
2412 | } |
2413 | ||
2414 | /* | |
2415 | * There are a few paths in the higher layers of the kernel that directly | |
2416 | * set the page dirty bit without asking the filesystem if it is a | |
2417 | * good idea. This causes problems because we want to make sure COW | |
2418 | * properly happens and the data=ordered rules are followed. | |
2419 | * | |
c8b97818 | 2420 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2421 | * hasn't been properly setup for IO. We kick off an async process |
2422 | * to fix it up. The async helper will wait for ordered extents, set | |
2423 | * the delalloc bit and make it safe to write the page. | |
2424 | */ | |
d75855b4 | 2425 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
247e743c CM |
2426 | { |
2427 | struct inode *inode = page->mapping->host; | |
0b246afa | 2428 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2429 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2430 | |
8b62b72b CM |
2431 | /* this page is properly in the ordered list */ |
2432 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2433 | return 0; |
2434 | ||
25f3c502 CM |
2435 | /* |
2436 | * PageChecked is set below when we create a fixup worker for this page, | |
2437 | * don't try to create another one if we're already PageChecked() | |
2438 | * | |
2439 | * The extent_io writepage code will redirty the page if we send back | |
2440 | * EAGAIN. | |
2441 | */ | |
247e743c CM |
2442 | if (PageChecked(page)) |
2443 | return -EAGAIN; | |
2444 | ||
2445 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2446 | if (!fixup) | |
2447 | return -EAGAIN; | |
f421950f | 2448 | |
f4b1363c JB |
2449 | /* |
2450 | * We are already holding a reference to this inode from | |
2451 | * write_cache_pages. We need to hold it because the space reservation | |
2452 | * takes place outside of the page lock, and we can't trust | |
2453 | * page->mapping outside of the page lock. | |
2454 | */ | |
2455 | ihold(inode); | |
247e743c | 2456 | SetPageChecked(page); |
09cbfeaf | 2457 | get_page(page); |
a0cac0ec | 2458 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 2459 | fixup->page = page; |
f4b1363c | 2460 | fixup->inode = inode; |
0b246afa | 2461 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
25f3c502 CM |
2462 | |
2463 | return -EAGAIN; | |
247e743c CM |
2464 | } |
2465 | ||
d899e052 YZ |
2466 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2467 | struct inode *inode, u64 file_pos, | |
9729f10a QW |
2468 | struct btrfs_file_extent_item *stack_fi, |
2469 | u64 qgroup_reserved) | |
d899e052 YZ |
2470 | { |
2471 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d899e052 YZ |
2472 | struct btrfs_path *path; |
2473 | struct extent_buffer *leaf; | |
2474 | struct btrfs_key ins; | |
203f44c5 QW |
2475 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
2476 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); | |
2477 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); | |
2478 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); | |
1acae57b | 2479 | int extent_inserted = 0; |
d899e052 YZ |
2480 | int ret; |
2481 | ||
2482 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2483 | if (!path) |
2484 | return -ENOMEM; | |
d899e052 | 2485 | |
a1ed835e CM |
2486 | /* |
2487 | * we may be replacing one extent in the tree with another. | |
2488 | * The new extent is pinned in the extent map, and we don't want | |
2489 | * to drop it from the cache until it is completely in the btree. | |
2490 | * | |
2491 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2492 | * the caller is expected to unpin it and allow it to be merged | |
2493 | * with the others. | |
2494 | */ | |
906c448c | 2495 | ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path, file_pos, |
1acae57b | 2496 | file_pos + num_bytes, NULL, 0, |
203f44c5 | 2497 | 1, sizeof(*stack_fi), &extent_inserted); |
79787eaa JM |
2498 | if (ret) |
2499 | goto out; | |
d899e052 | 2500 | |
1acae57b | 2501 | if (!extent_inserted) { |
4a0cc7ca | 2502 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2503 | ins.offset = file_pos; |
2504 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2505 | ||
2506 | path->leave_spinning = 1; | |
2507 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
203f44c5 | 2508 | sizeof(*stack_fi)); |
1acae57b FDBM |
2509 | if (ret) |
2510 | goto out; | |
2511 | } | |
d899e052 | 2512 | leaf = path->nodes[0]; |
203f44c5 QW |
2513 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
2514 | write_extent_buffer(leaf, stack_fi, | |
2515 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2516 | sizeof(struct btrfs_file_extent_item)); | |
b9473439 | 2517 | |
d899e052 | 2518 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2519 | btrfs_release_path(path); |
d899e052 YZ |
2520 | |
2521 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2522 | |
2523 | ins.objectid = disk_bytenr; | |
2524 | ins.offset = disk_num_bytes; | |
2525 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2526 | |
9ddc959e JB |
2527 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos, |
2528 | ram_bytes); | |
2529 | if (ret) | |
2530 | goto out; | |
2531 | ||
84f7d8e6 JB |
2532 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2533 | btrfs_ino(BTRFS_I(inode)), | |
9729f10a | 2534 | file_pos, qgroup_reserved, &ins); |
79787eaa | 2535 | out: |
d899e052 | 2536 | btrfs_free_path(path); |
b9473439 | 2537 | |
79787eaa | 2538 | return ret; |
d899e052 YZ |
2539 | } |
2540 | ||
2ff7e61e | 2541 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2542 | u64 start, u64 len) |
2543 | { | |
32da5386 | 2544 | struct btrfs_block_group *cache; |
e570fd27 | 2545 | |
0b246afa | 2546 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2547 | ASSERT(cache); |
2548 | ||
2549 | spin_lock(&cache->lock); | |
2550 | cache->delalloc_bytes -= len; | |
2551 | spin_unlock(&cache->lock); | |
2552 | ||
2553 | btrfs_put_block_group(cache); | |
2554 | } | |
2555 | ||
203f44c5 QW |
2556 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
2557 | struct inode *inode, | |
2558 | struct btrfs_ordered_extent *oe) | |
2559 | { | |
2560 | struct btrfs_file_extent_item stack_fi; | |
2561 | u64 logical_len; | |
2562 | ||
2563 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
2564 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); | |
2565 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); | |
2566 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, | |
2567 | oe->disk_num_bytes); | |
2568 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) | |
2569 | logical_len = oe->truncated_len; | |
2570 | else | |
2571 | logical_len = oe->num_bytes; | |
2572 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); | |
2573 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); | |
2574 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); | |
2575 | /* Encryption and other encoding is reserved and all 0 */ | |
2576 | ||
2577 | return insert_reserved_file_extent(trans, inode, oe->file_offset, | |
7dbeaad0 | 2578 | &stack_fi, oe->qgroup_rsv); |
203f44c5 QW |
2579 | } |
2580 | ||
2581 | /* | |
2582 | * As ordered data IO finishes, this gets called so we can finish | |
d352ac68 CM |
2583 | * an ordered extent if the range of bytes in the file it covers are |
2584 | * fully written. | |
2585 | */ | |
5fd02043 | 2586 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2587 | { |
5fd02043 | 2588 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2589 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2590 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2591 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2592 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2593 | struct extent_state *cached_state = NULL; |
bffe633e | 2594 | u64 start, end; |
261507a0 | 2595 | int compress_type = 0; |
77cef2ec | 2596 | int ret = 0; |
bffe633e | 2597 | u64 logical_len = ordered_extent->num_bytes; |
8d510121 | 2598 | bool freespace_inode; |
77cef2ec | 2599 | bool truncated = false; |
a7e3b975 FM |
2600 | bool range_locked = false; |
2601 | bool clear_new_delalloc_bytes = false; | |
49940bdd | 2602 | bool clear_reserved_extent = true; |
313facc5 | 2603 | unsigned int clear_bits; |
a7e3b975 | 2604 | |
bffe633e OS |
2605 | start = ordered_extent->file_offset; |
2606 | end = start + ordered_extent->num_bytes - 1; | |
2607 | ||
a7e3b975 FM |
2608 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
2609 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2610 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2611 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2612 | |
8d510121 | 2613 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2614 | |
5fd02043 JB |
2615 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2616 | ret = -EIO; | |
2617 | goto out; | |
2618 | } | |
2619 | ||
bffe633e | 2620 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
f612496b | 2621 | |
77cef2ec JB |
2622 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2623 | truncated = true; | |
2624 | logical_len = ordered_extent->truncated_len; | |
2625 | /* Truncated the entire extent, don't bother adding */ | |
2626 | if (!logical_len) | |
2627 | goto out; | |
2628 | } | |
2629 | ||
c2167754 | 2630 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2631 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a | 2632 | |
d923afe9 | 2633 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
8d510121 NB |
2634 | if (freespace_inode) |
2635 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
2636 | else |
2637 | trans = btrfs_join_transaction(root); | |
2638 | if (IS_ERR(trans)) { | |
2639 | ret = PTR_ERR(trans); | |
2640 | trans = NULL; | |
2641 | goto out; | |
c2167754 | 2642 | } |
69fe2d75 | 2643 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
2644 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2645 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2646 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2647 | goto out; |
2648 | } | |
e6dcd2dc | 2649 | |
a7e3b975 | 2650 | range_locked = true; |
bffe633e | 2651 | lock_extent_bits(io_tree, start, end, &cached_state); |
e6dcd2dc | 2652 | |
8d510121 NB |
2653 | if (freespace_inode) |
2654 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 2655 | else |
7a7eaa40 | 2656 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2657 | if (IS_ERR(trans)) { |
2658 | ret = PTR_ERR(trans); | |
2659 | trans = NULL; | |
a7e3b975 | 2660 | goto out; |
79787eaa | 2661 | } |
a79b7d4b | 2662 | |
69fe2d75 | 2663 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 2664 | |
c8b97818 | 2665 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2666 | compress_type = ordered_extent->compress_type; |
d899e052 | 2667 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2668 | BUG_ON(compress_type); |
7a6d7067 | 2669 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2670 | ordered_extent->file_offset, |
2671 | ordered_extent->file_offset + | |
77cef2ec | 2672 | logical_len); |
d899e052 | 2673 | } else { |
0b246afa | 2674 | BUG_ON(root == fs_info->tree_root); |
203f44c5 QW |
2675 | ret = insert_ordered_extent_file_extent(trans, inode, |
2676 | ordered_extent); | |
49940bdd JB |
2677 | if (!ret) { |
2678 | clear_reserved_extent = false; | |
2ff7e61e | 2679 | btrfs_release_delalloc_bytes(fs_info, |
bffe633e OS |
2680 | ordered_extent->disk_bytenr, |
2681 | ordered_extent->disk_num_bytes); | |
49940bdd | 2682 | } |
d899e052 | 2683 | } |
5dc562c5 | 2684 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
bffe633e OS |
2685 | ordered_extent->file_offset, |
2686 | ordered_extent->num_bytes, trans->transid); | |
79787eaa | 2687 | if (ret < 0) { |
66642832 | 2688 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2689 | goto out; |
79787eaa | 2690 | } |
2ac55d41 | 2691 | |
ac01f26a NB |
2692 | ret = add_pending_csums(trans, inode, &ordered_extent->list); |
2693 | if (ret) { | |
2694 | btrfs_abort_transaction(trans, ret); | |
2695 | goto out; | |
2696 | } | |
e6dcd2dc | 2697 | |
d923afe9 | 2698 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
6c760c07 JB |
2699 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2700 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 2701 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 2702 | goto out; |
1ef30be1 JB |
2703 | } |
2704 | ret = 0; | |
c2167754 | 2705 | out: |
313facc5 OS |
2706 | clear_bits = EXTENT_DEFRAG; |
2707 | if (range_locked) | |
2708 | clear_bits |= EXTENT_LOCKED; | |
2709 | if (clear_new_delalloc_bytes) | |
2710 | clear_bits |= EXTENT_DELALLOC_NEW; | |
bffe633e OS |
2711 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
2712 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, | |
313facc5 | 2713 | &cached_state); |
a7e3b975 | 2714 | |
a698d075 | 2715 | if (trans) |
3a45bb20 | 2716 | btrfs_end_transaction(trans); |
0cb59c99 | 2717 | |
77cef2ec | 2718 | if (ret || truncated) { |
bffe633e | 2719 | u64 unwritten_start = start; |
77cef2ec JB |
2720 | |
2721 | if (truncated) | |
bffe633e OS |
2722 | unwritten_start += logical_len; |
2723 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); | |
77cef2ec JB |
2724 | |
2725 | /* Drop the cache for the part of the extent we didn't write. */ | |
bffe633e | 2726 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
5fd02043 | 2727 | |
0bec9ef5 JB |
2728 | /* |
2729 | * If the ordered extent had an IOERR or something else went | |
2730 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2731 | * back to the allocator. We only free the extent in the |
2732 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
2733 | * |
2734 | * If we made it past insert_reserved_file_extent before we | |
2735 | * errored out then we don't need to do this as the accounting | |
2736 | * has already been done. | |
0bec9ef5 | 2737 | */ |
77cef2ec | 2738 | if ((ret || !logical_len) && |
49940bdd | 2739 | clear_reserved_extent && |
77cef2ec | 2740 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
4eaaec24 NB |
2741 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
2742 | /* | |
2743 | * Discard the range before returning it back to the | |
2744 | * free space pool | |
2745 | */ | |
46b27f50 | 2746 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
4eaaec24 | 2747 | btrfs_discard_extent(fs_info, |
bffe633e OS |
2748 | ordered_extent->disk_bytenr, |
2749 | ordered_extent->disk_num_bytes, | |
2750 | NULL); | |
2ff7e61e | 2751 | btrfs_free_reserved_extent(fs_info, |
bffe633e OS |
2752 | ordered_extent->disk_bytenr, |
2753 | ordered_extent->disk_num_bytes, 1); | |
4eaaec24 | 2754 | } |
0bec9ef5 JB |
2755 | } |
2756 | ||
5fd02043 | 2757 | /* |
8bad3c02 LB |
2758 | * This needs to be done to make sure anybody waiting knows we are done |
2759 | * updating everything for this ordered extent. | |
5fd02043 JB |
2760 | */ |
2761 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2762 | ||
e6dcd2dc CM |
2763 | /* once for us */ |
2764 | btrfs_put_ordered_extent(ordered_extent); | |
2765 | /* once for the tree */ | |
2766 | btrfs_put_ordered_extent(ordered_extent); | |
2767 | ||
5fd02043 JB |
2768 | return ret; |
2769 | } | |
2770 | ||
2771 | static void finish_ordered_fn(struct btrfs_work *work) | |
2772 | { | |
2773 | struct btrfs_ordered_extent *ordered_extent; | |
2774 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2775 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2776 | } |
2777 | ||
c629732d NB |
2778 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
2779 | u64 end, int uptodate) | |
211f90e6 | 2780 | { |
5fd02043 | 2781 | struct inode *inode = page->mapping->host; |
0b246afa | 2782 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 2783 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 | 2784 | struct btrfs_workqueue *wq; |
5fd02043 | 2785 | |
1abe9b8a | 2786 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2787 | ||
8b62b72b | 2788 | ClearPagePrivate2(page); |
5fd02043 JB |
2789 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2790 | end - start + 1, uptodate)) | |
c3988d63 | 2791 | return; |
5fd02043 | 2792 | |
a0cac0ec | 2793 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0b246afa | 2794 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 2795 | else |
0b246afa | 2796 | wq = fs_info->endio_write_workers; |
5fd02043 | 2797 | |
a0cac0ec | 2798 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
9e0af237 | 2799 | btrfs_queue_work(wq, &ordered_extent->work); |
211f90e6 CM |
2800 | } |
2801 | ||
47df7765 OS |
2802 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
2803 | int icsum, struct page *page, int pgoff, u64 start, | |
2804 | size_t len) | |
dc380aea | 2805 | { |
d5178578 JT |
2806 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2807 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
dc380aea | 2808 | char *kaddr; |
d5178578 JT |
2809 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
2810 | u8 *csum_expected; | |
2811 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 2812 | |
d5178578 | 2813 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
dc380aea MX |
2814 | |
2815 | kaddr = kmap_atomic(page); | |
d5178578 JT |
2816 | shash->tfm = fs_info->csum_shash; |
2817 | ||
fd08001f | 2818 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
d5178578 JT |
2819 | |
2820 | if (memcmp(csum, csum_expected, csum_size)) | |
dc380aea MX |
2821 | goto zeroit; |
2822 | ||
2823 | kunmap_atomic(kaddr); | |
2824 | return 0; | |
2825 | zeroit: | |
ea41d6b2 JT |
2826 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
2827 | io_bio->mirror_num); | |
dc380aea MX |
2828 | memset(kaddr + pgoff, 1, len); |
2829 | flush_dcache_page(page); | |
2830 | kunmap_atomic(kaddr); | |
dc380aea MX |
2831 | return -EIO; |
2832 | } | |
2833 | ||
d352ac68 CM |
2834 | /* |
2835 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
2836 | * if there's a match, we allow the bio to finish. If not, the code in |
2837 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 2838 | */ |
facc8a22 MX |
2839 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
2840 | u64 phy_offset, struct page *page, | |
2841 | u64 start, u64 end, int mirror) | |
07157aac | 2842 | { |
4eee4fa4 | 2843 | size_t offset = start - page_offset(page); |
07157aac | 2844 | struct inode *inode = page->mapping->host; |
d1310b2e | 2845 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 2846 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 2847 | |
d20f7043 CM |
2848 | if (PageChecked(page)) { |
2849 | ClearPageChecked(page); | |
dc380aea | 2850 | return 0; |
d20f7043 | 2851 | } |
6cbff00f CH |
2852 | |
2853 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 2854 | return 0; |
17d217fe YZ |
2855 | |
2856 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 2857 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 2858 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 2859 | return 0; |
17d217fe | 2860 | } |
d20f7043 | 2861 | |
facc8a22 | 2862 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
47df7765 OS |
2863 | return check_data_csum(inode, io_bio, phy_offset, page, offset, start, |
2864 | (size_t)(end - start + 1)); | |
07157aac | 2865 | } |
b888db2b | 2866 | |
c1c3fac2 NB |
2867 | /* |
2868 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
2869 | * | |
2870 | * @inode: The inode we want to perform iput on | |
2871 | * | |
2872 | * This function uses the generic vfs_inode::i_count to track whether we should | |
2873 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
2874 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
2875 | * transaction commit time/superblock commit/cleaner kthread. | |
2876 | */ | |
24bbcf04 YZ |
2877 | void btrfs_add_delayed_iput(struct inode *inode) |
2878 | { | |
0b246afa | 2879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 2880 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
2881 | |
2882 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
2883 | return; | |
2884 | ||
034f784d | 2885 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 2886 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
2887 | ASSERT(list_empty(&binode->delayed_iput)); |
2888 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 2889 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
2890 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
2891 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
2892 | } |
2893 | ||
63611e73 JB |
2894 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
2895 | struct btrfs_inode *inode) | |
2896 | { | |
2897 | list_del_init(&inode->delayed_iput); | |
2898 | spin_unlock(&fs_info->delayed_iput_lock); | |
2899 | iput(&inode->vfs_inode); | |
2900 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
2901 | wake_up(&fs_info->delayed_iputs_wait); | |
2902 | spin_lock(&fs_info->delayed_iput_lock); | |
2903 | } | |
2904 | ||
2905 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
2906 | struct btrfs_inode *inode) | |
2907 | { | |
2908 | if (!list_empty(&inode->delayed_iput)) { | |
2909 | spin_lock(&fs_info->delayed_iput_lock); | |
2910 | if (!list_empty(&inode->delayed_iput)) | |
2911 | run_delayed_iput_locked(fs_info, inode); | |
2912 | spin_unlock(&fs_info->delayed_iput_lock); | |
2913 | } | |
2914 | } | |
2915 | ||
2ff7e61e | 2916 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 2917 | { |
24bbcf04 | 2918 | |
24bbcf04 | 2919 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
2920 | while (!list_empty(&fs_info->delayed_iputs)) { |
2921 | struct btrfs_inode *inode; | |
2922 | ||
2923 | inode = list_first_entry(&fs_info->delayed_iputs, | |
2924 | struct btrfs_inode, delayed_iput); | |
63611e73 | 2925 | run_delayed_iput_locked(fs_info, inode); |
24bbcf04 | 2926 | } |
8089fe62 | 2927 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
2928 | } |
2929 | ||
034f784d JB |
2930 | /** |
2931 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running | |
2932 | * @fs_info - the fs_info for this fs | |
2933 | * @return - EINTR if we were killed, 0 if nothing's pending | |
2934 | * | |
2935 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
2936 | * set. Once they are all done running we will return, unless we are killed in | |
2937 | * which case we return EINTR. This helps in user operations like fallocate etc | |
2938 | * that might get blocked on the iputs. | |
2939 | */ | |
2940 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
2941 | { | |
2942 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
2943 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
2944 | if (ret) | |
2945 | return -EINTR; | |
2946 | return 0; | |
2947 | } | |
2948 | ||
7b128766 | 2949 | /* |
f7e9e8fc OS |
2950 | * This creates an orphan entry for the given inode in case something goes wrong |
2951 | * in the middle of an unlink. | |
7b128766 | 2952 | */ |
73f2e545 | 2953 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 2954 | struct btrfs_inode *inode) |
7b128766 | 2955 | { |
d68fc57b | 2956 | int ret; |
7b128766 | 2957 | |
27919067 OS |
2958 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
2959 | if (ret && ret != -EEXIST) { | |
2960 | btrfs_abort_transaction(trans, ret); | |
2961 | return ret; | |
d68fc57b YZ |
2962 | } |
2963 | ||
d68fc57b | 2964 | return 0; |
7b128766 JB |
2965 | } |
2966 | ||
2967 | /* | |
f7e9e8fc OS |
2968 | * We have done the delete so we can go ahead and remove the orphan item for |
2969 | * this particular inode. | |
7b128766 | 2970 | */ |
48a3b636 | 2971 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 2972 | struct btrfs_inode *inode) |
7b128766 | 2973 | { |
27919067 | 2974 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
2975 | } |
2976 | ||
2977 | /* | |
2978 | * this cleans up any orphans that may be left on the list from the last use | |
2979 | * of this root. | |
2980 | */ | |
66b4ffd1 | 2981 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 2982 | { |
0b246afa | 2983 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
2984 | struct btrfs_path *path; |
2985 | struct extent_buffer *leaf; | |
7b128766 JB |
2986 | struct btrfs_key key, found_key; |
2987 | struct btrfs_trans_handle *trans; | |
2988 | struct inode *inode; | |
8f6d7f4f | 2989 | u64 last_objectid = 0; |
f7e9e8fc | 2990 | int ret = 0, nr_unlink = 0; |
7b128766 | 2991 | |
d68fc57b | 2992 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 2993 | return 0; |
c71bf099 YZ |
2994 | |
2995 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
2996 | if (!path) { |
2997 | ret = -ENOMEM; | |
2998 | goto out; | |
2999 | } | |
e4058b54 | 3000 | path->reada = READA_BACK; |
7b128766 JB |
3001 | |
3002 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3003 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3004 | key.offset = (u64)-1; |
3005 | ||
7b128766 JB |
3006 | while (1) { |
3007 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3008 | if (ret < 0) |
3009 | goto out; | |
7b128766 JB |
3010 | |
3011 | /* | |
3012 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3013 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3014 | * find the key and see if we have stuff that matches |
3015 | */ | |
3016 | if (ret > 0) { | |
66b4ffd1 | 3017 | ret = 0; |
7b128766 JB |
3018 | if (path->slots[0] == 0) |
3019 | break; | |
3020 | path->slots[0]--; | |
3021 | } | |
3022 | ||
3023 | /* pull out the item */ | |
3024 | leaf = path->nodes[0]; | |
7b128766 JB |
3025 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3026 | ||
3027 | /* make sure the item matches what we want */ | |
3028 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3029 | break; | |
962a298f | 3030 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3031 | break; |
3032 | ||
3033 | /* release the path since we're done with it */ | |
b3b4aa74 | 3034 | btrfs_release_path(path); |
7b128766 JB |
3035 | |
3036 | /* | |
3037 | * this is where we are basically btrfs_lookup, without the | |
3038 | * crossing root thing. we store the inode number in the | |
3039 | * offset of the orphan item. | |
3040 | */ | |
8f6d7f4f JB |
3041 | |
3042 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3043 | btrfs_err(fs_info, |
3044 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3045 | ret = -EINVAL; |
3046 | goto out; | |
3047 | } | |
3048 | ||
3049 | last_objectid = found_key.offset; | |
3050 | ||
5d4f98a2 YZ |
3051 | found_key.objectid = found_key.offset; |
3052 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3053 | found_key.offset = 0; | |
0202e83f | 3054 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
8c6ffba0 | 3055 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3056 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3057 | goto out; |
7b128766 | 3058 | |
0b246afa | 3059 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3060 | struct btrfs_root *dead_root; |
3061 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3062 | int is_dead_root = 0; | |
3063 | ||
3064 | /* | |
3065 | * this is an orphan in the tree root. Currently these | |
3066 | * could come from 2 sources: | |
3067 | * a) a snapshot deletion in progress | |
3068 | * b) a free space cache inode | |
3069 | * We need to distinguish those two, as the snapshot | |
3070 | * orphan must not get deleted. | |
3071 | * find_dead_roots already ran before us, so if this | |
3072 | * is a snapshot deletion, we should find the root | |
a619b3c7 | 3073 | * in the fs_roots radix tree. |
f8e9e0b0 | 3074 | */ |
a619b3c7 RK |
3075 | |
3076 | spin_lock(&fs_info->fs_roots_radix_lock); | |
3077 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
3078 | (unsigned long)found_key.objectid); | |
3079 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) | |
3080 | is_dead_root = 1; | |
3081 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
3082 | ||
f8e9e0b0 AJ |
3083 | if (is_dead_root) { |
3084 | /* prevent this orphan from being found again */ | |
3085 | key.offset = found_key.objectid - 1; | |
3086 | continue; | |
3087 | } | |
f7e9e8fc | 3088 | |
f8e9e0b0 | 3089 | } |
f7e9e8fc | 3090 | |
7b128766 | 3091 | /* |
f7e9e8fc OS |
3092 | * If we have an inode with links, there are a couple of |
3093 | * possibilities. Old kernels (before v3.12) used to create an | |
3094 | * orphan item for truncate indicating that there were possibly | |
3095 | * extent items past i_size that needed to be deleted. In v3.12, | |
3096 | * truncate was changed to update i_size in sync with the extent | |
3097 | * items, but the (useless) orphan item was still created. Since | |
3098 | * v4.18, we don't create the orphan item for truncate at all. | |
3099 | * | |
3100 | * So, this item could mean that we need to do a truncate, but | |
3101 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3102 | * and was not cleanly unmounted. The odds of that are quite | |
3103 | * slim, and it's a pain to do the truncate now, so just delete | |
3104 | * the orphan item. | |
3105 | * | |
3106 | * It's also possible that this orphan item was supposed to be | |
3107 | * deleted but wasn't. The inode number may have been reused, | |
3108 | * but either way, we can delete the orphan item. | |
7b128766 | 3109 | */ |
f7e9e8fc OS |
3110 | if (ret == -ENOENT || inode->i_nlink) { |
3111 | if (!ret) | |
3112 | iput(inode); | |
a8c9e576 | 3113 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3114 | if (IS_ERR(trans)) { |
3115 | ret = PTR_ERR(trans); | |
3116 | goto out; | |
3117 | } | |
0b246afa JM |
3118 | btrfs_debug(fs_info, "auto deleting %Lu", |
3119 | found_key.objectid); | |
a8c9e576 JB |
3120 | ret = btrfs_del_orphan_item(trans, root, |
3121 | found_key.objectid); | |
3a45bb20 | 3122 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3123 | if (ret) |
3124 | goto out; | |
7b128766 JB |
3125 | continue; |
3126 | } | |
3127 | ||
f7e9e8fc | 3128 | nr_unlink++; |
7b128766 JB |
3129 | |
3130 | /* this will do delete_inode and everything for us */ | |
3131 | iput(inode); | |
3132 | } | |
3254c876 MX |
3133 | /* release the path since we're done with it */ |
3134 | btrfs_release_path(path); | |
3135 | ||
d68fc57b YZ |
3136 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3137 | ||
a575ceeb | 3138 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3139 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3140 | if (!IS_ERR(trans)) |
3a45bb20 | 3141 | btrfs_end_transaction(trans); |
d68fc57b | 3142 | } |
7b128766 JB |
3143 | |
3144 | if (nr_unlink) | |
0b246afa | 3145 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3146 | |
3147 | out: | |
3148 | if (ret) | |
0b246afa | 3149 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3150 | btrfs_free_path(path); |
3151 | return ret; | |
7b128766 JB |
3152 | } |
3153 | ||
46a53cca CM |
3154 | /* |
3155 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3156 | * don't find any xattrs, we know there can't be any acls. | |
3157 | * | |
3158 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3159 | */ | |
3160 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3161 | int slot, u64 objectid, |
3162 | int *first_xattr_slot) | |
46a53cca CM |
3163 | { |
3164 | u32 nritems = btrfs_header_nritems(leaf); | |
3165 | struct btrfs_key found_key; | |
f23b5a59 JB |
3166 | static u64 xattr_access = 0; |
3167 | static u64 xattr_default = 0; | |
46a53cca CM |
3168 | int scanned = 0; |
3169 | ||
f23b5a59 | 3170 | if (!xattr_access) { |
97d79299 AG |
3171 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3172 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3173 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3174 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3175 | } |
3176 | ||
46a53cca | 3177 | slot++; |
63541927 | 3178 | *first_xattr_slot = -1; |
46a53cca CM |
3179 | while (slot < nritems) { |
3180 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3181 | ||
3182 | /* we found a different objectid, there must not be acls */ | |
3183 | if (found_key.objectid != objectid) | |
3184 | return 0; | |
3185 | ||
3186 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3187 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3188 | if (*first_xattr_slot == -1) |
3189 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3190 | if (found_key.offset == xattr_access || |
3191 | found_key.offset == xattr_default) | |
3192 | return 1; | |
3193 | } | |
46a53cca CM |
3194 | |
3195 | /* | |
3196 | * we found a key greater than an xattr key, there can't | |
3197 | * be any acls later on | |
3198 | */ | |
3199 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3200 | return 0; | |
3201 | ||
3202 | slot++; | |
3203 | scanned++; | |
3204 | ||
3205 | /* | |
3206 | * it goes inode, inode backrefs, xattrs, extents, | |
3207 | * so if there are a ton of hard links to an inode there can | |
3208 | * be a lot of backrefs. Don't waste time searching too hard, | |
3209 | * this is just an optimization | |
3210 | */ | |
3211 | if (scanned >= 8) | |
3212 | break; | |
3213 | } | |
3214 | /* we hit the end of the leaf before we found an xattr or | |
3215 | * something larger than an xattr. We have to assume the inode | |
3216 | * has acls | |
3217 | */ | |
63541927 FDBM |
3218 | if (*first_xattr_slot == -1) |
3219 | *first_xattr_slot = slot; | |
46a53cca CM |
3220 | return 1; |
3221 | } | |
3222 | ||
d352ac68 CM |
3223 | /* |
3224 | * read an inode from the btree into the in-memory inode | |
3225 | */ | |
4222ea71 FM |
3226 | static int btrfs_read_locked_inode(struct inode *inode, |
3227 | struct btrfs_path *in_path) | |
39279cc3 | 3228 | { |
0b246afa | 3229 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3230 | struct btrfs_path *path = in_path; |
5f39d397 | 3231 | struct extent_buffer *leaf; |
39279cc3 CM |
3232 | struct btrfs_inode_item *inode_item; |
3233 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3234 | struct btrfs_key location; | |
67de1176 | 3235 | unsigned long ptr; |
46a53cca | 3236 | int maybe_acls; |
618e21d5 | 3237 | u32 rdev; |
39279cc3 | 3238 | int ret; |
2f7e33d4 | 3239 | bool filled = false; |
63541927 | 3240 | int first_xattr_slot; |
2f7e33d4 MX |
3241 | |
3242 | ret = btrfs_fill_inode(inode, &rdev); | |
3243 | if (!ret) | |
3244 | filled = true; | |
39279cc3 | 3245 | |
4222ea71 FM |
3246 | if (!path) { |
3247 | path = btrfs_alloc_path(); | |
3248 | if (!path) | |
3249 | return -ENOMEM; | |
3250 | } | |
1748f843 | 3251 | |
39279cc3 | 3252 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3253 | |
39279cc3 | 3254 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3255 | if (ret) { |
4222ea71 FM |
3256 | if (path != in_path) |
3257 | btrfs_free_path(path); | |
f5b3a417 | 3258 | return ret; |
67710892 | 3259 | } |
39279cc3 | 3260 | |
5f39d397 | 3261 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3262 | |
3263 | if (filled) | |
67de1176 | 3264 | goto cache_index; |
2f7e33d4 | 3265 | |
5f39d397 CM |
3266 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3267 | struct btrfs_inode_item); | |
5f39d397 | 3268 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3269 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3270 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3271 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3272 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
41a2ee75 JB |
3273 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
3274 | round_up(i_size_read(inode), fs_info->sectorsize)); | |
5f39d397 | 3275 | |
a937b979 DS |
3276 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3277 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3278 | |
a937b979 DS |
3279 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3280 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3281 | |
a937b979 DS |
3282 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3283 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3284 | |
9cc97d64 | 3285 | BTRFS_I(inode)->i_otime.tv_sec = |
3286 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3287 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3288 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3289 | |
a76a3cd4 | 3290 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3291 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3292 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3293 | ||
c7f88c4e JL |
3294 | inode_set_iversion_queried(inode, |
3295 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3296 | inode->i_generation = BTRFS_I(inode)->generation; |
3297 | inode->i_rdev = 0; | |
3298 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3299 | ||
3300 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3301 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3302 | ||
3303 | cache_index: | |
5dc562c5 JB |
3304 | /* |
3305 | * If we were modified in the current generation and evicted from memory | |
3306 | * and then re-read we need to do a full sync since we don't have any | |
3307 | * idea about which extents were modified before we were evicted from | |
3308 | * cache. | |
6e17d30b YD |
3309 | * |
3310 | * This is required for both inode re-read from disk and delayed inode | |
3311 | * in delayed_nodes_tree. | |
5dc562c5 | 3312 | */ |
0b246afa | 3313 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3314 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3315 | &BTRFS_I(inode)->runtime_flags); | |
3316 | ||
bde6c242 FM |
3317 | /* |
3318 | * We don't persist the id of the transaction where an unlink operation | |
3319 | * against the inode was last made. So here we assume the inode might | |
3320 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3321 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3322 | * between the inode and its parent if the inode is fsync'ed and the log | |
3323 | * replayed. For example, in the scenario: | |
3324 | * | |
3325 | * touch mydir/foo | |
3326 | * ln mydir/foo mydir/bar | |
3327 | * sync | |
3328 | * unlink mydir/bar | |
3329 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3330 | * xfs_io -c fsync mydir/foo | |
3331 | * <power failure> | |
3332 | * mount fs, triggers fsync log replay | |
3333 | * | |
3334 | * We must make sure that when we fsync our inode foo we also log its | |
3335 | * parent inode, otherwise after log replay the parent still has the | |
3336 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3337 | * and doesn't have an inode ref with the name "bar" anymore. | |
3338 | * | |
3339 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3340 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3341 | * transaction commits on fsync if our inode is a directory, or if our |
3342 | * inode is not a directory, logging its parent unnecessarily. | |
3343 | */ | |
3344 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3345 | ||
67de1176 MX |
3346 | path->slots[0]++; |
3347 | if (inode->i_nlink != 1 || | |
3348 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3349 | goto cache_acl; | |
3350 | ||
3351 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3352 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3353 | goto cache_acl; |
3354 | ||
3355 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3356 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3357 | struct btrfs_inode_ref *ref; | |
3358 | ||
3359 | ref = (struct btrfs_inode_ref *)ptr; | |
3360 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3361 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3362 | struct btrfs_inode_extref *extref; | |
3363 | ||
3364 | extref = (struct btrfs_inode_extref *)ptr; | |
3365 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3366 | extref); | |
3367 | } | |
2f7e33d4 | 3368 | cache_acl: |
46a53cca CM |
3369 | /* |
3370 | * try to precache a NULL acl entry for files that don't have | |
3371 | * any xattrs or acls | |
3372 | */ | |
33345d01 | 3373 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3374 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3375 | if (first_xattr_slot != -1) { |
3376 | path->slots[0] = first_xattr_slot; | |
3377 | ret = btrfs_load_inode_props(inode, path); | |
3378 | if (ret) | |
0b246afa | 3379 | btrfs_err(fs_info, |
351fd353 | 3380 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3381 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3382 | root->root_key.objectid, ret); |
3383 | } | |
4222ea71 FM |
3384 | if (path != in_path) |
3385 | btrfs_free_path(path); | |
63541927 | 3386 | |
72c04902 AV |
3387 | if (!maybe_acls) |
3388 | cache_no_acl(inode); | |
46a53cca | 3389 | |
39279cc3 | 3390 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3391 | case S_IFREG: |
3392 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3393 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3394 | inode->i_fop = &btrfs_file_operations; |
3395 | inode->i_op = &btrfs_file_inode_operations; | |
3396 | break; | |
3397 | case S_IFDIR: | |
3398 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3399 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3400 | break; |
3401 | case S_IFLNK: | |
3402 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3403 | inode_nohighmem(inode); |
4779cc04 | 3404 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3405 | break; |
618e21d5 | 3406 | default: |
0279b4cd | 3407 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3408 | init_special_inode(inode, inode->i_mode, rdev); |
3409 | break; | |
39279cc3 | 3410 | } |
6cbff00f | 3411 | |
7b6a221e | 3412 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3413 | return 0; |
39279cc3 CM |
3414 | } |
3415 | ||
d352ac68 CM |
3416 | /* |
3417 | * given a leaf and an inode, copy the inode fields into the leaf | |
3418 | */ | |
e02119d5 CM |
3419 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3420 | struct extent_buffer *leaf, | |
5f39d397 | 3421 | struct btrfs_inode_item *item, |
39279cc3 CM |
3422 | struct inode *inode) |
3423 | { | |
51fab693 LB |
3424 | struct btrfs_map_token token; |
3425 | ||
c82f823c | 3426 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3427 | |
cc4c13d5 DS |
3428 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
3429 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); | |
3430 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); | |
3431 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); | |
3432 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); | |
3433 | ||
3434 | btrfs_set_token_timespec_sec(&token, &item->atime, | |
3435 | inode->i_atime.tv_sec); | |
3436 | btrfs_set_token_timespec_nsec(&token, &item->atime, | |
3437 | inode->i_atime.tv_nsec); | |
3438 | ||
3439 | btrfs_set_token_timespec_sec(&token, &item->mtime, | |
3440 | inode->i_mtime.tv_sec); | |
3441 | btrfs_set_token_timespec_nsec(&token, &item->mtime, | |
3442 | inode->i_mtime.tv_nsec); | |
3443 | ||
3444 | btrfs_set_token_timespec_sec(&token, &item->ctime, | |
3445 | inode->i_ctime.tv_sec); | |
3446 | btrfs_set_token_timespec_nsec(&token, &item->ctime, | |
3447 | inode->i_ctime.tv_nsec); | |
3448 | ||
3449 | btrfs_set_token_timespec_sec(&token, &item->otime, | |
3450 | BTRFS_I(inode)->i_otime.tv_sec); | |
3451 | btrfs_set_token_timespec_nsec(&token, &item->otime, | |
3452 | BTRFS_I(inode)->i_otime.tv_nsec); | |
3453 | ||
3454 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); | |
3455 | btrfs_set_token_inode_generation(&token, item, | |
3456 | BTRFS_I(inode)->generation); | |
3457 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); | |
3458 | btrfs_set_token_inode_transid(&token, item, trans->transid); | |
3459 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); | |
3460 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); | |
3461 | btrfs_set_token_inode_block_group(&token, item, 0); | |
39279cc3 CM |
3462 | } |
3463 | ||
d352ac68 CM |
3464 | /* |
3465 | * copy everything in the in-memory inode into the btree. | |
3466 | */ | |
2115133f | 3467 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3468 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3469 | { |
3470 | struct btrfs_inode_item *inode_item; | |
3471 | struct btrfs_path *path; | |
5f39d397 | 3472 | struct extent_buffer *leaf; |
39279cc3 CM |
3473 | int ret; |
3474 | ||
3475 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3476 | if (!path) |
3477 | return -ENOMEM; | |
3478 | ||
b9473439 | 3479 | path->leave_spinning = 1; |
16cdcec7 MX |
3480 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3481 | 1); | |
39279cc3 CM |
3482 | if (ret) { |
3483 | if (ret > 0) | |
3484 | ret = -ENOENT; | |
3485 | goto failed; | |
3486 | } | |
3487 | ||
5f39d397 CM |
3488 | leaf = path->nodes[0]; |
3489 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3490 | struct btrfs_inode_item); |
39279cc3 | 3491 | |
e02119d5 | 3492 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3493 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3494 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3495 | ret = 0; |
3496 | failed: | |
39279cc3 CM |
3497 | btrfs_free_path(path); |
3498 | return ret; | |
3499 | } | |
3500 | ||
2115133f CM |
3501 | /* |
3502 | * copy everything in the in-memory inode into the btree. | |
3503 | */ | |
3504 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3505 | struct btrfs_root *root, struct inode *inode) | |
3506 | { | |
0b246afa | 3507 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3508 | int ret; |
3509 | ||
3510 | /* | |
3511 | * If the inode is a free space inode, we can deadlock during commit | |
3512 | * if we put it into the delayed code. | |
3513 | * | |
3514 | * The data relocation inode should also be directly updated | |
3515 | * without delay | |
3516 | */ | |
70ddc553 | 3517 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3518 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3519 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3520 | btrfs_update_root_times(trans, root); |
3521 | ||
2115133f CM |
3522 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3523 | if (!ret) | |
3524 | btrfs_set_inode_last_trans(trans, inode); | |
3525 | return ret; | |
3526 | } | |
3527 | ||
3528 | return btrfs_update_inode_item(trans, root, inode); | |
3529 | } | |
3530 | ||
be6aef60 JB |
3531 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3532 | struct btrfs_root *root, | |
3533 | struct inode *inode) | |
2115133f CM |
3534 | { |
3535 | int ret; | |
3536 | ||
3537 | ret = btrfs_update_inode(trans, root, inode); | |
3538 | if (ret == -ENOSPC) | |
3539 | return btrfs_update_inode_item(trans, root, inode); | |
3540 | return ret; | |
3541 | } | |
3542 | ||
d352ac68 CM |
3543 | /* |
3544 | * unlink helper that gets used here in inode.c and in the tree logging | |
3545 | * recovery code. It remove a link in a directory with a given name, and | |
3546 | * also drops the back refs in the inode to the directory | |
3547 | */ | |
92986796 AV |
3548 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3549 | struct btrfs_root *root, | |
4ec5934e NB |
3550 | struct btrfs_inode *dir, |
3551 | struct btrfs_inode *inode, | |
92986796 | 3552 | const char *name, int name_len) |
39279cc3 | 3553 | { |
0b246afa | 3554 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 3555 | struct btrfs_path *path; |
39279cc3 | 3556 | int ret = 0; |
39279cc3 | 3557 | struct btrfs_dir_item *di; |
aec7477b | 3558 | u64 index; |
33345d01 LZ |
3559 | u64 ino = btrfs_ino(inode); |
3560 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3561 | |
3562 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3563 | if (!path) { |
3564 | ret = -ENOMEM; | |
554233a6 | 3565 | goto out; |
54aa1f4d CM |
3566 | } |
3567 | ||
b9473439 | 3568 | path->leave_spinning = 1; |
33345d01 | 3569 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 3570 | name, name_len, -1); |
3cf5068f LB |
3571 | if (IS_ERR_OR_NULL(di)) { |
3572 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
3573 | goto err; |
3574 | } | |
39279cc3 | 3575 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3576 | if (ret) |
3577 | goto err; | |
b3b4aa74 | 3578 | btrfs_release_path(path); |
39279cc3 | 3579 | |
67de1176 MX |
3580 | /* |
3581 | * If we don't have dir index, we have to get it by looking up | |
3582 | * the inode ref, since we get the inode ref, remove it directly, | |
3583 | * it is unnecessary to do delayed deletion. | |
3584 | * | |
3585 | * But if we have dir index, needn't search inode ref to get it. | |
3586 | * Since the inode ref is close to the inode item, it is better | |
3587 | * that we delay to delete it, and just do this deletion when | |
3588 | * we update the inode item. | |
3589 | */ | |
4ec5934e | 3590 | if (inode->dir_index) { |
67de1176 MX |
3591 | ret = btrfs_delayed_delete_inode_ref(inode); |
3592 | if (!ret) { | |
4ec5934e | 3593 | index = inode->dir_index; |
67de1176 MX |
3594 | goto skip_backref; |
3595 | } | |
3596 | } | |
3597 | ||
33345d01 LZ |
3598 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3599 | dir_ino, &index); | |
aec7477b | 3600 | if (ret) { |
0b246afa | 3601 | btrfs_info(fs_info, |
c2cf52eb | 3602 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 3603 | name_len, name, ino, dir_ino); |
66642832 | 3604 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
3605 | goto err; |
3606 | } | |
67de1176 | 3607 | skip_backref: |
9add2945 | 3608 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 3609 | if (ret) { |
66642832 | 3610 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 3611 | goto err; |
79787eaa | 3612 | } |
39279cc3 | 3613 | |
4ec5934e NB |
3614 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
3615 | dir_ino); | |
79787eaa | 3616 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 3617 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3618 | goto err; |
3619 | } | |
e02119d5 | 3620 | |
4ec5934e NB |
3621 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
3622 | index); | |
6418c961 CM |
3623 | if (ret == -ENOENT) |
3624 | ret = 0; | |
d4e3991b | 3625 | else if (ret) |
66642832 | 3626 | btrfs_abort_transaction(trans, ret); |
63611e73 JB |
3627 | |
3628 | /* | |
3629 | * If we have a pending delayed iput we could end up with the final iput | |
3630 | * being run in btrfs-cleaner context. If we have enough of these built | |
3631 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
3632 | * way to throttle the unlinks. Since we're currently holding a ref on | |
3633 | * the inode we can run the delayed iput here without any issues as the | |
3634 | * final iput won't be done until after we drop the ref we're currently | |
3635 | * holding. | |
3636 | */ | |
3637 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
3638 | err: |
3639 | btrfs_free_path(path); | |
e02119d5 CM |
3640 | if (ret) |
3641 | goto out; | |
3642 | ||
6ef06d27 | 3643 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
3644 | inode_inc_iversion(&inode->vfs_inode); |
3645 | inode_inc_iversion(&dir->vfs_inode); | |
3646 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
3647 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
3648 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 3649 | out: |
39279cc3 CM |
3650 | return ret; |
3651 | } | |
3652 | ||
92986796 AV |
3653 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3654 | struct btrfs_root *root, | |
4ec5934e | 3655 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
3656 | const char *name, int name_len) |
3657 | { | |
3658 | int ret; | |
3659 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
3660 | if (!ret) { | |
4ec5934e NB |
3661 | drop_nlink(&inode->vfs_inode); |
3662 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
3663 | } |
3664 | return ret; | |
3665 | } | |
39279cc3 | 3666 | |
a22285a6 YZ |
3667 | /* |
3668 | * helper to start transaction for unlink and rmdir. | |
3669 | * | |
d52be818 JB |
3670 | * unlink and rmdir are special in btrfs, they do not always free space, so |
3671 | * if we cannot make our reservations the normal way try and see if there is | |
3672 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
3673 | * allow the unlink to occur. | |
a22285a6 | 3674 | */ |
d52be818 | 3675 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 3676 | { |
a22285a6 | 3677 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 3678 | |
e70bea5f JB |
3679 | /* |
3680 | * 1 for the possible orphan item | |
3681 | * 1 for the dir item | |
3682 | * 1 for the dir index | |
3683 | * 1 for the inode ref | |
e70bea5f JB |
3684 | * 1 for the inode |
3685 | */ | |
7f9fe614 | 3686 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
a22285a6 YZ |
3687 | } |
3688 | ||
3689 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
3690 | { | |
3691 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3692 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 3693 | struct inode *inode = d_inode(dentry); |
a22285a6 | 3694 | int ret; |
a22285a6 | 3695 | |
d52be818 | 3696 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
3697 | if (IS_ERR(trans)) |
3698 | return PTR_ERR(trans); | |
5f39d397 | 3699 | |
4ec5934e NB |
3700 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
3701 | 0); | |
12fcfd22 | 3702 | |
4ec5934e NB |
3703 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
3704 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
3705 | dentry->d_name.len); | |
b532402e TI |
3706 | if (ret) |
3707 | goto out; | |
7b128766 | 3708 | |
a22285a6 | 3709 | if (inode->i_nlink == 0) { |
73f2e545 | 3710 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
3711 | if (ret) |
3712 | goto out; | |
a22285a6 | 3713 | } |
7b128766 | 3714 | |
b532402e | 3715 | out: |
3a45bb20 | 3716 | btrfs_end_transaction(trans); |
2ff7e61e | 3717 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
3718 | return ret; |
3719 | } | |
3720 | ||
f60a2364 | 3721 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
045d3967 | 3722 | struct inode *dir, struct dentry *dentry) |
4df27c4d | 3723 | { |
401b3b19 | 3724 | struct btrfs_root *root = BTRFS_I(dir)->root; |
045d3967 | 3725 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
4df27c4d YZ |
3726 | struct btrfs_path *path; |
3727 | struct extent_buffer *leaf; | |
3728 | struct btrfs_dir_item *di; | |
3729 | struct btrfs_key key; | |
045d3967 JB |
3730 | const char *name = dentry->d_name.name; |
3731 | int name_len = dentry->d_name.len; | |
4df27c4d YZ |
3732 | u64 index; |
3733 | int ret; | |
045d3967 | 3734 | u64 objectid; |
4a0cc7ca | 3735 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d | 3736 | |
045d3967 JB |
3737 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
3738 | objectid = inode->root->root_key.objectid; | |
3739 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
3740 | objectid = inode->location.objectid; | |
3741 | } else { | |
3742 | WARN_ON(1); | |
3743 | return -EINVAL; | |
3744 | } | |
3745 | ||
4df27c4d YZ |
3746 | path = btrfs_alloc_path(); |
3747 | if (!path) | |
3748 | return -ENOMEM; | |
3749 | ||
33345d01 | 3750 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 3751 | name, name_len, -1); |
79787eaa | 3752 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 3753 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
3754 | goto out; |
3755 | } | |
4df27c4d YZ |
3756 | |
3757 | leaf = path->nodes[0]; | |
3758 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
3759 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
3760 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 3761 | if (ret) { |
66642832 | 3762 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3763 | goto out; |
3764 | } | |
b3b4aa74 | 3765 | btrfs_release_path(path); |
4df27c4d | 3766 | |
d49d3287 JB |
3767 | /* |
3768 | * This is a placeholder inode for a subvolume we didn't have a | |
3769 | * reference to at the time of the snapshot creation. In the meantime | |
3770 | * we could have renamed the real subvol link into our snapshot, so | |
3771 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. | |
3772 | * Instead simply lookup the dir_index_item for this entry so we can | |
3773 | * remove it. Otherwise we know we have a ref to the root and we can | |
3774 | * call btrfs_del_root_ref, and it _shouldn't_ fail. | |
3775 | */ | |
3776 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { | |
33345d01 | 3777 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 3778 | name, name_len); |
79787eaa JM |
3779 | if (IS_ERR_OR_NULL(di)) { |
3780 | if (!di) | |
3781 | ret = -ENOENT; | |
3782 | else | |
3783 | ret = PTR_ERR(di); | |
66642832 | 3784 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3785 | goto out; |
3786 | } | |
4df27c4d YZ |
3787 | |
3788 | leaf = path->nodes[0]; | |
3789 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d | 3790 | index = key.offset; |
d49d3287 JB |
3791 | btrfs_release_path(path); |
3792 | } else { | |
3793 | ret = btrfs_del_root_ref(trans, objectid, | |
3794 | root->root_key.objectid, dir_ino, | |
3795 | &index, name, name_len); | |
3796 | if (ret) { | |
3797 | btrfs_abort_transaction(trans, ret); | |
3798 | goto out; | |
3799 | } | |
4df27c4d YZ |
3800 | } |
3801 | ||
9add2945 | 3802 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 3803 | if (ret) { |
66642832 | 3804 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3805 | goto out; |
3806 | } | |
4df27c4d | 3807 | |
6ef06d27 | 3808 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 3809 | inode_inc_iversion(dir); |
c2050a45 | 3810 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 3811 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 3812 | if (ret) |
66642832 | 3813 | btrfs_abort_transaction(trans, ret); |
79787eaa | 3814 | out: |
71d7aed0 | 3815 | btrfs_free_path(path); |
79787eaa | 3816 | return ret; |
4df27c4d YZ |
3817 | } |
3818 | ||
ec42f167 MT |
3819 | /* |
3820 | * Helper to check if the subvolume references other subvolumes or if it's | |
3821 | * default. | |
3822 | */ | |
f60a2364 | 3823 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
3824 | { |
3825 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3826 | struct btrfs_path *path; | |
3827 | struct btrfs_dir_item *di; | |
3828 | struct btrfs_key key; | |
3829 | u64 dir_id; | |
3830 | int ret; | |
3831 | ||
3832 | path = btrfs_alloc_path(); | |
3833 | if (!path) | |
3834 | return -ENOMEM; | |
3835 | ||
3836 | /* Make sure this root isn't set as the default subvol */ | |
3837 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
3838 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
3839 | dir_id, "default", 7, 0); | |
3840 | if (di && !IS_ERR(di)) { | |
3841 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
3842 | if (key.objectid == root->root_key.objectid) { | |
3843 | ret = -EPERM; | |
3844 | btrfs_err(fs_info, | |
3845 | "deleting default subvolume %llu is not allowed", | |
3846 | key.objectid); | |
3847 | goto out; | |
3848 | } | |
3849 | btrfs_release_path(path); | |
3850 | } | |
3851 | ||
3852 | key.objectid = root->root_key.objectid; | |
3853 | key.type = BTRFS_ROOT_REF_KEY; | |
3854 | key.offset = (u64)-1; | |
3855 | ||
3856 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
3857 | if (ret < 0) | |
3858 | goto out; | |
3859 | BUG_ON(ret == 0); | |
3860 | ||
3861 | ret = 0; | |
3862 | if (path->slots[0] > 0) { | |
3863 | path->slots[0]--; | |
3864 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
3865 | if (key.objectid == root->root_key.objectid && | |
3866 | key.type == BTRFS_ROOT_REF_KEY) | |
3867 | ret = -ENOTEMPTY; | |
3868 | } | |
3869 | out: | |
3870 | btrfs_free_path(path); | |
3871 | return ret; | |
3872 | } | |
3873 | ||
20a68004 NB |
3874 | /* Delete all dentries for inodes belonging to the root */ |
3875 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
3876 | { | |
3877 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3878 | struct rb_node *node; | |
3879 | struct rb_node *prev; | |
3880 | struct btrfs_inode *entry; | |
3881 | struct inode *inode; | |
3882 | u64 objectid = 0; | |
3883 | ||
3884 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
3885 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
3886 | ||
3887 | spin_lock(&root->inode_lock); | |
3888 | again: | |
3889 | node = root->inode_tree.rb_node; | |
3890 | prev = NULL; | |
3891 | while (node) { | |
3892 | prev = node; | |
3893 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
3894 | ||
37508515 | 3895 | if (objectid < btrfs_ino(entry)) |
20a68004 | 3896 | node = node->rb_left; |
37508515 | 3897 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
3898 | node = node->rb_right; |
3899 | else | |
3900 | break; | |
3901 | } | |
3902 | if (!node) { | |
3903 | while (prev) { | |
3904 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 3905 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
3906 | node = prev; |
3907 | break; | |
3908 | } | |
3909 | prev = rb_next(prev); | |
3910 | } | |
3911 | } | |
3912 | while (node) { | |
3913 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 3914 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
3915 | inode = igrab(&entry->vfs_inode); |
3916 | if (inode) { | |
3917 | spin_unlock(&root->inode_lock); | |
3918 | if (atomic_read(&inode->i_count) > 1) | |
3919 | d_prune_aliases(inode); | |
3920 | /* | |
3921 | * btrfs_drop_inode will have it removed from the inode | |
3922 | * cache when its usage count hits zero. | |
3923 | */ | |
3924 | iput(inode); | |
3925 | cond_resched(); | |
3926 | spin_lock(&root->inode_lock); | |
3927 | goto again; | |
3928 | } | |
3929 | ||
3930 | if (cond_resched_lock(&root->inode_lock)) | |
3931 | goto again; | |
3932 | ||
3933 | node = rb_next(node); | |
3934 | } | |
3935 | spin_unlock(&root->inode_lock); | |
3936 | } | |
3937 | ||
f60a2364 MT |
3938 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
3939 | { | |
3940 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
3941 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3942 | struct inode *inode = d_inode(dentry); | |
3943 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
3944 | struct btrfs_trans_handle *trans; | |
3945 | struct btrfs_block_rsv block_rsv; | |
3946 | u64 root_flags; | |
f60a2364 MT |
3947 | int ret; |
3948 | int err; | |
3949 | ||
3950 | /* | |
3951 | * Don't allow to delete a subvolume with send in progress. This is | |
3952 | * inside the inode lock so the error handling that has to drop the bit | |
3953 | * again is not run concurrently. | |
3954 | */ | |
3955 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 3956 | if (dest->send_in_progress) { |
f60a2364 MT |
3957 | spin_unlock(&dest->root_item_lock); |
3958 | btrfs_warn(fs_info, | |
3959 | "attempt to delete subvolume %llu during send", | |
3960 | dest->root_key.objectid); | |
3961 | return -EPERM; | |
3962 | } | |
a7176f74 LF |
3963 | root_flags = btrfs_root_flags(&dest->root_item); |
3964 | btrfs_set_root_flags(&dest->root_item, | |
3965 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
3966 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
3967 | |
3968 | down_write(&fs_info->subvol_sem); | |
3969 | ||
3970 | err = may_destroy_subvol(dest); | |
3971 | if (err) | |
3972 | goto out_up_write; | |
3973 | ||
3974 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
3975 | /* | |
3976 | * One for dir inode, | |
3977 | * two for dir entries, | |
3978 | * two for root ref/backref. | |
3979 | */ | |
c4c129db | 3980 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
f60a2364 MT |
3981 | if (err) |
3982 | goto out_up_write; | |
3983 | ||
3984 | trans = btrfs_start_transaction(root, 0); | |
3985 | if (IS_ERR(trans)) { | |
3986 | err = PTR_ERR(trans); | |
3987 | goto out_release; | |
3988 | } | |
3989 | trans->block_rsv = &block_rsv; | |
3990 | trans->bytes_reserved = block_rsv.size; | |
3991 | ||
3992 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
3993 | ||
045d3967 | 3994 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
f60a2364 MT |
3995 | if (ret) { |
3996 | err = ret; | |
3997 | btrfs_abort_transaction(trans, ret); | |
3998 | goto out_end_trans; | |
3999 | } | |
4000 | ||
4001 | btrfs_record_root_in_trans(trans, dest); | |
4002 | ||
4003 | memset(&dest->root_item.drop_progress, 0, | |
4004 | sizeof(dest->root_item.drop_progress)); | |
4005 | dest->root_item.drop_level = 0; | |
4006 | btrfs_set_root_refs(&dest->root_item, 0); | |
4007 | ||
4008 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4009 | ret = btrfs_insert_orphan_item(trans, | |
4010 | fs_info->tree_root, | |
4011 | dest->root_key.objectid); | |
4012 | if (ret) { | |
4013 | btrfs_abort_transaction(trans, ret); | |
4014 | err = ret; | |
4015 | goto out_end_trans; | |
4016 | } | |
4017 | } | |
4018 | ||
d1957791 | 4019 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4020 | BTRFS_UUID_KEY_SUBVOL, |
4021 | dest->root_key.objectid); | |
4022 | if (ret && ret != -ENOENT) { | |
4023 | btrfs_abort_transaction(trans, ret); | |
4024 | err = ret; | |
4025 | goto out_end_trans; | |
4026 | } | |
4027 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4028 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4029 | dest->root_item.received_uuid, |
4030 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4031 | dest->root_key.objectid); | |
4032 | if (ret && ret != -ENOENT) { | |
4033 | btrfs_abort_transaction(trans, ret); | |
4034 | err = ret; | |
4035 | goto out_end_trans; | |
4036 | } | |
4037 | } | |
4038 | ||
4039 | out_end_trans: | |
4040 | trans->block_rsv = NULL; | |
4041 | trans->bytes_reserved = 0; | |
4042 | ret = btrfs_end_transaction(trans); | |
4043 | if (ret && !err) | |
4044 | err = ret; | |
4045 | inode->i_flags |= S_DEAD; | |
4046 | out_release: | |
4047 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); | |
4048 | out_up_write: | |
4049 | up_write(&fs_info->subvol_sem); | |
4050 | if (err) { | |
4051 | spin_lock(&dest->root_item_lock); | |
4052 | root_flags = btrfs_root_flags(&dest->root_item); | |
4053 | btrfs_set_root_flags(&dest->root_item, | |
4054 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4055 | spin_unlock(&dest->root_item_lock); | |
4056 | } else { | |
4057 | d_invalidate(dentry); | |
20a68004 | 4058 | btrfs_prune_dentries(dest); |
f60a2364 MT |
4059 | ASSERT(dest->send_in_progress == 0); |
4060 | ||
4061 | /* the last ref */ | |
4062 | if (dest->ino_cache_inode) { | |
4063 | iput(dest->ino_cache_inode); | |
4064 | dest->ino_cache_inode = NULL; | |
4065 | } | |
4066 | } | |
4067 | ||
4068 | return err; | |
4069 | } | |
4070 | ||
39279cc3 CM |
4071 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4072 | { | |
2b0143b5 | 4073 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4074 | int err = 0; |
39279cc3 | 4075 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4076 | struct btrfs_trans_handle *trans; |
44f714da | 4077 | u64 last_unlink_trans; |
39279cc3 | 4078 | |
b3ae244e | 4079 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4080 | return -ENOTEMPTY; |
4a0cc7ca | 4081 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4082 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4083 | |
d52be818 | 4084 | trans = __unlink_start_trans(dir); |
a22285a6 | 4085 | if (IS_ERR(trans)) |
5df6a9f6 | 4086 | return PTR_ERR(trans); |
5df6a9f6 | 4087 | |
4a0cc7ca | 4088 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 4089 | err = btrfs_unlink_subvol(trans, dir, dentry); |
4df27c4d YZ |
4090 | goto out; |
4091 | } | |
4092 | ||
73f2e545 | 4093 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4094 | if (err) |
4df27c4d | 4095 | goto out; |
7b128766 | 4096 | |
44f714da FM |
4097 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4098 | ||
39279cc3 | 4099 | /* now the directory is empty */ |
4ec5934e NB |
4100 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4101 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4102 | dentry->d_name.len); | |
44f714da | 4103 | if (!err) { |
6ef06d27 | 4104 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4105 | /* |
4106 | * Propagate the last_unlink_trans value of the deleted dir to | |
4107 | * its parent directory. This is to prevent an unrecoverable | |
4108 | * log tree in the case we do something like this: | |
4109 | * 1) create dir foo | |
4110 | * 2) create snapshot under dir foo | |
4111 | * 3) delete the snapshot | |
4112 | * 4) rmdir foo | |
4113 | * 5) mkdir foo | |
4114 | * 6) fsync foo or some file inside foo | |
4115 | */ | |
4116 | if (last_unlink_trans >= trans->transid) | |
4117 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4118 | } | |
4df27c4d | 4119 | out: |
3a45bb20 | 4120 | btrfs_end_transaction(trans); |
2ff7e61e | 4121 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4122 | |
39279cc3 CM |
4123 | return err; |
4124 | } | |
4125 | ||
ddfae63c JB |
4126 | /* |
4127 | * Return this if we need to call truncate_block for the last bit of the | |
4128 | * truncate. | |
4129 | */ | |
4130 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4131 | |
39279cc3 CM |
4132 | /* |
4133 | * this can truncate away extent items, csum items and directory items. | |
4134 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4135 | * any higher than new_size |
39279cc3 CM |
4136 | * |
4137 | * csum items that cross the new i_size are truncated to the new size | |
4138 | * as well. | |
7b128766 JB |
4139 | * |
4140 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4141 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4142 | */ |
8082510e YZ |
4143 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4144 | struct btrfs_root *root, | |
4145 | struct inode *inode, | |
4146 | u64 new_size, u32 min_type) | |
39279cc3 | 4147 | { |
0b246afa | 4148 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4149 | struct btrfs_path *path; |
5f39d397 | 4150 | struct extent_buffer *leaf; |
39279cc3 | 4151 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4152 | struct btrfs_key key; |
4153 | struct btrfs_key found_key; | |
39279cc3 | 4154 | u64 extent_start = 0; |
db94535d | 4155 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4156 | u64 extent_offset = 0; |
39279cc3 | 4157 | u64 item_end = 0; |
c1aa4575 | 4158 | u64 last_size = new_size; |
8082510e | 4159 | u32 found_type = (u8)-1; |
39279cc3 CM |
4160 | int found_extent; |
4161 | int del_item; | |
85e21bac CM |
4162 | int pending_del_nr = 0; |
4163 | int pending_del_slot = 0; | |
179e29e4 | 4164 | int extent_type = -1; |
8082510e | 4165 | int ret; |
4a0cc7ca | 4166 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4167 | u64 bytes_deleted = 0; |
897ca819 TM |
4168 | bool be_nice = false; |
4169 | bool should_throttle = false; | |
28553fa9 FM |
4170 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
4171 | struct extent_state *cached_state = NULL; | |
8082510e YZ |
4172 | |
4173 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4174 | |
28ed1345 | 4175 | /* |
92a7cc42 QW |
4176 | * For non-free space inodes and non-shareable roots, we want to back |
4177 | * off from time to time. This means all inodes in subvolume roots, | |
4178 | * reloc roots, and data reloc roots. | |
28ed1345 | 4179 | */ |
70ddc553 | 4180 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
92a7cc42 | 4181 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
897ca819 | 4182 | be_nice = true; |
28ed1345 | 4183 | |
0eb0e19c MF |
4184 | path = btrfs_alloc_path(); |
4185 | if (!path) | |
4186 | return -ENOMEM; | |
e4058b54 | 4187 | path->reada = READA_BACK; |
0eb0e19c | 4188 | |
82028e0a | 4189 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
a5ae50de FM |
4190 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
4191 | &cached_state); | |
28553fa9 | 4192 | |
82028e0a QW |
4193 | /* |
4194 | * We want to drop from the next block forward in case this | |
4195 | * new size is not block aligned since we will be keeping the | |
4196 | * last block of the extent just the way it is. | |
4197 | */ | |
dcdbc059 | 4198 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4199 | fs_info->sectorsize), |
da17066c | 4200 | (u64)-1, 0); |
82028e0a | 4201 | } |
8082510e | 4202 | |
16cdcec7 MX |
4203 | /* |
4204 | * This function is also used to drop the items in the log tree before | |
4205 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
52042d8e | 4206 | * it is used to drop the logged items. So we shouldn't kill the delayed |
16cdcec7 MX |
4207 | * items. |
4208 | */ | |
4209 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4210 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4211 | |
33345d01 | 4212 | key.objectid = ino; |
39279cc3 | 4213 | key.offset = (u64)-1; |
5f39d397 CM |
4214 | key.type = (u8)-1; |
4215 | ||
85e21bac | 4216 | search_again: |
28ed1345 CM |
4217 | /* |
4218 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4219 | * up a huge file in a single leaf. Most of the time that | |
4220 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4221 | */ | |
fd86a3a3 OS |
4222 | if (be_nice && bytes_deleted > SZ_32M && |
4223 | btrfs_should_end_transaction(trans)) { | |
4224 | ret = -EAGAIN; | |
4225 | goto out; | |
28ed1345 CM |
4226 | } |
4227 | ||
85e21bac | 4228 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
fd86a3a3 | 4229 | if (ret < 0) |
8082510e | 4230 | goto out; |
d397712b | 4231 | |
85e21bac | 4232 | if (ret > 0) { |
fd86a3a3 | 4233 | ret = 0; |
e02119d5 CM |
4234 | /* there are no items in the tree for us to truncate, we're |
4235 | * done | |
4236 | */ | |
8082510e YZ |
4237 | if (path->slots[0] == 0) |
4238 | goto out; | |
85e21bac CM |
4239 | path->slots[0]--; |
4240 | } | |
4241 | ||
d397712b | 4242 | while (1) { |
9ddc959e JB |
4243 | u64 clear_start = 0, clear_len = 0; |
4244 | ||
39279cc3 | 4245 | fi = NULL; |
5f39d397 CM |
4246 | leaf = path->nodes[0]; |
4247 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4248 | found_type = found_key.type; |
39279cc3 | 4249 | |
33345d01 | 4250 | if (found_key.objectid != ino) |
39279cc3 | 4251 | break; |
5f39d397 | 4252 | |
85e21bac | 4253 | if (found_type < min_type) |
39279cc3 CM |
4254 | break; |
4255 | ||
5f39d397 | 4256 | item_end = found_key.offset; |
39279cc3 | 4257 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4258 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4259 | struct btrfs_file_extent_item); |
179e29e4 CM |
4260 | extent_type = btrfs_file_extent_type(leaf, fi); |
4261 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4262 | item_end += |
db94535d | 4263 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4264 | |
4265 | trace_btrfs_truncate_show_fi_regular( | |
4266 | BTRFS_I(inode), leaf, fi, | |
4267 | found_key.offset); | |
179e29e4 | 4268 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 QW |
4269 | item_end += btrfs_file_extent_ram_bytes(leaf, |
4270 | fi); | |
09ed2f16 LB |
4271 | |
4272 | trace_btrfs_truncate_show_fi_inline( | |
4273 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4274 | found_key.offset); | |
39279cc3 | 4275 | } |
008630c1 | 4276 | item_end--; |
39279cc3 | 4277 | } |
8082510e YZ |
4278 | if (found_type > min_type) { |
4279 | del_item = 1; | |
4280 | } else { | |
76b42abb | 4281 | if (item_end < new_size) |
b888db2b | 4282 | break; |
8082510e YZ |
4283 | if (found_key.offset >= new_size) |
4284 | del_item = 1; | |
4285 | else | |
4286 | del_item = 0; | |
39279cc3 | 4287 | } |
39279cc3 | 4288 | found_extent = 0; |
39279cc3 | 4289 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4290 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4291 | goto delete; | |
4292 | ||
4293 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4294 | u64 num_dec; |
9ddc959e JB |
4295 | |
4296 | clear_start = found_key.offset; | |
db94535d | 4297 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4298 | if (!del_item) { |
db94535d CM |
4299 | u64 orig_num_bytes = |
4300 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4301 | extent_num_bytes = ALIGN(new_size - |
4302 | found_key.offset, | |
0b246afa | 4303 | fs_info->sectorsize); |
9ddc959e | 4304 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
db94535d CM |
4305 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4306 | extent_num_bytes); | |
4307 | num_dec = (orig_num_bytes - | |
9069218d | 4308 | extent_num_bytes); |
92a7cc42 | 4309 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 MX |
4310 | &root->state) && |
4311 | extent_start != 0) | |
a76a3cd4 | 4312 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4313 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4314 | } else { |
db94535d CM |
4315 | extent_num_bytes = |
4316 | btrfs_file_extent_disk_num_bytes(leaf, | |
4317 | fi); | |
5d4f98a2 YZ |
4318 | extent_offset = found_key.offset - |
4319 | btrfs_file_extent_offset(leaf, fi); | |
4320 | ||
39279cc3 | 4321 | /* FIXME blocksize != 4096 */ |
9069218d | 4322 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4323 | if (extent_start != 0) { |
4324 | found_extent = 1; | |
92a7cc42 | 4325 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
27cdeb70 | 4326 | &root->state)) |
a76a3cd4 | 4327 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4328 | } |
39279cc3 | 4329 | } |
9ddc959e | 4330 | clear_len = num_dec; |
9069218d | 4331 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4332 | /* |
4333 | * we can't truncate inline items that have had | |
4334 | * special encodings | |
4335 | */ | |
4336 | if (!del_item && | |
c8b97818 | 4337 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4338 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4339 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4340 | u32 size = (u32)(new_size - found_key.offset); | |
4341 | ||
4342 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4343 | size = btrfs_file_extent_calc_inline_size(size); | |
78ac4f9e | 4344 | btrfs_truncate_item(path, size, 1); |
ddfae63c | 4345 | } else if (!del_item) { |
514ac8ad | 4346 | /* |
ddfae63c JB |
4347 | * We have to bail so the last_size is set to |
4348 | * just before this extent. | |
514ac8ad | 4349 | */ |
fd86a3a3 | 4350 | ret = NEED_TRUNCATE_BLOCK; |
ddfae63c | 4351 | break; |
9ddc959e JB |
4352 | } else { |
4353 | /* | |
4354 | * Inline extents are special, we just treat | |
4355 | * them as a full sector worth in the file | |
4356 | * extent tree just for simplicity sake. | |
4357 | */ | |
4358 | clear_len = fs_info->sectorsize; | |
ddfae63c | 4359 | } |
0305cd5f | 4360 | |
92a7cc42 | 4361 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
0305cd5f | 4362 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4363 | } |
179e29e4 | 4364 | delete: |
9ddc959e JB |
4365 | /* |
4366 | * We use btrfs_truncate_inode_items() to clean up log trees for | |
4367 | * multiple fsyncs, and in this case we don't want to clear the | |
4368 | * file extent range because it's just the log. | |
4369 | */ | |
4370 | if (root == BTRFS_I(inode)->root) { | |
4371 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), | |
4372 | clear_start, clear_len); | |
4373 | if (ret) { | |
4374 | btrfs_abort_transaction(trans, ret); | |
4375 | break; | |
4376 | } | |
4377 | } | |
4378 | ||
ddfae63c JB |
4379 | if (del_item) |
4380 | last_size = found_key.offset; | |
4381 | else | |
4382 | last_size = new_size; | |
39279cc3 | 4383 | if (del_item) { |
85e21bac CM |
4384 | if (!pending_del_nr) { |
4385 | /* no pending yet, add ourselves */ | |
4386 | pending_del_slot = path->slots[0]; | |
4387 | pending_del_nr = 1; | |
4388 | } else if (pending_del_nr && | |
4389 | path->slots[0] + 1 == pending_del_slot) { | |
4390 | /* hop on the pending chunk */ | |
4391 | pending_del_nr++; | |
4392 | pending_del_slot = path->slots[0]; | |
4393 | } else { | |
d397712b | 4394 | BUG(); |
85e21bac | 4395 | } |
39279cc3 CM |
4396 | } else { |
4397 | break; | |
4398 | } | |
897ca819 | 4399 | should_throttle = false; |
28f75a0e | 4400 | |
27cdeb70 | 4401 | if (found_extent && |
82028e0a | 4402 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
ffd4bb2a QW |
4403 | struct btrfs_ref ref = { 0 }; |
4404 | ||
28ed1345 | 4405 | bytes_deleted += extent_num_bytes; |
ffd4bb2a QW |
4406 | |
4407 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, | |
4408 | extent_start, extent_num_bytes, 0); | |
4409 | ref.real_root = root->root_key.objectid; | |
4410 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
4411 | ino, extent_offset); | |
4412 | ret = btrfs_free_extent(trans, &ref); | |
05522109 OS |
4413 | if (ret) { |
4414 | btrfs_abort_transaction(trans, ret); | |
4415 | break; | |
4416 | } | |
28f75a0e | 4417 | if (be_nice) { |
7c861627 | 4418 | if (btrfs_should_throttle_delayed_refs(trans)) |
897ca819 | 4419 | should_throttle = true; |
28f75a0e | 4420 | } |
39279cc3 | 4421 | } |
85e21bac | 4422 | |
8082510e YZ |
4423 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4424 | break; | |
4425 | ||
4426 | if (path->slots[0] == 0 || | |
1262133b | 4427 | path->slots[0] != pending_del_slot || |
28bad212 | 4428 | should_throttle) { |
8082510e YZ |
4429 | if (pending_del_nr) { |
4430 | ret = btrfs_del_items(trans, root, path, | |
4431 | pending_del_slot, | |
4432 | pending_del_nr); | |
79787eaa | 4433 | if (ret) { |
66642832 | 4434 | btrfs_abort_transaction(trans, ret); |
fd86a3a3 | 4435 | break; |
79787eaa | 4436 | } |
8082510e YZ |
4437 | pending_del_nr = 0; |
4438 | } | |
b3b4aa74 | 4439 | btrfs_release_path(path); |
28bad212 | 4440 | |
28f75a0e | 4441 | /* |
28bad212 JB |
4442 | * We can generate a lot of delayed refs, so we need to |
4443 | * throttle every once and a while and make sure we're | |
4444 | * adding enough space to keep up with the work we are | |
4445 | * generating. Since we hold a transaction here we | |
4446 | * can't flush, and we don't want to FLUSH_LIMIT because | |
4447 | * we could have generated too many delayed refs to | |
4448 | * actually allocate, so just bail if we're short and | |
4449 | * let the normal reservation dance happen higher up. | |
28f75a0e | 4450 | */ |
28bad212 JB |
4451 | if (should_throttle) { |
4452 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
4453 | BTRFS_RESERVE_NO_FLUSH); | |
4454 | if (ret) { | |
4455 | ret = -EAGAIN; | |
4456 | break; | |
4457 | } | |
28f75a0e | 4458 | } |
85e21bac | 4459 | goto search_again; |
8082510e YZ |
4460 | } else { |
4461 | path->slots[0]--; | |
85e21bac | 4462 | } |
39279cc3 | 4463 | } |
8082510e | 4464 | out: |
fd86a3a3 OS |
4465 | if (ret >= 0 && pending_del_nr) { |
4466 | int err; | |
4467 | ||
4468 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
85e21bac | 4469 | pending_del_nr); |
fd86a3a3 OS |
4470 | if (err) { |
4471 | btrfs_abort_transaction(trans, err); | |
4472 | ret = err; | |
4473 | } | |
85e21bac | 4474 | } |
76b42abb FM |
4475 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4476 | ASSERT(last_size >= new_size); | |
fd86a3a3 | 4477 | if (!ret && last_size > new_size) |
76b42abb | 4478 | last_size = new_size; |
d923afe9 | 4479 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
a5ae50de FM |
4480 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
4481 | (u64)-1, &cached_state); | |
76b42abb | 4482 | } |
28ed1345 | 4483 | |
39279cc3 | 4484 | btrfs_free_path(path); |
fd86a3a3 | 4485 | return ret; |
39279cc3 CM |
4486 | } |
4487 | ||
4488 | /* | |
9703fefe | 4489 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4490 | * @inode - inode that we're zeroing |
4491 | * @from - the offset to start zeroing | |
4492 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4493 | * offset | |
4494 | * @front - zero up to the offset instead of from the offset on | |
4495 | * | |
9703fefe | 4496 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4497 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4498 | */ |
9703fefe | 4499 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4500 | int front) |
39279cc3 | 4501 | { |
0b246afa | 4502 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4503 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4504 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4505 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4506 | struct extent_state *cached_state = NULL; |
364ecf36 | 4507 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4508 | char *kaddr; |
6d4572a9 | 4509 | bool only_release_metadata = false; |
0b246afa | 4510 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4511 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4512 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4513 | struct page *page; |
3b16a4e3 | 4514 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
6d4572a9 | 4515 | size_t write_bytes = blocksize; |
39279cc3 | 4516 | int ret = 0; |
9703fefe CR |
4517 | u64 block_start; |
4518 | u64 block_end; | |
39279cc3 | 4519 | |
b03ebd99 NB |
4520 | if (IS_ALIGNED(offset, blocksize) && |
4521 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4522 | goto out; |
9703fefe | 4523 | |
8b62f87b JB |
4524 | block_start = round_down(from, blocksize); |
4525 | block_end = block_start + blocksize - 1; | |
4526 | ||
39279cc3 | 4527 | |
6d4572a9 QW |
4528 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
4529 | blocksize); | |
4530 | if (ret < 0) { | |
38d37aa9 QW |
4531 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
4532 | &write_bytes) > 0) { | |
6d4572a9 QW |
4533 | /* For nocow case, no need to reserve data space */ |
4534 | only_release_metadata = true; | |
4535 | } else { | |
4536 | goto out; | |
4537 | } | |
4538 | } | |
4539 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); | |
4540 | if (ret < 0) { | |
4541 | if (!only_release_metadata) | |
4542 | btrfs_free_reserved_data_space(inode, data_reserved, | |
4543 | block_start, blocksize); | |
4544 | goto out; | |
4545 | } | |
211c17f5 | 4546 | again: |
3b16a4e3 | 4547 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4548 | if (!page) { |
bc42bda2 | 4549 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 | 4550 | block_start, blocksize, true); |
8702ba93 | 4551 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
ac6a2b36 | 4552 | ret = -ENOMEM; |
39279cc3 | 4553 | goto out; |
5d5e103a | 4554 | } |
e6dcd2dc | 4555 | |
39279cc3 | 4556 | if (!PageUptodate(page)) { |
9ebefb18 | 4557 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4558 | lock_page(page); |
211c17f5 CM |
4559 | if (page->mapping != mapping) { |
4560 | unlock_page(page); | |
09cbfeaf | 4561 | put_page(page); |
211c17f5 CM |
4562 | goto again; |
4563 | } | |
39279cc3 CM |
4564 | if (!PageUptodate(page)) { |
4565 | ret = -EIO; | |
89642229 | 4566 | goto out_unlock; |
39279cc3 CM |
4567 | } |
4568 | } | |
211c17f5 | 4569 | wait_on_page_writeback(page); |
e6dcd2dc | 4570 | |
9703fefe | 4571 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4572 | set_page_extent_mapped(page); |
4573 | ||
c3504372 | 4574 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
e6dcd2dc | 4575 | if (ordered) { |
9703fefe | 4576 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4577 | &cached_state); |
e6dcd2dc | 4578 | unlock_page(page); |
09cbfeaf | 4579 | put_page(page); |
eb84ae03 | 4580 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4581 | btrfs_put_ordered_extent(ordered); |
4582 | goto again; | |
4583 | } | |
4584 | ||
9703fefe | 4585 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
e182163d OS |
4586 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
4587 | 0, 0, &cached_state); | |
5d5e103a | 4588 | |
e3b8a485 | 4589 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 4590 | &cached_state); |
9ed74f2d | 4591 | if (ret) { |
9703fefe | 4592 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4593 | &cached_state); |
9ed74f2d JB |
4594 | goto out_unlock; |
4595 | } | |
4596 | ||
9703fefe | 4597 | if (offset != blocksize) { |
2aaa6655 | 4598 | if (!len) |
9703fefe | 4599 | len = blocksize - offset; |
e6dcd2dc | 4600 | kaddr = kmap(page); |
2aaa6655 | 4601 | if (front) |
9703fefe CR |
4602 | memset(kaddr + (block_start - page_offset(page)), |
4603 | 0, offset); | |
2aaa6655 | 4604 | else |
9703fefe CR |
4605 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4606 | 0, len); | |
e6dcd2dc CM |
4607 | flush_dcache_page(page); |
4608 | kunmap(page); | |
4609 | } | |
247e743c | 4610 | ClearPageChecked(page); |
e6dcd2dc | 4611 | set_page_dirty(page); |
e43bbe5e | 4612 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4613 | |
6d4572a9 QW |
4614 | if (only_release_metadata) |
4615 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, | |
4616 | block_end, EXTENT_NORESERVE, NULL, NULL, | |
4617 | GFP_NOFS); | |
4618 | ||
89642229 | 4619 | out_unlock: |
6d4572a9 QW |
4620 | if (ret) { |
4621 | if (only_release_metadata) | |
4622 | btrfs_delalloc_release_metadata(BTRFS_I(inode), | |
4623 | blocksize, true); | |
4624 | else | |
4625 | btrfs_delalloc_release_space(inode, data_reserved, | |
4626 | block_start, blocksize, true); | |
4627 | } | |
8702ba93 | 4628 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
39279cc3 | 4629 | unlock_page(page); |
09cbfeaf | 4630 | put_page(page); |
39279cc3 | 4631 | out: |
6d4572a9 | 4632 | if (only_release_metadata) |
38d37aa9 | 4633 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
364ecf36 | 4634 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4635 | return ret; |
4636 | } | |
4637 | ||
16e7549f JB |
4638 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4639 | u64 offset, u64 len) | |
4640 | { | |
0b246afa | 4641 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4642 | struct btrfs_trans_handle *trans; |
4643 | int ret; | |
4644 | ||
4645 | /* | |
4646 | * Still need to make sure the inode looks like it's been updated so | |
4647 | * that any holes get logged if we fsync. | |
4648 | */ | |
0b246afa JM |
4649 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4650 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4651 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4652 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4653 | return 0; | |
4654 | } | |
4655 | ||
4656 | /* | |
4657 | * 1 - for the one we're dropping | |
4658 | * 1 - for the one we're adding | |
4659 | * 1 - for updating the inode. | |
4660 | */ | |
4661 | trans = btrfs_start_transaction(root, 3); | |
4662 | if (IS_ERR(trans)) | |
4663 | return PTR_ERR(trans); | |
4664 | ||
4665 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4666 | if (ret) { | |
66642832 | 4667 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4668 | btrfs_end_transaction(trans); |
16e7549f JB |
4669 | return ret; |
4670 | } | |
4671 | ||
f85b7379 DS |
4672 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4673 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4674 | if (ret) |
66642832 | 4675 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4676 | else |
4677 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4678 | btrfs_end_transaction(trans); |
16e7549f JB |
4679 | return ret; |
4680 | } | |
4681 | ||
695a0d0d JB |
4682 | /* |
4683 | * This function puts in dummy file extents for the area we're creating a hole | |
4684 | * for. So if we are truncating this file to a larger size we need to insert | |
4685 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4686 | * the range between oldsize and size | |
4687 | */ | |
a41ad394 | 4688 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4689 | { |
0b246afa | 4690 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4691 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4692 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4693 | struct extent_map *em = NULL; |
2ac55d41 | 4694 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4695 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4696 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4697 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4698 | u64 last_byte; |
4699 | u64 cur_offset; | |
4700 | u64 hole_size; | |
9ed74f2d | 4701 | int err = 0; |
39279cc3 | 4702 | |
a71754fc | 4703 | /* |
9703fefe CR |
4704 | * If our size started in the middle of a block we need to zero out the |
4705 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4706 | * expose stale data. |
4707 | */ | |
9703fefe | 4708 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4709 | if (err) |
4710 | return err; | |
4711 | ||
9036c102 YZ |
4712 | if (size <= hole_start) |
4713 | return 0; | |
4714 | ||
b272ae22 | 4715 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
23d31bd4 | 4716 | block_end - 1, &cached_state); |
9036c102 YZ |
4717 | cur_offset = hole_start; |
4718 | while (1) { | |
fc4f21b1 | 4719 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
39b07b5d | 4720 | block_end - cur_offset); |
79787eaa JM |
4721 | if (IS_ERR(em)) { |
4722 | err = PTR_ERR(em); | |
f2767956 | 4723 | em = NULL; |
79787eaa JM |
4724 | break; |
4725 | } | |
9036c102 | 4726 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4727 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
9ddc959e JB |
4728 | hole_size = last_byte - cur_offset; |
4729 | ||
8082510e | 4730 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4731 | struct extent_map *hole_em; |
9ed74f2d | 4732 | |
16e7549f JB |
4733 | err = maybe_insert_hole(root, inode, cur_offset, |
4734 | hole_size); | |
4735 | if (err) | |
3893e33b | 4736 | break; |
9ddc959e JB |
4737 | |
4738 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4739 | cur_offset, hole_size); | |
4740 | if (err) | |
4741 | break; | |
4742 | ||
dcdbc059 | 4743 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4744 | cur_offset + hole_size - 1, 0); |
4745 | hole_em = alloc_extent_map(); | |
4746 | if (!hole_em) { | |
4747 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4748 | &BTRFS_I(inode)->runtime_flags); | |
4749 | goto next; | |
4750 | } | |
4751 | hole_em->start = cur_offset; | |
4752 | hole_em->len = hole_size; | |
4753 | hole_em->orig_start = cur_offset; | |
8082510e | 4754 | |
5dc562c5 JB |
4755 | hole_em->block_start = EXTENT_MAP_HOLE; |
4756 | hole_em->block_len = 0; | |
b4939680 | 4757 | hole_em->orig_block_len = 0; |
cc95bef6 | 4758 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 4759 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4760 | hole_em->generation = fs_info->generation; |
8082510e | 4761 | |
5dc562c5 JB |
4762 | while (1) { |
4763 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4764 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4765 | write_unlock(&em_tree->lock); |
4766 | if (err != -EEXIST) | |
4767 | break; | |
dcdbc059 NB |
4768 | btrfs_drop_extent_cache(BTRFS_I(inode), |
4769 | cur_offset, | |
5dc562c5 JB |
4770 | cur_offset + |
4771 | hole_size - 1, 0); | |
4772 | } | |
4773 | free_extent_map(hole_em); | |
9ddc959e JB |
4774 | } else { |
4775 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), | |
4776 | cur_offset, hole_size); | |
4777 | if (err) | |
4778 | break; | |
9036c102 | 4779 | } |
16e7549f | 4780 | next: |
9036c102 | 4781 | free_extent_map(em); |
a22285a6 | 4782 | em = NULL; |
9036c102 | 4783 | cur_offset = last_byte; |
8082510e | 4784 | if (cur_offset >= block_end) |
9036c102 YZ |
4785 | break; |
4786 | } | |
a22285a6 | 4787 | free_extent_map(em); |
e43bbe5e | 4788 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
4789 | return err; |
4790 | } | |
39279cc3 | 4791 | |
3972f260 | 4792 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4793 | { |
f4a2f4c5 MX |
4794 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4795 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4796 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4797 | loff_t newsize = attr->ia_size; |
4798 | int mask = attr->ia_valid; | |
8082510e YZ |
4799 | int ret; |
4800 | ||
3972f260 ES |
4801 | /* |
4802 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4803 | * special case where we need to update the times despite not having | |
4804 | * these flags set. For all other operations the VFS set these flags | |
4805 | * explicitly if it wants a timestamp update. | |
4806 | */ | |
dff6efc3 CH |
4807 | if (newsize != oldsize) { |
4808 | inode_inc_iversion(inode); | |
4809 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4810 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 4811 | current_time(inode); |
dff6efc3 | 4812 | } |
3972f260 | 4813 | |
a41ad394 | 4814 | if (newsize > oldsize) { |
9ea24bbe | 4815 | /* |
ea14b57f | 4816 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
4817 | * This is to ensure the snapshot captures a fully consistent |
4818 | * state of this file - if the snapshot captures this expanding | |
4819 | * truncation, it must capture all writes that happened before | |
4820 | * this truncation. | |
4821 | */ | |
dcc3eb96 | 4822 | btrfs_drew_write_lock(&root->snapshot_lock); |
a41ad394 | 4823 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 4824 | if (ret) { |
dcc3eb96 | 4825 | btrfs_drew_write_unlock(&root->snapshot_lock); |
8082510e | 4826 | return ret; |
9ea24bbe | 4827 | } |
8082510e | 4828 | |
f4a2f4c5 | 4829 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 4830 | if (IS_ERR(trans)) { |
dcc3eb96 | 4831 | btrfs_drew_write_unlock(&root->snapshot_lock); |
f4a2f4c5 | 4832 | return PTR_ERR(trans); |
9ea24bbe | 4833 | } |
f4a2f4c5 MX |
4834 | |
4835 | i_size_write(inode, newsize); | |
d923afe9 | 4836 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
27772b68 | 4837 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 4838 | ret = btrfs_update_inode(trans, root, inode); |
dcc3eb96 | 4839 | btrfs_drew_write_unlock(&root->snapshot_lock); |
3a45bb20 | 4840 | btrfs_end_transaction(trans); |
a41ad394 | 4841 | } else { |
8082510e | 4842 | |
a41ad394 JB |
4843 | /* |
4844 | * We're truncating a file that used to have good data down to | |
4845 | * zero. Make sure it gets into the ordered flush list so that | |
4846 | * any new writes get down to disk quickly. | |
4847 | */ | |
4848 | if (newsize == 0) | |
72ac3c0d JB |
4849 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4850 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4851 | |
a41ad394 | 4852 | truncate_setsize(inode, newsize); |
2e60a51e | 4853 | |
8e0fa5d7 DS |
4854 | /* Disable nonlocked read DIO to avoid the endless truncate */ |
4855 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); | |
2e60a51e | 4856 | inode_dio_wait(inode); |
8e0fa5d7 | 4857 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 4858 | |
213e8c55 | 4859 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
4860 | if (ret && inode->i_nlink) { |
4861 | int err; | |
4862 | ||
4863 | /* | |
f7e9e8fc OS |
4864 | * Truncate failed, so fix up the in-memory size. We |
4865 | * adjusted disk_i_size down as we removed extents, so | |
4866 | * wait for disk_i_size to be stable and then update the | |
4867 | * in-memory size to match. | |
7f4f6e0a | 4868 | */ |
f7e9e8fc | 4869 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 4870 | if (err) |
f7e9e8fc OS |
4871 | return err; |
4872 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 4873 | } |
8082510e YZ |
4874 | } |
4875 | ||
a41ad394 | 4876 | return ret; |
8082510e YZ |
4877 | } |
4878 | ||
9036c102 YZ |
4879 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4880 | { | |
2b0143b5 | 4881 | struct inode *inode = d_inode(dentry); |
b83cc969 | 4882 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4883 | int err; |
39279cc3 | 4884 | |
b83cc969 LZ |
4885 | if (btrfs_root_readonly(root)) |
4886 | return -EROFS; | |
4887 | ||
31051c85 | 4888 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
4889 | if (err) |
4890 | return err; | |
2bf5a725 | 4891 | |
5a3f23d5 | 4892 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4893 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4894 | if (err) |
4895 | return err; | |
39279cc3 | 4896 | } |
9036c102 | 4897 | |
1025774c CH |
4898 | if (attr->ia_valid) { |
4899 | setattr_copy(inode, attr); | |
0c4d2d95 | 4900 | inode_inc_iversion(inode); |
22c44fe6 | 4901 | err = btrfs_dirty_inode(inode); |
1025774c | 4902 | |
22c44fe6 | 4903 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 4904 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 4905 | } |
33268eaf | 4906 | |
39279cc3 CM |
4907 | return err; |
4908 | } | |
61295eb8 | 4909 | |
131e404a FDBM |
4910 | /* |
4911 | * While truncating the inode pages during eviction, we get the VFS calling | |
4912 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4913 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4914 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4915 | * extent_state structures over and over, wasting lots of time. | |
4916 | * | |
4917 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4918 | * those expensive operations on a per page basis and do only the ordered io | |
4919 | * finishing, while we release here the extent_map and extent_state structures, | |
4920 | * without the excessive merging and splitting. | |
4921 | */ | |
4922 | static void evict_inode_truncate_pages(struct inode *inode) | |
4923 | { | |
4924 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4925 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4926 | struct rb_node *node; | |
4927 | ||
4928 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 4929 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
4930 | |
4931 | write_lock(&map_tree->lock); | |
07e1ce09 | 4932 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
4933 | struct extent_map *em; |
4934 | ||
07e1ce09 | 4935 | node = rb_first_cached(&map_tree->map); |
131e404a | 4936 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
4937 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
4938 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
4939 | remove_extent_mapping(map_tree, em); |
4940 | free_extent_map(em); | |
7064dd5c FM |
4941 | if (need_resched()) { |
4942 | write_unlock(&map_tree->lock); | |
4943 | cond_resched(); | |
4944 | write_lock(&map_tree->lock); | |
4945 | } | |
131e404a FDBM |
4946 | } |
4947 | write_unlock(&map_tree->lock); | |
4948 | ||
6ca07097 FM |
4949 | /* |
4950 | * Keep looping until we have no more ranges in the io tree. | |
ba206a02 MWO |
4951 | * We can have ongoing bios started by readahead that have |
4952 | * their endio callback (extent_io.c:end_bio_extent_readpage) | |
9c6429d9 FM |
4953 | * still in progress (unlocked the pages in the bio but did not yet |
4954 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
4955 | * ranges can still be locked and eviction started because before |
4956 | * submitting those bios, which are executed by a separate task (work | |
4957 | * queue kthread), inode references (inode->i_count) were not taken | |
4958 | * (which would be dropped in the end io callback of each bio). | |
4959 | * Therefore here we effectively end up waiting for those bios and | |
4960 | * anyone else holding locked ranges without having bumped the inode's | |
4961 | * reference count - if we don't do it, when they access the inode's | |
4962 | * io_tree to unlock a range it may be too late, leading to an | |
4963 | * use-after-free issue. | |
4964 | */ | |
131e404a FDBM |
4965 | spin_lock(&io_tree->lock); |
4966 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
4967 | struct extent_state *state; | |
4968 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
4969 | u64 start; |
4970 | u64 end; | |
421f0922 | 4971 | unsigned state_flags; |
131e404a FDBM |
4972 | |
4973 | node = rb_first(&io_tree->state); | |
4974 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
4975 | start = state->start; |
4976 | end = state->end; | |
421f0922 | 4977 | state_flags = state->state; |
131e404a FDBM |
4978 | spin_unlock(&io_tree->lock); |
4979 | ||
ff13db41 | 4980 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
4981 | |
4982 | /* | |
4983 | * If still has DELALLOC flag, the extent didn't reach disk, | |
4984 | * and its reserved space won't be freed by delayed_ref. | |
4985 | * So we need to free its reserved space here. | |
4986 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
4987 | * | |
4988 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
4989 | */ | |
421f0922 | 4990 | if (state_flags & EXTENT_DELALLOC) |
8b8a979f NB |
4991 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
4992 | end - start + 1); | |
b9d0b389 | 4993 | |
6ca07097 | 4994 | clear_extent_bit(io_tree, start, end, |
e182163d OS |
4995 | EXTENT_LOCKED | EXTENT_DELALLOC | |
4996 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
4997 | &cached_state); | |
131e404a | 4998 | |
7064dd5c | 4999 | cond_resched(); |
131e404a FDBM |
5000 | spin_lock(&io_tree->lock); |
5001 | } | |
5002 | spin_unlock(&io_tree->lock); | |
5003 | } | |
5004 | ||
4b9d7b59 | 5005 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5006 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5007 | { |
5008 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5009 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
d3984c90 | 5010 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5011 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5012 | int ret; |
4b9d7b59 | 5013 | |
d3984c90 JB |
5014 | /* |
5015 | * Eviction should be taking place at some place safe because of our | |
5016 | * delayed iputs. However the normal flushing code will run delayed | |
5017 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5018 | * | |
5019 | * We reserve the delayed_refs_extra here again because we can't use | |
5020 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5021 | * above. We reserve our extra bit here because we generate a ton of | |
5022 | * delayed refs activity by truncating. | |
5023 | * | |
5024 | * If we cannot make our reservation we'll attempt to steal from the | |
5025 | * global reserve, because we really want to be able to free up space. | |
5026 | */ | |
5027 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, | |
5028 | BTRFS_RESERVE_FLUSH_EVICT); | |
5029 | if (ret) { | |
4b9d7b59 OS |
5030 | /* |
5031 | * Try to steal from the global reserve if there is space for | |
5032 | * it. | |
5033 | */ | |
d3984c90 JB |
5034 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
5035 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { | |
5036 | btrfs_warn(fs_info, | |
5037 | "could not allocate space for delete; will truncate on mount"); | |
5038 | return ERR_PTR(-ENOSPC); | |
5039 | } | |
5040 | delayed_refs_extra = 0; | |
5041 | } | |
4b9d7b59 | 5042 | |
d3984c90 JB |
5043 | trans = btrfs_join_transaction(root); |
5044 | if (IS_ERR(trans)) | |
5045 | return trans; | |
5046 | ||
5047 | if (delayed_refs_extra) { | |
5048 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5049 | trans->bytes_reserved = delayed_refs_extra; | |
5050 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5051 | delayed_refs_extra, 1); | |
4b9d7b59 | 5052 | } |
d3984c90 | 5053 | return trans; |
4b9d7b59 OS |
5054 | } |
5055 | ||
bd555975 | 5056 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5057 | { |
0b246afa | 5058 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5059 | struct btrfs_trans_handle *trans; |
5060 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5061 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5062 | int ret; |
5063 | ||
1abe9b8a | 5064 | trace_btrfs_inode_evict(inode); |
5065 | ||
3d48d981 | 5066 | if (!root) { |
e8f1bc14 | 5067 | clear_inode(inode); |
3d48d981 NB |
5068 | return; |
5069 | } | |
5070 | ||
131e404a FDBM |
5071 | evict_inode_truncate_pages(inode); |
5072 | ||
69e9c6c6 SB |
5073 | if (inode->i_nlink && |
5074 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5075 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5076 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5077 | goto no_delete; |
5078 | ||
27919067 | 5079 | if (is_bad_inode(inode)) |
39279cc3 | 5080 | goto no_delete; |
5f39d397 | 5081 | |
7ab7956e | 5082 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5083 | |
7b40b695 | 5084 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5085 | goto no_delete; |
c71bf099 | 5086 | |
76dda93c | 5087 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5088 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5089 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5090 | goto no_delete; |
5091 | } | |
5092 | ||
aa79021f | 5093 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5094 | if (ret) |
0e8c36a9 | 5095 | goto no_delete; |
0e8c36a9 | 5096 | |
2ff7e61e | 5097 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5098 | if (!rsv) |
4289a667 | 5099 | goto no_delete; |
2bd36e7b | 5100 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
ca7e70f5 | 5101 | rsv->failfast = 1; |
4289a667 | 5102 | |
6ef06d27 | 5103 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5104 | |
8082510e | 5105 | while (1) { |
ad80cf50 | 5106 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5107 | if (IS_ERR(trans)) |
5108 | goto free_rsv; | |
7b128766 | 5109 | |
4289a667 JB |
5110 | trans->block_rsv = rsv; |
5111 | ||
d68fc57b | 5112 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
27919067 OS |
5113 | trans->block_rsv = &fs_info->trans_block_rsv; |
5114 | btrfs_end_transaction(trans); | |
5115 | btrfs_btree_balance_dirty(fs_info); | |
5116 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5117 | goto free_rsv; | |
5118 | else if (!ret) | |
8082510e | 5119 | break; |
8082510e | 5120 | } |
5f39d397 | 5121 | |
4ef31a45 | 5122 | /* |
27919067 OS |
5123 | * Errors here aren't a big deal, it just means we leave orphan items in |
5124 | * the tree. They will be cleaned up on the next mount. If the inode | |
5125 | * number gets reused, cleanup deletes the orphan item without doing | |
5126 | * anything, and unlink reuses the existing orphan item. | |
5127 | * | |
5128 | * If it turns out that we are dropping too many of these, we might want | |
5129 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5130 | */ |
ad80cf50 | 5131 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5132 | if (!IS_ERR(trans)) { |
5133 | trans->block_rsv = rsv; | |
5134 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5135 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5136 | btrfs_end_transaction(trans); | |
5137 | } | |
54aa1f4d | 5138 | |
0b246afa | 5139 | if (!(root == fs_info->tree_root || |
581bb050 | 5140 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5141 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5142 | |
27919067 OS |
5143 | free_rsv: |
5144 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5145 | no_delete: |
27919067 OS |
5146 | /* |
5147 | * If we didn't successfully delete, the orphan item will still be in | |
5148 | * the tree and we'll retry on the next mount. Again, we might also want | |
5149 | * to retry these periodically in the future. | |
5150 | */ | |
f48d1cf5 | 5151 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5152 | clear_inode(inode); |
39279cc3 CM |
5153 | } |
5154 | ||
5155 | /* | |
6bf9e4bd QW |
5156 | * Return the key found in the dir entry in the location pointer, fill @type |
5157 | * with BTRFS_FT_*, and return 0. | |
5158 | * | |
005d6712 SY |
5159 | * If no dir entries were found, returns -ENOENT. |
5160 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5161 | */ |
5162 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5163 | struct btrfs_key *location, u8 *type) |
39279cc3 CM |
5164 | { |
5165 | const char *name = dentry->d_name.name; | |
5166 | int namelen = dentry->d_name.len; | |
5167 | struct btrfs_dir_item *di; | |
5168 | struct btrfs_path *path; | |
5169 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5170 | int ret = 0; |
39279cc3 CM |
5171 | |
5172 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5173 | if (!path) |
5174 | return -ENOMEM; | |
3954401f | 5175 | |
f85b7379 DS |
5176 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5177 | name, namelen, 0); | |
3cf5068f LB |
5178 | if (IS_ERR_OR_NULL(di)) { |
5179 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5180 | goto out; |
5181 | } | |
d397712b | 5182 | |
5f39d397 | 5183 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5184 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5185 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5186 | ret = -EUCLEAN; |
56a0e706 LB |
5187 | btrfs_warn(root->fs_info, |
5188 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5189 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5190 | location->objectid, location->type, location->offset); | |
56a0e706 | 5191 | } |
6bf9e4bd QW |
5192 | if (!ret) |
5193 | *type = btrfs_dir_type(path->nodes[0], di); | |
39279cc3 | 5194 | out: |
39279cc3 CM |
5195 | btrfs_free_path(path); |
5196 | return ret; | |
5197 | } | |
5198 | ||
5199 | /* | |
5200 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5201 | * needs to be changed to reflect the root directory of the tree root. This | |
5202 | * is kind of like crossing a mount point. | |
5203 | */ | |
2ff7e61e | 5204 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5205 | struct inode *dir, |
5206 | struct dentry *dentry, | |
5207 | struct btrfs_key *location, | |
5208 | struct btrfs_root **sub_root) | |
39279cc3 | 5209 | { |
4df27c4d YZ |
5210 | struct btrfs_path *path; |
5211 | struct btrfs_root *new_root; | |
5212 | struct btrfs_root_ref *ref; | |
5213 | struct extent_buffer *leaf; | |
1d4c08e0 | 5214 | struct btrfs_key key; |
4df27c4d YZ |
5215 | int ret; |
5216 | int err = 0; | |
39279cc3 | 5217 | |
4df27c4d YZ |
5218 | path = btrfs_alloc_path(); |
5219 | if (!path) { | |
5220 | err = -ENOMEM; | |
5221 | goto out; | |
5222 | } | |
39279cc3 | 5223 | |
4df27c4d | 5224 | err = -ENOENT; |
1d4c08e0 DS |
5225 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5226 | key.type = BTRFS_ROOT_REF_KEY; | |
5227 | key.offset = location->objectid; | |
5228 | ||
0b246afa | 5229 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5230 | if (ret) { |
5231 | if (ret < 0) | |
5232 | err = ret; | |
5233 | goto out; | |
5234 | } | |
39279cc3 | 5235 | |
4df27c4d YZ |
5236 | leaf = path->nodes[0]; |
5237 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5238 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5239 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5240 | goto out; | |
39279cc3 | 5241 | |
4df27c4d YZ |
5242 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5243 | (unsigned long)(ref + 1), | |
5244 | dentry->d_name.len); | |
5245 | if (ret) | |
5246 | goto out; | |
5247 | ||
b3b4aa74 | 5248 | btrfs_release_path(path); |
4df27c4d | 5249 | |
56e9357a | 5250 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
4df27c4d YZ |
5251 | if (IS_ERR(new_root)) { |
5252 | err = PTR_ERR(new_root); | |
5253 | goto out; | |
5254 | } | |
5255 | ||
4df27c4d YZ |
5256 | *sub_root = new_root; |
5257 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5258 | location->type = BTRFS_INODE_ITEM_KEY; | |
5259 | location->offset = 0; | |
5260 | err = 0; | |
5261 | out: | |
5262 | btrfs_free_path(path); | |
5263 | return err; | |
39279cc3 CM |
5264 | } |
5265 | ||
5d4f98a2 YZ |
5266 | static void inode_tree_add(struct inode *inode) |
5267 | { | |
5268 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5269 | struct btrfs_inode *entry; | |
03e860bd NP |
5270 | struct rb_node **p; |
5271 | struct rb_node *parent; | |
cef21937 | 5272 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5273 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5274 | |
1d3382cb | 5275 | if (inode_unhashed(inode)) |
76dda93c | 5276 | return; |
e1409cef | 5277 | parent = NULL; |
5d4f98a2 | 5278 | spin_lock(&root->inode_lock); |
e1409cef | 5279 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5280 | while (*p) { |
5281 | parent = *p; | |
5282 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5283 | ||
37508515 | 5284 | if (ino < btrfs_ino(entry)) |
03e860bd | 5285 | p = &parent->rb_left; |
37508515 | 5286 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5287 | p = &parent->rb_right; |
5d4f98a2 YZ |
5288 | else { |
5289 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5290 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5291 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5292 | RB_CLEAR_NODE(parent); |
5293 | spin_unlock(&root->inode_lock); | |
cef21937 | 5294 | return; |
5d4f98a2 YZ |
5295 | } |
5296 | } | |
cef21937 FDBM |
5297 | rb_link_node(new, parent, p); |
5298 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5299 | spin_unlock(&root->inode_lock); |
5300 | } | |
5301 | ||
5302 | static void inode_tree_del(struct inode *inode) | |
5303 | { | |
5304 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5305 | int empty = 0; |
5d4f98a2 | 5306 | |
03e860bd | 5307 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5308 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5309 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5310 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5311 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5312 | } |
03e860bd | 5313 | spin_unlock(&root->inode_lock); |
76dda93c | 5314 | |
69e9c6c6 | 5315 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5316 | spin_lock(&root->inode_lock); |
5317 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5318 | spin_unlock(&root->inode_lock); | |
5319 | if (empty) | |
5320 | btrfs_add_dead_root(root); | |
5321 | } | |
5322 | } | |
5323 | ||
5d4f98a2 | 5324 | |
e02119d5 CM |
5325 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5326 | { | |
5327 | struct btrfs_iget_args *args = p; | |
0202e83f DS |
5328 | |
5329 | inode->i_ino = args->ino; | |
5330 | BTRFS_I(inode)->location.objectid = args->ino; | |
5331 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5332 | BTRFS_I(inode)->location.offset = 0; | |
5c8fd99f JB |
5333 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
5334 | BUG_ON(args->root && !BTRFS_I(inode)->root); | |
39279cc3 CM |
5335 | return 0; |
5336 | } | |
5337 | ||
5338 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5339 | { | |
5340 | struct btrfs_iget_args *args = opaque; | |
0202e83f DS |
5341 | |
5342 | return args->ino == BTRFS_I(inode)->location.objectid && | |
d397712b | 5343 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5344 | } |
5345 | ||
0202e83f | 5346 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
5d4f98a2 | 5347 | struct btrfs_root *root) |
39279cc3 CM |
5348 | { |
5349 | struct inode *inode; | |
5350 | struct btrfs_iget_args args; | |
0202e83f | 5351 | unsigned long hashval = btrfs_inode_hash(ino, root); |
778ba82b | 5352 | |
0202e83f | 5353 | args.ino = ino; |
39279cc3 CM |
5354 | args.root = root; |
5355 | ||
778ba82b | 5356 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5357 | btrfs_init_locked_inode, |
5358 | (void *)&args); | |
5359 | return inode; | |
5360 | } | |
5361 | ||
4c66e0d4 | 5362 | /* |
0202e83f | 5363 | * Get an inode object given its inode number and corresponding root. |
4c66e0d4 DS |
5364 | * Path can be preallocated to prevent recursing back to iget through |
5365 | * allocator. NULL is also valid but may require an additional allocation | |
5366 | * later. | |
1a54ef8c | 5367 | */ |
0202e83f | 5368 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
4c66e0d4 | 5369 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5370 | { |
5371 | struct inode *inode; | |
5372 | ||
0202e83f | 5373 | inode = btrfs_iget_locked(s, ino, root); |
1a54ef8c | 5374 | if (!inode) |
5d4f98a2 | 5375 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5376 | |
5377 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5378 | int ret; |
5379 | ||
4222ea71 | 5380 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5381 | if (!ret) { |
1748f843 MF |
5382 | inode_tree_add(inode); |
5383 | unlock_new_inode(inode); | |
1748f843 | 5384 | } else { |
f5b3a417 AV |
5385 | iget_failed(inode); |
5386 | /* | |
5387 | * ret > 0 can come from btrfs_search_slot called by | |
5388 | * btrfs_read_locked_inode, this means the inode item | |
5389 | * was not found. | |
5390 | */ | |
5391 | if (ret > 0) | |
5392 | ret = -ENOENT; | |
5393 | inode = ERR_PTR(ret); | |
1748f843 MF |
5394 | } |
5395 | } | |
5396 | ||
1a54ef8c BR |
5397 | return inode; |
5398 | } | |
5399 | ||
0202e83f | 5400 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
4222ea71 | 5401 | { |
0202e83f | 5402 | return btrfs_iget_path(s, ino, root, NULL); |
4222ea71 FM |
5403 | } |
5404 | ||
4df27c4d YZ |
5405 | static struct inode *new_simple_dir(struct super_block *s, |
5406 | struct btrfs_key *key, | |
5407 | struct btrfs_root *root) | |
5408 | { | |
5409 | struct inode *inode = new_inode(s); | |
5410 | ||
5411 | if (!inode) | |
5412 | return ERR_PTR(-ENOMEM); | |
5413 | ||
5c8fd99f | 5414 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
4df27c4d | 5415 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
72ac3c0d | 5416 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5417 | |
5418 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
6bb6b514 OS |
5419 | /* |
5420 | * We only need lookup, the rest is read-only and there's no inode | |
5421 | * associated with the dentry | |
5422 | */ | |
5423 | inode->i_op = &simple_dir_inode_operations; | |
1fdf4194 | 5424 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5425 | inode->i_fop = &simple_dir_operations; |
5426 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5427 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5428 | inode->i_atime = inode->i_mtime; |
5429 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5430 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5431 | |
5432 | return inode; | |
5433 | } | |
5434 | ||
6bf9e4bd QW |
5435 | static inline u8 btrfs_inode_type(struct inode *inode) |
5436 | { | |
5437 | /* | |
5438 | * Compile-time asserts that generic FT_* types still match | |
5439 | * BTRFS_FT_* types | |
5440 | */ | |
5441 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); | |
5442 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); | |
5443 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); | |
5444 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); | |
5445 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); | |
5446 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); | |
5447 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); | |
5448 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); | |
5449 | ||
5450 | return fs_umode_to_ftype(inode->i_mode); | |
5451 | } | |
5452 | ||
3de4586c | 5453 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5454 | { |
0b246afa | 5455 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5456 | struct inode *inode; |
4df27c4d | 5457 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5458 | struct btrfs_root *sub_root = root; |
5459 | struct btrfs_key location; | |
6bf9e4bd | 5460 | u8 di_type = 0; |
b4aff1f8 | 5461 | int ret = 0; |
39279cc3 CM |
5462 | |
5463 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5464 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5465 | |
6bf9e4bd | 5466 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5467 | if (ret < 0) |
5468 | return ERR_PTR(ret); | |
5f39d397 | 5469 | |
4df27c4d | 5470 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
0202e83f | 5471 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
6bf9e4bd QW |
5472 | if (IS_ERR(inode)) |
5473 | return inode; | |
5474 | ||
5475 | /* Do extra check against inode mode with di_type */ | |
5476 | if (btrfs_inode_type(inode) != di_type) { | |
5477 | btrfs_crit(fs_info, | |
5478 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5479 | inode->i_mode, btrfs_inode_type(inode), | |
5480 | di_type); | |
5481 | iput(inode); | |
5482 | return ERR_PTR(-EUCLEAN); | |
5483 | } | |
4df27c4d YZ |
5484 | return inode; |
5485 | } | |
5486 | ||
2ff7e61e | 5487 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5488 | &location, &sub_root); |
5489 | if (ret < 0) { | |
5490 | if (ret != -ENOENT) | |
5491 | inode = ERR_PTR(ret); | |
5492 | else | |
5493 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5494 | } else { | |
0202e83f | 5495 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
39279cc3 | 5496 | } |
8727002f | 5497 | if (root != sub_root) |
00246528 | 5498 | btrfs_put_root(sub_root); |
76dda93c | 5499 | |
34d19bad | 5500 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5501 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5502 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5503 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5504 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5505 | if (ret) { |
5506 | iput(inode); | |
66b4ffd1 | 5507 | inode = ERR_PTR(ret); |
01cd3367 | 5508 | } |
c71bf099 YZ |
5509 | } |
5510 | ||
3de4586c CM |
5511 | return inode; |
5512 | } | |
5513 | ||
fe15ce44 | 5514 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5515 | { |
5516 | struct btrfs_root *root; | |
2b0143b5 | 5517 | struct inode *inode = d_inode(dentry); |
76dda93c | 5518 | |
848cce0d | 5519 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5520 | inode = d_inode(dentry->d_parent); |
76dda93c | 5521 | |
848cce0d LZ |
5522 | if (inode) { |
5523 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5524 | if (btrfs_root_refs(&root->root_item) == 0) |
5525 | return 1; | |
848cce0d | 5526 | |
4a0cc7ca | 5527 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5528 | return 1; |
efefb143 | 5529 | } |
76dda93c YZ |
5530 | return 0; |
5531 | } | |
5532 | ||
3de4586c | 5533 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5534 | unsigned int flags) |
3de4586c | 5535 | { |
3837d208 | 5536 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5537 | |
3837d208 AV |
5538 | if (inode == ERR_PTR(-ENOENT)) |
5539 | inode = NULL; | |
41d28bca | 5540 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5541 | } |
5542 | ||
23b5ec74 JB |
5543 | /* |
5544 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5545 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5546 | * our information into that, and then dir_emit from the buffer. This is | |
5547 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5548 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5549 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5550 | * tree lock. | |
5551 | */ | |
5552 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5553 | { | |
5554 | struct btrfs_file_private *private; | |
5555 | ||
5556 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5557 | if (!private) | |
5558 | return -ENOMEM; | |
5559 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5560 | if (!private->filldir_buf) { | |
5561 | kfree(private); | |
5562 | return -ENOMEM; | |
5563 | } | |
5564 | file->private_data = private; | |
5565 | return 0; | |
5566 | } | |
5567 | ||
5568 | struct dir_entry { | |
5569 | u64 ino; | |
5570 | u64 offset; | |
5571 | unsigned type; | |
5572 | int name_len; | |
5573 | }; | |
5574 | ||
5575 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5576 | { | |
5577 | while (entries--) { | |
5578 | struct dir_entry *entry = addr; | |
5579 | char *name = (char *)(entry + 1); | |
5580 | ||
92d32170 DS |
5581 | ctx->pos = get_unaligned(&entry->offset); |
5582 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5583 | get_unaligned(&entry->ino), | |
5584 | get_unaligned(&entry->type))) | |
23b5ec74 | 5585 | return 1; |
92d32170 DS |
5586 | addr += sizeof(struct dir_entry) + |
5587 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5588 | ctx->pos++; |
5589 | } | |
5590 | return 0; | |
5591 | } | |
5592 | ||
9cdda8d3 | 5593 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5594 | { |
9cdda8d3 | 5595 | struct inode *inode = file_inode(file); |
39279cc3 | 5596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5597 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5598 | struct btrfs_dir_item *di; |
5599 | struct btrfs_key key; | |
5f39d397 | 5600 | struct btrfs_key found_key; |
39279cc3 | 5601 | struct btrfs_path *path; |
23b5ec74 | 5602 | void *addr; |
16cdcec7 MX |
5603 | struct list_head ins_list; |
5604 | struct list_head del_list; | |
39279cc3 | 5605 | int ret; |
5f39d397 | 5606 | struct extent_buffer *leaf; |
39279cc3 | 5607 | int slot; |
5f39d397 CM |
5608 | char *name_ptr; |
5609 | int name_len; | |
23b5ec74 JB |
5610 | int entries = 0; |
5611 | int total_len = 0; | |
02dbfc99 | 5612 | bool put = false; |
c2951f32 | 5613 | struct btrfs_key location; |
5f39d397 | 5614 | |
9cdda8d3 AV |
5615 | if (!dir_emit_dots(file, ctx)) |
5616 | return 0; | |
5617 | ||
49593bfa | 5618 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5619 | if (!path) |
5620 | return -ENOMEM; | |
ff5714cc | 5621 | |
23b5ec74 | 5622 | addr = private->filldir_buf; |
e4058b54 | 5623 | path->reada = READA_FORWARD; |
49593bfa | 5624 | |
c2951f32 JM |
5625 | INIT_LIST_HEAD(&ins_list); |
5626 | INIT_LIST_HEAD(&del_list); | |
5627 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5628 | |
23b5ec74 | 5629 | again: |
c2951f32 | 5630 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5631 | key.offset = ctx->pos; |
4a0cc7ca | 5632 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5633 | |
39279cc3 CM |
5634 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5635 | if (ret < 0) | |
5636 | goto err; | |
49593bfa DW |
5637 | |
5638 | while (1) { | |
23b5ec74 JB |
5639 | struct dir_entry *entry; |
5640 | ||
5f39d397 | 5641 | leaf = path->nodes[0]; |
39279cc3 | 5642 | slot = path->slots[0]; |
b9e03af0 LZ |
5643 | if (slot >= btrfs_header_nritems(leaf)) { |
5644 | ret = btrfs_next_leaf(root, path); | |
5645 | if (ret < 0) | |
5646 | goto err; | |
5647 | else if (ret > 0) | |
5648 | break; | |
5649 | continue; | |
39279cc3 | 5650 | } |
3de4586c | 5651 | |
5f39d397 CM |
5652 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5653 | ||
5654 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5655 | break; |
c2951f32 | 5656 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5657 | break; |
9cdda8d3 | 5658 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5659 | goto next; |
c2951f32 | 5660 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5661 | goto next; |
39279cc3 | 5662 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 5663 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5664 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5665 | PAGE_SIZE) { | |
5666 | btrfs_release_path(path); | |
5667 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5668 | if (ret) | |
5669 | goto nopos; | |
5670 | addr = private->filldir_buf; | |
5671 | entries = 0; | |
5672 | total_len = 0; | |
5673 | goto again; | |
c2951f32 | 5674 | } |
23b5ec74 JB |
5675 | |
5676 | entry = addr; | |
92d32170 | 5677 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 5678 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
5679 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5680 | name_len); | |
7d157c3d | 5681 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
92d32170 | 5682 | &entry->type); |
c2951f32 | 5683 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5684 | put_unaligned(location.objectid, &entry->ino); |
5685 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5686 | entries++; |
5687 | addr += sizeof(struct dir_entry) + name_len; | |
5688 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5689 | next: |
5690 | path->slots[0]++; | |
39279cc3 | 5691 | } |
23b5ec74 JB |
5692 | btrfs_release_path(path); |
5693 | ||
5694 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5695 | if (ret) | |
5696 | goto nopos; | |
49593bfa | 5697 | |
d2fbb2b5 | 5698 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5699 | if (ret) |
bc4ef759 DS |
5700 | goto nopos; |
5701 | ||
db62efbb ZB |
5702 | /* |
5703 | * Stop new entries from being returned after we return the last | |
5704 | * entry. | |
5705 | * | |
5706 | * New directory entries are assigned a strictly increasing | |
5707 | * offset. This means that new entries created during readdir | |
5708 | * are *guaranteed* to be seen in the future by that readdir. | |
5709 | * This has broken buggy programs which operate on names as | |
5710 | * they're returned by readdir. Until we re-use freed offsets | |
5711 | * we have this hack to stop new entries from being returned | |
5712 | * under the assumption that they'll never reach this huge | |
5713 | * offset. | |
5714 | * | |
5715 | * This is being careful not to overflow 32bit loff_t unless the | |
5716 | * last entry requires it because doing so has broken 32bit apps | |
5717 | * in the past. | |
5718 | */ | |
c2951f32 JM |
5719 | if (ctx->pos >= INT_MAX) |
5720 | ctx->pos = LLONG_MAX; | |
5721 | else | |
5722 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5723 | nopos: |
5724 | ret = 0; | |
5725 | err: | |
02dbfc99 OS |
5726 | if (put) |
5727 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5728 | btrfs_free_path(path); |
39279cc3 CM |
5729 | return ret; |
5730 | } | |
5731 | ||
39279cc3 | 5732 | /* |
54aa1f4d | 5733 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5734 | * inode changes. But, it is most likely to find the inode in cache. |
5735 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5736 | * to keep or drop this code. | |
5737 | */ | |
48a3b636 | 5738 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 5739 | { |
2ff7e61e | 5740 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5742 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5743 | int ret; |
5744 | ||
72ac3c0d | 5745 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5746 | return 0; |
39279cc3 | 5747 | |
7a7eaa40 | 5748 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5749 | if (IS_ERR(trans)) |
5750 | return PTR_ERR(trans); | |
8929ecfa YZ |
5751 | |
5752 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5753 | if (ret && ret == -ENOSPC) { |
5754 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 5755 | btrfs_end_transaction(trans); |
94b60442 | 5756 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
5757 | if (IS_ERR(trans)) |
5758 | return PTR_ERR(trans); | |
8929ecfa | 5759 | |
94b60442 | 5760 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5761 | } |
3a45bb20 | 5762 | btrfs_end_transaction(trans); |
16cdcec7 | 5763 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 5764 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
5765 | |
5766 | return ret; | |
5767 | } | |
5768 | ||
5769 | /* | |
5770 | * This is a copy of file_update_time. We need this so we can return error on | |
5771 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5772 | */ | |
95582b00 | 5773 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 5774 | int flags) |
22c44fe6 | 5775 | { |
2bc55652 | 5776 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 5777 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
5778 | |
5779 | if (btrfs_root_readonly(root)) | |
5780 | return -EROFS; | |
5781 | ||
e41f941a | 5782 | if (flags & S_VERSION) |
3a8c7231 | 5783 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
5784 | if (flags & S_CTIME) |
5785 | inode->i_ctime = *now; | |
5786 | if (flags & S_MTIME) | |
5787 | inode->i_mtime = *now; | |
5788 | if (flags & S_ATIME) | |
5789 | inode->i_atime = *now; | |
3a8c7231 | 5790 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
5791 | } |
5792 | ||
d352ac68 CM |
5793 | /* |
5794 | * find the highest existing sequence number in a directory | |
5795 | * and then set the in-memory index_cnt variable to reflect | |
5796 | * free sequence numbers | |
5797 | */ | |
4c570655 | 5798 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 5799 | { |
4c570655 | 5800 | struct btrfs_root *root = inode->root; |
aec7477b JB |
5801 | struct btrfs_key key, found_key; |
5802 | struct btrfs_path *path; | |
5803 | struct extent_buffer *leaf; | |
5804 | int ret; | |
5805 | ||
4c570655 | 5806 | key.objectid = btrfs_ino(inode); |
962a298f | 5807 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5808 | key.offset = (u64)-1; |
5809 | ||
5810 | path = btrfs_alloc_path(); | |
5811 | if (!path) | |
5812 | return -ENOMEM; | |
5813 | ||
5814 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5815 | if (ret < 0) | |
5816 | goto out; | |
5817 | /* FIXME: we should be able to handle this */ | |
5818 | if (ret == 0) | |
5819 | goto out; | |
5820 | ret = 0; | |
5821 | ||
5822 | /* | |
5823 | * MAGIC NUMBER EXPLANATION: | |
5824 | * since we search a directory based on f_pos we have to start at 2 | |
5825 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5826 | * else has to start at 2 | |
5827 | */ | |
5828 | if (path->slots[0] == 0) { | |
4c570655 | 5829 | inode->index_cnt = 2; |
aec7477b JB |
5830 | goto out; |
5831 | } | |
5832 | ||
5833 | path->slots[0]--; | |
5834 | ||
5835 | leaf = path->nodes[0]; | |
5836 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5837 | ||
4c570655 | 5838 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5839 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 5840 | inode->index_cnt = 2; |
aec7477b JB |
5841 | goto out; |
5842 | } | |
5843 | ||
4c570655 | 5844 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
5845 | out: |
5846 | btrfs_free_path(path); | |
5847 | return ret; | |
5848 | } | |
5849 | ||
d352ac68 CM |
5850 | /* |
5851 | * helper to find a free sequence number in a given directory. This current | |
5852 | * code is very simple, later versions will do smarter things in the btree | |
5853 | */ | |
877574e2 | 5854 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
5855 | { |
5856 | int ret = 0; | |
5857 | ||
877574e2 NB |
5858 | if (dir->index_cnt == (u64)-1) { |
5859 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
5860 | if (ret) { |
5861 | ret = btrfs_set_inode_index_count(dir); | |
5862 | if (ret) | |
5863 | return ret; | |
5864 | } | |
aec7477b JB |
5865 | } |
5866 | ||
877574e2 NB |
5867 | *index = dir->index_cnt; |
5868 | dir->index_cnt++; | |
aec7477b JB |
5869 | |
5870 | return ret; | |
5871 | } | |
5872 | ||
b0d5d10f CM |
5873 | static int btrfs_insert_inode_locked(struct inode *inode) |
5874 | { | |
5875 | struct btrfs_iget_args args; | |
0202e83f DS |
5876 | |
5877 | args.ino = BTRFS_I(inode)->location.objectid; | |
b0d5d10f CM |
5878 | args.root = BTRFS_I(inode)->root; |
5879 | ||
5880 | return insert_inode_locked4(inode, | |
5881 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5882 | btrfs_find_actor, &args); | |
5883 | } | |
5884 | ||
19aee8de AJ |
5885 | /* |
5886 | * Inherit flags from the parent inode. | |
5887 | * | |
5888 | * Currently only the compression flags and the cow flags are inherited. | |
5889 | */ | |
5890 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
5891 | { | |
5892 | unsigned int flags; | |
5893 | ||
5894 | if (!dir) | |
5895 | return; | |
5896 | ||
5897 | flags = BTRFS_I(dir)->flags; | |
5898 | ||
5899 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
5900 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
5901 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
5902 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
5903 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
5904 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
5905 | } | |
5906 | ||
5907 | if (flags & BTRFS_INODE_NODATACOW) { | |
5908 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
5909 | if (S_ISREG(inode->i_mode)) | |
5910 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
5911 | } | |
5912 | ||
7b6a221e | 5913 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
5914 | } |
5915 | ||
39279cc3 CM |
5916 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
5917 | struct btrfs_root *root, | |
aec7477b | 5918 | struct inode *dir, |
9c58309d | 5919 | const char *name, int name_len, |
175a4eb7 AV |
5920 | u64 ref_objectid, u64 objectid, |
5921 | umode_t mode, u64 *index) | |
39279cc3 | 5922 | { |
0b246afa | 5923 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 5924 | struct inode *inode; |
5f39d397 | 5925 | struct btrfs_inode_item *inode_item; |
39279cc3 | 5926 | struct btrfs_key *location; |
5f39d397 | 5927 | struct btrfs_path *path; |
9c58309d CM |
5928 | struct btrfs_inode_ref *ref; |
5929 | struct btrfs_key key[2]; | |
5930 | u32 sizes[2]; | |
ef3b9af5 | 5931 | int nitems = name ? 2 : 1; |
9c58309d | 5932 | unsigned long ptr; |
11a19a90 | 5933 | unsigned int nofs_flag; |
39279cc3 | 5934 | int ret; |
39279cc3 | 5935 | |
5f39d397 | 5936 | path = btrfs_alloc_path(); |
d8926bb3 MF |
5937 | if (!path) |
5938 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 5939 | |
11a19a90 | 5940 | nofs_flag = memalloc_nofs_save(); |
0b246afa | 5941 | inode = new_inode(fs_info->sb); |
11a19a90 | 5942 | memalloc_nofs_restore(nofs_flag); |
8fb27640 YS |
5943 | if (!inode) { |
5944 | btrfs_free_path(path); | |
39279cc3 | 5945 | return ERR_PTR(-ENOMEM); |
8fb27640 | 5946 | } |
39279cc3 | 5947 | |
5762b5c9 FM |
5948 | /* |
5949 | * O_TMPFILE, set link count to 0, so that after this point, | |
5950 | * we fill in an inode item with the correct link count. | |
5951 | */ | |
5952 | if (!name) | |
5953 | set_nlink(inode, 0); | |
5954 | ||
581bb050 LZ |
5955 | /* |
5956 | * we have to initialize this early, so we can reclaim the inode | |
5957 | * number if we fail afterwards in this function. | |
5958 | */ | |
5959 | inode->i_ino = objectid; | |
5960 | ||
ef3b9af5 | 5961 | if (dir && name) { |
1abe9b8a | 5962 | trace_btrfs_inode_request(dir); |
5963 | ||
877574e2 | 5964 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 5965 | if (ret) { |
8fb27640 | 5966 | btrfs_free_path(path); |
09771430 | 5967 | iput(inode); |
aec7477b | 5968 | return ERR_PTR(ret); |
09771430 | 5969 | } |
ef3b9af5 FM |
5970 | } else if (dir) { |
5971 | *index = 0; | |
aec7477b JB |
5972 | } |
5973 | /* | |
5974 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 5975 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
5976 | * number |
5977 | */ | |
5978 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 5979 | BTRFS_I(inode)->dir_index = *index; |
5c8fd99f | 5980 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
e02119d5 | 5981 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 5982 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 5983 | |
5dc562c5 JB |
5984 | /* |
5985 | * We could have gotten an inode number from somebody who was fsynced | |
5986 | * and then removed in this same transaction, so let's just set full | |
5987 | * sync since it will be a full sync anyway and this will blow away the | |
5988 | * old info in the log. | |
5989 | */ | |
5990 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
5991 | ||
9c58309d | 5992 | key[0].objectid = objectid; |
962a298f | 5993 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
5994 | key[0].offset = 0; |
5995 | ||
9c58309d | 5996 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
5997 | |
5998 | if (name) { | |
5999 | /* | |
6000 | * Start new inodes with an inode_ref. This is slightly more | |
6001 | * efficient for small numbers of hard links since they will | |
6002 | * be packed into one item. Extended refs will kick in if we | |
6003 | * add more hard links than can fit in the ref item. | |
6004 | */ | |
6005 | key[1].objectid = objectid; | |
962a298f | 6006 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6007 | key[1].offset = ref_objectid; |
6008 | ||
6009 | sizes[1] = name_len + sizeof(*ref); | |
6010 | } | |
9c58309d | 6011 | |
b0d5d10f CM |
6012 | location = &BTRFS_I(inode)->location; |
6013 | location->objectid = objectid; | |
6014 | location->offset = 0; | |
962a298f | 6015 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6016 | |
6017 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6018 | if (ret < 0) { |
6019 | iput(inode); | |
b0d5d10f | 6020 | goto fail; |
32955c54 | 6021 | } |
b0d5d10f | 6022 | |
b9473439 | 6023 | path->leave_spinning = 1; |
ef3b9af5 | 6024 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6025 | if (ret != 0) |
b0d5d10f | 6026 | goto fail_unlock; |
5f39d397 | 6027 | |
ecc11fab | 6028 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6029 | inode_set_bytes(inode, 0); |
9cc97d64 | 6030 | |
c2050a45 | 6031 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6032 | inode->i_atime = inode->i_mtime; |
6033 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6034 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6035 | |
5f39d397 CM |
6036 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6037 | struct btrfs_inode_item); | |
b159fa28 | 6038 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6039 | sizeof(*inode_item)); |
e02119d5 | 6040 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6041 | |
ef3b9af5 FM |
6042 | if (name) { |
6043 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6044 | struct btrfs_inode_ref); | |
6045 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6046 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6047 | ptr = (unsigned long)(ref + 1); | |
6048 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6049 | } | |
9c58309d | 6050 | |
5f39d397 CM |
6051 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6052 | btrfs_free_path(path); | |
6053 | ||
6cbff00f CH |
6054 | btrfs_inherit_iflags(inode, dir); |
6055 | ||
569254b0 | 6056 | if (S_ISREG(mode)) { |
0b246afa | 6057 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6058 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6059 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6060 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6061 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6062 | } |
6063 | ||
5d4f98a2 | 6064 | inode_tree_add(inode); |
1abe9b8a | 6065 | |
6066 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6067 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6068 | |
8ea05e3a AB |
6069 | btrfs_update_root_times(trans, root); |
6070 | ||
63541927 FDBM |
6071 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6072 | if (ret) | |
0b246afa | 6073 | btrfs_err(fs_info, |
63541927 | 6074 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6075 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6076 | |
39279cc3 | 6077 | return inode; |
b0d5d10f CM |
6078 | |
6079 | fail_unlock: | |
32955c54 | 6080 | discard_new_inode(inode); |
5f39d397 | 6081 | fail: |
ef3b9af5 | 6082 | if (dir && name) |
aec7477b | 6083 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6084 | btrfs_free_path(path); |
6085 | return ERR_PTR(ret); | |
39279cc3 CM |
6086 | } |
6087 | ||
d352ac68 CM |
6088 | /* |
6089 | * utility function to add 'inode' into 'parent_inode' with | |
6090 | * a give name and a given sequence number. | |
6091 | * if 'add_backref' is true, also insert a backref from the | |
6092 | * inode to the parent directory. | |
6093 | */ | |
e02119d5 | 6094 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6095 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6096 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6097 | { |
4df27c4d | 6098 | int ret = 0; |
39279cc3 | 6099 | struct btrfs_key key; |
db0a669f NB |
6100 | struct btrfs_root *root = parent_inode->root; |
6101 | u64 ino = btrfs_ino(inode); | |
6102 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6103 | |
33345d01 | 6104 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6105 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6106 | } else { |
33345d01 | 6107 | key.objectid = ino; |
962a298f | 6108 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6109 | key.offset = 0; |
6110 | } | |
6111 | ||
33345d01 | 6112 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6113 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6114 | root->root_key.objectid, parent_ino, |
6115 | index, name, name_len); | |
4df27c4d | 6116 | } else if (add_backref) { |
33345d01 LZ |
6117 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6118 | parent_ino, index); | |
4df27c4d | 6119 | } |
39279cc3 | 6120 | |
79787eaa JM |
6121 | /* Nothing to clean up yet */ |
6122 | if (ret) | |
6123 | return ret; | |
4df27c4d | 6124 | |
684572df | 6125 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6126 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6127 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6128 | goto fail_dir_item; |
6129 | else if (ret) { | |
66642832 | 6130 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6131 | return ret; |
39279cc3 | 6132 | } |
79787eaa | 6133 | |
db0a669f | 6134 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6135 | name_len * 2); |
db0a669f | 6136 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6137 | /* |
6138 | * If we are replaying a log tree, we do not want to update the mtime | |
6139 | * and ctime of the parent directory with the current time, since the | |
6140 | * log replay procedure is responsible for setting them to their correct | |
6141 | * values (the ones it had when the fsync was done). | |
6142 | */ | |
6143 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6144 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6145 | ||
6146 | parent_inode->vfs_inode.i_mtime = now; | |
6147 | parent_inode->vfs_inode.i_ctime = now; | |
6148 | } | |
db0a669f | 6149 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
79787eaa | 6150 | if (ret) |
66642832 | 6151 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6152 | return ret; |
fe66a05a CM |
6153 | |
6154 | fail_dir_item: | |
6155 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6156 | u64 local_index; | |
6157 | int err; | |
3ee1c553 | 6158 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6159 | root->root_key.objectid, parent_ino, |
6160 | &local_index, name, name_len); | |
1690dd41 JT |
6161 | if (err) |
6162 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6163 | } else if (add_backref) { |
6164 | u64 local_index; | |
6165 | int err; | |
6166 | ||
6167 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6168 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6169 | if (err) |
6170 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6171 | } |
1690dd41 JT |
6172 | |
6173 | /* Return the original error code */ | |
fe66a05a | 6174 | return ret; |
39279cc3 CM |
6175 | } |
6176 | ||
6177 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6178 | struct btrfs_inode *dir, struct dentry *dentry, |
6179 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6180 | { |
a1b075d2 JB |
6181 | int err = btrfs_add_link(trans, dir, inode, |
6182 | dentry->d_name.name, dentry->d_name.len, | |
6183 | backref, index); | |
39279cc3 CM |
6184 | if (err > 0) |
6185 | err = -EEXIST; | |
6186 | return err; | |
6187 | } | |
6188 | ||
618e21d5 | 6189 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6190 | umode_t mode, dev_t rdev) |
618e21d5 | 6191 | { |
2ff7e61e | 6192 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6193 | struct btrfs_trans_handle *trans; |
6194 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6195 | struct inode *inode = NULL; |
618e21d5 | 6196 | int err; |
618e21d5 | 6197 | u64 objectid; |
00e4e6b3 | 6198 | u64 index = 0; |
618e21d5 | 6199 | |
9ed74f2d JB |
6200 | /* |
6201 | * 2 for inode item and ref | |
6202 | * 2 for dir items | |
6203 | * 1 for xattr if selinux is on | |
6204 | */ | |
a22285a6 YZ |
6205 | trans = btrfs_start_transaction(root, 5); |
6206 | if (IS_ERR(trans)) | |
6207 | return PTR_ERR(trans); | |
1832a6d5 | 6208 | |
581bb050 LZ |
6209 | err = btrfs_find_free_ino(root, &objectid); |
6210 | if (err) | |
6211 | goto out_unlock; | |
6212 | ||
aec7477b | 6213 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6214 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6215 | mode, &index); | |
7cf96da3 TI |
6216 | if (IS_ERR(inode)) { |
6217 | err = PTR_ERR(inode); | |
32955c54 | 6218 | inode = NULL; |
618e21d5 | 6219 | goto out_unlock; |
7cf96da3 | 6220 | } |
618e21d5 | 6221 | |
ad19db71 CS |
6222 | /* |
6223 | * If the active LSM wants to access the inode during | |
6224 | * d_instantiate it needs these. Smack checks to see | |
6225 | * if the filesystem supports xattrs by looking at the | |
6226 | * ops vector. | |
6227 | */ | |
ad19db71 | 6228 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6229 | init_special_inode(inode, inode->i_mode, rdev); |
6230 | ||
6231 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6232 | if (err) |
32955c54 | 6233 | goto out_unlock; |
b0d5d10f | 6234 | |
cef415af NB |
6235 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6236 | 0, index); | |
32955c54 AV |
6237 | if (err) |
6238 | goto out_unlock; | |
6239 | ||
6240 | btrfs_update_inode(trans, root, inode); | |
6241 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6242 | |
618e21d5 | 6243 | out_unlock: |
3a45bb20 | 6244 | btrfs_end_transaction(trans); |
2ff7e61e | 6245 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6246 | if (err && inode) { |
618e21d5 | 6247 | inode_dec_link_count(inode); |
32955c54 | 6248 | discard_new_inode(inode); |
618e21d5 | 6249 | } |
618e21d5 JB |
6250 | return err; |
6251 | } | |
6252 | ||
39279cc3 | 6253 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6254 | umode_t mode, bool excl) |
39279cc3 | 6255 | { |
2ff7e61e | 6256 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6257 | struct btrfs_trans_handle *trans; |
6258 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6259 | struct inode *inode = NULL; |
a22285a6 | 6260 | int err; |
39279cc3 | 6261 | u64 objectid; |
00e4e6b3 | 6262 | u64 index = 0; |
39279cc3 | 6263 | |
9ed74f2d JB |
6264 | /* |
6265 | * 2 for inode item and ref | |
6266 | * 2 for dir items | |
6267 | * 1 for xattr if selinux is on | |
6268 | */ | |
a22285a6 YZ |
6269 | trans = btrfs_start_transaction(root, 5); |
6270 | if (IS_ERR(trans)) | |
6271 | return PTR_ERR(trans); | |
9ed74f2d | 6272 | |
581bb050 LZ |
6273 | err = btrfs_find_free_ino(root, &objectid); |
6274 | if (err) | |
6275 | goto out_unlock; | |
6276 | ||
aec7477b | 6277 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6278 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6279 | mode, &index); | |
7cf96da3 TI |
6280 | if (IS_ERR(inode)) { |
6281 | err = PTR_ERR(inode); | |
32955c54 | 6282 | inode = NULL; |
39279cc3 | 6283 | goto out_unlock; |
7cf96da3 | 6284 | } |
ad19db71 CS |
6285 | /* |
6286 | * If the active LSM wants to access the inode during | |
6287 | * d_instantiate it needs these. Smack checks to see | |
6288 | * if the filesystem supports xattrs by looking at the | |
6289 | * ops vector. | |
6290 | */ | |
6291 | inode->i_fop = &btrfs_file_operations; | |
6292 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6293 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6294 | |
6295 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6296 | if (err) | |
32955c54 | 6297 | goto out_unlock; |
b0d5d10f CM |
6298 | |
6299 | err = btrfs_update_inode(trans, root, inode); | |
6300 | if (err) | |
32955c54 | 6301 | goto out_unlock; |
ad19db71 | 6302 | |
cef415af NB |
6303 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6304 | 0, index); | |
39279cc3 | 6305 | if (err) |
32955c54 | 6306 | goto out_unlock; |
43baa579 | 6307 | |
43baa579 | 6308 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
1e2e547a | 6309 | d_instantiate_new(dentry, inode); |
43baa579 | 6310 | |
39279cc3 | 6311 | out_unlock: |
3a45bb20 | 6312 | btrfs_end_transaction(trans); |
32955c54 | 6313 | if (err && inode) { |
39279cc3 | 6314 | inode_dec_link_count(inode); |
32955c54 | 6315 | discard_new_inode(inode); |
39279cc3 | 6316 | } |
2ff7e61e | 6317 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6318 | return err; |
6319 | } | |
6320 | ||
6321 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6322 | struct dentry *dentry) | |
6323 | { | |
271dba45 | 6324 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6325 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6326 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6327 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6328 | u64 index; |
39279cc3 CM |
6329 | int err; |
6330 | int drop_inode = 0; | |
6331 | ||
4a8be425 | 6332 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6333 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6334 | return -EXDEV; |
4a8be425 | 6335 | |
f186373f | 6336 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6337 | return -EMLINK; |
4a8be425 | 6338 | |
877574e2 | 6339 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6340 | if (err) |
6341 | goto fail; | |
6342 | ||
a22285a6 | 6343 | /* |
7e6b6465 | 6344 | * 2 items for inode and inode ref |
a22285a6 | 6345 | * 2 items for dir items |
7e6b6465 | 6346 | * 1 item for parent inode |
399b0bbf | 6347 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6348 | */ |
399b0bbf | 6349 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6350 | if (IS_ERR(trans)) { |
6351 | err = PTR_ERR(trans); | |
271dba45 | 6352 | trans = NULL; |
a22285a6 YZ |
6353 | goto fail; |
6354 | } | |
5f39d397 | 6355 | |
67de1176 MX |
6356 | /* There are several dir indexes for this inode, clear the cache. */ |
6357 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6358 | inc_nlink(inode); |
0c4d2d95 | 6359 | inode_inc_iversion(inode); |
c2050a45 | 6360 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6361 | ihold(inode); |
e9976151 | 6362 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6363 | |
cef415af NB |
6364 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6365 | 1, index); | |
5f39d397 | 6366 | |
a5719521 | 6367 | if (err) { |
54aa1f4d | 6368 | drop_inode = 1; |
a5719521 | 6369 | } else { |
10d9f309 | 6370 | struct dentry *parent = dentry->d_parent; |
d4682ba0 FM |
6371 | int ret; |
6372 | ||
a5719521 | 6373 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6374 | if (err) |
6375 | goto fail; | |
ef3b9af5 FM |
6376 | if (inode->i_nlink == 1) { |
6377 | /* | |
6378 | * If new hard link count is 1, it's a file created | |
6379 | * with open(2) O_TMPFILE flag. | |
6380 | */ | |
3d6ae7bb | 6381 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6382 | if (err) |
6383 | goto fail; | |
6384 | } | |
08c422c2 | 6385 | d_instantiate(dentry, inode); |
d4682ba0 FM |
6386 | ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent, |
6387 | true, NULL); | |
6388 | if (ret == BTRFS_NEED_TRANS_COMMIT) { | |
6389 | err = btrfs_commit_transaction(trans); | |
6390 | trans = NULL; | |
6391 | } | |
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; |
a52d9a80 CM |
6537 | int ret; |
6538 | int err = 0; | |
a52d9a80 CM |
6539 | u64 extent_start = 0; |
6540 | u64 extent_end = 0; | |
fc4f21b1 | 6541 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6542 | int extent_type = -1; |
f421950f | 6543 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6544 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6545 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6546 | struct extent_buffer *leaf; |
6547 | struct btrfs_key found_key; | |
a52d9a80 | 6548 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6549 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6550 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6551 | |
890871be | 6552 | read_lock(&em_tree->lock); |
d1310b2e | 6553 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6554 | read_unlock(&em_tree->lock); |
d1310b2e | 6555 | |
a52d9a80 | 6556 | if (em) { |
e1c4b745 CM |
6557 | if (em->start > start || em->start + em->len <= start) |
6558 | free_extent_map(em); | |
6559 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6560 | free_extent_map(em); |
6561 | else | |
6562 | goto out; | |
a52d9a80 | 6563 | } |
172ddd60 | 6564 | em = alloc_extent_map(); |
a52d9a80 | 6565 | if (!em) { |
d1310b2e CM |
6566 | err = -ENOMEM; |
6567 | goto out; | |
a52d9a80 | 6568 | } |
d1310b2e | 6569 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6570 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6571 | em->len = (u64)-1; |
c8b97818 | 6572 | em->block_len = (u64)-1; |
f421950f | 6573 | |
bee6ec82 | 6574 | path = btrfs_alloc_path(); |
f421950f | 6575 | if (!path) { |
bee6ec82 LB |
6576 | err = -ENOMEM; |
6577 | goto out; | |
f421950f CM |
6578 | } |
6579 | ||
bee6ec82 LB |
6580 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6581 | path->reada = READA_FORWARD; | |
6582 | ||
e49aabd9 LB |
6583 | /* |
6584 | * Unless we're going to uncompress the inline extent, no sleep would | |
6585 | * happen. | |
6586 | */ | |
6587 | path->leave_spinning = 1; | |
6588 | ||
5c9a702e | 6589 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 CM |
6590 | if (ret < 0) { |
6591 | err = ret; | |
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]--; | |
6597 | } | |
6598 | ||
5f39d397 CM |
6599 | leaf = path->nodes[0]; |
6600 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6601 | struct btrfs_file_extent_item); |
5f39d397 | 6602 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6603 | if (found_key.objectid != objectid || |
694c12ed | 6604 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6605 | /* |
6606 | * If we backup past the first extent we want to move forward | |
6607 | * and see if there is an extent in front of us, otherwise we'll | |
6608 | * say there is a hole for our whole search range which can | |
6609 | * cause problems. | |
6610 | */ | |
6611 | extent_end = start; | |
6612 | goto next; | |
a52d9a80 CM |
6613 | } |
6614 | ||
694c12ed | 6615 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6616 | extent_start = found_key.offset; |
a5eeb3d1 | 6617 | extent_end = btrfs_file_extent_end(path); |
694c12ed NB |
6618 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6619 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
6620 | /* Only regular file could have regular/prealloc extent */ |
6621 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
6622 | ret = -EUCLEAN; | |
6623 | btrfs_crit(fs_info, | |
6624 | "regular/prealloc extent found for non-regular inode %llu", | |
6625 | btrfs_ino(inode)); | |
6626 | goto out; | |
6627 | } | |
09ed2f16 LB |
6628 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
6629 | extent_start); | |
694c12ed | 6630 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
09ed2f16 LB |
6631 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
6632 | path->slots[0], | |
6633 | extent_start); | |
9036c102 | 6634 | } |
25a50341 | 6635 | next: |
9036c102 YZ |
6636 | if (start >= extent_end) { |
6637 | path->slots[0]++; | |
6638 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6639 | ret = btrfs_next_leaf(root, path); | |
6640 | if (ret < 0) { | |
6641 | err = ret; | |
6642 | goto out; | |
b8eeab7f | 6643 | } else if (ret > 0) { |
9036c102 | 6644 | goto not_found; |
b8eeab7f | 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); |
166ae5a4 ZB |
6696 | if (ret) { |
6697 | err = ret; | |
6698 | goto out; | |
6699 | } | |
c8b97818 CM |
6700 | } else { |
6701 | map = kmap(page); | |
6702 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6703 | copy_size); | |
09cbfeaf | 6704 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6705 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6706 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6707 | copy_size); |
6708 | } | |
c8b97818 CM |
6709 | kunmap(page); |
6710 | } | |
179e29e4 | 6711 | flush_dcache_page(page); |
a52d9a80 | 6712 | } |
d1310b2e | 6713 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6714 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6715 | goto insert; |
a52d9a80 CM |
6716 | } |
6717 | not_found: | |
6718 | em->start = start; | |
70c8a91c | 6719 | em->orig_start = start; |
d1310b2e | 6720 | em->len = len; |
5f39d397 | 6721 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6722 | insert: |
b3b4aa74 | 6723 | btrfs_release_path(path); |
d1310b2e | 6724 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6725 | btrfs_err(fs_info, |
5d163e0e JM |
6726 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6727 | em->start, em->len, start, len); | |
a52d9a80 CM |
6728 | err = -EIO; |
6729 | goto out; | |
6730 | } | |
d1310b2e CM |
6731 | |
6732 | err = 0; | |
890871be | 6733 | write_lock(&em_tree->lock); |
f46b24c9 | 6734 | err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6735 | write_unlock(&em_tree->lock); |
a52d9a80 | 6736 | out: |
c6414280 | 6737 | btrfs_free_path(path); |
1abe9b8a | 6738 | |
fc4f21b1 | 6739 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6740 | |
a52d9a80 CM |
6741 | if (err) { |
6742 | free_extent_map(em); | |
a52d9a80 CM |
6743 | return ERR_PTR(err); |
6744 | } | |
79787eaa | 6745 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6746 | return em; |
6747 | } | |
6748 | ||
fc4f21b1 | 6749 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 6750 | u64 start, u64 len) |
ec29ed5b CM |
6751 | { |
6752 | struct extent_map *em; | |
6753 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 6754 | u64 delalloc_start = start; |
ec29ed5b | 6755 | u64 end; |
f3714ef4 NB |
6756 | u64 delalloc_len; |
6757 | u64 delalloc_end; | |
ec29ed5b CM |
6758 | int err = 0; |
6759 | ||
39b07b5d | 6760 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
ec29ed5b CM |
6761 | if (IS_ERR(em)) |
6762 | return em; | |
9986277e DC |
6763 | /* |
6764 | * If our em maps to: | |
6765 | * - a hole or | |
6766 | * - a pre-alloc extent, | |
6767 | * there might actually be delalloc bytes behind it. | |
6768 | */ | |
6769 | if (em->block_start != EXTENT_MAP_HOLE && | |
6770 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6771 | return em; | |
6772 | else | |
6773 | hole_em = em; | |
ec29ed5b CM |
6774 | |
6775 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6776 | end = start + len; | |
6777 | if (end < start) | |
6778 | end = (u64)-1; | |
6779 | else | |
6780 | end -= 1; | |
6781 | ||
6782 | em = NULL; | |
6783 | ||
6784 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 6785 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 6786 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
6787 | delalloc_end = delalloc_start + delalloc_len; |
6788 | if (delalloc_end < delalloc_start) | |
6789 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
6790 | |
6791 | /* | |
f3714ef4 NB |
6792 | * We didn't find anything useful, return the original results from |
6793 | * get_extent() | |
ec29ed5b | 6794 | */ |
f3714ef4 | 6795 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
6796 | em = hole_em; |
6797 | hole_em = NULL; | |
6798 | goto out; | |
6799 | } | |
6800 | ||
f3714ef4 NB |
6801 | /* |
6802 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
6803 | * the start they passed in | |
ec29ed5b | 6804 | */ |
f3714ef4 NB |
6805 | delalloc_start = max(start, delalloc_start); |
6806 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 6807 | |
f3714ef4 NB |
6808 | if (delalloc_len > 0) { |
6809 | u64 hole_start; | |
02950af4 | 6810 | u64 hole_len; |
f3714ef4 | 6811 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 6812 | |
172ddd60 | 6813 | em = alloc_extent_map(); |
ec29ed5b CM |
6814 | if (!em) { |
6815 | err = -ENOMEM; | |
6816 | goto out; | |
6817 | } | |
f3714ef4 NB |
6818 | |
6819 | ASSERT(hole_em); | |
ec29ed5b | 6820 | /* |
f3714ef4 NB |
6821 | * When btrfs_get_extent can't find anything it returns one |
6822 | * huge hole | |
ec29ed5b | 6823 | * |
f3714ef4 NB |
6824 | * Make sure what it found really fits our range, and adjust to |
6825 | * make sure it is based on the start from the caller | |
ec29ed5b | 6826 | */ |
f3714ef4 NB |
6827 | if (hole_end <= start || hole_em->start > end) { |
6828 | free_extent_map(hole_em); | |
6829 | hole_em = NULL; | |
6830 | } else { | |
6831 | hole_start = max(hole_em->start, start); | |
6832 | hole_len = hole_end - hole_start; | |
ec29ed5b | 6833 | } |
f3714ef4 NB |
6834 | |
6835 | if (hole_em && delalloc_start > hole_start) { | |
6836 | /* | |
6837 | * Our hole starts before our delalloc, so we have to | |
6838 | * return just the parts of the hole that go until the | |
6839 | * delalloc starts | |
ec29ed5b | 6840 | */ |
f3714ef4 | 6841 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
6842 | em->start = hole_start; |
6843 | em->orig_start = hole_start; | |
6844 | /* | |
f3714ef4 NB |
6845 | * Don't adjust block start at all, it is fixed at |
6846 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
6847 | */ |
6848 | em->block_start = hole_em->block_start; | |
6849 | em->block_len = hole_len; | |
f9e4fb53 LB |
6850 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6851 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 6852 | } else { |
f3714ef4 NB |
6853 | /* |
6854 | * Hole is out of passed range or it starts after | |
6855 | * delalloc range | |
6856 | */ | |
6857 | em->start = delalloc_start; | |
6858 | em->len = delalloc_len; | |
6859 | em->orig_start = delalloc_start; | |
ec29ed5b | 6860 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 6861 | em->block_len = delalloc_len; |
ec29ed5b | 6862 | } |
bf8d32b9 | 6863 | } else { |
ec29ed5b CM |
6864 | return hole_em; |
6865 | } | |
6866 | out: | |
6867 | ||
6868 | free_extent_map(hole_em); | |
6869 | if (err) { | |
6870 | free_extent_map(em); | |
6871 | return ERR_PTR(err); | |
6872 | } | |
6873 | return em; | |
6874 | } | |
6875 | ||
5f9a8a51 FM |
6876 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
6877 | const u64 start, | |
6878 | const u64 len, | |
6879 | const u64 orig_start, | |
6880 | const u64 block_start, | |
6881 | const u64 block_len, | |
6882 | const u64 orig_block_len, | |
6883 | const u64 ram_bytes, | |
6884 | const int type) | |
6885 | { | |
6886 | struct extent_map *em = NULL; | |
6887 | int ret; | |
6888 | ||
5f9a8a51 | 6889 | if (type != BTRFS_ORDERED_NOCOW) { |
4b67c11d | 6890 | em = create_io_em(BTRFS_I(inode), start, len, orig_start, |
6f9994db LB |
6891 | block_start, block_len, orig_block_len, |
6892 | ram_bytes, | |
6893 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
6894 | type); | |
5f9a8a51 FM |
6895 | if (IS_ERR(em)) |
6896 | goto out; | |
6897 | } | |
6898 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
6899 | len, block_len, type); | |
6900 | if (ret) { | |
6901 | if (em) { | |
6902 | free_extent_map(em); | |
dcdbc059 | 6903 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
6904 | start + len - 1, 0); |
6905 | } | |
6906 | em = ERR_PTR(ret); | |
6907 | } | |
6908 | out: | |
5f9a8a51 FM |
6909 | |
6910 | return em; | |
6911 | } | |
6912 | ||
4b46fce2 JB |
6913 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
6914 | u64 start, u64 len) | |
6915 | { | |
0b246afa | 6916 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 6917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 6918 | struct extent_map *em; |
4b46fce2 JB |
6919 | struct btrfs_key ins; |
6920 | u64 alloc_hint; | |
6921 | int ret; | |
4b46fce2 | 6922 | |
43c69849 | 6923 | alloc_hint = get_extent_allocation_hint(BTRFS_I(inode), start, len); |
0b246afa | 6924 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 6925 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
6926 | if (ret) |
6927 | return ERR_PTR(ret); | |
4b46fce2 | 6928 | |
5f9a8a51 FM |
6929 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
6930 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 6931 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 6932 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 6933 | if (IS_ERR(em)) |
2ff7e61e JM |
6934 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
6935 | ins.offset, 1); | |
de0ee0ed | 6936 | |
4b46fce2 JB |
6937 | return em; |
6938 | } | |
6939 | ||
46bfbb5c | 6940 | /* |
e4ecaf90 QW |
6941 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
6942 | * | |
6943 | * @offset: File offset | |
6944 | * @len: The length to write, will be updated to the nocow writeable | |
6945 | * range | |
6946 | * @orig_start: (optional) Return the original file offset of the file extent | |
6947 | * @orig_len: (optional) Return the original on-disk length of the file extent | |
6948 | * @ram_bytes: (optional) Return the ram_bytes of the file extent | |
6949 | * | |
6950 | * This function will flush ordered extents in the range to ensure proper | |
6951 | * nocow checks for (nowait == false) case. | |
6952 | * | |
6953 | * Return: | |
6954 | * >0 and update @len if we can do nocow write | |
6955 | * 0 if we can't do nocow write | |
6956 | * <0 if error happened | |
6957 | * | |
6958 | * NOTE: This only checks the file extents, caller is responsible to wait for | |
6959 | * any ordered extents. | |
46bfbb5c | 6960 | */ |
00361589 | 6961 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
6962 | u64 *orig_start, u64 *orig_block_len, |
6963 | u64 *ram_bytes) | |
46bfbb5c | 6964 | { |
2ff7e61e | 6965 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
6966 | struct btrfs_path *path; |
6967 | int ret; | |
6968 | struct extent_buffer *leaf; | |
6969 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 6970 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
6971 | struct btrfs_file_extent_item *fi; |
6972 | struct btrfs_key key; | |
6973 | u64 disk_bytenr; | |
6974 | u64 backref_offset; | |
6975 | u64 extent_end; | |
6976 | u64 num_bytes; | |
6977 | int slot; | |
6978 | int found_type; | |
7ee9e440 | 6979 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 6980 | |
46bfbb5c CM |
6981 | path = btrfs_alloc_path(); |
6982 | if (!path) | |
6983 | return -ENOMEM; | |
6984 | ||
f85b7379 DS |
6985 | ret = btrfs_lookup_file_extent(NULL, root, path, |
6986 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
6987 | if (ret < 0) |
6988 | goto out; | |
6989 | ||
6990 | slot = path->slots[0]; | |
6991 | if (ret == 1) { | |
6992 | if (slot == 0) { | |
6993 | /* can't find the item, must cow */ | |
6994 | ret = 0; | |
6995 | goto out; | |
6996 | } | |
6997 | slot--; | |
6998 | } | |
6999 | ret = 0; | |
7000 | leaf = path->nodes[0]; | |
7001 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7002 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7003 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7004 | /* not our file or wrong item type, must cow */ | |
7005 | goto out; | |
7006 | } | |
7007 | ||
7008 | if (key.offset > offset) { | |
7009 | /* Wrong offset, must cow */ | |
7010 | goto out; | |
7011 | } | |
7012 | ||
7013 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7014 | found_type = btrfs_file_extent_type(leaf, fi); | |
7015 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7016 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7017 | /* not a regular extent, must cow */ | |
7018 | goto out; | |
7019 | } | |
7ee9e440 JB |
7020 | |
7021 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7022 | goto out; | |
7023 | ||
e77751aa MX |
7024 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7025 | if (extent_end <= offset) | |
7026 | goto out; | |
7027 | ||
46bfbb5c | 7028 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7029 | if (disk_bytenr == 0) |
7030 | goto out; | |
7031 | ||
7032 | if (btrfs_file_extent_compression(leaf, fi) || | |
7033 | btrfs_file_extent_encryption(leaf, fi) || | |
7034 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7035 | goto out; | |
7036 | ||
78d4295b EL |
7037 | /* |
7038 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7039 | * unnecessary search. | |
7040 | */ | |
7041 | if (btrfs_file_extent_generation(leaf, fi) <= | |
7042 | btrfs_root_last_snapshot(&root->root_item)) | |
7043 | goto out; | |
7044 | ||
46bfbb5c CM |
7045 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7046 | ||
7ee9e440 JB |
7047 | if (orig_start) { |
7048 | *orig_start = key.offset - backref_offset; | |
7049 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7050 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7051 | } | |
eb384b55 | 7052 | |
2ff7e61e | 7053 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7054 | goto out; |
7b2b7085 MX |
7055 | |
7056 | num_bytes = min(offset + *len, extent_end) - offset; | |
7057 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7058 | u64 range_end; | |
7059 | ||
da17066c JM |
7060 | range_end = round_up(offset + num_bytes, |
7061 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7062 | ret = test_range_bit(io_tree, offset, range_end, |
7063 | EXTENT_DELALLOC, 0, NULL); | |
7064 | if (ret) { | |
7065 | ret = -EAGAIN; | |
7066 | goto out; | |
7067 | } | |
7068 | } | |
7069 | ||
1bda19eb | 7070 | btrfs_release_path(path); |
46bfbb5c CM |
7071 | |
7072 | /* | |
7073 | * look for other files referencing this extent, if we | |
7074 | * find any we must cow | |
7075 | */ | |
00361589 | 7076 | |
e4c3b2dc | 7077 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7078 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7079 | if (ret) { |
7080 | ret = 0; | |
7081 | goto out; | |
7082 | } | |
46bfbb5c CM |
7083 | |
7084 | /* | |
7085 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7086 | * in this extent we are about to write. If there | |
7087 | * are any csums in that range we have to cow in order | |
7088 | * to keep the csums correct | |
7089 | */ | |
7090 | disk_bytenr += backref_offset; | |
7091 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7092 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7093 | goto out; | |
46bfbb5c CM |
7094 | /* |
7095 | * all of the above have passed, it is safe to overwrite this extent | |
7096 | * without cow | |
7097 | */ | |
eb384b55 | 7098 | *len = num_bytes; |
46bfbb5c CM |
7099 | ret = 1; |
7100 | out: | |
7101 | btrfs_free_path(path); | |
7102 | return ret; | |
7103 | } | |
7104 | ||
eb838e73 | 7105 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
55e20bd1 | 7106 | struct extent_state **cached_state, int writing) |
eb838e73 JB |
7107 | { |
7108 | struct btrfs_ordered_extent *ordered; | |
7109 | int ret = 0; | |
7110 | ||
7111 | while (1) { | |
7112 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7113 | cached_state); |
eb838e73 JB |
7114 | /* |
7115 | * We're concerned with the entire range that we're going to be | |
01327610 | 7116 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7117 | * extents in this range. |
7118 | */ | |
a776c6fa | 7119 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7120 | lockend - lockstart + 1); |
7121 | ||
7122 | /* | |
7123 | * We need to make sure there are no buffered pages in this | |
7124 | * range either, we could have raced between the invalidate in | |
7125 | * generic_file_direct_write and locking the extent. The | |
7126 | * invalidate needs to happen so that reads after a write do not | |
7127 | * get stale data. | |
7128 | */ | |
fc4adbff | 7129 | if (!ordered && |
051c98eb DS |
7130 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7131 | lockstart, lockend))) | |
eb838e73 JB |
7132 | break; |
7133 | ||
7134 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7135 | cached_state); |
eb838e73 JB |
7136 | |
7137 | if (ordered) { | |
ade77029 FM |
7138 | /* |
7139 | * If we are doing a DIO read and the ordered extent we | |
7140 | * found is for a buffered write, we can not wait for it | |
7141 | * to complete and retry, because if we do so we can | |
7142 | * deadlock with concurrent buffered writes on page | |
7143 | * locks. This happens only if our DIO read covers more | |
7144 | * than one extent map, if at this point has already | |
7145 | * created an ordered extent for a previous extent map | |
7146 | * and locked its range in the inode's io tree, and a | |
7147 | * concurrent write against that previous extent map's | |
7148 | * range and this range started (we unlock the ranges | |
7149 | * in the io tree only when the bios complete and | |
7150 | * buffered writes always lock pages before attempting | |
7151 | * to lock range in the io tree). | |
7152 | */ | |
7153 | if (writing || | |
7154 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7155 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7156 | else | |
7157 | ret = -ENOTBLK; | |
eb838e73 JB |
7158 | btrfs_put_ordered_extent(ordered); |
7159 | } else { | |
eb838e73 | 7160 | /* |
b850ae14 FM |
7161 | * We could trigger writeback for this range (and wait |
7162 | * for it to complete) and then invalidate the pages for | |
7163 | * this range (through invalidate_inode_pages2_range()), | |
7164 | * but that can lead us to a deadlock with a concurrent | |
ba206a02 | 7165 | * call to readahead (a buffered read or a defrag call |
b850ae14 FM |
7166 | * triggered a readahead) on a page lock due to an |
7167 | * ordered dio extent we created before but did not have | |
7168 | * yet a corresponding bio submitted (whence it can not | |
ba206a02 | 7169 | * complete), which makes readahead wait for that |
b850ae14 FM |
7170 | * ordered extent to complete while holding a lock on |
7171 | * that page. | |
eb838e73 | 7172 | */ |
b850ae14 | 7173 | ret = -ENOTBLK; |
eb838e73 JB |
7174 | } |
7175 | ||
ade77029 FM |
7176 | if (ret) |
7177 | break; | |
7178 | ||
eb838e73 JB |
7179 | cond_resched(); |
7180 | } | |
7181 | ||
7182 | return ret; | |
7183 | } | |
7184 | ||
6f9994db | 7185 | /* The callers of this must take lock_extent() */ |
4b67c11d NB |
7186 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
7187 | u64 len, u64 orig_start, u64 block_start, | |
6f9994db LB |
7188 | u64 block_len, u64 orig_block_len, |
7189 | u64 ram_bytes, int compress_type, | |
7190 | int type) | |
69ffb543 JB |
7191 | { |
7192 | struct extent_map_tree *em_tree; | |
7193 | struct extent_map *em; | |
69ffb543 JB |
7194 | int ret; |
7195 | ||
6f9994db LB |
7196 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7197 | type == BTRFS_ORDERED_COMPRESSED || | |
7198 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7199 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7200 | |
4b67c11d | 7201 | em_tree = &inode->extent_tree; |
69ffb543 JB |
7202 | em = alloc_extent_map(); |
7203 | if (!em) | |
7204 | return ERR_PTR(-ENOMEM); | |
7205 | ||
7206 | em->start = start; | |
7207 | em->orig_start = orig_start; | |
7208 | em->len = len; | |
7209 | em->block_len = block_len; | |
7210 | em->block_start = block_start; | |
b4939680 | 7211 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7212 | em->ram_bytes = ram_bytes; |
70c8a91c | 7213 | em->generation = -1; |
69ffb543 | 7214 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7215 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7216 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7217 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7218 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7219 | em->compress_type = compress_type; | |
7220 | } | |
69ffb543 JB |
7221 | |
7222 | do { | |
4b67c11d NB |
7223 | btrfs_drop_extent_cache(inode, em->start, |
7224 | em->start + em->len - 1, 0); | |
69ffb543 | 7225 | write_lock(&em_tree->lock); |
09a2a8f9 | 7226 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7227 | write_unlock(&em_tree->lock); |
6f9994db LB |
7228 | /* |
7229 | * The caller has taken lock_extent(), who could race with us | |
7230 | * to add em? | |
7231 | */ | |
69ffb543 JB |
7232 | } while (ret == -EEXIST); |
7233 | ||
7234 | if (ret) { | |
7235 | free_extent_map(em); | |
7236 | return ERR_PTR(ret); | |
7237 | } | |
7238 | ||
6f9994db | 7239 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7240 | return em; |
7241 | } | |
7242 | ||
1c8d0175 | 7243 | |
55e20bd1 DS |
7244 | static int btrfs_get_blocks_direct_read(struct extent_map *em, |
7245 | struct buffer_head *bh_result, | |
7246 | struct inode *inode, | |
7247 | u64 start, u64 len) | |
7248 | { | |
7249 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7250 | ||
7251 | if (em->block_start == EXTENT_MAP_HOLE || | |
7252 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7253 | return -ENOENT; | |
7254 | ||
7255 | len = min(len, em->len - (start - em->start)); | |
7256 | ||
7257 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7258 | inode->i_blkbits; | |
7259 | bh_result->b_size = len; | |
7260 | bh_result->b_bdev = fs_info->fs_devices->latest_bdev; | |
7261 | set_buffer_mapped(bh_result); | |
7262 | ||
7263 | return 0; | |
7264 | } | |
7265 | ||
c5794e51 | 7266 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
55e20bd1 | 7267 | struct buffer_head *bh_result, |
c5794e51 NB |
7268 | struct inode *inode, |
7269 | struct btrfs_dio_data *dio_data, | |
7270 | u64 start, u64 len) | |
7271 | { | |
7272 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7273 | struct extent_map *em = *map; | |
7274 | int ret = 0; | |
7275 | ||
7276 | /* | |
7277 | * We don't allocate a new extent in the following cases | |
7278 | * | |
7279 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7280 | * existing extent. | |
7281 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7282 | * just use the extent. | |
7283 | * | |
7284 | */ | |
7285 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7286 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7287 | em->block_start != EXTENT_MAP_HOLE)) { | |
7288 | int type; | |
7289 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7290 | ||
7291 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7292 | type = BTRFS_ORDERED_PREALLOC; | |
7293 | else | |
7294 | type = BTRFS_ORDERED_NOCOW; | |
7295 | len = min(len, em->len - (start - em->start)); | |
7296 | block_start = em->block_start + (start - em->start); | |
7297 | ||
7298 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
7299 | &orig_block_len, &ram_bytes) == 1 && | |
7300 | btrfs_inc_nocow_writers(fs_info, block_start)) { | |
7301 | struct extent_map *em2; | |
7302 | ||
7303 | em2 = btrfs_create_dio_extent(inode, start, len, | |
7304 | orig_start, block_start, | |
7305 | len, orig_block_len, | |
7306 | ram_bytes, type); | |
7307 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7308 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7309 | free_extent_map(em); | |
7310 | *map = em = em2; | |
7311 | } | |
7312 | ||
7313 | if (em2 && IS_ERR(em2)) { | |
7314 | ret = PTR_ERR(em2); | |
7315 | goto out; | |
7316 | } | |
7317 | /* | |
7318 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7319 | * use the existing or preallocated extent, so does not | |
7320 | * need to adjust btrfs_space_info's bytes_may_use. | |
7321 | */ | |
46d4dac8 | 7322 | btrfs_free_reserved_data_space_noquota(inode, len); |
c5794e51 NB |
7323 | goto skip_cow; |
7324 | } | |
7325 | } | |
7326 | ||
7327 | /* this will cow the extent */ | |
55e20bd1 | 7328 | len = bh_result->b_size; |
c5794e51 NB |
7329 | free_extent_map(em); |
7330 | *map = em = btrfs_new_extent_direct(inode, start, len); | |
7331 | if (IS_ERR(em)) { | |
7332 | ret = PTR_ERR(em); | |
7333 | goto out; | |
7334 | } | |
7335 | ||
7336 | len = min(len, em->len - (start - em->start)); | |
7337 | ||
7338 | skip_cow: | |
55e20bd1 DS |
7339 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7340 | inode->i_blkbits; | |
7341 | bh_result->b_size = len; | |
7342 | bh_result->b_bdev = fs_info->fs_devices->latest_bdev; | |
7343 | set_buffer_mapped(bh_result); | |
7344 | ||
7345 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7346 | set_buffer_new(bh_result); | |
7347 | ||
c5794e51 NB |
7348 | /* |
7349 | * Need to update the i_size under the extent lock so buffered | |
7350 | * readers will get the updated i_size when we unlock. | |
7351 | */ | |
55e20bd1 | 7352 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
c5794e51 NB |
7353 | i_size_write(inode, start + len); |
7354 | ||
55e20bd1 | 7355 | WARN_ON(dio_data->reserve < len); |
c5794e51 | 7356 | dio_data->reserve -= len; |
55e20bd1 DS |
7357 | dio_data->unsubmitted_oe_range_end = start + len; |
7358 | current->journal_info = dio_data; | |
c5794e51 NB |
7359 | out: |
7360 | return ret; | |
7361 | } | |
7362 | ||
55e20bd1 DS |
7363 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7364 | struct buffer_head *bh_result, int create) | |
4b46fce2 | 7365 | { |
0b246afa | 7366 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7367 | struct extent_map *em; |
eb838e73 | 7368 | struct extent_state *cached_state = NULL; |
50745b0a | 7369 | struct btrfs_dio_data *dio_data = NULL; |
55e20bd1 | 7370 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7371 | u64 lockstart, lockend; |
55e20bd1 | 7372 | u64 len = bh_result->b_size; |
0934856d | 7373 | int ret = 0; |
eb838e73 | 7374 | |
55e20bd1 | 7375 | if (!create) |
0b246afa | 7376 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7377 | |
c329861d JB |
7378 | lockstart = start; |
7379 | lockend = start + len - 1; | |
7380 | ||
55e20bd1 DS |
7381 | if (current->journal_info) { |
7382 | /* | |
7383 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7384 | * that anything that needs to check if there's a transaction doesn't get | |
7385 | * confused. | |
7386 | */ | |
7387 | dio_data = current->journal_info; | |
7388 | current->journal_info = NULL; | |
e1cbbfa5 JB |
7389 | } |
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 | */ | |
55e20bd1 DS |
7395 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7396 | create)) { | |
9c9464cc FM |
7397 | ret = -ENOTBLK; |
7398 | goto err; | |
7399 | } | |
eb838e73 | 7400 | |
39b07b5d | 7401 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
eb838e73 JB |
7402 | if (IS_ERR(em)) { |
7403 | ret = PTR_ERR(em); | |
7404 | goto unlock_err; | |
7405 | } | |
4b46fce2 JB |
7406 | |
7407 | /* | |
7408 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7409 | * io. INLINE is special, and we could probably kludge it in here, but | |
7410 | * it's still buffered so for safety lets just fall back to the generic | |
7411 | * buffered path. | |
7412 | * | |
7413 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7414 | * decompress it, so there will be buffering required no matter what we | |
7415 | * do, so go ahead and fallback to buffered. | |
7416 | * | |
01327610 | 7417 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7418 | * to buffered IO. Don't blame me, this is the price we pay for using |
7419 | * the generic code. | |
7420 | */ | |
7421 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7422 | em->block_start == EXTENT_MAP_INLINE) { | |
7423 | free_extent_map(em); | |
eb838e73 JB |
7424 | ret = -ENOTBLK; |
7425 | goto unlock_err; | |
4b46fce2 JB |
7426 | } |
7427 | ||
55e20bd1 DS |
7428 | if (create) { |
7429 | ret = btrfs_get_blocks_direct_write(&em, bh_result, inode, | |
7430 | dio_data, start, len); | |
c5794e51 NB |
7431 | if (ret < 0) |
7432 | goto unlock_err; | |
55e20bd1 DS |
7433 | |
7434 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
7435 | lockend, &cached_state); | |
c5794e51 | 7436 | } else { |
55e20bd1 DS |
7437 | ret = btrfs_get_blocks_direct_read(em, bh_result, inode, |
7438 | start, len); | |
7439 | /* Can be negative only if we read from a hole */ | |
7440 | if (ret < 0) { | |
7441 | ret = 0; | |
7442 | free_extent_map(em); | |
7443 | goto unlock_err; | |
7444 | } | |
1c8d0175 NB |
7445 | /* |
7446 | * We need to unlock only the end area that we aren't using. | |
7447 | * The rest is going to be unlocked by the endio routine. | |
7448 | */ | |
55e20bd1 DS |
7449 | lockstart = start + bh_result->b_size; |
7450 | if (lockstart < lockend) { | |
7451 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
7452 | lockstart, lockend, &cached_state); | |
7453 | } else { | |
7454 | free_extent_state(cached_state); | |
7455 | } | |
a43a67a2 | 7456 | } |
a43a67a2 | 7457 | |
4b46fce2 JB |
7458 | free_extent_map(em); |
7459 | ||
7460 | return 0; | |
eb838e73 JB |
7461 | |
7462 | unlock_err: | |
e182163d OS |
7463 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7464 | &cached_state); | |
9c9464cc | 7465 | err: |
55e20bd1 DS |
7466 | if (dio_data) |
7467 | current->journal_info = dio_data; | |
8b110e39 MX |
7468 | return ret; |
7469 | } | |
7470 | ||
769b4f24 | 7471 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
8b110e39 | 7472 | { |
769b4f24 OS |
7473 | /* |
7474 | * This implies a barrier so that stores to dio_bio->bi_status before | |
7475 | * this and loads of dio_bio->bi_status after this are fully ordered. | |
7476 | */ | |
7477 | if (!refcount_dec_and_test(&dip->refs)) | |
7478 | return; | |
8b110e39 | 7479 | |
769b4f24 OS |
7480 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
7481 | __endio_write_update_ordered(dip->inode, dip->logical_offset, | |
7482 | dip->bytes, | |
7483 | !dip->dio_bio->bi_status); | |
7484 | } else { | |
7485 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, | |
7486 | dip->logical_offset, | |
7487 | dip->logical_offset + dip->bytes - 1); | |
8b110e39 MX |
7488 | } |
7489 | ||
55e20bd1 | 7490 | dio_end_io(dip->dio_bio); |
769b4f24 | 7491 | kfree(dip); |
8b110e39 MX |
7492 | } |
7493 | ||
77d5d689 OS |
7494 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7495 | int mirror_num, | |
7496 | unsigned long bio_flags) | |
8b110e39 | 7497 | { |
77d5d689 | 7498 | struct btrfs_dio_private *dip = bio->bi_private; |
2ff7e61e | 7499 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7500 | blk_status_t ret; |
8b110e39 | 7501 | |
37226b21 | 7502 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7503 | |
5c047a69 | 7504 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
8b110e39 | 7505 | if (ret) |
ea057f6d | 7506 | return ret; |
8b110e39 | 7507 | |
77d5d689 | 7508 | refcount_inc(&dip->refs); |
08635bae | 7509 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
8b110e39 | 7510 | if (ret) |
fd9d6670 | 7511 | refcount_dec(&dip->refs); |
77d5d689 | 7512 | return ret; |
8b110e39 MX |
7513 | } |
7514 | ||
fd9d6670 OS |
7515 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
7516 | struct btrfs_io_bio *io_bio, | |
7517 | const bool uptodate) | |
4b46fce2 | 7518 | { |
fd9d6670 OS |
7519 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
7520 | const u32 sectorsize = fs_info->sectorsize; | |
7521 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
7522 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
7523 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); | |
17347cec LB |
7524 | struct bio_vec bvec; |
7525 | struct bvec_iter iter; | |
fd9d6670 OS |
7526 | u64 start = io_bio->logical; |
7527 | int icsum = 0; | |
58efbc9f | 7528 | blk_status_t err = BLK_STS_OK; |
4b46fce2 | 7529 | |
fd9d6670 OS |
7530 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
7531 | unsigned int i, nr_sectors, pgoff; | |
8b110e39 | 7532 | |
17347cec LB |
7533 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
7534 | pgoff = bvec.bv_offset; | |
fd9d6670 | 7535 | for (i = 0; i < nr_sectors; i++) { |
97bf5a55 | 7536 | ASSERT(pgoff < PAGE_SIZE); |
fd9d6670 OS |
7537 | if (uptodate && |
7538 | (!csum || !check_data_csum(inode, io_bio, icsum, | |
7539 | bvec.bv_page, pgoff, | |
7540 | start, sectorsize))) { | |
7541 | clean_io_failure(fs_info, failure_tree, io_tree, | |
7542 | start, bvec.bv_page, | |
7543 | btrfs_ino(BTRFS_I(inode)), | |
7544 | pgoff); | |
7545 | } else { | |
7546 | blk_status_t status; | |
7547 | ||
77d5d689 OS |
7548 | status = btrfs_submit_read_repair(inode, |
7549 | &io_bio->bio, | |
7550 | start - io_bio->logical, | |
fd9d6670 OS |
7551 | bvec.bv_page, pgoff, |
7552 | start, | |
7553 | start + sectorsize - 1, | |
77d5d689 OS |
7554 | io_bio->mirror_num, |
7555 | submit_dio_repair_bio); | |
fd9d6670 OS |
7556 | if (status) |
7557 | err = status; | |
7558 | } | |
7559 | start += sectorsize; | |
7560 | icsum++; | |
2dabb324 | 7561 | pgoff += sectorsize; |
2dabb324 | 7562 | } |
2c30c71b | 7563 | } |
c1dc0896 MX |
7564 | return err; |
7565 | } | |
7566 | ||
52427260 QW |
7567 | static void __endio_write_update_ordered(struct inode *inode, |
7568 | const u64 offset, const u64 bytes, | |
7569 | const bool uptodate) | |
4b46fce2 | 7570 | { |
0b246afa | 7571 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7572 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 | 7573 | struct btrfs_workqueue *wq; |
14543774 FM |
7574 | u64 ordered_offset = offset; |
7575 | u64 ordered_bytes = bytes; | |
67c003f9 | 7576 | u64 last_offset; |
4b46fce2 | 7577 | |
a0cac0ec | 7578 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
52427260 | 7579 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 7580 | else |
52427260 | 7581 | wq = fs_info->endio_write_workers; |
52427260 | 7582 | |
b25f0d00 NB |
7583 | while (ordered_offset < offset + bytes) { |
7584 | last_offset = ordered_offset; | |
7585 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
7586 | &ordered_offset, | |
7587 | ordered_bytes, | |
7588 | uptodate)) { | |
a0cac0ec OS |
7589 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
7590 | NULL); | |
b25f0d00 NB |
7591 | btrfs_queue_work(wq, &ordered->work); |
7592 | } | |
7593 | /* | |
7594 | * If btrfs_dec_test_ordered_pending does not find any ordered | |
7595 | * extent in the range, we can exit. | |
7596 | */ | |
7597 | if (ordered_offset == last_offset) | |
7598 | return; | |
7599 | /* | |
7600 | * Our bio might span multiple ordered extents. In this case | |
52042d8e | 7601 | * we keep going until we have accounted the whole dio. |
b25f0d00 NB |
7602 | */ |
7603 | if (ordered_offset < offset + bytes) { | |
7604 | ordered_bytes = offset + bytes - ordered_offset; | |
7605 | ordered = NULL; | |
7606 | } | |
163cf09c | 7607 | } |
14543774 FM |
7608 | } |
7609 | ||
d0ee3934 | 7610 | static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, |
d0779291 | 7611 | struct bio *bio, u64 offset) |
eaf25d93 | 7612 | { |
c6100a4b | 7613 | struct inode *inode = private_data; |
4e4cbee9 | 7614 | blk_status_t ret; |
bd242a08 | 7615 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
79787eaa | 7616 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
7617 | return 0; |
7618 | } | |
7619 | ||
4246a0b6 | 7620 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
7621 | { |
7622 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 7623 | blk_status_t err = bio->bi_status; |
e65e1535 | 7624 | |
8b110e39 MX |
7625 | if (err) |
7626 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 7627 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
7628 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
7629 | bio->bi_opf, | |
8b110e39 MX |
7630 | (unsigned long long)bio->bi_iter.bi_sector, |
7631 | bio->bi_iter.bi_size, err); | |
7632 | ||
769b4f24 OS |
7633 | if (bio_op(bio) == REQ_OP_READ) { |
7634 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), | |
fd9d6670 | 7635 | !err); |
e65e1535 MX |
7636 | } |
7637 | ||
769b4f24 OS |
7638 | if (err) |
7639 | dip->dio_bio->bi_status = err; | |
e65e1535 | 7640 | |
e65e1535 | 7641 | bio_put(bio); |
769b4f24 | 7642 | btrfs_dio_private_put(dip); |
c1dc0896 MX |
7643 | } |
7644 | ||
d0ee3934 DS |
7645 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
7646 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 7647 | { |
0b246afa | 7648 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 7649 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 7650 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 7651 | blk_status_t ret; |
e65e1535 | 7652 | |
4c274bc6 | 7653 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
7654 | if (async_submit) |
7655 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
7656 | ||
5fd02043 | 7657 | if (!write) { |
0b246afa | 7658 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
7659 | if (ret) |
7660 | goto err; | |
7661 | } | |
e65e1535 | 7662 | |
e6961cac | 7663 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
7664 | goto map; |
7665 | ||
7666 | if (write && async_submit) { | |
c6100a4b JB |
7667 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
7668 | file_offset, inode, | |
e288c080 | 7669 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 7670 | goto err; |
1ae39938 JB |
7671 | } else if (write) { |
7672 | /* | |
7673 | * If we aren't doing async submit, calculate the csum of the | |
7674 | * bio now. | |
7675 | */ | |
bd242a08 | 7676 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
1ae39938 JB |
7677 | if (ret) |
7678 | goto err; | |
23ea8e5a | 7679 | } else { |
85879573 OS |
7680 | u64 csum_offset; |
7681 | ||
7682 | csum_offset = file_offset - dip->logical_offset; | |
7683 | csum_offset >>= inode->i_sb->s_blocksize_bits; | |
7684 | csum_offset *= btrfs_super_csum_size(fs_info->super_copy); | |
7685 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; | |
c2db1073 | 7686 | } |
1ae39938 | 7687 | map: |
08635bae | 7688 | ret = btrfs_map_bio(fs_info, bio, 0); |
e65e1535 | 7689 | err: |
e65e1535 MX |
7690 | return ret; |
7691 | } | |
7692 | ||
c36cac28 OS |
7693 | /* |
7694 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked | |
7695 | * or ordered extents whether or not we submit any bios. | |
7696 | */ | |
7697 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, | |
7698 | struct inode *inode, | |
7699 | loff_t file_offset) | |
e65e1535 | 7700 | { |
c36cac28 | 7701 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
85879573 OS |
7702 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
7703 | size_t dip_size; | |
c36cac28 | 7704 | struct btrfs_dio_private *dip; |
c36cac28 | 7705 | |
85879573 OS |
7706 | dip_size = sizeof(*dip); |
7707 | if (!write && csum) { | |
7708 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7709 | const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); | |
7710 | size_t nblocks; | |
7711 | ||
7712 | nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; | |
7713 | dip_size += csum_size * nblocks; | |
7714 | } | |
7715 | ||
7716 | dip = kzalloc(dip_size, GFP_NOFS); | |
c36cac28 OS |
7717 | if (!dip) |
7718 | return NULL; | |
7719 | ||
c36cac28 OS |
7720 | dip->inode = inode; |
7721 | dip->logical_offset = file_offset; | |
7722 | dip->bytes = dio_bio->bi_iter.bi_size; | |
7723 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
c36cac28 | 7724 | dip->dio_bio = dio_bio; |
e3b318d1 | 7725 | refcount_set(&dip->refs, 1); |
55e20bd1 DS |
7726 | |
7727 | if (write) { | |
7728 | struct btrfs_dio_data *dio_data = current->journal_info; | |
7729 | ||
7730 | /* | |
7731 | * Setting range start and end to the same value means that | |
7732 | * no cleanup will happen in btrfs_direct_IO | |
7733 | */ | |
7734 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
7735 | dip->bytes; | |
7736 | dio_data->unsubmitted_oe_range_start = | |
7737 | dio_data->unsubmitted_oe_range_end; | |
7738 | } | |
c36cac28 OS |
7739 | return dip; |
7740 | } | |
7741 | ||
55e20bd1 DS |
7742 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
7743 | loff_t file_offset) | |
c36cac28 OS |
7744 | { |
7745 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); | |
85879573 | 7746 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
0b246afa | 7747 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
769b4f24 OS |
7748 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
7749 | BTRFS_BLOCK_GROUP_RAID56_MASK); | |
c36cac28 | 7750 | struct btrfs_dio_private *dip; |
e65e1535 | 7751 | struct bio *bio; |
c36cac28 | 7752 | u64 start_sector; |
1ae39938 | 7753 | int async_submit = 0; |
725130ba LB |
7754 | u64 submit_len; |
7755 | int clone_offset = 0; | |
7756 | int clone_len; | |
5f4dc8fc | 7757 | int ret; |
58efbc9f | 7758 | blk_status_t status; |
89b798ad | 7759 | struct btrfs_io_geometry geom; |
e65e1535 | 7760 | |
c36cac28 OS |
7761 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
7762 | if (!dip) { | |
7763 | if (!write) { | |
7764 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
7765 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
7766 | } | |
7767 | dio_bio->bi_status = BLK_STS_RESOURCE; | |
55e20bd1 DS |
7768 | dio_end_io(dio_bio); |
7769 | return; | |
c36cac28 | 7770 | } |
facc8a22 | 7771 | |
85879573 OS |
7772 | if (!write && csum) { |
7773 | /* | |
7774 | * Load the csums up front to reduce csum tree searches and | |
7775 | * contention when submitting bios. | |
7776 | */ | |
7777 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, | |
7778 | dip->csums); | |
7779 | if (status != BLK_STS_OK) | |
7780 | goto out_err; | |
02f57c7a JB |
7781 | } |
7782 | ||
769b4f24 OS |
7783 | start_sector = dio_bio->bi_iter.bi_sector; |
7784 | submit_len = dio_bio->bi_iter.bi_size; | |
53b381b3 | 7785 | |
3c91ee69 | 7786 | do { |
769b4f24 OS |
7787 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
7788 | start_sector << 9, submit_len, | |
7789 | &geom); | |
7790 | if (ret) { | |
7791 | status = errno_to_blk_status(ret); | |
7792 | goto out_err; | |
7793 | } | |
7794 | ASSERT(geom.len <= INT_MAX); | |
7795 | ||
89b798ad | 7796 | clone_len = min_t(int, submit_len, geom.len); |
02f57c7a | 7797 | |
725130ba LB |
7798 | /* |
7799 | * This will never fail as it's passing GPF_NOFS and | |
7800 | * the allocation is backed by btrfs_bioset. | |
7801 | */ | |
769b4f24 | 7802 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
725130ba LB |
7803 | bio->bi_private = dip; |
7804 | bio->bi_end_io = btrfs_end_dio_bio; | |
7805 | btrfs_io_bio(bio)->logical = file_offset; | |
7806 | ||
7807 | ASSERT(submit_len >= clone_len); | |
7808 | submit_len -= clone_len; | |
e65e1535 | 7809 | |
725130ba LB |
7810 | /* |
7811 | * Increase the count before we submit the bio so we know | |
7812 | * the end IO handler won't happen before we increase the | |
7813 | * count. Otherwise, the dip might get freed before we're | |
7814 | * done setting it up. | |
769b4f24 OS |
7815 | * |
7816 | * We transfer the initial reference to the last bio, so we | |
7817 | * don't need to increment the reference count for the last one. | |
725130ba | 7818 | */ |
769b4f24 OS |
7819 | if (submit_len > 0) { |
7820 | refcount_inc(&dip->refs); | |
7821 | /* | |
7822 | * If we are submitting more than one bio, submit them | |
7823 | * all asynchronously. The exception is RAID 5 or 6, as | |
7824 | * asynchronous checksums make it difficult to collect | |
7825 | * full stripe writes. | |
7826 | */ | |
7827 | if (!raid56) | |
7828 | async_submit = 1; | |
7829 | } | |
e65e1535 | 7830 | |
d0ee3934 | 7831 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
7832 | async_submit); |
7833 | if (status) { | |
725130ba | 7834 | bio_put(bio); |
769b4f24 OS |
7835 | if (submit_len > 0) |
7836 | refcount_dec(&dip->refs); | |
725130ba LB |
7837 | goto out_err; |
7838 | } | |
e65e1535 | 7839 | |
725130ba LB |
7840 | clone_offset += clone_len; |
7841 | start_sector += clone_len >> 9; | |
7842 | file_offset += clone_len; | |
3c91ee69 | 7843 | } while (submit_len > 0); |
55e20bd1 | 7844 | return; |
e65e1535 | 7845 | |
e65e1535 | 7846 | out_err: |
769b4f24 OS |
7847 | dip->dio_bio->bi_status = status; |
7848 | btrfs_dio_private_put(dip); | |
4b46fce2 JB |
7849 | } |
7850 | ||
f4c48b44 DS |
7851 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
7852 | const struct iov_iter *iter, loff_t offset) | |
7853 | { | |
7854 | int seg; | |
7855 | int i; | |
7856 | unsigned int blocksize_mask = fs_info->sectorsize - 1; | |
7857 | ssize_t retval = -EINVAL; | |
0934856d | 7858 | |
f4c48b44 DS |
7859 | if (offset & blocksize_mask) |
7860 | goto out; | |
7861 | ||
7862 | if (iov_iter_alignment(iter) & blocksize_mask) | |
7863 | goto out; | |
7864 | ||
7865 | /* If this is a write we don't need to check anymore */ | |
7866 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) | |
7867 | return 0; | |
7868 | /* | |
7869 | * Check to make sure we don't have duplicate iov_base's in this | |
7870 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
7871 | * when reading back. | |
7872 | */ | |
7873 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
7874 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
7875 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
7876 | goto out; | |
7877 | } | |
7878 | } | |
7879 | retval = 0; | |
7880 | out: | |
7881 | return retval; | |
7882 | } | |
7883 | ||
55e20bd1 | 7884 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
f4c48b44 DS |
7885 | { |
7886 | struct file *file = iocb->ki_filp; | |
7887 | struct inode *inode = file->f_mapping->host; | |
7888 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
55e20bd1 | 7889 | struct btrfs_dio_data dio_data = { 0 }; |
f4c48b44 DS |
7890 | struct extent_changeset *data_reserved = NULL; |
7891 | loff_t offset = iocb->ki_pos; | |
7892 | size_t count = 0; | |
55e20bd1 DS |
7893 | int flags = 0; |
7894 | bool wakeup = true; | |
f4c48b44 DS |
7895 | bool relock = false; |
7896 | ssize_t ret; | |
7897 | ||
7898 | if (check_direct_IO(fs_info, iter, offset)) | |
7899 | return 0; | |
7900 | ||
55e20bd1 DS |
7901 | inode_dio_begin(inode); |
7902 | ||
7903 | /* | |
7904 | * The generic stuff only does filemap_write_and_wait_range, which | |
7905 | * isn't enough if we've written compressed pages to this area, so | |
7906 | * we need to flush the dirty pages again to make absolutely sure | |
7907 | * that any outstanding dirty pages are on disk. | |
7908 | */ | |
f4c48b44 | 7909 | count = iov_iter_count(iter); |
55e20bd1 DS |
7910 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
7911 | &BTRFS_I(inode)->runtime_flags)) | |
7912 | filemap_fdatawrite_range(inode->i_mapping, offset, | |
7913 | offset + count - 1); | |
7914 | ||
f4c48b44 DS |
7915 | if (iov_iter_rw(iter) == WRITE) { |
7916 | /* | |
7917 | * If the write DIO is beyond the EOF, we need update | |
7918 | * the isize, but it is protected by i_mutex. So we can | |
7919 | * not unlock the i_mutex at this case. | |
7920 | */ | |
7921 | if (offset + count <= inode->i_size) { | |
55e20bd1 | 7922 | dio_data.overwrite = 1; |
f4c48b44 DS |
7923 | inode_unlock(inode); |
7924 | relock = true; | |
f4c48b44 | 7925 | } |
55e20bd1 DS |
7926 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
7927 | offset, count); | |
7928 | if (ret) | |
7929 | goto out; | |
7930 | ||
7931 | /* | |
7932 | * We need to know how many extents we reserved so that we can | |
7933 | * do the accounting properly if we go over the number we | |
7934 | * originally calculated. Abuse current->journal_info for this. | |
7935 | */ | |
7936 | dio_data.reserve = round_up(count, | |
7937 | fs_info->sectorsize); | |
7938 | dio_data.unsubmitted_oe_range_start = (u64)offset; | |
7939 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
7940 | current->journal_info = &dio_data; | |
f4c48b44 | 7941 | down_read(&BTRFS_I(inode)->dio_sem); |
55e20bd1 DS |
7942 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
7943 | &BTRFS_I(inode)->runtime_flags)) { | |
7944 | inode_dio_end(inode); | |
7945 | flags = DIO_LOCKING | DIO_SKIP_HOLES; | |
7946 | wakeup = false; | |
f4c48b44 DS |
7947 | } |
7948 | ||
55e20bd1 DS |
7949 | ret = __blockdev_direct_IO(iocb, inode, |
7950 | fs_info->fs_devices->latest_bdev, | |
7951 | iter, btrfs_get_blocks_direct, NULL, | |
7952 | btrfs_submit_direct, flags); | |
f4c48b44 DS |
7953 | if (iov_iter_rw(iter) == WRITE) { |
7954 | up_read(&BTRFS_I(inode)->dio_sem); | |
55e20bd1 DS |
7955 | current->journal_info = NULL; |
7956 | if (ret < 0 && ret != -EIOCBQUEUED) { | |
7957 | if (dio_data.reserve) | |
7958 | btrfs_delalloc_release_space(inode, data_reserved, | |
7959 | offset, dio_data.reserve, true); | |
7960 | /* | |
7961 | * On error we might have left some ordered extents | |
7962 | * without submitting corresponding bios for them, so | |
7963 | * cleanup them up to avoid other tasks getting them | |
7964 | * and waiting for them to complete forever. | |
7965 | */ | |
7966 | if (dio_data.unsubmitted_oe_range_start < | |
7967 | dio_data.unsubmitted_oe_range_end) | |
7968 | __endio_write_update_ordered(inode, | |
7969 | dio_data.unsubmitted_oe_range_start, | |
7970 | dio_data.unsubmitted_oe_range_end - | |
7971 | dio_data.unsubmitted_oe_range_start, | |
7972 | false); | |
7973 | } else if (ret >= 0 && (size_t)ret < count) | |
7974 | btrfs_delalloc_release_space(inode, data_reserved, | |
7975 | offset, count - (size_t)ret, true); | |
7976 | btrfs_delalloc_release_extents(BTRFS_I(inode), count); | |
f4c48b44 DS |
7977 | } |
7978 | out: | |
55e20bd1 DS |
7979 | if (wakeup) |
7980 | inode_dio_end(inode); | |
f4c48b44 DS |
7981 | if (relock) |
7982 | inode_lock(inode); | |
55e20bd1 | 7983 | |
f4c48b44 DS |
7984 | extent_changeset_free(data_reserved); |
7985 | return ret; | |
7986 | } | |
16432985 | 7987 | |
1506fcc8 | 7988 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 7989 | u64 start, u64 len) |
1506fcc8 | 7990 | { |
05dadc09 TI |
7991 | int ret; |
7992 | ||
45dd052e | 7993 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
05dadc09 TI |
7994 | if (ret) |
7995 | return ret; | |
7996 | ||
2135fb9b | 7997 | return extent_fiemap(inode, fieinfo, start, len); |
1506fcc8 YS |
7998 | } |
7999 | ||
a52d9a80 | 8000 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8001 | { |
71ad38b4 | 8002 | return extent_read_full_page(page, btrfs_get_extent, 0); |
9ebefb18 | 8003 | } |
1832a6d5 | 8004 | |
a52d9a80 | 8005 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8006 | { |
be7bd730 JB |
8007 | struct inode *inode = page->mapping->host; |
8008 | int ret; | |
b888db2b CM |
8009 | |
8010 | if (current->flags & PF_MEMALLOC) { | |
8011 | redirty_page_for_writepage(wbc, page); | |
8012 | unlock_page(page); | |
8013 | return 0; | |
8014 | } | |
be7bd730 JB |
8015 | |
8016 | /* | |
8017 | * If we are under memory pressure we will call this directly from the | |
8018 | * VM, we need to make sure we have the inode referenced for the ordered | |
8019 | * extent. If not just return like we didn't do anything. | |
8020 | */ | |
8021 | if (!igrab(inode)) { | |
8022 | redirty_page_for_writepage(wbc, page); | |
8023 | return AOP_WRITEPAGE_ACTIVATE; | |
8024 | } | |
0a9b0e53 | 8025 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8026 | btrfs_add_delayed_iput(inode); |
8027 | return ret; | |
9ebefb18 CM |
8028 | } |
8029 | ||
48a3b636 ES |
8030 | static int btrfs_writepages(struct address_space *mapping, |
8031 | struct writeback_control *wbc) | |
b293f02e | 8032 | { |
8ae225a8 | 8033 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8034 | } |
8035 | ||
ba206a02 | 8036 | static void btrfs_readahead(struct readahead_control *rac) |
3ab2fb5a | 8037 | { |
ba206a02 | 8038 | extent_readahead(rac); |
3ab2fb5a | 8039 | } |
2a3ff0ad | 8040 | |
e6dcd2dc | 8041 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8042 | { |
477a30ba | 8043 | int ret = try_release_extent_mapping(page, gfp_flags); |
d1b89bc0 GJ |
8044 | if (ret == 1) |
8045 | detach_page_private(page); | |
a52d9a80 | 8046 | return ret; |
39279cc3 CM |
8047 | } |
8048 | ||
e6dcd2dc CM |
8049 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8050 | { | |
98509cfc CM |
8051 | if (PageWriteback(page) || PageDirty(page)) |
8052 | return 0; | |
3ba7ab22 | 8053 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8054 | } |
8055 | ||
f8e66081 RG |
8056 | #ifdef CONFIG_MIGRATION |
8057 | static int btrfs_migratepage(struct address_space *mapping, | |
8058 | struct page *newpage, struct page *page, | |
8059 | enum migrate_mode mode) | |
8060 | { | |
8061 | int ret; | |
8062 | ||
8063 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); | |
8064 | if (ret != MIGRATEPAGE_SUCCESS) | |
8065 | return ret; | |
8066 | ||
d1b89bc0 GJ |
8067 | if (page_has_private(page)) |
8068 | attach_page_private(newpage, detach_page_private(page)); | |
f8e66081 RG |
8069 | |
8070 | if (PagePrivate2(page)) { | |
8071 | ClearPagePrivate2(page); | |
8072 | SetPagePrivate2(newpage); | |
8073 | } | |
8074 | ||
8075 | if (mode != MIGRATE_SYNC_NO_COPY) | |
8076 | migrate_page_copy(newpage, page); | |
8077 | else | |
8078 | migrate_page_states(newpage, page); | |
8079 | return MIGRATEPAGE_SUCCESS; | |
8080 | } | |
8081 | #endif | |
8082 | ||
d47992f8 LC |
8083 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8084 | unsigned int length) | |
39279cc3 | 8085 | { |
5fd02043 | 8086 | struct inode *inode = page->mapping->host; |
d1310b2e | 8087 | struct extent_io_tree *tree; |
e6dcd2dc | 8088 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8089 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8090 | u64 page_start = page_offset(page); |
09cbfeaf | 8091 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8092 | u64 start; |
8093 | u64 end; | |
131e404a | 8094 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8095 | |
8b62b72b CM |
8096 | /* |
8097 | * we have the page locked, so new writeback can't start, | |
8098 | * and the dirty bit won't be cleared while we are here. | |
8099 | * | |
8100 | * Wait for IO on this page so that we can safely clear | |
8101 | * the PagePrivate2 bit and do ordered accounting | |
8102 | */ | |
e6dcd2dc | 8103 | wait_on_page_writeback(page); |
8b62b72b | 8104 | |
5fd02043 | 8105 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8106 | if (offset) { |
8107 | btrfs_releasepage(page, GFP_NOFS); | |
8108 | return; | |
8109 | } | |
131e404a FDBM |
8110 | |
8111 | if (!inode_evicting) | |
ff13db41 | 8112 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8113 | again: |
8114 | start = page_start; | |
a776c6fa | 8115 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8116 | page_end - start + 1); |
e6dcd2dc | 8117 | if (ordered) { |
bffe633e OS |
8118 | end = min(page_end, |
8119 | ordered->file_offset + ordered->num_bytes - 1); | |
eb84ae03 CM |
8120 | /* |
8121 | * IO on this page will never be started, so we need | |
8122 | * to account for any ordered extents now | |
8123 | */ | |
131e404a | 8124 | if (!inode_evicting) |
dbfdb6d1 | 8125 | clear_extent_bit(tree, start, end, |
e182163d | 8126 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
131e404a | 8127 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8128 | EXTENT_DEFRAG, 1, 0, &cached_state); |
8b62b72b CM |
8129 | /* |
8130 | * whoever cleared the private bit is responsible | |
8131 | * for the finish_ordered_io | |
8132 | */ | |
77cef2ec JB |
8133 | if (TestClearPagePrivate2(page)) { |
8134 | struct btrfs_ordered_inode_tree *tree; | |
8135 | u64 new_len; | |
8136 | ||
8137 | tree = &BTRFS_I(inode)->ordered_tree; | |
8138 | ||
8139 | spin_lock_irq(&tree->lock); | |
8140 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8141 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8142 | if (new_len < ordered->truncated_len) |
8143 | ordered->truncated_len = new_len; | |
8144 | spin_unlock_irq(&tree->lock); | |
8145 | ||
8146 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8147 | start, |
8148 | end - start + 1, 1)) | |
77cef2ec | 8149 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8150 | } |
e6dcd2dc | 8151 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8152 | if (!inode_evicting) { |
8153 | cached_state = NULL; | |
dbfdb6d1 | 8154 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8155 | &cached_state); |
8156 | } | |
dbfdb6d1 CR |
8157 | |
8158 | start = end + 1; | |
8159 | if (start < page_end) | |
8160 | goto again; | |
131e404a FDBM |
8161 | } |
8162 | ||
b9d0b389 QW |
8163 | /* |
8164 | * Qgroup reserved space handler | |
8165 | * Page here will be either | |
fa91e4aa QW |
8166 | * 1) Already written to disk or ordered extent already submitted |
8167 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. | |
8168 | * Qgroup will be handled by its qgroup_record then. | |
8169 | * btrfs_qgroup_free_data() call will do nothing here. | |
8170 | * | |
8171 | * 2) Not written to disk yet | |
8172 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED | |
8173 | * bit of its io_tree, and free the qgroup reserved data space. | |
8174 | * Since the IO will never happen for this page. | |
b9d0b389 | 8175 | */ |
8b8a979f | 8176 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE); |
131e404a | 8177 | if (!inode_evicting) { |
e182163d | 8178 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
a7e3b975 FM |
8179 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
8180 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
ae0f1625 | 8181 | &cached_state); |
131e404a FDBM |
8182 | |
8183 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8184 | } |
e6dcd2dc | 8185 | |
4a096752 | 8186 | ClearPageChecked(page); |
d1b89bc0 | 8187 | detach_page_private(page); |
39279cc3 CM |
8188 | } |
8189 | ||
9ebefb18 CM |
8190 | /* |
8191 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8192 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8193 | * be careful to check for EOF conditions here. We set the page up correctly | |
8194 | * for a written page which means we get ENOSPC checking when writing into | |
8195 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8196 | * support these features. | |
8197 | * | |
8198 | * We are not allowed to take the i_mutex here so we have to play games to | |
8199 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8200 | * truncate_setsize() writes the inode size before removing pages, once we have |
8201 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8202 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8203 | * unlock the page. | |
8204 | */ | |
a528a241 | 8205 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8206 | { |
c2ec175c | 8207 | struct page *page = vmf->page; |
11bac800 | 8208 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8209 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8210 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8211 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8212 | struct extent_state *cached_state = NULL; |
364ecf36 | 8213 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
8214 | char *kaddr; |
8215 | unsigned long zero_start; | |
9ebefb18 | 8216 | loff_t size; |
a528a241 SJ |
8217 | vm_fault_t ret; |
8218 | int ret2; | |
9998eb70 | 8219 | int reserved = 0; |
d0b7da88 | 8220 | u64 reserved_space; |
a52d9a80 | 8221 | u64 page_start; |
e6dcd2dc | 8222 | u64 page_end; |
d0b7da88 CR |
8223 | u64 end; |
8224 | ||
09cbfeaf | 8225 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8226 | |
b2b5ef5c | 8227 | sb_start_pagefault(inode->i_sb); |
df480633 | 8228 | page_start = page_offset(page); |
09cbfeaf | 8229 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8230 | end = page_end; |
df480633 | 8231 | |
d0b7da88 CR |
8232 | /* |
8233 | * Reserving delalloc space after obtaining the page lock can lead to | |
8234 | * deadlock. For example, if a dirty page is locked by this function | |
8235 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8236 | * dirty page write out, then the btrfs_writepage() function could | |
8237 | * end up waiting indefinitely to get a lock on the page currently | |
8238 | * being processed by btrfs_page_mkwrite() function. | |
8239 | */ | |
a528a241 | 8240 | ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 8241 | reserved_space); |
a528a241 SJ |
8242 | if (!ret2) { |
8243 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8244 | reserved = 1; |
8245 | } | |
a528a241 SJ |
8246 | if (ret2) { |
8247 | ret = vmf_error(ret2); | |
9998eb70 CM |
8248 | if (reserved) |
8249 | goto out; | |
8250 | goto out_noreserve; | |
56a76f82 | 8251 | } |
1832a6d5 | 8252 | |
56a76f82 | 8253 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8254 | again: |
9ebefb18 | 8255 | lock_page(page); |
9ebefb18 | 8256 | size = i_size_read(inode); |
a52d9a80 | 8257 | |
9ebefb18 | 8258 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8259 | (page_start >= size)) { |
9ebefb18 CM |
8260 | /* page got truncated out from underneath us */ |
8261 | goto out_unlock; | |
8262 | } | |
e6dcd2dc CM |
8263 | wait_on_page_writeback(page); |
8264 | ||
ff13db41 | 8265 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
8266 | set_page_extent_mapped(page); |
8267 | ||
eb84ae03 CM |
8268 | /* |
8269 | * we can't set the delalloc bits if there are pending ordered | |
8270 | * extents. Drop our locks and wait for them to finish | |
8271 | */ | |
a776c6fa NB |
8272 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8273 | PAGE_SIZE); | |
e6dcd2dc | 8274 | if (ordered) { |
2ac55d41 | 8275 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8276 | &cached_state); |
e6dcd2dc | 8277 | unlock_page(page); |
eb84ae03 | 8278 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8279 | btrfs_put_ordered_extent(ordered); |
8280 | goto again; | |
8281 | } | |
8282 | ||
09cbfeaf | 8283 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8284 | reserved_space = round_up(size - page_start, |
0b246afa | 8285 | fs_info->sectorsize); |
09cbfeaf | 8286 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8287 | end = page_start + reserved_space - 1; |
bc42bda2 | 8288 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 QW |
8289 | page_start, PAGE_SIZE - reserved_space, |
8290 | true); | |
d0b7da88 CR |
8291 | } |
8292 | } | |
8293 | ||
fbf19087 | 8294 | /* |
5416034f LB |
8295 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8296 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8297 | * bits, thus in this case for space account reason, we still need to | |
8298 | * clear any delalloc bits within this page range since we have to | |
8299 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8300 | */ |
d0b7da88 | 8301 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d OS |
8302 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
8303 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
fbf19087 | 8304 | |
a528a241 | 8305 | ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0, |
330a5827 | 8306 | &cached_state); |
a528a241 | 8307 | if (ret2) { |
2ac55d41 | 8308 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8309 | &cached_state); |
9ed74f2d JB |
8310 | ret = VM_FAULT_SIGBUS; |
8311 | goto out_unlock; | |
8312 | } | |
9ebefb18 CM |
8313 | |
8314 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8315 | if (page_start + PAGE_SIZE > size) |
7073017a | 8316 | zero_start = offset_in_page(size); |
9ebefb18 | 8317 | else |
09cbfeaf | 8318 | zero_start = PAGE_SIZE; |
9ebefb18 | 8319 | |
09cbfeaf | 8320 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 8321 | kaddr = kmap(page); |
09cbfeaf | 8322 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
8323 | flush_dcache_page(page); |
8324 | kunmap(page); | |
8325 | } | |
247e743c | 8326 | ClearPageChecked(page); |
e6dcd2dc | 8327 | set_page_dirty(page); |
50a9b214 | 8328 | SetPageUptodate(page); |
5a3f23d5 | 8329 | |
0b246afa | 8330 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 8331 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 8332 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8333 | |
e43bbe5e | 8334 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
9ebefb18 | 8335 | |
76de60ed YY |
8336 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
8337 | sb_end_pagefault(inode->i_sb); | |
8338 | extent_changeset_free(data_reserved); | |
8339 | return VM_FAULT_LOCKED; | |
717beb96 CM |
8340 | |
8341 | out_unlock: | |
9ebefb18 | 8342 | unlock_page(page); |
1832a6d5 | 8343 | out: |
8702ba93 | 8344 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
bc42bda2 | 8345 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
43b18595 | 8346 | reserved_space, (ret != 0)); |
9998eb70 | 8347 | out_noreserve: |
b2b5ef5c | 8348 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8349 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8350 | return ret; |
8351 | } | |
8352 | ||
213e8c55 | 8353 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8354 | { |
0b246afa | 8355 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8356 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8357 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8358 | int ret; |
39279cc3 | 8359 | struct btrfs_trans_handle *trans; |
0b246afa | 8360 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 8361 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 8362 | |
213e8c55 FM |
8363 | if (!skip_writeback) { |
8364 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8365 | (u64)-1); | |
8366 | if (ret) | |
8367 | return ret; | |
8368 | } | |
39279cc3 | 8369 | |
fcb80c2a | 8370 | /* |
f7e9e8fc OS |
8371 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8372 | * things going on here: | |
fcb80c2a | 8373 | * |
f7e9e8fc | 8374 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8375 | * |
f7e9e8fc | 8376 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8377 | * be free'd up by the truncate operation, but also have some slack |
8378 | * space reserved in case it uses space during the truncate (thank you | |
8379 | * very much snapshotting). | |
8380 | * | |
f7e9e8fc | 8381 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8382 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8383 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8384 | * doesn't end up using space reserved for updating the inode. We also |
8385 | * need to be able to stop the transaction and start a new one, which | |
8386 | * means we need to be able to update the inode several times, and we | |
8387 | * have no idea of knowing how many times that will be, so we can't just | |
8388 | * reserve 1 item for the entirety of the operation, so that has to be | |
8389 | * done separately as well. | |
fcb80c2a JB |
8390 | * |
8391 | * So that leaves us with | |
8392 | * | |
f7e9e8fc | 8393 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 8394 | * transaction reservation. |
f7e9e8fc | 8395 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
8396 | * updating the inode. |
8397 | */ | |
2ff7e61e | 8398 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8399 | if (!rsv) |
8400 | return -ENOMEM; | |
4a338542 | 8401 | rsv->size = min_size; |
ca7e70f5 | 8402 | rsv->failfast = 1; |
f0cd846e | 8403 | |
907cbceb | 8404 | /* |
07127184 | 8405 | * 1 for the truncate slack space |
907cbceb JB |
8406 | * 1 for updating the inode. |
8407 | */ | |
f3fe820c | 8408 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 8409 | if (IS_ERR(trans)) { |
ad7e1a74 | 8410 | ret = PTR_ERR(trans); |
fcb80c2a JB |
8411 | goto out; |
8412 | } | |
f0cd846e | 8413 | |
907cbceb | 8414 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 8415 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 8416 | min_size, false); |
fcb80c2a | 8417 | BUG_ON(ret); |
f0cd846e | 8418 | |
5dc562c5 JB |
8419 | /* |
8420 | * So if we truncate and then write and fsync we normally would just | |
8421 | * write the extents that changed, which is a problem if we need to | |
8422 | * first truncate that entire inode. So set this flag so we write out | |
8423 | * all of the extents in the inode to the sync log so we're completely | |
8424 | * safe. | |
8425 | */ | |
8426 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8427 | trans->block_rsv = rsv; |
907cbceb | 8428 | |
8082510e YZ |
8429 | while (1) { |
8430 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8431 | inode->i_size, | |
8432 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 8433 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 8434 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 8435 | break; |
39279cc3 | 8436 | |
8082510e | 8437 | ret = btrfs_update_inode(trans, root, inode); |
ad7e1a74 | 8438 | if (ret) |
3893e33b | 8439 | break; |
ca7e70f5 | 8440 | |
3a45bb20 | 8441 | btrfs_end_transaction(trans); |
2ff7e61e | 8442 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
8443 | |
8444 | trans = btrfs_start_transaction(root, 2); | |
8445 | if (IS_ERR(trans)) { | |
ad7e1a74 | 8446 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
8447 | trans = NULL; |
8448 | break; | |
8449 | } | |
8450 | ||
63f018be | 8451 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
0b246afa | 8452 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 8453 | rsv, min_size, false); |
ca7e70f5 JB |
8454 | BUG_ON(ret); /* shouldn't happen */ |
8455 | trans->block_rsv = rsv; | |
8082510e YZ |
8456 | } |
8457 | ||
ddfae63c JB |
8458 | /* |
8459 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
8460 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
8461 | * we've truncated everything except the last little bit, and can do | |
8462 | * btrfs_truncate_block and then update the disk_i_size. | |
8463 | */ | |
8464 | if (ret == NEED_TRUNCATE_BLOCK) { | |
8465 | btrfs_end_transaction(trans); | |
8466 | btrfs_btree_balance_dirty(fs_info); | |
8467 | ||
8468 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
8469 | if (ret) | |
8470 | goto out; | |
8471 | trans = btrfs_start_transaction(root, 1); | |
8472 | if (IS_ERR(trans)) { | |
8473 | ret = PTR_ERR(trans); | |
8474 | goto out; | |
8475 | } | |
d923afe9 | 8476 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
ddfae63c JB |
8477 | } |
8478 | ||
917c16b2 | 8479 | if (trans) { |
ad7e1a74 OS |
8480 | int ret2; |
8481 | ||
0b246afa | 8482 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 OS |
8483 | ret2 = btrfs_update_inode(trans, root, inode); |
8484 | if (ret2 && !ret) | |
8485 | ret = ret2; | |
7b128766 | 8486 | |
ad7e1a74 OS |
8487 | ret2 = btrfs_end_transaction(trans); |
8488 | if (ret2 && !ret) | |
8489 | ret = ret2; | |
2ff7e61e | 8490 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 8491 | } |
fcb80c2a | 8492 | out: |
2ff7e61e | 8493 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 8494 | |
ad7e1a74 | 8495 | return ret; |
39279cc3 CM |
8496 | } |
8497 | ||
d352ac68 CM |
8498 | /* |
8499 | * create a new subvolume directory/inode (helper for the ioctl). | |
8500 | */ | |
d2fb3437 | 8501 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8502 | struct btrfs_root *new_root, |
8503 | struct btrfs_root *parent_root, | |
8504 | u64 new_dirid) | |
39279cc3 | 8505 | { |
39279cc3 | 8506 | struct inode *inode; |
76dda93c | 8507 | int err; |
00e4e6b3 | 8508 | u64 index = 0; |
39279cc3 | 8509 | |
12fc9d09 FA |
8510 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8511 | new_dirid, new_dirid, | |
8512 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8513 | &index); | |
54aa1f4d | 8514 | if (IS_ERR(inode)) |
f46b5a66 | 8515 | return PTR_ERR(inode); |
39279cc3 CM |
8516 | inode->i_op = &btrfs_dir_inode_operations; |
8517 | inode->i_fop = &btrfs_dir_file_operations; | |
8518 | ||
bfe86848 | 8519 | set_nlink(inode, 1); |
6ef06d27 | 8520 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 8521 | unlock_new_inode(inode); |
3b96362c | 8522 | |
63541927 FDBM |
8523 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8524 | if (err) | |
8525 | btrfs_err(new_root->fs_info, | |
351fd353 | 8526 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8527 | new_root->root_key.objectid, err); |
8528 | ||
76dda93c | 8529 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8530 | |
76dda93c | 8531 | iput(inode); |
ce598979 | 8532 | return err; |
39279cc3 CM |
8533 | } |
8534 | ||
39279cc3 CM |
8535 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8536 | { | |
69fe2d75 | 8537 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 8538 | struct btrfs_inode *ei; |
2ead6ae7 | 8539 | struct inode *inode; |
39279cc3 | 8540 | |
712e36c5 | 8541 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
8542 | if (!ei) |
8543 | return NULL; | |
2ead6ae7 YZ |
8544 | |
8545 | ei->root = NULL; | |
2ead6ae7 | 8546 | ei->generation = 0; |
15ee9bc7 | 8547 | ei->last_trans = 0; |
257c62e1 | 8548 | ei->last_sub_trans = 0; |
e02119d5 | 8549 | ei->logged_trans = 0; |
2ead6ae7 | 8550 | ei->delalloc_bytes = 0; |
a7e3b975 | 8551 | ei->new_delalloc_bytes = 0; |
47059d93 | 8552 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8553 | ei->disk_i_size = 0; |
8554 | ei->flags = 0; | |
7709cde3 | 8555 | ei->csum_bytes = 0; |
2ead6ae7 | 8556 | ei->index_cnt = (u64)-1; |
67de1176 | 8557 | ei->dir_index = 0; |
2ead6ae7 | 8558 | ei->last_unlink_trans = 0; |
46d8bc34 | 8559 | ei->last_log_commit = 0; |
2ead6ae7 | 8560 | |
9e0baf60 JB |
8561 | spin_lock_init(&ei->lock); |
8562 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
8563 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
8564 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
8565 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 8566 | ei->runtime_flags = 0; |
b52aa8c9 | 8567 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 8568 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8569 | |
16cdcec7 MX |
8570 | ei->delayed_node = NULL; |
8571 | ||
9cc97d64 | 8572 | ei->i_otime.tv_sec = 0; |
8573 | ei->i_otime.tv_nsec = 0; | |
8574 | ||
2ead6ae7 | 8575 | inode = &ei->vfs_inode; |
a8067e02 | 8576 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 QW |
8577 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
8578 | extent_io_tree_init(fs_info, &ei->io_failure_tree, | |
8579 | IO_TREE_INODE_IO_FAILURE, inode); | |
41a2ee75 JB |
8580 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
8581 | IO_TREE_INODE_FILE_EXTENT, inode); | |
7b439738 DS |
8582 | ei->io_tree.track_uptodate = true; |
8583 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 8584 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8585 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 8586 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8587 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 8588 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 8589 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 8590 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
8591 | |
8592 | return inode; | |
39279cc3 CM |
8593 | } |
8594 | ||
aaedb55b JB |
8595 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8596 | void btrfs_test_destroy_inode(struct inode *inode) | |
8597 | { | |
dcdbc059 | 8598 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
8599 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8600 | } | |
8601 | #endif | |
8602 | ||
26602cab | 8603 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 8604 | { |
fa0d7e3d NP |
8605 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8606 | } | |
8607 | ||
39279cc3 CM |
8608 | void btrfs_destroy_inode(struct inode *inode) |
8609 | { | |
0b246afa | 8610 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 8611 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
8612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8613 | ||
b3d9b7a3 | 8614 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 8615 | WARN_ON(inode->i_data.nrpages); |
69fe2d75 JB |
8616 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
8617 | WARN_ON(BTRFS_I(inode)->block_rsv.size); | |
9e0baf60 | 8618 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7709cde3 | 8619 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 8620 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 8621 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 8622 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 8623 | |
a6dbd429 JB |
8624 | /* |
8625 | * This can happen where we create an inode, but somebody else also | |
8626 | * created the same inode and we need to destroy the one we already | |
8627 | * created. | |
8628 | */ | |
8629 | if (!root) | |
26602cab | 8630 | return; |
a6dbd429 | 8631 | |
d397712b | 8632 | while (1) { |
e6dcd2dc CM |
8633 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
8634 | if (!ordered) | |
8635 | break; | |
8636 | else { | |
0b246afa | 8637 | btrfs_err(fs_info, |
5d163e0e | 8638 | "found ordered extent %llu %llu on inode cleanup", |
bffe633e | 8639 | ordered->file_offset, ordered->num_bytes); |
e6dcd2dc CM |
8640 | btrfs_remove_ordered_extent(inode, ordered); |
8641 | btrfs_put_ordered_extent(ordered); | |
8642 | btrfs_put_ordered_extent(ordered); | |
8643 | } | |
8644 | } | |
56fa9d07 | 8645 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 8646 | inode_tree_del(inode); |
dcdbc059 | 8647 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
41a2ee75 | 8648 | btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1); |
5c8fd99f | 8649 | btrfs_put_root(BTRFS_I(inode)->root); |
39279cc3 CM |
8650 | } |
8651 | ||
45321ac5 | 8652 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8653 | { |
8654 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8655 | |
6379ef9f NA |
8656 | if (root == NULL) |
8657 | return 1; | |
8658 | ||
fa6ac876 | 8659 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8660 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8661 | return 1; |
76dda93c | 8662 | else |
45321ac5 | 8663 | return generic_drop_inode(inode); |
76dda93c YZ |
8664 | } |
8665 | ||
0ee0fda0 | 8666 | static void init_once(void *foo) |
39279cc3 CM |
8667 | { |
8668 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8669 | ||
8670 | inode_init_once(&ei->vfs_inode); | |
8671 | } | |
8672 | ||
e67c718b | 8673 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 8674 | { |
8c0a8537 KS |
8675 | /* |
8676 | * Make sure all delayed rcu free inodes are flushed before we | |
8677 | * destroy cache. | |
8678 | */ | |
8679 | rcu_barrier(); | |
5598e900 KM |
8680 | kmem_cache_destroy(btrfs_inode_cachep); |
8681 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
8682 | kmem_cache_destroy(btrfs_path_cachep); |
8683 | kmem_cache_destroy(btrfs_free_space_cachep); | |
3acd4850 | 8684 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
39279cc3 CM |
8685 | } |
8686 | ||
f5c29bd9 | 8687 | int __init btrfs_init_cachep(void) |
39279cc3 | 8688 | { |
837e1972 | 8689 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 8690 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
8691 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
8692 | init_once); | |
39279cc3 CM |
8693 | if (!btrfs_inode_cachep) |
8694 | goto fail; | |
9601e3f6 | 8695 | |
837e1972 | 8696 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 8697 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 8698 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8699 | if (!btrfs_trans_handle_cachep) |
8700 | goto fail; | |
9601e3f6 | 8701 | |
837e1972 | 8702 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 8703 | sizeof(struct btrfs_path), 0, |
fba4b697 | 8704 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
8705 | if (!btrfs_path_cachep) |
8706 | goto fail; | |
9601e3f6 | 8707 | |
837e1972 | 8708 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 8709 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 8710 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
8711 | if (!btrfs_free_space_cachep) |
8712 | goto fail; | |
8713 | ||
3acd4850 CL |
8714 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
8715 | PAGE_SIZE, PAGE_SIZE, | |
8716 | SLAB_RED_ZONE, NULL); | |
8717 | if (!btrfs_free_space_bitmap_cachep) | |
8718 | goto fail; | |
8719 | ||
39279cc3 CM |
8720 | return 0; |
8721 | fail: | |
8722 | btrfs_destroy_cachep(); | |
8723 | return -ENOMEM; | |
8724 | } | |
8725 | ||
a528d35e DH |
8726 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
8727 | u32 request_mask, unsigned int flags) | |
39279cc3 | 8728 | { |
df0af1a5 | 8729 | u64 delalloc_bytes; |
a528d35e | 8730 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 8731 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
8732 | u32 bi_flags = BTRFS_I(inode)->flags; |
8733 | ||
8734 | stat->result_mask |= STATX_BTIME; | |
8735 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
8736 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
8737 | if (bi_flags & BTRFS_INODE_APPEND) | |
8738 | stat->attributes |= STATX_ATTR_APPEND; | |
8739 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
8740 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
8741 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
8742 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
8743 | if (bi_flags & BTRFS_INODE_NODUMP) | |
8744 | stat->attributes |= STATX_ATTR_NODUMP; | |
8745 | ||
8746 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
8747 | STATX_ATTR_COMPRESSED | | |
8748 | STATX_ATTR_IMMUTABLE | | |
8749 | STATX_ATTR_NODUMP); | |
fadc0d8b | 8750 | |
39279cc3 | 8751 | generic_fillattr(inode, stat); |
0ee5dc67 | 8752 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
8753 | |
8754 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 8755 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 8756 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 8757 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 8758 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
8759 | return 0; |
8760 | } | |
8761 | ||
cdd1fedf DF |
8762 | static int btrfs_rename_exchange(struct inode *old_dir, |
8763 | struct dentry *old_dentry, | |
8764 | struct inode *new_dir, | |
8765 | struct dentry *new_dentry) | |
8766 | { | |
0b246afa | 8767 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
8768 | struct btrfs_trans_handle *trans; |
8769 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
8770 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
8771 | struct inode *new_inode = new_dentry->d_inode; | |
8772 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 8773 | struct timespec64 ctime = current_time(old_inode); |
cdd1fedf | 8774 | struct dentry *parent; |
4a0cc7ca NB |
8775 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
8776 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
8777 | u64 old_idx = 0; |
8778 | u64 new_idx = 0; | |
cdd1fedf | 8779 | int ret; |
86e8aa0e FM |
8780 | bool root_log_pinned = false; |
8781 | bool dest_log_pinned = false; | |
d4682ba0 FM |
8782 | struct btrfs_log_ctx ctx_root; |
8783 | struct btrfs_log_ctx ctx_dest; | |
8784 | bool sync_log_root = false; | |
8785 | bool sync_log_dest = false; | |
8786 | bool commit_transaction = false; | |
cdd1fedf DF |
8787 | |
8788 | /* we only allow rename subvolume link between subvolumes */ | |
8789 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
8790 | return -EXDEV; | |
8791 | ||
d4682ba0 FM |
8792 | btrfs_init_log_ctx(&ctx_root, old_inode); |
8793 | btrfs_init_log_ctx(&ctx_dest, new_inode); | |
8794 | ||
cdd1fedf | 8795 | /* close the race window with snapshot create/destroy ioctl */ |
943eb3bf JB |
8796 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
8797 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 8798 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
8799 | |
8800 | /* | |
8801 | * We want to reserve the absolute worst case amount of items. So if | |
8802 | * both inodes are subvols and we need to unlink them then that would | |
8803 | * require 4 item modifications, but if they are both normal inodes it | |
8804 | * would require 5 item modifications, so we'll assume their normal | |
8805 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
8806 | * should cover the worst case number of items we'll modify. | |
8807 | */ | |
8808 | trans = btrfs_start_transaction(root, 12); | |
8809 | if (IS_ERR(trans)) { | |
8810 | ret = PTR_ERR(trans); | |
8811 | goto out_notrans; | |
8812 | } | |
8813 | ||
3e174099 JB |
8814 | if (dest != root) |
8815 | btrfs_record_root_in_trans(trans, dest); | |
8816 | ||
cdd1fedf DF |
8817 | /* |
8818 | * We need to find a free sequence number both in the source and | |
8819 | * in the destination directory for the exchange. | |
8820 | */ | |
877574e2 | 8821 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
8822 | if (ret) |
8823 | goto out_fail; | |
877574e2 | 8824 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
8825 | if (ret) |
8826 | goto out_fail; | |
8827 | ||
8828 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
8829 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
8830 | ||
8831 | /* Reference for the source. */ | |
8832 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8833 | /* force full log commit if subvolume involved. */ | |
90787766 | 8834 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8835 | } else { |
376e5a57 FM |
8836 | btrfs_pin_log_trans(root); |
8837 | root_log_pinned = true; | |
cdd1fedf DF |
8838 | ret = btrfs_insert_inode_ref(trans, dest, |
8839 | new_dentry->d_name.name, | |
8840 | new_dentry->d_name.len, | |
8841 | old_ino, | |
f85b7379 DS |
8842 | btrfs_ino(BTRFS_I(new_dir)), |
8843 | old_idx); | |
cdd1fedf DF |
8844 | if (ret) |
8845 | goto out_fail; | |
cdd1fedf DF |
8846 | } |
8847 | ||
8848 | /* And now for the dest. */ | |
8849 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
8850 | /* force full log commit if subvolume involved. */ | |
90787766 | 8851 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 8852 | } else { |
376e5a57 FM |
8853 | btrfs_pin_log_trans(dest); |
8854 | dest_log_pinned = true; | |
cdd1fedf DF |
8855 | ret = btrfs_insert_inode_ref(trans, root, |
8856 | old_dentry->d_name.name, | |
8857 | old_dentry->d_name.len, | |
8858 | new_ino, | |
f85b7379 DS |
8859 | btrfs_ino(BTRFS_I(old_dir)), |
8860 | new_idx); | |
cdd1fedf DF |
8861 | if (ret) |
8862 | goto out_fail; | |
cdd1fedf DF |
8863 | } |
8864 | ||
8865 | /* Update inode version and ctime/mtime. */ | |
8866 | inode_inc_iversion(old_dir); | |
8867 | inode_inc_iversion(new_dir); | |
8868 | inode_inc_iversion(old_inode); | |
8869 | inode_inc_iversion(new_inode); | |
8870 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
8871 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
8872 | old_inode->i_ctime = ctime; | |
8873 | new_inode->i_ctime = ctime; | |
8874 | ||
8875 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
8876 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
8877 | BTRFS_I(old_inode), 1); | |
8878 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
8879 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
8880 | } |
8881 | ||
8882 | /* src is a subvolume */ | |
8883 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8884 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
cdd1fedf | 8885 | } else { /* src is an inode */ |
4ec5934e NB |
8886 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
8887 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
8888 | old_dentry->d_name.name, |
8889 | old_dentry->d_name.len); | |
8890 | if (!ret) | |
8891 | ret = btrfs_update_inode(trans, root, old_inode); | |
8892 | } | |
8893 | if (ret) { | |
66642832 | 8894 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8895 | goto out_fail; |
8896 | } | |
8897 | ||
8898 | /* dest is a subvolume */ | |
8899 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
045d3967 | 8900 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
cdd1fedf | 8901 | } else { /* dest is an inode */ |
4ec5934e NB |
8902 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
8903 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
8904 | new_dentry->d_name.name, |
8905 | new_dentry->d_name.len); | |
8906 | if (!ret) | |
8907 | ret = btrfs_update_inode(trans, dest, new_inode); | |
8908 | } | |
8909 | if (ret) { | |
66642832 | 8910 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8911 | goto out_fail; |
8912 | } | |
8913 | ||
db0a669f | 8914 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
8915 | new_dentry->d_name.name, |
8916 | new_dentry->d_name.len, 0, old_idx); | |
8917 | if (ret) { | |
66642832 | 8918 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8919 | goto out_fail; |
8920 | } | |
8921 | ||
db0a669f | 8922 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
8923 | old_dentry->d_name.name, |
8924 | old_dentry->d_name.len, 0, new_idx); | |
8925 | if (ret) { | |
66642832 | 8926 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
8927 | goto out_fail; |
8928 | } | |
8929 | ||
8930 | if (old_inode->i_nlink == 1) | |
8931 | BTRFS_I(old_inode)->dir_index = old_idx; | |
8932 | if (new_inode->i_nlink == 1) | |
8933 | BTRFS_I(new_inode)->dir_index = new_idx; | |
8934 | ||
86e8aa0e | 8935 | if (root_log_pinned) { |
cdd1fedf | 8936 | parent = new_dentry->d_parent; |
d4682ba0 FM |
8937 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), |
8938 | BTRFS_I(old_dir), parent, | |
8939 | false, &ctx_root); | |
8940 | if (ret == BTRFS_NEED_LOG_SYNC) | |
8941 | sync_log_root = true; | |
8942 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
8943 | commit_transaction = true; | |
8944 | ret = 0; | |
cdd1fedf | 8945 | btrfs_end_log_trans(root); |
86e8aa0e | 8946 | root_log_pinned = false; |
cdd1fedf | 8947 | } |
86e8aa0e | 8948 | if (dest_log_pinned) { |
d4682ba0 FM |
8949 | if (!commit_transaction) { |
8950 | parent = old_dentry->d_parent; | |
8951 | ret = btrfs_log_new_name(trans, BTRFS_I(new_inode), | |
8952 | BTRFS_I(new_dir), parent, | |
8953 | false, &ctx_dest); | |
8954 | if (ret == BTRFS_NEED_LOG_SYNC) | |
8955 | sync_log_dest = true; | |
8956 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
8957 | commit_transaction = true; | |
8958 | ret = 0; | |
8959 | } | |
cdd1fedf | 8960 | btrfs_end_log_trans(dest); |
86e8aa0e | 8961 | dest_log_pinned = false; |
cdd1fedf DF |
8962 | } |
8963 | out_fail: | |
86e8aa0e FM |
8964 | /* |
8965 | * If we have pinned a log and an error happened, we unpin tasks | |
8966 | * trying to sync the log and force them to fallback to a transaction | |
8967 | * commit if the log currently contains any of the inodes involved in | |
8968 | * this rename operation (to ensure we do not persist a log with an | |
8969 | * inconsistent state for any of these inodes or leading to any | |
8970 | * inconsistencies when replayed). If the transaction was aborted, the | |
8971 | * abortion reason is propagated to userspace when attempting to commit | |
8972 | * the transaction. If the log does not contain any of these inodes, we | |
8973 | * allow the tasks to sync it. | |
8974 | */ | |
8975 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
8976 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
8977 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
8978 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 8979 | (new_inode && |
0f8939b8 | 8980 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 8981 | btrfs_set_log_full_commit(trans); |
86e8aa0e FM |
8982 | |
8983 | if (root_log_pinned) { | |
8984 | btrfs_end_log_trans(root); | |
8985 | root_log_pinned = false; | |
8986 | } | |
8987 | if (dest_log_pinned) { | |
8988 | btrfs_end_log_trans(dest); | |
8989 | dest_log_pinned = false; | |
8990 | } | |
8991 | } | |
d4682ba0 FM |
8992 | if (!ret && sync_log_root && !commit_transaction) { |
8993 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, | |
8994 | &ctx_root); | |
8995 | if (ret) | |
8996 | commit_transaction = true; | |
8997 | } | |
8998 | if (!ret && sync_log_dest && !commit_transaction) { | |
8999 | ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root, | |
9000 | &ctx_dest); | |
9001 | if (ret) | |
9002 | commit_transaction = true; | |
9003 | } | |
9004 | if (commit_transaction) { | |
e6c61710 FM |
9005 | /* |
9006 | * We may have set commit_transaction when logging the new name | |
9007 | * in the destination root, in which case we left the source | |
9008 | * root context in the list of log contextes. So make sure we | |
9009 | * remove it to avoid invalid memory accesses, since the context | |
9010 | * was allocated in our stack frame. | |
9011 | */ | |
9012 | if (sync_log_root) { | |
9013 | mutex_lock(&root->log_mutex); | |
9014 | list_del_init(&ctx_root.list); | |
9015 | mutex_unlock(&root->log_mutex); | |
9016 | } | |
d4682ba0 FM |
9017 | ret = btrfs_commit_transaction(trans); |
9018 | } else { | |
9019 | int ret2; | |
9020 | ||
9021 | ret2 = btrfs_end_transaction(trans); | |
9022 | ret = ret ? ret : ret2; | |
9023 | } | |
cdd1fedf | 9024 | out_notrans: |
943eb3bf JB |
9025 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
9026 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9027 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9028 | |
e6c61710 FM |
9029 | ASSERT(list_empty(&ctx_root.list)); |
9030 | ASSERT(list_empty(&ctx_dest.list)); | |
9031 | ||
cdd1fedf DF |
9032 | return ret; |
9033 | } | |
9034 | ||
9035 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9036 | struct btrfs_root *root, | |
9037 | struct inode *dir, | |
9038 | struct dentry *dentry) | |
9039 | { | |
9040 | int ret; | |
9041 | struct inode *inode; | |
9042 | u64 objectid; | |
9043 | u64 index; | |
9044 | ||
9045 | ret = btrfs_find_free_ino(root, &objectid); | |
9046 | if (ret) | |
9047 | return ret; | |
9048 | ||
9049 | inode = btrfs_new_inode(trans, root, dir, | |
9050 | dentry->d_name.name, | |
9051 | dentry->d_name.len, | |
4a0cc7ca | 9052 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9053 | objectid, |
9054 | S_IFCHR | WHITEOUT_MODE, | |
9055 | &index); | |
9056 | ||
9057 | if (IS_ERR(inode)) { | |
9058 | ret = PTR_ERR(inode); | |
9059 | return ret; | |
9060 | } | |
9061 | ||
9062 | inode->i_op = &btrfs_special_inode_operations; | |
9063 | init_special_inode(inode, inode->i_mode, | |
9064 | WHITEOUT_DEV); | |
9065 | ||
9066 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9067 | &dentry->d_name); | |
9068 | if (ret) | |
c9901618 | 9069 | goto out; |
cdd1fedf | 9070 | |
cef415af NB |
9071 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9072 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9073 | if (ret) |
c9901618 | 9074 | goto out; |
cdd1fedf DF |
9075 | |
9076 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9077 | out: |
cdd1fedf | 9078 | unlock_new_inode(inode); |
c9901618 FM |
9079 | if (ret) |
9080 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9081 | iput(inode); |
9082 | ||
c9901618 | 9083 | return ret; |
cdd1fedf DF |
9084 | } |
9085 | ||
d397712b | 9086 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9087 | struct inode *new_dir, struct dentry *new_dentry, |
9088 | unsigned int flags) | |
39279cc3 | 9089 | { |
0b246afa | 9090 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9091 | struct btrfs_trans_handle *trans; |
5062af35 | 9092 | unsigned int trans_num_items; |
39279cc3 | 9093 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9094 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9095 | struct inode *new_inode = d_inode(new_dentry); |
9096 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9097 | u64 index = 0; |
39279cc3 | 9098 | int ret; |
4a0cc7ca | 9099 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9100 | bool log_pinned = false; |
d4682ba0 FM |
9101 | struct btrfs_log_ctx ctx; |
9102 | bool sync_log = false; | |
9103 | bool commit_transaction = false; | |
39279cc3 | 9104 | |
4a0cc7ca | 9105 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9106 | return -EPERM; |
9107 | ||
4df27c4d | 9108 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9109 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9110 | return -EXDEV; |
9111 | ||
33345d01 | 9112 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9113 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9114 | return -ENOTEMPTY; |
5f39d397 | 9115 | |
4df27c4d YZ |
9116 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9117 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9118 | return -ENOTEMPTY; | |
9c52057c CM |
9119 | |
9120 | ||
9121 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9122 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9123 | new_dentry->d_name.name, |
9124 | new_dentry->d_name.len); | |
9125 | ||
9126 | if (ret) { | |
9127 | if (ret == -EEXIST) { | |
9128 | /* we shouldn't get | |
9129 | * eexist without a new_inode */ | |
fae7f21c | 9130 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9131 | return ret; |
9132 | } | |
9133 | } else { | |
9134 | /* maybe -EOVERFLOW */ | |
9135 | return ret; | |
9136 | } | |
9137 | } | |
9138 | ret = 0; | |
9139 | ||
5a3f23d5 | 9140 | /* |
8d875f95 CM |
9141 | * we're using rename to replace one file with another. Start IO on it |
9142 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9143 | */ |
8d875f95 | 9144 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9145 | filemap_flush(old_inode->i_mapping); |
9146 | ||
76dda93c | 9147 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9148 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9149 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9150 | /* |
9151 | * We want to reserve the absolute worst case amount of items. So if | |
9152 | * both inodes are subvols and we need to unlink them then that would | |
9153 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9154 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9155 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9156 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9157 | * If our rename has the whiteout flag, we need more 5 units for the |
9158 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9159 | * when selinux is enabled). | |
a22285a6 | 9160 | */ |
5062af35 FM |
9161 | trans_num_items = 11; |
9162 | if (flags & RENAME_WHITEOUT) | |
9163 | trans_num_items += 5; | |
9164 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9165 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9166 | ret = PTR_ERR(trans); |
9167 | goto out_notrans; | |
9168 | } | |
76dda93c | 9169 | |
4df27c4d YZ |
9170 | if (dest != root) |
9171 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9172 | |
877574e2 | 9173 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9174 | if (ret) |
9175 | goto out_fail; | |
5a3f23d5 | 9176 | |
67de1176 | 9177 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9178 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9179 | /* force full log commit if subvolume involved. */ |
90787766 | 9180 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9181 | } else { |
c4aba954 FM |
9182 | btrfs_pin_log_trans(root); |
9183 | log_pinned = true; | |
a5719521 YZ |
9184 | ret = btrfs_insert_inode_ref(trans, dest, |
9185 | new_dentry->d_name.name, | |
9186 | new_dentry->d_name.len, | |
33345d01 | 9187 | old_ino, |
4a0cc7ca | 9188 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9189 | if (ret) |
9190 | goto out_fail; | |
4df27c4d | 9191 | } |
5a3f23d5 | 9192 | |
0c4d2d95 JB |
9193 | inode_inc_iversion(old_dir); |
9194 | inode_inc_iversion(new_dir); | |
9195 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9196 | old_dir->i_ctime = old_dir->i_mtime = |
9197 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9198 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9199 | |
12fcfd22 | 9200 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9201 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9202 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9203 | |
33345d01 | 9204 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
045d3967 | 9205 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
4df27c4d | 9206 | } else { |
4ec5934e NB |
9207 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9208 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9209 | old_dentry->d_name.name, |
9210 | old_dentry->d_name.len); | |
9211 | if (!ret) | |
9212 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9213 | } |
79787eaa | 9214 | if (ret) { |
66642832 | 9215 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9216 | goto out_fail; |
9217 | } | |
39279cc3 CM |
9218 | |
9219 | if (new_inode) { | |
0c4d2d95 | 9220 | inode_inc_iversion(new_inode); |
c2050a45 | 9221 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9222 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d | 9223 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
045d3967 | 9224 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
4df27c4d YZ |
9225 | BUG_ON(new_inode->i_nlink == 0); |
9226 | } else { | |
4ec5934e NB |
9227 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9228 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
9229 | new_dentry->d_name.name, |
9230 | new_dentry->d_name.len); | |
9231 | } | |
4ef31a45 | 9232 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9233 | ret = btrfs_orphan_add(trans, |
9234 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9235 | if (ret) { |
66642832 | 9236 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9237 | goto out_fail; |
9238 | } | |
39279cc3 | 9239 | } |
aec7477b | 9240 | |
db0a669f | 9241 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 9242 | new_dentry->d_name.name, |
a5719521 | 9243 | new_dentry->d_name.len, 0, index); |
79787eaa | 9244 | if (ret) { |
66642832 | 9245 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9246 | goto out_fail; |
9247 | } | |
39279cc3 | 9248 | |
67de1176 MX |
9249 | if (old_inode->i_nlink == 1) |
9250 | BTRFS_I(old_inode)->dir_index = index; | |
9251 | ||
3dc9e8f7 | 9252 | if (log_pinned) { |
10d9f309 | 9253 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 9254 | |
d4682ba0 FM |
9255 | btrfs_init_log_ctx(&ctx, old_inode); |
9256 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), | |
9257 | BTRFS_I(old_dir), parent, | |
9258 | false, &ctx); | |
9259 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9260 | sync_log = true; | |
9261 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9262 | commit_transaction = true; | |
9263 | ret = 0; | |
4df27c4d | 9264 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9265 | log_pinned = false; |
4df27c4d | 9266 | } |
cdd1fedf DF |
9267 | |
9268 | if (flags & RENAME_WHITEOUT) { | |
9269 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9270 | old_dentry); | |
9271 | ||
9272 | if (ret) { | |
66642832 | 9273 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9274 | goto out_fail; |
9275 | } | |
4df27c4d | 9276 | } |
39279cc3 | 9277 | out_fail: |
3dc9e8f7 FM |
9278 | /* |
9279 | * If we have pinned the log and an error happened, we unpin tasks | |
9280 | * trying to sync the log and force them to fallback to a transaction | |
9281 | * commit if the log currently contains any of the inodes involved in | |
9282 | * this rename operation (to ensure we do not persist a log with an | |
9283 | * inconsistent state for any of these inodes or leading to any | |
9284 | * inconsistencies when replayed). If the transaction was aborted, the | |
9285 | * abortion reason is propagated to userspace when attempting to commit | |
9286 | * the transaction. If the log does not contain any of these inodes, we | |
9287 | * allow the tasks to sync it. | |
9288 | */ | |
9289 | if (ret && log_pinned) { | |
0f8939b8 NB |
9290 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9291 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9292 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9293 | (new_inode && |
0f8939b8 | 9294 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 9295 | btrfs_set_log_full_commit(trans); |
3dc9e8f7 FM |
9296 | |
9297 | btrfs_end_log_trans(root); | |
9298 | log_pinned = false; | |
9299 | } | |
d4682ba0 FM |
9300 | if (!ret && sync_log) { |
9301 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx); | |
9302 | if (ret) | |
9303 | commit_transaction = true; | |
236ebc20 FM |
9304 | } else if (sync_log) { |
9305 | mutex_lock(&root->log_mutex); | |
9306 | list_del(&ctx.list); | |
9307 | mutex_unlock(&root->log_mutex); | |
d4682ba0 FM |
9308 | } |
9309 | if (commit_transaction) { | |
9310 | ret = btrfs_commit_transaction(trans); | |
9311 | } else { | |
9312 | int ret2; | |
9313 | ||
9314 | ret2 = btrfs_end_transaction(trans); | |
9315 | ret = ret ? ret : ret2; | |
9316 | } | |
b44c59a8 | 9317 | out_notrans: |
33345d01 | 9318 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9319 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9320 | |
39279cc3 CM |
9321 | return ret; |
9322 | } | |
9323 | ||
80ace85c MS |
9324 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9325 | struct inode *new_dir, struct dentry *new_dentry, | |
9326 | unsigned int flags) | |
9327 | { | |
cdd1fedf | 9328 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9329 | return -EINVAL; |
9330 | ||
cdd1fedf DF |
9331 | if (flags & RENAME_EXCHANGE) |
9332 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9333 | new_dentry); | |
9334 | ||
9335 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9336 | } |
9337 | ||
3a2f8c07 NB |
9338 | struct btrfs_delalloc_work { |
9339 | struct inode *inode; | |
9340 | struct completion completion; | |
9341 | struct list_head list; | |
9342 | struct btrfs_work work; | |
9343 | }; | |
9344 | ||
8ccf6f19 MX |
9345 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9346 | { | |
9347 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9348 | struct inode *inode; |
8ccf6f19 MX |
9349 | |
9350 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9351 | work); | |
9f23e289 | 9352 | inode = delalloc_work->inode; |
30424601 DS |
9353 | filemap_flush(inode->i_mapping); |
9354 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9355 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9356 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9357 | |
076da91c | 9358 | iput(inode); |
8ccf6f19 MX |
9359 | complete(&delalloc_work->completion); |
9360 | } | |
9361 | ||
3a2f8c07 | 9362 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9363 | { |
9364 | struct btrfs_delalloc_work *work; | |
9365 | ||
100d5702 | 9366 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9367 | if (!work) |
9368 | return NULL; | |
9369 | ||
9370 | init_completion(&work->completion); | |
9371 | INIT_LIST_HEAD(&work->list); | |
9372 | work->inode = inode; | |
a0cac0ec | 9373 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
9374 | |
9375 | return work; | |
9376 | } | |
9377 | ||
d352ac68 CM |
9378 | /* |
9379 | * some fairly slow code that needs optimization. This walks the list | |
9380 | * of all the inodes with pending delalloc and forces them to disk. | |
9381 | */ | |
3cd24c69 | 9382 | static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot) |
ea8c2819 | 9383 | { |
ea8c2819 | 9384 | struct btrfs_inode *binode; |
5b21f2ed | 9385 | struct inode *inode; |
8ccf6f19 MX |
9386 | struct btrfs_delalloc_work *work, *next; |
9387 | struct list_head works; | |
1eafa6c7 | 9388 | struct list_head splice; |
8ccf6f19 | 9389 | int ret = 0; |
ea8c2819 | 9390 | |
8ccf6f19 | 9391 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9392 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9393 | |
573bfb72 | 9394 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9395 | spin_lock(&root->delalloc_lock); |
9396 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9397 | while (!list_empty(&splice)) { |
9398 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9399 | delalloc_inodes); |
1eafa6c7 | 9400 | |
eb73c1b7 MX |
9401 | list_move_tail(&binode->delalloc_inodes, |
9402 | &root->delalloc_inodes); | |
5b21f2ed | 9403 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9404 | if (!inode) { |
eb73c1b7 | 9405 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9406 | continue; |
df0af1a5 | 9407 | } |
eb73c1b7 | 9408 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 9409 | |
3cd24c69 EL |
9410 | if (snapshot) |
9411 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
9412 | &binode->runtime_flags); | |
076da91c | 9413 | work = btrfs_alloc_delalloc_work(inode); |
5d99a998 | 9414 | if (!work) { |
4fbb5147 | 9415 | iput(inode); |
1eafa6c7 | 9416 | ret = -ENOMEM; |
a1ecaabb | 9417 | goto out; |
5b21f2ed | 9418 | } |
1eafa6c7 | 9419 | list_add_tail(&work->list, &works); |
a44903ab QW |
9420 | btrfs_queue_work(root->fs_info->flush_workers, |
9421 | &work->work); | |
6c255e67 MX |
9422 | ret++; |
9423 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 9424 | goto out; |
5b21f2ed | 9425 | cond_resched(); |
eb73c1b7 | 9426 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9427 | } |
eb73c1b7 | 9428 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9429 | |
a1ecaabb | 9430 | out: |
eb73c1b7 MX |
9431 | list_for_each_entry_safe(work, next, &works, list) { |
9432 | list_del_init(&work->list); | |
40012f96 NB |
9433 | wait_for_completion(&work->completion); |
9434 | kfree(work); | |
eb73c1b7 MX |
9435 | } |
9436 | ||
81f1d390 | 9437 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9438 | spin_lock(&root->delalloc_lock); |
9439 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9440 | spin_unlock(&root->delalloc_lock); | |
9441 | } | |
573bfb72 | 9442 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9443 | return ret; |
9444 | } | |
1eafa6c7 | 9445 | |
3cd24c69 | 9446 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
eb73c1b7 | 9447 | { |
0b246afa | 9448 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 9449 | int ret; |
1eafa6c7 | 9450 | |
0b246afa | 9451 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9452 | return -EROFS; |
9453 | ||
3cd24c69 | 9454 | ret = start_delalloc_inodes(root, -1, true); |
6c255e67 MX |
9455 | if (ret > 0) |
9456 | ret = 0; | |
eb73c1b7 MX |
9457 | return ret; |
9458 | } | |
9459 | ||
82b3e53b | 9460 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr) |
eb73c1b7 MX |
9461 | { |
9462 | struct btrfs_root *root; | |
9463 | struct list_head splice; | |
9464 | int ret; | |
9465 | ||
2c21b4d7 | 9466 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9467 | return -EROFS; |
9468 | ||
9469 | INIT_LIST_HEAD(&splice); | |
9470 | ||
573bfb72 | 9471 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9472 | spin_lock(&fs_info->delalloc_root_lock); |
9473 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9474 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9475 | root = list_first_entry(&splice, struct btrfs_root, |
9476 | delalloc_root); | |
00246528 | 9477 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
9478 | BUG_ON(!root); |
9479 | list_move_tail(&root->delalloc_root, | |
9480 | &fs_info->delalloc_roots); | |
9481 | spin_unlock(&fs_info->delalloc_root_lock); | |
9482 | ||
3cd24c69 | 9483 | ret = start_delalloc_inodes(root, nr, false); |
00246528 | 9484 | btrfs_put_root(root); |
6c255e67 | 9485 | if (ret < 0) |
eb73c1b7 MX |
9486 | goto out; |
9487 | ||
6c255e67 MX |
9488 | if (nr != -1) { |
9489 | nr -= ret; | |
9490 | WARN_ON(nr < 0); | |
9491 | } | |
eb73c1b7 | 9492 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9493 | } |
eb73c1b7 | 9494 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9495 | |
6c255e67 | 9496 | ret = 0; |
eb73c1b7 | 9497 | out: |
81f1d390 | 9498 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
9499 | spin_lock(&fs_info->delalloc_root_lock); |
9500 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9501 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9502 | } |
573bfb72 | 9503 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9504 | return ret; |
ea8c2819 CM |
9505 | } |
9506 | ||
39279cc3 CM |
9507 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9508 | const char *symname) | |
9509 | { | |
0b246afa | 9510 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
9511 | struct btrfs_trans_handle *trans; |
9512 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9513 | struct btrfs_path *path; | |
9514 | struct btrfs_key key; | |
1832a6d5 | 9515 | struct inode *inode = NULL; |
39279cc3 | 9516 | int err; |
39279cc3 | 9517 | u64 objectid; |
67871254 | 9518 | u64 index = 0; |
39279cc3 CM |
9519 | int name_len; |
9520 | int datasize; | |
5f39d397 | 9521 | unsigned long ptr; |
39279cc3 | 9522 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9523 | struct extent_buffer *leaf; |
39279cc3 | 9524 | |
f06becc4 | 9525 | name_len = strlen(symname); |
0b246afa | 9526 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 9527 | return -ENAMETOOLONG; |
1832a6d5 | 9528 | |
9ed74f2d JB |
9529 | /* |
9530 | * 2 items for inode item and ref | |
9531 | * 2 items for dir items | |
9269d12b FM |
9532 | * 1 item for updating parent inode item |
9533 | * 1 item for the inline extent item | |
9ed74f2d JB |
9534 | * 1 item for xattr if selinux is on |
9535 | */ | |
9269d12b | 9536 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
9537 | if (IS_ERR(trans)) |
9538 | return PTR_ERR(trans); | |
1832a6d5 | 9539 | |
581bb050 LZ |
9540 | err = btrfs_find_free_ino(root, &objectid); |
9541 | if (err) | |
9542 | goto out_unlock; | |
9543 | ||
aec7477b | 9544 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
9545 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
9546 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
9547 | if (IS_ERR(inode)) { |
9548 | err = PTR_ERR(inode); | |
32955c54 | 9549 | inode = NULL; |
39279cc3 | 9550 | goto out_unlock; |
7cf96da3 | 9551 | } |
39279cc3 | 9552 | |
ad19db71 CS |
9553 | /* |
9554 | * If the active LSM wants to access the inode during | |
9555 | * d_instantiate it needs these. Smack checks to see | |
9556 | * if the filesystem supports xattrs by looking at the | |
9557 | * ops vector. | |
9558 | */ | |
9559 | inode->i_fop = &btrfs_file_operations; | |
9560 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9561 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9562 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9563 | ||
9564 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9565 | if (err) | |
32955c54 | 9566 | goto out_unlock; |
ad19db71 | 9567 | |
39279cc3 | 9568 | path = btrfs_alloc_path(); |
d8926bb3 MF |
9569 | if (!path) { |
9570 | err = -ENOMEM; | |
32955c54 | 9571 | goto out_unlock; |
d8926bb3 | 9572 | } |
4a0cc7ca | 9573 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 9574 | key.offset = 0; |
962a298f | 9575 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9576 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9577 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9578 | datasize); | |
54aa1f4d | 9579 | if (err) { |
b0839166 | 9580 | btrfs_free_path(path); |
32955c54 | 9581 | goto out_unlock; |
54aa1f4d | 9582 | } |
5f39d397 CM |
9583 | leaf = path->nodes[0]; |
9584 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9585 | struct btrfs_file_extent_item); | |
9586 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9587 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9588 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9589 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9590 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9591 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9592 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9593 | ||
39279cc3 | 9594 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9595 | write_extent_buffer(leaf, symname, ptr, name_len); |
9596 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9597 | btrfs_free_path(path); |
5f39d397 | 9598 | |
39279cc3 | 9599 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 9600 | inode_nohighmem(inode); |
d899e052 | 9601 | inode_set_bytes(inode, name_len); |
6ef06d27 | 9602 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 9603 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
9604 | /* |
9605 | * Last step, add directory indexes for our symlink inode. This is the | |
9606 | * last step to avoid extra cleanup of these indexes if an error happens | |
9607 | * elsewhere above. | |
9608 | */ | |
9609 | if (!err) | |
cef415af NB |
9610 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9611 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
9612 | if (err) |
9613 | goto out_unlock; | |
b0d5d10f | 9614 | |
1e2e547a | 9615 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
9616 | |
9617 | out_unlock: | |
3a45bb20 | 9618 | btrfs_end_transaction(trans); |
32955c54 | 9619 | if (err && inode) { |
39279cc3 | 9620 | inode_dec_link_count(inode); |
32955c54 | 9621 | discard_new_inode(inode); |
39279cc3 | 9622 | } |
2ff7e61e | 9623 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
9624 | return err; |
9625 | } | |
16432985 | 9626 | |
203f44c5 QW |
9627 | static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans, |
9628 | struct inode *inode, struct btrfs_key *ins, | |
9629 | u64 file_offset) | |
9630 | { | |
9631 | struct btrfs_file_extent_item stack_fi; | |
9632 | u64 start = ins->objectid; | |
9633 | u64 len = ins->offset; | |
9729f10a | 9634 | int ret; |
203f44c5 QW |
9635 | |
9636 | memset(&stack_fi, 0, sizeof(stack_fi)); | |
9637 | ||
9638 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); | |
9639 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); | |
9640 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); | |
9641 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); | |
9642 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); | |
9643 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); | |
9644 | /* Encryption and other encoding is reserved and all 0 */ | |
9645 | ||
72b7d15b | 9646 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
9729f10a QW |
9647 | if (ret < 0) |
9648 | return ret; | |
203f44c5 | 9649 | return insert_reserved_file_extent(trans, inode, file_offset, |
9729f10a | 9650 | &stack_fi, ret); |
203f44c5 | 9651 | } |
0af3d00b JB |
9652 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9653 | u64 start, u64 num_bytes, u64 min_size, | |
9654 | loff_t actual_len, u64 *alloc_hint, | |
9655 | struct btrfs_trans_handle *trans) | |
d899e052 | 9656 | { |
0b246afa | 9657 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
9658 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9659 | struct extent_map *em; | |
d899e052 YZ |
9660 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9661 | struct btrfs_key ins; | |
d899e052 | 9662 | u64 cur_offset = start; |
b778cf96 | 9663 | u64 clear_offset = start; |
55a61d1d | 9664 | u64 i_size; |
154ea289 | 9665 | u64 cur_bytes; |
0b670dc4 | 9666 | u64 last_alloc = (u64)-1; |
d899e052 | 9667 | int ret = 0; |
0af3d00b | 9668 | bool own_trans = true; |
18513091 | 9669 | u64 end = start + num_bytes - 1; |
d899e052 | 9670 | |
0af3d00b JB |
9671 | if (trans) |
9672 | own_trans = false; | |
d899e052 | 9673 | while (num_bytes > 0) { |
0af3d00b JB |
9674 | if (own_trans) { |
9675 | trans = btrfs_start_transaction(root, 3); | |
9676 | if (IS_ERR(trans)) { | |
9677 | ret = PTR_ERR(trans); | |
9678 | break; | |
9679 | } | |
5a303d5d YZ |
9680 | } |
9681 | ||
ee22184b | 9682 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 9683 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
9684 | /* |
9685 | * If we are severely fragmented we could end up with really | |
9686 | * small allocations, so if the allocator is returning small | |
9687 | * chunks lets make its job easier by only searching for those | |
9688 | * sized chunks. | |
9689 | */ | |
9690 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
9691 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
9692 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 9693 | if (ret) { |
0af3d00b | 9694 | if (own_trans) |
3a45bb20 | 9695 | btrfs_end_transaction(trans); |
a22285a6 | 9696 | break; |
d899e052 | 9697 | } |
b778cf96 JB |
9698 | |
9699 | /* | |
9700 | * We've reserved this space, and thus converted it from | |
9701 | * ->bytes_may_use to ->bytes_reserved. Any error that happens | |
9702 | * from here on out we will only need to clear our reservation | |
9703 | * for the remaining unreserved area, so advance our | |
9704 | * clear_offset by our extent size. | |
9705 | */ | |
9706 | clear_offset += ins.offset; | |
0b246afa | 9707 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 9708 | |
0b670dc4 | 9709 | last_alloc = ins.offset; |
203f44c5 QW |
9710 | ret = insert_prealloc_file_extent(trans, inode, &ins, |
9711 | cur_offset); | |
79787eaa | 9712 | if (ret) { |
2ff7e61e | 9713 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 9714 | ins.offset, 0); |
66642832 | 9715 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9716 | if (own_trans) |
3a45bb20 | 9717 | btrfs_end_transaction(trans); |
79787eaa JM |
9718 | break; |
9719 | } | |
31193213 | 9720 | |
dcdbc059 | 9721 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 9722 | cur_offset + ins.offset -1, 0); |
5a303d5d | 9723 | |
5dc562c5 JB |
9724 | em = alloc_extent_map(); |
9725 | if (!em) { | |
9726 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9727 | &BTRFS_I(inode)->runtime_flags); | |
9728 | goto next; | |
9729 | } | |
9730 | ||
9731 | em->start = cur_offset; | |
9732 | em->orig_start = cur_offset; | |
9733 | em->len = ins.offset; | |
9734 | em->block_start = ins.objectid; | |
9735 | em->block_len = ins.offset; | |
b4939680 | 9736 | em->orig_block_len = ins.offset; |
cc95bef6 | 9737 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9738 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
9739 | em->generation = trans->transid; | |
9740 | ||
9741 | while (1) { | |
9742 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9743 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9744 | write_unlock(&em_tree->lock); |
9745 | if (ret != -EEXIST) | |
9746 | break; | |
dcdbc059 | 9747 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
9748 | cur_offset + ins.offset - 1, |
9749 | 0); | |
9750 | } | |
9751 | free_extent_map(em); | |
9752 | next: | |
d899e052 YZ |
9753 | num_bytes -= ins.offset; |
9754 | cur_offset += ins.offset; | |
efa56464 | 9755 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9756 | |
0c4d2d95 | 9757 | inode_inc_iversion(inode); |
c2050a45 | 9758 | inode->i_ctime = current_time(inode); |
6cbff00f | 9759 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9760 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9761 | (actual_len > inode->i_size) && |
9762 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9763 | if (cur_offset > actual_len) |
55a61d1d | 9764 | i_size = actual_len; |
d1ea6a61 | 9765 | else |
55a61d1d JB |
9766 | i_size = cur_offset; |
9767 | i_size_write(inode, i_size); | |
d923afe9 | 9768 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
5a303d5d YZ |
9769 | } |
9770 | ||
d899e052 | 9771 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9772 | |
9773 | if (ret) { | |
66642832 | 9774 | btrfs_abort_transaction(trans, ret); |
79787eaa | 9775 | if (own_trans) |
3a45bb20 | 9776 | btrfs_end_transaction(trans); |
79787eaa JM |
9777 | break; |
9778 | } | |
d899e052 | 9779 | |
0af3d00b | 9780 | if (own_trans) |
3a45bb20 | 9781 | btrfs_end_transaction(trans); |
5a303d5d | 9782 | } |
b778cf96 JB |
9783 | if (clear_offset < end) |
9784 | btrfs_free_reserved_data_space(inode, NULL, clear_offset, | |
9785 | end - clear_offset + 1); | |
d899e052 YZ |
9786 | return ret; |
9787 | } | |
9788 | ||
0af3d00b JB |
9789 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9790 | u64 start, u64 num_bytes, u64 min_size, | |
9791 | loff_t actual_len, u64 *alloc_hint) | |
9792 | { | |
9793 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9794 | min_size, actual_len, alloc_hint, | |
9795 | NULL); | |
9796 | } | |
9797 | ||
9798 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9799 | struct btrfs_trans_handle *trans, int mode, | |
9800 | u64 start, u64 num_bytes, u64 min_size, | |
9801 | loff_t actual_len, u64 *alloc_hint) | |
9802 | { | |
9803 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9804 | min_size, actual_len, alloc_hint, trans); | |
9805 | } | |
9806 | ||
e6dcd2dc CM |
9807 | static int btrfs_set_page_dirty(struct page *page) |
9808 | { | |
e6dcd2dc CM |
9809 | return __set_page_dirty_nobuffers(page); |
9810 | } | |
9811 | ||
10556cb2 | 9812 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9813 | { |
b83cc969 | 9814 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9815 | umode_t mode = inode->i_mode; |
b83cc969 | 9816 | |
cb6db4e5 JM |
9817 | if (mask & MAY_WRITE && |
9818 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9819 | if (btrfs_root_readonly(root)) | |
9820 | return -EROFS; | |
9821 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9822 | return -EACCES; | |
9823 | } | |
2830ba7f | 9824 | return generic_permission(inode, mask); |
fdebe2bd | 9825 | } |
39279cc3 | 9826 | |
ef3b9af5 FM |
9827 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9828 | { | |
2ff7e61e | 9829 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
9830 | struct btrfs_trans_handle *trans; |
9831 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9832 | struct inode *inode = NULL; | |
9833 | u64 objectid; | |
9834 | u64 index; | |
9835 | int ret = 0; | |
9836 | ||
9837 | /* | |
9838 | * 5 units required for adding orphan entry | |
9839 | */ | |
9840 | trans = btrfs_start_transaction(root, 5); | |
9841 | if (IS_ERR(trans)) | |
9842 | return PTR_ERR(trans); | |
9843 | ||
9844 | ret = btrfs_find_free_ino(root, &objectid); | |
9845 | if (ret) | |
9846 | goto out; | |
9847 | ||
9848 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 9849 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
9850 | if (IS_ERR(inode)) { |
9851 | ret = PTR_ERR(inode); | |
9852 | inode = NULL; | |
9853 | goto out; | |
9854 | } | |
9855 | ||
ef3b9af5 FM |
9856 | inode->i_fop = &btrfs_file_operations; |
9857 | inode->i_op = &btrfs_file_inode_operations; | |
9858 | ||
9859 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9860 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9861 | ||
b0d5d10f CM |
9862 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9863 | if (ret) | |
32955c54 | 9864 | goto out; |
b0d5d10f CM |
9865 | |
9866 | ret = btrfs_update_inode(trans, root, inode); | |
9867 | if (ret) | |
32955c54 | 9868 | goto out; |
73f2e545 | 9869 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 9870 | if (ret) |
32955c54 | 9871 | goto out; |
ef3b9af5 | 9872 | |
5762b5c9 FM |
9873 | /* |
9874 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9875 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9876 | * through: | |
9877 | * | |
9878 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9879 | */ | |
9880 | set_nlink(inode, 1); | |
ef3b9af5 | 9881 | d_tmpfile(dentry, inode); |
32955c54 | 9882 | unlock_new_inode(inode); |
ef3b9af5 | 9883 | mark_inode_dirty(inode); |
ef3b9af5 | 9884 | out: |
3a45bb20 | 9885 | btrfs_end_transaction(trans); |
32955c54 AV |
9886 | if (ret && inode) |
9887 | discard_new_inode(inode); | |
2ff7e61e | 9888 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
9889 | return ret; |
9890 | } | |
9891 | ||
5cdc84bf | 9892 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
c6100a4b | 9893 | { |
5cdc84bf | 9894 | struct inode *inode = tree->private_data; |
c6100a4b JB |
9895 | unsigned long index = start >> PAGE_SHIFT; |
9896 | unsigned long end_index = end >> PAGE_SHIFT; | |
9897 | struct page *page; | |
9898 | ||
9899 | while (index <= end_index) { | |
9900 | page = find_get_page(inode->i_mapping, index); | |
9901 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
9902 | set_page_writeback(page); | |
9903 | put_page(page); | |
9904 | index++; | |
9905 | } | |
9906 | } | |
9907 | ||
ed46ff3d OS |
9908 | #ifdef CONFIG_SWAP |
9909 | /* | |
9910 | * Add an entry indicating a block group or device which is pinned by a | |
9911 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
9912 | * negative errno on failure. | |
9913 | */ | |
9914 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
9915 | bool is_block_group) | |
9916 | { | |
9917 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9918 | struct btrfs_swapfile_pin *sp, *entry; | |
9919 | struct rb_node **p; | |
9920 | struct rb_node *parent = NULL; | |
9921 | ||
9922 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
9923 | if (!sp) | |
9924 | return -ENOMEM; | |
9925 | sp->ptr = ptr; | |
9926 | sp->inode = inode; | |
9927 | sp->is_block_group = is_block_group; | |
9928 | ||
9929 | spin_lock(&fs_info->swapfile_pins_lock); | |
9930 | p = &fs_info->swapfile_pins.rb_node; | |
9931 | while (*p) { | |
9932 | parent = *p; | |
9933 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
9934 | if (sp->ptr < entry->ptr || | |
9935 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
9936 | p = &(*p)->rb_left; | |
9937 | } else if (sp->ptr > entry->ptr || | |
9938 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
9939 | p = &(*p)->rb_right; | |
9940 | } else { | |
9941 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9942 | kfree(sp); | |
9943 | return 1; | |
9944 | } | |
9945 | } | |
9946 | rb_link_node(&sp->node, parent, p); | |
9947 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
9948 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9949 | return 0; | |
9950 | } | |
9951 | ||
9952 | /* Free all of the entries pinned by this swapfile. */ | |
9953 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
9954 | { | |
9955 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
9956 | struct btrfs_swapfile_pin *sp; | |
9957 | struct rb_node *node, *next; | |
9958 | ||
9959 | spin_lock(&fs_info->swapfile_pins_lock); | |
9960 | node = rb_first(&fs_info->swapfile_pins); | |
9961 | while (node) { | |
9962 | next = rb_next(node); | |
9963 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
9964 | if (sp->inode == inode) { | |
9965 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
9966 | if (sp->is_block_group) | |
9967 | btrfs_put_block_group(sp->ptr); | |
9968 | kfree(sp); | |
9969 | } | |
9970 | node = next; | |
9971 | } | |
9972 | spin_unlock(&fs_info->swapfile_pins_lock); | |
9973 | } | |
9974 | ||
9975 | struct btrfs_swap_info { | |
9976 | u64 start; | |
9977 | u64 block_start; | |
9978 | u64 block_len; | |
9979 | u64 lowest_ppage; | |
9980 | u64 highest_ppage; | |
9981 | unsigned long nr_pages; | |
9982 | int nr_extents; | |
9983 | }; | |
9984 | ||
9985 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
9986 | struct btrfs_swap_info *bsi) | |
9987 | { | |
9988 | unsigned long nr_pages; | |
9989 | u64 first_ppage, first_ppage_reported, next_ppage; | |
9990 | int ret; | |
9991 | ||
9992 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
9993 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
9994 | PAGE_SIZE) >> PAGE_SHIFT; | |
9995 | ||
9996 | if (first_ppage >= next_ppage) | |
9997 | return 0; | |
9998 | nr_pages = next_ppage - first_ppage; | |
9999 | ||
10000 | first_ppage_reported = first_ppage; | |
10001 | if (bsi->start == 0) | |
10002 | first_ppage_reported++; | |
10003 | if (bsi->lowest_ppage > first_ppage_reported) | |
10004 | bsi->lowest_ppage = first_ppage_reported; | |
10005 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10006 | bsi->highest_ppage = next_ppage - 1; | |
10007 | ||
10008 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10009 | if (ret < 0) | |
10010 | return ret; | |
10011 | bsi->nr_extents += ret; | |
10012 | bsi->nr_pages += nr_pages; | |
10013 | return 0; | |
10014 | } | |
10015 | ||
10016 | static void btrfs_swap_deactivate(struct file *file) | |
10017 | { | |
10018 | struct inode *inode = file_inode(file); | |
10019 | ||
10020 | btrfs_free_swapfile_pins(inode); | |
10021 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10022 | } | |
10023 | ||
10024 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10025 | sector_t *span) | |
10026 | { | |
10027 | struct inode *inode = file_inode(file); | |
10028 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10029 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
10030 | struct extent_state *cached_state = NULL; | |
10031 | struct extent_map *em = NULL; | |
10032 | struct btrfs_device *device = NULL; | |
10033 | struct btrfs_swap_info bsi = { | |
10034 | .lowest_ppage = (sector_t)-1ULL, | |
10035 | }; | |
10036 | int ret = 0; | |
10037 | u64 isize; | |
10038 | u64 start; | |
10039 | ||
10040 | /* | |
10041 | * If the swap file was just created, make sure delalloc is done. If the | |
10042 | * file changes again after this, the user is doing something stupid and | |
10043 | * we don't really care. | |
10044 | */ | |
10045 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10046 | if (ret) | |
10047 | return ret; | |
10048 | ||
10049 | /* | |
10050 | * The inode is locked, so these flags won't change after we check them. | |
10051 | */ | |
10052 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10053 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10054 | return -EINVAL; | |
10055 | } | |
10056 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10057 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10058 | return -EINVAL; | |
10059 | } | |
10060 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10061 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10062 | return -EINVAL; | |
10063 | } | |
10064 | ||
10065 | /* | |
10066 | * Balance or device remove/replace/resize can move stuff around from | |
10067 | * under us. The EXCL_OP flag makes sure they aren't running/won't run | |
10068 | * concurrently while we are mapping the swap extents, and | |
10069 | * fs_info->swapfile_pins prevents them from running while the swap file | |
10070 | * is active and moving the extents. Note that this also prevents a | |
10071 | * concurrent device add which isn't actually necessary, but it's not | |
10072 | * really worth the trouble to allow it. | |
10073 | */ | |
10074 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { | |
10075 | btrfs_warn(fs_info, | |
10076 | "cannot activate swapfile while exclusive operation is running"); | |
10077 | return -EBUSY; | |
10078 | } | |
10079 | /* | |
10080 | * Snapshots can create extents which require COW even if NODATACOW is | |
10081 | * set. We use this counter to prevent snapshots. We must increment it | |
10082 | * before walking the extents because we don't want a concurrent | |
10083 | * snapshot to run after we've already checked the extents. | |
10084 | */ | |
10085 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); | |
10086 | ||
10087 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10088 | ||
10089 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10090 | start = 0; | |
10091 | while (start < isize) { | |
10092 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10093 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10094 | u64 len = isize - start; |
10095 | ||
39b07b5d | 10096 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
ed46ff3d OS |
10097 | if (IS_ERR(em)) { |
10098 | ret = PTR_ERR(em); | |
10099 | goto out; | |
10100 | } | |
10101 | ||
10102 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10103 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10104 | ret = -EINVAL; | |
10105 | goto out; | |
10106 | } | |
10107 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10108 | /* | |
10109 | * It's unlikely we'll ever actually find ourselves | |
10110 | * here, as a file small enough to fit inline won't be | |
10111 | * big enough to store more than the swap header, but in | |
10112 | * case something changes in the future, let's catch it | |
10113 | * here rather than later. | |
10114 | */ | |
10115 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10116 | ret = -EINVAL; | |
10117 | goto out; | |
10118 | } | |
10119 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10120 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10121 | ret = -EINVAL; | |
10122 | goto out; | |
10123 | } | |
10124 | ||
10125 | logical_block_start = em->block_start + (start - em->start); | |
10126 | len = min(len, em->len - (start - em->start)); | |
10127 | free_extent_map(em); | |
10128 | em = NULL; | |
10129 | ||
10130 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL); | |
10131 | if (ret < 0) { | |
10132 | goto out; | |
10133 | } else if (ret) { | |
10134 | ret = 0; | |
10135 | } else { | |
10136 | btrfs_warn(fs_info, | |
10137 | "swapfile must not be copy-on-write"); | |
10138 | ret = -EINVAL; | |
10139 | goto out; | |
10140 | } | |
10141 | ||
10142 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10143 | if (IS_ERR(em)) { | |
10144 | ret = PTR_ERR(em); | |
10145 | goto out; | |
10146 | } | |
10147 | ||
10148 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10149 | btrfs_warn(fs_info, | |
10150 | "swapfile must have single data profile"); | |
10151 | ret = -EINVAL; | |
10152 | goto out; | |
10153 | } | |
10154 | ||
10155 | if (device == NULL) { | |
10156 | device = em->map_lookup->stripes[0].dev; | |
10157 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10158 | if (ret == 1) | |
10159 | ret = 0; | |
10160 | else if (ret) | |
10161 | goto out; | |
10162 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10163 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10164 | ret = -EINVAL; | |
10165 | goto out; | |
10166 | } | |
10167 | ||
10168 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10169 | (logical_block_start - em->start)); | |
10170 | len = min(len, em->len - (logical_block_start - em->start)); | |
10171 | free_extent_map(em); | |
10172 | em = NULL; | |
10173 | ||
10174 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10175 | if (!bg) { | |
10176 | btrfs_warn(fs_info, | |
10177 | "could not find block group containing swapfile"); | |
10178 | ret = -EINVAL; | |
10179 | goto out; | |
10180 | } | |
10181 | ||
10182 | ret = btrfs_add_swapfile_pin(inode, bg, true); | |
10183 | if (ret) { | |
10184 | btrfs_put_block_group(bg); | |
10185 | if (ret == 1) | |
10186 | ret = 0; | |
10187 | else | |
10188 | goto out; | |
10189 | } | |
10190 | ||
10191 | if (bsi.block_len && | |
10192 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10193 | bsi.block_len += len; | |
10194 | } else { | |
10195 | if (bsi.block_len) { | |
10196 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10197 | if (ret) | |
10198 | goto out; | |
10199 | } | |
10200 | bsi.start = start; | |
10201 | bsi.block_start = physical_block_start; | |
10202 | bsi.block_len = len; | |
10203 | } | |
10204 | ||
10205 | start += len; | |
10206 | } | |
10207 | ||
10208 | if (bsi.block_len) | |
10209 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10210 | ||
10211 | out: | |
10212 | if (!IS_ERR_OR_NULL(em)) | |
10213 | free_extent_map(em); | |
10214 | ||
10215 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10216 | ||
10217 | if (ret) | |
10218 | btrfs_swap_deactivate(file); | |
10219 | ||
10220 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); | |
10221 | ||
10222 | if (ret) | |
10223 | return ret; | |
10224 | ||
10225 | if (device) | |
10226 | sis->bdev = device->bdev; | |
10227 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10228 | sis->max = bsi.nr_pages; | |
10229 | sis->pages = bsi.nr_pages - 1; | |
10230 | sis->highest_bit = bsi.nr_pages - 1; | |
10231 | return bsi.nr_extents; | |
10232 | } | |
10233 | #else | |
10234 | static void btrfs_swap_deactivate(struct file *file) | |
10235 | { | |
10236 | } | |
10237 | ||
10238 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10239 | sector_t *span) | |
10240 | { | |
10241 | return -EOPNOTSUPP; | |
10242 | } | |
10243 | #endif | |
10244 | ||
6e1d5dcc | 10245 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10246 | .getattr = btrfs_getattr, |
39279cc3 CM |
10247 | .lookup = btrfs_lookup, |
10248 | .create = btrfs_create, | |
10249 | .unlink = btrfs_unlink, | |
10250 | .link = btrfs_link, | |
10251 | .mkdir = btrfs_mkdir, | |
10252 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10253 | .rename = btrfs_rename2, |
39279cc3 CM |
10254 | .symlink = btrfs_symlink, |
10255 | .setattr = btrfs_setattr, | |
618e21d5 | 10256 | .mknod = btrfs_mknod, |
5103e947 | 10257 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10258 | .permission = btrfs_permission, |
4e34e719 | 10259 | .get_acl = btrfs_get_acl, |
996a710d | 10260 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10261 | .update_time = btrfs_update_time, |
ef3b9af5 | 10262 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10263 | }; |
76dda93c | 10264 | |
828c0950 | 10265 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10266 | .llseek = generic_file_llseek, |
10267 | .read = generic_read_dir, | |
02dbfc99 | 10268 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10269 | .open = btrfs_opendir, |
34287aa3 | 10270 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10271 | #ifdef CONFIG_COMPAT |
4c63c245 | 10272 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10273 | #endif |
6bf13c0c | 10274 | .release = btrfs_release_file, |
e02119d5 | 10275 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10276 | }; |
10277 | ||
20e5506b | 10278 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10279 | /* mandatory callbacks */ |
065631f6 | 10280 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac CM |
10281 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
10282 | }; | |
10283 | ||
35054394 CM |
10284 | /* |
10285 | * btrfs doesn't support the bmap operation because swapfiles | |
10286 | * use bmap to make a mapping of extents in the file. They assume | |
10287 | * these extents won't change over the life of the file and they | |
10288 | * use the bmap result to do IO directly to the drive. | |
10289 | * | |
10290 | * the btrfs bmap call would return logical addresses that aren't | |
10291 | * suitable for IO and they also will change frequently as COW | |
10292 | * operations happen. So, swapfile + btrfs == corruption. | |
10293 | * | |
10294 | * For now we're avoiding this by dropping bmap. | |
10295 | */ | |
7f09410b | 10296 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10297 | .readpage = btrfs_readpage, |
10298 | .writepage = btrfs_writepage, | |
b293f02e | 10299 | .writepages = btrfs_writepages, |
ba206a02 | 10300 | .readahead = btrfs_readahead, |
55e20bd1 | 10301 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10302 | .invalidatepage = btrfs_invalidatepage, |
10303 | .releasepage = btrfs_releasepage, | |
f8e66081 RG |
10304 | #ifdef CONFIG_MIGRATION |
10305 | .migratepage = btrfs_migratepage, | |
10306 | #endif | |
e6dcd2dc | 10307 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10308 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10309 | .swap_activate = btrfs_swap_activate, |
10310 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10311 | }; |
10312 | ||
6e1d5dcc | 10313 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10314 | .getattr = btrfs_getattr, |
10315 | .setattr = btrfs_setattr, | |
5103e947 | 10316 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10317 | .permission = btrfs_permission, |
1506fcc8 | 10318 | .fiemap = btrfs_fiemap, |
4e34e719 | 10319 | .get_acl = btrfs_get_acl, |
996a710d | 10320 | .set_acl = btrfs_set_acl, |
e41f941a | 10321 | .update_time = btrfs_update_time, |
39279cc3 | 10322 | }; |
6e1d5dcc | 10323 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10324 | .getattr = btrfs_getattr, |
10325 | .setattr = btrfs_setattr, | |
fdebe2bd | 10326 | .permission = btrfs_permission, |
33268eaf | 10327 | .listxattr = btrfs_listxattr, |
4e34e719 | 10328 | .get_acl = btrfs_get_acl, |
996a710d | 10329 | .set_acl = btrfs_set_acl, |
e41f941a | 10330 | .update_time = btrfs_update_time, |
618e21d5 | 10331 | }; |
6e1d5dcc | 10332 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10333 | .get_link = page_get_link, |
f209561a | 10334 | .getattr = btrfs_getattr, |
22c44fe6 | 10335 | .setattr = btrfs_setattr, |
fdebe2bd | 10336 | .permission = btrfs_permission, |
0279b4cd | 10337 | .listxattr = btrfs_listxattr, |
e41f941a | 10338 | .update_time = btrfs_update_time, |
39279cc3 | 10339 | }; |
76dda93c | 10340 | |
82d339d9 | 10341 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
10342 | .d_delete = btrfs_dentry_delete, |
10343 | }; |