]>
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> |
39279cc3 | 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> |
b1c16ac9 | 31 | #include <linux/sched/mm.h> |
92d32170 | 32 | #include <asm/unaligned.h> |
602cbe91 | 33 | #include "misc.h" |
39279cc3 CM |
34 | #include "ctree.h" |
35 | #include "disk-io.h" | |
36 | #include "transaction.h" | |
37 | #include "btrfs_inode.h" | |
39279cc3 | 38 | #include "print-tree.h" |
e6dcd2dc | 39 | #include "ordered-data.h" |
95819c05 | 40 | #include "xattr.h" |
e02119d5 | 41 | #include "tree-log.h" |
4a54c8c1 | 42 | #include "volumes.h" |
c8b97818 | 43 | #include "compression.h" |
b4ce94de | 44 | #include "locking.h" |
dc89e982 | 45 | #include "free-space-cache.h" |
581bb050 | 46 | #include "inode-map.h" |
38c227d8 | 47 | #include "backref.h" |
63541927 | 48 | #include "props.h" |
31193213 | 49 | #include "qgroup.h" |
86736342 | 50 | #include "delalloc-space.h" |
aac0023c | 51 | #include "block-group.h" |
39279cc3 CM |
52 | |
53 | struct btrfs_iget_args { | |
90d3e592 | 54 | struct btrfs_key *location; |
39279cc3 CM |
55 | struct btrfs_root *root; |
56 | }; | |
57 | ||
f28a4928 | 58 | struct btrfs_dio_data { |
f28a4928 FM |
59 | u64 reserve; |
60 | u64 unsubmitted_oe_range_start; | |
61 | u64 unsubmitted_oe_range_end; | |
4aaedfb0 | 62 | int overwrite; |
f28a4928 FM |
63 | }; |
64 | ||
6e1d5dcc AD |
65 | static const struct inode_operations btrfs_dir_inode_operations; |
66 | static const struct inode_operations btrfs_symlink_inode_operations; | |
67 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
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); |
771ed689 CM |
83 | static noinline int cow_file_range(struct inode *inode, |
84 | struct page *locked_page, | |
74e9194a | 85 | u64 start, u64 end, int *page_started, |
330a5827 | 86 | unsigned long *nr_written, int unlock); |
6f9994db LB |
87 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
88 | u64 orig_start, u64 block_start, | |
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 | |
c2167754 YZ |
246 | /* |
247 | * we're an inline extent, so nobody can | |
248 | * extend the file past i_size without locking | |
249 | * a page we already have locked. | |
250 | * | |
251 | * We must do any isize and inode updates | |
252 | * before we unlock the pages. Otherwise we | |
253 | * could end up racing with unlink. | |
254 | */ | |
c8b97818 | 255 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 256 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 257 | |
c8b97818 | 258 | fail: |
79b4f4c6 | 259 | return ret; |
c8b97818 CM |
260 | } |
261 | ||
262 | ||
263 | /* | |
264 | * conditionally insert an inline extent into the file. This | |
265 | * does the checks required to make sure the data is small enough | |
266 | * to fit as an inline extent. | |
267 | */ | |
d02c0e20 | 268 | static noinline int cow_file_range_inline(struct inode *inode, u64 start, |
00361589 JB |
269 | u64 end, size_t compressed_size, |
270 | int compress_type, | |
271 | struct page **compressed_pages) | |
c8b97818 | 272 | { |
d02c0e20 | 273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0b246afa | 274 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 275 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
276 | u64 isize = i_size_read(inode); |
277 | u64 actual_end = min(end + 1, isize); | |
278 | u64 inline_len = actual_end - start; | |
0b246afa | 279 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
280 | u64 data_len = inline_len; |
281 | int ret; | |
1acae57b FDBM |
282 | struct btrfs_path *path; |
283 | int extent_inserted = 0; | |
284 | u32 extent_item_size; | |
c8b97818 CM |
285 | |
286 | if (compressed_size) | |
287 | data_len = compressed_size; | |
288 | ||
289 | if (start > 0 || | |
0b246afa JM |
290 | actual_end > fs_info->sectorsize || |
291 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 292 | (!compressed_size && |
0b246afa | 293 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 294 | end + 1 < isize || |
0b246afa | 295 | data_len > fs_info->max_inline) { |
c8b97818 CM |
296 | return 1; |
297 | } | |
298 | ||
1acae57b FDBM |
299 | path = btrfs_alloc_path(); |
300 | if (!path) | |
301 | return -ENOMEM; | |
302 | ||
00361589 | 303 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
304 | if (IS_ERR(trans)) { |
305 | btrfs_free_path(path); | |
00361589 | 306 | return PTR_ERR(trans); |
1acae57b | 307 | } |
69fe2d75 | 308 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
00361589 | 309 | |
1acae57b FDBM |
310 | if (compressed_size && compressed_pages) |
311 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
312 | compressed_size); | |
313 | else | |
314 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
315 | inline_len); | |
316 | ||
317 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
318 | start, aligned_end, NULL, | |
319 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 | 320 | if (ret) { |
66642832 | 321 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
322 | goto out; |
323 | } | |
c8b97818 CM |
324 | |
325 | if (isize > actual_end) | |
326 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
327 | ret = insert_inline_extent(trans, path, extent_inserted, |
328 | root, inode, start, | |
c8b97818 | 329 | inline_len, compressed_size, |
fe3f566c | 330 | compress_type, compressed_pages); |
2adcac1a | 331 | if (ret && ret != -ENOSPC) { |
66642832 | 332 | btrfs_abort_transaction(trans, ret); |
00361589 | 333 | goto out; |
2adcac1a | 334 | } else if (ret == -ENOSPC) { |
00361589 JB |
335 | ret = 1; |
336 | goto out; | |
79787eaa | 337 | } |
2adcac1a | 338 | |
bdc20e67 | 339 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
dcdbc059 | 340 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
00361589 | 341 | out: |
94ed938a QW |
342 | /* |
343 | * Don't forget to free the reserved space, as for inlined extent | |
344 | * it won't count as data extent, free them directly here. | |
345 | * And at reserve time, it's always aligned to page size, so | |
346 | * just free one page here. | |
347 | */ | |
bc42bda2 | 348 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 349 | btrfs_free_path(path); |
3a45bb20 | 350 | btrfs_end_transaction(trans); |
00361589 | 351 | return ret; |
c8b97818 CM |
352 | } |
353 | ||
771ed689 CM |
354 | struct async_extent { |
355 | u64 start; | |
356 | u64 ram_size; | |
357 | u64 compressed_size; | |
358 | struct page **pages; | |
359 | unsigned long nr_pages; | |
261507a0 | 360 | int compress_type; |
771ed689 CM |
361 | struct list_head list; |
362 | }; | |
363 | ||
97db1204 | 364 | struct async_chunk { |
771ed689 | 365 | struct inode *inode; |
771ed689 CM |
366 | struct page *locked_page; |
367 | u64 start; | |
368 | u64 end; | |
f82b7359 | 369 | unsigned int write_flags; |
771ed689 | 370 | struct list_head extents; |
ec39f769 | 371 | struct cgroup_subsys_state *blkcg_css; |
771ed689 | 372 | struct btrfs_work work; |
97db1204 | 373 | atomic_t *pending; |
771ed689 CM |
374 | }; |
375 | ||
97db1204 NB |
376 | struct async_cow { |
377 | /* Number of chunks in flight; must be first in the structure */ | |
378 | atomic_t num_chunks; | |
379 | struct async_chunk chunks[]; | |
771ed689 CM |
380 | }; |
381 | ||
97db1204 | 382 | static noinline int add_async_extent(struct async_chunk *cow, |
771ed689 CM |
383 | u64 start, u64 ram_size, |
384 | u64 compressed_size, | |
385 | struct page **pages, | |
261507a0 LZ |
386 | unsigned long nr_pages, |
387 | int compress_type) | |
771ed689 CM |
388 | { |
389 | struct async_extent *async_extent; | |
390 | ||
391 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 392 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
393 | async_extent->start = start; |
394 | async_extent->ram_size = ram_size; | |
395 | async_extent->compressed_size = compressed_size; | |
396 | async_extent->pages = pages; | |
397 | async_extent->nr_pages = nr_pages; | |
261507a0 | 398 | async_extent->compress_type = compress_type; |
771ed689 CM |
399 | list_add_tail(&async_extent->list, &cow->extents); |
400 | return 0; | |
401 | } | |
402 | ||
42c16da6 QW |
403 | /* |
404 | * Check if the inode has flags compatible with compression | |
405 | */ | |
406 | static inline bool inode_can_compress(struct inode *inode) | |
407 | { | |
408 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW || | |
409 | BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
410 | return false; | |
411 | return true; | |
412 | } | |
413 | ||
414 | /* | |
415 | * Check if the inode needs to be submitted to compression, based on mount | |
416 | * options, defragmentation, properties or heuristics. | |
417 | */ | |
c2fcdcdf | 418 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
f79707b0 | 419 | { |
0b246afa | 420 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 | 421 | |
42c16da6 QW |
422 | if (!inode_can_compress(inode)) { |
423 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), | |
424 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", | |
425 | btrfs_ino(BTRFS_I(inode))); | |
426 | return 0; | |
427 | } | |
f79707b0 | 428 | /* force compress */ |
0b246afa | 429 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 430 | return 1; |
eec63c65 DS |
431 | /* defrag ioctl */ |
432 | if (BTRFS_I(inode)->defrag_compress) | |
433 | return 1; | |
f79707b0 WS |
434 | /* bad compression ratios */ |
435 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
436 | return 0; | |
0b246afa | 437 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 | 438 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
b52aa8c9 | 439 | BTRFS_I(inode)->prop_compress) |
c2fcdcdf | 440 | return btrfs_compress_heuristic(inode, start, end); |
f79707b0 WS |
441 | return 0; |
442 | } | |
443 | ||
6158e1ce | 444 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
445 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
446 | { | |
447 | /* If this is a small write inside eof, kick off a defrag */ | |
448 | if (num_bytes < small_write && | |
6158e1ce | 449 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
450 | btrfs_add_inode_defrag(NULL, inode); |
451 | } | |
452 | ||
d352ac68 | 453 | /* |
771ed689 CM |
454 | * we create compressed extents in two phases. The first |
455 | * phase compresses a range of pages that have already been | |
456 | * locked (both pages and state bits are locked). | |
c8b97818 | 457 | * |
771ed689 CM |
458 | * This is done inside an ordered work queue, and the compression |
459 | * is spread across many cpus. The actual IO submission is step | |
460 | * two, and the ordered work queue takes care of making sure that | |
461 | * happens in the same order things were put onto the queue by | |
462 | * writepages and friends. | |
c8b97818 | 463 | * |
771ed689 CM |
464 | * If this code finds it can't get good compression, it puts an |
465 | * entry onto the work queue to write the uncompressed bytes. This | |
466 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
467 | * are written in the same order that the flusher thread sent them |
468 | * down. | |
d352ac68 | 469 | */ |
ac3e9933 | 470 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
b888db2b | 471 | { |
1368c6da | 472 | struct inode *inode = async_chunk->inode; |
0b246afa | 473 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 474 | u64 blocksize = fs_info->sectorsize; |
1368c6da NB |
475 | u64 start = async_chunk->start; |
476 | u64 end = async_chunk->end; | |
c8b97818 | 477 | u64 actual_end; |
d98da499 | 478 | u64 i_size; |
e6dcd2dc | 479 | int ret = 0; |
c8b97818 CM |
480 | struct page **pages = NULL; |
481 | unsigned long nr_pages; | |
c8b97818 CM |
482 | unsigned long total_compressed = 0; |
483 | unsigned long total_in = 0; | |
c8b97818 CM |
484 | int i; |
485 | int will_compress; | |
0b246afa | 486 | int compress_type = fs_info->compress_type; |
ac3e9933 | 487 | int compressed_extents = 0; |
4adaa611 | 488 | int redirty = 0; |
b888db2b | 489 | |
6158e1ce NB |
490 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
491 | SZ_16K); | |
4cb5300b | 492 | |
d98da499 JB |
493 | /* |
494 | * We need to save i_size before now because it could change in between | |
495 | * us evaluating the size and assigning it. This is because we lock and | |
496 | * unlock the page in truncate and fallocate, and then modify the i_size | |
497 | * later on. | |
498 | * | |
499 | * The barriers are to emulate READ_ONCE, remove that once i_size_read | |
500 | * does that for us. | |
501 | */ | |
502 | barrier(); | |
503 | i_size = i_size_read(inode); | |
504 | barrier(); | |
505 | actual_end = min_t(u64, i_size, end + 1); | |
c8b97818 CM |
506 | again: |
507 | will_compress = 0; | |
09cbfeaf | 508 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
509 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
510 | nr_pages = min_t(unsigned long, nr_pages, | |
511 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 512 | |
f03d9301 CM |
513 | /* |
514 | * we don't want to send crud past the end of i_size through | |
515 | * compression, that's just a waste of CPU time. So, if the | |
516 | * end of the file is before the start of our current | |
517 | * requested range of bytes, we bail out to the uncompressed | |
518 | * cleanup code that can deal with all of this. | |
519 | * | |
520 | * It isn't really the fastest way to fix things, but this is a | |
521 | * very uncommon corner. | |
522 | */ | |
523 | if (actual_end <= start) | |
524 | goto cleanup_and_bail_uncompressed; | |
525 | ||
c8b97818 CM |
526 | total_compressed = actual_end - start; |
527 | ||
4bcbb332 SW |
528 | /* |
529 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 530 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
531 | */ |
532 | if (total_compressed <= blocksize && | |
533 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
534 | goto cleanup_and_bail_uncompressed; | |
535 | ||
069eac78 DS |
536 | total_compressed = min_t(unsigned long, total_compressed, |
537 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
538 | total_in = 0; |
539 | ret = 0; | |
db94535d | 540 | |
771ed689 CM |
541 | /* |
542 | * we do compression for mount -o compress and when the | |
543 | * inode has not been flagged as nocompress. This flag can | |
544 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 545 | */ |
c2fcdcdf | 546 | if (inode_need_compress(inode, start, end)) { |
c8b97818 | 547 | WARN_ON(pages); |
31e818fe | 548 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
549 | if (!pages) { |
550 | /* just bail out to the uncompressed code */ | |
3527a018 | 551 | nr_pages = 0; |
560f7d75 LZ |
552 | goto cont; |
553 | } | |
c8b97818 | 554 | |
eec63c65 DS |
555 | if (BTRFS_I(inode)->defrag_compress) |
556 | compress_type = BTRFS_I(inode)->defrag_compress; | |
557 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 558 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 559 | |
4adaa611 CM |
560 | /* |
561 | * we need to call clear_page_dirty_for_io on each | |
562 | * page in the range. Otherwise applications with the file | |
563 | * mmap'd can wander in and change the page contents while | |
564 | * we are compressing them. | |
565 | * | |
566 | * If the compression fails for any reason, we set the pages | |
567 | * dirty again later on. | |
e9679de3 TT |
568 | * |
569 | * Note that the remaining part is redirtied, the start pointer | |
570 | * has moved, the end is the original one. | |
4adaa611 | 571 | */ |
e9679de3 TT |
572 | if (!redirty) { |
573 | extent_range_clear_dirty_for_io(inode, start, end); | |
574 | redirty = 1; | |
575 | } | |
f51d2b59 DS |
576 | |
577 | /* Compression level is applied here and only here */ | |
578 | ret = btrfs_compress_pages( | |
579 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 580 | inode->i_mapping, start, |
38c31464 | 581 | pages, |
4d3a800e | 582 | &nr_pages, |
261507a0 | 583 | &total_in, |
e5d74902 | 584 | &total_compressed); |
c8b97818 CM |
585 | |
586 | if (!ret) { | |
7073017a | 587 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 588 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
589 | char *kaddr; |
590 | ||
591 | /* zero the tail end of the last page, we might be | |
592 | * sending it down to disk | |
593 | */ | |
594 | if (offset) { | |
7ac687d9 | 595 | kaddr = kmap_atomic(page); |
c8b97818 | 596 | memset(kaddr + offset, 0, |
09cbfeaf | 597 | PAGE_SIZE - offset); |
7ac687d9 | 598 | kunmap_atomic(kaddr); |
c8b97818 CM |
599 | } |
600 | will_compress = 1; | |
601 | } | |
602 | } | |
560f7d75 | 603 | cont: |
c8b97818 CM |
604 | if (start == 0) { |
605 | /* lets try to make an inline extent */ | |
6018ba0a | 606 | if (ret || total_in < actual_end) { |
c8b97818 | 607 | /* we didn't compress the entire range, try |
771ed689 | 608 | * to make an uncompressed inline extent. |
c8b97818 | 609 | */ |
d02c0e20 NB |
610 | ret = cow_file_range_inline(inode, start, end, 0, |
611 | BTRFS_COMPRESS_NONE, NULL); | |
c8b97818 | 612 | } else { |
771ed689 | 613 | /* try making a compressed inline extent */ |
d02c0e20 | 614 | ret = cow_file_range_inline(inode, start, end, |
fe3f566c LZ |
615 | total_compressed, |
616 | compress_type, pages); | |
c8b97818 | 617 | } |
79787eaa | 618 | if (ret <= 0) { |
151a41bc | 619 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
620 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
621 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
622 | unsigned long page_error_op; |
623 | ||
e6eb4314 | 624 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 625 | |
771ed689 | 626 | /* |
79787eaa JM |
627 | * inline extent creation worked or returned error, |
628 | * we don't need to create any more async work items. | |
629 | * Unlock and free up our temp pages. | |
8b62f87b JB |
630 | * |
631 | * We use DO_ACCOUNTING here because we need the | |
632 | * delalloc_release_metadata to be done _after_ we drop | |
633 | * our outstanding extent for clearing delalloc for this | |
634 | * range. | |
771ed689 | 635 | */ |
74e9194a NB |
636 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
637 | clear_flags, | |
ba8b04c1 | 638 | PAGE_UNLOCK | |
c2790a2e JB |
639 | PAGE_CLEAR_DIRTY | |
640 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 641 | page_error_op | |
c2790a2e | 642 | PAGE_END_WRITEBACK); |
cecc8d90 NB |
643 | |
644 | for (i = 0; i < nr_pages; i++) { | |
645 | WARN_ON(pages[i]->mapping); | |
646 | put_page(pages[i]); | |
647 | } | |
648 | kfree(pages); | |
649 | ||
650 | return 0; | |
c8b97818 CM |
651 | } |
652 | } | |
653 | ||
654 | if (will_compress) { | |
655 | /* | |
656 | * we aren't doing an inline extent round the compressed size | |
657 | * up to a block size boundary so the allocator does sane | |
658 | * things | |
659 | */ | |
fda2832f | 660 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
661 | |
662 | /* | |
663 | * one last check to make sure the compression is really a | |
170607eb TT |
664 | * win, compare the page count read with the blocks on disk, |
665 | * compression must free at least one sector size | |
c8b97818 | 666 | */ |
09cbfeaf | 667 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 668 | if (total_compressed + blocksize <= total_in) { |
ac3e9933 | 669 | compressed_extents++; |
c8bb0c8b AS |
670 | |
671 | /* | |
672 | * The async work queues will take care of doing actual | |
673 | * allocation on disk for these compressed pages, and | |
674 | * will submit them to the elevator. | |
675 | */ | |
b5326271 | 676 | add_async_extent(async_chunk, start, total_in, |
4d3a800e | 677 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
678 | compress_type); |
679 | ||
1170862d TT |
680 | if (start + total_in < end) { |
681 | start += total_in; | |
c8bb0c8b AS |
682 | pages = NULL; |
683 | cond_resched(); | |
684 | goto again; | |
685 | } | |
ac3e9933 | 686 | return compressed_extents; |
c8b97818 CM |
687 | } |
688 | } | |
c8bb0c8b | 689 | if (pages) { |
c8b97818 CM |
690 | /* |
691 | * the compression code ran but failed to make things smaller, | |
692 | * free any pages it allocated and our page pointer array | |
693 | */ | |
4d3a800e | 694 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 695 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 696 | put_page(pages[i]); |
c8b97818 CM |
697 | } |
698 | kfree(pages); | |
699 | pages = NULL; | |
700 | total_compressed = 0; | |
4d3a800e | 701 | nr_pages = 0; |
c8b97818 CM |
702 | |
703 | /* flag the file so we don't compress in the future */ | |
0b246afa | 704 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 705 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 706 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 707 | } |
c8b97818 | 708 | } |
f03d9301 | 709 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
710 | /* |
711 | * No compression, but we still need to write the pages in the file | |
712 | * we've been given so far. redirty the locked page if it corresponds | |
713 | * to our extent and set things up for the async work queue to run | |
714 | * cow_file_range to do the normal delalloc dance. | |
715 | */ | |
1d53c9e6 CM |
716 | if (async_chunk->locked_page && |
717 | (page_offset(async_chunk->locked_page) >= start && | |
718 | page_offset(async_chunk->locked_page)) <= end) { | |
1368c6da | 719 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
c8bb0c8b | 720 | /* unlocked later on in the async handlers */ |
1d53c9e6 | 721 | } |
c8bb0c8b AS |
722 | |
723 | if (redirty) | |
724 | extent_range_redirty_for_io(inode, start, end); | |
b5326271 | 725 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
c8bb0c8b | 726 | BTRFS_COMPRESS_NONE); |
ac3e9933 | 727 | compressed_extents++; |
3b951516 | 728 | |
ac3e9933 | 729 | return compressed_extents; |
771ed689 | 730 | } |
771ed689 | 731 | |
40ae837b FM |
732 | static void free_async_extent_pages(struct async_extent *async_extent) |
733 | { | |
734 | int i; | |
735 | ||
736 | if (!async_extent->pages) | |
737 | return; | |
738 | ||
739 | for (i = 0; i < async_extent->nr_pages; i++) { | |
740 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 741 | put_page(async_extent->pages[i]); |
40ae837b FM |
742 | } |
743 | kfree(async_extent->pages); | |
744 | async_extent->nr_pages = 0; | |
745 | async_extent->pages = NULL; | |
771ed689 CM |
746 | } |
747 | ||
748 | /* | |
749 | * phase two of compressed writeback. This is the ordered portion | |
750 | * of the code, which only gets called in the order the work was | |
751 | * queued. We walk all the async extents created by compress_file_range | |
752 | * and send them down to the disk. | |
753 | */ | |
b5326271 | 754 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
771ed689 | 755 | { |
b5326271 | 756 | struct inode *inode = async_chunk->inode; |
0b246afa | 757 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
758 | struct async_extent *async_extent; |
759 | u64 alloc_hint = 0; | |
771ed689 CM |
760 | struct btrfs_key ins; |
761 | struct extent_map *em; | |
762 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4336650a | 763 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
f5a84ee3 | 764 | int ret = 0; |
771ed689 | 765 | |
3e04e7f1 | 766 | again: |
b5326271 NB |
767 | while (!list_empty(&async_chunk->extents)) { |
768 | async_extent = list_entry(async_chunk->extents.next, | |
771ed689 CM |
769 | struct async_extent, list); |
770 | list_del(&async_extent->list); | |
c8b97818 | 771 | |
f5a84ee3 | 772 | retry: |
7447555f NB |
773 | lock_extent(io_tree, async_extent->start, |
774 | async_extent->start + async_extent->ram_size - 1); | |
771ed689 CM |
775 | /* did the compression code fall back to uncompressed IO? */ |
776 | if (!async_extent->pages) { | |
777 | int page_started = 0; | |
778 | unsigned long nr_written = 0; | |
779 | ||
771ed689 | 780 | /* allocate blocks */ |
b5326271 | 781 | ret = cow_file_range(inode, async_chunk->locked_page, |
f5a84ee3 JB |
782 | async_extent->start, |
783 | async_extent->start + | |
784 | async_extent->ram_size - 1, | |
330a5827 | 785 | &page_started, &nr_written, 0); |
771ed689 | 786 | |
79787eaa JM |
787 | /* JDM XXX */ |
788 | ||
771ed689 CM |
789 | /* |
790 | * if page_started, cow_file_range inserted an | |
791 | * inline extent and took care of all the unlocking | |
792 | * and IO for us. Otherwise, we need to submit | |
793 | * all those pages down to the drive. | |
794 | */ | |
f5a84ee3 | 795 | if (!page_started && !ret) |
5e3ee236 NB |
796 | extent_write_locked_range(inode, |
797 | async_extent->start, | |
d397712b | 798 | async_extent->start + |
771ed689 | 799 | async_extent->ram_size - 1, |
771ed689 | 800 | WB_SYNC_ALL); |
1d53c9e6 | 801 | else if (ret && async_chunk->locked_page) |
b5326271 | 802 | unlock_page(async_chunk->locked_page); |
771ed689 CM |
803 | kfree(async_extent); |
804 | cond_resched(); | |
805 | continue; | |
806 | } | |
807 | ||
18513091 | 808 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
809 | async_extent->compressed_size, |
810 | async_extent->compressed_size, | |
e570fd27 | 811 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 812 | if (ret) { |
40ae837b | 813 | free_async_extent_pages(async_extent); |
3e04e7f1 | 814 | |
fdf8e2ea JB |
815 | if (ret == -ENOSPC) { |
816 | unlock_extent(io_tree, async_extent->start, | |
817 | async_extent->start + | |
818 | async_extent->ram_size - 1); | |
ce62003f LB |
819 | |
820 | /* | |
821 | * we need to redirty the pages if we decide to | |
822 | * fallback to uncompressed IO, otherwise we | |
823 | * will not submit these pages down to lower | |
824 | * layers. | |
825 | */ | |
826 | extent_range_redirty_for_io(inode, | |
827 | async_extent->start, | |
828 | async_extent->start + | |
829 | async_extent->ram_size - 1); | |
830 | ||
79787eaa | 831 | goto retry; |
fdf8e2ea | 832 | } |
3e04e7f1 | 833 | goto out_free; |
f5a84ee3 | 834 | } |
c2167754 YZ |
835 | /* |
836 | * here we're doing allocation and writeback of the | |
837 | * compressed pages | |
838 | */ | |
6f9994db LB |
839 | em = create_io_em(inode, async_extent->start, |
840 | async_extent->ram_size, /* len */ | |
841 | async_extent->start, /* orig_start */ | |
842 | ins.objectid, /* block_start */ | |
843 | ins.offset, /* block_len */ | |
844 | ins.offset, /* orig_block_len */ | |
845 | async_extent->ram_size, /* ram_bytes */ | |
846 | async_extent->compress_type, | |
847 | BTRFS_ORDERED_COMPRESSED); | |
848 | if (IS_ERR(em)) | |
849 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 850 | goto out_free_reserve; |
6f9994db | 851 | free_extent_map(em); |
3e04e7f1 | 852 | |
261507a0 LZ |
853 | ret = btrfs_add_ordered_extent_compress(inode, |
854 | async_extent->start, | |
855 | ins.objectid, | |
856 | async_extent->ram_size, | |
857 | ins.offset, | |
858 | BTRFS_ORDERED_COMPRESSED, | |
859 | async_extent->compress_type); | |
d9f85963 | 860 | if (ret) { |
dcdbc059 NB |
861 | btrfs_drop_extent_cache(BTRFS_I(inode), |
862 | async_extent->start, | |
d9f85963 FM |
863 | async_extent->start + |
864 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 865 | goto out_free_reserve; |
d9f85963 | 866 | } |
0b246afa | 867 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 868 | |
771ed689 CM |
869 | /* |
870 | * clear dirty, set writeback and unlock the pages. | |
871 | */ | |
c2790a2e | 872 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
873 | async_extent->start + |
874 | async_extent->ram_size - 1, | |
151a41bc JB |
875 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
876 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 877 | PAGE_SET_WRITEBACK); |
4e4cbee9 | 878 | if (btrfs_submit_compressed_write(inode, |
d397712b CM |
879 | async_extent->start, |
880 | async_extent->ram_size, | |
881 | ins.objectid, | |
882 | ins.offset, async_extent->pages, | |
f82b7359 | 883 | async_extent->nr_pages, |
ec39f769 CM |
884 | async_chunk->write_flags, |
885 | async_chunk->blkcg_css)) { | |
fce2a4e6 FM |
886 | struct page *p = async_extent->pages[0]; |
887 | const u64 start = async_extent->start; | |
888 | const u64 end = start + async_extent->ram_size - 1; | |
889 | ||
890 | p->mapping = inode->i_mapping; | |
c629732d | 891 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
7087a9d8 | 892 | |
fce2a4e6 | 893 | p->mapping = NULL; |
74e9194a | 894 | extent_clear_unlock_delalloc(inode, start, end, |
ba8b04c1 | 895 | NULL, 0, |
fce2a4e6 FM |
896 | PAGE_END_WRITEBACK | |
897 | PAGE_SET_ERROR); | |
40ae837b | 898 | free_async_extent_pages(async_extent); |
fce2a4e6 | 899 | } |
771ed689 CM |
900 | alloc_hint = ins.objectid + ins.offset; |
901 | kfree(async_extent); | |
902 | cond_resched(); | |
903 | } | |
dec8f175 | 904 | return; |
3e04e7f1 | 905 | out_free_reserve: |
0b246afa | 906 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 907 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 908 | out_free: |
c2790a2e | 909 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
910 | async_extent->start + |
911 | async_extent->ram_size - 1, | |
c2790a2e | 912 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 913 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
914 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
915 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
916 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
917 | PAGE_SET_ERROR); | |
40ae837b | 918 | free_async_extent_pages(async_extent); |
79787eaa | 919 | kfree(async_extent); |
3e04e7f1 | 920 | goto again; |
771ed689 CM |
921 | } |
922 | ||
4b46fce2 JB |
923 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
924 | u64 num_bytes) | |
925 | { | |
926 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
927 | struct extent_map *em; | |
928 | u64 alloc_hint = 0; | |
929 | ||
930 | read_lock(&em_tree->lock); | |
931 | em = search_extent_mapping(em_tree, start, num_bytes); | |
932 | if (em) { | |
933 | /* | |
934 | * if block start isn't an actual block number then find the | |
935 | * first block in this inode and use that as a hint. If that | |
936 | * block is also bogus then just don't worry about it. | |
937 | */ | |
938 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
939 | free_extent_map(em); | |
940 | em = search_extent_mapping(em_tree, 0, 0); | |
941 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
942 | alloc_hint = em->block_start; | |
943 | if (em) | |
944 | free_extent_map(em); | |
945 | } else { | |
946 | alloc_hint = em->block_start; | |
947 | free_extent_map(em); | |
948 | } | |
949 | } | |
950 | read_unlock(&em_tree->lock); | |
951 | ||
952 | return alloc_hint; | |
953 | } | |
954 | ||
771ed689 CM |
955 | /* |
956 | * when extent_io.c finds a delayed allocation range in the file, | |
957 | * the call backs end up in this code. The basic idea is to | |
958 | * allocate extents on disk for the range, and create ordered data structs | |
959 | * in ram to track those extents. | |
960 | * | |
961 | * locked_page is the page that writepage had locked already. We use | |
962 | * it to make sure we don't do extra locks or unlocks. | |
963 | * | |
964 | * *page_started is set to one if we unlock locked_page and do everything | |
965 | * required to start IO on it. It may be clean and already done with | |
966 | * IO when we return. | |
967 | */ | |
00361589 JB |
968 | static noinline int cow_file_range(struct inode *inode, |
969 | struct page *locked_page, | |
74e9194a | 970 | u64 start, u64 end, int *page_started, |
330a5827 | 971 | unsigned long *nr_written, int unlock) |
771ed689 | 972 | { |
0b246afa | 973 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 974 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
975 | u64 alloc_hint = 0; |
976 | u64 num_bytes; | |
977 | unsigned long ram_size; | |
a315e68f | 978 | u64 cur_alloc_size = 0; |
0b246afa | 979 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
980 | struct btrfs_key ins; |
981 | struct extent_map *em; | |
a315e68f FM |
982 | unsigned clear_bits; |
983 | unsigned long page_ops; | |
984 | bool extent_reserved = false; | |
771ed689 CM |
985 | int ret = 0; |
986 | ||
70ddc553 | 987 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 988 | WARN_ON_ONCE(1); |
29bce2f3 JB |
989 | ret = -EINVAL; |
990 | goto out_unlock; | |
02ecd2c2 | 991 | } |
771ed689 | 992 | |
fda2832f | 993 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 994 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 995 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 996 | |
6158e1ce | 997 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 998 | |
771ed689 CM |
999 | if (start == 0) { |
1000 | /* lets try to make an inline extent */ | |
d02c0e20 NB |
1001 | ret = cow_file_range_inline(inode, start, end, 0, |
1002 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 1003 | if (ret == 0) { |
8b62f87b JB |
1004 | /* |
1005 | * We use DO_ACCOUNTING here because we need the | |
1006 | * delalloc_release_metadata to be run _after_ we drop | |
1007 | * our outstanding extent for clearing delalloc for this | |
1008 | * range. | |
1009 | */ | |
74e9194a | 1010 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
c2790a2e | 1011 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
1012 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
1013 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
1014 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1015 | PAGE_END_WRITEBACK); | |
771ed689 | 1016 | *nr_written = *nr_written + |
09cbfeaf | 1017 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 1018 | *page_started = 1; |
771ed689 | 1019 | goto out; |
79787eaa | 1020 | } else if (ret < 0) { |
79787eaa | 1021 | goto out_unlock; |
771ed689 CM |
1022 | } |
1023 | } | |
1024 | ||
4b46fce2 | 1025 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
1026 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1027 | start + num_bytes - 1, 0); | |
771ed689 | 1028 | |
3752d22f AJ |
1029 | while (num_bytes > 0) { |
1030 | cur_alloc_size = num_bytes; | |
18513091 | 1031 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 1032 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 1033 | &ins, 1, 1); |
00361589 | 1034 | if (ret < 0) |
79787eaa | 1035 | goto out_unlock; |
a315e68f FM |
1036 | cur_alloc_size = ins.offset; |
1037 | extent_reserved = true; | |
d397712b | 1038 | |
771ed689 | 1039 | ram_size = ins.offset; |
6f9994db LB |
1040 | em = create_io_em(inode, start, ins.offset, /* len */ |
1041 | start, /* orig_start */ | |
1042 | ins.objectid, /* block_start */ | |
1043 | ins.offset, /* block_len */ | |
1044 | ins.offset, /* orig_block_len */ | |
1045 | ram_size, /* ram_bytes */ | |
1046 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1047 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1048 | if (IS_ERR(em)) { |
1049 | ret = PTR_ERR(em); | |
ace68bac | 1050 | goto out_reserve; |
090a127a | 1051 | } |
6f9994db | 1052 | free_extent_map(em); |
e6dcd2dc | 1053 | |
e6dcd2dc | 1054 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1055 | ram_size, cur_alloc_size, 0); |
ace68bac | 1056 | if (ret) |
d9f85963 | 1057 | goto out_drop_extent_cache; |
c8b97818 | 1058 | |
17d217fe YZ |
1059 | if (root->root_key.objectid == |
1060 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1061 | ret = btrfs_reloc_clone_csums(inode, start, | |
1062 | cur_alloc_size); | |
4dbd80fb QW |
1063 | /* |
1064 | * Only drop cache here, and process as normal. | |
1065 | * | |
1066 | * We must not allow extent_clear_unlock_delalloc() | |
1067 | * at out_unlock label to free meta of this ordered | |
1068 | * extent, as its meta should be freed by | |
1069 | * btrfs_finish_ordered_io(). | |
1070 | * | |
1071 | * So we must continue until @start is increased to | |
1072 | * skip current ordered extent. | |
1073 | */ | |
00361589 | 1074 | if (ret) |
4dbd80fb QW |
1075 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1076 | start + ram_size - 1, 0); | |
17d217fe YZ |
1077 | } |
1078 | ||
0b246afa | 1079 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1080 | |
c8b97818 CM |
1081 | /* we're not doing compressed IO, don't unlock the first |
1082 | * page (which the caller expects to stay locked), don't | |
1083 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1084 | * |
1085 | * Do set the Private2 bit so we know this page was properly | |
1086 | * setup for writepage | |
c8b97818 | 1087 | */ |
a315e68f FM |
1088 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1089 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1090 | |
c2790a2e | 1091 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 | 1092 | start + ram_size - 1, |
74e9194a | 1093 | locked_page, |
c2790a2e | 1094 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1095 | page_ops); |
3752d22f AJ |
1096 | if (num_bytes < cur_alloc_size) |
1097 | num_bytes = 0; | |
4dbd80fb | 1098 | else |
3752d22f | 1099 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1100 | alloc_hint = ins.objectid + ins.offset; |
1101 | start += cur_alloc_size; | |
a315e68f | 1102 | extent_reserved = false; |
4dbd80fb QW |
1103 | |
1104 | /* | |
1105 | * btrfs_reloc_clone_csums() error, since start is increased | |
1106 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1107 | * free metadata of current ordered extent, we're OK to exit. | |
1108 | */ | |
1109 | if (ret) | |
1110 | goto out_unlock; | |
b888db2b | 1111 | } |
79787eaa | 1112 | out: |
be20aa9d | 1113 | return ret; |
b7d5b0a8 | 1114 | |
d9f85963 | 1115 | out_drop_extent_cache: |
dcdbc059 | 1116 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1117 | out_reserve: |
0b246afa | 1118 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1119 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1120 | out_unlock: |
a7e3b975 FM |
1121 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1122 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1123 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1124 | PAGE_END_WRITEBACK; | |
1125 | /* | |
1126 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1127 | * failed to create the respective ordered extent, then it means that | |
1128 | * when we reserved the extent we decremented the extent's size from | |
1129 | * the data space_info's bytes_may_use counter and incremented the | |
1130 | * space_info's bytes_reserved counter by the same amount. We must make | |
1131 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1132 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1133 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1134 | */ | |
1135 | if (extent_reserved) { | |
1136 | extent_clear_unlock_delalloc(inode, start, | |
a315e68f FM |
1137 | start + cur_alloc_size, |
1138 | locked_page, | |
1139 | clear_bits, | |
1140 | page_ops); | |
1141 | start += cur_alloc_size; | |
1142 | if (start >= end) | |
1143 | goto out; | |
1144 | } | |
74e9194a | 1145 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
a315e68f FM |
1146 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1147 | page_ops); | |
79787eaa | 1148 | goto out; |
771ed689 | 1149 | } |
c8b97818 | 1150 | |
771ed689 CM |
1151 | /* |
1152 | * work queue call back to started compression on a file and pages | |
1153 | */ | |
1154 | static noinline void async_cow_start(struct btrfs_work *work) | |
1155 | { | |
b5326271 | 1156 | struct async_chunk *async_chunk; |
ac3e9933 | 1157 | int compressed_extents; |
771ed689 | 1158 | |
b5326271 | 1159 | async_chunk = container_of(work, struct async_chunk, work); |
771ed689 | 1160 | |
ac3e9933 NB |
1161 | compressed_extents = compress_file_range(async_chunk); |
1162 | if (compressed_extents == 0) { | |
b5326271 NB |
1163 | btrfs_add_delayed_iput(async_chunk->inode); |
1164 | async_chunk->inode = NULL; | |
8180ef88 | 1165 | } |
771ed689 CM |
1166 | } |
1167 | ||
1168 | /* | |
1169 | * work queue call back to submit previously compressed pages | |
1170 | */ | |
1171 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1172 | { | |
c5a68aec NB |
1173 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
1174 | work); | |
1175 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); | |
771ed689 CM |
1176 | unsigned long nr_pages; |
1177 | ||
b5326271 | 1178 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
09cbfeaf | 1179 | PAGE_SHIFT; |
771ed689 | 1180 | |
093258e6 | 1181 | /* atomic_sub_return implies a barrier */ |
0b246afa | 1182 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
093258e6 DS |
1183 | 5 * SZ_1M) |
1184 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1185 | |
4546d178 | 1186 | /* |
b5326271 | 1187 | * ->inode could be NULL if async_chunk_start has failed to compress, |
4546d178 NB |
1188 | * in which case we don't have anything to submit, yet we need to |
1189 | * always adjust ->async_delalloc_pages as its paired with the init | |
1190 | * happening in cow_file_range_async | |
1191 | */ | |
b5326271 NB |
1192 | if (async_chunk->inode) |
1193 | submit_compressed_extents(async_chunk); | |
771ed689 | 1194 | } |
c8b97818 | 1195 | |
771ed689 CM |
1196 | static noinline void async_cow_free(struct btrfs_work *work) |
1197 | { | |
b5326271 | 1198 | struct async_chunk *async_chunk; |
97db1204 | 1199 | |
b5326271 NB |
1200 | async_chunk = container_of(work, struct async_chunk, work); |
1201 | if (async_chunk->inode) | |
1202 | btrfs_add_delayed_iput(async_chunk->inode); | |
ec39f769 CM |
1203 | if (async_chunk->blkcg_css) |
1204 | css_put(async_chunk->blkcg_css); | |
97db1204 NB |
1205 | /* |
1206 | * Since the pointer to 'pending' is at the beginning of the array of | |
b5326271 | 1207 | * async_chunk's, freeing it ensures the whole array has been freed. |
97db1204 | 1208 | */ |
b5326271 | 1209 | if (atomic_dec_and_test(async_chunk->pending)) |
b1c16ac9 | 1210 | kvfree(async_chunk->pending); |
771ed689 CM |
1211 | } |
1212 | ||
ec39f769 CM |
1213 | static int cow_file_range_async(struct inode *inode, |
1214 | struct writeback_control *wbc, | |
1215 | struct page *locked_page, | |
771ed689 | 1216 | u64 start, u64 end, int *page_started, |
fac07d2b | 1217 | unsigned long *nr_written) |
771ed689 | 1218 | { |
0b246afa | 1219 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
ec39f769 | 1220 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
97db1204 NB |
1221 | struct async_cow *ctx; |
1222 | struct async_chunk *async_chunk; | |
771ed689 CM |
1223 | unsigned long nr_pages; |
1224 | u64 cur_end; | |
97db1204 NB |
1225 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
1226 | int i; | |
1227 | bool should_compress; | |
b1c16ac9 | 1228 | unsigned nofs_flag; |
fac07d2b | 1229 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
771ed689 | 1230 | |
69684c5a | 1231 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
97db1204 NB |
1232 | |
1233 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && | |
1234 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { | |
1235 | num_chunks = 1; | |
1236 | should_compress = false; | |
1237 | } else { | |
1238 | should_compress = true; | |
1239 | } | |
1240 | ||
b1c16ac9 NB |
1241 | nofs_flag = memalloc_nofs_save(); |
1242 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); | |
1243 | memalloc_nofs_restore(nofs_flag); | |
1244 | ||
97db1204 NB |
1245 | if (!ctx) { |
1246 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | | |
1247 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | | |
1248 | EXTENT_DO_ACCOUNTING; | |
1249 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1250 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | | |
1251 | PAGE_SET_ERROR; | |
1252 | ||
74e9194a | 1253 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
97db1204 NB |
1254 | clear_bits, page_ops); |
1255 | return -ENOMEM; | |
1256 | } | |
1257 | ||
1258 | async_chunk = ctx->chunks; | |
1259 | atomic_set(&ctx->num_chunks, num_chunks); | |
1260 | ||
1261 | for (i = 0; i < num_chunks; i++) { | |
1262 | if (should_compress) | |
1263 | cur_end = min(end, start + SZ_512K - 1); | |
1264 | else | |
1265 | cur_end = end; | |
771ed689 | 1266 | |
bd4691a0 NB |
1267 | /* |
1268 | * igrab is called higher up in the call chain, take only the | |
1269 | * lightweight reference for the callback lifetime | |
1270 | */ | |
1271 | ihold(inode); | |
97db1204 NB |
1272 | async_chunk[i].pending = &ctx->num_chunks; |
1273 | async_chunk[i].inode = inode; | |
1274 | async_chunk[i].start = start; | |
1275 | async_chunk[i].end = cur_end; | |
97db1204 NB |
1276 | async_chunk[i].write_flags = write_flags; |
1277 | INIT_LIST_HEAD(&async_chunk[i].extents); | |
1278 | ||
1d53c9e6 CM |
1279 | /* |
1280 | * The locked_page comes all the way from writepage and its | |
1281 | * the original page we were actually given. As we spread | |
1282 | * this large delalloc region across multiple async_chunk | |
1283 | * structs, only the first struct needs a pointer to locked_page | |
1284 | * | |
1285 | * This way we don't need racey decisions about who is supposed | |
1286 | * to unlock it. | |
1287 | */ | |
1288 | if (locked_page) { | |
ec39f769 CM |
1289 | /* |
1290 | * Depending on the compressibility, the pages might or | |
1291 | * might not go through async. We want all of them to | |
1292 | * be accounted against wbc once. Let's do it here | |
1293 | * before the paths diverge. wbc accounting is used | |
1294 | * only for foreign writeback detection and doesn't | |
1295 | * need full accuracy. Just account the whole thing | |
1296 | * against the first page. | |
1297 | */ | |
1298 | wbc_account_cgroup_owner(wbc, locked_page, | |
1299 | cur_end - start); | |
1d53c9e6 CM |
1300 | async_chunk[i].locked_page = locked_page; |
1301 | locked_page = NULL; | |
1302 | } else { | |
1303 | async_chunk[i].locked_page = NULL; | |
1304 | } | |
1305 | ||
ec39f769 CM |
1306 | if (blkcg_css != blkcg_root_css) { |
1307 | css_get(blkcg_css); | |
1308 | async_chunk[i].blkcg_css = blkcg_css; | |
1309 | } else { | |
1310 | async_chunk[i].blkcg_css = NULL; | |
1311 | } | |
1312 | ||
a0cac0ec OS |
1313 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
1314 | async_cow_submit, async_cow_free); | |
771ed689 | 1315 | |
97db1204 | 1316 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
0b246afa | 1317 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1318 | |
97db1204 | 1319 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
771ed689 | 1320 | |
771ed689 CM |
1321 | *nr_written += nr_pages; |
1322 | start = cur_end + 1; | |
1323 | } | |
1324 | *page_started = 1; | |
1325 | return 0; | |
be20aa9d CM |
1326 | } |
1327 | ||
2ff7e61e | 1328 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1329 | u64 bytenr, u64 num_bytes) |
1330 | { | |
1331 | int ret; | |
1332 | struct btrfs_ordered_sum *sums; | |
1333 | LIST_HEAD(list); | |
1334 | ||
0b246afa | 1335 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1336 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1337 | if (ret == 0 && list_empty(&list)) |
1338 | return 0; | |
1339 | ||
1340 | while (!list_empty(&list)) { | |
1341 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1342 | list_del(&sums->list); | |
1343 | kfree(sums); | |
1344 | } | |
58113753 LB |
1345 | if (ret < 0) |
1346 | return ret; | |
17d217fe YZ |
1347 | return 1; |
1348 | } | |
1349 | ||
d352ac68 CM |
1350 | /* |
1351 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1352 | * of the extents that exist in the file, and COWs the file as required. | |
1353 | * | |
1354 | * If no cow copies or snapshots exist, we write directly to the existing | |
1355 | * blocks on disk | |
1356 | */ | |
7f366cfe CM |
1357 | static noinline int run_delalloc_nocow(struct inode *inode, |
1358 | struct page *locked_page, | |
3e024846 NB |
1359 | const u64 start, const u64 end, |
1360 | int *page_started, int force, | |
1361 | unsigned long *nr_written) | |
be20aa9d | 1362 | { |
0b246afa | 1363 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d | 1364 | struct btrfs_root *root = BTRFS_I(inode)->root; |
be20aa9d | 1365 | struct btrfs_path *path; |
3e024846 NB |
1366 | u64 cow_start = (u64)-1; |
1367 | u64 cur_offset = start; | |
8ecebf4d | 1368 | int ret; |
3e024846 NB |
1369 | bool check_prev = true; |
1370 | const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); | |
4a0cc7ca | 1371 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
762bf098 NB |
1372 | bool nocow = false; |
1373 | u64 disk_bytenr = 0; | |
be20aa9d CM |
1374 | |
1375 | path = btrfs_alloc_path(); | |
17ca04af | 1376 | if (!path) { |
74e9194a | 1377 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
c2790a2e | 1378 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1379 | EXTENT_DO_ACCOUNTING | |
1380 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1381 | PAGE_CLEAR_DIRTY | |
1382 | PAGE_SET_WRITEBACK | | |
1383 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1384 | return -ENOMEM; |
17ca04af | 1385 | } |
82d5902d | 1386 | |
80ff3856 | 1387 | while (1) { |
3e024846 NB |
1388 | struct btrfs_key found_key; |
1389 | struct btrfs_file_extent_item *fi; | |
1390 | struct extent_buffer *leaf; | |
1391 | u64 extent_end; | |
1392 | u64 extent_offset; | |
3e024846 NB |
1393 | u64 num_bytes = 0; |
1394 | u64 disk_num_bytes; | |
3e024846 NB |
1395 | u64 ram_bytes; |
1396 | int extent_type; | |
762bf098 NB |
1397 | |
1398 | nocow = false; | |
3e024846 | 1399 | |
e4c3b2dc | 1400 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1401 | cur_offset, 0); |
d788a349 | 1402 | if (ret < 0) |
79787eaa | 1403 | goto error; |
a6bd9cd1 NB |
1404 | |
1405 | /* | |
1406 | * If there is no extent for our range when doing the initial | |
1407 | * search, then go back to the previous slot as it will be the | |
1408 | * one containing the search offset | |
1409 | */ | |
80ff3856 YZ |
1410 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1411 | leaf = path->nodes[0]; | |
1412 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1413 | path->slots[0] - 1); | |
33345d01 | 1414 | if (found_key.objectid == ino && |
80ff3856 YZ |
1415 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1416 | path->slots[0]--; | |
1417 | } | |
3e024846 | 1418 | check_prev = false; |
80ff3856 | 1419 | next_slot: |
a6bd9cd1 | 1420 | /* Go to next leaf if we have exhausted the current one */ |
80ff3856 YZ |
1421 | leaf = path->nodes[0]; |
1422 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1423 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1424 | if (ret < 0) { |
1425 | if (cow_start != (u64)-1) | |
1426 | cur_offset = cow_start; | |
79787eaa | 1427 | goto error; |
e8916699 | 1428 | } |
80ff3856 YZ |
1429 | if (ret > 0) |
1430 | break; | |
1431 | leaf = path->nodes[0]; | |
1432 | } | |
be20aa9d | 1433 | |
80ff3856 YZ |
1434 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1435 | ||
a6bd9cd1 | 1436 | /* Didn't find anything for our INO */ |
1d512cb7 FM |
1437 | if (found_key.objectid > ino) |
1438 | break; | |
a6bd9cd1 NB |
1439 | /* |
1440 | * Keep searching until we find an EXTENT_ITEM or there are no | |
1441 | * more extents for this inode | |
1442 | */ | |
1d512cb7 FM |
1443 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
1444 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1445 | path->slots[0]++; | |
1446 | goto next_slot; | |
1447 | } | |
a6bd9cd1 NB |
1448 | |
1449 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ | |
1d512cb7 | 1450 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
80ff3856 YZ |
1451 | found_key.offset > end) |
1452 | break; | |
1453 | ||
a6bd9cd1 NB |
1454 | /* |
1455 | * If the found extent starts after requested offset, then | |
1456 | * adjust extent_end to be right before this extent begins | |
1457 | */ | |
80ff3856 YZ |
1458 | if (found_key.offset > cur_offset) { |
1459 | extent_end = found_key.offset; | |
e9061e21 | 1460 | extent_type = 0; |
80ff3856 YZ |
1461 | goto out_check; |
1462 | } | |
1463 | ||
a6bd9cd1 NB |
1464 | /* |
1465 | * Found extent which begins before our range and potentially | |
1466 | * intersect it | |
1467 | */ | |
80ff3856 YZ |
1468 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1469 | struct btrfs_file_extent_item); | |
1470 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1471 | ||
cc95bef6 | 1472 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1473 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1474 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1475 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1476 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1477 | extent_end = found_key.offset + |
1478 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1479 | disk_num_bytes = |
1480 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
a6bd9cd1 | 1481 | /* |
de7999af FM |
1482 | * If the extent we got ends before our current offset, |
1483 | * skip to the next extent. | |
a6bd9cd1 | 1484 | */ |
de7999af | 1485 | if (extent_end <= cur_offset) { |
80ff3856 YZ |
1486 | path->slots[0]++; |
1487 | goto next_slot; | |
1488 | } | |
a6bd9cd1 | 1489 | /* Skip holes */ |
17d217fe YZ |
1490 | if (disk_bytenr == 0) |
1491 | goto out_check; | |
a6bd9cd1 | 1492 | /* Skip compressed/encrypted/encoded extents */ |
80ff3856 YZ |
1493 | if (btrfs_file_extent_compression(leaf, fi) || |
1494 | btrfs_file_extent_encryption(leaf, fi) || | |
1495 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1496 | goto out_check; | |
78d4295b | 1497 | /* |
a6bd9cd1 NB |
1498 | * If extent is created before the last volume's snapshot |
1499 | * this implies the extent is shared, hence we can't do | |
1500 | * nocow. This is the same check as in | |
1501 | * btrfs_cross_ref_exist but without calling | |
1502 | * btrfs_search_slot. | |
78d4295b | 1503 | */ |
3e024846 | 1504 | if (!freespace_inode && |
27a7ff55 | 1505 | btrfs_file_extent_generation(leaf, fi) <= |
78d4295b EL |
1506 | btrfs_root_last_snapshot(&root->root_item)) |
1507 | goto out_check; | |
d899e052 YZ |
1508 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1509 | goto out_check; | |
a6bd9cd1 | 1510 | /* If extent is RO, we must COW it */ |
2ff7e61e | 1511 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1512 | goto out_check; |
58113753 LB |
1513 | ret = btrfs_cross_ref_exist(root, ino, |
1514 | found_key.offset - | |
1515 | extent_offset, disk_bytenr); | |
1516 | if (ret) { | |
1517 | /* | |
1518 | * ret could be -EIO if the above fails to read | |
1519 | * metadata. | |
1520 | */ | |
1521 | if (ret < 0) { | |
1522 | if (cow_start != (u64)-1) | |
1523 | cur_offset = cow_start; | |
1524 | goto error; | |
1525 | } | |
1526 | ||
3e024846 | 1527 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1528 | goto out_check; |
58113753 | 1529 | } |
5d4f98a2 | 1530 | disk_bytenr += extent_offset; |
17d217fe YZ |
1531 | disk_bytenr += cur_offset - found_key.offset; |
1532 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 | 1533 | /* |
a6bd9cd1 NB |
1534 | * If there are pending snapshots for this root, we |
1535 | * fall into common COW way | |
e9894fd3 | 1536 | */ |
3e024846 | 1537 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
8ecebf4d | 1538 | goto out_check; |
17d217fe YZ |
1539 | /* |
1540 | * force cow if csum exists in the range. | |
1541 | * this ensure that csum for a given extent are | |
1542 | * either valid or do not exist. | |
1543 | */ | |
58113753 LB |
1544 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1545 | num_bytes); | |
1546 | if (ret) { | |
58113753 LB |
1547 | /* |
1548 | * ret could be -EIO if the above fails to read | |
1549 | * metadata. | |
1550 | */ | |
1551 | if (ret < 0) { | |
1552 | if (cow_start != (u64)-1) | |
1553 | cur_offset = cow_start; | |
1554 | goto error; | |
1555 | } | |
3e024846 | 1556 | WARN_ON_ONCE(freespace_inode); |
17d217fe | 1557 | goto out_check; |
91e1f56a | 1558 | } |
8ecebf4d | 1559 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1560 | goto out_check; |
3e024846 | 1561 | nocow = true; |
80ff3856 | 1562 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e8e21007 NB |
1563 | extent_end = found_key.offset + ram_bytes; |
1564 | extent_end = ALIGN(extent_end, fs_info->sectorsize); | |
922f0518 NB |
1565 | /* Skip extents outside of our requested range */ |
1566 | if (extent_end <= start) { | |
1567 | path->slots[0]++; | |
1568 | goto next_slot; | |
1569 | } | |
80ff3856 | 1570 | } else { |
e8e21007 | 1571 | /* If this triggers then we have a memory corruption */ |
290342f6 | 1572 | BUG(); |
80ff3856 YZ |
1573 | } |
1574 | out_check: | |
a6bd9cd1 NB |
1575 | /* |
1576 | * If nocow is false then record the beginning of the range | |
1577 | * that needs to be COWed | |
1578 | */ | |
80ff3856 YZ |
1579 | if (!nocow) { |
1580 | if (cow_start == (u64)-1) | |
1581 | cow_start = cur_offset; | |
1582 | cur_offset = extent_end; | |
1583 | if (cur_offset > end) | |
1584 | break; | |
1585 | path->slots[0]++; | |
1586 | goto next_slot; | |
7ea394f1 YZ |
1587 | } |
1588 | ||
b3b4aa74 | 1589 | btrfs_release_path(path); |
a6bd9cd1 NB |
1590 | |
1591 | /* | |
1592 | * COW range from cow_start to found_key.offset - 1. As the key | |
1593 | * will contain the beginning of the first extent that can be | |
1594 | * NOCOW, following one which needs to be COW'ed | |
1595 | */ | |
80ff3856 | 1596 | if (cow_start != (u64)-1) { |
00361589 JB |
1597 | ret = cow_file_range(inode, locked_page, |
1598 | cow_start, found_key.offset - 1, | |
330a5827 | 1599 | page_started, nr_written, 1); |
e9894fd3 | 1600 | if (ret) { |
f78c436c | 1601 | if (nocow) |
0b246afa | 1602 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1603 | disk_bytenr); |
79787eaa | 1604 | goto error; |
e9894fd3 | 1605 | } |
80ff3856 | 1606 | cow_start = (u64)-1; |
7ea394f1 | 1607 | } |
80ff3856 | 1608 | |
d899e052 | 1609 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db | 1610 | u64 orig_start = found_key.offset - extent_offset; |
3e024846 | 1611 | struct extent_map *em; |
6f9994db LB |
1612 | |
1613 | em = create_io_em(inode, cur_offset, num_bytes, | |
1614 | orig_start, | |
1615 | disk_bytenr, /* block_start */ | |
1616 | num_bytes, /* block_len */ | |
1617 | disk_num_bytes, /* orig_block_len */ | |
1618 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1619 | BTRFS_ORDERED_PREALLOC); | |
1620 | if (IS_ERR(em)) { | |
6f9994db LB |
1621 | if (nocow) |
1622 | btrfs_dec_nocow_writers(fs_info, | |
1623 | disk_bytenr); | |
1624 | ret = PTR_ERR(em); | |
1625 | goto error; | |
d899e052 | 1626 | } |
6f9994db | 1627 | free_extent_map(em); |
bb55f626 NB |
1628 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
1629 | disk_bytenr, num_bytes, | |
1630 | num_bytes, | |
1631 | BTRFS_ORDERED_PREALLOC); | |
762bf098 NB |
1632 | if (ret) { |
1633 | btrfs_drop_extent_cache(BTRFS_I(inode), | |
1634 | cur_offset, | |
1635 | cur_offset + num_bytes - 1, | |
1636 | 0); | |
1637 | goto error; | |
1638 | } | |
d899e052 | 1639 | } else { |
bb55f626 NB |
1640 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
1641 | disk_bytenr, num_bytes, | |
1642 | num_bytes, | |
1643 | BTRFS_ORDERED_NOCOW); | |
762bf098 NB |
1644 | if (ret) |
1645 | goto error; | |
d899e052 | 1646 | } |
80ff3856 | 1647 | |
f78c436c | 1648 | if (nocow) |
0b246afa | 1649 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
762bf098 | 1650 | nocow = false; |
771ed689 | 1651 | |
efa56464 | 1652 | if (root->root_key.objectid == |
4dbd80fb QW |
1653 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1654 | /* | |
1655 | * Error handled later, as we must prevent | |
1656 | * extent_clear_unlock_delalloc() in error handler | |
1657 | * from freeing metadata of created ordered extent. | |
1658 | */ | |
efa56464 YZ |
1659 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1660 | num_bytes); | |
efa56464 | 1661 | |
c2790a2e | 1662 | extent_clear_unlock_delalloc(inode, cur_offset, |
74e9194a | 1663 | cur_offset + num_bytes - 1, |
c2790a2e | 1664 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1665 | EXTENT_DELALLOC | |
1666 | EXTENT_CLEAR_DATA_RESV, | |
1667 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1668 | ||
80ff3856 | 1669 | cur_offset = extent_end; |
4dbd80fb QW |
1670 | |
1671 | /* | |
1672 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1673 | * handler, as metadata for created ordered extent will only | |
1674 | * be freed by btrfs_finish_ordered_io(). | |
1675 | */ | |
1676 | if (ret) | |
1677 | goto error; | |
80ff3856 YZ |
1678 | if (cur_offset > end) |
1679 | break; | |
be20aa9d | 1680 | } |
b3b4aa74 | 1681 | btrfs_release_path(path); |
80ff3856 | 1682 | |
506481b2 | 1683 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1684 | cow_start = cur_offset; |
17ca04af | 1685 | |
80ff3856 | 1686 | if (cow_start != (u64)-1) { |
506481b2 | 1687 | cur_offset = end; |
74e9194a | 1688 | ret = cow_file_range(inode, locked_page, cow_start, end, |
330a5827 | 1689 | page_started, nr_written, 1); |
d788a349 | 1690 | if (ret) |
79787eaa | 1691 | goto error; |
80ff3856 YZ |
1692 | } |
1693 | ||
79787eaa | 1694 | error: |
762bf098 NB |
1695 | if (nocow) |
1696 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); | |
1697 | ||
17ca04af | 1698 | if (ret && cur_offset < end) |
74e9194a | 1699 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
c2790a2e | 1700 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1701 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1702 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1703 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1704 | PAGE_SET_WRITEBACK | |
1705 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1706 | btrfs_free_path(path); |
79787eaa | 1707 | return ret; |
be20aa9d CM |
1708 | } |
1709 | ||
47059d93 WS |
1710 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1711 | { | |
1712 | ||
1713 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1714 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1715 | return 0; | |
1716 | ||
1717 | /* | |
1718 | * @defrag_bytes is a hint value, no spinlock held here, | |
1719 | * if is not zero, it means the file is defragging. | |
1720 | * Force cow if given extent needs to be defragged. | |
1721 | */ | |
1722 | if (BTRFS_I(inode)->defrag_bytes && | |
1723 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1724 | EXTENT_DEFRAG, 0, NULL)) | |
1725 | return 1; | |
1726 | ||
1727 | return 0; | |
1728 | } | |
1729 | ||
d352ac68 | 1730 | /* |
5eaad97a NB |
1731 | * Function to process delayed allocation (create CoW) for ranges which are |
1732 | * being touched for the first time. | |
d352ac68 | 1733 | */ |
bc9a8bf7 | 1734 | int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page, |
5eaad97a NB |
1735 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1736 | struct writeback_control *wbc) | |
be20aa9d | 1737 | { |
be20aa9d | 1738 | int ret; |
47059d93 | 1739 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1740 | |
47059d93 | 1741 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1742 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1743 | page_started, 1, nr_written); |
47059d93 | 1744 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1745 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1746 | page_started, 0, nr_written); |
42c16da6 QW |
1747 | } else if (!inode_can_compress(inode) || |
1748 | !inode_need_compress(inode, start, end)) { | |
74e9194a | 1749 | ret = cow_file_range(inode, locked_page, start, end, |
330a5827 | 1750 | page_started, nr_written, 1); |
7ddf5a42 JB |
1751 | } else { |
1752 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1753 | &BTRFS_I(inode)->runtime_flags); | |
ec39f769 | 1754 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
fac07d2b | 1755 | page_started, nr_written); |
7ddf5a42 | 1756 | } |
52427260 | 1757 | if (ret) |
d1051d6e NB |
1758 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
1759 | end - start + 1); | |
b888db2b CM |
1760 | return ret; |
1761 | } | |
1762 | ||
abbb55f4 NB |
1763 | void btrfs_split_delalloc_extent(struct inode *inode, |
1764 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1765 | { |
dcab6a3b JB |
1766 | u64 size; |
1767 | ||
0ca1f7ce | 1768 | /* not delalloc, ignore it */ |
9ed74f2d | 1769 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1770 | return; |
9ed74f2d | 1771 | |
dcab6a3b JB |
1772 | size = orig->end - orig->start + 1; |
1773 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1774 | u32 num_extents; |
dcab6a3b JB |
1775 | u64 new_size; |
1776 | ||
1777 | /* | |
5c848198 | 1778 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 1779 | * applies here, just in reverse. |
dcab6a3b JB |
1780 | */ |
1781 | new_size = orig->end - split + 1; | |
823bb20a | 1782 | num_extents = count_max_extents(new_size); |
ba117213 | 1783 | new_size = split - orig->start; |
823bb20a DS |
1784 | num_extents += count_max_extents(new_size); |
1785 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1786 | return; |
1787 | } | |
1788 | ||
9e0baf60 | 1789 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1790 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1791 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1792 | } |
1793 | ||
1794 | /* | |
5c848198 NB |
1795 | * Handle merged delayed allocation extents so we can keep track of new extents |
1796 | * that are just merged onto old extents, such as when we are doing sequential | |
1797 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 1798 | */ |
5c848198 NB |
1799 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
1800 | struct extent_state *other) | |
9ed74f2d | 1801 | { |
dcab6a3b | 1802 | u64 new_size, old_size; |
823bb20a | 1803 | u32 num_extents; |
dcab6a3b | 1804 | |
9ed74f2d JB |
1805 | /* not delalloc, ignore it */ |
1806 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1807 | return; |
9ed74f2d | 1808 | |
8461a3de JB |
1809 | if (new->start > other->start) |
1810 | new_size = new->end - other->start + 1; | |
1811 | else | |
1812 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1813 | |
1814 | /* we're not bigger than the max, unreserve the space and go */ | |
1815 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1816 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1817 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1818 | spin_unlock(&BTRFS_I(inode)->lock); |
1819 | return; | |
1820 | } | |
1821 | ||
1822 | /* | |
ba117213 JB |
1823 | * We have to add up either side to figure out how many extents were |
1824 | * accounted for before we merged into one big extent. If the number of | |
1825 | * extents we accounted for is <= the amount we need for the new range | |
1826 | * then we can return, otherwise drop. Think of it like this | |
1827 | * | |
1828 | * [ 4k][MAX_SIZE] | |
1829 | * | |
1830 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1831 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1832 | * we have 1 so they are == and we can return. But in this case | |
1833 | * | |
1834 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1835 | * | |
1836 | * Each range on their own accounts for 2 extents, but merged together | |
1837 | * they are only 3 extents worth of accounting, so we need to drop in | |
1838 | * this case. | |
dcab6a3b | 1839 | */ |
ba117213 | 1840 | old_size = other->end - other->start + 1; |
823bb20a | 1841 | num_extents = count_max_extents(old_size); |
ba117213 | 1842 | old_size = new->end - new->start + 1; |
823bb20a DS |
1843 | num_extents += count_max_extents(old_size); |
1844 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1845 | return; |
1846 | ||
9e0baf60 | 1847 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1848 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1849 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1850 | } |
1851 | ||
eb73c1b7 MX |
1852 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1853 | struct inode *inode) | |
1854 | { | |
0b246afa JM |
1855 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1856 | ||
eb73c1b7 MX |
1857 | spin_lock(&root->delalloc_lock); |
1858 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1859 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1860 | &root->delalloc_inodes); | |
1861 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1862 | &BTRFS_I(inode)->runtime_flags); | |
1863 | root->nr_delalloc_inodes++; | |
1864 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1865 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1866 | BUG_ON(!list_empty(&root->delalloc_root)); |
1867 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1868 | &fs_info->delalloc_roots); |
1869 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1870 | } |
1871 | } | |
1872 | spin_unlock(&root->delalloc_lock); | |
1873 | } | |
1874 | ||
2b877331 NB |
1875 | |
1876 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1877 | struct btrfs_inode *inode) | |
eb73c1b7 | 1878 | { |
3ffbd68c | 1879 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 1880 | |
9e3e97f4 NB |
1881 | if (!list_empty(&inode->delalloc_inodes)) { |
1882 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1883 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1884 | &inode->runtime_flags); |
eb73c1b7 MX |
1885 | root->nr_delalloc_inodes--; |
1886 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 1887 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 1888 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1889 | BUG_ON(list_empty(&root->delalloc_root)); |
1890 | list_del_init(&root->delalloc_root); | |
0b246afa | 1891 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1892 | } |
1893 | } | |
2b877331 NB |
1894 | } |
1895 | ||
1896 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1897 | struct btrfs_inode *inode) | |
1898 | { | |
1899 | spin_lock(&root->delalloc_lock); | |
1900 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
1901 | spin_unlock(&root->delalloc_lock); |
1902 | } | |
1903 | ||
d352ac68 | 1904 | /* |
e06a1fc9 NB |
1905 | * Properly track delayed allocation bytes in the inode and to maintain the |
1906 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 1907 | */ |
e06a1fc9 NB |
1908 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
1909 | unsigned *bits) | |
291d673e | 1910 | { |
0b246afa JM |
1911 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1912 | ||
47059d93 WS |
1913 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1914 | WARN_ON(1); | |
75eff68e CM |
1915 | /* |
1916 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1917 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1918 | * bit, which is only set or cleared with irqs on |
1919 | */ | |
0ca1f7ce | 1920 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1921 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1922 | u64 len = state->end + 1 - state->start; |
8b62f87b | 1923 | u32 num_extents = count_max_extents(len); |
70ddc553 | 1924 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1925 | |
8b62f87b JB |
1926 | spin_lock(&BTRFS_I(inode)->lock); |
1927 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
1928 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 1929 | |
6a3891c5 | 1930 | /* For sanity tests */ |
0b246afa | 1931 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1932 | return; |
1933 | ||
104b4e51 NB |
1934 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
1935 | fs_info->delalloc_batch); | |
df0af1a5 | 1936 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1937 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1938 | if (*bits & EXTENT_DEFRAG) |
1939 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1940 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1941 | &BTRFS_I(inode)->runtime_flags)) |
1942 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1943 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1944 | } |
a7e3b975 FM |
1945 | |
1946 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1947 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1948 | spin_lock(&BTRFS_I(inode)->lock); | |
1949 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1950 | state->start; | |
1951 | spin_unlock(&BTRFS_I(inode)->lock); | |
1952 | } | |
291d673e CM |
1953 | } |
1954 | ||
d352ac68 | 1955 | /* |
a36bb5f9 NB |
1956 | * Once a range is no longer delalloc this function ensures that proper |
1957 | * accounting happens. | |
d352ac68 | 1958 | */ |
a36bb5f9 NB |
1959 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
1960 | struct extent_state *state, unsigned *bits) | |
291d673e | 1961 | { |
a36bb5f9 NB |
1962 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
1963 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 1964 | u64 len = state->end + 1 - state->start; |
823bb20a | 1965 | u32 num_extents = count_max_extents(len); |
47059d93 | 1966 | |
4a4b964f FM |
1967 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
1968 | spin_lock(&inode->lock); | |
6fc0ef68 | 1969 | inode->defrag_bytes -= len; |
4a4b964f FM |
1970 | spin_unlock(&inode->lock); |
1971 | } | |
47059d93 | 1972 | |
75eff68e CM |
1973 | /* |
1974 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1975 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1976 | * bit, which is only set or cleared with irqs on |
1977 | */ | |
0ca1f7ce | 1978 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1979 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1980 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1981 | |
8b62f87b JB |
1982 | spin_lock(&inode->lock); |
1983 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
1984 | spin_unlock(&inode->lock); | |
0ca1f7ce | 1985 | |
b6d08f06 JB |
1986 | /* |
1987 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 1988 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
1989 | * error. |
1990 | */ | |
a315e68f | 1991 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1992 | root != fs_info->tree_root) |
43b18595 | 1993 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 1994 | |
6a3891c5 | 1995 | /* For sanity tests. */ |
0b246afa | 1996 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1997 | return; |
1998 | ||
a315e68f FM |
1999 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
2000 | do_list && !(state->state & EXTENT_NORESERVE) && | |
2001 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
2002 | btrfs_free_reserved_data_space_noquota( |
2003 | &inode->vfs_inode, | |
51773bec | 2004 | state->start, len); |
9ed74f2d | 2005 | |
104b4e51 NB |
2006 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
2007 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
2008 | spin_lock(&inode->lock); |
2009 | inode->delalloc_bytes -= len; | |
2010 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 2011 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 2012 | &inode->runtime_flags)) |
eb73c1b7 | 2013 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 2014 | spin_unlock(&inode->lock); |
291d673e | 2015 | } |
a7e3b975 FM |
2016 | |
2017 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
2018 | (*bits & EXTENT_DELALLOC_NEW)) { | |
2019 | spin_lock(&inode->lock); | |
2020 | ASSERT(inode->new_delalloc_bytes >= len); | |
2021 | inode->new_delalloc_bytes -= len; | |
2022 | spin_unlock(&inode->lock); | |
2023 | } | |
291d673e CM |
2024 | } |
2025 | ||
d352ac68 | 2026 | /* |
da12fe54 NB |
2027 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
2028 | * in a chunk's stripe. This function ensures that bios do not span a | |
2029 | * stripe/chunk | |
6f034ece | 2030 | * |
da12fe54 NB |
2031 | * @page - The page we are about to add to the bio |
2032 | * @size - size we want to add to the bio | |
2033 | * @bio - bio we want to ensure is smaller than a stripe | |
2034 | * @bio_flags - flags of the bio | |
2035 | * | |
2036 | * return 1 if page cannot be added to the bio | |
2037 | * return 0 if page can be added to the bio | |
6f034ece | 2038 | * return error otherwise |
d352ac68 | 2039 | */ |
da12fe54 NB |
2040 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
2041 | unsigned long bio_flags) | |
239b14b3 | 2042 | { |
0b246afa JM |
2043 | struct inode *inode = page->mapping->host; |
2044 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 2045 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
2046 | u64 length = 0; |
2047 | u64 map_length; | |
239b14b3 | 2048 | int ret; |
89b798ad | 2049 | struct btrfs_io_geometry geom; |
239b14b3 | 2050 | |
771ed689 CM |
2051 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
2052 | return 0; | |
2053 | ||
4f024f37 | 2054 | length = bio->bi_iter.bi_size; |
239b14b3 | 2055 | map_length = length; |
89b798ad NB |
2056 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
2057 | &geom); | |
6f034ece LB |
2058 | if (ret < 0) |
2059 | return ret; | |
89b798ad NB |
2060 | |
2061 | if (geom.len < length + size) | |
239b14b3 | 2062 | return 1; |
3444a972 | 2063 | return 0; |
239b14b3 CM |
2064 | } |
2065 | ||
d352ac68 CM |
2066 | /* |
2067 | * in order to insert checksums into the metadata in large chunks, | |
2068 | * we wait until bio submission time. All the pages in the bio are | |
2069 | * checksummed and sums are attached onto the ordered extent record. | |
2070 | * | |
2071 | * At IO completion time the cums attached on the ordered extent record | |
2072 | * are inserted into the btree | |
2073 | */ | |
d0ee3934 | 2074 | static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, |
eaf25d93 | 2075 | u64 bio_offset) |
065631f6 | 2076 | { |
c6100a4b | 2077 | struct inode *inode = private_data; |
4e4cbee9 | 2078 | blk_status_t ret = 0; |
e015640f | 2079 | |
2ff7e61e | 2080 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 2081 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
2082 | return 0; |
2083 | } | |
e015640f | 2084 | |
d352ac68 | 2085 | /* |
cad321ad | 2086 | * extent_io.c submission hook. This does the right thing for csum calculation |
4c274bc6 LB |
2087 | * on write, or reading the csums from the tree before a read. |
2088 | * | |
2089 | * Rules about async/sync submit, | |
2090 | * a) read: sync submit | |
2091 | * | |
2092 | * b) write without checksum: sync submit | |
2093 | * | |
2094 | * c) write with checksum: | |
2095 | * c-1) if bio is issued by fsync: sync submit | |
2096 | * (sync_writers != 0) | |
2097 | * | |
2098 | * c-2) if root is reloc root: sync submit | |
2099 | * (only in case of buffered IO) | |
2100 | * | |
2101 | * c-3) otherwise: async submit | |
d352ac68 | 2102 | */ |
a56b1c7b | 2103 | static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
50489a57 NB |
2104 | int mirror_num, |
2105 | unsigned long bio_flags) | |
2106 | ||
44b8bd7e | 2107 | { |
0b246afa | 2108 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 2109 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 2110 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 2111 | blk_status_t ret = 0; |
19b9bdb0 | 2112 | int skip_sum; |
b812ce28 | 2113 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 2114 | |
6cbff00f | 2115 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 2116 | |
70ddc553 | 2117 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 2118 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 2119 | |
37226b21 | 2120 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 2121 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 2122 | if (ret) |
61891923 | 2123 | goto out; |
5fd02043 | 2124 | |
d20f7043 | 2125 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
2126 | ret = btrfs_submit_compressed_read(inode, bio, |
2127 | mirror_num, | |
2128 | bio_flags); | |
2129 | goto out; | |
c2db1073 | 2130 | } else if (!skip_sum) { |
2ff7e61e | 2131 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 2132 | if (ret) |
61891923 | 2133 | goto out; |
c2db1073 | 2134 | } |
4d1b5fb4 | 2135 | goto mapit; |
b812ce28 | 2136 | } else if (async && !skip_sum) { |
17d217fe YZ |
2137 | /* csum items have already been cloned */ |
2138 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
2139 | goto mapit; | |
19b9bdb0 | 2140 | /* we're doing a write, do the async checksumming */ |
c6100a4b | 2141 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
e7681167 | 2142 | 0, inode, btrfs_submit_bio_start); |
61891923 | 2143 | goto out; |
b812ce28 | 2144 | } else if (!skip_sum) { |
2ff7e61e | 2145 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
2146 | if (ret) |
2147 | goto out; | |
19b9bdb0 CM |
2148 | } |
2149 | ||
0b86a832 | 2150 | mapit: |
08635bae | 2151 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
61891923 SB |
2152 | |
2153 | out: | |
4e4cbee9 CH |
2154 | if (ret) { |
2155 | bio->bi_status = ret; | |
4246a0b6 CH |
2156 | bio_endio(bio); |
2157 | } | |
61891923 | 2158 | return ret; |
065631f6 | 2159 | } |
6885f308 | 2160 | |
d352ac68 CM |
2161 | /* |
2162 | * given a list of ordered sums record them in the inode. This happens | |
2163 | * at IO completion time based on sums calculated at bio submission time. | |
2164 | */ | |
ba1da2f4 | 2165 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2166 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2167 | { |
e6dcd2dc | 2168 | struct btrfs_ordered_sum *sum; |
ac01f26a | 2169 | int ret; |
e6dcd2dc | 2170 | |
c6e30871 | 2171 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2172 | trans->adding_csums = true; |
ac01f26a | 2173 | ret = btrfs_csum_file_blocks(trans, |
d20f7043 | 2174 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
7c2871a2 | 2175 | trans->adding_csums = false; |
ac01f26a NB |
2176 | if (ret) |
2177 | return ret; | |
e6dcd2dc CM |
2178 | } |
2179 | return 0; | |
2180 | } | |
2181 | ||
2ac55d41 | 2182 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
e3b8a485 | 2183 | unsigned int extra_bits, |
330a5827 | 2184 | struct extent_state **cached_state) |
ea8c2819 | 2185 | { |
fdb1e121 | 2186 | WARN_ON(PAGE_ALIGNED(end)); |
ea8c2819 | 2187 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
e3b8a485 | 2188 | extra_bits, cached_state); |
ea8c2819 CM |
2189 | } |
2190 | ||
d352ac68 | 2191 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2192 | struct btrfs_writepage_fixup { |
2193 | struct page *page; | |
2194 | struct btrfs_work work; | |
2195 | }; | |
2196 | ||
b2950863 | 2197 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2198 | { |
2199 | struct btrfs_writepage_fixup *fixup; | |
2200 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2201 | struct extent_state *cached_state = NULL; |
364ecf36 | 2202 | struct extent_changeset *data_reserved = NULL; |
247e743c CM |
2203 | struct page *page; |
2204 | struct inode *inode; | |
2205 | u64 page_start; | |
2206 | u64 page_end; | |
87826df0 | 2207 | int ret; |
247e743c CM |
2208 | |
2209 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2210 | page = fixup->page; | |
4a096752 | 2211 | again: |
247e743c CM |
2212 | lock_page(page); |
2213 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2214 | ClearPageChecked(page); | |
2215 | goto out_page; | |
2216 | } | |
2217 | ||
2218 | inode = page->mapping->host; | |
2219 | page_start = page_offset(page); | |
09cbfeaf | 2220 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2221 | |
ff13db41 | 2222 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2223 | &cached_state); |
4a096752 CM |
2224 | |
2225 | /* already ordered? We're done */ | |
8b62b72b | 2226 | if (PagePrivate2(page)) |
247e743c | 2227 | goto out; |
4a096752 | 2228 | |
a776c6fa | 2229 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2230 | PAGE_SIZE); |
4a096752 | 2231 | if (ordered) { |
2ac55d41 | 2232 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
e43bbe5e | 2233 | page_end, &cached_state); |
4a096752 CM |
2234 | unlock_page(page); |
2235 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2236 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2237 | goto again; |
2238 | } | |
247e743c | 2239 | |
364ecf36 | 2240 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
09cbfeaf | 2241 | PAGE_SIZE); |
87826df0 JM |
2242 | if (ret) { |
2243 | mapping_set_error(page->mapping, ret); | |
2244 | end_extent_writepage(page, ret, page_start, page_end); | |
2245 | ClearPageChecked(page); | |
2246 | goto out; | |
2247 | } | |
2248 | ||
f3038ee3 | 2249 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 2250 | &cached_state); |
f3038ee3 NB |
2251 | if (ret) { |
2252 | mapping_set_error(page->mapping, ret); | |
2253 | end_extent_writepage(page, ret, page_start, page_end); | |
2254 | ClearPageChecked(page); | |
53687007 | 2255 | goto out_reserved; |
f3038ee3 NB |
2256 | } |
2257 | ||
247e743c | 2258 | ClearPageChecked(page); |
87826df0 | 2259 | set_page_dirty(page); |
53687007 | 2260 | out_reserved: |
8702ba93 | 2261 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
53687007 FM |
2262 | if (ret) |
2263 | btrfs_delalloc_release_space(inode, data_reserved, page_start, | |
2264 | PAGE_SIZE, true); | |
247e743c | 2265 | out: |
2ac55d41 | 2266 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
e43bbe5e | 2267 | &cached_state); |
247e743c CM |
2268 | out_page: |
2269 | unlock_page(page); | |
09cbfeaf | 2270 | put_page(page); |
b897abec | 2271 | kfree(fixup); |
364ecf36 | 2272 | extent_changeset_free(data_reserved); |
247e743c CM |
2273 | } |
2274 | ||
2275 | /* | |
2276 | * There are a few paths in the higher layers of the kernel that directly | |
2277 | * set the page dirty bit without asking the filesystem if it is a | |
2278 | * good idea. This causes problems because we want to make sure COW | |
2279 | * properly happens and the data=ordered rules are followed. | |
2280 | * | |
c8b97818 | 2281 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2282 | * hasn't been properly setup for IO. We kick off an async process |
2283 | * to fix it up. The async helper will wait for ordered extents, set | |
2284 | * the delalloc bit and make it safe to write the page. | |
2285 | */ | |
d75855b4 | 2286 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
247e743c CM |
2287 | { |
2288 | struct inode *inode = page->mapping->host; | |
0b246afa | 2289 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2290 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2291 | |
8b62b72b CM |
2292 | /* this page is properly in the ordered list */ |
2293 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2294 | return 0; |
2295 | ||
2296 | if (PageChecked(page)) | |
2297 | return -EAGAIN; | |
2298 | ||
2299 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2300 | if (!fixup) | |
2301 | return -EAGAIN; | |
f421950f | 2302 | |
247e743c | 2303 | SetPageChecked(page); |
09cbfeaf | 2304 | get_page(page); |
a0cac0ec | 2305 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
247e743c | 2306 | fixup->page = page; |
0b246afa | 2307 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2308 | return -EBUSY; |
247e743c CM |
2309 | } |
2310 | ||
d899e052 YZ |
2311 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2312 | struct inode *inode, u64 file_pos, | |
2313 | u64 disk_bytenr, u64 disk_num_bytes, | |
2314 | u64 num_bytes, u64 ram_bytes, | |
2315 | u8 compression, u8 encryption, | |
2316 | u16 other_encoding, int extent_type) | |
2317 | { | |
2318 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2319 | struct btrfs_file_extent_item *fi; | |
2320 | struct btrfs_path *path; | |
2321 | struct extent_buffer *leaf; | |
2322 | struct btrfs_key ins; | |
a12b877b | 2323 | u64 qg_released; |
1acae57b | 2324 | int extent_inserted = 0; |
d899e052 YZ |
2325 | int ret; |
2326 | ||
2327 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2328 | if (!path) |
2329 | return -ENOMEM; | |
d899e052 | 2330 | |
a1ed835e CM |
2331 | /* |
2332 | * we may be replacing one extent in the tree with another. | |
2333 | * The new extent is pinned in the extent map, and we don't want | |
2334 | * to drop it from the cache until it is completely in the btree. | |
2335 | * | |
2336 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2337 | * the caller is expected to unpin it and allow it to be merged | |
2338 | * with the others. | |
2339 | */ | |
1acae57b FDBM |
2340 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2341 | file_pos + num_bytes, NULL, 0, | |
2342 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2343 | if (ret) |
2344 | goto out; | |
d899e052 | 2345 | |
1acae57b | 2346 | if (!extent_inserted) { |
4a0cc7ca | 2347 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2348 | ins.offset = file_pos; |
2349 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2350 | ||
2351 | path->leave_spinning = 1; | |
2352 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2353 | sizeof(*fi)); | |
2354 | if (ret) | |
2355 | goto out; | |
2356 | } | |
d899e052 YZ |
2357 | leaf = path->nodes[0]; |
2358 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2359 | struct btrfs_file_extent_item); | |
2360 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2361 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2362 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2363 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2364 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2365 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2366 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2367 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2368 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2369 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2370 | |
d899e052 | 2371 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2372 | btrfs_release_path(path); |
d899e052 YZ |
2373 | |
2374 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2375 | |
2376 | ins.objectid = disk_bytenr; | |
2377 | ins.offset = disk_num_bytes; | |
2378 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2379 | |
297d750b | 2380 | /* |
5846a3c2 QW |
2381 | * Release the reserved range from inode dirty range map, as it is |
2382 | * already moved into delayed_ref_head | |
297d750b | 2383 | */ |
a12b877b QW |
2384 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
2385 | if (ret < 0) | |
2386 | goto out; | |
2387 | qg_released = ret; | |
84f7d8e6 JB |
2388 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2389 | btrfs_ino(BTRFS_I(inode)), | |
2390 | file_pos, qg_released, &ins); | |
79787eaa | 2391 | out: |
d899e052 | 2392 | btrfs_free_path(path); |
b9473439 | 2393 | |
79787eaa | 2394 | return ret; |
d899e052 YZ |
2395 | } |
2396 | ||
38c227d8 LB |
2397 | /* snapshot-aware defrag */ |
2398 | struct sa_defrag_extent_backref { | |
2399 | struct rb_node node; | |
2400 | struct old_sa_defrag_extent *old; | |
2401 | u64 root_id; | |
2402 | u64 inum; | |
2403 | u64 file_pos; | |
2404 | u64 extent_offset; | |
2405 | u64 num_bytes; | |
2406 | u64 generation; | |
2407 | }; | |
2408 | ||
2409 | struct old_sa_defrag_extent { | |
2410 | struct list_head list; | |
2411 | struct new_sa_defrag_extent *new; | |
2412 | ||
2413 | u64 extent_offset; | |
2414 | u64 bytenr; | |
2415 | u64 offset; | |
2416 | u64 len; | |
2417 | int count; | |
2418 | }; | |
2419 | ||
2420 | struct new_sa_defrag_extent { | |
2421 | struct rb_root root; | |
2422 | struct list_head head; | |
2423 | struct btrfs_path *path; | |
2424 | struct inode *inode; | |
2425 | u64 file_pos; | |
2426 | u64 len; | |
2427 | u64 bytenr; | |
2428 | u64 disk_len; | |
2429 | u8 compress_type; | |
2430 | }; | |
2431 | ||
2432 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2433 | struct sa_defrag_extent_backref *b2) | |
2434 | { | |
2435 | if (b1->root_id < b2->root_id) | |
2436 | return -1; | |
2437 | else if (b1->root_id > b2->root_id) | |
2438 | return 1; | |
2439 | ||
2440 | if (b1->inum < b2->inum) | |
2441 | return -1; | |
2442 | else if (b1->inum > b2->inum) | |
2443 | return 1; | |
2444 | ||
2445 | if (b1->file_pos < b2->file_pos) | |
2446 | return -1; | |
2447 | else if (b1->file_pos > b2->file_pos) | |
2448 | return 1; | |
2449 | ||
2450 | /* | |
2451 | * [------------------------------] ===> (a range of space) | |
2452 | * |<--->| |<---->| =============> (fs/file tree A) | |
2453 | * |<---------------------------->| ===> (fs/file tree B) | |
2454 | * | |
2455 | * A range of space can refer to two file extents in one tree while | |
2456 | * refer to only one file extent in another tree. | |
2457 | * | |
2458 | * So we may process a disk offset more than one time(two extents in A) | |
2459 | * and locate at the same extent(one extent in B), then insert two same | |
2460 | * backrefs(both refer to the extent in B). | |
2461 | */ | |
2462 | return 0; | |
2463 | } | |
2464 | ||
2465 | static void backref_insert(struct rb_root *root, | |
2466 | struct sa_defrag_extent_backref *backref) | |
2467 | { | |
2468 | struct rb_node **p = &root->rb_node; | |
2469 | struct rb_node *parent = NULL; | |
2470 | struct sa_defrag_extent_backref *entry; | |
2471 | int ret; | |
2472 | ||
2473 | while (*p) { | |
2474 | parent = *p; | |
2475 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2476 | ||
2477 | ret = backref_comp(backref, entry); | |
2478 | if (ret < 0) | |
2479 | p = &(*p)->rb_left; | |
2480 | else | |
2481 | p = &(*p)->rb_right; | |
2482 | } | |
2483 | ||
2484 | rb_link_node(&backref->node, parent, p); | |
2485 | rb_insert_color(&backref->node, root); | |
2486 | } | |
2487 | ||
2488 | /* | |
2489 | * Note the backref might has changed, and in this case we just return 0. | |
2490 | */ | |
2491 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2492 | void *ctx) | |
2493 | { | |
2494 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2495 | struct old_sa_defrag_extent *old = ctx; |
2496 | struct new_sa_defrag_extent *new = old->new; | |
2497 | struct btrfs_path *path = new->path; | |
2498 | struct btrfs_key key; | |
2499 | struct btrfs_root *root; | |
2500 | struct sa_defrag_extent_backref *backref; | |
2501 | struct extent_buffer *leaf; | |
2502 | struct inode *inode = new->inode; | |
0b246afa | 2503 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2504 | int slot; |
2505 | int ret; | |
2506 | u64 extent_offset; | |
2507 | u64 num_bytes; | |
2508 | ||
2509 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2510 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2511 | return 0; |
2512 | ||
2513 | key.objectid = root_id; | |
2514 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2515 | key.offset = (u64)-1; | |
2516 | ||
38c227d8 LB |
2517 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2518 | if (IS_ERR(root)) { | |
2519 | if (PTR_ERR(root) == -ENOENT) | |
2520 | return 0; | |
2521 | WARN_ON(1); | |
ab8d0fc4 | 2522 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2523 | inum, offset, root_id); |
2524 | return PTR_ERR(root); | |
2525 | } | |
2526 | ||
2527 | key.objectid = inum; | |
2528 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2529 | if (offset > (u64)-1 << 32) | |
2530 | key.offset = 0; | |
2531 | else | |
2532 | key.offset = offset; | |
2533 | ||
2534 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2535 | if (WARN_ON(ret < 0)) |
38c227d8 | 2536 | return ret; |
50f1319c | 2537 | ret = 0; |
38c227d8 LB |
2538 | |
2539 | while (1) { | |
2540 | cond_resched(); | |
2541 | ||
2542 | leaf = path->nodes[0]; | |
2543 | slot = path->slots[0]; | |
2544 | ||
2545 | if (slot >= btrfs_header_nritems(leaf)) { | |
2546 | ret = btrfs_next_leaf(root, path); | |
2547 | if (ret < 0) { | |
2548 | goto out; | |
2549 | } else if (ret > 0) { | |
2550 | ret = 0; | |
2551 | goto out; | |
2552 | } | |
2553 | continue; | |
2554 | } | |
2555 | ||
2556 | path->slots[0]++; | |
2557 | ||
2558 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2559 | ||
2560 | if (key.objectid > inum) | |
2561 | goto out; | |
2562 | ||
2563 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2564 | continue; | |
2565 | ||
2566 | extent = btrfs_item_ptr(leaf, slot, | |
2567 | struct btrfs_file_extent_item); | |
2568 | ||
2569 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2570 | continue; | |
2571 | ||
e68afa49 LB |
2572 | /* |
2573 | * 'offset' refers to the exact key.offset, | |
2574 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2575 | * (key.offset - extent_offset). | |
2576 | */ | |
2577 | if (key.offset != offset) | |
38c227d8 LB |
2578 | continue; |
2579 | ||
e68afa49 | 2580 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2581 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2582 | |
38c227d8 LB |
2583 | if (extent_offset >= old->extent_offset + old->offset + |
2584 | old->len || extent_offset + num_bytes <= | |
2585 | old->extent_offset + old->offset) | |
2586 | continue; | |
38c227d8 LB |
2587 | break; |
2588 | } | |
2589 | ||
2590 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2591 | if (!backref) { | |
2592 | ret = -ENOENT; | |
2593 | goto out; | |
2594 | } | |
2595 | ||
2596 | backref->root_id = root_id; | |
2597 | backref->inum = inum; | |
e68afa49 | 2598 | backref->file_pos = offset; |
38c227d8 LB |
2599 | backref->num_bytes = num_bytes; |
2600 | backref->extent_offset = extent_offset; | |
2601 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2602 | backref->old = old; | |
2603 | backref_insert(&new->root, backref); | |
2604 | old->count++; | |
2605 | out: | |
2606 | btrfs_release_path(path); | |
2607 | WARN_ON(ret); | |
2608 | return ret; | |
2609 | } | |
2610 | ||
2611 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2612 | struct new_sa_defrag_extent *new) | |
2613 | { | |
0b246afa | 2614 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2615 | struct old_sa_defrag_extent *old, *tmp; |
2616 | int ret; | |
2617 | ||
2618 | new->path = path; | |
2619 | ||
2620 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2621 | ret = iterate_inodes_from_logical(old->bytenr + |
2622 | old->extent_offset, fs_info, | |
38c227d8 | 2623 | path, record_one_backref, |
c995ab3c | 2624 | old, false); |
4724b106 JB |
2625 | if (ret < 0 && ret != -ENOENT) |
2626 | return false; | |
38c227d8 LB |
2627 | |
2628 | /* no backref to be processed for this extent */ | |
2629 | if (!old->count) { | |
2630 | list_del(&old->list); | |
2631 | kfree(old); | |
2632 | } | |
2633 | } | |
2634 | ||
2635 | if (list_empty(&new->head)) | |
2636 | return false; | |
2637 | ||
2638 | return true; | |
2639 | } | |
2640 | ||
2641 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2642 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2643 | struct new_sa_defrag_extent *new) |
38c227d8 | 2644 | { |
116e0024 | 2645 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2646 | return 0; |
2647 | ||
2648 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2649 | return 0; | |
2650 | ||
116e0024 LB |
2651 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2652 | return 0; | |
2653 | ||
2654 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2655 | btrfs_file_extent_other_encoding(leaf, fi)) |
2656 | return 0; | |
2657 | ||
2658 | return 1; | |
2659 | } | |
2660 | ||
2661 | /* | |
2662 | * Note the backref might has changed, and in this case we just return 0. | |
2663 | */ | |
2664 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2665 | struct sa_defrag_extent_backref *prev, | |
2666 | struct sa_defrag_extent_backref *backref) | |
2667 | { | |
2668 | struct btrfs_file_extent_item *extent; | |
2669 | struct btrfs_file_extent_item *item; | |
2670 | struct btrfs_ordered_extent *ordered; | |
2671 | struct btrfs_trans_handle *trans; | |
82fa113f | 2672 | struct btrfs_ref ref = { 0 }; |
38c227d8 LB |
2673 | struct btrfs_root *root; |
2674 | struct btrfs_key key; | |
2675 | struct extent_buffer *leaf; | |
2676 | struct old_sa_defrag_extent *old = backref->old; | |
2677 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2678 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2679 | struct inode *inode; |
2680 | struct extent_state *cached = NULL; | |
2681 | int ret = 0; | |
2682 | u64 start; | |
2683 | u64 len; | |
2684 | u64 lock_start; | |
2685 | u64 lock_end; | |
2686 | bool merge = false; | |
2687 | int index; | |
2688 | ||
2689 | if (prev && prev->root_id == backref->root_id && | |
2690 | prev->inum == backref->inum && | |
2691 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2692 | merge = true; | |
2693 | ||
2694 | /* step 1: get root */ | |
2695 | key.objectid = backref->root_id; | |
2696 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2697 | key.offset = (u64)-1; | |
2698 | ||
38c227d8 LB |
2699 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2700 | ||
2701 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2702 | if (IS_ERR(root)) { | |
2703 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2704 | if (PTR_ERR(root) == -ENOENT) | |
2705 | return 0; | |
2706 | return PTR_ERR(root); | |
2707 | } | |
38c227d8 | 2708 | |
bcbba5e6 WS |
2709 | if (btrfs_root_readonly(root)) { |
2710 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2711 | return 0; | |
2712 | } | |
2713 | ||
38c227d8 LB |
2714 | /* step 2: get inode */ |
2715 | key.objectid = backref->inum; | |
2716 | key.type = BTRFS_INODE_ITEM_KEY; | |
2717 | key.offset = 0; | |
2718 | ||
4c66e0d4 | 2719 | inode = btrfs_iget(fs_info->sb, &key, root); |
38c227d8 LB |
2720 | if (IS_ERR(inode)) { |
2721 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2722 | return 0; | |
2723 | } | |
2724 | ||
2725 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2726 | ||
2727 | /* step 3: relink backref */ | |
2728 | lock_start = backref->file_pos; | |
2729 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2730 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2731 | &cached); |
38c227d8 LB |
2732 | |
2733 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2734 | if (ordered) { | |
2735 | btrfs_put_ordered_extent(ordered); | |
2736 | goto out_unlock; | |
2737 | } | |
2738 | ||
2739 | trans = btrfs_join_transaction(root); | |
2740 | if (IS_ERR(trans)) { | |
2741 | ret = PTR_ERR(trans); | |
2742 | goto out_unlock; | |
2743 | } | |
2744 | ||
2745 | key.objectid = backref->inum; | |
2746 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2747 | key.offset = backref->file_pos; | |
2748 | ||
2749 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2750 | if (ret < 0) { | |
2751 | goto out_free_path; | |
2752 | } else if (ret > 0) { | |
2753 | ret = 0; | |
2754 | goto out_free_path; | |
2755 | } | |
2756 | ||
2757 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2758 | struct btrfs_file_extent_item); | |
2759 | ||
2760 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2761 | backref->generation) | |
2762 | goto out_free_path; | |
2763 | ||
2764 | btrfs_release_path(path); | |
2765 | ||
2766 | start = backref->file_pos; | |
2767 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2768 | start += old->extent_offset + old->offset - | |
2769 | backref->extent_offset; | |
2770 | ||
2771 | len = min(backref->extent_offset + backref->num_bytes, | |
2772 | old->extent_offset + old->offset + old->len); | |
2773 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2774 | ||
2775 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2776 | start + len, 1); | |
2777 | if (ret) | |
2778 | goto out_free_path; | |
2779 | again: | |
4a0cc7ca | 2780 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2781 | key.type = BTRFS_EXTENT_DATA_KEY; |
2782 | key.offset = start; | |
2783 | ||
a09a0a70 | 2784 | path->leave_spinning = 1; |
38c227d8 LB |
2785 | if (merge) { |
2786 | struct btrfs_file_extent_item *fi; | |
2787 | u64 extent_len; | |
2788 | struct btrfs_key found_key; | |
2789 | ||
3c9665df | 2790 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2791 | if (ret < 0) |
2792 | goto out_free_path; | |
2793 | ||
2794 | path->slots[0]--; | |
2795 | leaf = path->nodes[0]; | |
2796 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2797 | ||
2798 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2799 | struct btrfs_file_extent_item); | |
2800 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2801 | ||
116e0024 LB |
2802 | if (extent_len + found_key.offset == start && |
2803 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2804 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2805 | extent_len + len); | |
2806 | btrfs_mark_buffer_dirty(leaf); | |
2807 | inode_add_bytes(inode, len); | |
2808 | ||
2809 | ret = 1; | |
2810 | goto out_free_path; | |
2811 | } else { | |
2812 | merge = false; | |
2813 | btrfs_release_path(path); | |
2814 | goto again; | |
2815 | } | |
2816 | } | |
2817 | ||
2818 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2819 | sizeof(*extent)); | |
2820 | if (ret) { | |
66642832 | 2821 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2822 | goto out_free_path; |
2823 | } | |
2824 | ||
2825 | leaf = path->nodes[0]; | |
2826 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2827 | struct btrfs_file_extent_item); | |
2828 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2829 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2830 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2831 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2832 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2833 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2834 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2835 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2836 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2837 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2838 | ||
2839 | btrfs_mark_buffer_dirty(leaf); | |
2840 | inode_add_bytes(inode, len); | |
a09a0a70 | 2841 | btrfs_release_path(path); |
38c227d8 | 2842 | |
82fa113f QW |
2843 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr, |
2844 | new->disk_len, 0); | |
2845 | btrfs_init_data_ref(&ref, backref->root_id, backref->inum, | |
2846 | new->file_pos); /* start - extent_offset */ | |
2847 | ret = btrfs_inc_extent_ref(trans, &ref); | |
38c227d8 | 2848 | if (ret) { |
66642832 | 2849 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2850 | goto out_free_path; |
2851 | } | |
2852 | ||
2853 | ret = 1; | |
2854 | out_free_path: | |
2855 | btrfs_release_path(path); | |
a09a0a70 | 2856 | path->leave_spinning = 0; |
3a45bb20 | 2857 | btrfs_end_transaction(trans); |
38c227d8 LB |
2858 | out_unlock: |
2859 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
e43bbe5e | 2860 | &cached); |
38c227d8 LB |
2861 | iput(inode); |
2862 | return ret; | |
2863 | } | |
2864 | ||
6f519564 LB |
2865 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2866 | { | |
2867 | struct old_sa_defrag_extent *old, *tmp; | |
2868 | ||
2869 | if (!new) | |
2870 | return; | |
2871 | ||
2872 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2873 | kfree(old); |
2874 | } | |
2875 | kfree(new); | |
2876 | } | |
2877 | ||
38c227d8 LB |
2878 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2879 | { | |
0b246afa | 2880 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2881 | struct btrfs_path *path; |
38c227d8 LB |
2882 | struct sa_defrag_extent_backref *backref; |
2883 | struct sa_defrag_extent_backref *prev = NULL; | |
38c227d8 LB |
2884 | struct rb_node *node; |
2885 | int ret; | |
2886 | ||
38c227d8 LB |
2887 | path = btrfs_alloc_path(); |
2888 | if (!path) | |
2889 | return; | |
2890 | ||
2891 | if (!record_extent_backrefs(path, new)) { | |
2892 | btrfs_free_path(path); | |
2893 | goto out; | |
2894 | } | |
2895 | btrfs_release_path(path); | |
2896 | ||
2897 | while (1) { | |
2898 | node = rb_first(&new->root); | |
2899 | if (!node) | |
2900 | break; | |
2901 | rb_erase(node, &new->root); | |
2902 | ||
2903 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2904 | ||
2905 | ret = relink_extent_backref(path, prev, backref); | |
2906 | WARN_ON(ret < 0); | |
2907 | ||
2908 | kfree(prev); | |
2909 | ||
2910 | if (ret == 1) | |
2911 | prev = backref; | |
2912 | else | |
2913 | prev = NULL; | |
2914 | cond_resched(); | |
2915 | } | |
2916 | kfree(prev); | |
2917 | ||
2918 | btrfs_free_path(path); | |
38c227d8 | 2919 | out: |
6f519564 LB |
2920 | free_sa_defrag_extent(new); |
2921 | ||
0b246afa JM |
2922 | atomic_dec(&fs_info->defrag_running); |
2923 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2924 | } |
2925 | ||
2926 | static struct new_sa_defrag_extent * | |
2927 | record_old_file_extents(struct inode *inode, | |
2928 | struct btrfs_ordered_extent *ordered) | |
2929 | { | |
0b246afa | 2930 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2931 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2932 | struct btrfs_path *path; | |
2933 | struct btrfs_key key; | |
6f519564 | 2934 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2935 | struct new_sa_defrag_extent *new; |
2936 | int ret; | |
2937 | ||
2938 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2939 | if (!new) | |
2940 | return NULL; | |
2941 | ||
2942 | new->inode = inode; | |
2943 | new->file_pos = ordered->file_offset; | |
2944 | new->len = ordered->len; | |
2945 | new->bytenr = ordered->start; | |
2946 | new->disk_len = ordered->disk_len; | |
2947 | new->compress_type = ordered->compress_type; | |
2948 | new->root = RB_ROOT; | |
2949 | INIT_LIST_HEAD(&new->head); | |
2950 | ||
2951 | path = btrfs_alloc_path(); | |
2952 | if (!path) | |
2953 | goto out_kfree; | |
2954 | ||
4a0cc7ca | 2955 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2956 | key.type = BTRFS_EXTENT_DATA_KEY; |
2957 | key.offset = new->file_pos; | |
2958 | ||
2959 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2960 | if (ret < 0) | |
2961 | goto out_free_path; | |
2962 | if (ret > 0 && path->slots[0] > 0) | |
2963 | path->slots[0]--; | |
2964 | ||
2965 | /* find out all the old extents for the file range */ | |
2966 | while (1) { | |
2967 | struct btrfs_file_extent_item *extent; | |
2968 | struct extent_buffer *l; | |
2969 | int slot; | |
2970 | u64 num_bytes; | |
2971 | u64 offset; | |
2972 | u64 end; | |
2973 | u64 disk_bytenr; | |
2974 | u64 extent_offset; | |
2975 | ||
2976 | l = path->nodes[0]; | |
2977 | slot = path->slots[0]; | |
2978 | ||
2979 | if (slot >= btrfs_header_nritems(l)) { | |
2980 | ret = btrfs_next_leaf(root, path); | |
2981 | if (ret < 0) | |
6f519564 | 2982 | goto out_free_path; |
38c227d8 LB |
2983 | else if (ret > 0) |
2984 | break; | |
2985 | continue; | |
2986 | } | |
2987 | ||
2988 | btrfs_item_key_to_cpu(l, &key, slot); | |
2989 | ||
4a0cc7ca | 2990 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2991 | break; |
2992 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2993 | break; | |
2994 | if (key.offset >= new->file_pos + new->len) | |
2995 | break; | |
2996 | ||
2997 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2998 | ||
2999 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
3000 | if (key.offset + num_bytes < new->file_pos) | |
3001 | goto next; | |
3002 | ||
3003 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
3004 | if (!disk_bytenr) | |
3005 | goto next; | |
3006 | ||
3007 | extent_offset = btrfs_file_extent_offset(l, extent); | |
3008 | ||
3009 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
3010 | if (!old) | |
6f519564 | 3011 | goto out_free_path; |
38c227d8 LB |
3012 | |
3013 | offset = max(new->file_pos, key.offset); | |
3014 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
3015 | ||
3016 | old->bytenr = disk_bytenr; | |
3017 | old->extent_offset = extent_offset; | |
3018 | old->offset = offset - key.offset; | |
3019 | old->len = end - offset; | |
3020 | old->new = new; | |
3021 | old->count = 0; | |
3022 | list_add_tail(&old->list, &new->head); | |
3023 | next: | |
3024 | path->slots[0]++; | |
3025 | cond_resched(); | |
3026 | } | |
3027 | ||
3028 | btrfs_free_path(path); | |
0b246afa | 3029 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
3030 | |
3031 | return new; | |
3032 | ||
38c227d8 LB |
3033 | out_free_path: |
3034 | btrfs_free_path(path); | |
3035 | out_kfree: | |
6f519564 | 3036 | free_sa_defrag_extent(new); |
38c227d8 LB |
3037 | return NULL; |
3038 | } | |
3039 | ||
2ff7e61e | 3040 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
3041 | u64 start, u64 len) |
3042 | { | |
32da5386 | 3043 | struct btrfs_block_group *cache; |
e570fd27 | 3044 | |
0b246afa | 3045 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
3046 | ASSERT(cache); |
3047 | ||
3048 | spin_lock(&cache->lock); | |
3049 | cache->delalloc_bytes -= len; | |
3050 | spin_unlock(&cache->lock); | |
3051 | ||
3052 | btrfs_put_block_group(cache); | |
3053 | } | |
3054 | ||
d352ac68 CM |
3055 | /* as ordered data IO finishes, this gets called so we can finish |
3056 | * an ordered extent if the range of bytes in the file it covers are | |
3057 | * fully written. | |
3058 | */ | |
5fd02043 | 3059 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 3060 | { |
5fd02043 | 3061 | struct inode *inode = ordered_extent->inode; |
0b246afa | 3062 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 3063 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 3064 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 3065 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 3066 | struct extent_state *cached_state = NULL; |
38c227d8 | 3067 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 3068 | int compress_type = 0; |
77cef2ec JB |
3069 | int ret = 0; |
3070 | u64 logical_len = ordered_extent->len; | |
8d510121 | 3071 | bool freespace_inode; |
77cef2ec | 3072 | bool truncated = false; |
a7e3b975 FM |
3073 | bool range_locked = false; |
3074 | bool clear_new_delalloc_bytes = false; | |
49940bdd | 3075 | bool clear_reserved_extent = true; |
a7e3b975 FM |
3076 | |
3077 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
3078 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
3079 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
3080 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 3081 | |
8d510121 | 3082 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 3083 | |
5fd02043 JB |
3084 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
3085 | ret = -EIO; | |
3086 | goto out; | |
3087 | } | |
3088 | ||
7ab7956e NB |
3089 | btrfs_free_io_failure_record(BTRFS_I(inode), |
3090 | ordered_extent->file_offset, | |
3091 | ordered_extent->file_offset + | |
3092 | ordered_extent->len - 1); | |
f612496b | 3093 | |
77cef2ec JB |
3094 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
3095 | truncated = true; | |
3096 | logical_len = ordered_extent->truncated_len; | |
3097 | /* Truncated the entire extent, don't bother adding */ | |
3098 | if (!logical_len) | |
3099 | goto out; | |
3100 | } | |
3101 | ||
c2167754 | 3102 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 3103 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
3104 | |
3105 | /* | |
3106 | * For mwrite(mmap + memset to write) case, we still reserve | |
3107 | * space for NOCOW range. | |
3108 | * As NOCOW won't cause a new delayed ref, just free the space | |
3109 | */ | |
bc42bda2 | 3110 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
94ed938a | 3111 | ordered_extent->len); |
6c760c07 | 3112 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
8d510121 NB |
3113 | if (freespace_inode) |
3114 | trans = btrfs_join_transaction_spacecache(root); | |
6c760c07 JB |
3115 | else |
3116 | trans = btrfs_join_transaction(root); | |
3117 | if (IS_ERR(trans)) { | |
3118 | ret = PTR_ERR(trans); | |
3119 | trans = NULL; | |
3120 | goto out; | |
c2167754 | 3121 | } |
69fe2d75 | 3122 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
3123 | ret = btrfs_update_inode_fallback(trans, root, inode); |
3124 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 3125 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
3126 | goto out; |
3127 | } | |
e6dcd2dc | 3128 | |
a7e3b975 | 3129 | range_locked = true; |
2ac55d41 JB |
3130 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
3131 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 3132 | &cached_state); |
e6dcd2dc | 3133 | |
38c227d8 LB |
3134 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
3135 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 3136 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
3137 | if (ret) { |
3138 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 3139 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
3140 | /* the inode is shared */ |
3141 | new = record_old_file_extents(inode, ordered_extent); | |
3142 | ||
3143 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
3144 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ae0f1625 | 3145 | EXTENT_DEFRAG, 0, 0, &cached_state); |
38c227d8 LB |
3146 | } |
3147 | ||
8d510121 NB |
3148 | if (freespace_inode) |
3149 | trans = btrfs_join_transaction_spacecache(root); | |
0cb59c99 | 3150 | else |
7a7eaa40 | 3151 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3152 | if (IS_ERR(trans)) { |
3153 | ret = PTR_ERR(trans); | |
3154 | trans = NULL; | |
a7e3b975 | 3155 | goto out; |
79787eaa | 3156 | } |
a79b7d4b | 3157 | |
69fe2d75 | 3158 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 3159 | |
c8b97818 | 3160 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3161 | compress_type = ordered_extent->compress_type; |
d899e052 | 3162 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3163 | BUG_ON(compress_type); |
b430b775 JM |
3164 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
3165 | ordered_extent->len); | |
7a6d7067 | 3166 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
3167 | ordered_extent->file_offset, |
3168 | ordered_extent->file_offset + | |
77cef2ec | 3169 | logical_len); |
d899e052 | 3170 | } else { |
0b246afa | 3171 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
3172 | ret = insert_reserved_file_extent(trans, inode, |
3173 | ordered_extent->file_offset, | |
3174 | ordered_extent->start, | |
3175 | ordered_extent->disk_len, | |
77cef2ec | 3176 | logical_len, logical_len, |
261507a0 | 3177 | compress_type, 0, 0, |
d899e052 | 3178 | BTRFS_FILE_EXTENT_REG); |
49940bdd JB |
3179 | if (!ret) { |
3180 | clear_reserved_extent = false; | |
2ff7e61e | 3181 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3182 | ordered_extent->start, |
3183 | ordered_extent->disk_len); | |
49940bdd | 3184 | } |
d899e052 | 3185 | } |
5dc562c5 JB |
3186 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3187 | ordered_extent->file_offset, ordered_extent->len, | |
3188 | trans->transid); | |
79787eaa | 3189 | if (ret < 0) { |
66642832 | 3190 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3191 | goto out; |
79787eaa | 3192 | } |
2ac55d41 | 3193 | |
ac01f26a NB |
3194 | ret = add_pending_csums(trans, inode, &ordered_extent->list); |
3195 | if (ret) { | |
3196 | btrfs_abort_transaction(trans, ret); | |
3197 | goto out; | |
3198 | } | |
e6dcd2dc | 3199 | |
6c760c07 JB |
3200 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3201 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3202 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3203 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3204 | goto out; |
1ef30be1 JB |
3205 | } |
3206 | ret = 0; | |
c2167754 | 3207 | out: |
a7e3b975 FM |
3208 | if (range_locked || clear_new_delalloc_bytes) { |
3209 | unsigned int clear_bits = 0; | |
3210 | ||
3211 | if (range_locked) | |
3212 | clear_bits |= EXTENT_LOCKED; | |
3213 | if (clear_new_delalloc_bytes) | |
3214 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3215 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3216 | ordered_extent->file_offset, | |
3217 | ordered_extent->file_offset + | |
3218 | ordered_extent->len - 1, | |
3219 | clear_bits, | |
3220 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
ae0f1625 | 3221 | 0, &cached_state); |
a7e3b975 FM |
3222 | } |
3223 | ||
a698d075 | 3224 | if (trans) |
3a45bb20 | 3225 | btrfs_end_transaction(trans); |
0cb59c99 | 3226 | |
77cef2ec JB |
3227 | if (ret || truncated) { |
3228 | u64 start, end; | |
3229 | ||
3230 | if (truncated) | |
3231 | start = ordered_extent->file_offset + logical_len; | |
3232 | else | |
3233 | start = ordered_extent->file_offset; | |
3234 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
f08dc36f | 3235 | clear_extent_uptodate(io_tree, start, end, NULL); |
77cef2ec JB |
3236 | |
3237 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3238 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3239 | |
0bec9ef5 JB |
3240 | /* |
3241 | * If the ordered extent had an IOERR or something else went | |
3242 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3243 | * back to the allocator. We only free the extent in the |
3244 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3245 | * |
3246 | * If we made it past insert_reserved_file_extent before we | |
3247 | * errored out then we don't need to do this as the accounting | |
3248 | * has already been done. | |
0bec9ef5 | 3249 | */ |
77cef2ec | 3250 | if ((ret || !logical_len) && |
49940bdd | 3251 | clear_reserved_extent && |
77cef2ec | 3252 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
0bec9ef5 | 3253 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3254 | btrfs_free_reserved_extent(fs_info, |
3255 | ordered_extent->start, | |
e570fd27 | 3256 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3257 | } |
3258 | ||
3259 | ||
5fd02043 | 3260 | /* |
8bad3c02 LB |
3261 | * This needs to be done to make sure anybody waiting knows we are done |
3262 | * updating everything for this ordered extent. | |
5fd02043 JB |
3263 | */ |
3264 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3265 | ||
38c227d8 | 3266 | /* for snapshot-aware defrag */ |
6f519564 LB |
3267 | if (new) { |
3268 | if (ret) { | |
3269 | free_sa_defrag_extent(new); | |
0b246afa | 3270 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3271 | } else { |
3272 | relink_file_extents(new); | |
3273 | } | |
3274 | } | |
38c227d8 | 3275 | |
e6dcd2dc CM |
3276 | /* once for us */ |
3277 | btrfs_put_ordered_extent(ordered_extent); | |
3278 | /* once for the tree */ | |
3279 | btrfs_put_ordered_extent(ordered_extent); | |
3280 | ||
5fd02043 JB |
3281 | return ret; |
3282 | } | |
3283 | ||
3284 | static void finish_ordered_fn(struct btrfs_work *work) | |
3285 | { | |
3286 | struct btrfs_ordered_extent *ordered_extent; | |
3287 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3288 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3289 | } |
3290 | ||
c629732d NB |
3291 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
3292 | u64 end, int uptodate) | |
211f90e6 | 3293 | { |
5fd02043 | 3294 | struct inode *inode = page->mapping->host; |
0b246afa | 3295 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3296 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 | 3297 | struct btrfs_workqueue *wq; |
5fd02043 | 3298 | |
1abe9b8a | 3299 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3300 | ||
8b62b72b | 3301 | ClearPagePrivate2(page); |
5fd02043 JB |
3302 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3303 | end - start + 1, uptodate)) | |
c3988d63 | 3304 | return; |
5fd02043 | 3305 | |
a0cac0ec | 3306 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0b246afa | 3307 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 3308 | else |
0b246afa | 3309 | wq = fs_info->endio_write_workers; |
5fd02043 | 3310 | |
a0cac0ec | 3311 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
9e0af237 | 3312 | btrfs_queue_work(wq, &ordered_extent->work); |
211f90e6 CM |
3313 | } |
3314 | ||
dc380aea MX |
3315 | static int __readpage_endio_check(struct inode *inode, |
3316 | struct btrfs_io_bio *io_bio, | |
3317 | int icsum, struct page *page, | |
3318 | int pgoff, u64 start, size_t len) | |
3319 | { | |
d5178578 JT |
3320 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
3321 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
dc380aea | 3322 | char *kaddr; |
d5178578 JT |
3323 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
3324 | u8 *csum_expected; | |
3325 | u8 csum[BTRFS_CSUM_SIZE]; | |
dc380aea | 3326 | |
d5178578 | 3327 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
dc380aea MX |
3328 | |
3329 | kaddr = kmap_atomic(page); | |
d5178578 JT |
3330 | shash->tfm = fs_info->csum_shash; |
3331 | ||
3332 | crypto_shash_init(shash); | |
3333 | crypto_shash_update(shash, kaddr + pgoff, len); | |
3334 | crypto_shash_final(shash, csum); | |
3335 | ||
3336 | if (memcmp(csum, csum_expected, csum_size)) | |
dc380aea MX |
3337 | goto zeroit; |
3338 | ||
3339 | kunmap_atomic(kaddr); | |
3340 | return 0; | |
3341 | zeroit: | |
ea41d6b2 JT |
3342 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
3343 | io_bio->mirror_num); | |
dc380aea MX |
3344 | memset(kaddr + pgoff, 1, len); |
3345 | flush_dcache_page(page); | |
3346 | kunmap_atomic(kaddr); | |
dc380aea MX |
3347 | return -EIO; |
3348 | } | |
3349 | ||
d352ac68 CM |
3350 | /* |
3351 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3352 | * if there's a match, we allow the bio to finish. If not, the code in |
3353 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3354 | */ |
facc8a22 MX |
3355 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3356 | u64 phy_offset, struct page *page, | |
3357 | u64 start, u64 end, int mirror) | |
07157aac | 3358 | { |
4eee4fa4 | 3359 | size_t offset = start - page_offset(page); |
07157aac | 3360 | struct inode *inode = page->mapping->host; |
d1310b2e | 3361 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3362 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3363 | |
d20f7043 CM |
3364 | if (PageChecked(page)) { |
3365 | ClearPageChecked(page); | |
dc380aea | 3366 | return 0; |
d20f7043 | 3367 | } |
6cbff00f CH |
3368 | |
3369 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3370 | return 0; |
17d217fe YZ |
3371 | |
3372 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3373 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3374 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3375 | return 0; |
17d217fe | 3376 | } |
d20f7043 | 3377 | |
facc8a22 | 3378 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3379 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3380 | start, (size_t)(end - start + 1)); | |
07157aac | 3381 | } |
b888db2b | 3382 | |
c1c3fac2 NB |
3383 | /* |
3384 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
3385 | * | |
3386 | * @inode: The inode we want to perform iput on | |
3387 | * | |
3388 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3389 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3390 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3391 | * transaction commit time/superblock commit/cleaner kthread. | |
3392 | */ | |
24bbcf04 YZ |
3393 | void btrfs_add_delayed_iput(struct inode *inode) |
3394 | { | |
0b246afa | 3395 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3396 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3397 | |
3398 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3399 | return; | |
3400 | ||
034f784d | 3401 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 3402 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
3403 | ASSERT(list_empty(&binode->delayed_iput)); |
3404 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 3405 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
3406 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3407 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3408 | } |
3409 | ||
63611e73 JB |
3410 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
3411 | struct btrfs_inode *inode) | |
3412 | { | |
3413 | list_del_init(&inode->delayed_iput); | |
3414 | spin_unlock(&fs_info->delayed_iput_lock); | |
3415 | iput(&inode->vfs_inode); | |
3416 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) | |
3417 | wake_up(&fs_info->delayed_iputs_wait); | |
3418 | spin_lock(&fs_info->delayed_iput_lock); | |
3419 | } | |
3420 | ||
3421 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, | |
3422 | struct btrfs_inode *inode) | |
3423 | { | |
3424 | if (!list_empty(&inode->delayed_iput)) { | |
3425 | spin_lock(&fs_info->delayed_iput_lock); | |
3426 | if (!list_empty(&inode->delayed_iput)) | |
3427 | run_delayed_iput_locked(fs_info, inode); | |
3428 | spin_unlock(&fs_info->delayed_iput_lock); | |
3429 | } | |
3430 | } | |
3431 | ||
2ff7e61e | 3432 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3433 | { |
24bbcf04 | 3434 | |
24bbcf04 | 3435 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3436 | while (!list_empty(&fs_info->delayed_iputs)) { |
3437 | struct btrfs_inode *inode; | |
3438 | ||
3439 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3440 | struct btrfs_inode, delayed_iput); | |
63611e73 | 3441 | run_delayed_iput_locked(fs_info, inode); |
24bbcf04 | 3442 | } |
8089fe62 | 3443 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3444 | } |
3445 | ||
034f784d JB |
3446 | /** |
3447 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running | |
3448 | * @fs_info - the fs_info for this fs | |
3449 | * @return - EINTR if we were killed, 0 if nothing's pending | |
3450 | * | |
3451 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3452 | * set. Once they are all done running we will return, unless we are killed in | |
3453 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3454 | * that might get blocked on the iputs. | |
3455 | */ | |
3456 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3457 | { | |
3458 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3459 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3460 | if (ret) | |
3461 | return -EINTR; | |
3462 | return 0; | |
3463 | } | |
3464 | ||
7b128766 | 3465 | /* |
f7e9e8fc OS |
3466 | * This creates an orphan entry for the given inode in case something goes wrong |
3467 | * in the middle of an unlink. | |
7b128766 | 3468 | */ |
73f2e545 | 3469 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3470 | struct btrfs_inode *inode) |
7b128766 | 3471 | { |
d68fc57b | 3472 | int ret; |
7b128766 | 3473 | |
27919067 OS |
3474 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3475 | if (ret && ret != -EEXIST) { | |
3476 | btrfs_abort_transaction(trans, ret); | |
3477 | return ret; | |
d68fc57b YZ |
3478 | } |
3479 | ||
d68fc57b | 3480 | return 0; |
7b128766 JB |
3481 | } |
3482 | ||
3483 | /* | |
f7e9e8fc OS |
3484 | * We have done the delete so we can go ahead and remove the orphan item for |
3485 | * this particular inode. | |
7b128766 | 3486 | */ |
48a3b636 | 3487 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3488 | struct btrfs_inode *inode) |
7b128766 | 3489 | { |
27919067 | 3490 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3491 | } |
3492 | ||
3493 | /* | |
3494 | * this cleans up any orphans that may be left on the list from the last use | |
3495 | * of this root. | |
3496 | */ | |
66b4ffd1 | 3497 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3498 | { |
0b246afa | 3499 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3500 | struct btrfs_path *path; |
3501 | struct extent_buffer *leaf; | |
7b128766 JB |
3502 | struct btrfs_key key, found_key; |
3503 | struct btrfs_trans_handle *trans; | |
3504 | struct inode *inode; | |
8f6d7f4f | 3505 | u64 last_objectid = 0; |
f7e9e8fc | 3506 | int ret = 0, nr_unlink = 0; |
7b128766 | 3507 | |
d68fc57b | 3508 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3509 | return 0; |
c71bf099 YZ |
3510 | |
3511 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3512 | if (!path) { |
3513 | ret = -ENOMEM; | |
3514 | goto out; | |
3515 | } | |
e4058b54 | 3516 | path->reada = READA_BACK; |
7b128766 JB |
3517 | |
3518 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3519 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3520 | key.offset = (u64)-1; |
3521 | ||
7b128766 JB |
3522 | while (1) { |
3523 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3524 | if (ret < 0) |
3525 | goto out; | |
7b128766 JB |
3526 | |
3527 | /* | |
3528 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3529 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3530 | * find the key and see if we have stuff that matches |
3531 | */ | |
3532 | if (ret > 0) { | |
66b4ffd1 | 3533 | ret = 0; |
7b128766 JB |
3534 | if (path->slots[0] == 0) |
3535 | break; | |
3536 | path->slots[0]--; | |
3537 | } | |
3538 | ||
3539 | /* pull out the item */ | |
3540 | leaf = path->nodes[0]; | |
7b128766 JB |
3541 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3542 | ||
3543 | /* make sure the item matches what we want */ | |
3544 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3545 | break; | |
962a298f | 3546 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3547 | break; |
3548 | ||
3549 | /* release the path since we're done with it */ | |
b3b4aa74 | 3550 | btrfs_release_path(path); |
7b128766 JB |
3551 | |
3552 | /* | |
3553 | * this is where we are basically btrfs_lookup, without the | |
3554 | * crossing root thing. we store the inode number in the | |
3555 | * offset of the orphan item. | |
3556 | */ | |
8f6d7f4f JB |
3557 | |
3558 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3559 | btrfs_err(fs_info, |
3560 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3561 | ret = -EINVAL; |
3562 | goto out; | |
3563 | } | |
3564 | ||
3565 | last_objectid = found_key.offset; | |
3566 | ||
5d4f98a2 YZ |
3567 | found_key.objectid = found_key.offset; |
3568 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3569 | found_key.offset = 0; | |
4c66e0d4 | 3570 | inode = btrfs_iget(fs_info->sb, &found_key, root); |
8c6ffba0 | 3571 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3572 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3573 | goto out; |
7b128766 | 3574 | |
0b246afa | 3575 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3576 | struct btrfs_root *dead_root; |
3577 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3578 | int is_dead_root = 0; | |
3579 | ||
3580 | /* | |
3581 | * this is an orphan in the tree root. Currently these | |
3582 | * could come from 2 sources: | |
3583 | * a) a snapshot deletion in progress | |
3584 | * b) a free space cache inode | |
3585 | * We need to distinguish those two, as the snapshot | |
3586 | * orphan must not get deleted. | |
3587 | * find_dead_roots already ran before us, so if this | |
3588 | * is a snapshot deletion, we should find the root | |
3589 | * in the dead_roots list | |
3590 | */ | |
3591 | spin_lock(&fs_info->trans_lock); | |
3592 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3593 | root_list) { | |
3594 | if (dead_root->root_key.objectid == | |
3595 | found_key.objectid) { | |
3596 | is_dead_root = 1; | |
3597 | break; | |
3598 | } | |
3599 | } | |
3600 | spin_unlock(&fs_info->trans_lock); | |
3601 | if (is_dead_root) { | |
3602 | /* prevent this orphan from being found again */ | |
3603 | key.offset = found_key.objectid - 1; | |
3604 | continue; | |
3605 | } | |
f7e9e8fc | 3606 | |
f8e9e0b0 | 3607 | } |
f7e9e8fc | 3608 | |
7b128766 | 3609 | /* |
f7e9e8fc OS |
3610 | * If we have an inode with links, there are a couple of |
3611 | * possibilities. Old kernels (before v3.12) used to create an | |
3612 | * orphan item for truncate indicating that there were possibly | |
3613 | * extent items past i_size that needed to be deleted. In v3.12, | |
3614 | * truncate was changed to update i_size in sync with the extent | |
3615 | * items, but the (useless) orphan item was still created. Since | |
3616 | * v4.18, we don't create the orphan item for truncate at all. | |
3617 | * | |
3618 | * So, this item could mean that we need to do a truncate, but | |
3619 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3620 | * and was not cleanly unmounted. The odds of that are quite | |
3621 | * slim, and it's a pain to do the truncate now, so just delete | |
3622 | * the orphan item. | |
3623 | * | |
3624 | * It's also possible that this orphan item was supposed to be | |
3625 | * deleted but wasn't. The inode number may have been reused, | |
3626 | * but either way, we can delete the orphan item. | |
7b128766 | 3627 | */ |
f7e9e8fc OS |
3628 | if (ret == -ENOENT || inode->i_nlink) { |
3629 | if (!ret) | |
3630 | iput(inode); | |
a8c9e576 | 3631 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3632 | if (IS_ERR(trans)) { |
3633 | ret = PTR_ERR(trans); | |
3634 | goto out; | |
3635 | } | |
0b246afa JM |
3636 | btrfs_debug(fs_info, "auto deleting %Lu", |
3637 | found_key.objectid); | |
a8c9e576 JB |
3638 | ret = btrfs_del_orphan_item(trans, root, |
3639 | found_key.objectid); | |
3a45bb20 | 3640 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3641 | if (ret) |
3642 | goto out; | |
7b128766 JB |
3643 | continue; |
3644 | } | |
3645 | ||
f7e9e8fc | 3646 | nr_unlink++; |
7b128766 JB |
3647 | |
3648 | /* this will do delete_inode and everything for us */ | |
3649 | iput(inode); | |
3650 | } | |
3254c876 MX |
3651 | /* release the path since we're done with it */ |
3652 | btrfs_release_path(path); | |
3653 | ||
d68fc57b YZ |
3654 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3655 | ||
a575ceeb | 3656 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3657 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3658 | if (!IS_ERR(trans)) |
3a45bb20 | 3659 | btrfs_end_transaction(trans); |
d68fc57b | 3660 | } |
7b128766 JB |
3661 | |
3662 | if (nr_unlink) | |
0b246afa | 3663 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3664 | |
3665 | out: | |
3666 | if (ret) | |
0b246afa | 3667 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3668 | btrfs_free_path(path); |
3669 | return ret; | |
7b128766 JB |
3670 | } |
3671 | ||
46a53cca CM |
3672 | /* |
3673 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3674 | * don't find any xattrs, we know there can't be any acls. | |
3675 | * | |
3676 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3677 | */ | |
3678 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3679 | int slot, u64 objectid, |
3680 | int *first_xattr_slot) | |
46a53cca CM |
3681 | { |
3682 | u32 nritems = btrfs_header_nritems(leaf); | |
3683 | struct btrfs_key found_key; | |
f23b5a59 JB |
3684 | static u64 xattr_access = 0; |
3685 | static u64 xattr_default = 0; | |
46a53cca CM |
3686 | int scanned = 0; |
3687 | ||
f23b5a59 | 3688 | if (!xattr_access) { |
97d79299 AG |
3689 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3690 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3691 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3692 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3693 | } |
3694 | ||
46a53cca | 3695 | slot++; |
63541927 | 3696 | *first_xattr_slot = -1; |
46a53cca CM |
3697 | while (slot < nritems) { |
3698 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3699 | ||
3700 | /* we found a different objectid, there must not be acls */ | |
3701 | if (found_key.objectid != objectid) | |
3702 | return 0; | |
3703 | ||
3704 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3705 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3706 | if (*first_xattr_slot == -1) |
3707 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3708 | if (found_key.offset == xattr_access || |
3709 | found_key.offset == xattr_default) | |
3710 | return 1; | |
3711 | } | |
46a53cca CM |
3712 | |
3713 | /* | |
3714 | * we found a key greater than an xattr key, there can't | |
3715 | * be any acls later on | |
3716 | */ | |
3717 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3718 | return 0; | |
3719 | ||
3720 | slot++; | |
3721 | scanned++; | |
3722 | ||
3723 | /* | |
3724 | * it goes inode, inode backrefs, xattrs, extents, | |
3725 | * so if there are a ton of hard links to an inode there can | |
3726 | * be a lot of backrefs. Don't waste time searching too hard, | |
3727 | * this is just an optimization | |
3728 | */ | |
3729 | if (scanned >= 8) | |
3730 | break; | |
3731 | } | |
3732 | /* we hit the end of the leaf before we found an xattr or | |
3733 | * something larger than an xattr. We have to assume the inode | |
3734 | * has acls | |
3735 | */ | |
63541927 FDBM |
3736 | if (*first_xattr_slot == -1) |
3737 | *first_xattr_slot = slot; | |
46a53cca CM |
3738 | return 1; |
3739 | } | |
3740 | ||
d352ac68 CM |
3741 | /* |
3742 | * read an inode from the btree into the in-memory inode | |
3743 | */ | |
4222ea71 FM |
3744 | static int btrfs_read_locked_inode(struct inode *inode, |
3745 | struct btrfs_path *in_path) | |
39279cc3 | 3746 | { |
0b246afa | 3747 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3748 | struct btrfs_path *path = in_path; |
5f39d397 | 3749 | struct extent_buffer *leaf; |
39279cc3 CM |
3750 | struct btrfs_inode_item *inode_item; |
3751 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3752 | struct btrfs_key location; | |
67de1176 | 3753 | unsigned long ptr; |
46a53cca | 3754 | int maybe_acls; |
618e21d5 | 3755 | u32 rdev; |
39279cc3 | 3756 | int ret; |
2f7e33d4 | 3757 | bool filled = false; |
63541927 | 3758 | int first_xattr_slot; |
2f7e33d4 MX |
3759 | |
3760 | ret = btrfs_fill_inode(inode, &rdev); | |
3761 | if (!ret) | |
3762 | filled = true; | |
39279cc3 | 3763 | |
4222ea71 FM |
3764 | if (!path) { |
3765 | path = btrfs_alloc_path(); | |
3766 | if (!path) | |
3767 | return -ENOMEM; | |
3768 | } | |
1748f843 | 3769 | |
39279cc3 | 3770 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3771 | |
39279cc3 | 3772 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3773 | if (ret) { |
4222ea71 FM |
3774 | if (path != in_path) |
3775 | btrfs_free_path(path); | |
f5b3a417 | 3776 | return ret; |
67710892 | 3777 | } |
39279cc3 | 3778 | |
5f39d397 | 3779 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3780 | |
3781 | if (filled) | |
67de1176 | 3782 | goto cache_index; |
2f7e33d4 | 3783 | |
5f39d397 CM |
3784 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3785 | struct btrfs_inode_item); | |
5f39d397 | 3786 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3787 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3788 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3789 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3790 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3791 | |
a937b979 DS |
3792 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3793 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3794 | |
a937b979 DS |
3795 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3796 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3797 | |
a937b979 DS |
3798 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3799 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3800 | |
9cc97d64 | 3801 | BTRFS_I(inode)->i_otime.tv_sec = |
3802 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3803 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3804 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3805 | |
a76a3cd4 | 3806 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3807 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3808 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3809 | ||
c7f88c4e JL |
3810 | inode_set_iversion_queried(inode, |
3811 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3812 | inode->i_generation = BTRFS_I(inode)->generation; |
3813 | inode->i_rdev = 0; | |
3814 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3815 | ||
3816 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3817 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3818 | ||
3819 | cache_index: | |
5dc562c5 JB |
3820 | /* |
3821 | * If we were modified in the current generation and evicted from memory | |
3822 | * and then re-read we need to do a full sync since we don't have any | |
3823 | * idea about which extents were modified before we were evicted from | |
3824 | * cache. | |
6e17d30b YD |
3825 | * |
3826 | * This is required for both inode re-read from disk and delayed inode | |
3827 | * in delayed_nodes_tree. | |
5dc562c5 | 3828 | */ |
0b246afa | 3829 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3830 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3831 | &BTRFS_I(inode)->runtime_flags); | |
3832 | ||
bde6c242 FM |
3833 | /* |
3834 | * We don't persist the id of the transaction where an unlink operation | |
3835 | * against the inode was last made. So here we assume the inode might | |
3836 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3837 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3838 | * between the inode and its parent if the inode is fsync'ed and the log | |
3839 | * replayed. For example, in the scenario: | |
3840 | * | |
3841 | * touch mydir/foo | |
3842 | * ln mydir/foo mydir/bar | |
3843 | * sync | |
3844 | * unlink mydir/bar | |
3845 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3846 | * xfs_io -c fsync mydir/foo | |
3847 | * <power failure> | |
3848 | * mount fs, triggers fsync log replay | |
3849 | * | |
3850 | * We must make sure that when we fsync our inode foo we also log its | |
3851 | * parent inode, otherwise after log replay the parent still has the | |
3852 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3853 | * and doesn't have an inode ref with the name "bar" anymore. | |
3854 | * | |
3855 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3856 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3857 | * transaction commits on fsync if our inode is a directory, or if our |
3858 | * inode is not a directory, logging its parent unnecessarily. | |
3859 | */ | |
3860 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3861 | ||
67de1176 MX |
3862 | path->slots[0]++; |
3863 | if (inode->i_nlink != 1 || | |
3864 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3865 | goto cache_acl; | |
3866 | ||
3867 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3868 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3869 | goto cache_acl; |
3870 | ||
3871 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3872 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3873 | struct btrfs_inode_ref *ref; | |
3874 | ||
3875 | ref = (struct btrfs_inode_ref *)ptr; | |
3876 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3877 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3878 | struct btrfs_inode_extref *extref; | |
3879 | ||
3880 | extref = (struct btrfs_inode_extref *)ptr; | |
3881 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3882 | extref); | |
3883 | } | |
2f7e33d4 | 3884 | cache_acl: |
46a53cca CM |
3885 | /* |
3886 | * try to precache a NULL acl entry for files that don't have | |
3887 | * any xattrs or acls | |
3888 | */ | |
33345d01 | 3889 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3890 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3891 | if (first_xattr_slot != -1) { |
3892 | path->slots[0] = first_xattr_slot; | |
3893 | ret = btrfs_load_inode_props(inode, path); | |
3894 | if (ret) | |
0b246afa | 3895 | btrfs_err(fs_info, |
351fd353 | 3896 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3897 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3898 | root->root_key.objectid, ret); |
3899 | } | |
4222ea71 FM |
3900 | if (path != in_path) |
3901 | btrfs_free_path(path); | |
63541927 | 3902 | |
72c04902 AV |
3903 | if (!maybe_acls) |
3904 | cache_no_acl(inode); | |
46a53cca | 3905 | |
39279cc3 | 3906 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3907 | case S_IFREG: |
3908 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3909 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3910 | inode->i_fop = &btrfs_file_operations; |
3911 | inode->i_op = &btrfs_file_inode_operations; | |
3912 | break; | |
3913 | case S_IFDIR: | |
3914 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3915 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3916 | break; |
3917 | case S_IFLNK: | |
3918 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3919 | inode_nohighmem(inode); |
4779cc04 | 3920 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3921 | break; |
618e21d5 | 3922 | default: |
0279b4cd | 3923 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3924 | init_special_inode(inode, inode->i_mode, rdev); |
3925 | break; | |
39279cc3 | 3926 | } |
6cbff00f | 3927 | |
7b6a221e | 3928 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3929 | return 0; |
39279cc3 CM |
3930 | } |
3931 | ||
d352ac68 CM |
3932 | /* |
3933 | * given a leaf and an inode, copy the inode fields into the leaf | |
3934 | */ | |
e02119d5 CM |
3935 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3936 | struct extent_buffer *leaf, | |
5f39d397 | 3937 | struct btrfs_inode_item *item, |
39279cc3 CM |
3938 | struct inode *inode) |
3939 | { | |
51fab693 LB |
3940 | struct btrfs_map_token token; |
3941 | ||
c82f823c | 3942 | btrfs_init_map_token(&token, leaf); |
5f39d397 | 3943 | |
51fab693 LB |
3944 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3945 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3946 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3947 | &token); | |
3948 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3949 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3950 | |
a937b979 | 3951 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3952 | inode->i_atime.tv_sec, &token); |
a937b979 | 3953 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3954 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3955 | |
a937b979 | 3956 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3957 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3958 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3959 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3960 | |
a937b979 | 3961 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3962 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3963 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3964 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3965 | |
9cc97d64 | 3966 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3967 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3968 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3969 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3970 | ||
51fab693 LB |
3971 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3972 | &token); | |
3973 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3974 | &token); | |
c7f88c4e JL |
3975 | btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode), |
3976 | &token); | |
51fab693 LB |
3977 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
3978 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3979 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3980 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3981 | } |
3982 | ||
d352ac68 CM |
3983 | /* |
3984 | * copy everything in the in-memory inode into the btree. | |
3985 | */ | |
2115133f | 3986 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3987 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3988 | { |
3989 | struct btrfs_inode_item *inode_item; | |
3990 | struct btrfs_path *path; | |
5f39d397 | 3991 | struct extent_buffer *leaf; |
39279cc3 CM |
3992 | int ret; |
3993 | ||
3994 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3995 | if (!path) |
3996 | return -ENOMEM; | |
3997 | ||
b9473439 | 3998 | path->leave_spinning = 1; |
16cdcec7 MX |
3999 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
4000 | 1); | |
39279cc3 CM |
4001 | if (ret) { |
4002 | if (ret > 0) | |
4003 | ret = -ENOENT; | |
4004 | goto failed; | |
4005 | } | |
4006 | ||
5f39d397 CM |
4007 | leaf = path->nodes[0]; |
4008 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 4009 | struct btrfs_inode_item); |
39279cc3 | 4010 | |
e02119d5 | 4011 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 4012 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 4013 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
4014 | ret = 0; |
4015 | failed: | |
39279cc3 CM |
4016 | btrfs_free_path(path); |
4017 | return ret; | |
4018 | } | |
4019 | ||
2115133f CM |
4020 | /* |
4021 | * copy everything in the in-memory inode into the btree. | |
4022 | */ | |
4023 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
4024 | struct btrfs_root *root, struct inode *inode) | |
4025 | { | |
0b246afa | 4026 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
4027 | int ret; |
4028 | ||
4029 | /* | |
4030 | * If the inode is a free space inode, we can deadlock during commit | |
4031 | * if we put it into the delayed code. | |
4032 | * | |
4033 | * The data relocation inode should also be directly updated | |
4034 | * without delay | |
4035 | */ | |
70ddc553 | 4036 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 4037 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 4038 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4039 | btrfs_update_root_times(trans, root); |
4040 | ||
2115133f CM |
4041 | ret = btrfs_delayed_update_inode(trans, root, inode); |
4042 | if (!ret) | |
4043 | btrfs_set_inode_last_trans(trans, inode); | |
4044 | return ret; | |
4045 | } | |
4046 | ||
4047 | return btrfs_update_inode_item(trans, root, inode); | |
4048 | } | |
4049 | ||
be6aef60 JB |
4050 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4051 | struct btrfs_root *root, | |
4052 | struct inode *inode) | |
2115133f CM |
4053 | { |
4054 | int ret; | |
4055 | ||
4056 | ret = btrfs_update_inode(trans, root, inode); | |
4057 | if (ret == -ENOSPC) | |
4058 | return btrfs_update_inode_item(trans, root, inode); | |
4059 | return ret; | |
4060 | } | |
4061 | ||
d352ac68 CM |
4062 | /* |
4063 | * unlink helper that gets used here in inode.c and in the tree logging | |
4064 | * recovery code. It remove a link in a directory with a given name, and | |
4065 | * also drops the back refs in the inode to the directory | |
4066 | */ | |
92986796 AV |
4067 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4068 | struct btrfs_root *root, | |
4ec5934e NB |
4069 | struct btrfs_inode *dir, |
4070 | struct btrfs_inode *inode, | |
92986796 | 4071 | const char *name, int name_len) |
39279cc3 | 4072 | { |
0b246afa | 4073 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4074 | struct btrfs_path *path; |
39279cc3 | 4075 | int ret = 0; |
39279cc3 | 4076 | struct btrfs_dir_item *di; |
aec7477b | 4077 | u64 index; |
33345d01 LZ |
4078 | u64 ino = btrfs_ino(inode); |
4079 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4080 | |
4081 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4082 | if (!path) { |
4083 | ret = -ENOMEM; | |
554233a6 | 4084 | goto out; |
54aa1f4d CM |
4085 | } |
4086 | ||
b9473439 | 4087 | path->leave_spinning = 1; |
33345d01 | 4088 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 4089 | name, name_len, -1); |
3cf5068f LB |
4090 | if (IS_ERR_OR_NULL(di)) { |
4091 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
4092 | goto err; |
4093 | } | |
39279cc3 | 4094 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4095 | if (ret) |
4096 | goto err; | |
b3b4aa74 | 4097 | btrfs_release_path(path); |
39279cc3 | 4098 | |
67de1176 MX |
4099 | /* |
4100 | * If we don't have dir index, we have to get it by looking up | |
4101 | * the inode ref, since we get the inode ref, remove it directly, | |
4102 | * it is unnecessary to do delayed deletion. | |
4103 | * | |
4104 | * But if we have dir index, needn't search inode ref to get it. | |
4105 | * Since the inode ref is close to the inode item, it is better | |
4106 | * that we delay to delete it, and just do this deletion when | |
4107 | * we update the inode item. | |
4108 | */ | |
4ec5934e | 4109 | if (inode->dir_index) { |
67de1176 MX |
4110 | ret = btrfs_delayed_delete_inode_ref(inode); |
4111 | if (!ret) { | |
4ec5934e | 4112 | index = inode->dir_index; |
67de1176 MX |
4113 | goto skip_backref; |
4114 | } | |
4115 | } | |
4116 | ||
33345d01 LZ |
4117 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4118 | dir_ino, &index); | |
aec7477b | 4119 | if (ret) { |
0b246afa | 4120 | btrfs_info(fs_info, |
c2cf52eb | 4121 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4122 | name_len, name, ino, dir_ino); |
66642832 | 4123 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4124 | goto err; |
4125 | } | |
67de1176 | 4126 | skip_backref: |
9add2945 | 4127 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 4128 | if (ret) { |
66642832 | 4129 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4130 | goto err; |
79787eaa | 4131 | } |
39279cc3 | 4132 | |
4ec5934e NB |
4133 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4134 | dir_ino); | |
79787eaa | 4135 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4136 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4137 | goto err; |
4138 | } | |
e02119d5 | 4139 | |
4ec5934e NB |
4140 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4141 | index); | |
6418c961 CM |
4142 | if (ret == -ENOENT) |
4143 | ret = 0; | |
d4e3991b | 4144 | else if (ret) |
66642832 | 4145 | btrfs_abort_transaction(trans, ret); |
63611e73 JB |
4146 | |
4147 | /* | |
4148 | * If we have a pending delayed iput we could end up with the final iput | |
4149 | * being run in btrfs-cleaner context. If we have enough of these built | |
4150 | * up we can end up burning a lot of time in btrfs-cleaner without any | |
4151 | * way to throttle the unlinks. Since we're currently holding a ref on | |
4152 | * the inode we can run the delayed iput here without any issues as the | |
4153 | * final iput won't be done until after we drop the ref we're currently | |
4154 | * holding. | |
4155 | */ | |
4156 | btrfs_run_delayed_iput(fs_info, inode); | |
39279cc3 CM |
4157 | err: |
4158 | btrfs_free_path(path); | |
e02119d5 CM |
4159 | if (ret) |
4160 | goto out; | |
4161 | ||
6ef06d27 | 4162 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4163 | inode_inc_iversion(&inode->vfs_inode); |
4164 | inode_inc_iversion(&dir->vfs_inode); | |
4165 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4166 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4167 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4168 | out: |
39279cc3 CM |
4169 | return ret; |
4170 | } | |
4171 | ||
92986796 AV |
4172 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4173 | struct btrfs_root *root, | |
4ec5934e | 4174 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4175 | const char *name, int name_len) |
4176 | { | |
4177 | int ret; | |
4178 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4179 | if (!ret) { | |
4ec5934e NB |
4180 | drop_nlink(&inode->vfs_inode); |
4181 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4182 | } |
4183 | return ret; | |
4184 | } | |
39279cc3 | 4185 | |
a22285a6 YZ |
4186 | /* |
4187 | * helper to start transaction for unlink and rmdir. | |
4188 | * | |
d52be818 JB |
4189 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4190 | * if we cannot make our reservations the normal way try and see if there is | |
4191 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4192 | * allow the unlink to occur. | |
a22285a6 | 4193 | */ |
d52be818 | 4194 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4195 | { |
a22285a6 | 4196 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4197 | |
e70bea5f JB |
4198 | /* |
4199 | * 1 for the possible orphan item | |
4200 | * 1 for the dir item | |
4201 | * 1 for the dir index | |
4202 | * 1 for the inode ref | |
e70bea5f JB |
4203 | * 1 for the inode |
4204 | */ | |
8eab77ff | 4205 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4206 | } |
4207 | ||
4208 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4209 | { | |
4210 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4211 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4212 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4213 | int ret; |
a22285a6 | 4214 | |
d52be818 | 4215 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4216 | if (IS_ERR(trans)) |
4217 | return PTR_ERR(trans); | |
5f39d397 | 4218 | |
4ec5934e NB |
4219 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4220 | 0); | |
12fcfd22 | 4221 | |
4ec5934e NB |
4222 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4223 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4224 | dentry->d_name.len); | |
b532402e TI |
4225 | if (ret) |
4226 | goto out; | |
7b128766 | 4227 | |
a22285a6 | 4228 | if (inode->i_nlink == 0) { |
73f2e545 | 4229 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4230 | if (ret) |
4231 | goto out; | |
a22285a6 | 4232 | } |
7b128766 | 4233 | |
b532402e | 4234 | out: |
3a45bb20 | 4235 | btrfs_end_transaction(trans); |
2ff7e61e | 4236 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4237 | return ret; |
4238 | } | |
4239 | ||
f60a2364 | 4240 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
401b3b19 LF |
4241 | struct inode *dir, u64 objectid, |
4242 | const char *name, int name_len) | |
4df27c4d | 4243 | { |
401b3b19 | 4244 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
4245 | struct btrfs_path *path; |
4246 | struct extent_buffer *leaf; | |
4247 | struct btrfs_dir_item *di; | |
4248 | struct btrfs_key key; | |
4249 | u64 index; | |
4250 | int ret; | |
4a0cc7ca | 4251 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4252 | |
4253 | path = btrfs_alloc_path(); | |
4254 | if (!path) | |
4255 | return -ENOMEM; | |
4256 | ||
33345d01 | 4257 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4258 | name, name_len, -1); |
79787eaa | 4259 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4260 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4261 | goto out; |
4262 | } | |
4df27c4d YZ |
4263 | |
4264 | leaf = path->nodes[0]; | |
4265 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4266 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4267 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4268 | if (ret) { |
66642832 | 4269 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4270 | goto out; |
4271 | } | |
b3b4aa74 | 4272 | btrfs_release_path(path); |
4df27c4d | 4273 | |
3ee1c553 LF |
4274 | ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid, |
4275 | dir_ino, &index, name, name_len); | |
4df27c4d | 4276 | if (ret < 0) { |
79787eaa | 4277 | if (ret != -ENOENT) { |
66642832 | 4278 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4279 | goto out; |
4280 | } | |
33345d01 | 4281 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4282 | name, name_len); |
79787eaa JM |
4283 | if (IS_ERR_OR_NULL(di)) { |
4284 | if (!di) | |
4285 | ret = -ENOENT; | |
4286 | else | |
4287 | ret = PTR_ERR(di); | |
66642832 | 4288 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4289 | goto out; |
4290 | } | |
4df27c4d YZ |
4291 | |
4292 | leaf = path->nodes[0]; | |
4293 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d YZ |
4294 | index = key.offset; |
4295 | } | |
945d8962 | 4296 | btrfs_release_path(path); |
4df27c4d | 4297 | |
9add2945 | 4298 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 4299 | if (ret) { |
66642832 | 4300 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4301 | goto out; |
4302 | } | |
4df27c4d | 4303 | |
6ef06d27 | 4304 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4305 | inode_inc_iversion(dir); |
c2050a45 | 4306 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4307 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4308 | if (ret) |
66642832 | 4309 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4310 | out: |
71d7aed0 | 4311 | btrfs_free_path(path); |
79787eaa | 4312 | return ret; |
4df27c4d YZ |
4313 | } |
4314 | ||
ec42f167 MT |
4315 | /* |
4316 | * Helper to check if the subvolume references other subvolumes or if it's | |
4317 | * default. | |
4318 | */ | |
f60a2364 | 4319 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4320 | { |
4321 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4322 | struct btrfs_path *path; | |
4323 | struct btrfs_dir_item *di; | |
4324 | struct btrfs_key key; | |
4325 | u64 dir_id; | |
4326 | int ret; | |
4327 | ||
4328 | path = btrfs_alloc_path(); | |
4329 | if (!path) | |
4330 | return -ENOMEM; | |
4331 | ||
4332 | /* Make sure this root isn't set as the default subvol */ | |
4333 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4334 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
4335 | dir_id, "default", 7, 0); | |
4336 | if (di && !IS_ERR(di)) { | |
4337 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4338 | if (key.objectid == root->root_key.objectid) { | |
4339 | ret = -EPERM; | |
4340 | btrfs_err(fs_info, | |
4341 | "deleting default subvolume %llu is not allowed", | |
4342 | key.objectid); | |
4343 | goto out; | |
4344 | } | |
4345 | btrfs_release_path(path); | |
4346 | } | |
4347 | ||
4348 | key.objectid = root->root_key.objectid; | |
4349 | key.type = BTRFS_ROOT_REF_KEY; | |
4350 | key.offset = (u64)-1; | |
4351 | ||
4352 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4353 | if (ret < 0) | |
4354 | goto out; | |
4355 | BUG_ON(ret == 0); | |
4356 | ||
4357 | ret = 0; | |
4358 | if (path->slots[0] > 0) { | |
4359 | path->slots[0]--; | |
4360 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4361 | if (key.objectid == root->root_key.objectid && | |
4362 | key.type == BTRFS_ROOT_REF_KEY) | |
4363 | ret = -ENOTEMPTY; | |
4364 | } | |
4365 | out: | |
4366 | btrfs_free_path(path); | |
4367 | return ret; | |
4368 | } | |
4369 | ||
20a68004 NB |
4370 | /* Delete all dentries for inodes belonging to the root */ |
4371 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4372 | { | |
4373 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4374 | struct rb_node *node; | |
4375 | struct rb_node *prev; | |
4376 | struct btrfs_inode *entry; | |
4377 | struct inode *inode; | |
4378 | u64 objectid = 0; | |
4379 | ||
4380 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
4381 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
4382 | ||
4383 | spin_lock(&root->inode_lock); | |
4384 | again: | |
4385 | node = root->inode_tree.rb_node; | |
4386 | prev = NULL; | |
4387 | while (node) { | |
4388 | prev = node; | |
4389 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4390 | ||
37508515 | 4391 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4392 | node = node->rb_left; |
37508515 | 4393 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4394 | node = node->rb_right; |
4395 | else | |
4396 | break; | |
4397 | } | |
4398 | if (!node) { | |
4399 | while (prev) { | |
4400 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4401 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4402 | node = prev; |
4403 | break; | |
4404 | } | |
4405 | prev = rb_next(prev); | |
4406 | } | |
4407 | } | |
4408 | while (node) { | |
4409 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4410 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4411 | inode = igrab(&entry->vfs_inode); |
4412 | if (inode) { | |
4413 | spin_unlock(&root->inode_lock); | |
4414 | if (atomic_read(&inode->i_count) > 1) | |
4415 | d_prune_aliases(inode); | |
4416 | /* | |
4417 | * btrfs_drop_inode will have it removed from the inode | |
4418 | * cache when its usage count hits zero. | |
4419 | */ | |
4420 | iput(inode); | |
4421 | cond_resched(); | |
4422 | spin_lock(&root->inode_lock); | |
4423 | goto again; | |
4424 | } | |
4425 | ||
4426 | if (cond_resched_lock(&root->inode_lock)) | |
4427 | goto again; | |
4428 | ||
4429 | node = rb_next(node); | |
4430 | } | |
4431 | spin_unlock(&root->inode_lock); | |
4432 | } | |
4433 | ||
f60a2364 MT |
4434 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
4435 | { | |
4436 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
4437 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4438 | struct inode *inode = d_inode(dentry); | |
4439 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4440 | struct btrfs_trans_handle *trans; | |
4441 | struct btrfs_block_rsv block_rsv; | |
4442 | u64 root_flags; | |
f60a2364 MT |
4443 | int ret; |
4444 | int err; | |
4445 | ||
4446 | /* | |
4447 | * Don't allow to delete a subvolume with send in progress. This is | |
4448 | * inside the inode lock so the error handling that has to drop the bit | |
4449 | * again is not run concurrently. | |
4450 | */ | |
4451 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4452 | if (dest->send_in_progress) { |
f60a2364 MT |
4453 | spin_unlock(&dest->root_item_lock); |
4454 | btrfs_warn(fs_info, | |
4455 | "attempt to delete subvolume %llu during send", | |
4456 | dest->root_key.objectid); | |
4457 | return -EPERM; | |
4458 | } | |
a7176f74 LF |
4459 | root_flags = btrfs_root_flags(&dest->root_item); |
4460 | btrfs_set_root_flags(&dest->root_item, | |
4461 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4462 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4463 | |
4464 | down_write(&fs_info->subvol_sem); | |
4465 | ||
4466 | err = may_destroy_subvol(dest); | |
4467 | if (err) | |
4468 | goto out_up_write; | |
4469 | ||
4470 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4471 | /* | |
4472 | * One for dir inode, | |
4473 | * two for dir entries, | |
4474 | * two for root ref/backref. | |
4475 | */ | |
c4c129db | 4476 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
f60a2364 MT |
4477 | if (err) |
4478 | goto out_up_write; | |
4479 | ||
4480 | trans = btrfs_start_transaction(root, 0); | |
4481 | if (IS_ERR(trans)) { | |
4482 | err = PTR_ERR(trans); | |
4483 | goto out_release; | |
4484 | } | |
4485 | trans->block_rsv = &block_rsv; | |
4486 | trans->bytes_reserved = block_rsv.size; | |
4487 | ||
4488 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
4489 | ||
401b3b19 LF |
4490 | ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid, |
4491 | dentry->d_name.name, dentry->d_name.len); | |
f60a2364 MT |
4492 | if (ret) { |
4493 | err = ret; | |
4494 | btrfs_abort_transaction(trans, ret); | |
4495 | goto out_end_trans; | |
4496 | } | |
4497 | ||
4498 | btrfs_record_root_in_trans(trans, dest); | |
4499 | ||
4500 | memset(&dest->root_item.drop_progress, 0, | |
4501 | sizeof(dest->root_item.drop_progress)); | |
4502 | dest->root_item.drop_level = 0; | |
4503 | btrfs_set_root_refs(&dest->root_item, 0); | |
4504 | ||
4505 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4506 | ret = btrfs_insert_orphan_item(trans, | |
4507 | fs_info->tree_root, | |
4508 | dest->root_key.objectid); | |
4509 | if (ret) { | |
4510 | btrfs_abort_transaction(trans, ret); | |
4511 | err = ret; | |
4512 | goto out_end_trans; | |
4513 | } | |
4514 | } | |
4515 | ||
d1957791 | 4516 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4517 | BTRFS_UUID_KEY_SUBVOL, |
4518 | dest->root_key.objectid); | |
4519 | if (ret && ret != -ENOENT) { | |
4520 | btrfs_abort_transaction(trans, ret); | |
4521 | err = ret; | |
4522 | goto out_end_trans; | |
4523 | } | |
4524 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4525 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4526 | dest->root_item.received_uuid, |
4527 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4528 | dest->root_key.objectid); | |
4529 | if (ret && ret != -ENOENT) { | |
4530 | btrfs_abort_transaction(trans, ret); | |
4531 | err = ret; | |
4532 | goto out_end_trans; | |
4533 | } | |
4534 | } | |
4535 | ||
4536 | out_end_trans: | |
4537 | trans->block_rsv = NULL; | |
4538 | trans->bytes_reserved = 0; | |
4539 | ret = btrfs_end_transaction(trans); | |
4540 | if (ret && !err) | |
4541 | err = ret; | |
4542 | inode->i_flags |= S_DEAD; | |
4543 | out_release: | |
4544 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); | |
4545 | out_up_write: | |
4546 | up_write(&fs_info->subvol_sem); | |
4547 | if (err) { | |
4548 | spin_lock(&dest->root_item_lock); | |
4549 | root_flags = btrfs_root_flags(&dest->root_item); | |
4550 | btrfs_set_root_flags(&dest->root_item, | |
4551 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4552 | spin_unlock(&dest->root_item_lock); | |
4553 | } else { | |
4554 | d_invalidate(dentry); | |
20a68004 | 4555 | btrfs_prune_dentries(dest); |
f60a2364 MT |
4556 | ASSERT(dest->send_in_progress == 0); |
4557 | ||
4558 | /* the last ref */ | |
4559 | if (dest->ino_cache_inode) { | |
4560 | iput(dest->ino_cache_inode); | |
4561 | dest->ino_cache_inode = NULL; | |
4562 | } | |
4563 | } | |
4564 | ||
4565 | return err; | |
4566 | } | |
4567 | ||
39279cc3 CM |
4568 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4569 | { | |
2b0143b5 | 4570 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4571 | int err = 0; |
39279cc3 | 4572 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4573 | struct btrfs_trans_handle *trans; |
44f714da | 4574 | u64 last_unlink_trans; |
39279cc3 | 4575 | |
b3ae244e | 4576 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4577 | return -ENOTEMPTY; |
4a0cc7ca | 4578 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4579 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4580 | |
d52be818 | 4581 | trans = __unlink_start_trans(dir); |
a22285a6 | 4582 | if (IS_ERR(trans)) |
5df6a9f6 | 4583 | return PTR_ERR(trans); |
5df6a9f6 | 4584 | |
4a0cc7ca | 4585 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
401b3b19 | 4586 | err = btrfs_unlink_subvol(trans, dir, |
4df27c4d YZ |
4587 | BTRFS_I(inode)->location.objectid, |
4588 | dentry->d_name.name, | |
4589 | dentry->d_name.len); | |
4590 | goto out; | |
4591 | } | |
4592 | ||
73f2e545 | 4593 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4594 | if (err) |
4df27c4d | 4595 | goto out; |
7b128766 | 4596 | |
44f714da FM |
4597 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4598 | ||
39279cc3 | 4599 | /* now the directory is empty */ |
4ec5934e NB |
4600 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4601 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4602 | dentry->d_name.len); | |
44f714da | 4603 | if (!err) { |
6ef06d27 | 4604 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4605 | /* |
4606 | * Propagate the last_unlink_trans value of the deleted dir to | |
4607 | * its parent directory. This is to prevent an unrecoverable | |
4608 | * log tree in the case we do something like this: | |
4609 | * 1) create dir foo | |
4610 | * 2) create snapshot under dir foo | |
4611 | * 3) delete the snapshot | |
4612 | * 4) rmdir foo | |
4613 | * 5) mkdir foo | |
4614 | * 6) fsync foo or some file inside foo | |
4615 | */ | |
4616 | if (last_unlink_trans >= trans->transid) | |
4617 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4618 | } | |
4df27c4d | 4619 | out: |
3a45bb20 | 4620 | btrfs_end_transaction(trans); |
2ff7e61e | 4621 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4622 | |
39279cc3 CM |
4623 | return err; |
4624 | } | |
4625 | ||
ddfae63c JB |
4626 | /* |
4627 | * Return this if we need to call truncate_block for the last bit of the | |
4628 | * truncate. | |
4629 | */ | |
4630 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4631 | |
39279cc3 CM |
4632 | /* |
4633 | * this can truncate away extent items, csum items and directory items. | |
4634 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4635 | * any higher than new_size |
39279cc3 CM |
4636 | * |
4637 | * csum items that cross the new i_size are truncated to the new size | |
4638 | * as well. | |
7b128766 JB |
4639 | * |
4640 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4641 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4642 | */ |
8082510e YZ |
4643 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4644 | struct btrfs_root *root, | |
4645 | struct inode *inode, | |
4646 | u64 new_size, u32 min_type) | |
39279cc3 | 4647 | { |
0b246afa | 4648 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4649 | struct btrfs_path *path; |
5f39d397 | 4650 | struct extent_buffer *leaf; |
39279cc3 | 4651 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4652 | struct btrfs_key key; |
4653 | struct btrfs_key found_key; | |
39279cc3 | 4654 | u64 extent_start = 0; |
db94535d | 4655 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4656 | u64 extent_offset = 0; |
39279cc3 | 4657 | u64 item_end = 0; |
c1aa4575 | 4658 | u64 last_size = new_size; |
8082510e | 4659 | u32 found_type = (u8)-1; |
39279cc3 CM |
4660 | int found_extent; |
4661 | int del_item; | |
85e21bac CM |
4662 | int pending_del_nr = 0; |
4663 | int pending_del_slot = 0; | |
179e29e4 | 4664 | int extent_type = -1; |
8082510e | 4665 | int ret; |
4a0cc7ca | 4666 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4667 | u64 bytes_deleted = 0; |
897ca819 TM |
4668 | bool be_nice = false; |
4669 | bool should_throttle = false; | |
8082510e YZ |
4670 | |
4671 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4672 | |
28ed1345 CM |
4673 | /* |
4674 | * for non-free space inodes and ref cows, we want to back off from | |
4675 | * time to time | |
4676 | */ | |
70ddc553 | 4677 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 | 4678 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
897ca819 | 4679 | be_nice = true; |
28ed1345 | 4680 | |
0eb0e19c MF |
4681 | path = btrfs_alloc_path(); |
4682 | if (!path) | |
4683 | return -ENOMEM; | |
e4058b54 | 4684 | path->reada = READA_BACK; |
0eb0e19c | 4685 | |
5dc562c5 JB |
4686 | /* |
4687 | * We want to drop from the next block forward in case this new size is | |
4688 | * not block aligned since we will be keeping the last block of the | |
4689 | * extent just the way it is. | |
4690 | */ | |
27cdeb70 | 4691 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4692 | root == fs_info->tree_root) |
dcdbc059 | 4693 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4694 | fs_info->sectorsize), |
da17066c | 4695 | (u64)-1, 0); |
8082510e | 4696 | |
16cdcec7 MX |
4697 | /* |
4698 | * This function is also used to drop the items in the log tree before | |
4699 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
52042d8e | 4700 | * it is used to drop the logged items. So we shouldn't kill the delayed |
16cdcec7 MX |
4701 | * items. |
4702 | */ | |
4703 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4704 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4705 | |
33345d01 | 4706 | key.objectid = ino; |
39279cc3 | 4707 | key.offset = (u64)-1; |
5f39d397 CM |
4708 | key.type = (u8)-1; |
4709 | ||
85e21bac | 4710 | search_again: |
28ed1345 CM |
4711 | /* |
4712 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4713 | * up a huge file in a single leaf. Most of the time that | |
4714 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4715 | */ | |
fd86a3a3 OS |
4716 | if (be_nice && bytes_deleted > SZ_32M && |
4717 | btrfs_should_end_transaction(trans)) { | |
4718 | ret = -EAGAIN; | |
4719 | goto out; | |
28ed1345 CM |
4720 | } |
4721 | ||
b9473439 | 4722 | path->leave_spinning = 1; |
85e21bac | 4723 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
fd86a3a3 | 4724 | if (ret < 0) |
8082510e | 4725 | goto out; |
d397712b | 4726 | |
85e21bac | 4727 | if (ret > 0) { |
fd86a3a3 | 4728 | ret = 0; |
e02119d5 CM |
4729 | /* there are no items in the tree for us to truncate, we're |
4730 | * done | |
4731 | */ | |
8082510e YZ |
4732 | if (path->slots[0] == 0) |
4733 | goto out; | |
85e21bac CM |
4734 | path->slots[0]--; |
4735 | } | |
4736 | ||
d397712b | 4737 | while (1) { |
39279cc3 | 4738 | fi = NULL; |
5f39d397 CM |
4739 | leaf = path->nodes[0]; |
4740 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4741 | found_type = found_key.type; |
39279cc3 | 4742 | |
33345d01 | 4743 | if (found_key.objectid != ino) |
39279cc3 | 4744 | break; |
5f39d397 | 4745 | |
85e21bac | 4746 | if (found_type < min_type) |
39279cc3 CM |
4747 | break; |
4748 | ||
5f39d397 | 4749 | item_end = found_key.offset; |
39279cc3 | 4750 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4751 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4752 | struct btrfs_file_extent_item); |
179e29e4 CM |
4753 | extent_type = btrfs_file_extent_type(leaf, fi); |
4754 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4755 | item_end += |
db94535d | 4756 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4757 | |
4758 | trace_btrfs_truncate_show_fi_regular( | |
4759 | BTRFS_I(inode), leaf, fi, | |
4760 | found_key.offset); | |
179e29e4 | 4761 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 QW |
4762 | item_end += btrfs_file_extent_ram_bytes(leaf, |
4763 | fi); | |
09ed2f16 LB |
4764 | |
4765 | trace_btrfs_truncate_show_fi_inline( | |
4766 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4767 | found_key.offset); | |
39279cc3 | 4768 | } |
008630c1 | 4769 | item_end--; |
39279cc3 | 4770 | } |
8082510e YZ |
4771 | if (found_type > min_type) { |
4772 | del_item = 1; | |
4773 | } else { | |
76b42abb | 4774 | if (item_end < new_size) |
b888db2b | 4775 | break; |
8082510e YZ |
4776 | if (found_key.offset >= new_size) |
4777 | del_item = 1; | |
4778 | else | |
4779 | del_item = 0; | |
39279cc3 | 4780 | } |
39279cc3 | 4781 | found_extent = 0; |
39279cc3 | 4782 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4783 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4784 | goto delete; | |
4785 | ||
4786 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4787 | u64 num_dec; |
db94535d | 4788 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4789 | if (!del_item) { |
db94535d CM |
4790 | u64 orig_num_bytes = |
4791 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4792 | extent_num_bytes = ALIGN(new_size - |
4793 | found_key.offset, | |
0b246afa | 4794 | fs_info->sectorsize); |
db94535d CM |
4795 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4796 | extent_num_bytes); | |
4797 | num_dec = (orig_num_bytes - | |
9069218d | 4798 | extent_num_bytes); |
27cdeb70 MX |
4799 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4800 | &root->state) && | |
4801 | extent_start != 0) | |
a76a3cd4 | 4802 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4803 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4804 | } else { |
db94535d CM |
4805 | extent_num_bytes = |
4806 | btrfs_file_extent_disk_num_bytes(leaf, | |
4807 | fi); | |
5d4f98a2 YZ |
4808 | extent_offset = found_key.offset - |
4809 | btrfs_file_extent_offset(leaf, fi); | |
4810 | ||
39279cc3 | 4811 | /* FIXME blocksize != 4096 */ |
9069218d | 4812 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4813 | if (extent_start != 0) { |
4814 | found_extent = 1; | |
27cdeb70 MX |
4815 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4816 | &root->state)) | |
a76a3cd4 | 4817 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4818 | } |
39279cc3 | 4819 | } |
9069218d | 4820 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4821 | /* |
4822 | * we can't truncate inline items that have had | |
4823 | * special encodings | |
4824 | */ | |
4825 | if (!del_item && | |
c8b97818 | 4826 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4827 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4828 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4829 | u32 size = (u32)(new_size - found_key.offset); | |
4830 | ||
4831 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4832 | size = btrfs_file_extent_calc_inline_size(size); | |
78ac4f9e | 4833 | btrfs_truncate_item(path, size, 1); |
ddfae63c | 4834 | } else if (!del_item) { |
514ac8ad | 4835 | /* |
ddfae63c JB |
4836 | * We have to bail so the last_size is set to |
4837 | * just before this extent. | |
514ac8ad | 4838 | */ |
fd86a3a3 | 4839 | ret = NEED_TRUNCATE_BLOCK; |
ddfae63c JB |
4840 | break; |
4841 | } | |
0305cd5f | 4842 | |
ddfae63c | 4843 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
0305cd5f | 4844 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4845 | } |
179e29e4 | 4846 | delete: |
ddfae63c JB |
4847 | if (del_item) |
4848 | last_size = found_key.offset; | |
4849 | else | |
4850 | last_size = new_size; | |
39279cc3 | 4851 | if (del_item) { |
85e21bac CM |
4852 | if (!pending_del_nr) { |
4853 | /* no pending yet, add ourselves */ | |
4854 | pending_del_slot = path->slots[0]; | |
4855 | pending_del_nr = 1; | |
4856 | } else if (pending_del_nr && | |
4857 | path->slots[0] + 1 == pending_del_slot) { | |
4858 | /* hop on the pending chunk */ | |
4859 | pending_del_nr++; | |
4860 | pending_del_slot = path->slots[0]; | |
4861 | } else { | |
d397712b | 4862 | BUG(); |
85e21bac | 4863 | } |
39279cc3 CM |
4864 | } else { |
4865 | break; | |
4866 | } | |
897ca819 | 4867 | should_throttle = false; |
28f75a0e | 4868 | |
27cdeb70 MX |
4869 | if (found_extent && |
4870 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4871 | root == fs_info->tree_root)) { |
ffd4bb2a QW |
4872 | struct btrfs_ref ref = { 0 }; |
4873 | ||
b9473439 | 4874 | btrfs_set_path_blocking(path); |
28ed1345 | 4875 | bytes_deleted += extent_num_bytes; |
ffd4bb2a QW |
4876 | |
4877 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, | |
4878 | extent_start, extent_num_bytes, 0); | |
4879 | ref.real_root = root->root_key.objectid; | |
4880 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
4881 | ino, extent_offset); | |
4882 | ret = btrfs_free_extent(trans, &ref); | |
05522109 OS |
4883 | if (ret) { |
4884 | btrfs_abort_transaction(trans, ret); | |
4885 | break; | |
4886 | } | |
28f75a0e | 4887 | if (be_nice) { |
7c861627 | 4888 | if (btrfs_should_throttle_delayed_refs(trans)) |
897ca819 | 4889 | should_throttle = true; |
28f75a0e | 4890 | } |
39279cc3 | 4891 | } |
85e21bac | 4892 | |
8082510e YZ |
4893 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4894 | break; | |
4895 | ||
4896 | if (path->slots[0] == 0 || | |
1262133b | 4897 | path->slots[0] != pending_del_slot || |
28bad212 | 4898 | should_throttle) { |
8082510e YZ |
4899 | if (pending_del_nr) { |
4900 | ret = btrfs_del_items(trans, root, path, | |
4901 | pending_del_slot, | |
4902 | pending_del_nr); | |
79787eaa | 4903 | if (ret) { |
66642832 | 4904 | btrfs_abort_transaction(trans, ret); |
fd86a3a3 | 4905 | break; |
79787eaa | 4906 | } |
8082510e YZ |
4907 | pending_del_nr = 0; |
4908 | } | |
b3b4aa74 | 4909 | btrfs_release_path(path); |
28bad212 | 4910 | |
28f75a0e | 4911 | /* |
28bad212 JB |
4912 | * We can generate a lot of delayed refs, so we need to |
4913 | * throttle every once and a while and make sure we're | |
4914 | * adding enough space to keep up with the work we are | |
4915 | * generating. Since we hold a transaction here we | |
4916 | * can't flush, and we don't want to FLUSH_LIMIT because | |
4917 | * we could have generated too many delayed refs to | |
4918 | * actually allocate, so just bail if we're short and | |
4919 | * let the normal reservation dance happen higher up. | |
28f75a0e | 4920 | */ |
28bad212 JB |
4921 | if (should_throttle) { |
4922 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
4923 | BTRFS_RESERVE_NO_FLUSH); | |
4924 | if (ret) { | |
4925 | ret = -EAGAIN; | |
4926 | break; | |
4927 | } | |
28f75a0e | 4928 | } |
85e21bac | 4929 | goto search_again; |
8082510e YZ |
4930 | } else { |
4931 | path->slots[0]--; | |
85e21bac | 4932 | } |
39279cc3 | 4933 | } |
8082510e | 4934 | out: |
fd86a3a3 OS |
4935 | if (ret >= 0 && pending_del_nr) { |
4936 | int err; | |
4937 | ||
4938 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
85e21bac | 4939 | pending_del_nr); |
fd86a3a3 OS |
4940 | if (err) { |
4941 | btrfs_abort_transaction(trans, err); | |
4942 | ret = err; | |
4943 | } | |
85e21bac | 4944 | } |
76b42abb FM |
4945 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4946 | ASSERT(last_size >= new_size); | |
fd86a3a3 | 4947 | if (!ret && last_size > new_size) |
76b42abb | 4948 | last_size = new_size; |
7f4f6e0a | 4949 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4950 | } |
28ed1345 | 4951 | |
39279cc3 | 4952 | btrfs_free_path(path); |
fd86a3a3 | 4953 | return ret; |
39279cc3 CM |
4954 | } |
4955 | ||
4956 | /* | |
9703fefe | 4957 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4958 | * @inode - inode that we're zeroing |
4959 | * @from - the offset to start zeroing | |
4960 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4961 | * offset | |
4962 | * @front - zero up to the offset instead of from the offset on | |
4963 | * | |
9703fefe | 4964 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4965 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4966 | */ |
9703fefe | 4967 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4968 | int front) |
39279cc3 | 4969 | { |
0b246afa | 4970 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4971 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4972 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4973 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4974 | struct extent_state *cached_state = NULL; |
364ecf36 | 4975 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4976 | char *kaddr; |
0b246afa | 4977 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4978 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4979 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4980 | struct page *page; |
3b16a4e3 | 4981 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4982 | int ret = 0; |
9703fefe CR |
4983 | u64 block_start; |
4984 | u64 block_end; | |
39279cc3 | 4985 | |
b03ebd99 NB |
4986 | if (IS_ALIGNED(offset, blocksize) && |
4987 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4988 | goto out; |
9703fefe | 4989 | |
8b62f87b JB |
4990 | block_start = round_down(from, blocksize); |
4991 | block_end = block_start + blocksize - 1; | |
4992 | ||
364ecf36 | 4993 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8b62f87b | 4994 | block_start, blocksize); |
5d5e103a JB |
4995 | if (ret) |
4996 | goto out; | |
39279cc3 | 4997 | |
211c17f5 | 4998 | again: |
3b16a4e3 | 4999 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 5000 | if (!page) { |
bc42bda2 | 5001 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 | 5002 | block_start, blocksize, true); |
8702ba93 | 5003 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
ac6a2b36 | 5004 | ret = -ENOMEM; |
39279cc3 | 5005 | goto out; |
5d5e103a | 5006 | } |
e6dcd2dc | 5007 | |
39279cc3 | 5008 | if (!PageUptodate(page)) { |
9ebefb18 | 5009 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 5010 | lock_page(page); |
211c17f5 CM |
5011 | if (page->mapping != mapping) { |
5012 | unlock_page(page); | |
09cbfeaf | 5013 | put_page(page); |
211c17f5 CM |
5014 | goto again; |
5015 | } | |
39279cc3 CM |
5016 | if (!PageUptodate(page)) { |
5017 | ret = -EIO; | |
89642229 | 5018 | goto out_unlock; |
39279cc3 CM |
5019 | } |
5020 | } | |
211c17f5 | 5021 | wait_on_page_writeback(page); |
e6dcd2dc | 5022 | |
9703fefe | 5023 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
5024 | set_page_extent_mapped(page); |
5025 | ||
9703fefe | 5026 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 5027 | if (ordered) { |
9703fefe | 5028 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 5029 | &cached_state); |
e6dcd2dc | 5030 | unlock_page(page); |
09cbfeaf | 5031 | put_page(page); |
eb84ae03 | 5032 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
5033 | btrfs_put_ordered_extent(ordered); |
5034 | goto again; | |
5035 | } | |
5036 | ||
9703fefe | 5037 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
e182163d OS |
5038 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
5039 | 0, 0, &cached_state); | |
5d5e103a | 5040 | |
e3b8a485 | 5041 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
330a5827 | 5042 | &cached_state); |
9ed74f2d | 5043 | if (ret) { |
9703fefe | 5044 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 5045 | &cached_state); |
9ed74f2d JB |
5046 | goto out_unlock; |
5047 | } | |
5048 | ||
9703fefe | 5049 | if (offset != blocksize) { |
2aaa6655 | 5050 | if (!len) |
9703fefe | 5051 | len = blocksize - offset; |
e6dcd2dc | 5052 | kaddr = kmap(page); |
2aaa6655 | 5053 | if (front) |
9703fefe CR |
5054 | memset(kaddr + (block_start - page_offset(page)), |
5055 | 0, offset); | |
2aaa6655 | 5056 | else |
9703fefe CR |
5057 | memset(kaddr + (block_start - page_offset(page)) + offset, |
5058 | 0, len); | |
e6dcd2dc CM |
5059 | flush_dcache_page(page); |
5060 | kunmap(page); | |
5061 | } | |
247e743c | 5062 | ClearPageChecked(page); |
e6dcd2dc | 5063 | set_page_dirty(page); |
e43bbe5e | 5064 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 5065 | |
89642229 | 5066 | out_unlock: |
5d5e103a | 5067 | if (ret) |
bc42bda2 | 5068 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
43b18595 | 5069 | blocksize, true); |
8702ba93 | 5070 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
39279cc3 | 5071 | unlock_page(page); |
09cbfeaf | 5072 | put_page(page); |
39279cc3 | 5073 | out: |
364ecf36 | 5074 | extent_changeset_free(data_reserved); |
39279cc3 CM |
5075 | return ret; |
5076 | } | |
5077 | ||
16e7549f JB |
5078 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
5079 | u64 offset, u64 len) | |
5080 | { | |
0b246afa | 5081 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
5082 | struct btrfs_trans_handle *trans; |
5083 | int ret; | |
5084 | ||
5085 | /* | |
5086 | * Still need to make sure the inode looks like it's been updated so | |
5087 | * that any holes get logged if we fsync. | |
5088 | */ | |
0b246afa JM |
5089 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
5090 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
5091 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
5092 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
5093 | return 0; | |
5094 | } | |
5095 | ||
5096 | /* | |
5097 | * 1 - for the one we're dropping | |
5098 | * 1 - for the one we're adding | |
5099 | * 1 - for updating the inode. | |
5100 | */ | |
5101 | trans = btrfs_start_transaction(root, 3); | |
5102 | if (IS_ERR(trans)) | |
5103 | return PTR_ERR(trans); | |
5104 | ||
5105 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
5106 | if (ret) { | |
66642832 | 5107 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 5108 | btrfs_end_transaction(trans); |
16e7549f JB |
5109 | return ret; |
5110 | } | |
5111 | ||
f85b7379 DS |
5112 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
5113 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 5114 | if (ret) |
66642832 | 5115 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
5116 | else |
5117 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 5118 | btrfs_end_transaction(trans); |
16e7549f JB |
5119 | return ret; |
5120 | } | |
5121 | ||
695a0d0d JB |
5122 | /* |
5123 | * This function puts in dummy file extents for the area we're creating a hole | |
5124 | * for. So if we are truncating this file to a larger size we need to insert | |
5125 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
5126 | * the range between oldsize and size | |
5127 | */ | |
a41ad394 | 5128 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 5129 | { |
0b246afa | 5130 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
5131 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5132 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 5133 | struct extent_map *em = NULL; |
2ac55d41 | 5134 | struct extent_state *cached_state = NULL; |
5dc562c5 | 5135 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
5136 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
5137 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
5138 | u64 last_byte; |
5139 | u64 cur_offset; | |
5140 | u64 hole_size; | |
9ed74f2d | 5141 | int err = 0; |
39279cc3 | 5142 | |
a71754fc | 5143 | /* |
9703fefe CR |
5144 | * If our size started in the middle of a block we need to zero out the |
5145 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
5146 | * expose stale data. |
5147 | */ | |
9703fefe | 5148 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
5149 | if (err) |
5150 | return err; | |
5151 | ||
9036c102 YZ |
5152 | if (size <= hole_start) |
5153 | return 0; | |
5154 | ||
23d31bd4 NB |
5155 | btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start, |
5156 | block_end - 1, &cached_state); | |
9036c102 YZ |
5157 | cur_offset = hole_start; |
5158 | while (1) { | |
fc4f21b1 | 5159 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 5160 | block_end - cur_offset, 0); |
79787eaa JM |
5161 | if (IS_ERR(em)) { |
5162 | err = PTR_ERR(em); | |
f2767956 | 5163 | em = NULL; |
79787eaa JM |
5164 | break; |
5165 | } | |
9036c102 | 5166 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 5167 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 5168 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 5169 | struct extent_map *hole_em; |
9036c102 | 5170 | hole_size = last_byte - cur_offset; |
9ed74f2d | 5171 | |
16e7549f JB |
5172 | err = maybe_insert_hole(root, inode, cur_offset, |
5173 | hole_size); | |
5174 | if (err) | |
3893e33b | 5175 | break; |
dcdbc059 | 5176 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
5177 | cur_offset + hole_size - 1, 0); |
5178 | hole_em = alloc_extent_map(); | |
5179 | if (!hole_em) { | |
5180 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
5181 | &BTRFS_I(inode)->runtime_flags); | |
5182 | goto next; | |
5183 | } | |
5184 | hole_em->start = cur_offset; | |
5185 | hole_em->len = hole_size; | |
5186 | hole_em->orig_start = cur_offset; | |
8082510e | 5187 | |
5dc562c5 JB |
5188 | hole_em->block_start = EXTENT_MAP_HOLE; |
5189 | hole_em->block_len = 0; | |
b4939680 | 5190 | hole_em->orig_block_len = 0; |
cc95bef6 | 5191 | hole_em->ram_bytes = hole_size; |
5dc562c5 | 5192 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 5193 | hole_em->generation = fs_info->generation; |
8082510e | 5194 | |
5dc562c5 JB |
5195 | while (1) { |
5196 | write_lock(&em_tree->lock); | |
09a2a8f9 | 5197 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
5198 | write_unlock(&em_tree->lock); |
5199 | if (err != -EEXIST) | |
5200 | break; | |
dcdbc059 NB |
5201 | btrfs_drop_extent_cache(BTRFS_I(inode), |
5202 | cur_offset, | |
5dc562c5 JB |
5203 | cur_offset + |
5204 | hole_size - 1, 0); | |
5205 | } | |
5206 | free_extent_map(hole_em); | |
9036c102 | 5207 | } |
16e7549f | 5208 | next: |
9036c102 | 5209 | free_extent_map(em); |
a22285a6 | 5210 | em = NULL; |
9036c102 | 5211 | cur_offset = last_byte; |
8082510e | 5212 | if (cur_offset >= block_end) |
9036c102 YZ |
5213 | break; |
5214 | } | |
a22285a6 | 5215 | free_extent_map(em); |
e43bbe5e | 5216 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
5217 | return err; |
5218 | } | |
39279cc3 | 5219 | |
3972f260 | 5220 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5221 | { |
f4a2f4c5 MX |
5222 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5223 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5224 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5225 | loff_t newsize = attr->ia_size; |
5226 | int mask = attr->ia_valid; | |
8082510e YZ |
5227 | int ret; |
5228 | ||
3972f260 ES |
5229 | /* |
5230 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5231 | * special case where we need to update the times despite not having | |
5232 | * these flags set. For all other operations the VFS set these flags | |
5233 | * explicitly if it wants a timestamp update. | |
5234 | */ | |
dff6efc3 CH |
5235 | if (newsize != oldsize) { |
5236 | inode_inc_iversion(inode); | |
5237 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5238 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5239 | current_time(inode); |
dff6efc3 | 5240 | } |
3972f260 | 5241 | |
a41ad394 | 5242 | if (newsize > oldsize) { |
9ea24bbe | 5243 | /* |
ea14b57f | 5244 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5245 | * This is to ensure the snapshot captures a fully consistent |
5246 | * state of this file - if the snapshot captures this expanding | |
5247 | * truncation, it must capture all writes that happened before | |
5248 | * this truncation. | |
5249 | */ | |
0bc19f90 | 5250 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5251 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 5252 | if (ret) { |
ea14b57f | 5253 | btrfs_end_write_no_snapshotting(root); |
8082510e | 5254 | return ret; |
9ea24bbe | 5255 | } |
8082510e | 5256 | |
f4a2f4c5 | 5257 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5258 | if (IS_ERR(trans)) { |
ea14b57f | 5259 | btrfs_end_write_no_snapshotting(root); |
f4a2f4c5 | 5260 | return PTR_ERR(trans); |
9ea24bbe | 5261 | } |
f4a2f4c5 MX |
5262 | |
5263 | i_size_write(inode, newsize); | |
5264 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5265 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5266 | ret = btrfs_update_inode(trans, root, inode); |
ea14b57f | 5267 | btrfs_end_write_no_snapshotting(root); |
3a45bb20 | 5268 | btrfs_end_transaction(trans); |
a41ad394 | 5269 | } else { |
8082510e | 5270 | |
a41ad394 JB |
5271 | /* |
5272 | * We're truncating a file that used to have good data down to | |
5273 | * zero. Make sure it gets into the ordered flush list so that | |
5274 | * any new writes get down to disk quickly. | |
5275 | */ | |
5276 | if (newsize == 0) | |
72ac3c0d JB |
5277 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5278 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5279 | |
a41ad394 | 5280 | truncate_setsize(inode, newsize); |
2e60a51e | 5281 | |
52042d8e | 5282 | /* Disable nonlocked read DIO to avoid the endless truncate */ |
abcefb1e | 5283 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5284 | inode_dio_wait(inode); |
0b581701 | 5285 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5286 | |
213e8c55 | 5287 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
5288 | if (ret && inode->i_nlink) { |
5289 | int err; | |
5290 | ||
5291 | /* | |
f7e9e8fc OS |
5292 | * Truncate failed, so fix up the in-memory size. We |
5293 | * adjusted disk_i_size down as we removed extents, so | |
5294 | * wait for disk_i_size to be stable and then update the | |
5295 | * in-memory size to match. | |
7f4f6e0a | 5296 | */ |
f7e9e8fc | 5297 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5298 | if (err) |
f7e9e8fc OS |
5299 | return err; |
5300 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5301 | } |
8082510e YZ |
5302 | } |
5303 | ||
a41ad394 | 5304 | return ret; |
8082510e YZ |
5305 | } |
5306 | ||
9036c102 YZ |
5307 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5308 | { | |
2b0143b5 | 5309 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5310 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5311 | int err; |
39279cc3 | 5312 | |
b83cc969 LZ |
5313 | if (btrfs_root_readonly(root)) |
5314 | return -EROFS; | |
5315 | ||
31051c85 | 5316 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5317 | if (err) |
5318 | return err; | |
2bf5a725 | 5319 | |
5a3f23d5 | 5320 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5321 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5322 | if (err) |
5323 | return err; | |
39279cc3 | 5324 | } |
9036c102 | 5325 | |
1025774c CH |
5326 | if (attr->ia_valid) { |
5327 | setattr_copy(inode, attr); | |
0c4d2d95 | 5328 | inode_inc_iversion(inode); |
22c44fe6 | 5329 | err = btrfs_dirty_inode(inode); |
1025774c | 5330 | |
22c44fe6 | 5331 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5332 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5333 | } |
33268eaf | 5334 | |
39279cc3 CM |
5335 | return err; |
5336 | } | |
61295eb8 | 5337 | |
131e404a FDBM |
5338 | /* |
5339 | * While truncating the inode pages during eviction, we get the VFS calling | |
5340 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5341 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5342 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5343 | * extent_state structures over and over, wasting lots of time. | |
5344 | * | |
5345 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5346 | * those expensive operations on a per page basis and do only the ordered io | |
5347 | * finishing, while we release here the extent_map and extent_state structures, | |
5348 | * without the excessive merging and splitting. | |
5349 | */ | |
5350 | static void evict_inode_truncate_pages(struct inode *inode) | |
5351 | { | |
5352 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5353 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5354 | struct rb_node *node; | |
5355 | ||
5356 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5357 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5358 | |
5359 | write_lock(&map_tree->lock); | |
07e1ce09 | 5360 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
5361 | struct extent_map *em; |
5362 | ||
07e1ce09 | 5363 | node = rb_first_cached(&map_tree->map); |
131e404a | 5364 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
5365 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5366 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5367 | remove_extent_mapping(map_tree, em); |
5368 | free_extent_map(em); | |
7064dd5c FM |
5369 | if (need_resched()) { |
5370 | write_unlock(&map_tree->lock); | |
5371 | cond_resched(); | |
5372 | write_lock(&map_tree->lock); | |
5373 | } | |
131e404a FDBM |
5374 | } |
5375 | write_unlock(&map_tree->lock); | |
5376 | ||
6ca07097 FM |
5377 | /* |
5378 | * Keep looping until we have no more ranges in the io tree. | |
5379 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5380 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5381 | * still in progress (unlocked the pages in the bio but did not yet | |
5382 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5383 | * ranges can still be locked and eviction started because before |
5384 | * submitting those bios, which are executed by a separate task (work | |
5385 | * queue kthread), inode references (inode->i_count) were not taken | |
5386 | * (which would be dropped in the end io callback of each bio). | |
5387 | * Therefore here we effectively end up waiting for those bios and | |
5388 | * anyone else holding locked ranges without having bumped the inode's | |
5389 | * reference count - if we don't do it, when they access the inode's | |
5390 | * io_tree to unlock a range it may be too late, leading to an | |
5391 | * use-after-free issue. | |
5392 | */ | |
131e404a FDBM |
5393 | spin_lock(&io_tree->lock); |
5394 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5395 | struct extent_state *state; | |
5396 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5397 | u64 start; |
5398 | u64 end; | |
421f0922 | 5399 | unsigned state_flags; |
131e404a FDBM |
5400 | |
5401 | node = rb_first(&io_tree->state); | |
5402 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5403 | start = state->start; |
5404 | end = state->end; | |
421f0922 | 5405 | state_flags = state->state; |
131e404a FDBM |
5406 | spin_unlock(&io_tree->lock); |
5407 | ||
ff13db41 | 5408 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5409 | |
5410 | /* | |
5411 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5412 | * and its reserved space won't be freed by delayed_ref. | |
5413 | * So we need to free its reserved space here. | |
5414 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5415 | * | |
5416 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5417 | */ | |
421f0922 | 5418 | if (state_flags & EXTENT_DELALLOC) |
bc42bda2 | 5419 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
b9d0b389 | 5420 | |
6ca07097 | 5421 | clear_extent_bit(io_tree, start, end, |
e182163d OS |
5422 | EXTENT_LOCKED | EXTENT_DELALLOC | |
5423 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
5424 | &cached_state); | |
131e404a | 5425 | |
7064dd5c | 5426 | cond_resched(); |
131e404a FDBM |
5427 | spin_lock(&io_tree->lock); |
5428 | } | |
5429 | spin_unlock(&io_tree->lock); | |
5430 | } | |
5431 | ||
4b9d7b59 | 5432 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5433 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5434 | { |
5435 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5436 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
d3984c90 | 5437 | struct btrfs_trans_handle *trans; |
2bd36e7b | 5438 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
d3984c90 | 5439 | int ret; |
4b9d7b59 | 5440 | |
d3984c90 JB |
5441 | /* |
5442 | * Eviction should be taking place at some place safe because of our | |
5443 | * delayed iputs. However the normal flushing code will run delayed | |
5444 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. | |
5445 | * | |
5446 | * We reserve the delayed_refs_extra here again because we can't use | |
5447 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as | |
5448 | * above. We reserve our extra bit here because we generate a ton of | |
5449 | * delayed refs activity by truncating. | |
5450 | * | |
5451 | * If we cannot make our reservation we'll attempt to steal from the | |
5452 | * global reserve, because we really want to be able to free up space. | |
5453 | */ | |
5454 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, | |
5455 | BTRFS_RESERVE_FLUSH_EVICT); | |
5456 | if (ret) { | |
4b9d7b59 OS |
5457 | /* |
5458 | * Try to steal from the global reserve if there is space for | |
5459 | * it. | |
5460 | */ | |
d3984c90 JB |
5461 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
5462 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { | |
5463 | btrfs_warn(fs_info, | |
5464 | "could not allocate space for delete; will truncate on mount"); | |
5465 | return ERR_PTR(-ENOSPC); | |
5466 | } | |
5467 | delayed_refs_extra = 0; | |
5468 | } | |
4b9d7b59 | 5469 | |
d3984c90 JB |
5470 | trans = btrfs_join_transaction(root); |
5471 | if (IS_ERR(trans)) | |
5472 | return trans; | |
5473 | ||
5474 | if (delayed_refs_extra) { | |
5475 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5476 | trans->bytes_reserved = delayed_refs_extra; | |
5477 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5478 | delayed_refs_extra, 1); | |
4b9d7b59 | 5479 | } |
d3984c90 | 5480 | return trans; |
4b9d7b59 OS |
5481 | } |
5482 | ||
bd555975 | 5483 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5484 | { |
0b246afa | 5485 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5486 | struct btrfs_trans_handle *trans; |
5487 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5488 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5489 | int ret; |
5490 | ||
1abe9b8a | 5491 | trace_btrfs_inode_evict(inode); |
5492 | ||
3d48d981 | 5493 | if (!root) { |
e8f1bc14 | 5494 | clear_inode(inode); |
3d48d981 NB |
5495 | return; |
5496 | } | |
5497 | ||
131e404a FDBM |
5498 | evict_inode_truncate_pages(inode); |
5499 | ||
69e9c6c6 SB |
5500 | if (inode->i_nlink && |
5501 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5502 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5503 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5504 | goto no_delete; |
5505 | ||
27919067 | 5506 | if (is_bad_inode(inode)) |
39279cc3 | 5507 | goto no_delete; |
5f39d397 | 5508 | |
7ab7956e | 5509 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5510 | |
7b40b695 | 5511 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5512 | goto no_delete; |
c71bf099 | 5513 | |
76dda93c | 5514 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5515 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5516 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5517 | goto no_delete; |
5518 | } | |
5519 | ||
aa79021f | 5520 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5521 | if (ret) |
0e8c36a9 | 5522 | goto no_delete; |
0e8c36a9 | 5523 | |
2ff7e61e | 5524 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5525 | if (!rsv) |
4289a667 | 5526 | goto no_delete; |
2bd36e7b | 5527 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
ca7e70f5 | 5528 | rsv->failfast = 1; |
4289a667 | 5529 | |
6ef06d27 | 5530 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5531 | |
8082510e | 5532 | while (1) { |
ad80cf50 | 5533 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5534 | if (IS_ERR(trans)) |
5535 | goto free_rsv; | |
7b128766 | 5536 | |
4289a667 JB |
5537 | trans->block_rsv = rsv; |
5538 | ||
d68fc57b | 5539 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
27919067 OS |
5540 | trans->block_rsv = &fs_info->trans_block_rsv; |
5541 | btrfs_end_transaction(trans); | |
5542 | btrfs_btree_balance_dirty(fs_info); | |
5543 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5544 | goto free_rsv; | |
5545 | else if (!ret) | |
8082510e | 5546 | break; |
8082510e | 5547 | } |
5f39d397 | 5548 | |
4ef31a45 | 5549 | /* |
27919067 OS |
5550 | * Errors here aren't a big deal, it just means we leave orphan items in |
5551 | * the tree. They will be cleaned up on the next mount. If the inode | |
5552 | * number gets reused, cleanup deletes the orphan item without doing | |
5553 | * anything, and unlink reuses the existing orphan item. | |
5554 | * | |
5555 | * If it turns out that we are dropping too many of these, we might want | |
5556 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5557 | */ |
ad80cf50 | 5558 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5559 | if (!IS_ERR(trans)) { |
5560 | trans->block_rsv = rsv; | |
5561 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5562 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5563 | btrfs_end_transaction(trans); | |
5564 | } | |
54aa1f4d | 5565 | |
0b246afa | 5566 | if (!(root == fs_info->tree_root || |
581bb050 | 5567 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5568 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5569 | |
27919067 OS |
5570 | free_rsv: |
5571 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5572 | no_delete: |
27919067 OS |
5573 | /* |
5574 | * If we didn't successfully delete, the orphan item will still be in | |
5575 | * the tree and we'll retry on the next mount. Again, we might also want | |
5576 | * to retry these periodically in the future. | |
5577 | */ | |
f48d1cf5 | 5578 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5579 | clear_inode(inode); |
39279cc3 CM |
5580 | } |
5581 | ||
5582 | /* | |
6bf9e4bd QW |
5583 | * Return the key found in the dir entry in the location pointer, fill @type |
5584 | * with BTRFS_FT_*, and return 0. | |
5585 | * | |
005d6712 SY |
5586 | * If no dir entries were found, returns -ENOENT. |
5587 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5588 | */ |
5589 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
6bf9e4bd | 5590 | struct btrfs_key *location, u8 *type) |
39279cc3 CM |
5591 | { |
5592 | const char *name = dentry->d_name.name; | |
5593 | int namelen = dentry->d_name.len; | |
5594 | struct btrfs_dir_item *di; | |
5595 | struct btrfs_path *path; | |
5596 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5597 | int ret = 0; |
39279cc3 CM |
5598 | |
5599 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5600 | if (!path) |
5601 | return -ENOMEM; | |
3954401f | 5602 | |
f85b7379 DS |
5603 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5604 | name, namelen, 0); | |
3cf5068f LB |
5605 | if (IS_ERR_OR_NULL(di)) { |
5606 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5607 | goto out; |
5608 | } | |
d397712b | 5609 | |
5f39d397 | 5610 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5611 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5612 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5613 | ret = -EUCLEAN; |
56a0e706 LB |
5614 | btrfs_warn(root->fs_info, |
5615 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5616 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5617 | location->objectid, location->type, location->offset); | |
56a0e706 | 5618 | } |
6bf9e4bd QW |
5619 | if (!ret) |
5620 | *type = btrfs_dir_type(path->nodes[0], di); | |
39279cc3 | 5621 | out: |
39279cc3 CM |
5622 | btrfs_free_path(path); |
5623 | return ret; | |
5624 | } | |
5625 | ||
5626 | /* | |
5627 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5628 | * needs to be changed to reflect the root directory of the tree root. This | |
5629 | * is kind of like crossing a mount point. | |
5630 | */ | |
2ff7e61e | 5631 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5632 | struct inode *dir, |
5633 | struct dentry *dentry, | |
5634 | struct btrfs_key *location, | |
5635 | struct btrfs_root **sub_root) | |
39279cc3 | 5636 | { |
4df27c4d YZ |
5637 | struct btrfs_path *path; |
5638 | struct btrfs_root *new_root; | |
5639 | struct btrfs_root_ref *ref; | |
5640 | struct extent_buffer *leaf; | |
1d4c08e0 | 5641 | struct btrfs_key key; |
4df27c4d YZ |
5642 | int ret; |
5643 | int err = 0; | |
39279cc3 | 5644 | |
4df27c4d YZ |
5645 | path = btrfs_alloc_path(); |
5646 | if (!path) { | |
5647 | err = -ENOMEM; | |
5648 | goto out; | |
5649 | } | |
39279cc3 | 5650 | |
4df27c4d | 5651 | err = -ENOENT; |
1d4c08e0 DS |
5652 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5653 | key.type = BTRFS_ROOT_REF_KEY; | |
5654 | key.offset = location->objectid; | |
5655 | ||
0b246afa | 5656 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5657 | if (ret) { |
5658 | if (ret < 0) | |
5659 | err = ret; | |
5660 | goto out; | |
5661 | } | |
39279cc3 | 5662 | |
4df27c4d YZ |
5663 | leaf = path->nodes[0]; |
5664 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5665 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5666 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5667 | goto out; | |
39279cc3 | 5668 | |
4df27c4d YZ |
5669 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5670 | (unsigned long)(ref + 1), | |
5671 | dentry->d_name.len); | |
5672 | if (ret) | |
5673 | goto out; | |
5674 | ||
b3b4aa74 | 5675 | btrfs_release_path(path); |
4df27c4d | 5676 | |
0b246afa | 5677 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5678 | if (IS_ERR(new_root)) { |
5679 | err = PTR_ERR(new_root); | |
5680 | goto out; | |
5681 | } | |
5682 | ||
4df27c4d YZ |
5683 | *sub_root = new_root; |
5684 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5685 | location->type = BTRFS_INODE_ITEM_KEY; | |
5686 | location->offset = 0; | |
5687 | err = 0; | |
5688 | out: | |
5689 | btrfs_free_path(path); | |
5690 | return err; | |
39279cc3 CM |
5691 | } |
5692 | ||
5d4f98a2 YZ |
5693 | static void inode_tree_add(struct inode *inode) |
5694 | { | |
5695 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5696 | struct btrfs_inode *entry; | |
03e860bd NP |
5697 | struct rb_node **p; |
5698 | struct rb_node *parent; | |
cef21937 | 5699 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5700 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5701 | |
1d3382cb | 5702 | if (inode_unhashed(inode)) |
76dda93c | 5703 | return; |
e1409cef | 5704 | parent = NULL; |
5d4f98a2 | 5705 | spin_lock(&root->inode_lock); |
e1409cef | 5706 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5707 | while (*p) { |
5708 | parent = *p; | |
5709 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5710 | ||
37508515 | 5711 | if (ino < btrfs_ino(entry)) |
03e860bd | 5712 | p = &parent->rb_left; |
37508515 | 5713 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5714 | p = &parent->rb_right; |
5d4f98a2 YZ |
5715 | else { |
5716 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5717 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5718 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5719 | RB_CLEAR_NODE(parent); |
5720 | spin_unlock(&root->inode_lock); | |
cef21937 | 5721 | return; |
5d4f98a2 YZ |
5722 | } |
5723 | } | |
cef21937 FDBM |
5724 | rb_link_node(new, parent, p); |
5725 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5726 | spin_unlock(&root->inode_lock); |
5727 | } | |
5728 | ||
5729 | static void inode_tree_del(struct inode *inode) | |
5730 | { | |
5731 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5732 | int empty = 0; |
5d4f98a2 | 5733 | |
03e860bd | 5734 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5735 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5736 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5737 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5738 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5739 | } |
03e860bd | 5740 | spin_unlock(&root->inode_lock); |
76dda93c | 5741 | |
69e9c6c6 | 5742 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5743 | spin_lock(&root->inode_lock); |
5744 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5745 | spin_unlock(&root->inode_lock); | |
5746 | if (empty) | |
5747 | btrfs_add_dead_root(root); | |
5748 | } | |
5749 | } | |
5750 | ||
5d4f98a2 | 5751 | |
e02119d5 CM |
5752 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5753 | { | |
5754 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5755 | inode->i_ino = args->location->objectid; |
5756 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5757 | sizeof(*args->location)); | |
e02119d5 | 5758 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5759 | return 0; |
5760 | } | |
5761 | ||
5762 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5763 | { | |
5764 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5765 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5766 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5767 | } |
5768 | ||
5d4f98a2 | 5769 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5770 | struct btrfs_key *location, |
5d4f98a2 | 5771 | struct btrfs_root *root) |
39279cc3 CM |
5772 | { |
5773 | struct inode *inode; | |
5774 | struct btrfs_iget_args args; | |
90d3e592 | 5775 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5776 | |
90d3e592 | 5777 | args.location = location; |
39279cc3 CM |
5778 | args.root = root; |
5779 | ||
778ba82b | 5780 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5781 | btrfs_init_locked_inode, |
5782 | (void *)&args); | |
5783 | return inode; | |
5784 | } | |
5785 | ||
4c66e0d4 DS |
5786 | /* |
5787 | * Get an inode object given its location and corresponding root. | |
5788 | * Path can be preallocated to prevent recursing back to iget through | |
5789 | * allocator. NULL is also valid but may require an additional allocation | |
5790 | * later. | |
1a54ef8c | 5791 | */ |
4222ea71 | 5792 | struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location, |
4c66e0d4 | 5793 | struct btrfs_root *root, struct btrfs_path *path) |
1a54ef8c BR |
5794 | { |
5795 | struct inode *inode; | |
5796 | ||
90d3e592 | 5797 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5798 | if (!inode) |
5d4f98a2 | 5799 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5800 | |
5801 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5802 | int ret; |
5803 | ||
4222ea71 | 5804 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5805 | if (!ret) { |
1748f843 MF |
5806 | inode_tree_add(inode); |
5807 | unlock_new_inode(inode); | |
1748f843 | 5808 | } else { |
f5b3a417 AV |
5809 | iget_failed(inode); |
5810 | /* | |
5811 | * ret > 0 can come from btrfs_search_slot called by | |
5812 | * btrfs_read_locked_inode, this means the inode item | |
5813 | * was not found. | |
5814 | */ | |
5815 | if (ret > 0) | |
5816 | ret = -ENOENT; | |
5817 | inode = ERR_PTR(ret); | |
1748f843 MF |
5818 | } |
5819 | } | |
5820 | ||
1a54ef8c BR |
5821 | return inode; |
5822 | } | |
5823 | ||
4222ea71 | 5824 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
4c66e0d4 | 5825 | struct btrfs_root *root) |
4222ea71 | 5826 | { |
4c66e0d4 | 5827 | return btrfs_iget_path(s, location, root, NULL); |
4222ea71 FM |
5828 | } |
5829 | ||
4df27c4d YZ |
5830 | static struct inode *new_simple_dir(struct super_block *s, |
5831 | struct btrfs_key *key, | |
5832 | struct btrfs_root *root) | |
5833 | { | |
5834 | struct inode *inode = new_inode(s); | |
5835 | ||
5836 | if (!inode) | |
5837 | return ERR_PTR(-ENOMEM); | |
5838 | ||
4df27c4d YZ |
5839 | BTRFS_I(inode)->root = root; |
5840 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5841 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5842 | |
5843 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5844 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5845 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5846 | inode->i_fop = &simple_dir_operations; |
5847 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5848 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5849 | inode->i_atime = inode->i_mtime; |
5850 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5851 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5852 | |
5853 | return inode; | |
5854 | } | |
5855 | ||
6bf9e4bd QW |
5856 | static inline u8 btrfs_inode_type(struct inode *inode) |
5857 | { | |
5858 | /* | |
5859 | * Compile-time asserts that generic FT_* types still match | |
5860 | * BTRFS_FT_* types | |
5861 | */ | |
5862 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); | |
5863 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); | |
5864 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); | |
5865 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); | |
5866 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); | |
5867 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); | |
5868 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); | |
5869 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); | |
5870 | ||
5871 | return fs_umode_to_ftype(inode->i_mode); | |
5872 | } | |
5873 | ||
3de4586c | 5874 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5875 | { |
0b246afa | 5876 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5877 | struct inode *inode; |
4df27c4d | 5878 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5879 | struct btrfs_root *sub_root = root; |
5880 | struct btrfs_key location; | |
6bf9e4bd | 5881 | u8 di_type = 0; |
76dda93c | 5882 | int index; |
b4aff1f8 | 5883 | int ret = 0; |
39279cc3 CM |
5884 | |
5885 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5886 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5887 | |
6bf9e4bd | 5888 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
39279cc3 CM |
5889 | if (ret < 0) |
5890 | return ERR_PTR(ret); | |
5f39d397 | 5891 | |
4df27c4d | 5892 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
4c66e0d4 | 5893 | inode = btrfs_iget(dir->i_sb, &location, root); |
6bf9e4bd QW |
5894 | if (IS_ERR(inode)) |
5895 | return inode; | |
5896 | ||
5897 | /* Do extra check against inode mode with di_type */ | |
5898 | if (btrfs_inode_type(inode) != di_type) { | |
5899 | btrfs_crit(fs_info, | |
5900 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", | |
5901 | inode->i_mode, btrfs_inode_type(inode), | |
5902 | di_type); | |
5903 | iput(inode); | |
5904 | return ERR_PTR(-EUCLEAN); | |
5905 | } | |
4df27c4d YZ |
5906 | return inode; |
5907 | } | |
5908 | ||
0b246afa | 5909 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5910 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5911 | &location, &sub_root); |
5912 | if (ret < 0) { | |
5913 | if (ret != -ENOENT) | |
5914 | inode = ERR_PTR(ret); | |
5915 | else | |
5916 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5917 | } else { | |
4c66e0d4 | 5918 | inode = btrfs_iget(dir->i_sb, &location, sub_root); |
39279cc3 | 5919 | } |
0b246afa | 5920 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5921 | |
34d19bad | 5922 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5923 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5924 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5925 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5926 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5927 | if (ret) { |
5928 | iput(inode); | |
66b4ffd1 | 5929 | inode = ERR_PTR(ret); |
01cd3367 | 5930 | } |
c71bf099 YZ |
5931 | } |
5932 | ||
3de4586c CM |
5933 | return inode; |
5934 | } | |
5935 | ||
fe15ce44 | 5936 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5937 | { |
5938 | struct btrfs_root *root; | |
2b0143b5 | 5939 | struct inode *inode = d_inode(dentry); |
76dda93c | 5940 | |
848cce0d | 5941 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5942 | inode = d_inode(dentry->d_parent); |
76dda93c | 5943 | |
848cce0d LZ |
5944 | if (inode) { |
5945 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5946 | if (btrfs_root_refs(&root->root_item) == 0) |
5947 | return 1; | |
848cce0d | 5948 | |
4a0cc7ca | 5949 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5950 | return 1; |
efefb143 | 5951 | } |
76dda93c YZ |
5952 | return 0; |
5953 | } | |
5954 | ||
3de4586c | 5955 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5956 | unsigned int flags) |
3de4586c | 5957 | { |
3837d208 | 5958 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5959 | |
3837d208 AV |
5960 | if (inode == ERR_PTR(-ENOENT)) |
5961 | inode = NULL; | |
41d28bca | 5962 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5963 | } |
5964 | ||
23b5ec74 JB |
5965 | /* |
5966 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5967 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5968 | * our information into that, and then dir_emit from the buffer. This is | |
5969 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5970 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5971 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5972 | * tree lock. | |
5973 | */ | |
5974 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5975 | { | |
5976 | struct btrfs_file_private *private; | |
5977 | ||
5978 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5979 | if (!private) | |
5980 | return -ENOMEM; | |
5981 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5982 | if (!private->filldir_buf) { | |
5983 | kfree(private); | |
5984 | return -ENOMEM; | |
5985 | } | |
5986 | file->private_data = private; | |
5987 | return 0; | |
5988 | } | |
5989 | ||
5990 | struct dir_entry { | |
5991 | u64 ino; | |
5992 | u64 offset; | |
5993 | unsigned type; | |
5994 | int name_len; | |
5995 | }; | |
5996 | ||
5997 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5998 | { | |
5999 | while (entries--) { | |
6000 | struct dir_entry *entry = addr; | |
6001 | char *name = (char *)(entry + 1); | |
6002 | ||
92d32170 DS |
6003 | ctx->pos = get_unaligned(&entry->offset); |
6004 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
6005 | get_unaligned(&entry->ino), | |
6006 | get_unaligned(&entry->type))) | |
23b5ec74 | 6007 | return 1; |
92d32170 DS |
6008 | addr += sizeof(struct dir_entry) + |
6009 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
6010 | ctx->pos++; |
6011 | } | |
6012 | return 0; | |
6013 | } | |
6014 | ||
9cdda8d3 | 6015 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 6016 | { |
9cdda8d3 | 6017 | struct inode *inode = file_inode(file); |
39279cc3 | 6018 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 6019 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
6020 | struct btrfs_dir_item *di; |
6021 | struct btrfs_key key; | |
5f39d397 | 6022 | struct btrfs_key found_key; |
39279cc3 | 6023 | struct btrfs_path *path; |
23b5ec74 | 6024 | void *addr; |
16cdcec7 MX |
6025 | struct list_head ins_list; |
6026 | struct list_head del_list; | |
39279cc3 | 6027 | int ret; |
5f39d397 | 6028 | struct extent_buffer *leaf; |
39279cc3 | 6029 | int slot; |
5f39d397 CM |
6030 | char *name_ptr; |
6031 | int name_len; | |
23b5ec74 JB |
6032 | int entries = 0; |
6033 | int total_len = 0; | |
02dbfc99 | 6034 | bool put = false; |
c2951f32 | 6035 | struct btrfs_key location; |
5f39d397 | 6036 | |
9cdda8d3 AV |
6037 | if (!dir_emit_dots(file, ctx)) |
6038 | return 0; | |
6039 | ||
49593bfa | 6040 | path = btrfs_alloc_path(); |
16cdcec7 MX |
6041 | if (!path) |
6042 | return -ENOMEM; | |
ff5714cc | 6043 | |
23b5ec74 | 6044 | addr = private->filldir_buf; |
e4058b54 | 6045 | path->reada = READA_FORWARD; |
49593bfa | 6046 | |
c2951f32 JM |
6047 | INIT_LIST_HEAD(&ins_list); |
6048 | INIT_LIST_HEAD(&del_list); | |
6049 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 6050 | |
23b5ec74 | 6051 | again: |
c2951f32 | 6052 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 6053 | key.offset = ctx->pos; |
4a0cc7ca | 6054 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 6055 | |
39279cc3 CM |
6056 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6057 | if (ret < 0) | |
6058 | goto err; | |
49593bfa DW |
6059 | |
6060 | while (1) { | |
23b5ec74 JB |
6061 | struct dir_entry *entry; |
6062 | ||
5f39d397 | 6063 | leaf = path->nodes[0]; |
39279cc3 | 6064 | slot = path->slots[0]; |
b9e03af0 LZ |
6065 | if (slot >= btrfs_header_nritems(leaf)) { |
6066 | ret = btrfs_next_leaf(root, path); | |
6067 | if (ret < 0) | |
6068 | goto err; | |
6069 | else if (ret > 0) | |
6070 | break; | |
6071 | continue; | |
39279cc3 | 6072 | } |
3de4586c | 6073 | |
5f39d397 CM |
6074 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
6075 | ||
6076 | if (found_key.objectid != key.objectid) | |
39279cc3 | 6077 | break; |
c2951f32 | 6078 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 6079 | break; |
9cdda8d3 | 6080 | if (found_key.offset < ctx->pos) |
b9e03af0 | 6081 | goto next; |
c2951f32 | 6082 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 6083 | goto next; |
39279cc3 | 6084 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 6085 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
6086 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
6087 | PAGE_SIZE) { | |
6088 | btrfs_release_path(path); | |
6089 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6090 | if (ret) | |
6091 | goto nopos; | |
6092 | addr = private->filldir_buf; | |
6093 | entries = 0; | |
6094 | total_len = 0; | |
6095 | goto again; | |
c2951f32 | 6096 | } |
23b5ec74 JB |
6097 | |
6098 | entry = addr; | |
92d32170 | 6099 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 6100 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
6101 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
6102 | name_len); | |
7d157c3d | 6103 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
92d32170 | 6104 | &entry->type); |
c2951f32 | 6105 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
6106 | put_unaligned(location.objectid, &entry->ino); |
6107 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
6108 | entries++; |
6109 | addr += sizeof(struct dir_entry) + name_len; | |
6110 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
6111 | next: |
6112 | path->slots[0]++; | |
39279cc3 | 6113 | } |
23b5ec74 JB |
6114 | btrfs_release_path(path); |
6115 | ||
6116 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6117 | if (ret) | |
6118 | goto nopos; | |
49593bfa | 6119 | |
d2fbb2b5 | 6120 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 6121 | if (ret) |
bc4ef759 DS |
6122 | goto nopos; |
6123 | ||
db62efbb ZB |
6124 | /* |
6125 | * Stop new entries from being returned after we return the last | |
6126 | * entry. | |
6127 | * | |
6128 | * New directory entries are assigned a strictly increasing | |
6129 | * offset. This means that new entries created during readdir | |
6130 | * are *guaranteed* to be seen in the future by that readdir. | |
6131 | * This has broken buggy programs which operate on names as | |
6132 | * they're returned by readdir. Until we re-use freed offsets | |
6133 | * we have this hack to stop new entries from being returned | |
6134 | * under the assumption that they'll never reach this huge | |
6135 | * offset. | |
6136 | * | |
6137 | * This is being careful not to overflow 32bit loff_t unless the | |
6138 | * last entry requires it because doing so has broken 32bit apps | |
6139 | * in the past. | |
6140 | */ | |
c2951f32 JM |
6141 | if (ctx->pos >= INT_MAX) |
6142 | ctx->pos = LLONG_MAX; | |
6143 | else | |
6144 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6145 | nopos: |
6146 | ret = 0; | |
6147 | err: | |
02dbfc99 OS |
6148 | if (put) |
6149 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6150 | btrfs_free_path(path); |
39279cc3 CM |
6151 | return ret; |
6152 | } | |
6153 | ||
39279cc3 | 6154 | /* |
54aa1f4d | 6155 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6156 | * inode changes. But, it is most likely to find the inode in cache. |
6157 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6158 | * to keep or drop this code. | |
6159 | */ | |
48a3b636 | 6160 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6161 | { |
2ff7e61e | 6162 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6163 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6164 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6165 | int ret; |
6166 | ||
72ac3c0d | 6167 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6168 | return 0; |
39279cc3 | 6169 | |
7a7eaa40 | 6170 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6171 | if (IS_ERR(trans)) |
6172 | return PTR_ERR(trans); | |
8929ecfa YZ |
6173 | |
6174 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6175 | if (ret && ret == -ENOSPC) { |
6176 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6177 | btrfs_end_transaction(trans); |
94b60442 | 6178 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6179 | if (IS_ERR(trans)) |
6180 | return PTR_ERR(trans); | |
8929ecfa | 6181 | |
94b60442 | 6182 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6183 | } |
3a45bb20 | 6184 | btrfs_end_transaction(trans); |
16cdcec7 | 6185 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6186 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6187 | |
6188 | return ret; | |
6189 | } | |
6190 | ||
6191 | /* | |
6192 | * This is a copy of file_update_time. We need this so we can return error on | |
6193 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6194 | */ | |
95582b00 | 6195 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 6196 | int flags) |
22c44fe6 | 6197 | { |
2bc55652 | 6198 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 6199 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
6200 | |
6201 | if (btrfs_root_readonly(root)) | |
6202 | return -EROFS; | |
6203 | ||
e41f941a | 6204 | if (flags & S_VERSION) |
3a8c7231 | 6205 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
6206 | if (flags & S_CTIME) |
6207 | inode->i_ctime = *now; | |
6208 | if (flags & S_MTIME) | |
6209 | inode->i_mtime = *now; | |
6210 | if (flags & S_ATIME) | |
6211 | inode->i_atime = *now; | |
3a8c7231 | 6212 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
6213 | } |
6214 | ||
d352ac68 CM |
6215 | /* |
6216 | * find the highest existing sequence number in a directory | |
6217 | * and then set the in-memory index_cnt variable to reflect | |
6218 | * free sequence numbers | |
6219 | */ | |
4c570655 | 6220 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6221 | { |
4c570655 | 6222 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6223 | struct btrfs_key key, found_key; |
6224 | struct btrfs_path *path; | |
6225 | struct extent_buffer *leaf; | |
6226 | int ret; | |
6227 | ||
4c570655 | 6228 | key.objectid = btrfs_ino(inode); |
962a298f | 6229 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6230 | key.offset = (u64)-1; |
6231 | ||
6232 | path = btrfs_alloc_path(); | |
6233 | if (!path) | |
6234 | return -ENOMEM; | |
6235 | ||
6236 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6237 | if (ret < 0) | |
6238 | goto out; | |
6239 | /* FIXME: we should be able to handle this */ | |
6240 | if (ret == 0) | |
6241 | goto out; | |
6242 | ret = 0; | |
6243 | ||
6244 | /* | |
6245 | * MAGIC NUMBER EXPLANATION: | |
6246 | * since we search a directory based on f_pos we have to start at 2 | |
6247 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6248 | * else has to start at 2 | |
6249 | */ | |
6250 | if (path->slots[0] == 0) { | |
4c570655 | 6251 | inode->index_cnt = 2; |
aec7477b JB |
6252 | goto out; |
6253 | } | |
6254 | ||
6255 | path->slots[0]--; | |
6256 | ||
6257 | leaf = path->nodes[0]; | |
6258 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6259 | ||
4c570655 | 6260 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6261 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6262 | inode->index_cnt = 2; |
aec7477b JB |
6263 | goto out; |
6264 | } | |
6265 | ||
4c570655 | 6266 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6267 | out: |
6268 | btrfs_free_path(path); | |
6269 | return ret; | |
6270 | } | |
6271 | ||
d352ac68 CM |
6272 | /* |
6273 | * helper to find a free sequence number in a given directory. This current | |
6274 | * code is very simple, later versions will do smarter things in the btree | |
6275 | */ | |
877574e2 | 6276 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6277 | { |
6278 | int ret = 0; | |
6279 | ||
877574e2 NB |
6280 | if (dir->index_cnt == (u64)-1) { |
6281 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6282 | if (ret) { |
6283 | ret = btrfs_set_inode_index_count(dir); | |
6284 | if (ret) | |
6285 | return ret; | |
6286 | } | |
aec7477b JB |
6287 | } |
6288 | ||
877574e2 NB |
6289 | *index = dir->index_cnt; |
6290 | dir->index_cnt++; | |
aec7477b JB |
6291 | |
6292 | return ret; | |
6293 | } | |
6294 | ||
b0d5d10f CM |
6295 | static int btrfs_insert_inode_locked(struct inode *inode) |
6296 | { | |
6297 | struct btrfs_iget_args args; | |
6298 | args.location = &BTRFS_I(inode)->location; | |
6299 | args.root = BTRFS_I(inode)->root; | |
6300 | ||
6301 | return insert_inode_locked4(inode, | |
6302 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6303 | btrfs_find_actor, &args); | |
6304 | } | |
6305 | ||
19aee8de AJ |
6306 | /* |
6307 | * Inherit flags from the parent inode. | |
6308 | * | |
6309 | * Currently only the compression flags and the cow flags are inherited. | |
6310 | */ | |
6311 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6312 | { | |
6313 | unsigned int flags; | |
6314 | ||
6315 | if (!dir) | |
6316 | return; | |
6317 | ||
6318 | flags = BTRFS_I(dir)->flags; | |
6319 | ||
6320 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6321 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6322 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6323 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6324 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6325 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6326 | } | |
6327 | ||
6328 | if (flags & BTRFS_INODE_NODATACOW) { | |
6329 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6330 | if (S_ISREG(inode->i_mode)) | |
6331 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6332 | } | |
6333 | ||
7b6a221e | 6334 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
6335 | } |
6336 | ||
39279cc3 CM |
6337 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6338 | struct btrfs_root *root, | |
aec7477b | 6339 | struct inode *dir, |
9c58309d | 6340 | const char *name, int name_len, |
175a4eb7 AV |
6341 | u64 ref_objectid, u64 objectid, |
6342 | umode_t mode, u64 *index) | |
39279cc3 | 6343 | { |
0b246afa | 6344 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6345 | struct inode *inode; |
5f39d397 | 6346 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6347 | struct btrfs_key *location; |
5f39d397 | 6348 | struct btrfs_path *path; |
9c58309d CM |
6349 | struct btrfs_inode_ref *ref; |
6350 | struct btrfs_key key[2]; | |
6351 | u32 sizes[2]; | |
ef3b9af5 | 6352 | int nitems = name ? 2 : 1; |
9c58309d | 6353 | unsigned long ptr; |
11a19a90 | 6354 | unsigned int nofs_flag; |
39279cc3 | 6355 | int ret; |
39279cc3 | 6356 | |
5f39d397 | 6357 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6358 | if (!path) |
6359 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6360 | |
11a19a90 | 6361 | nofs_flag = memalloc_nofs_save(); |
0b246afa | 6362 | inode = new_inode(fs_info->sb); |
11a19a90 | 6363 | memalloc_nofs_restore(nofs_flag); |
8fb27640 YS |
6364 | if (!inode) { |
6365 | btrfs_free_path(path); | |
39279cc3 | 6366 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6367 | } |
39279cc3 | 6368 | |
5762b5c9 FM |
6369 | /* |
6370 | * O_TMPFILE, set link count to 0, so that after this point, | |
6371 | * we fill in an inode item with the correct link count. | |
6372 | */ | |
6373 | if (!name) | |
6374 | set_nlink(inode, 0); | |
6375 | ||
581bb050 LZ |
6376 | /* |
6377 | * we have to initialize this early, so we can reclaim the inode | |
6378 | * number if we fail afterwards in this function. | |
6379 | */ | |
6380 | inode->i_ino = objectid; | |
6381 | ||
ef3b9af5 | 6382 | if (dir && name) { |
1abe9b8a | 6383 | trace_btrfs_inode_request(dir); |
6384 | ||
877574e2 | 6385 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6386 | if (ret) { |
8fb27640 | 6387 | btrfs_free_path(path); |
09771430 | 6388 | iput(inode); |
aec7477b | 6389 | return ERR_PTR(ret); |
09771430 | 6390 | } |
ef3b9af5 FM |
6391 | } else if (dir) { |
6392 | *index = 0; | |
aec7477b JB |
6393 | } |
6394 | /* | |
6395 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 6396 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
6397 | * number |
6398 | */ | |
6399 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6400 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6401 | BTRFS_I(inode)->root = root; |
e02119d5 | 6402 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6403 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6404 | |
5dc562c5 JB |
6405 | /* |
6406 | * We could have gotten an inode number from somebody who was fsynced | |
6407 | * and then removed in this same transaction, so let's just set full | |
6408 | * sync since it will be a full sync anyway and this will blow away the | |
6409 | * old info in the log. | |
6410 | */ | |
6411 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6412 | ||
9c58309d | 6413 | key[0].objectid = objectid; |
962a298f | 6414 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6415 | key[0].offset = 0; |
6416 | ||
9c58309d | 6417 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6418 | |
6419 | if (name) { | |
6420 | /* | |
6421 | * Start new inodes with an inode_ref. This is slightly more | |
6422 | * efficient for small numbers of hard links since they will | |
6423 | * be packed into one item. Extended refs will kick in if we | |
6424 | * add more hard links than can fit in the ref item. | |
6425 | */ | |
6426 | key[1].objectid = objectid; | |
962a298f | 6427 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6428 | key[1].offset = ref_objectid; |
6429 | ||
6430 | sizes[1] = name_len + sizeof(*ref); | |
6431 | } | |
9c58309d | 6432 | |
b0d5d10f CM |
6433 | location = &BTRFS_I(inode)->location; |
6434 | location->objectid = objectid; | |
6435 | location->offset = 0; | |
962a298f | 6436 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6437 | |
6438 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6439 | if (ret < 0) { |
6440 | iput(inode); | |
b0d5d10f | 6441 | goto fail; |
32955c54 | 6442 | } |
b0d5d10f | 6443 | |
b9473439 | 6444 | path->leave_spinning = 1; |
ef3b9af5 | 6445 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6446 | if (ret != 0) |
b0d5d10f | 6447 | goto fail_unlock; |
5f39d397 | 6448 | |
ecc11fab | 6449 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6450 | inode_set_bytes(inode, 0); |
9cc97d64 | 6451 | |
c2050a45 | 6452 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6453 | inode->i_atime = inode->i_mtime; |
6454 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6455 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6456 | |
5f39d397 CM |
6457 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6458 | struct btrfs_inode_item); | |
b159fa28 | 6459 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6460 | sizeof(*inode_item)); |
e02119d5 | 6461 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6462 | |
ef3b9af5 FM |
6463 | if (name) { |
6464 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6465 | struct btrfs_inode_ref); | |
6466 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6467 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6468 | ptr = (unsigned long)(ref + 1); | |
6469 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6470 | } | |
9c58309d | 6471 | |
5f39d397 CM |
6472 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6473 | btrfs_free_path(path); | |
6474 | ||
6cbff00f CH |
6475 | btrfs_inherit_iflags(inode, dir); |
6476 | ||
569254b0 | 6477 | if (S_ISREG(mode)) { |
0b246afa | 6478 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6479 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6480 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6481 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6482 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6483 | } |
6484 | ||
5d4f98a2 | 6485 | inode_tree_add(inode); |
1abe9b8a | 6486 | |
6487 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6488 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6489 | |
8ea05e3a AB |
6490 | btrfs_update_root_times(trans, root); |
6491 | ||
63541927 FDBM |
6492 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6493 | if (ret) | |
0b246afa | 6494 | btrfs_err(fs_info, |
63541927 | 6495 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6496 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6497 | |
39279cc3 | 6498 | return inode; |
b0d5d10f CM |
6499 | |
6500 | fail_unlock: | |
32955c54 | 6501 | discard_new_inode(inode); |
5f39d397 | 6502 | fail: |
ef3b9af5 | 6503 | if (dir && name) |
aec7477b | 6504 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6505 | btrfs_free_path(path); |
6506 | return ERR_PTR(ret); | |
39279cc3 CM |
6507 | } |
6508 | ||
d352ac68 CM |
6509 | /* |
6510 | * utility function to add 'inode' into 'parent_inode' with | |
6511 | * a give name and a given sequence number. | |
6512 | * if 'add_backref' is true, also insert a backref from the | |
6513 | * inode to the parent directory. | |
6514 | */ | |
e02119d5 | 6515 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6516 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6517 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6518 | { |
4df27c4d | 6519 | int ret = 0; |
39279cc3 | 6520 | struct btrfs_key key; |
db0a669f NB |
6521 | struct btrfs_root *root = parent_inode->root; |
6522 | u64 ino = btrfs_ino(inode); | |
6523 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6524 | |
33345d01 | 6525 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6526 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6527 | } else { |
33345d01 | 6528 | key.objectid = ino; |
962a298f | 6529 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6530 | key.offset = 0; |
6531 | } | |
6532 | ||
33345d01 | 6533 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6534 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6535 | root->root_key.objectid, parent_ino, |
6536 | index, name, name_len); | |
4df27c4d | 6537 | } else if (add_backref) { |
33345d01 LZ |
6538 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6539 | parent_ino, index); | |
4df27c4d | 6540 | } |
39279cc3 | 6541 | |
79787eaa JM |
6542 | /* Nothing to clean up yet */ |
6543 | if (ret) | |
6544 | return ret; | |
4df27c4d | 6545 | |
684572df | 6546 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6547 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6548 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6549 | goto fail_dir_item; |
6550 | else if (ret) { | |
66642832 | 6551 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6552 | return ret; |
39279cc3 | 6553 | } |
79787eaa | 6554 | |
db0a669f | 6555 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6556 | name_len * 2); |
db0a669f | 6557 | inode_inc_iversion(&parent_inode->vfs_inode); |
5338e43a FM |
6558 | /* |
6559 | * If we are replaying a log tree, we do not want to update the mtime | |
6560 | * and ctime of the parent directory with the current time, since the | |
6561 | * log replay procedure is responsible for setting them to their correct | |
6562 | * values (the ones it had when the fsync was done). | |
6563 | */ | |
6564 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { | |
6565 | struct timespec64 now = current_time(&parent_inode->vfs_inode); | |
6566 | ||
6567 | parent_inode->vfs_inode.i_mtime = now; | |
6568 | parent_inode->vfs_inode.i_ctime = now; | |
6569 | } | |
db0a669f | 6570 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
79787eaa | 6571 | if (ret) |
66642832 | 6572 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6573 | return ret; |
fe66a05a CM |
6574 | |
6575 | fail_dir_item: | |
6576 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6577 | u64 local_index; | |
6578 | int err; | |
3ee1c553 | 6579 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6580 | root->root_key.objectid, parent_ino, |
6581 | &local_index, name, name_len); | |
1690dd41 JT |
6582 | if (err) |
6583 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6584 | } else if (add_backref) { |
6585 | u64 local_index; | |
6586 | int err; | |
6587 | ||
6588 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6589 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6590 | if (err) |
6591 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6592 | } |
1690dd41 JT |
6593 | |
6594 | /* Return the original error code */ | |
fe66a05a | 6595 | return ret; |
39279cc3 CM |
6596 | } |
6597 | ||
6598 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6599 | struct btrfs_inode *dir, struct dentry *dentry, |
6600 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6601 | { |
a1b075d2 JB |
6602 | int err = btrfs_add_link(trans, dir, inode, |
6603 | dentry->d_name.name, dentry->d_name.len, | |
6604 | backref, index); | |
39279cc3 CM |
6605 | if (err > 0) |
6606 | err = -EEXIST; | |
6607 | return err; | |
6608 | } | |
6609 | ||
618e21d5 | 6610 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6611 | umode_t mode, dev_t rdev) |
618e21d5 | 6612 | { |
2ff7e61e | 6613 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6614 | struct btrfs_trans_handle *trans; |
6615 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6616 | struct inode *inode = NULL; |
618e21d5 | 6617 | int err; |
618e21d5 | 6618 | u64 objectid; |
00e4e6b3 | 6619 | u64 index = 0; |
618e21d5 | 6620 | |
9ed74f2d JB |
6621 | /* |
6622 | * 2 for inode item and ref | |
6623 | * 2 for dir items | |
6624 | * 1 for xattr if selinux is on | |
6625 | */ | |
a22285a6 YZ |
6626 | trans = btrfs_start_transaction(root, 5); |
6627 | if (IS_ERR(trans)) | |
6628 | return PTR_ERR(trans); | |
1832a6d5 | 6629 | |
581bb050 LZ |
6630 | err = btrfs_find_free_ino(root, &objectid); |
6631 | if (err) | |
6632 | goto out_unlock; | |
6633 | ||
aec7477b | 6634 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6635 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6636 | mode, &index); | |
7cf96da3 TI |
6637 | if (IS_ERR(inode)) { |
6638 | err = PTR_ERR(inode); | |
32955c54 | 6639 | inode = NULL; |
618e21d5 | 6640 | goto out_unlock; |
7cf96da3 | 6641 | } |
618e21d5 | 6642 | |
ad19db71 CS |
6643 | /* |
6644 | * If the active LSM wants to access the inode during | |
6645 | * d_instantiate it needs these. Smack checks to see | |
6646 | * if the filesystem supports xattrs by looking at the | |
6647 | * ops vector. | |
6648 | */ | |
ad19db71 | 6649 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6650 | init_special_inode(inode, inode->i_mode, rdev); |
6651 | ||
6652 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6653 | if (err) |
32955c54 | 6654 | goto out_unlock; |
b0d5d10f | 6655 | |
cef415af NB |
6656 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6657 | 0, index); | |
32955c54 AV |
6658 | if (err) |
6659 | goto out_unlock; | |
6660 | ||
6661 | btrfs_update_inode(trans, root, inode); | |
6662 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6663 | |
618e21d5 | 6664 | out_unlock: |
3a45bb20 | 6665 | btrfs_end_transaction(trans); |
2ff7e61e | 6666 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6667 | if (err && inode) { |
618e21d5 | 6668 | inode_dec_link_count(inode); |
32955c54 | 6669 | discard_new_inode(inode); |
618e21d5 | 6670 | } |
618e21d5 JB |
6671 | return err; |
6672 | } | |
6673 | ||
39279cc3 | 6674 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6675 | umode_t mode, bool excl) |
39279cc3 | 6676 | { |
2ff7e61e | 6677 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6678 | struct btrfs_trans_handle *trans; |
6679 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6680 | struct inode *inode = NULL; |
a22285a6 | 6681 | int err; |
39279cc3 | 6682 | u64 objectid; |
00e4e6b3 | 6683 | u64 index = 0; |
39279cc3 | 6684 | |
9ed74f2d JB |
6685 | /* |
6686 | * 2 for inode item and ref | |
6687 | * 2 for dir items | |
6688 | * 1 for xattr if selinux is on | |
6689 | */ | |
a22285a6 YZ |
6690 | trans = btrfs_start_transaction(root, 5); |
6691 | if (IS_ERR(trans)) | |
6692 | return PTR_ERR(trans); | |
9ed74f2d | 6693 | |
581bb050 LZ |
6694 | err = btrfs_find_free_ino(root, &objectid); |
6695 | if (err) | |
6696 | goto out_unlock; | |
6697 | ||
aec7477b | 6698 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6699 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6700 | mode, &index); | |
7cf96da3 TI |
6701 | if (IS_ERR(inode)) { |
6702 | err = PTR_ERR(inode); | |
32955c54 | 6703 | inode = NULL; |
39279cc3 | 6704 | goto out_unlock; |
7cf96da3 | 6705 | } |
ad19db71 CS |
6706 | /* |
6707 | * If the active LSM wants to access the inode during | |
6708 | * d_instantiate it needs these. Smack checks to see | |
6709 | * if the filesystem supports xattrs by looking at the | |
6710 | * ops vector. | |
6711 | */ | |
6712 | inode->i_fop = &btrfs_file_operations; | |
6713 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6714 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6715 | |
6716 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6717 | if (err) | |
32955c54 | 6718 | goto out_unlock; |
b0d5d10f CM |
6719 | |
6720 | err = btrfs_update_inode(trans, root, inode); | |
6721 | if (err) | |
32955c54 | 6722 | goto out_unlock; |
ad19db71 | 6723 | |
cef415af NB |
6724 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6725 | 0, index); | |
39279cc3 | 6726 | if (err) |
32955c54 | 6727 | goto out_unlock; |
43baa579 | 6728 | |
43baa579 | 6729 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
1e2e547a | 6730 | d_instantiate_new(dentry, inode); |
43baa579 | 6731 | |
39279cc3 | 6732 | out_unlock: |
3a45bb20 | 6733 | btrfs_end_transaction(trans); |
32955c54 | 6734 | if (err && inode) { |
39279cc3 | 6735 | inode_dec_link_count(inode); |
32955c54 | 6736 | discard_new_inode(inode); |
39279cc3 | 6737 | } |
2ff7e61e | 6738 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6739 | return err; |
6740 | } | |
6741 | ||
6742 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6743 | struct dentry *dentry) | |
6744 | { | |
271dba45 | 6745 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6746 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6747 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6748 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6749 | u64 index; |
39279cc3 CM |
6750 | int err; |
6751 | int drop_inode = 0; | |
6752 | ||
4a8be425 | 6753 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6754 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6755 | return -EXDEV; |
4a8be425 | 6756 | |
f186373f | 6757 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6758 | return -EMLINK; |
4a8be425 | 6759 | |
877574e2 | 6760 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6761 | if (err) |
6762 | goto fail; | |
6763 | ||
a22285a6 | 6764 | /* |
7e6b6465 | 6765 | * 2 items for inode and inode ref |
a22285a6 | 6766 | * 2 items for dir items |
7e6b6465 | 6767 | * 1 item for parent inode |
399b0bbf | 6768 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6769 | */ |
399b0bbf | 6770 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6771 | if (IS_ERR(trans)) { |
6772 | err = PTR_ERR(trans); | |
271dba45 | 6773 | trans = NULL; |
a22285a6 YZ |
6774 | goto fail; |
6775 | } | |
5f39d397 | 6776 | |
67de1176 MX |
6777 | /* There are several dir indexes for this inode, clear the cache. */ |
6778 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6779 | inc_nlink(inode); |
0c4d2d95 | 6780 | inode_inc_iversion(inode); |
c2050a45 | 6781 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6782 | ihold(inode); |
e9976151 | 6783 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6784 | |
cef415af NB |
6785 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6786 | 1, index); | |
5f39d397 | 6787 | |
a5719521 | 6788 | if (err) { |
54aa1f4d | 6789 | drop_inode = 1; |
a5719521 | 6790 | } else { |
10d9f309 | 6791 | struct dentry *parent = dentry->d_parent; |
d4682ba0 FM |
6792 | int ret; |
6793 | ||
a5719521 | 6794 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6795 | if (err) |
6796 | goto fail; | |
ef3b9af5 FM |
6797 | if (inode->i_nlink == 1) { |
6798 | /* | |
6799 | * If new hard link count is 1, it's a file created | |
6800 | * with open(2) O_TMPFILE flag. | |
6801 | */ | |
3d6ae7bb | 6802 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6803 | if (err) |
6804 | goto fail; | |
6805 | } | |
08c422c2 | 6806 | d_instantiate(dentry, inode); |
d4682ba0 FM |
6807 | ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent, |
6808 | true, NULL); | |
6809 | if (ret == BTRFS_NEED_TRANS_COMMIT) { | |
6810 | err = btrfs_commit_transaction(trans); | |
6811 | trans = NULL; | |
6812 | } | |
a5719521 | 6813 | } |
39279cc3 | 6814 | |
1832a6d5 | 6815 | fail: |
271dba45 | 6816 | if (trans) |
3a45bb20 | 6817 | btrfs_end_transaction(trans); |
39279cc3 CM |
6818 | if (drop_inode) { |
6819 | inode_dec_link_count(inode); | |
6820 | iput(inode); | |
6821 | } | |
2ff7e61e | 6822 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6823 | return err; |
6824 | } | |
6825 | ||
18bb1db3 | 6826 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6827 | { |
2ff7e61e | 6828 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6829 | struct inode *inode = NULL; |
39279cc3 CM |
6830 | struct btrfs_trans_handle *trans; |
6831 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6832 | int err = 0; | |
b9d86667 | 6833 | u64 objectid = 0; |
00e4e6b3 | 6834 | u64 index = 0; |
39279cc3 | 6835 | |
9ed74f2d JB |
6836 | /* |
6837 | * 2 items for inode and ref | |
6838 | * 2 items for dir items | |
6839 | * 1 for xattr if selinux is on | |
6840 | */ | |
a22285a6 YZ |
6841 | trans = btrfs_start_transaction(root, 5); |
6842 | if (IS_ERR(trans)) | |
6843 | return PTR_ERR(trans); | |
39279cc3 | 6844 | |
581bb050 LZ |
6845 | err = btrfs_find_free_ino(root, &objectid); |
6846 | if (err) | |
6847 | goto out_fail; | |
6848 | ||
aec7477b | 6849 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6850 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6851 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6852 | if (IS_ERR(inode)) { |
6853 | err = PTR_ERR(inode); | |
32955c54 | 6854 | inode = NULL; |
39279cc3 CM |
6855 | goto out_fail; |
6856 | } | |
5f39d397 | 6857 | |
b0d5d10f CM |
6858 | /* these must be set before we unlock the inode */ |
6859 | inode->i_op = &btrfs_dir_inode_operations; | |
6860 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6861 | |
2a7dba39 | 6862 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6863 | if (err) |
32955c54 | 6864 | goto out_fail; |
39279cc3 | 6865 | |
6ef06d27 | 6866 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6867 | err = btrfs_update_inode(trans, root, inode); |
6868 | if (err) | |
32955c54 | 6869 | goto out_fail; |
5f39d397 | 6870 | |
db0a669f NB |
6871 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6872 | dentry->d_name.name, | |
6873 | dentry->d_name.len, 0, index); | |
39279cc3 | 6874 | if (err) |
32955c54 | 6875 | goto out_fail; |
5f39d397 | 6876 | |
1e2e547a | 6877 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
6878 | |
6879 | out_fail: | |
3a45bb20 | 6880 | btrfs_end_transaction(trans); |
32955c54 | 6881 | if (err && inode) { |
c7cfb8a5 | 6882 | inode_dec_link_count(inode); |
32955c54 | 6883 | discard_new_inode(inode); |
c7cfb8a5 | 6884 | } |
2ff7e61e | 6885 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6886 | return err; |
6887 | } | |
6888 | ||
c8b97818 | 6889 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6890 | struct page *page, |
c8b97818 CM |
6891 | size_t pg_offset, u64 extent_offset, |
6892 | struct btrfs_file_extent_item *item) | |
6893 | { | |
6894 | int ret; | |
6895 | struct extent_buffer *leaf = path->nodes[0]; | |
6896 | char *tmp; | |
6897 | size_t max_size; | |
6898 | unsigned long inline_size; | |
6899 | unsigned long ptr; | |
261507a0 | 6900 | int compress_type; |
c8b97818 CM |
6901 | |
6902 | WARN_ON(pg_offset != 0); | |
261507a0 | 6903 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6904 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6905 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6906 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6907 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6908 | if (!tmp) |
6909 | return -ENOMEM; | |
c8b97818 CM |
6910 | ptr = btrfs_file_extent_inline_start(item); |
6911 | ||
6912 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6913 | ||
09cbfeaf | 6914 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6915 | ret = btrfs_decompress(compress_type, tmp, page, |
6916 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6917 | |
6918 | /* | |
6919 | * decompression code contains a memset to fill in any space between the end | |
6920 | * of the uncompressed data and the end of max_size in case the decompressed | |
6921 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6922 | * the end of an inline extent and the beginning of the next block, so we | |
6923 | * cover that region here. | |
6924 | */ | |
6925 | ||
6926 | if (max_size + pg_offset < PAGE_SIZE) { | |
6927 | char *map = kmap(page); | |
6928 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6929 | kunmap(page); | |
6930 | } | |
c8b97818 | 6931 | kfree(tmp); |
166ae5a4 | 6932 | return ret; |
c8b97818 CM |
6933 | } |
6934 | ||
d352ac68 CM |
6935 | /* |
6936 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6937 | * the ugly parts come from merging extents from the disk with the in-ram |
6938 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6939 | * where the in-ram extents might be locked pending data=ordered completion. |
6940 | * | |
6941 | * This also copies inline extents directly into the page. | |
6942 | */ | |
fc4f21b1 | 6943 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
de2c6615 LB |
6944 | struct page *page, |
6945 | size_t pg_offset, u64 start, u64 len, | |
6946 | int create) | |
a52d9a80 | 6947 | { |
3ffbd68c | 6948 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
a52d9a80 CM |
6949 | int ret; |
6950 | int err = 0; | |
a52d9a80 CM |
6951 | u64 extent_start = 0; |
6952 | u64 extent_end = 0; | |
fc4f21b1 | 6953 | u64 objectid = btrfs_ino(inode); |
7e74e235 | 6954 | int extent_type = -1; |
f421950f | 6955 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6956 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6957 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6958 | struct extent_buffer *leaf; |
6959 | struct btrfs_key found_key; | |
a52d9a80 | 6960 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6961 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6962 | struct extent_io_tree *io_tree = &inode->io_tree; | |
7ffbb598 | 6963 | const bool new_inline = !page || create; |
a52d9a80 | 6964 | |
890871be | 6965 | read_lock(&em_tree->lock); |
d1310b2e | 6966 | em = lookup_extent_mapping(em_tree, start, len); |
890871be | 6967 | read_unlock(&em_tree->lock); |
d1310b2e | 6968 | |
a52d9a80 | 6969 | if (em) { |
e1c4b745 CM |
6970 | if (em->start > start || em->start + em->len <= start) |
6971 | free_extent_map(em); | |
6972 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6973 | free_extent_map(em); |
6974 | else | |
6975 | goto out; | |
a52d9a80 | 6976 | } |
172ddd60 | 6977 | em = alloc_extent_map(); |
a52d9a80 | 6978 | if (!em) { |
d1310b2e CM |
6979 | err = -ENOMEM; |
6980 | goto out; | |
a52d9a80 | 6981 | } |
d1310b2e | 6982 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6983 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6984 | em->len = (u64)-1; |
c8b97818 | 6985 | em->block_len = (u64)-1; |
f421950f | 6986 | |
bee6ec82 | 6987 | path = btrfs_alloc_path(); |
f421950f | 6988 | if (!path) { |
bee6ec82 LB |
6989 | err = -ENOMEM; |
6990 | goto out; | |
f421950f CM |
6991 | } |
6992 | ||
bee6ec82 LB |
6993 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6994 | path->reada = READA_FORWARD; | |
6995 | ||
e49aabd9 LB |
6996 | /* |
6997 | * Unless we're going to uncompress the inline extent, no sleep would | |
6998 | * happen. | |
6999 | */ | |
7000 | path->leave_spinning = 1; | |
7001 | ||
5c9a702e | 7002 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 CM |
7003 | if (ret < 0) { |
7004 | err = ret; | |
7005 | goto out; | |
b8eeab7f | 7006 | } else if (ret > 0) { |
a52d9a80 CM |
7007 | if (path->slots[0] == 0) |
7008 | goto not_found; | |
7009 | path->slots[0]--; | |
7010 | } | |
7011 | ||
5f39d397 CM |
7012 | leaf = path->nodes[0]; |
7013 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 7014 | struct btrfs_file_extent_item); |
5f39d397 | 7015 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 7016 | if (found_key.objectid != objectid || |
694c12ed | 7017 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
7018 | /* |
7019 | * If we backup past the first extent we want to move forward | |
7020 | * and see if there is an extent in front of us, otherwise we'll | |
7021 | * say there is a hole for our whole search range which can | |
7022 | * cause problems. | |
7023 | */ | |
7024 | extent_end = start; | |
7025 | goto next; | |
a52d9a80 CM |
7026 | } |
7027 | ||
694c12ed | 7028 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 7029 | extent_start = found_key.offset; |
694c12ed NB |
7030 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
7031 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6bf9e4bd QW |
7032 | /* Only regular file could have regular/prealloc extent */ |
7033 | if (!S_ISREG(inode->vfs_inode.i_mode)) { | |
7034 | ret = -EUCLEAN; | |
7035 | btrfs_crit(fs_info, | |
7036 | "regular/prealloc extent found for non-regular inode %llu", | |
7037 | btrfs_ino(inode)); | |
7038 | goto out; | |
7039 | } | |
a52d9a80 | 7040 | extent_end = extent_start + |
db94535d | 7041 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
7042 | |
7043 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
7044 | extent_start); | |
694c12ed | 7045 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
9036c102 | 7046 | size_t size; |
e41ca589 QW |
7047 | |
7048 | size = btrfs_file_extent_ram_bytes(leaf, item); | |
da17066c | 7049 | extent_end = ALIGN(extent_start + size, |
0b246afa | 7050 | fs_info->sectorsize); |
09ed2f16 LB |
7051 | |
7052 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
7053 | path->slots[0], | |
7054 | extent_start); | |
9036c102 | 7055 | } |
25a50341 | 7056 | next: |
9036c102 YZ |
7057 | if (start >= extent_end) { |
7058 | path->slots[0]++; | |
7059 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
7060 | ret = btrfs_next_leaf(root, path); | |
7061 | if (ret < 0) { | |
7062 | err = ret; | |
7063 | goto out; | |
b8eeab7f | 7064 | } else if (ret > 0) { |
9036c102 | 7065 | goto not_found; |
b8eeab7f | 7066 | } |
9036c102 | 7067 | leaf = path->nodes[0]; |
a52d9a80 | 7068 | } |
9036c102 YZ |
7069 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
7070 | if (found_key.objectid != objectid || | |
7071 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
7072 | goto not_found; | |
7073 | if (start + len <= found_key.offset) | |
7074 | goto not_found; | |
e2eca69d WS |
7075 | if (start > found_key.offset) |
7076 | goto next; | |
02a033df NB |
7077 | |
7078 | /* New extent overlaps with existing one */ | |
9036c102 | 7079 | em->start = start; |
70c8a91c | 7080 | em->orig_start = start; |
9036c102 | 7081 | em->len = found_key.offset - start; |
02a033df NB |
7082 | em->block_start = EXTENT_MAP_HOLE; |
7083 | goto insert; | |
9036c102 YZ |
7084 | } |
7085 | ||
fc4f21b1 | 7086 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 7087 | new_inline, em); |
7ffbb598 | 7088 | |
694c12ed NB |
7089 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
7090 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 7091 | goto insert; |
694c12ed | 7092 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 7093 | unsigned long ptr; |
a52d9a80 | 7094 | char *map; |
3326d1b0 CM |
7095 | size_t size; |
7096 | size_t extent_offset; | |
7097 | size_t copy_size; | |
a52d9a80 | 7098 | |
7ffbb598 | 7099 | if (new_inline) |
689f9346 | 7100 | goto out; |
5f39d397 | 7101 | |
e41ca589 | 7102 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 7103 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7104 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7105 | size - extent_offset); | |
3326d1b0 | 7106 | em->start = extent_start + extent_offset; |
0b246afa | 7107 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7108 | em->orig_block_len = em->len; |
70c8a91c | 7109 | em->orig_start = em->start; |
689f9346 | 7110 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 LB |
7111 | |
7112 | btrfs_set_path_blocking(path); | |
bf46f52d | 7113 | if (!PageUptodate(page)) { |
261507a0 LZ |
7114 | if (btrfs_file_extent_compression(leaf, item) != |
7115 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7116 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7117 | extent_offset, item); |
166ae5a4 ZB |
7118 | if (ret) { |
7119 | err = ret; | |
7120 | goto out; | |
7121 | } | |
c8b97818 CM |
7122 | } else { |
7123 | map = kmap(page); | |
7124 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7125 | copy_size); | |
09cbfeaf | 7126 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7127 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7128 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7129 | copy_size); |
7130 | } | |
c8b97818 CM |
7131 | kunmap(page); |
7132 | } | |
179e29e4 | 7133 | flush_dcache_page(page); |
a52d9a80 | 7134 | } |
d1310b2e | 7135 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7136 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7137 | goto insert; |
a52d9a80 CM |
7138 | } |
7139 | not_found: | |
7140 | em->start = start; | |
70c8a91c | 7141 | em->orig_start = start; |
d1310b2e | 7142 | em->len = len; |
5f39d397 | 7143 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 7144 | insert: |
b3b4aa74 | 7145 | btrfs_release_path(path); |
d1310b2e | 7146 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7147 | btrfs_err(fs_info, |
5d163e0e JM |
7148 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7149 | em->start, em->len, start, len); | |
a52d9a80 CM |
7150 | err = -EIO; |
7151 | goto out; | |
7152 | } | |
d1310b2e CM |
7153 | |
7154 | err = 0; | |
890871be | 7155 | write_lock(&em_tree->lock); |
f46b24c9 | 7156 | err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 7157 | write_unlock(&em_tree->lock); |
a52d9a80 | 7158 | out: |
c6414280 | 7159 | btrfs_free_path(path); |
1abe9b8a | 7160 | |
fc4f21b1 | 7161 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7162 | |
a52d9a80 CM |
7163 | if (err) { |
7164 | free_extent_map(em); | |
a52d9a80 CM |
7165 | return ERR_PTR(err); |
7166 | } | |
79787eaa | 7167 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7168 | return em; |
7169 | } | |
7170 | ||
fc4f21b1 | 7171 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 7172 | u64 start, u64 len) |
ec29ed5b CM |
7173 | { |
7174 | struct extent_map *em; | |
7175 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 7176 | u64 delalloc_start = start; |
ec29ed5b | 7177 | u64 end; |
f3714ef4 NB |
7178 | u64 delalloc_len; |
7179 | u64 delalloc_end; | |
ec29ed5b CM |
7180 | int err = 0; |
7181 | ||
4ab47a8d | 7182 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
ec29ed5b CM |
7183 | if (IS_ERR(em)) |
7184 | return em; | |
9986277e DC |
7185 | /* |
7186 | * If our em maps to: | |
7187 | * - a hole or | |
7188 | * - a pre-alloc extent, | |
7189 | * there might actually be delalloc bytes behind it. | |
7190 | */ | |
7191 | if (em->block_start != EXTENT_MAP_HOLE && | |
7192 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7193 | return em; | |
7194 | else | |
7195 | hole_em = em; | |
ec29ed5b CM |
7196 | |
7197 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7198 | end = start + len; | |
7199 | if (end < start) | |
7200 | end = (u64)-1; | |
7201 | else | |
7202 | end -= 1; | |
7203 | ||
7204 | em = NULL; | |
7205 | ||
7206 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 7207 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 7208 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
7209 | delalloc_end = delalloc_start + delalloc_len; |
7210 | if (delalloc_end < delalloc_start) | |
7211 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
7212 | |
7213 | /* | |
f3714ef4 NB |
7214 | * We didn't find anything useful, return the original results from |
7215 | * get_extent() | |
ec29ed5b | 7216 | */ |
f3714ef4 | 7217 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
7218 | em = hole_em; |
7219 | hole_em = NULL; | |
7220 | goto out; | |
7221 | } | |
7222 | ||
f3714ef4 NB |
7223 | /* |
7224 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
7225 | * the start they passed in | |
ec29ed5b | 7226 | */ |
f3714ef4 NB |
7227 | delalloc_start = max(start, delalloc_start); |
7228 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 7229 | |
f3714ef4 NB |
7230 | if (delalloc_len > 0) { |
7231 | u64 hole_start; | |
02950af4 | 7232 | u64 hole_len; |
f3714ef4 | 7233 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 7234 | |
172ddd60 | 7235 | em = alloc_extent_map(); |
ec29ed5b CM |
7236 | if (!em) { |
7237 | err = -ENOMEM; | |
7238 | goto out; | |
7239 | } | |
f3714ef4 NB |
7240 | |
7241 | ASSERT(hole_em); | |
ec29ed5b | 7242 | /* |
f3714ef4 NB |
7243 | * When btrfs_get_extent can't find anything it returns one |
7244 | * huge hole | |
ec29ed5b | 7245 | * |
f3714ef4 NB |
7246 | * Make sure what it found really fits our range, and adjust to |
7247 | * make sure it is based on the start from the caller | |
ec29ed5b | 7248 | */ |
f3714ef4 NB |
7249 | if (hole_end <= start || hole_em->start > end) { |
7250 | free_extent_map(hole_em); | |
7251 | hole_em = NULL; | |
7252 | } else { | |
7253 | hole_start = max(hole_em->start, start); | |
7254 | hole_len = hole_end - hole_start; | |
ec29ed5b | 7255 | } |
f3714ef4 NB |
7256 | |
7257 | if (hole_em && delalloc_start > hole_start) { | |
7258 | /* | |
7259 | * Our hole starts before our delalloc, so we have to | |
7260 | * return just the parts of the hole that go until the | |
7261 | * delalloc starts | |
ec29ed5b | 7262 | */ |
f3714ef4 | 7263 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
7264 | em->start = hole_start; |
7265 | em->orig_start = hole_start; | |
7266 | /* | |
f3714ef4 NB |
7267 | * Don't adjust block start at all, it is fixed at |
7268 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
7269 | */ |
7270 | em->block_start = hole_em->block_start; | |
7271 | em->block_len = hole_len; | |
f9e4fb53 LB |
7272 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7273 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 7274 | } else { |
f3714ef4 NB |
7275 | /* |
7276 | * Hole is out of passed range or it starts after | |
7277 | * delalloc range | |
7278 | */ | |
7279 | em->start = delalloc_start; | |
7280 | em->len = delalloc_len; | |
7281 | em->orig_start = delalloc_start; | |
ec29ed5b | 7282 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 7283 | em->block_len = delalloc_len; |
ec29ed5b | 7284 | } |
bf8d32b9 | 7285 | } else { |
ec29ed5b CM |
7286 | return hole_em; |
7287 | } | |
7288 | out: | |
7289 | ||
7290 | free_extent_map(hole_em); | |
7291 | if (err) { | |
7292 | free_extent_map(em); | |
7293 | return ERR_PTR(err); | |
7294 | } | |
7295 | return em; | |
7296 | } | |
7297 | ||
5f9a8a51 FM |
7298 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7299 | const u64 start, | |
7300 | const u64 len, | |
7301 | const u64 orig_start, | |
7302 | const u64 block_start, | |
7303 | const u64 block_len, | |
7304 | const u64 orig_block_len, | |
7305 | const u64 ram_bytes, | |
7306 | const int type) | |
7307 | { | |
7308 | struct extent_map *em = NULL; | |
7309 | int ret; | |
7310 | ||
5f9a8a51 | 7311 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7312 | em = create_io_em(inode, start, len, orig_start, |
7313 | block_start, block_len, orig_block_len, | |
7314 | ram_bytes, | |
7315 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7316 | type); | |
5f9a8a51 FM |
7317 | if (IS_ERR(em)) |
7318 | goto out; | |
7319 | } | |
7320 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7321 | len, block_len, type); | |
7322 | if (ret) { | |
7323 | if (em) { | |
7324 | free_extent_map(em); | |
dcdbc059 | 7325 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7326 | start + len - 1, 0); |
7327 | } | |
7328 | em = ERR_PTR(ret); | |
7329 | } | |
7330 | out: | |
5f9a8a51 FM |
7331 | |
7332 | return em; | |
7333 | } | |
7334 | ||
4b46fce2 JB |
7335 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7336 | u64 start, u64 len) | |
7337 | { | |
0b246afa | 7338 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7340 | struct extent_map *em; |
4b46fce2 JB |
7341 | struct btrfs_key ins; |
7342 | u64 alloc_hint; | |
7343 | int ret; | |
4b46fce2 | 7344 | |
4b46fce2 | 7345 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7346 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7347 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7348 | if (ret) |
7349 | return ERR_PTR(ret); | |
4b46fce2 | 7350 | |
5f9a8a51 FM |
7351 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7352 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7353 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7354 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7355 | if (IS_ERR(em)) |
2ff7e61e JM |
7356 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7357 | ins.offset, 1); | |
de0ee0ed | 7358 | |
4b46fce2 JB |
7359 | return em; |
7360 | } | |
7361 | ||
46bfbb5c CM |
7362 | /* |
7363 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7364 | * block must be cow'd | |
7365 | */ | |
00361589 | 7366 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7367 | u64 *orig_start, u64 *orig_block_len, |
7368 | u64 *ram_bytes) | |
46bfbb5c | 7369 | { |
2ff7e61e | 7370 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7371 | struct btrfs_path *path; |
7372 | int ret; | |
7373 | struct extent_buffer *leaf; | |
7374 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7375 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7376 | struct btrfs_file_extent_item *fi; |
7377 | struct btrfs_key key; | |
7378 | u64 disk_bytenr; | |
7379 | u64 backref_offset; | |
7380 | u64 extent_end; | |
7381 | u64 num_bytes; | |
7382 | int slot; | |
7383 | int found_type; | |
7ee9e440 | 7384 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7385 | |
46bfbb5c CM |
7386 | path = btrfs_alloc_path(); |
7387 | if (!path) | |
7388 | return -ENOMEM; | |
7389 | ||
f85b7379 DS |
7390 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7391 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7392 | if (ret < 0) |
7393 | goto out; | |
7394 | ||
7395 | slot = path->slots[0]; | |
7396 | if (ret == 1) { | |
7397 | if (slot == 0) { | |
7398 | /* can't find the item, must cow */ | |
7399 | ret = 0; | |
7400 | goto out; | |
7401 | } | |
7402 | slot--; | |
7403 | } | |
7404 | ret = 0; | |
7405 | leaf = path->nodes[0]; | |
7406 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7407 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7408 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7409 | /* not our file or wrong item type, must cow */ | |
7410 | goto out; | |
7411 | } | |
7412 | ||
7413 | if (key.offset > offset) { | |
7414 | /* Wrong offset, must cow */ | |
7415 | goto out; | |
7416 | } | |
7417 | ||
7418 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7419 | found_type = btrfs_file_extent_type(leaf, fi); | |
7420 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7421 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7422 | /* not a regular extent, must cow */ | |
7423 | goto out; | |
7424 | } | |
7ee9e440 JB |
7425 | |
7426 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7427 | goto out; | |
7428 | ||
e77751aa MX |
7429 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7430 | if (extent_end <= offset) | |
7431 | goto out; | |
7432 | ||
46bfbb5c | 7433 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7434 | if (disk_bytenr == 0) |
7435 | goto out; | |
7436 | ||
7437 | if (btrfs_file_extent_compression(leaf, fi) || | |
7438 | btrfs_file_extent_encryption(leaf, fi) || | |
7439 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7440 | goto out; | |
7441 | ||
78d4295b EL |
7442 | /* |
7443 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7444 | * unnecessary search. | |
7445 | */ | |
7446 | if (btrfs_file_extent_generation(leaf, fi) <= | |
7447 | btrfs_root_last_snapshot(&root->root_item)) | |
7448 | goto out; | |
7449 | ||
46bfbb5c CM |
7450 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7451 | ||
7ee9e440 JB |
7452 | if (orig_start) { |
7453 | *orig_start = key.offset - backref_offset; | |
7454 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7455 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7456 | } | |
eb384b55 | 7457 | |
2ff7e61e | 7458 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7459 | goto out; |
7b2b7085 MX |
7460 | |
7461 | num_bytes = min(offset + *len, extent_end) - offset; | |
7462 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7463 | u64 range_end; | |
7464 | ||
da17066c JM |
7465 | range_end = round_up(offset + num_bytes, |
7466 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7467 | ret = test_range_bit(io_tree, offset, range_end, |
7468 | EXTENT_DELALLOC, 0, NULL); | |
7469 | if (ret) { | |
7470 | ret = -EAGAIN; | |
7471 | goto out; | |
7472 | } | |
7473 | } | |
7474 | ||
1bda19eb | 7475 | btrfs_release_path(path); |
46bfbb5c CM |
7476 | |
7477 | /* | |
7478 | * look for other files referencing this extent, if we | |
7479 | * find any we must cow | |
7480 | */ | |
00361589 | 7481 | |
e4c3b2dc | 7482 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7483 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7484 | if (ret) { |
7485 | ret = 0; | |
7486 | goto out; | |
7487 | } | |
46bfbb5c CM |
7488 | |
7489 | /* | |
7490 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7491 | * in this extent we are about to write. If there | |
7492 | * are any csums in that range we have to cow in order | |
7493 | * to keep the csums correct | |
7494 | */ | |
7495 | disk_bytenr += backref_offset; | |
7496 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7497 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7498 | goto out; | |
46bfbb5c CM |
7499 | /* |
7500 | * all of the above have passed, it is safe to overwrite this extent | |
7501 | * without cow | |
7502 | */ | |
eb384b55 | 7503 | *len = num_bytes; |
46bfbb5c CM |
7504 | ret = 1; |
7505 | out: | |
7506 | btrfs_free_path(path); | |
7507 | return ret; | |
7508 | } | |
7509 | ||
eb838e73 JB |
7510 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7511 | struct extent_state **cached_state, int writing) | |
7512 | { | |
7513 | struct btrfs_ordered_extent *ordered; | |
7514 | int ret = 0; | |
7515 | ||
7516 | while (1) { | |
7517 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7518 | cached_state); |
eb838e73 JB |
7519 | /* |
7520 | * We're concerned with the entire range that we're going to be | |
01327610 | 7521 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7522 | * extents in this range. |
7523 | */ | |
a776c6fa | 7524 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7525 | lockend - lockstart + 1); |
7526 | ||
7527 | /* | |
7528 | * We need to make sure there are no buffered pages in this | |
7529 | * range either, we could have raced between the invalidate in | |
7530 | * generic_file_direct_write and locking the extent. The | |
7531 | * invalidate needs to happen so that reads after a write do not | |
7532 | * get stale data. | |
7533 | */ | |
fc4adbff | 7534 | if (!ordered && |
051c98eb DS |
7535 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7536 | lockstart, lockend))) | |
eb838e73 JB |
7537 | break; |
7538 | ||
7539 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7540 | cached_state); |
eb838e73 JB |
7541 | |
7542 | if (ordered) { | |
ade77029 FM |
7543 | /* |
7544 | * If we are doing a DIO read and the ordered extent we | |
7545 | * found is for a buffered write, we can not wait for it | |
7546 | * to complete and retry, because if we do so we can | |
7547 | * deadlock with concurrent buffered writes on page | |
7548 | * locks. This happens only if our DIO read covers more | |
7549 | * than one extent map, if at this point has already | |
7550 | * created an ordered extent for a previous extent map | |
7551 | * and locked its range in the inode's io tree, and a | |
7552 | * concurrent write against that previous extent map's | |
7553 | * range and this range started (we unlock the ranges | |
7554 | * in the io tree only when the bios complete and | |
7555 | * buffered writes always lock pages before attempting | |
7556 | * to lock range in the io tree). | |
7557 | */ | |
7558 | if (writing || | |
7559 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7560 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7561 | else | |
7562 | ret = -ENOTBLK; | |
eb838e73 JB |
7563 | btrfs_put_ordered_extent(ordered); |
7564 | } else { | |
eb838e73 | 7565 | /* |
b850ae14 FM |
7566 | * We could trigger writeback for this range (and wait |
7567 | * for it to complete) and then invalidate the pages for | |
7568 | * this range (through invalidate_inode_pages2_range()), | |
7569 | * but that can lead us to a deadlock with a concurrent | |
7570 | * call to readpages() (a buffered read or a defrag call | |
7571 | * triggered a readahead) on a page lock due to an | |
7572 | * ordered dio extent we created before but did not have | |
7573 | * yet a corresponding bio submitted (whence it can not | |
7574 | * complete), which makes readpages() wait for that | |
7575 | * ordered extent to complete while holding a lock on | |
7576 | * that page. | |
eb838e73 | 7577 | */ |
b850ae14 | 7578 | ret = -ENOTBLK; |
eb838e73 JB |
7579 | } |
7580 | ||
ade77029 FM |
7581 | if (ret) |
7582 | break; | |
7583 | ||
eb838e73 JB |
7584 | cond_resched(); |
7585 | } | |
7586 | ||
7587 | return ret; | |
7588 | } | |
7589 | ||
6f9994db LB |
7590 | /* The callers of this must take lock_extent() */ |
7591 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7592 | u64 orig_start, u64 block_start, | |
7593 | u64 block_len, u64 orig_block_len, | |
7594 | u64 ram_bytes, int compress_type, | |
7595 | int type) | |
69ffb543 JB |
7596 | { |
7597 | struct extent_map_tree *em_tree; | |
7598 | struct extent_map *em; | |
69ffb543 JB |
7599 | int ret; |
7600 | ||
6f9994db LB |
7601 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7602 | type == BTRFS_ORDERED_COMPRESSED || | |
7603 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7604 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7605 | |
69ffb543 JB |
7606 | em_tree = &BTRFS_I(inode)->extent_tree; |
7607 | em = alloc_extent_map(); | |
7608 | if (!em) | |
7609 | return ERR_PTR(-ENOMEM); | |
7610 | ||
7611 | em->start = start; | |
7612 | em->orig_start = orig_start; | |
7613 | em->len = len; | |
7614 | em->block_len = block_len; | |
7615 | em->block_start = block_start; | |
b4939680 | 7616 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7617 | em->ram_bytes = ram_bytes; |
70c8a91c | 7618 | em->generation = -1; |
69ffb543 | 7619 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7620 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7621 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7622 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7623 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7624 | em->compress_type = compress_type; | |
7625 | } | |
69ffb543 JB |
7626 | |
7627 | do { | |
dcdbc059 | 7628 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7629 | em->start + em->len - 1, 0); |
7630 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7631 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7632 | write_unlock(&em_tree->lock); |
6f9994db LB |
7633 | /* |
7634 | * The caller has taken lock_extent(), who could race with us | |
7635 | * to add em? | |
7636 | */ | |
69ffb543 JB |
7637 | } while (ret == -EEXIST); |
7638 | ||
7639 | if (ret) { | |
7640 | free_extent_map(em); | |
7641 | return ERR_PTR(ret); | |
7642 | } | |
7643 | ||
6f9994db | 7644 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7645 | return em; |
7646 | } | |
7647 | ||
1c8d0175 NB |
7648 | |
7649 | static int btrfs_get_blocks_direct_read(struct extent_map *em, | |
7650 | struct buffer_head *bh_result, | |
7651 | struct inode *inode, | |
7652 | u64 start, u64 len) | |
7653 | { | |
8530c37a DS |
7654 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7655 | ||
1c8d0175 NB |
7656 | if (em->block_start == EXTENT_MAP_HOLE || |
7657 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7658 | return -ENOENT; | |
7659 | ||
7660 | len = min(len, em->len - (start - em->start)); | |
7661 | ||
7662 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7663 | inode->i_blkbits; | |
7664 | bh_result->b_size = len; | |
8530c37a | 7665 | bh_result->b_bdev = fs_info->fs_devices->latest_bdev; |
1c8d0175 NB |
7666 | set_buffer_mapped(bh_result); |
7667 | ||
7668 | return 0; | |
7669 | } | |
7670 | ||
c5794e51 NB |
7671 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
7672 | struct buffer_head *bh_result, | |
7673 | struct inode *inode, | |
7674 | struct btrfs_dio_data *dio_data, | |
7675 | u64 start, u64 len) | |
7676 | { | |
7677 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7678 | struct extent_map *em = *map; | |
7679 | int ret = 0; | |
7680 | ||
7681 | /* | |
7682 | * We don't allocate a new extent in the following cases | |
7683 | * | |
7684 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7685 | * existing extent. | |
7686 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7687 | * just use the extent. | |
7688 | * | |
7689 | */ | |
7690 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7691 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7692 | em->block_start != EXTENT_MAP_HOLE)) { | |
7693 | int type; | |
7694 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7695 | ||
7696 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7697 | type = BTRFS_ORDERED_PREALLOC; | |
7698 | else | |
7699 | type = BTRFS_ORDERED_NOCOW; | |
7700 | len = min(len, em->len - (start - em->start)); | |
7701 | block_start = em->block_start + (start - em->start); | |
7702 | ||
7703 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
7704 | &orig_block_len, &ram_bytes) == 1 && | |
7705 | btrfs_inc_nocow_writers(fs_info, block_start)) { | |
7706 | struct extent_map *em2; | |
7707 | ||
7708 | em2 = btrfs_create_dio_extent(inode, start, len, | |
7709 | orig_start, block_start, | |
7710 | len, orig_block_len, | |
7711 | ram_bytes, type); | |
7712 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7713 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7714 | free_extent_map(em); | |
7715 | *map = em = em2; | |
7716 | } | |
7717 | ||
7718 | if (em2 && IS_ERR(em2)) { | |
7719 | ret = PTR_ERR(em2); | |
7720 | goto out; | |
7721 | } | |
7722 | /* | |
7723 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7724 | * use the existing or preallocated extent, so does not | |
7725 | * need to adjust btrfs_space_info's bytes_may_use. | |
7726 | */ | |
7727 | btrfs_free_reserved_data_space_noquota(inode, start, | |
7728 | len); | |
7729 | goto skip_cow; | |
7730 | } | |
7731 | } | |
7732 | ||
7733 | /* this will cow the extent */ | |
7734 | len = bh_result->b_size; | |
7735 | free_extent_map(em); | |
7736 | *map = em = btrfs_new_extent_direct(inode, start, len); | |
7737 | if (IS_ERR(em)) { | |
7738 | ret = PTR_ERR(em); | |
7739 | goto out; | |
7740 | } | |
7741 | ||
7742 | len = min(len, em->len - (start - em->start)); | |
7743 | ||
7744 | skip_cow: | |
7745 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7746 | inode->i_blkbits; | |
7747 | bh_result->b_size = len; | |
8530c37a | 7748 | bh_result->b_bdev = fs_info->fs_devices->latest_bdev; |
c5794e51 NB |
7749 | set_buffer_mapped(bh_result); |
7750 | ||
7751 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7752 | set_buffer_new(bh_result); | |
7753 | ||
7754 | /* | |
7755 | * Need to update the i_size under the extent lock so buffered | |
7756 | * readers will get the updated i_size when we unlock. | |
7757 | */ | |
7758 | if (!dio_data->overwrite && start + len > i_size_read(inode)) | |
7759 | i_size_write(inode, start + len); | |
7760 | ||
7761 | WARN_ON(dio_data->reserve < len); | |
7762 | dio_data->reserve -= len; | |
7763 | dio_data->unsubmitted_oe_range_end = start + len; | |
7764 | current->journal_info = dio_data; | |
7765 | out: | |
7766 | return ret; | |
7767 | } | |
7768 | ||
4b46fce2 JB |
7769 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7770 | struct buffer_head *bh_result, int create) | |
7771 | { | |
0b246afa | 7772 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7773 | struct extent_map *em; |
eb838e73 | 7774 | struct extent_state *cached_state = NULL; |
50745b0a | 7775 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7776 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7777 | u64 lockstart, lockend; |
4b46fce2 | 7778 | u64 len = bh_result->b_size; |
0934856d | 7779 | int ret = 0; |
eb838e73 | 7780 | |
e182163d | 7781 | if (!create) |
0b246afa | 7782 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7783 | |
c329861d JB |
7784 | lockstart = start; |
7785 | lockend = start + len - 1; | |
7786 | ||
e1cbbfa5 JB |
7787 | if (current->journal_info) { |
7788 | /* | |
7789 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7790 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7791 | * confused. |
7792 | */ | |
50745b0a | 7793 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7794 | current->journal_info = NULL; |
7795 | } | |
7796 | ||
eb838e73 JB |
7797 | /* |
7798 | * If this errors out it's because we couldn't invalidate pagecache for | |
7799 | * this range and we need to fallback to buffered. | |
7800 | */ | |
9c9464cc FM |
7801 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7802 | create)) { | |
7803 | ret = -ENOTBLK; | |
7804 | goto err; | |
7805 | } | |
eb838e73 | 7806 | |
fc4f21b1 | 7807 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7808 | if (IS_ERR(em)) { |
7809 | ret = PTR_ERR(em); | |
7810 | goto unlock_err; | |
7811 | } | |
4b46fce2 JB |
7812 | |
7813 | /* | |
7814 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7815 | * io. INLINE is special, and we could probably kludge it in here, but | |
7816 | * it's still buffered so for safety lets just fall back to the generic | |
7817 | * buffered path. | |
7818 | * | |
7819 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7820 | * decompress it, so there will be buffering required no matter what we | |
7821 | * do, so go ahead and fallback to buffered. | |
7822 | * | |
01327610 | 7823 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7824 | * to buffered IO. Don't blame me, this is the price we pay for using |
7825 | * the generic code. | |
7826 | */ | |
7827 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7828 | em->block_start == EXTENT_MAP_INLINE) { | |
7829 | free_extent_map(em); | |
eb838e73 JB |
7830 | ret = -ENOTBLK; |
7831 | goto unlock_err; | |
4b46fce2 JB |
7832 | } |
7833 | ||
c5794e51 NB |
7834 | if (create) { |
7835 | ret = btrfs_get_blocks_direct_write(&em, bh_result, inode, | |
7836 | dio_data, start, len); | |
7837 | if (ret < 0) | |
7838 | goto unlock_err; | |
7839 | ||
e182163d OS |
7840 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, |
7841 | lockend, &cached_state); | |
c5794e51 | 7842 | } else { |
1c8d0175 NB |
7843 | ret = btrfs_get_blocks_direct_read(em, bh_result, inode, |
7844 | start, len); | |
7845 | /* Can be negative only if we read from a hole */ | |
7846 | if (ret < 0) { | |
7847 | ret = 0; | |
7848 | free_extent_map(em); | |
7849 | goto unlock_err; | |
7850 | } | |
7851 | /* | |
7852 | * We need to unlock only the end area that we aren't using. | |
7853 | * The rest is going to be unlocked by the endio routine. | |
7854 | */ | |
7855 | lockstart = start + bh_result->b_size; | |
7856 | if (lockstart < lockend) { | |
e182163d OS |
7857 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
7858 | lockstart, lockend, &cached_state); | |
1c8d0175 NB |
7859 | } else { |
7860 | free_extent_state(cached_state); | |
7861 | } | |
4b46fce2 JB |
7862 | } |
7863 | ||
4b46fce2 JB |
7864 | free_extent_map(em); |
7865 | ||
7866 | return 0; | |
eb838e73 JB |
7867 | |
7868 | unlock_err: | |
e182163d OS |
7869 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7870 | &cached_state); | |
9c9464cc | 7871 | err: |
50745b0a | 7872 | if (dio_data) |
7873 | current->journal_info = dio_data; | |
eb838e73 | 7874 | return ret; |
4b46fce2 JB |
7875 | } |
7876 | ||
58efbc9f OS |
7877 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
7878 | struct bio *bio, | |
7879 | int mirror_num) | |
8b110e39 | 7880 | { |
2ff7e61e | 7881 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7882 | blk_status_t ret; |
8b110e39 | 7883 | |
37226b21 | 7884 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7885 | |
2ff7e61e | 7886 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 | 7887 | if (ret) |
ea057f6d | 7888 | return ret; |
8b110e39 | 7889 | |
08635bae | 7890 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
ea057f6d | 7891 | |
8b110e39 MX |
7892 | return ret; |
7893 | } | |
7894 | ||
7895 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7896 | struct bio *failed_bio, | |
7897 | struct io_failure_record *failrec, | |
7898 | int failed_mirror) | |
7899 | { | |
ab8d0fc4 | 7900 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7901 | int num_copies; |
7902 | ||
ab8d0fc4 | 7903 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7904 | if (num_copies == 1) { |
7905 | /* | |
7906 | * we only have a single copy of the data, so don't bother with | |
7907 | * all the retry and error correction code that follows. no | |
7908 | * matter what the error is, it is very likely to persist. | |
7909 | */ | |
ab8d0fc4 JM |
7910 | btrfs_debug(fs_info, |
7911 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7912 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7913 | return 0; |
7914 | } | |
7915 | ||
7916 | failrec->failed_mirror = failed_mirror; | |
7917 | failrec->this_mirror++; | |
7918 | if (failrec->this_mirror == failed_mirror) | |
7919 | failrec->this_mirror++; | |
7920 | ||
7921 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7922 | btrfs_debug(fs_info, |
7923 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7924 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7925 | return 0; |
7926 | } | |
7927 | ||
7928 | return 1; | |
7929 | } | |
7930 | ||
58efbc9f OS |
7931 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
7932 | struct page *page, unsigned int pgoff, | |
7933 | u64 start, u64 end, int failed_mirror, | |
7934 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7935 | { |
7936 | struct io_failure_record *failrec; | |
7870d082 JB |
7937 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7938 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
7939 | struct bio *bio; |
7940 | int isector; | |
f1c77c55 | 7941 | unsigned int read_mode = 0; |
17347cec | 7942 | int segs; |
8b110e39 | 7943 | int ret; |
58efbc9f | 7944 | blk_status_t status; |
c16a8ac3 | 7945 | struct bio_vec bvec; |
8b110e39 | 7946 | |
37226b21 | 7947 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7948 | |
7949 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7950 | if (ret) | |
58efbc9f | 7951 | return errno_to_blk_status(ret); |
8b110e39 MX |
7952 | |
7953 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7954 | failed_mirror); | |
7955 | if (!ret) { | |
7870d082 | 7956 | free_io_failure(failure_tree, io_tree, failrec); |
58efbc9f | 7957 | return BLK_STS_IOERR; |
8b110e39 MX |
7958 | } |
7959 | ||
17347cec | 7960 | segs = bio_segments(failed_bio); |
c16a8ac3 | 7961 | bio_get_first_bvec(failed_bio, &bvec); |
17347cec | 7962 | if (segs > 1 || |
c16a8ac3 | 7963 | (bvec.bv_len > btrfs_inode_sectorsize(inode))) |
70fd7614 | 7964 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
7965 | |
7966 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7967 | isector >>= inode->i_sb->s_blocksize_bits; | |
7968 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 7969 | pgoff, isector, repair_endio, repair_arg); |
ebcc3263 | 7970 | bio->bi_opf = REQ_OP_READ | read_mode; |
8b110e39 MX |
7971 | |
7972 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
913e1535 | 7973 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
8b110e39 MX |
7974 | read_mode, failrec->this_mirror, failrec->in_validation); |
7975 | ||
58efbc9f OS |
7976 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
7977 | if (status) { | |
7870d082 | 7978 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
7979 | bio_put(bio); |
7980 | } | |
7981 | ||
58efbc9f | 7982 | return status; |
8b110e39 MX |
7983 | } |
7984 | ||
7985 | struct btrfs_retry_complete { | |
7986 | struct completion done; | |
7987 | struct inode *inode; | |
7988 | u64 start; | |
7989 | int uptodate; | |
7990 | }; | |
7991 | ||
4246a0b6 | 7992 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7993 | { |
7994 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 7995 | struct inode *inode = done->inode; |
8b110e39 | 7996 | struct bio_vec *bvec; |
7870d082 | 7997 | struct extent_io_tree *io_tree, *failure_tree; |
6dc4f100 | 7998 | struct bvec_iter_all iter_all; |
8b110e39 | 7999 | |
4e4cbee9 | 8000 | if (bio->bi_status) |
8b110e39 MX |
8001 | goto end; |
8002 | ||
2dabb324 | 8003 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
8004 | io_tree = &BTRFS_I(inode)->io_tree; |
8005 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
263663cd | 8006 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode)); |
2dabb324 | 8007 | |
8b110e39 | 8008 | done->uptodate = 1; |
c09abff8 | 8009 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 8010 | bio_for_each_segment_all(bvec, bio, iter_all) |
7870d082 JB |
8011 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
8012 | io_tree, done->start, bvec->bv_page, | |
8013 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
8014 | end: |
8015 | complete(&done->done); | |
8016 | bio_put(bio); | |
8017 | } | |
8018 | ||
58efbc9f OS |
8019 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
8020 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 8021 | { |
2dabb324 | 8022 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8023 | struct bio_vec bvec; |
8024 | struct bvec_iter iter; | |
8b110e39 | 8025 | struct btrfs_retry_complete done; |
4b46fce2 | 8026 | u64 start; |
2dabb324 CR |
8027 | unsigned int pgoff; |
8028 | u32 sectorsize; | |
8029 | int nr_sectors; | |
58efbc9f OS |
8030 | blk_status_t ret; |
8031 | blk_status_t err = BLK_STS_OK; | |
4b46fce2 | 8032 | |
2dabb324 | 8033 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8034 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8035 | |
8b110e39 MX |
8036 | start = io_bio->logical; |
8037 | done.inode = inode; | |
17347cec | 8038 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8039 | |
17347cec LB |
8040 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8041 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8042 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
8043 | |
8044 | next_block_or_try_again: | |
8b110e39 MX |
8045 | done.uptodate = 0; |
8046 | done.start = start; | |
8047 | init_completion(&done.done); | |
8048 | ||
17347cec | 8049 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8050 | pgoff, start, start + sectorsize - 1, |
8051 | io_bio->mirror_num, | |
8052 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
8053 | if (ret) { |
8054 | err = ret; | |
8055 | goto next; | |
8056 | } | |
8b110e39 | 8057 | |
9c17f6cd | 8058 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8059 | |
8060 | if (!done.uptodate) { | |
8061 | /* We might have another mirror, so try again */ | |
2dabb324 | 8062 | goto next_block_or_try_again; |
8b110e39 MX |
8063 | } |
8064 | ||
629ebf4f | 8065 | next: |
2dabb324 CR |
8066 | start += sectorsize; |
8067 | ||
97bf5a55 LB |
8068 | nr_sectors--; |
8069 | if (nr_sectors) { | |
2dabb324 | 8070 | pgoff += sectorsize; |
97bf5a55 | 8071 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8072 | goto next_block_or_try_again; |
8073 | } | |
8b110e39 MX |
8074 | } |
8075 | ||
629ebf4f | 8076 | return err; |
8b110e39 MX |
8077 | } |
8078 | ||
4246a0b6 | 8079 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8080 | { |
8081 | struct btrfs_retry_complete *done = bio->bi_private; | |
8082 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
8083 | struct extent_io_tree *io_tree, *failure_tree; |
8084 | struct inode *inode = done->inode; | |
8b110e39 MX |
8085 | struct bio_vec *bvec; |
8086 | int uptodate; | |
8087 | int ret; | |
2b070cfe | 8088 | int i = 0; |
6dc4f100 | 8089 | struct bvec_iter_all iter_all; |
8b110e39 | 8090 | |
4e4cbee9 | 8091 | if (bio->bi_status) |
8b110e39 MX |
8092 | goto end; |
8093 | ||
8094 | uptodate = 1; | |
2dabb324 | 8095 | |
2dabb324 | 8096 | ASSERT(bio->bi_vcnt == 1); |
263663cd | 8097 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 8098 | |
7870d082 JB |
8099 | io_tree = &BTRFS_I(inode)->io_tree; |
8100 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8101 | ||
c09abff8 | 8102 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 8103 | bio_for_each_segment_all(bvec, bio, iter_all) { |
7870d082 JB |
8104 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
8105 | bvec->bv_offset, done->start, | |
8106 | bvec->bv_len); | |
8b110e39 | 8107 | if (!ret) |
7870d082 JB |
8108 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
8109 | failure_tree, io_tree, done->start, | |
8110 | bvec->bv_page, | |
8111 | btrfs_ino(BTRFS_I(inode)), | |
8112 | bvec->bv_offset); | |
8b110e39 MX |
8113 | else |
8114 | uptodate = 0; | |
2b070cfe | 8115 | i++; |
8b110e39 MX |
8116 | } |
8117 | ||
8118 | done->uptodate = uptodate; | |
8119 | end: | |
8120 | complete(&done->done); | |
8121 | bio_put(bio); | |
8122 | } | |
8123 | ||
4e4cbee9 CH |
8124 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
8125 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 | 8126 | { |
2dabb324 | 8127 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8128 | struct bio_vec bvec; |
8129 | struct bvec_iter iter; | |
8b110e39 MX |
8130 | struct btrfs_retry_complete done; |
8131 | u64 start; | |
8132 | u64 offset = 0; | |
2dabb324 CR |
8133 | u32 sectorsize; |
8134 | int nr_sectors; | |
8135 | unsigned int pgoff; | |
8136 | int csum_pos; | |
ef7cdac1 | 8137 | bool uptodate = (err == 0); |
8b110e39 | 8138 | int ret; |
58efbc9f | 8139 | blk_status_t status; |
dc380aea | 8140 | |
2dabb324 | 8141 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8142 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8143 | |
58efbc9f | 8144 | err = BLK_STS_OK; |
c1dc0896 | 8145 | start = io_bio->logical; |
8b110e39 | 8146 | done.inode = inode; |
17347cec | 8147 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8148 | |
17347cec LB |
8149 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8150 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 8151 | |
17347cec | 8152 | pgoff = bvec.bv_offset; |
2dabb324 | 8153 | next_block: |
ef7cdac1 LB |
8154 | if (uptodate) { |
8155 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8156 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8157 | bvec.bv_page, pgoff, start, sectorsize); | |
8158 | if (likely(!ret)) | |
8159 | goto next; | |
8160 | } | |
8b110e39 MX |
8161 | try_again: |
8162 | done.uptodate = 0; | |
8163 | done.start = start; | |
8164 | init_completion(&done.done); | |
8165 | ||
58efbc9f OS |
8166 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
8167 | pgoff, start, start + sectorsize - 1, | |
8168 | io_bio->mirror_num, btrfs_retry_endio, | |
8169 | &done); | |
8170 | if (status) { | |
8171 | err = status; | |
8b110e39 MX |
8172 | goto next; |
8173 | } | |
8174 | ||
9c17f6cd | 8175 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8176 | |
8177 | if (!done.uptodate) { | |
8178 | /* We might have another mirror, so try again */ | |
8179 | goto try_again; | |
8180 | } | |
8181 | next: | |
2dabb324 CR |
8182 | offset += sectorsize; |
8183 | start += sectorsize; | |
8184 | ||
8185 | ASSERT(nr_sectors); | |
8186 | ||
97bf5a55 LB |
8187 | nr_sectors--; |
8188 | if (nr_sectors) { | |
2dabb324 | 8189 | pgoff += sectorsize; |
97bf5a55 | 8190 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8191 | goto next_block; |
8192 | } | |
2c30c71b | 8193 | } |
c1dc0896 MX |
8194 | |
8195 | return err; | |
8196 | } | |
8197 | ||
4e4cbee9 CH |
8198 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
8199 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 MX |
8200 | { |
8201 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8202 | ||
8203 | if (skip_csum) { | |
8204 | if (unlikely(err)) | |
8205 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8206 | else | |
58efbc9f | 8207 | return BLK_STS_OK; |
8b110e39 MX |
8208 | } else { |
8209 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8210 | } | |
8211 | } | |
8212 | ||
4246a0b6 | 8213 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8214 | { |
8215 | struct btrfs_dio_private *dip = bio->bi_private; | |
8216 | struct inode *inode = dip->inode; | |
8217 | struct bio *dio_bio; | |
8218 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4e4cbee9 | 8219 | blk_status_t err = bio->bi_status; |
c1dc0896 | 8220 | |
99c4e3b9 | 8221 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8b110e39 | 8222 | err = btrfs_subio_endio_read(inode, io_bio, err); |
c1dc0896 | 8223 | |
4b46fce2 | 8224 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8225 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8226 | dio_bio = dip->dio_bio; |
4b46fce2 | 8227 | |
4b46fce2 | 8228 | kfree(dip); |
c0da7aa1 | 8229 | |
99c4e3b9 | 8230 | dio_bio->bi_status = err; |
4055351c | 8231 | dio_end_io(dio_bio); |
b3a0dd50 | 8232 | btrfs_io_bio_free_csum(io_bio); |
9be3395b | 8233 | bio_put(bio); |
4b46fce2 JB |
8234 | } |
8235 | ||
52427260 QW |
8236 | static void __endio_write_update_ordered(struct inode *inode, |
8237 | const u64 offset, const u64 bytes, | |
8238 | const bool uptodate) | |
4b46fce2 | 8239 | { |
0b246afa | 8240 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8241 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 | 8242 | struct btrfs_workqueue *wq; |
14543774 FM |
8243 | u64 ordered_offset = offset; |
8244 | u64 ordered_bytes = bytes; | |
67c003f9 | 8245 | u64 last_offset; |
4b46fce2 | 8246 | |
a0cac0ec | 8247 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
52427260 | 8248 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 8249 | else |
52427260 | 8250 | wq = fs_info->endio_write_workers; |
52427260 | 8251 | |
b25f0d00 NB |
8252 | while (ordered_offset < offset + bytes) { |
8253 | last_offset = ordered_offset; | |
8254 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8255 | &ordered_offset, | |
8256 | ordered_bytes, | |
8257 | uptodate)) { | |
a0cac0ec OS |
8258 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
8259 | NULL); | |
b25f0d00 NB |
8260 | btrfs_queue_work(wq, &ordered->work); |
8261 | } | |
8262 | /* | |
8263 | * If btrfs_dec_test_ordered_pending does not find any ordered | |
8264 | * extent in the range, we can exit. | |
8265 | */ | |
8266 | if (ordered_offset == last_offset) | |
8267 | return; | |
8268 | /* | |
8269 | * Our bio might span multiple ordered extents. In this case | |
52042d8e | 8270 | * we keep going until we have accounted the whole dio. |
b25f0d00 NB |
8271 | */ |
8272 | if (ordered_offset < offset + bytes) { | |
8273 | ordered_bytes = offset + bytes - ordered_offset; | |
8274 | ordered = NULL; | |
8275 | } | |
163cf09c | 8276 | } |
14543774 FM |
8277 | } |
8278 | ||
8279 | static void btrfs_endio_direct_write(struct bio *bio) | |
8280 | { | |
8281 | struct btrfs_dio_private *dip = bio->bi_private; | |
8282 | struct bio *dio_bio = dip->dio_bio; | |
8283 | ||
52427260 | 8284 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
4e4cbee9 | 8285 | dip->bytes, !bio->bi_status); |
4b46fce2 | 8286 | |
4b46fce2 | 8287 | kfree(dip); |
c0da7aa1 | 8288 | |
4e4cbee9 | 8289 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8290 | dio_end_io(dio_bio); |
9be3395b | 8291 | bio_put(bio); |
4b46fce2 JB |
8292 | } |
8293 | ||
d0ee3934 | 8294 | static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, |
d0779291 | 8295 | struct bio *bio, u64 offset) |
eaf25d93 | 8296 | { |
c6100a4b | 8297 | struct inode *inode = private_data; |
4e4cbee9 | 8298 | blk_status_t ret; |
2ff7e61e | 8299 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8300 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8301 | return 0; |
8302 | } | |
8303 | ||
4246a0b6 | 8304 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8305 | { |
8306 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 8307 | blk_status_t err = bio->bi_status; |
e65e1535 | 8308 | |
8b110e39 MX |
8309 | if (err) |
8310 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8311 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8312 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8313 | bio->bi_opf, | |
8b110e39 MX |
8314 | (unsigned long long)bio->bi_iter.bi_sector, |
8315 | bio->bi_iter.bi_size, err); | |
8316 | ||
8317 | if (dip->subio_endio) | |
8318 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8319 | |
8320 | if (err) { | |
e65e1535 | 8321 | /* |
de224b7c NB |
8322 | * We want to perceive the errors flag being set before |
8323 | * decrementing the reference count. We don't need a barrier | |
8324 | * since atomic operations with a return value are fully | |
8325 | * ordered as per atomic_t.txt | |
e65e1535 | 8326 | */ |
de224b7c | 8327 | dip->errors = 1; |
e65e1535 MX |
8328 | } |
8329 | ||
8330 | /* if there are more bios still pending for this dio, just exit */ | |
8331 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8332 | goto out; | |
8333 | ||
9be3395b | 8334 | if (dip->errors) { |
e65e1535 | 8335 | bio_io_error(dip->orig_bio); |
9be3395b | 8336 | } else { |
2dbe0c77 | 8337 | dip->dio_bio->bi_status = BLK_STS_OK; |
4246a0b6 | 8338 | bio_endio(dip->orig_bio); |
e65e1535 MX |
8339 | } |
8340 | out: | |
8341 | bio_put(bio); | |
8342 | } | |
8343 | ||
4e4cbee9 | 8344 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8345 | struct btrfs_dio_private *dip, |
8346 | struct bio *bio, | |
8347 | u64 file_offset) | |
8348 | { | |
8349 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8350 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
4e4cbee9 | 8351 | blk_status_t ret; |
c1dc0896 MX |
8352 | |
8353 | /* | |
8354 | * We load all the csum data we need when we submit | |
8355 | * the first bio to reduce the csum tree search and | |
8356 | * contention. | |
8357 | */ | |
8358 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8359 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8360 | file_offset); |
8361 | if (ret) | |
8362 | return ret; | |
8363 | } | |
8364 | ||
8365 | if (bio == dip->orig_bio) | |
8366 | return 0; | |
8367 | ||
8368 | file_offset -= dip->logical_offset; | |
8369 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8370 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8371 | ||
8372 | return 0; | |
8373 | } | |
8374 | ||
d0ee3934 DS |
8375 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
8376 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 8377 | { |
0b246afa | 8378 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8379 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8380 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 8381 | blk_status_t ret; |
e65e1535 | 8382 | |
4c274bc6 | 8383 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
8384 | if (async_submit) |
8385 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8386 | ||
5fd02043 | 8387 | if (!write) { |
0b246afa | 8388 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8389 | if (ret) |
8390 | goto err; | |
8391 | } | |
e65e1535 | 8392 | |
e6961cac | 8393 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
8394 | goto map; |
8395 | ||
8396 | if (write && async_submit) { | |
c6100a4b JB |
8397 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8398 | file_offset, inode, | |
e288c080 | 8399 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 8400 | goto err; |
1ae39938 JB |
8401 | } else if (write) { |
8402 | /* | |
8403 | * If we aren't doing async submit, calculate the csum of the | |
8404 | * bio now. | |
8405 | */ | |
2ff7e61e | 8406 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8407 | if (ret) |
8408 | goto err; | |
23ea8e5a | 8409 | } else { |
2ff7e61e | 8410 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8411 | file_offset); |
c2db1073 TI |
8412 | if (ret) |
8413 | goto err; | |
8414 | } | |
1ae39938 | 8415 | map: |
08635bae | 8416 | ret = btrfs_map_bio(fs_info, bio, 0); |
e65e1535 | 8417 | err: |
e65e1535 MX |
8418 | return ret; |
8419 | } | |
8420 | ||
e6961cac | 8421 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
e65e1535 MX |
8422 | { |
8423 | struct inode *inode = dip->inode; | |
0b246afa | 8424 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8425 | struct bio *bio; |
8426 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8427 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8428 | u64 file_offset = dip->logical_offset; |
1ae39938 | 8429 | int async_submit = 0; |
725130ba LB |
8430 | u64 submit_len; |
8431 | int clone_offset = 0; | |
8432 | int clone_len; | |
5f4dc8fc | 8433 | int ret; |
58efbc9f | 8434 | blk_status_t status; |
89b798ad | 8435 | struct btrfs_io_geometry geom; |
e65e1535 | 8436 | |
89b798ad NB |
8437 | submit_len = orig_bio->bi_iter.bi_size; |
8438 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio), | |
8439 | start_sector << 9, submit_len, &geom); | |
7a5c3c9b | 8440 | if (ret) |
e65e1535 | 8441 | return -EIO; |
facc8a22 | 8442 | |
89b798ad | 8443 | if (geom.len >= submit_len) { |
02f57c7a | 8444 | bio = orig_bio; |
c1dc0896 | 8445 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8446 | goto submit; |
8447 | } | |
8448 | ||
53b381b3 | 8449 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8450 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8451 | async_submit = 0; |
8452 | else | |
8453 | async_submit = 1; | |
8454 | ||
725130ba | 8455 | /* bio split */ |
89b798ad | 8456 | ASSERT(geom.len <= INT_MAX); |
02f57c7a | 8457 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8458 | do { |
89b798ad | 8459 | clone_len = min_t(int, submit_len, geom.len); |
02f57c7a | 8460 | |
725130ba LB |
8461 | /* |
8462 | * This will never fail as it's passing GPF_NOFS and | |
8463 | * the allocation is backed by btrfs_bioset. | |
8464 | */ | |
e477094f | 8465 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8466 | clone_len); |
8467 | bio->bi_private = dip; | |
8468 | bio->bi_end_io = btrfs_end_dio_bio; | |
8469 | btrfs_io_bio(bio)->logical = file_offset; | |
8470 | ||
8471 | ASSERT(submit_len >= clone_len); | |
8472 | submit_len -= clone_len; | |
8473 | if (submit_len == 0) | |
8474 | break; | |
e65e1535 | 8475 | |
725130ba LB |
8476 | /* |
8477 | * Increase the count before we submit the bio so we know | |
8478 | * the end IO handler won't happen before we increase the | |
8479 | * count. Otherwise, the dip might get freed before we're | |
8480 | * done setting it up. | |
8481 | */ | |
8482 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8483 | |
d0ee3934 | 8484 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
8485 | async_submit); |
8486 | if (status) { | |
725130ba LB |
8487 | bio_put(bio); |
8488 | atomic_dec(&dip->pending_bios); | |
8489 | goto out_err; | |
8490 | } | |
e65e1535 | 8491 | |
725130ba LB |
8492 | clone_offset += clone_len; |
8493 | start_sector += clone_len >> 9; | |
8494 | file_offset += clone_len; | |
5f4dc8fc | 8495 | |
89b798ad NB |
8496 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio), |
8497 | start_sector << 9, submit_len, &geom); | |
725130ba LB |
8498 | if (ret) |
8499 | goto out_err; | |
3c91ee69 | 8500 | } while (submit_len > 0); |
e65e1535 | 8501 | |
02f57c7a | 8502 | submit: |
d0ee3934 | 8503 | status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
58efbc9f | 8504 | if (!status) |
e65e1535 MX |
8505 | return 0; |
8506 | ||
8507 | bio_put(bio); | |
8508 | out_err: | |
8509 | dip->errors = 1; | |
8510 | /* | |
de224b7c NB |
8511 | * Before atomic variable goto zero, we must make sure dip->errors is |
8512 | * perceived to be set. This ordering is ensured by the fact that an | |
8513 | * atomic operations with a return value are fully ordered as per | |
8514 | * atomic_t.txt | |
e65e1535 | 8515 | */ |
e65e1535 MX |
8516 | if (atomic_dec_and_test(&dip->pending_bios)) |
8517 | bio_io_error(dip->orig_bio); | |
8518 | ||
8519 | /* bio_end_io() will handle error, so we needn't return it */ | |
8520 | return 0; | |
8521 | } | |
8522 | ||
8a4c1e42 MC |
8523 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8524 | loff_t file_offset) | |
4b46fce2 | 8525 | { |
61de718f | 8526 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8527 | struct bio *bio = NULL; |
8528 | struct btrfs_io_bio *io_bio; | |
8a4c1e42 | 8529 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8530 | int ret = 0; |
8531 | ||
8b6c1d56 | 8532 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8533 | |
c1dc0896 | 8534 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8535 | if (!dip) { |
8536 | ret = -ENOMEM; | |
61de718f | 8537 | goto free_ordered; |
4b46fce2 | 8538 | } |
4b46fce2 | 8539 | |
9be3395b | 8540 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8541 | dip->inode = inode; |
8542 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8543 | dip->bytes = dio_bio->bi_iter.bi_size; |
8544 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8545 | bio->bi_private = dip; |
8546 | dip->orig_bio = bio; | |
9be3395b | 8547 | dip->dio_bio = dio_bio; |
e65e1535 | 8548 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8549 | io_bio = btrfs_io_bio(bio); |
8550 | io_bio->logical = file_offset; | |
4b46fce2 | 8551 | |
c1dc0896 | 8552 | if (write) { |
3892ac90 | 8553 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8554 | } else { |
3892ac90 | 8555 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8556 | dip->subio_endio = btrfs_subio_endio_read; |
8557 | } | |
4b46fce2 | 8558 | |
f28a4928 FM |
8559 | /* |
8560 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8561 | * even if we fail to submit a bio, because in such case we do the | |
8562 | * corresponding error handling below and it must not be done a second | |
8563 | * time by btrfs_direct_IO(). | |
8564 | */ | |
8565 | if (write) { | |
8566 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8567 | ||
8568 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8569 | dip->bytes; | |
8570 | dio_data->unsubmitted_oe_range_start = | |
8571 | dio_data->unsubmitted_oe_range_end; | |
8572 | } | |
8573 | ||
e6961cac | 8574 | ret = btrfs_submit_direct_hook(dip); |
e65e1535 | 8575 | if (!ret) |
eaf25d93 | 8576 | return; |
9be3395b | 8577 | |
b3a0dd50 | 8578 | btrfs_io_bio_free_csum(io_bio); |
9be3395b | 8579 | |
4b46fce2 JB |
8580 | free_ordered: |
8581 | /* | |
61de718f FM |
8582 | * If we arrived here it means either we failed to submit the dip |
8583 | * or we either failed to clone the dio_bio or failed to allocate the | |
8584 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8585 | * call bio_endio against our io_bio so that we get proper resource | |
8586 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8587 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8588 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8589 | */ |
3892ac90 | 8590 | if (bio && dip) { |
054ec2f6 | 8591 | bio_io_error(bio); |
61de718f | 8592 | /* |
3892ac90 | 8593 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8594 | * and all the cleanup and final put for dio_bio (through |
8595 | * dio_end_io()). | |
8596 | */ | |
8597 | dip = NULL; | |
3892ac90 | 8598 | bio = NULL; |
61de718f | 8599 | } else { |
14543774 | 8600 | if (write) |
52427260 | 8601 | __endio_write_update_ordered(inode, |
14543774 FM |
8602 | file_offset, |
8603 | dio_bio->bi_iter.bi_size, | |
52427260 | 8604 | false); |
14543774 | 8605 | else |
61de718f FM |
8606 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8607 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8608 | |
4e4cbee9 | 8609 | dio_bio->bi_status = BLK_STS_IOERR; |
61de718f FM |
8610 | /* |
8611 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8612 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8613 | */ | |
4055351c | 8614 | dio_end_io(dio_bio); |
4b46fce2 | 8615 | } |
3892ac90 LB |
8616 | if (bio) |
8617 | bio_put(bio); | |
61de718f | 8618 | kfree(dip); |
4b46fce2 JB |
8619 | } |
8620 | ||
2ff7e61e | 8621 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
2ff7e61e | 8622 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8623 | { |
8624 | int seg; | |
a1b75f7d | 8625 | int i; |
0b246afa | 8626 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8627 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8628 | |
8629 | if (offset & blocksize_mask) | |
8630 | goto out; | |
8631 | ||
28060d5d AV |
8632 | if (iov_iter_alignment(iter) & blocksize_mask) |
8633 | goto out; | |
a1b75f7d | 8634 | |
28060d5d | 8635 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8636 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8637 | return 0; |
8638 | /* | |
8639 | * Check to make sure we don't have duplicate iov_base's in this | |
8640 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8641 | * when reading back. | |
8642 | */ | |
8643 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8644 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8645 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8646 | goto out; |
8647 | } | |
5a5f79b5 CM |
8648 | } |
8649 | retval = 0; | |
8650 | out: | |
8651 | return retval; | |
8652 | } | |
eb838e73 | 8653 | |
c8b8e32d | 8654 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8655 | { |
4b46fce2 JB |
8656 | struct file *file = iocb->ki_filp; |
8657 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8658 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8659 | struct btrfs_dio_data dio_data = { 0 }; |
364ecf36 | 8660 | struct extent_changeset *data_reserved = NULL; |
c8b8e32d | 8661 | loff_t offset = iocb->ki_pos; |
0934856d | 8662 | size_t count = 0; |
2e60a51e | 8663 | int flags = 0; |
38851cc1 MX |
8664 | bool wakeup = true; |
8665 | bool relock = false; | |
0934856d | 8666 | ssize_t ret; |
4b46fce2 | 8667 | |
8c70c9f8 | 8668 | if (check_direct_IO(fs_info, iter, offset)) |
5a5f79b5 | 8669 | return 0; |
3f7c579c | 8670 | |
fe0f07d0 | 8671 | inode_dio_begin(inode); |
38851cc1 | 8672 | |
0e267c44 | 8673 | /* |
41bd9ca4 MX |
8674 | * The generic stuff only does filemap_write_and_wait_range, which |
8675 | * isn't enough if we've written compressed pages to this area, so | |
8676 | * we need to flush the dirty pages again to make absolutely sure | |
8677 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8678 | */ |
a6cbcd4a | 8679 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8680 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8681 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8682 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8683 | offset + count - 1); | |
0e267c44 | 8684 | |
6f673763 | 8685 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8686 | /* |
8687 | * If the write DIO is beyond the EOF, we need update | |
8688 | * the isize, but it is protected by i_mutex. So we can | |
8689 | * not unlock the i_mutex at this case. | |
8690 | */ | |
8691 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8692 | dio_data.overwrite = 1; |
5955102c | 8693 | inode_unlock(inode); |
38851cc1 | 8694 | relock = true; |
edf064e7 GR |
8695 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
8696 | ret = -EAGAIN; | |
8697 | goto out; | |
38851cc1 | 8698 | } |
364ecf36 QW |
8699 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8700 | offset, count); | |
0934856d | 8701 | if (ret) |
38851cc1 | 8702 | goto out; |
e1cbbfa5 JB |
8703 | |
8704 | /* | |
8705 | * We need to know how many extents we reserved so that we can | |
8706 | * do the accounting properly if we go over the number we | |
8707 | * originally calculated. Abuse current->journal_info for this. | |
8708 | */ | |
da17066c | 8709 | dio_data.reserve = round_up(count, |
0b246afa | 8710 | fs_info->sectorsize); |
f28a4928 FM |
8711 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8712 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8713 | current->journal_info = &dio_data; |
97dcdea0 | 8714 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8715 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8716 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8717 | inode_dio_end(inode); |
38851cc1 MX |
8718 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8719 | wakeup = false; | |
0934856d MX |
8720 | } |
8721 | ||
17f8c842 | 8722 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8723 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8724 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8725 | btrfs_submit_direct, flags); |
6f673763 | 8726 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8727 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8728 | current->journal_info = NULL; |
ddba1bfc | 8729 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8730 | if (dio_data.reserve) |
bc42bda2 | 8731 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 | 8732 | offset, dio_data.reserve, true); |
f28a4928 FM |
8733 | /* |
8734 | * On error we might have left some ordered extents | |
8735 | * without submitting corresponding bios for them, so | |
8736 | * cleanup them up to avoid other tasks getting them | |
8737 | * and waiting for them to complete forever. | |
8738 | */ | |
8739 | if (dio_data.unsubmitted_oe_range_start < | |
8740 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8741 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8742 | dio_data.unsubmitted_oe_range_start, |
8743 | dio_data.unsubmitted_oe_range_end - | |
8744 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8745 | false); |
ddba1bfc | 8746 | } else if (ret >= 0 && (size_t)ret < count) |
bc42bda2 | 8747 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 | 8748 | offset, count - (size_t)ret, true); |
8702ba93 | 8749 | btrfs_delalloc_release_extents(BTRFS_I(inode), count); |
0934856d | 8750 | } |
38851cc1 | 8751 | out: |
2e60a51e | 8752 | if (wakeup) |
fe0f07d0 | 8753 | inode_dio_end(inode); |
38851cc1 | 8754 | if (relock) |
5955102c | 8755 | inode_lock(inode); |
0934856d | 8756 | |
364ecf36 | 8757 | extent_changeset_free(data_reserved); |
0934856d | 8758 | return ret; |
16432985 CM |
8759 | } |
8760 | ||
05dadc09 TI |
8761 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8762 | ||
1506fcc8 YS |
8763 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8764 | __u64 start, __u64 len) | |
8765 | { | |
05dadc09 TI |
8766 | int ret; |
8767 | ||
8768 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8769 | if (ret) | |
8770 | return ret; | |
8771 | ||
2135fb9b | 8772 | return extent_fiemap(inode, fieinfo, start, len); |
1506fcc8 YS |
8773 | } |
8774 | ||
a52d9a80 | 8775 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8776 | { |
d1310b2e CM |
8777 | struct extent_io_tree *tree; |
8778 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8779 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8780 | } |
1832a6d5 | 8781 | |
a52d9a80 | 8782 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8783 | { |
be7bd730 JB |
8784 | struct inode *inode = page->mapping->host; |
8785 | int ret; | |
b888db2b CM |
8786 | |
8787 | if (current->flags & PF_MEMALLOC) { | |
8788 | redirty_page_for_writepage(wbc, page); | |
8789 | unlock_page(page); | |
8790 | return 0; | |
8791 | } | |
be7bd730 JB |
8792 | |
8793 | /* | |
8794 | * If we are under memory pressure we will call this directly from the | |
8795 | * VM, we need to make sure we have the inode referenced for the ordered | |
8796 | * extent. If not just return like we didn't do anything. | |
8797 | */ | |
8798 | if (!igrab(inode)) { | |
8799 | redirty_page_for_writepage(wbc, page); | |
8800 | return AOP_WRITEPAGE_ACTIVATE; | |
8801 | } | |
0a9b0e53 | 8802 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8803 | btrfs_add_delayed_iput(inode); |
8804 | return ret; | |
9ebefb18 CM |
8805 | } |
8806 | ||
48a3b636 ES |
8807 | static int btrfs_writepages(struct address_space *mapping, |
8808 | struct writeback_control *wbc) | |
b293f02e | 8809 | { |
8ae225a8 | 8810 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8811 | } |
8812 | ||
3ab2fb5a CM |
8813 | static int |
8814 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8815 | struct list_head *pages, unsigned nr_pages) | |
8816 | { | |
2a3ff0ad | 8817 | return extent_readpages(mapping, pages, nr_pages); |
3ab2fb5a | 8818 | } |
2a3ff0ad | 8819 | |
e6dcd2dc | 8820 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8821 | { |
477a30ba | 8822 | int ret = try_release_extent_mapping(page, gfp_flags); |
a52d9a80 CM |
8823 | if (ret == 1) { |
8824 | ClearPagePrivate(page); | |
8825 | set_page_private(page, 0); | |
09cbfeaf | 8826 | put_page(page); |
39279cc3 | 8827 | } |
a52d9a80 | 8828 | return ret; |
39279cc3 CM |
8829 | } |
8830 | ||
e6dcd2dc CM |
8831 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8832 | { | |
98509cfc CM |
8833 | if (PageWriteback(page) || PageDirty(page)) |
8834 | return 0; | |
3ba7ab22 | 8835 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8836 | } |
8837 | ||
d47992f8 LC |
8838 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8839 | unsigned int length) | |
39279cc3 | 8840 | { |
5fd02043 | 8841 | struct inode *inode = page->mapping->host; |
d1310b2e | 8842 | struct extent_io_tree *tree; |
e6dcd2dc | 8843 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8844 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8845 | u64 page_start = page_offset(page); |
09cbfeaf | 8846 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8847 | u64 start; |
8848 | u64 end; | |
131e404a | 8849 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8850 | |
8b62b72b CM |
8851 | /* |
8852 | * we have the page locked, so new writeback can't start, | |
8853 | * and the dirty bit won't be cleared while we are here. | |
8854 | * | |
8855 | * Wait for IO on this page so that we can safely clear | |
8856 | * the PagePrivate2 bit and do ordered accounting | |
8857 | */ | |
e6dcd2dc | 8858 | wait_on_page_writeback(page); |
8b62b72b | 8859 | |
5fd02043 | 8860 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8861 | if (offset) { |
8862 | btrfs_releasepage(page, GFP_NOFS); | |
8863 | return; | |
8864 | } | |
131e404a FDBM |
8865 | |
8866 | if (!inode_evicting) | |
ff13db41 | 8867 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8868 | again: |
8869 | start = page_start; | |
a776c6fa | 8870 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8871 | page_end - start + 1); |
e6dcd2dc | 8872 | if (ordered) { |
dbfdb6d1 | 8873 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8874 | /* |
8875 | * IO on this page will never be started, so we need | |
8876 | * to account for any ordered extents now | |
8877 | */ | |
131e404a | 8878 | if (!inode_evicting) |
dbfdb6d1 | 8879 | clear_extent_bit(tree, start, end, |
e182163d | 8880 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
131e404a | 8881 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8882 | EXTENT_DEFRAG, 1, 0, &cached_state); |
8b62b72b CM |
8883 | /* |
8884 | * whoever cleared the private bit is responsible | |
8885 | * for the finish_ordered_io | |
8886 | */ | |
77cef2ec JB |
8887 | if (TestClearPagePrivate2(page)) { |
8888 | struct btrfs_ordered_inode_tree *tree; | |
8889 | u64 new_len; | |
8890 | ||
8891 | tree = &BTRFS_I(inode)->ordered_tree; | |
8892 | ||
8893 | spin_lock_irq(&tree->lock); | |
8894 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8895 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8896 | if (new_len < ordered->truncated_len) |
8897 | ordered->truncated_len = new_len; | |
8898 | spin_unlock_irq(&tree->lock); | |
8899 | ||
8900 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8901 | start, |
8902 | end - start + 1, 1)) | |
77cef2ec | 8903 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8904 | } |
e6dcd2dc | 8905 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8906 | if (!inode_evicting) { |
8907 | cached_state = NULL; | |
dbfdb6d1 | 8908 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8909 | &cached_state); |
8910 | } | |
dbfdb6d1 CR |
8911 | |
8912 | start = end + 1; | |
8913 | if (start < page_end) | |
8914 | goto again; | |
131e404a FDBM |
8915 | } |
8916 | ||
b9d0b389 QW |
8917 | /* |
8918 | * Qgroup reserved space handler | |
8919 | * Page here will be either | |
8920 | * 1) Already written to disk | |
8921 | * In this case, its reserved space is released from data rsv map | |
8922 | * and will be freed by delayed_ref handler finally. | |
8923 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8924 | * space. | |
8925 | * 2) Not written to disk | |
0b34c261 GR |
8926 | * This means the reserved space should be freed here. However, |
8927 | * if a truncate invalidates the page (by clearing PageDirty) | |
8928 | * and the page is accounted for while allocating extent | |
8929 | * in btrfs_check_data_free_space() we let delayed_ref to | |
8930 | * free the entire extent. | |
b9d0b389 | 8931 | */ |
0b34c261 | 8932 | if (PageDirty(page)) |
bc42bda2 | 8933 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a | 8934 | if (!inode_evicting) { |
e182163d | 8935 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
a7e3b975 FM |
8936 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
8937 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
ae0f1625 | 8938 | &cached_state); |
131e404a FDBM |
8939 | |
8940 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8941 | } |
e6dcd2dc | 8942 | |
4a096752 | 8943 | ClearPageChecked(page); |
9ad6b7bc | 8944 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8945 | ClearPagePrivate(page); |
8946 | set_page_private(page, 0); | |
09cbfeaf | 8947 | put_page(page); |
9ad6b7bc | 8948 | } |
39279cc3 CM |
8949 | } |
8950 | ||
9ebefb18 CM |
8951 | /* |
8952 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8953 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8954 | * be careful to check for EOF conditions here. We set the page up correctly | |
8955 | * for a written page which means we get ENOSPC checking when writing into | |
8956 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8957 | * support these features. | |
8958 | * | |
8959 | * We are not allowed to take the i_mutex here so we have to play games to | |
8960 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8961 | * truncate_setsize() writes the inode size before removing pages, once we have |
8962 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8963 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8964 | * unlock the page. | |
8965 | */ | |
a528a241 | 8966 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8967 | { |
c2ec175c | 8968 | struct page *page = vmf->page; |
11bac800 | 8969 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8970 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8971 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8972 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8973 | struct extent_state *cached_state = NULL; |
364ecf36 | 8974 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
8975 | char *kaddr; |
8976 | unsigned long zero_start; | |
9ebefb18 | 8977 | loff_t size; |
a528a241 SJ |
8978 | vm_fault_t ret; |
8979 | int ret2; | |
9998eb70 | 8980 | int reserved = 0; |
d0b7da88 | 8981 | u64 reserved_space; |
a52d9a80 | 8982 | u64 page_start; |
e6dcd2dc | 8983 | u64 page_end; |
d0b7da88 CR |
8984 | u64 end; |
8985 | ||
09cbfeaf | 8986 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8987 | |
b2b5ef5c | 8988 | sb_start_pagefault(inode->i_sb); |
df480633 | 8989 | page_start = page_offset(page); |
09cbfeaf | 8990 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8991 | end = page_end; |
df480633 | 8992 | |
d0b7da88 CR |
8993 | /* |
8994 | * Reserving delalloc space after obtaining the page lock can lead to | |
8995 | * deadlock. For example, if a dirty page is locked by this function | |
8996 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8997 | * dirty page write out, then the btrfs_writepage() function could | |
8998 | * end up waiting indefinitely to get a lock on the page currently | |
8999 | * being processed by btrfs_page_mkwrite() function. | |
9000 | */ | |
a528a241 | 9001 | ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 9002 | reserved_space); |
a528a241 SJ |
9003 | if (!ret2) { |
9004 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
9005 | reserved = 1; |
9006 | } | |
a528a241 SJ |
9007 | if (ret2) { |
9008 | ret = vmf_error(ret2); | |
9998eb70 CM |
9009 | if (reserved) |
9010 | goto out; | |
9011 | goto out_noreserve; | |
56a76f82 | 9012 | } |
1832a6d5 | 9013 | |
56a76f82 | 9014 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9015 | again: |
9ebefb18 | 9016 | lock_page(page); |
9ebefb18 | 9017 | size = i_size_read(inode); |
a52d9a80 | 9018 | |
9ebefb18 | 9019 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9020 | (page_start >= size)) { |
9ebefb18 CM |
9021 | /* page got truncated out from underneath us */ |
9022 | goto out_unlock; | |
9023 | } | |
e6dcd2dc CM |
9024 | wait_on_page_writeback(page); |
9025 | ||
ff13db41 | 9026 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9027 | set_page_extent_mapped(page); |
9028 | ||
eb84ae03 CM |
9029 | /* |
9030 | * we can't set the delalloc bits if there are pending ordered | |
9031 | * extents. Drop our locks and wait for them to finish | |
9032 | */ | |
a776c6fa NB |
9033 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
9034 | PAGE_SIZE); | |
e6dcd2dc | 9035 | if (ordered) { |
2ac55d41 | 9036 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 9037 | &cached_state); |
e6dcd2dc | 9038 | unlock_page(page); |
eb84ae03 | 9039 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9040 | btrfs_put_ordered_extent(ordered); |
9041 | goto again; | |
9042 | } | |
9043 | ||
09cbfeaf | 9044 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9045 | reserved_space = round_up(size - page_start, |
0b246afa | 9046 | fs_info->sectorsize); |
09cbfeaf | 9047 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 9048 | end = page_start + reserved_space - 1; |
bc42bda2 | 9049 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 QW |
9050 | page_start, PAGE_SIZE - reserved_space, |
9051 | true); | |
d0b7da88 CR |
9052 | } |
9053 | } | |
9054 | ||
fbf19087 | 9055 | /* |
5416034f LB |
9056 | * page_mkwrite gets called when the page is firstly dirtied after it's |
9057 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9058 | * bits, thus in this case for space account reason, we still need to | |
9059 | * clear any delalloc bits within this page range since we have to | |
9060 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 9061 | */ |
d0b7da88 | 9062 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
e182163d OS |
9063 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
9064 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
fbf19087 | 9065 | |
a528a241 | 9066 | ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0, |
330a5827 | 9067 | &cached_state); |
a528a241 | 9068 | if (ret2) { |
2ac55d41 | 9069 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 9070 | &cached_state); |
9ed74f2d JB |
9071 | ret = VM_FAULT_SIGBUS; |
9072 | goto out_unlock; | |
9073 | } | |
a528a241 | 9074 | ret2 = 0; |
9ebefb18 CM |
9075 | |
9076 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 9077 | if (page_start + PAGE_SIZE > size) |
7073017a | 9078 | zero_start = offset_in_page(size); |
9ebefb18 | 9079 | else |
09cbfeaf | 9080 | zero_start = PAGE_SIZE; |
9ebefb18 | 9081 | |
09cbfeaf | 9082 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9083 | kaddr = kmap(page); |
09cbfeaf | 9084 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9085 | flush_dcache_page(page); |
9086 | kunmap(page); | |
9087 | } | |
247e743c | 9088 | ClearPageChecked(page); |
e6dcd2dc | 9089 | set_page_dirty(page); |
50a9b214 | 9090 | SetPageUptodate(page); |
5a3f23d5 | 9091 | |
0b246afa | 9092 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9093 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9094 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9095 | |
e43bbe5e | 9096 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
9ebefb18 | 9097 | |
a528a241 | 9098 | if (!ret2) { |
8702ba93 | 9099 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
b2b5ef5c | 9100 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9101 | extent_changeset_free(data_reserved); |
50a9b214 | 9102 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9103 | } |
717beb96 CM |
9104 | |
9105 | out_unlock: | |
9ebefb18 | 9106 | unlock_page(page); |
1832a6d5 | 9107 | out: |
8702ba93 | 9108 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
bc42bda2 | 9109 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
43b18595 | 9110 | reserved_space, (ret != 0)); |
9998eb70 | 9111 | out_noreserve: |
b2b5ef5c | 9112 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9113 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
9114 | return ret; |
9115 | } | |
9116 | ||
213e8c55 | 9117 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 9118 | { |
0b246afa | 9119 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9120 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9121 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 9122 | int ret; |
39279cc3 | 9123 | struct btrfs_trans_handle *trans; |
0b246afa | 9124 | u64 mask = fs_info->sectorsize - 1; |
2bd36e7b | 9125 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
39279cc3 | 9126 | |
213e8c55 FM |
9127 | if (!skip_writeback) { |
9128 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
9129 | (u64)-1); | |
9130 | if (ret) | |
9131 | return ret; | |
9132 | } | |
39279cc3 | 9133 | |
fcb80c2a | 9134 | /* |
f7e9e8fc OS |
9135 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
9136 | * things going on here: | |
fcb80c2a | 9137 | * |
f7e9e8fc | 9138 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 9139 | * |
f7e9e8fc | 9140 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
9141 | * be free'd up by the truncate operation, but also have some slack |
9142 | * space reserved in case it uses space during the truncate (thank you | |
9143 | * very much snapshotting). | |
9144 | * | |
f7e9e8fc | 9145 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 9146 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9147 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
9148 | * doesn't end up using space reserved for updating the inode. We also |
9149 | * need to be able to stop the transaction and start a new one, which | |
9150 | * means we need to be able to update the inode several times, and we | |
9151 | * have no idea of knowing how many times that will be, so we can't just | |
9152 | * reserve 1 item for the entirety of the operation, so that has to be | |
9153 | * done separately as well. | |
fcb80c2a JB |
9154 | * |
9155 | * So that leaves us with | |
9156 | * | |
f7e9e8fc | 9157 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 9158 | * transaction reservation. |
f7e9e8fc | 9159 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
9160 | * updating the inode. |
9161 | */ | |
2ff7e61e | 9162 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9163 | if (!rsv) |
9164 | return -ENOMEM; | |
4a338542 | 9165 | rsv->size = min_size; |
ca7e70f5 | 9166 | rsv->failfast = 1; |
f0cd846e | 9167 | |
907cbceb | 9168 | /* |
07127184 | 9169 | * 1 for the truncate slack space |
907cbceb JB |
9170 | * 1 for updating the inode. |
9171 | */ | |
f3fe820c | 9172 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 9173 | if (IS_ERR(trans)) { |
ad7e1a74 | 9174 | ret = PTR_ERR(trans); |
fcb80c2a JB |
9175 | goto out; |
9176 | } | |
f0cd846e | 9177 | |
907cbceb | 9178 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9179 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 9180 | min_size, false); |
fcb80c2a | 9181 | BUG_ON(ret); |
f0cd846e | 9182 | |
5dc562c5 JB |
9183 | /* |
9184 | * So if we truncate and then write and fsync we normally would just | |
9185 | * write the extents that changed, which is a problem if we need to | |
9186 | * first truncate that entire inode. So set this flag so we write out | |
9187 | * all of the extents in the inode to the sync log so we're completely | |
9188 | * safe. | |
9189 | */ | |
9190 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9191 | trans->block_rsv = rsv; |
907cbceb | 9192 | |
8082510e YZ |
9193 | while (1) { |
9194 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9195 | inode->i_size, | |
9196 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 9197 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 9198 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 9199 | break; |
39279cc3 | 9200 | |
8082510e | 9201 | ret = btrfs_update_inode(trans, root, inode); |
ad7e1a74 | 9202 | if (ret) |
3893e33b | 9203 | break; |
ca7e70f5 | 9204 | |
3a45bb20 | 9205 | btrfs_end_transaction(trans); |
2ff7e61e | 9206 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9207 | |
9208 | trans = btrfs_start_transaction(root, 2); | |
9209 | if (IS_ERR(trans)) { | |
ad7e1a74 | 9210 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
9211 | trans = NULL; |
9212 | break; | |
9213 | } | |
9214 | ||
47b5d646 | 9215 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9216 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 9217 | rsv, min_size, false); |
ca7e70f5 JB |
9218 | BUG_ON(ret); /* shouldn't happen */ |
9219 | trans->block_rsv = rsv; | |
8082510e YZ |
9220 | } |
9221 | ||
ddfae63c JB |
9222 | /* |
9223 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
9224 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
9225 | * we've truncated everything except the last little bit, and can do | |
9226 | * btrfs_truncate_block and then update the disk_i_size. | |
9227 | */ | |
9228 | if (ret == NEED_TRUNCATE_BLOCK) { | |
9229 | btrfs_end_transaction(trans); | |
9230 | btrfs_btree_balance_dirty(fs_info); | |
9231 | ||
9232 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
9233 | if (ret) | |
9234 | goto out; | |
9235 | trans = btrfs_start_transaction(root, 1); | |
9236 | if (IS_ERR(trans)) { | |
9237 | ret = PTR_ERR(trans); | |
9238 | goto out; | |
9239 | } | |
9240 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); | |
9241 | } | |
9242 | ||
917c16b2 | 9243 | if (trans) { |
ad7e1a74 OS |
9244 | int ret2; |
9245 | ||
0b246afa | 9246 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 OS |
9247 | ret2 = btrfs_update_inode(trans, root, inode); |
9248 | if (ret2 && !ret) | |
9249 | ret = ret2; | |
7b128766 | 9250 | |
ad7e1a74 OS |
9251 | ret2 = btrfs_end_transaction(trans); |
9252 | if (ret2 && !ret) | |
9253 | ret = ret2; | |
2ff7e61e | 9254 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9255 | } |
fcb80c2a | 9256 | out: |
2ff7e61e | 9257 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9258 | |
ad7e1a74 | 9259 | return ret; |
39279cc3 CM |
9260 | } |
9261 | ||
d352ac68 CM |
9262 | /* |
9263 | * create a new subvolume directory/inode (helper for the ioctl). | |
9264 | */ | |
d2fb3437 | 9265 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9266 | struct btrfs_root *new_root, |
9267 | struct btrfs_root *parent_root, | |
9268 | u64 new_dirid) | |
39279cc3 | 9269 | { |
39279cc3 | 9270 | struct inode *inode; |
76dda93c | 9271 | int err; |
00e4e6b3 | 9272 | u64 index = 0; |
39279cc3 | 9273 | |
12fc9d09 FA |
9274 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9275 | new_dirid, new_dirid, | |
9276 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9277 | &index); | |
54aa1f4d | 9278 | if (IS_ERR(inode)) |
f46b5a66 | 9279 | return PTR_ERR(inode); |
39279cc3 CM |
9280 | inode->i_op = &btrfs_dir_inode_operations; |
9281 | inode->i_fop = &btrfs_dir_file_operations; | |
9282 | ||
bfe86848 | 9283 | set_nlink(inode, 1); |
6ef06d27 | 9284 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9285 | unlock_new_inode(inode); |
3b96362c | 9286 | |
63541927 FDBM |
9287 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9288 | if (err) | |
9289 | btrfs_err(new_root->fs_info, | |
351fd353 | 9290 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9291 | new_root->root_key.objectid, err); |
9292 | ||
76dda93c | 9293 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9294 | |
76dda93c | 9295 | iput(inode); |
ce598979 | 9296 | return err; |
39279cc3 CM |
9297 | } |
9298 | ||
39279cc3 CM |
9299 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9300 | { | |
69fe2d75 | 9301 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 9302 | struct btrfs_inode *ei; |
2ead6ae7 | 9303 | struct inode *inode; |
39279cc3 | 9304 | |
712e36c5 | 9305 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
9306 | if (!ei) |
9307 | return NULL; | |
2ead6ae7 YZ |
9308 | |
9309 | ei->root = NULL; | |
2ead6ae7 | 9310 | ei->generation = 0; |
15ee9bc7 | 9311 | ei->last_trans = 0; |
257c62e1 | 9312 | ei->last_sub_trans = 0; |
e02119d5 | 9313 | ei->logged_trans = 0; |
2ead6ae7 | 9314 | ei->delalloc_bytes = 0; |
a7e3b975 | 9315 | ei->new_delalloc_bytes = 0; |
47059d93 | 9316 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9317 | ei->disk_i_size = 0; |
9318 | ei->flags = 0; | |
7709cde3 | 9319 | ei->csum_bytes = 0; |
2ead6ae7 | 9320 | ei->index_cnt = (u64)-1; |
67de1176 | 9321 | ei->dir_index = 0; |
2ead6ae7 | 9322 | ei->last_unlink_trans = 0; |
46d8bc34 | 9323 | ei->last_log_commit = 0; |
2ead6ae7 | 9324 | |
9e0baf60 JB |
9325 | spin_lock_init(&ei->lock); |
9326 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
9327 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
9328 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
9329 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 9330 | ei->runtime_flags = 0; |
b52aa8c9 | 9331 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 9332 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9333 | |
16cdcec7 MX |
9334 | ei->delayed_node = NULL; |
9335 | ||
9cc97d64 | 9336 | ei->i_otime.tv_sec = 0; |
9337 | ei->i_otime.tv_nsec = 0; | |
9338 | ||
2ead6ae7 | 9339 | inode = &ei->vfs_inode; |
a8067e02 | 9340 | extent_map_tree_init(&ei->extent_tree); |
43eb5f29 QW |
9341 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
9342 | extent_io_tree_init(fs_info, &ei->io_failure_tree, | |
9343 | IO_TREE_INODE_IO_FAILURE, inode); | |
7b439738 DS |
9344 | ei->io_tree.track_uptodate = true; |
9345 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 9346 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9347 | mutex_init(&ei->log_mutex); |
e6dcd2dc | 9348 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9349 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9350 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9351 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9352 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9353 | |
9354 | return inode; | |
39279cc3 CM |
9355 | } |
9356 | ||
aaedb55b JB |
9357 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9358 | void btrfs_test_destroy_inode(struct inode *inode) | |
9359 | { | |
dcdbc059 | 9360 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9361 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9362 | } | |
9363 | #endif | |
9364 | ||
26602cab | 9365 | void btrfs_free_inode(struct inode *inode) |
fa0d7e3d | 9366 | { |
fa0d7e3d NP |
9367 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9368 | } | |
9369 | ||
39279cc3 CM |
9370 | void btrfs_destroy_inode(struct inode *inode) |
9371 | { | |
0b246afa | 9372 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9373 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9374 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9375 | ||
b3d9b7a3 | 9376 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9377 | WARN_ON(inode->i_data.nrpages); |
69fe2d75 JB |
9378 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
9379 | WARN_ON(BTRFS_I(inode)->block_rsv.size); | |
9e0baf60 | 9380 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7709cde3 | 9381 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9382 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9383 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9384 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9385 | |
a6dbd429 JB |
9386 | /* |
9387 | * This can happen where we create an inode, but somebody else also | |
9388 | * created the same inode and we need to destroy the one we already | |
9389 | * created. | |
9390 | */ | |
9391 | if (!root) | |
26602cab | 9392 | return; |
a6dbd429 | 9393 | |
d397712b | 9394 | while (1) { |
e6dcd2dc CM |
9395 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9396 | if (!ordered) | |
9397 | break; | |
9398 | else { | |
0b246afa | 9399 | btrfs_err(fs_info, |
5d163e0e JM |
9400 | "found ordered extent %llu %llu on inode cleanup", |
9401 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9402 | btrfs_remove_ordered_extent(inode, ordered); |
9403 | btrfs_put_ordered_extent(ordered); | |
9404 | btrfs_put_ordered_extent(ordered); | |
9405 | } | |
9406 | } | |
56fa9d07 | 9407 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9408 | inode_tree_del(inode); |
dcdbc059 | 9409 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
39279cc3 CM |
9410 | } |
9411 | ||
45321ac5 | 9412 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9413 | { |
9414 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9415 | |
6379ef9f NA |
9416 | if (root == NULL) |
9417 | return 1; | |
9418 | ||
fa6ac876 | 9419 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9420 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9421 | return 1; |
76dda93c | 9422 | else |
45321ac5 | 9423 | return generic_drop_inode(inode); |
76dda93c YZ |
9424 | } |
9425 | ||
0ee0fda0 | 9426 | static void init_once(void *foo) |
39279cc3 CM |
9427 | { |
9428 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9429 | ||
9430 | inode_init_once(&ei->vfs_inode); | |
9431 | } | |
9432 | ||
e67c718b | 9433 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 9434 | { |
8c0a8537 KS |
9435 | /* |
9436 | * Make sure all delayed rcu free inodes are flushed before we | |
9437 | * destroy cache. | |
9438 | */ | |
9439 | rcu_barrier(); | |
5598e900 KM |
9440 | kmem_cache_destroy(btrfs_inode_cachep); |
9441 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9442 | kmem_cache_destroy(btrfs_path_cachep); |
9443 | kmem_cache_destroy(btrfs_free_space_cachep); | |
3acd4850 | 9444 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
39279cc3 CM |
9445 | } |
9446 | ||
f5c29bd9 | 9447 | int __init btrfs_init_cachep(void) |
39279cc3 | 9448 | { |
837e1972 | 9449 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9450 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9451 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9452 | init_once); | |
39279cc3 CM |
9453 | if (!btrfs_inode_cachep) |
9454 | goto fail; | |
9601e3f6 | 9455 | |
837e1972 | 9456 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9457 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9458 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9459 | if (!btrfs_trans_handle_cachep) |
9460 | goto fail; | |
9601e3f6 | 9461 | |
837e1972 | 9462 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9463 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9464 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9465 | if (!btrfs_path_cachep) |
9466 | goto fail; | |
9601e3f6 | 9467 | |
837e1972 | 9468 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9469 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9470 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9471 | if (!btrfs_free_space_cachep) |
9472 | goto fail; | |
9473 | ||
3acd4850 CL |
9474 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
9475 | PAGE_SIZE, PAGE_SIZE, | |
9476 | SLAB_RED_ZONE, NULL); | |
9477 | if (!btrfs_free_space_bitmap_cachep) | |
9478 | goto fail; | |
9479 | ||
39279cc3 CM |
9480 | return 0; |
9481 | fail: | |
9482 | btrfs_destroy_cachep(); | |
9483 | return -ENOMEM; | |
9484 | } | |
9485 | ||
a528d35e DH |
9486 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9487 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9488 | { |
df0af1a5 | 9489 | u64 delalloc_bytes; |
a528d35e | 9490 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9491 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9492 | u32 bi_flags = BTRFS_I(inode)->flags; |
9493 | ||
9494 | stat->result_mask |= STATX_BTIME; | |
9495 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9496 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9497 | if (bi_flags & BTRFS_INODE_APPEND) | |
9498 | stat->attributes |= STATX_ATTR_APPEND; | |
9499 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9500 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9501 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9502 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9503 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9504 | stat->attributes |= STATX_ATTR_NODUMP; | |
9505 | ||
9506 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9507 | STATX_ATTR_COMPRESSED | | |
9508 | STATX_ATTR_IMMUTABLE | | |
9509 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9510 | |
39279cc3 | 9511 | generic_fillattr(inode, stat); |
0ee5dc67 | 9512 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9513 | |
9514 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9515 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9516 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9517 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9518 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9519 | return 0; |
9520 | } | |
9521 | ||
cdd1fedf DF |
9522 | static int btrfs_rename_exchange(struct inode *old_dir, |
9523 | struct dentry *old_dentry, | |
9524 | struct inode *new_dir, | |
9525 | struct dentry *new_dentry) | |
9526 | { | |
0b246afa | 9527 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9528 | struct btrfs_trans_handle *trans; |
9529 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9530 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9531 | struct inode *new_inode = new_dentry->d_inode; | |
9532 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 9533 | struct timespec64 ctime = current_time(old_inode); |
cdd1fedf | 9534 | struct dentry *parent; |
4a0cc7ca NB |
9535 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9536 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9537 | u64 old_idx = 0; |
9538 | u64 new_idx = 0; | |
9539 | u64 root_objectid; | |
9540 | int ret; | |
86e8aa0e FM |
9541 | bool root_log_pinned = false; |
9542 | bool dest_log_pinned = false; | |
d4682ba0 FM |
9543 | struct btrfs_log_ctx ctx_root; |
9544 | struct btrfs_log_ctx ctx_dest; | |
9545 | bool sync_log_root = false; | |
9546 | bool sync_log_dest = false; | |
9547 | bool commit_transaction = false; | |
cdd1fedf DF |
9548 | |
9549 | /* we only allow rename subvolume link between subvolumes */ | |
9550 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9551 | return -EXDEV; | |
9552 | ||
d4682ba0 FM |
9553 | btrfs_init_log_ctx(&ctx_root, old_inode); |
9554 | btrfs_init_log_ctx(&ctx_dest, new_inode); | |
9555 | ||
cdd1fedf | 9556 | /* close the race window with snapshot create/destroy ioctl */ |
943eb3bf JB |
9557 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
9558 | new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9559 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9560 | |
9561 | /* | |
9562 | * We want to reserve the absolute worst case amount of items. So if | |
9563 | * both inodes are subvols and we need to unlink them then that would | |
9564 | * require 4 item modifications, but if they are both normal inodes it | |
9565 | * would require 5 item modifications, so we'll assume their normal | |
9566 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9567 | * should cover the worst case number of items we'll modify. | |
9568 | */ | |
9569 | trans = btrfs_start_transaction(root, 12); | |
9570 | if (IS_ERR(trans)) { | |
9571 | ret = PTR_ERR(trans); | |
9572 | goto out_notrans; | |
9573 | } | |
9574 | ||
3e174099 JB |
9575 | if (dest != root) |
9576 | btrfs_record_root_in_trans(trans, dest); | |
9577 | ||
cdd1fedf DF |
9578 | /* |
9579 | * We need to find a free sequence number both in the source and | |
9580 | * in the destination directory for the exchange. | |
9581 | */ | |
877574e2 | 9582 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9583 | if (ret) |
9584 | goto out_fail; | |
877574e2 | 9585 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9586 | if (ret) |
9587 | goto out_fail; | |
9588 | ||
9589 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9590 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9591 | ||
9592 | /* Reference for the source. */ | |
9593 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9594 | /* force full log commit if subvolume involved. */ | |
90787766 | 9595 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 9596 | } else { |
376e5a57 FM |
9597 | btrfs_pin_log_trans(root); |
9598 | root_log_pinned = true; | |
cdd1fedf DF |
9599 | ret = btrfs_insert_inode_ref(trans, dest, |
9600 | new_dentry->d_name.name, | |
9601 | new_dentry->d_name.len, | |
9602 | old_ino, | |
f85b7379 DS |
9603 | btrfs_ino(BTRFS_I(new_dir)), |
9604 | old_idx); | |
cdd1fedf DF |
9605 | if (ret) |
9606 | goto out_fail; | |
cdd1fedf DF |
9607 | } |
9608 | ||
9609 | /* And now for the dest. */ | |
9610 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9611 | /* force full log commit if subvolume involved. */ | |
90787766 | 9612 | btrfs_set_log_full_commit(trans); |
cdd1fedf | 9613 | } else { |
376e5a57 FM |
9614 | btrfs_pin_log_trans(dest); |
9615 | dest_log_pinned = true; | |
cdd1fedf DF |
9616 | ret = btrfs_insert_inode_ref(trans, root, |
9617 | old_dentry->d_name.name, | |
9618 | old_dentry->d_name.len, | |
9619 | new_ino, | |
f85b7379 DS |
9620 | btrfs_ino(BTRFS_I(old_dir)), |
9621 | new_idx); | |
cdd1fedf DF |
9622 | if (ret) |
9623 | goto out_fail; | |
cdd1fedf DF |
9624 | } |
9625 | ||
9626 | /* Update inode version and ctime/mtime. */ | |
9627 | inode_inc_iversion(old_dir); | |
9628 | inode_inc_iversion(new_dir); | |
9629 | inode_inc_iversion(old_inode); | |
9630 | inode_inc_iversion(new_inode); | |
9631 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9632 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9633 | old_inode->i_ctime = ctime; | |
9634 | new_inode->i_ctime = ctime; | |
9635 | ||
9636 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9637 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9638 | BTRFS_I(old_inode), 1); | |
9639 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9640 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9641 | } |
9642 | ||
9643 | /* src is a subvolume */ | |
9644 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9645 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
401b3b19 | 9646 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
cdd1fedf DF |
9647 | old_dentry->d_name.name, |
9648 | old_dentry->d_name.len); | |
9649 | } else { /* src is an inode */ | |
4ec5934e NB |
9650 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9651 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9652 | old_dentry->d_name.name, |
9653 | old_dentry->d_name.len); | |
9654 | if (!ret) | |
9655 | ret = btrfs_update_inode(trans, root, old_inode); | |
9656 | } | |
9657 | if (ret) { | |
66642832 | 9658 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9659 | goto out_fail; |
9660 | } | |
9661 | ||
9662 | /* dest is a subvolume */ | |
9663 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9664 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
401b3b19 | 9665 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
cdd1fedf DF |
9666 | new_dentry->d_name.name, |
9667 | new_dentry->d_name.len); | |
9668 | } else { /* dest is an inode */ | |
4ec5934e NB |
9669 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9670 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9671 | new_dentry->d_name.name, |
9672 | new_dentry->d_name.len); | |
9673 | if (!ret) | |
9674 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9675 | } | |
9676 | if (ret) { | |
66642832 | 9677 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9678 | goto out_fail; |
9679 | } | |
9680 | ||
db0a669f | 9681 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9682 | new_dentry->d_name.name, |
9683 | new_dentry->d_name.len, 0, old_idx); | |
9684 | if (ret) { | |
66642832 | 9685 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9686 | goto out_fail; |
9687 | } | |
9688 | ||
db0a669f | 9689 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9690 | old_dentry->d_name.name, |
9691 | old_dentry->d_name.len, 0, new_idx); | |
9692 | if (ret) { | |
66642832 | 9693 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9694 | goto out_fail; |
9695 | } | |
9696 | ||
9697 | if (old_inode->i_nlink == 1) | |
9698 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9699 | if (new_inode->i_nlink == 1) | |
9700 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9701 | ||
86e8aa0e | 9702 | if (root_log_pinned) { |
cdd1fedf | 9703 | parent = new_dentry->d_parent; |
d4682ba0 FM |
9704 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), |
9705 | BTRFS_I(old_dir), parent, | |
9706 | false, &ctx_root); | |
9707 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9708 | sync_log_root = true; | |
9709 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9710 | commit_transaction = true; | |
9711 | ret = 0; | |
cdd1fedf | 9712 | btrfs_end_log_trans(root); |
86e8aa0e | 9713 | root_log_pinned = false; |
cdd1fedf | 9714 | } |
86e8aa0e | 9715 | if (dest_log_pinned) { |
d4682ba0 FM |
9716 | if (!commit_transaction) { |
9717 | parent = old_dentry->d_parent; | |
9718 | ret = btrfs_log_new_name(trans, BTRFS_I(new_inode), | |
9719 | BTRFS_I(new_dir), parent, | |
9720 | false, &ctx_dest); | |
9721 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9722 | sync_log_dest = true; | |
9723 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9724 | commit_transaction = true; | |
9725 | ret = 0; | |
9726 | } | |
cdd1fedf | 9727 | btrfs_end_log_trans(dest); |
86e8aa0e | 9728 | dest_log_pinned = false; |
cdd1fedf DF |
9729 | } |
9730 | out_fail: | |
86e8aa0e FM |
9731 | /* |
9732 | * If we have pinned a log and an error happened, we unpin tasks | |
9733 | * trying to sync the log and force them to fallback to a transaction | |
9734 | * commit if the log currently contains any of the inodes involved in | |
9735 | * this rename operation (to ensure we do not persist a log with an | |
9736 | * inconsistent state for any of these inodes or leading to any | |
9737 | * inconsistencies when replayed). If the transaction was aborted, the | |
9738 | * abortion reason is propagated to userspace when attempting to commit | |
9739 | * the transaction. If the log does not contain any of these inodes, we | |
9740 | * allow the tasks to sync it. | |
9741 | */ | |
9742 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9743 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9744 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9745 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9746 | (new_inode && |
0f8939b8 | 9747 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 9748 | btrfs_set_log_full_commit(trans); |
86e8aa0e FM |
9749 | |
9750 | if (root_log_pinned) { | |
9751 | btrfs_end_log_trans(root); | |
9752 | root_log_pinned = false; | |
9753 | } | |
9754 | if (dest_log_pinned) { | |
9755 | btrfs_end_log_trans(dest); | |
9756 | dest_log_pinned = false; | |
9757 | } | |
9758 | } | |
d4682ba0 FM |
9759 | if (!ret && sync_log_root && !commit_transaction) { |
9760 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, | |
9761 | &ctx_root); | |
9762 | if (ret) | |
9763 | commit_transaction = true; | |
9764 | } | |
9765 | if (!ret && sync_log_dest && !commit_transaction) { | |
9766 | ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root, | |
9767 | &ctx_dest); | |
9768 | if (ret) | |
9769 | commit_transaction = true; | |
9770 | } | |
9771 | if (commit_transaction) { | |
e6c61710 FM |
9772 | /* |
9773 | * We may have set commit_transaction when logging the new name | |
9774 | * in the destination root, in which case we left the source | |
9775 | * root context in the list of log contextes. So make sure we | |
9776 | * remove it to avoid invalid memory accesses, since the context | |
9777 | * was allocated in our stack frame. | |
9778 | */ | |
9779 | if (sync_log_root) { | |
9780 | mutex_lock(&root->log_mutex); | |
9781 | list_del_init(&ctx_root.list); | |
9782 | mutex_unlock(&root->log_mutex); | |
9783 | } | |
d4682ba0 FM |
9784 | ret = btrfs_commit_transaction(trans); |
9785 | } else { | |
9786 | int ret2; | |
9787 | ||
9788 | ret2 = btrfs_end_transaction(trans); | |
9789 | ret = ret ? ret : ret2; | |
9790 | } | |
cdd1fedf | 9791 | out_notrans: |
943eb3bf JB |
9792 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
9793 | old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9794 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9795 | |
e6c61710 FM |
9796 | ASSERT(list_empty(&ctx_root.list)); |
9797 | ASSERT(list_empty(&ctx_dest.list)); | |
9798 | ||
cdd1fedf DF |
9799 | return ret; |
9800 | } | |
9801 | ||
9802 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9803 | struct btrfs_root *root, | |
9804 | struct inode *dir, | |
9805 | struct dentry *dentry) | |
9806 | { | |
9807 | int ret; | |
9808 | struct inode *inode; | |
9809 | u64 objectid; | |
9810 | u64 index; | |
9811 | ||
9812 | ret = btrfs_find_free_ino(root, &objectid); | |
9813 | if (ret) | |
9814 | return ret; | |
9815 | ||
9816 | inode = btrfs_new_inode(trans, root, dir, | |
9817 | dentry->d_name.name, | |
9818 | dentry->d_name.len, | |
4a0cc7ca | 9819 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9820 | objectid, |
9821 | S_IFCHR | WHITEOUT_MODE, | |
9822 | &index); | |
9823 | ||
9824 | if (IS_ERR(inode)) { | |
9825 | ret = PTR_ERR(inode); | |
9826 | return ret; | |
9827 | } | |
9828 | ||
9829 | inode->i_op = &btrfs_special_inode_operations; | |
9830 | init_special_inode(inode, inode->i_mode, | |
9831 | WHITEOUT_DEV); | |
9832 | ||
9833 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9834 | &dentry->d_name); | |
9835 | if (ret) | |
c9901618 | 9836 | goto out; |
cdd1fedf | 9837 | |
cef415af NB |
9838 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9839 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9840 | if (ret) |
c9901618 | 9841 | goto out; |
cdd1fedf DF |
9842 | |
9843 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9844 | out: |
cdd1fedf | 9845 | unlock_new_inode(inode); |
c9901618 FM |
9846 | if (ret) |
9847 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9848 | iput(inode); |
9849 | ||
c9901618 | 9850 | return ret; |
cdd1fedf DF |
9851 | } |
9852 | ||
d397712b | 9853 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9854 | struct inode *new_dir, struct dentry *new_dentry, |
9855 | unsigned int flags) | |
39279cc3 | 9856 | { |
0b246afa | 9857 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9858 | struct btrfs_trans_handle *trans; |
5062af35 | 9859 | unsigned int trans_num_items; |
39279cc3 | 9860 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9861 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9862 | struct inode *new_inode = d_inode(new_dentry); |
9863 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9864 | u64 index = 0; |
4df27c4d | 9865 | u64 root_objectid; |
39279cc3 | 9866 | int ret; |
4a0cc7ca | 9867 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9868 | bool log_pinned = false; |
d4682ba0 FM |
9869 | struct btrfs_log_ctx ctx; |
9870 | bool sync_log = false; | |
9871 | bool commit_transaction = false; | |
39279cc3 | 9872 | |
4a0cc7ca | 9873 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9874 | return -EPERM; |
9875 | ||
4df27c4d | 9876 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9877 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9878 | return -EXDEV; |
9879 | ||
33345d01 | 9880 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9881 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9882 | return -ENOTEMPTY; |
5f39d397 | 9883 | |
4df27c4d YZ |
9884 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9885 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9886 | return -ENOTEMPTY; | |
9c52057c CM |
9887 | |
9888 | ||
9889 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9890 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9891 | new_dentry->d_name.name, |
9892 | new_dentry->d_name.len); | |
9893 | ||
9894 | if (ret) { | |
9895 | if (ret == -EEXIST) { | |
9896 | /* we shouldn't get | |
9897 | * eexist without a new_inode */ | |
fae7f21c | 9898 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9899 | return ret; |
9900 | } | |
9901 | } else { | |
9902 | /* maybe -EOVERFLOW */ | |
9903 | return ret; | |
9904 | } | |
9905 | } | |
9906 | ret = 0; | |
9907 | ||
5a3f23d5 | 9908 | /* |
8d875f95 CM |
9909 | * we're using rename to replace one file with another. Start IO on it |
9910 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9911 | */ |
8d875f95 | 9912 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9913 | filemap_flush(old_inode->i_mapping); |
9914 | ||
76dda93c | 9915 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9916 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9917 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9918 | /* |
9919 | * We want to reserve the absolute worst case amount of items. So if | |
9920 | * both inodes are subvols and we need to unlink them then that would | |
9921 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9922 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9923 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9924 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9925 | * If our rename has the whiteout flag, we need more 5 units for the |
9926 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9927 | * when selinux is enabled). | |
a22285a6 | 9928 | */ |
5062af35 FM |
9929 | trans_num_items = 11; |
9930 | if (flags & RENAME_WHITEOUT) | |
9931 | trans_num_items += 5; | |
9932 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9933 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9934 | ret = PTR_ERR(trans); |
9935 | goto out_notrans; | |
9936 | } | |
76dda93c | 9937 | |
4df27c4d YZ |
9938 | if (dest != root) |
9939 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9940 | |
877574e2 | 9941 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9942 | if (ret) |
9943 | goto out_fail; | |
5a3f23d5 | 9944 | |
67de1176 | 9945 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9946 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9947 | /* force full log commit if subvolume involved. */ |
90787766 | 9948 | btrfs_set_log_full_commit(trans); |
4df27c4d | 9949 | } else { |
c4aba954 FM |
9950 | btrfs_pin_log_trans(root); |
9951 | log_pinned = true; | |
a5719521 YZ |
9952 | ret = btrfs_insert_inode_ref(trans, dest, |
9953 | new_dentry->d_name.name, | |
9954 | new_dentry->d_name.len, | |
33345d01 | 9955 | old_ino, |
4a0cc7ca | 9956 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9957 | if (ret) |
9958 | goto out_fail; | |
4df27c4d | 9959 | } |
5a3f23d5 | 9960 | |
0c4d2d95 JB |
9961 | inode_inc_iversion(old_dir); |
9962 | inode_inc_iversion(new_dir); | |
9963 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9964 | old_dir->i_ctime = old_dir->i_mtime = |
9965 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9966 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9967 | |
12fcfd22 | 9968 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9969 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9970 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9971 | |
33345d01 | 9972 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9973 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
401b3b19 | 9974 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
4df27c4d YZ |
9975 | old_dentry->d_name.name, |
9976 | old_dentry->d_name.len); | |
9977 | } else { | |
4ec5934e NB |
9978 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9979 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9980 | old_dentry->d_name.name, |
9981 | old_dentry->d_name.len); | |
9982 | if (!ret) | |
9983 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9984 | } |
79787eaa | 9985 | if (ret) { |
66642832 | 9986 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9987 | goto out_fail; |
9988 | } | |
39279cc3 CM |
9989 | |
9990 | if (new_inode) { | |
0c4d2d95 | 9991 | inode_inc_iversion(new_inode); |
c2050a45 | 9992 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9993 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
9994 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9995 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
401b3b19 | 9996 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
4df27c4d YZ |
9997 | new_dentry->d_name.name, |
9998 | new_dentry->d_name.len); | |
9999 | BUG_ON(new_inode->i_nlink == 0); | |
10000 | } else { | |
4ec5934e NB |
10001 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
10002 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
10003 | new_dentry->d_name.name, |
10004 | new_dentry->d_name.len); | |
10005 | } | |
4ef31a45 | 10006 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
10007 | ret = btrfs_orphan_add(trans, |
10008 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 10009 | if (ret) { |
66642832 | 10010 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10011 | goto out_fail; |
10012 | } | |
39279cc3 | 10013 | } |
aec7477b | 10014 | |
db0a669f | 10015 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 10016 | new_dentry->d_name.name, |
a5719521 | 10017 | new_dentry->d_name.len, 0, index); |
79787eaa | 10018 | if (ret) { |
66642832 | 10019 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10020 | goto out_fail; |
10021 | } | |
39279cc3 | 10022 | |
67de1176 MX |
10023 | if (old_inode->i_nlink == 1) |
10024 | BTRFS_I(old_inode)->dir_index = index; | |
10025 | ||
3dc9e8f7 | 10026 | if (log_pinned) { |
10d9f309 | 10027 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 10028 | |
d4682ba0 FM |
10029 | btrfs_init_log_ctx(&ctx, old_inode); |
10030 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), | |
10031 | BTRFS_I(old_dir), parent, | |
10032 | false, &ctx); | |
10033 | if (ret == BTRFS_NEED_LOG_SYNC) | |
10034 | sync_log = true; | |
10035 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
10036 | commit_transaction = true; | |
10037 | ret = 0; | |
4df27c4d | 10038 | btrfs_end_log_trans(root); |
3dc9e8f7 | 10039 | log_pinned = false; |
4df27c4d | 10040 | } |
cdd1fedf DF |
10041 | |
10042 | if (flags & RENAME_WHITEOUT) { | |
10043 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10044 | old_dentry); | |
10045 | ||
10046 | if (ret) { | |
66642832 | 10047 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
10048 | goto out_fail; |
10049 | } | |
4df27c4d | 10050 | } |
39279cc3 | 10051 | out_fail: |
3dc9e8f7 FM |
10052 | /* |
10053 | * If we have pinned the log and an error happened, we unpin tasks | |
10054 | * trying to sync the log and force them to fallback to a transaction | |
10055 | * commit if the log currently contains any of the inodes involved in | |
10056 | * this rename operation (to ensure we do not persist a log with an | |
10057 | * inconsistent state for any of these inodes or leading to any | |
10058 | * inconsistencies when replayed). If the transaction was aborted, the | |
10059 | * abortion reason is propagated to userspace when attempting to commit | |
10060 | * the transaction. If the log does not contain any of these inodes, we | |
10061 | * allow the tasks to sync it. | |
10062 | */ | |
10063 | if (ret && log_pinned) { | |
0f8939b8 NB |
10064 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
10065 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10066 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 10067 | (new_inode && |
0f8939b8 | 10068 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
90787766 | 10069 | btrfs_set_log_full_commit(trans); |
3dc9e8f7 FM |
10070 | |
10071 | btrfs_end_log_trans(root); | |
10072 | log_pinned = false; | |
10073 | } | |
d4682ba0 FM |
10074 | if (!ret && sync_log) { |
10075 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx); | |
10076 | if (ret) | |
10077 | commit_transaction = true; | |
10078 | } | |
10079 | if (commit_transaction) { | |
10080 | ret = btrfs_commit_transaction(trans); | |
10081 | } else { | |
10082 | int ret2; | |
10083 | ||
10084 | ret2 = btrfs_end_transaction(trans); | |
10085 | ret = ret ? ret : ret2; | |
10086 | } | |
b44c59a8 | 10087 | out_notrans: |
33345d01 | 10088 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10089 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 10090 | |
39279cc3 CM |
10091 | return ret; |
10092 | } | |
10093 | ||
80ace85c MS |
10094 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
10095 | struct inode *new_dir, struct dentry *new_dentry, | |
10096 | unsigned int flags) | |
10097 | { | |
cdd1fedf | 10098 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
10099 | return -EINVAL; |
10100 | ||
cdd1fedf DF |
10101 | if (flags & RENAME_EXCHANGE) |
10102 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10103 | new_dentry); | |
10104 | ||
10105 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
10106 | } |
10107 | ||
3a2f8c07 NB |
10108 | struct btrfs_delalloc_work { |
10109 | struct inode *inode; | |
10110 | struct completion completion; | |
10111 | struct list_head list; | |
10112 | struct btrfs_work work; | |
10113 | }; | |
10114 | ||
8ccf6f19 MX |
10115 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
10116 | { | |
10117 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 10118 | struct inode *inode; |
8ccf6f19 MX |
10119 | |
10120 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10121 | work); | |
9f23e289 | 10122 | inode = delalloc_work->inode; |
30424601 DS |
10123 | filemap_flush(inode->i_mapping); |
10124 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10125 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10126 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 10127 | |
076da91c | 10128 | iput(inode); |
8ccf6f19 MX |
10129 | complete(&delalloc_work->completion); |
10130 | } | |
10131 | ||
3a2f8c07 | 10132 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
10133 | { |
10134 | struct btrfs_delalloc_work *work; | |
10135 | ||
100d5702 | 10136 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10137 | if (!work) |
10138 | return NULL; | |
10139 | ||
10140 | init_completion(&work->completion); | |
10141 | INIT_LIST_HEAD(&work->list); | |
10142 | work->inode = inode; | |
a0cac0ec | 10143 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
8ccf6f19 MX |
10144 | |
10145 | return work; | |
10146 | } | |
10147 | ||
d352ac68 CM |
10148 | /* |
10149 | * some fairly slow code that needs optimization. This walks the list | |
10150 | * of all the inodes with pending delalloc and forces them to disk. | |
10151 | */ | |
3cd24c69 | 10152 | static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot) |
ea8c2819 | 10153 | { |
ea8c2819 | 10154 | struct btrfs_inode *binode; |
5b21f2ed | 10155 | struct inode *inode; |
8ccf6f19 MX |
10156 | struct btrfs_delalloc_work *work, *next; |
10157 | struct list_head works; | |
1eafa6c7 | 10158 | struct list_head splice; |
8ccf6f19 | 10159 | int ret = 0; |
ea8c2819 | 10160 | |
8ccf6f19 | 10161 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10162 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10163 | |
573bfb72 | 10164 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10165 | spin_lock(&root->delalloc_lock); |
10166 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10167 | while (!list_empty(&splice)) { |
10168 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10169 | delalloc_inodes); |
1eafa6c7 | 10170 | |
eb73c1b7 MX |
10171 | list_move_tail(&binode->delalloc_inodes, |
10172 | &root->delalloc_inodes); | |
5b21f2ed | 10173 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10174 | if (!inode) { |
eb73c1b7 | 10175 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10176 | continue; |
df0af1a5 | 10177 | } |
eb73c1b7 | 10178 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10179 | |
3cd24c69 EL |
10180 | if (snapshot) |
10181 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
10182 | &binode->runtime_flags); | |
076da91c | 10183 | work = btrfs_alloc_delalloc_work(inode); |
5d99a998 | 10184 | if (!work) { |
4fbb5147 | 10185 | iput(inode); |
1eafa6c7 | 10186 | ret = -ENOMEM; |
a1ecaabb | 10187 | goto out; |
5b21f2ed | 10188 | } |
1eafa6c7 | 10189 | list_add_tail(&work->list, &works); |
a44903ab QW |
10190 | btrfs_queue_work(root->fs_info->flush_workers, |
10191 | &work->work); | |
6c255e67 MX |
10192 | ret++; |
10193 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10194 | goto out; |
5b21f2ed | 10195 | cond_resched(); |
eb73c1b7 | 10196 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10197 | } |
eb73c1b7 | 10198 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10199 | |
a1ecaabb | 10200 | out: |
eb73c1b7 MX |
10201 | list_for_each_entry_safe(work, next, &works, list) { |
10202 | list_del_init(&work->list); | |
40012f96 NB |
10203 | wait_for_completion(&work->completion); |
10204 | kfree(work); | |
eb73c1b7 MX |
10205 | } |
10206 | ||
81f1d390 | 10207 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
10208 | spin_lock(&root->delalloc_lock); |
10209 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10210 | spin_unlock(&root->delalloc_lock); | |
10211 | } | |
573bfb72 | 10212 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10213 | return ret; |
10214 | } | |
1eafa6c7 | 10215 | |
3cd24c69 | 10216 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
eb73c1b7 | 10217 | { |
0b246afa | 10218 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10219 | int ret; |
1eafa6c7 | 10220 | |
0b246afa | 10221 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10222 | return -EROFS; |
10223 | ||
3cd24c69 | 10224 | ret = start_delalloc_inodes(root, -1, true); |
6c255e67 MX |
10225 | if (ret > 0) |
10226 | ret = 0; | |
eb73c1b7 MX |
10227 | return ret; |
10228 | } | |
10229 | ||
82b3e53b | 10230 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr) |
eb73c1b7 MX |
10231 | { |
10232 | struct btrfs_root *root; | |
10233 | struct list_head splice; | |
10234 | int ret; | |
10235 | ||
2c21b4d7 | 10236 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10237 | return -EROFS; |
10238 | ||
10239 | INIT_LIST_HEAD(&splice); | |
10240 | ||
573bfb72 | 10241 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10242 | spin_lock(&fs_info->delalloc_root_lock); |
10243 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10244 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10245 | root = list_first_entry(&splice, struct btrfs_root, |
10246 | delalloc_root); | |
10247 | root = btrfs_grab_fs_root(root); | |
10248 | BUG_ON(!root); | |
10249 | list_move_tail(&root->delalloc_root, | |
10250 | &fs_info->delalloc_roots); | |
10251 | spin_unlock(&fs_info->delalloc_root_lock); | |
10252 | ||
3cd24c69 | 10253 | ret = start_delalloc_inodes(root, nr, false); |
eb73c1b7 | 10254 | btrfs_put_fs_root(root); |
6c255e67 | 10255 | if (ret < 0) |
eb73c1b7 MX |
10256 | goto out; |
10257 | ||
6c255e67 MX |
10258 | if (nr != -1) { |
10259 | nr -= ret; | |
10260 | WARN_ON(nr < 0); | |
10261 | } | |
eb73c1b7 | 10262 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10263 | } |
eb73c1b7 | 10264 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10265 | |
6c255e67 | 10266 | ret = 0; |
eb73c1b7 | 10267 | out: |
81f1d390 | 10268 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
10269 | spin_lock(&fs_info->delalloc_root_lock); |
10270 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10271 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10272 | } |
573bfb72 | 10273 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10274 | return ret; |
ea8c2819 CM |
10275 | } |
10276 | ||
39279cc3 CM |
10277 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10278 | const char *symname) | |
10279 | { | |
0b246afa | 10280 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10281 | struct btrfs_trans_handle *trans; |
10282 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10283 | struct btrfs_path *path; | |
10284 | struct btrfs_key key; | |
1832a6d5 | 10285 | struct inode *inode = NULL; |
39279cc3 | 10286 | int err; |
39279cc3 | 10287 | u64 objectid; |
67871254 | 10288 | u64 index = 0; |
39279cc3 CM |
10289 | int name_len; |
10290 | int datasize; | |
5f39d397 | 10291 | unsigned long ptr; |
39279cc3 | 10292 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10293 | struct extent_buffer *leaf; |
39279cc3 | 10294 | |
f06becc4 | 10295 | name_len = strlen(symname); |
0b246afa | 10296 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10297 | return -ENAMETOOLONG; |
1832a6d5 | 10298 | |
9ed74f2d JB |
10299 | /* |
10300 | * 2 items for inode item and ref | |
10301 | * 2 items for dir items | |
9269d12b FM |
10302 | * 1 item for updating parent inode item |
10303 | * 1 item for the inline extent item | |
9ed74f2d JB |
10304 | * 1 item for xattr if selinux is on |
10305 | */ | |
9269d12b | 10306 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10307 | if (IS_ERR(trans)) |
10308 | return PTR_ERR(trans); | |
1832a6d5 | 10309 | |
581bb050 LZ |
10310 | err = btrfs_find_free_ino(root, &objectid); |
10311 | if (err) | |
10312 | goto out_unlock; | |
10313 | ||
aec7477b | 10314 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10315 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10316 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10317 | if (IS_ERR(inode)) { |
10318 | err = PTR_ERR(inode); | |
32955c54 | 10319 | inode = NULL; |
39279cc3 | 10320 | goto out_unlock; |
7cf96da3 | 10321 | } |
39279cc3 | 10322 | |
ad19db71 CS |
10323 | /* |
10324 | * If the active LSM wants to access the inode during | |
10325 | * d_instantiate it needs these. Smack checks to see | |
10326 | * if the filesystem supports xattrs by looking at the | |
10327 | * ops vector. | |
10328 | */ | |
10329 | inode->i_fop = &btrfs_file_operations; | |
10330 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10331 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10332 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10333 | ||
10334 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10335 | if (err) | |
32955c54 | 10336 | goto out_unlock; |
ad19db71 | 10337 | |
39279cc3 | 10338 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10339 | if (!path) { |
10340 | err = -ENOMEM; | |
32955c54 | 10341 | goto out_unlock; |
d8926bb3 | 10342 | } |
4a0cc7ca | 10343 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10344 | key.offset = 0; |
962a298f | 10345 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10346 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10347 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10348 | datasize); | |
54aa1f4d | 10349 | if (err) { |
b0839166 | 10350 | btrfs_free_path(path); |
32955c54 | 10351 | goto out_unlock; |
54aa1f4d | 10352 | } |
5f39d397 CM |
10353 | leaf = path->nodes[0]; |
10354 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10355 | struct btrfs_file_extent_item); | |
10356 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10357 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10358 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10359 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10360 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10361 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10362 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10363 | ||
39279cc3 | 10364 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10365 | write_extent_buffer(leaf, symname, ptr, name_len); |
10366 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10367 | btrfs_free_path(path); |
5f39d397 | 10368 | |
39279cc3 | 10369 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10370 | inode_nohighmem(inode); |
d899e052 | 10371 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10372 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10373 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10374 | /* |
10375 | * Last step, add directory indexes for our symlink inode. This is the | |
10376 | * last step to avoid extra cleanup of these indexes if an error happens | |
10377 | * elsewhere above. | |
10378 | */ | |
10379 | if (!err) | |
cef415af NB |
10380 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10381 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
10382 | if (err) |
10383 | goto out_unlock; | |
b0d5d10f | 10384 | |
1e2e547a | 10385 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
10386 | |
10387 | out_unlock: | |
3a45bb20 | 10388 | btrfs_end_transaction(trans); |
32955c54 | 10389 | if (err && inode) { |
39279cc3 | 10390 | inode_dec_link_count(inode); |
32955c54 | 10391 | discard_new_inode(inode); |
39279cc3 | 10392 | } |
2ff7e61e | 10393 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
10394 | return err; |
10395 | } | |
16432985 | 10396 | |
0af3d00b JB |
10397 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10398 | u64 start, u64 num_bytes, u64 min_size, | |
10399 | loff_t actual_len, u64 *alloc_hint, | |
10400 | struct btrfs_trans_handle *trans) | |
d899e052 | 10401 | { |
0b246afa | 10402 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10403 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10404 | struct extent_map *em; | |
d899e052 YZ |
10405 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10406 | struct btrfs_key ins; | |
d899e052 | 10407 | u64 cur_offset = start; |
55a61d1d | 10408 | u64 i_size; |
154ea289 | 10409 | u64 cur_bytes; |
0b670dc4 | 10410 | u64 last_alloc = (u64)-1; |
d899e052 | 10411 | int ret = 0; |
0af3d00b | 10412 | bool own_trans = true; |
18513091 | 10413 | u64 end = start + num_bytes - 1; |
d899e052 | 10414 | |
0af3d00b JB |
10415 | if (trans) |
10416 | own_trans = false; | |
d899e052 | 10417 | while (num_bytes > 0) { |
0af3d00b JB |
10418 | if (own_trans) { |
10419 | trans = btrfs_start_transaction(root, 3); | |
10420 | if (IS_ERR(trans)) { | |
10421 | ret = PTR_ERR(trans); | |
10422 | break; | |
10423 | } | |
5a303d5d YZ |
10424 | } |
10425 | ||
ee22184b | 10426 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10427 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10428 | /* |
10429 | * If we are severely fragmented we could end up with really | |
10430 | * small allocations, so if the allocator is returning small | |
10431 | * chunks lets make its job easier by only searching for those | |
10432 | * sized chunks. | |
10433 | */ | |
10434 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10435 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10436 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10437 | if (ret) { |
0af3d00b | 10438 | if (own_trans) |
3a45bb20 | 10439 | btrfs_end_transaction(trans); |
a22285a6 | 10440 | break; |
d899e052 | 10441 | } |
0b246afa | 10442 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10443 | |
0b670dc4 | 10444 | last_alloc = ins.offset; |
d899e052 YZ |
10445 | ret = insert_reserved_file_extent(trans, inode, |
10446 | cur_offset, ins.objectid, | |
10447 | ins.offset, ins.offset, | |
920bbbfb | 10448 | ins.offset, 0, 0, 0, |
d899e052 | 10449 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10450 | if (ret) { |
2ff7e61e | 10451 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10452 | ins.offset, 0); |
66642832 | 10453 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10454 | if (own_trans) |
3a45bb20 | 10455 | btrfs_end_transaction(trans); |
79787eaa JM |
10456 | break; |
10457 | } | |
31193213 | 10458 | |
dcdbc059 | 10459 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10460 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10461 | |
5dc562c5 JB |
10462 | em = alloc_extent_map(); |
10463 | if (!em) { | |
10464 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10465 | &BTRFS_I(inode)->runtime_flags); | |
10466 | goto next; | |
10467 | } | |
10468 | ||
10469 | em->start = cur_offset; | |
10470 | em->orig_start = cur_offset; | |
10471 | em->len = ins.offset; | |
10472 | em->block_start = ins.objectid; | |
10473 | em->block_len = ins.offset; | |
b4939680 | 10474 | em->orig_block_len = ins.offset; |
cc95bef6 | 10475 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
10476 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10477 | em->generation = trans->transid; | |
10478 | ||
10479 | while (1) { | |
10480 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10481 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10482 | write_unlock(&em_tree->lock); |
10483 | if (ret != -EEXIST) | |
10484 | break; | |
dcdbc059 | 10485 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10486 | cur_offset + ins.offset - 1, |
10487 | 0); | |
10488 | } | |
10489 | free_extent_map(em); | |
10490 | next: | |
d899e052 YZ |
10491 | num_bytes -= ins.offset; |
10492 | cur_offset += ins.offset; | |
efa56464 | 10493 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10494 | |
0c4d2d95 | 10495 | inode_inc_iversion(inode); |
c2050a45 | 10496 | inode->i_ctime = current_time(inode); |
6cbff00f | 10497 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10498 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10499 | (actual_len > inode->i_size) && |
10500 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10501 | if (cur_offset > actual_len) |
55a61d1d | 10502 | i_size = actual_len; |
d1ea6a61 | 10503 | else |
55a61d1d JB |
10504 | i_size = cur_offset; |
10505 | i_size_write(inode, i_size); | |
10506 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10507 | } |
10508 | ||
d899e052 | 10509 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10510 | |
10511 | if (ret) { | |
66642832 | 10512 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10513 | if (own_trans) |
3a45bb20 | 10514 | btrfs_end_transaction(trans); |
79787eaa JM |
10515 | break; |
10516 | } | |
d899e052 | 10517 | |
0af3d00b | 10518 | if (own_trans) |
3a45bb20 | 10519 | btrfs_end_transaction(trans); |
5a303d5d | 10520 | } |
18513091 | 10521 | if (cur_offset < end) |
bc42bda2 | 10522 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
18513091 | 10523 | end - cur_offset + 1); |
d899e052 YZ |
10524 | return ret; |
10525 | } | |
10526 | ||
0af3d00b JB |
10527 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10528 | u64 start, u64 num_bytes, u64 min_size, | |
10529 | loff_t actual_len, u64 *alloc_hint) | |
10530 | { | |
10531 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10532 | min_size, actual_len, alloc_hint, | |
10533 | NULL); | |
10534 | } | |
10535 | ||
10536 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10537 | struct btrfs_trans_handle *trans, int mode, | |
10538 | u64 start, u64 num_bytes, u64 min_size, | |
10539 | loff_t actual_len, u64 *alloc_hint) | |
10540 | { | |
10541 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10542 | min_size, actual_len, alloc_hint, trans); | |
10543 | } | |
10544 | ||
e6dcd2dc CM |
10545 | static int btrfs_set_page_dirty(struct page *page) |
10546 | { | |
e6dcd2dc CM |
10547 | return __set_page_dirty_nobuffers(page); |
10548 | } | |
10549 | ||
10556cb2 | 10550 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10551 | { |
b83cc969 | 10552 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10553 | umode_t mode = inode->i_mode; |
b83cc969 | 10554 | |
cb6db4e5 JM |
10555 | if (mask & MAY_WRITE && |
10556 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10557 | if (btrfs_root_readonly(root)) | |
10558 | return -EROFS; | |
10559 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10560 | return -EACCES; | |
10561 | } | |
2830ba7f | 10562 | return generic_permission(inode, mask); |
fdebe2bd | 10563 | } |
39279cc3 | 10564 | |
ef3b9af5 FM |
10565 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10566 | { | |
2ff7e61e | 10567 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10568 | struct btrfs_trans_handle *trans; |
10569 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10570 | struct inode *inode = NULL; | |
10571 | u64 objectid; | |
10572 | u64 index; | |
10573 | int ret = 0; | |
10574 | ||
10575 | /* | |
10576 | * 5 units required for adding orphan entry | |
10577 | */ | |
10578 | trans = btrfs_start_transaction(root, 5); | |
10579 | if (IS_ERR(trans)) | |
10580 | return PTR_ERR(trans); | |
10581 | ||
10582 | ret = btrfs_find_free_ino(root, &objectid); | |
10583 | if (ret) | |
10584 | goto out; | |
10585 | ||
10586 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10587 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10588 | if (IS_ERR(inode)) { |
10589 | ret = PTR_ERR(inode); | |
10590 | inode = NULL; | |
10591 | goto out; | |
10592 | } | |
10593 | ||
ef3b9af5 FM |
10594 | inode->i_fop = &btrfs_file_operations; |
10595 | inode->i_op = &btrfs_file_inode_operations; | |
10596 | ||
10597 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10598 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10599 | ||
b0d5d10f CM |
10600 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10601 | if (ret) | |
32955c54 | 10602 | goto out; |
b0d5d10f CM |
10603 | |
10604 | ret = btrfs_update_inode(trans, root, inode); | |
10605 | if (ret) | |
32955c54 | 10606 | goto out; |
73f2e545 | 10607 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10608 | if (ret) |
32955c54 | 10609 | goto out; |
ef3b9af5 | 10610 | |
5762b5c9 FM |
10611 | /* |
10612 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10613 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10614 | * through: | |
10615 | * | |
10616 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10617 | */ | |
10618 | set_nlink(inode, 1); | |
ef3b9af5 | 10619 | d_tmpfile(dentry, inode); |
32955c54 | 10620 | unlock_new_inode(inode); |
ef3b9af5 | 10621 | mark_inode_dirty(inode); |
ef3b9af5 | 10622 | out: |
3a45bb20 | 10623 | btrfs_end_transaction(trans); |
32955c54 AV |
10624 | if (ret && inode) |
10625 | discard_new_inode(inode); | |
2ff7e61e | 10626 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
10627 | return ret; |
10628 | } | |
10629 | ||
5cdc84bf | 10630 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
c6100a4b | 10631 | { |
5cdc84bf | 10632 | struct inode *inode = tree->private_data; |
c6100a4b JB |
10633 | unsigned long index = start >> PAGE_SHIFT; |
10634 | unsigned long end_index = end >> PAGE_SHIFT; | |
10635 | struct page *page; | |
10636 | ||
10637 | while (index <= end_index) { | |
10638 | page = find_get_page(inode->i_mapping, index); | |
10639 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10640 | set_page_writeback(page); | |
10641 | put_page(page); | |
10642 | index++; | |
10643 | } | |
10644 | } | |
10645 | ||
ed46ff3d OS |
10646 | #ifdef CONFIG_SWAP |
10647 | /* | |
10648 | * Add an entry indicating a block group or device which is pinned by a | |
10649 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10650 | * negative errno on failure. | |
10651 | */ | |
10652 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10653 | bool is_block_group) | |
10654 | { | |
10655 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10656 | struct btrfs_swapfile_pin *sp, *entry; | |
10657 | struct rb_node **p; | |
10658 | struct rb_node *parent = NULL; | |
10659 | ||
10660 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10661 | if (!sp) | |
10662 | return -ENOMEM; | |
10663 | sp->ptr = ptr; | |
10664 | sp->inode = inode; | |
10665 | sp->is_block_group = is_block_group; | |
10666 | ||
10667 | spin_lock(&fs_info->swapfile_pins_lock); | |
10668 | p = &fs_info->swapfile_pins.rb_node; | |
10669 | while (*p) { | |
10670 | parent = *p; | |
10671 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10672 | if (sp->ptr < entry->ptr || | |
10673 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10674 | p = &(*p)->rb_left; | |
10675 | } else if (sp->ptr > entry->ptr || | |
10676 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10677 | p = &(*p)->rb_right; | |
10678 | } else { | |
10679 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10680 | kfree(sp); | |
10681 | return 1; | |
10682 | } | |
10683 | } | |
10684 | rb_link_node(&sp->node, parent, p); | |
10685 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10686 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10687 | return 0; | |
10688 | } | |
10689 | ||
10690 | /* Free all of the entries pinned by this swapfile. */ | |
10691 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10692 | { | |
10693 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10694 | struct btrfs_swapfile_pin *sp; | |
10695 | struct rb_node *node, *next; | |
10696 | ||
10697 | spin_lock(&fs_info->swapfile_pins_lock); | |
10698 | node = rb_first(&fs_info->swapfile_pins); | |
10699 | while (node) { | |
10700 | next = rb_next(node); | |
10701 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10702 | if (sp->inode == inode) { | |
10703 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
10704 | if (sp->is_block_group) | |
10705 | btrfs_put_block_group(sp->ptr); | |
10706 | kfree(sp); | |
10707 | } | |
10708 | node = next; | |
10709 | } | |
10710 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10711 | } | |
10712 | ||
10713 | struct btrfs_swap_info { | |
10714 | u64 start; | |
10715 | u64 block_start; | |
10716 | u64 block_len; | |
10717 | u64 lowest_ppage; | |
10718 | u64 highest_ppage; | |
10719 | unsigned long nr_pages; | |
10720 | int nr_extents; | |
10721 | }; | |
10722 | ||
10723 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10724 | struct btrfs_swap_info *bsi) | |
10725 | { | |
10726 | unsigned long nr_pages; | |
10727 | u64 first_ppage, first_ppage_reported, next_ppage; | |
10728 | int ret; | |
10729 | ||
10730 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
10731 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
10732 | PAGE_SIZE) >> PAGE_SHIFT; | |
10733 | ||
10734 | if (first_ppage >= next_ppage) | |
10735 | return 0; | |
10736 | nr_pages = next_ppage - first_ppage; | |
10737 | ||
10738 | first_ppage_reported = first_ppage; | |
10739 | if (bsi->start == 0) | |
10740 | first_ppage_reported++; | |
10741 | if (bsi->lowest_ppage > first_ppage_reported) | |
10742 | bsi->lowest_ppage = first_ppage_reported; | |
10743 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10744 | bsi->highest_ppage = next_ppage - 1; | |
10745 | ||
10746 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10747 | if (ret < 0) | |
10748 | return ret; | |
10749 | bsi->nr_extents += ret; | |
10750 | bsi->nr_pages += nr_pages; | |
10751 | return 0; | |
10752 | } | |
10753 | ||
10754 | static void btrfs_swap_deactivate(struct file *file) | |
10755 | { | |
10756 | struct inode *inode = file_inode(file); | |
10757 | ||
10758 | btrfs_free_swapfile_pins(inode); | |
10759 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10760 | } | |
10761 | ||
10762 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10763 | sector_t *span) | |
10764 | { | |
10765 | struct inode *inode = file_inode(file); | |
10766 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10767 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
10768 | struct extent_state *cached_state = NULL; | |
10769 | struct extent_map *em = NULL; | |
10770 | struct btrfs_device *device = NULL; | |
10771 | struct btrfs_swap_info bsi = { | |
10772 | .lowest_ppage = (sector_t)-1ULL, | |
10773 | }; | |
10774 | int ret = 0; | |
10775 | u64 isize; | |
10776 | u64 start; | |
10777 | ||
10778 | /* | |
10779 | * If the swap file was just created, make sure delalloc is done. If the | |
10780 | * file changes again after this, the user is doing something stupid and | |
10781 | * we don't really care. | |
10782 | */ | |
10783 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10784 | if (ret) | |
10785 | return ret; | |
10786 | ||
10787 | /* | |
10788 | * The inode is locked, so these flags won't change after we check them. | |
10789 | */ | |
10790 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10791 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10792 | return -EINVAL; | |
10793 | } | |
10794 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10795 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10796 | return -EINVAL; | |
10797 | } | |
10798 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10799 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10800 | return -EINVAL; | |
10801 | } | |
10802 | ||
10803 | /* | |
10804 | * Balance or device remove/replace/resize can move stuff around from | |
10805 | * under us. The EXCL_OP flag makes sure they aren't running/won't run | |
10806 | * concurrently while we are mapping the swap extents, and | |
10807 | * fs_info->swapfile_pins prevents them from running while the swap file | |
10808 | * is active and moving the extents. Note that this also prevents a | |
10809 | * concurrent device add which isn't actually necessary, but it's not | |
10810 | * really worth the trouble to allow it. | |
10811 | */ | |
10812 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { | |
10813 | btrfs_warn(fs_info, | |
10814 | "cannot activate swapfile while exclusive operation is running"); | |
10815 | return -EBUSY; | |
10816 | } | |
10817 | /* | |
10818 | * Snapshots can create extents which require COW even if NODATACOW is | |
10819 | * set. We use this counter to prevent snapshots. We must increment it | |
10820 | * before walking the extents because we don't want a concurrent | |
10821 | * snapshot to run after we've already checked the extents. | |
10822 | */ | |
10823 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); | |
10824 | ||
10825 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10826 | ||
10827 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10828 | start = 0; | |
10829 | while (start < isize) { | |
10830 | u64 logical_block_start, physical_block_start; | |
32da5386 | 10831 | struct btrfs_block_group *bg; |
ed46ff3d OS |
10832 | u64 len = isize - start; |
10833 | ||
10834 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); | |
10835 | if (IS_ERR(em)) { | |
10836 | ret = PTR_ERR(em); | |
10837 | goto out; | |
10838 | } | |
10839 | ||
10840 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10841 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10842 | ret = -EINVAL; | |
10843 | goto out; | |
10844 | } | |
10845 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10846 | /* | |
10847 | * It's unlikely we'll ever actually find ourselves | |
10848 | * here, as a file small enough to fit inline won't be | |
10849 | * big enough to store more than the swap header, but in | |
10850 | * case something changes in the future, let's catch it | |
10851 | * here rather than later. | |
10852 | */ | |
10853 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10854 | ret = -EINVAL; | |
10855 | goto out; | |
10856 | } | |
10857 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10858 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10859 | ret = -EINVAL; | |
10860 | goto out; | |
10861 | } | |
10862 | ||
10863 | logical_block_start = em->block_start + (start - em->start); | |
10864 | len = min(len, em->len - (start - em->start)); | |
10865 | free_extent_map(em); | |
10866 | em = NULL; | |
10867 | ||
10868 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL); | |
10869 | if (ret < 0) { | |
10870 | goto out; | |
10871 | } else if (ret) { | |
10872 | ret = 0; | |
10873 | } else { | |
10874 | btrfs_warn(fs_info, | |
10875 | "swapfile must not be copy-on-write"); | |
10876 | ret = -EINVAL; | |
10877 | goto out; | |
10878 | } | |
10879 | ||
10880 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10881 | if (IS_ERR(em)) { | |
10882 | ret = PTR_ERR(em); | |
10883 | goto out; | |
10884 | } | |
10885 | ||
10886 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10887 | btrfs_warn(fs_info, | |
10888 | "swapfile must have single data profile"); | |
10889 | ret = -EINVAL; | |
10890 | goto out; | |
10891 | } | |
10892 | ||
10893 | if (device == NULL) { | |
10894 | device = em->map_lookup->stripes[0].dev; | |
10895 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10896 | if (ret == 1) | |
10897 | ret = 0; | |
10898 | else if (ret) | |
10899 | goto out; | |
10900 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10901 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10902 | ret = -EINVAL; | |
10903 | goto out; | |
10904 | } | |
10905 | ||
10906 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10907 | (logical_block_start - em->start)); | |
10908 | len = min(len, em->len - (logical_block_start - em->start)); | |
10909 | free_extent_map(em); | |
10910 | em = NULL; | |
10911 | ||
10912 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10913 | if (!bg) { | |
10914 | btrfs_warn(fs_info, | |
10915 | "could not find block group containing swapfile"); | |
10916 | ret = -EINVAL; | |
10917 | goto out; | |
10918 | } | |
10919 | ||
10920 | ret = btrfs_add_swapfile_pin(inode, bg, true); | |
10921 | if (ret) { | |
10922 | btrfs_put_block_group(bg); | |
10923 | if (ret == 1) | |
10924 | ret = 0; | |
10925 | else | |
10926 | goto out; | |
10927 | } | |
10928 | ||
10929 | if (bsi.block_len && | |
10930 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10931 | bsi.block_len += len; | |
10932 | } else { | |
10933 | if (bsi.block_len) { | |
10934 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10935 | if (ret) | |
10936 | goto out; | |
10937 | } | |
10938 | bsi.start = start; | |
10939 | bsi.block_start = physical_block_start; | |
10940 | bsi.block_len = len; | |
10941 | } | |
10942 | ||
10943 | start += len; | |
10944 | } | |
10945 | ||
10946 | if (bsi.block_len) | |
10947 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10948 | ||
10949 | out: | |
10950 | if (!IS_ERR_OR_NULL(em)) | |
10951 | free_extent_map(em); | |
10952 | ||
10953 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10954 | ||
10955 | if (ret) | |
10956 | btrfs_swap_deactivate(file); | |
10957 | ||
10958 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); | |
10959 | ||
10960 | if (ret) | |
10961 | return ret; | |
10962 | ||
10963 | if (device) | |
10964 | sis->bdev = device->bdev; | |
10965 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10966 | sis->max = bsi.nr_pages; | |
10967 | sis->pages = bsi.nr_pages - 1; | |
10968 | sis->highest_bit = bsi.nr_pages - 1; | |
10969 | return bsi.nr_extents; | |
10970 | } | |
10971 | #else | |
10972 | static void btrfs_swap_deactivate(struct file *file) | |
10973 | { | |
10974 | } | |
10975 | ||
10976 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10977 | sector_t *span) | |
10978 | { | |
10979 | return -EOPNOTSUPP; | |
10980 | } | |
10981 | #endif | |
10982 | ||
6e1d5dcc | 10983 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10984 | .getattr = btrfs_getattr, |
39279cc3 CM |
10985 | .lookup = btrfs_lookup, |
10986 | .create = btrfs_create, | |
10987 | .unlink = btrfs_unlink, | |
10988 | .link = btrfs_link, | |
10989 | .mkdir = btrfs_mkdir, | |
10990 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10991 | .rename = btrfs_rename2, |
39279cc3 CM |
10992 | .symlink = btrfs_symlink, |
10993 | .setattr = btrfs_setattr, | |
618e21d5 | 10994 | .mknod = btrfs_mknod, |
5103e947 | 10995 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10996 | .permission = btrfs_permission, |
4e34e719 | 10997 | .get_acl = btrfs_get_acl, |
996a710d | 10998 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10999 | .update_time = btrfs_update_time, |
ef3b9af5 | 11000 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 11001 | }; |
6e1d5dcc | 11002 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 11003 | .lookup = btrfs_lookup, |
fdebe2bd | 11004 | .permission = btrfs_permission, |
93fd63c2 | 11005 | .update_time = btrfs_update_time, |
39279cc3 | 11006 | }; |
76dda93c | 11007 | |
828c0950 | 11008 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
11009 | .llseek = generic_file_llseek, |
11010 | .read = generic_read_dir, | |
02dbfc99 | 11011 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 11012 | .open = btrfs_opendir, |
34287aa3 | 11013 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 11014 | #ifdef CONFIG_COMPAT |
4c63c245 | 11015 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 11016 | #endif |
6bf13c0c | 11017 | .release = btrfs_release_file, |
e02119d5 | 11018 | .fsync = btrfs_sync_file, |
39279cc3 CM |
11019 | }; |
11020 | ||
20e5506b | 11021 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 11022 | /* mandatory callbacks */ |
065631f6 | 11023 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac CM |
11024 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
11025 | }; | |
11026 | ||
35054394 CM |
11027 | /* |
11028 | * btrfs doesn't support the bmap operation because swapfiles | |
11029 | * use bmap to make a mapping of extents in the file. They assume | |
11030 | * these extents won't change over the life of the file and they | |
11031 | * use the bmap result to do IO directly to the drive. | |
11032 | * | |
11033 | * the btrfs bmap call would return logical addresses that aren't | |
11034 | * suitable for IO and they also will change frequently as COW | |
11035 | * operations happen. So, swapfile + btrfs == corruption. | |
11036 | * | |
11037 | * For now we're avoiding this by dropping bmap. | |
11038 | */ | |
7f09410b | 11039 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
11040 | .readpage = btrfs_readpage, |
11041 | .writepage = btrfs_writepage, | |
b293f02e | 11042 | .writepages = btrfs_writepages, |
3ab2fb5a | 11043 | .readpages = btrfs_readpages, |
16432985 | 11044 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
11045 | .invalidatepage = btrfs_invalidatepage, |
11046 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 11047 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 11048 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
11049 | .swap_activate = btrfs_swap_activate, |
11050 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
11051 | }; |
11052 | ||
6e1d5dcc | 11053 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
11054 | .getattr = btrfs_getattr, |
11055 | .setattr = btrfs_setattr, | |
5103e947 | 11056 | .listxattr = btrfs_listxattr, |
fdebe2bd | 11057 | .permission = btrfs_permission, |
1506fcc8 | 11058 | .fiemap = btrfs_fiemap, |
4e34e719 | 11059 | .get_acl = btrfs_get_acl, |
996a710d | 11060 | .set_acl = btrfs_set_acl, |
e41f941a | 11061 | .update_time = btrfs_update_time, |
39279cc3 | 11062 | }; |
6e1d5dcc | 11063 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
11064 | .getattr = btrfs_getattr, |
11065 | .setattr = btrfs_setattr, | |
fdebe2bd | 11066 | .permission = btrfs_permission, |
33268eaf | 11067 | .listxattr = btrfs_listxattr, |
4e34e719 | 11068 | .get_acl = btrfs_get_acl, |
996a710d | 11069 | .set_acl = btrfs_set_acl, |
e41f941a | 11070 | .update_time = btrfs_update_time, |
618e21d5 | 11071 | }; |
6e1d5dcc | 11072 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 11073 | .get_link = page_get_link, |
f209561a | 11074 | .getattr = btrfs_getattr, |
22c44fe6 | 11075 | .setattr = btrfs_setattr, |
fdebe2bd | 11076 | .permission = btrfs_permission, |
0279b4cd | 11077 | .listxattr = btrfs_listxattr, |
e41f941a | 11078 | .update_time = btrfs_update_time, |
39279cc3 | 11079 | }; |
76dda93c | 11080 | |
82d339d9 | 11081 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
11082 | .d_delete = btrfs_dentry_delete, |
11083 | }; |