Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
8f18cf13 | 19 | #include <linux/kernel.h> |
065631f6 | 20 | #include <linux/bio.h> |
39279cc3 | 21 | #include <linux/buffer_head.h> |
f2eb0a24 | 22 | #include <linux/file.h> |
39279cc3 CM |
23 | #include <linux/fs.h> |
24 | #include <linux/pagemap.h> | |
25 | #include <linux/highmem.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/string.h> | |
39279cc3 CM |
29 | #include <linux/backing-dev.h> |
30 | #include <linux/mpage.h> | |
31 | #include <linux/swap.h> | |
32 | #include <linux/writeback.h> | |
39279cc3 | 33 | #include <linux/compat.h> |
9ebefb18 | 34 | #include <linux/bit_spinlock.h> |
5103e947 | 35 | #include <linux/xattr.h> |
33268eaf | 36 | #include <linux/posix_acl.h> |
d899e052 | 37 | #include <linux/falloc.h> |
5a0e3ad6 | 38 | #include <linux/slab.h> |
7a36ddec | 39 | #include <linux/ratelimit.h> |
22c44fe6 | 40 | #include <linux/mount.h> |
55e301fd | 41 | #include <linux/btrfs.h> |
53b381b3 | 42 | #include <linux/blkdev.h> |
f23b5a59 | 43 | #include <linux/posix_acl_xattr.h> |
e2e40f2c | 44 | #include <linux/uio.h> |
69fe2d75 | 45 | #include <linux/magic.h> |
39279cc3 CM |
46 | #include "ctree.h" |
47 | #include "disk-io.h" | |
48 | #include "transaction.h" | |
49 | #include "btrfs_inode.h" | |
39279cc3 | 50 | #include "print-tree.h" |
e6dcd2dc | 51 | #include "ordered-data.h" |
95819c05 | 52 | #include "xattr.h" |
e02119d5 | 53 | #include "tree-log.h" |
4a54c8c1 | 54 | #include "volumes.h" |
c8b97818 | 55 | #include "compression.h" |
b4ce94de | 56 | #include "locking.h" |
dc89e982 | 57 | #include "free-space-cache.h" |
581bb050 | 58 | #include "inode-map.h" |
38c227d8 | 59 | #include "backref.h" |
f23b5a59 | 60 | #include "hash.h" |
63541927 | 61 | #include "props.h" |
31193213 | 62 | #include "qgroup.h" |
dda3245e | 63 | #include "dedupe.h" |
39279cc3 CM |
64 | |
65 | struct btrfs_iget_args { | |
90d3e592 | 66 | struct btrfs_key *location; |
39279cc3 CM |
67 | struct btrfs_root *root; |
68 | }; | |
69 | ||
f28a4928 | 70 | struct btrfs_dio_data { |
f28a4928 FM |
71 | u64 reserve; |
72 | u64 unsubmitted_oe_range_start; | |
73 | u64 unsubmitted_oe_range_end; | |
4aaedfb0 | 74 | int overwrite; |
f28a4928 FM |
75 | }; |
76 | ||
6e1d5dcc AD |
77 | static const struct inode_operations btrfs_dir_inode_operations; |
78 | static const struct inode_operations btrfs_symlink_inode_operations; | |
79 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
80 | static const struct inode_operations btrfs_special_inode_operations; | |
81 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
82 | static const struct address_space_operations btrfs_aops; |
83 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 84 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
86 | |
87 | static struct kmem_cache *btrfs_inode_cachep; | |
88 | struct kmem_cache *btrfs_trans_handle_cachep; | |
39279cc3 | 89 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 90 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
91 | |
92 | #define S_SHIFT 12 | |
4d4ab6d6 | 93 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
39279cc3 CM |
94 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
95 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
96 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
97 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
98 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
99 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
100 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
101 | }; | |
102 | ||
3972f260 | 103 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 104 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 105 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
106 | static noinline int cow_file_range(struct inode *inode, |
107 | struct page *locked_page, | |
dda3245e WX |
108 | u64 start, u64 end, u64 delalloc_end, |
109 | int *page_started, unsigned long *nr_written, | |
110 | int unlock, struct btrfs_dedupe_hash *hash); | |
6f9994db LB |
111 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
112 | u64 orig_start, u64 block_start, | |
113 | u64 block_len, u64 orig_block_len, | |
114 | u64 ram_bytes, int compress_type, | |
115 | int type); | |
7b128766 | 116 | |
52427260 QW |
117 | static void __endio_write_update_ordered(struct inode *inode, |
118 | const u64 offset, const u64 bytes, | |
119 | const bool uptodate); | |
120 | ||
121 | /* | |
122 | * Cleanup all submitted ordered extents in specified range to handle errors | |
123 | * from the fill_dellaloc() callback. | |
124 | * | |
125 | * NOTE: caller must ensure that when an error happens, it can not call | |
126 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING | |
127 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata | |
128 | * to be released, which we want to happen only when finishing the ordered | |
129 | * extent (btrfs_finish_ordered_io()). Also note that the caller of the | |
130 | * fill_delalloc() callback already does proper cleanup for the first page of | |
131 | * the range, that is, it invokes the callback writepage_end_io_hook() for the | |
132 | * range of the first page. | |
133 | */ | |
134 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, | |
135 | const u64 offset, | |
136 | const u64 bytes) | |
137 | { | |
63d71450 NA |
138 | unsigned long index = offset >> PAGE_SHIFT; |
139 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; | |
140 | struct page *page; | |
141 | ||
142 | while (index <= end_index) { | |
143 | page = find_get_page(inode->i_mapping, index); | |
144 | index++; | |
145 | if (!page) | |
146 | continue; | |
147 | ClearPagePrivate2(page); | |
148 | put_page(page); | |
149 | } | |
52427260 QW |
150 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, |
151 | bytes - PAGE_SIZE, false); | |
152 | } | |
153 | ||
48a3b636 | 154 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 155 | |
6a3891c5 JB |
156 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
157 | void btrfs_test_inode_set_ops(struct inode *inode) | |
158 | { | |
159 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
160 | } | |
161 | #endif | |
162 | ||
f34f57a3 | 163 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
164 | struct inode *inode, struct inode *dir, |
165 | const struct qstr *qstr) | |
0279b4cd JO |
166 | { |
167 | int err; | |
168 | ||
f34f57a3 | 169 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 170 | if (!err) |
2a7dba39 | 171 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
172 | return err; |
173 | } | |
174 | ||
c8b97818 CM |
175 | /* |
176 | * this does all the hard work for inserting an inline extent into | |
177 | * the btree. The caller should have done a btrfs_drop_extents so that | |
178 | * no overlapping inline items exist in the btree | |
179 | */ | |
40f76580 | 180 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 181 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
182 | struct btrfs_root *root, struct inode *inode, |
183 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 184 | int compress_type, |
c8b97818 CM |
185 | struct page **compressed_pages) |
186 | { | |
c8b97818 CM |
187 | struct extent_buffer *leaf; |
188 | struct page *page = NULL; | |
189 | char *kaddr; | |
190 | unsigned long ptr; | |
191 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
192 | int ret; |
193 | size_t cur_size = size; | |
c8b97818 | 194 | unsigned long offset; |
c8b97818 | 195 | |
fe3f566c | 196 | if (compressed_size && compressed_pages) |
c8b97818 | 197 | cur_size = compressed_size; |
c8b97818 | 198 | |
1acae57b | 199 | inode_add_bytes(inode, size); |
c8b97818 | 200 | |
1acae57b FDBM |
201 | if (!extent_inserted) { |
202 | struct btrfs_key key; | |
203 | size_t datasize; | |
c8b97818 | 204 | |
4a0cc7ca | 205 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 206 | key.offset = start; |
962a298f | 207 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 208 | |
1acae57b FDBM |
209 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
210 | path->leave_spinning = 1; | |
211 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
212 | datasize); | |
79b4f4c6 | 213 | if (ret) |
1acae57b | 214 | goto fail; |
c8b97818 CM |
215 | } |
216 | leaf = path->nodes[0]; | |
217 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
218 | struct btrfs_file_extent_item); | |
219 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
220 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
221 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
222 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
223 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
224 | ptr = btrfs_file_extent_inline_start(ei); | |
225 | ||
261507a0 | 226 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
227 | struct page *cpage; |
228 | int i = 0; | |
d397712b | 229 | while (compressed_size > 0) { |
c8b97818 | 230 | cpage = compressed_pages[i]; |
5b050f04 | 231 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 232 | PAGE_SIZE); |
c8b97818 | 233 | |
7ac687d9 | 234 | kaddr = kmap_atomic(cpage); |
c8b97818 | 235 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 236 | kunmap_atomic(kaddr); |
c8b97818 CM |
237 | |
238 | i++; | |
239 | ptr += cur_size; | |
240 | compressed_size -= cur_size; | |
241 | } | |
242 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 243 | compress_type); |
c8b97818 CM |
244 | } else { |
245 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 246 | start >> PAGE_SHIFT); |
c8b97818 | 247 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 248 | kaddr = kmap_atomic(page); |
09cbfeaf | 249 | offset = start & (PAGE_SIZE - 1); |
c8b97818 | 250 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 251 | kunmap_atomic(kaddr); |
09cbfeaf | 252 | put_page(page); |
c8b97818 CM |
253 | } |
254 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 255 | btrfs_release_path(path); |
c8b97818 | 256 | |
c2167754 YZ |
257 | /* |
258 | * we're an inline extent, so nobody can | |
259 | * extend the file past i_size without locking | |
260 | * a page we already have locked. | |
261 | * | |
262 | * We must do any isize and inode updates | |
263 | * before we unlock the pages. Otherwise we | |
264 | * could end up racing with unlink. | |
265 | */ | |
c8b97818 | 266 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 267 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 268 | |
c8b97818 | 269 | fail: |
79b4f4c6 | 270 | return ret; |
c8b97818 CM |
271 | } |
272 | ||
273 | ||
274 | /* | |
275 | * conditionally insert an inline extent into the file. This | |
276 | * does the checks required to make sure the data is small enough | |
277 | * to fit as an inline extent. | |
278 | */ | |
00361589 JB |
279 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
280 | struct inode *inode, u64 start, | |
281 | u64 end, size_t compressed_size, | |
282 | int compress_type, | |
283 | struct page **compressed_pages) | |
c8b97818 | 284 | { |
0b246afa | 285 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 286 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
287 | u64 isize = i_size_read(inode); |
288 | u64 actual_end = min(end + 1, isize); | |
289 | u64 inline_len = actual_end - start; | |
0b246afa | 290 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
291 | u64 data_len = inline_len; |
292 | int ret; | |
1acae57b FDBM |
293 | struct btrfs_path *path; |
294 | int extent_inserted = 0; | |
295 | u32 extent_item_size; | |
c8b97818 CM |
296 | |
297 | if (compressed_size) | |
298 | data_len = compressed_size; | |
299 | ||
300 | if (start > 0 || | |
0b246afa JM |
301 | actual_end > fs_info->sectorsize || |
302 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 303 | (!compressed_size && |
0b246afa | 304 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 305 | end + 1 < isize || |
0b246afa | 306 | data_len > fs_info->max_inline) { |
c8b97818 CM |
307 | return 1; |
308 | } | |
309 | ||
1acae57b FDBM |
310 | path = btrfs_alloc_path(); |
311 | if (!path) | |
312 | return -ENOMEM; | |
313 | ||
00361589 | 314 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
315 | if (IS_ERR(trans)) { |
316 | btrfs_free_path(path); | |
00361589 | 317 | return PTR_ERR(trans); |
1acae57b | 318 | } |
69fe2d75 | 319 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
00361589 | 320 | |
1acae57b FDBM |
321 | if (compressed_size && compressed_pages) |
322 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
323 | compressed_size); | |
324 | else | |
325 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
326 | inline_len); | |
327 | ||
328 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
329 | start, aligned_end, NULL, | |
330 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 | 331 | if (ret) { |
66642832 | 332 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
333 | goto out; |
334 | } | |
c8b97818 CM |
335 | |
336 | if (isize > actual_end) | |
337 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
338 | ret = insert_inline_extent(trans, path, extent_inserted, |
339 | root, inode, start, | |
c8b97818 | 340 | inline_len, compressed_size, |
fe3f566c | 341 | compress_type, compressed_pages); |
2adcac1a | 342 | if (ret && ret != -ENOSPC) { |
66642832 | 343 | btrfs_abort_transaction(trans, ret); |
00361589 | 344 | goto out; |
2adcac1a | 345 | } else if (ret == -ENOSPC) { |
00361589 JB |
346 | ret = 1; |
347 | goto out; | |
79787eaa | 348 | } |
2adcac1a | 349 | |
bdc20e67 | 350 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
dcdbc059 | 351 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
00361589 | 352 | out: |
94ed938a QW |
353 | /* |
354 | * Don't forget to free the reserved space, as for inlined extent | |
355 | * it won't count as data extent, free them directly here. | |
356 | * And at reserve time, it's always aligned to page size, so | |
357 | * just free one page here. | |
358 | */ | |
bc42bda2 | 359 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 360 | btrfs_free_path(path); |
3a45bb20 | 361 | btrfs_end_transaction(trans); |
00361589 | 362 | return ret; |
c8b97818 CM |
363 | } |
364 | ||
771ed689 CM |
365 | struct async_extent { |
366 | u64 start; | |
367 | u64 ram_size; | |
368 | u64 compressed_size; | |
369 | struct page **pages; | |
370 | unsigned long nr_pages; | |
261507a0 | 371 | int compress_type; |
771ed689 CM |
372 | struct list_head list; |
373 | }; | |
374 | ||
375 | struct async_cow { | |
376 | struct inode *inode; | |
377 | struct btrfs_root *root; | |
378 | struct page *locked_page; | |
379 | u64 start; | |
380 | u64 end; | |
381 | struct list_head extents; | |
382 | struct btrfs_work work; | |
383 | }; | |
384 | ||
385 | static noinline int add_async_extent(struct async_cow *cow, | |
386 | u64 start, u64 ram_size, | |
387 | u64 compressed_size, | |
388 | struct page **pages, | |
261507a0 LZ |
389 | unsigned long nr_pages, |
390 | int compress_type) | |
771ed689 CM |
391 | { |
392 | struct async_extent *async_extent; | |
393 | ||
394 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 395 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
396 | async_extent->start = start; |
397 | async_extent->ram_size = ram_size; | |
398 | async_extent->compressed_size = compressed_size; | |
399 | async_extent->pages = pages; | |
400 | async_extent->nr_pages = nr_pages; | |
261507a0 | 401 | async_extent->compress_type = compress_type; |
771ed689 CM |
402 | list_add_tail(&async_extent->list, &cow->extents); |
403 | return 0; | |
404 | } | |
405 | ||
c2fcdcdf | 406 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
f79707b0 | 407 | { |
0b246afa | 408 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 WS |
409 | |
410 | /* force compress */ | |
0b246afa | 411 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 | 412 | return 1; |
eec63c65 DS |
413 | /* defrag ioctl */ |
414 | if (BTRFS_I(inode)->defrag_compress) | |
415 | return 1; | |
f79707b0 WS |
416 | /* bad compression ratios */ |
417 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
418 | return 0; | |
0b246afa | 419 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 | 420 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
b52aa8c9 | 421 | BTRFS_I(inode)->prop_compress) |
c2fcdcdf | 422 | return btrfs_compress_heuristic(inode, start, end); |
f79707b0 WS |
423 | return 0; |
424 | } | |
425 | ||
6158e1ce | 426 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
427 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
428 | { | |
429 | /* If this is a small write inside eof, kick off a defrag */ | |
430 | if (num_bytes < small_write && | |
6158e1ce | 431 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
432 | btrfs_add_inode_defrag(NULL, inode); |
433 | } | |
434 | ||
d352ac68 | 435 | /* |
771ed689 CM |
436 | * we create compressed extents in two phases. The first |
437 | * phase compresses a range of pages that have already been | |
438 | * locked (both pages and state bits are locked). | |
c8b97818 | 439 | * |
771ed689 CM |
440 | * This is done inside an ordered work queue, and the compression |
441 | * is spread across many cpus. The actual IO submission is step | |
442 | * two, and the ordered work queue takes care of making sure that | |
443 | * happens in the same order things were put onto the queue by | |
444 | * writepages and friends. | |
c8b97818 | 445 | * |
771ed689 CM |
446 | * If this code finds it can't get good compression, it puts an |
447 | * entry onto the work queue to write the uncompressed bytes. This | |
448 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
449 | * are written in the same order that the flusher thread sent them |
450 | * down. | |
d352ac68 | 451 | */ |
c44f649e | 452 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
453 | struct page *locked_page, |
454 | u64 start, u64 end, | |
455 | struct async_cow *async_cow, | |
456 | int *num_added) | |
b888db2b | 457 | { |
0b246afa | 458 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
b888db2b | 459 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0b246afa | 460 | u64 blocksize = fs_info->sectorsize; |
c8b97818 | 461 | u64 actual_end; |
42dc7bab | 462 | u64 isize = i_size_read(inode); |
e6dcd2dc | 463 | int ret = 0; |
c8b97818 CM |
464 | struct page **pages = NULL; |
465 | unsigned long nr_pages; | |
c8b97818 CM |
466 | unsigned long total_compressed = 0; |
467 | unsigned long total_in = 0; | |
c8b97818 CM |
468 | int i; |
469 | int will_compress; | |
0b246afa | 470 | int compress_type = fs_info->compress_type; |
4adaa611 | 471 | int redirty = 0; |
b888db2b | 472 | |
6158e1ce NB |
473 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
474 | SZ_16K); | |
4cb5300b | 475 | |
42dc7bab | 476 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
477 | again: |
478 | will_compress = 0; | |
09cbfeaf | 479 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
480 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
481 | nr_pages = min_t(unsigned long, nr_pages, | |
482 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 483 | |
f03d9301 CM |
484 | /* |
485 | * we don't want to send crud past the end of i_size through | |
486 | * compression, that's just a waste of CPU time. So, if the | |
487 | * end of the file is before the start of our current | |
488 | * requested range of bytes, we bail out to the uncompressed | |
489 | * cleanup code that can deal with all of this. | |
490 | * | |
491 | * It isn't really the fastest way to fix things, but this is a | |
492 | * very uncommon corner. | |
493 | */ | |
494 | if (actual_end <= start) | |
495 | goto cleanup_and_bail_uncompressed; | |
496 | ||
c8b97818 CM |
497 | total_compressed = actual_end - start; |
498 | ||
4bcbb332 SW |
499 | /* |
500 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 501 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
502 | */ |
503 | if (total_compressed <= blocksize && | |
504 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
505 | goto cleanup_and_bail_uncompressed; | |
506 | ||
069eac78 DS |
507 | total_compressed = min_t(unsigned long, total_compressed, |
508 | BTRFS_MAX_UNCOMPRESSED); | |
c8b97818 CM |
509 | total_in = 0; |
510 | ret = 0; | |
db94535d | 511 | |
771ed689 CM |
512 | /* |
513 | * we do compression for mount -o compress and when the | |
514 | * inode has not been flagged as nocompress. This flag can | |
515 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 516 | */ |
c2fcdcdf | 517 | if (inode_need_compress(inode, start, end)) { |
c8b97818 | 518 | WARN_ON(pages); |
31e818fe | 519 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
520 | if (!pages) { |
521 | /* just bail out to the uncompressed code */ | |
522 | goto cont; | |
523 | } | |
c8b97818 | 524 | |
eec63c65 DS |
525 | if (BTRFS_I(inode)->defrag_compress) |
526 | compress_type = BTRFS_I(inode)->defrag_compress; | |
527 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 528 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 529 | |
4adaa611 CM |
530 | /* |
531 | * we need to call clear_page_dirty_for_io on each | |
532 | * page in the range. Otherwise applications with the file | |
533 | * mmap'd can wander in and change the page contents while | |
534 | * we are compressing them. | |
535 | * | |
536 | * If the compression fails for any reason, we set the pages | |
537 | * dirty again later on. | |
538 | */ | |
539 | extent_range_clear_dirty_for_io(inode, start, end); | |
540 | redirty = 1; | |
f51d2b59 DS |
541 | |
542 | /* Compression level is applied here and only here */ | |
543 | ret = btrfs_compress_pages( | |
544 | compress_type | (fs_info->compress_level << 4), | |
261507a0 | 545 | inode->i_mapping, start, |
38c31464 | 546 | pages, |
4d3a800e | 547 | &nr_pages, |
261507a0 | 548 | &total_in, |
e5d74902 | 549 | &total_compressed); |
c8b97818 CM |
550 | |
551 | if (!ret) { | |
552 | unsigned long offset = total_compressed & | |
09cbfeaf | 553 | (PAGE_SIZE - 1); |
4d3a800e | 554 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
555 | char *kaddr; |
556 | ||
557 | /* zero the tail end of the last page, we might be | |
558 | * sending it down to disk | |
559 | */ | |
560 | if (offset) { | |
7ac687d9 | 561 | kaddr = kmap_atomic(page); |
c8b97818 | 562 | memset(kaddr + offset, 0, |
09cbfeaf | 563 | PAGE_SIZE - offset); |
7ac687d9 | 564 | kunmap_atomic(kaddr); |
c8b97818 CM |
565 | } |
566 | will_compress = 1; | |
567 | } | |
568 | } | |
560f7d75 | 569 | cont: |
c8b97818 CM |
570 | if (start == 0) { |
571 | /* lets try to make an inline extent */ | |
6018ba0a | 572 | if (ret || total_in < actual_end) { |
c8b97818 | 573 | /* we didn't compress the entire range, try |
771ed689 | 574 | * to make an uncompressed inline extent. |
c8b97818 | 575 | */ |
00361589 | 576 | ret = cow_file_range_inline(root, inode, start, end, |
f74670f7 | 577 | 0, BTRFS_COMPRESS_NONE, NULL); |
c8b97818 | 578 | } else { |
771ed689 | 579 | /* try making a compressed inline extent */ |
00361589 | 580 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
581 | total_compressed, |
582 | compress_type, pages); | |
c8b97818 | 583 | } |
79787eaa | 584 | if (ret <= 0) { |
151a41bc | 585 | unsigned long clear_flags = EXTENT_DELALLOC | |
8b62f87b JB |
586 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
587 | EXTENT_DO_ACCOUNTING; | |
e6eb4314 FM |
588 | unsigned long page_error_op; |
589 | ||
e6eb4314 | 590 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 591 | |
771ed689 | 592 | /* |
79787eaa JM |
593 | * inline extent creation worked or returned error, |
594 | * we don't need to create any more async work items. | |
595 | * Unlock and free up our temp pages. | |
8b62f87b JB |
596 | * |
597 | * We use DO_ACCOUNTING here because we need the | |
598 | * delalloc_release_metadata to be done _after_ we drop | |
599 | * our outstanding extent for clearing delalloc for this | |
600 | * range. | |
771ed689 | 601 | */ |
ba8b04c1 QW |
602 | extent_clear_unlock_delalloc(inode, start, end, end, |
603 | NULL, clear_flags, | |
604 | PAGE_UNLOCK | | |
c2790a2e JB |
605 | PAGE_CLEAR_DIRTY | |
606 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 607 | page_error_op | |
c2790a2e | 608 | PAGE_END_WRITEBACK); |
c8b97818 CM |
609 | goto free_pages_out; |
610 | } | |
611 | } | |
612 | ||
613 | if (will_compress) { | |
614 | /* | |
615 | * we aren't doing an inline extent round the compressed size | |
616 | * up to a block size boundary so the allocator does sane | |
617 | * things | |
618 | */ | |
fda2832f | 619 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
620 | |
621 | /* | |
622 | * one last check to make sure the compression is really a | |
170607eb TT |
623 | * win, compare the page count read with the blocks on disk, |
624 | * compression must free at least one sector size | |
c8b97818 | 625 | */ |
09cbfeaf | 626 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 627 | if (total_compressed + blocksize <= total_in) { |
c8bb0c8b AS |
628 | *num_added += 1; |
629 | ||
630 | /* | |
631 | * The async work queues will take care of doing actual | |
632 | * allocation on disk for these compressed pages, and | |
633 | * will submit them to the elevator. | |
634 | */ | |
1170862d | 635 | add_async_extent(async_cow, start, total_in, |
4d3a800e | 636 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
637 | compress_type); |
638 | ||
1170862d TT |
639 | if (start + total_in < end) { |
640 | start += total_in; | |
c8bb0c8b AS |
641 | pages = NULL; |
642 | cond_resched(); | |
643 | goto again; | |
644 | } | |
645 | return; | |
c8b97818 CM |
646 | } |
647 | } | |
c8bb0c8b | 648 | if (pages) { |
c8b97818 CM |
649 | /* |
650 | * the compression code ran but failed to make things smaller, | |
651 | * free any pages it allocated and our page pointer array | |
652 | */ | |
4d3a800e | 653 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 654 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 655 | put_page(pages[i]); |
c8b97818 CM |
656 | } |
657 | kfree(pages); | |
658 | pages = NULL; | |
659 | total_compressed = 0; | |
4d3a800e | 660 | nr_pages = 0; |
c8b97818 CM |
661 | |
662 | /* flag the file so we don't compress in the future */ | |
0b246afa | 663 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 664 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 665 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 666 | } |
c8b97818 | 667 | } |
f03d9301 | 668 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
669 | /* |
670 | * No compression, but we still need to write the pages in the file | |
671 | * we've been given so far. redirty the locked page if it corresponds | |
672 | * to our extent and set things up for the async work queue to run | |
673 | * cow_file_range to do the normal delalloc dance. | |
674 | */ | |
675 | if (page_offset(locked_page) >= start && | |
676 | page_offset(locked_page) <= end) | |
677 | __set_page_dirty_nobuffers(locked_page); | |
678 | /* unlocked later on in the async handlers */ | |
679 | ||
680 | if (redirty) | |
681 | extent_range_redirty_for_io(inode, start, end); | |
682 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
683 | BTRFS_COMPRESS_NONE); | |
684 | *num_added += 1; | |
3b951516 | 685 | |
c44f649e | 686 | return; |
771ed689 CM |
687 | |
688 | free_pages_out: | |
4d3a800e | 689 | for (i = 0; i < nr_pages; i++) { |
771ed689 | 690 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 691 | put_page(pages[i]); |
771ed689 | 692 | } |
d397712b | 693 | kfree(pages); |
771ed689 | 694 | } |
771ed689 | 695 | |
40ae837b FM |
696 | static void free_async_extent_pages(struct async_extent *async_extent) |
697 | { | |
698 | int i; | |
699 | ||
700 | if (!async_extent->pages) | |
701 | return; | |
702 | ||
703 | for (i = 0; i < async_extent->nr_pages; i++) { | |
704 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 705 | put_page(async_extent->pages[i]); |
40ae837b FM |
706 | } |
707 | kfree(async_extent->pages); | |
708 | async_extent->nr_pages = 0; | |
709 | async_extent->pages = NULL; | |
771ed689 CM |
710 | } |
711 | ||
712 | /* | |
713 | * phase two of compressed writeback. This is the ordered portion | |
714 | * of the code, which only gets called in the order the work was | |
715 | * queued. We walk all the async extents created by compress_file_range | |
716 | * and send them down to the disk. | |
717 | */ | |
dec8f175 | 718 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
719 | struct async_cow *async_cow) |
720 | { | |
0b246afa | 721 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
722 | struct async_extent *async_extent; |
723 | u64 alloc_hint = 0; | |
771ed689 CM |
724 | struct btrfs_key ins; |
725 | struct extent_map *em; | |
726 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
771ed689 | 727 | struct extent_io_tree *io_tree; |
f5a84ee3 | 728 | int ret = 0; |
771ed689 | 729 | |
3e04e7f1 | 730 | again: |
d397712b | 731 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
732 | async_extent = list_entry(async_cow->extents.next, |
733 | struct async_extent, list); | |
734 | list_del(&async_extent->list); | |
c8b97818 | 735 | |
771ed689 CM |
736 | io_tree = &BTRFS_I(inode)->io_tree; |
737 | ||
f5a84ee3 | 738 | retry: |
771ed689 CM |
739 | /* did the compression code fall back to uncompressed IO? */ |
740 | if (!async_extent->pages) { | |
741 | int page_started = 0; | |
742 | unsigned long nr_written = 0; | |
743 | ||
744 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 745 | async_extent->start + |
d0082371 | 746 | async_extent->ram_size - 1); |
771ed689 CM |
747 | |
748 | /* allocate blocks */ | |
f5a84ee3 JB |
749 | ret = cow_file_range(inode, async_cow->locked_page, |
750 | async_extent->start, | |
751 | async_extent->start + | |
752 | async_extent->ram_size - 1, | |
dda3245e WX |
753 | async_extent->start + |
754 | async_extent->ram_size - 1, | |
755 | &page_started, &nr_written, 0, | |
756 | NULL); | |
771ed689 | 757 | |
79787eaa JM |
758 | /* JDM XXX */ |
759 | ||
771ed689 CM |
760 | /* |
761 | * if page_started, cow_file_range inserted an | |
762 | * inline extent and took care of all the unlocking | |
763 | * and IO for us. Otherwise, we need to submit | |
764 | * all those pages down to the drive. | |
765 | */ | |
f5a84ee3 | 766 | if (!page_started && !ret) |
771ed689 CM |
767 | extent_write_locked_range(io_tree, |
768 | inode, async_extent->start, | |
d397712b | 769 | async_extent->start + |
771ed689 CM |
770 | async_extent->ram_size - 1, |
771 | btrfs_get_extent, | |
772 | WB_SYNC_ALL); | |
3e04e7f1 JB |
773 | else if (ret) |
774 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
775 | kfree(async_extent); |
776 | cond_resched(); | |
777 | continue; | |
778 | } | |
779 | ||
780 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 781 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 782 | |
18513091 | 783 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
784 | async_extent->compressed_size, |
785 | async_extent->compressed_size, | |
e570fd27 | 786 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 787 | if (ret) { |
40ae837b | 788 | free_async_extent_pages(async_extent); |
3e04e7f1 | 789 | |
fdf8e2ea JB |
790 | if (ret == -ENOSPC) { |
791 | unlock_extent(io_tree, async_extent->start, | |
792 | async_extent->start + | |
793 | async_extent->ram_size - 1); | |
ce62003f LB |
794 | |
795 | /* | |
796 | * we need to redirty the pages if we decide to | |
797 | * fallback to uncompressed IO, otherwise we | |
798 | * will not submit these pages down to lower | |
799 | * layers. | |
800 | */ | |
801 | extent_range_redirty_for_io(inode, | |
802 | async_extent->start, | |
803 | async_extent->start + | |
804 | async_extent->ram_size - 1); | |
805 | ||
79787eaa | 806 | goto retry; |
fdf8e2ea | 807 | } |
3e04e7f1 | 808 | goto out_free; |
f5a84ee3 | 809 | } |
c2167754 YZ |
810 | /* |
811 | * here we're doing allocation and writeback of the | |
812 | * compressed pages | |
813 | */ | |
6f9994db LB |
814 | em = create_io_em(inode, async_extent->start, |
815 | async_extent->ram_size, /* len */ | |
816 | async_extent->start, /* orig_start */ | |
817 | ins.objectid, /* block_start */ | |
818 | ins.offset, /* block_len */ | |
819 | ins.offset, /* orig_block_len */ | |
820 | async_extent->ram_size, /* ram_bytes */ | |
821 | async_extent->compress_type, | |
822 | BTRFS_ORDERED_COMPRESSED); | |
823 | if (IS_ERR(em)) | |
824 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 825 | goto out_free_reserve; |
6f9994db | 826 | free_extent_map(em); |
3e04e7f1 | 827 | |
261507a0 LZ |
828 | ret = btrfs_add_ordered_extent_compress(inode, |
829 | async_extent->start, | |
830 | ins.objectid, | |
831 | async_extent->ram_size, | |
832 | ins.offset, | |
833 | BTRFS_ORDERED_COMPRESSED, | |
834 | async_extent->compress_type); | |
d9f85963 | 835 | if (ret) { |
dcdbc059 NB |
836 | btrfs_drop_extent_cache(BTRFS_I(inode), |
837 | async_extent->start, | |
d9f85963 FM |
838 | async_extent->start + |
839 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 840 | goto out_free_reserve; |
d9f85963 | 841 | } |
0b246afa | 842 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 843 | |
771ed689 CM |
844 | /* |
845 | * clear dirty, set writeback and unlock the pages. | |
846 | */ | |
c2790a2e | 847 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
848 | async_extent->start + |
849 | async_extent->ram_size - 1, | |
a791e35e CM |
850 | async_extent->start + |
851 | async_extent->ram_size - 1, | |
151a41bc JB |
852 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
853 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 854 | PAGE_SET_WRITEBACK); |
4e4cbee9 | 855 | if (btrfs_submit_compressed_write(inode, |
d397712b CM |
856 | async_extent->start, |
857 | async_extent->ram_size, | |
858 | ins.objectid, | |
859 | ins.offset, async_extent->pages, | |
4e4cbee9 | 860 | async_extent->nr_pages)) { |
fce2a4e6 FM |
861 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
862 | struct page *p = async_extent->pages[0]; | |
863 | const u64 start = async_extent->start; | |
864 | const u64 end = start + async_extent->ram_size - 1; | |
865 | ||
866 | p->mapping = inode->i_mapping; | |
867 | tree->ops->writepage_end_io_hook(p, start, end, | |
868 | NULL, 0); | |
869 | p->mapping = NULL; | |
ba8b04c1 QW |
870 | extent_clear_unlock_delalloc(inode, start, end, end, |
871 | NULL, 0, | |
fce2a4e6 FM |
872 | PAGE_END_WRITEBACK | |
873 | PAGE_SET_ERROR); | |
40ae837b | 874 | free_async_extent_pages(async_extent); |
fce2a4e6 | 875 | } |
771ed689 CM |
876 | alloc_hint = ins.objectid + ins.offset; |
877 | kfree(async_extent); | |
878 | cond_resched(); | |
879 | } | |
dec8f175 | 880 | return; |
3e04e7f1 | 881 | out_free_reserve: |
0b246afa | 882 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 883 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 884 | out_free: |
c2790a2e | 885 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
886 | async_extent->start + |
887 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
888 | async_extent->start + |
889 | async_extent->ram_size - 1, | |
c2790a2e | 890 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 891 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
892 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
893 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
894 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
895 | PAGE_SET_ERROR); | |
40ae837b | 896 | free_async_extent_pages(async_extent); |
79787eaa | 897 | kfree(async_extent); |
3e04e7f1 | 898 | goto again; |
771ed689 CM |
899 | } |
900 | ||
4b46fce2 JB |
901 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
902 | u64 num_bytes) | |
903 | { | |
904 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
905 | struct extent_map *em; | |
906 | u64 alloc_hint = 0; | |
907 | ||
908 | read_lock(&em_tree->lock); | |
909 | em = search_extent_mapping(em_tree, start, num_bytes); | |
910 | if (em) { | |
911 | /* | |
912 | * if block start isn't an actual block number then find the | |
913 | * first block in this inode and use that as a hint. If that | |
914 | * block is also bogus then just don't worry about it. | |
915 | */ | |
916 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
917 | free_extent_map(em); | |
918 | em = search_extent_mapping(em_tree, 0, 0); | |
919 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
920 | alloc_hint = em->block_start; | |
921 | if (em) | |
922 | free_extent_map(em); | |
923 | } else { | |
924 | alloc_hint = em->block_start; | |
925 | free_extent_map(em); | |
926 | } | |
927 | } | |
928 | read_unlock(&em_tree->lock); | |
929 | ||
930 | return alloc_hint; | |
931 | } | |
932 | ||
771ed689 CM |
933 | /* |
934 | * when extent_io.c finds a delayed allocation range in the file, | |
935 | * the call backs end up in this code. The basic idea is to | |
936 | * allocate extents on disk for the range, and create ordered data structs | |
937 | * in ram to track those extents. | |
938 | * | |
939 | * locked_page is the page that writepage had locked already. We use | |
940 | * it to make sure we don't do extra locks or unlocks. | |
941 | * | |
942 | * *page_started is set to one if we unlock locked_page and do everything | |
943 | * required to start IO on it. It may be clean and already done with | |
944 | * IO when we return. | |
945 | */ | |
00361589 JB |
946 | static noinline int cow_file_range(struct inode *inode, |
947 | struct page *locked_page, | |
dda3245e WX |
948 | u64 start, u64 end, u64 delalloc_end, |
949 | int *page_started, unsigned long *nr_written, | |
950 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 951 | { |
0b246afa | 952 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 953 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
954 | u64 alloc_hint = 0; |
955 | u64 num_bytes; | |
956 | unsigned long ram_size; | |
957 | u64 disk_num_bytes; | |
a315e68f | 958 | u64 cur_alloc_size = 0; |
0b246afa | 959 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
960 | struct btrfs_key ins; |
961 | struct extent_map *em; | |
a315e68f FM |
962 | unsigned clear_bits; |
963 | unsigned long page_ops; | |
964 | bool extent_reserved = false; | |
771ed689 CM |
965 | int ret = 0; |
966 | ||
70ddc553 | 967 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 968 | WARN_ON_ONCE(1); |
29bce2f3 JB |
969 | ret = -EINVAL; |
970 | goto out_unlock; | |
02ecd2c2 | 971 | } |
771ed689 | 972 | |
fda2832f | 973 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
974 | num_bytes = max(blocksize, num_bytes); |
975 | disk_num_bytes = num_bytes; | |
771ed689 | 976 | |
6158e1ce | 977 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 978 | |
771ed689 CM |
979 | if (start == 0) { |
980 | /* lets try to make an inline extent */ | |
f74670f7 AJ |
981 | ret = cow_file_range_inline(root, inode, start, end, 0, |
982 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 983 | if (ret == 0) { |
8b62f87b JB |
984 | /* |
985 | * We use DO_ACCOUNTING here because we need the | |
986 | * delalloc_release_metadata to be run _after_ we drop | |
987 | * our outstanding extent for clearing delalloc for this | |
988 | * range. | |
989 | */ | |
ba8b04c1 QW |
990 | extent_clear_unlock_delalloc(inode, start, end, |
991 | delalloc_end, NULL, | |
c2790a2e | 992 | EXTENT_LOCKED | EXTENT_DELALLOC | |
8b62f87b JB |
993 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
994 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
c2790a2e JB |
995 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
996 | PAGE_END_WRITEBACK); | |
771ed689 | 997 | *nr_written = *nr_written + |
09cbfeaf | 998 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 999 | *page_started = 1; |
771ed689 | 1000 | goto out; |
79787eaa | 1001 | } else if (ret < 0) { |
79787eaa | 1002 | goto out_unlock; |
771ed689 CM |
1003 | } |
1004 | } | |
1005 | ||
1006 | BUG_ON(disk_num_bytes > | |
0b246afa | 1007 | btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1008 | |
4b46fce2 | 1009 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
1010 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1011 | start + num_bytes - 1, 0); | |
771ed689 | 1012 | |
d397712b | 1013 | while (disk_num_bytes > 0) { |
287a0ab9 | 1014 | cur_alloc_size = disk_num_bytes; |
18513091 | 1015 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 1016 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 1017 | &ins, 1, 1); |
00361589 | 1018 | if (ret < 0) |
79787eaa | 1019 | goto out_unlock; |
a315e68f FM |
1020 | cur_alloc_size = ins.offset; |
1021 | extent_reserved = true; | |
d397712b | 1022 | |
771ed689 | 1023 | ram_size = ins.offset; |
6f9994db LB |
1024 | em = create_io_em(inode, start, ins.offset, /* len */ |
1025 | start, /* orig_start */ | |
1026 | ins.objectid, /* block_start */ | |
1027 | ins.offset, /* block_len */ | |
1028 | ins.offset, /* orig_block_len */ | |
1029 | ram_size, /* ram_bytes */ | |
1030 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1031 | BTRFS_ORDERED_REGULAR /* type */); |
6f9994db | 1032 | if (IS_ERR(em)) |
ace68bac | 1033 | goto out_reserve; |
6f9994db | 1034 | free_extent_map(em); |
e6dcd2dc | 1035 | |
e6dcd2dc | 1036 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1037 | ram_size, cur_alloc_size, 0); |
ace68bac | 1038 | if (ret) |
d9f85963 | 1039 | goto out_drop_extent_cache; |
c8b97818 | 1040 | |
17d217fe YZ |
1041 | if (root->root_key.objectid == |
1042 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1043 | ret = btrfs_reloc_clone_csums(inode, start, | |
1044 | cur_alloc_size); | |
4dbd80fb QW |
1045 | /* |
1046 | * Only drop cache here, and process as normal. | |
1047 | * | |
1048 | * We must not allow extent_clear_unlock_delalloc() | |
1049 | * at out_unlock label to free meta of this ordered | |
1050 | * extent, as its meta should be freed by | |
1051 | * btrfs_finish_ordered_io(). | |
1052 | * | |
1053 | * So we must continue until @start is increased to | |
1054 | * skip current ordered extent. | |
1055 | */ | |
00361589 | 1056 | if (ret) |
4dbd80fb QW |
1057 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1058 | start + ram_size - 1, 0); | |
17d217fe YZ |
1059 | } |
1060 | ||
0b246afa | 1061 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1062 | |
c8b97818 CM |
1063 | /* we're not doing compressed IO, don't unlock the first |
1064 | * page (which the caller expects to stay locked), don't | |
1065 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1066 | * |
1067 | * Do set the Private2 bit so we know this page was properly | |
1068 | * setup for writepage | |
c8b97818 | 1069 | */ |
a315e68f FM |
1070 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1071 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1072 | |
c2790a2e | 1073 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1074 | start + ram_size - 1, |
1075 | delalloc_end, locked_page, | |
c2790a2e | 1076 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1077 | page_ops); |
4dbd80fb QW |
1078 | if (disk_num_bytes < cur_alloc_size) |
1079 | disk_num_bytes = 0; | |
1080 | else | |
1081 | disk_num_bytes -= cur_alloc_size; | |
c59f8951 CM |
1082 | num_bytes -= cur_alloc_size; |
1083 | alloc_hint = ins.objectid + ins.offset; | |
1084 | start += cur_alloc_size; | |
a315e68f | 1085 | extent_reserved = false; |
4dbd80fb QW |
1086 | |
1087 | /* | |
1088 | * btrfs_reloc_clone_csums() error, since start is increased | |
1089 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1090 | * free metadata of current ordered extent, we're OK to exit. | |
1091 | */ | |
1092 | if (ret) | |
1093 | goto out_unlock; | |
b888db2b | 1094 | } |
79787eaa | 1095 | out: |
be20aa9d | 1096 | return ret; |
b7d5b0a8 | 1097 | |
d9f85963 | 1098 | out_drop_extent_cache: |
dcdbc059 | 1099 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1100 | out_reserve: |
0b246afa | 1101 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1102 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1103 | out_unlock: |
a7e3b975 FM |
1104 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1105 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1106 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1107 | PAGE_END_WRITEBACK; | |
1108 | /* | |
1109 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1110 | * failed to create the respective ordered extent, then it means that | |
1111 | * when we reserved the extent we decremented the extent's size from | |
1112 | * the data space_info's bytes_may_use counter and incremented the | |
1113 | * space_info's bytes_reserved counter by the same amount. We must make | |
1114 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1115 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1116 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1117 | */ | |
1118 | if (extent_reserved) { | |
1119 | extent_clear_unlock_delalloc(inode, start, | |
1120 | start + cur_alloc_size, | |
1121 | start + cur_alloc_size, | |
1122 | locked_page, | |
1123 | clear_bits, | |
1124 | page_ops); | |
1125 | start += cur_alloc_size; | |
1126 | if (start >= end) | |
1127 | goto out; | |
1128 | } | |
ba8b04c1 QW |
1129 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1130 | locked_page, | |
a315e68f FM |
1131 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1132 | page_ops); | |
79787eaa | 1133 | goto out; |
771ed689 | 1134 | } |
c8b97818 | 1135 | |
771ed689 CM |
1136 | /* |
1137 | * work queue call back to started compression on a file and pages | |
1138 | */ | |
1139 | static noinline void async_cow_start(struct btrfs_work *work) | |
1140 | { | |
1141 | struct async_cow *async_cow; | |
1142 | int num_added = 0; | |
1143 | async_cow = container_of(work, struct async_cow, work); | |
1144 | ||
1145 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1146 | async_cow->start, async_cow->end, async_cow, | |
1147 | &num_added); | |
8180ef88 | 1148 | if (num_added == 0) { |
cb77fcd8 | 1149 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1150 | async_cow->inode = NULL; |
8180ef88 | 1151 | } |
771ed689 CM |
1152 | } |
1153 | ||
1154 | /* | |
1155 | * work queue call back to submit previously compressed pages | |
1156 | */ | |
1157 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1158 | { | |
0b246afa | 1159 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
1160 | struct async_cow *async_cow; |
1161 | struct btrfs_root *root; | |
1162 | unsigned long nr_pages; | |
1163 | ||
1164 | async_cow = container_of(work, struct async_cow, work); | |
1165 | ||
1166 | root = async_cow->root; | |
0b246afa | 1167 | fs_info = root->fs_info; |
09cbfeaf KS |
1168 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1169 | PAGE_SHIFT; | |
771ed689 | 1170 | |
ee863954 DS |
1171 | /* |
1172 | * atomic_sub_return implies a barrier for waitqueue_active | |
1173 | */ | |
0b246afa | 1174 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
ee22184b | 1175 | 5 * SZ_1M && |
0b246afa JM |
1176 | waitqueue_active(&fs_info->async_submit_wait)) |
1177 | wake_up(&fs_info->async_submit_wait); | |
771ed689 | 1178 | |
d397712b | 1179 | if (async_cow->inode) |
771ed689 | 1180 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1181 | } |
c8b97818 | 1182 | |
771ed689 CM |
1183 | static noinline void async_cow_free(struct btrfs_work *work) |
1184 | { | |
1185 | struct async_cow *async_cow; | |
1186 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1187 | if (async_cow->inode) |
cb77fcd8 | 1188 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1189 | kfree(async_cow); |
1190 | } | |
1191 | ||
1192 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1193 | u64 start, u64 end, int *page_started, | |
1194 | unsigned long *nr_written) | |
1195 | { | |
0b246afa | 1196 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
1197 | struct async_cow *async_cow; |
1198 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1199 | unsigned long nr_pages; | |
1200 | u64 cur_end; | |
771ed689 | 1201 | |
a3429ab7 CM |
1202 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1203 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1204 | while (start < end) { |
771ed689 | 1205 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1206 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1207 | async_cow->inode = igrab(inode); |
771ed689 CM |
1208 | async_cow->root = root; |
1209 | async_cow->locked_page = locked_page; | |
1210 | async_cow->start = start; | |
1211 | ||
f79707b0 | 1212 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1213 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1214 | cur_end = end; |
1215 | else | |
ee22184b | 1216 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1217 | |
1218 | async_cow->end = cur_end; | |
1219 | INIT_LIST_HEAD(&async_cow->extents); | |
1220 | ||
9e0af237 LB |
1221 | btrfs_init_work(&async_cow->work, |
1222 | btrfs_delalloc_helper, | |
1223 | async_cow_start, async_cow_submit, | |
1224 | async_cow_free); | |
771ed689 | 1225 | |
09cbfeaf KS |
1226 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1227 | PAGE_SHIFT; | |
0b246afa | 1228 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1229 | |
0b246afa | 1230 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1231 | |
771ed689 CM |
1232 | *nr_written += nr_pages; |
1233 | start = cur_end + 1; | |
1234 | } | |
1235 | *page_started = 1; | |
1236 | return 0; | |
be20aa9d CM |
1237 | } |
1238 | ||
2ff7e61e | 1239 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1240 | u64 bytenr, u64 num_bytes) |
1241 | { | |
1242 | int ret; | |
1243 | struct btrfs_ordered_sum *sums; | |
1244 | LIST_HEAD(list); | |
1245 | ||
0b246afa | 1246 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1247 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1248 | if (ret == 0 && list_empty(&list)) |
1249 | return 0; | |
1250 | ||
1251 | while (!list_empty(&list)) { | |
1252 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1253 | list_del(&sums->list); | |
1254 | kfree(sums); | |
1255 | } | |
1256 | return 1; | |
1257 | } | |
1258 | ||
d352ac68 CM |
1259 | /* |
1260 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1261 | * of the extents that exist in the file, and COWs the file as required. | |
1262 | * | |
1263 | * If no cow copies or snapshots exist, we write directly to the existing | |
1264 | * blocks on disk | |
1265 | */ | |
7f366cfe CM |
1266 | static noinline int run_delalloc_nocow(struct inode *inode, |
1267 | struct page *locked_page, | |
771ed689 CM |
1268 | u64 start, u64 end, int *page_started, int force, |
1269 | unsigned long *nr_written) | |
be20aa9d | 1270 | { |
0b246afa | 1271 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d CM |
1272 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1273 | struct extent_buffer *leaf; | |
be20aa9d | 1274 | struct btrfs_path *path; |
80ff3856 | 1275 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1276 | struct btrfs_key found_key; |
6f9994db | 1277 | struct extent_map *em; |
80ff3856 YZ |
1278 | u64 cow_start; |
1279 | u64 cur_offset; | |
1280 | u64 extent_end; | |
5d4f98a2 | 1281 | u64 extent_offset; |
80ff3856 YZ |
1282 | u64 disk_bytenr; |
1283 | u64 num_bytes; | |
b4939680 | 1284 | u64 disk_num_bytes; |
cc95bef6 | 1285 | u64 ram_bytes; |
80ff3856 | 1286 | int extent_type; |
79787eaa | 1287 | int ret, err; |
d899e052 | 1288 | int type; |
80ff3856 YZ |
1289 | int nocow; |
1290 | int check_prev = 1; | |
82d5902d | 1291 | bool nolock; |
4a0cc7ca | 1292 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1293 | |
1294 | path = btrfs_alloc_path(); | |
17ca04af | 1295 | if (!path) { |
ba8b04c1 QW |
1296 | extent_clear_unlock_delalloc(inode, start, end, end, |
1297 | locked_page, | |
c2790a2e | 1298 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1299 | EXTENT_DO_ACCOUNTING | |
1300 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1301 | PAGE_CLEAR_DIRTY | |
1302 | PAGE_SET_WRITEBACK | | |
1303 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1304 | return -ENOMEM; |
17ca04af | 1305 | } |
82d5902d | 1306 | |
70ddc553 | 1307 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
82d5902d | 1308 | |
80ff3856 YZ |
1309 | cow_start = (u64)-1; |
1310 | cur_offset = start; | |
1311 | while (1) { | |
e4c3b2dc | 1312 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1313 | cur_offset, 0); |
d788a349 | 1314 | if (ret < 0) |
79787eaa | 1315 | goto error; |
80ff3856 YZ |
1316 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1317 | leaf = path->nodes[0]; | |
1318 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1319 | path->slots[0] - 1); | |
33345d01 | 1320 | if (found_key.objectid == ino && |
80ff3856 YZ |
1321 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1322 | path->slots[0]--; | |
1323 | } | |
1324 | check_prev = 0; | |
1325 | next_slot: | |
1326 | leaf = path->nodes[0]; | |
1327 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1328 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1329 | if (ret < 0) |
79787eaa | 1330 | goto error; |
80ff3856 YZ |
1331 | if (ret > 0) |
1332 | break; | |
1333 | leaf = path->nodes[0]; | |
1334 | } | |
be20aa9d | 1335 | |
80ff3856 YZ |
1336 | nocow = 0; |
1337 | disk_bytenr = 0; | |
17d217fe | 1338 | num_bytes = 0; |
80ff3856 YZ |
1339 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1340 | ||
1d512cb7 FM |
1341 | if (found_key.objectid > ino) |
1342 | break; | |
1343 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1344 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1345 | path->slots[0]++; | |
1346 | goto next_slot; | |
1347 | } | |
1348 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1349 | found_key.offset > end) |
1350 | break; | |
1351 | ||
1352 | if (found_key.offset > cur_offset) { | |
1353 | extent_end = found_key.offset; | |
e9061e21 | 1354 | extent_type = 0; |
80ff3856 YZ |
1355 | goto out_check; |
1356 | } | |
1357 | ||
1358 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1359 | struct btrfs_file_extent_item); | |
1360 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1361 | ||
cc95bef6 | 1362 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1363 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1364 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1365 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1366 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1367 | extent_end = found_key.offset + |
1368 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1369 | disk_num_bytes = |
1370 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1371 | if (extent_end <= start) { |
1372 | path->slots[0]++; | |
1373 | goto next_slot; | |
1374 | } | |
17d217fe YZ |
1375 | if (disk_bytenr == 0) |
1376 | goto out_check; | |
80ff3856 YZ |
1377 | if (btrfs_file_extent_compression(leaf, fi) || |
1378 | btrfs_file_extent_encryption(leaf, fi) || | |
1379 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1380 | goto out_check; | |
d899e052 YZ |
1381 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1382 | goto out_check; | |
2ff7e61e | 1383 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1384 | goto out_check; |
e4c3b2dc | 1385 | if (btrfs_cross_ref_exist(root, ino, |
5d4f98a2 YZ |
1386 | found_key.offset - |
1387 | extent_offset, disk_bytenr)) | |
17d217fe | 1388 | goto out_check; |
5d4f98a2 | 1389 | disk_bytenr += extent_offset; |
17d217fe YZ |
1390 | disk_bytenr += cur_offset - found_key.offset; |
1391 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1392 | /* |
1393 | * if there are pending snapshots for this root, | |
1394 | * we fall into common COW way. | |
1395 | */ | |
1396 | if (!nolock) { | |
ea14b57f | 1397 | err = btrfs_start_write_no_snapshotting(root); |
e9894fd3 WS |
1398 | if (!err) |
1399 | goto out_check; | |
1400 | } | |
17d217fe YZ |
1401 | /* |
1402 | * force cow if csum exists in the range. | |
1403 | * this ensure that csum for a given extent are | |
1404 | * either valid or do not exist. | |
1405 | */ | |
2ff7e61e | 1406 | if (csum_exist_in_range(fs_info, disk_bytenr, |
91e1f56a RK |
1407 | num_bytes)) { |
1408 | if (!nolock) | |
ea14b57f | 1409 | btrfs_end_write_no_snapshotting(root); |
17d217fe | 1410 | goto out_check; |
91e1f56a RK |
1411 | } |
1412 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { | |
1413 | if (!nolock) | |
ea14b57f | 1414 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1415 | goto out_check; |
91e1f56a | 1416 | } |
80ff3856 YZ |
1417 | nocow = 1; |
1418 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1419 | extent_end = found_key.offset + | |
514ac8ad CM |
1420 | btrfs_file_extent_inline_len(leaf, |
1421 | path->slots[0], fi); | |
da17066c | 1422 | extent_end = ALIGN(extent_end, |
0b246afa | 1423 | fs_info->sectorsize); |
80ff3856 YZ |
1424 | } else { |
1425 | BUG_ON(1); | |
1426 | } | |
1427 | out_check: | |
1428 | if (extent_end <= start) { | |
1429 | path->slots[0]++; | |
e9894fd3 | 1430 | if (!nolock && nocow) |
ea14b57f | 1431 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1432 | if (nocow) |
0b246afa | 1433 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1434 | goto next_slot; |
1435 | } | |
1436 | if (!nocow) { | |
1437 | if (cow_start == (u64)-1) | |
1438 | cow_start = cur_offset; | |
1439 | cur_offset = extent_end; | |
1440 | if (cur_offset > end) | |
1441 | break; | |
1442 | path->slots[0]++; | |
1443 | goto next_slot; | |
7ea394f1 YZ |
1444 | } |
1445 | ||
b3b4aa74 | 1446 | btrfs_release_path(path); |
80ff3856 | 1447 | if (cow_start != (u64)-1) { |
00361589 JB |
1448 | ret = cow_file_range(inode, locked_page, |
1449 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1450 | end, page_started, nr_written, 1, |
1451 | NULL); | |
e9894fd3 WS |
1452 | if (ret) { |
1453 | if (!nolock && nocow) | |
ea14b57f | 1454 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1455 | if (nocow) |
0b246afa | 1456 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1457 | disk_bytenr); |
79787eaa | 1458 | goto error; |
e9894fd3 | 1459 | } |
80ff3856 | 1460 | cow_start = (u64)-1; |
7ea394f1 | 1461 | } |
80ff3856 | 1462 | |
d899e052 | 1463 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db LB |
1464 | u64 orig_start = found_key.offset - extent_offset; |
1465 | ||
1466 | em = create_io_em(inode, cur_offset, num_bytes, | |
1467 | orig_start, | |
1468 | disk_bytenr, /* block_start */ | |
1469 | num_bytes, /* block_len */ | |
1470 | disk_num_bytes, /* orig_block_len */ | |
1471 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1472 | BTRFS_ORDERED_PREALLOC); | |
1473 | if (IS_ERR(em)) { | |
1474 | if (!nolock && nocow) | |
ea14b57f | 1475 | btrfs_end_write_no_snapshotting(root); |
6f9994db LB |
1476 | if (nocow) |
1477 | btrfs_dec_nocow_writers(fs_info, | |
1478 | disk_bytenr); | |
1479 | ret = PTR_ERR(em); | |
1480 | goto error; | |
d899e052 | 1481 | } |
6f9994db LB |
1482 | free_extent_map(em); |
1483 | } | |
1484 | ||
1485 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d899e052 YZ |
1486 | type = BTRFS_ORDERED_PREALLOC; |
1487 | } else { | |
1488 | type = BTRFS_ORDERED_NOCOW; | |
1489 | } | |
80ff3856 YZ |
1490 | |
1491 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1492 | num_bytes, num_bytes, type); |
f78c436c | 1493 | if (nocow) |
0b246afa | 1494 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1495 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1496 | |
efa56464 | 1497 | if (root->root_key.objectid == |
4dbd80fb QW |
1498 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1499 | /* | |
1500 | * Error handled later, as we must prevent | |
1501 | * extent_clear_unlock_delalloc() in error handler | |
1502 | * from freeing metadata of created ordered extent. | |
1503 | */ | |
efa56464 YZ |
1504 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1505 | num_bytes); | |
efa56464 | 1506 | |
c2790a2e | 1507 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1508 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1509 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1510 | EXTENT_DELALLOC | |
1511 | EXTENT_CLEAR_DATA_RESV, | |
1512 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1513 | ||
e9894fd3 | 1514 | if (!nolock && nocow) |
ea14b57f | 1515 | btrfs_end_write_no_snapshotting(root); |
80ff3856 | 1516 | cur_offset = extent_end; |
4dbd80fb QW |
1517 | |
1518 | /* | |
1519 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1520 | * handler, as metadata for created ordered extent will only | |
1521 | * be freed by btrfs_finish_ordered_io(). | |
1522 | */ | |
1523 | if (ret) | |
1524 | goto error; | |
80ff3856 YZ |
1525 | if (cur_offset > end) |
1526 | break; | |
be20aa9d | 1527 | } |
b3b4aa74 | 1528 | btrfs_release_path(path); |
80ff3856 | 1529 | |
17ca04af | 1530 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1531 | cow_start = cur_offset; |
17ca04af JB |
1532 | cur_offset = end; |
1533 | } | |
1534 | ||
80ff3856 | 1535 | if (cow_start != (u64)-1) { |
dda3245e WX |
1536 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1537 | page_started, nr_written, 1, NULL); | |
d788a349 | 1538 | if (ret) |
79787eaa | 1539 | goto error; |
80ff3856 YZ |
1540 | } |
1541 | ||
79787eaa | 1542 | error: |
17ca04af | 1543 | if (ret && cur_offset < end) |
ba8b04c1 | 1544 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1545 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1546 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1547 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1548 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1549 | PAGE_SET_WRITEBACK | |
1550 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1551 | btrfs_free_path(path); |
79787eaa | 1552 | return ret; |
be20aa9d CM |
1553 | } |
1554 | ||
47059d93 WS |
1555 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1556 | { | |
1557 | ||
1558 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1559 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1560 | return 0; | |
1561 | ||
1562 | /* | |
1563 | * @defrag_bytes is a hint value, no spinlock held here, | |
1564 | * if is not zero, it means the file is defragging. | |
1565 | * Force cow if given extent needs to be defragged. | |
1566 | */ | |
1567 | if (BTRFS_I(inode)->defrag_bytes && | |
1568 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1569 | EXTENT_DEFRAG, 0, NULL)) | |
1570 | return 1; | |
1571 | ||
1572 | return 0; | |
1573 | } | |
1574 | ||
d352ac68 CM |
1575 | /* |
1576 | * extent_io.c call back to do delayed allocation processing | |
1577 | */ | |
c6100a4b | 1578 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
771ed689 CM |
1579 | u64 start, u64 end, int *page_started, |
1580 | unsigned long *nr_written) | |
be20aa9d | 1581 | { |
c6100a4b | 1582 | struct inode *inode = private_data; |
be20aa9d | 1583 | int ret; |
47059d93 | 1584 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1585 | |
47059d93 | 1586 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1587 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1588 | page_started, 1, nr_written); |
47059d93 | 1589 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1590 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1591 | page_started, 0, nr_written); |
c2fcdcdf | 1592 | } else if (!inode_need_compress(inode, start, end)) { |
dda3245e WX |
1593 | ret = cow_file_range(inode, locked_page, start, end, end, |
1594 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1595 | } else { |
1596 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1597 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1598 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1599 | page_started, nr_written); |
7ddf5a42 | 1600 | } |
52427260 QW |
1601 | if (ret) |
1602 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); | |
b888db2b CM |
1603 | return ret; |
1604 | } | |
1605 | ||
c6100a4b | 1606 | static void btrfs_split_extent_hook(void *private_data, |
1bf85046 | 1607 | struct extent_state *orig, u64 split) |
9ed74f2d | 1608 | { |
c6100a4b | 1609 | struct inode *inode = private_data; |
dcab6a3b JB |
1610 | u64 size; |
1611 | ||
0ca1f7ce | 1612 | /* not delalloc, ignore it */ |
9ed74f2d | 1613 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1614 | return; |
9ed74f2d | 1615 | |
dcab6a3b JB |
1616 | size = orig->end - orig->start + 1; |
1617 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1618 | u32 num_extents; |
dcab6a3b JB |
1619 | u64 new_size; |
1620 | ||
1621 | /* | |
ba117213 JB |
1622 | * See the explanation in btrfs_merge_extent_hook, the same |
1623 | * applies here, just in reverse. | |
dcab6a3b JB |
1624 | */ |
1625 | new_size = orig->end - split + 1; | |
823bb20a | 1626 | num_extents = count_max_extents(new_size); |
ba117213 | 1627 | new_size = split - orig->start; |
823bb20a DS |
1628 | num_extents += count_max_extents(new_size); |
1629 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1630 | return; |
1631 | } | |
1632 | ||
9e0baf60 | 1633 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1634 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1635 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1636 | } |
1637 | ||
1638 | /* | |
1639 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1640 | * extents so we can keep track of new extents that are just merged onto old | |
1641 | * extents, such as when we are doing sequential writes, so we can properly | |
1642 | * account for the metadata space we'll need. | |
1643 | */ | |
c6100a4b | 1644 | static void btrfs_merge_extent_hook(void *private_data, |
1bf85046 JM |
1645 | struct extent_state *new, |
1646 | struct extent_state *other) | |
9ed74f2d | 1647 | { |
c6100a4b | 1648 | struct inode *inode = private_data; |
dcab6a3b | 1649 | u64 new_size, old_size; |
823bb20a | 1650 | u32 num_extents; |
dcab6a3b | 1651 | |
9ed74f2d JB |
1652 | /* not delalloc, ignore it */ |
1653 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1654 | return; |
9ed74f2d | 1655 | |
8461a3de JB |
1656 | if (new->start > other->start) |
1657 | new_size = new->end - other->start + 1; | |
1658 | else | |
1659 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1660 | |
1661 | /* we're not bigger than the max, unreserve the space and go */ | |
1662 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1663 | spin_lock(&BTRFS_I(inode)->lock); | |
8b62f87b | 1664 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
dcab6a3b JB |
1665 | spin_unlock(&BTRFS_I(inode)->lock); |
1666 | return; | |
1667 | } | |
1668 | ||
1669 | /* | |
ba117213 JB |
1670 | * We have to add up either side to figure out how many extents were |
1671 | * accounted for before we merged into one big extent. If the number of | |
1672 | * extents we accounted for is <= the amount we need for the new range | |
1673 | * then we can return, otherwise drop. Think of it like this | |
1674 | * | |
1675 | * [ 4k][MAX_SIZE] | |
1676 | * | |
1677 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1678 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1679 | * we have 1 so they are == and we can return. But in this case | |
1680 | * | |
1681 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1682 | * | |
1683 | * Each range on their own accounts for 2 extents, but merged together | |
1684 | * they are only 3 extents worth of accounting, so we need to drop in | |
1685 | * this case. | |
dcab6a3b | 1686 | */ |
ba117213 | 1687 | old_size = other->end - other->start + 1; |
823bb20a | 1688 | num_extents = count_max_extents(old_size); |
ba117213 | 1689 | old_size = new->end - new->start + 1; |
823bb20a DS |
1690 | num_extents += count_max_extents(old_size); |
1691 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1692 | return; |
1693 | ||
9e0baf60 | 1694 | spin_lock(&BTRFS_I(inode)->lock); |
8b62f87b | 1695 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
9e0baf60 | 1696 | spin_unlock(&BTRFS_I(inode)->lock); |
9ed74f2d JB |
1697 | } |
1698 | ||
eb73c1b7 MX |
1699 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1700 | struct inode *inode) | |
1701 | { | |
0b246afa JM |
1702 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1703 | ||
eb73c1b7 MX |
1704 | spin_lock(&root->delalloc_lock); |
1705 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1706 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1707 | &root->delalloc_inodes); | |
1708 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1709 | &BTRFS_I(inode)->runtime_flags); | |
1710 | root->nr_delalloc_inodes++; | |
1711 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1712 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1713 | BUG_ON(!list_empty(&root->delalloc_root)); |
1714 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1715 | &fs_info->delalloc_roots); |
1716 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1717 | } |
1718 | } | |
1719 | spin_unlock(&root->delalloc_lock); | |
1720 | } | |
1721 | ||
1722 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
9e3e97f4 | 1723 | struct btrfs_inode *inode) |
eb73c1b7 | 1724 | { |
9e3e97f4 | 1725 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
0b246afa | 1726 | |
eb73c1b7 | 1727 | spin_lock(&root->delalloc_lock); |
9e3e97f4 NB |
1728 | if (!list_empty(&inode->delalloc_inodes)) { |
1729 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1730 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1731 | &inode->runtime_flags); |
eb73c1b7 MX |
1732 | root->nr_delalloc_inodes--; |
1733 | if (!root->nr_delalloc_inodes) { | |
0b246afa | 1734 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1735 | BUG_ON(list_empty(&root->delalloc_root)); |
1736 | list_del_init(&root->delalloc_root); | |
0b246afa | 1737 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1738 | } |
1739 | } | |
1740 | spin_unlock(&root->delalloc_lock); | |
1741 | } | |
1742 | ||
d352ac68 CM |
1743 | /* |
1744 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1745 | * bytes in this file, and to maintain the list of inodes that | |
1746 | * have pending delalloc work to be done. | |
1747 | */ | |
c6100a4b | 1748 | static void btrfs_set_bit_hook(void *private_data, |
9ee49a04 | 1749 | struct extent_state *state, unsigned *bits) |
291d673e | 1750 | { |
c6100a4b | 1751 | struct inode *inode = private_data; |
9ed74f2d | 1752 | |
0b246afa JM |
1753 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1754 | ||
47059d93 WS |
1755 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1756 | WARN_ON(1); | |
75eff68e CM |
1757 | /* |
1758 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1759 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1760 | * bit, which is only set or cleared with irqs on |
1761 | */ | |
0ca1f7ce | 1762 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1763 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1764 | u64 len = state->end + 1 - state->start; |
8b62f87b | 1765 | u32 num_extents = count_max_extents(len); |
70ddc553 | 1766 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1767 | |
8b62f87b JB |
1768 | spin_lock(&BTRFS_I(inode)->lock); |
1769 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); | |
1770 | spin_unlock(&BTRFS_I(inode)->lock); | |
287a0ab9 | 1771 | |
6a3891c5 | 1772 | /* For sanity tests */ |
0b246afa | 1773 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1774 | return; |
1775 | ||
104b4e51 NB |
1776 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
1777 | fs_info->delalloc_batch); | |
df0af1a5 | 1778 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1779 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1780 | if (*bits & EXTENT_DEFRAG) |
1781 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1782 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1783 | &BTRFS_I(inode)->runtime_flags)) |
1784 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1785 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1786 | } |
a7e3b975 FM |
1787 | |
1788 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1789 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1790 | spin_lock(&BTRFS_I(inode)->lock); | |
1791 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1792 | state->start; | |
1793 | spin_unlock(&BTRFS_I(inode)->lock); | |
1794 | } | |
291d673e CM |
1795 | } |
1796 | ||
d352ac68 CM |
1797 | /* |
1798 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1799 | */ | |
c6100a4b | 1800 | static void btrfs_clear_bit_hook(void *private_data, |
41074888 | 1801 | struct extent_state *state, |
9ee49a04 | 1802 | unsigned *bits) |
291d673e | 1803 | { |
c6100a4b | 1804 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
6fc0ef68 | 1805 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
47059d93 | 1806 | u64 len = state->end + 1 - state->start; |
823bb20a | 1807 | u32 num_extents = count_max_extents(len); |
47059d93 | 1808 | |
4a4b964f FM |
1809 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
1810 | spin_lock(&inode->lock); | |
6fc0ef68 | 1811 | inode->defrag_bytes -= len; |
4a4b964f FM |
1812 | spin_unlock(&inode->lock); |
1813 | } | |
47059d93 | 1814 | |
75eff68e CM |
1815 | /* |
1816 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1817 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1818 | * bit, which is only set or cleared with irqs on |
1819 | */ | |
0ca1f7ce | 1820 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1821 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1822 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1823 | |
8b62f87b JB |
1824 | spin_lock(&inode->lock); |
1825 | btrfs_mod_outstanding_extents(inode, -num_extents); | |
1826 | spin_unlock(&inode->lock); | |
0ca1f7ce | 1827 | |
b6d08f06 JB |
1828 | /* |
1829 | * We don't reserve metadata space for space cache inodes so we | |
1830 | * don't need to call dellalloc_release_metadata if there is an | |
1831 | * error. | |
1832 | */ | |
a315e68f | 1833 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1834 | root != fs_info->tree_root) |
0ca1f7ce YZ |
1835 | btrfs_delalloc_release_metadata(inode, len); |
1836 | ||
6a3891c5 | 1837 | /* For sanity tests. */ |
0b246afa | 1838 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1839 | return; |
1840 | ||
a315e68f FM |
1841 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
1842 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1843 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
1844 | btrfs_free_reserved_data_space_noquota( |
1845 | &inode->vfs_inode, | |
51773bec | 1846 | state->start, len); |
9ed74f2d | 1847 | |
104b4e51 NB |
1848 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
1849 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
1850 | spin_lock(&inode->lock); |
1851 | inode->delalloc_bytes -= len; | |
1852 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 1853 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1854 | &inode->runtime_flags)) |
eb73c1b7 | 1855 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 1856 | spin_unlock(&inode->lock); |
291d673e | 1857 | } |
a7e3b975 FM |
1858 | |
1859 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1860 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1861 | spin_lock(&inode->lock); | |
1862 | ASSERT(inode->new_delalloc_bytes >= len); | |
1863 | inode->new_delalloc_bytes -= len; | |
1864 | spin_unlock(&inode->lock); | |
1865 | } | |
291d673e CM |
1866 | } |
1867 | ||
d352ac68 CM |
1868 | /* |
1869 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1870 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1871 | * |
1872 | * return 1 if page cannot be merged to bio | |
1873 | * return 0 if page can be merged to bio | |
1874 | * return error otherwise | |
d352ac68 | 1875 | */ |
81a75f67 | 1876 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1877 | size_t size, struct bio *bio, |
1878 | unsigned long bio_flags) | |
239b14b3 | 1879 | { |
0b246afa JM |
1880 | struct inode *inode = page->mapping->host; |
1881 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1882 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1883 | u64 length = 0; |
1884 | u64 map_length; | |
239b14b3 CM |
1885 | int ret; |
1886 | ||
771ed689 CM |
1887 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1888 | return 0; | |
1889 | ||
4f024f37 | 1890 | length = bio->bi_iter.bi_size; |
239b14b3 | 1891 | map_length = length; |
0b246afa JM |
1892 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1893 | NULL, 0); | |
6f034ece LB |
1894 | if (ret < 0) |
1895 | return ret; | |
d397712b | 1896 | if (map_length < length + size) |
239b14b3 | 1897 | return 1; |
3444a972 | 1898 | return 0; |
239b14b3 CM |
1899 | } |
1900 | ||
d352ac68 CM |
1901 | /* |
1902 | * in order to insert checksums into the metadata in large chunks, | |
1903 | * we wait until bio submission time. All the pages in the bio are | |
1904 | * checksummed and sums are attached onto the ordered extent record. | |
1905 | * | |
1906 | * At IO completion time the cums attached on the ordered extent record | |
1907 | * are inserted into the btree | |
1908 | */ | |
8c27cb35 | 1909 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
81a75f67 | 1910 | int mirror_num, unsigned long bio_flags, |
eaf25d93 | 1911 | u64 bio_offset) |
065631f6 | 1912 | { |
c6100a4b | 1913 | struct inode *inode = private_data; |
4e4cbee9 | 1914 | blk_status_t ret = 0; |
e015640f | 1915 | |
2ff7e61e | 1916 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1917 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1918 | return 0; |
1919 | } | |
e015640f | 1920 | |
4a69a410 CM |
1921 | /* |
1922 | * in order to insert checksums into the metadata in large chunks, | |
1923 | * we wait until bio submission time. All the pages in the bio are | |
1924 | * checksummed and sums are attached onto the ordered extent record. | |
1925 | * | |
1926 | * At IO completion time the cums attached on the ordered extent record | |
1927 | * are inserted into the btree | |
1928 | */ | |
8c27cb35 | 1929 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
eaf25d93 CM |
1930 | int mirror_num, unsigned long bio_flags, |
1931 | u64 bio_offset) | |
4a69a410 | 1932 | { |
c6100a4b | 1933 | struct inode *inode = private_data; |
2ff7e61e | 1934 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4e4cbee9 | 1935 | blk_status_t ret; |
61891923 | 1936 | |
2ff7e61e | 1937 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
4246a0b6 | 1938 | if (ret) { |
4e4cbee9 | 1939 | bio->bi_status = ret; |
4246a0b6 CH |
1940 | bio_endio(bio); |
1941 | } | |
61891923 | 1942 | return ret; |
44b8bd7e CM |
1943 | } |
1944 | ||
d352ac68 | 1945 | /* |
cad321ad CM |
1946 | * extent_io.c submission hook. This does the right thing for csum calculation |
1947 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1948 | */ |
8c27cb35 | 1949 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
c6100a4b JB |
1950 | int mirror_num, unsigned long bio_flags, |
1951 | u64 bio_offset) | |
44b8bd7e | 1952 | { |
c6100a4b | 1953 | struct inode *inode = private_data; |
0b246afa | 1954 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1955 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1956 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 1957 | blk_status_t ret = 0; |
19b9bdb0 | 1958 | int skip_sum; |
b812ce28 | 1959 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1960 | |
6cbff00f | 1961 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1962 | |
70ddc553 | 1963 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 1964 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1965 | |
37226b21 | 1966 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1967 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1968 | if (ret) |
61891923 | 1969 | goto out; |
5fd02043 | 1970 | |
d20f7043 | 1971 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1972 | ret = btrfs_submit_compressed_read(inode, bio, |
1973 | mirror_num, | |
1974 | bio_flags); | |
1975 | goto out; | |
c2db1073 | 1976 | } else if (!skip_sum) { |
2ff7e61e | 1977 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1978 | if (ret) |
61891923 | 1979 | goto out; |
c2db1073 | 1980 | } |
4d1b5fb4 | 1981 | goto mapit; |
b812ce28 | 1982 | } else if (async && !skip_sum) { |
17d217fe YZ |
1983 | /* csum items have already been cloned */ |
1984 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1985 | goto mapit; | |
19b9bdb0 | 1986 | /* we're doing a write, do the async checksumming */ |
c6100a4b JB |
1987 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
1988 | bio_offset, inode, | |
0b246afa JM |
1989 | __btrfs_submit_bio_start, |
1990 | __btrfs_submit_bio_done); | |
61891923 | 1991 | goto out; |
b812ce28 | 1992 | } else if (!skip_sum) { |
2ff7e61e | 1993 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
1994 | if (ret) |
1995 | goto out; | |
19b9bdb0 CM |
1996 | } |
1997 | ||
0b86a832 | 1998 | mapit: |
2ff7e61e | 1999 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
2000 | |
2001 | out: | |
4e4cbee9 CH |
2002 | if (ret) { |
2003 | bio->bi_status = ret; | |
4246a0b6 CH |
2004 | bio_endio(bio); |
2005 | } | |
61891923 | 2006 | return ret; |
065631f6 | 2007 | } |
6885f308 | 2008 | |
d352ac68 CM |
2009 | /* |
2010 | * given a list of ordered sums record them in the inode. This happens | |
2011 | * at IO completion time based on sums calculated at bio submission time. | |
2012 | */ | |
ba1da2f4 | 2013 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2014 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2015 | { |
e6dcd2dc CM |
2016 | struct btrfs_ordered_sum *sum; |
2017 | ||
c6e30871 | 2018 | list_for_each_entry(sum, list, list) { |
39847c4d | 2019 | trans->adding_csums = 1; |
d20f7043 CM |
2020 | btrfs_csum_file_blocks(trans, |
2021 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 2022 | trans->adding_csums = 0; |
e6dcd2dc CM |
2023 | } |
2024 | return 0; | |
2025 | } | |
2026 | ||
2ac55d41 | 2027 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2028 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2029 | { |
09cbfeaf | 2030 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2031 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2032 | cached_state); |
ea8c2819 CM |
2033 | } |
2034 | ||
d352ac68 | 2035 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2036 | struct btrfs_writepage_fixup { |
2037 | struct page *page; | |
2038 | struct btrfs_work work; | |
2039 | }; | |
2040 | ||
b2950863 | 2041 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2042 | { |
2043 | struct btrfs_writepage_fixup *fixup; | |
2044 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2045 | struct extent_state *cached_state = NULL; |
364ecf36 | 2046 | struct extent_changeset *data_reserved = NULL; |
247e743c CM |
2047 | struct page *page; |
2048 | struct inode *inode; | |
2049 | u64 page_start; | |
2050 | u64 page_end; | |
87826df0 | 2051 | int ret; |
247e743c CM |
2052 | |
2053 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2054 | page = fixup->page; | |
4a096752 | 2055 | again: |
247e743c CM |
2056 | lock_page(page); |
2057 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2058 | ClearPageChecked(page); | |
2059 | goto out_page; | |
2060 | } | |
2061 | ||
2062 | inode = page->mapping->host; | |
2063 | page_start = page_offset(page); | |
09cbfeaf | 2064 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2065 | |
ff13db41 | 2066 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2067 | &cached_state); |
4a096752 CM |
2068 | |
2069 | /* already ordered? We're done */ | |
8b62b72b | 2070 | if (PagePrivate2(page)) |
247e743c | 2071 | goto out; |
4a096752 | 2072 | |
a776c6fa | 2073 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2074 | PAGE_SIZE); |
4a096752 | 2075 | if (ordered) { |
2ac55d41 JB |
2076 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2077 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2078 | unlock_page(page); |
2079 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2080 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2081 | goto again; |
2082 | } | |
247e743c | 2083 | |
364ecf36 | 2084 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
09cbfeaf | 2085 | PAGE_SIZE); |
87826df0 JM |
2086 | if (ret) { |
2087 | mapping_set_error(page->mapping, ret); | |
2088 | end_extent_writepage(page, ret, page_start, page_end); | |
2089 | ClearPageChecked(page); | |
2090 | goto out; | |
2091 | } | |
2092 | ||
ba8b04c1 QW |
2093 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
2094 | 0); | |
247e743c | 2095 | ClearPageChecked(page); |
87826df0 | 2096 | set_page_dirty(page); |
8b62f87b | 2097 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
247e743c | 2098 | out: |
2ac55d41 JB |
2099 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2100 | &cached_state, GFP_NOFS); | |
247e743c CM |
2101 | out_page: |
2102 | unlock_page(page); | |
09cbfeaf | 2103 | put_page(page); |
b897abec | 2104 | kfree(fixup); |
364ecf36 | 2105 | extent_changeset_free(data_reserved); |
247e743c CM |
2106 | } |
2107 | ||
2108 | /* | |
2109 | * There are a few paths in the higher layers of the kernel that directly | |
2110 | * set the page dirty bit without asking the filesystem if it is a | |
2111 | * good idea. This causes problems because we want to make sure COW | |
2112 | * properly happens and the data=ordered rules are followed. | |
2113 | * | |
c8b97818 | 2114 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2115 | * hasn't been properly setup for IO. We kick off an async process |
2116 | * to fix it up. The async helper will wait for ordered extents, set | |
2117 | * the delalloc bit and make it safe to write the page. | |
2118 | */ | |
b2950863 | 2119 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2120 | { |
2121 | struct inode *inode = page->mapping->host; | |
0b246afa | 2122 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2123 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2124 | |
8b62b72b CM |
2125 | /* this page is properly in the ordered list */ |
2126 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2127 | return 0; |
2128 | ||
2129 | if (PageChecked(page)) | |
2130 | return -EAGAIN; | |
2131 | ||
2132 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2133 | if (!fixup) | |
2134 | return -EAGAIN; | |
f421950f | 2135 | |
247e743c | 2136 | SetPageChecked(page); |
09cbfeaf | 2137 | get_page(page); |
9e0af237 LB |
2138 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2139 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2140 | fixup->page = page; |
0b246afa | 2141 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2142 | return -EBUSY; |
247e743c CM |
2143 | } |
2144 | ||
d899e052 YZ |
2145 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2146 | struct inode *inode, u64 file_pos, | |
2147 | u64 disk_bytenr, u64 disk_num_bytes, | |
2148 | u64 num_bytes, u64 ram_bytes, | |
2149 | u8 compression, u8 encryption, | |
2150 | u16 other_encoding, int extent_type) | |
2151 | { | |
2152 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2153 | struct btrfs_file_extent_item *fi; | |
2154 | struct btrfs_path *path; | |
2155 | struct extent_buffer *leaf; | |
2156 | struct btrfs_key ins; | |
a12b877b | 2157 | u64 qg_released; |
1acae57b | 2158 | int extent_inserted = 0; |
d899e052 YZ |
2159 | int ret; |
2160 | ||
2161 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2162 | if (!path) |
2163 | return -ENOMEM; | |
d899e052 | 2164 | |
a1ed835e CM |
2165 | /* |
2166 | * we may be replacing one extent in the tree with another. | |
2167 | * The new extent is pinned in the extent map, and we don't want | |
2168 | * to drop it from the cache until it is completely in the btree. | |
2169 | * | |
2170 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2171 | * the caller is expected to unpin it and allow it to be merged | |
2172 | * with the others. | |
2173 | */ | |
1acae57b FDBM |
2174 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2175 | file_pos + num_bytes, NULL, 0, | |
2176 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2177 | if (ret) |
2178 | goto out; | |
d899e052 | 2179 | |
1acae57b | 2180 | if (!extent_inserted) { |
4a0cc7ca | 2181 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2182 | ins.offset = file_pos; |
2183 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2184 | ||
2185 | path->leave_spinning = 1; | |
2186 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2187 | sizeof(*fi)); | |
2188 | if (ret) | |
2189 | goto out; | |
2190 | } | |
d899e052 YZ |
2191 | leaf = path->nodes[0]; |
2192 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2193 | struct btrfs_file_extent_item); | |
2194 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2195 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2196 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2197 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2198 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2199 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2200 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2201 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2202 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2203 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2204 | |
d899e052 | 2205 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2206 | btrfs_release_path(path); |
d899e052 YZ |
2207 | |
2208 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2209 | |
2210 | ins.objectid = disk_bytenr; | |
2211 | ins.offset = disk_num_bytes; | |
2212 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2213 | |
297d750b | 2214 | /* |
5846a3c2 QW |
2215 | * Release the reserved range from inode dirty range map, as it is |
2216 | * already moved into delayed_ref_head | |
297d750b | 2217 | */ |
a12b877b QW |
2218 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
2219 | if (ret < 0) | |
2220 | goto out; | |
2221 | qg_released = ret; | |
84f7d8e6 JB |
2222 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2223 | btrfs_ino(BTRFS_I(inode)), | |
2224 | file_pos, qg_released, &ins); | |
79787eaa | 2225 | out: |
d899e052 | 2226 | btrfs_free_path(path); |
b9473439 | 2227 | |
79787eaa | 2228 | return ret; |
d899e052 YZ |
2229 | } |
2230 | ||
38c227d8 LB |
2231 | /* snapshot-aware defrag */ |
2232 | struct sa_defrag_extent_backref { | |
2233 | struct rb_node node; | |
2234 | struct old_sa_defrag_extent *old; | |
2235 | u64 root_id; | |
2236 | u64 inum; | |
2237 | u64 file_pos; | |
2238 | u64 extent_offset; | |
2239 | u64 num_bytes; | |
2240 | u64 generation; | |
2241 | }; | |
2242 | ||
2243 | struct old_sa_defrag_extent { | |
2244 | struct list_head list; | |
2245 | struct new_sa_defrag_extent *new; | |
2246 | ||
2247 | u64 extent_offset; | |
2248 | u64 bytenr; | |
2249 | u64 offset; | |
2250 | u64 len; | |
2251 | int count; | |
2252 | }; | |
2253 | ||
2254 | struct new_sa_defrag_extent { | |
2255 | struct rb_root root; | |
2256 | struct list_head head; | |
2257 | struct btrfs_path *path; | |
2258 | struct inode *inode; | |
2259 | u64 file_pos; | |
2260 | u64 len; | |
2261 | u64 bytenr; | |
2262 | u64 disk_len; | |
2263 | u8 compress_type; | |
2264 | }; | |
2265 | ||
2266 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2267 | struct sa_defrag_extent_backref *b2) | |
2268 | { | |
2269 | if (b1->root_id < b2->root_id) | |
2270 | return -1; | |
2271 | else if (b1->root_id > b2->root_id) | |
2272 | return 1; | |
2273 | ||
2274 | if (b1->inum < b2->inum) | |
2275 | return -1; | |
2276 | else if (b1->inum > b2->inum) | |
2277 | return 1; | |
2278 | ||
2279 | if (b1->file_pos < b2->file_pos) | |
2280 | return -1; | |
2281 | else if (b1->file_pos > b2->file_pos) | |
2282 | return 1; | |
2283 | ||
2284 | /* | |
2285 | * [------------------------------] ===> (a range of space) | |
2286 | * |<--->| |<---->| =============> (fs/file tree A) | |
2287 | * |<---------------------------->| ===> (fs/file tree B) | |
2288 | * | |
2289 | * A range of space can refer to two file extents in one tree while | |
2290 | * refer to only one file extent in another tree. | |
2291 | * | |
2292 | * So we may process a disk offset more than one time(two extents in A) | |
2293 | * and locate at the same extent(one extent in B), then insert two same | |
2294 | * backrefs(both refer to the extent in B). | |
2295 | */ | |
2296 | return 0; | |
2297 | } | |
2298 | ||
2299 | static void backref_insert(struct rb_root *root, | |
2300 | struct sa_defrag_extent_backref *backref) | |
2301 | { | |
2302 | struct rb_node **p = &root->rb_node; | |
2303 | struct rb_node *parent = NULL; | |
2304 | struct sa_defrag_extent_backref *entry; | |
2305 | int ret; | |
2306 | ||
2307 | while (*p) { | |
2308 | parent = *p; | |
2309 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2310 | ||
2311 | ret = backref_comp(backref, entry); | |
2312 | if (ret < 0) | |
2313 | p = &(*p)->rb_left; | |
2314 | else | |
2315 | p = &(*p)->rb_right; | |
2316 | } | |
2317 | ||
2318 | rb_link_node(&backref->node, parent, p); | |
2319 | rb_insert_color(&backref->node, root); | |
2320 | } | |
2321 | ||
2322 | /* | |
2323 | * Note the backref might has changed, and in this case we just return 0. | |
2324 | */ | |
2325 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2326 | void *ctx) | |
2327 | { | |
2328 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2329 | struct old_sa_defrag_extent *old = ctx; |
2330 | struct new_sa_defrag_extent *new = old->new; | |
2331 | struct btrfs_path *path = new->path; | |
2332 | struct btrfs_key key; | |
2333 | struct btrfs_root *root; | |
2334 | struct sa_defrag_extent_backref *backref; | |
2335 | struct extent_buffer *leaf; | |
2336 | struct inode *inode = new->inode; | |
0b246afa | 2337 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2338 | int slot; |
2339 | int ret; | |
2340 | u64 extent_offset; | |
2341 | u64 num_bytes; | |
2342 | ||
2343 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2344 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2345 | return 0; |
2346 | ||
2347 | key.objectid = root_id; | |
2348 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2349 | key.offset = (u64)-1; | |
2350 | ||
38c227d8 LB |
2351 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2352 | if (IS_ERR(root)) { | |
2353 | if (PTR_ERR(root) == -ENOENT) | |
2354 | return 0; | |
2355 | WARN_ON(1); | |
ab8d0fc4 | 2356 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2357 | inum, offset, root_id); |
2358 | return PTR_ERR(root); | |
2359 | } | |
2360 | ||
2361 | key.objectid = inum; | |
2362 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2363 | if (offset > (u64)-1 << 32) | |
2364 | key.offset = 0; | |
2365 | else | |
2366 | key.offset = offset; | |
2367 | ||
2368 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2369 | if (WARN_ON(ret < 0)) |
38c227d8 | 2370 | return ret; |
50f1319c | 2371 | ret = 0; |
38c227d8 LB |
2372 | |
2373 | while (1) { | |
2374 | cond_resched(); | |
2375 | ||
2376 | leaf = path->nodes[0]; | |
2377 | slot = path->slots[0]; | |
2378 | ||
2379 | if (slot >= btrfs_header_nritems(leaf)) { | |
2380 | ret = btrfs_next_leaf(root, path); | |
2381 | if (ret < 0) { | |
2382 | goto out; | |
2383 | } else if (ret > 0) { | |
2384 | ret = 0; | |
2385 | goto out; | |
2386 | } | |
2387 | continue; | |
2388 | } | |
2389 | ||
2390 | path->slots[0]++; | |
2391 | ||
2392 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2393 | ||
2394 | if (key.objectid > inum) | |
2395 | goto out; | |
2396 | ||
2397 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2398 | continue; | |
2399 | ||
2400 | extent = btrfs_item_ptr(leaf, slot, | |
2401 | struct btrfs_file_extent_item); | |
2402 | ||
2403 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2404 | continue; | |
2405 | ||
e68afa49 LB |
2406 | /* |
2407 | * 'offset' refers to the exact key.offset, | |
2408 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2409 | * (key.offset - extent_offset). | |
2410 | */ | |
2411 | if (key.offset != offset) | |
38c227d8 LB |
2412 | continue; |
2413 | ||
e68afa49 | 2414 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2415 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2416 | |
38c227d8 LB |
2417 | if (extent_offset >= old->extent_offset + old->offset + |
2418 | old->len || extent_offset + num_bytes <= | |
2419 | old->extent_offset + old->offset) | |
2420 | continue; | |
38c227d8 LB |
2421 | break; |
2422 | } | |
2423 | ||
2424 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2425 | if (!backref) { | |
2426 | ret = -ENOENT; | |
2427 | goto out; | |
2428 | } | |
2429 | ||
2430 | backref->root_id = root_id; | |
2431 | backref->inum = inum; | |
e68afa49 | 2432 | backref->file_pos = offset; |
38c227d8 LB |
2433 | backref->num_bytes = num_bytes; |
2434 | backref->extent_offset = extent_offset; | |
2435 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2436 | backref->old = old; | |
2437 | backref_insert(&new->root, backref); | |
2438 | old->count++; | |
2439 | out: | |
2440 | btrfs_release_path(path); | |
2441 | WARN_ON(ret); | |
2442 | return ret; | |
2443 | } | |
2444 | ||
2445 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2446 | struct new_sa_defrag_extent *new) | |
2447 | { | |
0b246afa | 2448 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2449 | struct old_sa_defrag_extent *old, *tmp; |
2450 | int ret; | |
2451 | ||
2452 | new->path = path; | |
2453 | ||
2454 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2455 | ret = iterate_inodes_from_logical(old->bytenr + |
2456 | old->extent_offset, fs_info, | |
38c227d8 | 2457 | path, record_one_backref, |
c995ab3c | 2458 | old, false); |
4724b106 JB |
2459 | if (ret < 0 && ret != -ENOENT) |
2460 | return false; | |
38c227d8 LB |
2461 | |
2462 | /* no backref to be processed for this extent */ | |
2463 | if (!old->count) { | |
2464 | list_del(&old->list); | |
2465 | kfree(old); | |
2466 | } | |
2467 | } | |
2468 | ||
2469 | if (list_empty(&new->head)) | |
2470 | return false; | |
2471 | ||
2472 | return true; | |
2473 | } | |
2474 | ||
2475 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2476 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2477 | struct new_sa_defrag_extent *new) |
38c227d8 | 2478 | { |
116e0024 | 2479 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2480 | return 0; |
2481 | ||
2482 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2483 | return 0; | |
2484 | ||
116e0024 LB |
2485 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2486 | return 0; | |
2487 | ||
2488 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2489 | btrfs_file_extent_other_encoding(leaf, fi)) |
2490 | return 0; | |
2491 | ||
2492 | return 1; | |
2493 | } | |
2494 | ||
2495 | /* | |
2496 | * Note the backref might has changed, and in this case we just return 0. | |
2497 | */ | |
2498 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2499 | struct sa_defrag_extent_backref *prev, | |
2500 | struct sa_defrag_extent_backref *backref) | |
2501 | { | |
2502 | struct btrfs_file_extent_item *extent; | |
2503 | struct btrfs_file_extent_item *item; | |
2504 | struct btrfs_ordered_extent *ordered; | |
2505 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2506 | struct btrfs_root *root; |
2507 | struct btrfs_key key; | |
2508 | struct extent_buffer *leaf; | |
2509 | struct old_sa_defrag_extent *old = backref->old; | |
2510 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2511 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2512 | struct inode *inode; |
2513 | struct extent_state *cached = NULL; | |
2514 | int ret = 0; | |
2515 | u64 start; | |
2516 | u64 len; | |
2517 | u64 lock_start; | |
2518 | u64 lock_end; | |
2519 | bool merge = false; | |
2520 | int index; | |
2521 | ||
2522 | if (prev && prev->root_id == backref->root_id && | |
2523 | prev->inum == backref->inum && | |
2524 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2525 | merge = true; | |
2526 | ||
2527 | /* step 1: get root */ | |
2528 | key.objectid = backref->root_id; | |
2529 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2530 | key.offset = (u64)-1; | |
2531 | ||
38c227d8 LB |
2532 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2533 | ||
2534 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2535 | if (IS_ERR(root)) { | |
2536 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2537 | if (PTR_ERR(root) == -ENOENT) | |
2538 | return 0; | |
2539 | return PTR_ERR(root); | |
2540 | } | |
38c227d8 | 2541 | |
bcbba5e6 WS |
2542 | if (btrfs_root_readonly(root)) { |
2543 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2544 | return 0; | |
2545 | } | |
2546 | ||
38c227d8 LB |
2547 | /* step 2: get inode */ |
2548 | key.objectid = backref->inum; | |
2549 | key.type = BTRFS_INODE_ITEM_KEY; | |
2550 | key.offset = 0; | |
2551 | ||
2552 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2553 | if (IS_ERR(inode)) { | |
2554 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2555 | return 0; | |
2556 | } | |
2557 | ||
2558 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2559 | ||
2560 | /* step 3: relink backref */ | |
2561 | lock_start = backref->file_pos; | |
2562 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2563 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2564 | &cached); |
38c227d8 LB |
2565 | |
2566 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2567 | if (ordered) { | |
2568 | btrfs_put_ordered_extent(ordered); | |
2569 | goto out_unlock; | |
2570 | } | |
2571 | ||
2572 | trans = btrfs_join_transaction(root); | |
2573 | if (IS_ERR(trans)) { | |
2574 | ret = PTR_ERR(trans); | |
2575 | goto out_unlock; | |
2576 | } | |
2577 | ||
2578 | key.objectid = backref->inum; | |
2579 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2580 | key.offset = backref->file_pos; | |
2581 | ||
2582 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2583 | if (ret < 0) { | |
2584 | goto out_free_path; | |
2585 | } else if (ret > 0) { | |
2586 | ret = 0; | |
2587 | goto out_free_path; | |
2588 | } | |
2589 | ||
2590 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2591 | struct btrfs_file_extent_item); | |
2592 | ||
2593 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2594 | backref->generation) | |
2595 | goto out_free_path; | |
2596 | ||
2597 | btrfs_release_path(path); | |
2598 | ||
2599 | start = backref->file_pos; | |
2600 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2601 | start += old->extent_offset + old->offset - | |
2602 | backref->extent_offset; | |
2603 | ||
2604 | len = min(backref->extent_offset + backref->num_bytes, | |
2605 | old->extent_offset + old->offset + old->len); | |
2606 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2607 | ||
2608 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2609 | start + len, 1); | |
2610 | if (ret) | |
2611 | goto out_free_path; | |
2612 | again: | |
4a0cc7ca | 2613 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2614 | key.type = BTRFS_EXTENT_DATA_KEY; |
2615 | key.offset = start; | |
2616 | ||
a09a0a70 | 2617 | path->leave_spinning = 1; |
38c227d8 LB |
2618 | if (merge) { |
2619 | struct btrfs_file_extent_item *fi; | |
2620 | u64 extent_len; | |
2621 | struct btrfs_key found_key; | |
2622 | ||
3c9665df | 2623 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2624 | if (ret < 0) |
2625 | goto out_free_path; | |
2626 | ||
2627 | path->slots[0]--; | |
2628 | leaf = path->nodes[0]; | |
2629 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2630 | ||
2631 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2632 | struct btrfs_file_extent_item); | |
2633 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2634 | ||
116e0024 LB |
2635 | if (extent_len + found_key.offset == start && |
2636 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2637 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2638 | extent_len + len); | |
2639 | btrfs_mark_buffer_dirty(leaf); | |
2640 | inode_add_bytes(inode, len); | |
2641 | ||
2642 | ret = 1; | |
2643 | goto out_free_path; | |
2644 | } else { | |
2645 | merge = false; | |
2646 | btrfs_release_path(path); | |
2647 | goto again; | |
2648 | } | |
2649 | } | |
2650 | ||
2651 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2652 | sizeof(*extent)); | |
2653 | if (ret) { | |
66642832 | 2654 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2655 | goto out_free_path; |
2656 | } | |
2657 | ||
2658 | leaf = path->nodes[0]; | |
2659 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2660 | struct btrfs_file_extent_item); | |
2661 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2662 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2663 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2664 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2665 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2666 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2667 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2668 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2669 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2670 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2671 | ||
2672 | btrfs_mark_buffer_dirty(leaf); | |
2673 | inode_add_bytes(inode, len); | |
a09a0a70 | 2674 | btrfs_release_path(path); |
38c227d8 | 2675 | |
84f7d8e6 | 2676 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
38c227d8 LB |
2677 | new->disk_len, 0, |
2678 | backref->root_id, backref->inum, | |
b06c4bf5 | 2679 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2680 | if (ret) { |
66642832 | 2681 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2682 | goto out_free_path; |
2683 | } | |
2684 | ||
2685 | ret = 1; | |
2686 | out_free_path: | |
2687 | btrfs_release_path(path); | |
a09a0a70 | 2688 | path->leave_spinning = 0; |
3a45bb20 | 2689 | btrfs_end_transaction(trans); |
38c227d8 LB |
2690 | out_unlock: |
2691 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2692 | &cached, GFP_NOFS); | |
2693 | iput(inode); | |
2694 | return ret; | |
2695 | } | |
2696 | ||
6f519564 LB |
2697 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2698 | { | |
2699 | struct old_sa_defrag_extent *old, *tmp; | |
2700 | ||
2701 | if (!new) | |
2702 | return; | |
2703 | ||
2704 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2705 | kfree(old); |
2706 | } | |
2707 | kfree(new); | |
2708 | } | |
2709 | ||
38c227d8 LB |
2710 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2711 | { | |
0b246afa | 2712 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2713 | struct btrfs_path *path; |
38c227d8 LB |
2714 | struct sa_defrag_extent_backref *backref; |
2715 | struct sa_defrag_extent_backref *prev = NULL; | |
2716 | struct inode *inode; | |
2717 | struct btrfs_root *root; | |
2718 | struct rb_node *node; | |
2719 | int ret; | |
2720 | ||
2721 | inode = new->inode; | |
2722 | root = BTRFS_I(inode)->root; | |
2723 | ||
2724 | path = btrfs_alloc_path(); | |
2725 | if (!path) | |
2726 | return; | |
2727 | ||
2728 | if (!record_extent_backrefs(path, new)) { | |
2729 | btrfs_free_path(path); | |
2730 | goto out; | |
2731 | } | |
2732 | btrfs_release_path(path); | |
2733 | ||
2734 | while (1) { | |
2735 | node = rb_first(&new->root); | |
2736 | if (!node) | |
2737 | break; | |
2738 | rb_erase(node, &new->root); | |
2739 | ||
2740 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2741 | ||
2742 | ret = relink_extent_backref(path, prev, backref); | |
2743 | WARN_ON(ret < 0); | |
2744 | ||
2745 | kfree(prev); | |
2746 | ||
2747 | if (ret == 1) | |
2748 | prev = backref; | |
2749 | else | |
2750 | prev = NULL; | |
2751 | cond_resched(); | |
2752 | } | |
2753 | kfree(prev); | |
2754 | ||
2755 | btrfs_free_path(path); | |
38c227d8 | 2756 | out: |
6f519564 LB |
2757 | free_sa_defrag_extent(new); |
2758 | ||
0b246afa JM |
2759 | atomic_dec(&fs_info->defrag_running); |
2760 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2761 | } |
2762 | ||
2763 | static struct new_sa_defrag_extent * | |
2764 | record_old_file_extents(struct inode *inode, | |
2765 | struct btrfs_ordered_extent *ordered) | |
2766 | { | |
0b246afa | 2767 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2768 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2769 | struct btrfs_path *path; | |
2770 | struct btrfs_key key; | |
6f519564 | 2771 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2772 | struct new_sa_defrag_extent *new; |
2773 | int ret; | |
2774 | ||
2775 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2776 | if (!new) | |
2777 | return NULL; | |
2778 | ||
2779 | new->inode = inode; | |
2780 | new->file_pos = ordered->file_offset; | |
2781 | new->len = ordered->len; | |
2782 | new->bytenr = ordered->start; | |
2783 | new->disk_len = ordered->disk_len; | |
2784 | new->compress_type = ordered->compress_type; | |
2785 | new->root = RB_ROOT; | |
2786 | INIT_LIST_HEAD(&new->head); | |
2787 | ||
2788 | path = btrfs_alloc_path(); | |
2789 | if (!path) | |
2790 | goto out_kfree; | |
2791 | ||
4a0cc7ca | 2792 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2793 | key.type = BTRFS_EXTENT_DATA_KEY; |
2794 | key.offset = new->file_pos; | |
2795 | ||
2796 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2797 | if (ret < 0) | |
2798 | goto out_free_path; | |
2799 | if (ret > 0 && path->slots[0] > 0) | |
2800 | path->slots[0]--; | |
2801 | ||
2802 | /* find out all the old extents for the file range */ | |
2803 | while (1) { | |
2804 | struct btrfs_file_extent_item *extent; | |
2805 | struct extent_buffer *l; | |
2806 | int slot; | |
2807 | u64 num_bytes; | |
2808 | u64 offset; | |
2809 | u64 end; | |
2810 | u64 disk_bytenr; | |
2811 | u64 extent_offset; | |
2812 | ||
2813 | l = path->nodes[0]; | |
2814 | slot = path->slots[0]; | |
2815 | ||
2816 | if (slot >= btrfs_header_nritems(l)) { | |
2817 | ret = btrfs_next_leaf(root, path); | |
2818 | if (ret < 0) | |
6f519564 | 2819 | goto out_free_path; |
38c227d8 LB |
2820 | else if (ret > 0) |
2821 | break; | |
2822 | continue; | |
2823 | } | |
2824 | ||
2825 | btrfs_item_key_to_cpu(l, &key, slot); | |
2826 | ||
4a0cc7ca | 2827 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2828 | break; |
2829 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2830 | break; | |
2831 | if (key.offset >= new->file_pos + new->len) | |
2832 | break; | |
2833 | ||
2834 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2835 | ||
2836 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2837 | if (key.offset + num_bytes < new->file_pos) | |
2838 | goto next; | |
2839 | ||
2840 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2841 | if (!disk_bytenr) | |
2842 | goto next; | |
2843 | ||
2844 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2845 | ||
2846 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2847 | if (!old) | |
6f519564 | 2848 | goto out_free_path; |
38c227d8 LB |
2849 | |
2850 | offset = max(new->file_pos, key.offset); | |
2851 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2852 | ||
2853 | old->bytenr = disk_bytenr; | |
2854 | old->extent_offset = extent_offset; | |
2855 | old->offset = offset - key.offset; | |
2856 | old->len = end - offset; | |
2857 | old->new = new; | |
2858 | old->count = 0; | |
2859 | list_add_tail(&old->list, &new->head); | |
2860 | next: | |
2861 | path->slots[0]++; | |
2862 | cond_resched(); | |
2863 | } | |
2864 | ||
2865 | btrfs_free_path(path); | |
0b246afa | 2866 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2867 | |
2868 | return new; | |
2869 | ||
38c227d8 LB |
2870 | out_free_path: |
2871 | btrfs_free_path(path); | |
2872 | out_kfree: | |
6f519564 | 2873 | free_sa_defrag_extent(new); |
38c227d8 LB |
2874 | return NULL; |
2875 | } | |
2876 | ||
2ff7e61e | 2877 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2878 | u64 start, u64 len) |
2879 | { | |
2880 | struct btrfs_block_group_cache *cache; | |
2881 | ||
0b246afa | 2882 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2883 | ASSERT(cache); |
2884 | ||
2885 | spin_lock(&cache->lock); | |
2886 | cache->delalloc_bytes -= len; | |
2887 | spin_unlock(&cache->lock); | |
2888 | ||
2889 | btrfs_put_block_group(cache); | |
2890 | } | |
2891 | ||
d352ac68 CM |
2892 | /* as ordered data IO finishes, this gets called so we can finish |
2893 | * an ordered extent if the range of bytes in the file it covers are | |
2894 | * fully written. | |
2895 | */ | |
5fd02043 | 2896 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2897 | { |
5fd02043 | 2898 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2899 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2900 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2901 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2902 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2903 | struct extent_state *cached_state = NULL; |
38c227d8 | 2904 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2905 | int compress_type = 0; |
77cef2ec JB |
2906 | int ret = 0; |
2907 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2908 | bool nolock; |
77cef2ec | 2909 | bool truncated = false; |
a7e3b975 FM |
2910 | bool range_locked = false; |
2911 | bool clear_new_delalloc_bytes = false; | |
2912 | ||
2913 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2914 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2915 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2916 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2917 | |
70ddc553 | 2918 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2919 | |
5fd02043 JB |
2920 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2921 | ret = -EIO; | |
2922 | goto out; | |
2923 | } | |
2924 | ||
7ab7956e NB |
2925 | btrfs_free_io_failure_record(BTRFS_I(inode), |
2926 | ordered_extent->file_offset, | |
2927 | ordered_extent->file_offset + | |
2928 | ordered_extent->len - 1); | |
f612496b | 2929 | |
77cef2ec JB |
2930 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2931 | truncated = true; | |
2932 | logical_len = ordered_extent->truncated_len; | |
2933 | /* Truncated the entire extent, don't bother adding */ | |
2934 | if (!logical_len) | |
2935 | goto out; | |
2936 | } | |
2937 | ||
c2167754 | 2938 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2939 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2940 | |
2941 | /* | |
2942 | * For mwrite(mmap + memset to write) case, we still reserve | |
2943 | * space for NOCOW range. | |
2944 | * As NOCOW won't cause a new delayed ref, just free the space | |
2945 | */ | |
bc42bda2 | 2946 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
94ed938a | 2947 | ordered_extent->len); |
6c760c07 JB |
2948 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2949 | if (nolock) | |
2950 | trans = btrfs_join_transaction_nolock(root); | |
2951 | else | |
2952 | trans = btrfs_join_transaction(root); | |
2953 | if (IS_ERR(trans)) { | |
2954 | ret = PTR_ERR(trans); | |
2955 | trans = NULL; | |
2956 | goto out; | |
c2167754 | 2957 | } |
69fe2d75 | 2958 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
6c760c07 JB |
2959 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2960 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2961 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2962 | goto out; |
2963 | } | |
e6dcd2dc | 2964 | |
a7e3b975 | 2965 | range_locked = true; |
2ac55d41 JB |
2966 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2967 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2968 | &cached_state); |
e6dcd2dc | 2969 | |
38c227d8 LB |
2970 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2971 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 2972 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
2973 | if (ret) { |
2974 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2975 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2976 | /* the inode is shared */ |
2977 | new = record_old_file_extents(inode, ordered_extent); | |
2978 | ||
2979 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2980 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2981 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2982 | } | |
2983 | ||
0cb59c99 | 2984 | if (nolock) |
7a7eaa40 | 2985 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2986 | else |
7a7eaa40 | 2987 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2988 | if (IS_ERR(trans)) { |
2989 | ret = PTR_ERR(trans); | |
2990 | trans = NULL; | |
a7e3b975 | 2991 | goto out; |
79787eaa | 2992 | } |
a79b7d4b | 2993 | |
69fe2d75 | 2994 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
c2167754 | 2995 | |
c8b97818 | 2996 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2997 | compress_type = ordered_extent->compress_type; |
d899e052 | 2998 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2999 | BUG_ON(compress_type); |
7a6d7067 | 3000 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
3001 | ordered_extent->file_offset, |
3002 | ordered_extent->file_offset + | |
77cef2ec | 3003 | logical_len); |
d899e052 | 3004 | } else { |
0b246afa | 3005 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
3006 | ret = insert_reserved_file_extent(trans, inode, |
3007 | ordered_extent->file_offset, | |
3008 | ordered_extent->start, | |
3009 | ordered_extent->disk_len, | |
77cef2ec | 3010 | logical_len, logical_len, |
261507a0 | 3011 | compress_type, 0, 0, |
d899e052 | 3012 | BTRFS_FILE_EXTENT_REG); |
e570fd27 | 3013 | if (!ret) |
2ff7e61e | 3014 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3015 | ordered_extent->start, |
3016 | ordered_extent->disk_len); | |
d899e052 | 3017 | } |
5dc562c5 JB |
3018 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3019 | ordered_extent->file_offset, ordered_extent->len, | |
3020 | trans->transid); | |
79787eaa | 3021 | if (ret < 0) { |
66642832 | 3022 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3023 | goto out; |
79787eaa | 3024 | } |
2ac55d41 | 3025 | |
df9f628e | 3026 | add_pending_csums(trans, inode, &ordered_extent->list); |
e6dcd2dc | 3027 | |
6c760c07 JB |
3028 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3029 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3030 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3031 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3032 | goto out; |
1ef30be1 JB |
3033 | } |
3034 | ret = 0; | |
c2167754 | 3035 | out: |
a7e3b975 FM |
3036 | if (range_locked || clear_new_delalloc_bytes) { |
3037 | unsigned int clear_bits = 0; | |
3038 | ||
3039 | if (range_locked) | |
3040 | clear_bits |= EXTENT_LOCKED; | |
3041 | if (clear_new_delalloc_bytes) | |
3042 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3043 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3044 | ordered_extent->file_offset, | |
3045 | ordered_extent->file_offset + | |
3046 | ordered_extent->len - 1, | |
3047 | clear_bits, | |
3048 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
3049 | 0, &cached_state, GFP_NOFS); | |
3050 | } | |
3051 | ||
a698d075 | 3052 | if (trans) |
3a45bb20 | 3053 | btrfs_end_transaction(trans); |
0cb59c99 | 3054 | |
77cef2ec JB |
3055 | if (ret || truncated) { |
3056 | u64 start, end; | |
3057 | ||
3058 | if (truncated) | |
3059 | start = ordered_extent->file_offset + logical_len; | |
3060 | else | |
3061 | start = ordered_extent->file_offset; | |
3062 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3063 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3064 | ||
3065 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3066 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3067 | |
0bec9ef5 JB |
3068 | /* |
3069 | * If the ordered extent had an IOERR or something else went | |
3070 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3071 | * back to the allocator. We only free the extent in the |
3072 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3073 | */ |
77cef2ec JB |
3074 | if ((ret || !logical_len) && |
3075 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 | 3076 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3077 | btrfs_free_reserved_extent(fs_info, |
3078 | ordered_extent->start, | |
e570fd27 | 3079 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3080 | } |
3081 | ||
3082 | ||
5fd02043 | 3083 | /* |
8bad3c02 LB |
3084 | * This needs to be done to make sure anybody waiting knows we are done |
3085 | * updating everything for this ordered extent. | |
5fd02043 JB |
3086 | */ |
3087 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3088 | ||
38c227d8 | 3089 | /* for snapshot-aware defrag */ |
6f519564 LB |
3090 | if (new) { |
3091 | if (ret) { | |
3092 | free_sa_defrag_extent(new); | |
0b246afa | 3093 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3094 | } else { |
3095 | relink_file_extents(new); | |
3096 | } | |
3097 | } | |
38c227d8 | 3098 | |
e6dcd2dc CM |
3099 | /* once for us */ |
3100 | btrfs_put_ordered_extent(ordered_extent); | |
3101 | /* once for the tree */ | |
3102 | btrfs_put_ordered_extent(ordered_extent); | |
3103 | ||
5fd02043 JB |
3104 | return ret; |
3105 | } | |
3106 | ||
3107 | static void finish_ordered_fn(struct btrfs_work *work) | |
3108 | { | |
3109 | struct btrfs_ordered_extent *ordered_extent; | |
3110 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3111 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3112 | } |
3113 | ||
c3988d63 | 3114 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3115 | struct extent_state *state, int uptodate) |
3116 | { | |
5fd02043 | 3117 | struct inode *inode = page->mapping->host; |
0b246afa | 3118 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3119 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3120 | struct btrfs_workqueue *wq; |
3121 | btrfs_work_func_t func; | |
5fd02043 | 3122 | |
1abe9b8a | 3123 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3124 | ||
8b62b72b | 3125 | ClearPagePrivate2(page); |
5fd02043 JB |
3126 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3127 | end - start + 1, uptodate)) | |
c3988d63 | 3128 | return; |
5fd02043 | 3129 | |
70ddc553 | 3130 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
0b246afa | 3131 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3132 | func = btrfs_freespace_write_helper; |
3133 | } else { | |
0b246afa | 3134 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3135 | func = btrfs_endio_write_helper; |
3136 | } | |
5fd02043 | 3137 | |
9e0af237 LB |
3138 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3139 | NULL); | |
3140 | btrfs_queue_work(wq, &ordered_extent->work); | |
211f90e6 CM |
3141 | } |
3142 | ||
dc380aea MX |
3143 | static int __readpage_endio_check(struct inode *inode, |
3144 | struct btrfs_io_bio *io_bio, | |
3145 | int icsum, struct page *page, | |
3146 | int pgoff, u64 start, size_t len) | |
3147 | { | |
3148 | char *kaddr; | |
3149 | u32 csum_expected; | |
3150 | u32 csum = ~(u32)0; | |
dc380aea MX |
3151 | |
3152 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3153 | ||
3154 | kaddr = kmap_atomic(page); | |
3155 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3156 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3157 | if (csum != csum_expected) |
3158 | goto zeroit; | |
3159 | ||
3160 | kunmap_atomic(kaddr); | |
3161 | return 0; | |
3162 | zeroit: | |
0970a22e | 3163 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
6f6b643e | 3164 | io_bio->mirror_num); |
dc380aea MX |
3165 | memset(kaddr + pgoff, 1, len); |
3166 | flush_dcache_page(page); | |
3167 | kunmap_atomic(kaddr); | |
dc380aea MX |
3168 | return -EIO; |
3169 | } | |
3170 | ||
d352ac68 CM |
3171 | /* |
3172 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3173 | * if there's a match, we allow the bio to finish. If not, the code in |
3174 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3175 | */ |
facc8a22 MX |
3176 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3177 | u64 phy_offset, struct page *page, | |
3178 | u64 start, u64 end, int mirror) | |
07157aac | 3179 | { |
4eee4fa4 | 3180 | size_t offset = start - page_offset(page); |
07157aac | 3181 | struct inode *inode = page->mapping->host; |
d1310b2e | 3182 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3183 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3184 | |
d20f7043 CM |
3185 | if (PageChecked(page)) { |
3186 | ClearPageChecked(page); | |
dc380aea | 3187 | return 0; |
d20f7043 | 3188 | } |
6cbff00f CH |
3189 | |
3190 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3191 | return 0; |
17d217fe YZ |
3192 | |
3193 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3194 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3195 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3196 | return 0; |
17d217fe | 3197 | } |
d20f7043 | 3198 | |
facc8a22 | 3199 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3200 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3201 | start, (size_t)(end - start + 1)); | |
07157aac | 3202 | } |
b888db2b | 3203 | |
24bbcf04 YZ |
3204 | void btrfs_add_delayed_iput(struct inode *inode) |
3205 | { | |
0b246afa | 3206 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3207 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3208 | |
3209 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3210 | return; | |
3211 | ||
24bbcf04 | 3212 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3213 | if (binode->delayed_iput_count == 0) { |
3214 | ASSERT(list_empty(&binode->delayed_iput)); | |
3215 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3216 | } else { | |
3217 | binode->delayed_iput_count++; | |
3218 | } | |
24bbcf04 YZ |
3219 | spin_unlock(&fs_info->delayed_iput_lock); |
3220 | } | |
3221 | ||
2ff7e61e | 3222 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3223 | { |
24bbcf04 | 3224 | |
24bbcf04 | 3225 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3226 | while (!list_empty(&fs_info->delayed_iputs)) { |
3227 | struct btrfs_inode *inode; | |
3228 | ||
3229 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3230 | struct btrfs_inode, delayed_iput); | |
3231 | if (inode->delayed_iput_count) { | |
3232 | inode->delayed_iput_count--; | |
3233 | list_move_tail(&inode->delayed_iput, | |
3234 | &fs_info->delayed_iputs); | |
3235 | } else { | |
3236 | list_del_init(&inode->delayed_iput); | |
3237 | } | |
3238 | spin_unlock(&fs_info->delayed_iput_lock); | |
3239 | iput(&inode->vfs_inode); | |
3240 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3241 | } |
8089fe62 | 3242 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3243 | } |
3244 | ||
d68fc57b | 3245 | /* |
42b2aa86 | 3246 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3247 | * files in the subvolume, it removes orphan item and frees block_rsv |
3248 | * structure. | |
3249 | */ | |
3250 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3251 | struct btrfs_root *root) | |
3252 | { | |
0b246afa | 3253 | struct btrfs_fs_info *fs_info = root->fs_info; |
90290e19 | 3254 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3255 | int ret; |
3256 | ||
8a35d95f | 3257 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3258 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3259 | return; | |
3260 | ||
90290e19 | 3261 | spin_lock(&root->orphan_lock); |
8a35d95f | 3262 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3263 | spin_unlock(&root->orphan_lock); |
3264 | return; | |
3265 | } | |
3266 | ||
3267 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3268 | spin_unlock(&root->orphan_lock); | |
3269 | return; | |
3270 | } | |
3271 | ||
3272 | block_rsv = root->orphan_block_rsv; | |
3273 | root->orphan_block_rsv = NULL; | |
3274 | spin_unlock(&root->orphan_lock); | |
3275 | ||
27cdeb70 | 3276 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b | 3277 | btrfs_root_refs(&root->root_item) > 0) { |
0b246afa | 3278 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3279 | root->root_key.objectid); |
4ef31a45 | 3280 | if (ret) |
66642832 | 3281 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3282 | else |
27cdeb70 MX |
3283 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3284 | &root->state); | |
d68fc57b YZ |
3285 | } |
3286 | ||
90290e19 JB |
3287 | if (block_rsv) { |
3288 | WARN_ON(block_rsv->size > 0); | |
2ff7e61e | 3289 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b YZ |
3290 | } |
3291 | } | |
3292 | ||
7b128766 JB |
3293 | /* |
3294 | * This creates an orphan entry for the given inode in case something goes | |
3295 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3296 | * |
3297 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3298 | * this function. | |
7b128766 | 3299 | */ |
73f2e545 NB |
3300 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
3301 | struct btrfs_inode *inode) | |
7b128766 | 3302 | { |
73f2e545 NB |
3303 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
3304 | struct btrfs_root *root = inode->root; | |
d68fc57b YZ |
3305 | struct btrfs_block_rsv *block_rsv = NULL; |
3306 | int reserve = 0; | |
3307 | int insert = 0; | |
3308 | int ret; | |
7b128766 | 3309 | |
d68fc57b | 3310 | if (!root->orphan_block_rsv) { |
2ff7e61e JM |
3311 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
3312 | BTRFS_BLOCK_RSV_TEMP); | |
b532402e TI |
3313 | if (!block_rsv) |
3314 | return -ENOMEM; | |
d68fc57b | 3315 | } |
7b128766 | 3316 | |
d68fc57b YZ |
3317 | spin_lock(&root->orphan_lock); |
3318 | if (!root->orphan_block_rsv) { | |
3319 | root->orphan_block_rsv = block_rsv; | |
3320 | } else if (block_rsv) { | |
2ff7e61e | 3321 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b | 3322 | block_rsv = NULL; |
7b128766 | 3323 | } |
7b128766 | 3324 | |
8a35d95f | 3325 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3326 | &inode->runtime_flags)) { |
d68fc57b YZ |
3327 | #if 0 |
3328 | /* | |
3329 | * For proper ENOSPC handling, we should do orphan | |
3330 | * cleanup when mounting. But this introduces backward | |
3331 | * compatibility issue. | |
3332 | */ | |
3333 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3334 | insert = 2; | |
3335 | else | |
3336 | insert = 1; | |
3337 | #endif | |
3338 | insert = 1; | |
321f0e70 | 3339 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3340 | } |
3341 | ||
72ac3c0d | 3342 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
73f2e545 | 3343 | &inode->runtime_flags)) |
d68fc57b | 3344 | reserve = 1; |
d68fc57b | 3345 | spin_unlock(&root->orphan_lock); |
7b128766 | 3346 | |
d68fc57b YZ |
3347 | /* grab metadata reservation from transaction handle */ |
3348 | if (reserve) { | |
3349 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3350 | ASSERT(!ret); |
3351 | if (ret) { | |
3352 | atomic_dec(&root->orphan_inodes); | |
3353 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3354 | &inode->runtime_flags); |
3b6571c1 JB |
3355 | if (insert) |
3356 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
73f2e545 | 3357 | &inode->runtime_flags); |
3b6571c1 JB |
3358 | return ret; |
3359 | } | |
d68fc57b | 3360 | } |
7b128766 | 3361 | |
d68fc57b YZ |
3362 | /* insert an orphan item to track this unlinked/truncated file */ |
3363 | if (insert >= 1) { | |
73f2e545 | 3364 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3365 | if (ret) { |
703c88e0 | 3366 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3367 | if (reserve) { |
3368 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3369 | &inode->runtime_flags); |
4ef31a45 JB |
3370 | btrfs_orphan_release_metadata(inode); |
3371 | } | |
3372 | if (ret != -EEXIST) { | |
e8e7cff6 | 3373 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3374 | &inode->runtime_flags); |
66642832 | 3375 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3376 | return ret; |
3377 | } | |
79787eaa JM |
3378 | } |
3379 | ret = 0; | |
d68fc57b YZ |
3380 | } |
3381 | ||
3382 | /* insert an orphan item to track subvolume contains orphan files */ | |
3383 | if (insert >= 2) { | |
0b246afa | 3384 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3385 | root->root_key.objectid); |
79787eaa | 3386 | if (ret && ret != -EEXIST) { |
66642832 | 3387 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3388 | return ret; |
3389 | } | |
d68fc57b YZ |
3390 | } |
3391 | return 0; | |
7b128766 JB |
3392 | } |
3393 | ||
3394 | /* | |
3395 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3396 | * item for this particular inode. | |
3397 | */ | |
48a3b636 | 3398 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3399 | struct btrfs_inode *inode) |
7b128766 | 3400 | { |
3d6ae7bb | 3401 | struct btrfs_root *root = inode->root; |
d68fc57b YZ |
3402 | int delete_item = 0; |
3403 | int release_rsv = 0; | |
7b128766 JB |
3404 | int ret = 0; |
3405 | ||
d68fc57b | 3406 | spin_lock(&root->orphan_lock); |
8a35d95f | 3407 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3d6ae7bb | 3408 | &inode->runtime_flags)) |
d68fc57b | 3409 | delete_item = 1; |
7b128766 | 3410 | |
72ac3c0d | 3411 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3d6ae7bb | 3412 | &inode->runtime_flags)) |
d68fc57b | 3413 | release_rsv = 1; |
d68fc57b | 3414 | spin_unlock(&root->orphan_lock); |
7b128766 | 3415 | |
703c88e0 | 3416 | if (delete_item) { |
8a35d95f | 3417 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3418 | if (trans) |
3419 | ret = btrfs_del_orphan_item(trans, root, | |
3d6ae7bb | 3420 | btrfs_ino(inode)); |
8a35d95f | 3421 | } |
7b128766 | 3422 | |
703c88e0 FDBM |
3423 | if (release_rsv) |
3424 | btrfs_orphan_release_metadata(inode); | |
3425 | ||
4ef31a45 | 3426 | return ret; |
7b128766 JB |
3427 | } |
3428 | ||
3429 | /* | |
3430 | * this cleans up any orphans that may be left on the list from the last use | |
3431 | * of this root. | |
3432 | */ | |
66b4ffd1 | 3433 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3434 | { |
0b246afa | 3435 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3436 | struct btrfs_path *path; |
3437 | struct extent_buffer *leaf; | |
7b128766 JB |
3438 | struct btrfs_key key, found_key; |
3439 | struct btrfs_trans_handle *trans; | |
3440 | struct inode *inode; | |
8f6d7f4f | 3441 | u64 last_objectid = 0; |
7b128766 JB |
3442 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3443 | ||
d68fc57b | 3444 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3445 | return 0; |
c71bf099 YZ |
3446 | |
3447 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3448 | if (!path) { |
3449 | ret = -ENOMEM; | |
3450 | goto out; | |
3451 | } | |
e4058b54 | 3452 | path->reada = READA_BACK; |
7b128766 JB |
3453 | |
3454 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3455 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3456 | key.offset = (u64)-1; |
3457 | ||
7b128766 JB |
3458 | while (1) { |
3459 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3460 | if (ret < 0) |
3461 | goto out; | |
7b128766 JB |
3462 | |
3463 | /* | |
3464 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3465 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3466 | * find the key and see if we have stuff that matches |
3467 | */ | |
3468 | if (ret > 0) { | |
66b4ffd1 | 3469 | ret = 0; |
7b128766 JB |
3470 | if (path->slots[0] == 0) |
3471 | break; | |
3472 | path->slots[0]--; | |
3473 | } | |
3474 | ||
3475 | /* pull out the item */ | |
3476 | leaf = path->nodes[0]; | |
7b128766 JB |
3477 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3478 | ||
3479 | /* make sure the item matches what we want */ | |
3480 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3481 | break; | |
962a298f | 3482 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3483 | break; |
3484 | ||
3485 | /* release the path since we're done with it */ | |
b3b4aa74 | 3486 | btrfs_release_path(path); |
7b128766 JB |
3487 | |
3488 | /* | |
3489 | * this is where we are basically btrfs_lookup, without the | |
3490 | * crossing root thing. we store the inode number in the | |
3491 | * offset of the orphan item. | |
3492 | */ | |
8f6d7f4f JB |
3493 | |
3494 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3495 | btrfs_err(fs_info, |
3496 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3497 | ret = -EINVAL; |
3498 | goto out; | |
3499 | } | |
3500 | ||
3501 | last_objectid = found_key.offset; | |
3502 | ||
5d4f98a2 YZ |
3503 | found_key.objectid = found_key.offset; |
3504 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3505 | found_key.offset = 0; | |
0b246afa | 3506 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3507 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3508 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3509 | goto out; |
7b128766 | 3510 | |
0b246afa | 3511 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3512 | struct btrfs_root *dead_root; |
3513 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3514 | int is_dead_root = 0; | |
3515 | ||
3516 | /* | |
3517 | * this is an orphan in the tree root. Currently these | |
3518 | * could come from 2 sources: | |
3519 | * a) a snapshot deletion in progress | |
3520 | * b) a free space cache inode | |
3521 | * We need to distinguish those two, as the snapshot | |
3522 | * orphan must not get deleted. | |
3523 | * find_dead_roots already ran before us, so if this | |
3524 | * is a snapshot deletion, we should find the root | |
3525 | * in the dead_roots list | |
3526 | */ | |
3527 | spin_lock(&fs_info->trans_lock); | |
3528 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3529 | root_list) { | |
3530 | if (dead_root->root_key.objectid == | |
3531 | found_key.objectid) { | |
3532 | is_dead_root = 1; | |
3533 | break; | |
3534 | } | |
3535 | } | |
3536 | spin_unlock(&fs_info->trans_lock); | |
3537 | if (is_dead_root) { | |
3538 | /* prevent this orphan from being found again */ | |
3539 | key.offset = found_key.objectid - 1; | |
3540 | continue; | |
3541 | } | |
3542 | } | |
7b128766 | 3543 | /* |
a8c9e576 JB |
3544 | * Inode is already gone but the orphan item is still there, |
3545 | * kill the orphan item. | |
7b128766 | 3546 | */ |
67710892 | 3547 | if (ret == -ENOENT) { |
a8c9e576 | 3548 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3549 | if (IS_ERR(trans)) { |
3550 | ret = PTR_ERR(trans); | |
3551 | goto out; | |
3552 | } | |
0b246afa JM |
3553 | btrfs_debug(fs_info, "auto deleting %Lu", |
3554 | found_key.objectid); | |
a8c9e576 JB |
3555 | ret = btrfs_del_orphan_item(trans, root, |
3556 | found_key.objectid); | |
3a45bb20 | 3557 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3558 | if (ret) |
3559 | goto out; | |
7b128766 JB |
3560 | continue; |
3561 | } | |
3562 | ||
a8c9e576 JB |
3563 | /* |
3564 | * add this inode to the orphan list so btrfs_orphan_del does | |
3565 | * the proper thing when we hit it | |
3566 | */ | |
8a35d95f JB |
3567 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3568 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3569 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3570 | |
7b128766 JB |
3571 | /* if we have links, this was a truncate, lets do that */ |
3572 | if (inode->i_nlink) { | |
fae7f21c | 3573 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3574 | iput(inode); |
3575 | continue; | |
3576 | } | |
7b128766 | 3577 | nr_truncate++; |
f3fe820c JB |
3578 | |
3579 | /* 1 for the orphan item deletion. */ | |
3580 | trans = btrfs_start_transaction(root, 1); | |
3581 | if (IS_ERR(trans)) { | |
c69b26b0 | 3582 | iput(inode); |
f3fe820c JB |
3583 | ret = PTR_ERR(trans); |
3584 | goto out; | |
3585 | } | |
73f2e545 | 3586 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 3587 | btrfs_end_transaction(trans); |
c69b26b0 JB |
3588 | if (ret) { |
3589 | iput(inode); | |
f3fe820c | 3590 | goto out; |
c69b26b0 | 3591 | } |
f3fe820c | 3592 | |
66b4ffd1 | 3593 | ret = btrfs_truncate(inode); |
4a7d0f68 | 3594 | if (ret) |
3d6ae7bb | 3595 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7b128766 JB |
3596 | } else { |
3597 | nr_unlink++; | |
3598 | } | |
3599 | ||
3600 | /* this will do delete_inode and everything for us */ | |
3601 | iput(inode); | |
66b4ffd1 JB |
3602 | if (ret) |
3603 | goto out; | |
7b128766 | 3604 | } |
3254c876 MX |
3605 | /* release the path since we're done with it */ |
3606 | btrfs_release_path(path); | |
3607 | ||
d68fc57b YZ |
3608 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3609 | ||
3610 | if (root->orphan_block_rsv) | |
2ff7e61e | 3611 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
d68fc57b YZ |
3612 | (u64)-1); |
3613 | ||
27cdeb70 MX |
3614 | if (root->orphan_block_rsv || |
3615 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3616 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3617 | if (!IS_ERR(trans)) |
3a45bb20 | 3618 | btrfs_end_transaction(trans); |
d68fc57b | 3619 | } |
7b128766 JB |
3620 | |
3621 | if (nr_unlink) | |
0b246afa | 3622 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3623 | if (nr_truncate) |
0b246afa | 3624 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3625 | |
3626 | out: | |
3627 | if (ret) | |
0b246afa | 3628 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3629 | btrfs_free_path(path); |
3630 | return ret; | |
7b128766 JB |
3631 | } |
3632 | ||
46a53cca CM |
3633 | /* |
3634 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3635 | * don't find any xattrs, we know there can't be any acls. | |
3636 | * | |
3637 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3638 | */ | |
3639 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3640 | int slot, u64 objectid, |
3641 | int *first_xattr_slot) | |
46a53cca CM |
3642 | { |
3643 | u32 nritems = btrfs_header_nritems(leaf); | |
3644 | struct btrfs_key found_key; | |
f23b5a59 JB |
3645 | static u64 xattr_access = 0; |
3646 | static u64 xattr_default = 0; | |
46a53cca CM |
3647 | int scanned = 0; |
3648 | ||
f23b5a59 | 3649 | if (!xattr_access) { |
97d79299 AG |
3650 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3651 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3652 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3653 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3654 | } |
3655 | ||
46a53cca | 3656 | slot++; |
63541927 | 3657 | *first_xattr_slot = -1; |
46a53cca CM |
3658 | while (slot < nritems) { |
3659 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3660 | ||
3661 | /* we found a different objectid, there must not be acls */ | |
3662 | if (found_key.objectid != objectid) | |
3663 | return 0; | |
3664 | ||
3665 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3666 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3667 | if (*first_xattr_slot == -1) |
3668 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3669 | if (found_key.offset == xattr_access || |
3670 | found_key.offset == xattr_default) | |
3671 | return 1; | |
3672 | } | |
46a53cca CM |
3673 | |
3674 | /* | |
3675 | * we found a key greater than an xattr key, there can't | |
3676 | * be any acls later on | |
3677 | */ | |
3678 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3679 | return 0; | |
3680 | ||
3681 | slot++; | |
3682 | scanned++; | |
3683 | ||
3684 | /* | |
3685 | * it goes inode, inode backrefs, xattrs, extents, | |
3686 | * so if there are a ton of hard links to an inode there can | |
3687 | * be a lot of backrefs. Don't waste time searching too hard, | |
3688 | * this is just an optimization | |
3689 | */ | |
3690 | if (scanned >= 8) | |
3691 | break; | |
3692 | } | |
3693 | /* we hit the end of the leaf before we found an xattr or | |
3694 | * something larger than an xattr. We have to assume the inode | |
3695 | * has acls | |
3696 | */ | |
63541927 FDBM |
3697 | if (*first_xattr_slot == -1) |
3698 | *first_xattr_slot = slot; | |
46a53cca CM |
3699 | return 1; |
3700 | } | |
3701 | ||
d352ac68 CM |
3702 | /* |
3703 | * read an inode from the btree into the in-memory inode | |
3704 | */ | |
67710892 | 3705 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 | 3706 | { |
0b246afa | 3707 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 3708 | struct btrfs_path *path; |
5f39d397 | 3709 | struct extent_buffer *leaf; |
39279cc3 CM |
3710 | struct btrfs_inode_item *inode_item; |
3711 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3712 | struct btrfs_key location; | |
67de1176 | 3713 | unsigned long ptr; |
46a53cca | 3714 | int maybe_acls; |
618e21d5 | 3715 | u32 rdev; |
39279cc3 | 3716 | int ret; |
2f7e33d4 | 3717 | bool filled = false; |
63541927 | 3718 | int first_xattr_slot; |
2f7e33d4 MX |
3719 | |
3720 | ret = btrfs_fill_inode(inode, &rdev); | |
3721 | if (!ret) | |
3722 | filled = true; | |
39279cc3 CM |
3723 | |
3724 | path = btrfs_alloc_path(); | |
67710892 FM |
3725 | if (!path) { |
3726 | ret = -ENOMEM; | |
1748f843 | 3727 | goto make_bad; |
67710892 | 3728 | } |
1748f843 | 3729 | |
39279cc3 | 3730 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3731 | |
39279cc3 | 3732 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3733 | if (ret) { |
3734 | if (ret > 0) | |
3735 | ret = -ENOENT; | |
39279cc3 | 3736 | goto make_bad; |
67710892 | 3737 | } |
39279cc3 | 3738 | |
5f39d397 | 3739 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3740 | |
3741 | if (filled) | |
67de1176 | 3742 | goto cache_index; |
2f7e33d4 | 3743 | |
5f39d397 CM |
3744 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3745 | struct btrfs_inode_item); | |
5f39d397 | 3746 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3747 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3748 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3749 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3750 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3751 | |
a937b979 DS |
3752 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3753 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3754 | |
a937b979 DS |
3755 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3756 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3757 | |
a937b979 DS |
3758 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3759 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3760 | |
9cc97d64 | 3761 | BTRFS_I(inode)->i_otime.tv_sec = |
3762 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3763 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3764 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3765 | |
a76a3cd4 | 3766 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3767 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3768 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3769 | ||
6e17d30b YD |
3770 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3771 | inode->i_generation = BTRFS_I(inode)->generation; | |
3772 | inode->i_rdev = 0; | |
3773 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3774 | ||
3775 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3776 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3777 | ||
3778 | cache_index: | |
5dc562c5 JB |
3779 | /* |
3780 | * If we were modified in the current generation and evicted from memory | |
3781 | * and then re-read we need to do a full sync since we don't have any | |
3782 | * idea about which extents were modified before we were evicted from | |
3783 | * cache. | |
6e17d30b YD |
3784 | * |
3785 | * This is required for both inode re-read from disk and delayed inode | |
3786 | * in delayed_nodes_tree. | |
5dc562c5 | 3787 | */ |
0b246afa | 3788 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3789 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3790 | &BTRFS_I(inode)->runtime_flags); | |
3791 | ||
bde6c242 FM |
3792 | /* |
3793 | * We don't persist the id of the transaction where an unlink operation | |
3794 | * against the inode was last made. So here we assume the inode might | |
3795 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3796 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3797 | * between the inode and its parent if the inode is fsync'ed and the log | |
3798 | * replayed. For example, in the scenario: | |
3799 | * | |
3800 | * touch mydir/foo | |
3801 | * ln mydir/foo mydir/bar | |
3802 | * sync | |
3803 | * unlink mydir/bar | |
3804 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3805 | * xfs_io -c fsync mydir/foo | |
3806 | * <power failure> | |
3807 | * mount fs, triggers fsync log replay | |
3808 | * | |
3809 | * We must make sure that when we fsync our inode foo we also log its | |
3810 | * parent inode, otherwise after log replay the parent still has the | |
3811 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3812 | * and doesn't have an inode ref with the name "bar" anymore. | |
3813 | * | |
3814 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3815 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3816 | * transaction commits on fsync if our inode is a directory, or if our |
3817 | * inode is not a directory, logging its parent unnecessarily. | |
3818 | */ | |
3819 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3820 | ||
67de1176 MX |
3821 | path->slots[0]++; |
3822 | if (inode->i_nlink != 1 || | |
3823 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3824 | goto cache_acl; | |
3825 | ||
3826 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3827 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3828 | goto cache_acl; |
3829 | ||
3830 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3831 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3832 | struct btrfs_inode_ref *ref; | |
3833 | ||
3834 | ref = (struct btrfs_inode_ref *)ptr; | |
3835 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3836 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3837 | struct btrfs_inode_extref *extref; | |
3838 | ||
3839 | extref = (struct btrfs_inode_extref *)ptr; | |
3840 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3841 | extref); | |
3842 | } | |
2f7e33d4 | 3843 | cache_acl: |
46a53cca CM |
3844 | /* |
3845 | * try to precache a NULL acl entry for files that don't have | |
3846 | * any xattrs or acls | |
3847 | */ | |
33345d01 | 3848 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3849 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3850 | if (first_xattr_slot != -1) { |
3851 | path->slots[0] = first_xattr_slot; | |
3852 | ret = btrfs_load_inode_props(inode, path); | |
3853 | if (ret) | |
0b246afa | 3854 | btrfs_err(fs_info, |
351fd353 | 3855 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3856 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3857 | root->root_key.objectid, ret); |
3858 | } | |
3859 | btrfs_free_path(path); | |
3860 | ||
72c04902 AV |
3861 | if (!maybe_acls) |
3862 | cache_no_acl(inode); | |
46a53cca | 3863 | |
39279cc3 | 3864 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3865 | case S_IFREG: |
3866 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3867 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3868 | inode->i_fop = &btrfs_file_operations; |
3869 | inode->i_op = &btrfs_file_inode_operations; | |
3870 | break; | |
3871 | case S_IFDIR: | |
3872 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3873 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3874 | break; |
3875 | case S_IFLNK: | |
3876 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3877 | inode_nohighmem(inode); |
39279cc3 CM |
3878 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3879 | break; | |
618e21d5 | 3880 | default: |
0279b4cd | 3881 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3882 | init_special_inode(inode, inode->i_mode, rdev); |
3883 | break; | |
39279cc3 | 3884 | } |
6cbff00f CH |
3885 | |
3886 | btrfs_update_iflags(inode); | |
67710892 | 3887 | return 0; |
39279cc3 CM |
3888 | |
3889 | make_bad: | |
39279cc3 | 3890 | btrfs_free_path(path); |
39279cc3 | 3891 | make_bad_inode(inode); |
67710892 | 3892 | return ret; |
39279cc3 CM |
3893 | } |
3894 | ||
d352ac68 CM |
3895 | /* |
3896 | * given a leaf and an inode, copy the inode fields into the leaf | |
3897 | */ | |
e02119d5 CM |
3898 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3899 | struct extent_buffer *leaf, | |
5f39d397 | 3900 | struct btrfs_inode_item *item, |
39279cc3 CM |
3901 | struct inode *inode) |
3902 | { | |
51fab693 LB |
3903 | struct btrfs_map_token token; |
3904 | ||
3905 | btrfs_init_map_token(&token); | |
5f39d397 | 3906 | |
51fab693 LB |
3907 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3908 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3909 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3910 | &token); | |
3911 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3912 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3913 | |
a937b979 | 3914 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3915 | inode->i_atime.tv_sec, &token); |
a937b979 | 3916 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3917 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3918 | |
a937b979 | 3919 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3920 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3921 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3922 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3923 | |
a937b979 | 3924 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3925 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3926 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3927 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3928 | |
9cc97d64 | 3929 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3930 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3931 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3932 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3933 | ||
51fab693 LB |
3934 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3935 | &token); | |
3936 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3937 | &token); | |
3938 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3939 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3940 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3941 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3942 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3943 | } |
3944 | ||
d352ac68 CM |
3945 | /* |
3946 | * copy everything in the in-memory inode into the btree. | |
3947 | */ | |
2115133f | 3948 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3949 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3950 | { |
3951 | struct btrfs_inode_item *inode_item; | |
3952 | struct btrfs_path *path; | |
5f39d397 | 3953 | struct extent_buffer *leaf; |
39279cc3 CM |
3954 | int ret; |
3955 | ||
3956 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3957 | if (!path) |
3958 | return -ENOMEM; | |
3959 | ||
b9473439 | 3960 | path->leave_spinning = 1; |
16cdcec7 MX |
3961 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3962 | 1); | |
39279cc3 CM |
3963 | if (ret) { |
3964 | if (ret > 0) | |
3965 | ret = -ENOENT; | |
3966 | goto failed; | |
3967 | } | |
3968 | ||
5f39d397 CM |
3969 | leaf = path->nodes[0]; |
3970 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3971 | struct btrfs_inode_item); |
39279cc3 | 3972 | |
e02119d5 | 3973 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3974 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3975 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3976 | ret = 0; |
3977 | failed: | |
39279cc3 CM |
3978 | btrfs_free_path(path); |
3979 | return ret; | |
3980 | } | |
3981 | ||
2115133f CM |
3982 | /* |
3983 | * copy everything in the in-memory inode into the btree. | |
3984 | */ | |
3985 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3986 | struct btrfs_root *root, struct inode *inode) | |
3987 | { | |
0b246afa | 3988 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3989 | int ret; |
3990 | ||
3991 | /* | |
3992 | * If the inode is a free space inode, we can deadlock during commit | |
3993 | * if we put it into the delayed code. | |
3994 | * | |
3995 | * The data relocation inode should also be directly updated | |
3996 | * without delay | |
3997 | */ | |
70ddc553 | 3998 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3999 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 4000 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4001 | btrfs_update_root_times(trans, root); |
4002 | ||
2115133f CM |
4003 | ret = btrfs_delayed_update_inode(trans, root, inode); |
4004 | if (!ret) | |
4005 | btrfs_set_inode_last_trans(trans, inode); | |
4006 | return ret; | |
4007 | } | |
4008 | ||
4009 | return btrfs_update_inode_item(trans, root, inode); | |
4010 | } | |
4011 | ||
be6aef60 JB |
4012 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4013 | struct btrfs_root *root, | |
4014 | struct inode *inode) | |
2115133f CM |
4015 | { |
4016 | int ret; | |
4017 | ||
4018 | ret = btrfs_update_inode(trans, root, inode); | |
4019 | if (ret == -ENOSPC) | |
4020 | return btrfs_update_inode_item(trans, root, inode); | |
4021 | return ret; | |
4022 | } | |
4023 | ||
d352ac68 CM |
4024 | /* |
4025 | * unlink helper that gets used here in inode.c and in the tree logging | |
4026 | * recovery code. It remove a link in a directory with a given name, and | |
4027 | * also drops the back refs in the inode to the directory | |
4028 | */ | |
92986796 AV |
4029 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4030 | struct btrfs_root *root, | |
4ec5934e NB |
4031 | struct btrfs_inode *dir, |
4032 | struct btrfs_inode *inode, | |
92986796 | 4033 | const char *name, int name_len) |
39279cc3 | 4034 | { |
0b246afa | 4035 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4036 | struct btrfs_path *path; |
39279cc3 | 4037 | int ret = 0; |
5f39d397 | 4038 | struct extent_buffer *leaf; |
39279cc3 | 4039 | struct btrfs_dir_item *di; |
5f39d397 | 4040 | struct btrfs_key key; |
aec7477b | 4041 | u64 index; |
33345d01 LZ |
4042 | u64 ino = btrfs_ino(inode); |
4043 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4044 | |
4045 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4046 | if (!path) { |
4047 | ret = -ENOMEM; | |
554233a6 | 4048 | goto out; |
54aa1f4d CM |
4049 | } |
4050 | ||
b9473439 | 4051 | path->leave_spinning = 1; |
33345d01 | 4052 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4053 | name, name_len, -1); |
4054 | if (IS_ERR(di)) { | |
4055 | ret = PTR_ERR(di); | |
4056 | goto err; | |
4057 | } | |
4058 | if (!di) { | |
4059 | ret = -ENOENT; | |
4060 | goto err; | |
4061 | } | |
5f39d397 CM |
4062 | leaf = path->nodes[0]; |
4063 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4064 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4065 | if (ret) |
4066 | goto err; | |
b3b4aa74 | 4067 | btrfs_release_path(path); |
39279cc3 | 4068 | |
67de1176 MX |
4069 | /* |
4070 | * If we don't have dir index, we have to get it by looking up | |
4071 | * the inode ref, since we get the inode ref, remove it directly, | |
4072 | * it is unnecessary to do delayed deletion. | |
4073 | * | |
4074 | * But if we have dir index, needn't search inode ref to get it. | |
4075 | * Since the inode ref is close to the inode item, it is better | |
4076 | * that we delay to delete it, and just do this deletion when | |
4077 | * we update the inode item. | |
4078 | */ | |
4ec5934e | 4079 | if (inode->dir_index) { |
67de1176 MX |
4080 | ret = btrfs_delayed_delete_inode_ref(inode); |
4081 | if (!ret) { | |
4ec5934e | 4082 | index = inode->dir_index; |
67de1176 MX |
4083 | goto skip_backref; |
4084 | } | |
4085 | } | |
4086 | ||
33345d01 LZ |
4087 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4088 | dir_ino, &index); | |
aec7477b | 4089 | if (ret) { |
0b246afa | 4090 | btrfs_info(fs_info, |
c2cf52eb | 4091 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4092 | name_len, name, ino, dir_ino); |
66642832 | 4093 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4094 | goto err; |
4095 | } | |
67de1176 | 4096 | skip_backref: |
2ff7e61e | 4097 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
79787eaa | 4098 | if (ret) { |
66642832 | 4099 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4100 | goto err; |
79787eaa | 4101 | } |
39279cc3 | 4102 | |
4ec5934e NB |
4103 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4104 | dir_ino); | |
79787eaa | 4105 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4106 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4107 | goto err; |
4108 | } | |
e02119d5 | 4109 | |
4ec5934e NB |
4110 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4111 | index); | |
6418c961 CM |
4112 | if (ret == -ENOENT) |
4113 | ret = 0; | |
d4e3991b | 4114 | else if (ret) |
66642832 | 4115 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4116 | err: |
4117 | btrfs_free_path(path); | |
e02119d5 CM |
4118 | if (ret) |
4119 | goto out; | |
4120 | ||
6ef06d27 | 4121 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4122 | inode_inc_iversion(&inode->vfs_inode); |
4123 | inode_inc_iversion(&dir->vfs_inode); | |
4124 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4125 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4126 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4127 | out: |
39279cc3 CM |
4128 | return ret; |
4129 | } | |
4130 | ||
92986796 AV |
4131 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4132 | struct btrfs_root *root, | |
4ec5934e | 4133 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4134 | const char *name, int name_len) |
4135 | { | |
4136 | int ret; | |
4137 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4138 | if (!ret) { | |
4ec5934e NB |
4139 | drop_nlink(&inode->vfs_inode); |
4140 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4141 | } |
4142 | return ret; | |
4143 | } | |
39279cc3 | 4144 | |
a22285a6 YZ |
4145 | /* |
4146 | * helper to start transaction for unlink and rmdir. | |
4147 | * | |
d52be818 JB |
4148 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4149 | * if we cannot make our reservations the normal way try and see if there is | |
4150 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4151 | * allow the unlink to occur. | |
a22285a6 | 4152 | */ |
d52be818 | 4153 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4154 | { |
a22285a6 | 4155 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4156 | |
e70bea5f JB |
4157 | /* |
4158 | * 1 for the possible orphan item | |
4159 | * 1 for the dir item | |
4160 | * 1 for the dir index | |
4161 | * 1 for the inode ref | |
e70bea5f JB |
4162 | * 1 for the inode |
4163 | */ | |
8eab77ff | 4164 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4165 | } |
4166 | ||
4167 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4168 | { | |
4169 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4170 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4171 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4172 | int ret; |
a22285a6 | 4173 | |
d52be818 | 4174 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4175 | if (IS_ERR(trans)) |
4176 | return PTR_ERR(trans); | |
5f39d397 | 4177 | |
4ec5934e NB |
4178 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4179 | 0); | |
12fcfd22 | 4180 | |
4ec5934e NB |
4181 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4182 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4183 | dentry->d_name.len); | |
b532402e TI |
4184 | if (ret) |
4185 | goto out; | |
7b128766 | 4186 | |
a22285a6 | 4187 | if (inode->i_nlink == 0) { |
73f2e545 | 4188 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4189 | if (ret) |
4190 | goto out; | |
a22285a6 | 4191 | } |
7b128766 | 4192 | |
b532402e | 4193 | out: |
3a45bb20 | 4194 | btrfs_end_transaction(trans); |
2ff7e61e | 4195 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4196 | return ret; |
4197 | } | |
4198 | ||
4df27c4d YZ |
4199 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4200 | struct btrfs_root *root, | |
4201 | struct inode *dir, u64 objectid, | |
4202 | const char *name, int name_len) | |
4203 | { | |
0b246afa | 4204 | struct btrfs_fs_info *fs_info = root->fs_info; |
4df27c4d YZ |
4205 | struct btrfs_path *path; |
4206 | struct extent_buffer *leaf; | |
4207 | struct btrfs_dir_item *di; | |
4208 | struct btrfs_key key; | |
4209 | u64 index; | |
4210 | int ret; | |
4a0cc7ca | 4211 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4212 | |
4213 | path = btrfs_alloc_path(); | |
4214 | if (!path) | |
4215 | return -ENOMEM; | |
4216 | ||
33345d01 | 4217 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4218 | name, name_len, -1); |
79787eaa JM |
4219 | if (IS_ERR_OR_NULL(di)) { |
4220 | if (!di) | |
4221 | ret = -ENOENT; | |
4222 | else | |
4223 | ret = PTR_ERR(di); | |
4224 | goto out; | |
4225 | } | |
4df27c4d YZ |
4226 | |
4227 | leaf = path->nodes[0]; | |
4228 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4229 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4230 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4231 | if (ret) { |
66642832 | 4232 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4233 | goto out; |
4234 | } | |
b3b4aa74 | 4235 | btrfs_release_path(path); |
4df27c4d | 4236 | |
0b246afa JM |
4237 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
4238 | root->root_key.objectid, dir_ino, | |
4239 | &index, name, name_len); | |
4df27c4d | 4240 | if (ret < 0) { |
79787eaa | 4241 | if (ret != -ENOENT) { |
66642832 | 4242 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4243 | goto out; |
4244 | } | |
33345d01 | 4245 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4246 | name, name_len); |
79787eaa JM |
4247 | if (IS_ERR_OR_NULL(di)) { |
4248 | if (!di) | |
4249 | ret = -ENOENT; | |
4250 | else | |
4251 | ret = PTR_ERR(di); | |
66642832 | 4252 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4253 | goto out; |
4254 | } | |
4df27c4d YZ |
4255 | |
4256 | leaf = path->nodes[0]; | |
4257 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4258 | btrfs_release_path(path); |
4df27c4d YZ |
4259 | index = key.offset; |
4260 | } | |
945d8962 | 4261 | btrfs_release_path(path); |
4df27c4d | 4262 | |
e67bbbb9 | 4263 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4264 | if (ret) { |
66642832 | 4265 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4266 | goto out; |
4267 | } | |
4df27c4d | 4268 | |
6ef06d27 | 4269 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4270 | inode_inc_iversion(dir); |
c2050a45 | 4271 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4272 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4273 | if (ret) |
66642832 | 4274 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4275 | out: |
71d7aed0 | 4276 | btrfs_free_path(path); |
79787eaa | 4277 | return ret; |
4df27c4d YZ |
4278 | } |
4279 | ||
39279cc3 CM |
4280 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4281 | { | |
2b0143b5 | 4282 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4283 | int err = 0; |
39279cc3 | 4284 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4285 | struct btrfs_trans_handle *trans; |
44f714da | 4286 | u64 last_unlink_trans; |
39279cc3 | 4287 | |
b3ae244e | 4288 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4289 | return -ENOTEMPTY; |
4a0cc7ca | 4290 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
b3ae244e | 4291 | return -EPERM; |
134d4512 | 4292 | |
d52be818 | 4293 | trans = __unlink_start_trans(dir); |
a22285a6 | 4294 | if (IS_ERR(trans)) |
5df6a9f6 | 4295 | return PTR_ERR(trans); |
5df6a9f6 | 4296 | |
4a0cc7ca | 4297 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4298 | err = btrfs_unlink_subvol(trans, root, dir, |
4299 | BTRFS_I(inode)->location.objectid, | |
4300 | dentry->d_name.name, | |
4301 | dentry->d_name.len); | |
4302 | goto out; | |
4303 | } | |
4304 | ||
73f2e545 | 4305 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4306 | if (err) |
4df27c4d | 4307 | goto out; |
7b128766 | 4308 | |
44f714da FM |
4309 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4310 | ||
39279cc3 | 4311 | /* now the directory is empty */ |
4ec5934e NB |
4312 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4313 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4314 | dentry->d_name.len); | |
44f714da | 4315 | if (!err) { |
6ef06d27 | 4316 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4317 | /* |
4318 | * Propagate the last_unlink_trans value of the deleted dir to | |
4319 | * its parent directory. This is to prevent an unrecoverable | |
4320 | * log tree in the case we do something like this: | |
4321 | * 1) create dir foo | |
4322 | * 2) create snapshot under dir foo | |
4323 | * 3) delete the snapshot | |
4324 | * 4) rmdir foo | |
4325 | * 5) mkdir foo | |
4326 | * 6) fsync foo or some file inside foo | |
4327 | */ | |
4328 | if (last_unlink_trans >= trans->transid) | |
4329 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4330 | } | |
4df27c4d | 4331 | out: |
3a45bb20 | 4332 | btrfs_end_transaction(trans); |
2ff7e61e | 4333 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4334 | |
39279cc3 CM |
4335 | return err; |
4336 | } | |
4337 | ||
28f75a0e CM |
4338 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4339 | struct btrfs_root *root, | |
4340 | u64 bytes_deleted) | |
4341 | { | |
0b246afa | 4342 | struct btrfs_fs_info *fs_info = root->fs_info; |
28f75a0e CM |
4343 | int ret; |
4344 | ||
dc95f7bf JB |
4345 | /* |
4346 | * This is only used to apply pressure to the enospc system, we don't | |
4347 | * intend to use this reservation at all. | |
4348 | */ | |
2ff7e61e | 4349 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
0b246afa JM |
4350 | bytes_deleted *= fs_info->nodesize; |
4351 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
28f75a0e | 4352 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
dc95f7bf | 4353 | if (!ret) { |
0b246afa | 4354 | trace_btrfs_space_reservation(fs_info, "transaction", |
dc95f7bf JB |
4355 | trans->transid, |
4356 | bytes_deleted, 1); | |
28f75a0e | 4357 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4358 | } |
28f75a0e CM |
4359 | return ret; |
4360 | ||
4361 | } | |
4362 | ||
ddfae63c JB |
4363 | /* |
4364 | * Return this if we need to call truncate_block for the last bit of the | |
4365 | * truncate. | |
4366 | */ | |
4367 | #define NEED_TRUNCATE_BLOCK 1 | |
0305cd5f | 4368 | |
39279cc3 CM |
4369 | /* |
4370 | * this can truncate away extent items, csum items and directory items. | |
4371 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4372 | * any higher than new_size |
39279cc3 CM |
4373 | * |
4374 | * csum items that cross the new i_size are truncated to the new size | |
4375 | * as well. | |
7b128766 JB |
4376 | * |
4377 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4378 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4379 | */ |
8082510e YZ |
4380 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4381 | struct btrfs_root *root, | |
4382 | struct inode *inode, | |
4383 | u64 new_size, u32 min_type) | |
39279cc3 | 4384 | { |
0b246afa | 4385 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4386 | struct btrfs_path *path; |
5f39d397 | 4387 | struct extent_buffer *leaf; |
39279cc3 | 4388 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4389 | struct btrfs_key key; |
4390 | struct btrfs_key found_key; | |
39279cc3 | 4391 | u64 extent_start = 0; |
db94535d | 4392 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4393 | u64 extent_offset = 0; |
39279cc3 | 4394 | u64 item_end = 0; |
c1aa4575 | 4395 | u64 last_size = new_size; |
8082510e | 4396 | u32 found_type = (u8)-1; |
39279cc3 CM |
4397 | int found_extent; |
4398 | int del_item; | |
85e21bac CM |
4399 | int pending_del_nr = 0; |
4400 | int pending_del_slot = 0; | |
179e29e4 | 4401 | int extent_type = -1; |
8082510e YZ |
4402 | int ret; |
4403 | int err = 0; | |
4a0cc7ca | 4404 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4405 | u64 bytes_deleted = 0; |
897ca819 TM |
4406 | bool be_nice = false; |
4407 | bool should_throttle = false; | |
4408 | bool should_end = false; | |
8082510e YZ |
4409 | |
4410 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4411 | |
28ed1345 CM |
4412 | /* |
4413 | * for non-free space inodes and ref cows, we want to back off from | |
4414 | * time to time | |
4415 | */ | |
70ddc553 | 4416 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 | 4417 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
897ca819 | 4418 | be_nice = true; |
28ed1345 | 4419 | |
0eb0e19c MF |
4420 | path = btrfs_alloc_path(); |
4421 | if (!path) | |
4422 | return -ENOMEM; | |
e4058b54 | 4423 | path->reada = READA_BACK; |
0eb0e19c | 4424 | |
5dc562c5 JB |
4425 | /* |
4426 | * We want to drop from the next block forward in case this new size is | |
4427 | * not block aligned since we will be keeping the last block of the | |
4428 | * extent just the way it is. | |
4429 | */ | |
27cdeb70 | 4430 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4431 | root == fs_info->tree_root) |
dcdbc059 | 4432 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4433 | fs_info->sectorsize), |
da17066c | 4434 | (u64)-1, 0); |
8082510e | 4435 | |
16cdcec7 MX |
4436 | /* |
4437 | * This function is also used to drop the items in the log tree before | |
4438 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4439 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4440 | * items. | |
4441 | */ | |
4442 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4443 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4444 | |
33345d01 | 4445 | key.objectid = ino; |
39279cc3 | 4446 | key.offset = (u64)-1; |
5f39d397 CM |
4447 | key.type = (u8)-1; |
4448 | ||
85e21bac | 4449 | search_again: |
28ed1345 CM |
4450 | /* |
4451 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4452 | * up a huge file in a single leaf. Most of the time that | |
4453 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4454 | */ | |
ee22184b | 4455 | if (be_nice && bytes_deleted > SZ_32M) { |
3a45bb20 | 4456 | if (btrfs_should_end_transaction(trans)) { |
28ed1345 CM |
4457 | err = -EAGAIN; |
4458 | goto error; | |
4459 | } | |
4460 | } | |
4461 | ||
4462 | ||
b9473439 | 4463 | path->leave_spinning = 1; |
85e21bac | 4464 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4465 | if (ret < 0) { |
4466 | err = ret; | |
4467 | goto out; | |
4468 | } | |
d397712b | 4469 | |
85e21bac | 4470 | if (ret > 0) { |
e02119d5 CM |
4471 | /* there are no items in the tree for us to truncate, we're |
4472 | * done | |
4473 | */ | |
8082510e YZ |
4474 | if (path->slots[0] == 0) |
4475 | goto out; | |
85e21bac CM |
4476 | path->slots[0]--; |
4477 | } | |
4478 | ||
d397712b | 4479 | while (1) { |
39279cc3 | 4480 | fi = NULL; |
5f39d397 CM |
4481 | leaf = path->nodes[0]; |
4482 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4483 | found_type = found_key.type; |
39279cc3 | 4484 | |
33345d01 | 4485 | if (found_key.objectid != ino) |
39279cc3 | 4486 | break; |
5f39d397 | 4487 | |
85e21bac | 4488 | if (found_type < min_type) |
39279cc3 CM |
4489 | break; |
4490 | ||
5f39d397 | 4491 | item_end = found_key.offset; |
39279cc3 | 4492 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4493 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4494 | struct btrfs_file_extent_item); |
179e29e4 CM |
4495 | extent_type = btrfs_file_extent_type(leaf, fi); |
4496 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4497 | item_end += |
db94535d | 4498 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4499 | |
4500 | trace_btrfs_truncate_show_fi_regular( | |
4501 | BTRFS_I(inode), leaf, fi, | |
4502 | found_key.offset); | |
179e29e4 | 4503 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4504 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4505 | path->slots[0], fi); |
09ed2f16 LB |
4506 | |
4507 | trace_btrfs_truncate_show_fi_inline( | |
4508 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4509 | found_key.offset); | |
39279cc3 | 4510 | } |
008630c1 | 4511 | item_end--; |
39279cc3 | 4512 | } |
8082510e YZ |
4513 | if (found_type > min_type) { |
4514 | del_item = 1; | |
4515 | } else { | |
76b42abb | 4516 | if (item_end < new_size) |
b888db2b | 4517 | break; |
8082510e YZ |
4518 | if (found_key.offset >= new_size) |
4519 | del_item = 1; | |
4520 | else | |
4521 | del_item = 0; | |
39279cc3 | 4522 | } |
39279cc3 | 4523 | found_extent = 0; |
39279cc3 | 4524 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4525 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4526 | goto delete; | |
4527 | ||
4528 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 4529 | u64 num_dec; |
db94535d | 4530 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4531 | if (!del_item) { |
db94535d CM |
4532 | u64 orig_num_bytes = |
4533 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4534 | extent_num_bytes = ALIGN(new_size - |
4535 | found_key.offset, | |
0b246afa | 4536 | fs_info->sectorsize); |
db94535d CM |
4537 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4538 | extent_num_bytes); | |
4539 | num_dec = (orig_num_bytes - | |
9069218d | 4540 | extent_num_bytes); |
27cdeb70 MX |
4541 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4542 | &root->state) && | |
4543 | extent_start != 0) | |
a76a3cd4 | 4544 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4545 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4546 | } else { |
db94535d CM |
4547 | extent_num_bytes = |
4548 | btrfs_file_extent_disk_num_bytes(leaf, | |
4549 | fi); | |
5d4f98a2 YZ |
4550 | extent_offset = found_key.offset - |
4551 | btrfs_file_extent_offset(leaf, fi); | |
4552 | ||
39279cc3 | 4553 | /* FIXME blocksize != 4096 */ |
9069218d | 4554 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4555 | if (extent_start != 0) { |
4556 | found_extent = 1; | |
27cdeb70 MX |
4557 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4558 | &root->state)) | |
a76a3cd4 | 4559 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4560 | } |
39279cc3 | 4561 | } |
9069218d | 4562 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4563 | /* |
4564 | * we can't truncate inline items that have had | |
4565 | * special encodings | |
4566 | */ | |
4567 | if (!del_item && | |
c8b97818 | 4568 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
ddfae63c JB |
4569 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
4570 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
4571 | u32 size = (u32)(new_size - found_key.offset); | |
4572 | ||
4573 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4574 | size = btrfs_file_extent_calc_inline_size(size); | |
4575 | btrfs_truncate_item(root->fs_info, path, size, 1); | |
4576 | } else if (!del_item) { | |
514ac8ad | 4577 | /* |
ddfae63c JB |
4578 | * We have to bail so the last_size is set to |
4579 | * just before this extent. | |
514ac8ad | 4580 | */ |
ddfae63c JB |
4581 | err = NEED_TRUNCATE_BLOCK; |
4582 | break; | |
4583 | } | |
0305cd5f | 4584 | |
ddfae63c | 4585 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
0305cd5f | 4586 | inode_sub_bytes(inode, item_end + 1 - new_size); |
39279cc3 | 4587 | } |
179e29e4 | 4588 | delete: |
ddfae63c JB |
4589 | if (del_item) |
4590 | last_size = found_key.offset; | |
4591 | else | |
4592 | last_size = new_size; | |
39279cc3 | 4593 | if (del_item) { |
85e21bac CM |
4594 | if (!pending_del_nr) { |
4595 | /* no pending yet, add ourselves */ | |
4596 | pending_del_slot = path->slots[0]; | |
4597 | pending_del_nr = 1; | |
4598 | } else if (pending_del_nr && | |
4599 | path->slots[0] + 1 == pending_del_slot) { | |
4600 | /* hop on the pending chunk */ | |
4601 | pending_del_nr++; | |
4602 | pending_del_slot = path->slots[0]; | |
4603 | } else { | |
d397712b | 4604 | BUG(); |
85e21bac | 4605 | } |
39279cc3 CM |
4606 | } else { |
4607 | break; | |
4608 | } | |
897ca819 | 4609 | should_throttle = false; |
28f75a0e | 4610 | |
27cdeb70 MX |
4611 | if (found_extent && |
4612 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4613 | root == fs_info->tree_root)) { |
b9473439 | 4614 | btrfs_set_path_blocking(path); |
28ed1345 | 4615 | bytes_deleted += extent_num_bytes; |
84f7d8e6 | 4616 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4617 | extent_num_bytes, 0, |
4618 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4619 | ino, extent_offset); |
39279cc3 | 4620 | BUG_ON(ret); |
2ff7e61e JM |
4621 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
4622 | btrfs_async_run_delayed_refs(fs_info, | |
dd4b857a WX |
4623 | trans->delayed_ref_updates * 2, |
4624 | trans->transid, 0); | |
28f75a0e CM |
4625 | if (be_nice) { |
4626 | if (truncate_space_check(trans, root, | |
4627 | extent_num_bytes)) { | |
897ca819 | 4628 | should_end = true; |
28f75a0e CM |
4629 | } |
4630 | if (btrfs_should_throttle_delayed_refs(trans, | |
2ff7e61e | 4631 | fs_info)) |
897ca819 | 4632 | should_throttle = true; |
28f75a0e | 4633 | } |
39279cc3 | 4634 | } |
85e21bac | 4635 | |
8082510e YZ |
4636 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4637 | break; | |
4638 | ||
4639 | if (path->slots[0] == 0 || | |
1262133b | 4640 | path->slots[0] != pending_del_slot || |
28f75a0e | 4641 | should_throttle || should_end) { |
8082510e YZ |
4642 | if (pending_del_nr) { |
4643 | ret = btrfs_del_items(trans, root, path, | |
4644 | pending_del_slot, | |
4645 | pending_del_nr); | |
79787eaa | 4646 | if (ret) { |
66642832 | 4647 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4648 | goto error; |
4649 | } | |
8082510e YZ |
4650 | pending_del_nr = 0; |
4651 | } | |
b3b4aa74 | 4652 | btrfs_release_path(path); |
28f75a0e | 4653 | if (should_throttle) { |
1262133b JB |
4654 | unsigned long updates = trans->delayed_ref_updates; |
4655 | if (updates) { | |
4656 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4657 | ret = btrfs_run_delayed_refs(trans, |
4658 | fs_info, | |
4659 | updates * 2); | |
1262133b JB |
4660 | if (ret && !err) |
4661 | err = ret; | |
4662 | } | |
4663 | } | |
28f75a0e CM |
4664 | /* |
4665 | * if we failed to refill our space rsv, bail out | |
4666 | * and let the transaction restart | |
4667 | */ | |
4668 | if (should_end) { | |
4669 | err = -EAGAIN; | |
4670 | goto error; | |
4671 | } | |
85e21bac | 4672 | goto search_again; |
8082510e YZ |
4673 | } else { |
4674 | path->slots[0]--; | |
85e21bac | 4675 | } |
39279cc3 | 4676 | } |
8082510e | 4677 | out: |
85e21bac CM |
4678 | if (pending_del_nr) { |
4679 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4680 | pending_del_nr); | |
79787eaa | 4681 | if (ret) |
66642832 | 4682 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4683 | } |
79787eaa | 4684 | error: |
76b42abb FM |
4685 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4686 | ASSERT(last_size >= new_size); | |
4687 | if (!err && last_size > new_size) | |
4688 | last_size = new_size; | |
7f4f6e0a | 4689 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4690 | } |
28ed1345 | 4691 | |
39279cc3 | 4692 | btrfs_free_path(path); |
28ed1345 | 4693 | |
ee22184b | 4694 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4695 | unsigned long updates = trans->delayed_ref_updates; |
4696 | if (updates) { | |
4697 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4698 | ret = btrfs_run_delayed_refs(trans, fs_info, |
4699 | updates * 2); | |
28ed1345 CM |
4700 | if (ret && !err) |
4701 | err = ret; | |
4702 | } | |
4703 | } | |
8082510e | 4704 | return err; |
39279cc3 CM |
4705 | } |
4706 | ||
4707 | /* | |
9703fefe | 4708 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4709 | * @inode - inode that we're zeroing |
4710 | * @from - the offset to start zeroing | |
4711 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4712 | * offset | |
4713 | * @front - zero up to the offset instead of from the offset on | |
4714 | * | |
9703fefe | 4715 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4716 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4717 | */ |
9703fefe | 4718 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4719 | int front) |
39279cc3 | 4720 | { |
0b246afa | 4721 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4722 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4723 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4724 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4725 | struct extent_state *cached_state = NULL; |
364ecf36 | 4726 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4727 | char *kaddr; |
0b246afa | 4728 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4729 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4730 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4731 | struct page *page; |
3b16a4e3 | 4732 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4733 | int ret = 0; |
9703fefe CR |
4734 | u64 block_start; |
4735 | u64 block_end; | |
39279cc3 | 4736 | |
2aaa6655 JB |
4737 | if ((offset & (blocksize - 1)) == 0 && |
4738 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4739 | goto out; |
9703fefe | 4740 | |
8b62f87b JB |
4741 | block_start = round_down(from, blocksize); |
4742 | block_end = block_start + blocksize - 1; | |
4743 | ||
364ecf36 | 4744 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8b62f87b | 4745 | block_start, blocksize); |
5d5e103a JB |
4746 | if (ret) |
4747 | goto out; | |
39279cc3 | 4748 | |
211c17f5 | 4749 | again: |
3b16a4e3 | 4750 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4751 | if (!page) { |
bc42bda2 | 4752 | btrfs_delalloc_release_space(inode, data_reserved, |
8b62f87b JB |
4753 | block_start, blocksize); |
4754 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); | |
ac6a2b36 | 4755 | ret = -ENOMEM; |
39279cc3 | 4756 | goto out; |
5d5e103a | 4757 | } |
e6dcd2dc | 4758 | |
39279cc3 | 4759 | if (!PageUptodate(page)) { |
9ebefb18 | 4760 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4761 | lock_page(page); |
211c17f5 CM |
4762 | if (page->mapping != mapping) { |
4763 | unlock_page(page); | |
09cbfeaf | 4764 | put_page(page); |
211c17f5 CM |
4765 | goto again; |
4766 | } | |
39279cc3 CM |
4767 | if (!PageUptodate(page)) { |
4768 | ret = -EIO; | |
89642229 | 4769 | goto out_unlock; |
39279cc3 CM |
4770 | } |
4771 | } | |
211c17f5 | 4772 | wait_on_page_writeback(page); |
e6dcd2dc | 4773 | |
9703fefe | 4774 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4775 | set_page_extent_mapped(page); |
4776 | ||
9703fefe | 4777 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4778 | if (ordered) { |
9703fefe | 4779 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4780 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4781 | unlock_page(page); |
09cbfeaf | 4782 | put_page(page); |
eb84ae03 | 4783 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4784 | btrfs_put_ordered_extent(ordered); |
4785 | goto again; | |
4786 | } | |
4787 | ||
9703fefe | 4788 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4789 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4790 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4791 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4792 | |
9703fefe | 4793 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4794 | &cached_state, 0); |
9ed74f2d | 4795 | if (ret) { |
9703fefe | 4796 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4797 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4798 | goto out_unlock; |
4799 | } | |
4800 | ||
9703fefe | 4801 | if (offset != blocksize) { |
2aaa6655 | 4802 | if (!len) |
9703fefe | 4803 | len = blocksize - offset; |
e6dcd2dc | 4804 | kaddr = kmap(page); |
2aaa6655 | 4805 | if (front) |
9703fefe CR |
4806 | memset(kaddr + (block_start - page_offset(page)), |
4807 | 0, offset); | |
2aaa6655 | 4808 | else |
9703fefe CR |
4809 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4810 | 0, len); | |
e6dcd2dc CM |
4811 | flush_dcache_page(page); |
4812 | kunmap(page); | |
4813 | } | |
247e743c | 4814 | ClearPageChecked(page); |
e6dcd2dc | 4815 | set_page_dirty(page); |
9703fefe | 4816 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4817 | GFP_NOFS); |
39279cc3 | 4818 | |
89642229 | 4819 | out_unlock: |
5d5e103a | 4820 | if (ret) |
bc42bda2 | 4821 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
9703fefe | 4822 | blocksize); |
8b62f87b | 4823 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
39279cc3 | 4824 | unlock_page(page); |
09cbfeaf | 4825 | put_page(page); |
39279cc3 | 4826 | out: |
364ecf36 | 4827 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4828 | return ret; |
4829 | } | |
4830 | ||
16e7549f JB |
4831 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4832 | u64 offset, u64 len) | |
4833 | { | |
0b246afa | 4834 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4835 | struct btrfs_trans_handle *trans; |
4836 | int ret; | |
4837 | ||
4838 | /* | |
4839 | * Still need to make sure the inode looks like it's been updated so | |
4840 | * that any holes get logged if we fsync. | |
4841 | */ | |
0b246afa JM |
4842 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4843 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4844 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4845 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4846 | return 0; | |
4847 | } | |
4848 | ||
4849 | /* | |
4850 | * 1 - for the one we're dropping | |
4851 | * 1 - for the one we're adding | |
4852 | * 1 - for updating the inode. | |
4853 | */ | |
4854 | trans = btrfs_start_transaction(root, 3); | |
4855 | if (IS_ERR(trans)) | |
4856 | return PTR_ERR(trans); | |
4857 | ||
4858 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4859 | if (ret) { | |
66642832 | 4860 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4861 | btrfs_end_transaction(trans); |
16e7549f JB |
4862 | return ret; |
4863 | } | |
4864 | ||
f85b7379 DS |
4865 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4866 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4867 | if (ret) |
66642832 | 4868 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4869 | else |
4870 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4871 | btrfs_end_transaction(trans); |
16e7549f JB |
4872 | return ret; |
4873 | } | |
4874 | ||
695a0d0d JB |
4875 | /* |
4876 | * This function puts in dummy file extents for the area we're creating a hole | |
4877 | * for. So if we are truncating this file to a larger size we need to insert | |
4878 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4879 | * the range between oldsize and size | |
4880 | */ | |
a41ad394 | 4881 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4882 | { |
0b246afa | 4883 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4884 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4885 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4886 | struct extent_map *em = NULL; |
2ac55d41 | 4887 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4888 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4889 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4890 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4891 | u64 last_byte; |
4892 | u64 cur_offset; | |
4893 | u64 hole_size; | |
9ed74f2d | 4894 | int err = 0; |
39279cc3 | 4895 | |
a71754fc | 4896 | /* |
9703fefe CR |
4897 | * If our size started in the middle of a block we need to zero out the |
4898 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4899 | * expose stale data. |
4900 | */ | |
9703fefe | 4901 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4902 | if (err) |
4903 | return err; | |
4904 | ||
9036c102 YZ |
4905 | if (size <= hole_start) |
4906 | return 0; | |
4907 | ||
9036c102 YZ |
4908 | while (1) { |
4909 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4910 | |
ff13db41 | 4911 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4912 | &cached_state); |
a776c6fa | 4913 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
fa7c1494 | 4914 | block_end - hole_start); |
9036c102 YZ |
4915 | if (!ordered) |
4916 | break; | |
2ac55d41 JB |
4917 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4918 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4919 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4920 | btrfs_put_ordered_extent(ordered); |
4921 | } | |
39279cc3 | 4922 | |
9036c102 YZ |
4923 | cur_offset = hole_start; |
4924 | while (1) { | |
fc4f21b1 | 4925 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 4926 | block_end - cur_offset, 0); |
79787eaa JM |
4927 | if (IS_ERR(em)) { |
4928 | err = PTR_ERR(em); | |
f2767956 | 4929 | em = NULL; |
79787eaa JM |
4930 | break; |
4931 | } | |
9036c102 | 4932 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4933 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 4934 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4935 | struct extent_map *hole_em; |
9036c102 | 4936 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4937 | |
16e7549f JB |
4938 | err = maybe_insert_hole(root, inode, cur_offset, |
4939 | hole_size); | |
4940 | if (err) | |
3893e33b | 4941 | break; |
dcdbc059 | 4942 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4943 | cur_offset + hole_size - 1, 0); |
4944 | hole_em = alloc_extent_map(); | |
4945 | if (!hole_em) { | |
4946 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4947 | &BTRFS_I(inode)->runtime_flags); | |
4948 | goto next; | |
4949 | } | |
4950 | hole_em->start = cur_offset; | |
4951 | hole_em->len = hole_size; | |
4952 | hole_em->orig_start = cur_offset; | |
8082510e | 4953 | |
5dc562c5 JB |
4954 | hole_em->block_start = EXTENT_MAP_HOLE; |
4955 | hole_em->block_len = 0; | |
b4939680 | 4956 | hole_em->orig_block_len = 0; |
cc95bef6 | 4957 | hole_em->ram_bytes = hole_size; |
0b246afa | 4958 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 4959 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4960 | hole_em->generation = fs_info->generation; |
8082510e | 4961 | |
5dc562c5 JB |
4962 | while (1) { |
4963 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4964 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4965 | write_unlock(&em_tree->lock); |
4966 | if (err != -EEXIST) | |
4967 | break; | |
dcdbc059 NB |
4968 | btrfs_drop_extent_cache(BTRFS_I(inode), |
4969 | cur_offset, | |
5dc562c5 JB |
4970 | cur_offset + |
4971 | hole_size - 1, 0); | |
4972 | } | |
4973 | free_extent_map(hole_em); | |
9036c102 | 4974 | } |
16e7549f | 4975 | next: |
9036c102 | 4976 | free_extent_map(em); |
a22285a6 | 4977 | em = NULL; |
9036c102 | 4978 | cur_offset = last_byte; |
8082510e | 4979 | if (cur_offset >= block_end) |
9036c102 YZ |
4980 | break; |
4981 | } | |
a22285a6 | 4982 | free_extent_map(em); |
2ac55d41 JB |
4983 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4984 | GFP_NOFS); | |
9036c102 YZ |
4985 | return err; |
4986 | } | |
39279cc3 | 4987 | |
3972f260 | 4988 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4989 | { |
f4a2f4c5 MX |
4990 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4991 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4992 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4993 | loff_t newsize = attr->ia_size; |
4994 | int mask = attr->ia_valid; | |
8082510e YZ |
4995 | int ret; |
4996 | ||
3972f260 ES |
4997 | /* |
4998 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4999 | * special case where we need to update the times despite not having | |
5000 | * these flags set. For all other operations the VFS set these flags | |
5001 | * explicitly if it wants a timestamp update. | |
5002 | */ | |
dff6efc3 CH |
5003 | if (newsize != oldsize) { |
5004 | inode_inc_iversion(inode); | |
5005 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5006 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5007 | current_time(inode); |
dff6efc3 | 5008 | } |
3972f260 | 5009 | |
a41ad394 | 5010 | if (newsize > oldsize) { |
9ea24bbe | 5011 | /* |
ea14b57f | 5012 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5013 | * This is to ensure the snapshot captures a fully consistent |
5014 | * state of this file - if the snapshot captures this expanding | |
5015 | * truncation, it must capture all writes that happened before | |
5016 | * this truncation. | |
5017 | */ | |
0bc19f90 | 5018 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5019 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 5020 | if (ret) { |
ea14b57f | 5021 | btrfs_end_write_no_snapshotting(root); |
8082510e | 5022 | return ret; |
9ea24bbe | 5023 | } |
8082510e | 5024 | |
f4a2f4c5 | 5025 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5026 | if (IS_ERR(trans)) { |
ea14b57f | 5027 | btrfs_end_write_no_snapshotting(root); |
f4a2f4c5 | 5028 | return PTR_ERR(trans); |
9ea24bbe | 5029 | } |
f4a2f4c5 MX |
5030 | |
5031 | i_size_write(inode, newsize); | |
5032 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5033 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5034 | ret = btrfs_update_inode(trans, root, inode); |
ea14b57f | 5035 | btrfs_end_write_no_snapshotting(root); |
3a45bb20 | 5036 | btrfs_end_transaction(trans); |
a41ad394 | 5037 | } else { |
8082510e | 5038 | |
a41ad394 JB |
5039 | /* |
5040 | * We're truncating a file that used to have good data down to | |
5041 | * zero. Make sure it gets into the ordered flush list so that | |
5042 | * any new writes get down to disk quickly. | |
5043 | */ | |
5044 | if (newsize == 0) | |
72ac3c0d JB |
5045 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5046 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5047 | |
f3fe820c JB |
5048 | /* |
5049 | * 1 for the orphan item we're going to add | |
5050 | * 1 for the orphan item deletion. | |
5051 | */ | |
5052 | trans = btrfs_start_transaction(root, 2); | |
5053 | if (IS_ERR(trans)) | |
5054 | return PTR_ERR(trans); | |
5055 | ||
5056 | /* | |
5057 | * We need to do this in case we fail at _any_ point during the | |
5058 | * actual truncate. Once we do the truncate_setsize we could | |
5059 | * invalidate pages which forces any outstanding ordered io to | |
5060 | * be instantly completed which will give us extents that need | |
5061 | * to be truncated. If we fail to get an orphan inode down we | |
5062 | * could have left over extents that were never meant to live, | |
01327610 | 5063 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5064 | * will be consistent. |
5065 | */ | |
73f2e545 | 5066 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 5067 | btrfs_end_transaction(trans); |
f3fe820c JB |
5068 | if (ret) |
5069 | return ret; | |
5070 | ||
a41ad394 JB |
5071 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5072 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5073 | |
5074 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
abcefb1e | 5075 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5076 | inode_dio_wait(inode); |
0b581701 | 5077 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5078 | |
a41ad394 | 5079 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5080 | if (ret && inode->i_nlink) { |
5081 | int err; | |
5082 | ||
19fd2df5 LB |
5083 | /* To get a stable disk_i_size */ |
5084 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5085 | if (err) { | |
3d6ae7bb | 5086 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
19fd2df5 LB |
5087 | return err; |
5088 | } | |
5089 | ||
7f4f6e0a JB |
5090 | /* |
5091 | * failed to truncate, disk_i_size is only adjusted down | |
5092 | * as we remove extents, so it should represent the true | |
5093 | * size of the inode, so reset the in memory size and | |
5094 | * delete our orphan entry. | |
5095 | */ | |
5096 | trans = btrfs_join_transaction(root); | |
5097 | if (IS_ERR(trans)) { | |
3d6ae7bb | 5098 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7f4f6e0a JB |
5099 | return ret; |
5100 | } | |
5101 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
3d6ae7bb | 5102 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
7f4f6e0a | 5103 | if (err) |
66642832 | 5104 | btrfs_abort_transaction(trans, err); |
3a45bb20 | 5105 | btrfs_end_transaction(trans); |
7f4f6e0a | 5106 | } |
8082510e YZ |
5107 | } |
5108 | ||
a41ad394 | 5109 | return ret; |
8082510e YZ |
5110 | } |
5111 | ||
9036c102 YZ |
5112 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5113 | { | |
2b0143b5 | 5114 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5115 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5116 | int err; |
39279cc3 | 5117 | |
b83cc969 LZ |
5118 | if (btrfs_root_readonly(root)) |
5119 | return -EROFS; | |
5120 | ||
31051c85 | 5121 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5122 | if (err) |
5123 | return err; | |
2bf5a725 | 5124 | |
5a3f23d5 | 5125 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5126 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5127 | if (err) |
5128 | return err; | |
39279cc3 | 5129 | } |
9036c102 | 5130 | |
1025774c CH |
5131 | if (attr->ia_valid) { |
5132 | setattr_copy(inode, attr); | |
0c4d2d95 | 5133 | inode_inc_iversion(inode); |
22c44fe6 | 5134 | err = btrfs_dirty_inode(inode); |
1025774c | 5135 | |
22c44fe6 | 5136 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5137 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5138 | } |
33268eaf | 5139 | |
39279cc3 CM |
5140 | return err; |
5141 | } | |
61295eb8 | 5142 | |
131e404a FDBM |
5143 | /* |
5144 | * While truncating the inode pages during eviction, we get the VFS calling | |
5145 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5146 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5147 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5148 | * extent_state structures over and over, wasting lots of time. | |
5149 | * | |
5150 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5151 | * those expensive operations on a per page basis and do only the ordered io | |
5152 | * finishing, while we release here the extent_map and extent_state structures, | |
5153 | * without the excessive merging and splitting. | |
5154 | */ | |
5155 | static void evict_inode_truncate_pages(struct inode *inode) | |
5156 | { | |
5157 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5158 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5159 | struct rb_node *node; | |
5160 | ||
5161 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5162 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5163 | |
5164 | write_lock(&map_tree->lock); | |
5165 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5166 | struct extent_map *em; | |
5167 | ||
5168 | node = rb_first(&map_tree->map); | |
5169 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5170 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5171 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5172 | remove_extent_mapping(map_tree, em); |
5173 | free_extent_map(em); | |
7064dd5c FM |
5174 | if (need_resched()) { |
5175 | write_unlock(&map_tree->lock); | |
5176 | cond_resched(); | |
5177 | write_lock(&map_tree->lock); | |
5178 | } | |
131e404a FDBM |
5179 | } |
5180 | write_unlock(&map_tree->lock); | |
5181 | ||
6ca07097 FM |
5182 | /* |
5183 | * Keep looping until we have no more ranges in the io tree. | |
5184 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5185 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5186 | * still in progress (unlocked the pages in the bio but did not yet | |
5187 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5188 | * ranges can still be locked and eviction started because before |
5189 | * submitting those bios, which are executed by a separate task (work | |
5190 | * queue kthread), inode references (inode->i_count) were not taken | |
5191 | * (which would be dropped in the end io callback of each bio). | |
5192 | * Therefore here we effectively end up waiting for those bios and | |
5193 | * anyone else holding locked ranges without having bumped the inode's | |
5194 | * reference count - if we don't do it, when they access the inode's | |
5195 | * io_tree to unlock a range it may be too late, leading to an | |
5196 | * use-after-free issue. | |
5197 | */ | |
131e404a FDBM |
5198 | spin_lock(&io_tree->lock); |
5199 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5200 | struct extent_state *state; | |
5201 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5202 | u64 start; |
5203 | u64 end; | |
131e404a FDBM |
5204 | |
5205 | node = rb_first(&io_tree->state); | |
5206 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5207 | start = state->start; |
5208 | end = state->end; | |
131e404a FDBM |
5209 | spin_unlock(&io_tree->lock); |
5210 | ||
ff13db41 | 5211 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5212 | |
5213 | /* | |
5214 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5215 | * and its reserved space won't be freed by delayed_ref. | |
5216 | * So we need to free its reserved space here. | |
5217 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5218 | * | |
5219 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5220 | */ | |
5221 | if (state->state & EXTENT_DELALLOC) | |
bc42bda2 | 5222 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
b9d0b389 | 5223 | |
6ca07097 | 5224 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5225 | EXTENT_LOCKED | EXTENT_DIRTY | |
5226 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5227 | EXTENT_DEFRAG, 1, 1, | |
5228 | &cached_state, GFP_NOFS); | |
131e404a | 5229 | |
7064dd5c | 5230 | cond_resched(); |
131e404a FDBM |
5231 | spin_lock(&io_tree->lock); |
5232 | } | |
5233 | spin_unlock(&io_tree->lock); | |
5234 | } | |
5235 | ||
bd555975 | 5236 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5237 | { |
0b246afa | 5238 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5239 | struct btrfs_trans_handle *trans; |
5240 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5241 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5242 | int steal_from_global = 0; |
3d48d981 | 5243 | u64 min_size; |
39279cc3 CM |
5244 | int ret; |
5245 | ||
1abe9b8a | 5246 | trace_btrfs_inode_evict(inode); |
5247 | ||
3d48d981 NB |
5248 | if (!root) { |
5249 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5250 | return; | |
5251 | } | |
5252 | ||
0b246afa | 5253 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
3d48d981 | 5254 | |
131e404a FDBM |
5255 | evict_inode_truncate_pages(inode); |
5256 | ||
69e9c6c6 SB |
5257 | if (inode->i_nlink && |
5258 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5259 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5260 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5261 | goto no_delete; |
5262 | ||
39279cc3 | 5263 | if (is_bad_inode(inode)) { |
3d6ae7bb | 5264 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
39279cc3 CM |
5265 | goto no_delete; |
5266 | } | |
bd555975 | 5267 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5268 | if (!special_file(inode->i_mode)) |
5269 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5270 | |
7ab7956e | 5271 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5272 | |
0b246afa | 5273 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
6bf02314 | 5274 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5275 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5276 | goto no_delete; |
5277 | } | |
5278 | ||
76dda93c | 5279 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5280 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5281 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5282 | goto no_delete; |
5283 | } | |
5284 | ||
aa79021f | 5285 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
0e8c36a9 | 5286 | if (ret) { |
3d6ae7bb | 5287 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
0e8c36a9 MX |
5288 | goto no_delete; |
5289 | } | |
5290 | ||
2ff7e61e | 5291 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
4289a667 | 5292 | if (!rsv) { |
3d6ae7bb | 5293 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
4289a667 JB |
5294 | goto no_delete; |
5295 | } | |
4a338542 | 5296 | rsv->size = min_size; |
ca7e70f5 | 5297 | rsv->failfast = 1; |
0b246afa | 5298 | global_rsv = &fs_info->global_block_rsv; |
4289a667 | 5299 | |
6ef06d27 | 5300 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5301 | |
4289a667 | 5302 | /* |
8407aa46 MX |
5303 | * This is a bit simpler than btrfs_truncate since we've already |
5304 | * reserved our space for our orphan item in the unlink, so we just | |
5305 | * need to reserve some slack space in case we add bytes and update | |
5306 | * inode item when doing the truncate. | |
4289a667 | 5307 | */ |
8082510e | 5308 | while (1) { |
08e007d2 MX |
5309 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5310 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5311 | |
5312 | /* | |
5313 | * Try and steal from the global reserve since we will | |
5314 | * likely not use this space anyway, we want to try as | |
5315 | * hard as possible to get this to work. | |
5316 | */ | |
5317 | if (ret) | |
3bce876f JB |
5318 | steal_from_global++; |
5319 | else | |
5320 | steal_from_global = 0; | |
5321 | ret = 0; | |
d68fc57b | 5322 | |
3bce876f JB |
5323 | /* |
5324 | * steal_from_global == 0: we reserved stuff, hooray! | |
5325 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5326 | * steal_from_global == 2: we've committed, still not a lot of | |
5327 | * room but maybe we'll have room in the global reserve this | |
5328 | * time. | |
5329 | * steal_from_global == 3: abandon all hope! | |
5330 | */ | |
5331 | if (steal_from_global > 2) { | |
0b246afa JM |
5332 | btrfs_warn(fs_info, |
5333 | "Could not get space for a delete, will truncate on mount %d", | |
5334 | ret); | |
3d6ae7bb | 5335 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5336 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5337 | goto no_delete; |
d68fc57b | 5338 | } |
7b128766 | 5339 | |
0e8c36a9 | 5340 | trans = btrfs_join_transaction(root); |
4289a667 | 5341 | if (IS_ERR(trans)) { |
3d6ae7bb | 5342 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5343 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5344 | goto no_delete; |
d68fc57b | 5345 | } |
7b128766 | 5346 | |
3bce876f | 5347 | /* |
01327610 | 5348 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5349 | * sure there is room to do it, if not we need to commit and try |
5350 | * again. | |
5351 | */ | |
5352 | if (steal_from_global) { | |
2ff7e61e | 5353 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
3bce876f | 5354 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
25d609f8 | 5355 | min_size, 0); |
3bce876f JB |
5356 | else |
5357 | ret = -ENOSPC; | |
5358 | } | |
5359 | ||
5360 | /* | |
5361 | * Couldn't steal from the global reserve, we have too much | |
5362 | * pending stuff built up, commit the transaction and try it | |
5363 | * again. | |
5364 | */ | |
5365 | if (ret) { | |
3a45bb20 | 5366 | ret = btrfs_commit_transaction(trans); |
3bce876f | 5367 | if (ret) { |
3d6ae7bb | 5368 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5369 | btrfs_free_block_rsv(fs_info, rsv); |
3bce876f JB |
5370 | goto no_delete; |
5371 | } | |
5372 | continue; | |
5373 | } else { | |
5374 | steal_from_global = 0; | |
5375 | } | |
5376 | ||
4289a667 JB |
5377 | trans->block_rsv = rsv; |
5378 | ||
d68fc57b | 5379 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5380 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5381 | break; |
85e21bac | 5382 | |
0b246afa | 5383 | trans->block_rsv = &fs_info->trans_block_rsv; |
3a45bb20 | 5384 | btrfs_end_transaction(trans); |
8082510e | 5385 | trans = NULL; |
2ff7e61e | 5386 | btrfs_btree_balance_dirty(fs_info); |
8082510e | 5387 | } |
5f39d397 | 5388 | |
2ff7e61e | 5389 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5390 | |
4ef31a45 JB |
5391 | /* |
5392 | * Errors here aren't a big deal, it just means we leave orphan items | |
5393 | * in the tree. They will be cleaned up on the next mount. | |
5394 | */ | |
8082510e | 5395 | if (ret == 0) { |
4289a667 | 5396 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 5397 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
4ef31a45 | 5398 | } else { |
3d6ae7bb | 5399 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
8082510e | 5400 | } |
54aa1f4d | 5401 | |
0b246afa JM |
5402 | trans->block_rsv = &fs_info->trans_block_rsv; |
5403 | if (!(root == fs_info->tree_root || | |
581bb050 | 5404 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5405 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5406 | |
3a45bb20 | 5407 | btrfs_end_transaction(trans); |
2ff7e61e | 5408 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 5409 | no_delete: |
f48d1cf5 | 5410 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5411 | clear_inode(inode); |
39279cc3 CM |
5412 | } |
5413 | ||
5414 | /* | |
5415 | * this returns the key found in the dir entry in the location pointer. | |
5416 | * If no dir entries were found, location->objectid is 0. | |
5417 | */ | |
5418 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5419 | struct btrfs_key *location) | |
5420 | { | |
5421 | const char *name = dentry->d_name.name; | |
5422 | int namelen = dentry->d_name.len; | |
5423 | struct btrfs_dir_item *di; | |
5424 | struct btrfs_path *path; | |
5425 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5426 | int ret = 0; |
39279cc3 CM |
5427 | |
5428 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5429 | if (!path) |
5430 | return -ENOMEM; | |
3954401f | 5431 | |
f85b7379 DS |
5432 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5433 | name, namelen, 0); | |
0d9f7f3e Y |
5434 | if (IS_ERR(di)) |
5435 | ret = PTR_ERR(di); | |
d397712b | 5436 | |
c704005d | 5437 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5438 | goto out_err; |
d397712b | 5439 | |
5f39d397 | 5440 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5441 | out: |
39279cc3 CM |
5442 | btrfs_free_path(path); |
5443 | return ret; | |
3954401f CM |
5444 | out_err: |
5445 | location->objectid = 0; | |
5446 | goto out; | |
39279cc3 CM |
5447 | } |
5448 | ||
5449 | /* | |
5450 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5451 | * needs to be changed to reflect the root directory of the tree root. This | |
5452 | * is kind of like crossing a mount point. | |
5453 | */ | |
2ff7e61e | 5454 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5455 | struct inode *dir, |
5456 | struct dentry *dentry, | |
5457 | struct btrfs_key *location, | |
5458 | struct btrfs_root **sub_root) | |
39279cc3 | 5459 | { |
4df27c4d YZ |
5460 | struct btrfs_path *path; |
5461 | struct btrfs_root *new_root; | |
5462 | struct btrfs_root_ref *ref; | |
5463 | struct extent_buffer *leaf; | |
1d4c08e0 | 5464 | struct btrfs_key key; |
4df27c4d YZ |
5465 | int ret; |
5466 | int err = 0; | |
39279cc3 | 5467 | |
4df27c4d YZ |
5468 | path = btrfs_alloc_path(); |
5469 | if (!path) { | |
5470 | err = -ENOMEM; | |
5471 | goto out; | |
5472 | } | |
39279cc3 | 5473 | |
4df27c4d | 5474 | err = -ENOENT; |
1d4c08e0 DS |
5475 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5476 | key.type = BTRFS_ROOT_REF_KEY; | |
5477 | key.offset = location->objectid; | |
5478 | ||
0b246afa | 5479 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5480 | if (ret) { |
5481 | if (ret < 0) | |
5482 | err = ret; | |
5483 | goto out; | |
5484 | } | |
39279cc3 | 5485 | |
4df27c4d YZ |
5486 | leaf = path->nodes[0]; |
5487 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5488 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5489 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5490 | goto out; | |
39279cc3 | 5491 | |
4df27c4d YZ |
5492 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5493 | (unsigned long)(ref + 1), | |
5494 | dentry->d_name.len); | |
5495 | if (ret) | |
5496 | goto out; | |
5497 | ||
b3b4aa74 | 5498 | btrfs_release_path(path); |
4df27c4d | 5499 | |
0b246afa | 5500 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5501 | if (IS_ERR(new_root)) { |
5502 | err = PTR_ERR(new_root); | |
5503 | goto out; | |
5504 | } | |
5505 | ||
4df27c4d YZ |
5506 | *sub_root = new_root; |
5507 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5508 | location->type = BTRFS_INODE_ITEM_KEY; | |
5509 | location->offset = 0; | |
5510 | err = 0; | |
5511 | out: | |
5512 | btrfs_free_path(path); | |
5513 | return err; | |
39279cc3 CM |
5514 | } |
5515 | ||
5d4f98a2 YZ |
5516 | static void inode_tree_add(struct inode *inode) |
5517 | { | |
5518 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5519 | struct btrfs_inode *entry; | |
03e860bd FNP |
5520 | struct rb_node **p; |
5521 | struct rb_node *parent; | |
cef21937 | 5522 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5523 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5524 | |
1d3382cb | 5525 | if (inode_unhashed(inode)) |
76dda93c | 5526 | return; |
e1409cef | 5527 | parent = NULL; |
5d4f98a2 | 5528 | spin_lock(&root->inode_lock); |
e1409cef | 5529 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5530 | while (*p) { |
5531 | parent = *p; | |
5532 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5533 | ||
4a0cc7ca | 5534 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5535 | p = &parent->rb_left; |
4a0cc7ca | 5536 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5537 | p = &parent->rb_right; |
5d4f98a2 YZ |
5538 | else { |
5539 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5540 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5541 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5542 | RB_CLEAR_NODE(parent); |
5543 | spin_unlock(&root->inode_lock); | |
cef21937 | 5544 | return; |
5d4f98a2 YZ |
5545 | } |
5546 | } | |
cef21937 FDBM |
5547 | rb_link_node(new, parent, p); |
5548 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5549 | spin_unlock(&root->inode_lock); |
5550 | } | |
5551 | ||
5552 | static void inode_tree_del(struct inode *inode) | |
5553 | { | |
0b246afa | 5554 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5555 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5556 | int empty = 0; |
5d4f98a2 | 5557 | |
03e860bd | 5558 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5559 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5560 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5561 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5562 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5563 | } |
03e860bd | 5564 | spin_unlock(&root->inode_lock); |
76dda93c | 5565 | |
69e9c6c6 | 5566 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5567 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5568 | spin_lock(&root->inode_lock); |
5569 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5570 | spin_unlock(&root->inode_lock); | |
5571 | if (empty) | |
5572 | btrfs_add_dead_root(root); | |
5573 | } | |
5574 | } | |
5575 | ||
143bede5 | 5576 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c | 5577 | { |
0b246afa | 5578 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c YZ |
5579 | struct rb_node *node; |
5580 | struct rb_node *prev; | |
5581 | struct btrfs_inode *entry; | |
5582 | struct inode *inode; | |
5583 | u64 objectid = 0; | |
5584 | ||
0b246afa | 5585 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
7813b3db | 5586 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
76dda93c YZ |
5587 | |
5588 | spin_lock(&root->inode_lock); | |
5589 | again: | |
5590 | node = root->inode_tree.rb_node; | |
5591 | prev = NULL; | |
5592 | while (node) { | |
5593 | prev = node; | |
5594 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5595 | ||
4a0cc7ca | 5596 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c | 5597 | node = node->rb_left; |
4a0cc7ca | 5598 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c YZ |
5599 | node = node->rb_right; |
5600 | else | |
5601 | break; | |
5602 | } | |
5603 | if (!node) { | |
5604 | while (prev) { | |
5605 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5606 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
76dda93c YZ |
5607 | node = prev; |
5608 | break; | |
5609 | } | |
5610 | prev = rb_next(prev); | |
5611 | } | |
5612 | } | |
5613 | while (node) { | |
5614 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5615 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
76dda93c YZ |
5616 | inode = igrab(&entry->vfs_inode); |
5617 | if (inode) { | |
5618 | spin_unlock(&root->inode_lock); | |
5619 | if (atomic_read(&inode->i_count) > 1) | |
5620 | d_prune_aliases(inode); | |
5621 | /* | |
45321ac5 | 5622 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5623 | * the inode cache when its usage count |
5624 | * hits zero. | |
5625 | */ | |
5626 | iput(inode); | |
5627 | cond_resched(); | |
5628 | spin_lock(&root->inode_lock); | |
5629 | goto again; | |
5630 | } | |
5631 | ||
5632 | if (cond_resched_lock(&root->inode_lock)) | |
5633 | goto again; | |
5634 | ||
5635 | node = rb_next(node); | |
5636 | } | |
5637 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5638 | } |
5639 | ||
e02119d5 CM |
5640 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5641 | { | |
5642 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5643 | inode->i_ino = args->location->objectid; |
5644 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5645 | sizeof(*args->location)); | |
e02119d5 | 5646 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5647 | return 0; |
5648 | } | |
5649 | ||
5650 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5651 | { | |
5652 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5653 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5654 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5655 | } |
5656 | ||
5d4f98a2 | 5657 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5658 | struct btrfs_key *location, |
5d4f98a2 | 5659 | struct btrfs_root *root) |
39279cc3 CM |
5660 | { |
5661 | struct inode *inode; | |
5662 | struct btrfs_iget_args args; | |
90d3e592 | 5663 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5664 | |
90d3e592 | 5665 | args.location = location; |
39279cc3 CM |
5666 | args.root = root; |
5667 | ||
778ba82b | 5668 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5669 | btrfs_init_locked_inode, |
5670 | (void *)&args); | |
5671 | return inode; | |
5672 | } | |
5673 | ||
1a54ef8c BR |
5674 | /* Get an inode object given its location and corresponding root. |
5675 | * Returns in *is_new if the inode was read from disk | |
5676 | */ | |
5677 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5678 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5679 | { |
5680 | struct inode *inode; | |
5681 | ||
90d3e592 | 5682 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5683 | if (!inode) |
5d4f98a2 | 5684 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5685 | |
5686 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5687 | int ret; |
5688 | ||
5689 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5690 | if (!is_bad_inode(inode)) { |
5691 | inode_tree_add(inode); | |
5692 | unlock_new_inode(inode); | |
5693 | if (new) | |
5694 | *new = 1; | |
5695 | } else { | |
e0b6d65b ST |
5696 | unlock_new_inode(inode); |
5697 | iput(inode); | |
67710892 FM |
5698 | ASSERT(ret < 0); |
5699 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5700 | } |
5701 | } | |
5702 | ||
1a54ef8c BR |
5703 | return inode; |
5704 | } | |
5705 | ||
4df27c4d YZ |
5706 | static struct inode *new_simple_dir(struct super_block *s, |
5707 | struct btrfs_key *key, | |
5708 | struct btrfs_root *root) | |
5709 | { | |
5710 | struct inode *inode = new_inode(s); | |
5711 | ||
5712 | if (!inode) | |
5713 | return ERR_PTR(-ENOMEM); | |
5714 | ||
4df27c4d YZ |
5715 | BTRFS_I(inode)->root = root; |
5716 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5717 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5718 | |
5719 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5720 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5721 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5722 | inode->i_fop = &simple_dir_operations; |
5723 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5724 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5725 | inode->i_atime = inode->i_mtime; |
5726 | inode->i_ctime = inode->i_mtime; | |
5727 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5728 | |
5729 | return inode; | |
5730 | } | |
5731 | ||
3de4586c | 5732 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5733 | { |
0b246afa | 5734 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5735 | struct inode *inode; |
4df27c4d | 5736 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5737 | struct btrfs_root *sub_root = root; |
5738 | struct btrfs_key location; | |
76dda93c | 5739 | int index; |
b4aff1f8 | 5740 | int ret = 0; |
39279cc3 CM |
5741 | |
5742 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5743 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5744 | |
39e3c955 | 5745 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5746 | if (ret < 0) |
5747 | return ERR_PTR(ret); | |
5f39d397 | 5748 | |
4df27c4d | 5749 | if (location.objectid == 0) |
5662344b | 5750 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5751 | |
5752 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5753 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5754 | return inode; |
5755 | } | |
5756 | ||
5757 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5758 | ||
0b246afa | 5759 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5760 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5761 | &location, &sub_root); |
5762 | if (ret < 0) { | |
5763 | if (ret != -ENOENT) | |
5764 | inode = ERR_PTR(ret); | |
5765 | else | |
5766 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5767 | } else { | |
73f73415 | 5768 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5769 | } |
0b246afa | 5770 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5771 | |
34d19bad | 5772 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5773 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5774 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5775 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5776 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5777 | if (ret) { |
5778 | iput(inode); | |
66b4ffd1 | 5779 | inode = ERR_PTR(ret); |
01cd3367 | 5780 | } |
c71bf099 YZ |
5781 | } |
5782 | ||
3de4586c CM |
5783 | return inode; |
5784 | } | |
5785 | ||
fe15ce44 | 5786 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5787 | { |
5788 | struct btrfs_root *root; | |
2b0143b5 | 5789 | struct inode *inode = d_inode(dentry); |
76dda93c | 5790 | |
848cce0d | 5791 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5792 | inode = d_inode(dentry->d_parent); |
76dda93c | 5793 | |
848cce0d LZ |
5794 | if (inode) { |
5795 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5796 | if (btrfs_root_refs(&root->root_item) == 0) |
5797 | return 1; | |
848cce0d | 5798 | |
4a0cc7ca | 5799 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5800 | return 1; |
efefb143 | 5801 | } |
76dda93c YZ |
5802 | return 0; |
5803 | } | |
5804 | ||
b4aff1f8 JB |
5805 | static void btrfs_dentry_release(struct dentry *dentry) |
5806 | { | |
944a4515 | 5807 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5808 | } |
5809 | ||
3de4586c | 5810 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5811 | unsigned int flags) |
3de4586c | 5812 | { |
5662344b | 5813 | struct inode *inode; |
a66e7cc6 | 5814 | |
5662344b TI |
5815 | inode = btrfs_lookup_dentry(dir, dentry); |
5816 | if (IS_ERR(inode)) { | |
5817 | if (PTR_ERR(inode) == -ENOENT) | |
5818 | inode = NULL; | |
5819 | else | |
5820 | return ERR_CAST(inode); | |
5821 | } | |
5822 | ||
41d28bca | 5823 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5824 | } |
5825 | ||
16cdcec7 | 5826 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5827 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5828 | }; | |
5829 | ||
23b5ec74 JB |
5830 | /* |
5831 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5832 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5833 | * our information into that, and then dir_emit from the buffer. This is | |
5834 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5835 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5836 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5837 | * tree lock. | |
5838 | */ | |
5839 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5840 | { | |
5841 | struct btrfs_file_private *private; | |
5842 | ||
5843 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5844 | if (!private) | |
5845 | return -ENOMEM; | |
5846 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5847 | if (!private->filldir_buf) { | |
5848 | kfree(private); | |
5849 | return -ENOMEM; | |
5850 | } | |
5851 | file->private_data = private; | |
5852 | return 0; | |
5853 | } | |
5854 | ||
5855 | struct dir_entry { | |
5856 | u64 ino; | |
5857 | u64 offset; | |
5858 | unsigned type; | |
5859 | int name_len; | |
5860 | }; | |
5861 | ||
5862 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5863 | { | |
5864 | while (entries--) { | |
5865 | struct dir_entry *entry = addr; | |
5866 | char *name = (char *)(entry + 1); | |
5867 | ||
5868 | ctx->pos = entry->offset; | |
5869 | if (!dir_emit(ctx, name, entry->name_len, entry->ino, | |
5870 | entry->type)) | |
5871 | return 1; | |
5872 | addr += sizeof(struct dir_entry) + entry->name_len; | |
5873 | ctx->pos++; | |
5874 | } | |
5875 | return 0; | |
5876 | } | |
5877 | ||
9cdda8d3 | 5878 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5879 | { |
9cdda8d3 | 5880 | struct inode *inode = file_inode(file); |
2ff7e61e | 5881 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 5882 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5883 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5884 | struct btrfs_dir_item *di; |
5885 | struct btrfs_key key; | |
5f39d397 | 5886 | struct btrfs_key found_key; |
39279cc3 | 5887 | struct btrfs_path *path; |
23b5ec74 | 5888 | void *addr; |
16cdcec7 MX |
5889 | struct list_head ins_list; |
5890 | struct list_head del_list; | |
39279cc3 | 5891 | int ret; |
5f39d397 | 5892 | struct extent_buffer *leaf; |
39279cc3 | 5893 | int slot; |
5f39d397 CM |
5894 | char *name_ptr; |
5895 | int name_len; | |
23b5ec74 JB |
5896 | int entries = 0; |
5897 | int total_len = 0; | |
02dbfc99 | 5898 | bool put = false; |
c2951f32 | 5899 | struct btrfs_key location; |
5f39d397 | 5900 | |
9cdda8d3 AV |
5901 | if (!dir_emit_dots(file, ctx)) |
5902 | return 0; | |
5903 | ||
49593bfa | 5904 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5905 | if (!path) |
5906 | return -ENOMEM; | |
ff5714cc | 5907 | |
23b5ec74 | 5908 | addr = private->filldir_buf; |
e4058b54 | 5909 | path->reada = READA_FORWARD; |
49593bfa | 5910 | |
c2951f32 JM |
5911 | INIT_LIST_HEAD(&ins_list); |
5912 | INIT_LIST_HEAD(&del_list); | |
5913 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5914 | |
23b5ec74 | 5915 | again: |
c2951f32 | 5916 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5917 | key.offset = ctx->pos; |
4a0cc7ca | 5918 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5919 | |
39279cc3 CM |
5920 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5921 | if (ret < 0) | |
5922 | goto err; | |
49593bfa DW |
5923 | |
5924 | while (1) { | |
23b5ec74 JB |
5925 | struct dir_entry *entry; |
5926 | ||
5f39d397 | 5927 | leaf = path->nodes[0]; |
39279cc3 | 5928 | slot = path->slots[0]; |
b9e03af0 LZ |
5929 | if (slot >= btrfs_header_nritems(leaf)) { |
5930 | ret = btrfs_next_leaf(root, path); | |
5931 | if (ret < 0) | |
5932 | goto err; | |
5933 | else if (ret > 0) | |
5934 | break; | |
5935 | continue; | |
39279cc3 | 5936 | } |
3de4586c | 5937 | |
5f39d397 CM |
5938 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5939 | ||
5940 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5941 | break; |
c2951f32 | 5942 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5943 | break; |
9cdda8d3 | 5944 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5945 | goto next; |
c2951f32 | 5946 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5947 | goto next; |
39279cc3 | 5948 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e79a3327 | 5949 | if (verify_dir_item(fs_info, leaf, slot, di)) |
c2951f32 | 5950 | goto next; |
22a94d44 | 5951 | |
c2951f32 | 5952 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
5953 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
5954 | PAGE_SIZE) { | |
5955 | btrfs_release_path(path); | |
5956 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5957 | if (ret) | |
5958 | goto nopos; | |
5959 | addr = private->filldir_buf; | |
5960 | entries = 0; | |
5961 | total_len = 0; | |
5962 | goto again; | |
c2951f32 | 5963 | } |
23b5ec74 JB |
5964 | |
5965 | entry = addr; | |
5966 | entry->name_len = name_len; | |
5967 | name_ptr = (char *)(entry + 1); | |
c2951f32 JM |
5968 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
5969 | name_len); | |
23b5ec74 | 5970 | entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
c2951f32 | 5971 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
23b5ec74 JB |
5972 | entry->ino = location.objectid; |
5973 | entry->offset = found_key.offset; | |
5974 | entries++; | |
5975 | addr += sizeof(struct dir_entry) + name_len; | |
5976 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
5977 | next: |
5978 | path->slots[0]++; | |
39279cc3 | 5979 | } |
23b5ec74 JB |
5980 | btrfs_release_path(path); |
5981 | ||
5982 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
5983 | if (ret) | |
5984 | goto nopos; | |
49593bfa | 5985 | |
d2fbb2b5 | 5986 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5987 | if (ret) |
bc4ef759 DS |
5988 | goto nopos; |
5989 | ||
db62efbb ZB |
5990 | /* |
5991 | * Stop new entries from being returned after we return the last | |
5992 | * entry. | |
5993 | * | |
5994 | * New directory entries are assigned a strictly increasing | |
5995 | * offset. This means that new entries created during readdir | |
5996 | * are *guaranteed* to be seen in the future by that readdir. | |
5997 | * This has broken buggy programs which operate on names as | |
5998 | * they're returned by readdir. Until we re-use freed offsets | |
5999 | * we have this hack to stop new entries from being returned | |
6000 | * under the assumption that they'll never reach this huge | |
6001 | * offset. | |
6002 | * | |
6003 | * This is being careful not to overflow 32bit loff_t unless the | |
6004 | * last entry requires it because doing so has broken 32bit apps | |
6005 | * in the past. | |
6006 | */ | |
c2951f32 JM |
6007 | if (ctx->pos >= INT_MAX) |
6008 | ctx->pos = LLONG_MAX; | |
6009 | else | |
6010 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6011 | nopos: |
6012 | ret = 0; | |
6013 | err: | |
02dbfc99 OS |
6014 | if (put) |
6015 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6016 | btrfs_free_path(path); |
39279cc3 CM |
6017 | return ret; |
6018 | } | |
6019 | ||
a9185b41 | 6020 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
6021 | { |
6022 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6023 | struct btrfs_trans_handle *trans; | |
6024 | int ret = 0; | |
0af3d00b | 6025 | bool nolock = false; |
39279cc3 | 6026 | |
72ac3c0d | 6027 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
6028 | return 0; |
6029 | ||
70ddc553 NB |
6030 | if (btrfs_fs_closing(root->fs_info) && |
6031 | btrfs_is_free_space_inode(BTRFS_I(inode))) | |
82d5902d | 6032 | nolock = true; |
0af3d00b | 6033 | |
a9185b41 | 6034 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 6035 | if (nolock) |
7a7eaa40 | 6036 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 6037 | else |
7a7eaa40 | 6038 | trans = btrfs_join_transaction(root); |
3612b495 TI |
6039 | if (IS_ERR(trans)) |
6040 | return PTR_ERR(trans); | |
3a45bb20 | 6041 | ret = btrfs_commit_transaction(trans); |
39279cc3 CM |
6042 | } |
6043 | return ret; | |
6044 | } | |
6045 | ||
6046 | /* | |
54aa1f4d | 6047 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6048 | * inode changes. But, it is most likely to find the inode in cache. |
6049 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6050 | * to keep or drop this code. | |
6051 | */ | |
48a3b636 | 6052 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6053 | { |
2ff7e61e | 6054 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6056 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6057 | int ret; |
6058 | ||
72ac3c0d | 6059 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6060 | return 0; |
39279cc3 | 6061 | |
7a7eaa40 | 6062 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6063 | if (IS_ERR(trans)) |
6064 | return PTR_ERR(trans); | |
8929ecfa YZ |
6065 | |
6066 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6067 | if (ret && ret == -ENOSPC) { |
6068 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6069 | btrfs_end_transaction(trans); |
94b60442 | 6070 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6071 | if (IS_ERR(trans)) |
6072 | return PTR_ERR(trans); | |
8929ecfa | 6073 | |
94b60442 | 6074 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6075 | } |
3a45bb20 | 6076 | btrfs_end_transaction(trans); |
16cdcec7 | 6077 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6078 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6079 | |
6080 | return ret; | |
6081 | } | |
6082 | ||
6083 | /* | |
6084 | * This is a copy of file_update_time. We need this so we can return error on | |
6085 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6086 | */ | |
e41f941a JB |
6087 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6088 | int flags) | |
22c44fe6 | 6089 | { |
2bc55652 AB |
6090 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6091 | ||
6092 | if (btrfs_root_readonly(root)) | |
6093 | return -EROFS; | |
6094 | ||
e41f941a | 6095 | if (flags & S_VERSION) |
22c44fe6 | 6096 | inode_inc_iversion(inode); |
e41f941a JB |
6097 | if (flags & S_CTIME) |
6098 | inode->i_ctime = *now; | |
6099 | if (flags & S_MTIME) | |
6100 | inode->i_mtime = *now; | |
6101 | if (flags & S_ATIME) | |
6102 | inode->i_atime = *now; | |
6103 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6104 | } |
6105 | ||
d352ac68 CM |
6106 | /* |
6107 | * find the highest existing sequence number in a directory | |
6108 | * and then set the in-memory index_cnt variable to reflect | |
6109 | * free sequence numbers | |
6110 | */ | |
4c570655 | 6111 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6112 | { |
4c570655 | 6113 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6114 | struct btrfs_key key, found_key; |
6115 | struct btrfs_path *path; | |
6116 | struct extent_buffer *leaf; | |
6117 | int ret; | |
6118 | ||
4c570655 | 6119 | key.objectid = btrfs_ino(inode); |
962a298f | 6120 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6121 | key.offset = (u64)-1; |
6122 | ||
6123 | path = btrfs_alloc_path(); | |
6124 | if (!path) | |
6125 | return -ENOMEM; | |
6126 | ||
6127 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6128 | if (ret < 0) | |
6129 | goto out; | |
6130 | /* FIXME: we should be able to handle this */ | |
6131 | if (ret == 0) | |
6132 | goto out; | |
6133 | ret = 0; | |
6134 | ||
6135 | /* | |
6136 | * MAGIC NUMBER EXPLANATION: | |
6137 | * since we search a directory based on f_pos we have to start at 2 | |
6138 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6139 | * else has to start at 2 | |
6140 | */ | |
6141 | if (path->slots[0] == 0) { | |
4c570655 | 6142 | inode->index_cnt = 2; |
aec7477b JB |
6143 | goto out; |
6144 | } | |
6145 | ||
6146 | path->slots[0]--; | |
6147 | ||
6148 | leaf = path->nodes[0]; | |
6149 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6150 | ||
4c570655 | 6151 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6152 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6153 | inode->index_cnt = 2; |
aec7477b JB |
6154 | goto out; |
6155 | } | |
6156 | ||
4c570655 | 6157 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6158 | out: |
6159 | btrfs_free_path(path); | |
6160 | return ret; | |
6161 | } | |
6162 | ||
d352ac68 CM |
6163 | /* |
6164 | * helper to find a free sequence number in a given directory. This current | |
6165 | * code is very simple, later versions will do smarter things in the btree | |
6166 | */ | |
877574e2 | 6167 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6168 | { |
6169 | int ret = 0; | |
6170 | ||
877574e2 NB |
6171 | if (dir->index_cnt == (u64)-1) { |
6172 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6173 | if (ret) { |
6174 | ret = btrfs_set_inode_index_count(dir); | |
6175 | if (ret) | |
6176 | return ret; | |
6177 | } | |
aec7477b JB |
6178 | } |
6179 | ||
877574e2 NB |
6180 | *index = dir->index_cnt; |
6181 | dir->index_cnt++; | |
aec7477b JB |
6182 | |
6183 | return ret; | |
6184 | } | |
6185 | ||
b0d5d10f CM |
6186 | static int btrfs_insert_inode_locked(struct inode *inode) |
6187 | { | |
6188 | struct btrfs_iget_args args; | |
6189 | args.location = &BTRFS_I(inode)->location; | |
6190 | args.root = BTRFS_I(inode)->root; | |
6191 | ||
6192 | return insert_inode_locked4(inode, | |
6193 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6194 | btrfs_find_actor, &args); | |
6195 | } | |
6196 | ||
19aee8de AJ |
6197 | /* |
6198 | * Inherit flags from the parent inode. | |
6199 | * | |
6200 | * Currently only the compression flags and the cow flags are inherited. | |
6201 | */ | |
6202 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6203 | { | |
6204 | unsigned int flags; | |
6205 | ||
6206 | if (!dir) | |
6207 | return; | |
6208 | ||
6209 | flags = BTRFS_I(dir)->flags; | |
6210 | ||
6211 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6212 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6213 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6214 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6215 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6216 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6217 | } | |
6218 | ||
6219 | if (flags & BTRFS_INODE_NODATACOW) { | |
6220 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6221 | if (S_ISREG(inode->i_mode)) | |
6222 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6223 | } | |
6224 | ||
6225 | btrfs_update_iflags(inode); | |
6226 | } | |
6227 | ||
39279cc3 CM |
6228 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6229 | struct btrfs_root *root, | |
aec7477b | 6230 | struct inode *dir, |
9c58309d | 6231 | const char *name, int name_len, |
175a4eb7 AV |
6232 | u64 ref_objectid, u64 objectid, |
6233 | umode_t mode, u64 *index) | |
39279cc3 | 6234 | { |
0b246afa | 6235 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6236 | struct inode *inode; |
5f39d397 | 6237 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6238 | struct btrfs_key *location; |
5f39d397 | 6239 | struct btrfs_path *path; |
9c58309d CM |
6240 | struct btrfs_inode_ref *ref; |
6241 | struct btrfs_key key[2]; | |
6242 | u32 sizes[2]; | |
ef3b9af5 | 6243 | int nitems = name ? 2 : 1; |
9c58309d | 6244 | unsigned long ptr; |
39279cc3 | 6245 | int ret; |
39279cc3 | 6246 | |
5f39d397 | 6247 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6248 | if (!path) |
6249 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6250 | |
0b246afa | 6251 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6252 | if (!inode) { |
6253 | btrfs_free_path(path); | |
39279cc3 | 6254 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6255 | } |
39279cc3 | 6256 | |
5762b5c9 FM |
6257 | /* |
6258 | * O_TMPFILE, set link count to 0, so that after this point, | |
6259 | * we fill in an inode item with the correct link count. | |
6260 | */ | |
6261 | if (!name) | |
6262 | set_nlink(inode, 0); | |
6263 | ||
581bb050 LZ |
6264 | /* |
6265 | * we have to initialize this early, so we can reclaim the inode | |
6266 | * number if we fail afterwards in this function. | |
6267 | */ | |
6268 | inode->i_ino = objectid; | |
6269 | ||
ef3b9af5 | 6270 | if (dir && name) { |
1abe9b8a | 6271 | trace_btrfs_inode_request(dir); |
6272 | ||
877574e2 | 6273 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6274 | if (ret) { |
8fb27640 | 6275 | btrfs_free_path(path); |
09771430 | 6276 | iput(inode); |
aec7477b | 6277 | return ERR_PTR(ret); |
09771430 | 6278 | } |
ef3b9af5 FM |
6279 | } else if (dir) { |
6280 | *index = 0; | |
aec7477b JB |
6281 | } |
6282 | /* | |
6283 | * index_cnt is ignored for everything but a dir, | |
6284 | * btrfs_get_inode_index_count has an explanation for the magic | |
6285 | * number | |
6286 | */ | |
6287 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6288 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6289 | BTRFS_I(inode)->root = root; |
e02119d5 | 6290 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6291 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6292 | |
5dc562c5 JB |
6293 | /* |
6294 | * We could have gotten an inode number from somebody who was fsynced | |
6295 | * and then removed in this same transaction, so let's just set full | |
6296 | * sync since it will be a full sync anyway and this will blow away the | |
6297 | * old info in the log. | |
6298 | */ | |
6299 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6300 | ||
9c58309d | 6301 | key[0].objectid = objectid; |
962a298f | 6302 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6303 | key[0].offset = 0; |
6304 | ||
9c58309d | 6305 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6306 | |
6307 | if (name) { | |
6308 | /* | |
6309 | * Start new inodes with an inode_ref. This is slightly more | |
6310 | * efficient for small numbers of hard links since they will | |
6311 | * be packed into one item. Extended refs will kick in if we | |
6312 | * add more hard links than can fit in the ref item. | |
6313 | */ | |
6314 | key[1].objectid = objectid; | |
962a298f | 6315 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6316 | key[1].offset = ref_objectid; |
6317 | ||
6318 | sizes[1] = name_len + sizeof(*ref); | |
6319 | } | |
9c58309d | 6320 | |
b0d5d10f CM |
6321 | location = &BTRFS_I(inode)->location; |
6322 | location->objectid = objectid; | |
6323 | location->offset = 0; | |
962a298f | 6324 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6325 | |
6326 | ret = btrfs_insert_inode_locked(inode); | |
6327 | if (ret < 0) | |
6328 | goto fail; | |
6329 | ||
b9473439 | 6330 | path->leave_spinning = 1; |
ef3b9af5 | 6331 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6332 | if (ret != 0) |
b0d5d10f | 6333 | goto fail_unlock; |
5f39d397 | 6334 | |
ecc11fab | 6335 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6336 | inode_set_bytes(inode, 0); |
9cc97d64 | 6337 | |
c2050a45 | 6338 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6339 | inode->i_atime = inode->i_mtime; |
6340 | inode->i_ctime = inode->i_mtime; | |
6341 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6342 | ||
5f39d397 CM |
6343 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6344 | struct btrfs_inode_item); | |
b159fa28 | 6345 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6346 | sizeof(*inode_item)); |
e02119d5 | 6347 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6348 | |
ef3b9af5 FM |
6349 | if (name) { |
6350 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6351 | struct btrfs_inode_ref); | |
6352 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6353 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6354 | ptr = (unsigned long)(ref + 1); | |
6355 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6356 | } | |
9c58309d | 6357 | |
5f39d397 CM |
6358 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6359 | btrfs_free_path(path); | |
6360 | ||
6cbff00f CH |
6361 | btrfs_inherit_iflags(inode, dir); |
6362 | ||
569254b0 | 6363 | if (S_ISREG(mode)) { |
0b246afa | 6364 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6365 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6366 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6367 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6368 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6369 | } |
6370 | ||
5d4f98a2 | 6371 | inode_tree_add(inode); |
1abe9b8a | 6372 | |
6373 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6374 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6375 | |
8ea05e3a AB |
6376 | btrfs_update_root_times(trans, root); |
6377 | ||
63541927 FDBM |
6378 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6379 | if (ret) | |
0b246afa | 6380 | btrfs_err(fs_info, |
63541927 | 6381 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6382 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6383 | |
39279cc3 | 6384 | return inode; |
b0d5d10f CM |
6385 | |
6386 | fail_unlock: | |
6387 | unlock_new_inode(inode); | |
5f39d397 | 6388 | fail: |
ef3b9af5 | 6389 | if (dir && name) |
aec7477b | 6390 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6391 | btrfs_free_path(path); |
09771430 | 6392 | iput(inode); |
5f39d397 | 6393 | return ERR_PTR(ret); |
39279cc3 CM |
6394 | } |
6395 | ||
6396 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6397 | { | |
6398 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6399 | } | |
6400 | ||
d352ac68 CM |
6401 | /* |
6402 | * utility function to add 'inode' into 'parent_inode' with | |
6403 | * a give name and a given sequence number. | |
6404 | * if 'add_backref' is true, also insert a backref from the | |
6405 | * inode to the parent directory. | |
6406 | */ | |
e02119d5 | 6407 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6408 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6409 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6410 | { |
db0a669f | 6411 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
4df27c4d | 6412 | int ret = 0; |
39279cc3 | 6413 | struct btrfs_key key; |
db0a669f NB |
6414 | struct btrfs_root *root = parent_inode->root; |
6415 | u64 ino = btrfs_ino(inode); | |
6416 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6417 | |
33345d01 | 6418 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6419 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6420 | } else { |
33345d01 | 6421 | key.objectid = ino; |
962a298f | 6422 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6423 | key.offset = 0; |
6424 | } | |
6425 | ||
33345d01 | 6426 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
0b246afa JM |
6427 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
6428 | root->root_key.objectid, parent_ino, | |
6429 | index, name, name_len); | |
4df27c4d | 6430 | } else if (add_backref) { |
33345d01 LZ |
6431 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6432 | parent_ino, index); | |
4df27c4d | 6433 | } |
39279cc3 | 6434 | |
79787eaa JM |
6435 | /* Nothing to clean up yet */ |
6436 | if (ret) | |
6437 | return ret; | |
4df27c4d | 6438 | |
79787eaa JM |
6439 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6440 | parent_inode, &key, | |
db0a669f | 6441 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6442 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6443 | goto fail_dir_item; |
6444 | else if (ret) { | |
66642832 | 6445 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6446 | return ret; |
39279cc3 | 6447 | } |
79787eaa | 6448 | |
db0a669f | 6449 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6450 | name_len * 2); |
db0a669f NB |
6451 | inode_inc_iversion(&parent_inode->vfs_inode); |
6452 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6453 | current_time(&parent_inode->vfs_inode); | |
6454 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
79787eaa | 6455 | if (ret) |
66642832 | 6456 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6457 | return ret; |
fe66a05a CM |
6458 | |
6459 | fail_dir_item: | |
6460 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6461 | u64 local_index; | |
6462 | int err; | |
0b246afa JM |
6463 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
6464 | root->root_key.objectid, parent_ino, | |
6465 | &local_index, name, name_len); | |
fe66a05a CM |
6466 | |
6467 | } else if (add_backref) { | |
6468 | u64 local_index; | |
6469 | int err; | |
6470 | ||
6471 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6472 | ino, parent_ino, &local_index); | |
6473 | } | |
6474 | return ret; | |
39279cc3 CM |
6475 | } |
6476 | ||
6477 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6478 | struct btrfs_inode *dir, struct dentry *dentry, |
6479 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6480 | { |
a1b075d2 JB |
6481 | int err = btrfs_add_link(trans, dir, inode, |
6482 | dentry->d_name.name, dentry->d_name.len, | |
6483 | backref, index); | |
39279cc3 CM |
6484 | if (err > 0) |
6485 | err = -EEXIST; | |
6486 | return err; | |
6487 | } | |
6488 | ||
618e21d5 | 6489 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6490 | umode_t mode, dev_t rdev) |
618e21d5 | 6491 | { |
2ff7e61e | 6492 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6493 | struct btrfs_trans_handle *trans; |
6494 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6495 | struct inode *inode = NULL; |
618e21d5 JB |
6496 | int err; |
6497 | int drop_inode = 0; | |
6498 | u64 objectid; | |
00e4e6b3 | 6499 | u64 index = 0; |
618e21d5 | 6500 | |
9ed74f2d JB |
6501 | /* |
6502 | * 2 for inode item and ref | |
6503 | * 2 for dir items | |
6504 | * 1 for xattr if selinux is on | |
6505 | */ | |
a22285a6 YZ |
6506 | trans = btrfs_start_transaction(root, 5); |
6507 | if (IS_ERR(trans)) | |
6508 | return PTR_ERR(trans); | |
1832a6d5 | 6509 | |
581bb050 LZ |
6510 | err = btrfs_find_free_ino(root, &objectid); |
6511 | if (err) | |
6512 | goto out_unlock; | |
6513 | ||
aec7477b | 6514 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6515 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6516 | mode, &index); | |
7cf96da3 TI |
6517 | if (IS_ERR(inode)) { |
6518 | err = PTR_ERR(inode); | |
618e21d5 | 6519 | goto out_unlock; |
7cf96da3 | 6520 | } |
618e21d5 | 6521 | |
ad19db71 CS |
6522 | /* |
6523 | * If the active LSM wants to access the inode during | |
6524 | * d_instantiate it needs these. Smack checks to see | |
6525 | * if the filesystem supports xattrs by looking at the | |
6526 | * ops vector. | |
6527 | */ | |
ad19db71 | 6528 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6529 | init_special_inode(inode, inode->i_mode, rdev); |
6530 | ||
6531 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6532 | if (err) |
b0d5d10f CM |
6533 | goto out_unlock_inode; |
6534 | ||
cef415af NB |
6535 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6536 | 0, index); | |
b0d5d10f CM |
6537 | if (err) { |
6538 | goto out_unlock_inode; | |
6539 | } else { | |
1b4ab1bb | 6540 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6541 | unlock_new_inode(inode); |
08c422c2 | 6542 | d_instantiate(dentry, inode); |
618e21d5 | 6543 | } |
b0d5d10f | 6544 | |
618e21d5 | 6545 | out_unlock: |
3a45bb20 | 6546 | btrfs_end_transaction(trans); |
2ff7e61e JM |
6547 | btrfs_balance_delayed_items(fs_info); |
6548 | btrfs_btree_balance_dirty(fs_info); | |
618e21d5 JB |
6549 | if (drop_inode) { |
6550 | inode_dec_link_count(inode); | |
6551 | iput(inode); | |
6552 | } | |
618e21d5 | 6553 | return err; |
b0d5d10f CM |
6554 | |
6555 | out_unlock_inode: | |
6556 | drop_inode = 1; | |
6557 | unlock_new_inode(inode); | |
6558 | goto out_unlock; | |
6559 | ||
618e21d5 JB |
6560 | } |
6561 | ||
39279cc3 | 6562 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6563 | umode_t mode, bool excl) |
39279cc3 | 6564 | { |
2ff7e61e | 6565 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6566 | struct btrfs_trans_handle *trans; |
6567 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6568 | struct inode *inode = NULL; |
43baa579 | 6569 | int drop_inode_on_err = 0; |
a22285a6 | 6570 | int err; |
39279cc3 | 6571 | u64 objectid; |
00e4e6b3 | 6572 | u64 index = 0; |
39279cc3 | 6573 | |
9ed74f2d JB |
6574 | /* |
6575 | * 2 for inode item and ref | |
6576 | * 2 for dir items | |
6577 | * 1 for xattr if selinux is on | |
6578 | */ | |
a22285a6 YZ |
6579 | trans = btrfs_start_transaction(root, 5); |
6580 | if (IS_ERR(trans)) | |
6581 | return PTR_ERR(trans); | |
9ed74f2d | 6582 | |
581bb050 LZ |
6583 | err = btrfs_find_free_ino(root, &objectid); |
6584 | if (err) | |
6585 | goto out_unlock; | |
6586 | ||
aec7477b | 6587 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6588 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6589 | mode, &index); | |
7cf96da3 TI |
6590 | if (IS_ERR(inode)) { |
6591 | err = PTR_ERR(inode); | |
39279cc3 | 6592 | goto out_unlock; |
7cf96da3 | 6593 | } |
43baa579 | 6594 | drop_inode_on_err = 1; |
ad19db71 CS |
6595 | /* |
6596 | * If the active LSM wants to access the inode during | |
6597 | * d_instantiate it needs these. Smack checks to see | |
6598 | * if the filesystem supports xattrs by looking at the | |
6599 | * ops vector. | |
6600 | */ | |
6601 | inode->i_fop = &btrfs_file_operations; | |
6602 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6603 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6604 | |
6605 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6606 | if (err) | |
6607 | goto out_unlock_inode; | |
6608 | ||
6609 | err = btrfs_update_inode(trans, root, inode); | |
6610 | if (err) | |
6611 | goto out_unlock_inode; | |
ad19db71 | 6612 | |
cef415af NB |
6613 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6614 | 0, index); | |
39279cc3 | 6615 | if (err) |
b0d5d10f | 6616 | goto out_unlock_inode; |
43baa579 | 6617 | |
43baa579 | 6618 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6619 | unlock_new_inode(inode); |
43baa579 FB |
6620 | d_instantiate(dentry, inode); |
6621 | ||
39279cc3 | 6622 | out_unlock: |
3a45bb20 | 6623 | btrfs_end_transaction(trans); |
43baa579 | 6624 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6625 | inode_dec_link_count(inode); |
6626 | iput(inode); | |
6627 | } | |
2ff7e61e JM |
6628 | btrfs_balance_delayed_items(fs_info); |
6629 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6630 | return err; |
b0d5d10f CM |
6631 | |
6632 | out_unlock_inode: | |
6633 | unlock_new_inode(inode); | |
6634 | goto out_unlock; | |
6635 | ||
39279cc3 CM |
6636 | } |
6637 | ||
6638 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6639 | struct dentry *dentry) | |
6640 | { | |
271dba45 | 6641 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6642 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6643 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6644 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6645 | u64 index; |
39279cc3 CM |
6646 | int err; |
6647 | int drop_inode = 0; | |
6648 | ||
4a8be425 TH |
6649 | /* do not allow sys_link's with other subvols of the same device */ |
6650 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6651 | return -EXDEV; |
4a8be425 | 6652 | |
f186373f | 6653 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6654 | return -EMLINK; |
4a8be425 | 6655 | |
877574e2 | 6656 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6657 | if (err) |
6658 | goto fail; | |
6659 | ||
a22285a6 | 6660 | /* |
7e6b6465 | 6661 | * 2 items for inode and inode ref |
a22285a6 | 6662 | * 2 items for dir items |
7e6b6465 | 6663 | * 1 item for parent inode |
a22285a6 | 6664 | */ |
7e6b6465 | 6665 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6666 | if (IS_ERR(trans)) { |
6667 | err = PTR_ERR(trans); | |
271dba45 | 6668 | trans = NULL; |
a22285a6 YZ |
6669 | goto fail; |
6670 | } | |
5f39d397 | 6671 | |
67de1176 MX |
6672 | /* There are several dir indexes for this inode, clear the cache. */ |
6673 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6674 | inc_nlink(inode); |
0c4d2d95 | 6675 | inode_inc_iversion(inode); |
c2050a45 | 6676 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6677 | ihold(inode); |
e9976151 | 6678 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6679 | |
cef415af NB |
6680 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6681 | 1, index); | |
5f39d397 | 6682 | |
a5719521 | 6683 | if (err) { |
54aa1f4d | 6684 | drop_inode = 1; |
a5719521 | 6685 | } else { |
10d9f309 | 6686 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6687 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6688 | if (err) |
6689 | goto fail; | |
ef3b9af5 FM |
6690 | if (inode->i_nlink == 1) { |
6691 | /* | |
6692 | * If new hard link count is 1, it's a file created | |
6693 | * with open(2) O_TMPFILE flag. | |
6694 | */ | |
3d6ae7bb | 6695 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6696 | if (err) |
6697 | goto fail; | |
6698 | } | |
08c422c2 | 6699 | d_instantiate(dentry, inode); |
9ca5fbfb | 6700 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6701 | } |
39279cc3 | 6702 | |
2ff7e61e | 6703 | btrfs_balance_delayed_items(fs_info); |
1832a6d5 | 6704 | fail: |
271dba45 | 6705 | if (trans) |
3a45bb20 | 6706 | btrfs_end_transaction(trans); |
39279cc3 CM |
6707 | if (drop_inode) { |
6708 | inode_dec_link_count(inode); | |
6709 | iput(inode); | |
6710 | } | |
2ff7e61e | 6711 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6712 | return err; |
6713 | } | |
6714 | ||
18bb1db3 | 6715 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6716 | { |
2ff7e61e | 6717 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6718 | struct inode *inode = NULL; |
39279cc3 CM |
6719 | struct btrfs_trans_handle *trans; |
6720 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6721 | int err = 0; | |
6722 | int drop_on_err = 0; | |
b9d86667 | 6723 | u64 objectid = 0; |
00e4e6b3 | 6724 | u64 index = 0; |
39279cc3 | 6725 | |
9ed74f2d JB |
6726 | /* |
6727 | * 2 items for inode and ref | |
6728 | * 2 items for dir items | |
6729 | * 1 for xattr if selinux is on | |
6730 | */ | |
a22285a6 YZ |
6731 | trans = btrfs_start_transaction(root, 5); |
6732 | if (IS_ERR(trans)) | |
6733 | return PTR_ERR(trans); | |
39279cc3 | 6734 | |
581bb050 LZ |
6735 | err = btrfs_find_free_ino(root, &objectid); |
6736 | if (err) | |
6737 | goto out_fail; | |
6738 | ||
aec7477b | 6739 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6740 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6741 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6742 | if (IS_ERR(inode)) { |
6743 | err = PTR_ERR(inode); | |
6744 | goto out_fail; | |
6745 | } | |
5f39d397 | 6746 | |
39279cc3 | 6747 | drop_on_err = 1; |
b0d5d10f CM |
6748 | /* these must be set before we unlock the inode */ |
6749 | inode->i_op = &btrfs_dir_inode_operations; | |
6750 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6751 | |
2a7dba39 | 6752 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6753 | if (err) |
b0d5d10f | 6754 | goto out_fail_inode; |
39279cc3 | 6755 | |
6ef06d27 | 6756 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6757 | err = btrfs_update_inode(trans, root, inode); |
6758 | if (err) | |
b0d5d10f | 6759 | goto out_fail_inode; |
5f39d397 | 6760 | |
db0a669f NB |
6761 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6762 | dentry->d_name.name, | |
6763 | dentry->d_name.len, 0, index); | |
39279cc3 | 6764 | if (err) |
b0d5d10f | 6765 | goto out_fail_inode; |
5f39d397 | 6766 | |
39279cc3 | 6767 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6768 | /* |
6769 | * mkdir is special. We're unlocking after we call d_instantiate | |
6770 | * to avoid a race with nfsd calling d_instantiate. | |
6771 | */ | |
6772 | unlock_new_inode(inode); | |
39279cc3 | 6773 | drop_on_err = 0; |
39279cc3 CM |
6774 | |
6775 | out_fail: | |
3a45bb20 | 6776 | btrfs_end_transaction(trans); |
c7cfb8a5 WS |
6777 | if (drop_on_err) { |
6778 | inode_dec_link_count(inode); | |
39279cc3 | 6779 | iput(inode); |
c7cfb8a5 | 6780 | } |
2ff7e61e JM |
6781 | btrfs_balance_delayed_items(fs_info); |
6782 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6783 | return err; |
b0d5d10f CM |
6784 | |
6785 | out_fail_inode: | |
6786 | unlock_new_inode(inode); | |
6787 | goto out_fail; | |
39279cc3 CM |
6788 | } |
6789 | ||
e6c4efd8 QW |
6790 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6791 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6792 | { | |
6793 | struct rb_node *next; | |
6794 | ||
6795 | next = rb_next(&em->rb_node); | |
6796 | if (!next) | |
6797 | return NULL; | |
6798 | return container_of(next, struct extent_map, rb_node); | |
6799 | } | |
6800 | ||
6801 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6802 | { | |
6803 | struct rb_node *prev; | |
6804 | ||
6805 | prev = rb_prev(&em->rb_node); | |
6806 | if (!prev) | |
6807 | return NULL; | |
6808 | return container_of(prev, struct extent_map, rb_node); | |
6809 | } | |
6810 | ||
d352ac68 | 6811 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6812 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6813 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6814 | * the best fitted new extent into the tree. |
d352ac68 | 6815 | */ |
3b951516 CM |
6816 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6817 | struct extent_map *existing, | |
e6dcd2dc | 6818 | struct extent_map *em, |
51f395ad | 6819 | u64 map_start) |
3b951516 | 6820 | { |
e6c4efd8 QW |
6821 | struct extent_map *prev; |
6822 | struct extent_map *next; | |
6823 | u64 start; | |
6824 | u64 end; | |
3b951516 | 6825 | u64 start_diff; |
3b951516 | 6826 | |
e6dcd2dc | 6827 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6828 | |
6829 | if (existing->start > map_start) { | |
6830 | next = existing; | |
6831 | prev = prev_extent_map(next); | |
6832 | } else { | |
6833 | prev = existing; | |
6834 | next = next_extent_map(prev); | |
6835 | } | |
6836 | ||
6837 | start = prev ? extent_map_end(prev) : em->start; | |
6838 | start = max_t(u64, start, em->start); | |
6839 | end = next ? next->start : extent_map_end(em); | |
6840 | end = min_t(u64, end, extent_map_end(em)); | |
6841 | start_diff = start - em->start; | |
6842 | em->start = start; | |
6843 | em->len = end - start; | |
c8b97818 CM |
6844 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6845 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6846 | em->block_start += start_diff; |
c8b97818 CM |
6847 | em->block_len -= start_diff; |
6848 | } | |
09a2a8f9 | 6849 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6850 | } |
6851 | ||
c8b97818 | 6852 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6853 | struct page *page, |
c8b97818 CM |
6854 | size_t pg_offset, u64 extent_offset, |
6855 | struct btrfs_file_extent_item *item) | |
6856 | { | |
6857 | int ret; | |
6858 | struct extent_buffer *leaf = path->nodes[0]; | |
6859 | char *tmp; | |
6860 | size_t max_size; | |
6861 | unsigned long inline_size; | |
6862 | unsigned long ptr; | |
261507a0 | 6863 | int compress_type; |
c8b97818 CM |
6864 | |
6865 | WARN_ON(pg_offset != 0); | |
261507a0 | 6866 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6867 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6868 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6869 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6870 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6871 | if (!tmp) |
6872 | return -ENOMEM; | |
c8b97818 CM |
6873 | ptr = btrfs_file_extent_inline_start(item); |
6874 | ||
6875 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6876 | ||
09cbfeaf | 6877 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6878 | ret = btrfs_decompress(compress_type, tmp, page, |
6879 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6880 | |
6881 | /* | |
6882 | * decompression code contains a memset to fill in any space between the end | |
6883 | * of the uncompressed data and the end of max_size in case the decompressed | |
6884 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6885 | * the end of an inline extent and the beginning of the next block, so we | |
6886 | * cover that region here. | |
6887 | */ | |
6888 | ||
6889 | if (max_size + pg_offset < PAGE_SIZE) { | |
6890 | char *map = kmap(page); | |
6891 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6892 | kunmap(page); | |
6893 | } | |
c8b97818 | 6894 | kfree(tmp); |
166ae5a4 | 6895 | return ret; |
c8b97818 CM |
6896 | } |
6897 | ||
d352ac68 CM |
6898 | /* |
6899 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6900 | * the ugly parts come from merging extents from the disk with the in-ram |
6901 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6902 | * where the in-ram extents might be locked pending data=ordered completion. |
6903 | * | |
6904 | * This also copies inline extents directly into the page. | |
6905 | */ | |
fc4f21b1 NB |
6906 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
6907 | struct page *page, | |
6908 | size_t pg_offset, u64 start, u64 len, | |
6909 | int create) | |
a52d9a80 | 6910 | { |
fc4f21b1 | 6911 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
a52d9a80 CM |
6912 | int ret; |
6913 | int err = 0; | |
a52d9a80 CM |
6914 | u64 extent_start = 0; |
6915 | u64 extent_end = 0; | |
fc4f21b1 | 6916 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6917 | u32 found_type; |
f421950f | 6918 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6919 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6920 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6921 | struct extent_buffer *leaf; |
6922 | struct btrfs_key found_key; | |
a52d9a80 | 6923 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6924 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6925 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6926 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6927 | const bool new_inline = !page || create; |
a52d9a80 | 6928 | |
a52d9a80 | 6929 | again: |
890871be | 6930 | read_lock(&em_tree->lock); |
d1310b2e | 6931 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6932 | if (em) |
0b246afa | 6933 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6934 | read_unlock(&em_tree->lock); |
d1310b2e | 6935 | |
a52d9a80 | 6936 | if (em) { |
e1c4b745 CM |
6937 | if (em->start > start || em->start + em->len <= start) |
6938 | free_extent_map(em); | |
6939 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6940 | free_extent_map(em); |
6941 | else | |
6942 | goto out; | |
a52d9a80 | 6943 | } |
172ddd60 | 6944 | em = alloc_extent_map(); |
a52d9a80 | 6945 | if (!em) { |
d1310b2e CM |
6946 | err = -ENOMEM; |
6947 | goto out; | |
a52d9a80 | 6948 | } |
0b246afa | 6949 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 6950 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6951 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6952 | em->len = (u64)-1; |
c8b97818 | 6953 | em->block_len = (u64)-1; |
f421950f CM |
6954 | |
6955 | if (!path) { | |
6956 | path = btrfs_alloc_path(); | |
026fd317 JB |
6957 | if (!path) { |
6958 | err = -ENOMEM; | |
6959 | goto out; | |
6960 | } | |
6961 | /* | |
6962 | * Chances are we'll be called again, so go ahead and do | |
6963 | * readahead | |
6964 | */ | |
e4058b54 | 6965 | path->reada = READA_FORWARD; |
f421950f CM |
6966 | } |
6967 | ||
179e29e4 CM |
6968 | ret = btrfs_lookup_file_extent(trans, root, path, |
6969 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6970 | if (ret < 0) { |
6971 | err = ret; | |
6972 | goto out; | |
6973 | } | |
6974 | ||
6975 | if (ret != 0) { | |
6976 | if (path->slots[0] == 0) | |
6977 | goto not_found; | |
6978 | path->slots[0]--; | |
6979 | } | |
6980 | ||
5f39d397 CM |
6981 | leaf = path->nodes[0]; |
6982 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6983 | struct btrfs_file_extent_item); |
a52d9a80 | 6984 | /* are we inside the extent that was found? */ |
5f39d397 | 6985 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6986 | found_type = found_key.type; |
5f39d397 | 6987 | if (found_key.objectid != objectid || |
a52d9a80 | 6988 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6989 | /* |
6990 | * If we backup past the first extent we want to move forward | |
6991 | * and see if there is an extent in front of us, otherwise we'll | |
6992 | * say there is a hole for our whole search range which can | |
6993 | * cause problems. | |
6994 | */ | |
6995 | extent_end = start; | |
6996 | goto next; | |
a52d9a80 CM |
6997 | } |
6998 | ||
5f39d397 CM |
6999 | found_type = btrfs_file_extent_type(leaf, item); |
7000 | extent_start = found_key.offset; | |
d899e052 YZ |
7001 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7002 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 7003 | extent_end = extent_start + |
db94535d | 7004 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
7005 | |
7006 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
7007 | extent_start); | |
9036c102 YZ |
7008 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
7009 | size_t size; | |
514ac8ad | 7010 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
da17066c | 7011 | extent_end = ALIGN(extent_start + size, |
0b246afa | 7012 | fs_info->sectorsize); |
09ed2f16 LB |
7013 | |
7014 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
7015 | path->slots[0], | |
7016 | extent_start); | |
9036c102 | 7017 | } |
25a50341 | 7018 | next: |
9036c102 YZ |
7019 | if (start >= extent_end) { |
7020 | path->slots[0]++; | |
7021 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
7022 | ret = btrfs_next_leaf(root, path); | |
7023 | if (ret < 0) { | |
7024 | err = ret; | |
7025 | goto out; | |
a52d9a80 | 7026 | } |
9036c102 YZ |
7027 | if (ret > 0) |
7028 | goto not_found; | |
7029 | leaf = path->nodes[0]; | |
a52d9a80 | 7030 | } |
9036c102 YZ |
7031 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
7032 | if (found_key.objectid != objectid || | |
7033 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
7034 | goto not_found; | |
7035 | if (start + len <= found_key.offset) | |
7036 | goto not_found; | |
e2eca69d WS |
7037 | if (start > found_key.offset) |
7038 | goto next; | |
9036c102 | 7039 | em->start = start; |
70c8a91c | 7040 | em->orig_start = start; |
9036c102 YZ |
7041 | em->len = found_key.offset - start; |
7042 | goto not_found_em; | |
7043 | } | |
7044 | ||
fc4f21b1 | 7045 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 7046 | new_inline, em); |
7ffbb598 | 7047 | |
d899e052 YZ |
7048 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7049 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
7050 | goto insert; |
7051 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 7052 | unsigned long ptr; |
a52d9a80 | 7053 | char *map; |
3326d1b0 CM |
7054 | size_t size; |
7055 | size_t extent_offset; | |
7056 | size_t copy_size; | |
a52d9a80 | 7057 | |
7ffbb598 | 7058 | if (new_inline) |
689f9346 | 7059 | goto out; |
5f39d397 | 7060 | |
514ac8ad | 7061 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 7062 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7063 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7064 | size - extent_offset); | |
3326d1b0 | 7065 | em->start = extent_start + extent_offset; |
0b246afa | 7066 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7067 | em->orig_block_len = em->len; |
70c8a91c | 7068 | em->orig_start = em->start; |
689f9346 | 7069 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 7070 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
7071 | if (btrfs_file_extent_compression(leaf, item) != |
7072 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7073 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7074 | extent_offset, item); |
166ae5a4 ZB |
7075 | if (ret) { |
7076 | err = ret; | |
7077 | goto out; | |
7078 | } | |
c8b97818 CM |
7079 | } else { |
7080 | map = kmap(page); | |
7081 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7082 | copy_size); | |
09cbfeaf | 7083 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7084 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7085 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7086 | copy_size); |
7087 | } | |
c8b97818 CM |
7088 | kunmap(page); |
7089 | } | |
179e29e4 CM |
7090 | flush_dcache_page(page); |
7091 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 7092 | BUG(); |
179e29e4 CM |
7093 | if (!trans) { |
7094 | kunmap(page); | |
7095 | free_extent_map(em); | |
7096 | em = NULL; | |
ff5714cc | 7097 | |
b3b4aa74 | 7098 | btrfs_release_path(path); |
7a7eaa40 | 7099 | trans = btrfs_join_transaction(root); |
ff5714cc | 7100 | |
3612b495 TI |
7101 | if (IS_ERR(trans)) |
7102 | return ERR_CAST(trans); | |
179e29e4 CM |
7103 | goto again; |
7104 | } | |
c8b97818 | 7105 | map = kmap(page); |
70dec807 | 7106 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 7107 | copy_size); |
c8b97818 | 7108 | kunmap(page); |
179e29e4 | 7109 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 7110 | } |
d1310b2e | 7111 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7112 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7113 | goto insert; |
a52d9a80 CM |
7114 | } |
7115 | not_found: | |
7116 | em->start = start; | |
70c8a91c | 7117 | em->orig_start = start; |
d1310b2e | 7118 | em->len = len; |
a52d9a80 | 7119 | not_found_em: |
5f39d397 | 7120 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 7121 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 7122 | insert: |
b3b4aa74 | 7123 | btrfs_release_path(path); |
d1310b2e | 7124 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7125 | btrfs_err(fs_info, |
5d163e0e JM |
7126 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7127 | em->start, em->len, start, len); | |
a52d9a80 CM |
7128 | err = -EIO; |
7129 | goto out; | |
7130 | } | |
d1310b2e CM |
7131 | |
7132 | err = 0; | |
890871be | 7133 | write_lock(&em_tree->lock); |
09a2a8f9 | 7134 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7135 | /* it is possible that someone inserted the extent into the tree |
7136 | * while we had the lock dropped. It is also possible that | |
7137 | * an overlapping map exists in the tree | |
7138 | */ | |
a52d9a80 | 7139 | if (ret == -EEXIST) { |
3b951516 | 7140 | struct extent_map *existing; |
e6dcd2dc CM |
7141 | |
7142 | ret = 0; | |
7143 | ||
e6c4efd8 QW |
7144 | existing = search_extent_mapping(em_tree, start, len); |
7145 | /* | |
7146 | * existing will always be non-NULL, since there must be | |
7147 | * extent causing the -EEXIST. | |
7148 | */ | |
8dff9c85 | 7149 | if (existing->start == em->start && |
8e2bd3b7 | 7150 | extent_map_end(existing) >= extent_map_end(em) && |
8dff9c85 CM |
7151 | em->block_start == existing->block_start) { |
7152 | /* | |
8e2bd3b7 OS |
7153 | * The existing extent map already encompasses the |
7154 | * entire extent map we tried to add. | |
8dff9c85 CM |
7155 | */ |
7156 | free_extent_map(em); | |
7157 | em = existing; | |
7158 | err = 0; | |
7159 | ||
7160 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7161 | start <= existing->start) { |
e6c4efd8 QW |
7162 | /* |
7163 | * The existing extent map is the one nearest to | |
7164 | * the [start, start + len) range which overlaps | |
7165 | */ | |
7166 | err = merge_extent_mapping(em_tree, existing, | |
7167 | em, start); | |
e1c4b745 | 7168 | free_extent_map(existing); |
e6c4efd8 | 7169 | if (err) { |
3b951516 CM |
7170 | free_extent_map(em); |
7171 | em = NULL; | |
7172 | } | |
7173 | } else { | |
7174 | free_extent_map(em); | |
7175 | em = existing; | |
e6dcd2dc | 7176 | err = 0; |
a52d9a80 | 7177 | } |
a52d9a80 | 7178 | } |
890871be | 7179 | write_unlock(&em_tree->lock); |
a52d9a80 | 7180 | out: |
1abe9b8a | 7181 | |
fc4f21b1 | 7182 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7183 | |
527afb44 | 7184 | btrfs_free_path(path); |
a52d9a80 | 7185 | if (trans) { |
3a45bb20 | 7186 | ret = btrfs_end_transaction(trans); |
d397712b | 7187 | if (!err) |
a52d9a80 CM |
7188 | err = ret; |
7189 | } | |
a52d9a80 CM |
7190 | if (err) { |
7191 | free_extent_map(em); | |
a52d9a80 CM |
7192 | return ERR_PTR(err); |
7193 | } | |
79787eaa | 7194 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7195 | return em; |
7196 | } | |
7197 | ||
fc4f21b1 NB |
7198 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
7199 | struct page *page, | |
7200 | size_t pg_offset, u64 start, u64 len, | |
7201 | int create) | |
ec29ed5b CM |
7202 | { |
7203 | struct extent_map *em; | |
7204 | struct extent_map *hole_em = NULL; | |
7205 | u64 range_start = start; | |
7206 | u64 end; | |
7207 | u64 found; | |
7208 | u64 found_end; | |
7209 | int err = 0; | |
7210 | ||
7211 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7212 | if (IS_ERR(em)) | |
7213 | return em; | |
9986277e DC |
7214 | /* |
7215 | * If our em maps to: | |
7216 | * - a hole or | |
7217 | * - a pre-alloc extent, | |
7218 | * there might actually be delalloc bytes behind it. | |
7219 | */ | |
7220 | if (em->block_start != EXTENT_MAP_HOLE && | |
7221 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7222 | return em; | |
7223 | else | |
7224 | hole_em = em; | |
ec29ed5b CM |
7225 | |
7226 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7227 | end = start + len; | |
7228 | if (end < start) | |
7229 | end = (u64)-1; | |
7230 | else | |
7231 | end -= 1; | |
7232 | ||
7233 | em = NULL; | |
7234 | ||
7235 | /* ok, we didn't find anything, lets look for delalloc */ | |
fc4f21b1 | 7236 | found = count_range_bits(&inode->io_tree, &range_start, |
ec29ed5b CM |
7237 | end, len, EXTENT_DELALLOC, 1); |
7238 | found_end = range_start + found; | |
7239 | if (found_end < range_start) | |
7240 | found_end = (u64)-1; | |
7241 | ||
7242 | /* | |
7243 | * we didn't find anything useful, return | |
7244 | * the original results from get_extent() | |
7245 | */ | |
7246 | if (range_start > end || found_end <= start) { | |
7247 | em = hole_em; | |
7248 | hole_em = NULL; | |
7249 | goto out; | |
7250 | } | |
7251 | ||
7252 | /* adjust the range_start to make sure it doesn't | |
7253 | * go backwards from the start they passed in | |
7254 | */ | |
67871254 | 7255 | range_start = max(start, range_start); |
ec29ed5b CM |
7256 | found = found_end - range_start; |
7257 | ||
7258 | if (found > 0) { | |
7259 | u64 hole_start = start; | |
7260 | u64 hole_len = len; | |
7261 | ||
172ddd60 | 7262 | em = alloc_extent_map(); |
ec29ed5b CM |
7263 | if (!em) { |
7264 | err = -ENOMEM; | |
7265 | goto out; | |
7266 | } | |
7267 | /* | |
7268 | * when btrfs_get_extent can't find anything it | |
7269 | * returns one huge hole | |
7270 | * | |
7271 | * make sure what it found really fits our range, and | |
7272 | * adjust to make sure it is based on the start from | |
7273 | * the caller | |
7274 | */ | |
7275 | if (hole_em) { | |
7276 | u64 calc_end = extent_map_end(hole_em); | |
7277 | ||
7278 | if (calc_end <= start || (hole_em->start > end)) { | |
7279 | free_extent_map(hole_em); | |
7280 | hole_em = NULL; | |
7281 | } else { | |
7282 | hole_start = max(hole_em->start, start); | |
7283 | hole_len = calc_end - hole_start; | |
7284 | } | |
7285 | } | |
7286 | em->bdev = NULL; | |
7287 | if (hole_em && range_start > hole_start) { | |
7288 | /* our hole starts before our delalloc, so we | |
7289 | * have to return just the parts of the hole | |
7290 | * that go until the delalloc starts | |
7291 | */ | |
7292 | em->len = min(hole_len, | |
7293 | range_start - hole_start); | |
7294 | em->start = hole_start; | |
7295 | em->orig_start = hole_start; | |
7296 | /* | |
7297 | * don't adjust block start at all, | |
7298 | * it is fixed at EXTENT_MAP_HOLE | |
7299 | */ | |
7300 | em->block_start = hole_em->block_start; | |
7301 | em->block_len = hole_len; | |
f9e4fb53 LB |
7302 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7303 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7304 | } else { |
7305 | em->start = range_start; | |
7306 | em->len = found; | |
7307 | em->orig_start = range_start; | |
7308 | em->block_start = EXTENT_MAP_DELALLOC; | |
7309 | em->block_len = found; | |
7310 | } | |
7311 | } else if (hole_em) { | |
7312 | return hole_em; | |
7313 | } | |
7314 | out: | |
7315 | ||
7316 | free_extent_map(hole_em); | |
7317 | if (err) { | |
7318 | free_extent_map(em); | |
7319 | return ERR_PTR(err); | |
7320 | } | |
7321 | return em; | |
7322 | } | |
7323 | ||
5f9a8a51 FM |
7324 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7325 | const u64 start, | |
7326 | const u64 len, | |
7327 | const u64 orig_start, | |
7328 | const u64 block_start, | |
7329 | const u64 block_len, | |
7330 | const u64 orig_block_len, | |
7331 | const u64 ram_bytes, | |
7332 | const int type) | |
7333 | { | |
7334 | struct extent_map *em = NULL; | |
7335 | int ret; | |
7336 | ||
5f9a8a51 | 7337 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7338 | em = create_io_em(inode, start, len, orig_start, |
7339 | block_start, block_len, orig_block_len, | |
7340 | ram_bytes, | |
7341 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7342 | type); | |
5f9a8a51 FM |
7343 | if (IS_ERR(em)) |
7344 | goto out; | |
7345 | } | |
7346 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7347 | len, block_len, type); | |
7348 | if (ret) { | |
7349 | if (em) { | |
7350 | free_extent_map(em); | |
dcdbc059 | 7351 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7352 | start + len - 1, 0); |
7353 | } | |
7354 | em = ERR_PTR(ret); | |
7355 | } | |
7356 | out: | |
5f9a8a51 FM |
7357 | |
7358 | return em; | |
7359 | } | |
7360 | ||
4b46fce2 JB |
7361 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7362 | u64 start, u64 len) | |
7363 | { | |
0b246afa | 7364 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7365 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7366 | struct extent_map *em; |
4b46fce2 JB |
7367 | struct btrfs_key ins; |
7368 | u64 alloc_hint; | |
7369 | int ret; | |
4b46fce2 | 7370 | |
4b46fce2 | 7371 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7372 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7373 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7374 | if (ret) |
7375 | return ERR_PTR(ret); | |
4b46fce2 | 7376 | |
5f9a8a51 FM |
7377 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7378 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7379 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7380 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7381 | if (IS_ERR(em)) |
2ff7e61e JM |
7382 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7383 | ins.offset, 1); | |
de0ee0ed | 7384 | |
4b46fce2 JB |
7385 | return em; |
7386 | } | |
7387 | ||
46bfbb5c CM |
7388 | /* |
7389 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7390 | * block must be cow'd | |
7391 | */ | |
00361589 | 7392 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7393 | u64 *orig_start, u64 *orig_block_len, |
7394 | u64 *ram_bytes) | |
46bfbb5c | 7395 | { |
2ff7e61e | 7396 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7397 | struct btrfs_path *path; |
7398 | int ret; | |
7399 | struct extent_buffer *leaf; | |
7400 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7401 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7402 | struct btrfs_file_extent_item *fi; |
7403 | struct btrfs_key key; | |
7404 | u64 disk_bytenr; | |
7405 | u64 backref_offset; | |
7406 | u64 extent_end; | |
7407 | u64 num_bytes; | |
7408 | int slot; | |
7409 | int found_type; | |
7ee9e440 | 7410 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7411 | |
46bfbb5c CM |
7412 | path = btrfs_alloc_path(); |
7413 | if (!path) | |
7414 | return -ENOMEM; | |
7415 | ||
f85b7379 DS |
7416 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7417 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7418 | if (ret < 0) |
7419 | goto out; | |
7420 | ||
7421 | slot = path->slots[0]; | |
7422 | if (ret == 1) { | |
7423 | if (slot == 0) { | |
7424 | /* can't find the item, must cow */ | |
7425 | ret = 0; | |
7426 | goto out; | |
7427 | } | |
7428 | slot--; | |
7429 | } | |
7430 | ret = 0; | |
7431 | leaf = path->nodes[0]; | |
7432 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7433 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7434 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7435 | /* not our file or wrong item type, must cow */ | |
7436 | goto out; | |
7437 | } | |
7438 | ||
7439 | if (key.offset > offset) { | |
7440 | /* Wrong offset, must cow */ | |
7441 | goto out; | |
7442 | } | |
7443 | ||
7444 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7445 | found_type = btrfs_file_extent_type(leaf, fi); | |
7446 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7447 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7448 | /* not a regular extent, must cow */ | |
7449 | goto out; | |
7450 | } | |
7ee9e440 JB |
7451 | |
7452 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7453 | goto out; | |
7454 | ||
e77751aa MX |
7455 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7456 | if (extent_end <= offset) | |
7457 | goto out; | |
7458 | ||
46bfbb5c | 7459 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7460 | if (disk_bytenr == 0) |
7461 | goto out; | |
7462 | ||
7463 | if (btrfs_file_extent_compression(leaf, fi) || | |
7464 | btrfs_file_extent_encryption(leaf, fi) || | |
7465 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7466 | goto out; | |
7467 | ||
46bfbb5c CM |
7468 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7469 | ||
7ee9e440 JB |
7470 | if (orig_start) { |
7471 | *orig_start = key.offset - backref_offset; | |
7472 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7473 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7474 | } | |
eb384b55 | 7475 | |
2ff7e61e | 7476 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7477 | goto out; |
7b2b7085 MX |
7478 | |
7479 | num_bytes = min(offset + *len, extent_end) - offset; | |
7480 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7481 | u64 range_end; | |
7482 | ||
da17066c JM |
7483 | range_end = round_up(offset + num_bytes, |
7484 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7485 | ret = test_range_bit(io_tree, offset, range_end, |
7486 | EXTENT_DELALLOC, 0, NULL); | |
7487 | if (ret) { | |
7488 | ret = -EAGAIN; | |
7489 | goto out; | |
7490 | } | |
7491 | } | |
7492 | ||
1bda19eb | 7493 | btrfs_release_path(path); |
46bfbb5c CM |
7494 | |
7495 | /* | |
7496 | * look for other files referencing this extent, if we | |
7497 | * find any we must cow | |
7498 | */ | |
00361589 | 7499 | |
e4c3b2dc | 7500 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7501 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7502 | if (ret) { |
7503 | ret = 0; | |
7504 | goto out; | |
7505 | } | |
46bfbb5c CM |
7506 | |
7507 | /* | |
7508 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7509 | * in this extent we are about to write. If there | |
7510 | * are any csums in that range we have to cow in order | |
7511 | * to keep the csums correct | |
7512 | */ | |
7513 | disk_bytenr += backref_offset; | |
7514 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7515 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7516 | goto out; | |
46bfbb5c CM |
7517 | /* |
7518 | * all of the above have passed, it is safe to overwrite this extent | |
7519 | * without cow | |
7520 | */ | |
eb384b55 | 7521 | *len = num_bytes; |
46bfbb5c CM |
7522 | ret = 1; |
7523 | out: | |
7524 | btrfs_free_path(path); | |
7525 | return ret; | |
7526 | } | |
7527 | ||
fc4adbff AG |
7528 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7529 | { | |
7530 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
e03733da | 7531 | bool found = false; |
fc4adbff AG |
7532 | void **pagep = NULL; |
7533 | struct page *page = NULL; | |
cc2b702c DS |
7534 | unsigned long start_idx; |
7535 | unsigned long end_idx; | |
fc4adbff | 7536 | |
09cbfeaf | 7537 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7538 | |
7539 | /* | |
7540 | * end is the last byte in the last page. end == start is legal | |
7541 | */ | |
09cbfeaf | 7542 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7543 | |
7544 | rcu_read_lock(); | |
7545 | ||
7546 | /* Most of the code in this while loop is lifted from | |
7547 | * find_get_page. It's been modified to begin searching from a | |
7548 | * page and return just the first page found in that range. If the | |
7549 | * found idx is less than or equal to the end idx then we know that | |
7550 | * a page exists. If no pages are found or if those pages are | |
7551 | * outside of the range then we're fine (yay!) */ | |
7552 | while (page == NULL && | |
7553 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7554 | page = radix_tree_deref_slot(pagep); | |
7555 | if (unlikely(!page)) | |
7556 | break; | |
7557 | ||
7558 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7559 | if (radix_tree_deref_retry(page)) { |
7560 | page = NULL; | |
fc4adbff | 7561 | continue; |
809f9016 | 7562 | } |
fc4adbff AG |
7563 | /* |
7564 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7565 | * here as an exceptional entry: so return it without | |
7566 | * attempting to raise page count. | |
7567 | */ | |
6fdef6d4 | 7568 | page = NULL; |
fc4adbff AG |
7569 | break; /* TODO: Is this relevant for this use case? */ |
7570 | } | |
7571 | ||
91405151 FM |
7572 | if (!page_cache_get_speculative(page)) { |
7573 | page = NULL; | |
fc4adbff | 7574 | continue; |
91405151 | 7575 | } |
fc4adbff AG |
7576 | |
7577 | /* | |
7578 | * Has the page moved? | |
7579 | * This is part of the lockless pagecache protocol. See | |
7580 | * include/linux/pagemap.h for details. | |
7581 | */ | |
7582 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7583 | put_page(page); |
fc4adbff AG |
7584 | page = NULL; |
7585 | } | |
7586 | } | |
7587 | ||
7588 | if (page) { | |
7589 | if (page->index <= end_idx) | |
7590 | found = true; | |
09cbfeaf | 7591 | put_page(page); |
fc4adbff AG |
7592 | } |
7593 | ||
7594 | rcu_read_unlock(); | |
7595 | return found; | |
7596 | } | |
7597 | ||
eb838e73 JB |
7598 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7599 | struct extent_state **cached_state, int writing) | |
7600 | { | |
7601 | struct btrfs_ordered_extent *ordered; | |
7602 | int ret = 0; | |
7603 | ||
7604 | while (1) { | |
7605 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7606 | cached_state); |
eb838e73 JB |
7607 | /* |
7608 | * We're concerned with the entire range that we're going to be | |
01327610 | 7609 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7610 | * extents in this range. |
7611 | */ | |
a776c6fa | 7612 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7613 | lockend - lockstart + 1); |
7614 | ||
7615 | /* | |
7616 | * We need to make sure there are no buffered pages in this | |
7617 | * range either, we could have raced between the invalidate in | |
7618 | * generic_file_direct_write and locking the extent. The | |
7619 | * invalidate needs to happen so that reads after a write do not | |
7620 | * get stale data. | |
7621 | */ | |
fc4adbff AG |
7622 | if (!ordered && |
7623 | (!writing || | |
7624 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7625 | break; |
7626 | ||
7627 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7628 | cached_state, GFP_NOFS); | |
7629 | ||
7630 | if (ordered) { | |
ade77029 FM |
7631 | /* |
7632 | * If we are doing a DIO read and the ordered extent we | |
7633 | * found is for a buffered write, we can not wait for it | |
7634 | * to complete and retry, because if we do so we can | |
7635 | * deadlock with concurrent buffered writes on page | |
7636 | * locks. This happens only if our DIO read covers more | |
7637 | * than one extent map, if at this point has already | |
7638 | * created an ordered extent for a previous extent map | |
7639 | * and locked its range in the inode's io tree, and a | |
7640 | * concurrent write against that previous extent map's | |
7641 | * range and this range started (we unlock the ranges | |
7642 | * in the io tree only when the bios complete and | |
7643 | * buffered writes always lock pages before attempting | |
7644 | * to lock range in the io tree). | |
7645 | */ | |
7646 | if (writing || | |
7647 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7648 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7649 | else | |
7650 | ret = -ENOTBLK; | |
eb838e73 JB |
7651 | btrfs_put_ordered_extent(ordered); |
7652 | } else { | |
eb838e73 | 7653 | /* |
b850ae14 FM |
7654 | * We could trigger writeback for this range (and wait |
7655 | * for it to complete) and then invalidate the pages for | |
7656 | * this range (through invalidate_inode_pages2_range()), | |
7657 | * but that can lead us to a deadlock with a concurrent | |
7658 | * call to readpages() (a buffered read or a defrag call | |
7659 | * triggered a readahead) on a page lock due to an | |
7660 | * ordered dio extent we created before but did not have | |
7661 | * yet a corresponding bio submitted (whence it can not | |
7662 | * complete), which makes readpages() wait for that | |
7663 | * ordered extent to complete while holding a lock on | |
7664 | * that page. | |
eb838e73 | 7665 | */ |
b850ae14 | 7666 | ret = -ENOTBLK; |
eb838e73 JB |
7667 | } |
7668 | ||
ade77029 FM |
7669 | if (ret) |
7670 | break; | |
7671 | ||
eb838e73 JB |
7672 | cond_resched(); |
7673 | } | |
7674 | ||
7675 | return ret; | |
7676 | } | |
7677 | ||
6f9994db LB |
7678 | /* The callers of this must take lock_extent() */ |
7679 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7680 | u64 orig_start, u64 block_start, | |
7681 | u64 block_len, u64 orig_block_len, | |
7682 | u64 ram_bytes, int compress_type, | |
7683 | int type) | |
69ffb543 JB |
7684 | { |
7685 | struct extent_map_tree *em_tree; | |
7686 | struct extent_map *em; | |
7687 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7688 | int ret; | |
7689 | ||
6f9994db LB |
7690 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7691 | type == BTRFS_ORDERED_COMPRESSED || | |
7692 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7693 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7694 | |
69ffb543 JB |
7695 | em_tree = &BTRFS_I(inode)->extent_tree; |
7696 | em = alloc_extent_map(); | |
7697 | if (!em) | |
7698 | return ERR_PTR(-ENOMEM); | |
7699 | ||
7700 | em->start = start; | |
7701 | em->orig_start = orig_start; | |
7702 | em->len = len; | |
7703 | em->block_len = block_len; | |
7704 | em->block_start = block_start; | |
7705 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7706 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7707 | em->ram_bytes = ram_bytes; |
70c8a91c | 7708 | em->generation = -1; |
69ffb543 | 7709 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7710 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7711 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7712 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7713 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7714 | em->compress_type = compress_type; | |
7715 | } | |
69ffb543 JB |
7716 | |
7717 | do { | |
dcdbc059 | 7718 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7719 | em->start + em->len - 1, 0); |
7720 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7721 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7722 | write_unlock(&em_tree->lock); |
6f9994db LB |
7723 | /* |
7724 | * The caller has taken lock_extent(), who could race with us | |
7725 | * to add em? | |
7726 | */ | |
69ffb543 JB |
7727 | } while (ret == -EEXIST); |
7728 | ||
7729 | if (ret) { | |
7730 | free_extent_map(em); | |
7731 | return ERR_PTR(ret); | |
7732 | } | |
7733 | ||
6f9994db | 7734 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7735 | return em; |
7736 | } | |
7737 | ||
4b46fce2 JB |
7738 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7739 | struct buffer_head *bh_result, int create) | |
7740 | { | |
0b246afa | 7741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7742 | struct extent_map *em; |
eb838e73 | 7743 | struct extent_state *cached_state = NULL; |
50745b0a | 7744 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7745 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7746 | u64 lockstart, lockend; |
4b46fce2 | 7747 | u64 len = bh_result->b_size; |
eb838e73 | 7748 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7749 | int ret = 0; |
eb838e73 | 7750 | |
172a5049 | 7751 | if (create) |
3266789f | 7752 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7753 | else |
0b246afa | 7754 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7755 | |
c329861d JB |
7756 | lockstart = start; |
7757 | lockend = start + len - 1; | |
7758 | ||
e1cbbfa5 JB |
7759 | if (current->journal_info) { |
7760 | /* | |
7761 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7762 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7763 | * confused. |
7764 | */ | |
50745b0a | 7765 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7766 | current->journal_info = NULL; |
7767 | } | |
7768 | ||
eb838e73 JB |
7769 | /* |
7770 | * If this errors out it's because we couldn't invalidate pagecache for | |
7771 | * this range and we need to fallback to buffered. | |
7772 | */ | |
9c9464cc FM |
7773 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7774 | create)) { | |
7775 | ret = -ENOTBLK; | |
7776 | goto err; | |
7777 | } | |
eb838e73 | 7778 | |
fc4f21b1 | 7779 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7780 | if (IS_ERR(em)) { |
7781 | ret = PTR_ERR(em); | |
7782 | goto unlock_err; | |
7783 | } | |
4b46fce2 JB |
7784 | |
7785 | /* | |
7786 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7787 | * io. INLINE is special, and we could probably kludge it in here, but | |
7788 | * it's still buffered so for safety lets just fall back to the generic | |
7789 | * buffered path. | |
7790 | * | |
7791 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7792 | * decompress it, so there will be buffering required no matter what we | |
7793 | * do, so go ahead and fallback to buffered. | |
7794 | * | |
01327610 | 7795 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7796 | * to buffered IO. Don't blame me, this is the price we pay for using |
7797 | * the generic code. | |
7798 | */ | |
7799 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7800 | em->block_start == EXTENT_MAP_INLINE) { | |
7801 | free_extent_map(em); | |
eb838e73 JB |
7802 | ret = -ENOTBLK; |
7803 | goto unlock_err; | |
4b46fce2 JB |
7804 | } |
7805 | ||
7806 | /* Just a good old fashioned hole, return */ | |
7807 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7808 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7809 | free_extent_map(em); | |
eb838e73 | 7810 | goto unlock_err; |
4b46fce2 JB |
7811 | } |
7812 | ||
7813 | /* | |
7814 | * We don't allocate a new extent in the following cases | |
7815 | * | |
7816 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7817 | * existing extent. | |
7818 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7819 | * just use the extent. | |
7820 | * | |
7821 | */ | |
46bfbb5c | 7822 | if (!create) { |
eb838e73 JB |
7823 | len = min(len, em->len - (start - em->start)); |
7824 | lockstart = start + len; | |
7825 | goto unlock; | |
46bfbb5c | 7826 | } |
4b46fce2 JB |
7827 | |
7828 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7829 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7830 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7831 | int type; |
eb384b55 | 7832 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7833 | |
7834 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7835 | type = BTRFS_ORDERED_PREALLOC; | |
7836 | else | |
7837 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7838 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7839 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7840 | |
00361589 | 7841 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c | 7842 | &orig_block_len, &ram_bytes) == 1 && |
0b246afa | 7843 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
5f9a8a51 | 7844 | struct extent_map *em2; |
0b901916 | 7845 | |
5f9a8a51 FM |
7846 | em2 = btrfs_create_dio_extent(inode, start, len, |
7847 | orig_start, block_start, | |
7848 | len, orig_block_len, | |
7849 | ram_bytes, type); | |
0b246afa | 7850 | btrfs_dec_nocow_writers(fs_info, block_start); |
69ffb543 JB |
7851 | if (type == BTRFS_ORDERED_PREALLOC) { |
7852 | free_extent_map(em); | |
5f9a8a51 | 7853 | em = em2; |
69ffb543 | 7854 | } |
5f9a8a51 FM |
7855 | if (em2 && IS_ERR(em2)) { |
7856 | ret = PTR_ERR(em2); | |
eb838e73 | 7857 | goto unlock_err; |
46bfbb5c | 7858 | } |
18513091 WX |
7859 | /* |
7860 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7861 | * use the existing or preallocated extent, so does not | |
7862 | * need to adjust btrfs_space_info's bytes_may_use. | |
7863 | */ | |
7864 | btrfs_free_reserved_data_space_noquota(inode, | |
7865 | start, len); | |
46bfbb5c | 7866 | goto unlock; |
4b46fce2 | 7867 | } |
4b46fce2 | 7868 | } |
00361589 | 7869 | |
46bfbb5c CM |
7870 | /* |
7871 | * this will cow the extent, reset the len in case we changed | |
7872 | * it above | |
7873 | */ | |
7874 | len = bh_result->b_size; | |
70c8a91c JB |
7875 | free_extent_map(em); |
7876 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7877 | if (IS_ERR(em)) { |
7878 | ret = PTR_ERR(em); | |
7879 | goto unlock_err; | |
7880 | } | |
46bfbb5c CM |
7881 | len = min(len, em->len - (start - em->start)); |
7882 | unlock: | |
4b46fce2 JB |
7883 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7884 | inode->i_blkbits; | |
46bfbb5c | 7885 | bh_result->b_size = len; |
4b46fce2 JB |
7886 | bh_result->b_bdev = em->bdev; |
7887 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7888 | if (create) { |
7889 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7890 | set_buffer_new(bh_result); | |
7891 | ||
7892 | /* | |
7893 | * Need to update the i_size under the extent lock so buffered | |
7894 | * readers will get the updated i_size when we unlock. | |
7895 | */ | |
4aaedfb0 | 7896 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
c3473e83 | 7897 | i_size_write(inode, start + len); |
0934856d | 7898 | |
50745b0a | 7899 | WARN_ON(dio_data->reserve < len); |
7900 | dio_data->reserve -= len; | |
f28a4928 | 7901 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7902 | current->journal_info = dio_data; |
c3473e83 | 7903 | } |
4b46fce2 | 7904 | |
eb838e73 JB |
7905 | /* |
7906 | * In the case of write we need to clear and unlock the entire range, | |
7907 | * in the case of read we need to unlock only the end area that we | |
7908 | * aren't using if there is any left over space. | |
7909 | */ | |
24c03fa5 | 7910 | if (lockstart < lockend) { |
0934856d MX |
7911 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7912 | lockend, unlock_bits, 1, 0, | |
7913 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7914 | } else { |
eb838e73 | 7915 | free_extent_state(cached_state); |
24c03fa5 | 7916 | } |
eb838e73 | 7917 | |
4b46fce2 JB |
7918 | free_extent_map(em); |
7919 | ||
7920 | return 0; | |
eb838e73 JB |
7921 | |
7922 | unlock_err: | |
eb838e73 JB |
7923 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7924 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7925 | err: |
50745b0a | 7926 | if (dio_data) |
7927 | current->journal_info = dio_data; | |
eb838e73 | 7928 | return ret; |
4b46fce2 JB |
7929 | } |
7930 | ||
58efbc9f OS |
7931 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
7932 | struct bio *bio, | |
7933 | int mirror_num) | |
8b110e39 | 7934 | { |
2ff7e61e | 7935 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 7936 | blk_status_t ret; |
8b110e39 | 7937 | |
37226b21 | 7938 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
7939 | |
7940 | bio_get(bio); | |
7941 | ||
2ff7e61e | 7942 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 MX |
7943 | if (ret) |
7944 | goto err; | |
7945 | ||
2ff7e61e | 7946 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
8b110e39 MX |
7947 | err: |
7948 | bio_put(bio); | |
7949 | return ret; | |
7950 | } | |
7951 | ||
7952 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7953 | struct bio *failed_bio, | |
7954 | struct io_failure_record *failrec, | |
7955 | int failed_mirror) | |
7956 | { | |
ab8d0fc4 | 7957 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7958 | int num_copies; |
7959 | ||
ab8d0fc4 | 7960 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7961 | if (num_copies == 1) { |
7962 | /* | |
7963 | * we only have a single copy of the data, so don't bother with | |
7964 | * all the retry and error correction code that follows. no | |
7965 | * matter what the error is, it is very likely to persist. | |
7966 | */ | |
ab8d0fc4 JM |
7967 | btrfs_debug(fs_info, |
7968 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7969 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7970 | return 0; |
7971 | } | |
7972 | ||
7973 | failrec->failed_mirror = failed_mirror; | |
7974 | failrec->this_mirror++; | |
7975 | if (failrec->this_mirror == failed_mirror) | |
7976 | failrec->this_mirror++; | |
7977 | ||
7978 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7979 | btrfs_debug(fs_info, |
7980 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7981 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7982 | return 0; |
7983 | } | |
7984 | ||
7985 | return 1; | |
7986 | } | |
7987 | ||
58efbc9f OS |
7988 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
7989 | struct page *page, unsigned int pgoff, | |
7990 | u64 start, u64 end, int failed_mirror, | |
7991 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7992 | { |
7993 | struct io_failure_record *failrec; | |
7870d082 JB |
7994 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7995 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
7996 | struct bio *bio; |
7997 | int isector; | |
f1c77c55 | 7998 | unsigned int read_mode = 0; |
17347cec | 7999 | int segs; |
8b110e39 | 8000 | int ret; |
58efbc9f | 8001 | blk_status_t status; |
8b110e39 | 8002 | |
37226b21 | 8003 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
8004 | |
8005 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
8006 | if (ret) | |
58efbc9f | 8007 | return errno_to_blk_status(ret); |
8b110e39 MX |
8008 | |
8009 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
8010 | failed_mirror); | |
8011 | if (!ret) { | |
7870d082 | 8012 | free_io_failure(failure_tree, io_tree, failrec); |
58efbc9f | 8013 | return BLK_STS_IOERR; |
8b110e39 MX |
8014 | } |
8015 | ||
17347cec LB |
8016 | segs = bio_segments(failed_bio); |
8017 | if (segs > 1 || | |
8018 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) | |
70fd7614 | 8019 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
8020 | |
8021 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
8022 | isector >>= inode->i_sb->s_blocksize_bits; | |
8023 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 8024 | pgoff, isector, repair_endio, repair_arg); |
37226b21 | 8025 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
8026 | |
8027 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
913e1535 | 8028 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
8b110e39 MX |
8029 | read_mode, failrec->this_mirror, failrec->in_validation); |
8030 | ||
58efbc9f OS |
8031 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8032 | if (status) { | |
7870d082 | 8033 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8034 | bio_put(bio); |
8035 | } | |
8036 | ||
58efbc9f | 8037 | return status; |
8b110e39 MX |
8038 | } |
8039 | ||
8040 | struct btrfs_retry_complete { | |
8041 | struct completion done; | |
8042 | struct inode *inode; | |
8043 | u64 start; | |
8044 | int uptodate; | |
8045 | }; | |
8046 | ||
4246a0b6 | 8047 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
8048 | { |
8049 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 8050 | struct inode *inode = done->inode; |
8b110e39 | 8051 | struct bio_vec *bvec; |
7870d082 | 8052 | struct extent_io_tree *io_tree, *failure_tree; |
8b110e39 MX |
8053 | int i; |
8054 | ||
4e4cbee9 | 8055 | if (bio->bi_status) |
8b110e39 MX |
8056 | goto end; |
8057 | ||
2dabb324 | 8058 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
8059 | io_tree = &BTRFS_I(inode)->io_tree; |
8060 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8061 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); | |
2dabb324 | 8062 | |
8b110e39 | 8063 | done->uptodate = 1; |
c09abff8 | 8064 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8065 | bio_for_each_segment_all(bvec, bio, i) |
7870d082 JB |
8066 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
8067 | io_tree, done->start, bvec->bv_page, | |
8068 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
8069 | end: |
8070 | complete(&done->done); | |
8071 | bio_put(bio); | |
8072 | } | |
8073 | ||
58efbc9f OS |
8074 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
8075 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 8076 | { |
2dabb324 | 8077 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8078 | struct bio_vec bvec; |
8079 | struct bvec_iter iter; | |
8b110e39 | 8080 | struct btrfs_retry_complete done; |
4b46fce2 | 8081 | u64 start; |
2dabb324 CR |
8082 | unsigned int pgoff; |
8083 | u32 sectorsize; | |
8084 | int nr_sectors; | |
58efbc9f OS |
8085 | blk_status_t ret; |
8086 | blk_status_t err = BLK_STS_OK; | |
4b46fce2 | 8087 | |
2dabb324 | 8088 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8089 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8090 | |
8b110e39 MX |
8091 | start = io_bio->logical; |
8092 | done.inode = inode; | |
17347cec | 8093 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8094 | |
17347cec LB |
8095 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8096 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8097 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
8098 | |
8099 | next_block_or_try_again: | |
8b110e39 MX |
8100 | done.uptodate = 0; |
8101 | done.start = start; | |
8102 | init_completion(&done.done); | |
8103 | ||
17347cec | 8104 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8105 | pgoff, start, start + sectorsize - 1, |
8106 | io_bio->mirror_num, | |
8107 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
8108 | if (ret) { |
8109 | err = ret; | |
8110 | goto next; | |
8111 | } | |
8b110e39 | 8112 | |
9c17f6cd | 8113 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8114 | |
8115 | if (!done.uptodate) { | |
8116 | /* We might have another mirror, so try again */ | |
2dabb324 | 8117 | goto next_block_or_try_again; |
8b110e39 MX |
8118 | } |
8119 | ||
629ebf4f | 8120 | next: |
2dabb324 CR |
8121 | start += sectorsize; |
8122 | ||
97bf5a55 LB |
8123 | nr_sectors--; |
8124 | if (nr_sectors) { | |
2dabb324 | 8125 | pgoff += sectorsize; |
97bf5a55 | 8126 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8127 | goto next_block_or_try_again; |
8128 | } | |
8b110e39 MX |
8129 | } |
8130 | ||
629ebf4f | 8131 | return err; |
8b110e39 MX |
8132 | } |
8133 | ||
4246a0b6 | 8134 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8135 | { |
8136 | struct btrfs_retry_complete *done = bio->bi_private; | |
8137 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
8138 | struct extent_io_tree *io_tree, *failure_tree; |
8139 | struct inode *inode = done->inode; | |
8b110e39 MX |
8140 | struct bio_vec *bvec; |
8141 | int uptodate; | |
8142 | int ret; | |
8143 | int i; | |
8144 | ||
4e4cbee9 | 8145 | if (bio->bi_status) |
8b110e39 MX |
8146 | goto end; |
8147 | ||
8148 | uptodate = 1; | |
2dabb324 | 8149 | |
2dabb324 | 8150 | ASSERT(bio->bi_vcnt == 1); |
2e949b0a | 8151 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 8152 | |
7870d082 JB |
8153 | io_tree = &BTRFS_I(inode)->io_tree; |
8154 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8155 | ||
c09abff8 | 8156 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8157 | bio_for_each_segment_all(bvec, bio, i) { |
7870d082 JB |
8158 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
8159 | bvec->bv_offset, done->start, | |
8160 | bvec->bv_len); | |
8b110e39 | 8161 | if (!ret) |
7870d082 JB |
8162 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
8163 | failure_tree, io_tree, done->start, | |
8164 | bvec->bv_page, | |
8165 | btrfs_ino(BTRFS_I(inode)), | |
8166 | bvec->bv_offset); | |
8b110e39 MX |
8167 | else |
8168 | uptodate = 0; | |
8169 | } | |
8170 | ||
8171 | done->uptodate = uptodate; | |
8172 | end: | |
8173 | complete(&done->done); | |
8174 | bio_put(bio); | |
8175 | } | |
8176 | ||
4e4cbee9 CH |
8177 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
8178 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 | 8179 | { |
2dabb324 | 8180 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8181 | struct bio_vec bvec; |
8182 | struct bvec_iter iter; | |
8b110e39 MX |
8183 | struct btrfs_retry_complete done; |
8184 | u64 start; | |
8185 | u64 offset = 0; | |
2dabb324 CR |
8186 | u32 sectorsize; |
8187 | int nr_sectors; | |
8188 | unsigned int pgoff; | |
8189 | int csum_pos; | |
ef7cdac1 | 8190 | bool uptodate = (err == 0); |
8b110e39 | 8191 | int ret; |
58efbc9f | 8192 | blk_status_t status; |
dc380aea | 8193 | |
2dabb324 | 8194 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8195 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8196 | |
58efbc9f | 8197 | err = BLK_STS_OK; |
c1dc0896 | 8198 | start = io_bio->logical; |
8b110e39 | 8199 | done.inode = inode; |
17347cec | 8200 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8201 | |
17347cec LB |
8202 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8203 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 8204 | |
17347cec | 8205 | pgoff = bvec.bv_offset; |
2dabb324 | 8206 | next_block: |
ef7cdac1 LB |
8207 | if (uptodate) { |
8208 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8209 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8210 | bvec.bv_page, pgoff, start, sectorsize); | |
8211 | if (likely(!ret)) | |
8212 | goto next; | |
8213 | } | |
8b110e39 MX |
8214 | try_again: |
8215 | done.uptodate = 0; | |
8216 | done.start = start; | |
8217 | init_completion(&done.done); | |
8218 | ||
58efbc9f OS |
8219 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
8220 | pgoff, start, start + sectorsize - 1, | |
8221 | io_bio->mirror_num, btrfs_retry_endio, | |
8222 | &done); | |
8223 | if (status) { | |
8224 | err = status; | |
8b110e39 MX |
8225 | goto next; |
8226 | } | |
8227 | ||
9c17f6cd | 8228 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8229 | |
8230 | if (!done.uptodate) { | |
8231 | /* We might have another mirror, so try again */ | |
8232 | goto try_again; | |
8233 | } | |
8234 | next: | |
2dabb324 CR |
8235 | offset += sectorsize; |
8236 | start += sectorsize; | |
8237 | ||
8238 | ASSERT(nr_sectors); | |
8239 | ||
97bf5a55 LB |
8240 | nr_sectors--; |
8241 | if (nr_sectors) { | |
2dabb324 | 8242 | pgoff += sectorsize; |
97bf5a55 | 8243 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8244 | goto next_block; |
8245 | } | |
2c30c71b | 8246 | } |
c1dc0896 MX |
8247 | |
8248 | return err; | |
8249 | } | |
8250 | ||
4e4cbee9 CH |
8251 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
8252 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 MX |
8253 | { |
8254 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8255 | ||
8256 | if (skip_csum) { | |
8257 | if (unlikely(err)) | |
8258 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8259 | else | |
58efbc9f | 8260 | return BLK_STS_OK; |
8b110e39 MX |
8261 | } else { |
8262 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8263 | } | |
8264 | } | |
8265 | ||
4246a0b6 | 8266 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8267 | { |
8268 | struct btrfs_dio_private *dip = bio->bi_private; | |
8269 | struct inode *inode = dip->inode; | |
8270 | struct bio *dio_bio; | |
8271 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4e4cbee9 | 8272 | blk_status_t err = bio->bi_status; |
c1dc0896 | 8273 | |
99c4e3b9 | 8274 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8b110e39 | 8275 | err = btrfs_subio_endio_read(inode, io_bio, err); |
c1dc0896 | 8276 | |
4b46fce2 | 8277 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8278 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8279 | dio_bio = dip->dio_bio; |
4b46fce2 | 8280 | |
4b46fce2 | 8281 | kfree(dip); |
c0da7aa1 | 8282 | |
99c4e3b9 | 8283 | dio_bio->bi_status = err; |
4055351c | 8284 | dio_end_io(dio_bio); |
23ea8e5a MX |
8285 | |
8286 | if (io_bio->end_io) | |
4e4cbee9 | 8287 | io_bio->end_io(io_bio, blk_status_to_errno(err)); |
9be3395b | 8288 | bio_put(bio); |
4b46fce2 JB |
8289 | } |
8290 | ||
52427260 QW |
8291 | static void __endio_write_update_ordered(struct inode *inode, |
8292 | const u64 offset, const u64 bytes, | |
8293 | const bool uptodate) | |
4b46fce2 | 8294 | { |
0b246afa | 8295 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8296 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 QW |
8297 | struct btrfs_workqueue *wq; |
8298 | btrfs_work_func_t func; | |
14543774 FM |
8299 | u64 ordered_offset = offset; |
8300 | u64 ordered_bytes = bytes; | |
67c003f9 | 8301 | u64 last_offset; |
4b46fce2 JB |
8302 | int ret; |
8303 | ||
52427260 QW |
8304 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
8305 | wq = fs_info->endio_freespace_worker; | |
8306 | func = btrfs_freespace_write_helper; | |
8307 | } else { | |
8308 | wq = fs_info->endio_write_workers; | |
8309 | func = btrfs_endio_write_helper; | |
8310 | } | |
8311 | ||
163cf09c | 8312 | again: |
67c003f9 | 8313 | last_offset = ordered_offset; |
163cf09c CM |
8314 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
8315 | &ordered_offset, | |
4246a0b6 | 8316 | ordered_bytes, |
14543774 | 8317 | uptodate); |
4b46fce2 | 8318 | if (!ret) |
163cf09c | 8319 | goto out_test; |
4b46fce2 | 8320 | |
52427260 QW |
8321 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
8322 | btrfs_queue_work(wq, &ordered->work); | |
163cf09c | 8323 | out_test: |
67c003f9 NA |
8324 | /* |
8325 | * If btrfs_dec_test_ordered_pending does not find any ordered extent | |
8326 | * in the range, we can exit. | |
8327 | */ | |
8328 | if (ordered_offset == last_offset) | |
8329 | return; | |
163cf09c CM |
8330 | /* |
8331 | * our bio might span multiple ordered extents. If we haven't | |
8332 | * completed the accounting for the whole dio, go back and try again | |
8333 | */ | |
14543774 FM |
8334 | if (ordered_offset < offset + bytes) { |
8335 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8336 | ordered = NULL; |
163cf09c CM |
8337 | goto again; |
8338 | } | |
14543774 FM |
8339 | } |
8340 | ||
8341 | static void btrfs_endio_direct_write(struct bio *bio) | |
8342 | { | |
8343 | struct btrfs_dio_private *dip = bio->bi_private; | |
8344 | struct bio *dio_bio = dip->dio_bio; | |
8345 | ||
52427260 | 8346 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
4e4cbee9 | 8347 | dip->bytes, !bio->bi_status); |
4b46fce2 | 8348 | |
4b46fce2 | 8349 | kfree(dip); |
c0da7aa1 | 8350 | |
4e4cbee9 | 8351 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8352 | dio_end_io(dio_bio); |
9be3395b | 8353 | bio_put(bio); |
4b46fce2 JB |
8354 | } |
8355 | ||
8c27cb35 | 8356 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, |
eaf25d93 CM |
8357 | struct bio *bio, int mirror_num, |
8358 | unsigned long bio_flags, u64 offset) | |
8359 | { | |
c6100a4b | 8360 | struct inode *inode = private_data; |
4e4cbee9 | 8361 | blk_status_t ret; |
2ff7e61e | 8362 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8363 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8364 | return 0; |
8365 | } | |
8366 | ||
4246a0b6 | 8367 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8368 | { |
8369 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 8370 | blk_status_t err = bio->bi_status; |
e65e1535 | 8371 | |
8b110e39 MX |
8372 | if (err) |
8373 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8374 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8375 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8376 | bio->bi_opf, | |
8b110e39 MX |
8377 | (unsigned long long)bio->bi_iter.bi_sector, |
8378 | bio->bi_iter.bi_size, err); | |
8379 | ||
8380 | if (dip->subio_endio) | |
8381 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8382 | |
8383 | if (err) { | |
e65e1535 MX |
8384 | dip->errors = 1; |
8385 | ||
8386 | /* | |
8387 | * before atomic variable goto zero, we must make sure | |
8388 | * dip->errors is perceived to be set. | |
8389 | */ | |
4e857c58 | 8390 | smp_mb__before_atomic(); |
e65e1535 MX |
8391 | } |
8392 | ||
8393 | /* if there are more bios still pending for this dio, just exit */ | |
8394 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8395 | goto out; | |
8396 | ||
9be3395b | 8397 | if (dip->errors) { |
e65e1535 | 8398 | bio_io_error(dip->orig_bio); |
9be3395b | 8399 | } else { |
2dbe0c77 | 8400 | dip->dio_bio->bi_status = BLK_STS_OK; |
4246a0b6 | 8401 | bio_endio(dip->orig_bio); |
e65e1535 MX |
8402 | } |
8403 | out: | |
8404 | bio_put(bio); | |
8405 | } | |
8406 | ||
4e4cbee9 | 8407 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8408 | struct btrfs_dio_private *dip, |
8409 | struct bio *bio, | |
8410 | u64 file_offset) | |
8411 | { | |
8412 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8413 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
4e4cbee9 | 8414 | blk_status_t ret; |
c1dc0896 MX |
8415 | |
8416 | /* | |
8417 | * We load all the csum data we need when we submit | |
8418 | * the first bio to reduce the csum tree search and | |
8419 | * contention. | |
8420 | */ | |
8421 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8422 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8423 | file_offset); |
8424 | if (ret) | |
8425 | return ret; | |
8426 | } | |
8427 | ||
8428 | if (bio == dip->orig_bio) | |
8429 | return 0; | |
8430 | ||
8431 | file_offset -= dip->logical_offset; | |
8432 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8433 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8434 | ||
8435 | return 0; | |
8436 | } | |
8437 | ||
58efbc9f OS |
8438 | static inline blk_status_t |
8439 | __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset, | |
66ba772e | 8440 | int async_submit) |
e65e1535 | 8441 | { |
0b246afa | 8442 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8443 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8444 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 8445 | blk_status_t ret; |
e65e1535 | 8446 | |
b812ce28 JB |
8447 | if (async_submit) |
8448 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8449 | ||
e65e1535 | 8450 | bio_get(bio); |
5fd02043 JB |
8451 | |
8452 | if (!write) { | |
0b246afa | 8453 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8454 | if (ret) |
8455 | goto err; | |
8456 | } | |
e65e1535 | 8457 | |
e6961cac | 8458 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
8459 | goto map; |
8460 | ||
8461 | if (write && async_submit) { | |
c6100a4b JB |
8462 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8463 | file_offset, inode, | |
0b246afa JM |
8464 | __btrfs_submit_bio_start_direct_io, |
8465 | __btrfs_submit_bio_done); | |
e65e1535 | 8466 | goto err; |
1ae39938 JB |
8467 | } else if (write) { |
8468 | /* | |
8469 | * If we aren't doing async submit, calculate the csum of the | |
8470 | * bio now. | |
8471 | */ | |
2ff7e61e | 8472 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8473 | if (ret) |
8474 | goto err; | |
23ea8e5a | 8475 | } else { |
2ff7e61e | 8476 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8477 | file_offset); |
c2db1073 TI |
8478 | if (ret) |
8479 | goto err; | |
8480 | } | |
1ae39938 | 8481 | map: |
9b4a9b28 | 8482 | ret = btrfs_map_bio(fs_info, bio, 0, 0); |
e65e1535 MX |
8483 | err: |
8484 | bio_put(bio); | |
8485 | return ret; | |
8486 | } | |
8487 | ||
e6961cac | 8488 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
e65e1535 MX |
8489 | { |
8490 | struct inode *inode = dip->inode; | |
0b246afa | 8491 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8492 | struct bio *bio; |
8493 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8494 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8495 | u64 file_offset = dip->logical_offset; |
e65e1535 | 8496 | u64 map_length; |
1ae39938 | 8497 | int async_submit = 0; |
725130ba LB |
8498 | u64 submit_len; |
8499 | int clone_offset = 0; | |
8500 | int clone_len; | |
5f4dc8fc | 8501 | int ret; |
58efbc9f | 8502 | blk_status_t status; |
e65e1535 | 8503 | |
4f024f37 | 8504 | map_length = orig_bio->bi_iter.bi_size; |
725130ba | 8505 | submit_len = map_length; |
0b246afa JM |
8506 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8507 | &map_length, NULL, 0); | |
7a5c3c9b | 8508 | if (ret) |
e65e1535 | 8509 | return -EIO; |
facc8a22 | 8510 | |
725130ba | 8511 | if (map_length >= submit_len) { |
02f57c7a | 8512 | bio = orig_bio; |
c1dc0896 | 8513 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8514 | goto submit; |
8515 | } | |
8516 | ||
53b381b3 | 8517 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8518 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8519 | async_submit = 0; |
8520 | else | |
8521 | async_submit = 1; | |
8522 | ||
725130ba LB |
8523 | /* bio split */ |
8524 | ASSERT(map_length <= INT_MAX); | |
02f57c7a | 8525 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8526 | do { |
725130ba | 8527 | clone_len = min_t(int, submit_len, map_length); |
02f57c7a | 8528 | |
725130ba LB |
8529 | /* |
8530 | * This will never fail as it's passing GPF_NOFS and | |
8531 | * the allocation is backed by btrfs_bioset. | |
8532 | */ | |
e477094f | 8533 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8534 | clone_len); |
8535 | bio->bi_private = dip; | |
8536 | bio->bi_end_io = btrfs_end_dio_bio; | |
8537 | btrfs_io_bio(bio)->logical = file_offset; | |
8538 | ||
8539 | ASSERT(submit_len >= clone_len); | |
8540 | submit_len -= clone_len; | |
8541 | if (submit_len == 0) | |
8542 | break; | |
e65e1535 | 8543 | |
725130ba LB |
8544 | /* |
8545 | * Increase the count before we submit the bio so we know | |
8546 | * the end IO handler won't happen before we increase the | |
8547 | * count. Otherwise, the dip might get freed before we're | |
8548 | * done setting it up. | |
8549 | */ | |
8550 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8551 | |
66ba772e | 8552 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
8553 | async_submit); |
8554 | if (status) { | |
725130ba LB |
8555 | bio_put(bio); |
8556 | atomic_dec(&dip->pending_bios); | |
8557 | goto out_err; | |
8558 | } | |
e65e1535 | 8559 | |
725130ba LB |
8560 | clone_offset += clone_len; |
8561 | start_sector += clone_len >> 9; | |
8562 | file_offset += clone_len; | |
5f4dc8fc | 8563 | |
725130ba LB |
8564 | map_length = submit_len; |
8565 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8566 | start_sector << 9, &map_length, NULL, 0); | |
8567 | if (ret) | |
8568 | goto out_err; | |
3c91ee69 | 8569 | } while (submit_len > 0); |
e65e1535 | 8570 | |
02f57c7a | 8571 | submit: |
66ba772e | 8572 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
58efbc9f | 8573 | if (!status) |
e65e1535 MX |
8574 | return 0; |
8575 | ||
8576 | bio_put(bio); | |
8577 | out_err: | |
8578 | dip->errors = 1; | |
8579 | /* | |
8580 | * before atomic variable goto zero, we must | |
8581 | * make sure dip->errors is perceived to be set. | |
8582 | */ | |
4e857c58 | 8583 | smp_mb__before_atomic(); |
e65e1535 MX |
8584 | if (atomic_dec_and_test(&dip->pending_bios)) |
8585 | bio_io_error(dip->orig_bio); | |
8586 | ||
8587 | /* bio_end_io() will handle error, so we needn't return it */ | |
8588 | return 0; | |
8589 | } | |
8590 | ||
8a4c1e42 MC |
8591 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8592 | loff_t file_offset) | |
4b46fce2 | 8593 | { |
61de718f | 8594 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8595 | struct bio *bio = NULL; |
8596 | struct btrfs_io_bio *io_bio; | |
8a4c1e42 | 8597 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8598 | int ret = 0; |
8599 | ||
8b6c1d56 | 8600 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8601 | |
c1dc0896 | 8602 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8603 | if (!dip) { |
8604 | ret = -ENOMEM; | |
61de718f | 8605 | goto free_ordered; |
4b46fce2 | 8606 | } |
4b46fce2 | 8607 | |
9be3395b | 8608 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8609 | dip->inode = inode; |
8610 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8611 | dip->bytes = dio_bio->bi_iter.bi_size; |
8612 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8613 | bio->bi_private = dip; |
8614 | dip->orig_bio = bio; | |
9be3395b | 8615 | dip->dio_bio = dio_bio; |
e65e1535 | 8616 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8617 | io_bio = btrfs_io_bio(bio); |
8618 | io_bio->logical = file_offset; | |
4b46fce2 | 8619 | |
c1dc0896 | 8620 | if (write) { |
3892ac90 | 8621 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8622 | } else { |
3892ac90 | 8623 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8624 | dip->subio_endio = btrfs_subio_endio_read; |
8625 | } | |
4b46fce2 | 8626 | |
f28a4928 FM |
8627 | /* |
8628 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8629 | * even if we fail to submit a bio, because in such case we do the | |
8630 | * corresponding error handling below and it must not be done a second | |
8631 | * time by btrfs_direct_IO(). | |
8632 | */ | |
8633 | if (write) { | |
8634 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8635 | ||
8636 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8637 | dip->bytes; | |
8638 | dio_data->unsubmitted_oe_range_start = | |
8639 | dio_data->unsubmitted_oe_range_end; | |
8640 | } | |
8641 | ||
e6961cac | 8642 | ret = btrfs_submit_direct_hook(dip); |
e65e1535 | 8643 | if (!ret) |
eaf25d93 | 8644 | return; |
9be3395b | 8645 | |
3892ac90 LB |
8646 | if (io_bio->end_io) |
8647 | io_bio->end_io(io_bio, ret); | |
9be3395b | 8648 | |
4b46fce2 JB |
8649 | free_ordered: |
8650 | /* | |
61de718f FM |
8651 | * If we arrived here it means either we failed to submit the dip |
8652 | * or we either failed to clone the dio_bio or failed to allocate the | |
8653 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8654 | * call bio_endio against our io_bio so that we get proper resource | |
8655 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8656 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8657 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8658 | */ |
3892ac90 | 8659 | if (bio && dip) { |
054ec2f6 | 8660 | bio_io_error(bio); |
61de718f | 8661 | /* |
3892ac90 | 8662 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8663 | * and all the cleanup and final put for dio_bio (through |
8664 | * dio_end_io()). | |
8665 | */ | |
8666 | dip = NULL; | |
3892ac90 | 8667 | bio = NULL; |
61de718f | 8668 | } else { |
14543774 | 8669 | if (write) |
52427260 | 8670 | __endio_write_update_ordered(inode, |
14543774 FM |
8671 | file_offset, |
8672 | dio_bio->bi_iter.bi_size, | |
52427260 | 8673 | false); |
14543774 | 8674 | else |
61de718f FM |
8675 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8676 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8677 | |
4e4cbee9 | 8678 | dio_bio->bi_status = BLK_STS_IOERR; |
61de718f FM |
8679 | /* |
8680 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8681 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8682 | */ | |
4055351c | 8683 | dio_end_io(dio_bio); |
4b46fce2 | 8684 | } |
3892ac90 LB |
8685 | if (bio) |
8686 | bio_put(bio); | |
61de718f | 8687 | kfree(dip); |
4b46fce2 JB |
8688 | } |
8689 | ||
2ff7e61e | 8690 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
2ff7e61e | 8691 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8692 | { |
8693 | int seg; | |
a1b75f7d | 8694 | int i; |
0b246afa | 8695 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8696 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8697 | |
8698 | if (offset & blocksize_mask) | |
8699 | goto out; | |
8700 | ||
28060d5d AV |
8701 | if (iov_iter_alignment(iter) & blocksize_mask) |
8702 | goto out; | |
a1b75f7d | 8703 | |
28060d5d | 8704 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8705 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8706 | return 0; |
8707 | /* | |
8708 | * Check to make sure we don't have duplicate iov_base's in this | |
8709 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8710 | * when reading back. | |
8711 | */ | |
8712 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8713 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8714 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8715 | goto out; |
8716 | } | |
5a5f79b5 CM |
8717 | } |
8718 | retval = 0; | |
8719 | out: | |
8720 | return retval; | |
8721 | } | |
eb838e73 | 8722 | |
c8b8e32d | 8723 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8724 | { |
4b46fce2 JB |
8725 | struct file *file = iocb->ki_filp; |
8726 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8727 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8728 | struct btrfs_dio_data dio_data = { 0 }; |
364ecf36 | 8729 | struct extent_changeset *data_reserved = NULL; |
c8b8e32d | 8730 | loff_t offset = iocb->ki_pos; |
0934856d | 8731 | size_t count = 0; |
2e60a51e | 8732 | int flags = 0; |
38851cc1 MX |
8733 | bool wakeup = true; |
8734 | bool relock = false; | |
0934856d | 8735 | ssize_t ret; |
4b46fce2 | 8736 | |
8c70c9f8 | 8737 | if (check_direct_IO(fs_info, iter, offset)) |
5a5f79b5 | 8738 | return 0; |
3f7c579c | 8739 | |
fe0f07d0 | 8740 | inode_dio_begin(inode); |
38851cc1 | 8741 | |
0e267c44 | 8742 | /* |
41bd9ca4 MX |
8743 | * The generic stuff only does filemap_write_and_wait_range, which |
8744 | * isn't enough if we've written compressed pages to this area, so | |
8745 | * we need to flush the dirty pages again to make absolutely sure | |
8746 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8747 | */ |
a6cbcd4a | 8748 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8749 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8750 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8751 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8752 | offset + count - 1); | |
0e267c44 | 8753 | |
6f673763 | 8754 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8755 | /* |
8756 | * If the write DIO is beyond the EOF, we need update | |
8757 | * the isize, but it is protected by i_mutex. So we can | |
8758 | * not unlock the i_mutex at this case. | |
8759 | */ | |
8760 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8761 | dio_data.overwrite = 1; |
5955102c | 8762 | inode_unlock(inode); |
38851cc1 | 8763 | relock = true; |
edf064e7 GR |
8764 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
8765 | ret = -EAGAIN; | |
8766 | goto out; | |
38851cc1 | 8767 | } |
364ecf36 QW |
8768 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8769 | offset, count); | |
0934856d | 8770 | if (ret) |
38851cc1 | 8771 | goto out; |
e1cbbfa5 JB |
8772 | |
8773 | /* | |
8774 | * We need to know how many extents we reserved so that we can | |
8775 | * do the accounting properly if we go over the number we | |
8776 | * originally calculated. Abuse current->journal_info for this. | |
8777 | */ | |
da17066c | 8778 | dio_data.reserve = round_up(count, |
0b246afa | 8779 | fs_info->sectorsize); |
f28a4928 FM |
8780 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8781 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8782 | current->journal_info = &dio_data; |
97dcdea0 | 8783 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8784 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8785 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8786 | inode_dio_end(inode); |
38851cc1 MX |
8787 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8788 | wakeup = false; | |
0934856d MX |
8789 | } |
8790 | ||
17f8c842 | 8791 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8792 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8793 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8794 | btrfs_submit_direct, flags); |
6f673763 | 8795 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8796 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8797 | current->journal_info = NULL; |
ddba1bfc | 8798 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8799 | if (dio_data.reserve) |
bc42bda2 QW |
8800 | btrfs_delalloc_release_space(inode, data_reserved, |
8801 | offset, dio_data.reserve); | |
f28a4928 FM |
8802 | /* |
8803 | * On error we might have left some ordered extents | |
8804 | * without submitting corresponding bios for them, so | |
8805 | * cleanup them up to avoid other tasks getting them | |
8806 | * and waiting for them to complete forever. | |
8807 | */ | |
8808 | if (dio_data.unsubmitted_oe_range_start < | |
8809 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8810 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8811 | dio_data.unsubmitted_oe_range_start, |
8812 | dio_data.unsubmitted_oe_range_end - | |
8813 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8814 | false); |
ddba1bfc | 8815 | } else if (ret >= 0 && (size_t)ret < count) |
bc42bda2 QW |
8816 | btrfs_delalloc_release_space(inode, data_reserved, |
8817 | offset, count - (size_t)ret); | |
69fe2d75 | 8818 | btrfs_delalloc_release_extents(BTRFS_I(inode), count); |
0934856d | 8819 | } |
38851cc1 | 8820 | out: |
2e60a51e | 8821 | if (wakeup) |
fe0f07d0 | 8822 | inode_dio_end(inode); |
38851cc1 | 8823 | if (relock) |
5955102c | 8824 | inode_lock(inode); |
0934856d | 8825 | |
364ecf36 | 8826 | extent_changeset_free(data_reserved); |
0934856d | 8827 | return ret; |
16432985 CM |
8828 | } |
8829 | ||
05dadc09 TI |
8830 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8831 | ||
1506fcc8 YS |
8832 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8833 | __u64 start, __u64 len) | |
8834 | { | |
05dadc09 TI |
8835 | int ret; |
8836 | ||
8837 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8838 | if (ret) | |
8839 | return ret; | |
8840 | ||
ec29ed5b | 8841 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8842 | } |
8843 | ||
a52d9a80 | 8844 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8845 | { |
d1310b2e CM |
8846 | struct extent_io_tree *tree; |
8847 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8848 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8849 | } |
1832a6d5 | 8850 | |
a52d9a80 | 8851 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8852 | { |
d1310b2e | 8853 | struct extent_io_tree *tree; |
be7bd730 JB |
8854 | struct inode *inode = page->mapping->host; |
8855 | int ret; | |
b888db2b CM |
8856 | |
8857 | if (current->flags & PF_MEMALLOC) { | |
8858 | redirty_page_for_writepage(wbc, page); | |
8859 | unlock_page(page); | |
8860 | return 0; | |
8861 | } | |
be7bd730 JB |
8862 | |
8863 | /* | |
8864 | * If we are under memory pressure we will call this directly from the | |
8865 | * VM, we need to make sure we have the inode referenced for the ordered | |
8866 | * extent. If not just return like we didn't do anything. | |
8867 | */ | |
8868 | if (!igrab(inode)) { | |
8869 | redirty_page_for_writepage(wbc, page); | |
8870 | return AOP_WRITEPAGE_ACTIVATE; | |
8871 | } | |
d1310b2e | 8872 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8873 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8874 | btrfs_add_delayed_iput(inode); | |
8875 | return ret; | |
9ebefb18 CM |
8876 | } |
8877 | ||
48a3b636 ES |
8878 | static int btrfs_writepages(struct address_space *mapping, |
8879 | struct writeback_control *wbc) | |
b293f02e | 8880 | { |
d1310b2e | 8881 | struct extent_io_tree *tree; |
771ed689 | 8882 | |
d1310b2e | 8883 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8884 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8885 | } | |
8886 | ||
3ab2fb5a CM |
8887 | static int |
8888 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8889 | struct list_head *pages, unsigned nr_pages) | |
8890 | { | |
d1310b2e CM |
8891 | struct extent_io_tree *tree; |
8892 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8893 | return extent_readpages(tree, mapping, pages, nr_pages, |
8894 | btrfs_get_extent); | |
8895 | } | |
e6dcd2dc | 8896 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8897 | { |
d1310b2e CM |
8898 | struct extent_io_tree *tree; |
8899 | struct extent_map_tree *map; | |
a52d9a80 | 8900 | int ret; |
8c2383c3 | 8901 | |
d1310b2e CM |
8902 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8903 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8904 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8905 | if (ret == 1) { |
8906 | ClearPagePrivate(page); | |
8907 | set_page_private(page, 0); | |
09cbfeaf | 8908 | put_page(page); |
39279cc3 | 8909 | } |
a52d9a80 | 8910 | return ret; |
39279cc3 CM |
8911 | } |
8912 | ||
e6dcd2dc CM |
8913 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8914 | { | |
98509cfc CM |
8915 | if (PageWriteback(page) || PageDirty(page)) |
8916 | return 0; | |
3ba7ab22 | 8917 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8918 | } |
8919 | ||
d47992f8 LC |
8920 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8921 | unsigned int length) | |
39279cc3 | 8922 | { |
5fd02043 | 8923 | struct inode *inode = page->mapping->host; |
d1310b2e | 8924 | struct extent_io_tree *tree; |
e6dcd2dc | 8925 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8926 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8927 | u64 page_start = page_offset(page); |
09cbfeaf | 8928 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8929 | u64 start; |
8930 | u64 end; | |
131e404a | 8931 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8932 | |
8b62b72b CM |
8933 | /* |
8934 | * we have the page locked, so new writeback can't start, | |
8935 | * and the dirty bit won't be cleared while we are here. | |
8936 | * | |
8937 | * Wait for IO on this page so that we can safely clear | |
8938 | * the PagePrivate2 bit and do ordered accounting | |
8939 | */ | |
e6dcd2dc | 8940 | wait_on_page_writeback(page); |
8b62b72b | 8941 | |
5fd02043 | 8942 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8943 | if (offset) { |
8944 | btrfs_releasepage(page, GFP_NOFS); | |
8945 | return; | |
8946 | } | |
131e404a FDBM |
8947 | |
8948 | if (!inode_evicting) | |
ff13db41 | 8949 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8950 | again: |
8951 | start = page_start; | |
a776c6fa | 8952 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8953 | page_end - start + 1); |
e6dcd2dc | 8954 | if (ordered) { |
dbfdb6d1 | 8955 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8956 | /* |
8957 | * IO on this page will never be started, so we need | |
8958 | * to account for any ordered extents now | |
8959 | */ | |
131e404a | 8960 | if (!inode_evicting) |
dbfdb6d1 | 8961 | clear_extent_bit(tree, start, end, |
131e404a | 8962 | EXTENT_DIRTY | EXTENT_DELALLOC | |
a7e3b975 | 8963 | EXTENT_DELALLOC_NEW | |
131e404a FDBM |
8964 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
8965 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8966 | GFP_NOFS); | |
8b62b72b CM |
8967 | /* |
8968 | * whoever cleared the private bit is responsible | |
8969 | * for the finish_ordered_io | |
8970 | */ | |
77cef2ec JB |
8971 | if (TestClearPagePrivate2(page)) { |
8972 | struct btrfs_ordered_inode_tree *tree; | |
8973 | u64 new_len; | |
8974 | ||
8975 | tree = &BTRFS_I(inode)->ordered_tree; | |
8976 | ||
8977 | spin_lock_irq(&tree->lock); | |
8978 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8979 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8980 | if (new_len < ordered->truncated_len) |
8981 | ordered->truncated_len = new_len; | |
8982 | spin_unlock_irq(&tree->lock); | |
8983 | ||
8984 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8985 | start, |
8986 | end - start + 1, 1)) | |
77cef2ec | 8987 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8988 | } |
e6dcd2dc | 8989 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8990 | if (!inode_evicting) { |
8991 | cached_state = NULL; | |
dbfdb6d1 | 8992 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8993 | &cached_state); |
8994 | } | |
dbfdb6d1 CR |
8995 | |
8996 | start = end + 1; | |
8997 | if (start < page_end) | |
8998 | goto again; | |
131e404a FDBM |
8999 | } |
9000 | ||
b9d0b389 QW |
9001 | /* |
9002 | * Qgroup reserved space handler | |
9003 | * Page here will be either | |
9004 | * 1) Already written to disk | |
9005 | * In this case, its reserved space is released from data rsv map | |
9006 | * and will be freed by delayed_ref handler finally. | |
9007 | * So even we call qgroup_free_data(), it won't decrease reserved | |
9008 | * space. | |
9009 | * 2) Not written to disk | |
0b34c261 GR |
9010 | * This means the reserved space should be freed here. However, |
9011 | * if a truncate invalidates the page (by clearing PageDirty) | |
9012 | * and the page is accounted for while allocating extent | |
9013 | * in btrfs_check_data_free_space() we let delayed_ref to | |
9014 | * free the entire extent. | |
b9d0b389 | 9015 | */ |
0b34c261 | 9016 | if (PageDirty(page)) |
bc42bda2 | 9017 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a FDBM |
9018 | if (!inode_evicting) { |
9019 | clear_extent_bit(tree, page_start, page_end, | |
9020 | EXTENT_LOCKED | EXTENT_DIRTY | | |
a7e3b975 FM |
9021 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
9022 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
131e404a FDBM |
9023 | &cached_state, GFP_NOFS); |
9024 | ||
9025 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 9026 | } |
e6dcd2dc | 9027 | |
4a096752 | 9028 | ClearPageChecked(page); |
9ad6b7bc | 9029 | if (PagePrivate(page)) { |
9ad6b7bc CM |
9030 | ClearPagePrivate(page); |
9031 | set_page_private(page, 0); | |
09cbfeaf | 9032 | put_page(page); |
9ad6b7bc | 9033 | } |
39279cc3 CM |
9034 | } |
9035 | ||
9ebefb18 CM |
9036 | /* |
9037 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
9038 | * called from a page fault handler when a page is first dirtied. Hence we must | |
9039 | * be careful to check for EOF conditions here. We set the page up correctly | |
9040 | * for a written page which means we get ENOSPC checking when writing into | |
9041 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
9042 | * support these features. | |
9043 | * | |
9044 | * We are not allowed to take the i_mutex here so we have to play games to | |
9045 | * protect against truncate races as the page could now be beyond EOF. Because | |
9046 | * vmtruncate() writes the inode size before removing pages, once we have the | |
9047 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
9048 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
9049 | * unlock the page. | |
9050 | */ | |
11bac800 | 9051 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 9052 | { |
c2ec175c | 9053 | struct page *page = vmf->page; |
11bac800 | 9054 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 9055 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
9056 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
9057 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 9058 | struct extent_state *cached_state = NULL; |
364ecf36 | 9059 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
9060 | char *kaddr; |
9061 | unsigned long zero_start; | |
9ebefb18 | 9062 | loff_t size; |
1832a6d5 | 9063 | int ret; |
9998eb70 | 9064 | int reserved = 0; |
d0b7da88 | 9065 | u64 reserved_space; |
a52d9a80 | 9066 | u64 page_start; |
e6dcd2dc | 9067 | u64 page_end; |
d0b7da88 CR |
9068 | u64 end; |
9069 | ||
09cbfeaf | 9070 | reserved_space = PAGE_SIZE; |
9ebefb18 | 9071 | |
b2b5ef5c | 9072 | sb_start_pagefault(inode->i_sb); |
df480633 | 9073 | page_start = page_offset(page); |
09cbfeaf | 9074 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 9075 | end = page_end; |
df480633 | 9076 | |
d0b7da88 CR |
9077 | /* |
9078 | * Reserving delalloc space after obtaining the page lock can lead to | |
9079 | * deadlock. For example, if a dirty page is locked by this function | |
9080 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
9081 | * dirty page write out, then the btrfs_writepage() function could | |
9082 | * end up waiting indefinitely to get a lock on the page currently | |
9083 | * being processed by btrfs_page_mkwrite() function. | |
9084 | */ | |
364ecf36 | 9085 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 9086 | reserved_space); |
9998eb70 | 9087 | if (!ret) { |
11bac800 | 9088 | ret = file_update_time(vmf->vma->vm_file); |
9998eb70 CM |
9089 | reserved = 1; |
9090 | } | |
56a76f82 NP |
9091 | if (ret) { |
9092 | if (ret == -ENOMEM) | |
9093 | ret = VM_FAULT_OOM; | |
9094 | else /* -ENOSPC, -EIO, etc */ | |
9095 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
9096 | if (reserved) |
9097 | goto out; | |
9098 | goto out_noreserve; | |
56a76f82 | 9099 | } |
1832a6d5 | 9100 | |
56a76f82 | 9101 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9102 | again: |
9ebefb18 | 9103 | lock_page(page); |
9ebefb18 | 9104 | size = i_size_read(inode); |
a52d9a80 | 9105 | |
9ebefb18 | 9106 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9107 | (page_start >= size)) { |
9ebefb18 CM |
9108 | /* page got truncated out from underneath us */ |
9109 | goto out_unlock; | |
9110 | } | |
e6dcd2dc CM |
9111 | wait_on_page_writeback(page); |
9112 | ||
ff13db41 | 9113 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9114 | set_page_extent_mapped(page); |
9115 | ||
eb84ae03 CM |
9116 | /* |
9117 | * we can't set the delalloc bits if there are pending ordered | |
9118 | * extents. Drop our locks and wait for them to finish | |
9119 | */ | |
a776c6fa NB |
9120 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
9121 | PAGE_SIZE); | |
e6dcd2dc | 9122 | if (ordered) { |
2ac55d41 JB |
9123 | unlock_extent_cached(io_tree, page_start, page_end, |
9124 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9125 | unlock_page(page); |
eb84ae03 | 9126 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9127 | btrfs_put_ordered_extent(ordered); |
9128 | goto again; | |
9129 | } | |
9130 | ||
09cbfeaf | 9131 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9132 | reserved_space = round_up(size - page_start, |
0b246afa | 9133 | fs_info->sectorsize); |
09cbfeaf | 9134 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 | 9135 | end = page_start + reserved_space - 1; |
bc42bda2 QW |
9136 | btrfs_delalloc_release_space(inode, data_reserved, |
9137 | page_start, PAGE_SIZE - reserved_space); | |
d0b7da88 CR |
9138 | } |
9139 | } | |
9140 | ||
fbf19087 | 9141 | /* |
5416034f LB |
9142 | * page_mkwrite gets called when the page is firstly dirtied after it's |
9143 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9144 | * bits, thus in this case for space account reason, we still need to | |
9145 | * clear any delalloc bits within this page range since we have to | |
9146 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 9147 | */ |
d0b7da88 | 9148 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9149 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9150 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9151 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9152 | |
d0b7da88 | 9153 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9154 | &cached_state, 0); |
9ed74f2d | 9155 | if (ret) { |
2ac55d41 JB |
9156 | unlock_extent_cached(io_tree, page_start, page_end, |
9157 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9158 | ret = VM_FAULT_SIGBUS; |
9159 | goto out_unlock; | |
9160 | } | |
e6dcd2dc | 9161 | ret = 0; |
9ebefb18 CM |
9162 | |
9163 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9164 | if (page_start + PAGE_SIZE > size) |
9165 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9166 | else |
09cbfeaf | 9167 | zero_start = PAGE_SIZE; |
9ebefb18 | 9168 | |
09cbfeaf | 9169 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9170 | kaddr = kmap(page); |
09cbfeaf | 9171 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9172 | flush_dcache_page(page); |
9173 | kunmap(page); | |
9174 | } | |
247e743c | 9175 | ClearPageChecked(page); |
e6dcd2dc | 9176 | set_page_dirty(page); |
50a9b214 | 9177 | SetPageUptodate(page); |
5a3f23d5 | 9178 | |
0b246afa | 9179 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9180 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9181 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9182 | |
2ac55d41 | 9183 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9184 | |
9185 | out_unlock: | |
b2b5ef5c | 9186 | if (!ret) { |
8b62f87b | 9187 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
b2b5ef5c | 9188 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9189 | extent_changeset_free(data_reserved); |
50a9b214 | 9190 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9191 | } |
9ebefb18 | 9192 | unlock_page(page); |
1832a6d5 | 9193 | out: |
8b62f87b | 9194 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
bc42bda2 QW |
9195 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
9196 | reserved_space); | |
9998eb70 | 9197 | out_noreserve: |
b2b5ef5c | 9198 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9199 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
9200 | return ret; |
9201 | } | |
9202 | ||
a41ad394 | 9203 | static int btrfs_truncate(struct inode *inode) |
39279cc3 | 9204 | { |
0b246afa | 9205 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9206 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9207 | struct btrfs_block_rsv *rsv; |
a71754fc | 9208 | int ret = 0; |
3893e33b | 9209 | int err = 0; |
39279cc3 | 9210 | struct btrfs_trans_handle *trans; |
0b246afa JM |
9211 | u64 mask = fs_info->sectorsize - 1; |
9212 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 9213 | |
0ef8b726 JB |
9214 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9215 | (u64)-1); | |
9216 | if (ret) | |
9217 | return ret; | |
39279cc3 | 9218 | |
fcb80c2a | 9219 | /* |
01327610 | 9220 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9221 | * 3 things going on here |
9222 | * | |
9223 | * 1) We need to reserve space for our orphan item and the space to | |
9224 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9225 | * orphan item because we didn't reserve space to remove it. | |
9226 | * | |
9227 | * 2) We need to reserve space to update our inode. | |
9228 | * | |
9229 | * 3) We need to have something to cache all the space that is going to | |
9230 | * be free'd up by the truncate operation, but also have some slack | |
9231 | * space reserved in case it uses space during the truncate (thank you | |
9232 | * very much snapshotting). | |
9233 | * | |
01327610 | 9234 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9235 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9236 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9237 | * doesn't end up using space reserved for updating the inode or |
9238 | * removing the orphan item. We also need to be able to stop the | |
9239 | * transaction and start a new one, which means we need to be able to | |
9240 | * update the inode several times, and we have no idea of knowing how | |
9241 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9242 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9243 | * Then there is the orphan item, which does indeed need to be held on |
9244 | * to for the whole operation, and we need nobody to touch this reserved | |
9245 | * space except the orphan code. | |
9246 | * | |
9247 | * So that leaves us with | |
9248 | * | |
9249 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9250 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9251 | * transaction reservation. | |
9252 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9253 | * updating the inode. | |
9254 | */ | |
2ff7e61e | 9255 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9256 | if (!rsv) |
9257 | return -ENOMEM; | |
4a338542 | 9258 | rsv->size = min_size; |
ca7e70f5 | 9259 | rsv->failfast = 1; |
f0cd846e | 9260 | |
907cbceb | 9261 | /* |
07127184 | 9262 | * 1 for the truncate slack space |
907cbceb JB |
9263 | * 1 for updating the inode. |
9264 | */ | |
f3fe820c | 9265 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9266 | if (IS_ERR(trans)) { |
9267 | err = PTR_ERR(trans); | |
9268 | goto out; | |
9269 | } | |
f0cd846e | 9270 | |
907cbceb | 9271 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9272 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
25d609f8 | 9273 | min_size, 0); |
fcb80c2a | 9274 | BUG_ON(ret); |
f0cd846e | 9275 | |
5dc562c5 JB |
9276 | /* |
9277 | * So if we truncate and then write and fsync we normally would just | |
9278 | * write the extents that changed, which is a problem if we need to | |
9279 | * first truncate that entire inode. So set this flag so we write out | |
9280 | * all of the extents in the inode to the sync log so we're completely | |
9281 | * safe. | |
9282 | */ | |
9283 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9284 | trans->block_rsv = rsv; |
907cbceb | 9285 | |
8082510e YZ |
9286 | while (1) { |
9287 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9288 | inode->i_size, | |
9289 | BTRFS_EXTENT_DATA_KEY); | |
ddfae63c | 9290 | trans->block_rsv = &fs_info->trans_block_rsv; |
28ed1345 | 9291 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9292 | err = ret; |
8082510e | 9293 | break; |
3893e33b | 9294 | } |
39279cc3 | 9295 | |
8082510e | 9296 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9297 | if (ret) { |
9298 | err = ret; | |
9299 | break; | |
9300 | } | |
ca7e70f5 | 9301 | |
3a45bb20 | 9302 | btrfs_end_transaction(trans); |
2ff7e61e | 9303 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9304 | |
9305 | trans = btrfs_start_transaction(root, 2); | |
9306 | if (IS_ERR(trans)) { | |
9307 | ret = err = PTR_ERR(trans); | |
9308 | trans = NULL; | |
9309 | break; | |
9310 | } | |
9311 | ||
47b5d646 | 9312 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9313 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
25d609f8 | 9314 | rsv, min_size, 0); |
ca7e70f5 JB |
9315 | BUG_ON(ret); /* shouldn't happen */ |
9316 | trans->block_rsv = rsv; | |
8082510e YZ |
9317 | } |
9318 | ||
ddfae63c JB |
9319 | /* |
9320 | * We can't call btrfs_truncate_block inside a trans handle as we could | |
9321 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know | |
9322 | * we've truncated everything except the last little bit, and can do | |
9323 | * btrfs_truncate_block and then update the disk_i_size. | |
9324 | */ | |
9325 | if (ret == NEED_TRUNCATE_BLOCK) { | |
9326 | btrfs_end_transaction(trans); | |
9327 | btrfs_btree_balance_dirty(fs_info); | |
9328 | ||
9329 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); | |
9330 | if (ret) | |
9331 | goto out; | |
9332 | trans = btrfs_start_transaction(root, 1); | |
9333 | if (IS_ERR(trans)) { | |
9334 | ret = PTR_ERR(trans); | |
9335 | goto out; | |
9336 | } | |
9337 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); | |
9338 | } | |
9339 | ||
8082510e | 9340 | if (ret == 0 && inode->i_nlink > 0) { |
fcb80c2a | 9341 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 9342 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
3893e33b JB |
9343 | if (ret) |
9344 | err = ret; | |
8082510e YZ |
9345 | } |
9346 | ||
917c16b2 | 9347 | if (trans) { |
0b246afa | 9348 | trans->block_rsv = &fs_info->trans_block_rsv; |
917c16b2 CM |
9349 | ret = btrfs_update_inode(trans, root, inode); |
9350 | if (ret && !err) | |
9351 | err = ret; | |
7b128766 | 9352 | |
3a45bb20 | 9353 | ret = btrfs_end_transaction(trans); |
2ff7e61e | 9354 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9355 | } |
fcb80c2a | 9356 | out: |
2ff7e61e | 9357 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9358 | |
3893e33b JB |
9359 | if (ret && !err) |
9360 | err = ret; | |
a41ad394 | 9361 | |
3893e33b | 9362 | return err; |
39279cc3 CM |
9363 | } |
9364 | ||
d352ac68 CM |
9365 | /* |
9366 | * create a new subvolume directory/inode (helper for the ioctl). | |
9367 | */ | |
d2fb3437 | 9368 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9369 | struct btrfs_root *new_root, |
9370 | struct btrfs_root *parent_root, | |
9371 | u64 new_dirid) | |
39279cc3 | 9372 | { |
39279cc3 | 9373 | struct inode *inode; |
76dda93c | 9374 | int err; |
00e4e6b3 | 9375 | u64 index = 0; |
39279cc3 | 9376 | |
12fc9d09 FA |
9377 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9378 | new_dirid, new_dirid, | |
9379 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9380 | &index); | |
54aa1f4d | 9381 | if (IS_ERR(inode)) |
f46b5a66 | 9382 | return PTR_ERR(inode); |
39279cc3 CM |
9383 | inode->i_op = &btrfs_dir_inode_operations; |
9384 | inode->i_fop = &btrfs_dir_file_operations; | |
9385 | ||
bfe86848 | 9386 | set_nlink(inode, 1); |
6ef06d27 | 9387 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9388 | unlock_new_inode(inode); |
3b96362c | 9389 | |
63541927 FDBM |
9390 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9391 | if (err) | |
9392 | btrfs_err(new_root->fs_info, | |
351fd353 | 9393 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9394 | new_root->root_key.objectid, err); |
9395 | ||
76dda93c | 9396 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9397 | |
76dda93c | 9398 | iput(inode); |
ce598979 | 9399 | return err; |
39279cc3 CM |
9400 | } |
9401 | ||
39279cc3 CM |
9402 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9403 | { | |
69fe2d75 | 9404 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 9405 | struct btrfs_inode *ei; |
2ead6ae7 | 9406 | struct inode *inode; |
39279cc3 CM |
9407 | |
9408 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9409 | if (!ei) | |
9410 | return NULL; | |
2ead6ae7 YZ |
9411 | |
9412 | ei->root = NULL; | |
2ead6ae7 | 9413 | ei->generation = 0; |
15ee9bc7 | 9414 | ei->last_trans = 0; |
257c62e1 | 9415 | ei->last_sub_trans = 0; |
e02119d5 | 9416 | ei->logged_trans = 0; |
2ead6ae7 | 9417 | ei->delalloc_bytes = 0; |
a7e3b975 | 9418 | ei->new_delalloc_bytes = 0; |
47059d93 | 9419 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9420 | ei->disk_i_size = 0; |
9421 | ei->flags = 0; | |
7709cde3 | 9422 | ei->csum_bytes = 0; |
2ead6ae7 | 9423 | ei->index_cnt = (u64)-1; |
67de1176 | 9424 | ei->dir_index = 0; |
2ead6ae7 | 9425 | ei->last_unlink_trans = 0; |
46d8bc34 | 9426 | ei->last_log_commit = 0; |
8089fe62 | 9427 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9428 | |
9e0baf60 JB |
9429 | spin_lock_init(&ei->lock); |
9430 | ei->outstanding_extents = 0; | |
69fe2d75 JB |
9431 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
9432 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, | |
9433 | BTRFS_BLOCK_RSV_DELALLOC); | |
72ac3c0d | 9434 | ei->runtime_flags = 0; |
b52aa8c9 | 9435 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 9436 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9437 | |
16cdcec7 MX |
9438 | ei->delayed_node = NULL; |
9439 | ||
9cc97d64 | 9440 | ei->i_otime.tv_sec = 0; |
9441 | ei->i_otime.tv_nsec = 0; | |
9442 | ||
2ead6ae7 | 9443 | inode = &ei->vfs_inode; |
a8067e02 | 9444 | extent_map_tree_init(&ei->extent_tree); |
c6100a4b JB |
9445 | extent_io_tree_init(&ei->io_tree, inode); |
9446 | extent_io_tree_init(&ei->io_failure_tree, inode); | |
0b32f4bb JB |
9447 | ei->io_tree.track_uptodate = 1; |
9448 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9449 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9450 | mutex_init(&ei->log_mutex); |
f248679e | 9451 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9452 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9453 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9454 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9455 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9456 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9457 | |
9458 | return inode; | |
39279cc3 CM |
9459 | } |
9460 | ||
aaedb55b JB |
9461 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9462 | void btrfs_test_destroy_inode(struct inode *inode) | |
9463 | { | |
dcdbc059 | 9464 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9465 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9466 | } | |
9467 | #endif | |
9468 | ||
fa0d7e3d NP |
9469 | static void btrfs_i_callback(struct rcu_head *head) |
9470 | { | |
9471 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9472 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9473 | } | |
9474 | ||
39279cc3 CM |
9475 | void btrfs_destroy_inode(struct inode *inode) |
9476 | { | |
0b246afa | 9477 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9478 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9479 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9480 | ||
b3d9b7a3 | 9481 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9482 | WARN_ON(inode->i_data.nrpages); |
69fe2d75 JB |
9483 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
9484 | WARN_ON(BTRFS_I(inode)->block_rsv.size); | |
9e0baf60 | 9485 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7709cde3 | 9486 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9487 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9488 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9489 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9490 | |
a6dbd429 JB |
9491 | /* |
9492 | * This can happen where we create an inode, but somebody else also | |
9493 | * created the same inode and we need to destroy the one we already | |
9494 | * created. | |
9495 | */ | |
9496 | if (!root) | |
9497 | goto free; | |
9498 | ||
8a35d95f JB |
9499 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9500 | &BTRFS_I(inode)->runtime_flags)) { | |
0b246afa | 9501 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
4a0cc7ca | 9502 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 9503 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9504 | } |
7b128766 | 9505 | |
d397712b | 9506 | while (1) { |
e6dcd2dc CM |
9507 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9508 | if (!ordered) | |
9509 | break; | |
9510 | else { | |
0b246afa | 9511 | btrfs_err(fs_info, |
5d163e0e JM |
9512 | "found ordered extent %llu %llu on inode cleanup", |
9513 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9514 | btrfs_remove_ordered_extent(inode, ordered); |
9515 | btrfs_put_ordered_extent(ordered); | |
9516 | btrfs_put_ordered_extent(ordered); | |
9517 | } | |
9518 | } | |
56fa9d07 | 9519 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9520 | inode_tree_del(inode); |
dcdbc059 | 9521 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
a6dbd429 | 9522 | free: |
fa0d7e3d | 9523 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9524 | } |
9525 | ||
45321ac5 | 9526 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9527 | { |
9528 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9529 | |
6379ef9f NA |
9530 | if (root == NULL) |
9531 | return 1; | |
9532 | ||
fa6ac876 | 9533 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9534 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9535 | return 1; |
76dda93c | 9536 | else |
45321ac5 | 9537 | return generic_drop_inode(inode); |
76dda93c YZ |
9538 | } |
9539 | ||
0ee0fda0 | 9540 | static void init_once(void *foo) |
39279cc3 CM |
9541 | { |
9542 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9543 | ||
9544 | inode_init_once(&ei->vfs_inode); | |
9545 | } | |
9546 | ||
9547 | void btrfs_destroy_cachep(void) | |
9548 | { | |
8c0a8537 KS |
9549 | /* |
9550 | * Make sure all delayed rcu free inodes are flushed before we | |
9551 | * destroy cache. | |
9552 | */ | |
9553 | rcu_barrier(); | |
5598e900 KM |
9554 | kmem_cache_destroy(btrfs_inode_cachep); |
9555 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9556 | kmem_cache_destroy(btrfs_path_cachep); |
9557 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9558 | } |
9559 | ||
9560 | int btrfs_init_cachep(void) | |
9561 | { | |
837e1972 | 9562 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9563 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9564 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9565 | init_once); | |
39279cc3 CM |
9566 | if (!btrfs_inode_cachep) |
9567 | goto fail; | |
9601e3f6 | 9568 | |
837e1972 | 9569 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9570 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9571 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9572 | if (!btrfs_trans_handle_cachep) |
9573 | goto fail; | |
9601e3f6 | 9574 | |
837e1972 | 9575 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9576 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9577 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9578 | if (!btrfs_path_cachep) |
9579 | goto fail; | |
9601e3f6 | 9580 | |
837e1972 | 9581 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9582 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9583 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9584 | if (!btrfs_free_space_cachep) |
9585 | goto fail; | |
9586 | ||
39279cc3 CM |
9587 | return 0; |
9588 | fail: | |
9589 | btrfs_destroy_cachep(); | |
9590 | return -ENOMEM; | |
9591 | } | |
9592 | ||
a528d35e DH |
9593 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9594 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9595 | { |
df0af1a5 | 9596 | u64 delalloc_bytes; |
a528d35e | 9597 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9598 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9599 | u32 bi_flags = BTRFS_I(inode)->flags; |
9600 | ||
9601 | stat->result_mask |= STATX_BTIME; | |
9602 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9603 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9604 | if (bi_flags & BTRFS_INODE_APPEND) | |
9605 | stat->attributes |= STATX_ATTR_APPEND; | |
9606 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9607 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9608 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9609 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9610 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9611 | stat->attributes |= STATX_ATTR_NODUMP; | |
9612 | ||
9613 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9614 | STATX_ATTR_COMPRESSED | | |
9615 | STATX_ATTR_IMMUTABLE | | |
9616 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9617 | |
39279cc3 | 9618 | generic_fillattr(inode, stat); |
0ee5dc67 | 9619 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9620 | |
9621 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9622 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9623 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9624 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9625 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9626 | return 0; |
9627 | } | |
9628 | ||
cdd1fedf DF |
9629 | static int btrfs_rename_exchange(struct inode *old_dir, |
9630 | struct dentry *old_dentry, | |
9631 | struct inode *new_dir, | |
9632 | struct dentry *new_dentry) | |
9633 | { | |
0b246afa | 9634 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9635 | struct btrfs_trans_handle *trans; |
9636 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9637 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9638 | struct inode *new_inode = new_dentry->d_inode; | |
9639 | struct inode *old_inode = old_dentry->d_inode; | |
c2050a45 | 9640 | struct timespec ctime = current_time(old_inode); |
cdd1fedf | 9641 | struct dentry *parent; |
4a0cc7ca NB |
9642 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9643 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9644 | u64 old_idx = 0; |
9645 | u64 new_idx = 0; | |
9646 | u64 root_objectid; | |
9647 | int ret; | |
86e8aa0e FM |
9648 | bool root_log_pinned = false; |
9649 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9650 | |
9651 | /* we only allow rename subvolume link between subvolumes */ | |
9652 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9653 | return -EXDEV; | |
9654 | ||
9655 | /* close the race window with snapshot create/destroy ioctl */ | |
9656 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9657 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9658 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9659 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9660 | |
9661 | /* | |
9662 | * We want to reserve the absolute worst case amount of items. So if | |
9663 | * both inodes are subvols and we need to unlink them then that would | |
9664 | * require 4 item modifications, but if they are both normal inodes it | |
9665 | * would require 5 item modifications, so we'll assume their normal | |
9666 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9667 | * should cover the worst case number of items we'll modify. | |
9668 | */ | |
9669 | trans = btrfs_start_transaction(root, 12); | |
9670 | if (IS_ERR(trans)) { | |
9671 | ret = PTR_ERR(trans); | |
9672 | goto out_notrans; | |
9673 | } | |
9674 | ||
9675 | /* | |
9676 | * We need to find a free sequence number both in the source and | |
9677 | * in the destination directory for the exchange. | |
9678 | */ | |
877574e2 | 9679 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9680 | if (ret) |
9681 | goto out_fail; | |
877574e2 | 9682 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9683 | if (ret) |
9684 | goto out_fail; | |
9685 | ||
9686 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9687 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9688 | ||
9689 | /* Reference for the source. */ | |
9690 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9691 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9692 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9693 | } else { |
376e5a57 FM |
9694 | btrfs_pin_log_trans(root); |
9695 | root_log_pinned = true; | |
cdd1fedf DF |
9696 | ret = btrfs_insert_inode_ref(trans, dest, |
9697 | new_dentry->d_name.name, | |
9698 | new_dentry->d_name.len, | |
9699 | old_ino, | |
f85b7379 DS |
9700 | btrfs_ino(BTRFS_I(new_dir)), |
9701 | old_idx); | |
cdd1fedf DF |
9702 | if (ret) |
9703 | goto out_fail; | |
cdd1fedf DF |
9704 | } |
9705 | ||
9706 | /* And now for the dest. */ | |
9707 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9708 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9709 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9710 | } else { |
376e5a57 FM |
9711 | btrfs_pin_log_trans(dest); |
9712 | dest_log_pinned = true; | |
cdd1fedf DF |
9713 | ret = btrfs_insert_inode_ref(trans, root, |
9714 | old_dentry->d_name.name, | |
9715 | old_dentry->d_name.len, | |
9716 | new_ino, | |
f85b7379 DS |
9717 | btrfs_ino(BTRFS_I(old_dir)), |
9718 | new_idx); | |
cdd1fedf DF |
9719 | if (ret) |
9720 | goto out_fail; | |
cdd1fedf DF |
9721 | } |
9722 | ||
9723 | /* Update inode version and ctime/mtime. */ | |
9724 | inode_inc_iversion(old_dir); | |
9725 | inode_inc_iversion(new_dir); | |
9726 | inode_inc_iversion(old_inode); | |
9727 | inode_inc_iversion(new_inode); | |
9728 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9729 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9730 | old_inode->i_ctime = ctime; | |
9731 | new_inode->i_ctime = ctime; | |
9732 | ||
9733 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9734 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9735 | BTRFS_I(old_inode), 1); | |
9736 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9737 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9738 | } |
9739 | ||
9740 | /* src is a subvolume */ | |
9741 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9742 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9743 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9744 | root_objectid, | |
9745 | old_dentry->d_name.name, | |
9746 | old_dentry->d_name.len); | |
9747 | } else { /* src is an inode */ | |
4ec5934e NB |
9748 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9749 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9750 | old_dentry->d_name.name, |
9751 | old_dentry->d_name.len); | |
9752 | if (!ret) | |
9753 | ret = btrfs_update_inode(trans, root, old_inode); | |
9754 | } | |
9755 | if (ret) { | |
66642832 | 9756 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9757 | goto out_fail; |
9758 | } | |
9759 | ||
9760 | /* dest is a subvolume */ | |
9761 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9762 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9763 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9764 | root_objectid, | |
9765 | new_dentry->d_name.name, | |
9766 | new_dentry->d_name.len); | |
9767 | } else { /* dest is an inode */ | |
4ec5934e NB |
9768 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9769 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9770 | new_dentry->d_name.name, |
9771 | new_dentry->d_name.len); | |
9772 | if (!ret) | |
9773 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9774 | } | |
9775 | if (ret) { | |
66642832 | 9776 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9777 | goto out_fail; |
9778 | } | |
9779 | ||
db0a669f | 9780 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9781 | new_dentry->d_name.name, |
9782 | new_dentry->d_name.len, 0, old_idx); | |
9783 | if (ret) { | |
66642832 | 9784 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9785 | goto out_fail; |
9786 | } | |
9787 | ||
db0a669f | 9788 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9789 | old_dentry->d_name.name, |
9790 | old_dentry->d_name.len, 0, new_idx); | |
9791 | if (ret) { | |
66642832 | 9792 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9793 | goto out_fail; |
9794 | } | |
9795 | ||
9796 | if (old_inode->i_nlink == 1) | |
9797 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9798 | if (new_inode->i_nlink == 1) | |
9799 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9800 | ||
86e8aa0e | 9801 | if (root_log_pinned) { |
cdd1fedf | 9802 | parent = new_dentry->d_parent; |
f85b7379 DS |
9803 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9804 | parent); | |
cdd1fedf | 9805 | btrfs_end_log_trans(root); |
86e8aa0e | 9806 | root_log_pinned = false; |
cdd1fedf | 9807 | } |
86e8aa0e | 9808 | if (dest_log_pinned) { |
cdd1fedf | 9809 | parent = old_dentry->d_parent; |
f85b7379 DS |
9810 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
9811 | parent); | |
cdd1fedf | 9812 | btrfs_end_log_trans(dest); |
86e8aa0e | 9813 | dest_log_pinned = false; |
cdd1fedf DF |
9814 | } |
9815 | out_fail: | |
86e8aa0e FM |
9816 | /* |
9817 | * If we have pinned a log and an error happened, we unpin tasks | |
9818 | * trying to sync the log and force them to fallback to a transaction | |
9819 | * commit if the log currently contains any of the inodes involved in | |
9820 | * this rename operation (to ensure we do not persist a log with an | |
9821 | * inconsistent state for any of these inodes or leading to any | |
9822 | * inconsistencies when replayed). If the transaction was aborted, the | |
9823 | * abortion reason is propagated to userspace when attempting to commit | |
9824 | * the transaction. If the log does not contain any of these inodes, we | |
9825 | * allow the tasks to sync it. | |
9826 | */ | |
9827 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9828 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9829 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9830 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9831 | (new_inode && |
0f8939b8 | 9832 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9833 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9834 | |
9835 | if (root_log_pinned) { | |
9836 | btrfs_end_log_trans(root); | |
9837 | root_log_pinned = false; | |
9838 | } | |
9839 | if (dest_log_pinned) { | |
9840 | btrfs_end_log_trans(dest); | |
9841 | dest_log_pinned = false; | |
9842 | } | |
9843 | } | |
3a45bb20 | 9844 | ret = btrfs_end_transaction(trans); |
cdd1fedf DF |
9845 | out_notrans: |
9846 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9847 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9848 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9849 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9850 | |
9851 | return ret; | |
9852 | } | |
9853 | ||
9854 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9855 | struct btrfs_root *root, | |
9856 | struct inode *dir, | |
9857 | struct dentry *dentry) | |
9858 | { | |
9859 | int ret; | |
9860 | struct inode *inode; | |
9861 | u64 objectid; | |
9862 | u64 index; | |
9863 | ||
9864 | ret = btrfs_find_free_ino(root, &objectid); | |
9865 | if (ret) | |
9866 | return ret; | |
9867 | ||
9868 | inode = btrfs_new_inode(trans, root, dir, | |
9869 | dentry->d_name.name, | |
9870 | dentry->d_name.len, | |
4a0cc7ca | 9871 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9872 | objectid, |
9873 | S_IFCHR | WHITEOUT_MODE, | |
9874 | &index); | |
9875 | ||
9876 | if (IS_ERR(inode)) { | |
9877 | ret = PTR_ERR(inode); | |
9878 | return ret; | |
9879 | } | |
9880 | ||
9881 | inode->i_op = &btrfs_special_inode_operations; | |
9882 | init_special_inode(inode, inode->i_mode, | |
9883 | WHITEOUT_DEV); | |
9884 | ||
9885 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9886 | &dentry->d_name); | |
9887 | if (ret) | |
c9901618 | 9888 | goto out; |
cdd1fedf | 9889 | |
cef415af NB |
9890 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9891 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9892 | if (ret) |
c9901618 | 9893 | goto out; |
cdd1fedf DF |
9894 | |
9895 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9896 | out: |
cdd1fedf | 9897 | unlock_new_inode(inode); |
c9901618 FM |
9898 | if (ret) |
9899 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9900 | iput(inode); |
9901 | ||
c9901618 | 9902 | return ret; |
cdd1fedf DF |
9903 | } |
9904 | ||
d397712b | 9905 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9906 | struct inode *new_dir, struct dentry *new_dentry, |
9907 | unsigned int flags) | |
39279cc3 | 9908 | { |
0b246afa | 9909 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9910 | struct btrfs_trans_handle *trans; |
5062af35 | 9911 | unsigned int trans_num_items; |
39279cc3 | 9912 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9913 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9914 | struct inode *new_inode = d_inode(new_dentry); |
9915 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9916 | u64 index = 0; |
4df27c4d | 9917 | u64 root_objectid; |
39279cc3 | 9918 | int ret; |
4a0cc7ca | 9919 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9920 | bool log_pinned = false; |
39279cc3 | 9921 | |
4a0cc7ca | 9922 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9923 | return -EPERM; |
9924 | ||
4df27c4d | 9925 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9926 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9927 | return -EXDEV; |
9928 | ||
33345d01 | 9929 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9930 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9931 | return -ENOTEMPTY; |
5f39d397 | 9932 | |
4df27c4d YZ |
9933 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9934 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9935 | return -ENOTEMPTY; | |
9c52057c CM |
9936 | |
9937 | ||
9938 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9939 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9940 | new_dentry->d_name.name, |
9941 | new_dentry->d_name.len); | |
9942 | ||
9943 | if (ret) { | |
9944 | if (ret == -EEXIST) { | |
9945 | /* we shouldn't get | |
9946 | * eexist without a new_inode */ | |
fae7f21c | 9947 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9948 | return ret; |
9949 | } | |
9950 | } else { | |
9951 | /* maybe -EOVERFLOW */ | |
9952 | return ret; | |
9953 | } | |
9954 | } | |
9955 | ret = 0; | |
9956 | ||
5a3f23d5 | 9957 | /* |
8d875f95 CM |
9958 | * we're using rename to replace one file with another. Start IO on it |
9959 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9960 | */ |
8d875f95 | 9961 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9962 | filemap_flush(old_inode->i_mapping); |
9963 | ||
76dda93c | 9964 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9965 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9966 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9967 | /* |
9968 | * We want to reserve the absolute worst case amount of items. So if | |
9969 | * both inodes are subvols and we need to unlink them then that would | |
9970 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9971 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9972 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9973 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9974 | * If our rename has the whiteout flag, we need more 5 units for the |
9975 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9976 | * when selinux is enabled). | |
a22285a6 | 9977 | */ |
5062af35 FM |
9978 | trans_num_items = 11; |
9979 | if (flags & RENAME_WHITEOUT) | |
9980 | trans_num_items += 5; | |
9981 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9982 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9983 | ret = PTR_ERR(trans); |
9984 | goto out_notrans; | |
9985 | } | |
76dda93c | 9986 | |
4df27c4d YZ |
9987 | if (dest != root) |
9988 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9989 | |
877574e2 | 9990 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9991 | if (ret) |
9992 | goto out_fail; | |
5a3f23d5 | 9993 | |
67de1176 | 9994 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9995 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9996 | /* force full log commit if subvolume involved. */ |
0b246afa | 9997 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 9998 | } else { |
c4aba954 FM |
9999 | btrfs_pin_log_trans(root); |
10000 | log_pinned = true; | |
a5719521 YZ |
10001 | ret = btrfs_insert_inode_ref(trans, dest, |
10002 | new_dentry->d_name.name, | |
10003 | new_dentry->d_name.len, | |
33345d01 | 10004 | old_ino, |
4a0cc7ca | 10005 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
10006 | if (ret) |
10007 | goto out_fail; | |
4df27c4d | 10008 | } |
5a3f23d5 | 10009 | |
0c4d2d95 JB |
10010 | inode_inc_iversion(old_dir); |
10011 | inode_inc_iversion(new_dir); | |
10012 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
10013 | old_dir->i_ctime = old_dir->i_mtime = |
10014 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 10015 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 10016 | |
12fcfd22 | 10017 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
10018 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
10019 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 10020 | |
33345d01 | 10021 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
10022 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
10023 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
10024 | old_dentry->d_name.name, | |
10025 | old_dentry->d_name.len); | |
10026 | } else { | |
4ec5934e NB |
10027 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
10028 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
10029 | old_dentry->d_name.name, |
10030 | old_dentry->d_name.len); | |
10031 | if (!ret) | |
10032 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 10033 | } |
79787eaa | 10034 | if (ret) { |
66642832 | 10035 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10036 | goto out_fail; |
10037 | } | |
39279cc3 CM |
10038 | |
10039 | if (new_inode) { | |
0c4d2d95 | 10040 | inode_inc_iversion(new_inode); |
c2050a45 | 10041 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 10042 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
10043 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
10044 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
10045 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
10046 | root_objectid, | |
10047 | new_dentry->d_name.name, | |
10048 | new_dentry->d_name.len); | |
10049 | BUG_ON(new_inode->i_nlink == 0); | |
10050 | } else { | |
4ec5934e NB |
10051 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
10052 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
10053 | new_dentry->d_name.name, |
10054 | new_dentry->d_name.len); | |
10055 | } | |
4ef31a45 | 10056 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
10057 | ret = btrfs_orphan_add(trans, |
10058 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 10059 | if (ret) { |
66642832 | 10060 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10061 | goto out_fail; |
10062 | } | |
39279cc3 | 10063 | } |
aec7477b | 10064 | |
db0a669f | 10065 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 10066 | new_dentry->d_name.name, |
a5719521 | 10067 | new_dentry->d_name.len, 0, index); |
79787eaa | 10068 | if (ret) { |
66642832 | 10069 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10070 | goto out_fail; |
10071 | } | |
39279cc3 | 10072 | |
67de1176 MX |
10073 | if (old_inode->i_nlink == 1) |
10074 | BTRFS_I(old_inode)->dir_index = index; | |
10075 | ||
3dc9e8f7 | 10076 | if (log_pinned) { |
10d9f309 | 10077 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 10078 | |
f85b7379 DS |
10079 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
10080 | parent); | |
4df27c4d | 10081 | btrfs_end_log_trans(root); |
3dc9e8f7 | 10082 | log_pinned = false; |
4df27c4d | 10083 | } |
cdd1fedf DF |
10084 | |
10085 | if (flags & RENAME_WHITEOUT) { | |
10086 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10087 | old_dentry); | |
10088 | ||
10089 | if (ret) { | |
66642832 | 10090 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
10091 | goto out_fail; |
10092 | } | |
4df27c4d | 10093 | } |
39279cc3 | 10094 | out_fail: |
3dc9e8f7 FM |
10095 | /* |
10096 | * If we have pinned the log and an error happened, we unpin tasks | |
10097 | * trying to sync the log and force them to fallback to a transaction | |
10098 | * commit if the log currently contains any of the inodes involved in | |
10099 | * this rename operation (to ensure we do not persist a log with an | |
10100 | * inconsistent state for any of these inodes or leading to any | |
10101 | * inconsistencies when replayed). If the transaction was aborted, the | |
10102 | * abortion reason is propagated to userspace when attempting to commit | |
10103 | * the transaction. If the log does not contain any of these inodes, we | |
10104 | * allow the tasks to sync it. | |
10105 | */ | |
10106 | if (ret && log_pinned) { | |
0f8939b8 NB |
10107 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
10108 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10109 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 10110 | (new_inode && |
0f8939b8 | 10111 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 10112 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
10113 | |
10114 | btrfs_end_log_trans(root); | |
10115 | log_pinned = false; | |
10116 | } | |
3a45bb20 | 10117 | btrfs_end_transaction(trans); |
b44c59a8 | 10118 | out_notrans: |
33345d01 | 10119 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10120 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 10121 | |
39279cc3 CM |
10122 | return ret; |
10123 | } | |
10124 | ||
80ace85c MS |
10125 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
10126 | struct inode *new_dir, struct dentry *new_dentry, | |
10127 | unsigned int flags) | |
10128 | { | |
cdd1fedf | 10129 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
10130 | return -EINVAL; |
10131 | ||
cdd1fedf DF |
10132 | if (flags & RENAME_EXCHANGE) |
10133 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10134 | new_dentry); | |
10135 | ||
10136 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
10137 | } |
10138 | ||
8ccf6f19 MX |
10139 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
10140 | { | |
10141 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 10142 | struct inode *inode; |
8ccf6f19 MX |
10143 | |
10144 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10145 | work); | |
9f23e289 | 10146 | inode = delalloc_work->inode; |
30424601 DS |
10147 | filemap_flush(inode->i_mapping); |
10148 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10149 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10150 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10151 | |
10152 | if (delalloc_work->delay_iput) | |
9f23e289 | 10153 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10154 | else |
9f23e289 | 10155 | iput(inode); |
8ccf6f19 MX |
10156 | complete(&delalloc_work->completion); |
10157 | } | |
10158 | ||
10159 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10160 | int delay_iput) |
8ccf6f19 MX |
10161 | { |
10162 | struct btrfs_delalloc_work *work; | |
10163 | ||
100d5702 | 10164 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10165 | if (!work) |
10166 | return NULL; | |
10167 | ||
10168 | init_completion(&work->completion); | |
10169 | INIT_LIST_HEAD(&work->list); | |
10170 | work->inode = inode; | |
8ccf6f19 | 10171 | work->delay_iput = delay_iput; |
9e0af237 LB |
10172 | WARN_ON_ONCE(!inode); |
10173 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10174 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10175 | |
10176 | return work; | |
10177 | } | |
10178 | ||
10179 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10180 | { | |
10181 | wait_for_completion(&work->completion); | |
100d5702 | 10182 | kfree(work); |
8ccf6f19 MX |
10183 | } |
10184 | ||
d352ac68 CM |
10185 | /* |
10186 | * some fairly slow code that needs optimization. This walks the list | |
10187 | * of all the inodes with pending delalloc and forces them to disk. | |
10188 | */ | |
6c255e67 MX |
10189 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10190 | int nr) | |
ea8c2819 | 10191 | { |
ea8c2819 | 10192 | struct btrfs_inode *binode; |
5b21f2ed | 10193 | struct inode *inode; |
8ccf6f19 MX |
10194 | struct btrfs_delalloc_work *work, *next; |
10195 | struct list_head works; | |
1eafa6c7 | 10196 | struct list_head splice; |
8ccf6f19 | 10197 | int ret = 0; |
ea8c2819 | 10198 | |
8ccf6f19 | 10199 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10200 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10201 | |
573bfb72 | 10202 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10203 | spin_lock(&root->delalloc_lock); |
10204 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10205 | while (!list_empty(&splice)) { |
10206 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10207 | delalloc_inodes); |
1eafa6c7 | 10208 | |
eb73c1b7 MX |
10209 | list_move_tail(&binode->delalloc_inodes, |
10210 | &root->delalloc_inodes); | |
5b21f2ed | 10211 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10212 | if (!inode) { |
eb73c1b7 | 10213 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10214 | continue; |
df0af1a5 | 10215 | } |
eb73c1b7 | 10216 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10217 | |
651d494a | 10218 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10219 | if (!work) { |
f4ab9ea7 JB |
10220 | if (delay_iput) |
10221 | btrfs_add_delayed_iput(inode); | |
10222 | else | |
10223 | iput(inode); | |
1eafa6c7 | 10224 | ret = -ENOMEM; |
a1ecaabb | 10225 | goto out; |
5b21f2ed | 10226 | } |
1eafa6c7 | 10227 | list_add_tail(&work->list, &works); |
a44903ab QW |
10228 | btrfs_queue_work(root->fs_info->flush_workers, |
10229 | &work->work); | |
6c255e67 MX |
10230 | ret++; |
10231 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10232 | goto out; |
5b21f2ed | 10233 | cond_resched(); |
eb73c1b7 | 10234 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10235 | } |
eb73c1b7 | 10236 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10237 | |
a1ecaabb | 10238 | out: |
eb73c1b7 MX |
10239 | list_for_each_entry_safe(work, next, &works, list) { |
10240 | list_del_init(&work->list); | |
10241 | btrfs_wait_and_free_delalloc_work(work); | |
10242 | } | |
10243 | ||
10244 | if (!list_empty_careful(&splice)) { | |
10245 | spin_lock(&root->delalloc_lock); | |
10246 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10247 | spin_unlock(&root->delalloc_lock); | |
10248 | } | |
573bfb72 | 10249 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10250 | return ret; |
10251 | } | |
1eafa6c7 | 10252 | |
eb73c1b7 MX |
10253 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10254 | { | |
0b246afa | 10255 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10256 | int ret; |
1eafa6c7 | 10257 | |
0b246afa | 10258 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10259 | return -EROFS; |
10260 | ||
6c255e67 MX |
10261 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10262 | if (ret > 0) | |
10263 | ret = 0; | |
eb73c1b7 MX |
10264 | return ret; |
10265 | } | |
10266 | ||
6c255e67 MX |
10267 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10268 | int nr) | |
eb73c1b7 MX |
10269 | { |
10270 | struct btrfs_root *root; | |
10271 | struct list_head splice; | |
10272 | int ret; | |
10273 | ||
2c21b4d7 | 10274 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10275 | return -EROFS; |
10276 | ||
10277 | INIT_LIST_HEAD(&splice); | |
10278 | ||
573bfb72 | 10279 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10280 | spin_lock(&fs_info->delalloc_root_lock); |
10281 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10282 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10283 | root = list_first_entry(&splice, struct btrfs_root, |
10284 | delalloc_root); | |
10285 | root = btrfs_grab_fs_root(root); | |
10286 | BUG_ON(!root); | |
10287 | list_move_tail(&root->delalloc_root, | |
10288 | &fs_info->delalloc_roots); | |
10289 | spin_unlock(&fs_info->delalloc_root_lock); | |
10290 | ||
6c255e67 | 10291 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10292 | btrfs_put_fs_root(root); |
6c255e67 | 10293 | if (ret < 0) |
eb73c1b7 MX |
10294 | goto out; |
10295 | ||
6c255e67 MX |
10296 | if (nr != -1) { |
10297 | nr -= ret; | |
10298 | WARN_ON(nr < 0); | |
10299 | } | |
eb73c1b7 | 10300 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10301 | } |
eb73c1b7 | 10302 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10303 | |
6c255e67 | 10304 | ret = 0; |
eb73c1b7 | 10305 | out: |
1eafa6c7 | 10306 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10307 | spin_lock(&fs_info->delalloc_root_lock); |
10308 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10309 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10310 | } |
573bfb72 | 10311 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10312 | return ret; |
ea8c2819 CM |
10313 | } |
10314 | ||
39279cc3 CM |
10315 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10316 | const char *symname) | |
10317 | { | |
0b246afa | 10318 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10319 | struct btrfs_trans_handle *trans; |
10320 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10321 | struct btrfs_path *path; | |
10322 | struct btrfs_key key; | |
1832a6d5 | 10323 | struct inode *inode = NULL; |
39279cc3 CM |
10324 | int err; |
10325 | int drop_inode = 0; | |
10326 | u64 objectid; | |
67871254 | 10327 | u64 index = 0; |
39279cc3 CM |
10328 | int name_len; |
10329 | int datasize; | |
5f39d397 | 10330 | unsigned long ptr; |
39279cc3 | 10331 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10332 | struct extent_buffer *leaf; |
39279cc3 | 10333 | |
f06becc4 | 10334 | name_len = strlen(symname); |
0b246afa | 10335 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10336 | return -ENAMETOOLONG; |
1832a6d5 | 10337 | |
9ed74f2d JB |
10338 | /* |
10339 | * 2 items for inode item and ref | |
10340 | * 2 items for dir items | |
9269d12b FM |
10341 | * 1 item for updating parent inode item |
10342 | * 1 item for the inline extent item | |
9ed74f2d JB |
10343 | * 1 item for xattr if selinux is on |
10344 | */ | |
9269d12b | 10345 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10346 | if (IS_ERR(trans)) |
10347 | return PTR_ERR(trans); | |
1832a6d5 | 10348 | |
581bb050 LZ |
10349 | err = btrfs_find_free_ino(root, &objectid); |
10350 | if (err) | |
10351 | goto out_unlock; | |
10352 | ||
aec7477b | 10353 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10354 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10355 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10356 | if (IS_ERR(inode)) { |
10357 | err = PTR_ERR(inode); | |
39279cc3 | 10358 | goto out_unlock; |
7cf96da3 | 10359 | } |
39279cc3 | 10360 | |
ad19db71 CS |
10361 | /* |
10362 | * If the active LSM wants to access the inode during | |
10363 | * d_instantiate it needs these. Smack checks to see | |
10364 | * if the filesystem supports xattrs by looking at the | |
10365 | * ops vector. | |
10366 | */ | |
10367 | inode->i_fop = &btrfs_file_operations; | |
10368 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10369 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10370 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10371 | ||
10372 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10373 | if (err) | |
10374 | goto out_unlock_inode; | |
ad19db71 | 10375 | |
39279cc3 | 10376 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10377 | if (!path) { |
10378 | err = -ENOMEM; | |
b0d5d10f | 10379 | goto out_unlock_inode; |
d8926bb3 | 10380 | } |
4a0cc7ca | 10381 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10382 | key.offset = 0; |
962a298f | 10383 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10384 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10385 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10386 | datasize); | |
54aa1f4d | 10387 | if (err) { |
b0839166 | 10388 | btrfs_free_path(path); |
b0d5d10f | 10389 | goto out_unlock_inode; |
54aa1f4d | 10390 | } |
5f39d397 CM |
10391 | leaf = path->nodes[0]; |
10392 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10393 | struct btrfs_file_extent_item); | |
10394 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10395 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10396 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10397 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10398 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10399 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10400 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10401 | ||
39279cc3 | 10402 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10403 | write_extent_buffer(leaf, symname, ptr, name_len); |
10404 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10405 | btrfs_free_path(path); |
5f39d397 | 10406 | |
39279cc3 | 10407 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10408 | inode_nohighmem(inode); |
39279cc3 | 10409 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10410 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10411 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10412 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10413 | /* |
10414 | * Last step, add directory indexes for our symlink inode. This is the | |
10415 | * last step to avoid extra cleanup of these indexes if an error happens | |
10416 | * elsewhere above. | |
10417 | */ | |
10418 | if (!err) | |
cef415af NB |
10419 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10420 | BTRFS_I(inode), 0, index); | |
b0d5d10f | 10421 | if (err) { |
54aa1f4d | 10422 | drop_inode = 1; |
b0d5d10f CM |
10423 | goto out_unlock_inode; |
10424 | } | |
10425 | ||
10426 | unlock_new_inode(inode); | |
10427 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10428 | |
10429 | out_unlock: | |
3a45bb20 | 10430 | btrfs_end_transaction(trans); |
39279cc3 CM |
10431 | if (drop_inode) { |
10432 | inode_dec_link_count(inode); | |
10433 | iput(inode); | |
10434 | } | |
2ff7e61e | 10435 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 10436 | return err; |
b0d5d10f CM |
10437 | |
10438 | out_unlock_inode: | |
10439 | drop_inode = 1; | |
10440 | unlock_new_inode(inode); | |
10441 | goto out_unlock; | |
39279cc3 | 10442 | } |
16432985 | 10443 | |
0af3d00b JB |
10444 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10445 | u64 start, u64 num_bytes, u64 min_size, | |
10446 | loff_t actual_len, u64 *alloc_hint, | |
10447 | struct btrfs_trans_handle *trans) | |
d899e052 | 10448 | { |
0b246afa | 10449 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10450 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10451 | struct extent_map *em; | |
d899e052 YZ |
10452 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10453 | struct btrfs_key ins; | |
d899e052 | 10454 | u64 cur_offset = start; |
55a61d1d | 10455 | u64 i_size; |
154ea289 | 10456 | u64 cur_bytes; |
0b670dc4 | 10457 | u64 last_alloc = (u64)-1; |
d899e052 | 10458 | int ret = 0; |
0af3d00b | 10459 | bool own_trans = true; |
18513091 | 10460 | u64 end = start + num_bytes - 1; |
d899e052 | 10461 | |
0af3d00b JB |
10462 | if (trans) |
10463 | own_trans = false; | |
d899e052 | 10464 | while (num_bytes > 0) { |
0af3d00b JB |
10465 | if (own_trans) { |
10466 | trans = btrfs_start_transaction(root, 3); | |
10467 | if (IS_ERR(trans)) { | |
10468 | ret = PTR_ERR(trans); | |
10469 | break; | |
10470 | } | |
5a303d5d YZ |
10471 | } |
10472 | ||
ee22184b | 10473 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10474 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10475 | /* |
10476 | * If we are severely fragmented we could end up with really | |
10477 | * small allocations, so if the allocator is returning small | |
10478 | * chunks lets make its job easier by only searching for those | |
10479 | * sized chunks. | |
10480 | */ | |
10481 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10482 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10483 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10484 | if (ret) { |
0af3d00b | 10485 | if (own_trans) |
3a45bb20 | 10486 | btrfs_end_transaction(trans); |
a22285a6 | 10487 | break; |
d899e052 | 10488 | } |
0b246afa | 10489 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10490 | |
0b670dc4 | 10491 | last_alloc = ins.offset; |
d899e052 YZ |
10492 | ret = insert_reserved_file_extent(trans, inode, |
10493 | cur_offset, ins.objectid, | |
10494 | ins.offset, ins.offset, | |
920bbbfb | 10495 | ins.offset, 0, 0, 0, |
d899e052 | 10496 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10497 | if (ret) { |
2ff7e61e | 10498 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10499 | ins.offset, 0); |
66642832 | 10500 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10501 | if (own_trans) |
3a45bb20 | 10502 | btrfs_end_transaction(trans); |
79787eaa JM |
10503 | break; |
10504 | } | |
31193213 | 10505 | |
dcdbc059 | 10506 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10507 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10508 | |
5dc562c5 JB |
10509 | em = alloc_extent_map(); |
10510 | if (!em) { | |
10511 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10512 | &BTRFS_I(inode)->runtime_flags); | |
10513 | goto next; | |
10514 | } | |
10515 | ||
10516 | em->start = cur_offset; | |
10517 | em->orig_start = cur_offset; | |
10518 | em->len = ins.offset; | |
10519 | em->block_start = ins.objectid; | |
10520 | em->block_len = ins.offset; | |
b4939680 | 10521 | em->orig_block_len = ins.offset; |
cc95bef6 | 10522 | em->ram_bytes = ins.offset; |
0b246afa | 10523 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10524 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10525 | em->generation = trans->transid; | |
10526 | ||
10527 | while (1) { | |
10528 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10529 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10530 | write_unlock(&em_tree->lock); |
10531 | if (ret != -EEXIST) | |
10532 | break; | |
dcdbc059 | 10533 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10534 | cur_offset + ins.offset - 1, |
10535 | 0); | |
10536 | } | |
10537 | free_extent_map(em); | |
10538 | next: | |
d899e052 YZ |
10539 | num_bytes -= ins.offset; |
10540 | cur_offset += ins.offset; | |
efa56464 | 10541 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10542 | |
0c4d2d95 | 10543 | inode_inc_iversion(inode); |
c2050a45 | 10544 | inode->i_ctime = current_time(inode); |
6cbff00f | 10545 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10546 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10547 | (actual_len > inode->i_size) && |
10548 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10549 | if (cur_offset > actual_len) |
55a61d1d | 10550 | i_size = actual_len; |
d1ea6a61 | 10551 | else |
55a61d1d JB |
10552 | i_size = cur_offset; |
10553 | i_size_write(inode, i_size); | |
10554 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10555 | } |
10556 | ||
d899e052 | 10557 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10558 | |
10559 | if (ret) { | |
66642832 | 10560 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10561 | if (own_trans) |
3a45bb20 | 10562 | btrfs_end_transaction(trans); |
79787eaa JM |
10563 | break; |
10564 | } | |
d899e052 | 10565 | |
0af3d00b | 10566 | if (own_trans) |
3a45bb20 | 10567 | btrfs_end_transaction(trans); |
5a303d5d | 10568 | } |
18513091 | 10569 | if (cur_offset < end) |
bc42bda2 | 10570 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
18513091 | 10571 | end - cur_offset + 1); |
d899e052 YZ |
10572 | return ret; |
10573 | } | |
10574 | ||
0af3d00b JB |
10575 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10576 | u64 start, u64 num_bytes, u64 min_size, | |
10577 | loff_t actual_len, u64 *alloc_hint) | |
10578 | { | |
10579 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10580 | min_size, actual_len, alloc_hint, | |
10581 | NULL); | |
10582 | } | |
10583 | ||
10584 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10585 | struct btrfs_trans_handle *trans, int mode, | |
10586 | u64 start, u64 num_bytes, u64 min_size, | |
10587 | loff_t actual_len, u64 *alloc_hint) | |
10588 | { | |
10589 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10590 | min_size, actual_len, alloc_hint, trans); | |
10591 | } | |
10592 | ||
e6dcd2dc CM |
10593 | static int btrfs_set_page_dirty(struct page *page) |
10594 | { | |
e6dcd2dc CM |
10595 | return __set_page_dirty_nobuffers(page); |
10596 | } | |
10597 | ||
10556cb2 | 10598 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10599 | { |
b83cc969 | 10600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10601 | umode_t mode = inode->i_mode; |
b83cc969 | 10602 | |
cb6db4e5 JM |
10603 | if (mask & MAY_WRITE && |
10604 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10605 | if (btrfs_root_readonly(root)) | |
10606 | return -EROFS; | |
10607 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10608 | return -EACCES; | |
10609 | } | |
2830ba7f | 10610 | return generic_permission(inode, mask); |
fdebe2bd | 10611 | } |
39279cc3 | 10612 | |
ef3b9af5 FM |
10613 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10614 | { | |
2ff7e61e | 10615 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10616 | struct btrfs_trans_handle *trans; |
10617 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10618 | struct inode *inode = NULL; | |
10619 | u64 objectid; | |
10620 | u64 index; | |
10621 | int ret = 0; | |
10622 | ||
10623 | /* | |
10624 | * 5 units required for adding orphan entry | |
10625 | */ | |
10626 | trans = btrfs_start_transaction(root, 5); | |
10627 | if (IS_ERR(trans)) | |
10628 | return PTR_ERR(trans); | |
10629 | ||
10630 | ret = btrfs_find_free_ino(root, &objectid); | |
10631 | if (ret) | |
10632 | goto out; | |
10633 | ||
10634 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10635 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10636 | if (IS_ERR(inode)) { |
10637 | ret = PTR_ERR(inode); | |
10638 | inode = NULL; | |
10639 | goto out; | |
10640 | } | |
10641 | ||
ef3b9af5 FM |
10642 | inode->i_fop = &btrfs_file_operations; |
10643 | inode->i_op = &btrfs_file_inode_operations; | |
10644 | ||
10645 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10646 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10647 | ||
b0d5d10f CM |
10648 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10649 | if (ret) | |
10650 | goto out_inode; | |
10651 | ||
10652 | ret = btrfs_update_inode(trans, root, inode); | |
10653 | if (ret) | |
10654 | goto out_inode; | |
73f2e545 | 10655 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10656 | if (ret) |
b0d5d10f | 10657 | goto out_inode; |
ef3b9af5 | 10658 | |
5762b5c9 FM |
10659 | /* |
10660 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10661 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10662 | * through: | |
10663 | * | |
10664 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10665 | */ | |
10666 | set_nlink(inode, 1); | |
b0d5d10f | 10667 | unlock_new_inode(inode); |
ef3b9af5 FM |
10668 | d_tmpfile(dentry, inode); |
10669 | mark_inode_dirty(inode); | |
10670 | ||
10671 | out: | |
3a45bb20 | 10672 | btrfs_end_transaction(trans); |
ef3b9af5 FM |
10673 | if (ret) |
10674 | iput(inode); | |
2ff7e61e JM |
10675 | btrfs_balance_delayed_items(fs_info); |
10676 | btrfs_btree_balance_dirty(fs_info); | |
ef3b9af5 | 10677 | return ret; |
b0d5d10f CM |
10678 | |
10679 | out_inode: | |
10680 | unlock_new_inode(inode); | |
10681 | goto out; | |
10682 | ||
ef3b9af5 FM |
10683 | } |
10684 | ||
20a7db8a | 10685 | __attribute__((const)) |
9d0d1c8b | 10686 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) |
20a7db8a | 10687 | { |
9d0d1c8b | 10688 | return -EAGAIN; |
20a7db8a DS |
10689 | } |
10690 | ||
c6100a4b JB |
10691 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
10692 | { | |
10693 | struct inode *inode = private_data; | |
10694 | return btrfs_sb(inode->i_sb); | |
10695 | } | |
10696 | ||
10697 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, | |
10698 | u64 start, u64 end) | |
10699 | { | |
10700 | struct inode *inode = private_data; | |
10701 | u64 isize; | |
10702 | ||
10703 | isize = i_size_read(inode); | |
10704 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
10705 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, | |
10706 | "%s: ino %llu isize %llu odd range [%llu,%llu]", | |
10707 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); | |
10708 | } | |
10709 | } | |
10710 | ||
10711 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) | |
10712 | { | |
10713 | struct inode *inode = private_data; | |
10714 | unsigned long index = start >> PAGE_SHIFT; | |
10715 | unsigned long end_index = end >> PAGE_SHIFT; | |
10716 | struct page *page; | |
10717 | ||
10718 | while (index <= end_index) { | |
10719 | page = find_get_page(inode->i_mapping, index); | |
10720 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10721 | set_page_writeback(page); | |
10722 | put_page(page); | |
10723 | index++; | |
10724 | } | |
10725 | } | |
10726 | ||
6e1d5dcc | 10727 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10728 | .getattr = btrfs_getattr, |
39279cc3 CM |
10729 | .lookup = btrfs_lookup, |
10730 | .create = btrfs_create, | |
10731 | .unlink = btrfs_unlink, | |
10732 | .link = btrfs_link, | |
10733 | .mkdir = btrfs_mkdir, | |
10734 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10735 | .rename = btrfs_rename2, |
39279cc3 CM |
10736 | .symlink = btrfs_symlink, |
10737 | .setattr = btrfs_setattr, | |
618e21d5 | 10738 | .mknod = btrfs_mknod, |
5103e947 | 10739 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10740 | .permission = btrfs_permission, |
4e34e719 | 10741 | .get_acl = btrfs_get_acl, |
996a710d | 10742 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10743 | .update_time = btrfs_update_time, |
ef3b9af5 | 10744 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10745 | }; |
6e1d5dcc | 10746 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10747 | .lookup = btrfs_lookup, |
fdebe2bd | 10748 | .permission = btrfs_permission, |
93fd63c2 | 10749 | .update_time = btrfs_update_time, |
39279cc3 | 10750 | }; |
76dda93c | 10751 | |
828c0950 | 10752 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10753 | .llseek = generic_file_llseek, |
10754 | .read = generic_read_dir, | |
02dbfc99 | 10755 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10756 | .open = btrfs_opendir, |
34287aa3 | 10757 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10758 | #ifdef CONFIG_COMPAT |
4c63c245 | 10759 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10760 | #endif |
6bf13c0c | 10761 | .release = btrfs_release_file, |
e02119d5 | 10762 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10763 | }; |
10764 | ||
20e5506b | 10765 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10766 | /* mandatory callbacks */ |
065631f6 | 10767 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac | 10768 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
4d53dddb | 10769 | .merge_bio_hook = btrfs_merge_bio_hook, |
9d0d1c8b | 10770 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
c6100a4b JB |
10771 | .tree_fs_info = iotree_fs_info, |
10772 | .set_range_writeback = btrfs_set_range_writeback, | |
4d53dddb DS |
10773 | |
10774 | /* optional callbacks */ | |
10775 | .fill_delalloc = run_delalloc_range, | |
e6dcd2dc | 10776 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10777 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10778 | .set_bit_hook = btrfs_set_bit_hook, |
10779 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10780 | .merge_extent_hook = btrfs_merge_extent_hook, |
10781 | .split_extent_hook = btrfs_split_extent_hook, | |
c6100a4b | 10782 | .check_extent_io_range = btrfs_check_extent_io_range, |
07157aac CM |
10783 | }; |
10784 | ||
35054394 CM |
10785 | /* |
10786 | * btrfs doesn't support the bmap operation because swapfiles | |
10787 | * use bmap to make a mapping of extents in the file. They assume | |
10788 | * these extents won't change over the life of the file and they | |
10789 | * use the bmap result to do IO directly to the drive. | |
10790 | * | |
10791 | * the btrfs bmap call would return logical addresses that aren't | |
10792 | * suitable for IO and they also will change frequently as COW | |
10793 | * operations happen. So, swapfile + btrfs == corruption. | |
10794 | * | |
10795 | * For now we're avoiding this by dropping bmap. | |
10796 | */ | |
7f09410b | 10797 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10798 | .readpage = btrfs_readpage, |
10799 | .writepage = btrfs_writepage, | |
b293f02e | 10800 | .writepages = btrfs_writepages, |
3ab2fb5a | 10801 | .readpages = btrfs_readpages, |
16432985 | 10802 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10803 | .invalidatepage = btrfs_invalidatepage, |
10804 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10805 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10806 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10807 | }; |
10808 | ||
7f09410b | 10809 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10810 | .readpage = btrfs_readpage, |
10811 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10812 | .invalidatepage = btrfs_invalidatepage, |
10813 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10814 | }; |
10815 | ||
6e1d5dcc | 10816 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10817 | .getattr = btrfs_getattr, |
10818 | .setattr = btrfs_setattr, | |
5103e947 | 10819 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10820 | .permission = btrfs_permission, |
1506fcc8 | 10821 | .fiemap = btrfs_fiemap, |
4e34e719 | 10822 | .get_acl = btrfs_get_acl, |
996a710d | 10823 | .set_acl = btrfs_set_acl, |
e41f941a | 10824 | .update_time = btrfs_update_time, |
39279cc3 | 10825 | }; |
6e1d5dcc | 10826 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10827 | .getattr = btrfs_getattr, |
10828 | .setattr = btrfs_setattr, | |
fdebe2bd | 10829 | .permission = btrfs_permission, |
33268eaf | 10830 | .listxattr = btrfs_listxattr, |
4e34e719 | 10831 | .get_acl = btrfs_get_acl, |
996a710d | 10832 | .set_acl = btrfs_set_acl, |
e41f941a | 10833 | .update_time = btrfs_update_time, |
618e21d5 | 10834 | }; |
6e1d5dcc | 10835 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10836 | .get_link = page_get_link, |
f209561a | 10837 | .getattr = btrfs_getattr, |
22c44fe6 | 10838 | .setattr = btrfs_setattr, |
fdebe2bd | 10839 | .permission = btrfs_permission, |
0279b4cd | 10840 | .listxattr = btrfs_listxattr, |
e41f941a | 10841 | .update_time = btrfs_update_time, |
39279cc3 | 10842 | }; |
76dda93c | 10843 | |
82d339d9 | 10844 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10845 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10846 | .d_release = btrfs_dentry_release, |
76dda93c | 10847 | }; |