]>
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> |
92d32170 | 31 | #include <asm/unaligned.h> |
39279cc3 CM |
32 | #include "ctree.h" |
33 | #include "disk-io.h" | |
34 | #include "transaction.h" | |
35 | #include "btrfs_inode.h" | |
39279cc3 | 36 | #include "print-tree.h" |
e6dcd2dc | 37 | #include "ordered-data.h" |
95819c05 | 38 | #include "xattr.h" |
e02119d5 | 39 | #include "tree-log.h" |
4a54c8c1 | 40 | #include "volumes.h" |
c8b97818 | 41 | #include "compression.h" |
b4ce94de | 42 | #include "locking.h" |
dc89e982 | 43 | #include "free-space-cache.h" |
581bb050 | 44 | #include "inode-map.h" |
38c227d8 | 45 | #include "backref.h" |
63541927 | 46 | #include "props.h" |
31193213 | 47 | #include "qgroup.h" |
dda3245e | 48 | #include "dedupe.h" |
39279cc3 CM |
49 | |
50 | struct btrfs_iget_args { | |
90d3e592 | 51 | struct btrfs_key *location; |
39279cc3 CM |
52 | struct btrfs_root *root; |
53 | }; | |
54 | ||
f28a4928 | 55 | struct btrfs_dio_data { |
f28a4928 FM |
56 | u64 reserve; |
57 | u64 unsubmitted_oe_range_start; | |
58 | u64 unsubmitted_oe_range_end; | |
4aaedfb0 | 59 | int overwrite; |
f28a4928 FM |
60 | }; |
61 | ||
6e1d5dcc AD |
62 | static const struct inode_operations btrfs_dir_inode_operations; |
63 | static const struct inode_operations btrfs_symlink_inode_operations; | |
64 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
65 | static const struct inode_operations btrfs_special_inode_operations; | |
66 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b | 67 | static const struct address_space_operations btrfs_aops; |
828c0950 | 68 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 69 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
70 | |
71 | static struct kmem_cache *btrfs_inode_cachep; | |
72 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 73 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 74 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
75 | |
76 | #define S_SHIFT 12 | |
4d4ab6d6 | 77 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
39279cc3 CM |
78 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
79 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
80 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
81 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
82 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
83 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
84 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
85 | }; | |
86 | ||
3972f260 | 87 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
213e8c55 | 88 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
5fd02043 | 89 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
90 | static noinline int cow_file_range(struct inode *inode, |
91 | struct page *locked_page, | |
dda3245e WX |
92 | u64 start, u64 end, u64 delalloc_end, |
93 | int *page_started, unsigned long *nr_written, | |
94 | int unlock, struct btrfs_dedupe_hash *hash); | |
6f9994db LB |
95 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
96 | u64 orig_start, u64 block_start, | |
97 | u64 block_len, u64 orig_block_len, | |
98 | u64 ram_bytes, int compress_type, | |
99 | int type); | |
7b128766 | 100 | |
52427260 QW |
101 | static void __endio_write_update_ordered(struct inode *inode, |
102 | const u64 offset, const u64 bytes, | |
103 | const bool uptodate); | |
104 | ||
105 | /* | |
106 | * Cleanup all submitted ordered extents in specified range to handle errors | |
52042d8e | 107 | * from the btrfs_run_delalloc_range() callback. |
52427260 QW |
108 | * |
109 | * NOTE: caller must ensure that when an error happens, it can not call | |
110 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
111 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
112 | * to be released, which we want to happen only when finishing the ordered | |
d1051d6e | 113 | * extent (btrfs_finish_ordered_io()). |
52427260 QW |
114 | */ |
115 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, | |
d1051d6e NB |
116 | struct page *locked_page, |
117 | u64 offset, u64 bytes) | |
52427260 | 118 | { |
63d71450 NA |
119 | unsigned long index = offset >> PAGE_SHIFT; |
120 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
d1051d6e NB |
121 | u64 page_start = page_offset(locked_page); |
122 | u64 page_end = page_start + PAGE_SIZE - 1; | |
123 | ||
63d71450 NA |
124 | struct page *page; |
125 | ||
126 | while (index <= end_index) { | |
127 | page = find_get_page(inode->i_mapping, index); | |
128 | index++; | |
129 | if (!page) | |
130 | continue; | |
131 | ClearPagePrivate2(page); | |
132 | put_page(page); | |
133 | } | |
d1051d6e NB |
134 | |
135 | /* | |
136 | * In case this page belongs to the delalloc range being instantiated | |
137 | * then skip it, since the first page of a range is going to be | |
138 | * properly cleaned up by the caller of run_delalloc_range | |
139 | */ | |
140 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { | |
141 | offset += PAGE_SIZE; | |
142 | bytes -= PAGE_SIZE; | |
143 | } | |
144 | ||
145 | return __endio_write_update_ordered(inode, offset, bytes, false); | |
52427260 QW |
146 | } |
147 | ||
48a3b636 | 148 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 149 | |
6a3891c5 JB |
150 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
151 | void btrfs_test_inode_set_ops(struct inode *inode) | |
152 | { | |
153 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
154 | } | |
155 | #endif | |
156 | ||
f34f57a3 | 157 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
158 | struct inode *inode, struct inode *dir, |
159 | const struct qstr *qstr) | |
0279b4cd JO |
160 | { |
161 | int err; | |
162 | ||
f34f57a3 | 163 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 164 | if (!err) |
2a7dba39 | 165 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
166 | return err; |
167 | } | |
168 | ||
c8b97818 CM |
169 | /* |
170 | * this does all the hard work for inserting an inline extent into | |
171 | * the btree. The caller should have done a btrfs_drop_extents so that | |
172 | * no overlapping inline items exist in the btree | |
173 | */ | |
40f76580 | 174 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 175 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
176 | struct btrfs_root *root, struct inode *inode, |
177 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 178 | int compress_type, |
c8b97818 CM |
179 | struct page **compressed_pages) |
180 | { | |
c8b97818 CM |
181 | struct extent_buffer *leaf; |
182 | struct page *page = NULL; | |
183 | char *kaddr; | |
184 | unsigned long ptr; | |
185 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
186 | int ret; |
187 | size_t cur_size = size; | |
c8b97818 | 188 | unsigned long offset; |
c8b97818 | 189 | |
fe3f566c | 190 | if (compressed_size && compressed_pages) |
c8b97818 | 191 | cur_size = compressed_size; |
c8b97818 | 192 | |
1acae57b | 193 | inode_add_bytes(inode, size); |
c8b97818 | 194 | |
1acae57b FDBM |
195 | if (!extent_inserted) { |
196 | struct btrfs_key key; | |
197 | size_t datasize; | |
c8b97818 | 198 | |
4a0cc7ca | 199 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 200 | key.offset = start; |
962a298f | 201 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 202 | |
1acae57b FDBM |
203 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
204 | path->leave_spinning = 1; | |
205 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
206 | datasize); | |
79b4f4c6 | 207 | if (ret) |
1acae57b | 208 | goto fail; |
c8b97818 CM |
209 | } |
210 | leaf = path->nodes[0]; | |
211 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
212 | struct btrfs_file_extent_item); | |
213 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
214 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
215 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
216 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
217 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
218 | ptr = btrfs_file_extent_inline_start(ei); | |
219 | ||
261507a0 | 220 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
221 | struct page *cpage; |
222 | int i = 0; | |
d397712b | 223 | while (compressed_size > 0) { |
c8b97818 | 224 | cpage = compressed_pages[i]; |
5b050f04 | 225 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 226 | PAGE_SIZE); |
c8b97818 | 227 | |
7ac687d9 | 228 | kaddr = kmap_atomic(cpage); |
c8b97818 | 229 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 230 | kunmap_atomic(kaddr); |
c8b97818 CM |
231 | |
232 | i++; | |
233 | ptr += cur_size; | |
234 | compressed_size -= cur_size; | |
235 | } | |
236 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 237 | compress_type); |
c8b97818 CM |
238 | } else { |
239 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 240 | start >> PAGE_SHIFT); |
c8b97818 | 241 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 242 | kaddr = kmap_atomic(page); |
7073017a | 243 | offset = offset_in_page(start); |
c8b97818 | 244 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 245 | kunmap_atomic(kaddr); |
09cbfeaf | 246 | put_page(page); |
c8b97818 CM |
247 | } |
248 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 249 | btrfs_release_path(path); |
c8b97818 | 250 | |
c2167754 YZ |
251 | /* |
252 | * we're an inline extent, so nobody can | |
253 | * extend the file past i_size without locking | |
254 | * a page we already have locked. | |
255 | * | |
256 | * We must do any isize and inode updates | |
257 | * before we unlock the pages. Otherwise we | |
258 | * could end up racing with unlink. | |
259 | */ | |
c8b97818 | 260 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 261 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 262 | |
c8b97818 | 263 | fail: |
79b4f4c6 | 264 | return ret; |
c8b97818 CM |
265 | } |
266 | ||
267 | ||
268 | /* | |
269 | * conditionally insert an inline extent into the file. This | |
270 | * does the checks required to make sure the data is small enough | |
271 | * to fit as an inline extent. | |
272 | */ | |
d02c0e20 | 273 | static noinline int cow_file_range_inline(struct inode *inode, u64 start, |
00361589 JB |
274 | u64 end, size_t compressed_size, |
275 | int compress_type, | |
276 | struct page **compressed_pages) | |
c8b97818 | 277 | { |
d02c0e20 | 278 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0b246afa | 279 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 280 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
281 | u64 isize = i_size_read(inode); |
282 | u64 actual_end = min(end + 1, isize); | |
283 | u64 inline_len = actual_end - start; | |
0b246afa | 284 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
285 | u64 data_len = inline_len; |
286 | int ret; | |
1acae57b FDBM |
287 | struct btrfs_path *path; |
288 | int extent_inserted = 0; | |
289 | u32 extent_item_size; | |
c8b97818 CM |
290 | |
291 | if (compressed_size) | |
292 | data_len = compressed_size; | |
293 | ||
294 | if (start > 0 || | |
0b246afa JM |
295 | actual_end > fs_info->sectorsize || |
296 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 297 | (!compressed_size && |
0b246afa | 298 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 299 | end + 1 < isize || |
0b246afa | 300 | data_len > fs_info->max_inline) { |
c8b97818 CM |
301 | return 1; |
302 | } | |
303 | ||
1acae57b FDBM |
304 | path = btrfs_alloc_path(); |
305 | if (!path) | |
306 | return -ENOMEM; | |
307 | ||
00361589 | 308 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
309 | if (IS_ERR(trans)) { |
310 | btrfs_free_path(path); | |
00361589 | 311 | return PTR_ERR(trans); |
1acae57b | 312 | } |
69fe2d75 | 313 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
00361589 | 314 | |
1acae57b FDBM |
315 | if (compressed_size && compressed_pages) |
316 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
317 | compressed_size); | |
318 | else | |
319 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
320 | inline_len); | |
321 | ||
322 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
323 | start, aligned_end, NULL, | |
324 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 | 325 | if (ret) { |
66642832 | 326 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
327 | goto out; |
328 | } | |
c8b97818 CM |
329 | |
330 | if (isize > actual_end) | |
331 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
332 | ret = insert_inline_extent(trans, path, extent_inserted, |
333 | root, inode, start, | |
c8b97818 | 334 | inline_len, compressed_size, |
fe3f566c | 335 | compress_type, compressed_pages); |
2adcac1a | 336 | if (ret && ret != -ENOSPC) { |
66642832 | 337 | btrfs_abort_transaction(trans, ret); |
00361589 | 338 | goto out; |
2adcac1a | 339 | } else if (ret == -ENOSPC) { |
00361589 JB |
340 | ret = 1; |
341 | goto out; | |
79787eaa | 342 | } |
2adcac1a | 343 | |
bdc20e67 | 344 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
dcdbc059 | 345 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
00361589 | 346 | out: |
94ed938a QW |
347 | /* |
348 | * Don't forget to free the reserved space, as for inlined extent | |
349 | * it won't count as data extent, free them directly here. | |
350 | * And at reserve time, it's always aligned to page size, so | |
351 | * just free one page here. | |
352 | */ | |
bc42bda2 | 353 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 354 | btrfs_free_path(path); |
3a45bb20 | 355 | btrfs_end_transaction(trans); |
00361589 | 356 | return ret; |
c8b97818 CM |
357 | } |
358 | ||
771ed689 CM |
359 | struct async_extent { |
360 | u64 start; | |
361 | u64 ram_size; | |
362 | u64 compressed_size; | |
363 | struct page **pages; | |
364 | unsigned long nr_pages; | |
261507a0 | 365 | int compress_type; |
771ed689 CM |
366 | struct list_head list; |
367 | }; | |
368 | ||
369 | struct async_cow { | |
370 | struct inode *inode; | |
600b6cf4 | 371 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
372 | struct page *locked_page; |
373 | u64 start; | |
374 | u64 end; | |
f82b7359 | 375 | unsigned int write_flags; |
771ed689 CM |
376 | struct list_head extents; |
377 | struct btrfs_work work; | |
378 | }; | |
379 | ||
380 | static noinline int add_async_extent(struct async_cow *cow, | |
381 | u64 start, u64 ram_size, | |
382 | u64 compressed_size, | |
383 | struct page **pages, | |
261507a0 LZ |
384 | unsigned long nr_pages, |
385 | int compress_type) | |
771ed689 CM |
386 | { |
387 | struct async_extent *async_extent; | |
388 | ||
389 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 390 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
391 | async_extent->start = start; |
392 | async_extent->ram_size = ram_size; | |
393 | async_extent->compressed_size = compressed_size; | |
394 | async_extent->pages = pages; | |
395 | async_extent->nr_pages = nr_pages; | |
261507a0 | 396 | async_extent->compress_type = compress_type; |
771ed689 CM |
397 | list_add_tail(&async_extent->list, &cow->extents); |
398 | return 0; | |
399 | } | |
400 | ||
c2fcdcdf | 401 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
f79707b0 | 402 | { |
0b246afa | 403 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 WS |
404 | |
405 | /* force compress */ | |
0b246afa | 406 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 407 | return 1; |
eec63c65 DS |
408 | /* defrag ioctl */ |
409 | if (BTRFS_I(inode)->defrag_compress) | |
410 | return 1; | |
f79707b0 WS |
411 | /* bad compression ratios */ |
412 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
413 | return 0; | |
0b246afa | 414 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 | 415 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
b52aa8c9 | 416 | BTRFS_I(inode)->prop_compress) |
c2fcdcdf | 417 | return btrfs_compress_heuristic(inode, start, end); |
f79707b0 WS |
418 | return 0; |
419 | } | |
420 | ||
6158e1ce | 421 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
422 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
423 | { | |
424 | /* If this is a small write inside eof, kick off a defrag */ | |
425 | if (num_bytes < small_write && | |
6158e1ce | 426 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
427 | btrfs_add_inode_defrag(NULL, inode); |
428 | } | |
429 | ||
d352ac68 | 430 | /* |
771ed689 CM |
431 | * we create compressed extents in two phases. The first |
432 | * phase compresses a range of pages that have already been | |
433 | * locked (both pages and state bits are locked). | |
c8b97818 | 434 | * |
771ed689 CM |
435 | * This is done inside an ordered work queue, and the compression |
436 | * is spread across many cpus. The actual IO submission is step | |
437 | * two, and the ordered work queue takes care of making sure that | |
438 | * happens in the same order things were put onto the queue by | |
439 | * writepages and friends. | |
c8b97818 | 440 | * |
771ed689 CM |
441 | * If this code finds it can't get good compression, it puts an |
442 | * entry onto the work queue to write the uncompressed bytes. This | |
443 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
444 | * are written in the same order that the flusher thread sent them |
445 | * down. | |
d352ac68 | 446 | */ |
c44f649e | 447 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
448 | struct page *locked_page, |
449 | u64 start, u64 end, | |
450 | struct async_cow *async_cow, | |
451 | int *num_added) | |
b888db2b | 452 | { |
0b246afa | 453 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0b246afa | 454 | u64 blocksize = fs_info->sectorsize; |
c8b97818 | 455 | u64 actual_end; |
e6dcd2dc | 456 | int ret = 0; |
c8b97818 CM |
457 | struct page **pages = NULL; |
458 | unsigned long nr_pages; | |
c8b97818 CM |
459 | unsigned long total_compressed = 0; |
460 | unsigned long total_in = 0; | |
c8b97818 CM |
461 | int i; |
462 | int will_compress; | |
0b246afa | 463 | int compress_type = fs_info->compress_type; |
4adaa611 | 464 | int redirty = 0; |
b888db2b | 465 | |
6158e1ce NB |
466 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
467 | SZ_16K); | |
4cb5300b | 468 | |
62b37622 | 469 | actual_end = min_t(u64, i_size_read(inode), end + 1); |
c8b97818 CM |
470 | again: |
471 | will_compress = 0; | |
09cbfeaf | 472 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
473 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
474 | nr_pages = min_t(unsigned long, nr_pages, | |
475 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 476 | |
f03d9301 CM |
477 | /* |
478 | * we don't want to send crud past the end of i_size through | |
479 | * compression, that's just a waste of CPU time. So, if the | |
480 | * end of the file is before the start of our current | |
481 | * requested range of bytes, we bail out to the uncompressed | |
482 | * cleanup code that can deal with all of this. | |
483 | * | |
484 | * It isn't really the fastest way to fix things, but this is a | |
485 | * very uncommon corner. | |
486 | */ | |
487 | if (actual_end <= start) | |
488 | goto cleanup_and_bail_uncompressed; | |
489 | ||
c8b97818 CM |
490 | total_compressed = actual_end - start; |
491 | ||
4bcbb332 SW |
492 | /* |
493 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 494 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
495 | */ |
496 | if (total_compressed <= blocksize && | |
497 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
498 | goto cleanup_and_bail_uncompressed; | |
499 | ||
069eac78 DS |
500 | total_compressed = min_t(unsigned long, total_compressed, |
501 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
502 | total_in = 0; |
503 | ret = 0; | |
db94535d | 504 | |
771ed689 CM |
505 | /* |
506 | * we do compression for mount -o compress and when the | |
507 | * inode has not been flagged as nocompress. This flag can | |
508 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 509 | */ |
c2fcdcdf | 510 | if (inode_need_compress(inode, start, end)) { |
c8b97818 | 511 | WARN_ON(pages); |
31e818fe | 512 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
513 | if (!pages) { |
514 | /* just bail out to the uncompressed code */ | |
3527a018 | 515 | nr_pages = 0; |
560f7d75 LZ |
516 | goto cont; |
517 | } | |
c8b97818 | 518 | |
eec63c65 DS |
519 | if (BTRFS_I(inode)->defrag_compress) |
520 | compress_type = BTRFS_I(inode)->defrag_compress; | |
521 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 522 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 523 | |
4adaa611 CM |
524 | /* |
525 | * we need to call clear_page_dirty_for_io on each | |
526 | * page in the range. Otherwise applications with the file | |
527 | * mmap'd can wander in and change the page contents while | |
528 | * we are compressing them. | |
529 | * | |
530 | * If the compression fails for any reason, we set the pages | |
531 | * dirty again later on. | |
e9679de3 TT |
532 | * |
533 | * Note that the remaining part is redirtied, the start pointer | |
534 | * has moved, the end is the original one. | |
4adaa611 | 535 | */ |
e9679de3 TT |
536 | if (!redirty) { |
537 | extent_range_clear_dirty_for_io(inode, start, end); | |
538 | redirty = 1; | |
539 | } | |
f51d2b59 DS |
540 | |
541 | /* Compression level is applied here and only here */ | |
542 | ret = btrfs_compress_pages( | |
543 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 544 | inode->i_mapping, start, |
38c31464 | 545 | pages, |
4d3a800e | 546 | &nr_pages, |
261507a0 | 547 | &total_in, |
e5d74902 | 548 | &total_compressed); |
c8b97818 CM |
549 | |
550 | if (!ret) { | |
7073017a | 551 | unsigned long offset = offset_in_page(total_compressed); |
4d3a800e | 552 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
553 | char *kaddr; |
554 | ||
555 | /* zero the tail end of the last page, we might be | |
556 | * sending it down to disk | |
557 | */ | |
558 | if (offset) { | |
7ac687d9 | 559 | kaddr = kmap_atomic(page); |
c8b97818 | 560 | memset(kaddr + offset, 0, |
09cbfeaf | 561 | PAGE_SIZE - offset); |
7ac687d9 | 562 | kunmap_atomic(kaddr); |
c8b97818 CM |
563 | } |
564 | will_compress = 1; | |
565 | } | |
566 | } | |
560f7d75 | 567 | cont: |
c8b97818 CM |
568 | if (start == 0) { |
569 | /* lets try to make an inline extent */ | |
6018ba0a | 570 | if (ret || total_in < actual_end) { |
c8b97818 | 571 | /* we didn't compress the entire range, try |
771ed689 | 572 | * to make an uncompressed inline extent. |
c8b97818 | 573 | */ |
d02c0e20 NB |
574 | ret = cow_file_range_inline(inode, start, end, 0, |
575 | BTRFS_COMPRESS_NONE, NULL); | |
c8b97818 | 576 | } else { |
771ed689 | 577 | /* try making a compressed inline extent */ |
d02c0e20 | 578 | ret = cow_file_range_inline(inode, start, end, |
fe3f566c LZ |
579 | total_compressed, |
580 | compress_type, pages); | |
c8b97818 | 581 | } |
79787eaa | 582 | if (ret <= 0) { |
151a41bc | 583 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
584 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
585 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
586 | unsigned long page_error_op; |
587 | ||
e6eb4314 | 588 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 589 | |
771ed689 | 590 | /* |
79787eaa JM |
591 | * inline extent creation worked or returned error, |
592 | * we don't need to create any more async work items. | |
593 | * Unlock and free up our temp pages. | |
8b62f87b JB |
594 | * |
595 | * We use DO_ACCOUNTING here because we need the | |
596 | * delalloc_release_metadata to be done _after_ we drop | |
597 | * our outstanding extent for clearing delalloc for this | |
598 | * range. | |
771ed689 | 599 | */ |
ba8b04c1 QW |
600 | extent_clear_unlock_delalloc(inode, start, end, end, |
601 | NULL, clear_flags, | |
602 | PAGE_UNLOCK | | |
c2790a2e JB |
603 | PAGE_CLEAR_DIRTY | |
604 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 605 | page_error_op | |
c2790a2e | 606 | PAGE_END_WRITEBACK); |
c8b97818 CM |
607 | goto free_pages_out; |
608 | } | |
609 | } | |
610 | ||
611 | if (will_compress) { | |
612 | /* | |
613 | * we aren't doing an inline extent round the compressed size | |
614 | * up to a block size boundary so the allocator does sane | |
615 | * things | |
616 | */ | |
fda2832f | 617 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
618 | |
619 | /* | |
620 | * one last check to make sure the compression is really a | |
170607eb TT |
621 | * win, compare the page count read with the blocks on disk, |
622 | * compression must free at least one sector size | |
c8b97818 | 623 | */ |
09cbfeaf | 624 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 625 | if (total_compressed + blocksize <= total_in) { |
c8bb0c8b AS |
626 | *num_added += 1; |
627 | ||
628 | /* | |
629 | * The async work queues will take care of doing actual | |
630 | * allocation on disk for these compressed pages, and | |
631 | * will submit them to the elevator. | |
632 | */ | |
1170862d | 633 | add_async_extent(async_cow, start, total_in, |
4d3a800e | 634 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
635 | compress_type); |
636 | ||
1170862d TT |
637 | if (start + total_in < end) { |
638 | start += total_in; | |
c8bb0c8b AS |
639 | pages = NULL; |
640 | cond_resched(); | |
641 | goto again; | |
642 | } | |
643 | return; | |
c8b97818 CM |
644 | } |
645 | } | |
c8bb0c8b | 646 | if (pages) { |
c8b97818 CM |
647 | /* |
648 | * the compression code ran but failed to make things smaller, | |
649 | * free any pages it allocated and our page pointer array | |
650 | */ | |
4d3a800e | 651 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 652 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 653 | put_page(pages[i]); |
c8b97818 CM |
654 | } |
655 | kfree(pages); | |
656 | pages = NULL; | |
657 | total_compressed = 0; | |
4d3a800e | 658 | nr_pages = 0; |
c8b97818 CM |
659 | |
660 | /* flag the file so we don't compress in the future */ | |
0b246afa | 661 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 662 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 663 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 664 | } |
c8b97818 | 665 | } |
f03d9301 | 666 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
667 | /* |
668 | * No compression, but we still need to write the pages in the file | |
669 | * we've been given so far. redirty the locked page if it corresponds | |
670 | * to our extent and set things up for the async work queue to run | |
671 | * cow_file_range to do the normal delalloc dance. | |
672 | */ | |
673 | if (page_offset(locked_page) >= start && | |
674 | page_offset(locked_page) <= end) | |
675 | __set_page_dirty_nobuffers(locked_page); | |
676 | /* unlocked later on in the async handlers */ | |
677 | ||
678 | if (redirty) | |
679 | extent_range_redirty_for_io(inode, start, end); | |
680 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
681 | BTRFS_COMPRESS_NONE); | |
682 | *num_added += 1; | |
3b951516 | 683 | |
c44f649e | 684 | return; |
771ed689 CM |
685 | |
686 | free_pages_out: | |
4d3a800e | 687 | for (i = 0; i < nr_pages; i++) { |
771ed689 | 688 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 689 | put_page(pages[i]); |
771ed689 | 690 | } |
d397712b | 691 | kfree(pages); |
771ed689 | 692 | } |
771ed689 | 693 | |
40ae837b FM |
694 | static void free_async_extent_pages(struct async_extent *async_extent) |
695 | { | |
696 | int i; | |
697 | ||
698 | if (!async_extent->pages) | |
699 | return; | |
700 | ||
701 | for (i = 0; i < async_extent->nr_pages; i++) { | |
702 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 703 | put_page(async_extent->pages[i]); |
40ae837b FM |
704 | } |
705 | kfree(async_extent->pages); | |
706 | async_extent->nr_pages = 0; | |
707 | async_extent->pages = NULL; | |
771ed689 CM |
708 | } |
709 | ||
710 | /* | |
711 | * phase two of compressed writeback. This is the ordered portion | |
712 | * of the code, which only gets called in the order the work was | |
713 | * queued. We walk all the async extents created by compress_file_range | |
714 | * and send them down to the disk. | |
715 | */ | |
532425ff | 716 | static noinline void submit_compressed_extents(struct async_cow *async_cow) |
771ed689 | 717 | { |
532425ff | 718 | struct inode *inode = async_cow->inode; |
0b246afa | 719 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
720 | struct async_extent *async_extent; |
721 | u64 alloc_hint = 0; | |
771ed689 CM |
722 | struct btrfs_key ins; |
723 | struct extent_map *em; | |
724 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
771ed689 | 725 | struct extent_io_tree *io_tree; |
f5a84ee3 | 726 | int ret = 0; |
771ed689 | 727 | |
3e04e7f1 | 728 | again: |
d397712b | 729 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
730 | async_extent = list_entry(async_cow->extents.next, |
731 | struct async_extent, list); | |
732 | list_del(&async_extent->list); | |
c8b97818 | 733 | |
771ed689 CM |
734 | io_tree = &BTRFS_I(inode)->io_tree; |
735 | ||
f5a84ee3 | 736 | retry: |
771ed689 CM |
737 | /* did the compression code fall back to uncompressed IO? */ |
738 | if (!async_extent->pages) { | |
739 | int page_started = 0; | |
740 | unsigned long nr_written = 0; | |
741 | ||
742 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 743 | async_extent->start + |
d0082371 | 744 | async_extent->ram_size - 1); |
771ed689 CM |
745 | |
746 | /* allocate blocks */ | |
f5a84ee3 JB |
747 | ret = cow_file_range(inode, async_cow->locked_page, |
748 | async_extent->start, | |
749 | async_extent->start + | |
750 | async_extent->ram_size - 1, | |
dda3245e WX |
751 | async_extent->start + |
752 | async_extent->ram_size - 1, | |
753 | &page_started, &nr_written, 0, | |
754 | NULL); | |
771ed689 | 755 | |
79787eaa JM |
756 | /* JDM XXX */ |
757 | ||
771ed689 CM |
758 | /* |
759 | * if page_started, cow_file_range inserted an | |
760 | * inline extent and took care of all the unlocking | |
761 | * and IO for us. Otherwise, we need to submit | |
762 | * all those pages down to the drive. | |
763 | */ | |
f5a84ee3 | 764 | if (!page_started && !ret) |
5e3ee236 NB |
765 | extent_write_locked_range(inode, |
766 | async_extent->start, | |
d397712b | 767 | async_extent->start + |
771ed689 | 768 | async_extent->ram_size - 1, |
771ed689 | 769 | WB_SYNC_ALL); |
3e04e7f1 JB |
770 | else if (ret) |
771 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
772 | kfree(async_extent); |
773 | cond_resched(); | |
774 | continue; | |
775 | } | |
776 | ||
777 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 778 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 779 | |
18513091 | 780 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
781 | async_extent->compressed_size, |
782 | async_extent->compressed_size, | |
e570fd27 | 783 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 784 | if (ret) { |
40ae837b | 785 | free_async_extent_pages(async_extent); |
3e04e7f1 | 786 | |
fdf8e2ea JB |
787 | if (ret == -ENOSPC) { |
788 | unlock_extent(io_tree, async_extent->start, | |
789 | async_extent->start + | |
790 | async_extent->ram_size - 1); | |
ce62003f LB |
791 | |
792 | /* | |
793 | * we need to redirty the pages if we decide to | |
794 | * fallback to uncompressed IO, otherwise we | |
795 | * will not submit these pages down to lower | |
796 | * layers. | |
797 | */ | |
798 | extent_range_redirty_for_io(inode, | |
799 | async_extent->start, | |
800 | async_extent->start + | |
801 | async_extent->ram_size - 1); | |
802 | ||
79787eaa | 803 | goto retry; |
fdf8e2ea | 804 | } |
3e04e7f1 | 805 | goto out_free; |
f5a84ee3 | 806 | } |
c2167754 YZ |
807 | /* |
808 | * here we're doing allocation and writeback of the | |
809 | * compressed pages | |
810 | */ | |
6f9994db LB |
811 | em = create_io_em(inode, async_extent->start, |
812 | async_extent->ram_size, /* len */ | |
813 | async_extent->start, /* orig_start */ | |
814 | ins.objectid, /* block_start */ | |
815 | ins.offset, /* block_len */ | |
816 | ins.offset, /* orig_block_len */ | |
817 | async_extent->ram_size, /* ram_bytes */ | |
818 | async_extent->compress_type, | |
819 | BTRFS_ORDERED_COMPRESSED); | |
820 | if (IS_ERR(em)) | |
821 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 822 | goto out_free_reserve; |
6f9994db | 823 | free_extent_map(em); |
3e04e7f1 | 824 | |
261507a0 LZ |
825 | ret = btrfs_add_ordered_extent_compress(inode, |
826 | async_extent->start, | |
827 | ins.objectid, | |
828 | async_extent->ram_size, | |
829 | ins.offset, | |
830 | BTRFS_ORDERED_COMPRESSED, | |
831 | async_extent->compress_type); | |
d9f85963 | 832 | if (ret) { |
dcdbc059 NB |
833 | btrfs_drop_extent_cache(BTRFS_I(inode), |
834 | async_extent->start, | |
d9f85963 FM |
835 | async_extent->start + |
836 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 837 | goto out_free_reserve; |
d9f85963 | 838 | } |
0b246afa | 839 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 840 | |
771ed689 CM |
841 | /* |
842 | * clear dirty, set writeback and unlock the pages. | |
843 | */ | |
c2790a2e | 844 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
845 | async_extent->start + |
846 | async_extent->ram_size - 1, | |
a791e35e CM |
847 | async_extent->start + |
848 | async_extent->ram_size - 1, | |
151a41bc JB |
849 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
850 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 851 | PAGE_SET_WRITEBACK); |
4e4cbee9 | 852 | if (btrfs_submit_compressed_write(inode, |
d397712b CM |
853 | async_extent->start, |
854 | async_extent->ram_size, | |
855 | ins.objectid, | |
856 | ins.offset, async_extent->pages, | |
f82b7359 LB |
857 | async_extent->nr_pages, |
858 | async_cow->write_flags)) { | |
fce2a4e6 FM |
859 | struct page *p = async_extent->pages[0]; |
860 | const u64 start = async_extent->start; | |
861 | const u64 end = start + async_extent->ram_size - 1; | |
862 | ||
863 | p->mapping = inode->i_mapping; | |
c629732d | 864 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
7087a9d8 | 865 | |
fce2a4e6 | 866 | p->mapping = NULL; |
ba8b04c1 QW |
867 | extent_clear_unlock_delalloc(inode, start, end, end, |
868 | NULL, 0, | |
fce2a4e6 FM |
869 | PAGE_END_WRITEBACK | |
870 | PAGE_SET_ERROR); | |
40ae837b | 871 | free_async_extent_pages(async_extent); |
fce2a4e6 | 872 | } |
771ed689 CM |
873 | alloc_hint = ins.objectid + ins.offset; |
874 | kfree(async_extent); | |
875 | cond_resched(); | |
876 | } | |
dec8f175 | 877 | return; |
3e04e7f1 | 878 | out_free_reserve: |
0b246afa | 879 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 880 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 881 | out_free: |
c2790a2e | 882 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
883 | async_extent->start + |
884 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
885 | async_extent->start + |
886 | async_extent->ram_size - 1, | |
c2790a2e | 887 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 888 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
889 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
890 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
891 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
892 | PAGE_SET_ERROR); | |
40ae837b | 893 | free_async_extent_pages(async_extent); |
79787eaa | 894 | kfree(async_extent); |
3e04e7f1 | 895 | goto again; |
771ed689 CM |
896 | } |
897 | ||
4b46fce2 JB |
898 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
899 | u64 num_bytes) | |
900 | { | |
901 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
902 | struct extent_map *em; | |
903 | u64 alloc_hint = 0; | |
904 | ||
905 | read_lock(&em_tree->lock); | |
906 | em = search_extent_mapping(em_tree, start, num_bytes); | |
907 | if (em) { | |
908 | /* | |
909 | * if block start isn't an actual block number then find the | |
910 | * first block in this inode and use that as a hint. If that | |
911 | * block is also bogus then just don't worry about it. | |
912 | */ | |
913 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
914 | free_extent_map(em); | |
915 | em = search_extent_mapping(em_tree, 0, 0); | |
916 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
917 | alloc_hint = em->block_start; | |
918 | if (em) | |
919 | free_extent_map(em); | |
920 | } else { | |
921 | alloc_hint = em->block_start; | |
922 | free_extent_map(em); | |
923 | } | |
924 | } | |
925 | read_unlock(&em_tree->lock); | |
926 | ||
927 | return alloc_hint; | |
928 | } | |
929 | ||
771ed689 CM |
930 | /* |
931 | * when extent_io.c finds a delayed allocation range in the file, | |
932 | * the call backs end up in this code. The basic idea is to | |
933 | * allocate extents on disk for the range, and create ordered data structs | |
934 | * in ram to track those extents. | |
935 | * | |
936 | * locked_page is the page that writepage had locked already. We use | |
937 | * it to make sure we don't do extra locks or unlocks. | |
938 | * | |
939 | * *page_started is set to one if we unlock locked_page and do everything | |
940 | * required to start IO on it. It may be clean and already done with | |
941 | * IO when we return. | |
942 | */ | |
00361589 JB |
943 | static noinline int cow_file_range(struct inode *inode, |
944 | struct page *locked_page, | |
dda3245e WX |
945 | u64 start, u64 end, u64 delalloc_end, |
946 | int *page_started, unsigned long *nr_written, | |
947 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 948 | { |
0b246afa | 949 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 950 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
951 | u64 alloc_hint = 0; |
952 | u64 num_bytes; | |
953 | unsigned long ram_size; | |
a315e68f | 954 | u64 cur_alloc_size = 0; |
0b246afa | 955 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
956 | struct btrfs_key ins; |
957 | struct extent_map *em; | |
a315e68f FM |
958 | unsigned clear_bits; |
959 | unsigned long page_ops; | |
960 | bool extent_reserved = false; | |
771ed689 CM |
961 | int ret = 0; |
962 | ||
70ddc553 | 963 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 964 | WARN_ON_ONCE(1); |
29bce2f3 JB |
965 | ret = -EINVAL; |
966 | goto out_unlock; | |
02ecd2c2 | 967 | } |
771ed689 | 968 | |
fda2832f | 969 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 | 970 | num_bytes = max(blocksize, num_bytes); |
566b1760 | 971 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 972 | |
6158e1ce | 973 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 974 | |
771ed689 CM |
975 | if (start == 0) { |
976 | /* lets try to make an inline extent */ | |
d02c0e20 NB |
977 | ret = cow_file_range_inline(inode, start, end, 0, |
978 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 979 | if (ret == 0) { |
8b62f87b JB |
980 | /* |
981 | * We use DO_ACCOUNTING here because we need the | |
982 | * delalloc_release_metadata to be run _after_ we drop | |
983 | * our outstanding extent for clearing delalloc for this | |
984 | * range. | |
985 | */ | |
ba8b04c1 QW |
986 | extent_clear_unlock_delalloc(inode, start, end, |
987 | delalloc_end, NULL, | |
c2790a2e | 988 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
989 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
990 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
991 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
992 | PAGE_END_WRITEBACK); | |
771ed689 | 993 | *nr_written = *nr_written + |
09cbfeaf | 994 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 995 | *page_started = 1; |
771ed689 | 996 | goto out; |
79787eaa | 997 | } else if (ret < 0) { |
79787eaa | 998 | goto out_unlock; |
771ed689 CM |
999 | } |
1000 | } | |
1001 | ||
4b46fce2 | 1002 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
1003 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1004 | start + num_bytes - 1, 0); | |
771ed689 | 1005 | |
3752d22f AJ |
1006 | while (num_bytes > 0) { |
1007 | cur_alloc_size = num_bytes; | |
18513091 | 1008 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 1009 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 1010 | &ins, 1, 1); |
00361589 | 1011 | if (ret < 0) |
79787eaa | 1012 | goto out_unlock; |
a315e68f FM |
1013 | cur_alloc_size = ins.offset; |
1014 | extent_reserved = true; | |
d397712b | 1015 | |
771ed689 | 1016 | ram_size = ins.offset; |
6f9994db LB |
1017 | em = create_io_em(inode, start, ins.offset, /* len */ |
1018 | start, /* orig_start */ | |
1019 | ins.objectid, /* block_start */ | |
1020 | ins.offset, /* block_len */ | |
1021 | ins.offset, /* orig_block_len */ | |
1022 | ram_size, /* ram_bytes */ | |
1023 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1024 | BTRFS_ORDERED_REGULAR /* type */); |
090a127a SY |
1025 | if (IS_ERR(em)) { |
1026 | ret = PTR_ERR(em); | |
ace68bac | 1027 | goto out_reserve; |
090a127a | 1028 | } |
6f9994db | 1029 | free_extent_map(em); |
e6dcd2dc | 1030 | |
e6dcd2dc | 1031 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1032 | ram_size, cur_alloc_size, 0); |
ace68bac | 1033 | if (ret) |
d9f85963 | 1034 | goto out_drop_extent_cache; |
c8b97818 | 1035 | |
17d217fe YZ |
1036 | if (root->root_key.objectid == |
1037 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1038 | ret = btrfs_reloc_clone_csums(inode, start, | |
1039 | cur_alloc_size); | |
4dbd80fb QW |
1040 | /* |
1041 | * Only drop cache here, and process as normal. | |
1042 | * | |
1043 | * We must not allow extent_clear_unlock_delalloc() | |
1044 | * at out_unlock label to free meta of this ordered | |
1045 | * extent, as its meta should be freed by | |
1046 | * btrfs_finish_ordered_io(). | |
1047 | * | |
1048 | * So we must continue until @start is increased to | |
1049 | * skip current ordered extent. | |
1050 | */ | |
00361589 | 1051 | if (ret) |
4dbd80fb QW |
1052 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1053 | start + ram_size - 1, 0); | |
17d217fe YZ |
1054 | } |
1055 | ||
0b246afa | 1056 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1057 | |
c8b97818 CM |
1058 | /* we're not doing compressed IO, don't unlock the first |
1059 | * page (which the caller expects to stay locked), don't | |
1060 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1061 | * |
1062 | * Do set the Private2 bit so we know this page was properly | |
1063 | * setup for writepage | |
c8b97818 | 1064 | */ |
a315e68f FM |
1065 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1066 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1067 | |
c2790a2e | 1068 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1069 | start + ram_size - 1, |
1070 | delalloc_end, locked_page, | |
c2790a2e | 1071 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1072 | page_ops); |
3752d22f AJ |
1073 | if (num_bytes < cur_alloc_size) |
1074 | num_bytes = 0; | |
4dbd80fb | 1075 | else |
3752d22f | 1076 | num_bytes -= cur_alloc_size; |
c59f8951 CM |
1077 | alloc_hint = ins.objectid + ins.offset; |
1078 | start += cur_alloc_size; | |
a315e68f | 1079 | extent_reserved = false; |
4dbd80fb QW |
1080 | |
1081 | /* | |
1082 | * btrfs_reloc_clone_csums() error, since start is increased | |
1083 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1084 | * free metadata of current ordered extent, we're OK to exit. | |
1085 | */ | |
1086 | if (ret) | |
1087 | goto out_unlock; | |
b888db2b | 1088 | } |
79787eaa | 1089 | out: |
be20aa9d | 1090 | return ret; |
b7d5b0a8 | 1091 | |
d9f85963 | 1092 | out_drop_extent_cache: |
dcdbc059 | 1093 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1094 | out_reserve: |
0b246afa | 1095 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1096 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1097 | out_unlock: |
a7e3b975 FM |
1098 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1099 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1100 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1101 | PAGE_END_WRITEBACK; | |
1102 | /* | |
1103 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1104 | * failed to create the respective ordered extent, then it means that | |
1105 | * when we reserved the extent we decremented the extent's size from | |
1106 | * the data space_info's bytes_may_use counter and incremented the | |
1107 | * space_info's bytes_reserved counter by the same amount. We must make | |
1108 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1109 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1110 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1111 | */ | |
1112 | if (extent_reserved) { | |
1113 | extent_clear_unlock_delalloc(inode, start, | |
1114 | start + cur_alloc_size, | |
1115 | start + cur_alloc_size, | |
1116 | locked_page, | |
1117 | clear_bits, | |
1118 | page_ops); | |
1119 | start += cur_alloc_size; | |
1120 | if (start >= end) | |
1121 | goto out; | |
1122 | } | |
ba8b04c1 QW |
1123 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1124 | locked_page, | |
a315e68f FM |
1125 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1126 | page_ops); | |
79787eaa | 1127 | goto out; |
771ed689 | 1128 | } |
c8b97818 | 1129 | |
771ed689 CM |
1130 | /* |
1131 | * work queue call back to started compression on a file and pages | |
1132 | */ | |
1133 | static noinline void async_cow_start(struct btrfs_work *work) | |
1134 | { | |
1135 | struct async_cow *async_cow; | |
1136 | int num_added = 0; | |
1137 | async_cow = container_of(work, struct async_cow, work); | |
1138 | ||
1139 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1140 | async_cow->start, async_cow->end, async_cow, | |
1141 | &num_added); | |
8180ef88 | 1142 | if (num_added == 0) { |
cb77fcd8 | 1143 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1144 | async_cow->inode = NULL; |
8180ef88 | 1145 | } |
771ed689 CM |
1146 | } |
1147 | ||
1148 | /* | |
1149 | * work queue call back to submit previously compressed pages | |
1150 | */ | |
1151 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1152 | { | |
0b246afa | 1153 | struct btrfs_fs_info *fs_info; |
771ed689 | 1154 | struct async_cow *async_cow; |
771ed689 CM |
1155 | unsigned long nr_pages; |
1156 | ||
1157 | async_cow = container_of(work, struct async_cow, work); | |
1158 | ||
600b6cf4 | 1159 | fs_info = async_cow->fs_info; |
09cbfeaf KS |
1160 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1161 | PAGE_SHIFT; | |
771ed689 | 1162 | |
093258e6 | 1163 | /* atomic_sub_return implies a barrier */ |
0b246afa | 1164 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
093258e6 DS |
1165 | 5 * SZ_1M) |
1166 | cond_wake_up_nomb(&fs_info->async_submit_wait); | |
771ed689 | 1167 | |
4546d178 NB |
1168 | /* |
1169 | * ->inode could be NULL if async_cow_start has failed to compress, | |
1170 | * in which case we don't have anything to submit, yet we need to | |
1171 | * always adjust ->async_delalloc_pages as its paired with the init | |
1172 | * happening in cow_file_range_async | |
1173 | */ | |
d397712b | 1174 | if (async_cow->inode) |
532425ff | 1175 | submit_compressed_extents(async_cow); |
771ed689 | 1176 | } |
c8b97818 | 1177 | |
771ed689 CM |
1178 | static noinline void async_cow_free(struct btrfs_work *work) |
1179 | { | |
1180 | struct async_cow *async_cow; | |
1181 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1182 | if (async_cow->inode) |
cb77fcd8 | 1183 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1184 | kfree(async_cow); |
1185 | } | |
1186 | ||
1187 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1188 | u64 start, u64 end, int *page_started, | |
f82b7359 LB |
1189 | unsigned long *nr_written, |
1190 | unsigned int write_flags) | |
771ed689 | 1191 | { |
0b246afa | 1192 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 | 1193 | struct async_cow *async_cow; |
771ed689 CM |
1194 | unsigned long nr_pages; |
1195 | u64 cur_end; | |
771ed689 | 1196 | |
a3429ab7 | 1197 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
ae0f1625 | 1198 | 1, 0, NULL); |
d397712b | 1199 | while (start < end) { |
771ed689 | 1200 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1201 | BUG_ON(!async_cow); /* -ENOMEM */ |
bd4691a0 NB |
1202 | /* |
1203 | * igrab is called higher up in the call chain, take only the | |
1204 | * lightweight reference for the callback lifetime | |
1205 | */ | |
1206 | ihold(inode); | |
1207 | async_cow->inode = inode; | |
600b6cf4 | 1208 | async_cow->fs_info = fs_info; |
771ed689 CM |
1209 | async_cow->locked_page = locked_page; |
1210 | async_cow->start = start; | |
f82b7359 | 1211 | async_cow->write_flags = write_flags; |
771ed689 | 1212 | |
f79707b0 | 1213 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1214 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1215 | cur_end = end; |
1216 | else | |
ee22184b | 1217 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1218 | |
1219 | async_cow->end = cur_end; | |
1220 | INIT_LIST_HEAD(&async_cow->extents); | |
1221 | ||
9e0af237 LB |
1222 | btrfs_init_work(&async_cow->work, |
1223 | btrfs_delalloc_helper, | |
1224 | async_cow_start, async_cow_submit, | |
1225 | async_cow_free); | |
771ed689 | 1226 | |
09cbfeaf KS |
1227 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1228 | PAGE_SHIFT; | |
0b246afa | 1229 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1230 | |
0b246afa | 1231 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1232 | |
771ed689 CM |
1233 | *nr_written += nr_pages; |
1234 | start = cur_end + 1; | |
1235 | } | |
1236 | *page_started = 1; | |
1237 | return 0; | |
be20aa9d CM |
1238 | } |
1239 | ||
2ff7e61e | 1240 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1241 | u64 bytenr, u64 num_bytes) |
1242 | { | |
1243 | int ret; | |
1244 | struct btrfs_ordered_sum *sums; | |
1245 | LIST_HEAD(list); | |
1246 | ||
0b246afa | 1247 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1248 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1249 | if (ret == 0 && list_empty(&list)) |
1250 | return 0; | |
1251 | ||
1252 | while (!list_empty(&list)) { | |
1253 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1254 | list_del(&sums->list); | |
1255 | kfree(sums); | |
1256 | } | |
58113753 LB |
1257 | if (ret < 0) |
1258 | return ret; | |
17d217fe YZ |
1259 | return 1; |
1260 | } | |
1261 | ||
d352ac68 CM |
1262 | /* |
1263 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1264 | * of the extents that exist in the file, and COWs the file as required. | |
1265 | * | |
1266 | * If no cow copies or snapshots exist, we write directly to the existing | |
1267 | * blocks on disk | |
1268 | */ | |
7f366cfe CM |
1269 | static noinline int run_delalloc_nocow(struct inode *inode, |
1270 | struct page *locked_page, | |
771ed689 CM |
1271 | u64 start, u64 end, int *page_started, int force, |
1272 | unsigned long *nr_written) | |
be20aa9d | 1273 | { |
0b246afa | 1274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d CM |
1275 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1276 | struct extent_buffer *leaf; | |
be20aa9d | 1277 | struct btrfs_path *path; |
80ff3856 | 1278 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1279 | struct btrfs_key found_key; |
6f9994db | 1280 | struct extent_map *em; |
80ff3856 YZ |
1281 | u64 cow_start; |
1282 | u64 cur_offset; | |
1283 | u64 extent_end; | |
5d4f98a2 | 1284 | u64 extent_offset; |
80ff3856 YZ |
1285 | u64 disk_bytenr; |
1286 | u64 num_bytes; | |
b4939680 | 1287 | u64 disk_num_bytes; |
cc95bef6 | 1288 | u64 ram_bytes; |
80ff3856 | 1289 | int extent_type; |
8ecebf4d | 1290 | int ret; |
d899e052 | 1291 | int type; |
80ff3856 YZ |
1292 | int nocow; |
1293 | int check_prev = 1; | |
82d5902d | 1294 | bool nolock; |
4a0cc7ca | 1295 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1296 | |
1297 | path = btrfs_alloc_path(); | |
17ca04af | 1298 | if (!path) { |
ba8b04c1 QW |
1299 | extent_clear_unlock_delalloc(inode, start, end, end, |
1300 | locked_page, | |
c2790a2e | 1301 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1302 | EXTENT_DO_ACCOUNTING | |
1303 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1304 | PAGE_CLEAR_DIRTY | |
1305 | PAGE_SET_WRITEBACK | | |
1306 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1307 | return -ENOMEM; |
17ca04af | 1308 | } |
82d5902d | 1309 | |
70ddc553 | 1310 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
82d5902d | 1311 | |
80ff3856 YZ |
1312 | cow_start = (u64)-1; |
1313 | cur_offset = start; | |
1314 | while (1) { | |
e4c3b2dc | 1315 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1316 | cur_offset, 0); |
d788a349 | 1317 | if (ret < 0) |
79787eaa | 1318 | goto error; |
80ff3856 YZ |
1319 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1320 | leaf = path->nodes[0]; | |
1321 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1322 | path->slots[0] - 1); | |
33345d01 | 1323 | if (found_key.objectid == ino && |
80ff3856 YZ |
1324 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1325 | path->slots[0]--; | |
1326 | } | |
1327 | check_prev = 0; | |
1328 | next_slot: | |
1329 | leaf = path->nodes[0]; | |
1330 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1331 | ret = btrfs_next_leaf(root, path); | |
e8916699 LB |
1332 | if (ret < 0) { |
1333 | if (cow_start != (u64)-1) | |
1334 | cur_offset = cow_start; | |
79787eaa | 1335 | goto error; |
e8916699 | 1336 | } |
80ff3856 YZ |
1337 | if (ret > 0) |
1338 | break; | |
1339 | leaf = path->nodes[0]; | |
1340 | } | |
be20aa9d | 1341 | |
80ff3856 YZ |
1342 | nocow = 0; |
1343 | disk_bytenr = 0; | |
17d217fe | 1344 | num_bytes = 0; |
80ff3856 YZ |
1345 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1346 | ||
1d512cb7 FM |
1347 | if (found_key.objectid > ino) |
1348 | break; | |
1349 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1350 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1351 | path->slots[0]++; | |
1352 | goto next_slot; | |
1353 | } | |
1354 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1355 | found_key.offset > end) |
1356 | break; | |
1357 | ||
1358 | if (found_key.offset > cur_offset) { | |
1359 | extent_end = found_key.offset; | |
e9061e21 | 1360 | extent_type = 0; |
80ff3856 YZ |
1361 | goto out_check; |
1362 | } | |
1363 | ||
1364 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1365 | struct btrfs_file_extent_item); | |
1366 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1367 | ||
cc95bef6 | 1368 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1369 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1370 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1371 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1372 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1373 | extent_end = found_key.offset + |
1374 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1375 | disk_num_bytes = |
1376 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1377 | if (extent_end <= start) { |
1378 | path->slots[0]++; | |
1379 | goto next_slot; | |
1380 | } | |
17d217fe YZ |
1381 | if (disk_bytenr == 0) |
1382 | goto out_check; | |
80ff3856 YZ |
1383 | if (btrfs_file_extent_compression(leaf, fi) || |
1384 | btrfs_file_extent_encryption(leaf, fi) || | |
1385 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1386 | goto out_check; | |
78d4295b EL |
1387 | /* |
1388 | * Do the same check as in btrfs_cross_ref_exist but | |
1389 | * without the unnecessary search. | |
1390 | */ | |
27a7ff55 LF |
1391 | if (!nolock && |
1392 | btrfs_file_extent_generation(leaf, fi) <= | |
78d4295b EL |
1393 | btrfs_root_last_snapshot(&root->root_item)) |
1394 | goto out_check; | |
d899e052 YZ |
1395 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1396 | goto out_check; | |
2ff7e61e | 1397 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1398 | goto out_check; |
58113753 LB |
1399 | ret = btrfs_cross_ref_exist(root, ino, |
1400 | found_key.offset - | |
1401 | extent_offset, disk_bytenr); | |
1402 | if (ret) { | |
1403 | /* | |
1404 | * ret could be -EIO if the above fails to read | |
1405 | * metadata. | |
1406 | */ | |
1407 | if (ret < 0) { | |
1408 | if (cow_start != (u64)-1) | |
1409 | cur_offset = cow_start; | |
1410 | goto error; | |
1411 | } | |
1412 | ||
1413 | WARN_ON_ONCE(nolock); | |
17d217fe | 1414 | goto out_check; |
58113753 | 1415 | } |
5d4f98a2 | 1416 | disk_bytenr += extent_offset; |
17d217fe YZ |
1417 | disk_bytenr += cur_offset - found_key.offset; |
1418 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1419 | /* |
1420 | * if there are pending snapshots for this root, | |
1421 | * we fall into common COW way. | |
1422 | */ | |
8ecebf4d RK |
1423 | if (!nolock && atomic_read(&root->snapshot_force_cow)) |
1424 | goto out_check; | |
17d217fe YZ |
1425 | /* |
1426 | * force cow if csum exists in the range. | |
1427 | * this ensure that csum for a given extent are | |
1428 | * either valid or do not exist. | |
1429 | */ | |
58113753 LB |
1430 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
1431 | num_bytes); | |
1432 | if (ret) { | |
58113753 LB |
1433 | /* |
1434 | * ret could be -EIO if the above fails to read | |
1435 | * metadata. | |
1436 | */ | |
1437 | if (ret < 0) { | |
1438 | if (cow_start != (u64)-1) | |
1439 | cur_offset = cow_start; | |
1440 | goto error; | |
1441 | } | |
1442 | WARN_ON_ONCE(nolock); | |
17d217fe | 1443 | goto out_check; |
91e1f56a | 1444 | } |
8ecebf4d | 1445 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1446 | goto out_check; |
80ff3856 YZ |
1447 | nocow = 1; |
1448 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1449 | extent_end = found_key.offset + | |
e41ca589 | 1450 | btrfs_file_extent_ram_bytes(leaf, fi); |
da17066c | 1451 | extent_end = ALIGN(extent_end, |
0b246afa | 1452 | fs_info->sectorsize); |
80ff3856 YZ |
1453 | } else { |
1454 | BUG_ON(1); | |
1455 | } | |
1456 | out_check: | |
1457 | if (extent_end <= start) { | |
1458 | path->slots[0]++; | |
f78c436c | 1459 | if (nocow) |
0b246afa | 1460 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1461 | goto next_slot; |
1462 | } | |
1463 | if (!nocow) { | |
1464 | if (cow_start == (u64)-1) | |
1465 | cow_start = cur_offset; | |
1466 | cur_offset = extent_end; | |
1467 | if (cur_offset > end) | |
1468 | break; | |
1469 | path->slots[0]++; | |
1470 | goto next_slot; | |
7ea394f1 YZ |
1471 | } |
1472 | ||
b3b4aa74 | 1473 | btrfs_release_path(path); |
80ff3856 | 1474 | if (cow_start != (u64)-1) { |
00361589 JB |
1475 | ret = cow_file_range(inode, locked_page, |
1476 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1477 | end, page_started, nr_written, 1, |
1478 | NULL); | |
e9894fd3 | 1479 | if (ret) { |
f78c436c | 1480 | if (nocow) |
0b246afa | 1481 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1482 | disk_bytenr); |
79787eaa | 1483 | goto error; |
e9894fd3 | 1484 | } |
80ff3856 | 1485 | cow_start = (u64)-1; |
7ea394f1 | 1486 | } |
80ff3856 | 1487 | |
d899e052 | 1488 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db LB |
1489 | u64 orig_start = found_key.offset - extent_offset; |
1490 | ||
1491 | em = create_io_em(inode, cur_offset, num_bytes, | |
1492 | orig_start, | |
1493 | disk_bytenr, /* block_start */ | |
1494 | num_bytes, /* block_len */ | |
1495 | disk_num_bytes, /* orig_block_len */ | |
1496 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1497 | BTRFS_ORDERED_PREALLOC); | |
1498 | if (IS_ERR(em)) { | |
6f9994db LB |
1499 | if (nocow) |
1500 | btrfs_dec_nocow_writers(fs_info, | |
1501 | disk_bytenr); | |
1502 | ret = PTR_ERR(em); | |
1503 | goto error; | |
d899e052 | 1504 | } |
6f9994db LB |
1505 | free_extent_map(em); |
1506 | } | |
1507 | ||
1508 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d899e052 YZ |
1509 | type = BTRFS_ORDERED_PREALLOC; |
1510 | } else { | |
1511 | type = BTRFS_ORDERED_NOCOW; | |
1512 | } | |
80ff3856 YZ |
1513 | |
1514 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1515 | num_bytes, num_bytes, type); |
f78c436c | 1516 | if (nocow) |
0b246afa | 1517 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1518 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1519 | |
efa56464 | 1520 | if (root->root_key.objectid == |
4dbd80fb QW |
1521 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1522 | /* | |
1523 | * Error handled later, as we must prevent | |
1524 | * extent_clear_unlock_delalloc() in error handler | |
1525 | * from freeing metadata of created ordered extent. | |
1526 | */ | |
efa56464 YZ |
1527 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1528 | num_bytes); | |
efa56464 | 1529 | |
c2790a2e | 1530 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1531 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1532 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1533 | EXTENT_DELALLOC | |
1534 | EXTENT_CLEAR_DATA_RESV, | |
1535 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1536 | ||
80ff3856 | 1537 | cur_offset = extent_end; |
4dbd80fb QW |
1538 | |
1539 | /* | |
1540 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1541 | * handler, as metadata for created ordered extent will only | |
1542 | * be freed by btrfs_finish_ordered_io(). | |
1543 | */ | |
1544 | if (ret) | |
1545 | goto error; | |
80ff3856 YZ |
1546 | if (cur_offset > end) |
1547 | break; | |
be20aa9d | 1548 | } |
b3b4aa74 | 1549 | btrfs_release_path(path); |
80ff3856 | 1550 | |
506481b2 | 1551 | if (cur_offset <= end && cow_start == (u64)-1) |
80ff3856 | 1552 | cow_start = cur_offset; |
17ca04af | 1553 | |
80ff3856 | 1554 | if (cow_start != (u64)-1) { |
506481b2 | 1555 | cur_offset = end; |
dda3245e WX |
1556 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1557 | page_started, nr_written, 1, NULL); | |
d788a349 | 1558 | if (ret) |
79787eaa | 1559 | goto error; |
80ff3856 YZ |
1560 | } |
1561 | ||
79787eaa | 1562 | error: |
17ca04af | 1563 | if (ret && cur_offset < end) |
ba8b04c1 | 1564 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1565 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1566 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1567 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1568 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1569 | PAGE_SET_WRITEBACK | |
1570 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1571 | btrfs_free_path(path); |
79787eaa | 1572 | return ret; |
be20aa9d CM |
1573 | } |
1574 | ||
47059d93 WS |
1575 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1576 | { | |
1577 | ||
1578 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1579 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1580 | return 0; | |
1581 | ||
1582 | /* | |
1583 | * @defrag_bytes is a hint value, no spinlock held here, | |
1584 | * if is not zero, it means the file is defragging. | |
1585 | * Force cow if given extent needs to be defragged. | |
1586 | */ | |
1587 | if (BTRFS_I(inode)->defrag_bytes && | |
1588 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1589 | EXTENT_DEFRAG, 0, NULL)) | |
1590 | return 1; | |
1591 | ||
1592 | return 0; | |
1593 | } | |
1594 | ||
d352ac68 | 1595 | /* |
5eaad97a NB |
1596 | * Function to process delayed allocation (create CoW) for ranges which are |
1597 | * being touched for the first time. | |
d352ac68 | 1598 | */ |
bc9a8bf7 | 1599 | int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page, |
5eaad97a NB |
1600 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
1601 | struct writeback_control *wbc) | |
be20aa9d | 1602 | { |
be20aa9d | 1603 | int ret; |
47059d93 | 1604 | int force_cow = need_force_cow(inode, start, end); |
f82b7359 | 1605 | unsigned int write_flags = wbc_to_write_flags(wbc); |
a2135011 | 1606 | |
47059d93 | 1607 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1608 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1609 | page_started, 1, nr_written); |
47059d93 | 1610 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1611 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1612 | page_started, 0, nr_written); |
c2fcdcdf | 1613 | } else if (!inode_need_compress(inode, start, end)) { |
dda3245e WX |
1614 | ret = cow_file_range(inode, locked_page, start, end, end, |
1615 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1616 | } else { |
1617 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1618 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1619 | ret = cow_file_range_async(inode, locked_page, start, end, |
f82b7359 LB |
1620 | page_started, nr_written, |
1621 | write_flags); | |
7ddf5a42 | 1622 | } |
52427260 | 1623 | if (ret) |
d1051d6e NB |
1624 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
1625 | end - start + 1); | |
b888db2b CM |
1626 | return ret; |
1627 | } | |
1628 | ||
abbb55f4 NB |
1629 | void btrfs_split_delalloc_extent(struct inode *inode, |
1630 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1631 | { |
dcab6a3b JB |
1632 | u64 size; |
1633 | ||
0ca1f7ce | 1634 | /* not delalloc, ignore it */ |
9ed74f2d | 1635 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1636 | return; |
9ed74f2d | 1637 | |
dcab6a3b JB |
1638 | size = orig->end - orig->start + 1; |
1639 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1640 | u32 num_extents; |
dcab6a3b JB |
1641 | u64 new_size; |
1642 | ||
1643 | /* | |
5c848198 | 1644 | * See the explanation in btrfs_merge_delalloc_extent, the same |
ba117213 | 1645 | * applies here, just in reverse. |
dcab6a3b JB |
1646 | */ |
1647 | new_size = orig->end - split + 1; | |
823bb20a | 1648 | num_extents = count_max_extents(new_size); |
ba117213 | 1649 | new_size = split - orig->start; |
823bb20a DS |
1650 | num_extents += count_max_extents(new_size); |
1651 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1652 | return; |
1653 | } | |
1654 | ||
9e0baf60 | 1655 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1656 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1657 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1658 | } |
1659 | ||
1660 | /* | |
5c848198 NB |
1661 | * Handle merged delayed allocation extents so we can keep track of new extents |
1662 | * that are just merged onto old extents, such as when we are doing sequential | |
1663 | * writes, so we can properly account for the metadata space we'll need. | |
9ed74f2d | 1664 | */ |
5c848198 NB |
1665 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
1666 | struct extent_state *other) | |
9ed74f2d | 1667 | { |
dcab6a3b | 1668 | u64 new_size, old_size; |
823bb20a | 1669 | u32 num_extents; |
dcab6a3b | 1670 | |
9ed74f2d JB |
1671 | /* not delalloc, ignore it */ |
1672 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1673 | return; |
9ed74f2d | 1674 | |
8461a3de JB |
1675 | if (new->start > other->start) |
1676 | new_size = new->end - other->start + 1; | |
1677 | else | |
1678 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1679 | |
1680 | /* we're not bigger than the max, unreserve the space and go */ | |
1681 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1682 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1683 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1684 | spin_unlock(&BTRFS_I(inode)->lock); |
1685 | return; | |
1686 | } | |
1687 | ||
1688 | /* | |
ba117213 JB |
1689 | * We have to add up either side to figure out how many extents were |
1690 | * accounted for before we merged into one big extent. If the number of | |
1691 | * extents we accounted for is <= the amount we need for the new range | |
1692 | * then we can return, otherwise drop. Think of it like this | |
1693 | * | |
1694 | * [ 4k][MAX_SIZE] | |
1695 | * | |
1696 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1697 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1698 | * we have 1 so they are == and we can return. But in this case | |
1699 | * | |
1700 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1701 | * | |
1702 | * Each range on their own accounts for 2 extents, but merged together | |
1703 | * they are only 3 extents worth of accounting, so we need to drop in | |
1704 | * this case. | |
dcab6a3b | 1705 | */ |
ba117213 | 1706 | old_size = other->end - other->start + 1; |
823bb20a | 1707 | num_extents = count_max_extents(old_size); |
ba117213 | 1708 | old_size = new->end - new->start + 1; |
823bb20a DS |
1709 | num_extents += count_max_extents(old_size); |
1710 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1711 | return; |
1712 | ||
9e0baf60 | 1713 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1714 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1715 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1716 | } |
1717 | ||
eb73c1b7 MX |
1718 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1719 | struct inode *inode) | |
1720 | { | |
0b246afa JM |
1721 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1722 | ||
eb73c1b7 MX |
1723 | spin_lock(&root->delalloc_lock); |
1724 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1725 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1726 | &root->delalloc_inodes); | |
1727 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1728 | &BTRFS_I(inode)->runtime_flags); | |
1729 | root->nr_delalloc_inodes++; | |
1730 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1731 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1732 | BUG_ON(!list_empty(&root->delalloc_root)); |
1733 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1734 | &fs_info->delalloc_roots); |
1735 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1736 | } |
1737 | } | |
1738 | spin_unlock(&root->delalloc_lock); | |
1739 | } | |
1740 | ||
2b877331 NB |
1741 | |
1742 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1743 | struct btrfs_inode *inode) | |
eb73c1b7 | 1744 | { |
3ffbd68c | 1745 | struct btrfs_fs_info *fs_info = root->fs_info; |
0b246afa | 1746 | |
9e3e97f4 NB |
1747 | if (!list_empty(&inode->delalloc_inodes)) { |
1748 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1749 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1750 | &inode->runtime_flags); |
eb73c1b7 MX |
1751 | root->nr_delalloc_inodes--; |
1752 | if (!root->nr_delalloc_inodes) { | |
7c8a0d36 | 1753 | ASSERT(list_empty(&root->delalloc_inodes)); |
0b246afa | 1754 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1755 | BUG_ON(list_empty(&root->delalloc_root)); |
1756 | list_del_init(&root->delalloc_root); | |
0b246afa | 1757 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1758 | } |
1759 | } | |
2b877331 NB |
1760 | } |
1761 | ||
1762 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1763 | struct btrfs_inode *inode) | |
1764 | { | |
1765 | spin_lock(&root->delalloc_lock); | |
1766 | __btrfs_del_delalloc_inode(root, inode); | |
eb73c1b7 MX |
1767 | spin_unlock(&root->delalloc_lock); |
1768 | } | |
1769 | ||
d352ac68 | 1770 | /* |
e06a1fc9 NB |
1771 | * Properly track delayed allocation bytes in the inode and to maintain the |
1772 | * list of inodes that have pending delalloc work to be done. | |
d352ac68 | 1773 | */ |
e06a1fc9 NB |
1774 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
1775 | unsigned *bits) | |
291d673e | 1776 | { |
0b246afa JM |
1777 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1778 | ||
47059d93 WS |
1779 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1780 | WARN_ON(1); | |
75eff68e CM |
1781 | /* |
1782 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1783 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1784 | * bit, which is only set or cleared with irqs on |
1785 | */ | |
0ca1f7ce | 1786 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1787 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1788 | u64 len = state->end + 1 - state->start; |
8b62f87b | 1789 | u32 num_extents = count_max_extents(len); |
70ddc553 | 1790 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1791 | |
8b62f87b JB |
1792 | spin_lock(&BTRFS_I(inode)->lock); |
1793 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
1794 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 1795 | |
6a3891c5 | 1796 | /* For sanity tests */ |
0b246afa | 1797 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1798 | return; |
1799 | ||
104b4e51 NB |
1800 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
1801 | fs_info->delalloc_batch); | |
df0af1a5 | 1802 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1803 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1804 | if (*bits & EXTENT_DEFRAG) |
1805 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1806 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1807 | &BTRFS_I(inode)->runtime_flags)) |
1808 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1809 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1810 | } |
a7e3b975 FM |
1811 | |
1812 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1813 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1814 | spin_lock(&BTRFS_I(inode)->lock); | |
1815 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1816 | state->start; | |
1817 | spin_unlock(&BTRFS_I(inode)->lock); | |
1818 | } | |
291d673e CM |
1819 | } |
1820 | ||
d352ac68 | 1821 | /* |
a36bb5f9 NB |
1822 | * Once a range is no longer delalloc this function ensures that proper |
1823 | * accounting happens. | |
d352ac68 | 1824 | */ |
a36bb5f9 NB |
1825 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
1826 | struct extent_state *state, unsigned *bits) | |
291d673e | 1827 | { |
a36bb5f9 NB |
1828 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
1829 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); | |
47059d93 | 1830 | u64 len = state->end + 1 - state->start; |
823bb20a | 1831 | u32 num_extents = count_max_extents(len); |
47059d93 | 1832 | |
4a4b964f FM |
1833 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
1834 | spin_lock(&inode->lock); | |
6fc0ef68 | 1835 | inode->defrag_bytes -= len; |
4a4b964f FM |
1836 | spin_unlock(&inode->lock); |
1837 | } | |
47059d93 | 1838 | |
75eff68e CM |
1839 | /* |
1840 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1841 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1842 | * bit, which is only set or cleared with irqs on |
1843 | */ | |
0ca1f7ce | 1844 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1845 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1846 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1847 | |
8b62f87b JB |
1848 | spin_lock(&inode->lock); |
1849 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
1850 | spin_unlock(&inode->lock); | |
0ca1f7ce | 1851 | |
b6d08f06 JB |
1852 | /* |
1853 | * We don't reserve metadata space for space cache inodes so we | |
52042d8e | 1854 | * don't need to call delalloc_release_metadata if there is an |
b6d08f06 JB |
1855 | * error. |
1856 | */ | |
a315e68f | 1857 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1858 | root != fs_info->tree_root) |
43b18595 | 1859 | btrfs_delalloc_release_metadata(inode, len, false); |
0ca1f7ce | 1860 | |
6a3891c5 | 1861 | /* For sanity tests. */ |
0b246afa | 1862 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1863 | return; |
1864 | ||
a315e68f FM |
1865 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
1866 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1867 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
1868 | btrfs_free_reserved_data_space_noquota( |
1869 | &inode->vfs_inode, | |
51773bec | 1870 | state->start, len); |
9ed74f2d | 1871 | |
104b4e51 NB |
1872 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
1873 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
1874 | spin_lock(&inode->lock); |
1875 | inode->delalloc_bytes -= len; | |
1876 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 1877 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1878 | &inode->runtime_flags)) |
eb73c1b7 | 1879 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 1880 | spin_unlock(&inode->lock); |
291d673e | 1881 | } |
a7e3b975 FM |
1882 | |
1883 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1884 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1885 | spin_lock(&inode->lock); | |
1886 | ASSERT(inode->new_delalloc_bytes >= len); | |
1887 | inode->new_delalloc_bytes -= len; | |
1888 | spin_unlock(&inode->lock); | |
1889 | } | |
291d673e CM |
1890 | } |
1891 | ||
d352ac68 | 1892 | /* |
da12fe54 NB |
1893 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
1894 | * in a chunk's stripe. This function ensures that bios do not span a | |
1895 | * stripe/chunk | |
6f034ece | 1896 | * |
da12fe54 NB |
1897 | * @page - The page we are about to add to the bio |
1898 | * @size - size we want to add to the bio | |
1899 | * @bio - bio we want to ensure is smaller than a stripe | |
1900 | * @bio_flags - flags of the bio | |
1901 | * | |
1902 | * return 1 if page cannot be added to the bio | |
1903 | * return 0 if page can be added to the bio | |
6f034ece | 1904 | * return error otherwise |
d352ac68 | 1905 | */ |
da12fe54 NB |
1906 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
1907 | unsigned long bio_flags) | |
239b14b3 | 1908 | { |
0b246afa JM |
1909 | struct inode *inode = page->mapping->host; |
1910 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1911 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1912 | u64 length = 0; |
1913 | u64 map_length; | |
239b14b3 CM |
1914 | int ret; |
1915 | ||
771ed689 CM |
1916 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1917 | return 0; | |
1918 | ||
4f024f37 | 1919 | length = bio->bi_iter.bi_size; |
239b14b3 | 1920 | map_length = length; |
0b246afa JM |
1921 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1922 | NULL, 0); | |
6f034ece LB |
1923 | if (ret < 0) |
1924 | return ret; | |
d397712b | 1925 | if (map_length < length + size) |
239b14b3 | 1926 | return 1; |
3444a972 | 1927 | return 0; |
239b14b3 CM |
1928 | } |
1929 | ||
d352ac68 CM |
1930 | /* |
1931 | * in order to insert checksums into the metadata in large chunks, | |
1932 | * we wait until bio submission time. All the pages in the bio are | |
1933 | * checksummed and sums are attached onto the ordered extent record. | |
1934 | * | |
1935 | * At IO completion time the cums attached on the ordered extent record | |
1936 | * are inserted into the btree | |
1937 | */ | |
d0ee3934 | 1938 | static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, |
eaf25d93 | 1939 | u64 bio_offset) |
065631f6 | 1940 | { |
c6100a4b | 1941 | struct inode *inode = private_data; |
4e4cbee9 | 1942 | blk_status_t ret = 0; |
e015640f | 1943 | |
2ff7e61e | 1944 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1945 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1946 | return 0; |
1947 | } | |
e015640f | 1948 | |
d352ac68 | 1949 | /* |
cad321ad | 1950 | * extent_io.c submission hook. This does the right thing for csum calculation |
4c274bc6 LB |
1951 | * on write, or reading the csums from the tree before a read. |
1952 | * | |
1953 | * Rules about async/sync submit, | |
1954 | * a) read: sync submit | |
1955 | * | |
1956 | * b) write without checksum: sync submit | |
1957 | * | |
1958 | * c) write with checksum: | |
1959 | * c-1) if bio is issued by fsync: sync submit | |
1960 | * (sync_writers != 0) | |
1961 | * | |
1962 | * c-2) if root is reloc root: sync submit | |
1963 | * (only in case of buffered IO) | |
1964 | * | |
1965 | * c-3) otherwise: async submit | |
d352ac68 | 1966 | */ |
8c27cb35 | 1967 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
c6100a4b JB |
1968 | int mirror_num, unsigned long bio_flags, |
1969 | u64 bio_offset) | |
44b8bd7e | 1970 | { |
c6100a4b | 1971 | struct inode *inode = private_data; |
0b246afa | 1972 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1973 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1974 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 1975 | blk_status_t ret = 0; |
19b9bdb0 | 1976 | int skip_sum; |
b812ce28 | 1977 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1978 | |
6cbff00f | 1979 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1980 | |
70ddc553 | 1981 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 1982 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1983 | |
37226b21 | 1984 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1985 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1986 | if (ret) |
61891923 | 1987 | goto out; |
5fd02043 | 1988 | |
d20f7043 | 1989 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1990 | ret = btrfs_submit_compressed_read(inode, bio, |
1991 | mirror_num, | |
1992 | bio_flags); | |
1993 | goto out; | |
c2db1073 | 1994 | } else if (!skip_sum) { |
2ff7e61e | 1995 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1996 | if (ret) |
61891923 | 1997 | goto out; |
c2db1073 | 1998 | } |
4d1b5fb4 | 1999 | goto mapit; |
b812ce28 | 2000 | } else if (async && !skip_sum) { |
17d217fe YZ |
2001 | /* csum items have already been cloned */ |
2002 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
2003 | goto mapit; | |
19b9bdb0 | 2004 | /* we're doing a write, do the async checksumming */ |
c6100a4b JB |
2005 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
2006 | bio_offset, inode, | |
e288c080 | 2007 | btrfs_submit_bio_start); |
61891923 | 2008 | goto out; |
b812ce28 | 2009 | } else if (!skip_sum) { |
2ff7e61e | 2010 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
2011 | if (ret) |
2012 | goto out; | |
19b9bdb0 CM |
2013 | } |
2014 | ||
0b86a832 | 2015 | mapit: |
2ff7e61e | 2016 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
2017 | |
2018 | out: | |
4e4cbee9 CH |
2019 | if (ret) { |
2020 | bio->bi_status = ret; | |
4246a0b6 CH |
2021 | bio_endio(bio); |
2022 | } | |
61891923 | 2023 | return ret; |
065631f6 | 2024 | } |
6885f308 | 2025 | |
d352ac68 CM |
2026 | /* |
2027 | * given a list of ordered sums record them in the inode. This happens | |
2028 | * at IO completion time based on sums calculated at bio submission time. | |
2029 | */ | |
ba1da2f4 | 2030 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2031 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2032 | { |
e6dcd2dc | 2033 | struct btrfs_ordered_sum *sum; |
ac01f26a | 2034 | int ret; |
e6dcd2dc | 2035 | |
c6e30871 | 2036 | list_for_each_entry(sum, list, list) { |
7c2871a2 | 2037 | trans->adding_csums = true; |
ac01f26a | 2038 | ret = btrfs_csum_file_blocks(trans, |
d20f7043 | 2039 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
7c2871a2 | 2040 | trans->adding_csums = false; |
ac01f26a NB |
2041 | if (ret) |
2042 | return ret; | |
e6dcd2dc CM |
2043 | } |
2044 | return 0; | |
2045 | } | |
2046 | ||
2ac55d41 | 2047 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
e3b8a485 | 2048 | unsigned int extra_bits, |
ba8b04c1 | 2049 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2050 | { |
fdb1e121 | 2051 | WARN_ON(PAGE_ALIGNED(end)); |
ea8c2819 | 2052 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
e3b8a485 | 2053 | extra_bits, cached_state); |
ea8c2819 CM |
2054 | } |
2055 | ||
d352ac68 | 2056 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2057 | struct btrfs_writepage_fixup { |
2058 | struct page *page; | |
2059 | struct btrfs_work work; | |
2060 | }; | |
2061 | ||
b2950863 | 2062 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2063 | { |
2064 | struct btrfs_writepage_fixup *fixup; | |
2065 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2066 | struct extent_state *cached_state = NULL; |
364ecf36 | 2067 | struct extent_changeset *data_reserved = NULL; |
247e743c CM |
2068 | struct page *page; |
2069 | struct inode *inode; | |
2070 | u64 page_start; | |
2071 | u64 page_end; | |
87826df0 | 2072 | int ret; |
247e743c CM |
2073 | |
2074 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2075 | page = fixup->page; | |
4a096752 | 2076 | again: |
247e743c CM |
2077 | lock_page(page); |
2078 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2079 | ClearPageChecked(page); | |
2080 | goto out_page; | |
2081 | } | |
2082 | ||
2083 | inode = page->mapping->host; | |
2084 | page_start = page_offset(page); | |
09cbfeaf | 2085 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2086 | |
ff13db41 | 2087 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2088 | &cached_state); |
4a096752 CM |
2089 | |
2090 | /* already ordered? We're done */ | |
8b62b72b | 2091 | if (PagePrivate2(page)) |
247e743c | 2092 | goto out; |
4a096752 | 2093 | |
a776c6fa | 2094 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2095 | PAGE_SIZE); |
4a096752 | 2096 | if (ordered) { |
2ac55d41 | 2097 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
e43bbe5e | 2098 | page_end, &cached_state); |
4a096752 CM |
2099 | unlock_page(page); |
2100 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2101 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2102 | goto again; |
2103 | } | |
247e743c | 2104 | |
364ecf36 | 2105 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
09cbfeaf | 2106 | PAGE_SIZE); |
87826df0 JM |
2107 | if (ret) { |
2108 | mapping_set_error(page->mapping, ret); | |
2109 | end_extent_writepage(page, ret, page_start, page_end); | |
2110 | ClearPageChecked(page); | |
2111 | goto out; | |
2112 | } | |
2113 | ||
f3038ee3 NB |
2114 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
2115 | &cached_state, 0); | |
2116 | if (ret) { | |
2117 | mapping_set_error(page->mapping, ret); | |
2118 | end_extent_writepage(page, ret, page_start, page_end); | |
2119 | ClearPageChecked(page); | |
2120 | goto out; | |
2121 | } | |
2122 | ||
247e743c | 2123 | ClearPageChecked(page); |
87826df0 | 2124 | set_page_dirty(page); |
43b18595 | 2125 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false); |
247e743c | 2126 | out: |
2ac55d41 | 2127 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
e43bbe5e | 2128 | &cached_state); |
247e743c CM |
2129 | out_page: |
2130 | unlock_page(page); | |
09cbfeaf | 2131 | put_page(page); |
b897abec | 2132 | kfree(fixup); |
364ecf36 | 2133 | extent_changeset_free(data_reserved); |
247e743c CM |
2134 | } |
2135 | ||
2136 | /* | |
2137 | * There are a few paths in the higher layers of the kernel that directly | |
2138 | * set the page dirty bit without asking the filesystem if it is a | |
2139 | * good idea. This causes problems because we want to make sure COW | |
2140 | * properly happens and the data=ordered rules are followed. | |
2141 | * | |
c8b97818 | 2142 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2143 | * hasn't been properly setup for IO. We kick off an async process |
2144 | * to fix it up. The async helper will wait for ordered extents, set | |
2145 | * the delalloc bit and make it safe to write the page. | |
2146 | */ | |
d75855b4 | 2147 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
247e743c CM |
2148 | { |
2149 | struct inode *inode = page->mapping->host; | |
0b246afa | 2150 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2151 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2152 | |
8b62b72b CM |
2153 | /* this page is properly in the ordered list */ |
2154 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2155 | return 0; |
2156 | ||
2157 | if (PageChecked(page)) | |
2158 | return -EAGAIN; | |
2159 | ||
2160 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2161 | if (!fixup) | |
2162 | return -EAGAIN; | |
f421950f | 2163 | |
247e743c | 2164 | SetPageChecked(page); |
09cbfeaf | 2165 | get_page(page); |
9e0af237 LB |
2166 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2167 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2168 | fixup->page = page; |
0b246afa | 2169 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2170 | return -EBUSY; |
247e743c CM |
2171 | } |
2172 | ||
d899e052 YZ |
2173 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2174 | struct inode *inode, u64 file_pos, | |
2175 | u64 disk_bytenr, u64 disk_num_bytes, | |
2176 | u64 num_bytes, u64 ram_bytes, | |
2177 | u8 compression, u8 encryption, | |
2178 | u16 other_encoding, int extent_type) | |
2179 | { | |
2180 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2181 | struct btrfs_file_extent_item *fi; | |
2182 | struct btrfs_path *path; | |
2183 | struct extent_buffer *leaf; | |
2184 | struct btrfs_key ins; | |
a12b877b | 2185 | u64 qg_released; |
1acae57b | 2186 | int extent_inserted = 0; |
d899e052 YZ |
2187 | int ret; |
2188 | ||
2189 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2190 | if (!path) |
2191 | return -ENOMEM; | |
d899e052 | 2192 | |
a1ed835e CM |
2193 | /* |
2194 | * we may be replacing one extent in the tree with another. | |
2195 | * The new extent is pinned in the extent map, and we don't want | |
2196 | * to drop it from the cache until it is completely in the btree. | |
2197 | * | |
2198 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2199 | * the caller is expected to unpin it and allow it to be merged | |
2200 | * with the others. | |
2201 | */ | |
1acae57b FDBM |
2202 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2203 | file_pos + num_bytes, NULL, 0, | |
2204 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2205 | if (ret) |
2206 | goto out; | |
d899e052 | 2207 | |
1acae57b | 2208 | if (!extent_inserted) { |
4a0cc7ca | 2209 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2210 | ins.offset = file_pos; |
2211 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2212 | ||
2213 | path->leave_spinning = 1; | |
2214 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2215 | sizeof(*fi)); | |
2216 | if (ret) | |
2217 | goto out; | |
2218 | } | |
d899e052 YZ |
2219 | leaf = path->nodes[0]; |
2220 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2221 | struct btrfs_file_extent_item); | |
2222 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2223 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2224 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2225 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2226 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2227 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2228 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2229 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2230 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2231 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2232 | |
d899e052 | 2233 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2234 | btrfs_release_path(path); |
d899e052 YZ |
2235 | |
2236 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2237 | |
2238 | ins.objectid = disk_bytenr; | |
2239 | ins.offset = disk_num_bytes; | |
2240 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2241 | |
297d750b | 2242 | /* |
5846a3c2 QW |
2243 | * Release the reserved range from inode dirty range map, as it is |
2244 | * already moved into delayed_ref_head | |
297d750b | 2245 | */ |
a12b877b QW |
2246 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
2247 | if (ret < 0) | |
2248 | goto out; | |
2249 | qg_released = ret; | |
84f7d8e6 JB |
2250 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2251 | btrfs_ino(BTRFS_I(inode)), | |
2252 | file_pos, qg_released, &ins); | |
79787eaa | 2253 | out: |
d899e052 | 2254 | btrfs_free_path(path); |
b9473439 | 2255 | |
79787eaa | 2256 | return ret; |
d899e052 YZ |
2257 | } |
2258 | ||
38c227d8 LB |
2259 | /* snapshot-aware defrag */ |
2260 | struct sa_defrag_extent_backref { | |
2261 | struct rb_node node; | |
2262 | struct old_sa_defrag_extent *old; | |
2263 | u64 root_id; | |
2264 | u64 inum; | |
2265 | u64 file_pos; | |
2266 | u64 extent_offset; | |
2267 | u64 num_bytes; | |
2268 | u64 generation; | |
2269 | }; | |
2270 | ||
2271 | struct old_sa_defrag_extent { | |
2272 | struct list_head list; | |
2273 | struct new_sa_defrag_extent *new; | |
2274 | ||
2275 | u64 extent_offset; | |
2276 | u64 bytenr; | |
2277 | u64 offset; | |
2278 | u64 len; | |
2279 | int count; | |
2280 | }; | |
2281 | ||
2282 | struct new_sa_defrag_extent { | |
2283 | struct rb_root root; | |
2284 | struct list_head head; | |
2285 | struct btrfs_path *path; | |
2286 | struct inode *inode; | |
2287 | u64 file_pos; | |
2288 | u64 len; | |
2289 | u64 bytenr; | |
2290 | u64 disk_len; | |
2291 | u8 compress_type; | |
2292 | }; | |
2293 | ||
2294 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2295 | struct sa_defrag_extent_backref *b2) | |
2296 | { | |
2297 | if (b1->root_id < b2->root_id) | |
2298 | return -1; | |
2299 | else if (b1->root_id > b2->root_id) | |
2300 | return 1; | |
2301 | ||
2302 | if (b1->inum < b2->inum) | |
2303 | return -1; | |
2304 | else if (b1->inum > b2->inum) | |
2305 | return 1; | |
2306 | ||
2307 | if (b1->file_pos < b2->file_pos) | |
2308 | return -1; | |
2309 | else if (b1->file_pos > b2->file_pos) | |
2310 | return 1; | |
2311 | ||
2312 | /* | |
2313 | * [------------------------------] ===> (a range of space) | |
2314 | * |<--->| |<---->| =============> (fs/file tree A) | |
2315 | * |<---------------------------->| ===> (fs/file tree B) | |
2316 | * | |
2317 | * A range of space can refer to two file extents in one tree while | |
2318 | * refer to only one file extent in another tree. | |
2319 | * | |
2320 | * So we may process a disk offset more than one time(two extents in A) | |
2321 | * and locate at the same extent(one extent in B), then insert two same | |
2322 | * backrefs(both refer to the extent in B). | |
2323 | */ | |
2324 | return 0; | |
2325 | } | |
2326 | ||
2327 | static void backref_insert(struct rb_root *root, | |
2328 | struct sa_defrag_extent_backref *backref) | |
2329 | { | |
2330 | struct rb_node **p = &root->rb_node; | |
2331 | struct rb_node *parent = NULL; | |
2332 | struct sa_defrag_extent_backref *entry; | |
2333 | int ret; | |
2334 | ||
2335 | while (*p) { | |
2336 | parent = *p; | |
2337 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2338 | ||
2339 | ret = backref_comp(backref, entry); | |
2340 | if (ret < 0) | |
2341 | p = &(*p)->rb_left; | |
2342 | else | |
2343 | p = &(*p)->rb_right; | |
2344 | } | |
2345 | ||
2346 | rb_link_node(&backref->node, parent, p); | |
2347 | rb_insert_color(&backref->node, root); | |
2348 | } | |
2349 | ||
2350 | /* | |
2351 | * Note the backref might has changed, and in this case we just return 0. | |
2352 | */ | |
2353 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2354 | void *ctx) | |
2355 | { | |
2356 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2357 | struct old_sa_defrag_extent *old = ctx; |
2358 | struct new_sa_defrag_extent *new = old->new; | |
2359 | struct btrfs_path *path = new->path; | |
2360 | struct btrfs_key key; | |
2361 | struct btrfs_root *root; | |
2362 | struct sa_defrag_extent_backref *backref; | |
2363 | struct extent_buffer *leaf; | |
2364 | struct inode *inode = new->inode; | |
0b246afa | 2365 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2366 | int slot; |
2367 | int ret; | |
2368 | u64 extent_offset; | |
2369 | u64 num_bytes; | |
2370 | ||
2371 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2372 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2373 | return 0; |
2374 | ||
2375 | key.objectid = root_id; | |
2376 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2377 | key.offset = (u64)-1; | |
2378 | ||
38c227d8 LB |
2379 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2380 | if (IS_ERR(root)) { | |
2381 | if (PTR_ERR(root) == -ENOENT) | |
2382 | return 0; | |
2383 | WARN_ON(1); | |
ab8d0fc4 | 2384 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2385 | inum, offset, root_id); |
2386 | return PTR_ERR(root); | |
2387 | } | |
2388 | ||
2389 | key.objectid = inum; | |
2390 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2391 | if (offset > (u64)-1 << 32) | |
2392 | key.offset = 0; | |
2393 | else | |
2394 | key.offset = offset; | |
2395 | ||
2396 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2397 | if (WARN_ON(ret < 0)) |
38c227d8 | 2398 | return ret; |
50f1319c | 2399 | ret = 0; |
38c227d8 LB |
2400 | |
2401 | while (1) { | |
2402 | cond_resched(); | |
2403 | ||
2404 | leaf = path->nodes[0]; | |
2405 | slot = path->slots[0]; | |
2406 | ||
2407 | if (slot >= btrfs_header_nritems(leaf)) { | |
2408 | ret = btrfs_next_leaf(root, path); | |
2409 | if (ret < 0) { | |
2410 | goto out; | |
2411 | } else if (ret > 0) { | |
2412 | ret = 0; | |
2413 | goto out; | |
2414 | } | |
2415 | continue; | |
2416 | } | |
2417 | ||
2418 | path->slots[0]++; | |
2419 | ||
2420 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2421 | ||
2422 | if (key.objectid > inum) | |
2423 | goto out; | |
2424 | ||
2425 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2426 | continue; | |
2427 | ||
2428 | extent = btrfs_item_ptr(leaf, slot, | |
2429 | struct btrfs_file_extent_item); | |
2430 | ||
2431 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2432 | continue; | |
2433 | ||
e68afa49 LB |
2434 | /* |
2435 | * 'offset' refers to the exact key.offset, | |
2436 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2437 | * (key.offset - extent_offset). | |
2438 | */ | |
2439 | if (key.offset != offset) | |
38c227d8 LB |
2440 | continue; |
2441 | ||
e68afa49 | 2442 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2443 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2444 | |
38c227d8 LB |
2445 | if (extent_offset >= old->extent_offset + old->offset + |
2446 | old->len || extent_offset + num_bytes <= | |
2447 | old->extent_offset + old->offset) | |
2448 | continue; | |
38c227d8 LB |
2449 | break; |
2450 | } | |
2451 | ||
2452 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2453 | if (!backref) { | |
2454 | ret = -ENOENT; | |
2455 | goto out; | |
2456 | } | |
2457 | ||
2458 | backref->root_id = root_id; | |
2459 | backref->inum = inum; | |
e68afa49 | 2460 | backref->file_pos = offset; |
38c227d8 LB |
2461 | backref->num_bytes = num_bytes; |
2462 | backref->extent_offset = extent_offset; | |
2463 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2464 | backref->old = old; | |
2465 | backref_insert(&new->root, backref); | |
2466 | old->count++; | |
2467 | out: | |
2468 | btrfs_release_path(path); | |
2469 | WARN_ON(ret); | |
2470 | return ret; | |
2471 | } | |
2472 | ||
2473 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2474 | struct new_sa_defrag_extent *new) | |
2475 | { | |
0b246afa | 2476 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2477 | struct old_sa_defrag_extent *old, *tmp; |
2478 | int ret; | |
2479 | ||
2480 | new->path = path; | |
2481 | ||
2482 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2483 | ret = iterate_inodes_from_logical(old->bytenr + |
2484 | old->extent_offset, fs_info, | |
38c227d8 | 2485 | path, record_one_backref, |
c995ab3c | 2486 | old, false); |
4724b106 JB |
2487 | if (ret < 0 && ret != -ENOENT) |
2488 | return false; | |
38c227d8 LB |
2489 | |
2490 | /* no backref to be processed for this extent */ | |
2491 | if (!old->count) { | |
2492 | list_del(&old->list); | |
2493 | kfree(old); | |
2494 | } | |
2495 | } | |
2496 | ||
2497 | if (list_empty(&new->head)) | |
2498 | return false; | |
2499 | ||
2500 | return true; | |
2501 | } | |
2502 | ||
2503 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2504 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2505 | struct new_sa_defrag_extent *new) |
38c227d8 | 2506 | { |
116e0024 | 2507 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2508 | return 0; |
2509 | ||
2510 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2511 | return 0; | |
2512 | ||
116e0024 LB |
2513 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2514 | return 0; | |
2515 | ||
2516 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2517 | btrfs_file_extent_other_encoding(leaf, fi)) |
2518 | return 0; | |
2519 | ||
2520 | return 1; | |
2521 | } | |
2522 | ||
2523 | /* | |
2524 | * Note the backref might has changed, and in this case we just return 0. | |
2525 | */ | |
2526 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2527 | struct sa_defrag_extent_backref *prev, | |
2528 | struct sa_defrag_extent_backref *backref) | |
2529 | { | |
2530 | struct btrfs_file_extent_item *extent; | |
2531 | struct btrfs_file_extent_item *item; | |
2532 | struct btrfs_ordered_extent *ordered; | |
2533 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2534 | struct btrfs_root *root; |
2535 | struct btrfs_key key; | |
2536 | struct extent_buffer *leaf; | |
2537 | struct old_sa_defrag_extent *old = backref->old; | |
2538 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2539 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2540 | struct inode *inode; |
2541 | struct extent_state *cached = NULL; | |
2542 | int ret = 0; | |
2543 | u64 start; | |
2544 | u64 len; | |
2545 | u64 lock_start; | |
2546 | u64 lock_end; | |
2547 | bool merge = false; | |
2548 | int index; | |
2549 | ||
2550 | if (prev && prev->root_id == backref->root_id && | |
2551 | prev->inum == backref->inum && | |
2552 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2553 | merge = true; | |
2554 | ||
2555 | /* step 1: get root */ | |
2556 | key.objectid = backref->root_id; | |
2557 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2558 | key.offset = (u64)-1; | |
2559 | ||
38c227d8 LB |
2560 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2561 | ||
2562 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2563 | if (IS_ERR(root)) { | |
2564 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2565 | if (PTR_ERR(root) == -ENOENT) | |
2566 | return 0; | |
2567 | return PTR_ERR(root); | |
2568 | } | |
38c227d8 | 2569 | |
bcbba5e6 WS |
2570 | if (btrfs_root_readonly(root)) { |
2571 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2572 | return 0; | |
2573 | } | |
2574 | ||
38c227d8 LB |
2575 | /* step 2: get inode */ |
2576 | key.objectid = backref->inum; | |
2577 | key.type = BTRFS_INODE_ITEM_KEY; | |
2578 | key.offset = 0; | |
2579 | ||
2580 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2581 | if (IS_ERR(inode)) { | |
2582 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2583 | return 0; | |
2584 | } | |
2585 | ||
2586 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2587 | ||
2588 | /* step 3: relink backref */ | |
2589 | lock_start = backref->file_pos; | |
2590 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2591 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2592 | &cached); |
38c227d8 LB |
2593 | |
2594 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2595 | if (ordered) { | |
2596 | btrfs_put_ordered_extent(ordered); | |
2597 | goto out_unlock; | |
2598 | } | |
2599 | ||
2600 | trans = btrfs_join_transaction(root); | |
2601 | if (IS_ERR(trans)) { | |
2602 | ret = PTR_ERR(trans); | |
2603 | goto out_unlock; | |
2604 | } | |
2605 | ||
2606 | key.objectid = backref->inum; | |
2607 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2608 | key.offset = backref->file_pos; | |
2609 | ||
2610 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2611 | if (ret < 0) { | |
2612 | goto out_free_path; | |
2613 | } else if (ret > 0) { | |
2614 | ret = 0; | |
2615 | goto out_free_path; | |
2616 | } | |
2617 | ||
2618 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2619 | struct btrfs_file_extent_item); | |
2620 | ||
2621 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2622 | backref->generation) | |
2623 | goto out_free_path; | |
2624 | ||
2625 | btrfs_release_path(path); | |
2626 | ||
2627 | start = backref->file_pos; | |
2628 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2629 | start += old->extent_offset + old->offset - | |
2630 | backref->extent_offset; | |
2631 | ||
2632 | len = min(backref->extent_offset + backref->num_bytes, | |
2633 | old->extent_offset + old->offset + old->len); | |
2634 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2635 | ||
2636 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2637 | start + len, 1); | |
2638 | if (ret) | |
2639 | goto out_free_path; | |
2640 | again: | |
4a0cc7ca | 2641 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2642 | key.type = BTRFS_EXTENT_DATA_KEY; |
2643 | key.offset = start; | |
2644 | ||
a09a0a70 | 2645 | path->leave_spinning = 1; |
38c227d8 LB |
2646 | if (merge) { |
2647 | struct btrfs_file_extent_item *fi; | |
2648 | u64 extent_len; | |
2649 | struct btrfs_key found_key; | |
2650 | ||
3c9665df | 2651 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2652 | if (ret < 0) |
2653 | goto out_free_path; | |
2654 | ||
2655 | path->slots[0]--; | |
2656 | leaf = path->nodes[0]; | |
2657 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2658 | ||
2659 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2660 | struct btrfs_file_extent_item); | |
2661 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2662 | ||
116e0024 LB |
2663 | if (extent_len + found_key.offset == start && |
2664 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2665 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2666 | extent_len + len); | |
2667 | btrfs_mark_buffer_dirty(leaf); | |
2668 | inode_add_bytes(inode, len); | |
2669 | ||
2670 | ret = 1; | |
2671 | goto out_free_path; | |
2672 | } else { | |
2673 | merge = false; | |
2674 | btrfs_release_path(path); | |
2675 | goto again; | |
2676 | } | |
2677 | } | |
2678 | ||
2679 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2680 | sizeof(*extent)); | |
2681 | if (ret) { | |
66642832 | 2682 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2683 | goto out_free_path; |
2684 | } | |
2685 | ||
2686 | leaf = path->nodes[0]; | |
2687 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2688 | struct btrfs_file_extent_item); | |
2689 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2690 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2691 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2692 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2693 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2694 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2695 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2696 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2697 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2698 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2699 | ||
2700 | btrfs_mark_buffer_dirty(leaf); | |
2701 | inode_add_bytes(inode, len); | |
a09a0a70 | 2702 | btrfs_release_path(path); |
38c227d8 | 2703 | |
84f7d8e6 | 2704 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
38c227d8 LB |
2705 | new->disk_len, 0, |
2706 | backref->root_id, backref->inum, | |
b06c4bf5 | 2707 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2708 | if (ret) { |
66642832 | 2709 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2710 | goto out_free_path; |
2711 | } | |
2712 | ||
2713 | ret = 1; | |
2714 | out_free_path: | |
2715 | btrfs_release_path(path); | |
a09a0a70 | 2716 | path->leave_spinning = 0; |
3a45bb20 | 2717 | btrfs_end_transaction(trans); |
38c227d8 LB |
2718 | out_unlock: |
2719 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
e43bbe5e | 2720 | &cached); |
38c227d8 LB |
2721 | iput(inode); |
2722 | return ret; | |
2723 | } | |
2724 | ||
6f519564 LB |
2725 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2726 | { | |
2727 | struct old_sa_defrag_extent *old, *tmp; | |
2728 | ||
2729 | if (!new) | |
2730 | return; | |
2731 | ||
2732 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2733 | kfree(old); |
2734 | } | |
2735 | kfree(new); | |
2736 | } | |
2737 | ||
38c227d8 LB |
2738 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2739 | { | |
0b246afa | 2740 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2741 | struct btrfs_path *path; |
38c227d8 LB |
2742 | struct sa_defrag_extent_backref *backref; |
2743 | struct sa_defrag_extent_backref *prev = NULL; | |
38c227d8 LB |
2744 | struct rb_node *node; |
2745 | int ret; | |
2746 | ||
38c227d8 LB |
2747 | path = btrfs_alloc_path(); |
2748 | if (!path) | |
2749 | return; | |
2750 | ||
2751 | if (!record_extent_backrefs(path, new)) { | |
2752 | btrfs_free_path(path); | |
2753 | goto out; | |
2754 | } | |
2755 | btrfs_release_path(path); | |
2756 | ||
2757 | while (1) { | |
2758 | node = rb_first(&new->root); | |
2759 | if (!node) | |
2760 | break; | |
2761 | rb_erase(node, &new->root); | |
2762 | ||
2763 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2764 | ||
2765 | ret = relink_extent_backref(path, prev, backref); | |
2766 | WARN_ON(ret < 0); | |
2767 | ||
2768 | kfree(prev); | |
2769 | ||
2770 | if (ret == 1) | |
2771 | prev = backref; | |
2772 | else | |
2773 | prev = NULL; | |
2774 | cond_resched(); | |
2775 | } | |
2776 | kfree(prev); | |
2777 | ||
2778 | btrfs_free_path(path); | |
38c227d8 | 2779 | out: |
6f519564 LB |
2780 | free_sa_defrag_extent(new); |
2781 | ||
0b246afa JM |
2782 | atomic_dec(&fs_info->defrag_running); |
2783 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2784 | } |
2785 | ||
2786 | static struct new_sa_defrag_extent * | |
2787 | record_old_file_extents(struct inode *inode, | |
2788 | struct btrfs_ordered_extent *ordered) | |
2789 | { | |
0b246afa | 2790 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2791 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2792 | struct btrfs_path *path; | |
2793 | struct btrfs_key key; | |
6f519564 | 2794 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2795 | struct new_sa_defrag_extent *new; |
2796 | int ret; | |
2797 | ||
2798 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2799 | if (!new) | |
2800 | return NULL; | |
2801 | ||
2802 | new->inode = inode; | |
2803 | new->file_pos = ordered->file_offset; | |
2804 | new->len = ordered->len; | |
2805 | new->bytenr = ordered->start; | |
2806 | new->disk_len = ordered->disk_len; | |
2807 | new->compress_type = ordered->compress_type; | |
2808 | new->root = RB_ROOT; | |
2809 | INIT_LIST_HEAD(&new->head); | |
2810 | ||
2811 | path = btrfs_alloc_path(); | |
2812 | if (!path) | |
2813 | goto out_kfree; | |
2814 | ||
4a0cc7ca | 2815 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2816 | key.type = BTRFS_EXTENT_DATA_KEY; |
2817 | key.offset = new->file_pos; | |
2818 | ||
2819 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2820 | if (ret < 0) | |
2821 | goto out_free_path; | |
2822 | if (ret > 0 && path->slots[0] > 0) | |
2823 | path->slots[0]--; | |
2824 | ||
2825 | /* find out all the old extents for the file range */ | |
2826 | while (1) { | |
2827 | struct btrfs_file_extent_item *extent; | |
2828 | struct extent_buffer *l; | |
2829 | int slot; | |
2830 | u64 num_bytes; | |
2831 | u64 offset; | |
2832 | u64 end; | |
2833 | u64 disk_bytenr; | |
2834 | u64 extent_offset; | |
2835 | ||
2836 | l = path->nodes[0]; | |
2837 | slot = path->slots[0]; | |
2838 | ||
2839 | if (slot >= btrfs_header_nritems(l)) { | |
2840 | ret = btrfs_next_leaf(root, path); | |
2841 | if (ret < 0) | |
6f519564 | 2842 | goto out_free_path; |
38c227d8 LB |
2843 | else if (ret > 0) |
2844 | break; | |
2845 | continue; | |
2846 | } | |
2847 | ||
2848 | btrfs_item_key_to_cpu(l, &key, slot); | |
2849 | ||
4a0cc7ca | 2850 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2851 | break; |
2852 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2853 | break; | |
2854 | if (key.offset >= new->file_pos + new->len) | |
2855 | break; | |
2856 | ||
2857 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2858 | ||
2859 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2860 | if (key.offset + num_bytes < new->file_pos) | |
2861 | goto next; | |
2862 | ||
2863 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2864 | if (!disk_bytenr) | |
2865 | goto next; | |
2866 | ||
2867 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2868 | ||
2869 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2870 | if (!old) | |
6f519564 | 2871 | goto out_free_path; |
38c227d8 LB |
2872 | |
2873 | offset = max(new->file_pos, key.offset); | |
2874 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2875 | ||
2876 | old->bytenr = disk_bytenr; | |
2877 | old->extent_offset = extent_offset; | |
2878 | old->offset = offset - key.offset; | |
2879 | old->len = end - offset; | |
2880 | old->new = new; | |
2881 | old->count = 0; | |
2882 | list_add_tail(&old->list, &new->head); | |
2883 | next: | |
2884 | path->slots[0]++; | |
2885 | cond_resched(); | |
2886 | } | |
2887 | ||
2888 | btrfs_free_path(path); | |
0b246afa | 2889 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2890 | |
2891 | return new; | |
2892 | ||
38c227d8 LB |
2893 | out_free_path: |
2894 | btrfs_free_path(path); | |
2895 | out_kfree: | |
6f519564 | 2896 | free_sa_defrag_extent(new); |
38c227d8 LB |
2897 | return NULL; |
2898 | } | |
2899 | ||
2ff7e61e | 2900 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2901 | u64 start, u64 len) |
2902 | { | |
2903 | struct btrfs_block_group_cache *cache; | |
2904 | ||
0b246afa | 2905 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2906 | ASSERT(cache); |
2907 | ||
2908 | spin_lock(&cache->lock); | |
2909 | cache->delalloc_bytes -= len; | |
2910 | spin_unlock(&cache->lock); | |
2911 | ||
2912 | btrfs_put_block_group(cache); | |
2913 | } | |
2914 | ||
d352ac68 CM |
2915 | /* as ordered data IO finishes, this gets called so we can finish |
2916 | * an ordered extent if the range of bytes in the file it covers are | |
2917 | * fully written. | |
2918 | */ | |
5fd02043 | 2919 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2920 | { |
5fd02043 | 2921 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2922 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2924 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2925 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2926 | struct extent_state *cached_state = NULL; |
38c227d8 | 2927 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2928 | int compress_type = 0; |
77cef2ec JB |
2929 | int ret = 0; |
2930 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2931 | bool nolock; |
77cef2ec | 2932 | bool truncated = false; |
a7e3b975 FM |
2933 | bool range_locked = false; |
2934 | bool clear_new_delalloc_bytes = false; | |
49940bdd | 2935 | bool clear_reserved_extent = true; |
a7e3b975 FM |
2936 | |
2937 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2938 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2939 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2940 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2941 | |
70ddc553 | 2942 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2943 | |
5fd02043 JB |
2944 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2945 | ret = -EIO; | |
2946 | goto out; | |
2947 | } | |
2948 | ||
7ab7956e NB |
2949 | btrfs_free_io_failure_record(BTRFS_I(inode), |
2950 | ordered_extent->file_offset, | |
2951 | ordered_extent->file_offset + | |
2952 | ordered_extent->len - 1); | |
f612496b | 2953 | |
77cef2ec JB |
2954 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2955 | truncated = true; | |
2956 | logical_len = ordered_extent->truncated_len; | |
2957 | /* Truncated the entire extent, don't bother adding */ | |
2958 | if (!logical_len) | |
2959 | goto out; | |
2960 | } | |
2961 | ||
c2167754 | 2962 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2963 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2964 | |
2965 | /* | |
2966 | * For mwrite(mmap + memset to write) case, we still reserve | |
2967 | * space for NOCOW range. | |
2968 | * As NOCOW won't cause a new delayed ref, just free the space | |
2969 | */ | |
bc42bda2 | 2970 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
94ed938a | 2971 | ordered_extent->len); |
6c760c07 JB |
2972 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2973 | if (nolock) | |
2974 | trans = btrfs_join_transaction_nolock(root); | |
2975 | else | |
2976 | trans = btrfs_join_transaction(root); | |
2977 | if (IS_ERR(trans)) { | |
2978 | ret = PTR_ERR(trans); | |
2979 | trans = NULL; | |
2980 | goto out; | |
c2167754 | 2981 | } |
69fe2d75 | 2982 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
2983 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2984 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2985 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2986 | goto out; |
2987 | } | |
e6dcd2dc | 2988 | |
a7e3b975 | 2989 | range_locked = true; |
2ac55d41 JB |
2990 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2991 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2992 | &cached_state); |
e6dcd2dc | 2993 | |
38c227d8 LB |
2994 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2995 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 2996 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
2997 | if (ret) { |
2998 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2999 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
3000 | /* the inode is shared */ |
3001 | new = record_old_file_extents(inode, ordered_extent); | |
3002 | ||
3003 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
3004 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ae0f1625 | 3005 | EXTENT_DEFRAG, 0, 0, &cached_state); |
38c227d8 LB |
3006 | } |
3007 | ||
0cb59c99 | 3008 | if (nolock) |
7a7eaa40 | 3009 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 3010 | else |
7a7eaa40 | 3011 | trans = btrfs_join_transaction(root); |
79787eaa JM |
3012 | if (IS_ERR(trans)) { |
3013 | ret = PTR_ERR(trans); | |
3014 | trans = NULL; | |
a7e3b975 | 3015 | goto out; |
79787eaa | 3016 | } |
a79b7d4b | 3017 | |
69fe2d75 | 3018 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 3019 | |
c8b97818 | 3020 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 3021 | compress_type = ordered_extent->compress_type; |
d899e052 | 3022 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 3023 | BUG_ON(compress_type); |
b430b775 JM |
3024 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
3025 | ordered_extent->len); | |
7a6d7067 | 3026 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
3027 | ordered_extent->file_offset, |
3028 | ordered_extent->file_offset + | |
77cef2ec | 3029 | logical_len); |
d899e052 | 3030 | } else { |
0b246afa | 3031 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
3032 | ret = insert_reserved_file_extent(trans, inode, |
3033 | ordered_extent->file_offset, | |
3034 | ordered_extent->start, | |
3035 | ordered_extent->disk_len, | |
77cef2ec | 3036 | logical_len, logical_len, |
261507a0 | 3037 | compress_type, 0, 0, |
d899e052 | 3038 | BTRFS_FILE_EXTENT_REG); |
49940bdd JB |
3039 | if (!ret) { |
3040 | clear_reserved_extent = false; | |
2ff7e61e | 3041 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3042 | ordered_extent->start, |
3043 | ordered_extent->disk_len); | |
49940bdd | 3044 | } |
d899e052 | 3045 | } |
5dc562c5 JB |
3046 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3047 | ordered_extent->file_offset, ordered_extent->len, | |
3048 | trans->transid); | |
79787eaa | 3049 | if (ret < 0) { |
66642832 | 3050 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3051 | goto out; |
79787eaa | 3052 | } |
2ac55d41 | 3053 | |
ac01f26a NB |
3054 | ret = add_pending_csums(trans, inode, &ordered_extent->list); |
3055 | if (ret) { | |
3056 | btrfs_abort_transaction(trans, ret); | |
3057 | goto out; | |
3058 | } | |
e6dcd2dc | 3059 | |
6c760c07 JB |
3060 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3061 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3062 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3063 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3064 | goto out; |
1ef30be1 JB |
3065 | } |
3066 | ret = 0; | |
c2167754 | 3067 | out: |
a7e3b975 FM |
3068 | if (range_locked || clear_new_delalloc_bytes) { |
3069 | unsigned int clear_bits = 0; | |
3070 | ||
3071 | if (range_locked) | |
3072 | clear_bits |= EXTENT_LOCKED; | |
3073 | if (clear_new_delalloc_bytes) | |
3074 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3075 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3076 | ordered_extent->file_offset, | |
3077 | ordered_extent->file_offset + | |
3078 | ordered_extent->len - 1, | |
3079 | clear_bits, | |
3080 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
ae0f1625 | 3081 | 0, &cached_state); |
a7e3b975 FM |
3082 | } |
3083 | ||
a698d075 | 3084 | if (trans) |
3a45bb20 | 3085 | btrfs_end_transaction(trans); |
0cb59c99 | 3086 | |
77cef2ec JB |
3087 | if (ret || truncated) { |
3088 | u64 start, end; | |
3089 | ||
3090 | if (truncated) | |
3091 | start = ordered_extent->file_offset + logical_len; | |
3092 | else | |
3093 | start = ordered_extent->file_offset; | |
3094 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
f08dc36f | 3095 | clear_extent_uptodate(io_tree, start, end, NULL); |
77cef2ec JB |
3096 | |
3097 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3098 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3099 | |
0bec9ef5 JB |
3100 | /* |
3101 | * If the ordered extent had an IOERR or something else went | |
3102 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3103 | * back to the allocator. We only free the extent in the |
3104 | * truncated case if we didn't write out the extent at all. | |
49940bdd JB |
3105 | * |
3106 | * If we made it past insert_reserved_file_extent before we | |
3107 | * errored out then we don't need to do this as the accounting | |
3108 | * has already been done. | |
0bec9ef5 | 3109 | */ |
77cef2ec | 3110 | if ((ret || !logical_len) && |
49940bdd | 3111 | clear_reserved_extent && |
77cef2ec | 3112 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
0bec9ef5 | 3113 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3114 | btrfs_free_reserved_extent(fs_info, |
3115 | ordered_extent->start, | |
e570fd27 | 3116 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3117 | } |
3118 | ||
3119 | ||
5fd02043 | 3120 | /* |
8bad3c02 LB |
3121 | * This needs to be done to make sure anybody waiting knows we are done |
3122 | * updating everything for this ordered extent. | |
5fd02043 JB |
3123 | */ |
3124 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3125 | ||
38c227d8 | 3126 | /* for snapshot-aware defrag */ |
6f519564 LB |
3127 | if (new) { |
3128 | if (ret) { | |
3129 | free_sa_defrag_extent(new); | |
0b246afa | 3130 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3131 | } else { |
3132 | relink_file_extents(new); | |
3133 | } | |
3134 | } | |
38c227d8 | 3135 | |
e6dcd2dc CM |
3136 | /* once for us */ |
3137 | btrfs_put_ordered_extent(ordered_extent); | |
3138 | /* once for the tree */ | |
3139 | btrfs_put_ordered_extent(ordered_extent); | |
3140 | ||
5fd02043 JB |
3141 | return ret; |
3142 | } | |
3143 | ||
3144 | static void finish_ordered_fn(struct btrfs_work *work) | |
3145 | { | |
3146 | struct btrfs_ordered_extent *ordered_extent; | |
3147 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3148 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3149 | } |
3150 | ||
c629732d NB |
3151 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
3152 | u64 end, int uptodate) | |
211f90e6 | 3153 | { |
5fd02043 | 3154 | struct inode *inode = page->mapping->host; |
0b246afa | 3155 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3156 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3157 | struct btrfs_workqueue *wq; |
3158 | btrfs_work_func_t func; | |
5fd02043 | 3159 | |
1abe9b8a | 3160 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3161 | ||
8b62b72b | 3162 | ClearPagePrivate2(page); |
5fd02043 JB |
3163 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3164 | end - start + 1, uptodate)) | |
c3988d63 | 3165 | return; |
5fd02043 | 3166 | |
70ddc553 | 3167 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
0b246afa | 3168 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3169 | func = btrfs_freespace_write_helper; |
3170 | } else { | |
0b246afa | 3171 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3172 | func = btrfs_endio_write_helper; |
3173 | } | |
5fd02043 | 3174 | |
9e0af237 LB |
3175 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3176 | NULL); | |
3177 | btrfs_queue_work(wq, &ordered_extent->work); | |
211f90e6 CM |
3178 | } |
3179 | ||
dc380aea MX |
3180 | static int __readpage_endio_check(struct inode *inode, |
3181 | struct btrfs_io_bio *io_bio, | |
3182 | int icsum, struct page *page, | |
3183 | int pgoff, u64 start, size_t len) | |
3184 | { | |
3185 | char *kaddr; | |
3186 | u32 csum_expected; | |
3187 | u32 csum = ~(u32)0; | |
dc380aea MX |
3188 | |
3189 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3190 | ||
3191 | kaddr = kmap_atomic(page); | |
3192 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3193 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3194 | if (csum != csum_expected) |
3195 | goto zeroit; | |
3196 | ||
3197 | kunmap_atomic(kaddr); | |
3198 | return 0; | |
3199 | zeroit: | |
0970a22e | 3200 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
6f6b643e | 3201 | io_bio->mirror_num); |
dc380aea MX |
3202 | memset(kaddr + pgoff, 1, len); |
3203 | flush_dcache_page(page); | |
3204 | kunmap_atomic(kaddr); | |
dc380aea MX |
3205 | return -EIO; |
3206 | } | |
3207 | ||
d352ac68 CM |
3208 | /* |
3209 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3210 | * if there's a match, we allow the bio to finish. If not, the code in |
3211 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3212 | */ |
facc8a22 MX |
3213 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3214 | u64 phy_offset, struct page *page, | |
3215 | u64 start, u64 end, int mirror) | |
07157aac | 3216 | { |
4eee4fa4 | 3217 | size_t offset = start - page_offset(page); |
07157aac | 3218 | struct inode *inode = page->mapping->host; |
d1310b2e | 3219 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3220 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3221 | |
d20f7043 CM |
3222 | if (PageChecked(page)) { |
3223 | ClearPageChecked(page); | |
dc380aea | 3224 | return 0; |
d20f7043 | 3225 | } |
6cbff00f CH |
3226 | |
3227 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3228 | return 0; |
17d217fe YZ |
3229 | |
3230 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3231 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3232 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3233 | return 0; |
17d217fe | 3234 | } |
d20f7043 | 3235 | |
facc8a22 | 3236 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3237 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3238 | start, (size_t)(end - start + 1)); | |
07157aac | 3239 | } |
b888db2b | 3240 | |
c1c3fac2 NB |
3241 | /* |
3242 | * btrfs_add_delayed_iput - perform a delayed iput on @inode | |
3243 | * | |
3244 | * @inode: The inode we want to perform iput on | |
3245 | * | |
3246 | * This function uses the generic vfs_inode::i_count to track whether we should | |
3247 | * just decrement it (in case it's > 1) or if this is the last iput then link | |
3248 | * the inode to the delayed iput machinery. Delayed iputs are processed at | |
3249 | * transaction commit time/superblock commit/cleaner kthread. | |
3250 | */ | |
24bbcf04 YZ |
3251 | void btrfs_add_delayed_iput(struct inode *inode) |
3252 | { | |
0b246afa | 3253 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3254 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3255 | |
3256 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3257 | return; | |
3258 | ||
034f784d | 3259 | atomic_inc(&fs_info->nr_delayed_iputs); |
24bbcf04 | 3260 | spin_lock(&fs_info->delayed_iput_lock); |
c1c3fac2 NB |
3261 | ASSERT(list_empty(&binode->delayed_iput)); |
3262 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
24bbcf04 | 3263 | spin_unlock(&fs_info->delayed_iput_lock); |
fd340d0f JB |
3264 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
3265 | wake_up_process(fs_info->cleaner_kthread); | |
24bbcf04 YZ |
3266 | } |
3267 | ||
2ff7e61e | 3268 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3269 | { |
24bbcf04 | 3270 | |
24bbcf04 | 3271 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3272 | while (!list_empty(&fs_info->delayed_iputs)) { |
3273 | struct btrfs_inode *inode; | |
3274 | ||
3275 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3276 | struct btrfs_inode, delayed_iput); | |
c1c3fac2 | 3277 | list_del_init(&inode->delayed_iput); |
8089fe62 DS |
3278 | spin_unlock(&fs_info->delayed_iput_lock); |
3279 | iput(&inode->vfs_inode); | |
034f784d JB |
3280 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
3281 | wake_up(&fs_info->delayed_iputs_wait); | |
8089fe62 | 3282 | spin_lock(&fs_info->delayed_iput_lock); |
24bbcf04 | 3283 | } |
8089fe62 | 3284 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3285 | } |
3286 | ||
034f784d JB |
3287 | /** |
3288 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running | |
3289 | * @fs_info - the fs_info for this fs | |
3290 | * @return - EINTR if we were killed, 0 if nothing's pending | |
3291 | * | |
3292 | * This will wait on any delayed iputs that are currently running with KILLABLE | |
3293 | * set. Once they are all done running we will return, unless we are killed in | |
3294 | * which case we return EINTR. This helps in user operations like fallocate etc | |
3295 | * that might get blocked on the iputs. | |
3296 | */ | |
3297 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) | |
3298 | { | |
3299 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, | |
3300 | atomic_read(&fs_info->nr_delayed_iputs) == 0); | |
3301 | if (ret) | |
3302 | return -EINTR; | |
3303 | return 0; | |
3304 | } | |
3305 | ||
7b128766 | 3306 | /* |
f7e9e8fc OS |
3307 | * This creates an orphan entry for the given inode in case something goes wrong |
3308 | * in the middle of an unlink. | |
7b128766 | 3309 | */ |
73f2e545 | 3310 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
27919067 | 3311 | struct btrfs_inode *inode) |
7b128766 | 3312 | { |
d68fc57b | 3313 | int ret; |
7b128766 | 3314 | |
27919067 OS |
3315 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
3316 | if (ret && ret != -EEXIST) { | |
3317 | btrfs_abort_transaction(trans, ret); | |
3318 | return ret; | |
d68fc57b YZ |
3319 | } |
3320 | ||
d68fc57b | 3321 | return 0; |
7b128766 JB |
3322 | } |
3323 | ||
3324 | /* | |
f7e9e8fc OS |
3325 | * We have done the delete so we can go ahead and remove the orphan item for |
3326 | * this particular inode. | |
7b128766 | 3327 | */ |
48a3b636 | 3328 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3329 | struct btrfs_inode *inode) |
7b128766 | 3330 | { |
27919067 | 3331 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
7b128766 JB |
3332 | } |
3333 | ||
3334 | /* | |
3335 | * this cleans up any orphans that may be left on the list from the last use | |
3336 | * of this root. | |
3337 | */ | |
66b4ffd1 | 3338 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3339 | { |
0b246afa | 3340 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3341 | struct btrfs_path *path; |
3342 | struct extent_buffer *leaf; | |
7b128766 JB |
3343 | struct btrfs_key key, found_key; |
3344 | struct btrfs_trans_handle *trans; | |
3345 | struct inode *inode; | |
8f6d7f4f | 3346 | u64 last_objectid = 0; |
f7e9e8fc | 3347 | int ret = 0, nr_unlink = 0; |
7b128766 | 3348 | |
d68fc57b | 3349 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3350 | return 0; |
c71bf099 YZ |
3351 | |
3352 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3353 | if (!path) { |
3354 | ret = -ENOMEM; | |
3355 | goto out; | |
3356 | } | |
e4058b54 | 3357 | path->reada = READA_BACK; |
7b128766 JB |
3358 | |
3359 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3360 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3361 | key.offset = (u64)-1; |
3362 | ||
7b128766 JB |
3363 | while (1) { |
3364 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3365 | if (ret < 0) |
3366 | goto out; | |
7b128766 JB |
3367 | |
3368 | /* | |
3369 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3370 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3371 | * find the key and see if we have stuff that matches |
3372 | */ | |
3373 | if (ret > 0) { | |
66b4ffd1 | 3374 | ret = 0; |
7b128766 JB |
3375 | if (path->slots[0] == 0) |
3376 | break; | |
3377 | path->slots[0]--; | |
3378 | } | |
3379 | ||
3380 | /* pull out the item */ | |
3381 | leaf = path->nodes[0]; | |
7b128766 JB |
3382 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3383 | ||
3384 | /* make sure the item matches what we want */ | |
3385 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3386 | break; | |
962a298f | 3387 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3388 | break; |
3389 | ||
3390 | /* release the path since we're done with it */ | |
b3b4aa74 | 3391 | btrfs_release_path(path); |
7b128766 JB |
3392 | |
3393 | /* | |
3394 | * this is where we are basically btrfs_lookup, without the | |
3395 | * crossing root thing. we store the inode number in the | |
3396 | * offset of the orphan item. | |
3397 | */ | |
8f6d7f4f JB |
3398 | |
3399 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3400 | btrfs_err(fs_info, |
3401 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3402 | ret = -EINVAL; |
3403 | goto out; | |
3404 | } | |
3405 | ||
3406 | last_objectid = found_key.offset; | |
3407 | ||
5d4f98a2 YZ |
3408 | found_key.objectid = found_key.offset; |
3409 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3410 | found_key.offset = 0; | |
0b246afa | 3411 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3412 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3413 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3414 | goto out; |
7b128766 | 3415 | |
0b246afa | 3416 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3417 | struct btrfs_root *dead_root; |
3418 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3419 | int is_dead_root = 0; | |
3420 | ||
3421 | /* | |
3422 | * this is an orphan in the tree root. Currently these | |
3423 | * could come from 2 sources: | |
3424 | * a) a snapshot deletion in progress | |
3425 | * b) a free space cache inode | |
3426 | * We need to distinguish those two, as the snapshot | |
3427 | * orphan must not get deleted. | |
3428 | * find_dead_roots already ran before us, so if this | |
3429 | * is a snapshot deletion, we should find the root | |
3430 | * in the dead_roots list | |
3431 | */ | |
3432 | spin_lock(&fs_info->trans_lock); | |
3433 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3434 | root_list) { | |
3435 | if (dead_root->root_key.objectid == | |
3436 | found_key.objectid) { | |
3437 | is_dead_root = 1; | |
3438 | break; | |
3439 | } | |
3440 | } | |
3441 | spin_unlock(&fs_info->trans_lock); | |
3442 | if (is_dead_root) { | |
3443 | /* prevent this orphan from being found again */ | |
3444 | key.offset = found_key.objectid - 1; | |
3445 | continue; | |
3446 | } | |
f7e9e8fc | 3447 | |
f8e9e0b0 | 3448 | } |
f7e9e8fc | 3449 | |
7b128766 | 3450 | /* |
f7e9e8fc OS |
3451 | * If we have an inode with links, there are a couple of |
3452 | * possibilities. Old kernels (before v3.12) used to create an | |
3453 | * orphan item for truncate indicating that there were possibly | |
3454 | * extent items past i_size that needed to be deleted. In v3.12, | |
3455 | * truncate was changed to update i_size in sync with the extent | |
3456 | * items, but the (useless) orphan item was still created. Since | |
3457 | * v4.18, we don't create the orphan item for truncate at all. | |
3458 | * | |
3459 | * So, this item could mean that we need to do a truncate, but | |
3460 | * only if this filesystem was last used on a pre-v3.12 kernel | |
3461 | * and was not cleanly unmounted. The odds of that are quite | |
3462 | * slim, and it's a pain to do the truncate now, so just delete | |
3463 | * the orphan item. | |
3464 | * | |
3465 | * It's also possible that this orphan item was supposed to be | |
3466 | * deleted but wasn't. The inode number may have been reused, | |
3467 | * but either way, we can delete the orphan item. | |
7b128766 | 3468 | */ |
f7e9e8fc OS |
3469 | if (ret == -ENOENT || inode->i_nlink) { |
3470 | if (!ret) | |
3471 | iput(inode); | |
a8c9e576 | 3472 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3473 | if (IS_ERR(trans)) { |
3474 | ret = PTR_ERR(trans); | |
3475 | goto out; | |
3476 | } | |
0b246afa JM |
3477 | btrfs_debug(fs_info, "auto deleting %Lu", |
3478 | found_key.objectid); | |
a8c9e576 JB |
3479 | ret = btrfs_del_orphan_item(trans, root, |
3480 | found_key.objectid); | |
3a45bb20 | 3481 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3482 | if (ret) |
3483 | goto out; | |
7b128766 JB |
3484 | continue; |
3485 | } | |
3486 | ||
f7e9e8fc | 3487 | nr_unlink++; |
7b128766 JB |
3488 | |
3489 | /* this will do delete_inode and everything for us */ | |
3490 | iput(inode); | |
3491 | } | |
3254c876 MX |
3492 | /* release the path since we're done with it */ |
3493 | btrfs_release_path(path); | |
3494 | ||
d68fc57b YZ |
3495 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3496 | ||
a575ceeb | 3497 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
7a7eaa40 | 3498 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3499 | if (!IS_ERR(trans)) |
3a45bb20 | 3500 | btrfs_end_transaction(trans); |
d68fc57b | 3501 | } |
7b128766 JB |
3502 | |
3503 | if (nr_unlink) | |
0b246afa | 3504 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
66b4ffd1 JB |
3505 | |
3506 | out: | |
3507 | if (ret) | |
0b246afa | 3508 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3509 | btrfs_free_path(path); |
3510 | return ret; | |
7b128766 JB |
3511 | } |
3512 | ||
46a53cca CM |
3513 | /* |
3514 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3515 | * don't find any xattrs, we know there can't be any acls. | |
3516 | * | |
3517 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3518 | */ | |
3519 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3520 | int slot, u64 objectid, |
3521 | int *first_xattr_slot) | |
46a53cca CM |
3522 | { |
3523 | u32 nritems = btrfs_header_nritems(leaf); | |
3524 | struct btrfs_key found_key; | |
f23b5a59 JB |
3525 | static u64 xattr_access = 0; |
3526 | static u64 xattr_default = 0; | |
46a53cca CM |
3527 | int scanned = 0; |
3528 | ||
f23b5a59 | 3529 | if (!xattr_access) { |
97d79299 AG |
3530 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3531 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3532 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3533 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3534 | } |
3535 | ||
46a53cca | 3536 | slot++; |
63541927 | 3537 | *first_xattr_slot = -1; |
46a53cca CM |
3538 | while (slot < nritems) { |
3539 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3540 | ||
3541 | /* we found a different objectid, there must not be acls */ | |
3542 | if (found_key.objectid != objectid) | |
3543 | return 0; | |
3544 | ||
3545 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3546 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3547 | if (*first_xattr_slot == -1) |
3548 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3549 | if (found_key.offset == xattr_access || |
3550 | found_key.offset == xattr_default) | |
3551 | return 1; | |
3552 | } | |
46a53cca CM |
3553 | |
3554 | /* | |
3555 | * we found a key greater than an xattr key, there can't | |
3556 | * be any acls later on | |
3557 | */ | |
3558 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3559 | return 0; | |
3560 | ||
3561 | slot++; | |
3562 | scanned++; | |
3563 | ||
3564 | /* | |
3565 | * it goes inode, inode backrefs, xattrs, extents, | |
3566 | * so if there are a ton of hard links to an inode there can | |
3567 | * be a lot of backrefs. Don't waste time searching too hard, | |
3568 | * this is just an optimization | |
3569 | */ | |
3570 | if (scanned >= 8) | |
3571 | break; | |
3572 | } | |
3573 | /* we hit the end of the leaf before we found an xattr or | |
3574 | * something larger than an xattr. We have to assume the inode | |
3575 | * has acls | |
3576 | */ | |
63541927 FDBM |
3577 | if (*first_xattr_slot == -1) |
3578 | *first_xattr_slot = slot; | |
46a53cca CM |
3579 | return 1; |
3580 | } | |
3581 | ||
d352ac68 CM |
3582 | /* |
3583 | * read an inode from the btree into the in-memory inode | |
3584 | */ | |
4222ea71 FM |
3585 | static int btrfs_read_locked_inode(struct inode *inode, |
3586 | struct btrfs_path *in_path) | |
39279cc3 | 3587 | { |
0b246afa | 3588 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4222ea71 | 3589 | struct btrfs_path *path = in_path; |
5f39d397 | 3590 | struct extent_buffer *leaf; |
39279cc3 CM |
3591 | struct btrfs_inode_item *inode_item; |
3592 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3593 | struct btrfs_key location; | |
67de1176 | 3594 | unsigned long ptr; |
46a53cca | 3595 | int maybe_acls; |
618e21d5 | 3596 | u32 rdev; |
39279cc3 | 3597 | int ret; |
2f7e33d4 | 3598 | bool filled = false; |
63541927 | 3599 | int first_xattr_slot; |
2f7e33d4 MX |
3600 | |
3601 | ret = btrfs_fill_inode(inode, &rdev); | |
3602 | if (!ret) | |
3603 | filled = true; | |
39279cc3 | 3604 | |
4222ea71 FM |
3605 | if (!path) { |
3606 | path = btrfs_alloc_path(); | |
3607 | if (!path) | |
3608 | return -ENOMEM; | |
3609 | } | |
1748f843 | 3610 | |
39279cc3 | 3611 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3612 | |
39279cc3 | 3613 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 | 3614 | if (ret) { |
4222ea71 FM |
3615 | if (path != in_path) |
3616 | btrfs_free_path(path); | |
f5b3a417 | 3617 | return ret; |
67710892 | 3618 | } |
39279cc3 | 3619 | |
5f39d397 | 3620 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3621 | |
3622 | if (filled) | |
67de1176 | 3623 | goto cache_index; |
2f7e33d4 | 3624 | |
5f39d397 CM |
3625 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3626 | struct btrfs_inode_item); | |
5f39d397 | 3627 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3628 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3629 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3630 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3631 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3632 | |
a937b979 DS |
3633 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3634 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3635 | |
a937b979 DS |
3636 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3637 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3638 | |
a937b979 DS |
3639 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3640 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3641 | |
9cc97d64 | 3642 | BTRFS_I(inode)->i_otime.tv_sec = |
3643 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3644 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3645 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3646 | |
a76a3cd4 | 3647 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3648 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3649 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3650 | ||
c7f88c4e JL |
3651 | inode_set_iversion_queried(inode, |
3652 | btrfs_inode_sequence(leaf, inode_item)); | |
6e17d30b YD |
3653 | inode->i_generation = BTRFS_I(inode)->generation; |
3654 | inode->i_rdev = 0; | |
3655 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3656 | ||
3657 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3658 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3659 | ||
3660 | cache_index: | |
5dc562c5 JB |
3661 | /* |
3662 | * If we were modified in the current generation and evicted from memory | |
3663 | * and then re-read we need to do a full sync since we don't have any | |
3664 | * idea about which extents were modified before we were evicted from | |
3665 | * cache. | |
6e17d30b YD |
3666 | * |
3667 | * This is required for both inode re-read from disk and delayed inode | |
3668 | * in delayed_nodes_tree. | |
5dc562c5 | 3669 | */ |
0b246afa | 3670 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3671 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3672 | &BTRFS_I(inode)->runtime_flags); | |
3673 | ||
bde6c242 FM |
3674 | /* |
3675 | * We don't persist the id of the transaction where an unlink operation | |
3676 | * against the inode was last made. So here we assume the inode might | |
3677 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3678 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3679 | * between the inode and its parent if the inode is fsync'ed and the log | |
3680 | * replayed. For example, in the scenario: | |
3681 | * | |
3682 | * touch mydir/foo | |
3683 | * ln mydir/foo mydir/bar | |
3684 | * sync | |
3685 | * unlink mydir/bar | |
3686 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3687 | * xfs_io -c fsync mydir/foo | |
3688 | * <power failure> | |
3689 | * mount fs, triggers fsync log replay | |
3690 | * | |
3691 | * We must make sure that when we fsync our inode foo we also log its | |
3692 | * parent inode, otherwise after log replay the parent still has the | |
3693 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3694 | * and doesn't have an inode ref with the name "bar" anymore. | |
3695 | * | |
3696 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3697 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3698 | * transaction commits on fsync if our inode is a directory, or if our |
3699 | * inode is not a directory, logging its parent unnecessarily. | |
3700 | */ | |
3701 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
41bd6067 FM |
3702 | /* |
3703 | * Similar reasoning for last_link_trans, needs to be set otherwise | |
3704 | * for a case like the following: | |
3705 | * | |
3706 | * mkdir A | |
3707 | * touch foo | |
3708 | * ln foo A/bar | |
3709 | * echo 2 > /proc/sys/vm/drop_caches | |
3710 | * fsync foo | |
3711 | * <power failure> | |
3712 | * | |
3713 | * Would result in link bar and directory A not existing after the power | |
3714 | * failure. | |
3715 | */ | |
3716 | BTRFS_I(inode)->last_link_trans = BTRFS_I(inode)->last_trans; | |
bde6c242 | 3717 | |
67de1176 MX |
3718 | path->slots[0]++; |
3719 | if (inode->i_nlink != 1 || | |
3720 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3721 | goto cache_acl; | |
3722 | ||
3723 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3724 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3725 | goto cache_acl; |
3726 | ||
3727 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3728 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3729 | struct btrfs_inode_ref *ref; | |
3730 | ||
3731 | ref = (struct btrfs_inode_ref *)ptr; | |
3732 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3733 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3734 | struct btrfs_inode_extref *extref; | |
3735 | ||
3736 | extref = (struct btrfs_inode_extref *)ptr; | |
3737 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3738 | extref); | |
3739 | } | |
2f7e33d4 | 3740 | cache_acl: |
46a53cca CM |
3741 | /* |
3742 | * try to precache a NULL acl entry for files that don't have | |
3743 | * any xattrs or acls | |
3744 | */ | |
33345d01 | 3745 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3746 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3747 | if (first_xattr_slot != -1) { |
3748 | path->slots[0] = first_xattr_slot; | |
3749 | ret = btrfs_load_inode_props(inode, path); | |
3750 | if (ret) | |
0b246afa | 3751 | btrfs_err(fs_info, |
351fd353 | 3752 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3753 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3754 | root->root_key.objectid, ret); |
3755 | } | |
4222ea71 FM |
3756 | if (path != in_path) |
3757 | btrfs_free_path(path); | |
63541927 | 3758 | |
72c04902 AV |
3759 | if (!maybe_acls) |
3760 | cache_no_acl(inode); | |
46a53cca | 3761 | |
39279cc3 | 3762 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3763 | case S_IFREG: |
3764 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3765 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3766 | inode->i_fop = &btrfs_file_operations; |
3767 | inode->i_op = &btrfs_file_inode_operations; | |
3768 | break; | |
3769 | case S_IFDIR: | |
3770 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3771 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3772 | break; |
3773 | case S_IFLNK: | |
3774 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3775 | inode_nohighmem(inode); |
4779cc04 | 3776 | inode->i_mapping->a_ops = &btrfs_aops; |
39279cc3 | 3777 | break; |
618e21d5 | 3778 | default: |
0279b4cd | 3779 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3780 | init_special_inode(inode, inode->i_mode, rdev); |
3781 | break; | |
39279cc3 | 3782 | } |
6cbff00f | 3783 | |
7b6a221e | 3784 | btrfs_sync_inode_flags_to_i_flags(inode); |
67710892 | 3785 | return 0; |
39279cc3 CM |
3786 | } |
3787 | ||
d352ac68 CM |
3788 | /* |
3789 | * given a leaf and an inode, copy the inode fields into the leaf | |
3790 | */ | |
e02119d5 CM |
3791 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3792 | struct extent_buffer *leaf, | |
5f39d397 | 3793 | struct btrfs_inode_item *item, |
39279cc3 CM |
3794 | struct inode *inode) |
3795 | { | |
51fab693 LB |
3796 | struct btrfs_map_token token; |
3797 | ||
3798 | btrfs_init_map_token(&token); | |
5f39d397 | 3799 | |
51fab693 LB |
3800 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3801 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3802 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3803 | &token); | |
3804 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3805 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3806 | |
a937b979 | 3807 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3808 | inode->i_atime.tv_sec, &token); |
a937b979 | 3809 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3810 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3811 | |
a937b979 | 3812 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3813 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3814 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3815 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3816 | |
a937b979 | 3817 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3818 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3819 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3820 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3821 | |
9cc97d64 | 3822 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3823 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3824 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3825 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3826 | ||
51fab693 LB |
3827 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3828 | &token); | |
3829 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3830 | &token); | |
c7f88c4e JL |
3831 | btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode), |
3832 | &token); | |
51fab693 LB |
3833 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
3834 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3835 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3836 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3837 | } |
3838 | ||
d352ac68 CM |
3839 | /* |
3840 | * copy everything in the in-memory inode into the btree. | |
3841 | */ | |
2115133f | 3842 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3843 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3844 | { |
3845 | struct btrfs_inode_item *inode_item; | |
3846 | struct btrfs_path *path; | |
5f39d397 | 3847 | struct extent_buffer *leaf; |
39279cc3 CM |
3848 | int ret; |
3849 | ||
3850 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3851 | if (!path) |
3852 | return -ENOMEM; | |
3853 | ||
b9473439 | 3854 | path->leave_spinning = 1; |
16cdcec7 MX |
3855 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3856 | 1); | |
39279cc3 CM |
3857 | if (ret) { |
3858 | if (ret > 0) | |
3859 | ret = -ENOENT; | |
3860 | goto failed; | |
3861 | } | |
3862 | ||
5f39d397 CM |
3863 | leaf = path->nodes[0]; |
3864 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3865 | struct btrfs_inode_item); |
39279cc3 | 3866 | |
e02119d5 | 3867 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3868 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3869 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3870 | ret = 0; |
3871 | failed: | |
39279cc3 CM |
3872 | btrfs_free_path(path); |
3873 | return ret; | |
3874 | } | |
3875 | ||
2115133f CM |
3876 | /* |
3877 | * copy everything in the in-memory inode into the btree. | |
3878 | */ | |
3879 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3880 | struct btrfs_root *root, struct inode *inode) | |
3881 | { | |
0b246afa | 3882 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3883 | int ret; |
3884 | ||
3885 | /* | |
3886 | * If the inode is a free space inode, we can deadlock during commit | |
3887 | * if we put it into the delayed code. | |
3888 | * | |
3889 | * The data relocation inode should also be directly updated | |
3890 | * without delay | |
3891 | */ | |
70ddc553 | 3892 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3893 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3894 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3895 | btrfs_update_root_times(trans, root); |
3896 | ||
2115133f CM |
3897 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3898 | if (!ret) | |
3899 | btrfs_set_inode_last_trans(trans, inode); | |
3900 | return ret; | |
3901 | } | |
3902 | ||
3903 | return btrfs_update_inode_item(trans, root, inode); | |
3904 | } | |
3905 | ||
be6aef60 JB |
3906 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3907 | struct btrfs_root *root, | |
3908 | struct inode *inode) | |
2115133f CM |
3909 | { |
3910 | int ret; | |
3911 | ||
3912 | ret = btrfs_update_inode(trans, root, inode); | |
3913 | if (ret == -ENOSPC) | |
3914 | return btrfs_update_inode_item(trans, root, inode); | |
3915 | return ret; | |
3916 | } | |
3917 | ||
d352ac68 CM |
3918 | /* |
3919 | * unlink helper that gets used here in inode.c and in the tree logging | |
3920 | * recovery code. It remove a link in a directory with a given name, and | |
3921 | * also drops the back refs in the inode to the directory | |
3922 | */ | |
92986796 AV |
3923 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3924 | struct btrfs_root *root, | |
4ec5934e NB |
3925 | struct btrfs_inode *dir, |
3926 | struct btrfs_inode *inode, | |
92986796 | 3927 | const char *name, int name_len) |
39279cc3 | 3928 | { |
0b246afa | 3929 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 3930 | struct btrfs_path *path; |
39279cc3 | 3931 | int ret = 0; |
5f39d397 | 3932 | struct extent_buffer *leaf; |
39279cc3 | 3933 | struct btrfs_dir_item *di; |
5f39d397 | 3934 | struct btrfs_key key; |
aec7477b | 3935 | u64 index; |
33345d01 LZ |
3936 | u64 ino = btrfs_ino(inode); |
3937 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3938 | |
3939 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3940 | if (!path) { |
3941 | ret = -ENOMEM; | |
554233a6 | 3942 | goto out; |
54aa1f4d CM |
3943 | } |
3944 | ||
b9473439 | 3945 | path->leave_spinning = 1; |
33345d01 | 3946 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 | 3947 | name, name_len, -1); |
3cf5068f LB |
3948 | if (IS_ERR_OR_NULL(di)) { |
3949 | ret = di ? PTR_ERR(di) : -ENOENT; | |
39279cc3 CM |
3950 | goto err; |
3951 | } | |
5f39d397 CM |
3952 | leaf = path->nodes[0]; |
3953 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3954 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3955 | if (ret) |
3956 | goto err; | |
b3b4aa74 | 3957 | btrfs_release_path(path); |
39279cc3 | 3958 | |
67de1176 MX |
3959 | /* |
3960 | * If we don't have dir index, we have to get it by looking up | |
3961 | * the inode ref, since we get the inode ref, remove it directly, | |
3962 | * it is unnecessary to do delayed deletion. | |
3963 | * | |
3964 | * But if we have dir index, needn't search inode ref to get it. | |
3965 | * Since the inode ref is close to the inode item, it is better | |
3966 | * that we delay to delete it, and just do this deletion when | |
3967 | * we update the inode item. | |
3968 | */ | |
4ec5934e | 3969 | if (inode->dir_index) { |
67de1176 MX |
3970 | ret = btrfs_delayed_delete_inode_ref(inode); |
3971 | if (!ret) { | |
4ec5934e | 3972 | index = inode->dir_index; |
67de1176 MX |
3973 | goto skip_backref; |
3974 | } | |
3975 | } | |
3976 | ||
33345d01 LZ |
3977 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3978 | dir_ino, &index); | |
aec7477b | 3979 | if (ret) { |
0b246afa | 3980 | btrfs_info(fs_info, |
c2cf52eb | 3981 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 3982 | name_len, name, ino, dir_ino); |
66642832 | 3983 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
3984 | goto err; |
3985 | } | |
67de1176 | 3986 | skip_backref: |
9add2945 | 3987 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
79787eaa | 3988 | if (ret) { |
66642832 | 3989 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 3990 | goto err; |
79787eaa | 3991 | } |
39279cc3 | 3992 | |
4ec5934e NB |
3993 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
3994 | dir_ino); | |
79787eaa | 3995 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 3996 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3997 | goto err; |
3998 | } | |
e02119d5 | 3999 | |
4ec5934e NB |
4000 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4001 | index); | |
6418c961 CM |
4002 | if (ret == -ENOENT) |
4003 | ret = 0; | |
d4e3991b | 4004 | else if (ret) |
66642832 | 4005 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4006 | err: |
4007 | btrfs_free_path(path); | |
e02119d5 CM |
4008 | if (ret) |
4009 | goto out; | |
4010 | ||
6ef06d27 | 4011 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4012 | inode_inc_iversion(&inode->vfs_inode); |
4013 | inode_inc_iversion(&dir->vfs_inode); | |
4014 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4015 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4016 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4017 | out: |
39279cc3 CM |
4018 | return ret; |
4019 | } | |
4020 | ||
92986796 AV |
4021 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4022 | struct btrfs_root *root, | |
4ec5934e | 4023 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4024 | const char *name, int name_len) |
4025 | { | |
4026 | int ret; | |
4027 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4028 | if (!ret) { | |
4ec5934e NB |
4029 | drop_nlink(&inode->vfs_inode); |
4030 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4031 | } |
4032 | return ret; | |
4033 | } | |
39279cc3 | 4034 | |
a22285a6 YZ |
4035 | /* |
4036 | * helper to start transaction for unlink and rmdir. | |
4037 | * | |
d52be818 JB |
4038 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4039 | * if we cannot make our reservations the normal way try and see if there is | |
4040 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4041 | * allow the unlink to occur. | |
a22285a6 | 4042 | */ |
d52be818 | 4043 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4044 | { |
a22285a6 | 4045 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4046 | |
e70bea5f JB |
4047 | /* |
4048 | * 1 for the possible orphan item | |
4049 | * 1 for the dir item | |
4050 | * 1 for the dir index | |
4051 | * 1 for the inode ref | |
e70bea5f JB |
4052 | * 1 for the inode |
4053 | */ | |
8eab77ff | 4054 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4055 | } |
4056 | ||
4057 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4058 | { | |
4059 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4060 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4061 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4062 | int ret; |
a22285a6 | 4063 | |
d52be818 | 4064 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4065 | if (IS_ERR(trans)) |
4066 | return PTR_ERR(trans); | |
5f39d397 | 4067 | |
4ec5934e NB |
4068 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4069 | 0); | |
12fcfd22 | 4070 | |
4ec5934e NB |
4071 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4072 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4073 | dentry->d_name.len); | |
b532402e TI |
4074 | if (ret) |
4075 | goto out; | |
7b128766 | 4076 | |
a22285a6 | 4077 | if (inode->i_nlink == 0) { |
73f2e545 | 4078 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4079 | if (ret) |
4080 | goto out; | |
a22285a6 | 4081 | } |
7b128766 | 4082 | |
b532402e | 4083 | out: |
3a45bb20 | 4084 | btrfs_end_transaction(trans); |
2ff7e61e | 4085 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4086 | return ret; |
4087 | } | |
4088 | ||
f60a2364 | 4089 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
401b3b19 LF |
4090 | struct inode *dir, u64 objectid, |
4091 | const char *name, int name_len) | |
4df27c4d | 4092 | { |
401b3b19 | 4093 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
4094 | struct btrfs_path *path; |
4095 | struct extent_buffer *leaf; | |
4096 | struct btrfs_dir_item *di; | |
4097 | struct btrfs_key key; | |
4098 | u64 index; | |
4099 | int ret; | |
4a0cc7ca | 4100 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4101 | |
4102 | path = btrfs_alloc_path(); | |
4103 | if (!path) | |
4104 | return -ENOMEM; | |
4105 | ||
33345d01 | 4106 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4107 | name, name_len, -1); |
79787eaa | 4108 | if (IS_ERR_OR_NULL(di)) { |
3cf5068f | 4109 | ret = di ? PTR_ERR(di) : -ENOENT; |
79787eaa JM |
4110 | goto out; |
4111 | } | |
4df27c4d YZ |
4112 | |
4113 | leaf = path->nodes[0]; | |
4114 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4115 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4116 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4117 | if (ret) { |
66642832 | 4118 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4119 | goto out; |
4120 | } | |
b3b4aa74 | 4121 | btrfs_release_path(path); |
4df27c4d | 4122 | |
3ee1c553 LF |
4123 | ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid, |
4124 | dir_ino, &index, name, name_len); | |
4df27c4d | 4125 | if (ret < 0) { |
79787eaa | 4126 | if (ret != -ENOENT) { |
66642832 | 4127 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4128 | goto out; |
4129 | } | |
33345d01 | 4130 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4131 | name, name_len); |
79787eaa JM |
4132 | if (IS_ERR_OR_NULL(di)) { |
4133 | if (!di) | |
4134 | ret = -ENOENT; | |
4135 | else | |
4136 | ret = PTR_ERR(di); | |
66642832 | 4137 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4138 | goto out; |
4139 | } | |
4df27c4d YZ |
4140 | |
4141 | leaf = path->nodes[0]; | |
4142 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4df27c4d YZ |
4143 | index = key.offset; |
4144 | } | |
945d8962 | 4145 | btrfs_release_path(path); |
4df27c4d | 4146 | |
9add2945 | 4147 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
79787eaa | 4148 | if (ret) { |
66642832 | 4149 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4150 | goto out; |
4151 | } | |
4df27c4d | 4152 | |
6ef06d27 | 4153 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4154 | inode_inc_iversion(dir); |
c2050a45 | 4155 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4156 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4157 | if (ret) |
66642832 | 4158 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4159 | out: |
71d7aed0 | 4160 | btrfs_free_path(path); |
79787eaa | 4161 | return ret; |
4df27c4d YZ |
4162 | } |
4163 | ||
ec42f167 MT |
4164 | /* |
4165 | * Helper to check if the subvolume references other subvolumes or if it's | |
4166 | * default. | |
4167 | */ | |
f60a2364 | 4168 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
ec42f167 MT |
4169 | { |
4170 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4171 | struct btrfs_path *path; | |
4172 | struct btrfs_dir_item *di; | |
4173 | struct btrfs_key key; | |
4174 | u64 dir_id; | |
4175 | int ret; | |
4176 | ||
4177 | path = btrfs_alloc_path(); | |
4178 | if (!path) | |
4179 | return -ENOMEM; | |
4180 | ||
4181 | /* Make sure this root isn't set as the default subvol */ | |
4182 | dir_id = btrfs_super_root_dir(fs_info->super_copy); | |
4183 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, | |
4184 | dir_id, "default", 7, 0); | |
4185 | if (di && !IS_ERR(di)) { | |
4186 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
4187 | if (key.objectid == root->root_key.objectid) { | |
4188 | ret = -EPERM; | |
4189 | btrfs_err(fs_info, | |
4190 | "deleting default subvolume %llu is not allowed", | |
4191 | key.objectid); | |
4192 | goto out; | |
4193 | } | |
4194 | btrfs_release_path(path); | |
4195 | } | |
4196 | ||
4197 | key.objectid = root->root_key.objectid; | |
4198 | key.type = BTRFS_ROOT_REF_KEY; | |
4199 | key.offset = (u64)-1; | |
4200 | ||
4201 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
4202 | if (ret < 0) | |
4203 | goto out; | |
4204 | BUG_ON(ret == 0); | |
4205 | ||
4206 | ret = 0; | |
4207 | if (path->slots[0] > 0) { | |
4208 | path->slots[0]--; | |
4209 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4210 | if (key.objectid == root->root_key.objectid && | |
4211 | key.type == BTRFS_ROOT_REF_KEY) | |
4212 | ret = -ENOTEMPTY; | |
4213 | } | |
4214 | out: | |
4215 | btrfs_free_path(path); | |
4216 | return ret; | |
4217 | } | |
4218 | ||
20a68004 NB |
4219 | /* Delete all dentries for inodes belonging to the root */ |
4220 | static void btrfs_prune_dentries(struct btrfs_root *root) | |
4221 | { | |
4222 | struct btrfs_fs_info *fs_info = root->fs_info; | |
4223 | struct rb_node *node; | |
4224 | struct rb_node *prev; | |
4225 | struct btrfs_inode *entry; | |
4226 | struct inode *inode; | |
4227 | u64 objectid = 0; | |
4228 | ||
4229 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) | |
4230 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
4231 | ||
4232 | spin_lock(&root->inode_lock); | |
4233 | again: | |
4234 | node = root->inode_tree.rb_node; | |
4235 | prev = NULL; | |
4236 | while (node) { | |
4237 | prev = node; | |
4238 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4239 | ||
37508515 | 4240 | if (objectid < btrfs_ino(entry)) |
20a68004 | 4241 | node = node->rb_left; |
37508515 | 4242 | else if (objectid > btrfs_ino(entry)) |
20a68004 NB |
4243 | node = node->rb_right; |
4244 | else | |
4245 | break; | |
4246 | } | |
4247 | if (!node) { | |
4248 | while (prev) { | |
4249 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
37508515 | 4250 | if (objectid <= btrfs_ino(entry)) { |
20a68004 NB |
4251 | node = prev; |
4252 | break; | |
4253 | } | |
4254 | prev = rb_next(prev); | |
4255 | } | |
4256 | } | |
4257 | while (node) { | |
4258 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
37508515 | 4259 | objectid = btrfs_ino(entry) + 1; |
20a68004 NB |
4260 | inode = igrab(&entry->vfs_inode); |
4261 | if (inode) { | |
4262 | spin_unlock(&root->inode_lock); | |
4263 | if (atomic_read(&inode->i_count) > 1) | |
4264 | d_prune_aliases(inode); | |
4265 | /* | |
4266 | * btrfs_drop_inode will have it removed from the inode | |
4267 | * cache when its usage count hits zero. | |
4268 | */ | |
4269 | iput(inode); | |
4270 | cond_resched(); | |
4271 | spin_lock(&root->inode_lock); | |
4272 | goto again; | |
4273 | } | |
4274 | ||
4275 | if (cond_resched_lock(&root->inode_lock)) | |
4276 | goto again; | |
4277 | ||
4278 | node = rb_next(node); | |
4279 | } | |
4280 | spin_unlock(&root->inode_lock); | |
4281 | } | |
4282 | ||
f60a2364 MT |
4283 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
4284 | { | |
4285 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); | |
4286 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4287 | struct inode *inode = d_inode(dentry); | |
4288 | struct btrfs_root *dest = BTRFS_I(inode)->root; | |
4289 | struct btrfs_trans_handle *trans; | |
4290 | struct btrfs_block_rsv block_rsv; | |
4291 | u64 root_flags; | |
f60a2364 MT |
4292 | int ret; |
4293 | int err; | |
4294 | ||
4295 | /* | |
4296 | * Don't allow to delete a subvolume with send in progress. This is | |
4297 | * inside the inode lock so the error handling that has to drop the bit | |
4298 | * again is not run concurrently. | |
4299 | */ | |
4300 | spin_lock(&dest->root_item_lock); | |
a7176f74 | 4301 | if (dest->send_in_progress) { |
f60a2364 MT |
4302 | spin_unlock(&dest->root_item_lock); |
4303 | btrfs_warn(fs_info, | |
4304 | "attempt to delete subvolume %llu during send", | |
4305 | dest->root_key.objectid); | |
4306 | return -EPERM; | |
4307 | } | |
a7176f74 LF |
4308 | root_flags = btrfs_root_flags(&dest->root_item); |
4309 | btrfs_set_root_flags(&dest->root_item, | |
4310 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); | |
4311 | spin_unlock(&dest->root_item_lock); | |
f60a2364 MT |
4312 | |
4313 | down_write(&fs_info->subvol_sem); | |
4314 | ||
4315 | err = may_destroy_subvol(dest); | |
4316 | if (err) | |
4317 | goto out_up_write; | |
4318 | ||
4319 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); | |
4320 | /* | |
4321 | * One for dir inode, | |
4322 | * two for dir entries, | |
4323 | * two for root ref/backref. | |
4324 | */ | |
c4c129db | 4325 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
f60a2364 MT |
4326 | if (err) |
4327 | goto out_up_write; | |
4328 | ||
4329 | trans = btrfs_start_transaction(root, 0); | |
4330 | if (IS_ERR(trans)) { | |
4331 | err = PTR_ERR(trans); | |
4332 | goto out_release; | |
4333 | } | |
4334 | trans->block_rsv = &block_rsv; | |
4335 | trans->bytes_reserved = block_rsv.size; | |
4336 | ||
4337 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); | |
4338 | ||
401b3b19 LF |
4339 | ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid, |
4340 | dentry->d_name.name, dentry->d_name.len); | |
f60a2364 MT |
4341 | if (ret) { |
4342 | err = ret; | |
4343 | btrfs_abort_transaction(trans, ret); | |
4344 | goto out_end_trans; | |
4345 | } | |
4346 | ||
4347 | btrfs_record_root_in_trans(trans, dest); | |
4348 | ||
4349 | memset(&dest->root_item.drop_progress, 0, | |
4350 | sizeof(dest->root_item.drop_progress)); | |
4351 | dest->root_item.drop_level = 0; | |
4352 | btrfs_set_root_refs(&dest->root_item, 0); | |
4353 | ||
4354 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { | |
4355 | ret = btrfs_insert_orphan_item(trans, | |
4356 | fs_info->tree_root, | |
4357 | dest->root_key.objectid); | |
4358 | if (ret) { | |
4359 | btrfs_abort_transaction(trans, ret); | |
4360 | err = ret; | |
4361 | goto out_end_trans; | |
4362 | } | |
4363 | } | |
4364 | ||
d1957791 | 4365 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
f60a2364 MT |
4366 | BTRFS_UUID_KEY_SUBVOL, |
4367 | dest->root_key.objectid); | |
4368 | if (ret && ret != -ENOENT) { | |
4369 | btrfs_abort_transaction(trans, ret); | |
4370 | err = ret; | |
4371 | goto out_end_trans; | |
4372 | } | |
4373 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { | |
d1957791 | 4374 | ret = btrfs_uuid_tree_remove(trans, |
f60a2364 MT |
4375 | dest->root_item.received_uuid, |
4376 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4377 | dest->root_key.objectid); | |
4378 | if (ret && ret != -ENOENT) { | |
4379 | btrfs_abort_transaction(trans, ret); | |
4380 | err = ret; | |
4381 | goto out_end_trans; | |
4382 | } | |
4383 | } | |
4384 | ||
4385 | out_end_trans: | |
4386 | trans->block_rsv = NULL; | |
4387 | trans->bytes_reserved = 0; | |
4388 | ret = btrfs_end_transaction(trans); | |
4389 | if (ret && !err) | |
4390 | err = ret; | |
4391 | inode->i_flags |= S_DEAD; | |
4392 | out_release: | |
4393 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); | |
4394 | out_up_write: | |
4395 | up_write(&fs_info->subvol_sem); | |
4396 | if (err) { | |
4397 | spin_lock(&dest->root_item_lock); | |
4398 | root_flags = btrfs_root_flags(&dest->root_item); | |
4399 | btrfs_set_root_flags(&dest->root_item, | |
4400 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); | |
4401 | spin_unlock(&dest->root_item_lock); | |
4402 | } else { | |
4403 | d_invalidate(dentry); | |
20a68004 | 4404 | btrfs_prune_dentries(dest); |
f60a2364 MT |
4405 | ASSERT(dest->send_in_progress == 0); |
4406 | ||
4407 | /* the last ref */ | |
4408 | if (dest->ino_cache_inode) { | |
4409 | iput(dest->ino_cache_inode); | |
4410 | dest->ino_cache_inode = NULL; | |
4411 | } | |
4412 | } | |
4413 | ||
4414 | return err; | |
4415 | } | |
4416 | ||
39279cc3 CM |
4417 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4418 | { | |
2b0143b5 | 4419 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4420 | int err = 0; |
39279cc3 | 4421 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4422 | struct btrfs_trans_handle *trans; |
44f714da | 4423 | u64 last_unlink_trans; |
39279cc3 | 4424 | |
b3ae244e | 4425 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4426 | return -ENOTEMPTY; |
4a0cc7ca | 4427 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
a79a464d | 4428 | return btrfs_delete_subvolume(dir, dentry); |
134d4512 | 4429 | |
d52be818 | 4430 | trans = __unlink_start_trans(dir); |
a22285a6 | 4431 | if (IS_ERR(trans)) |
5df6a9f6 | 4432 | return PTR_ERR(trans); |
5df6a9f6 | 4433 | |
4a0cc7ca | 4434 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
401b3b19 | 4435 | err = btrfs_unlink_subvol(trans, dir, |
4df27c4d YZ |
4436 | BTRFS_I(inode)->location.objectid, |
4437 | dentry->d_name.name, | |
4438 | dentry->d_name.len); | |
4439 | goto out; | |
4440 | } | |
4441 | ||
73f2e545 | 4442 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4443 | if (err) |
4df27c4d | 4444 | goto out; |
7b128766 | 4445 | |
44f714da FM |
4446 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4447 | ||
39279cc3 | 4448 | /* now the directory is empty */ |
4ec5934e NB |
4449 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4450 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4451 | dentry->d_name.len); | |
44f714da | 4452 | if (!err) { |
6ef06d27 | 4453 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4454 | /* |
4455 | * Propagate the last_unlink_trans value of the deleted dir to | |
4456 | * its parent directory. This is to prevent an unrecoverable | |
4457 | * log tree in the case we do something like this: | |
4458 | * 1) create dir foo | |
4459 | * 2) create snapshot under dir foo | |
4460 | * 3) delete the snapshot | |
4461 | * 4) rmdir foo | |
4462 | * 5) mkdir foo | |
4463 | * 6) fsync foo or some file inside foo | |
4464 | */ | |
4465 | if (last_unlink_trans >= trans->transid) | |
4466 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4467 | } | |
4df27c4d | 4468 | out: |
3a45bb20 | 4469 | btrfs_end_transaction(trans); |
2ff7e61e | 4470 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4471 | |
39279cc3 CM |
4472 | return err; |
4473 | } | |
4474 | ||
ddfae63c JB |
4475 | /* |
4476 | * Return this if we need to call truncate_block for the last bit of the | |
4477 | * truncate. | |
4478 | */ | |
4479 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4480 | |
39279cc3 CM |
4481 | /* |
4482 | * this can truncate away extent items, csum items and directory items. | |
4483 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4484 | * any higher than new_size |
39279cc3 CM |
4485 | * |
4486 | * csum items that cross the new i_size are truncated to the new size | |
4487 | * as well. | |
7b128766 JB |
4488 | * |
4489 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4490 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4491 | */ |
8082510e YZ |
4492 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4493 | struct btrfs_root *root, | |
4494 | struct inode *inode, | |
4495 | u64 new_size, u32 min_type) | |
39279cc3 | 4496 | { |
0b246afa | 4497 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4498 | struct btrfs_path *path; |
5f39d397 | 4499 | struct extent_buffer *leaf; |
39279cc3 | 4500 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4501 | struct btrfs_key key; |
4502 | struct btrfs_key found_key; | |
39279cc3 | 4503 | u64 extent_start = 0; |
db94535d | 4504 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4505 | u64 extent_offset = 0; |
39279cc3 | 4506 | u64 item_end = 0; |
c1aa4575 | 4507 | u64 last_size = new_size; |
8082510e | 4508 | u32 found_type = (u8)-1; |
39279cc3 CM |
4509 | int found_extent; |
4510 | int del_item; | |
85e21bac CM |
4511 | int pending_del_nr = 0; |
4512 | int pending_del_slot = 0; | |
179e29e4 | 4513 | int extent_type = -1; |
8082510e | 4514 | int ret; |
4a0cc7ca | 4515 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4516 | u64 bytes_deleted = 0; |
897ca819 TM |
4517 | bool be_nice = false; |
4518 | bool should_throttle = false; | |
8082510e YZ |
4519 | |
4520 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4521 | |
28ed1345 CM |
4522 | /* |
4523 | * for non-free space inodes and ref cows, we want to back off from | |
4524 | * time to time | |
4525 | */ | |
70ddc553 | 4526 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 | 4527 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
897ca819 | 4528 | be_nice = true; |
28ed1345 | 4529 | |
0eb0e19c MF |
4530 | path = btrfs_alloc_path(); |
4531 | if (!path) | |
4532 | return -ENOMEM; | |
e4058b54 | 4533 | path->reada = READA_BACK; |
0eb0e19c | 4534 | |
5dc562c5 JB |
4535 | /* |
4536 | * We want to drop from the next block forward in case this new size is | |
4537 | * not block aligned since we will be keeping the last block of the | |
4538 | * extent just the way it is. | |
4539 | */ | |
27cdeb70 | 4540 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4541 | root == fs_info->tree_root) |
dcdbc059 | 4542 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4543 | fs_info->sectorsize), |
da17066c | 4544 | (u64)-1, 0); |
8082510e | 4545 | |
16cdcec7 MX |
4546 | /* |
4547 | * This function is also used to drop the items in the log tree before | |
4548 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
52042d8e | 4549 | * it is used to drop the logged items. So we shouldn't kill the delayed |
16cdcec7 MX |
4550 | * items. |
4551 | */ | |
4552 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4553 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4554 | |
33345d01 | 4555 | key.objectid = ino; |
39279cc3 | 4556 | key.offset = (u64)-1; |
5f39d397 CM |
4557 | key.type = (u8)-1; |
4558 | ||
85e21bac | 4559 | search_again: |
28ed1345 CM |
4560 | /* |
4561 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4562 | * up a huge file in a single leaf. Most of the time that | |
4563 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4564 | */ | |
fd86a3a3 OS |
4565 | if (be_nice && bytes_deleted > SZ_32M && |
4566 | btrfs_should_end_transaction(trans)) { | |
4567 | ret = -EAGAIN; | |
4568 | goto out; | |
28ed1345 CM |
4569 | } |
4570 | ||
b9473439 | 4571 | path->leave_spinning = 1; |
85e21bac | 4572 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
fd86a3a3 | 4573 | if (ret < 0) |
8082510e | 4574 | goto out; |
d397712b | 4575 | |
85e21bac | 4576 | if (ret > 0) { |
fd86a3a3 | 4577 | ret = 0; |
e02119d5 CM |
4578 | /* there are no items in the tree for us to truncate, we're |
4579 | * done | |
4580 | */ | |
8082510e YZ |
4581 | if (path->slots[0] == 0) |
4582 | goto out; | |
85e21bac CM |
4583 | path->slots[0]--; |
4584 | } | |
4585 | ||
d397712b | 4586 | while (1) { |
39279cc3 | 4587 | fi = NULL; |
5f39d397 CM |
4588 | leaf = path->nodes[0]; |
4589 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4590 | found_type = found_key.type; |
39279cc3 | 4591 | |
33345d01 | 4592 | if (found_key.objectid != ino) |
39279cc3 | 4593 | break; |
5f39d397 | 4594 | |
85e21bac | 4595 | if (found_type < min_type) |
39279cc3 CM |
4596 | break; |
4597 | ||
5f39d397 | 4598 | item_end = found_key.offset; |
39279cc3 | 4599 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4600 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4601 | struct btrfs_file_extent_item); |
179e29e4 CM |
4602 | extent_type = btrfs_file_extent_type(leaf, fi); |
4603 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4604 | item_end += |
db94535d | 4605 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4606 | |
4607 | trace_btrfs_truncate_show_fi_regular( | |
4608 | BTRFS_I(inode), leaf, fi, | |
4609 | found_key.offset); | |
179e29e4 | 4610 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 QW |
4611 | item_end += btrfs_file_extent_ram_bytes(leaf, |
4612 | fi); | |
09ed2f16 LB |
4613 | |
4614 | trace_btrfs_truncate_show_fi_inline( | |
4615 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4616 | found_key.offset); | |
39279cc3 | 4617 | } |
008630c1 | 4618 | item_end--; |
39279cc3 | 4619 | } |
8082510e YZ |
4620 | if (found_type > min_type) { |
4621 | del_item = 1; | |
4622 | } else { | |
76b42abb | 4623 | if (item_end < new_size) |
b888db2b | 4624 | break; |
8082510e YZ |
4625 | if (found_key.offset >= new_size) |
4626 | del_item = 1; | |
4627 | else | |
4628 | del_item = 0; | |
39279cc3 | 4629 | } |
39279cc3 | 4630 | found_extent = 0; |
39279cc3 | 4631 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4632 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4633 | goto delete; | |
4634 | ||
4635 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4636 | u64 num_dec; |
db94535d | 4637 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4638 | if (!del_item) { |
db94535d CM |
4639 | u64 orig_num_bytes = |
4640 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4641 | extent_num_bytes = ALIGN(new_size - |
4642 | found_key.offset, | |
0b246afa | 4643 | fs_info->sectorsize); |
db94535d CM |
4644 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4645 | extent_num_bytes); | |
4646 | num_dec = (orig_num_bytes - | |
9069218d | 4647 | extent_num_bytes); |
27cdeb70 MX |
4648 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4649 | &root->state) && | |
4650 | extent_start != 0) | |
a76a3cd4 | 4651 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4652 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4653 | } else { |
db94535d CM |
4654 | extent_num_bytes = |
4655 | btrfs_file_extent_disk_num_bytes(leaf, | |
4656 | fi); | |
5d4f98a2 YZ |
4657 | extent_offset = found_key.offset - |
4658 | btrfs_file_extent_offset(leaf, fi); | |
4659 | ||
39279cc3 | 4660 | /* FIXME blocksize != 4096 */ |
9069218d | 4661 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4662 | if (extent_start != 0) { |
4663 | found_extent = 1; | |
27cdeb70 MX |
4664 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4665 | &root->state)) | |
a76a3cd4 | 4666 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4667 | } |
39279cc3 | 4668 | } |
9069218d | 4669 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4670 | /* |
4671 | * we can't truncate inline items that have had | |
4672 | * special encodings | |
4673 | */ | |
4674 | if (!del_item && | |
c8b97818 | 4675 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4676 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4677 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4678 | u32 size = (u32)(new_size - found_key.offset); | |
4679 | ||
4680 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4681 | size = btrfs_file_extent_calc_inline_size(size); | |
4682 | btrfs_truncate_item(root->fs_info, path, size, 1); | |
4683 | } else if (!del_item) { | |
514ac8ad | 4684 | /* |
ddfae63c JB |
4685 | * We have to bail so the last_size is set to |
4686 | * just before this extent. | |
514ac8ad | 4687 | */ |
fd86a3a3 | 4688 | ret = NEED_TRUNCATE_BLOCK; |
ddfae63c JB |
4689 | break; |
4690 | } | |
0305cd5f | 4691 | |
ddfae63c | 4692 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
0305cd5f | 4693 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4694 | } |
179e29e4 | 4695 | delete: |
ddfae63c JB |
4696 | if (del_item) |
4697 | last_size = found_key.offset; | |
4698 | else | |
4699 | last_size = new_size; | |
39279cc3 | 4700 | if (del_item) { |
85e21bac CM |
4701 | if (!pending_del_nr) { |
4702 | /* no pending yet, add ourselves */ | |
4703 | pending_del_slot = path->slots[0]; | |
4704 | pending_del_nr = 1; | |
4705 | } else if (pending_del_nr && | |
4706 | path->slots[0] + 1 == pending_del_slot) { | |
4707 | /* hop on the pending chunk */ | |
4708 | pending_del_nr++; | |
4709 | pending_del_slot = path->slots[0]; | |
4710 | } else { | |
d397712b | 4711 | BUG(); |
85e21bac | 4712 | } |
39279cc3 CM |
4713 | } else { |
4714 | break; | |
4715 | } | |
897ca819 | 4716 | should_throttle = false; |
28f75a0e | 4717 | |
27cdeb70 MX |
4718 | if (found_extent && |
4719 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4720 | root == fs_info->tree_root)) { |
b9473439 | 4721 | btrfs_set_path_blocking(path); |
28ed1345 | 4722 | bytes_deleted += extent_num_bytes; |
84f7d8e6 | 4723 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4724 | extent_num_bytes, 0, |
4725 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4726 | ino, extent_offset); |
05522109 OS |
4727 | if (ret) { |
4728 | btrfs_abort_transaction(trans, ret); | |
4729 | break; | |
4730 | } | |
28f75a0e | 4731 | if (be_nice) { |
7c861627 | 4732 | if (btrfs_should_throttle_delayed_refs(trans)) |
897ca819 | 4733 | should_throttle = true; |
28f75a0e | 4734 | } |
39279cc3 | 4735 | } |
85e21bac | 4736 | |
8082510e YZ |
4737 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4738 | break; | |
4739 | ||
4740 | if (path->slots[0] == 0 || | |
1262133b | 4741 | path->slots[0] != pending_del_slot || |
28bad212 | 4742 | should_throttle) { |
8082510e YZ |
4743 | if (pending_del_nr) { |
4744 | ret = btrfs_del_items(trans, root, path, | |
4745 | pending_del_slot, | |
4746 | pending_del_nr); | |
79787eaa | 4747 | if (ret) { |
66642832 | 4748 | btrfs_abort_transaction(trans, ret); |
fd86a3a3 | 4749 | break; |
79787eaa | 4750 | } |
8082510e YZ |
4751 | pending_del_nr = 0; |
4752 | } | |
b3b4aa74 | 4753 | btrfs_release_path(path); |
28bad212 | 4754 | |
28f75a0e | 4755 | /* |
28bad212 JB |
4756 | * We can generate a lot of delayed refs, so we need to |
4757 | * throttle every once and a while and make sure we're | |
4758 | * adding enough space to keep up with the work we are | |
4759 | * generating. Since we hold a transaction here we | |
4760 | * can't flush, and we don't want to FLUSH_LIMIT because | |
4761 | * we could have generated too many delayed refs to | |
4762 | * actually allocate, so just bail if we're short and | |
4763 | * let the normal reservation dance happen higher up. | |
28f75a0e | 4764 | */ |
28bad212 JB |
4765 | if (should_throttle) { |
4766 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
4767 | BTRFS_RESERVE_NO_FLUSH); | |
4768 | if (ret) { | |
4769 | ret = -EAGAIN; | |
4770 | break; | |
4771 | } | |
28f75a0e | 4772 | } |
85e21bac | 4773 | goto search_again; |
8082510e YZ |
4774 | } else { |
4775 | path->slots[0]--; | |
85e21bac | 4776 | } |
39279cc3 | 4777 | } |
8082510e | 4778 | out: |
fd86a3a3 OS |
4779 | if (ret >= 0 && pending_del_nr) { |
4780 | int err; | |
4781 | ||
4782 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
85e21bac | 4783 | pending_del_nr); |
fd86a3a3 OS |
4784 | if (err) { |
4785 | btrfs_abort_transaction(trans, err); | |
4786 | ret = err; | |
4787 | } | |
85e21bac | 4788 | } |
76b42abb FM |
4789 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4790 | ASSERT(last_size >= new_size); | |
fd86a3a3 | 4791 | if (!ret && last_size > new_size) |
76b42abb | 4792 | last_size = new_size; |
7f4f6e0a | 4793 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4794 | } |
28ed1345 | 4795 | |
39279cc3 | 4796 | btrfs_free_path(path); |
fd86a3a3 | 4797 | return ret; |
39279cc3 CM |
4798 | } |
4799 | ||
4800 | /* | |
9703fefe | 4801 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4802 | * @inode - inode that we're zeroing |
4803 | * @from - the offset to start zeroing | |
4804 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4805 | * offset | |
4806 | * @front - zero up to the offset instead of from the offset on | |
4807 | * | |
9703fefe | 4808 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4809 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4810 | */ |
9703fefe | 4811 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4812 | int front) |
39279cc3 | 4813 | { |
0b246afa | 4814 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4815 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4816 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4817 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4818 | struct extent_state *cached_state = NULL; |
364ecf36 | 4819 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4820 | char *kaddr; |
0b246afa | 4821 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4822 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4823 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4824 | struct page *page; |
3b16a4e3 | 4825 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4826 | int ret = 0; |
9703fefe CR |
4827 | u64 block_start; |
4828 | u64 block_end; | |
39279cc3 | 4829 | |
b03ebd99 NB |
4830 | if (IS_ALIGNED(offset, blocksize) && |
4831 | (!len || IS_ALIGNED(len, blocksize))) | |
39279cc3 | 4832 | goto out; |
9703fefe | 4833 | |
8b62f87b JB |
4834 | block_start = round_down(from, blocksize); |
4835 | block_end = block_start + blocksize - 1; | |
4836 | ||
364ecf36 | 4837 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8b62f87b | 4838 | block_start, blocksize); |
5d5e103a JB |
4839 | if (ret) |
4840 | goto out; | |
39279cc3 | 4841 | |
211c17f5 | 4842 | again: |
3b16a4e3 | 4843 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4844 | if (!page) { |
bc42bda2 | 4845 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 QW |
4846 | block_start, blocksize, true); |
4847 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true); | |
ac6a2b36 | 4848 | ret = -ENOMEM; |
39279cc3 | 4849 | goto out; |
5d5e103a | 4850 | } |
e6dcd2dc | 4851 | |
39279cc3 | 4852 | if (!PageUptodate(page)) { |
9ebefb18 | 4853 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4854 | lock_page(page); |
211c17f5 CM |
4855 | if (page->mapping != mapping) { |
4856 | unlock_page(page); | |
09cbfeaf | 4857 | put_page(page); |
211c17f5 CM |
4858 | goto again; |
4859 | } | |
39279cc3 CM |
4860 | if (!PageUptodate(page)) { |
4861 | ret = -EIO; | |
89642229 | 4862 | goto out_unlock; |
39279cc3 CM |
4863 | } |
4864 | } | |
211c17f5 | 4865 | wait_on_page_writeback(page); |
e6dcd2dc | 4866 | |
9703fefe | 4867 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4868 | set_page_extent_mapped(page); |
4869 | ||
9703fefe | 4870 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4871 | if (ordered) { |
9703fefe | 4872 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4873 | &cached_state); |
e6dcd2dc | 4874 | unlock_page(page); |
09cbfeaf | 4875 | put_page(page); |
eb84ae03 | 4876 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4877 | btrfs_put_ordered_extent(ordered); |
4878 | goto again; | |
4879 | } | |
4880 | ||
9703fefe | 4881 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4882 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4883 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
ae0f1625 | 4884 | 0, 0, &cached_state); |
5d5e103a | 4885 | |
e3b8a485 | 4886 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
ba8b04c1 | 4887 | &cached_state, 0); |
9ed74f2d | 4888 | if (ret) { |
9703fefe | 4889 | unlock_extent_cached(io_tree, block_start, block_end, |
e43bbe5e | 4890 | &cached_state); |
9ed74f2d JB |
4891 | goto out_unlock; |
4892 | } | |
4893 | ||
9703fefe | 4894 | if (offset != blocksize) { |
2aaa6655 | 4895 | if (!len) |
9703fefe | 4896 | len = blocksize - offset; |
e6dcd2dc | 4897 | kaddr = kmap(page); |
2aaa6655 | 4898 | if (front) |
9703fefe CR |
4899 | memset(kaddr + (block_start - page_offset(page)), |
4900 | 0, offset); | |
2aaa6655 | 4901 | else |
9703fefe CR |
4902 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4903 | 0, len); | |
e6dcd2dc CM |
4904 | flush_dcache_page(page); |
4905 | kunmap(page); | |
4906 | } | |
247e743c | 4907 | ClearPageChecked(page); |
e6dcd2dc | 4908 | set_page_dirty(page); |
e43bbe5e | 4909 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
39279cc3 | 4910 | |
89642229 | 4911 | out_unlock: |
5d5e103a | 4912 | if (ret) |
bc42bda2 | 4913 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
43b18595 QW |
4914 | blocksize, true); |
4915 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0)); | |
39279cc3 | 4916 | unlock_page(page); |
09cbfeaf | 4917 | put_page(page); |
39279cc3 | 4918 | out: |
364ecf36 | 4919 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4920 | return ret; |
4921 | } | |
4922 | ||
16e7549f JB |
4923 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4924 | u64 offset, u64 len) | |
4925 | { | |
0b246afa | 4926 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4927 | struct btrfs_trans_handle *trans; |
4928 | int ret; | |
4929 | ||
4930 | /* | |
4931 | * Still need to make sure the inode looks like it's been updated so | |
4932 | * that any holes get logged if we fsync. | |
4933 | */ | |
0b246afa JM |
4934 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4935 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4936 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4937 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4938 | return 0; | |
4939 | } | |
4940 | ||
4941 | /* | |
4942 | * 1 - for the one we're dropping | |
4943 | * 1 - for the one we're adding | |
4944 | * 1 - for updating the inode. | |
4945 | */ | |
4946 | trans = btrfs_start_transaction(root, 3); | |
4947 | if (IS_ERR(trans)) | |
4948 | return PTR_ERR(trans); | |
4949 | ||
4950 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4951 | if (ret) { | |
66642832 | 4952 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4953 | btrfs_end_transaction(trans); |
16e7549f JB |
4954 | return ret; |
4955 | } | |
4956 | ||
f85b7379 DS |
4957 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4958 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4959 | if (ret) |
66642832 | 4960 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4961 | else |
4962 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4963 | btrfs_end_transaction(trans); |
16e7549f JB |
4964 | return ret; |
4965 | } | |
4966 | ||
695a0d0d JB |
4967 | /* |
4968 | * This function puts in dummy file extents for the area we're creating a hole | |
4969 | * for. So if we are truncating this file to a larger size we need to insert | |
4970 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4971 | * the range between oldsize and size | |
4972 | */ | |
a41ad394 | 4973 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4974 | { |
0b246afa | 4975 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4976 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4977 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4978 | struct extent_map *em = NULL; |
2ac55d41 | 4979 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4980 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4981 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4982 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4983 | u64 last_byte; |
4984 | u64 cur_offset; | |
4985 | u64 hole_size; | |
9ed74f2d | 4986 | int err = 0; |
39279cc3 | 4987 | |
a71754fc | 4988 | /* |
9703fefe CR |
4989 | * If our size started in the middle of a block we need to zero out the |
4990 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4991 | * expose stale data. |
4992 | */ | |
9703fefe | 4993 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4994 | if (err) |
4995 | return err; | |
4996 | ||
9036c102 YZ |
4997 | if (size <= hole_start) |
4998 | return 0; | |
4999 | ||
9036c102 YZ |
5000 | while (1) { |
5001 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 5002 | |
ff13db41 | 5003 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 5004 | &cached_state); |
a776c6fa | 5005 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
fa7c1494 | 5006 | block_end - hole_start); |
9036c102 YZ |
5007 | if (!ordered) |
5008 | break; | |
2ac55d41 | 5009 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
e43bbe5e | 5010 | &cached_state); |
fa7c1494 | 5011 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
5012 | btrfs_put_ordered_extent(ordered); |
5013 | } | |
39279cc3 | 5014 | |
9036c102 YZ |
5015 | cur_offset = hole_start; |
5016 | while (1) { | |
fc4f21b1 | 5017 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 5018 | block_end - cur_offset, 0); |
79787eaa JM |
5019 | if (IS_ERR(em)) { |
5020 | err = PTR_ERR(em); | |
f2767956 | 5021 | em = NULL; |
79787eaa JM |
5022 | break; |
5023 | } | |
9036c102 | 5024 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 5025 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 5026 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 5027 | struct extent_map *hole_em; |
9036c102 | 5028 | hole_size = last_byte - cur_offset; |
9ed74f2d | 5029 | |
16e7549f JB |
5030 | err = maybe_insert_hole(root, inode, cur_offset, |
5031 | hole_size); | |
5032 | if (err) | |
3893e33b | 5033 | break; |
dcdbc059 | 5034 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
5035 | cur_offset + hole_size - 1, 0); |
5036 | hole_em = alloc_extent_map(); | |
5037 | if (!hole_em) { | |
5038 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
5039 | &BTRFS_I(inode)->runtime_flags); | |
5040 | goto next; | |
5041 | } | |
5042 | hole_em->start = cur_offset; | |
5043 | hole_em->len = hole_size; | |
5044 | hole_em->orig_start = cur_offset; | |
8082510e | 5045 | |
5dc562c5 JB |
5046 | hole_em->block_start = EXTENT_MAP_HOLE; |
5047 | hole_em->block_len = 0; | |
b4939680 | 5048 | hole_em->orig_block_len = 0; |
cc95bef6 | 5049 | hole_em->ram_bytes = hole_size; |
0b246afa | 5050 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 5051 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 5052 | hole_em->generation = fs_info->generation; |
8082510e | 5053 | |
5dc562c5 JB |
5054 | while (1) { |
5055 | write_lock(&em_tree->lock); | |
09a2a8f9 | 5056 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
5057 | write_unlock(&em_tree->lock); |
5058 | if (err != -EEXIST) | |
5059 | break; | |
dcdbc059 NB |
5060 | btrfs_drop_extent_cache(BTRFS_I(inode), |
5061 | cur_offset, | |
5dc562c5 JB |
5062 | cur_offset + |
5063 | hole_size - 1, 0); | |
5064 | } | |
5065 | free_extent_map(hole_em); | |
9036c102 | 5066 | } |
16e7549f | 5067 | next: |
9036c102 | 5068 | free_extent_map(em); |
a22285a6 | 5069 | em = NULL; |
9036c102 | 5070 | cur_offset = last_byte; |
8082510e | 5071 | if (cur_offset >= block_end) |
9036c102 YZ |
5072 | break; |
5073 | } | |
a22285a6 | 5074 | free_extent_map(em); |
e43bbe5e | 5075 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
9036c102 YZ |
5076 | return err; |
5077 | } | |
39279cc3 | 5078 | |
3972f260 | 5079 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5080 | { |
f4a2f4c5 MX |
5081 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5082 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5083 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5084 | loff_t newsize = attr->ia_size; |
5085 | int mask = attr->ia_valid; | |
8082510e YZ |
5086 | int ret; |
5087 | ||
3972f260 ES |
5088 | /* |
5089 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5090 | * special case where we need to update the times despite not having | |
5091 | * these flags set. For all other operations the VFS set these flags | |
5092 | * explicitly if it wants a timestamp update. | |
5093 | */ | |
dff6efc3 CH |
5094 | if (newsize != oldsize) { |
5095 | inode_inc_iversion(inode); | |
5096 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5097 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5098 | current_time(inode); |
dff6efc3 | 5099 | } |
3972f260 | 5100 | |
a41ad394 | 5101 | if (newsize > oldsize) { |
9ea24bbe | 5102 | /* |
ea14b57f | 5103 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5104 | * This is to ensure the snapshot captures a fully consistent |
5105 | * state of this file - if the snapshot captures this expanding | |
5106 | * truncation, it must capture all writes that happened before | |
5107 | * this truncation. | |
5108 | */ | |
0bc19f90 | 5109 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5110 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 5111 | if (ret) { |
ea14b57f | 5112 | btrfs_end_write_no_snapshotting(root); |
8082510e | 5113 | return ret; |
9ea24bbe | 5114 | } |
8082510e | 5115 | |
f4a2f4c5 | 5116 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5117 | if (IS_ERR(trans)) { |
ea14b57f | 5118 | btrfs_end_write_no_snapshotting(root); |
f4a2f4c5 | 5119 | return PTR_ERR(trans); |
9ea24bbe | 5120 | } |
f4a2f4c5 MX |
5121 | |
5122 | i_size_write(inode, newsize); | |
5123 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5124 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5125 | ret = btrfs_update_inode(trans, root, inode); |
ea14b57f | 5126 | btrfs_end_write_no_snapshotting(root); |
3a45bb20 | 5127 | btrfs_end_transaction(trans); |
a41ad394 | 5128 | } else { |
8082510e | 5129 | |
a41ad394 JB |
5130 | /* |
5131 | * We're truncating a file that used to have good data down to | |
5132 | * zero. Make sure it gets into the ordered flush list so that | |
5133 | * any new writes get down to disk quickly. | |
5134 | */ | |
5135 | if (newsize == 0) | |
72ac3c0d JB |
5136 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5137 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5138 | |
a41ad394 | 5139 | truncate_setsize(inode, newsize); |
2e60a51e | 5140 | |
52042d8e | 5141 | /* Disable nonlocked read DIO to avoid the endless truncate */ |
abcefb1e | 5142 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5143 | inode_dio_wait(inode); |
0b581701 | 5144 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5145 | |
213e8c55 | 5146 | ret = btrfs_truncate(inode, newsize == oldsize); |
7f4f6e0a JB |
5147 | if (ret && inode->i_nlink) { |
5148 | int err; | |
5149 | ||
5150 | /* | |
f7e9e8fc OS |
5151 | * Truncate failed, so fix up the in-memory size. We |
5152 | * adjusted disk_i_size down as we removed extents, so | |
5153 | * wait for disk_i_size to be stable and then update the | |
5154 | * in-memory size to match. | |
7f4f6e0a | 5155 | */ |
f7e9e8fc | 5156 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
7f4f6e0a | 5157 | if (err) |
f7e9e8fc OS |
5158 | return err; |
5159 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
7f4f6e0a | 5160 | } |
8082510e YZ |
5161 | } |
5162 | ||
a41ad394 | 5163 | return ret; |
8082510e YZ |
5164 | } |
5165 | ||
9036c102 YZ |
5166 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5167 | { | |
2b0143b5 | 5168 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5169 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5170 | int err; |
39279cc3 | 5171 | |
b83cc969 LZ |
5172 | if (btrfs_root_readonly(root)) |
5173 | return -EROFS; | |
5174 | ||
31051c85 | 5175 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5176 | if (err) |
5177 | return err; | |
2bf5a725 | 5178 | |
5a3f23d5 | 5179 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5180 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5181 | if (err) |
5182 | return err; | |
39279cc3 | 5183 | } |
9036c102 | 5184 | |
1025774c CH |
5185 | if (attr->ia_valid) { |
5186 | setattr_copy(inode, attr); | |
0c4d2d95 | 5187 | inode_inc_iversion(inode); |
22c44fe6 | 5188 | err = btrfs_dirty_inode(inode); |
1025774c | 5189 | |
22c44fe6 | 5190 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5191 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5192 | } |
33268eaf | 5193 | |
39279cc3 CM |
5194 | return err; |
5195 | } | |
61295eb8 | 5196 | |
131e404a FDBM |
5197 | /* |
5198 | * While truncating the inode pages during eviction, we get the VFS calling | |
5199 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5200 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5201 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5202 | * extent_state structures over and over, wasting lots of time. | |
5203 | * | |
5204 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5205 | * those expensive operations on a per page basis and do only the ordered io | |
5206 | * finishing, while we release here the extent_map and extent_state structures, | |
5207 | * without the excessive merging and splitting. | |
5208 | */ | |
5209 | static void evict_inode_truncate_pages(struct inode *inode) | |
5210 | { | |
5211 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5212 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5213 | struct rb_node *node; | |
5214 | ||
5215 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5216 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5217 | |
5218 | write_lock(&map_tree->lock); | |
07e1ce09 | 5219 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
131e404a FDBM |
5220 | struct extent_map *em; |
5221 | ||
07e1ce09 | 5222 | node = rb_first_cached(&map_tree->map); |
131e404a | 5223 | em = rb_entry(node, struct extent_map, rb_node); |
180589ef WS |
5224 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5225 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5226 | remove_extent_mapping(map_tree, em); |
5227 | free_extent_map(em); | |
7064dd5c FM |
5228 | if (need_resched()) { |
5229 | write_unlock(&map_tree->lock); | |
5230 | cond_resched(); | |
5231 | write_lock(&map_tree->lock); | |
5232 | } | |
131e404a FDBM |
5233 | } |
5234 | write_unlock(&map_tree->lock); | |
5235 | ||
6ca07097 FM |
5236 | /* |
5237 | * Keep looping until we have no more ranges in the io tree. | |
5238 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5239 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5240 | * still in progress (unlocked the pages in the bio but did not yet | |
5241 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5242 | * ranges can still be locked and eviction started because before |
5243 | * submitting those bios, which are executed by a separate task (work | |
5244 | * queue kthread), inode references (inode->i_count) were not taken | |
5245 | * (which would be dropped in the end io callback of each bio). | |
5246 | * Therefore here we effectively end up waiting for those bios and | |
5247 | * anyone else holding locked ranges without having bumped the inode's | |
5248 | * reference count - if we don't do it, when they access the inode's | |
5249 | * io_tree to unlock a range it may be too late, leading to an | |
5250 | * use-after-free issue. | |
5251 | */ | |
131e404a FDBM |
5252 | spin_lock(&io_tree->lock); |
5253 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5254 | struct extent_state *state; | |
5255 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5256 | u64 start; |
5257 | u64 end; | |
421f0922 | 5258 | unsigned state_flags; |
131e404a FDBM |
5259 | |
5260 | node = rb_first(&io_tree->state); | |
5261 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5262 | start = state->start; |
5263 | end = state->end; | |
421f0922 | 5264 | state_flags = state->state; |
131e404a FDBM |
5265 | spin_unlock(&io_tree->lock); |
5266 | ||
ff13db41 | 5267 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5268 | |
5269 | /* | |
5270 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5271 | * and its reserved space won't be freed by delayed_ref. | |
5272 | * So we need to free its reserved space here. | |
5273 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5274 | * | |
5275 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5276 | */ | |
421f0922 | 5277 | if (state_flags & EXTENT_DELALLOC) |
bc42bda2 | 5278 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
b9d0b389 | 5279 | |
6ca07097 | 5280 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5281 | EXTENT_LOCKED | EXTENT_DIRTY | |
5282 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
ae0f1625 | 5283 | EXTENT_DEFRAG, 1, 1, &cached_state); |
131e404a | 5284 | |
7064dd5c | 5285 | cond_resched(); |
131e404a FDBM |
5286 | spin_lock(&io_tree->lock); |
5287 | } | |
5288 | spin_unlock(&io_tree->lock); | |
5289 | } | |
5290 | ||
4b9d7b59 | 5291 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
ad80cf50 | 5292 | struct btrfs_block_rsv *rsv) |
4b9d7b59 OS |
5293 | { |
5294 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5295 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
260e7702 | 5296 | u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1); |
4b9d7b59 OS |
5297 | int failures = 0; |
5298 | ||
5299 | for (;;) { | |
5300 | struct btrfs_trans_handle *trans; | |
5301 | int ret; | |
5302 | ||
260e7702 JB |
5303 | ret = btrfs_block_rsv_refill(root, rsv, |
5304 | rsv->size + delayed_refs_extra, | |
4b9d7b59 OS |
5305 | BTRFS_RESERVE_FLUSH_LIMIT); |
5306 | ||
5307 | if (ret && ++failures > 2) { | |
5308 | btrfs_warn(fs_info, | |
5309 | "could not allocate space for a delete; will truncate on mount"); | |
5310 | return ERR_PTR(-ENOSPC); | |
5311 | } | |
5312 | ||
260e7702 JB |
5313 | /* |
5314 | * Evict can generate a large amount of delayed refs without | |
5315 | * having a way to add space back since we exhaust our temporary | |
5316 | * block rsv. We aren't allowed to do FLUSH_ALL in this case | |
5317 | * because we could deadlock with so many things in the flushing | |
5318 | * code, so we have to try and hold some extra space to | |
5319 | * compensate for our delayed ref generation. If we can't get | |
5320 | * that space then we need see if we can steal our minimum from | |
5321 | * the global reserve. We will be ratelimited by the amount of | |
5322 | * space we have for the delayed refs rsv, so we'll end up | |
5323 | * committing and trying again. | |
5324 | */ | |
4b9d7b59 | 5325 | trans = btrfs_join_transaction(root); |
260e7702 JB |
5326 | if (IS_ERR(trans) || !ret) { |
5327 | if (!IS_ERR(trans)) { | |
5328 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5329 | trans->bytes_reserved = delayed_refs_extra; | |
5330 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, | |
5331 | delayed_refs_extra, 1); | |
5332 | } | |
4b9d7b59 | 5333 | return trans; |
260e7702 | 5334 | } |
4b9d7b59 OS |
5335 | |
5336 | /* | |
5337 | * Try to steal from the global reserve if there is space for | |
5338 | * it. | |
5339 | */ | |
64403612 JB |
5340 | if (!btrfs_check_space_for_delayed_refs(fs_info) && |
5341 | !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) | |
4b9d7b59 OS |
5342 | return trans; |
5343 | ||
5344 | /* If not, commit and try again. */ | |
5345 | ret = btrfs_commit_transaction(trans); | |
5346 | if (ret) | |
5347 | return ERR_PTR(ret); | |
5348 | } | |
5349 | } | |
5350 | ||
bd555975 | 5351 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5352 | { |
0b246afa | 5353 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5354 | struct btrfs_trans_handle *trans; |
5355 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b9d7b59 | 5356 | struct btrfs_block_rsv *rsv; |
39279cc3 CM |
5357 | int ret; |
5358 | ||
1abe9b8a | 5359 | trace_btrfs_inode_evict(inode); |
5360 | ||
3d48d981 | 5361 | if (!root) { |
e8f1bc14 | 5362 | clear_inode(inode); |
3d48d981 NB |
5363 | return; |
5364 | } | |
5365 | ||
131e404a FDBM |
5366 | evict_inode_truncate_pages(inode); |
5367 | ||
69e9c6c6 SB |
5368 | if (inode->i_nlink && |
5369 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5370 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5371 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5372 | goto no_delete; |
5373 | ||
27919067 | 5374 | if (is_bad_inode(inode)) |
39279cc3 | 5375 | goto no_delete; |
5f39d397 | 5376 | |
7ab7956e | 5377 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5378 | |
7b40b695 | 5379 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
c71bf099 | 5380 | goto no_delete; |
c71bf099 | 5381 | |
76dda93c | 5382 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5383 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5384 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5385 | goto no_delete; |
5386 | } | |
5387 | ||
aa79021f | 5388 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
27919067 | 5389 | if (ret) |
0e8c36a9 | 5390 | goto no_delete; |
0e8c36a9 | 5391 | |
2ff7e61e | 5392 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
27919067 | 5393 | if (!rsv) |
4289a667 | 5394 | goto no_delete; |
ad80cf50 | 5395 | rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
ca7e70f5 | 5396 | rsv->failfast = 1; |
4289a667 | 5397 | |
6ef06d27 | 5398 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5399 | |
8082510e | 5400 | while (1) { |
ad80cf50 | 5401 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5402 | if (IS_ERR(trans)) |
5403 | goto free_rsv; | |
7b128766 | 5404 | |
4289a667 JB |
5405 | trans->block_rsv = rsv; |
5406 | ||
d68fc57b | 5407 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
27919067 OS |
5408 | trans->block_rsv = &fs_info->trans_block_rsv; |
5409 | btrfs_end_transaction(trans); | |
5410 | btrfs_btree_balance_dirty(fs_info); | |
5411 | if (ret && ret != -ENOSPC && ret != -EAGAIN) | |
5412 | goto free_rsv; | |
5413 | else if (!ret) | |
8082510e | 5414 | break; |
8082510e | 5415 | } |
5f39d397 | 5416 | |
4ef31a45 | 5417 | /* |
27919067 OS |
5418 | * Errors here aren't a big deal, it just means we leave orphan items in |
5419 | * the tree. They will be cleaned up on the next mount. If the inode | |
5420 | * number gets reused, cleanup deletes the orphan item without doing | |
5421 | * anything, and unlink reuses the existing orphan item. | |
5422 | * | |
5423 | * If it turns out that we are dropping too many of these, we might want | |
5424 | * to add a mechanism for retrying these after a commit. | |
4ef31a45 | 5425 | */ |
ad80cf50 | 5426 | trans = evict_refill_and_join(root, rsv); |
27919067 OS |
5427 | if (!IS_ERR(trans)) { |
5428 | trans->block_rsv = rsv; | |
5429 | btrfs_orphan_del(trans, BTRFS_I(inode)); | |
5430 | trans->block_rsv = &fs_info->trans_block_rsv; | |
5431 | btrfs_end_transaction(trans); | |
5432 | } | |
54aa1f4d | 5433 | |
0b246afa | 5434 | if (!(root == fs_info->tree_root || |
581bb050 | 5435 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5436 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5437 | |
27919067 OS |
5438 | free_rsv: |
5439 | btrfs_free_block_rsv(fs_info, rsv); | |
39279cc3 | 5440 | no_delete: |
27919067 OS |
5441 | /* |
5442 | * If we didn't successfully delete, the orphan item will still be in | |
5443 | * the tree and we'll retry on the next mount. Again, we might also want | |
5444 | * to retry these periodically in the future. | |
5445 | */ | |
f48d1cf5 | 5446 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5447 | clear_inode(inode); |
39279cc3 CM |
5448 | } |
5449 | ||
5450 | /* | |
5451 | * this returns the key found in the dir entry in the location pointer. | |
005d6712 SY |
5452 | * If no dir entries were found, returns -ENOENT. |
5453 | * If found a corrupted location in dir entry, returns -EUCLEAN. | |
39279cc3 CM |
5454 | */ |
5455 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5456 | struct btrfs_key *location) | |
5457 | { | |
5458 | const char *name = dentry->d_name.name; | |
5459 | int namelen = dentry->d_name.len; | |
5460 | struct btrfs_dir_item *di; | |
5461 | struct btrfs_path *path; | |
5462 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5463 | int ret = 0; |
39279cc3 CM |
5464 | |
5465 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5466 | if (!path) |
5467 | return -ENOMEM; | |
3954401f | 5468 | |
f85b7379 DS |
5469 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5470 | name, namelen, 0); | |
3cf5068f LB |
5471 | if (IS_ERR_OR_NULL(di)) { |
5472 | ret = di ? PTR_ERR(di) : -ENOENT; | |
005d6712 SY |
5473 | goto out; |
5474 | } | |
d397712b | 5475 | |
5f39d397 | 5476 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
56a0e706 LB |
5477 | if (location->type != BTRFS_INODE_ITEM_KEY && |
5478 | location->type != BTRFS_ROOT_ITEM_KEY) { | |
005d6712 | 5479 | ret = -EUCLEAN; |
56a0e706 LB |
5480 | btrfs_warn(root->fs_info, |
5481 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", | |
5482 | __func__, name, btrfs_ino(BTRFS_I(dir)), | |
5483 | location->objectid, location->type, location->offset); | |
56a0e706 | 5484 | } |
39279cc3 | 5485 | out: |
39279cc3 CM |
5486 | btrfs_free_path(path); |
5487 | return ret; | |
5488 | } | |
5489 | ||
5490 | /* | |
5491 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5492 | * needs to be changed to reflect the root directory of the tree root. This | |
5493 | * is kind of like crossing a mount point. | |
5494 | */ | |
2ff7e61e | 5495 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5496 | struct inode *dir, |
5497 | struct dentry *dentry, | |
5498 | struct btrfs_key *location, | |
5499 | struct btrfs_root **sub_root) | |
39279cc3 | 5500 | { |
4df27c4d YZ |
5501 | struct btrfs_path *path; |
5502 | struct btrfs_root *new_root; | |
5503 | struct btrfs_root_ref *ref; | |
5504 | struct extent_buffer *leaf; | |
1d4c08e0 | 5505 | struct btrfs_key key; |
4df27c4d YZ |
5506 | int ret; |
5507 | int err = 0; | |
39279cc3 | 5508 | |
4df27c4d YZ |
5509 | path = btrfs_alloc_path(); |
5510 | if (!path) { | |
5511 | err = -ENOMEM; | |
5512 | goto out; | |
5513 | } | |
39279cc3 | 5514 | |
4df27c4d | 5515 | err = -ENOENT; |
1d4c08e0 DS |
5516 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5517 | key.type = BTRFS_ROOT_REF_KEY; | |
5518 | key.offset = location->objectid; | |
5519 | ||
0b246afa | 5520 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5521 | if (ret) { |
5522 | if (ret < 0) | |
5523 | err = ret; | |
5524 | goto out; | |
5525 | } | |
39279cc3 | 5526 | |
4df27c4d YZ |
5527 | leaf = path->nodes[0]; |
5528 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5529 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5530 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5531 | goto out; | |
39279cc3 | 5532 | |
4df27c4d YZ |
5533 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5534 | (unsigned long)(ref + 1), | |
5535 | dentry->d_name.len); | |
5536 | if (ret) | |
5537 | goto out; | |
5538 | ||
b3b4aa74 | 5539 | btrfs_release_path(path); |
4df27c4d | 5540 | |
0b246afa | 5541 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5542 | if (IS_ERR(new_root)) { |
5543 | err = PTR_ERR(new_root); | |
5544 | goto out; | |
5545 | } | |
5546 | ||
4df27c4d YZ |
5547 | *sub_root = new_root; |
5548 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5549 | location->type = BTRFS_INODE_ITEM_KEY; | |
5550 | location->offset = 0; | |
5551 | err = 0; | |
5552 | out: | |
5553 | btrfs_free_path(path); | |
5554 | return err; | |
39279cc3 CM |
5555 | } |
5556 | ||
5d4f98a2 YZ |
5557 | static void inode_tree_add(struct inode *inode) |
5558 | { | |
5559 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5560 | struct btrfs_inode *entry; | |
03e860bd NP |
5561 | struct rb_node **p; |
5562 | struct rb_node *parent; | |
cef21937 | 5563 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5564 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5565 | |
1d3382cb | 5566 | if (inode_unhashed(inode)) |
76dda93c | 5567 | return; |
e1409cef | 5568 | parent = NULL; |
5d4f98a2 | 5569 | spin_lock(&root->inode_lock); |
e1409cef | 5570 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5571 | while (*p) { |
5572 | parent = *p; | |
5573 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5574 | ||
37508515 | 5575 | if (ino < btrfs_ino(entry)) |
03e860bd | 5576 | p = &parent->rb_left; |
37508515 | 5577 | else if (ino > btrfs_ino(entry)) |
03e860bd | 5578 | p = &parent->rb_right; |
5d4f98a2 YZ |
5579 | else { |
5580 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5581 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5582 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5583 | RB_CLEAR_NODE(parent); |
5584 | spin_unlock(&root->inode_lock); | |
cef21937 | 5585 | return; |
5d4f98a2 YZ |
5586 | } |
5587 | } | |
cef21937 FDBM |
5588 | rb_link_node(new, parent, p); |
5589 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5590 | spin_unlock(&root->inode_lock); |
5591 | } | |
5592 | ||
5593 | static void inode_tree_del(struct inode *inode) | |
5594 | { | |
0b246afa | 5595 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5597 | int empty = 0; |
5d4f98a2 | 5598 | |
03e860bd | 5599 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5600 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5601 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5602 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5603 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5604 | } |
03e860bd | 5605 | spin_unlock(&root->inode_lock); |
76dda93c | 5606 | |
69e9c6c6 | 5607 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5608 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5609 | spin_lock(&root->inode_lock); |
5610 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5611 | spin_unlock(&root->inode_lock); | |
5612 | if (empty) | |
5613 | btrfs_add_dead_root(root); | |
5614 | } | |
5615 | } | |
5616 | ||
5d4f98a2 | 5617 | |
e02119d5 CM |
5618 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5619 | { | |
5620 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5621 | inode->i_ino = args->location->objectid; |
5622 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5623 | sizeof(*args->location)); | |
e02119d5 | 5624 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5625 | return 0; |
5626 | } | |
5627 | ||
5628 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5629 | { | |
5630 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5631 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5632 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5633 | } |
5634 | ||
5d4f98a2 | 5635 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5636 | struct btrfs_key *location, |
5d4f98a2 | 5637 | struct btrfs_root *root) |
39279cc3 CM |
5638 | { |
5639 | struct inode *inode; | |
5640 | struct btrfs_iget_args args; | |
90d3e592 | 5641 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5642 | |
90d3e592 | 5643 | args.location = location; |
39279cc3 CM |
5644 | args.root = root; |
5645 | ||
778ba82b | 5646 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5647 | btrfs_init_locked_inode, |
5648 | (void *)&args); | |
5649 | return inode; | |
5650 | } | |
5651 | ||
1a54ef8c BR |
5652 | /* Get an inode object given its location and corresponding root. |
5653 | * Returns in *is_new if the inode was read from disk | |
5654 | */ | |
4222ea71 FM |
5655 | struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location, |
5656 | struct btrfs_root *root, int *new, | |
5657 | struct btrfs_path *path) | |
1a54ef8c BR |
5658 | { |
5659 | struct inode *inode; | |
5660 | ||
90d3e592 | 5661 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5662 | if (!inode) |
5d4f98a2 | 5663 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5664 | |
5665 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5666 | int ret; |
5667 | ||
4222ea71 | 5668 | ret = btrfs_read_locked_inode(inode, path); |
9bc2ceff | 5669 | if (!ret) { |
1748f843 MF |
5670 | inode_tree_add(inode); |
5671 | unlock_new_inode(inode); | |
5672 | if (new) | |
5673 | *new = 1; | |
5674 | } else { | |
f5b3a417 AV |
5675 | iget_failed(inode); |
5676 | /* | |
5677 | * ret > 0 can come from btrfs_search_slot called by | |
5678 | * btrfs_read_locked_inode, this means the inode item | |
5679 | * was not found. | |
5680 | */ | |
5681 | if (ret > 0) | |
5682 | ret = -ENOENT; | |
5683 | inode = ERR_PTR(ret); | |
1748f843 MF |
5684 | } |
5685 | } | |
5686 | ||
1a54ef8c BR |
5687 | return inode; |
5688 | } | |
5689 | ||
4222ea71 FM |
5690 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
5691 | struct btrfs_root *root, int *new) | |
5692 | { | |
5693 | return btrfs_iget_path(s, location, root, new, NULL); | |
5694 | } | |
5695 | ||
4df27c4d YZ |
5696 | static struct inode *new_simple_dir(struct super_block *s, |
5697 | struct btrfs_key *key, | |
5698 | struct btrfs_root *root) | |
5699 | { | |
5700 | struct inode *inode = new_inode(s); | |
5701 | ||
5702 | if (!inode) | |
5703 | return ERR_PTR(-ENOMEM); | |
5704 | ||
4df27c4d YZ |
5705 | BTRFS_I(inode)->root = root; |
5706 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5707 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5708 | |
5709 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5710 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5711 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5712 | inode->i_fop = &simple_dir_operations; |
5713 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5714 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5715 | inode->i_atime = inode->i_mtime; |
5716 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 5717 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
4df27c4d YZ |
5718 | |
5719 | return inode; | |
5720 | } | |
5721 | ||
3de4586c | 5722 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5723 | { |
0b246afa | 5724 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5725 | struct inode *inode; |
4df27c4d | 5726 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5727 | struct btrfs_root *sub_root = root; |
5728 | struct btrfs_key location; | |
76dda93c | 5729 | int index; |
b4aff1f8 | 5730 | int ret = 0; |
39279cc3 CM |
5731 | |
5732 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5733 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5734 | |
39e3c955 | 5735 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5736 | if (ret < 0) |
5737 | return ERR_PTR(ret); | |
5f39d397 | 5738 | |
4df27c4d | 5739 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
73f73415 | 5740 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5741 | return inode; |
5742 | } | |
5743 | ||
0b246afa | 5744 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5745 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5746 | &location, &sub_root); |
5747 | if (ret < 0) { | |
5748 | if (ret != -ENOENT) | |
5749 | inode = ERR_PTR(ret); | |
5750 | else | |
5751 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5752 | } else { | |
73f73415 | 5753 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5754 | } |
0b246afa | 5755 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5756 | |
34d19bad | 5757 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5758 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5759 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5760 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5761 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5762 | if (ret) { |
5763 | iput(inode); | |
66b4ffd1 | 5764 | inode = ERR_PTR(ret); |
01cd3367 | 5765 | } |
c71bf099 YZ |
5766 | } |
5767 | ||
3de4586c CM |
5768 | return inode; |
5769 | } | |
5770 | ||
fe15ce44 | 5771 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5772 | { |
5773 | struct btrfs_root *root; | |
2b0143b5 | 5774 | struct inode *inode = d_inode(dentry); |
76dda93c | 5775 | |
848cce0d | 5776 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5777 | inode = d_inode(dentry->d_parent); |
76dda93c | 5778 | |
848cce0d LZ |
5779 | if (inode) { |
5780 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5781 | if (btrfs_root_refs(&root->root_item) == 0) |
5782 | return 1; | |
848cce0d | 5783 | |
4a0cc7ca | 5784 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5785 | return 1; |
efefb143 | 5786 | } |
76dda93c YZ |
5787 | return 0; |
5788 | } | |
5789 | ||
3de4586c | 5790 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5791 | unsigned int flags) |
3de4586c | 5792 | { |
3837d208 | 5793 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
5662344b | 5794 | |
3837d208 AV |
5795 | if (inode == ERR_PTR(-ENOENT)) |
5796 | inode = NULL; | |
41d28bca | 5797 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5798 | } |
5799 | ||
16cdcec7 | 5800 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5801 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5802 | }; | |
5803 | ||
23b5ec74 JB |
5804 | /* |
5805 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5806 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5807 | * our information into that, and then dir_emit from the buffer. This is | |
5808 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5809 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5810 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5811 | * tree lock. | |
5812 | */ | |
5813 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5814 | { | |
5815 | struct btrfs_file_private *private; | |
5816 | ||
5817 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5818 | if (!private) | |
5819 | return -ENOMEM; | |
5820 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5821 | if (!private->filldir_buf) { | |
5822 | kfree(private); | |
5823 | return -ENOMEM; | |
5824 | } | |
5825 | file->private_data = private; | |
5826 | return 0; | |
5827 | } | |
5828 | ||
5829 | struct dir_entry { | |
5830 | u64 ino; | |
5831 | u64 offset; | |
5832 | unsigned type; | |
5833 | int name_len; | |
5834 | }; | |
5835 | ||
5836 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5837 | { | |
5838 | while (entries--) { | |
5839 | struct dir_entry *entry = addr; | |
5840 | char *name = (char *)(entry + 1); | |
5841 | ||
92d32170 DS |
5842 | ctx->pos = get_unaligned(&entry->offset); |
5843 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), | |
5844 | get_unaligned(&entry->ino), | |
5845 | get_unaligned(&entry->type))) | |
23b5ec74 | 5846 | return 1; |
92d32170 DS |
5847 | addr += sizeof(struct dir_entry) + |
5848 | get_unaligned(&entry->name_len); | |
23b5ec74 JB |
5849 | ctx->pos++; |
5850 | } | |
5851 | return 0; | |
5852 | } | |
5853 | ||
9cdda8d3 | 5854 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5855 | { |
9cdda8d3 | 5856 | struct inode *inode = file_inode(file); |
39279cc3 | 5857 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5858 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5859 | struct btrfs_dir_item *di; |
5860 | struct btrfs_key key; | |
5f39d397 | 5861 | struct btrfs_key found_key; |
39279cc3 | 5862 | struct btrfs_path *path; |
23b5ec74 | 5863 | void *addr; |
16cdcec7 MX |
5864 | struct list_head ins_list; |
5865 | struct list_head del_list; | |
39279cc3 | 5866 | int ret; |
5f39d397 | 5867 | struct extent_buffer *leaf; |
39279cc3 | 5868 | int slot; |
5f39d397 CM |
5869 | char *name_ptr; |
5870 | int name_len; | |
23b5ec74 JB |
5871 | int entries = 0; |
5872 | int total_len = 0; | |
02dbfc99 | 5873 | bool put = false; |
c2951f32 | 5874 | struct btrfs_key location; |
5f39d397 | 5875 | |
9cdda8d3 AV |
5876 | if (!dir_emit_dots(file, ctx)) |
5877 | return 0; | |
5878 | ||
49593bfa | 5879 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5880 | if (!path) |
5881 | return -ENOMEM; | |
ff5714cc | 5882 | |
23b5ec74 | 5883 | addr = private->filldir_buf; |
e4058b54 | 5884 | path->reada = READA_FORWARD; |
49593bfa | 5885 | |
c2951f32 JM |
5886 | INIT_LIST_HEAD(&ins_list); |
5887 | INIT_LIST_HEAD(&del_list); | |
5888 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5889 | |
23b5ec74 | 5890 | again: |
c2951f32 | 5891 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5892 | key.offset = ctx->pos; |
4a0cc7ca | 5893 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5894 | |
39279cc3 CM |
5895 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5896 | if (ret < 0) | |
5897 | goto err; | |
49593bfa DW |
5898 | |
5899 | while (1) { | |
23b5ec74 JB |
5900 | struct dir_entry *entry; |
5901 | ||
5f39d397 | 5902 | leaf = path->nodes[0]; |
39279cc3 | 5903 | slot = path->slots[0]; |
b9e03af0 LZ |
5904 | if (slot >= btrfs_header_nritems(leaf)) { |
5905 | ret = btrfs_next_leaf(root, path); | |
5906 | if (ret < 0) | |
5907 | goto err; | |
5908 | else if (ret > 0) | |
5909 | break; | |
5910 | continue; | |
39279cc3 | 5911 | } |
3de4586c | 5912 | |
5f39d397 CM |
5913 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5914 | ||
5915 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5916 | break; |
c2951f32 | 5917 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5918 | break; |
9cdda8d3 | 5919 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5920 | goto next; |
c2951f32 | 5921 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5922 | goto next; |
39279cc3 | 5923 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
c2951f32 | 5924 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5925 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5926 | PAGE_SIZE) { | |
5927 | btrfs_release_path(path); | |
5928 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5929 | if (ret) | |
5930 | goto nopos; | |
5931 | addr = private->filldir_buf; | |
5932 | entries = 0; | |
5933 | total_len = 0; | |
5934 | goto again; | |
c2951f32 | 5935 | } |
23b5ec74 JB |
5936 | |
5937 | entry = addr; | |
92d32170 | 5938 | put_unaligned(name_len, &entry->name_len); |
23b5ec74 | 5939 | name_ptr = (char *)(entry + 1); |
c2951f32 JM |
5940 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5941 | name_len); | |
92d32170 DS |
5942 | put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)], |
5943 | &entry->type); | |
c2951f32 | 5944 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
92d32170 DS |
5945 | put_unaligned(location.objectid, &entry->ino); |
5946 | put_unaligned(found_key.offset, &entry->offset); | |
23b5ec74 JB |
5947 | entries++; |
5948 | addr += sizeof(struct dir_entry) + name_len; | |
5949 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5950 | next: |
5951 | path->slots[0]++; | |
39279cc3 | 5952 | } |
23b5ec74 JB |
5953 | btrfs_release_path(path); |
5954 | ||
5955 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5956 | if (ret) | |
5957 | goto nopos; | |
49593bfa | 5958 | |
d2fbb2b5 | 5959 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5960 | if (ret) |
bc4ef759 DS |
5961 | goto nopos; |
5962 | ||
db62efbb ZB |
5963 | /* |
5964 | * Stop new entries from being returned after we return the last | |
5965 | * entry. | |
5966 | * | |
5967 | * New directory entries are assigned a strictly increasing | |
5968 | * offset. This means that new entries created during readdir | |
5969 | * are *guaranteed* to be seen in the future by that readdir. | |
5970 | * This has broken buggy programs which operate on names as | |
5971 | * they're returned by readdir. Until we re-use freed offsets | |
5972 | * we have this hack to stop new entries from being returned | |
5973 | * under the assumption that they'll never reach this huge | |
5974 | * offset. | |
5975 | * | |
5976 | * This is being careful not to overflow 32bit loff_t unless the | |
5977 | * last entry requires it because doing so has broken 32bit apps | |
5978 | * in the past. | |
5979 | */ | |
c2951f32 JM |
5980 | if (ctx->pos >= INT_MAX) |
5981 | ctx->pos = LLONG_MAX; | |
5982 | else | |
5983 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5984 | nopos: |
5985 | ret = 0; | |
5986 | err: | |
02dbfc99 OS |
5987 | if (put) |
5988 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5989 | btrfs_free_path(path); |
39279cc3 CM |
5990 | return ret; |
5991 | } | |
5992 | ||
39279cc3 | 5993 | /* |
54aa1f4d | 5994 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5995 | * inode changes. But, it is most likely to find the inode in cache. |
5996 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5997 | * to keep or drop this code. | |
5998 | */ | |
48a3b636 | 5999 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6000 | { |
2ff7e61e | 6001 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6002 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6003 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6004 | int ret; |
6005 | ||
72ac3c0d | 6006 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6007 | return 0; |
39279cc3 | 6008 | |
7a7eaa40 | 6009 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6010 | if (IS_ERR(trans)) |
6011 | return PTR_ERR(trans); | |
8929ecfa YZ |
6012 | |
6013 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6014 | if (ret && ret == -ENOSPC) { |
6015 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6016 | btrfs_end_transaction(trans); |
94b60442 | 6017 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6018 | if (IS_ERR(trans)) |
6019 | return PTR_ERR(trans); | |
8929ecfa | 6020 | |
94b60442 | 6021 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6022 | } |
3a45bb20 | 6023 | btrfs_end_transaction(trans); |
16cdcec7 | 6024 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6025 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6026 | |
6027 | return ret; | |
6028 | } | |
6029 | ||
6030 | /* | |
6031 | * This is a copy of file_update_time. We need this so we can return error on | |
6032 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6033 | */ | |
95582b00 | 6034 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
e41f941a | 6035 | int flags) |
22c44fe6 | 6036 | { |
2bc55652 | 6037 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3a8c7231 | 6038 | bool dirty = flags & ~S_VERSION; |
2bc55652 AB |
6039 | |
6040 | if (btrfs_root_readonly(root)) | |
6041 | return -EROFS; | |
6042 | ||
e41f941a | 6043 | if (flags & S_VERSION) |
3a8c7231 | 6044 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
e41f941a JB |
6045 | if (flags & S_CTIME) |
6046 | inode->i_ctime = *now; | |
6047 | if (flags & S_MTIME) | |
6048 | inode->i_mtime = *now; | |
6049 | if (flags & S_ATIME) | |
6050 | inode->i_atime = *now; | |
3a8c7231 | 6051 | return dirty ? btrfs_dirty_inode(inode) : 0; |
39279cc3 CM |
6052 | } |
6053 | ||
d352ac68 CM |
6054 | /* |
6055 | * find the highest existing sequence number in a directory | |
6056 | * and then set the in-memory index_cnt variable to reflect | |
6057 | * free sequence numbers | |
6058 | */ | |
4c570655 | 6059 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6060 | { |
4c570655 | 6061 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6062 | struct btrfs_key key, found_key; |
6063 | struct btrfs_path *path; | |
6064 | struct extent_buffer *leaf; | |
6065 | int ret; | |
6066 | ||
4c570655 | 6067 | key.objectid = btrfs_ino(inode); |
962a298f | 6068 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6069 | key.offset = (u64)-1; |
6070 | ||
6071 | path = btrfs_alloc_path(); | |
6072 | if (!path) | |
6073 | return -ENOMEM; | |
6074 | ||
6075 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6076 | if (ret < 0) | |
6077 | goto out; | |
6078 | /* FIXME: we should be able to handle this */ | |
6079 | if (ret == 0) | |
6080 | goto out; | |
6081 | ret = 0; | |
6082 | ||
6083 | /* | |
6084 | * MAGIC NUMBER EXPLANATION: | |
6085 | * since we search a directory based on f_pos we have to start at 2 | |
6086 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6087 | * else has to start at 2 | |
6088 | */ | |
6089 | if (path->slots[0] == 0) { | |
4c570655 | 6090 | inode->index_cnt = 2; |
aec7477b JB |
6091 | goto out; |
6092 | } | |
6093 | ||
6094 | path->slots[0]--; | |
6095 | ||
6096 | leaf = path->nodes[0]; | |
6097 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6098 | ||
4c570655 | 6099 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6100 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6101 | inode->index_cnt = 2; |
aec7477b JB |
6102 | goto out; |
6103 | } | |
6104 | ||
4c570655 | 6105 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6106 | out: |
6107 | btrfs_free_path(path); | |
6108 | return ret; | |
6109 | } | |
6110 | ||
d352ac68 CM |
6111 | /* |
6112 | * helper to find a free sequence number in a given directory. This current | |
6113 | * code is very simple, later versions will do smarter things in the btree | |
6114 | */ | |
877574e2 | 6115 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6116 | { |
6117 | int ret = 0; | |
6118 | ||
877574e2 NB |
6119 | if (dir->index_cnt == (u64)-1) { |
6120 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6121 | if (ret) { |
6122 | ret = btrfs_set_inode_index_count(dir); | |
6123 | if (ret) | |
6124 | return ret; | |
6125 | } | |
aec7477b JB |
6126 | } |
6127 | ||
877574e2 NB |
6128 | *index = dir->index_cnt; |
6129 | dir->index_cnt++; | |
aec7477b JB |
6130 | |
6131 | return ret; | |
6132 | } | |
6133 | ||
b0d5d10f CM |
6134 | static int btrfs_insert_inode_locked(struct inode *inode) |
6135 | { | |
6136 | struct btrfs_iget_args args; | |
6137 | args.location = &BTRFS_I(inode)->location; | |
6138 | args.root = BTRFS_I(inode)->root; | |
6139 | ||
6140 | return insert_inode_locked4(inode, | |
6141 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6142 | btrfs_find_actor, &args); | |
6143 | } | |
6144 | ||
19aee8de AJ |
6145 | /* |
6146 | * Inherit flags from the parent inode. | |
6147 | * | |
6148 | * Currently only the compression flags and the cow flags are inherited. | |
6149 | */ | |
6150 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6151 | { | |
6152 | unsigned int flags; | |
6153 | ||
6154 | if (!dir) | |
6155 | return; | |
6156 | ||
6157 | flags = BTRFS_I(dir)->flags; | |
6158 | ||
6159 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6160 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6161 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6162 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6163 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6164 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6165 | } | |
6166 | ||
6167 | if (flags & BTRFS_INODE_NODATACOW) { | |
6168 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6169 | if (S_ISREG(inode->i_mode)) | |
6170 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6171 | } | |
6172 | ||
7b6a221e | 6173 | btrfs_sync_inode_flags_to_i_flags(inode); |
19aee8de AJ |
6174 | } |
6175 | ||
39279cc3 CM |
6176 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6177 | struct btrfs_root *root, | |
aec7477b | 6178 | struct inode *dir, |
9c58309d | 6179 | const char *name, int name_len, |
175a4eb7 AV |
6180 | u64 ref_objectid, u64 objectid, |
6181 | umode_t mode, u64 *index) | |
39279cc3 | 6182 | { |
0b246afa | 6183 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6184 | struct inode *inode; |
5f39d397 | 6185 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6186 | struct btrfs_key *location; |
5f39d397 | 6187 | struct btrfs_path *path; |
9c58309d CM |
6188 | struct btrfs_inode_ref *ref; |
6189 | struct btrfs_key key[2]; | |
6190 | u32 sizes[2]; | |
ef3b9af5 | 6191 | int nitems = name ? 2 : 1; |
9c58309d | 6192 | unsigned long ptr; |
39279cc3 | 6193 | int ret; |
39279cc3 | 6194 | |
5f39d397 | 6195 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6196 | if (!path) |
6197 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6198 | |
0b246afa | 6199 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6200 | if (!inode) { |
6201 | btrfs_free_path(path); | |
39279cc3 | 6202 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6203 | } |
39279cc3 | 6204 | |
5762b5c9 FM |
6205 | /* |
6206 | * O_TMPFILE, set link count to 0, so that after this point, | |
6207 | * we fill in an inode item with the correct link count. | |
6208 | */ | |
6209 | if (!name) | |
6210 | set_nlink(inode, 0); | |
6211 | ||
581bb050 LZ |
6212 | /* |
6213 | * we have to initialize this early, so we can reclaim the inode | |
6214 | * number if we fail afterwards in this function. | |
6215 | */ | |
6216 | inode->i_ino = objectid; | |
6217 | ||
ef3b9af5 | 6218 | if (dir && name) { |
1abe9b8a | 6219 | trace_btrfs_inode_request(dir); |
6220 | ||
877574e2 | 6221 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6222 | if (ret) { |
8fb27640 | 6223 | btrfs_free_path(path); |
09771430 | 6224 | iput(inode); |
aec7477b | 6225 | return ERR_PTR(ret); |
09771430 | 6226 | } |
ef3b9af5 FM |
6227 | } else if (dir) { |
6228 | *index = 0; | |
aec7477b JB |
6229 | } |
6230 | /* | |
6231 | * index_cnt is ignored for everything but a dir, | |
df6703e1 | 6232 | * btrfs_set_inode_index_count has an explanation for the magic |
aec7477b JB |
6233 | * number |
6234 | */ | |
6235 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6236 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6237 | BTRFS_I(inode)->root = root; |
e02119d5 | 6238 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6239 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6240 | |
5dc562c5 JB |
6241 | /* |
6242 | * We could have gotten an inode number from somebody who was fsynced | |
6243 | * and then removed in this same transaction, so let's just set full | |
6244 | * sync since it will be a full sync anyway and this will blow away the | |
6245 | * old info in the log. | |
6246 | */ | |
6247 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6248 | ||
9c58309d | 6249 | key[0].objectid = objectid; |
962a298f | 6250 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6251 | key[0].offset = 0; |
6252 | ||
9c58309d | 6253 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6254 | |
6255 | if (name) { | |
6256 | /* | |
6257 | * Start new inodes with an inode_ref. This is slightly more | |
6258 | * efficient for small numbers of hard links since they will | |
6259 | * be packed into one item. Extended refs will kick in if we | |
6260 | * add more hard links than can fit in the ref item. | |
6261 | */ | |
6262 | key[1].objectid = objectid; | |
962a298f | 6263 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6264 | key[1].offset = ref_objectid; |
6265 | ||
6266 | sizes[1] = name_len + sizeof(*ref); | |
6267 | } | |
9c58309d | 6268 | |
b0d5d10f CM |
6269 | location = &BTRFS_I(inode)->location; |
6270 | location->objectid = objectid; | |
6271 | location->offset = 0; | |
962a298f | 6272 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6273 | |
6274 | ret = btrfs_insert_inode_locked(inode); | |
32955c54 AV |
6275 | if (ret < 0) { |
6276 | iput(inode); | |
b0d5d10f | 6277 | goto fail; |
32955c54 | 6278 | } |
b0d5d10f | 6279 | |
b9473439 | 6280 | path->leave_spinning = 1; |
ef3b9af5 | 6281 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6282 | if (ret != 0) |
b0d5d10f | 6283 | goto fail_unlock; |
5f39d397 | 6284 | |
ecc11fab | 6285 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6286 | inode_set_bytes(inode, 0); |
9cc97d64 | 6287 | |
c2050a45 | 6288 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6289 | inode->i_atime = inode->i_mtime; |
6290 | inode->i_ctime = inode->i_mtime; | |
d3c6be6f | 6291 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
9cc97d64 | 6292 | |
5f39d397 CM |
6293 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6294 | struct btrfs_inode_item); | |
b159fa28 | 6295 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6296 | sizeof(*inode_item)); |
e02119d5 | 6297 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6298 | |
ef3b9af5 FM |
6299 | if (name) { |
6300 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6301 | struct btrfs_inode_ref); | |
6302 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6303 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6304 | ptr = (unsigned long)(ref + 1); | |
6305 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6306 | } | |
9c58309d | 6307 | |
5f39d397 CM |
6308 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6309 | btrfs_free_path(path); | |
6310 | ||
6cbff00f CH |
6311 | btrfs_inherit_iflags(inode, dir); |
6312 | ||
569254b0 | 6313 | if (S_ISREG(mode)) { |
0b246afa | 6314 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6315 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6316 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6317 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6318 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6319 | } |
6320 | ||
5d4f98a2 | 6321 | inode_tree_add(inode); |
1abe9b8a | 6322 | |
6323 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6324 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6325 | |
8ea05e3a AB |
6326 | btrfs_update_root_times(trans, root); |
6327 | ||
63541927 FDBM |
6328 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6329 | if (ret) | |
0b246afa | 6330 | btrfs_err(fs_info, |
63541927 | 6331 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6332 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6333 | |
39279cc3 | 6334 | return inode; |
b0d5d10f CM |
6335 | |
6336 | fail_unlock: | |
32955c54 | 6337 | discard_new_inode(inode); |
5f39d397 | 6338 | fail: |
ef3b9af5 | 6339 | if (dir && name) |
aec7477b | 6340 | BTRFS_I(dir)->index_cnt--; |
5f39d397 CM |
6341 | btrfs_free_path(path); |
6342 | return ERR_PTR(ret); | |
39279cc3 CM |
6343 | } |
6344 | ||
6345 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6346 | { | |
6347 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6348 | } | |
6349 | ||
d352ac68 CM |
6350 | /* |
6351 | * utility function to add 'inode' into 'parent_inode' with | |
6352 | * a give name and a given sequence number. | |
6353 | * if 'add_backref' is true, also insert a backref from the | |
6354 | * inode to the parent directory. | |
6355 | */ | |
e02119d5 | 6356 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6357 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6358 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6359 | { |
4df27c4d | 6360 | int ret = 0; |
39279cc3 | 6361 | struct btrfs_key key; |
db0a669f NB |
6362 | struct btrfs_root *root = parent_inode->root; |
6363 | u64 ino = btrfs_ino(inode); | |
6364 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6365 | |
33345d01 | 6366 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6367 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6368 | } else { |
33345d01 | 6369 | key.objectid = ino; |
962a298f | 6370 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6371 | key.offset = 0; |
6372 | } | |
6373 | ||
33345d01 | 6374 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
6025c19f | 6375 | ret = btrfs_add_root_ref(trans, key.objectid, |
0b246afa JM |
6376 | root->root_key.objectid, parent_ino, |
6377 | index, name, name_len); | |
4df27c4d | 6378 | } else if (add_backref) { |
33345d01 LZ |
6379 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6380 | parent_ino, index); | |
4df27c4d | 6381 | } |
39279cc3 | 6382 | |
79787eaa JM |
6383 | /* Nothing to clean up yet */ |
6384 | if (ret) | |
6385 | return ret; | |
4df27c4d | 6386 | |
684572df | 6387 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
db0a669f | 6388 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6389 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6390 | goto fail_dir_item; |
6391 | else if (ret) { | |
66642832 | 6392 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6393 | return ret; |
39279cc3 | 6394 | } |
79787eaa | 6395 | |
db0a669f | 6396 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6397 | name_len * 2); |
db0a669f NB |
6398 | inode_inc_iversion(&parent_inode->vfs_inode); |
6399 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6400 | current_time(&parent_inode->vfs_inode); | |
6401 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
79787eaa | 6402 | if (ret) |
66642832 | 6403 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6404 | return ret; |
fe66a05a CM |
6405 | |
6406 | fail_dir_item: | |
6407 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6408 | u64 local_index; | |
6409 | int err; | |
3ee1c553 | 6410 | err = btrfs_del_root_ref(trans, key.objectid, |
0b246afa JM |
6411 | root->root_key.objectid, parent_ino, |
6412 | &local_index, name, name_len); | |
1690dd41 JT |
6413 | if (err) |
6414 | btrfs_abort_transaction(trans, err); | |
fe66a05a CM |
6415 | } else if (add_backref) { |
6416 | u64 local_index; | |
6417 | int err; | |
6418 | ||
6419 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6420 | ino, parent_ino, &local_index); | |
1690dd41 JT |
6421 | if (err) |
6422 | btrfs_abort_transaction(trans, err); | |
fe66a05a | 6423 | } |
1690dd41 JT |
6424 | |
6425 | /* Return the original error code */ | |
fe66a05a | 6426 | return ret; |
39279cc3 CM |
6427 | } |
6428 | ||
6429 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6430 | struct btrfs_inode *dir, struct dentry *dentry, |
6431 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6432 | { |
a1b075d2 JB |
6433 | int err = btrfs_add_link(trans, dir, inode, |
6434 | dentry->d_name.name, dentry->d_name.len, | |
6435 | backref, index); | |
39279cc3 CM |
6436 | if (err > 0) |
6437 | err = -EEXIST; | |
6438 | return err; | |
6439 | } | |
6440 | ||
618e21d5 | 6441 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6442 | umode_t mode, dev_t rdev) |
618e21d5 | 6443 | { |
2ff7e61e | 6444 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6445 | struct btrfs_trans_handle *trans; |
6446 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6447 | struct inode *inode = NULL; |
618e21d5 | 6448 | int err; |
618e21d5 | 6449 | u64 objectid; |
00e4e6b3 | 6450 | u64 index = 0; |
618e21d5 | 6451 | |
9ed74f2d JB |
6452 | /* |
6453 | * 2 for inode item and ref | |
6454 | * 2 for dir items | |
6455 | * 1 for xattr if selinux is on | |
6456 | */ | |
a22285a6 YZ |
6457 | trans = btrfs_start_transaction(root, 5); |
6458 | if (IS_ERR(trans)) | |
6459 | return PTR_ERR(trans); | |
1832a6d5 | 6460 | |
581bb050 LZ |
6461 | err = btrfs_find_free_ino(root, &objectid); |
6462 | if (err) | |
6463 | goto out_unlock; | |
6464 | ||
aec7477b | 6465 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6466 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6467 | mode, &index); | |
7cf96da3 TI |
6468 | if (IS_ERR(inode)) { |
6469 | err = PTR_ERR(inode); | |
32955c54 | 6470 | inode = NULL; |
618e21d5 | 6471 | goto out_unlock; |
7cf96da3 | 6472 | } |
618e21d5 | 6473 | |
ad19db71 CS |
6474 | /* |
6475 | * If the active LSM wants to access the inode during | |
6476 | * d_instantiate it needs these. Smack checks to see | |
6477 | * if the filesystem supports xattrs by looking at the | |
6478 | * ops vector. | |
6479 | */ | |
ad19db71 | 6480 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6481 | init_special_inode(inode, inode->i_mode, rdev); |
6482 | ||
6483 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6484 | if (err) |
32955c54 | 6485 | goto out_unlock; |
b0d5d10f | 6486 | |
cef415af NB |
6487 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6488 | 0, index); | |
32955c54 AV |
6489 | if (err) |
6490 | goto out_unlock; | |
6491 | ||
6492 | btrfs_update_inode(trans, root, inode); | |
6493 | d_instantiate_new(dentry, inode); | |
b0d5d10f | 6494 | |
618e21d5 | 6495 | out_unlock: |
3a45bb20 | 6496 | btrfs_end_transaction(trans); |
2ff7e61e | 6497 | btrfs_btree_balance_dirty(fs_info); |
32955c54 | 6498 | if (err && inode) { |
618e21d5 | 6499 | inode_dec_link_count(inode); |
32955c54 | 6500 | discard_new_inode(inode); |
618e21d5 | 6501 | } |
618e21d5 JB |
6502 | return err; |
6503 | } | |
6504 | ||
39279cc3 | 6505 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6506 | umode_t mode, bool excl) |
39279cc3 | 6507 | { |
2ff7e61e | 6508 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6509 | struct btrfs_trans_handle *trans; |
6510 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6511 | struct inode *inode = NULL; |
a22285a6 | 6512 | int err; |
39279cc3 | 6513 | u64 objectid; |
00e4e6b3 | 6514 | u64 index = 0; |
39279cc3 | 6515 | |
9ed74f2d JB |
6516 | /* |
6517 | * 2 for inode item and ref | |
6518 | * 2 for dir items | |
6519 | * 1 for xattr if selinux is on | |
6520 | */ | |
a22285a6 YZ |
6521 | trans = btrfs_start_transaction(root, 5); |
6522 | if (IS_ERR(trans)) | |
6523 | return PTR_ERR(trans); | |
9ed74f2d | 6524 | |
581bb050 LZ |
6525 | err = btrfs_find_free_ino(root, &objectid); |
6526 | if (err) | |
6527 | goto out_unlock; | |
6528 | ||
aec7477b | 6529 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6530 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6531 | mode, &index); | |
7cf96da3 TI |
6532 | if (IS_ERR(inode)) { |
6533 | err = PTR_ERR(inode); | |
32955c54 | 6534 | inode = NULL; |
39279cc3 | 6535 | goto out_unlock; |
7cf96da3 | 6536 | } |
ad19db71 CS |
6537 | /* |
6538 | * If the active LSM wants to access the inode during | |
6539 | * d_instantiate it needs these. Smack checks to see | |
6540 | * if the filesystem supports xattrs by looking at the | |
6541 | * ops vector. | |
6542 | */ | |
6543 | inode->i_fop = &btrfs_file_operations; | |
6544 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6545 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6546 | |
6547 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6548 | if (err) | |
32955c54 | 6549 | goto out_unlock; |
b0d5d10f CM |
6550 | |
6551 | err = btrfs_update_inode(trans, root, inode); | |
6552 | if (err) | |
32955c54 | 6553 | goto out_unlock; |
ad19db71 | 6554 | |
cef415af NB |
6555 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6556 | 0, index); | |
39279cc3 | 6557 | if (err) |
32955c54 | 6558 | goto out_unlock; |
43baa579 | 6559 | |
43baa579 | 6560 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
1e2e547a | 6561 | d_instantiate_new(dentry, inode); |
43baa579 | 6562 | |
39279cc3 | 6563 | out_unlock: |
3a45bb20 | 6564 | btrfs_end_transaction(trans); |
32955c54 | 6565 | if (err && inode) { |
39279cc3 | 6566 | inode_dec_link_count(inode); |
32955c54 | 6567 | discard_new_inode(inode); |
39279cc3 | 6568 | } |
2ff7e61e | 6569 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6570 | return err; |
6571 | } | |
6572 | ||
6573 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6574 | struct dentry *dentry) | |
6575 | { | |
271dba45 | 6576 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6577 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6578 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6579 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6580 | u64 index; |
39279cc3 CM |
6581 | int err; |
6582 | int drop_inode = 0; | |
6583 | ||
4a8be425 | 6584 | /* do not allow sys_link's with other subvols of the same device */ |
4fd786e6 | 6585 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
3ab3564f | 6586 | return -EXDEV; |
4a8be425 | 6587 | |
f186373f | 6588 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6589 | return -EMLINK; |
4a8be425 | 6590 | |
877574e2 | 6591 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6592 | if (err) |
6593 | goto fail; | |
6594 | ||
a22285a6 | 6595 | /* |
7e6b6465 | 6596 | * 2 items for inode and inode ref |
a22285a6 | 6597 | * 2 items for dir items |
7e6b6465 | 6598 | * 1 item for parent inode |
399b0bbf | 6599 | * 1 item for orphan item deletion if O_TMPFILE |
a22285a6 | 6600 | */ |
399b0bbf | 6601 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
a22285a6 YZ |
6602 | if (IS_ERR(trans)) { |
6603 | err = PTR_ERR(trans); | |
271dba45 | 6604 | trans = NULL; |
a22285a6 YZ |
6605 | goto fail; |
6606 | } | |
5f39d397 | 6607 | |
67de1176 MX |
6608 | /* There are several dir indexes for this inode, clear the cache. */ |
6609 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6610 | inc_nlink(inode); |
0c4d2d95 | 6611 | inode_inc_iversion(inode); |
c2050a45 | 6612 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6613 | ihold(inode); |
e9976151 | 6614 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6615 | |
cef415af NB |
6616 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6617 | 1, index); | |
5f39d397 | 6618 | |
a5719521 | 6619 | if (err) { |
54aa1f4d | 6620 | drop_inode = 1; |
a5719521 | 6621 | } else { |
10d9f309 | 6622 | struct dentry *parent = dentry->d_parent; |
d4682ba0 FM |
6623 | int ret; |
6624 | ||
a5719521 | 6625 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6626 | if (err) |
6627 | goto fail; | |
ef3b9af5 FM |
6628 | if (inode->i_nlink == 1) { |
6629 | /* | |
6630 | * If new hard link count is 1, it's a file created | |
6631 | * with open(2) O_TMPFILE flag. | |
6632 | */ | |
3d6ae7bb | 6633 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6634 | if (err) |
6635 | goto fail; | |
6636 | } | |
41bd6067 | 6637 | BTRFS_I(inode)->last_link_trans = trans->transid; |
08c422c2 | 6638 | d_instantiate(dentry, inode); |
d4682ba0 FM |
6639 | ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent, |
6640 | true, NULL); | |
6641 | if (ret == BTRFS_NEED_TRANS_COMMIT) { | |
6642 | err = btrfs_commit_transaction(trans); | |
6643 | trans = NULL; | |
6644 | } | |
a5719521 | 6645 | } |
39279cc3 | 6646 | |
1832a6d5 | 6647 | fail: |
271dba45 | 6648 | if (trans) |
3a45bb20 | 6649 | btrfs_end_transaction(trans); |
39279cc3 CM |
6650 | if (drop_inode) { |
6651 | inode_dec_link_count(inode); | |
6652 | iput(inode); | |
6653 | } | |
2ff7e61e | 6654 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6655 | return err; |
6656 | } | |
6657 | ||
18bb1db3 | 6658 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6659 | { |
2ff7e61e | 6660 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6661 | struct inode *inode = NULL; |
39279cc3 CM |
6662 | struct btrfs_trans_handle *trans; |
6663 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6664 | int err = 0; | |
b9d86667 | 6665 | u64 objectid = 0; |
00e4e6b3 | 6666 | u64 index = 0; |
39279cc3 | 6667 | |
9ed74f2d JB |
6668 | /* |
6669 | * 2 items for inode and ref | |
6670 | * 2 items for dir items | |
6671 | * 1 for xattr if selinux is on | |
6672 | */ | |
a22285a6 YZ |
6673 | trans = btrfs_start_transaction(root, 5); |
6674 | if (IS_ERR(trans)) | |
6675 | return PTR_ERR(trans); | |
39279cc3 | 6676 | |
581bb050 LZ |
6677 | err = btrfs_find_free_ino(root, &objectid); |
6678 | if (err) | |
6679 | goto out_fail; | |
6680 | ||
aec7477b | 6681 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6682 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6683 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6684 | if (IS_ERR(inode)) { |
6685 | err = PTR_ERR(inode); | |
32955c54 | 6686 | inode = NULL; |
39279cc3 CM |
6687 | goto out_fail; |
6688 | } | |
5f39d397 | 6689 | |
b0d5d10f CM |
6690 | /* these must be set before we unlock the inode */ |
6691 | inode->i_op = &btrfs_dir_inode_operations; | |
6692 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6693 | |
2a7dba39 | 6694 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6695 | if (err) |
32955c54 | 6696 | goto out_fail; |
39279cc3 | 6697 | |
6ef06d27 | 6698 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6699 | err = btrfs_update_inode(trans, root, inode); |
6700 | if (err) | |
32955c54 | 6701 | goto out_fail; |
5f39d397 | 6702 | |
db0a669f NB |
6703 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6704 | dentry->d_name.name, | |
6705 | dentry->d_name.len, 0, index); | |
39279cc3 | 6706 | if (err) |
32955c54 | 6707 | goto out_fail; |
5f39d397 | 6708 | |
1e2e547a | 6709 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
6710 | |
6711 | out_fail: | |
3a45bb20 | 6712 | btrfs_end_transaction(trans); |
32955c54 | 6713 | if (err && inode) { |
c7cfb8a5 | 6714 | inode_dec_link_count(inode); |
32955c54 | 6715 | discard_new_inode(inode); |
c7cfb8a5 | 6716 | } |
2ff7e61e | 6717 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6718 | return err; |
6719 | } | |
6720 | ||
c8b97818 | 6721 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6722 | struct page *page, |
c8b97818 CM |
6723 | size_t pg_offset, u64 extent_offset, |
6724 | struct btrfs_file_extent_item *item) | |
6725 | { | |
6726 | int ret; | |
6727 | struct extent_buffer *leaf = path->nodes[0]; | |
6728 | char *tmp; | |
6729 | size_t max_size; | |
6730 | unsigned long inline_size; | |
6731 | unsigned long ptr; | |
261507a0 | 6732 | int compress_type; |
c8b97818 CM |
6733 | |
6734 | WARN_ON(pg_offset != 0); | |
261507a0 | 6735 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6736 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6737 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6738 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6739 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6740 | if (!tmp) |
6741 | return -ENOMEM; | |
c8b97818 CM |
6742 | ptr = btrfs_file_extent_inline_start(item); |
6743 | ||
6744 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6745 | ||
09cbfeaf | 6746 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6747 | ret = btrfs_decompress(compress_type, tmp, page, |
6748 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6749 | |
6750 | /* | |
6751 | * decompression code contains a memset to fill in any space between the end | |
6752 | * of the uncompressed data and the end of max_size in case the decompressed | |
6753 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6754 | * the end of an inline extent and the beginning of the next block, so we | |
6755 | * cover that region here. | |
6756 | */ | |
6757 | ||
6758 | if (max_size + pg_offset < PAGE_SIZE) { | |
6759 | char *map = kmap(page); | |
6760 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6761 | kunmap(page); | |
6762 | } | |
c8b97818 | 6763 | kfree(tmp); |
166ae5a4 | 6764 | return ret; |
c8b97818 CM |
6765 | } |
6766 | ||
d352ac68 CM |
6767 | /* |
6768 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6769 | * the ugly parts come from merging extents from the disk with the in-ram |
6770 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6771 | * where the in-ram extents might be locked pending data=ordered completion. |
6772 | * | |
6773 | * This also copies inline extents directly into the page. | |
6774 | */ | |
fc4f21b1 | 6775 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
de2c6615 LB |
6776 | struct page *page, |
6777 | size_t pg_offset, u64 start, u64 len, | |
6778 | int create) | |
a52d9a80 | 6779 | { |
3ffbd68c | 6780 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
a52d9a80 CM |
6781 | int ret; |
6782 | int err = 0; | |
a52d9a80 CM |
6783 | u64 extent_start = 0; |
6784 | u64 extent_end = 0; | |
fc4f21b1 | 6785 | u64 objectid = btrfs_ino(inode); |
694c12ed | 6786 | u8 extent_type; |
f421950f | 6787 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6788 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6789 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6790 | struct extent_buffer *leaf; |
6791 | struct btrfs_key found_key; | |
a52d9a80 | 6792 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6793 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6794 | struct extent_io_tree *io_tree = &inode->io_tree; | |
7ffbb598 | 6795 | const bool new_inline = !page || create; |
a52d9a80 | 6796 | |
890871be | 6797 | read_lock(&em_tree->lock); |
d1310b2e | 6798 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6799 | if (em) |
0b246afa | 6800 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6801 | read_unlock(&em_tree->lock); |
d1310b2e | 6802 | |
a52d9a80 | 6803 | if (em) { |
e1c4b745 CM |
6804 | if (em->start > start || em->start + em->len <= start) |
6805 | free_extent_map(em); | |
6806 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6807 | free_extent_map(em); |
6808 | else | |
6809 | goto out; | |
a52d9a80 | 6810 | } |
172ddd60 | 6811 | em = alloc_extent_map(); |
a52d9a80 | 6812 | if (!em) { |
d1310b2e CM |
6813 | err = -ENOMEM; |
6814 | goto out; | |
a52d9a80 | 6815 | } |
0b246afa | 6816 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 6817 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6818 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6819 | em->len = (u64)-1; |
c8b97818 | 6820 | em->block_len = (u64)-1; |
f421950f | 6821 | |
bee6ec82 | 6822 | path = btrfs_alloc_path(); |
f421950f | 6823 | if (!path) { |
bee6ec82 LB |
6824 | err = -ENOMEM; |
6825 | goto out; | |
f421950f CM |
6826 | } |
6827 | ||
bee6ec82 LB |
6828 | /* Chances are we'll be called again, so go ahead and do readahead */ |
6829 | path->reada = READA_FORWARD; | |
6830 | ||
e49aabd9 LB |
6831 | /* |
6832 | * Unless we're going to uncompress the inline extent, no sleep would | |
6833 | * happen. | |
6834 | */ | |
6835 | path->leave_spinning = 1; | |
6836 | ||
5c9a702e | 6837 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
a52d9a80 CM |
6838 | if (ret < 0) { |
6839 | err = ret; | |
6840 | goto out; | |
b8eeab7f | 6841 | } else if (ret > 0) { |
a52d9a80 CM |
6842 | if (path->slots[0] == 0) |
6843 | goto not_found; | |
6844 | path->slots[0]--; | |
6845 | } | |
6846 | ||
5f39d397 CM |
6847 | leaf = path->nodes[0]; |
6848 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6849 | struct btrfs_file_extent_item); |
5f39d397 | 6850 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
5f39d397 | 6851 | if (found_key.objectid != objectid || |
694c12ed | 6852 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6853 | /* |
6854 | * If we backup past the first extent we want to move forward | |
6855 | * and see if there is an extent in front of us, otherwise we'll | |
6856 | * say there is a hole for our whole search range which can | |
6857 | * cause problems. | |
6858 | */ | |
6859 | extent_end = start; | |
6860 | goto next; | |
a52d9a80 CM |
6861 | } |
6862 | ||
694c12ed | 6863 | extent_type = btrfs_file_extent_type(leaf, item); |
5f39d397 | 6864 | extent_start = found_key.offset; |
694c12ed NB |
6865 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6866 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6867 | extent_end = extent_start + |
db94535d | 6868 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
6869 | |
6870 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
6871 | extent_start); | |
694c12ed | 6872 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
9036c102 | 6873 | size_t size; |
e41ca589 QW |
6874 | |
6875 | size = btrfs_file_extent_ram_bytes(leaf, item); | |
da17066c | 6876 | extent_end = ALIGN(extent_start + size, |
0b246afa | 6877 | fs_info->sectorsize); |
09ed2f16 LB |
6878 | |
6879 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
6880 | path->slots[0], | |
6881 | extent_start); | |
9036c102 | 6882 | } |
25a50341 | 6883 | next: |
9036c102 YZ |
6884 | if (start >= extent_end) { |
6885 | path->slots[0]++; | |
6886 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6887 | ret = btrfs_next_leaf(root, path); | |
6888 | if (ret < 0) { | |
6889 | err = ret; | |
6890 | goto out; | |
b8eeab7f | 6891 | } else if (ret > 0) { |
9036c102 | 6892 | goto not_found; |
b8eeab7f | 6893 | } |
9036c102 | 6894 | leaf = path->nodes[0]; |
a52d9a80 | 6895 | } |
9036c102 YZ |
6896 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6897 | if (found_key.objectid != objectid || | |
6898 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6899 | goto not_found; | |
6900 | if (start + len <= found_key.offset) | |
6901 | goto not_found; | |
e2eca69d WS |
6902 | if (start > found_key.offset) |
6903 | goto next; | |
02a033df NB |
6904 | |
6905 | /* New extent overlaps with existing one */ | |
9036c102 | 6906 | em->start = start; |
70c8a91c | 6907 | em->orig_start = start; |
9036c102 | 6908 | em->len = found_key.offset - start; |
02a033df NB |
6909 | em->block_start = EXTENT_MAP_HOLE; |
6910 | goto insert; | |
9036c102 YZ |
6911 | } |
6912 | ||
fc4f21b1 | 6913 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 6914 | new_inline, em); |
7ffbb598 | 6915 | |
694c12ed NB |
6916 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
6917 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6918 | goto insert; |
694c12ed | 6919 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
5f39d397 | 6920 | unsigned long ptr; |
a52d9a80 | 6921 | char *map; |
3326d1b0 CM |
6922 | size_t size; |
6923 | size_t extent_offset; | |
6924 | size_t copy_size; | |
a52d9a80 | 6925 | |
7ffbb598 | 6926 | if (new_inline) |
689f9346 | 6927 | goto out; |
5f39d397 | 6928 | |
e41ca589 | 6929 | size = btrfs_file_extent_ram_bytes(leaf, item); |
9036c102 | 6930 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6931 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6932 | size - extent_offset); | |
3326d1b0 | 6933 | em->start = extent_start + extent_offset; |
0b246afa | 6934 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 6935 | em->orig_block_len = em->len; |
70c8a91c | 6936 | em->orig_start = em->start; |
689f9346 | 6937 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
e49aabd9 LB |
6938 | |
6939 | btrfs_set_path_blocking(path); | |
bf46f52d | 6940 | if (!PageUptodate(page)) { |
261507a0 LZ |
6941 | if (btrfs_file_extent_compression(leaf, item) != |
6942 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6943 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6944 | extent_offset, item); |
166ae5a4 ZB |
6945 | if (ret) { |
6946 | err = ret; | |
6947 | goto out; | |
6948 | } | |
c8b97818 CM |
6949 | } else { |
6950 | map = kmap(page); | |
6951 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6952 | copy_size); | |
09cbfeaf | 6953 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6954 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6955 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6956 | copy_size); |
6957 | } | |
c8b97818 CM |
6958 | kunmap(page); |
6959 | } | |
179e29e4 | 6960 | flush_dcache_page(page); |
a52d9a80 | 6961 | } |
d1310b2e | 6962 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6963 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6964 | goto insert; |
a52d9a80 CM |
6965 | } |
6966 | not_found: | |
6967 | em->start = start; | |
70c8a91c | 6968 | em->orig_start = start; |
d1310b2e | 6969 | em->len = len; |
5f39d397 | 6970 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 | 6971 | insert: |
b3b4aa74 | 6972 | btrfs_release_path(path); |
d1310b2e | 6973 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 6974 | btrfs_err(fs_info, |
5d163e0e JM |
6975 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
6976 | em->start, em->len, start, len); | |
a52d9a80 CM |
6977 | err = -EIO; |
6978 | goto out; | |
6979 | } | |
d1310b2e CM |
6980 | |
6981 | err = 0; | |
890871be | 6982 | write_lock(&em_tree->lock); |
f46b24c9 | 6983 | err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
890871be | 6984 | write_unlock(&em_tree->lock); |
a52d9a80 | 6985 | out: |
c6414280 | 6986 | btrfs_free_path(path); |
1abe9b8a | 6987 | |
fc4f21b1 | 6988 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 6989 | |
a52d9a80 CM |
6990 | if (err) { |
6991 | free_extent_map(em); | |
a52d9a80 CM |
6992 | return ERR_PTR(err); |
6993 | } | |
79787eaa | 6994 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6995 | return em; |
6996 | } | |
6997 | ||
fc4f21b1 | 6998 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
4ab47a8d | 6999 | u64 start, u64 len) |
ec29ed5b CM |
7000 | { |
7001 | struct extent_map *em; | |
7002 | struct extent_map *hole_em = NULL; | |
f3714ef4 | 7003 | u64 delalloc_start = start; |
ec29ed5b | 7004 | u64 end; |
f3714ef4 NB |
7005 | u64 delalloc_len; |
7006 | u64 delalloc_end; | |
ec29ed5b CM |
7007 | int err = 0; |
7008 | ||
4ab47a8d | 7009 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
ec29ed5b CM |
7010 | if (IS_ERR(em)) |
7011 | return em; | |
9986277e DC |
7012 | /* |
7013 | * If our em maps to: | |
7014 | * - a hole or | |
7015 | * - a pre-alloc extent, | |
7016 | * there might actually be delalloc bytes behind it. | |
7017 | */ | |
7018 | if (em->block_start != EXTENT_MAP_HOLE && | |
7019 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7020 | return em; | |
7021 | else | |
7022 | hole_em = em; | |
ec29ed5b CM |
7023 | |
7024 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7025 | end = start + len; | |
7026 | if (end < start) | |
7027 | end = (u64)-1; | |
7028 | else | |
7029 | end -= 1; | |
7030 | ||
7031 | em = NULL; | |
7032 | ||
7033 | /* ok, we didn't find anything, lets look for delalloc */ | |
f3714ef4 | 7034 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
ec29ed5b | 7035 | end, len, EXTENT_DELALLOC, 1); |
f3714ef4 NB |
7036 | delalloc_end = delalloc_start + delalloc_len; |
7037 | if (delalloc_end < delalloc_start) | |
7038 | delalloc_end = (u64)-1; | |
ec29ed5b CM |
7039 | |
7040 | /* | |
f3714ef4 NB |
7041 | * We didn't find anything useful, return the original results from |
7042 | * get_extent() | |
ec29ed5b | 7043 | */ |
f3714ef4 | 7044 | if (delalloc_start > end || delalloc_end <= start) { |
ec29ed5b CM |
7045 | em = hole_em; |
7046 | hole_em = NULL; | |
7047 | goto out; | |
7048 | } | |
7049 | ||
f3714ef4 NB |
7050 | /* |
7051 | * Adjust the delalloc_start to make sure it doesn't go backwards from | |
7052 | * the start they passed in | |
ec29ed5b | 7053 | */ |
f3714ef4 NB |
7054 | delalloc_start = max(start, delalloc_start); |
7055 | delalloc_len = delalloc_end - delalloc_start; | |
ec29ed5b | 7056 | |
f3714ef4 NB |
7057 | if (delalloc_len > 0) { |
7058 | u64 hole_start; | |
02950af4 | 7059 | u64 hole_len; |
f3714ef4 | 7060 | const u64 hole_end = extent_map_end(hole_em); |
ec29ed5b | 7061 | |
172ddd60 | 7062 | em = alloc_extent_map(); |
ec29ed5b CM |
7063 | if (!em) { |
7064 | err = -ENOMEM; | |
7065 | goto out; | |
7066 | } | |
f3714ef4 NB |
7067 | em->bdev = NULL; |
7068 | ||
7069 | ASSERT(hole_em); | |
ec29ed5b | 7070 | /* |
f3714ef4 NB |
7071 | * When btrfs_get_extent can't find anything it returns one |
7072 | * huge hole | |
ec29ed5b | 7073 | * |
f3714ef4 NB |
7074 | * Make sure what it found really fits our range, and adjust to |
7075 | * make sure it is based on the start from the caller | |
ec29ed5b | 7076 | */ |
f3714ef4 NB |
7077 | if (hole_end <= start || hole_em->start > end) { |
7078 | free_extent_map(hole_em); | |
7079 | hole_em = NULL; | |
7080 | } else { | |
7081 | hole_start = max(hole_em->start, start); | |
7082 | hole_len = hole_end - hole_start; | |
ec29ed5b | 7083 | } |
f3714ef4 NB |
7084 | |
7085 | if (hole_em && delalloc_start > hole_start) { | |
7086 | /* | |
7087 | * Our hole starts before our delalloc, so we have to | |
7088 | * return just the parts of the hole that go until the | |
7089 | * delalloc starts | |
ec29ed5b | 7090 | */ |
f3714ef4 | 7091 | em->len = min(hole_len, delalloc_start - hole_start); |
ec29ed5b CM |
7092 | em->start = hole_start; |
7093 | em->orig_start = hole_start; | |
7094 | /* | |
f3714ef4 NB |
7095 | * Don't adjust block start at all, it is fixed at |
7096 | * EXTENT_MAP_HOLE | |
ec29ed5b CM |
7097 | */ |
7098 | em->block_start = hole_em->block_start; | |
7099 | em->block_len = hole_len; | |
f9e4fb53 LB |
7100 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7101 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b | 7102 | } else { |
f3714ef4 NB |
7103 | /* |
7104 | * Hole is out of passed range or it starts after | |
7105 | * delalloc range | |
7106 | */ | |
7107 | em->start = delalloc_start; | |
7108 | em->len = delalloc_len; | |
7109 | em->orig_start = delalloc_start; | |
ec29ed5b | 7110 | em->block_start = EXTENT_MAP_DELALLOC; |
f3714ef4 | 7111 | em->block_len = delalloc_len; |
ec29ed5b | 7112 | } |
bf8d32b9 | 7113 | } else { |
ec29ed5b CM |
7114 | return hole_em; |
7115 | } | |
7116 | out: | |
7117 | ||
7118 | free_extent_map(hole_em); | |
7119 | if (err) { | |
7120 | free_extent_map(em); | |
7121 | return ERR_PTR(err); | |
7122 | } | |
7123 | return em; | |
7124 | } | |
7125 | ||
5f9a8a51 FM |
7126 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7127 | const u64 start, | |
7128 | const u64 len, | |
7129 | const u64 orig_start, | |
7130 | const u64 block_start, | |
7131 | const u64 block_len, | |
7132 | const u64 orig_block_len, | |
7133 | const u64 ram_bytes, | |
7134 | const int type) | |
7135 | { | |
7136 | struct extent_map *em = NULL; | |
7137 | int ret; | |
7138 | ||
5f9a8a51 | 7139 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7140 | em = create_io_em(inode, start, len, orig_start, |
7141 | block_start, block_len, orig_block_len, | |
7142 | ram_bytes, | |
7143 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7144 | type); | |
5f9a8a51 FM |
7145 | if (IS_ERR(em)) |
7146 | goto out; | |
7147 | } | |
7148 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7149 | len, block_len, type); | |
7150 | if (ret) { | |
7151 | if (em) { | |
7152 | free_extent_map(em); | |
dcdbc059 | 7153 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7154 | start + len - 1, 0); |
7155 | } | |
7156 | em = ERR_PTR(ret); | |
7157 | } | |
7158 | out: | |
5f9a8a51 FM |
7159 | |
7160 | return em; | |
7161 | } | |
7162 | ||
4b46fce2 JB |
7163 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7164 | u64 start, u64 len) | |
7165 | { | |
0b246afa | 7166 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7167 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7168 | struct extent_map *em; |
4b46fce2 JB |
7169 | struct btrfs_key ins; |
7170 | u64 alloc_hint; | |
7171 | int ret; | |
4b46fce2 | 7172 | |
4b46fce2 | 7173 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7174 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7175 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7176 | if (ret) |
7177 | return ERR_PTR(ret); | |
4b46fce2 | 7178 | |
5f9a8a51 FM |
7179 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7180 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7181 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7182 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7183 | if (IS_ERR(em)) |
2ff7e61e JM |
7184 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7185 | ins.offset, 1); | |
de0ee0ed | 7186 | |
4b46fce2 JB |
7187 | return em; |
7188 | } | |
7189 | ||
46bfbb5c CM |
7190 | /* |
7191 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7192 | * block must be cow'd | |
7193 | */ | |
00361589 | 7194 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7195 | u64 *orig_start, u64 *orig_block_len, |
7196 | u64 *ram_bytes) | |
46bfbb5c | 7197 | { |
2ff7e61e | 7198 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7199 | struct btrfs_path *path; |
7200 | int ret; | |
7201 | struct extent_buffer *leaf; | |
7202 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7203 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7204 | struct btrfs_file_extent_item *fi; |
7205 | struct btrfs_key key; | |
7206 | u64 disk_bytenr; | |
7207 | u64 backref_offset; | |
7208 | u64 extent_end; | |
7209 | u64 num_bytes; | |
7210 | int slot; | |
7211 | int found_type; | |
7ee9e440 | 7212 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7213 | |
46bfbb5c CM |
7214 | path = btrfs_alloc_path(); |
7215 | if (!path) | |
7216 | return -ENOMEM; | |
7217 | ||
f85b7379 DS |
7218 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7219 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7220 | if (ret < 0) |
7221 | goto out; | |
7222 | ||
7223 | slot = path->slots[0]; | |
7224 | if (ret == 1) { | |
7225 | if (slot == 0) { | |
7226 | /* can't find the item, must cow */ | |
7227 | ret = 0; | |
7228 | goto out; | |
7229 | } | |
7230 | slot--; | |
7231 | } | |
7232 | ret = 0; | |
7233 | leaf = path->nodes[0]; | |
7234 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7235 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7236 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7237 | /* not our file or wrong item type, must cow */ | |
7238 | goto out; | |
7239 | } | |
7240 | ||
7241 | if (key.offset > offset) { | |
7242 | /* Wrong offset, must cow */ | |
7243 | goto out; | |
7244 | } | |
7245 | ||
7246 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7247 | found_type = btrfs_file_extent_type(leaf, fi); | |
7248 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7249 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7250 | /* not a regular extent, must cow */ | |
7251 | goto out; | |
7252 | } | |
7ee9e440 JB |
7253 | |
7254 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7255 | goto out; | |
7256 | ||
e77751aa MX |
7257 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7258 | if (extent_end <= offset) | |
7259 | goto out; | |
7260 | ||
46bfbb5c | 7261 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7262 | if (disk_bytenr == 0) |
7263 | goto out; | |
7264 | ||
7265 | if (btrfs_file_extent_compression(leaf, fi) || | |
7266 | btrfs_file_extent_encryption(leaf, fi) || | |
7267 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7268 | goto out; | |
7269 | ||
78d4295b EL |
7270 | /* |
7271 | * Do the same check as in btrfs_cross_ref_exist but without the | |
7272 | * unnecessary search. | |
7273 | */ | |
7274 | if (btrfs_file_extent_generation(leaf, fi) <= | |
7275 | btrfs_root_last_snapshot(&root->root_item)) | |
7276 | goto out; | |
7277 | ||
46bfbb5c CM |
7278 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7279 | ||
7ee9e440 JB |
7280 | if (orig_start) { |
7281 | *orig_start = key.offset - backref_offset; | |
7282 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7283 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7284 | } | |
eb384b55 | 7285 | |
2ff7e61e | 7286 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7287 | goto out; |
7b2b7085 MX |
7288 | |
7289 | num_bytes = min(offset + *len, extent_end) - offset; | |
7290 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7291 | u64 range_end; | |
7292 | ||
da17066c JM |
7293 | range_end = round_up(offset + num_bytes, |
7294 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7295 | ret = test_range_bit(io_tree, offset, range_end, |
7296 | EXTENT_DELALLOC, 0, NULL); | |
7297 | if (ret) { | |
7298 | ret = -EAGAIN; | |
7299 | goto out; | |
7300 | } | |
7301 | } | |
7302 | ||
1bda19eb | 7303 | btrfs_release_path(path); |
46bfbb5c CM |
7304 | |
7305 | /* | |
7306 | * look for other files referencing this extent, if we | |
7307 | * find any we must cow | |
7308 | */ | |
00361589 | 7309 | |
e4c3b2dc | 7310 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7311 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7312 | if (ret) { |
7313 | ret = 0; | |
7314 | goto out; | |
7315 | } | |
46bfbb5c CM |
7316 | |
7317 | /* | |
7318 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7319 | * in this extent we are about to write. If there | |
7320 | * are any csums in that range we have to cow in order | |
7321 | * to keep the csums correct | |
7322 | */ | |
7323 | disk_bytenr += backref_offset; | |
7324 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7325 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7326 | goto out; | |
46bfbb5c CM |
7327 | /* |
7328 | * all of the above have passed, it is safe to overwrite this extent | |
7329 | * without cow | |
7330 | */ | |
eb384b55 | 7331 | *len = num_bytes; |
46bfbb5c CM |
7332 | ret = 1; |
7333 | out: | |
7334 | btrfs_free_path(path); | |
7335 | return ret; | |
7336 | } | |
7337 | ||
eb838e73 JB |
7338 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7339 | struct extent_state **cached_state, int writing) | |
7340 | { | |
7341 | struct btrfs_ordered_extent *ordered; | |
7342 | int ret = 0; | |
7343 | ||
7344 | while (1) { | |
7345 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7346 | cached_state); |
eb838e73 JB |
7347 | /* |
7348 | * We're concerned with the entire range that we're going to be | |
01327610 | 7349 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7350 | * extents in this range. |
7351 | */ | |
a776c6fa | 7352 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7353 | lockend - lockstart + 1); |
7354 | ||
7355 | /* | |
7356 | * We need to make sure there are no buffered pages in this | |
7357 | * range either, we could have raced between the invalidate in | |
7358 | * generic_file_direct_write and locking the extent. The | |
7359 | * invalidate needs to happen so that reads after a write do not | |
7360 | * get stale data. | |
7361 | */ | |
fc4adbff | 7362 | if (!ordered && |
051c98eb DS |
7363 | (!writing || !filemap_range_has_page(inode->i_mapping, |
7364 | lockstart, lockend))) | |
eb838e73 JB |
7365 | break; |
7366 | ||
7367 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 7368 | cached_state); |
eb838e73 JB |
7369 | |
7370 | if (ordered) { | |
ade77029 FM |
7371 | /* |
7372 | * If we are doing a DIO read and the ordered extent we | |
7373 | * found is for a buffered write, we can not wait for it | |
7374 | * to complete and retry, because if we do so we can | |
7375 | * deadlock with concurrent buffered writes on page | |
7376 | * locks. This happens only if our DIO read covers more | |
7377 | * than one extent map, if at this point has already | |
7378 | * created an ordered extent for a previous extent map | |
7379 | * and locked its range in the inode's io tree, and a | |
7380 | * concurrent write against that previous extent map's | |
7381 | * range and this range started (we unlock the ranges | |
7382 | * in the io tree only when the bios complete and | |
7383 | * buffered writes always lock pages before attempting | |
7384 | * to lock range in the io tree). | |
7385 | */ | |
7386 | if (writing || | |
7387 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7388 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7389 | else | |
7390 | ret = -ENOTBLK; | |
eb838e73 JB |
7391 | btrfs_put_ordered_extent(ordered); |
7392 | } else { | |
eb838e73 | 7393 | /* |
b850ae14 FM |
7394 | * We could trigger writeback for this range (and wait |
7395 | * for it to complete) and then invalidate the pages for | |
7396 | * this range (through invalidate_inode_pages2_range()), | |
7397 | * but that can lead us to a deadlock with a concurrent | |
7398 | * call to readpages() (a buffered read or a defrag call | |
7399 | * triggered a readahead) on a page lock due to an | |
7400 | * ordered dio extent we created before but did not have | |
7401 | * yet a corresponding bio submitted (whence it can not | |
7402 | * complete), which makes readpages() wait for that | |
7403 | * ordered extent to complete while holding a lock on | |
7404 | * that page. | |
eb838e73 | 7405 | */ |
b850ae14 | 7406 | ret = -ENOTBLK; |
eb838e73 JB |
7407 | } |
7408 | ||
ade77029 FM |
7409 | if (ret) |
7410 | break; | |
7411 | ||
eb838e73 JB |
7412 | cond_resched(); |
7413 | } | |
7414 | ||
7415 | return ret; | |
7416 | } | |
7417 | ||
6f9994db LB |
7418 | /* The callers of this must take lock_extent() */ |
7419 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7420 | u64 orig_start, u64 block_start, | |
7421 | u64 block_len, u64 orig_block_len, | |
7422 | u64 ram_bytes, int compress_type, | |
7423 | int type) | |
69ffb543 JB |
7424 | { |
7425 | struct extent_map_tree *em_tree; | |
7426 | struct extent_map *em; | |
7427 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7428 | int ret; | |
7429 | ||
6f9994db LB |
7430 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7431 | type == BTRFS_ORDERED_COMPRESSED || | |
7432 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7433 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7434 | |
69ffb543 JB |
7435 | em_tree = &BTRFS_I(inode)->extent_tree; |
7436 | em = alloc_extent_map(); | |
7437 | if (!em) | |
7438 | return ERR_PTR(-ENOMEM); | |
7439 | ||
7440 | em->start = start; | |
7441 | em->orig_start = orig_start; | |
7442 | em->len = len; | |
7443 | em->block_len = block_len; | |
7444 | em->block_start = block_start; | |
7445 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7446 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7447 | em->ram_bytes = ram_bytes; |
70c8a91c | 7448 | em->generation = -1; |
69ffb543 | 7449 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7450 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7451 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7452 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7453 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7454 | em->compress_type = compress_type; | |
7455 | } | |
69ffb543 JB |
7456 | |
7457 | do { | |
dcdbc059 | 7458 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7459 | em->start + em->len - 1, 0); |
7460 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7461 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7462 | write_unlock(&em_tree->lock); |
6f9994db LB |
7463 | /* |
7464 | * The caller has taken lock_extent(), who could race with us | |
7465 | * to add em? | |
7466 | */ | |
69ffb543 JB |
7467 | } while (ret == -EEXIST); |
7468 | ||
7469 | if (ret) { | |
7470 | free_extent_map(em); | |
7471 | return ERR_PTR(ret); | |
7472 | } | |
7473 | ||
6f9994db | 7474 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7475 | return em; |
7476 | } | |
7477 | ||
1c8d0175 NB |
7478 | |
7479 | static int btrfs_get_blocks_direct_read(struct extent_map *em, | |
7480 | struct buffer_head *bh_result, | |
7481 | struct inode *inode, | |
7482 | u64 start, u64 len) | |
7483 | { | |
7484 | if (em->block_start == EXTENT_MAP_HOLE || | |
7485 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7486 | return -ENOENT; | |
7487 | ||
7488 | len = min(len, em->len - (start - em->start)); | |
7489 | ||
7490 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7491 | inode->i_blkbits; | |
7492 | bh_result->b_size = len; | |
7493 | bh_result->b_bdev = em->bdev; | |
7494 | set_buffer_mapped(bh_result); | |
7495 | ||
7496 | return 0; | |
7497 | } | |
7498 | ||
c5794e51 NB |
7499 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
7500 | struct buffer_head *bh_result, | |
7501 | struct inode *inode, | |
7502 | struct btrfs_dio_data *dio_data, | |
7503 | u64 start, u64 len) | |
7504 | { | |
7505 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
7506 | struct extent_map *em = *map; | |
7507 | int ret = 0; | |
7508 | ||
7509 | /* | |
7510 | * We don't allocate a new extent in the following cases | |
7511 | * | |
7512 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7513 | * existing extent. | |
7514 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7515 | * just use the extent. | |
7516 | * | |
7517 | */ | |
7518 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7519 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7520 | em->block_start != EXTENT_MAP_HOLE)) { | |
7521 | int type; | |
7522 | u64 block_start, orig_start, orig_block_len, ram_bytes; | |
7523 | ||
7524 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7525 | type = BTRFS_ORDERED_PREALLOC; | |
7526 | else | |
7527 | type = BTRFS_ORDERED_NOCOW; | |
7528 | len = min(len, em->len - (start - em->start)); | |
7529 | block_start = em->block_start + (start - em->start); | |
7530 | ||
7531 | if (can_nocow_extent(inode, start, &len, &orig_start, | |
7532 | &orig_block_len, &ram_bytes) == 1 && | |
7533 | btrfs_inc_nocow_writers(fs_info, block_start)) { | |
7534 | struct extent_map *em2; | |
7535 | ||
7536 | em2 = btrfs_create_dio_extent(inode, start, len, | |
7537 | orig_start, block_start, | |
7538 | len, orig_block_len, | |
7539 | ram_bytes, type); | |
7540 | btrfs_dec_nocow_writers(fs_info, block_start); | |
7541 | if (type == BTRFS_ORDERED_PREALLOC) { | |
7542 | free_extent_map(em); | |
7543 | *map = em = em2; | |
7544 | } | |
7545 | ||
7546 | if (em2 && IS_ERR(em2)) { | |
7547 | ret = PTR_ERR(em2); | |
7548 | goto out; | |
7549 | } | |
7550 | /* | |
7551 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7552 | * use the existing or preallocated extent, so does not | |
7553 | * need to adjust btrfs_space_info's bytes_may_use. | |
7554 | */ | |
7555 | btrfs_free_reserved_data_space_noquota(inode, start, | |
7556 | len); | |
7557 | goto skip_cow; | |
7558 | } | |
7559 | } | |
7560 | ||
7561 | /* this will cow the extent */ | |
7562 | len = bh_result->b_size; | |
7563 | free_extent_map(em); | |
7564 | *map = em = btrfs_new_extent_direct(inode, start, len); | |
7565 | if (IS_ERR(em)) { | |
7566 | ret = PTR_ERR(em); | |
7567 | goto out; | |
7568 | } | |
7569 | ||
7570 | len = min(len, em->len - (start - em->start)); | |
7571 | ||
7572 | skip_cow: | |
7573 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | |
7574 | inode->i_blkbits; | |
7575 | bh_result->b_size = len; | |
7576 | bh_result->b_bdev = em->bdev; | |
7577 | set_buffer_mapped(bh_result); | |
7578 | ||
7579 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7580 | set_buffer_new(bh_result); | |
7581 | ||
7582 | /* | |
7583 | * Need to update the i_size under the extent lock so buffered | |
7584 | * readers will get the updated i_size when we unlock. | |
7585 | */ | |
7586 | if (!dio_data->overwrite && start + len > i_size_read(inode)) | |
7587 | i_size_write(inode, start + len); | |
7588 | ||
7589 | WARN_ON(dio_data->reserve < len); | |
7590 | dio_data->reserve -= len; | |
7591 | dio_data->unsubmitted_oe_range_end = start + len; | |
7592 | current->journal_info = dio_data; | |
7593 | out: | |
7594 | return ret; | |
7595 | } | |
7596 | ||
4b46fce2 JB |
7597 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7598 | struct buffer_head *bh_result, int create) | |
7599 | { | |
0b246afa | 7600 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7601 | struct extent_map *em; |
eb838e73 | 7602 | struct extent_state *cached_state = NULL; |
50745b0a | 7603 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7604 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7605 | u64 lockstart, lockend; |
4b46fce2 | 7606 | u64 len = bh_result->b_size; |
eb838e73 | 7607 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7608 | int ret = 0; |
eb838e73 | 7609 | |
172a5049 | 7610 | if (create) |
3266789f | 7611 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7612 | else |
0b246afa | 7613 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7614 | |
c329861d JB |
7615 | lockstart = start; |
7616 | lockend = start + len - 1; | |
7617 | ||
e1cbbfa5 JB |
7618 | if (current->journal_info) { |
7619 | /* | |
7620 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7621 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7622 | * confused. |
7623 | */ | |
50745b0a | 7624 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7625 | current->journal_info = NULL; |
7626 | } | |
7627 | ||
eb838e73 JB |
7628 | /* |
7629 | * If this errors out it's because we couldn't invalidate pagecache for | |
7630 | * this range and we need to fallback to buffered. | |
7631 | */ | |
9c9464cc FM |
7632 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7633 | create)) { | |
7634 | ret = -ENOTBLK; | |
7635 | goto err; | |
7636 | } | |
eb838e73 | 7637 | |
fc4f21b1 | 7638 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7639 | if (IS_ERR(em)) { |
7640 | ret = PTR_ERR(em); | |
7641 | goto unlock_err; | |
7642 | } | |
4b46fce2 JB |
7643 | |
7644 | /* | |
7645 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7646 | * io. INLINE is special, and we could probably kludge it in here, but | |
7647 | * it's still buffered so for safety lets just fall back to the generic | |
7648 | * buffered path. | |
7649 | * | |
7650 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7651 | * decompress it, so there will be buffering required no matter what we | |
7652 | * do, so go ahead and fallback to buffered. | |
7653 | * | |
01327610 | 7654 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7655 | * to buffered IO. Don't blame me, this is the price we pay for using |
7656 | * the generic code. | |
7657 | */ | |
7658 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7659 | em->block_start == EXTENT_MAP_INLINE) { | |
7660 | free_extent_map(em); | |
eb838e73 JB |
7661 | ret = -ENOTBLK; |
7662 | goto unlock_err; | |
4b46fce2 JB |
7663 | } |
7664 | ||
c5794e51 NB |
7665 | if (create) { |
7666 | ret = btrfs_get_blocks_direct_write(&em, bh_result, inode, | |
7667 | dio_data, start, len); | |
7668 | if (ret < 0) | |
7669 | goto unlock_err; | |
7670 | ||
7671 | /* clear and unlock the entire range */ | |
7672 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7673 | unlock_bits, 1, 0, &cached_state); | |
7674 | } else { | |
1c8d0175 NB |
7675 | ret = btrfs_get_blocks_direct_read(em, bh_result, inode, |
7676 | start, len); | |
7677 | /* Can be negative only if we read from a hole */ | |
7678 | if (ret < 0) { | |
7679 | ret = 0; | |
7680 | free_extent_map(em); | |
7681 | goto unlock_err; | |
7682 | } | |
7683 | /* | |
7684 | * We need to unlock only the end area that we aren't using. | |
7685 | * The rest is going to be unlocked by the endio routine. | |
7686 | */ | |
7687 | lockstart = start + bh_result->b_size; | |
7688 | if (lockstart < lockend) { | |
7689 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | |
7690 | lockend, unlock_bits, 1, 0, | |
7691 | &cached_state); | |
7692 | } else { | |
7693 | free_extent_state(cached_state); | |
7694 | } | |
4b46fce2 JB |
7695 | } |
7696 | ||
4b46fce2 JB |
7697 | free_extent_map(em); |
7698 | ||
7699 | return 0; | |
eb838e73 JB |
7700 | |
7701 | unlock_err: | |
eb838e73 | 7702 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
ae0f1625 | 7703 | unlock_bits, 1, 0, &cached_state); |
9c9464cc | 7704 | err: |
50745b0a | 7705 | if (dio_data) |
7706 | current->journal_info = dio_data; | |
eb838e73 | 7707 | return ret; |
4b46fce2 JB |
7708 | } |
7709 | ||
58efbc9f OS |
7710 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
7711 | struct bio *bio, | |
7712 | int mirror_num) | |
8b110e39 | 7713 | { |
2ff7e61e | 7714 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7715 | blk_status_t ret; |
8b110e39 | 7716 | |
37226b21 | 7717 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 | 7718 | |
2ff7e61e | 7719 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 | 7720 | if (ret) |
ea057f6d | 7721 | return ret; |
8b110e39 | 7722 | |
2ff7e61e | 7723 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
ea057f6d | 7724 | |
8b110e39 MX |
7725 | return ret; |
7726 | } | |
7727 | ||
7728 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7729 | struct bio *failed_bio, | |
7730 | struct io_failure_record *failrec, | |
7731 | int failed_mirror) | |
7732 | { | |
ab8d0fc4 | 7733 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7734 | int num_copies; |
7735 | ||
ab8d0fc4 | 7736 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7737 | if (num_copies == 1) { |
7738 | /* | |
7739 | * we only have a single copy of the data, so don't bother with | |
7740 | * all the retry and error correction code that follows. no | |
7741 | * matter what the error is, it is very likely to persist. | |
7742 | */ | |
ab8d0fc4 JM |
7743 | btrfs_debug(fs_info, |
7744 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7745 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7746 | return 0; |
7747 | } | |
7748 | ||
7749 | failrec->failed_mirror = failed_mirror; | |
7750 | failrec->this_mirror++; | |
7751 | if (failrec->this_mirror == failed_mirror) | |
7752 | failrec->this_mirror++; | |
7753 | ||
7754 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7755 | btrfs_debug(fs_info, |
7756 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7757 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7758 | return 0; |
7759 | } | |
7760 | ||
7761 | return 1; | |
7762 | } | |
7763 | ||
58efbc9f OS |
7764 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
7765 | struct page *page, unsigned int pgoff, | |
7766 | u64 start, u64 end, int failed_mirror, | |
7767 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7768 | { |
7769 | struct io_failure_record *failrec; | |
7870d082 JB |
7770 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7771 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
7772 | struct bio *bio; |
7773 | int isector; | |
f1c77c55 | 7774 | unsigned int read_mode = 0; |
17347cec | 7775 | int segs; |
8b110e39 | 7776 | int ret; |
58efbc9f | 7777 | blk_status_t status; |
c16a8ac3 | 7778 | struct bio_vec bvec; |
8b110e39 | 7779 | |
37226b21 | 7780 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7781 | |
7782 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7783 | if (ret) | |
58efbc9f | 7784 | return errno_to_blk_status(ret); |
8b110e39 MX |
7785 | |
7786 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7787 | failed_mirror); | |
7788 | if (!ret) { | |
7870d082 | 7789 | free_io_failure(failure_tree, io_tree, failrec); |
58efbc9f | 7790 | return BLK_STS_IOERR; |
8b110e39 MX |
7791 | } |
7792 | ||
17347cec | 7793 | segs = bio_segments(failed_bio); |
c16a8ac3 | 7794 | bio_get_first_bvec(failed_bio, &bvec); |
17347cec | 7795 | if (segs > 1 || |
c16a8ac3 | 7796 | (bvec.bv_len > btrfs_inode_sectorsize(inode))) |
70fd7614 | 7797 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
7798 | |
7799 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7800 | isector >>= inode->i_sb->s_blocksize_bits; | |
7801 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 7802 | pgoff, isector, repair_endio, repair_arg); |
ebcc3263 | 7803 | bio->bi_opf = REQ_OP_READ | read_mode; |
8b110e39 MX |
7804 | |
7805 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
913e1535 | 7806 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
8b110e39 MX |
7807 | read_mode, failrec->this_mirror, failrec->in_validation); |
7808 | ||
58efbc9f OS |
7809 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
7810 | if (status) { | |
7870d082 | 7811 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
7812 | bio_put(bio); |
7813 | } | |
7814 | ||
58efbc9f | 7815 | return status; |
8b110e39 MX |
7816 | } |
7817 | ||
7818 | struct btrfs_retry_complete { | |
7819 | struct completion done; | |
7820 | struct inode *inode; | |
7821 | u64 start; | |
7822 | int uptodate; | |
7823 | }; | |
7824 | ||
4246a0b6 | 7825 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7826 | { |
7827 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 7828 | struct inode *inode = done->inode; |
8b110e39 | 7829 | struct bio_vec *bvec; |
7870d082 | 7830 | struct extent_io_tree *io_tree, *failure_tree; |
8b110e39 | 7831 | int i; |
6dc4f100 | 7832 | struct bvec_iter_all iter_all; |
8b110e39 | 7833 | |
4e4cbee9 | 7834 | if (bio->bi_status) |
8b110e39 MX |
7835 | goto end; |
7836 | ||
2dabb324 | 7837 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
7838 | io_tree = &BTRFS_I(inode)->io_tree; |
7839 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
263663cd | 7840 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode)); |
2dabb324 | 7841 | |
8b110e39 | 7842 | done->uptodate = 1; |
c09abff8 | 7843 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
6dc4f100 | 7844 | bio_for_each_segment_all(bvec, bio, i, iter_all) |
7870d082 JB |
7845 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
7846 | io_tree, done->start, bvec->bv_page, | |
7847 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
7848 | end: |
7849 | complete(&done->done); | |
7850 | bio_put(bio); | |
7851 | } | |
7852 | ||
58efbc9f OS |
7853 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
7854 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7855 | { |
2dabb324 | 7856 | struct btrfs_fs_info *fs_info; |
17347cec LB |
7857 | struct bio_vec bvec; |
7858 | struct bvec_iter iter; | |
8b110e39 | 7859 | struct btrfs_retry_complete done; |
4b46fce2 | 7860 | u64 start; |
2dabb324 CR |
7861 | unsigned int pgoff; |
7862 | u32 sectorsize; | |
7863 | int nr_sectors; | |
58efbc9f OS |
7864 | blk_status_t ret; |
7865 | blk_status_t err = BLK_STS_OK; | |
4b46fce2 | 7866 | |
2dabb324 | 7867 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 7868 | sectorsize = fs_info->sectorsize; |
2dabb324 | 7869 | |
8b110e39 MX |
7870 | start = io_bio->logical; |
7871 | done.inode = inode; | |
17347cec | 7872 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 7873 | |
17347cec LB |
7874 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
7875 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
7876 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
7877 | |
7878 | next_block_or_try_again: | |
8b110e39 MX |
7879 | done.uptodate = 0; |
7880 | done.start = start; | |
7881 | init_completion(&done.done); | |
7882 | ||
17347cec | 7883 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
7884 | pgoff, start, start + sectorsize - 1, |
7885 | io_bio->mirror_num, | |
7886 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
7887 | if (ret) { |
7888 | err = ret; | |
7889 | goto next; | |
7890 | } | |
8b110e39 | 7891 | |
9c17f6cd | 7892 | wait_for_completion_io(&done.done); |
8b110e39 MX |
7893 | |
7894 | if (!done.uptodate) { | |
7895 | /* We might have another mirror, so try again */ | |
2dabb324 | 7896 | goto next_block_or_try_again; |
8b110e39 MX |
7897 | } |
7898 | ||
629ebf4f | 7899 | next: |
2dabb324 CR |
7900 | start += sectorsize; |
7901 | ||
97bf5a55 LB |
7902 | nr_sectors--; |
7903 | if (nr_sectors) { | |
2dabb324 | 7904 | pgoff += sectorsize; |
97bf5a55 | 7905 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
7906 | goto next_block_or_try_again; |
7907 | } | |
8b110e39 MX |
7908 | } |
7909 | ||
629ebf4f | 7910 | return err; |
8b110e39 MX |
7911 | } |
7912 | ||
4246a0b6 | 7913 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
7914 | { |
7915 | struct btrfs_retry_complete *done = bio->bi_private; | |
7916 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
7917 | struct extent_io_tree *io_tree, *failure_tree; |
7918 | struct inode *inode = done->inode; | |
8b110e39 MX |
7919 | struct bio_vec *bvec; |
7920 | int uptodate; | |
7921 | int ret; | |
7922 | int i; | |
6dc4f100 | 7923 | struct bvec_iter_all iter_all; |
8b110e39 | 7924 | |
4e4cbee9 | 7925 | if (bio->bi_status) |
8b110e39 MX |
7926 | goto end; |
7927 | ||
7928 | uptodate = 1; | |
2dabb324 | 7929 | |
2dabb324 | 7930 | ASSERT(bio->bi_vcnt == 1); |
263663cd | 7931 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 7932 | |
7870d082 JB |
7933 | io_tree = &BTRFS_I(inode)->io_tree; |
7934 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
7935 | ||
c09abff8 | 7936 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
6dc4f100 | 7937 | bio_for_each_segment_all(bvec, bio, i, iter_all) { |
7870d082 JB |
7938 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
7939 | bvec->bv_offset, done->start, | |
7940 | bvec->bv_len); | |
8b110e39 | 7941 | if (!ret) |
7870d082 JB |
7942 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
7943 | failure_tree, io_tree, done->start, | |
7944 | bvec->bv_page, | |
7945 | btrfs_ino(BTRFS_I(inode)), | |
7946 | bvec->bv_offset); | |
8b110e39 MX |
7947 | else |
7948 | uptodate = 0; | |
7949 | } | |
7950 | ||
7951 | done->uptodate = uptodate; | |
7952 | end: | |
7953 | complete(&done->done); | |
7954 | bio_put(bio); | |
7955 | } | |
7956 | ||
4e4cbee9 CH |
7957 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
7958 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 | 7959 | { |
2dabb324 | 7960 | struct btrfs_fs_info *fs_info; |
17347cec LB |
7961 | struct bio_vec bvec; |
7962 | struct bvec_iter iter; | |
8b110e39 MX |
7963 | struct btrfs_retry_complete done; |
7964 | u64 start; | |
7965 | u64 offset = 0; | |
2dabb324 CR |
7966 | u32 sectorsize; |
7967 | int nr_sectors; | |
7968 | unsigned int pgoff; | |
7969 | int csum_pos; | |
ef7cdac1 | 7970 | bool uptodate = (err == 0); |
8b110e39 | 7971 | int ret; |
58efbc9f | 7972 | blk_status_t status; |
dc380aea | 7973 | |
2dabb324 | 7974 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 7975 | sectorsize = fs_info->sectorsize; |
2dabb324 | 7976 | |
58efbc9f | 7977 | err = BLK_STS_OK; |
c1dc0896 | 7978 | start = io_bio->logical; |
8b110e39 | 7979 | done.inode = inode; |
17347cec | 7980 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 7981 | |
17347cec LB |
7982 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
7983 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 7984 | |
17347cec | 7985 | pgoff = bvec.bv_offset; |
2dabb324 | 7986 | next_block: |
ef7cdac1 LB |
7987 | if (uptodate) { |
7988 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
7989 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
7990 | bvec.bv_page, pgoff, start, sectorsize); | |
7991 | if (likely(!ret)) | |
7992 | goto next; | |
7993 | } | |
8b110e39 MX |
7994 | try_again: |
7995 | done.uptodate = 0; | |
7996 | done.start = start; | |
7997 | init_completion(&done.done); | |
7998 | ||
58efbc9f OS |
7999 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
8000 | pgoff, start, start + sectorsize - 1, | |
8001 | io_bio->mirror_num, btrfs_retry_endio, | |
8002 | &done); | |
8003 | if (status) { | |
8004 | err = status; | |
8b110e39 MX |
8005 | goto next; |
8006 | } | |
8007 | ||
9c17f6cd | 8008 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8009 | |
8010 | if (!done.uptodate) { | |
8011 | /* We might have another mirror, so try again */ | |
8012 | goto try_again; | |
8013 | } | |
8014 | next: | |
2dabb324 CR |
8015 | offset += sectorsize; |
8016 | start += sectorsize; | |
8017 | ||
8018 | ASSERT(nr_sectors); | |
8019 | ||
97bf5a55 LB |
8020 | nr_sectors--; |
8021 | if (nr_sectors) { | |
2dabb324 | 8022 | pgoff += sectorsize; |
97bf5a55 | 8023 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8024 | goto next_block; |
8025 | } | |
2c30c71b | 8026 | } |
c1dc0896 MX |
8027 | |
8028 | return err; | |
8029 | } | |
8030 | ||
4e4cbee9 CH |
8031 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
8032 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 MX |
8033 | { |
8034 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8035 | ||
8036 | if (skip_csum) { | |
8037 | if (unlikely(err)) | |
8038 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8039 | else | |
58efbc9f | 8040 | return BLK_STS_OK; |
8b110e39 MX |
8041 | } else { |
8042 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8043 | } | |
8044 | } | |
8045 | ||
4246a0b6 | 8046 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8047 | { |
8048 | struct btrfs_dio_private *dip = bio->bi_private; | |
8049 | struct inode *inode = dip->inode; | |
8050 | struct bio *dio_bio; | |
8051 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4e4cbee9 | 8052 | blk_status_t err = bio->bi_status; |
c1dc0896 | 8053 | |
99c4e3b9 | 8054 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8b110e39 | 8055 | err = btrfs_subio_endio_read(inode, io_bio, err); |
c1dc0896 | 8056 | |
4b46fce2 | 8057 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8058 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8059 | dio_bio = dip->dio_bio; |
4b46fce2 | 8060 | |
4b46fce2 | 8061 | kfree(dip); |
c0da7aa1 | 8062 | |
99c4e3b9 | 8063 | dio_bio->bi_status = err; |
4055351c | 8064 | dio_end_io(dio_bio); |
b3a0dd50 | 8065 | btrfs_io_bio_free_csum(io_bio); |
9be3395b | 8066 | bio_put(bio); |
4b46fce2 JB |
8067 | } |
8068 | ||
52427260 QW |
8069 | static void __endio_write_update_ordered(struct inode *inode, |
8070 | const u64 offset, const u64 bytes, | |
8071 | const bool uptodate) | |
4b46fce2 | 8072 | { |
0b246afa | 8073 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8074 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 QW |
8075 | struct btrfs_workqueue *wq; |
8076 | btrfs_work_func_t func; | |
14543774 FM |
8077 | u64 ordered_offset = offset; |
8078 | u64 ordered_bytes = bytes; | |
67c003f9 | 8079 | u64 last_offset; |
4b46fce2 | 8080 | |
52427260 QW |
8081 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
8082 | wq = fs_info->endio_freespace_worker; | |
8083 | func = btrfs_freespace_write_helper; | |
8084 | } else { | |
8085 | wq = fs_info->endio_write_workers; | |
8086 | func = btrfs_endio_write_helper; | |
8087 | } | |
8088 | ||
b25f0d00 NB |
8089 | while (ordered_offset < offset + bytes) { |
8090 | last_offset = ordered_offset; | |
8091 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8092 | &ordered_offset, | |
8093 | ordered_bytes, | |
8094 | uptodate)) { | |
8095 | btrfs_init_work(&ordered->work, func, | |
8096 | finish_ordered_fn, | |
8097 | NULL, NULL); | |
8098 | btrfs_queue_work(wq, &ordered->work); | |
8099 | } | |
8100 | /* | |
8101 | * If btrfs_dec_test_ordered_pending does not find any ordered | |
8102 | * extent in the range, we can exit. | |
8103 | */ | |
8104 | if (ordered_offset == last_offset) | |
8105 | return; | |
8106 | /* | |
8107 | * Our bio might span multiple ordered extents. In this case | |
52042d8e | 8108 | * we keep going until we have accounted the whole dio. |
b25f0d00 NB |
8109 | */ |
8110 | if (ordered_offset < offset + bytes) { | |
8111 | ordered_bytes = offset + bytes - ordered_offset; | |
8112 | ordered = NULL; | |
8113 | } | |
163cf09c | 8114 | } |
14543774 FM |
8115 | } |
8116 | ||
8117 | static void btrfs_endio_direct_write(struct bio *bio) | |
8118 | { | |
8119 | struct btrfs_dio_private *dip = bio->bi_private; | |
8120 | struct bio *dio_bio = dip->dio_bio; | |
8121 | ||
52427260 | 8122 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
4e4cbee9 | 8123 | dip->bytes, !bio->bi_status); |
4b46fce2 | 8124 | |
4b46fce2 | 8125 | kfree(dip); |
c0da7aa1 | 8126 | |
4e4cbee9 | 8127 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8128 | dio_end_io(dio_bio); |
9be3395b | 8129 | bio_put(bio); |
4b46fce2 JB |
8130 | } |
8131 | ||
d0ee3934 | 8132 | static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, |
d0779291 | 8133 | struct bio *bio, u64 offset) |
eaf25d93 | 8134 | { |
c6100a4b | 8135 | struct inode *inode = private_data; |
4e4cbee9 | 8136 | blk_status_t ret; |
2ff7e61e | 8137 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8138 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8139 | return 0; |
8140 | } | |
8141 | ||
4246a0b6 | 8142 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8143 | { |
8144 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 8145 | blk_status_t err = bio->bi_status; |
e65e1535 | 8146 | |
8b110e39 MX |
8147 | if (err) |
8148 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8149 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8150 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8151 | bio->bi_opf, | |
8b110e39 MX |
8152 | (unsigned long long)bio->bi_iter.bi_sector, |
8153 | bio->bi_iter.bi_size, err); | |
8154 | ||
8155 | if (dip->subio_endio) | |
8156 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8157 | |
8158 | if (err) { | |
e65e1535 | 8159 | /* |
de224b7c NB |
8160 | * We want to perceive the errors flag being set before |
8161 | * decrementing the reference count. We don't need a barrier | |
8162 | * since atomic operations with a return value are fully | |
8163 | * ordered as per atomic_t.txt | |
e65e1535 | 8164 | */ |
de224b7c | 8165 | dip->errors = 1; |
e65e1535 MX |
8166 | } |
8167 | ||
8168 | /* if there are more bios still pending for this dio, just exit */ | |
8169 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8170 | goto out; | |
8171 | ||
9be3395b | 8172 | if (dip->errors) { |
e65e1535 | 8173 | bio_io_error(dip->orig_bio); |
9be3395b | 8174 | } else { |
2dbe0c77 | 8175 | dip->dio_bio->bi_status = BLK_STS_OK; |
4246a0b6 | 8176 | bio_endio(dip->orig_bio); |
e65e1535 MX |
8177 | } |
8178 | out: | |
8179 | bio_put(bio); | |
8180 | } | |
8181 | ||
4e4cbee9 | 8182 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8183 | struct btrfs_dio_private *dip, |
8184 | struct bio *bio, | |
8185 | u64 file_offset) | |
8186 | { | |
8187 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8188 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
4e4cbee9 | 8189 | blk_status_t ret; |
c1dc0896 MX |
8190 | |
8191 | /* | |
8192 | * We load all the csum data we need when we submit | |
8193 | * the first bio to reduce the csum tree search and | |
8194 | * contention. | |
8195 | */ | |
8196 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8197 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8198 | file_offset); |
8199 | if (ret) | |
8200 | return ret; | |
8201 | } | |
8202 | ||
8203 | if (bio == dip->orig_bio) | |
8204 | return 0; | |
8205 | ||
8206 | file_offset -= dip->logical_offset; | |
8207 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8208 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8209 | ||
8210 | return 0; | |
8211 | } | |
8212 | ||
d0ee3934 DS |
8213 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
8214 | struct inode *inode, u64 file_offset, int async_submit) | |
e65e1535 | 8215 | { |
0b246afa | 8216 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8217 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8218 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 8219 | blk_status_t ret; |
e65e1535 | 8220 | |
4c274bc6 | 8221 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
b812ce28 JB |
8222 | if (async_submit) |
8223 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8224 | ||
5fd02043 | 8225 | if (!write) { |
0b246afa | 8226 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8227 | if (ret) |
8228 | goto err; | |
8229 | } | |
e65e1535 | 8230 | |
e6961cac | 8231 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
8232 | goto map; |
8233 | ||
8234 | if (write && async_submit) { | |
c6100a4b JB |
8235 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8236 | file_offset, inode, | |
e288c080 | 8237 | btrfs_submit_bio_start_direct_io); |
e65e1535 | 8238 | goto err; |
1ae39938 JB |
8239 | } else if (write) { |
8240 | /* | |
8241 | * If we aren't doing async submit, calculate the csum of the | |
8242 | * bio now. | |
8243 | */ | |
2ff7e61e | 8244 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8245 | if (ret) |
8246 | goto err; | |
23ea8e5a | 8247 | } else { |
2ff7e61e | 8248 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8249 | file_offset); |
c2db1073 TI |
8250 | if (ret) |
8251 | goto err; | |
8252 | } | |
1ae39938 | 8253 | map: |
9b4a9b28 | 8254 | ret = btrfs_map_bio(fs_info, bio, 0, 0); |
e65e1535 | 8255 | err: |
e65e1535 MX |
8256 | return ret; |
8257 | } | |
8258 | ||
e6961cac | 8259 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
e65e1535 MX |
8260 | { |
8261 | struct inode *inode = dip->inode; | |
0b246afa | 8262 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8263 | struct bio *bio; |
8264 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8265 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8266 | u64 file_offset = dip->logical_offset; |
e65e1535 | 8267 | u64 map_length; |
1ae39938 | 8268 | int async_submit = 0; |
725130ba LB |
8269 | u64 submit_len; |
8270 | int clone_offset = 0; | |
8271 | int clone_len; | |
5f4dc8fc | 8272 | int ret; |
58efbc9f | 8273 | blk_status_t status; |
e65e1535 | 8274 | |
4f024f37 | 8275 | map_length = orig_bio->bi_iter.bi_size; |
725130ba | 8276 | submit_len = map_length; |
0b246afa JM |
8277 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8278 | &map_length, NULL, 0); | |
7a5c3c9b | 8279 | if (ret) |
e65e1535 | 8280 | return -EIO; |
facc8a22 | 8281 | |
725130ba | 8282 | if (map_length >= submit_len) { |
02f57c7a | 8283 | bio = orig_bio; |
c1dc0896 | 8284 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8285 | goto submit; |
8286 | } | |
8287 | ||
53b381b3 | 8288 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8289 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8290 | async_submit = 0; |
8291 | else | |
8292 | async_submit = 1; | |
8293 | ||
725130ba LB |
8294 | /* bio split */ |
8295 | ASSERT(map_length <= INT_MAX); | |
02f57c7a | 8296 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8297 | do { |
725130ba | 8298 | clone_len = min_t(int, submit_len, map_length); |
02f57c7a | 8299 | |
725130ba LB |
8300 | /* |
8301 | * This will never fail as it's passing GPF_NOFS and | |
8302 | * the allocation is backed by btrfs_bioset. | |
8303 | */ | |
e477094f | 8304 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8305 | clone_len); |
8306 | bio->bi_private = dip; | |
8307 | bio->bi_end_io = btrfs_end_dio_bio; | |
8308 | btrfs_io_bio(bio)->logical = file_offset; | |
8309 | ||
8310 | ASSERT(submit_len >= clone_len); | |
8311 | submit_len -= clone_len; | |
8312 | if (submit_len == 0) | |
8313 | break; | |
e65e1535 | 8314 | |
725130ba LB |
8315 | /* |
8316 | * Increase the count before we submit the bio so we know | |
8317 | * the end IO handler won't happen before we increase the | |
8318 | * count. Otherwise, the dip might get freed before we're | |
8319 | * done setting it up. | |
8320 | */ | |
8321 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8322 | |
d0ee3934 | 8323 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
8324 | async_submit); |
8325 | if (status) { | |
725130ba LB |
8326 | bio_put(bio); |
8327 | atomic_dec(&dip->pending_bios); | |
8328 | goto out_err; | |
8329 | } | |
e65e1535 | 8330 | |
725130ba LB |
8331 | clone_offset += clone_len; |
8332 | start_sector += clone_len >> 9; | |
8333 | file_offset += clone_len; | |
5f4dc8fc | 8334 | |
725130ba LB |
8335 | map_length = submit_len; |
8336 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8337 | start_sector << 9, &map_length, NULL, 0); | |
8338 | if (ret) | |
8339 | goto out_err; | |
3c91ee69 | 8340 | } while (submit_len > 0); |
e65e1535 | 8341 | |
02f57c7a | 8342 | submit: |
d0ee3934 | 8343 | status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
58efbc9f | 8344 | if (!status) |
e65e1535 MX |
8345 | return 0; |
8346 | ||
8347 | bio_put(bio); | |
8348 | out_err: | |
8349 | dip->errors = 1; | |
8350 | /* | |
de224b7c NB |
8351 | * Before atomic variable goto zero, we must make sure dip->errors is |
8352 | * perceived to be set. This ordering is ensured by the fact that an | |
8353 | * atomic operations with a return value are fully ordered as per | |
8354 | * atomic_t.txt | |
e65e1535 | 8355 | */ |
e65e1535 MX |
8356 | if (atomic_dec_and_test(&dip->pending_bios)) |
8357 | bio_io_error(dip->orig_bio); | |
8358 | ||
8359 | /* bio_end_io() will handle error, so we needn't return it */ | |
8360 | return 0; | |
8361 | } | |
8362 | ||
8a4c1e42 MC |
8363 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8364 | loff_t file_offset) | |
4b46fce2 | 8365 | { |
61de718f | 8366 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8367 | struct bio *bio = NULL; |
8368 | struct btrfs_io_bio *io_bio; | |
8a4c1e42 | 8369 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8370 | int ret = 0; |
8371 | ||
8b6c1d56 | 8372 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8373 | |
c1dc0896 | 8374 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8375 | if (!dip) { |
8376 | ret = -ENOMEM; | |
61de718f | 8377 | goto free_ordered; |
4b46fce2 | 8378 | } |
4b46fce2 | 8379 | |
9be3395b | 8380 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8381 | dip->inode = inode; |
8382 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8383 | dip->bytes = dio_bio->bi_iter.bi_size; |
8384 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8385 | bio->bi_private = dip; |
8386 | dip->orig_bio = bio; | |
9be3395b | 8387 | dip->dio_bio = dio_bio; |
e65e1535 | 8388 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8389 | io_bio = btrfs_io_bio(bio); |
8390 | io_bio->logical = file_offset; | |
4b46fce2 | 8391 | |
c1dc0896 | 8392 | if (write) { |
3892ac90 | 8393 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8394 | } else { |
3892ac90 | 8395 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8396 | dip->subio_endio = btrfs_subio_endio_read; |
8397 | } | |
4b46fce2 | 8398 | |
f28a4928 FM |
8399 | /* |
8400 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8401 | * even if we fail to submit a bio, because in such case we do the | |
8402 | * corresponding error handling below and it must not be done a second | |
8403 | * time by btrfs_direct_IO(). | |
8404 | */ | |
8405 | if (write) { | |
8406 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8407 | ||
8408 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8409 | dip->bytes; | |
8410 | dio_data->unsubmitted_oe_range_start = | |
8411 | dio_data->unsubmitted_oe_range_end; | |
8412 | } | |
8413 | ||
e6961cac | 8414 | ret = btrfs_submit_direct_hook(dip); |
e65e1535 | 8415 | if (!ret) |
eaf25d93 | 8416 | return; |
9be3395b | 8417 | |
b3a0dd50 | 8418 | btrfs_io_bio_free_csum(io_bio); |
9be3395b | 8419 | |
4b46fce2 JB |
8420 | free_ordered: |
8421 | /* | |
61de718f FM |
8422 | * If we arrived here it means either we failed to submit the dip |
8423 | * or we either failed to clone the dio_bio or failed to allocate the | |
8424 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8425 | * call bio_endio against our io_bio so that we get proper resource | |
8426 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8427 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8428 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8429 | */ |
3892ac90 | 8430 | if (bio && dip) { |
054ec2f6 | 8431 | bio_io_error(bio); |
61de718f | 8432 | /* |
3892ac90 | 8433 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8434 | * and all the cleanup and final put for dio_bio (through |
8435 | * dio_end_io()). | |
8436 | */ | |
8437 | dip = NULL; | |
3892ac90 | 8438 | bio = NULL; |
61de718f | 8439 | } else { |
14543774 | 8440 | if (write) |
52427260 | 8441 | __endio_write_update_ordered(inode, |
14543774 FM |
8442 | file_offset, |
8443 | dio_bio->bi_iter.bi_size, | |
52427260 | 8444 | false); |
14543774 | 8445 | else |
61de718f FM |
8446 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8447 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8448 | |
4e4cbee9 | 8449 | dio_bio->bi_status = BLK_STS_IOERR; |
61de718f FM |
8450 | /* |
8451 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8452 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8453 | */ | |
4055351c | 8454 | dio_end_io(dio_bio); |
4b46fce2 | 8455 | } |
3892ac90 LB |
8456 | if (bio) |
8457 | bio_put(bio); | |
61de718f | 8458 | kfree(dip); |
4b46fce2 JB |
8459 | } |
8460 | ||
2ff7e61e | 8461 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
2ff7e61e | 8462 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8463 | { |
8464 | int seg; | |
a1b75f7d | 8465 | int i; |
0b246afa | 8466 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8467 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8468 | |
8469 | if (offset & blocksize_mask) | |
8470 | goto out; | |
8471 | ||
28060d5d AV |
8472 | if (iov_iter_alignment(iter) & blocksize_mask) |
8473 | goto out; | |
a1b75f7d | 8474 | |
28060d5d | 8475 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8476 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8477 | return 0; |
8478 | /* | |
8479 | * Check to make sure we don't have duplicate iov_base's in this | |
8480 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8481 | * when reading back. | |
8482 | */ | |
8483 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8484 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8485 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8486 | goto out; |
8487 | } | |
5a5f79b5 CM |
8488 | } |
8489 | retval = 0; | |
8490 | out: | |
8491 | return retval; | |
8492 | } | |
eb838e73 | 8493 | |
c8b8e32d | 8494 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8495 | { |
4b46fce2 JB |
8496 | struct file *file = iocb->ki_filp; |
8497 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8498 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8499 | struct btrfs_dio_data dio_data = { 0 }; |
364ecf36 | 8500 | struct extent_changeset *data_reserved = NULL; |
c8b8e32d | 8501 | loff_t offset = iocb->ki_pos; |
0934856d | 8502 | size_t count = 0; |
2e60a51e | 8503 | int flags = 0; |
38851cc1 MX |
8504 | bool wakeup = true; |
8505 | bool relock = false; | |
0934856d | 8506 | ssize_t ret; |
4b46fce2 | 8507 | |
8c70c9f8 | 8508 | if (check_direct_IO(fs_info, iter, offset)) |
5a5f79b5 | 8509 | return 0; |
3f7c579c | 8510 | |
fe0f07d0 | 8511 | inode_dio_begin(inode); |
38851cc1 | 8512 | |
0e267c44 | 8513 | /* |
41bd9ca4 MX |
8514 | * The generic stuff only does filemap_write_and_wait_range, which |
8515 | * isn't enough if we've written compressed pages to this area, so | |
8516 | * we need to flush the dirty pages again to make absolutely sure | |
8517 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8518 | */ |
a6cbcd4a | 8519 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8520 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8521 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8522 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8523 | offset + count - 1); | |
0e267c44 | 8524 | |
6f673763 | 8525 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8526 | /* |
8527 | * If the write DIO is beyond the EOF, we need update | |
8528 | * the isize, but it is protected by i_mutex. So we can | |
8529 | * not unlock the i_mutex at this case. | |
8530 | */ | |
8531 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8532 | dio_data.overwrite = 1; |
5955102c | 8533 | inode_unlock(inode); |
38851cc1 | 8534 | relock = true; |
edf064e7 GR |
8535 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
8536 | ret = -EAGAIN; | |
8537 | goto out; | |
38851cc1 | 8538 | } |
364ecf36 QW |
8539 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8540 | offset, count); | |
0934856d | 8541 | if (ret) |
38851cc1 | 8542 | goto out; |
e1cbbfa5 JB |
8543 | |
8544 | /* | |
8545 | * We need to know how many extents we reserved so that we can | |
8546 | * do the accounting properly if we go over the number we | |
8547 | * originally calculated. Abuse current->journal_info for this. | |
8548 | */ | |
da17066c | 8549 | dio_data.reserve = round_up(count, |
0b246afa | 8550 | fs_info->sectorsize); |
f28a4928 FM |
8551 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8552 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8553 | current->journal_info = &dio_data; |
97dcdea0 | 8554 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8555 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8556 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8557 | inode_dio_end(inode); |
38851cc1 MX |
8558 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8559 | wakeup = false; | |
0934856d MX |
8560 | } |
8561 | ||
17f8c842 | 8562 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8563 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8564 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8565 | btrfs_submit_direct, flags); |
6f673763 | 8566 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8567 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8568 | current->journal_info = NULL; |
ddba1bfc | 8569 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8570 | if (dio_data.reserve) |
bc42bda2 | 8571 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 | 8572 | offset, dio_data.reserve, true); |
f28a4928 FM |
8573 | /* |
8574 | * On error we might have left some ordered extents | |
8575 | * without submitting corresponding bios for them, so | |
8576 | * cleanup them up to avoid other tasks getting them | |
8577 | * and waiting for them to complete forever. | |
8578 | */ | |
8579 | if (dio_data.unsubmitted_oe_range_start < | |
8580 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8581 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8582 | dio_data.unsubmitted_oe_range_start, |
8583 | dio_data.unsubmitted_oe_range_end - | |
8584 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8585 | false); |
ddba1bfc | 8586 | } else if (ret >= 0 && (size_t)ret < count) |
bc42bda2 | 8587 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 QW |
8588 | offset, count - (size_t)ret, true); |
8589 | btrfs_delalloc_release_extents(BTRFS_I(inode), count, false); | |
0934856d | 8590 | } |
38851cc1 | 8591 | out: |
2e60a51e | 8592 | if (wakeup) |
fe0f07d0 | 8593 | inode_dio_end(inode); |
38851cc1 | 8594 | if (relock) |
5955102c | 8595 | inode_lock(inode); |
0934856d | 8596 | |
364ecf36 | 8597 | extent_changeset_free(data_reserved); |
0934856d | 8598 | return ret; |
16432985 CM |
8599 | } |
8600 | ||
05dadc09 TI |
8601 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8602 | ||
1506fcc8 YS |
8603 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8604 | __u64 start, __u64 len) | |
8605 | { | |
05dadc09 TI |
8606 | int ret; |
8607 | ||
8608 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8609 | if (ret) | |
8610 | return ret; | |
8611 | ||
2135fb9b | 8612 | return extent_fiemap(inode, fieinfo, start, len); |
1506fcc8 YS |
8613 | } |
8614 | ||
a52d9a80 | 8615 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8616 | { |
d1310b2e CM |
8617 | struct extent_io_tree *tree; |
8618 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8619 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8620 | } |
1832a6d5 | 8621 | |
a52d9a80 | 8622 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8623 | { |
be7bd730 JB |
8624 | struct inode *inode = page->mapping->host; |
8625 | int ret; | |
b888db2b CM |
8626 | |
8627 | if (current->flags & PF_MEMALLOC) { | |
8628 | redirty_page_for_writepage(wbc, page); | |
8629 | unlock_page(page); | |
8630 | return 0; | |
8631 | } | |
be7bd730 JB |
8632 | |
8633 | /* | |
8634 | * If we are under memory pressure we will call this directly from the | |
8635 | * VM, we need to make sure we have the inode referenced for the ordered | |
8636 | * extent. If not just return like we didn't do anything. | |
8637 | */ | |
8638 | if (!igrab(inode)) { | |
8639 | redirty_page_for_writepage(wbc, page); | |
8640 | return AOP_WRITEPAGE_ACTIVATE; | |
8641 | } | |
0a9b0e53 | 8642 | ret = extent_write_full_page(page, wbc); |
be7bd730 JB |
8643 | btrfs_add_delayed_iput(inode); |
8644 | return ret; | |
9ebefb18 CM |
8645 | } |
8646 | ||
48a3b636 ES |
8647 | static int btrfs_writepages(struct address_space *mapping, |
8648 | struct writeback_control *wbc) | |
b293f02e | 8649 | { |
8ae225a8 | 8650 | return extent_writepages(mapping, wbc); |
b293f02e CM |
8651 | } |
8652 | ||
3ab2fb5a CM |
8653 | static int |
8654 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8655 | struct list_head *pages, unsigned nr_pages) | |
8656 | { | |
2a3ff0ad | 8657 | return extent_readpages(mapping, pages, nr_pages); |
3ab2fb5a | 8658 | } |
2a3ff0ad | 8659 | |
e6dcd2dc | 8660 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8661 | { |
477a30ba | 8662 | int ret = try_release_extent_mapping(page, gfp_flags); |
a52d9a80 CM |
8663 | if (ret == 1) { |
8664 | ClearPagePrivate(page); | |
8665 | set_page_private(page, 0); | |
09cbfeaf | 8666 | put_page(page); |
39279cc3 | 8667 | } |
a52d9a80 | 8668 | return ret; |
39279cc3 CM |
8669 | } |
8670 | ||
e6dcd2dc CM |
8671 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8672 | { | |
98509cfc CM |
8673 | if (PageWriteback(page) || PageDirty(page)) |
8674 | return 0; | |
3ba7ab22 | 8675 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8676 | } |
8677 | ||
d47992f8 LC |
8678 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8679 | unsigned int length) | |
39279cc3 | 8680 | { |
5fd02043 | 8681 | struct inode *inode = page->mapping->host; |
d1310b2e | 8682 | struct extent_io_tree *tree; |
e6dcd2dc | 8683 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8684 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8685 | u64 page_start = page_offset(page); |
09cbfeaf | 8686 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8687 | u64 start; |
8688 | u64 end; | |
131e404a | 8689 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8690 | |
8b62b72b CM |
8691 | /* |
8692 | * we have the page locked, so new writeback can't start, | |
8693 | * and the dirty bit won't be cleared while we are here. | |
8694 | * | |
8695 | * Wait for IO on this page so that we can safely clear | |
8696 | * the PagePrivate2 bit and do ordered accounting | |
8697 | */ | |
e6dcd2dc | 8698 | wait_on_page_writeback(page); |
8b62b72b | 8699 | |
5fd02043 | 8700 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8701 | if (offset) { |
8702 | btrfs_releasepage(page, GFP_NOFS); | |
8703 | return; | |
8704 | } | |
131e404a FDBM |
8705 | |
8706 | if (!inode_evicting) | |
ff13db41 | 8707 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8708 | again: |
8709 | start = page_start; | |
a776c6fa | 8710 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8711 | page_end - start + 1); |
e6dcd2dc | 8712 | if (ordered) { |
dbfdb6d1 | 8713 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8714 | /* |
8715 | * IO on this page will never be started, so we need | |
8716 | * to account for any ordered extents now | |
8717 | */ | |
131e404a | 8718 | if (!inode_evicting) |
dbfdb6d1 | 8719 | clear_extent_bit(tree, start, end, |
131e404a | 8720 | EXTENT_DIRTY | EXTENT_DELALLOC | |
a7e3b975 | 8721 | EXTENT_DELALLOC_NEW | |
131e404a | 8722 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
ae0f1625 | 8723 | EXTENT_DEFRAG, 1, 0, &cached_state); |
8b62b72b CM |
8724 | /* |
8725 | * whoever cleared the private bit is responsible | |
8726 | * for the finish_ordered_io | |
8727 | */ | |
77cef2ec JB |
8728 | if (TestClearPagePrivate2(page)) { |
8729 | struct btrfs_ordered_inode_tree *tree; | |
8730 | u64 new_len; | |
8731 | ||
8732 | tree = &BTRFS_I(inode)->ordered_tree; | |
8733 | ||
8734 | spin_lock_irq(&tree->lock); | |
8735 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8736 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8737 | if (new_len < ordered->truncated_len) |
8738 | ordered->truncated_len = new_len; | |
8739 | spin_unlock_irq(&tree->lock); | |
8740 | ||
8741 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8742 | start, |
8743 | end - start + 1, 1)) | |
77cef2ec | 8744 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8745 | } |
e6dcd2dc | 8746 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8747 | if (!inode_evicting) { |
8748 | cached_state = NULL; | |
dbfdb6d1 | 8749 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8750 | &cached_state); |
8751 | } | |
dbfdb6d1 CR |
8752 | |
8753 | start = end + 1; | |
8754 | if (start < page_end) | |
8755 | goto again; | |
131e404a FDBM |
8756 | } |
8757 | ||
b9d0b389 QW |
8758 | /* |
8759 | * Qgroup reserved space handler | |
8760 | * Page here will be either | |
8761 | * 1) Already written to disk | |
8762 | * In this case, its reserved space is released from data rsv map | |
8763 | * and will be freed by delayed_ref handler finally. | |
8764 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8765 | * space. | |
8766 | * 2) Not written to disk | |
0b34c261 GR |
8767 | * This means the reserved space should be freed here. However, |
8768 | * if a truncate invalidates the page (by clearing PageDirty) | |
8769 | * and the page is accounted for while allocating extent | |
8770 | * in btrfs_check_data_free_space() we let delayed_ref to | |
8771 | * free the entire extent. | |
b9d0b389 | 8772 | */ |
0b34c261 | 8773 | if (PageDirty(page)) |
bc42bda2 | 8774 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a FDBM |
8775 | if (!inode_evicting) { |
8776 | clear_extent_bit(tree, page_start, page_end, | |
8777 | EXTENT_LOCKED | EXTENT_DIRTY | | |
a7e3b975 FM |
8778 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
8779 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
ae0f1625 | 8780 | &cached_state); |
131e404a FDBM |
8781 | |
8782 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8783 | } |
e6dcd2dc | 8784 | |
4a096752 | 8785 | ClearPageChecked(page); |
9ad6b7bc | 8786 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8787 | ClearPagePrivate(page); |
8788 | set_page_private(page, 0); | |
09cbfeaf | 8789 | put_page(page); |
9ad6b7bc | 8790 | } |
39279cc3 CM |
8791 | } |
8792 | ||
9ebefb18 CM |
8793 | /* |
8794 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8795 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8796 | * be careful to check for EOF conditions here. We set the page up correctly | |
8797 | * for a written page which means we get ENOSPC checking when writing into | |
8798 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8799 | * support these features. | |
8800 | * | |
8801 | * We are not allowed to take the i_mutex here so we have to play games to | |
8802 | * protect against truncate races as the page could now be beyond EOF. Because | |
d1342aad OS |
8803 | * truncate_setsize() writes the inode size before removing pages, once we have |
8804 | * the page lock we can determine safely if the page is beyond EOF. If it is not | |
9ebefb18 CM |
8805 | * beyond EOF, then the page is guaranteed safe against truncation until we |
8806 | * unlock the page. | |
8807 | */ | |
a528a241 | 8808 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 8809 | { |
c2ec175c | 8810 | struct page *page = vmf->page; |
11bac800 | 8811 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 8812 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8813 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8814 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8815 | struct extent_state *cached_state = NULL; |
364ecf36 | 8816 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
8817 | char *kaddr; |
8818 | unsigned long zero_start; | |
9ebefb18 | 8819 | loff_t size; |
a528a241 SJ |
8820 | vm_fault_t ret; |
8821 | int ret2; | |
9998eb70 | 8822 | int reserved = 0; |
d0b7da88 | 8823 | u64 reserved_space; |
a52d9a80 | 8824 | u64 page_start; |
e6dcd2dc | 8825 | u64 page_end; |
d0b7da88 CR |
8826 | u64 end; |
8827 | ||
09cbfeaf | 8828 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8829 | |
b2b5ef5c | 8830 | sb_start_pagefault(inode->i_sb); |
df480633 | 8831 | page_start = page_offset(page); |
09cbfeaf | 8832 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8833 | end = page_end; |
df480633 | 8834 | |
d0b7da88 CR |
8835 | /* |
8836 | * Reserving delalloc space after obtaining the page lock can lead to | |
8837 | * deadlock. For example, if a dirty page is locked by this function | |
8838 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8839 | * dirty page write out, then the btrfs_writepage() function could | |
8840 | * end up waiting indefinitely to get a lock on the page currently | |
8841 | * being processed by btrfs_page_mkwrite() function. | |
8842 | */ | |
a528a241 | 8843 | ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 8844 | reserved_space); |
a528a241 SJ |
8845 | if (!ret2) { |
8846 | ret2 = file_update_time(vmf->vma->vm_file); | |
9998eb70 CM |
8847 | reserved = 1; |
8848 | } | |
a528a241 SJ |
8849 | if (ret2) { |
8850 | ret = vmf_error(ret2); | |
9998eb70 CM |
8851 | if (reserved) |
8852 | goto out; | |
8853 | goto out_noreserve; | |
56a76f82 | 8854 | } |
1832a6d5 | 8855 | |
56a76f82 | 8856 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8857 | again: |
9ebefb18 | 8858 | lock_page(page); |
9ebefb18 | 8859 | size = i_size_read(inode); |
a52d9a80 | 8860 | |
9ebefb18 | 8861 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8862 | (page_start >= size)) { |
9ebefb18 CM |
8863 | /* page got truncated out from underneath us */ |
8864 | goto out_unlock; | |
8865 | } | |
e6dcd2dc CM |
8866 | wait_on_page_writeback(page); |
8867 | ||
ff13db41 | 8868 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
8869 | set_page_extent_mapped(page); |
8870 | ||
eb84ae03 CM |
8871 | /* |
8872 | * we can't set the delalloc bits if there are pending ordered | |
8873 | * extents. Drop our locks and wait for them to finish | |
8874 | */ | |
a776c6fa NB |
8875 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
8876 | PAGE_SIZE); | |
e6dcd2dc | 8877 | if (ordered) { |
2ac55d41 | 8878 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8879 | &cached_state); |
e6dcd2dc | 8880 | unlock_page(page); |
eb84ae03 | 8881 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8882 | btrfs_put_ordered_extent(ordered); |
8883 | goto again; | |
8884 | } | |
8885 | ||
09cbfeaf | 8886 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 8887 | reserved_space = round_up(size - page_start, |
0b246afa | 8888 | fs_info->sectorsize); |
09cbfeaf | 8889 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 8890 | end = page_start + reserved_space - 1; |
bc42bda2 | 8891 | btrfs_delalloc_release_space(inode, data_reserved, |
43b18595 QW |
8892 | page_start, PAGE_SIZE - reserved_space, |
8893 | true); | |
d0b7da88 CR |
8894 | } |
8895 | } | |
8896 | ||
fbf19087 | 8897 | /* |
5416034f LB |
8898 | * page_mkwrite gets called when the page is firstly dirtied after it's |
8899 | * faulted in, but write(2) could also dirty a page and set delalloc | |
8900 | * bits, thus in this case for space account reason, we still need to | |
8901 | * clear any delalloc bits within this page range since we have to | |
8902 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 8903 | */ |
d0b7da88 | 8904 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
8905 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8906 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
ae0f1625 | 8907 | 0, 0, &cached_state); |
fbf19087 | 8908 | |
a528a241 | 8909 | ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0, |
ba8b04c1 | 8910 | &cached_state, 0); |
a528a241 | 8911 | if (ret2) { |
2ac55d41 | 8912 | unlock_extent_cached(io_tree, page_start, page_end, |
e43bbe5e | 8913 | &cached_state); |
9ed74f2d JB |
8914 | ret = VM_FAULT_SIGBUS; |
8915 | goto out_unlock; | |
8916 | } | |
a528a241 | 8917 | ret2 = 0; |
9ebefb18 CM |
8918 | |
8919 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 8920 | if (page_start + PAGE_SIZE > size) |
7073017a | 8921 | zero_start = offset_in_page(size); |
9ebefb18 | 8922 | else |
09cbfeaf | 8923 | zero_start = PAGE_SIZE; |
9ebefb18 | 8924 | |
09cbfeaf | 8925 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 8926 | kaddr = kmap(page); |
09cbfeaf | 8927 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
8928 | flush_dcache_page(page); |
8929 | kunmap(page); | |
8930 | } | |
247e743c | 8931 | ClearPageChecked(page); |
e6dcd2dc | 8932 | set_page_dirty(page); |
50a9b214 | 8933 | SetPageUptodate(page); |
5a3f23d5 | 8934 | |
0b246afa | 8935 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 8936 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 8937 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8938 | |
e43bbe5e | 8939 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
9ebefb18 | 8940 | |
a528a241 | 8941 | if (!ret2) { |
43b18595 | 8942 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true); |
b2b5ef5c | 8943 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8944 | extent_changeset_free(data_reserved); |
50a9b214 | 8945 | return VM_FAULT_LOCKED; |
b2b5ef5c | 8946 | } |
717beb96 CM |
8947 | |
8948 | out_unlock: | |
9ebefb18 | 8949 | unlock_page(page); |
1832a6d5 | 8950 | out: |
43b18595 | 8951 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0)); |
bc42bda2 | 8952 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
43b18595 | 8953 | reserved_space, (ret != 0)); |
9998eb70 | 8954 | out_noreserve: |
b2b5ef5c | 8955 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 8956 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
8957 | return ret; |
8958 | } | |
8959 | ||
213e8c55 | 8960 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
39279cc3 | 8961 | { |
0b246afa | 8962 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 8963 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 8964 | struct btrfs_block_rsv *rsv; |
ad7e1a74 | 8965 | int ret; |
39279cc3 | 8966 | struct btrfs_trans_handle *trans; |
0b246afa JM |
8967 | u64 mask = fs_info->sectorsize - 1; |
8968 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 8969 | |
213e8c55 FM |
8970 | if (!skip_writeback) { |
8971 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), | |
8972 | (u64)-1); | |
8973 | if (ret) | |
8974 | return ret; | |
8975 | } | |
39279cc3 | 8976 | |
fcb80c2a | 8977 | /* |
f7e9e8fc OS |
8978 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
8979 | * things going on here: | |
fcb80c2a | 8980 | * |
f7e9e8fc | 8981 | * 1) We need to reserve space to update our inode. |
fcb80c2a | 8982 | * |
f7e9e8fc | 8983 | * 2) We need to have something to cache all the space that is going to |
fcb80c2a JB |
8984 | * be free'd up by the truncate operation, but also have some slack |
8985 | * space reserved in case it uses space during the truncate (thank you | |
8986 | * very much snapshotting). | |
8987 | * | |
f7e9e8fc | 8988 | * And we need these to be separate. The fact is we can use a lot of |
fcb80c2a | 8989 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 8990 | * we will use, so we need the truncate reservation to be separate so it |
f7e9e8fc OS |
8991 | * doesn't end up using space reserved for updating the inode. We also |
8992 | * need to be able to stop the transaction and start a new one, which | |
8993 | * means we need to be able to update the inode several times, and we | |
8994 | * have no idea of knowing how many times that will be, so we can't just | |
8995 | * reserve 1 item for the entirety of the operation, so that has to be | |
8996 | * done separately as well. | |
fcb80c2a JB |
8997 | * |
8998 | * So that leaves us with | |
8999 | * | |
f7e9e8fc | 9000 | * 1) rsv - for the truncate reservation, which we will steal from the |
fcb80c2a | 9001 | * transaction reservation. |
f7e9e8fc | 9002 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
fcb80c2a JB |
9003 | * updating the inode. |
9004 | */ | |
2ff7e61e | 9005 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9006 | if (!rsv) |
9007 | return -ENOMEM; | |
4a338542 | 9008 | rsv->size = min_size; |
ca7e70f5 | 9009 | rsv->failfast = 1; |
f0cd846e | 9010 | |
907cbceb | 9011 | /* |
07127184 | 9012 | * 1 for the truncate slack space |
907cbceb JB |
9013 | * 1 for updating the inode. |
9014 | */ | |
f3fe820c | 9015 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a | 9016 | if (IS_ERR(trans)) { |
ad7e1a74 | 9017 | ret = PTR_ERR(trans); |
fcb80c2a JB |
9018 | goto out; |
9019 | } | |
f0cd846e | 9020 | |
907cbceb | 9021 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9022 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
3a584174 | 9023 | min_size, false); |
fcb80c2a | 9024 | BUG_ON(ret); |
f0cd846e | 9025 | |
5dc562c5 JB |
9026 | /* |
9027 | * So if we truncate and then write and fsync we normally would just | |
9028 | * write the extents that changed, which is a problem if we need to | |
9029 | * first truncate that entire inode. So set this flag so we write out | |
9030 | * all of the extents in the inode to the sync log so we're completely | |
9031 | * safe. | |
9032 | */ | |
9033 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9034 | trans->block_rsv = rsv; |
907cbceb | 9035 | |
8082510e YZ |
9036 | while (1) { |
9037 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9038 | inode->i_size, | |
9039 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 9040 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 | 9041 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 9042 | break; |
39279cc3 | 9043 | |
8082510e | 9044 | ret = btrfs_update_inode(trans, root, inode); |
ad7e1a74 | 9045 | if (ret) |
3893e33b | 9046 | break; |
ca7e70f5 | 9047 | |
3a45bb20 | 9048 | btrfs_end_transaction(trans); |
2ff7e61e | 9049 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9050 | |
9051 | trans = btrfs_start_transaction(root, 2); | |
9052 | if (IS_ERR(trans)) { | |
ad7e1a74 | 9053 | ret = PTR_ERR(trans); |
ca7e70f5 JB |
9054 | trans = NULL; |
9055 | break; | |
9056 | } | |
9057 | ||
47b5d646 | 9058 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9059 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
3a584174 | 9060 | rsv, min_size, false); |
ca7e70f5 JB |
9061 | BUG_ON(ret); /* shouldn't happen */ |
9062 | trans->block_rsv = rsv; | |
8082510e YZ |
9063 | } |
9064 | ||
ddfae63c JB |
9065 | /* |
9066 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
9067 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
9068 | * we've truncated everything except the last little bit, and can do | |
9069 | * btrfs_truncate_block and then update the disk_i_size. | |
9070 | */ | |
9071 | if (ret == NEED_TRUNCATE_BLOCK) { | |
9072 | btrfs_end_transaction(trans); | |
9073 | btrfs_btree_balance_dirty(fs_info); | |
9074 | ||
9075 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
9076 | if (ret) | |
9077 | goto out; | |
9078 | trans = btrfs_start_transaction(root, 1); | |
9079 | if (IS_ERR(trans)) { | |
9080 | ret = PTR_ERR(trans); | |
9081 | goto out; | |
9082 | } | |
9083 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); | |
9084 | } | |
9085 | ||
917c16b2 | 9086 | if (trans) { |
ad7e1a74 OS |
9087 | int ret2; |
9088 | ||
0b246afa | 9089 | trans->block_rsv = &fs_info->trans_block_rsv; |
ad7e1a74 OS |
9090 | ret2 = btrfs_update_inode(trans, root, inode); |
9091 | if (ret2 && !ret) | |
9092 | ret = ret2; | |
7b128766 | 9093 | |
ad7e1a74 OS |
9094 | ret2 = btrfs_end_transaction(trans); |
9095 | if (ret2 && !ret) | |
9096 | ret = ret2; | |
2ff7e61e | 9097 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9098 | } |
fcb80c2a | 9099 | out: |
2ff7e61e | 9100 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9101 | |
ad7e1a74 | 9102 | return ret; |
39279cc3 CM |
9103 | } |
9104 | ||
d352ac68 CM |
9105 | /* |
9106 | * create a new subvolume directory/inode (helper for the ioctl). | |
9107 | */ | |
d2fb3437 | 9108 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9109 | struct btrfs_root *new_root, |
9110 | struct btrfs_root *parent_root, | |
9111 | u64 new_dirid) | |
39279cc3 | 9112 | { |
39279cc3 | 9113 | struct inode *inode; |
76dda93c | 9114 | int err; |
00e4e6b3 | 9115 | u64 index = 0; |
39279cc3 | 9116 | |
12fc9d09 FA |
9117 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9118 | new_dirid, new_dirid, | |
9119 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9120 | &index); | |
54aa1f4d | 9121 | if (IS_ERR(inode)) |
f46b5a66 | 9122 | return PTR_ERR(inode); |
39279cc3 CM |
9123 | inode->i_op = &btrfs_dir_inode_operations; |
9124 | inode->i_fop = &btrfs_dir_file_operations; | |
9125 | ||
bfe86848 | 9126 | set_nlink(inode, 1); |
6ef06d27 | 9127 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9128 | unlock_new_inode(inode); |
3b96362c | 9129 | |
63541927 FDBM |
9130 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9131 | if (err) | |
9132 | btrfs_err(new_root->fs_info, | |
351fd353 | 9133 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9134 | new_root->root_key.objectid, err); |
9135 | ||
76dda93c | 9136 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9137 | |
76dda93c | 9138 | iput(inode); |
ce598979 | 9139 | return err; |
39279cc3 CM |
9140 | } |
9141 | ||
39279cc3 CM |
9142 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9143 | { | |
69fe2d75 | 9144 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 9145 | struct btrfs_inode *ei; |
2ead6ae7 | 9146 | struct inode *inode; |
39279cc3 | 9147 | |
712e36c5 | 9148 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
39279cc3 CM |
9149 | if (!ei) |
9150 | return NULL; | |
2ead6ae7 YZ |
9151 | |
9152 | ei->root = NULL; | |
2ead6ae7 | 9153 | ei->generation = 0; |
15ee9bc7 | 9154 | ei->last_trans = 0; |
257c62e1 | 9155 | ei->last_sub_trans = 0; |
e02119d5 | 9156 | ei->logged_trans = 0; |
2ead6ae7 | 9157 | ei->delalloc_bytes = 0; |
a7e3b975 | 9158 | ei->new_delalloc_bytes = 0; |
47059d93 | 9159 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9160 | ei->disk_i_size = 0; |
9161 | ei->flags = 0; | |
7709cde3 | 9162 | ei->csum_bytes = 0; |
2ead6ae7 | 9163 | ei->index_cnt = (u64)-1; |
67de1176 | 9164 | ei->dir_index = 0; |
2ead6ae7 | 9165 | ei->last_unlink_trans = 0; |
41bd6067 | 9166 | ei->last_link_trans = 0; |
46d8bc34 | 9167 | ei->last_log_commit = 0; |
2ead6ae7 | 9168 | |
9e0baf60 JB |
9169 | spin_lock_init(&ei->lock); |
9170 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
9171 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
9172 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
9173 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 9174 | ei->runtime_flags = 0; |
b52aa8c9 | 9175 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 9176 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9177 | |
16cdcec7 MX |
9178 | ei->delayed_node = NULL; |
9179 | ||
9cc97d64 | 9180 | ei->i_otime.tv_sec = 0; |
9181 | ei->i_otime.tv_nsec = 0; | |
9182 | ||
2ead6ae7 | 9183 | inode = &ei->vfs_inode; |
a8067e02 | 9184 | extent_map_tree_init(&ei->extent_tree); |
c258d6e3 QW |
9185 | extent_io_tree_init(fs_info, &ei->io_tree, inode); |
9186 | extent_io_tree_init(fs_info, &ei->io_failure_tree, inode); | |
7b439738 DS |
9187 | ei->io_tree.track_uptodate = true; |
9188 | ei->io_failure_tree.track_uptodate = true; | |
b812ce28 | 9189 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9190 | mutex_init(&ei->log_mutex); |
f248679e | 9191 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9192 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9193 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9194 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9195 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9196 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9197 | |
9198 | return inode; | |
39279cc3 CM |
9199 | } |
9200 | ||
aaedb55b JB |
9201 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9202 | void btrfs_test_destroy_inode(struct inode *inode) | |
9203 | { | |
dcdbc059 | 9204 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9205 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9206 | } | |
9207 | #endif | |
9208 | ||
fa0d7e3d NP |
9209 | static void btrfs_i_callback(struct rcu_head *head) |
9210 | { | |
9211 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9212 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9213 | } | |
9214 | ||
39279cc3 CM |
9215 | void btrfs_destroy_inode(struct inode *inode) |
9216 | { | |
0b246afa | 9217 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9218 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9220 | ||
b3d9b7a3 | 9221 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9222 | WARN_ON(inode->i_data.nrpages); |
69fe2d75 JB |
9223 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
9224 | WARN_ON(BTRFS_I(inode)->block_rsv.size); | |
9e0baf60 | 9225 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7709cde3 | 9226 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9227 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9228 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9229 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9230 | |
a6dbd429 JB |
9231 | /* |
9232 | * This can happen where we create an inode, but somebody else also | |
9233 | * created the same inode and we need to destroy the one we already | |
9234 | * created. | |
9235 | */ | |
9236 | if (!root) | |
9237 | goto free; | |
9238 | ||
d397712b | 9239 | while (1) { |
e6dcd2dc CM |
9240 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9241 | if (!ordered) | |
9242 | break; | |
9243 | else { | |
0b246afa | 9244 | btrfs_err(fs_info, |
5d163e0e JM |
9245 | "found ordered extent %llu %llu on inode cleanup", |
9246 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9247 | btrfs_remove_ordered_extent(inode, ordered); |
9248 | btrfs_put_ordered_extent(ordered); | |
9249 | btrfs_put_ordered_extent(ordered); | |
9250 | } | |
9251 | } | |
56fa9d07 | 9252 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9253 | inode_tree_del(inode); |
dcdbc059 | 9254 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
a6dbd429 | 9255 | free: |
fa0d7e3d | 9256 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9257 | } |
9258 | ||
45321ac5 | 9259 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9260 | { |
9261 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9262 | |
6379ef9f NA |
9263 | if (root == NULL) |
9264 | return 1; | |
9265 | ||
fa6ac876 | 9266 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9267 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9268 | return 1; |
76dda93c | 9269 | else |
45321ac5 | 9270 | return generic_drop_inode(inode); |
76dda93c YZ |
9271 | } |
9272 | ||
0ee0fda0 | 9273 | static void init_once(void *foo) |
39279cc3 CM |
9274 | { |
9275 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9276 | ||
9277 | inode_init_once(&ei->vfs_inode); | |
9278 | } | |
9279 | ||
e67c718b | 9280 | void __cold btrfs_destroy_cachep(void) |
39279cc3 | 9281 | { |
8c0a8537 KS |
9282 | /* |
9283 | * Make sure all delayed rcu free inodes are flushed before we | |
9284 | * destroy cache. | |
9285 | */ | |
9286 | rcu_barrier(); | |
5598e900 KM |
9287 | kmem_cache_destroy(btrfs_inode_cachep); |
9288 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9289 | kmem_cache_destroy(btrfs_path_cachep); |
9290 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9291 | } |
9292 | ||
f5c29bd9 | 9293 | int __init btrfs_init_cachep(void) |
39279cc3 | 9294 | { |
837e1972 | 9295 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9296 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9297 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9298 | init_once); | |
39279cc3 CM |
9299 | if (!btrfs_inode_cachep) |
9300 | goto fail; | |
9601e3f6 | 9301 | |
837e1972 | 9302 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9303 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9304 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9305 | if (!btrfs_trans_handle_cachep) |
9306 | goto fail; | |
9601e3f6 | 9307 | |
837e1972 | 9308 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9309 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9310 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9311 | if (!btrfs_path_cachep) |
9312 | goto fail; | |
9601e3f6 | 9313 | |
837e1972 | 9314 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9315 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9316 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9317 | if (!btrfs_free_space_cachep) |
9318 | goto fail; | |
9319 | ||
39279cc3 CM |
9320 | return 0; |
9321 | fail: | |
9322 | btrfs_destroy_cachep(); | |
9323 | return -ENOMEM; | |
9324 | } | |
9325 | ||
a528d35e DH |
9326 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9327 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9328 | { |
df0af1a5 | 9329 | u64 delalloc_bytes; |
a528d35e | 9330 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9331 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9332 | u32 bi_flags = BTRFS_I(inode)->flags; |
9333 | ||
9334 | stat->result_mask |= STATX_BTIME; | |
9335 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9336 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9337 | if (bi_flags & BTRFS_INODE_APPEND) | |
9338 | stat->attributes |= STATX_ATTR_APPEND; | |
9339 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9340 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9341 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9342 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9343 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9344 | stat->attributes |= STATX_ATTR_NODUMP; | |
9345 | ||
9346 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9347 | STATX_ATTR_COMPRESSED | | |
9348 | STATX_ATTR_IMMUTABLE | | |
9349 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9350 | |
39279cc3 | 9351 | generic_fillattr(inode, stat); |
0ee5dc67 | 9352 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9353 | |
9354 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9355 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9356 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9357 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9358 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9359 | return 0; |
9360 | } | |
9361 | ||
cdd1fedf DF |
9362 | static int btrfs_rename_exchange(struct inode *old_dir, |
9363 | struct dentry *old_dentry, | |
9364 | struct inode *new_dir, | |
9365 | struct dentry *new_dentry) | |
9366 | { | |
0b246afa | 9367 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9368 | struct btrfs_trans_handle *trans; |
9369 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9370 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9371 | struct inode *new_inode = new_dentry->d_inode; | |
9372 | struct inode *old_inode = old_dentry->d_inode; | |
95582b00 | 9373 | struct timespec64 ctime = current_time(old_inode); |
cdd1fedf | 9374 | struct dentry *parent; |
4a0cc7ca NB |
9375 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9376 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9377 | u64 old_idx = 0; |
9378 | u64 new_idx = 0; | |
9379 | u64 root_objectid; | |
9380 | int ret; | |
86e8aa0e FM |
9381 | bool root_log_pinned = false; |
9382 | bool dest_log_pinned = false; | |
d4682ba0 FM |
9383 | struct btrfs_log_ctx ctx_root; |
9384 | struct btrfs_log_ctx ctx_dest; | |
9385 | bool sync_log_root = false; | |
9386 | bool sync_log_dest = false; | |
9387 | bool commit_transaction = false; | |
cdd1fedf DF |
9388 | |
9389 | /* we only allow rename subvolume link between subvolumes */ | |
9390 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9391 | return -EXDEV; | |
9392 | ||
d4682ba0 FM |
9393 | btrfs_init_log_ctx(&ctx_root, old_inode); |
9394 | btrfs_init_log_ctx(&ctx_dest, new_inode); | |
9395 | ||
cdd1fedf DF |
9396 | /* close the race window with snapshot create/destroy ioctl */ |
9397 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9398 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9399 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9400 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9401 | |
9402 | /* | |
9403 | * We want to reserve the absolute worst case amount of items. So if | |
9404 | * both inodes are subvols and we need to unlink them then that would | |
9405 | * require 4 item modifications, but if they are both normal inodes it | |
9406 | * would require 5 item modifications, so we'll assume their normal | |
9407 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9408 | * should cover the worst case number of items we'll modify. | |
9409 | */ | |
9410 | trans = btrfs_start_transaction(root, 12); | |
9411 | if (IS_ERR(trans)) { | |
9412 | ret = PTR_ERR(trans); | |
9413 | goto out_notrans; | |
9414 | } | |
9415 | ||
9416 | /* | |
9417 | * We need to find a free sequence number both in the source and | |
9418 | * in the destination directory for the exchange. | |
9419 | */ | |
877574e2 | 9420 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9421 | if (ret) |
9422 | goto out_fail; | |
877574e2 | 9423 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9424 | if (ret) |
9425 | goto out_fail; | |
9426 | ||
9427 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9428 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9429 | ||
9430 | /* Reference for the source. */ | |
9431 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9432 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9433 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9434 | } else { |
376e5a57 FM |
9435 | btrfs_pin_log_trans(root); |
9436 | root_log_pinned = true; | |
cdd1fedf DF |
9437 | ret = btrfs_insert_inode_ref(trans, dest, |
9438 | new_dentry->d_name.name, | |
9439 | new_dentry->d_name.len, | |
9440 | old_ino, | |
f85b7379 DS |
9441 | btrfs_ino(BTRFS_I(new_dir)), |
9442 | old_idx); | |
cdd1fedf DF |
9443 | if (ret) |
9444 | goto out_fail; | |
cdd1fedf DF |
9445 | } |
9446 | ||
9447 | /* And now for the dest. */ | |
9448 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9449 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9450 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9451 | } else { |
376e5a57 FM |
9452 | btrfs_pin_log_trans(dest); |
9453 | dest_log_pinned = true; | |
cdd1fedf DF |
9454 | ret = btrfs_insert_inode_ref(trans, root, |
9455 | old_dentry->d_name.name, | |
9456 | old_dentry->d_name.len, | |
9457 | new_ino, | |
f85b7379 DS |
9458 | btrfs_ino(BTRFS_I(old_dir)), |
9459 | new_idx); | |
cdd1fedf DF |
9460 | if (ret) |
9461 | goto out_fail; | |
cdd1fedf DF |
9462 | } |
9463 | ||
9464 | /* Update inode version and ctime/mtime. */ | |
9465 | inode_inc_iversion(old_dir); | |
9466 | inode_inc_iversion(new_dir); | |
9467 | inode_inc_iversion(old_inode); | |
9468 | inode_inc_iversion(new_inode); | |
9469 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9470 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9471 | old_inode->i_ctime = ctime; | |
9472 | new_inode->i_ctime = ctime; | |
9473 | ||
9474 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9475 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9476 | BTRFS_I(old_inode), 1); | |
9477 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9478 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9479 | } |
9480 | ||
9481 | /* src is a subvolume */ | |
9482 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9483 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
401b3b19 | 9484 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
cdd1fedf DF |
9485 | old_dentry->d_name.name, |
9486 | old_dentry->d_name.len); | |
9487 | } else { /* src is an inode */ | |
4ec5934e NB |
9488 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9489 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9490 | old_dentry->d_name.name, |
9491 | old_dentry->d_name.len); | |
9492 | if (!ret) | |
9493 | ret = btrfs_update_inode(trans, root, old_inode); | |
9494 | } | |
9495 | if (ret) { | |
66642832 | 9496 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9497 | goto out_fail; |
9498 | } | |
9499 | ||
9500 | /* dest is a subvolume */ | |
9501 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9502 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
401b3b19 | 9503 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
cdd1fedf DF |
9504 | new_dentry->d_name.name, |
9505 | new_dentry->d_name.len); | |
9506 | } else { /* dest is an inode */ | |
4ec5934e NB |
9507 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9508 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9509 | new_dentry->d_name.name, |
9510 | new_dentry->d_name.len); | |
9511 | if (!ret) | |
9512 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9513 | } | |
9514 | if (ret) { | |
66642832 | 9515 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9516 | goto out_fail; |
9517 | } | |
9518 | ||
db0a669f | 9519 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9520 | new_dentry->d_name.name, |
9521 | new_dentry->d_name.len, 0, old_idx); | |
9522 | if (ret) { | |
66642832 | 9523 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9524 | goto out_fail; |
9525 | } | |
9526 | ||
db0a669f | 9527 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9528 | old_dentry->d_name.name, |
9529 | old_dentry->d_name.len, 0, new_idx); | |
9530 | if (ret) { | |
66642832 | 9531 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9532 | goto out_fail; |
9533 | } | |
9534 | ||
9535 | if (old_inode->i_nlink == 1) | |
9536 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9537 | if (new_inode->i_nlink == 1) | |
9538 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9539 | ||
86e8aa0e | 9540 | if (root_log_pinned) { |
cdd1fedf | 9541 | parent = new_dentry->d_parent; |
d4682ba0 FM |
9542 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), |
9543 | BTRFS_I(old_dir), parent, | |
9544 | false, &ctx_root); | |
9545 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9546 | sync_log_root = true; | |
9547 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9548 | commit_transaction = true; | |
9549 | ret = 0; | |
cdd1fedf | 9550 | btrfs_end_log_trans(root); |
86e8aa0e | 9551 | root_log_pinned = false; |
cdd1fedf | 9552 | } |
86e8aa0e | 9553 | if (dest_log_pinned) { |
d4682ba0 FM |
9554 | if (!commit_transaction) { |
9555 | parent = old_dentry->d_parent; | |
9556 | ret = btrfs_log_new_name(trans, BTRFS_I(new_inode), | |
9557 | BTRFS_I(new_dir), parent, | |
9558 | false, &ctx_dest); | |
9559 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9560 | sync_log_dest = true; | |
9561 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9562 | commit_transaction = true; | |
9563 | ret = 0; | |
9564 | } | |
cdd1fedf | 9565 | btrfs_end_log_trans(dest); |
86e8aa0e | 9566 | dest_log_pinned = false; |
cdd1fedf DF |
9567 | } |
9568 | out_fail: | |
86e8aa0e FM |
9569 | /* |
9570 | * If we have pinned a log and an error happened, we unpin tasks | |
9571 | * trying to sync the log and force them to fallback to a transaction | |
9572 | * commit if the log currently contains any of the inodes involved in | |
9573 | * this rename operation (to ensure we do not persist a log with an | |
9574 | * inconsistent state for any of these inodes or leading to any | |
9575 | * inconsistencies when replayed). If the transaction was aborted, the | |
9576 | * abortion reason is propagated to userspace when attempting to commit | |
9577 | * the transaction. If the log does not contain any of these inodes, we | |
9578 | * allow the tasks to sync it. | |
9579 | */ | |
9580 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9581 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9582 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9583 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9584 | (new_inode && |
0f8939b8 | 9585 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9586 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9587 | |
9588 | if (root_log_pinned) { | |
9589 | btrfs_end_log_trans(root); | |
9590 | root_log_pinned = false; | |
9591 | } | |
9592 | if (dest_log_pinned) { | |
9593 | btrfs_end_log_trans(dest); | |
9594 | dest_log_pinned = false; | |
9595 | } | |
9596 | } | |
d4682ba0 FM |
9597 | if (!ret && sync_log_root && !commit_transaction) { |
9598 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, | |
9599 | &ctx_root); | |
9600 | if (ret) | |
9601 | commit_transaction = true; | |
9602 | } | |
9603 | if (!ret && sync_log_dest && !commit_transaction) { | |
9604 | ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root, | |
9605 | &ctx_dest); | |
9606 | if (ret) | |
9607 | commit_transaction = true; | |
9608 | } | |
9609 | if (commit_transaction) { | |
9610 | ret = btrfs_commit_transaction(trans); | |
9611 | } else { | |
9612 | int ret2; | |
9613 | ||
9614 | ret2 = btrfs_end_transaction(trans); | |
9615 | ret = ret ? ret : ret2; | |
9616 | } | |
cdd1fedf DF |
9617 | out_notrans: |
9618 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9619 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9620 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9621 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9622 | |
9623 | return ret; | |
9624 | } | |
9625 | ||
9626 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9627 | struct btrfs_root *root, | |
9628 | struct inode *dir, | |
9629 | struct dentry *dentry) | |
9630 | { | |
9631 | int ret; | |
9632 | struct inode *inode; | |
9633 | u64 objectid; | |
9634 | u64 index; | |
9635 | ||
9636 | ret = btrfs_find_free_ino(root, &objectid); | |
9637 | if (ret) | |
9638 | return ret; | |
9639 | ||
9640 | inode = btrfs_new_inode(trans, root, dir, | |
9641 | dentry->d_name.name, | |
9642 | dentry->d_name.len, | |
4a0cc7ca | 9643 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9644 | objectid, |
9645 | S_IFCHR | WHITEOUT_MODE, | |
9646 | &index); | |
9647 | ||
9648 | if (IS_ERR(inode)) { | |
9649 | ret = PTR_ERR(inode); | |
9650 | return ret; | |
9651 | } | |
9652 | ||
9653 | inode->i_op = &btrfs_special_inode_operations; | |
9654 | init_special_inode(inode, inode->i_mode, | |
9655 | WHITEOUT_DEV); | |
9656 | ||
9657 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9658 | &dentry->d_name); | |
9659 | if (ret) | |
c9901618 | 9660 | goto out; |
cdd1fedf | 9661 | |
cef415af NB |
9662 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9663 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9664 | if (ret) |
c9901618 | 9665 | goto out; |
cdd1fedf DF |
9666 | |
9667 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9668 | out: |
cdd1fedf | 9669 | unlock_new_inode(inode); |
c9901618 FM |
9670 | if (ret) |
9671 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9672 | iput(inode); |
9673 | ||
c9901618 | 9674 | return ret; |
cdd1fedf DF |
9675 | } |
9676 | ||
d397712b | 9677 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9678 | struct inode *new_dir, struct dentry *new_dentry, |
9679 | unsigned int flags) | |
39279cc3 | 9680 | { |
0b246afa | 9681 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9682 | struct btrfs_trans_handle *trans; |
5062af35 | 9683 | unsigned int trans_num_items; |
39279cc3 | 9684 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9685 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9686 | struct inode *new_inode = d_inode(new_dentry); |
9687 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9688 | u64 index = 0; |
4df27c4d | 9689 | u64 root_objectid; |
39279cc3 | 9690 | int ret; |
4a0cc7ca | 9691 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9692 | bool log_pinned = false; |
d4682ba0 FM |
9693 | struct btrfs_log_ctx ctx; |
9694 | bool sync_log = false; | |
9695 | bool commit_transaction = false; | |
39279cc3 | 9696 | |
4a0cc7ca | 9697 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9698 | return -EPERM; |
9699 | ||
4df27c4d | 9700 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9701 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9702 | return -EXDEV; |
9703 | ||
33345d01 | 9704 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9705 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9706 | return -ENOTEMPTY; |
5f39d397 | 9707 | |
4df27c4d YZ |
9708 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9709 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9710 | return -ENOTEMPTY; | |
9c52057c CM |
9711 | |
9712 | ||
9713 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9714 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9715 | new_dentry->d_name.name, |
9716 | new_dentry->d_name.len); | |
9717 | ||
9718 | if (ret) { | |
9719 | if (ret == -EEXIST) { | |
9720 | /* we shouldn't get | |
9721 | * eexist without a new_inode */ | |
fae7f21c | 9722 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9723 | return ret; |
9724 | } | |
9725 | } else { | |
9726 | /* maybe -EOVERFLOW */ | |
9727 | return ret; | |
9728 | } | |
9729 | } | |
9730 | ret = 0; | |
9731 | ||
5a3f23d5 | 9732 | /* |
8d875f95 CM |
9733 | * we're using rename to replace one file with another. Start IO on it |
9734 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9735 | */ |
8d875f95 | 9736 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9737 | filemap_flush(old_inode->i_mapping); |
9738 | ||
76dda93c | 9739 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9740 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9741 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9742 | /* |
9743 | * We want to reserve the absolute worst case amount of items. So if | |
9744 | * both inodes are subvols and we need to unlink them then that would | |
9745 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9746 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9747 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9748 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9749 | * If our rename has the whiteout flag, we need more 5 units for the |
9750 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9751 | * when selinux is enabled). | |
a22285a6 | 9752 | */ |
5062af35 FM |
9753 | trans_num_items = 11; |
9754 | if (flags & RENAME_WHITEOUT) | |
9755 | trans_num_items += 5; | |
9756 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9757 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9758 | ret = PTR_ERR(trans); |
9759 | goto out_notrans; | |
9760 | } | |
76dda93c | 9761 | |
4df27c4d YZ |
9762 | if (dest != root) |
9763 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9764 | |
877574e2 | 9765 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9766 | if (ret) |
9767 | goto out_fail; | |
5a3f23d5 | 9768 | |
67de1176 | 9769 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9770 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9771 | /* force full log commit if subvolume involved. */ |
0b246afa | 9772 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 9773 | } else { |
c4aba954 FM |
9774 | btrfs_pin_log_trans(root); |
9775 | log_pinned = true; | |
a5719521 YZ |
9776 | ret = btrfs_insert_inode_ref(trans, dest, |
9777 | new_dentry->d_name.name, | |
9778 | new_dentry->d_name.len, | |
33345d01 | 9779 | old_ino, |
4a0cc7ca | 9780 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9781 | if (ret) |
9782 | goto out_fail; | |
4df27c4d | 9783 | } |
5a3f23d5 | 9784 | |
0c4d2d95 JB |
9785 | inode_inc_iversion(old_dir); |
9786 | inode_inc_iversion(new_dir); | |
9787 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9788 | old_dir->i_ctime = old_dir->i_mtime = |
9789 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9790 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9791 | |
12fcfd22 | 9792 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9793 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9794 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9795 | |
33345d01 | 9796 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9797 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
401b3b19 | 9798 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
4df27c4d YZ |
9799 | old_dentry->d_name.name, |
9800 | old_dentry->d_name.len); | |
9801 | } else { | |
4ec5934e NB |
9802 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9803 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9804 | old_dentry->d_name.name, |
9805 | old_dentry->d_name.len); | |
9806 | if (!ret) | |
9807 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9808 | } |
79787eaa | 9809 | if (ret) { |
66642832 | 9810 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9811 | goto out_fail; |
9812 | } | |
39279cc3 CM |
9813 | |
9814 | if (new_inode) { | |
0c4d2d95 | 9815 | inode_inc_iversion(new_inode); |
c2050a45 | 9816 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9817 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
9818 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9819 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
401b3b19 | 9820 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
4df27c4d YZ |
9821 | new_dentry->d_name.name, |
9822 | new_dentry->d_name.len); | |
9823 | BUG_ON(new_inode->i_nlink == 0); | |
9824 | } else { | |
4ec5934e NB |
9825 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9826 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
9827 | new_dentry->d_name.name, |
9828 | new_dentry->d_name.len); | |
9829 | } | |
4ef31a45 | 9830 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
9831 | ret = btrfs_orphan_add(trans, |
9832 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 9833 | if (ret) { |
66642832 | 9834 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9835 | goto out_fail; |
9836 | } | |
39279cc3 | 9837 | } |
aec7477b | 9838 | |
db0a669f | 9839 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 9840 | new_dentry->d_name.name, |
a5719521 | 9841 | new_dentry->d_name.len, 0, index); |
79787eaa | 9842 | if (ret) { |
66642832 | 9843 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9844 | goto out_fail; |
9845 | } | |
39279cc3 | 9846 | |
67de1176 MX |
9847 | if (old_inode->i_nlink == 1) |
9848 | BTRFS_I(old_inode)->dir_index = index; | |
9849 | ||
3dc9e8f7 | 9850 | if (log_pinned) { |
10d9f309 | 9851 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 9852 | |
d4682ba0 FM |
9853 | btrfs_init_log_ctx(&ctx, old_inode); |
9854 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), | |
9855 | BTRFS_I(old_dir), parent, | |
9856 | false, &ctx); | |
9857 | if (ret == BTRFS_NEED_LOG_SYNC) | |
9858 | sync_log = true; | |
9859 | else if (ret == BTRFS_NEED_TRANS_COMMIT) | |
9860 | commit_transaction = true; | |
9861 | ret = 0; | |
4df27c4d | 9862 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9863 | log_pinned = false; |
4df27c4d | 9864 | } |
cdd1fedf DF |
9865 | |
9866 | if (flags & RENAME_WHITEOUT) { | |
9867 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9868 | old_dentry); | |
9869 | ||
9870 | if (ret) { | |
66642832 | 9871 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9872 | goto out_fail; |
9873 | } | |
4df27c4d | 9874 | } |
39279cc3 | 9875 | out_fail: |
3dc9e8f7 FM |
9876 | /* |
9877 | * If we have pinned the log and an error happened, we unpin tasks | |
9878 | * trying to sync the log and force them to fallback to a transaction | |
9879 | * commit if the log currently contains any of the inodes involved in | |
9880 | * this rename operation (to ensure we do not persist a log with an | |
9881 | * inconsistent state for any of these inodes or leading to any | |
9882 | * inconsistencies when replayed). If the transaction was aborted, the | |
9883 | * abortion reason is propagated to userspace when attempting to commit | |
9884 | * the transaction. If the log does not contain any of these inodes, we | |
9885 | * allow the tasks to sync it. | |
9886 | */ | |
9887 | if (ret && log_pinned) { | |
0f8939b8 NB |
9888 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9889 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9890 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9891 | (new_inode && |
0f8939b8 | 9892 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9893 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
9894 | |
9895 | btrfs_end_log_trans(root); | |
9896 | log_pinned = false; | |
9897 | } | |
d4682ba0 FM |
9898 | if (!ret && sync_log) { |
9899 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx); | |
9900 | if (ret) | |
9901 | commit_transaction = true; | |
9902 | } | |
9903 | if (commit_transaction) { | |
9904 | ret = btrfs_commit_transaction(trans); | |
9905 | } else { | |
9906 | int ret2; | |
9907 | ||
9908 | ret2 = btrfs_end_transaction(trans); | |
9909 | ret = ret ? ret : ret2; | |
9910 | } | |
b44c59a8 | 9911 | out_notrans: |
33345d01 | 9912 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9913 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9914 | |
39279cc3 CM |
9915 | return ret; |
9916 | } | |
9917 | ||
80ace85c MS |
9918 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9919 | struct inode *new_dir, struct dentry *new_dentry, | |
9920 | unsigned int flags) | |
9921 | { | |
cdd1fedf | 9922 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9923 | return -EINVAL; |
9924 | ||
cdd1fedf DF |
9925 | if (flags & RENAME_EXCHANGE) |
9926 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9927 | new_dentry); | |
9928 | ||
9929 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9930 | } |
9931 | ||
3a2f8c07 NB |
9932 | struct btrfs_delalloc_work { |
9933 | struct inode *inode; | |
9934 | struct completion completion; | |
9935 | struct list_head list; | |
9936 | struct btrfs_work work; | |
9937 | }; | |
9938 | ||
8ccf6f19 MX |
9939 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9940 | { | |
9941 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9942 | struct inode *inode; |
8ccf6f19 MX |
9943 | |
9944 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9945 | work); | |
9f23e289 | 9946 | inode = delalloc_work->inode; |
30424601 DS |
9947 | filemap_flush(inode->i_mapping); |
9948 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9949 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9950 | filemap_flush(inode->i_mapping); |
8ccf6f19 | 9951 | |
076da91c | 9952 | iput(inode); |
8ccf6f19 MX |
9953 | complete(&delalloc_work->completion); |
9954 | } | |
9955 | ||
3a2f8c07 | 9956 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
8ccf6f19 MX |
9957 | { |
9958 | struct btrfs_delalloc_work *work; | |
9959 | ||
100d5702 | 9960 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9961 | if (!work) |
9962 | return NULL; | |
9963 | ||
9964 | init_completion(&work->completion); | |
9965 | INIT_LIST_HEAD(&work->list); | |
9966 | work->inode = inode; | |
9e0af237 LB |
9967 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
9968 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9969 | |
9970 | return work; | |
9971 | } | |
9972 | ||
d352ac68 CM |
9973 | /* |
9974 | * some fairly slow code that needs optimization. This walks the list | |
9975 | * of all the inodes with pending delalloc and forces them to disk. | |
9976 | */ | |
3cd24c69 | 9977 | static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot) |
ea8c2819 | 9978 | { |
ea8c2819 | 9979 | struct btrfs_inode *binode; |
5b21f2ed | 9980 | struct inode *inode; |
8ccf6f19 MX |
9981 | struct btrfs_delalloc_work *work, *next; |
9982 | struct list_head works; | |
1eafa6c7 | 9983 | struct list_head splice; |
8ccf6f19 | 9984 | int ret = 0; |
ea8c2819 | 9985 | |
8ccf6f19 | 9986 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9987 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9988 | |
573bfb72 | 9989 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9990 | spin_lock(&root->delalloc_lock); |
9991 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9992 | while (!list_empty(&splice)) { |
9993 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9994 | delalloc_inodes); |
1eafa6c7 | 9995 | |
eb73c1b7 MX |
9996 | list_move_tail(&binode->delalloc_inodes, |
9997 | &root->delalloc_inodes); | |
5b21f2ed | 9998 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9999 | if (!inode) { |
eb73c1b7 | 10000 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10001 | continue; |
df0af1a5 | 10002 | } |
eb73c1b7 | 10003 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10004 | |
3cd24c69 EL |
10005 | if (snapshot) |
10006 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, | |
10007 | &binode->runtime_flags); | |
076da91c | 10008 | work = btrfs_alloc_delalloc_work(inode); |
5d99a998 | 10009 | if (!work) { |
4fbb5147 | 10010 | iput(inode); |
1eafa6c7 | 10011 | ret = -ENOMEM; |
a1ecaabb | 10012 | goto out; |
5b21f2ed | 10013 | } |
1eafa6c7 | 10014 | list_add_tail(&work->list, &works); |
a44903ab QW |
10015 | btrfs_queue_work(root->fs_info->flush_workers, |
10016 | &work->work); | |
6c255e67 MX |
10017 | ret++; |
10018 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10019 | goto out; |
5b21f2ed | 10020 | cond_resched(); |
eb73c1b7 | 10021 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10022 | } |
eb73c1b7 | 10023 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10024 | |
a1ecaabb | 10025 | out: |
eb73c1b7 MX |
10026 | list_for_each_entry_safe(work, next, &works, list) { |
10027 | list_del_init(&work->list); | |
40012f96 NB |
10028 | wait_for_completion(&work->completion); |
10029 | kfree(work); | |
eb73c1b7 MX |
10030 | } |
10031 | ||
81f1d390 | 10032 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
10033 | spin_lock(&root->delalloc_lock); |
10034 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10035 | spin_unlock(&root->delalloc_lock); | |
10036 | } | |
573bfb72 | 10037 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10038 | return ret; |
10039 | } | |
1eafa6c7 | 10040 | |
3cd24c69 | 10041 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
eb73c1b7 | 10042 | { |
0b246afa | 10043 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10044 | int ret; |
1eafa6c7 | 10045 | |
0b246afa | 10046 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10047 | return -EROFS; |
10048 | ||
3cd24c69 | 10049 | ret = start_delalloc_inodes(root, -1, true); |
6c255e67 MX |
10050 | if (ret > 0) |
10051 | ret = 0; | |
eb73c1b7 MX |
10052 | return ret; |
10053 | } | |
10054 | ||
82b3e53b | 10055 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr) |
eb73c1b7 MX |
10056 | { |
10057 | struct btrfs_root *root; | |
10058 | struct list_head splice; | |
10059 | int ret; | |
10060 | ||
2c21b4d7 | 10061 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10062 | return -EROFS; |
10063 | ||
10064 | INIT_LIST_HEAD(&splice); | |
10065 | ||
573bfb72 | 10066 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10067 | spin_lock(&fs_info->delalloc_root_lock); |
10068 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10069 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10070 | root = list_first_entry(&splice, struct btrfs_root, |
10071 | delalloc_root); | |
10072 | root = btrfs_grab_fs_root(root); | |
10073 | BUG_ON(!root); | |
10074 | list_move_tail(&root->delalloc_root, | |
10075 | &fs_info->delalloc_roots); | |
10076 | spin_unlock(&fs_info->delalloc_root_lock); | |
10077 | ||
3cd24c69 | 10078 | ret = start_delalloc_inodes(root, nr, false); |
eb73c1b7 | 10079 | btrfs_put_fs_root(root); |
6c255e67 | 10080 | if (ret < 0) |
eb73c1b7 MX |
10081 | goto out; |
10082 | ||
6c255e67 MX |
10083 | if (nr != -1) { |
10084 | nr -= ret; | |
10085 | WARN_ON(nr < 0); | |
10086 | } | |
eb73c1b7 | 10087 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10088 | } |
eb73c1b7 | 10089 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10090 | |
6c255e67 | 10091 | ret = 0; |
eb73c1b7 | 10092 | out: |
81f1d390 | 10093 | if (!list_empty(&splice)) { |
eb73c1b7 MX |
10094 | spin_lock(&fs_info->delalloc_root_lock); |
10095 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10096 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10097 | } |
573bfb72 | 10098 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10099 | return ret; |
ea8c2819 CM |
10100 | } |
10101 | ||
39279cc3 CM |
10102 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10103 | const char *symname) | |
10104 | { | |
0b246afa | 10105 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10106 | struct btrfs_trans_handle *trans; |
10107 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10108 | struct btrfs_path *path; | |
10109 | struct btrfs_key key; | |
1832a6d5 | 10110 | struct inode *inode = NULL; |
39279cc3 | 10111 | int err; |
39279cc3 | 10112 | u64 objectid; |
67871254 | 10113 | u64 index = 0; |
39279cc3 CM |
10114 | int name_len; |
10115 | int datasize; | |
5f39d397 | 10116 | unsigned long ptr; |
39279cc3 | 10117 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10118 | struct extent_buffer *leaf; |
39279cc3 | 10119 | |
f06becc4 | 10120 | name_len = strlen(symname); |
0b246afa | 10121 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10122 | return -ENAMETOOLONG; |
1832a6d5 | 10123 | |
9ed74f2d JB |
10124 | /* |
10125 | * 2 items for inode item and ref | |
10126 | * 2 items for dir items | |
9269d12b FM |
10127 | * 1 item for updating parent inode item |
10128 | * 1 item for the inline extent item | |
9ed74f2d JB |
10129 | * 1 item for xattr if selinux is on |
10130 | */ | |
9269d12b | 10131 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10132 | if (IS_ERR(trans)) |
10133 | return PTR_ERR(trans); | |
1832a6d5 | 10134 | |
581bb050 LZ |
10135 | err = btrfs_find_free_ino(root, &objectid); |
10136 | if (err) | |
10137 | goto out_unlock; | |
10138 | ||
aec7477b | 10139 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10140 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10141 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10142 | if (IS_ERR(inode)) { |
10143 | err = PTR_ERR(inode); | |
32955c54 | 10144 | inode = NULL; |
39279cc3 | 10145 | goto out_unlock; |
7cf96da3 | 10146 | } |
39279cc3 | 10147 | |
ad19db71 CS |
10148 | /* |
10149 | * If the active LSM wants to access the inode during | |
10150 | * d_instantiate it needs these. Smack checks to see | |
10151 | * if the filesystem supports xattrs by looking at the | |
10152 | * ops vector. | |
10153 | */ | |
10154 | inode->i_fop = &btrfs_file_operations; | |
10155 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10156 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10157 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10158 | ||
10159 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10160 | if (err) | |
32955c54 | 10161 | goto out_unlock; |
ad19db71 | 10162 | |
39279cc3 | 10163 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10164 | if (!path) { |
10165 | err = -ENOMEM; | |
32955c54 | 10166 | goto out_unlock; |
d8926bb3 | 10167 | } |
4a0cc7ca | 10168 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10169 | key.offset = 0; |
962a298f | 10170 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10171 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10172 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10173 | datasize); | |
54aa1f4d | 10174 | if (err) { |
b0839166 | 10175 | btrfs_free_path(path); |
32955c54 | 10176 | goto out_unlock; |
54aa1f4d | 10177 | } |
5f39d397 CM |
10178 | leaf = path->nodes[0]; |
10179 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10180 | struct btrfs_file_extent_item); | |
10181 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10182 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10183 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10184 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10185 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10186 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10187 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10188 | ||
39279cc3 | 10189 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10190 | write_extent_buffer(leaf, symname, ptr, name_len); |
10191 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10192 | btrfs_free_path(path); |
5f39d397 | 10193 | |
39279cc3 | 10194 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10195 | inode_nohighmem(inode); |
d899e052 | 10196 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10197 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10198 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10199 | /* |
10200 | * Last step, add directory indexes for our symlink inode. This is the | |
10201 | * last step to avoid extra cleanup of these indexes if an error happens | |
10202 | * elsewhere above. | |
10203 | */ | |
10204 | if (!err) | |
cef415af NB |
10205 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10206 | BTRFS_I(inode), 0, index); | |
32955c54 AV |
10207 | if (err) |
10208 | goto out_unlock; | |
b0d5d10f | 10209 | |
1e2e547a | 10210 | d_instantiate_new(dentry, inode); |
39279cc3 CM |
10211 | |
10212 | out_unlock: | |
3a45bb20 | 10213 | btrfs_end_transaction(trans); |
32955c54 | 10214 | if (err && inode) { |
39279cc3 | 10215 | inode_dec_link_count(inode); |
32955c54 | 10216 | discard_new_inode(inode); |
39279cc3 | 10217 | } |
2ff7e61e | 10218 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
10219 | return err; |
10220 | } | |
16432985 | 10221 | |
0af3d00b JB |
10222 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10223 | u64 start, u64 num_bytes, u64 min_size, | |
10224 | loff_t actual_len, u64 *alloc_hint, | |
10225 | struct btrfs_trans_handle *trans) | |
d899e052 | 10226 | { |
0b246afa | 10227 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10228 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10229 | struct extent_map *em; | |
d899e052 YZ |
10230 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10231 | struct btrfs_key ins; | |
d899e052 | 10232 | u64 cur_offset = start; |
55a61d1d | 10233 | u64 i_size; |
154ea289 | 10234 | u64 cur_bytes; |
0b670dc4 | 10235 | u64 last_alloc = (u64)-1; |
d899e052 | 10236 | int ret = 0; |
0af3d00b | 10237 | bool own_trans = true; |
18513091 | 10238 | u64 end = start + num_bytes - 1; |
d899e052 | 10239 | |
0af3d00b JB |
10240 | if (trans) |
10241 | own_trans = false; | |
d899e052 | 10242 | while (num_bytes > 0) { |
0af3d00b JB |
10243 | if (own_trans) { |
10244 | trans = btrfs_start_transaction(root, 3); | |
10245 | if (IS_ERR(trans)) { | |
10246 | ret = PTR_ERR(trans); | |
10247 | break; | |
10248 | } | |
5a303d5d YZ |
10249 | } |
10250 | ||
ee22184b | 10251 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10252 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10253 | /* |
10254 | * If we are severely fragmented we could end up with really | |
10255 | * small allocations, so if the allocator is returning small | |
10256 | * chunks lets make its job easier by only searching for those | |
10257 | * sized chunks. | |
10258 | */ | |
10259 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10260 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10261 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10262 | if (ret) { |
0af3d00b | 10263 | if (own_trans) |
3a45bb20 | 10264 | btrfs_end_transaction(trans); |
a22285a6 | 10265 | break; |
d899e052 | 10266 | } |
0b246afa | 10267 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10268 | |
0b670dc4 | 10269 | last_alloc = ins.offset; |
d899e052 YZ |
10270 | ret = insert_reserved_file_extent(trans, inode, |
10271 | cur_offset, ins.objectid, | |
10272 | ins.offset, ins.offset, | |
920bbbfb | 10273 | ins.offset, 0, 0, 0, |
d899e052 | 10274 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10275 | if (ret) { |
2ff7e61e | 10276 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10277 | ins.offset, 0); |
66642832 | 10278 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10279 | if (own_trans) |
3a45bb20 | 10280 | btrfs_end_transaction(trans); |
79787eaa JM |
10281 | break; |
10282 | } | |
31193213 | 10283 | |
dcdbc059 | 10284 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10285 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10286 | |
5dc562c5 JB |
10287 | em = alloc_extent_map(); |
10288 | if (!em) { | |
10289 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10290 | &BTRFS_I(inode)->runtime_flags); | |
10291 | goto next; | |
10292 | } | |
10293 | ||
10294 | em->start = cur_offset; | |
10295 | em->orig_start = cur_offset; | |
10296 | em->len = ins.offset; | |
10297 | em->block_start = ins.objectid; | |
10298 | em->block_len = ins.offset; | |
b4939680 | 10299 | em->orig_block_len = ins.offset; |
cc95bef6 | 10300 | em->ram_bytes = ins.offset; |
0b246afa | 10301 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10302 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10303 | em->generation = trans->transid; | |
10304 | ||
10305 | while (1) { | |
10306 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10307 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10308 | write_unlock(&em_tree->lock); |
10309 | if (ret != -EEXIST) | |
10310 | break; | |
dcdbc059 | 10311 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10312 | cur_offset + ins.offset - 1, |
10313 | 0); | |
10314 | } | |
10315 | free_extent_map(em); | |
10316 | next: | |
d899e052 YZ |
10317 | num_bytes -= ins.offset; |
10318 | cur_offset += ins.offset; | |
efa56464 | 10319 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10320 | |
0c4d2d95 | 10321 | inode_inc_iversion(inode); |
c2050a45 | 10322 | inode->i_ctime = current_time(inode); |
6cbff00f | 10323 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10324 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10325 | (actual_len > inode->i_size) && |
10326 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10327 | if (cur_offset > actual_len) |
55a61d1d | 10328 | i_size = actual_len; |
d1ea6a61 | 10329 | else |
55a61d1d JB |
10330 | i_size = cur_offset; |
10331 | i_size_write(inode, i_size); | |
10332 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10333 | } |
10334 | ||
d899e052 | 10335 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10336 | |
10337 | if (ret) { | |
66642832 | 10338 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10339 | if (own_trans) |
3a45bb20 | 10340 | btrfs_end_transaction(trans); |
79787eaa JM |
10341 | break; |
10342 | } | |
d899e052 | 10343 | |
0af3d00b | 10344 | if (own_trans) |
3a45bb20 | 10345 | btrfs_end_transaction(trans); |
5a303d5d | 10346 | } |
18513091 | 10347 | if (cur_offset < end) |
bc42bda2 | 10348 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
18513091 | 10349 | end - cur_offset + 1); |
d899e052 YZ |
10350 | return ret; |
10351 | } | |
10352 | ||
0af3d00b JB |
10353 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10354 | u64 start, u64 num_bytes, u64 min_size, | |
10355 | loff_t actual_len, u64 *alloc_hint) | |
10356 | { | |
10357 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10358 | min_size, actual_len, alloc_hint, | |
10359 | NULL); | |
10360 | } | |
10361 | ||
10362 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10363 | struct btrfs_trans_handle *trans, int mode, | |
10364 | u64 start, u64 num_bytes, u64 min_size, | |
10365 | loff_t actual_len, u64 *alloc_hint) | |
10366 | { | |
10367 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10368 | min_size, actual_len, alloc_hint, trans); | |
10369 | } | |
10370 | ||
e6dcd2dc CM |
10371 | static int btrfs_set_page_dirty(struct page *page) |
10372 | { | |
e6dcd2dc CM |
10373 | return __set_page_dirty_nobuffers(page); |
10374 | } | |
10375 | ||
10556cb2 | 10376 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10377 | { |
b83cc969 | 10378 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10379 | umode_t mode = inode->i_mode; |
b83cc969 | 10380 | |
cb6db4e5 JM |
10381 | if (mask & MAY_WRITE && |
10382 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10383 | if (btrfs_root_readonly(root)) | |
10384 | return -EROFS; | |
10385 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10386 | return -EACCES; | |
10387 | } | |
2830ba7f | 10388 | return generic_permission(inode, mask); |
fdebe2bd | 10389 | } |
39279cc3 | 10390 | |
ef3b9af5 FM |
10391 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10392 | { | |
2ff7e61e | 10393 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10394 | struct btrfs_trans_handle *trans; |
10395 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10396 | struct inode *inode = NULL; | |
10397 | u64 objectid; | |
10398 | u64 index; | |
10399 | int ret = 0; | |
10400 | ||
10401 | /* | |
10402 | * 5 units required for adding orphan entry | |
10403 | */ | |
10404 | trans = btrfs_start_transaction(root, 5); | |
10405 | if (IS_ERR(trans)) | |
10406 | return PTR_ERR(trans); | |
10407 | ||
10408 | ret = btrfs_find_free_ino(root, &objectid); | |
10409 | if (ret) | |
10410 | goto out; | |
10411 | ||
10412 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10413 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10414 | if (IS_ERR(inode)) { |
10415 | ret = PTR_ERR(inode); | |
10416 | inode = NULL; | |
10417 | goto out; | |
10418 | } | |
10419 | ||
ef3b9af5 FM |
10420 | inode->i_fop = &btrfs_file_operations; |
10421 | inode->i_op = &btrfs_file_inode_operations; | |
10422 | ||
10423 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10424 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10425 | ||
b0d5d10f CM |
10426 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10427 | if (ret) | |
32955c54 | 10428 | goto out; |
b0d5d10f CM |
10429 | |
10430 | ret = btrfs_update_inode(trans, root, inode); | |
10431 | if (ret) | |
32955c54 | 10432 | goto out; |
73f2e545 | 10433 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10434 | if (ret) |
32955c54 | 10435 | goto out; |
ef3b9af5 | 10436 | |
5762b5c9 FM |
10437 | /* |
10438 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10439 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10440 | * through: | |
10441 | * | |
10442 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10443 | */ | |
10444 | set_nlink(inode, 1); | |
ef3b9af5 | 10445 | d_tmpfile(dentry, inode); |
32955c54 | 10446 | unlock_new_inode(inode); |
ef3b9af5 | 10447 | mark_inode_dirty(inode); |
ef3b9af5 | 10448 | out: |
3a45bb20 | 10449 | btrfs_end_transaction(trans); |
32955c54 AV |
10450 | if (ret && inode) |
10451 | discard_new_inode(inode); | |
2ff7e61e | 10452 | btrfs_btree_balance_dirty(fs_info); |
ef3b9af5 FM |
10453 | return ret; |
10454 | } | |
10455 | ||
5cdc84bf | 10456 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
c6100a4b | 10457 | { |
5cdc84bf | 10458 | struct inode *inode = tree->private_data; |
c6100a4b JB |
10459 | unsigned long index = start >> PAGE_SHIFT; |
10460 | unsigned long end_index = end >> PAGE_SHIFT; | |
10461 | struct page *page; | |
10462 | ||
10463 | while (index <= end_index) { | |
10464 | page = find_get_page(inode->i_mapping, index); | |
10465 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10466 | set_page_writeback(page); | |
10467 | put_page(page); | |
10468 | index++; | |
10469 | } | |
10470 | } | |
10471 | ||
ed46ff3d OS |
10472 | #ifdef CONFIG_SWAP |
10473 | /* | |
10474 | * Add an entry indicating a block group or device which is pinned by a | |
10475 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a | |
10476 | * negative errno on failure. | |
10477 | */ | |
10478 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, | |
10479 | bool is_block_group) | |
10480 | { | |
10481 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10482 | struct btrfs_swapfile_pin *sp, *entry; | |
10483 | struct rb_node **p; | |
10484 | struct rb_node *parent = NULL; | |
10485 | ||
10486 | sp = kmalloc(sizeof(*sp), GFP_NOFS); | |
10487 | if (!sp) | |
10488 | return -ENOMEM; | |
10489 | sp->ptr = ptr; | |
10490 | sp->inode = inode; | |
10491 | sp->is_block_group = is_block_group; | |
10492 | ||
10493 | spin_lock(&fs_info->swapfile_pins_lock); | |
10494 | p = &fs_info->swapfile_pins.rb_node; | |
10495 | while (*p) { | |
10496 | parent = *p; | |
10497 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); | |
10498 | if (sp->ptr < entry->ptr || | |
10499 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { | |
10500 | p = &(*p)->rb_left; | |
10501 | } else if (sp->ptr > entry->ptr || | |
10502 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { | |
10503 | p = &(*p)->rb_right; | |
10504 | } else { | |
10505 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10506 | kfree(sp); | |
10507 | return 1; | |
10508 | } | |
10509 | } | |
10510 | rb_link_node(&sp->node, parent, p); | |
10511 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); | |
10512 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10513 | return 0; | |
10514 | } | |
10515 | ||
10516 | /* Free all of the entries pinned by this swapfile. */ | |
10517 | static void btrfs_free_swapfile_pins(struct inode *inode) | |
10518 | { | |
10519 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10520 | struct btrfs_swapfile_pin *sp; | |
10521 | struct rb_node *node, *next; | |
10522 | ||
10523 | spin_lock(&fs_info->swapfile_pins_lock); | |
10524 | node = rb_first(&fs_info->swapfile_pins); | |
10525 | while (node) { | |
10526 | next = rb_next(node); | |
10527 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
10528 | if (sp->inode == inode) { | |
10529 | rb_erase(&sp->node, &fs_info->swapfile_pins); | |
10530 | if (sp->is_block_group) | |
10531 | btrfs_put_block_group(sp->ptr); | |
10532 | kfree(sp); | |
10533 | } | |
10534 | node = next; | |
10535 | } | |
10536 | spin_unlock(&fs_info->swapfile_pins_lock); | |
10537 | } | |
10538 | ||
10539 | struct btrfs_swap_info { | |
10540 | u64 start; | |
10541 | u64 block_start; | |
10542 | u64 block_len; | |
10543 | u64 lowest_ppage; | |
10544 | u64 highest_ppage; | |
10545 | unsigned long nr_pages; | |
10546 | int nr_extents; | |
10547 | }; | |
10548 | ||
10549 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, | |
10550 | struct btrfs_swap_info *bsi) | |
10551 | { | |
10552 | unsigned long nr_pages; | |
10553 | u64 first_ppage, first_ppage_reported, next_ppage; | |
10554 | int ret; | |
10555 | ||
10556 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; | |
10557 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, | |
10558 | PAGE_SIZE) >> PAGE_SHIFT; | |
10559 | ||
10560 | if (first_ppage >= next_ppage) | |
10561 | return 0; | |
10562 | nr_pages = next_ppage - first_ppage; | |
10563 | ||
10564 | first_ppage_reported = first_ppage; | |
10565 | if (bsi->start == 0) | |
10566 | first_ppage_reported++; | |
10567 | if (bsi->lowest_ppage > first_ppage_reported) | |
10568 | bsi->lowest_ppage = first_ppage_reported; | |
10569 | if (bsi->highest_ppage < (next_ppage - 1)) | |
10570 | bsi->highest_ppage = next_ppage - 1; | |
10571 | ||
10572 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); | |
10573 | if (ret < 0) | |
10574 | return ret; | |
10575 | bsi->nr_extents += ret; | |
10576 | bsi->nr_pages += nr_pages; | |
10577 | return 0; | |
10578 | } | |
10579 | ||
10580 | static void btrfs_swap_deactivate(struct file *file) | |
10581 | { | |
10582 | struct inode *inode = file_inode(file); | |
10583 | ||
10584 | btrfs_free_swapfile_pins(inode); | |
10585 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); | |
10586 | } | |
10587 | ||
10588 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10589 | sector_t *span) | |
10590 | { | |
10591 | struct inode *inode = file_inode(file); | |
10592 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
10593 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
10594 | struct extent_state *cached_state = NULL; | |
10595 | struct extent_map *em = NULL; | |
10596 | struct btrfs_device *device = NULL; | |
10597 | struct btrfs_swap_info bsi = { | |
10598 | .lowest_ppage = (sector_t)-1ULL, | |
10599 | }; | |
10600 | int ret = 0; | |
10601 | u64 isize; | |
10602 | u64 start; | |
10603 | ||
10604 | /* | |
10605 | * If the swap file was just created, make sure delalloc is done. If the | |
10606 | * file changes again after this, the user is doing something stupid and | |
10607 | * we don't really care. | |
10608 | */ | |
10609 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
10610 | if (ret) | |
10611 | return ret; | |
10612 | ||
10613 | /* | |
10614 | * The inode is locked, so these flags won't change after we check them. | |
10615 | */ | |
10616 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { | |
10617 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10618 | return -EINVAL; | |
10619 | } | |
10620 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { | |
10621 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); | |
10622 | return -EINVAL; | |
10623 | } | |
10624 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
10625 | btrfs_warn(fs_info, "swapfile must not be checksummed"); | |
10626 | return -EINVAL; | |
10627 | } | |
10628 | ||
10629 | /* | |
10630 | * Balance or device remove/replace/resize can move stuff around from | |
10631 | * under us. The EXCL_OP flag makes sure they aren't running/won't run | |
10632 | * concurrently while we are mapping the swap extents, and | |
10633 | * fs_info->swapfile_pins prevents them from running while the swap file | |
10634 | * is active and moving the extents. Note that this also prevents a | |
10635 | * concurrent device add which isn't actually necessary, but it's not | |
10636 | * really worth the trouble to allow it. | |
10637 | */ | |
10638 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { | |
10639 | btrfs_warn(fs_info, | |
10640 | "cannot activate swapfile while exclusive operation is running"); | |
10641 | return -EBUSY; | |
10642 | } | |
10643 | /* | |
10644 | * Snapshots can create extents which require COW even if NODATACOW is | |
10645 | * set. We use this counter to prevent snapshots. We must increment it | |
10646 | * before walking the extents because we don't want a concurrent | |
10647 | * snapshot to run after we've already checked the extents. | |
10648 | */ | |
10649 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); | |
10650 | ||
10651 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); | |
10652 | ||
10653 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); | |
10654 | start = 0; | |
10655 | while (start < isize) { | |
10656 | u64 logical_block_start, physical_block_start; | |
10657 | struct btrfs_block_group_cache *bg; | |
10658 | u64 len = isize - start; | |
10659 | ||
10660 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); | |
10661 | if (IS_ERR(em)) { | |
10662 | ret = PTR_ERR(em); | |
10663 | goto out; | |
10664 | } | |
10665 | ||
10666 | if (em->block_start == EXTENT_MAP_HOLE) { | |
10667 | btrfs_warn(fs_info, "swapfile must not have holes"); | |
10668 | ret = -EINVAL; | |
10669 | goto out; | |
10670 | } | |
10671 | if (em->block_start == EXTENT_MAP_INLINE) { | |
10672 | /* | |
10673 | * It's unlikely we'll ever actually find ourselves | |
10674 | * here, as a file small enough to fit inline won't be | |
10675 | * big enough to store more than the swap header, but in | |
10676 | * case something changes in the future, let's catch it | |
10677 | * here rather than later. | |
10678 | */ | |
10679 | btrfs_warn(fs_info, "swapfile must not be inline"); | |
10680 | ret = -EINVAL; | |
10681 | goto out; | |
10682 | } | |
10683 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
10684 | btrfs_warn(fs_info, "swapfile must not be compressed"); | |
10685 | ret = -EINVAL; | |
10686 | goto out; | |
10687 | } | |
10688 | ||
10689 | logical_block_start = em->block_start + (start - em->start); | |
10690 | len = min(len, em->len - (start - em->start)); | |
10691 | free_extent_map(em); | |
10692 | em = NULL; | |
10693 | ||
10694 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL); | |
10695 | if (ret < 0) { | |
10696 | goto out; | |
10697 | } else if (ret) { | |
10698 | ret = 0; | |
10699 | } else { | |
10700 | btrfs_warn(fs_info, | |
10701 | "swapfile must not be copy-on-write"); | |
10702 | ret = -EINVAL; | |
10703 | goto out; | |
10704 | } | |
10705 | ||
10706 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); | |
10707 | if (IS_ERR(em)) { | |
10708 | ret = PTR_ERR(em); | |
10709 | goto out; | |
10710 | } | |
10711 | ||
10712 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
10713 | btrfs_warn(fs_info, | |
10714 | "swapfile must have single data profile"); | |
10715 | ret = -EINVAL; | |
10716 | goto out; | |
10717 | } | |
10718 | ||
10719 | if (device == NULL) { | |
10720 | device = em->map_lookup->stripes[0].dev; | |
10721 | ret = btrfs_add_swapfile_pin(inode, device, false); | |
10722 | if (ret == 1) | |
10723 | ret = 0; | |
10724 | else if (ret) | |
10725 | goto out; | |
10726 | } else if (device != em->map_lookup->stripes[0].dev) { | |
10727 | btrfs_warn(fs_info, "swapfile must be on one device"); | |
10728 | ret = -EINVAL; | |
10729 | goto out; | |
10730 | } | |
10731 | ||
10732 | physical_block_start = (em->map_lookup->stripes[0].physical + | |
10733 | (logical_block_start - em->start)); | |
10734 | len = min(len, em->len - (logical_block_start - em->start)); | |
10735 | free_extent_map(em); | |
10736 | em = NULL; | |
10737 | ||
10738 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); | |
10739 | if (!bg) { | |
10740 | btrfs_warn(fs_info, | |
10741 | "could not find block group containing swapfile"); | |
10742 | ret = -EINVAL; | |
10743 | goto out; | |
10744 | } | |
10745 | ||
10746 | ret = btrfs_add_swapfile_pin(inode, bg, true); | |
10747 | if (ret) { | |
10748 | btrfs_put_block_group(bg); | |
10749 | if (ret == 1) | |
10750 | ret = 0; | |
10751 | else | |
10752 | goto out; | |
10753 | } | |
10754 | ||
10755 | if (bsi.block_len && | |
10756 | bsi.block_start + bsi.block_len == physical_block_start) { | |
10757 | bsi.block_len += len; | |
10758 | } else { | |
10759 | if (bsi.block_len) { | |
10760 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10761 | if (ret) | |
10762 | goto out; | |
10763 | } | |
10764 | bsi.start = start; | |
10765 | bsi.block_start = physical_block_start; | |
10766 | bsi.block_len = len; | |
10767 | } | |
10768 | ||
10769 | start += len; | |
10770 | } | |
10771 | ||
10772 | if (bsi.block_len) | |
10773 | ret = btrfs_add_swap_extent(sis, &bsi); | |
10774 | ||
10775 | out: | |
10776 | if (!IS_ERR_OR_NULL(em)) | |
10777 | free_extent_map(em); | |
10778 | ||
10779 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); | |
10780 | ||
10781 | if (ret) | |
10782 | btrfs_swap_deactivate(file); | |
10783 | ||
10784 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); | |
10785 | ||
10786 | if (ret) | |
10787 | return ret; | |
10788 | ||
10789 | if (device) | |
10790 | sis->bdev = device->bdev; | |
10791 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; | |
10792 | sis->max = bsi.nr_pages; | |
10793 | sis->pages = bsi.nr_pages - 1; | |
10794 | sis->highest_bit = bsi.nr_pages - 1; | |
10795 | return bsi.nr_extents; | |
10796 | } | |
10797 | #else | |
10798 | static void btrfs_swap_deactivate(struct file *file) | |
10799 | { | |
10800 | } | |
10801 | ||
10802 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
10803 | sector_t *span) | |
10804 | { | |
10805 | return -EOPNOTSUPP; | |
10806 | } | |
10807 | #endif | |
10808 | ||
6e1d5dcc | 10809 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10810 | .getattr = btrfs_getattr, |
39279cc3 CM |
10811 | .lookup = btrfs_lookup, |
10812 | .create = btrfs_create, | |
10813 | .unlink = btrfs_unlink, | |
10814 | .link = btrfs_link, | |
10815 | .mkdir = btrfs_mkdir, | |
10816 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10817 | .rename = btrfs_rename2, |
39279cc3 CM |
10818 | .symlink = btrfs_symlink, |
10819 | .setattr = btrfs_setattr, | |
618e21d5 | 10820 | .mknod = btrfs_mknod, |
5103e947 | 10821 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10822 | .permission = btrfs_permission, |
4e34e719 | 10823 | .get_acl = btrfs_get_acl, |
996a710d | 10824 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10825 | .update_time = btrfs_update_time, |
ef3b9af5 | 10826 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10827 | }; |
6e1d5dcc | 10828 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10829 | .lookup = btrfs_lookup, |
fdebe2bd | 10830 | .permission = btrfs_permission, |
93fd63c2 | 10831 | .update_time = btrfs_update_time, |
39279cc3 | 10832 | }; |
76dda93c | 10833 | |
828c0950 | 10834 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10835 | .llseek = generic_file_llseek, |
10836 | .read = generic_read_dir, | |
02dbfc99 | 10837 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10838 | .open = btrfs_opendir, |
34287aa3 | 10839 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10840 | #ifdef CONFIG_COMPAT |
4c63c245 | 10841 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10842 | #endif |
6bf13c0c | 10843 | .release = btrfs_release_file, |
e02119d5 | 10844 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10845 | }; |
10846 | ||
20e5506b | 10847 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10848 | /* mandatory callbacks */ |
065631f6 | 10849 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac CM |
10850 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
10851 | }; | |
10852 | ||
35054394 CM |
10853 | /* |
10854 | * btrfs doesn't support the bmap operation because swapfiles | |
10855 | * use bmap to make a mapping of extents in the file. They assume | |
10856 | * these extents won't change over the life of the file and they | |
10857 | * use the bmap result to do IO directly to the drive. | |
10858 | * | |
10859 | * the btrfs bmap call would return logical addresses that aren't | |
10860 | * suitable for IO and they also will change frequently as COW | |
10861 | * operations happen. So, swapfile + btrfs == corruption. | |
10862 | * | |
10863 | * For now we're avoiding this by dropping bmap. | |
10864 | */ | |
7f09410b | 10865 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10866 | .readpage = btrfs_readpage, |
10867 | .writepage = btrfs_writepage, | |
b293f02e | 10868 | .writepages = btrfs_writepages, |
3ab2fb5a | 10869 | .readpages = btrfs_readpages, |
16432985 | 10870 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10871 | .invalidatepage = btrfs_invalidatepage, |
10872 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10873 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10874 | .error_remove_page = generic_error_remove_page, |
ed46ff3d OS |
10875 | .swap_activate = btrfs_swap_activate, |
10876 | .swap_deactivate = btrfs_swap_deactivate, | |
39279cc3 CM |
10877 | }; |
10878 | ||
6e1d5dcc | 10879 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10880 | .getattr = btrfs_getattr, |
10881 | .setattr = btrfs_setattr, | |
5103e947 | 10882 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10883 | .permission = btrfs_permission, |
1506fcc8 | 10884 | .fiemap = btrfs_fiemap, |
4e34e719 | 10885 | .get_acl = btrfs_get_acl, |
996a710d | 10886 | .set_acl = btrfs_set_acl, |
e41f941a | 10887 | .update_time = btrfs_update_time, |
39279cc3 | 10888 | }; |
6e1d5dcc | 10889 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10890 | .getattr = btrfs_getattr, |
10891 | .setattr = btrfs_setattr, | |
fdebe2bd | 10892 | .permission = btrfs_permission, |
33268eaf | 10893 | .listxattr = btrfs_listxattr, |
4e34e719 | 10894 | .get_acl = btrfs_get_acl, |
996a710d | 10895 | .set_acl = btrfs_set_acl, |
e41f941a | 10896 | .update_time = btrfs_update_time, |
618e21d5 | 10897 | }; |
6e1d5dcc | 10898 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10899 | .get_link = page_get_link, |
f209561a | 10900 | .getattr = btrfs_getattr, |
22c44fe6 | 10901 | .setattr = btrfs_setattr, |
fdebe2bd | 10902 | .permission = btrfs_permission, |
0279b4cd | 10903 | .listxattr = btrfs_listxattr, |
e41f941a | 10904 | .update_time = btrfs_update_time, |
39279cc3 | 10905 | }; |
76dda93c | 10906 | |
82d339d9 | 10907 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c YZ |
10908 | .d_delete = btrfs_dentry_delete, |
10909 | }; |