]>
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> |
39279cc3 CM |
45 | #include "ctree.h" |
46 | #include "disk-io.h" | |
47 | #include "transaction.h" | |
48 | #include "btrfs_inode.h" | |
39279cc3 | 49 | #include "print-tree.h" |
e6dcd2dc | 50 | #include "ordered-data.h" |
95819c05 | 51 | #include "xattr.h" |
e02119d5 | 52 | #include "tree-log.h" |
4a54c8c1 | 53 | #include "volumes.h" |
c8b97818 | 54 | #include "compression.h" |
b4ce94de | 55 | #include "locking.h" |
dc89e982 | 56 | #include "free-space-cache.h" |
581bb050 | 57 | #include "inode-map.h" |
38c227d8 | 58 | #include "backref.h" |
f23b5a59 | 59 | #include "hash.h" |
63541927 | 60 | #include "props.h" |
31193213 | 61 | #include "qgroup.h" |
dda3245e | 62 | #include "dedupe.h" |
39279cc3 CM |
63 | |
64 | struct btrfs_iget_args { | |
90d3e592 | 65 | struct btrfs_key *location; |
39279cc3 CM |
66 | struct btrfs_root *root; |
67 | }; | |
68 | ||
f28a4928 FM |
69 | struct btrfs_dio_data { |
70 | u64 outstanding_extents; | |
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 | { | |
138 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, | |
139 | bytes - PAGE_SIZE, false); | |
140 | } | |
141 | ||
48a3b636 | 142 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 143 | |
6a3891c5 JB |
144 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
145 | void btrfs_test_inode_set_ops(struct inode *inode) | |
146 | { | |
147 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
148 | } | |
149 | #endif | |
150 | ||
f34f57a3 | 151 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
152 | struct inode *inode, struct inode *dir, |
153 | const struct qstr *qstr) | |
0279b4cd JO |
154 | { |
155 | int err; | |
156 | ||
f34f57a3 | 157 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 158 | if (!err) |
2a7dba39 | 159 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
160 | return err; |
161 | } | |
162 | ||
c8b97818 CM |
163 | /* |
164 | * this does all the hard work for inserting an inline extent into | |
165 | * the btree. The caller should have done a btrfs_drop_extents so that | |
166 | * no overlapping inline items exist in the btree | |
167 | */ | |
40f76580 | 168 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 169 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
170 | struct btrfs_root *root, struct inode *inode, |
171 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 172 | int compress_type, |
c8b97818 CM |
173 | struct page **compressed_pages) |
174 | { | |
c8b97818 CM |
175 | struct extent_buffer *leaf; |
176 | struct page *page = NULL; | |
177 | char *kaddr; | |
178 | unsigned long ptr; | |
179 | struct btrfs_file_extent_item *ei; | |
c8b97818 CM |
180 | int ret; |
181 | size_t cur_size = size; | |
c8b97818 | 182 | unsigned long offset; |
c8b97818 | 183 | |
fe3f566c | 184 | if (compressed_size && compressed_pages) |
c8b97818 | 185 | cur_size = compressed_size; |
c8b97818 | 186 | |
1acae57b | 187 | inode_add_bytes(inode, size); |
c8b97818 | 188 | |
1acae57b FDBM |
189 | if (!extent_inserted) { |
190 | struct btrfs_key key; | |
191 | size_t datasize; | |
c8b97818 | 192 | |
4a0cc7ca | 193 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 194 | key.offset = start; |
962a298f | 195 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 196 | |
1acae57b FDBM |
197 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
198 | path->leave_spinning = 1; | |
199 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
200 | datasize); | |
79b4f4c6 | 201 | if (ret) |
1acae57b | 202 | goto fail; |
c8b97818 CM |
203 | } |
204 | leaf = path->nodes[0]; | |
205 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
206 | struct btrfs_file_extent_item); | |
207 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
208 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
209 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
210 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
211 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
212 | ptr = btrfs_file_extent_inline_start(ei); | |
213 | ||
261507a0 | 214 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
215 | struct page *cpage; |
216 | int i = 0; | |
d397712b | 217 | while (compressed_size > 0) { |
c8b97818 | 218 | cpage = compressed_pages[i]; |
5b050f04 | 219 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 220 | PAGE_SIZE); |
c8b97818 | 221 | |
7ac687d9 | 222 | kaddr = kmap_atomic(cpage); |
c8b97818 | 223 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 224 | kunmap_atomic(kaddr); |
c8b97818 CM |
225 | |
226 | i++; | |
227 | ptr += cur_size; | |
228 | compressed_size -= cur_size; | |
229 | } | |
230 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 231 | compress_type); |
c8b97818 CM |
232 | } else { |
233 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 234 | start >> PAGE_SHIFT); |
c8b97818 | 235 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 236 | kaddr = kmap_atomic(page); |
09cbfeaf | 237 | offset = start & (PAGE_SIZE - 1); |
c8b97818 | 238 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 239 | kunmap_atomic(kaddr); |
09cbfeaf | 240 | put_page(page); |
c8b97818 CM |
241 | } |
242 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 243 | btrfs_release_path(path); |
c8b97818 | 244 | |
c2167754 YZ |
245 | /* |
246 | * we're an inline extent, so nobody can | |
247 | * extend the file past i_size without locking | |
248 | * a page we already have locked. | |
249 | * | |
250 | * We must do any isize and inode updates | |
251 | * before we unlock the pages. Otherwise we | |
252 | * could end up racing with unlink. | |
253 | */ | |
c8b97818 | 254 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 255 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 256 | |
c8b97818 | 257 | fail: |
79b4f4c6 | 258 | return ret; |
c8b97818 CM |
259 | } |
260 | ||
261 | ||
262 | /* | |
263 | * conditionally insert an inline extent into the file. This | |
264 | * does the checks required to make sure the data is small enough | |
265 | * to fit as an inline extent. | |
266 | */ | |
00361589 JB |
267 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
268 | struct inode *inode, u64 start, | |
269 | u64 end, size_t compressed_size, | |
270 | int compress_type, | |
271 | struct page **compressed_pages) | |
c8b97818 | 272 | { |
0b246afa | 273 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 274 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
275 | u64 isize = i_size_read(inode); |
276 | u64 actual_end = min(end + 1, isize); | |
277 | u64 inline_len = actual_end - start; | |
0b246afa | 278 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
279 | u64 data_len = inline_len; |
280 | int ret; | |
1acae57b FDBM |
281 | struct btrfs_path *path; |
282 | int extent_inserted = 0; | |
283 | u32 extent_item_size; | |
c8b97818 CM |
284 | |
285 | if (compressed_size) | |
286 | data_len = compressed_size; | |
287 | ||
288 | if (start > 0 || | |
0b246afa JM |
289 | actual_end > fs_info->sectorsize || |
290 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 291 | (!compressed_size && |
0b246afa | 292 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 293 | end + 1 < isize || |
0b246afa | 294 | data_len > fs_info->max_inline) { |
c8b97818 CM |
295 | return 1; |
296 | } | |
297 | ||
1acae57b FDBM |
298 | path = btrfs_alloc_path(); |
299 | if (!path) | |
300 | return -ENOMEM; | |
301 | ||
00361589 | 302 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
303 | if (IS_ERR(trans)) { |
304 | btrfs_free_path(path); | |
00361589 | 305 | return PTR_ERR(trans); |
1acae57b | 306 | } |
0b246afa | 307 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
00361589 | 308 | |
1acae57b FDBM |
309 | if (compressed_size && compressed_pages) |
310 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
311 | compressed_size); | |
312 | else | |
313 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
314 | inline_len); | |
315 | ||
316 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
317 | start, aligned_end, NULL, | |
318 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 | 319 | if (ret) { |
66642832 | 320 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
321 | goto out; |
322 | } | |
c8b97818 CM |
323 | |
324 | if (isize > actual_end) | |
325 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
326 | ret = insert_inline_extent(trans, path, extent_inserted, |
327 | root, inode, start, | |
c8b97818 | 328 | inline_len, compressed_size, |
fe3f566c | 329 | compress_type, compressed_pages); |
2adcac1a | 330 | if (ret && ret != -ENOSPC) { |
66642832 | 331 | btrfs_abort_transaction(trans, ret); |
00361589 | 332 | goto out; |
2adcac1a | 333 | } else if (ret == -ENOSPC) { |
00361589 JB |
334 | ret = 1; |
335 | goto out; | |
79787eaa | 336 | } |
2adcac1a | 337 | |
bdc20e67 | 338 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
691fa059 | 339 | btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start); |
dcdbc059 | 340 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
00361589 | 341 | out: |
94ed938a QW |
342 | /* |
343 | * Don't forget to free the reserved space, as for inlined extent | |
344 | * it won't count as data extent, free them directly here. | |
345 | * And at reserve time, it's always aligned to page size, so | |
346 | * just free one page here. | |
347 | */ | |
09cbfeaf | 348 | btrfs_qgroup_free_data(inode, 0, PAGE_SIZE); |
1acae57b | 349 | btrfs_free_path(path); |
3a45bb20 | 350 | btrfs_end_transaction(trans); |
00361589 | 351 | return ret; |
c8b97818 CM |
352 | } |
353 | ||
771ed689 CM |
354 | struct async_extent { |
355 | u64 start; | |
356 | u64 ram_size; | |
357 | u64 compressed_size; | |
358 | struct page **pages; | |
359 | unsigned long nr_pages; | |
261507a0 | 360 | int compress_type; |
771ed689 CM |
361 | struct list_head list; |
362 | }; | |
363 | ||
364 | struct async_cow { | |
365 | struct inode *inode; | |
366 | struct btrfs_root *root; | |
367 | struct page *locked_page; | |
368 | u64 start; | |
369 | u64 end; | |
370 | struct list_head extents; | |
371 | struct btrfs_work work; | |
372 | }; | |
373 | ||
374 | static noinline int add_async_extent(struct async_cow *cow, | |
375 | u64 start, u64 ram_size, | |
376 | u64 compressed_size, | |
377 | struct page **pages, | |
261507a0 LZ |
378 | unsigned long nr_pages, |
379 | int compress_type) | |
771ed689 CM |
380 | { |
381 | struct async_extent *async_extent; | |
382 | ||
383 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 384 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
385 | async_extent->start = start; |
386 | async_extent->ram_size = ram_size; | |
387 | async_extent->compressed_size = compressed_size; | |
388 | async_extent->pages = pages; | |
389 | async_extent->nr_pages = nr_pages; | |
261507a0 | 390 | async_extent->compress_type = compress_type; |
771ed689 CM |
391 | list_add_tail(&async_extent->list, &cow->extents); |
392 | return 0; | |
393 | } | |
394 | ||
f79707b0 WS |
395 | static inline int inode_need_compress(struct inode *inode) |
396 | { | |
0b246afa | 397 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 WS |
398 | |
399 | /* force compress */ | |
0b246afa | 400 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 WS |
401 | return 1; |
402 | /* bad compression ratios */ | |
403 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
404 | return 0; | |
0b246afa | 405 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 WS |
406 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
407 | BTRFS_I(inode)->force_compress) | |
408 | return 1; | |
409 | return 0; | |
410 | } | |
411 | ||
6158e1ce | 412 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
413 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
414 | { | |
415 | /* If this is a small write inside eof, kick off a defrag */ | |
416 | if (num_bytes < small_write && | |
6158e1ce | 417 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
418 | btrfs_add_inode_defrag(NULL, inode); |
419 | } | |
420 | ||
d352ac68 | 421 | /* |
771ed689 CM |
422 | * we create compressed extents in two phases. The first |
423 | * phase compresses a range of pages that have already been | |
424 | * locked (both pages and state bits are locked). | |
c8b97818 | 425 | * |
771ed689 CM |
426 | * This is done inside an ordered work queue, and the compression |
427 | * is spread across many cpus. The actual IO submission is step | |
428 | * two, and the ordered work queue takes care of making sure that | |
429 | * happens in the same order things were put onto the queue by | |
430 | * writepages and friends. | |
c8b97818 | 431 | * |
771ed689 CM |
432 | * If this code finds it can't get good compression, it puts an |
433 | * entry onto the work queue to write the uncompressed bytes. This | |
434 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
435 | * are written in the same order that the flusher thread sent them |
436 | * down. | |
d352ac68 | 437 | */ |
c44f649e | 438 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
439 | struct page *locked_page, |
440 | u64 start, u64 end, | |
441 | struct async_cow *async_cow, | |
442 | int *num_added) | |
b888db2b | 443 | { |
0b246afa | 444 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
b888db2b | 445 | struct btrfs_root *root = BTRFS_I(inode)->root; |
db94535d | 446 | u64 num_bytes; |
0b246afa | 447 | u64 blocksize = fs_info->sectorsize; |
c8b97818 | 448 | u64 actual_end; |
42dc7bab | 449 | u64 isize = i_size_read(inode); |
e6dcd2dc | 450 | int ret = 0; |
c8b97818 CM |
451 | struct page **pages = NULL; |
452 | unsigned long nr_pages; | |
c8b97818 CM |
453 | unsigned long total_compressed = 0; |
454 | unsigned long total_in = 0; | |
c8b97818 CM |
455 | int i; |
456 | int will_compress; | |
0b246afa | 457 | int compress_type = fs_info->compress_type; |
4adaa611 | 458 | int redirty = 0; |
b888db2b | 459 | |
6158e1ce NB |
460 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
461 | SZ_16K); | |
4cb5300b | 462 | |
42dc7bab | 463 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
464 | again: |
465 | will_compress = 0; | |
09cbfeaf | 466 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
467 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
468 | nr_pages = min_t(unsigned long, nr_pages, | |
469 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 470 | |
f03d9301 CM |
471 | /* |
472 | * we don't want to send crud past the end of i_size through | |
473 | * compression, that's just a waste of CPU time. So, if the | |
474 | * end of the file is before the start of our current | |
475 | * requested range of bytes, we bail out to the uncompressed | |
476 | * cleanup code that can deal with all of this. | |
477 | * | |
478 | * It isn't really the fastest way to fix things, but this is a | |
479 | * very uncommon corner. | |
480 | */ | |
481 | if (actual_end <= start) | |
482 | goto cleanup_and_bail_uncompressed; | |
483 | ||
c8b97818 CM |
484 | total_compressed = actual_end - start; |
485 | ||
4bcbb332 SW |
486 | /* |
487 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 488 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
489 | */ |
490 | if (total_compressed <= blocksize && | |
491 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
492 | goto cleanup_and_bail_uncompressed; | |
493 | ||
069eac78 DS |
494 | total_compressed = min_t(unsigned long, total_compressed, |
495 | BTRFS_MAX_UNCOMPRESSED); | |
fda2832f | 496 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 497 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
498 | total_in = 0; |
499 | ret = 0; | |
db94535d | 500 | |
771ed689 CM |
501 | /* |
502 | * we do compression for mount -o compress and when the | |
503 | * inode has not been flagged as nocompress. This flag can | |
504 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 505 | */ |
f79707b0 | 506 | if (inode_need_compress(inode)) { |
c8b97818 | 507 | WARN_ON(pages); |
31e818fe | 508 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
509 | if (!pages) { |
510 | /* just bail out to the uncompressed code */ | |
511 | goto cont; | |
512 | } | |
c8b97818 | 513 | |
261507a0 LZ |
514 | if (BTRFS_I(inode)->force_compress) |
515 | compress_type = BTRFS_I(inode)->force_compress; | |
516 | ||
4adaa611 CM |
517 | /* |
518 | * we need to call clear_page_dirty_for_io on each | |
519 | * page in the range. Otherwise applications with the file | |
520 | * mmap'd can wander in and change the page contents while | |
521 | * we are compressing them. | |
522 | * | |
523 | * If the compression fails for any reason, we set the pages | |
524 | * dirty again later on. | |
525 | */ | |
526 | extent_range_clear_dirty_for_io(inode, start, end); | |
527 | redirty = 1; | |
261507a0 LZ |
528 | ret = btrfs_compress_pages(compress_type, |
529 | inode->i_mapping, start, | |
38c31464 | 530 | pages, |
4d3a800e | 531 | &nr_pages, |
261507a0 | 532 | &total_in, |
e5d74902 | 533 | &total_compressed); |
c8b97818 CM |
534 | |
535 | if (!ret) { | |
536 | unsigned long offset = total_compressed & | |
09cbfeaf | 537 | (PAGE_SIZE - 1); |
4d3a800e | 538 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
539 | char *kaddr; |
540 | ||
541 | /* zero the tail end of the last page, we might be | |
542 | * sending it down to disk | |
543 | */ | |
544 | if (offset) { | |
7ac687d9 | 545 | kaddr = kmap_atomic(page); |
c8b97818 | 546 | memset(kaddr + offset, 0, |
09cbfeaf | 547 | PAGE_SIZE - offset); |
7ac687d9 | 548 | kunmap_atomic(kaddr); |
c8b97818 CM |
549 | } |
550 | will_compress = 1; | |
551 | } | |
552 | } | |
560f7d75 | 553 | cont: |
c8b97818 CM |
554 | if (start == 0) { |
555 | /* lets try to make an inline extent */ | |
771ed689 | 556 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 557 | /* we didn't compress the entire range, try |
771ed689 | 558 | * to make an uncompressed inline extent. |
c8b97818 | 559 | */ |
00361589 | 560 | ret = cow_file_range_inline(root, inode, start, end, |
f74670f7 | 561 | 0, BTRFS_COMPRESS_NONE, NULL); |
c8b97818 | 562 | } else { |
771ed689 | 563 | /* try making a compressed inline extent */ |
00361589 | 564 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
565 | total_compressed, |
566 | compress_type, pages); | |
c8b97818 | 567 | } |
79787eaa | 568 | if (ret <= 0) { |
151a41bc | 569 | unsigned long clear_flags = EXTENT_DELALLOC | |
a7e3b975 | 570 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG; |
e6eb4314 FM |
571 | unsigned long page_error_op; |
572 | ||
151a41bc | 573 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 574 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 575 | |
771ed689 | 576 | /* |
79787eaa JM |
577 | * inline extent creation worked or returned error, |
578 | * we don't need to create any more async work items. | |
579 | * Unlock and free up our temp pages. | |
771ed689 | 580 | */ |
ba8b04c1 QW |
581 | extent_clear_unlock_delalloc(inode, start, end, end, |
582 | NULL, clear_flags, | |
583 | PAGE_UNLOCK | | |
c2790a2e JB |
584 | PAGE_CLEAR_DIRTY | |
585 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 586 | page_error_op | |
c2790a2e | 587 | PAGE_END_WRITEBACK); |
1c81ba23 FM |
588 | if (ret == 0) |
589 | btrfs_free_reserved_data_space_noquota(inode, | |
590 | start, | |
591 | end - start + 1); | |
c8b97818 CM |
592 | goto free_pages_out; |
593 | } | |
594 | } | |
595 | ||
596 | if (will_compress) { | |
597 | /* | |
598 | * we aren't doing an inline extent round the compressed size | |
599 | * up to a block size boundary so the allocator does sane | |
600 | * things | |
601 | */ | |
fda2832f | 602 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
603 | |
604 | /* | |
605 | * one last check to make sure the compression is really a | |
170607eb TT |
606 | * win, compare the page count read with the blocks on disk, |
607 | * compression must free at least one sector size | |
c8b97818 | 608 | */ |
09cbfeaf | 609 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 610 | if (total_compressed + blocksize <= total_in) { |
c8b97818 | 611 | num_bytes = total_in; |
c8bb0c8b AS |
612 | *num_added += 1; |
613 | ||
614 | /* | |
615 | * The async work queues will take care of doing actual | |
616 | * allocation on disk for these compressed pages, and | |
617 | * will submit them to the elevator. | |
618 | */ | |
619 | add_async_extent(async_cow, start, num_bytes, | |
4d3a800e | 620 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
621 | compress_type); |
622 | ||
623 | if (start + num_bytes < end) { | |
624 | start += num_bytes; | |
625 | pages = NULL; | |
626 | cond_resched(); | |
627 | goto again; | |
628 | } | |
629 | return; | |
c8b97818 CM |
630 | } |
631 | } | |
c8bb0c8b | 632 | if (pages) { |
c8b97818 CM |
633 | /* |
634 | * the compression code ran but failed to make things smaller, | |
635 | * free any pages it allocated and our page pointer array | |
636 | */ | |
4d3a800e | 637 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 638 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 639 | put_page(pages[i]); |
c8b97818 CM |
640 | } |
641 | kfree(pages); | |
642 | pages = NULL; | |
643 | total_compressed = 0; | |
4d3a800e | 644 | nr_pages = 0; |
c8b97818 CM |
645 | |
646 | /* flag the file so we don't compress in the future */ | |
0b246afa | 647 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
1e701a32 | 648 | !(BTRFS_I(inode)->force_compress)) { |
a555f810 | 649 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 650 | } |
c8b97818 | 651 | } |
f03d9301 | 652 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
653 | /* |
654 | * No compression, but we still need to write the pages in the file | |
655 | * we've been given so far. redirty the locked page if it corresponds | |
656 | * to our extent and set things up for the async work queue to run | |
657 | * cow_file_range to do the normal delalloc dance. | |
658 | */ | |
659 | if (page_offset(locked_page) >= start && | |
660 | page_offset(locked_page) <= end) | |
661 | __set_page_dirty_nobuffers(locked_page); | |
662 | /* unlocked later on in the async handlers */ | |
663 | ||
664 | if (redirty) | |
665 | extent_range_redirty_for_io(inode, start, end); | |
666 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
667 | BTRFS_COMPRESS_NONE); | |
668 | *num_added += 1; | |
3b951516 | 669 | |
c44f649e | 670 | return; |
771ed689 CM |
671 | |
672 | free_pages_out: | |
4d3a800e | 673 | for (i = 0; i < nr_pages; i++) { |
771ed689 | 674 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 675 | put_page(pages[i]); |
771ed689 | 676 | } |
d397712b | 677 | kfree(pages); |
771ed689 | 678 | } |
771ed689 | 679 | |
40ae837b FM |
680 | static void free_async_extent_pages(struct async_extent *async_extent) |
681 | { | |
682 | int i; | |
683 | ||
684 | if (!async_extent->pages) | |
685 | return; | |
686 | ||
687 | for (i = 0; i < async_extent->nr_pages; i++) { | |
688 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 689 | put_page(async_extent->pages[i]); |
40ae837b FM |
690 | } |
691 | kfree(async_extent->pages); | |
692 | async_extent->nr_pages = 0; | |
693 | async_extent->pages = NULL; | |
771ed689 CM |
694 | } |
695 | ||
696 | /* | |
697 | * phase two of compressed writeback. This is the ordered portion | |
698 | * of the code, which only gets called in the order the work was | |
699 | * queued. We walk all the async extents created by compress_file_range | |
700 | * and send them down to the disk. | |
701 | */ | |
dec8f175 | 702 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
703 | struct async_cow *async_cow) |
704 | { | |
0b246afa | 705 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
706 | struct async_extent *async_extent; |
707 | u64 alloc_hint = 0; | |
771ed689 CM |
708 | struct btrfs_key ins; |
709 | struct extent_map *em; | |
710 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
771ed689 | 711 | struct extent_io_tree *io_tree; |
f5a84ee3 | 712 | int ret = 0; |
771ed689 | 713 | |
3e04e7f1 | 714 | again: |
d397712b | 715 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
716 | async_extent = list_entry(async_cow->extents.next, |
717 | struct async_extent, list); | |
718 | list_del(&async_extent->list); | |
c8b97818 | 719 | |
771ed689 CM |
720 | io_tree = &BTRFS_I(inode)->io_tree; |
721 | ||
f5a84ee3 | 722 | retry: |
771ed689 CM |
723 | /* did the compression code fall back to uncompressed IO? */ |
724 | if (!async_extent->pages) { | |
725 | int page_started = 0; | |
726 | unsigned long nr_written = 0; | |
727 | ||
728 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 729 | async_extent->start + |
d0082371 | 730 | async_extent->ram_size - 1); |
771ed689 CM |
731 | |
732 | /* allocate blocks */ | |
f5a84ee3 JB |
733 | ret = cow_file_range(inode, async_cow->locked_page, |
734 | async_extent->start, | |
735 | async_extent->start + | |
736 | async_extent->ram_size - 1, | |
dda3245e WX |
737 | async_extent->start + |
738 | async_extent->ram_size - 1, | |
739 | &page_started, &nr_written, 0, | |
740 | NULL); | |
771ed689 | 741 | |
79787eaa JM |
742 | /* JDM XXX */ |
743 | ||
771ed689 CM |
744 | /* |
745 | * if page_started, cow_file_range inserted an | |
746 | * inline extent and took care of all the unlocking | |
747 | * and IO for us. Otherwise, we need to submit | |
748 | * all those pages down to the drive. | |
749 | */ | |
f5a84ee3 | 750 | if (!page_started && !ret) |
771ed689 CM |
751 | extent_write_locked_range(io_tree, |
752 | inode, async_extent->start, | |
d397712b | 753 | async_extent->start + |
771ed689 CM |
754 | async_extent->ram_size - 1, |
755 | btrfs_get_extent, | |
756 | WB_SYNC_ALL); | |
3e04e7f1 JB |
757 | else if (ret) |
758 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
759 | kfree(async_extent); |
760 | cond_resched(); | |
761 | continue; | |
762 | } | |
763 | ||
764 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 765 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 766 | |
18513091 | 767 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
768 | async_extent->compressed_size, |
769 | async_extent->compressed_size, | |
e570fd27 | 770 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 771 | if (ret) { |
40ae837b | 772 | free_async_extent_pages(async_extent); |
3e04e7f1 | 773 | |
fdf8e2ea JB |
774 | if (ret == -ENOSPC) { |
775 | unlock_extent(io_tree, async_extent->start, | |
776 | async_extent->start + | |
777 | async_extent->ram_size - 1); | |
ce62003f LB |
778 | |
779 | /* | |
780 | * we need to redirty the pages if we decide to | |
781 | * fallback to uncompressed IO, otherwise we | |
782 | * will not submit these pages down to lower | |
783 | * layers. | |
784 | */ | |
785 | extent_range_redirty_for_io(inode, | |
786 | async_extent->start, | |
787 | async_extent->start + | |
788 | async_extent->ram_size - 1); | |
789 | ||
79787eaa | 790 | goto retry; |
fdf8e2ea | 791 | } |
3e04e7f1 | 792 | goto out_free; |
f5a84ee3 | 793 | } |
c2167754 YZ |
794 | /* |
795 | * here we're doing allocation and writeback of the | |
796 | * compressed pages | |
797 | */ | |
6f9994db LB |
798 | em = create_io_em(inode, async_extent->start, |
799 | async_extent->ram_size, /* len */ | |
800 | async_extent->start, /* orig_start */ | |
801 | ins.objectid, /* block_start */ | |
802 | ins.offset, /* block_len */ | |
803 | ins.offset, /* orig_block_len */ | |
804 | async_extent->ram_size, /* ram_bytes */ | |
805 | async_extent->compress_type, | |
806 | BTRFS_ORDERED_COMPRESSED); | |
807 | if (IS_ERR(em)) | |
808 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 809 | goto out_free_reserve; |
6f9994db | 810 | free_extent_map(em); |
3e04e7f1 | 811 | |
261507a0 LZ |
812 | ret = btrfs_add_ordered_extent_compress(inode, |
813 | async_extent->start, | |
814 | ins.objectid, | |
815 | async_extent->ram_size, | |
816 | ins.offset, | |
817 | BTRFS_ORDERED_COMPRESSED, | |
818 | async_extent->compress_type); | |
d9f85963 | 819 | if (ret) { |
dcdbc059 NB |
820 | btrfs_drop_extent_cache(BTRFS_I(inode), |
821 | async_extent->start, | |
d9f85963 FM |
822 | async_extent->start + |
823 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 824 | goto out_free_reserve; |
d9f85963 | 825 | } |
0b246afa | 826 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 827 | |
771ed689 CM |
828 | /* |
829 | * clear dirty, set writeback and unlock the pages. | |
830 | */ | |
c2790a2e | 831 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
832 | async_extent->start + |
833 | async_extent->ram_size - 1, | |
a791e35e CM |
834 | async_extent->start + |
835 | async_extent->ram_size - 1, | |
151a41bc JB |
836 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
837 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 838 | PAGE_SET_WRITEBACK); |
771ed689 | 839 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
840 | async_extent->start, |
841 | async_extent->ram_size, | |
842 | ins.objectid, | |
843 | ins.offset, async_extent->pages, | |
844 | async_extent->nr_pages); | |
fce2a4e6 FM |
845 | if (ret) { |
846 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
847 | struct page *p = async_extent->pages[0]; | |
848 | const u64 start = async_extent->start; | |
849 | const u64 end = start + async_extent->ram_size - 1; | |
850 | ||
851 | p->mapping = inode->i_mapping; | |
852 | tree->ops->writepage_end_io_hook(p, start, end, | |
853 | NULL, 0); | |
854 | p->mapping = NULL; | |
ba8b04c1 QW |
855 | extent_clear_unlock_delalloc(inode, start, end, end, |
856 | NULL, 0, | |
fce2a4e6 FM |
857 | PAGE_END_WRITEBACK | |
858 | PAGE_SET_ERROR); | |
40ae837b | 859 | free_async_extent_pages(async_extent); |
fce2a4e6 | 860 | } |
771ed689 CM |
861 | alloc_hint = ins.objectid + ins.offset; |
862 | kfree(async_extent); | |
863 | cond_resched(); | |
864 | } | |
dec8f175 | 865 | return; |
3e04e7f1 | 866 | out_free_reserve: |
0b246afa | 867 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 868 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 869 | out_free: |
c2790a2e | 870 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
871 | async_extent->start + |
872 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
873 | async_extent->start + |
874 | async_extent->ram_size - 1, | |
c2790a2e | 875 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 876 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
877 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
878 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
879 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
880 | PAGE_SET_ERROR); | |
40ae837b | 881 | free_async_extent_pages(async_extent); |
79787eaa | 882 | kfree(async_extent); |
3e04e7f1 | 883 | goto again; |
771ed689 CM |
884 | } |
885 | ||
4b46fce2 JB |
886 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
887 | u64 num_bytes) | |
888 | { | |
889 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
890 | struct extent_map *em; | |
891 | u64 alloc_hint = 0; | |
892 | ||
893 | read_lock(&em_tree->lock); | |
894 | em = search_extent_mapping(em_tree, start, num_bytes); | |
895 | if (em) { | |
896 | /* | |
897 | * if block start isn't an actual block number then find the | |
898 | * first block in this inode and use that as a hint. If that | |
899 | * block is also bogus then just don't worry about it. | |
900 | */ | |
901 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
902 | free_extent_map(em); | |
903 | em = search_extent_mapping(em_tree, 0, 0); | |
904 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
905 | alloc_hint = em->block_start; | |
906 | if (em) | |
907 | free_extent_map(em); | |
908 | } else { | |
909 | alloc_hint = em->block_start; | |
910 | free_extent_map(em); | |
911 | } | |
912 | } | |
913 | read_unlock(&em_tree->lock); | |
914 | ||
915 | return alloc_hint; | |
916 | } | |
917 | ||
771ed689 CM |
918 | /* |
919 | * when extent_io.c finds a delayed allocation range in the file, | |
920 | * the call backs end up in this code. The basic idea is to | |
921 | * allocate extents on disk for the range, and create ordered data structs | |
922 | * in ram to track those extents. | |
923 | * | |
924 | * locked_page is the page that writepage had locked already. We use | |
925 | * it to make sure we don't do extra locks or unlocks. | |
926 | * | |
927 | * *page_started is set to one if we unlock locked_page and do everything | |
928 | * required to start IO on it. It may be clean and already done with | |
929 | * IO when we return. | |
930 | */ | |
00361589 JB |
931 | static noinline int cow_file_range(struct inode *inode, |
932 | struct page *locked_page, | |
dda3245e WX |
933 | u64 start, u64 end, u64 delalloc_end, |
934 | int *page_started, unsigned long *nr_written, | |
935 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 936 | { |
0b246afa | 937 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 938 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
939 | u64 alloc_hint = 0; |
940 | u64 num_bytes; | |
941 | unsigned long ram_size; | |
942 | u64 disk_num_bytes; | |
a315e68f | 943 | u64 cur_alloc_size = 0; |
0b246afa | 944 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
945 | struct btrfs_key ins; |
946 | struct extent_map *em; | |
a315e68f FM |
947 | unsigned clear_bits; |
948 | unsigned long page_ops; | |
949 | bool extent_reserved = false; | |
771ed689 CM |
950 | int ret = 0; |
951 | ||
70ddc553 | 952 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 953 | WARN_ON_ONCE(1); |
29bce2f3 JB |
954 | ret = -EINVAL; |
955 | goto out_unlock; | |
02ecd2c2 | 956 | } |
771ed689 | 957 | |
fda2832f | 958 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
959 | num_bytes = max(blocksize, num_bytes); |
960 | disk_num_bytes = num_bytes; | |
771ed689 | 961 | |
6158e1ce | 962 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 963 | |
771ed689 CM |
964 | if (start == 0) { |
965 | /* lets try to make an inline extent */ | |
f74670f7 AJ |
966 | ret = cow_file_range_inline(root, inode, start, end, 0, |
967 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 968 | if (ret == 0) { |
ba8b04c1 QW |
969 | extent_clear_unlock_delalloc(inode, start, end, |
970 | delalloc_end, NULL, | |
c2790a2e | 971 | EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 972 | EXTENT_DELALLOC_NEW | |
151a41bc | 973 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
974 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
975 | PAGE_END_WRITEBACK); | |
18513091 WX |
976 | btrfs_free_reserved_data_space_noquota(inode, start, |
977 | end - start + 1); | |
771ed689 | 978 | *nr_written = *nr_written + |
09cbfeaf | 979 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 980 | *page_started = 1; |
771ed689 | 981 | goto out; |
79787eaa | 982 | } else if (ret < 0) { |
79787eaa | 983 | goto out_unlock; |
771ed689 CM |
984 | } |
985 | } | |
986 | ||
987 | BUG_ON(disk_num_bytes > | |
0b246afa | 988 | btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 989 | |
4b46fce2 | 990 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
991 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
992 | start + num_bytes - 1, 0); | |
771ed689 | 993 | |
d397712b | 994 | while (disk_num_bytes > 0) { |
287a0ab9 | 995 | cur_alloc_size = disk_num_bytes; |
18513091 | 996 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 997 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 998 | &ins, 1, 1); |
00361589 | 999 | if (ret < 0) |
79787eaa | 1000 | goto out_unlock; |
a315e68f FM |
1001 | cur_alloc_size = ins.offset; |
1002 | extent_reserved = true; | |
d397712b | 1003 | |
771ed689 | 1004 | ram_size = ins.offset; |
6f9994db LB |
1005 | em = create_io_em(inode, start, ins.offset, /* len */ |
1006 | start, /* orig_start */ | |
1007 | ins.objectid, /* block_start */ | |
1008 | ins.offset, /* block_len */ | |
1009 | ins.offset, /* orig_block_len */ | |
1010 | ram_size, /* ram_bytes */ | |
1011 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1012 | BTRFS_ORDERED_REGULAR /* type */); |
6f9994db | 1013 | if (IS_ERR(em)) |
ace68bac | 1014 | goto out_reserve; |
6f9994db | 1015 | free_extent_map(em); |
e6dcd2dc | 1016 | |
e6dcd2dc | 1017 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1018 | ram_size, cur_alloc_size, 0); |
ace68bac | 1019 | if (ret) |
d9f85963 | 1020 | goto out_drop_extent_cache; |
c8b97818 | 1021 | |
17d217fe YZ |
1022 | if (root->root_key.objectid == |
1023 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1024 | ret = btrfs_reloc_clone_csums(inode, start, | |
1025 | cur_alloc_size); | |
4dbd80fb QW |
1026 | /* |
1027 | * Only drop cache here, and process as normal. | |
1028 | * | |
1029 | * We must not allow extent_clear_unlock_delalloc() | |
1030 | * at out_unlock label to free meta of this ordered | |
1031 | * extent, as its meta should be freed by | |
1032 | * btrfs_finish_ordered_io(). | |
1033 | * | |
1034 | * So we must continue until @start is increased to | |
1035 | * skip current ordered extent. | |
1036 | */ | |
00361589 | 1037 | if (ret) |
4dbd80fb QW |
1038 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1039 | start + ram_size - 1, 0); | |
17d217fe YZ |
1040 | } |
1041 | ||
0b246afa | 1042 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1043 | |
c8b97818 CM |
1044 | /* we're not doing compressed IO, don't unlock the first |
1045 | * page (which the caller expects to stay locked), don't | |
1046 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1047 | * |
1048 | * Do set the Private2 bit so we know this page was properly | |
1049 | * setup for writepage | |
c8b97818 | 1050 | */ |
a315e68f FM |
1051 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1052 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1053 | |
c2790a2e | 1054 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1055 | start + ram_size - 1, |
1056 | delalloc_end, locked_page, | |
c2790a2e | 1057 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1058 | page_ops); |
4dbd80fb QW |
1059 | if (disk_num_bytes < cur_alloc_size) |
1060 | disk_num_bytes = 0; | |
1061 | else | |
1062 | disk_num_bytes -= cur_alloc_size; | |
c59f8951 CM |
1063 | num_bytes -= cur_alloc_size; |
1064 | alloc_hint = ins.objectid + ins.offset; | |
1065 | start += cur_alloc_size; | |
a315e68f | 1066 | extent_reserved = false; |
4dbd80fb QW |
1067 | |
1068 | /* | |
1069 | * btrfs_reloc_clone_csums() error, since start is increased | |
1070 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1071 | * free metadata of current ordered extent, we're OK to exit. | |
1072 | */ | |
1073 | if (ret) | |
1074 | goto out_unlock; | |
b888db2b | 1075 | } |
79787eaa | 1076 | out: |
be20aa9d | 1077 | return ret; |
b7d5b0a8 | 1078 | |
d9f85963 | 1079 | out_drop_extent_cache: |
dcdbc059 | 1080 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1081 | out_reserve: |
0b246afa | 1082 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1083 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1084 | out_unlock: |
a7e3b975 FM |
1085 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1086 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1087 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1088 | PAGE_END_WRITEBACK; | |
1089 | /* | |
1090 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1091 | * failed to create the respective ordered extent, then it means that | |
1092 | * when we reserved the extent we decremented the extent's size from | |
1093 | * the data space_info's bytes_may_use counter and incremented the | |
1094 | * space_info's bytes_reserved counter by the same amount. We must make | |
1095 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1096 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1097 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1098 | */ | |
1099 | if (extent_reserved) { | |
1100 | extent_clear_unlock_delalloc(inode, start, | |
1101 | start + cur_alloc_size, | |
1102 | start + cur_alloc_size, | |
1103 | locked_page, | |
1104 | clear_bits, | |
1105 | page_ops); | |
1106 | start += cur_alloc_size; | |
1107 | if (start >= end) | |
1108 | goto out; | |
1109 | } | |
ba8b04c1 QW |
1110 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1111 | locked_page, | |
a315e68f FM |
1112 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1113 | page_ops); | |
79787eaa | 1114 | goto out; |
771ed689 | 1115 | } |
c8b97818 | 1116 | |
771ed689 CM |
1117 | /* |
1118 | * work queue call back to started compression on a file and pages | |
1119 | */ | |
1120 | static noinline void async_cow_start(struct btrfs_work *work) | |
1121 | { | |
1122 | struct async_cow *async_cow; | |
1123 | int num_added = 0; | |
1124 | async_cow = container_of(work, struct async_cow, work); | |
1125 | ||
1126 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1127 | async_cow->start, async_cow->end, async_cow, | |
1128 | &num_added); | |
8180ef88 | 1129 | if (num_added == 0) { |
cb77fcd8 | 1130 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1131 | async_cow->inode = NULL; |
8180ef88 | 1132 | } |
771ed689 CM |
1133 | } |
1134 | ||
1135 | /* | |
1136 | * work queue call back to submit previously compressed pages | |
1137 | */ | |
1138 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1139 | { | |
0b246afa | 1140 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
1141 | struct async_cow *async_cow; |
1142 | struct btrfs_root *root; | |
1143 | unsigned long nr_pages; | |
1144 | ||
1145 | async_cow = container_of(work, struct async_cow, work); | |
1146 | ||
1147 | root = async_cow->root; | |
0b246afa | 1148 | fs_info = root->fs_info; |
09cbfeaf KS |
1149 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1150 | PAGE_SHIFT; | |
771ed689 | 1151 | |
ee863954 DS |
1152 | /* |
1153 | * atomic_sub_return implies a barrier for waitqueue_active | |
1154 | */ | |
0b246afa | 1155 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
ee22184b | 1156 | 5 * SZ_1M && |
0b246afa JM |
1157 | waitqueue_active(&fs_info->async_submit_wait)) |
1158 | wake_up(&fs_info->async_submit_wait); | |
771ed689 | 1159 | |
d397712b | 1160 | if (async_cow->inode) |
771ed689 | 1161 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1162 | } |
c8b97818 | 1163 | |
771ed689 CM |
1164 | static noinline void async_cow_free(struct btrfs_work *work) |
1165 | { | |
1166 | struct async_cow *async_cow; | |
1167 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1168 | if (async_cow->inode) |
cb77fcd8 | 1169 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1170 | kfree(async_cow); |
1171 | } | |
1172 | ||
1173 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1174 | u64 start, u64 end, int *page_started, | |
1175 | unsigned long *nr_written) | |
1176 | { | |
0b246afa | 1177 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
1178 | struct async_cow *async_cow; |
1179 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1180 | unsigned long nr_pages; | |
1181 | u64 cur_end; | |
771ed689 | 1182 | |
a3429ab7 CM |
1183 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1184 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1185 | while (start < end) { |
771ed689 | 1186 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1187 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1188 | async_cow->inode = igrab(inode); |
771ed689 CM |
1189 | async_cow->root = root; |
1190 | async_cow->locked_page = locked_page; | |
1191 | async_cow->start = start; | |
1192 | ||
f79707b0 | 1193 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1194 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1195 | cur_end = end; |
1196 | else | |
ee22184b | 1197 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1198 | |
1199 | async_cow->end = cur_end; | |
1200 | INIT_LIST_HEAD(&async_cow->extents); | |
1201 | ||
9e0af237 LB |
1202 | btrfs_init_work(&async_cow->work, |
1203 | btrfs_delalloc_helper, | |
1204 | async_cow_start, async_cow_submit, | |
1205 | async_cow_free); | |
771ed689 | 1206 | |
09cbfeaf KS |
1207 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1208 | PAGE_SHIFT; | |
0b246afa | 1209 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1210 | |
0b246afa | 1211 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1212 | |
0b246afa JM |
1213 | while (atomic_read(&fs_info->async_submit_draining) && |
1214 | atomic_read(&fs_info->async_delalloc_pages)) { | |
1215 | wait_event(fs_info->async_submit_wait, | |
1216 | (atomic_read(&fs_info->async_delalloc_pages) == | |
1217 | 0)); | |
771ed689 CM |
1218 | } |
1219 | ||
1220 | *nr_written += nr_pages; | |
1221 | start = cur_end + 1; | |
1222 | } | |
1223 | *page_started = 1; | |
1224 | return 0; | |
be20aa9d CM |
1225 | } |
1226 | ||
2ff7e61e | 1227 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1228 | u64 bytenr, u64 num_bytes) |
1229 | { | |
1230 | int ret; | |
1231 | struct btrfs_ordered_sum *sums; | |
1232 | LIST_HEAD(list); | |
1233 | ||
0b246afa | 1234 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1235 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1236 | if (ret == 0 && list_empty(&list)) |
1237 | return 0; | |
1238 | ||
1239 | while (!list_empty(&list)) { | |
1240 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1241 | list_del(&sums->list); | |
1242 | kfree(sums); | |
1243 | } | |
1244 | return 1; | |
1245 | } | |
1246 | ||
d352ac68 CM |
1247 | /* |
1248 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1249 | * of the extents that exist in the file, and COWs the file as required. | |
1250 | * | |
1251 | * If no cow copies or snapshots exist, we write directly to the existing | |
1252 | * blocks on disk | |
1253 | */ | |
7f366cfe CM |
1254 | static noinline int run_delalloc_nocow(struct inode *inode, |
1255 | struct page *locked_page, | |
771ed689 CM |
1256 | u64 start, u64 end, int *page_started, int force, |
1257 | unsigned long *nr_written) | |
be20aa9d | 1258 | { |
0b246afa | 1259 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d CM |
1260 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1261 | struct extent_buffer *leaf; | |
be20aa9d | 1262 | struct btrfs_path *path; |
80ff3856 | 1263 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1264 | struct btrfs_key found_key; |
6f9994db | 1265 | struct extent_map *em; |
80ff3856 YZ |
1266 | u64 cow_start; |
1267 | u64 cur_offset; | |
1268 | u64 extent_end; | |
5d4f98a2 | 1269 | u64 extent_offset; |
80ff3856 YZ |
1270 | u64 disk_bytenr; |
1271 | u64 num_bytes; | |
b4939680 | 1272 | u64 disk_num_bytes; |
cc95bef6 | 1273 | u64 ram_bytes; |
80ff3856 | 1274 | int extent_type; |
79787eaa | 1275 | int ret, err; |
d899e052 | 1276 | int type; |
80ff3856 YZ |
1277 | int nocow; |
1278 | int check_prev = 1; | |
82d5902d | 1279 | bool nolock; |
4a0cc7ca | 1280 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1281 | |
1282 | path = btrfs_alloc_path(); | |
17ca04af | 1283 | if (!path) { |
ba8b04c1 QW |
1284 | extent_clear_unlock_delalloc(inode, start, end, end, |
1285 | locked_page, | |
c2790a2e | 1286 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1287 | EXTENT_DO_ACCOUNTING | |
1288 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1289 | PAGE_CLEAR_DIRTY | |
1290 | PAGE_SET_WRITEBACK | | |
1291 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1292 | return -ENOMEM; |
17ca04af | 1293 | } |
82d5902d | 1294 | |
70ddc553 | 1295 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
82d5902d | 1296 | |
80ff3856 YZ |
1297 | cow_start = (u64)-1; |
1298 | cur_offset = start; | |
1299 | while (1) { | |
e4c3b2dc | 1300 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1301 | cur_offset, 0); |
d788a349 | 1302 | if (ret < 0) |
79787eaa | 1303 | goto error; |
80ff3856 YZ |
1304 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1305 | leaf = path->nodes[0]; | |
1306 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1307 | path->slots[0] - 1); | |
33345d01 | 1308 | if (found_key.objectid == ino && |
80ff3856 YZ |
1309 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1310 | path->slots[0]--; | |
1311 | } | |
1312 | check_prev = 0; | |
1313 | next_slot: | |
1314 | leaf = path->nodes[0]; | |
1315 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1316 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1317 | if (ret < 0) |
79787eaa | 1318 | goto error; |
80ff3856 YZ |
1319 | if (ret > 0) |
1320 | break; | |
1321 | leaf = path->nodes[0]; | |
1322 | } | |
be20aa9d | 1323 | |
80ff3856 YZ |
1324 | nocow = 0; |
1325 | disk_bytenr = 0; | |
17d217fe | 1326 | num_bytes = 0; |
80ff3856 YZ |
1327 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1328 | ||
1d512cb7 FM |
1329 | if (found_key.objectid > ino) |
1330 | break; | |
1331 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1332 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1333 | path->slots[0]++; | |
1334 | goto next_slot; | |
1335 | } | |
1336 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1337 | found_key.offset > end) |
1338 | break; | |
1339 | ||
1340 | if (found_key.offset > cur_offset) { | |
1341 | extent_end = found_key.offset; | |
e9061e21 | 1342 | extent_type = 0; |
80ff3856 YZ |
1343 | goto out_check; |
1344 | } | |
1345 | ||
1346 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1347 | struct btrfs_file_extent_item); | |
1348 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1349 | ||
cc95bef6 | 1350 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1351 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1352 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1353 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1354 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1355 | extent_end = found_key.offset + |
1356 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1357 | disk_num_bytes = |
1358 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1359 | if (extent_end <= start) { |
1360 | path->slots[0]++; | |
1361 | goto next_slot; | |
1362 | } | |
17d217fe YZ |
1363 | if (disk_bytenr == 0) |
1364 | goto out_check; | |
80ff3856 YZ |
1365 | if (btrfs_file_extent_compression(leaf, fi) || |
1366 | btrfs_file_extent_encryption(leaf, fi) || | |
1367 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1368 | goto out_check; | |
d899e052 YZ |
1369 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1370 | goto out_check; | |
2ff7e61e | 1371 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1372 | goto out_check; |
e4c3b2dc | 1373 | if (btrfs_cross_ref_exist(root, ino, |
5d4f98a2 YZ |
1374 | found_key.offset - |
1375 | extent_offset, disk_bytenr)) | |
17d217fe | 1376 | goto out_check; |
5d4f98a2 | 1377 | disk_bytenr += extent_offset; |
17d217fe YZ |
1378 | disk_bytenr += cur_offset - found_key.offset; |
1379 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1380 | /* |
1381 | * if there are pending snapshots for this root, | |
1382 | * we fall into common COW way. | |
1383 | */ | |
1384 | if (!nolock) { | |
9ea24bbe | 1385 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1386 | if (!err) |
1387 | goto out_check; | |
1388 | } | |
17d217fe YZ |
1389 | /* |
1390 | * force cow if csum exists in the range. | |
1391 | * this ensure that csum for a given extent are | |
1392 | * either valid or do not exist. | |
1393 | */ | |
2ff7e61e | 1394 | if (csum_exist_in_range(fs_info, disk_bytenr, |
91e1f56a RK |
1395 | num_bytes)) { |
1396 | if (!nolock) | |
1397 | btrfs_end_write_no_snapshoting(root); | |
17d217fe | 1398 | goto out_check; |
91e1f56a RK |
1399 | } |
1400 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { | |
1401 | if (!nolock) | |
1402 | btrfs_end_write_no_snapshoting(root); | |
f78c436c | 1403 | goto out_check; |
91e1f56a | 1404 | } |
80ff3856 YZ |
1405 | nocow = 1; |
1406 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1407 | extent_end = found_key.offset + | |
514ac8ad CM |
1408 | btrfs_file_extent_inline_len(leaf, |
1409 | path->slots[0], fi); | |
da17066c | 1410 | extent_end = ALIGN(extent_end, |
0b246afa | 1411 | fs_info->sectorsize); |
80ff3856 YZ |
1412 | } else { |
1413 | BUG_ON(1); | |
1414 | } | |
1415 | out_check: | |
1416 | if (extent_end <= start) { | |
1417 | path->slots[0]++; | |
e9894fd3 | 1418 | if (!nolock && nocow) |
9ea24bbe | 1419 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1420 | if (nocow) |
0b246afa | 1421 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1422 | goto next_slot; |
1423 | } | |
1424 | if (!nocow) { | |
1425 | if (cow_start == (u64)-1) | |
1426 | cow_start = cur_offset; | |
1427 | cur_offset = extent_end; | |
1428 | if (cur_offset > end) | |
1429 | break; | |
1430 | path->slots[0]++; | |
1431 | goto next_slot; | |
7ea394f1 YZ |
1432 | } |
1433 | ||
b3b4aa74 | 1434 | btrfs_release_path(path); |
80ff3856 | 1435 | if (cow_start != (u64)-1) { |
00361589 JB |
1436 | ret = cow_file_range(inode, locked_page, |
1437 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1438 | end, page_started, nr_written, 1, |
1439 | NULL); | |
e9894fd3 WS |
1440 | if (ret) { |
1441 | if (!nolock && nocow) | |
9ea24bbe | 1442 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1443 | if (nocow) |
0b246afa | 1444 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1445 | disk_bytenr); |
79787eaa | 1446 | goto error; |
e9894fd3 | 1447 | } |
80ff3856 | 1448 | cow_start = (u64)-1; |
7ea394f1 | 1449 | } |
80ff3856 | 1450 | |
d899e052 | 1451 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db LB |
1452 | u64 orig_start = found_key.offset - extent_offset; |
1453 | ||
1454 | em = create_io_em(inode, cur_offset, num_bytes, | |
1455 | orig_start, | |
1456 | disk_bytenr, /* block_start */ | |
1457 | num_bytes, /* block_len */ | |
1458 | disk_num_bytes, /* orig_block_len */ | |
1459 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1460 | BTRFS_ORDERED_PREALLOC); | |
1461 | if (IS_ERR(em)) { | |
1462 | if (!nolock && nocow) | |
1463 | btrfs_end_write_no_snapshoting(root); | |
1464 | if (nocow) | |
1465 | btrfs_dec_nocow_writers(fs_info, | |
1466 | disk_bytenr); | |
1467 | ret = PTR_ERR(em); | |
1468 | goto error; | |
d899e052 | 1469 | } |
6f9994db LB |
1470 | free_extent_map(em); |
1471 | } | |
1472 | ||
1473 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d899e052 YZ |
1474 | type = BTRFS_ORDERED_PREALLOC; |
1475 | } else { | |
1476 | type = BTRFS_ORDERED_NOCOW; | |
1477 | } | |
80ff3856 YZ |
1478 | |
1479 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1480 | num_bytes, num_bytes, type); |
f78c436c | 1481 | if (nocow) |
0b246afa | 1482 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1483 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1484 | |
efa56464 | 1485 | if (root->root_key.objectid == |
4dbd80fb QW |
1486 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1487 | /* | |
1488 | * Error handled later, as we must prevent | |
1489 | * extent_clear_unlock_delalloc() in error handler | |
1490 | * from freeing metadata of created ordered extent. | |
1491 | */ | |
efa56464 YZ |
1492 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1493 | num_bytes); | |
efa56464 | 1494 | |
c2790a2e | 1495 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1496 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1497 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1498 | EXTENT_DELALLOC | |
1499 | EXTENT_CLEAR_DATA_RESV, | |
1500 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1501 | ||
e9894fd3 | 1502 | if (!nolock && nocow) |
9ea24bbe | 1503 | btrfs_end_write_no_snapshoting(root); |
80ff3856 | 1504 | cur_offset = extent_end; |
4dbd80fb QW |
1505 | |
1506 | /* | |
1507 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1508 | * handler, as metadata for created ordered extent will only | |
1509 | * be freed by btrfs_finish_ordered_io(). | |
1510 | */ | |
1511 | if (ret) | |
1512 | goto error; | |
80ff3856 YZ |
1513 | if (cur_offset > end) |
1514 | break; | |
be20aa9d | 1515 | } |
b3b4aa74 | 1516 | btrfs_release_path(path); |
80ff3856 | 1517 | |
17ca04af | 1518 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1519 | cow_start = cur_offset; |
17ca04af JB |
1520 | cur_offset = end; |
1521 | } | |
1522 | ||
80ff3856 | 1523 | if (cow_start != (u64)-1) { |
dda3245e WX |
1524 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1525 | page_started, nr_written, 1, NULL); | |
d788a349 | 1526 | if (ret) |
79787eaa | 1527 | goto error; |
80ff3856 YZ |
1528 | } |
1529 | ||
79787eaa | 1530 | error: |
17ca04af | 1531 | if (ret && cur_offset < end) |
ba8b04c1 | 1532 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1533 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1534 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1535 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1536 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1537 | PAGE_SET_WRITEBACK | |
1538 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1539 | btrfs_free_path(path); |
79787eaa | 1540 | return ret; |
be20aa9d CM |
1541 | } |
1542 | ||
47059d93 WS |
1543 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1544 | { | |
1545 | ||
1546 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1547 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1548 | return 0; | |
1549 | ||
1550 | /* | |
1551 | * @defrag_bytes is a hint value, no spinlock held here, | |
1552 | * if is not zero, it means the file is defragging. | |
1553 | * Force cow if given extent needs to be defragged. | |
1554 | */ | |
1555 | if (BTRFS_I(inode)->defrag_bytes && | |
1556 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1557 | EXTENT_DEFRAG, 0, NULL)) | |
1558 | return 1; | |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | ||
d352ac68 CM |
1563 | /* |
1564 | * extent_io.c call back to do delayed allocation processing | |
1565 | */ | |
c6100a4b | 1566 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
771ed689 CM |
1567 | u64 start, u64 end, int *page_started, |
1568 | unsigned long *nr_written) | |
be20aa9d | 1569 | { |
c6100a4b | 1570 | struct inode *inode = private_data; |
be20aa9d | 1571 | int ret; |
47059d93 | 1572 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1573 | |
47059d93 | 1574 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1575 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1576 | page_started, 1, nr_written); |
47059d93 | 1577 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1578 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1579 | page_started, 0, nr_written); |
7816030e | 1580 | } else if (!inode_need_compress(inode)) { |
dda3245e WX |
1581 | ret = cow_file_range(inode, locked_page, start, end, end, |
1582 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1583 | } else { |
1584 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1585 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1586 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1587 | page_started, nr_written); |
7ddf5a42 | 1588 | } |
52427260 QW |
1589 | if (ret) |
1590 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); | |
b888db2b CM |
1591 | return ret; |
1592 | } | |
1593 | ||
c6100a4b | 1594 | static void btrfs_split_extent_hook(void *private_data, |
1bf85046 | 1595 | struct extent_state *orig, u64 split) |
9ed74f2d | 1596 | { |
c6100a4b | 1597 | struct inode *inode = private_data; |
dcab6a3b JB |
1598 | u64 size; |
1599 | ||
0ca1f7ce | 1600 | /* not delalloc, ignore it */ |
9ed74f2d | 1601 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1602 | return; |
9ed74f2d | 1603 | |
dcab6a3b JB |
1604 | size = orig->end - orig->start + 1; |
1605 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1606 | u32 num_extents; |
dcab6a3b JB |
1607 | u64 new_size; |
1608 | ||
1609 | /* | |
ba117213 JB |
1610 | * See the explanation in btrfs_merge_extent_hook, the same |
1611 | * applies here, just in reverse. | |
dcab6a3b JB |
1612 | */ |
1613 | new_size = orig->end - split + 1; | |
823bb20a | 1614 | num_extents = count_max_extents(new_size); |
ba117213 | 1615 | new_size = split - orig->start; |
823bb20a DS |
1616 | num_extents += count_max_extents(new_size); |
1617 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1618 | return; |
1619 | } | |
1620 | ||
9e0baf60 JB |
1621 | spin_lock(&BTRFS_I(inode)->lock); |
1622 | BTRFS_I(inode)->outstanding_extents++; | |
1623 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1624 | } |
1625 | ||
1626 | /* | |
1627 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1628 | * extents so we can keep track of new extents that are just merged onto old | |
1629 | * extents, such as when we are doing sequential writes, so we can properly | |
1630 | * account for the metadata space we'll need. | |
1631 | */ | |
c6100a4b | 1632 | static void btrfs_merge_extent_hook(void *private_data, |
1bf85046 JM |
1633 | struct extent_state *new, |
1634 | struct extent_state *other) | |
9ed74f2d | 1635 | { |
c6100a4b | 1636 | struct inode *inode = private_data; |
dcab6a3b | 1637 | u64 new_size, old_size; |
823bb20a | 1638 | u32 num_extents; |
dcab6a3b | 1639 | |
9ed74f2d JB |
1640 | /* not delalloc, ignore it */ |
1641 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1642 | return; |
9ed74f2d | 1643 | |
8461a3de JB |
1644 | if (new->start > other->start) |
1645 | new_size = new->end - other->start + 1; | |
1646 | else | |
1647 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1648 | |
1649 | /* we're not bigger than the max, unreserve the space and go */ | |
1650 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1651 | spin_lock(&BTRFS_I(inode)->lock); | |
1652 | BTRFS_I(inode)->outstanding_extents--; | |
1653 | spin_unlock(&BTRFS_I(inode)->lock); | |
1654 | return; | |
1655 | } | |
1656 | ||
1657 | /* | |
ba117213 JB |
1658 | * We have to add up either side to figure out how many extents were |
1659 | * accounted for before we merged into one big extent. If the number of | |
1660 | * extents we accounted for is <= the amount we need for the new range | |
1661 | * then we can return, otherwise drop. Think of it like this | |
1662 | * | |
1663 | * [ 4k][MAX_SIZE] | |
1664 | * | |
1665 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1666 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1667 | * we have 1 so they are == and we can return. But in this case | |
1668 | * | |
1669 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1670 | * | |
1671 | * Each range on their own accounts for 2 extents, but merged together | |
1672 | * they are only 3 extents worth of accounting, so we need to drop in | |
1673 | * this case. | |
dcab6a3b | 1674 | */ |
ba117213 | 1675 | old_size = other->end - other->start + 1; |
823bb20a | 1676 | num_extents = count_max_extents(old_size); |
ba117213 | 1677 | old_size = new->end - new->start + 1; |
823bb20a DS |
1678 | num_extents += count_max_extents(old_size); |
1679 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1680 | return; |
1681 | ||
9e0baf60 JB |
1682 | spin_lock(&BTRFS_I(inode)->lock); |
1683 | BTRFS_I(inode)->outstanding_extents--; | |
1684 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1685 | } |
1686 | ||
eb73c1b7 MX |
1687 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1688 | struct inode *inode) | |
1689 | { | |
0b246afa JM |
1690 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1691 | ||
eb73c1b7 MX |
1692 | spin_lock(&root->delalloc_lock); |
1693 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1694 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1695 | &root->delalloc_inodes); | |
1696 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1697 | &BTRFS_I(inode)->runtime_flags); | |
1698 | root->nr_delalloc_inodes++; | |
1699 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1700 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1701 | BUG_ON(!list_empty(&root->delalloc_root)); |
1702 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1703 | &fs_info->delalloc_roots); |
1704 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1705 | } |
1706 | } | |
1707 | spin_unlock(&root->delalloc_lock); | |
1708 | } | |
1709 | ||
1710 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
9e3e97f4 | 1711 | struct btrfs_inode *inode) |
eb73c1b7 | 1712 | { |
9e3e97f4 | 1713 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
0b246afa | 1714 | |
eb73c1b7 | 1715 | spin_lock(&root->delalloc_lock); |
9e3e97f4 NB |
1716 | if (!list_empty(&inode->delalloc_inodes)) { |
1717 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1718 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1719 | &inode->runtime_flags); |
eb73c1b7 MX |
1720 | root->nr_delalloc_inodes--; |
1721 | if (!root->nr_delalloc_inodes) { | |
0b246afa | 1722 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1723 | BUG_ON(list_empty(&root->delalloc_root)); |
1724 | list_del_init(&root->delalloc_root); | |
0b246afa | 1725 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1726 | } |
1727 | } | |
1728 | spin_unlock(&root->delalloc_lock); | |
1729 | } | |
1730 | ||
d352ac68 CM |
1731 | /* |
1732 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1733 | * bytes in this file, and to maintain the list of inodes that | |
1734 | * have pending delalloc work to be done. | |
1735 | */ | |
c6100a4b | 1736 | static void btrfs_set_bit_hook(void *private_data, |
9ee49a04 | 1737 | struct extent_state *state, unsigned *bits) |
291d673e | 1738 | { |
c6100a4b | 1739 | struct inode *inode = private_data; |
9ed74f2d | 1740 | |
0b246afa JM |
1741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1742 | ||
47059d93 WS |
1743 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1744 | WARN_ON(1); | |
75eff68e CM |
1745 | /* |
1746 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1747 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1748 | * bit, which is only set or cleared with irqs on |
1749 | */ | |
0ca1f7ce | 1750 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1752 | u64 len = state->end + 1 - state->start; |
70ddc553 | 1753 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1754 | |
9e0baf60 | 1755 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1756 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1757 | } else { |
1758 | spin_lock(&BTRFS_I(inode)->lock); | |
1759 | BTRFS_I(inode)->outstanding_extents++; | |
1760 | spin_unlock(&BTRFS_I(inode)->lock); | |
1761 | } | |
287a0ab9 | 1762 | |
6a3891c5 | 1763 | /* For sanity tests */ |
0b246afa | 1764 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1765 | return; |
1766 | ||
0b246afa JM |
1767 | __percpu_counter_add(&fs_info->delalloc_bytes, len, |
1768 | fs_info->delalloc_batch); | |
df0af1a5 | 1769 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1770 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1771 | if (*bits & EXTENT_DEFRAG) |
1772 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1773 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1774 | &BTRFS_I(inode)->runtime_flags)) |
1775 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1776 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1777 | } |
a7e3b975 FM |
1778 | |
1779 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1780 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1781 | spin_lock(&BTRFS_I(inode)->lock); | |
1782 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1783 | state->start; | |
1784 | spin_unlock(&BTRFS_I(inode)->lock); | |
1785 | } | |
291d673e CM |
1786 | } |
1787 | ||
d352ac68 CM |
1788 | /* |
1789 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1790 | */ | |
c6100a4b | 1791 | static void btrfs_clear_bit_hook(void *private_data, |
41074888 | 1792 | struct extent_state *state, |
9ee49a04 | 1793 | unsigned *bits) |
291d673e | 1794 | { |
c6100a4b | 1795 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
6fc0ef68 | 1796 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
47059d93 | 1797 | u64 len = state->end + 1 - state->start; |
823bb20a | 1798 | u32 num_extents = count_max_extents(len); |
47059d93 | 1799 | |
6fc0ef68 | 1800 | spin_lock(&inode->lock); |
47059d93 | 1801 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
6fc0ef68 NB |
1802 | inode->defrag_bytes -= len; |
1803 | spin_unlock(&inode->lock); | |
47059d93 | 1804 | |
75eff68e CM |
1805 | /* |
1806 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1807 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1808 | * bit, which is only set or cleared with irqs on |
1809 | */ | |
0ca1f7ce | 1810 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1811 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1812 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1813 | |
9e0baf60 | 1814 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1815 | *bits &= ~EXTENT_FIRST_DELALLOC; |
a315e68f | 1816 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { |
6fc0ef68 NB |
1817 | spin_lock(&inode->lock); |
1818 | inode->outstanding_extents -= num_extents; | |
1819 | spin_unlock(&inode->lock); | |
9e0baf60 | 1820 | } |
0ca1f7ce | 1821 | |
b6d08f06 JB |
1822 | /* |
1823 | * We don't reserve metadata space for space cache inodes so we | |
1824 | * don't need to call dellalloc_release_metadata if there is an | |
1825 | * error. | |
1826 | */ | |
a315e68f | 1827 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1828 | root != fs_info->tree_root) |
0ca1f7ce YZ |
1829 | btrfs_delalloc_release_metadata(inode, len); |
1830 | ||
6a3891c5 | 1831 | /* For sanity tests. */ |
0b246afa | 1832 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1833 | return; |
1834 | ||
a315e68f FM |
1835 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
1836 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1837 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
1838 | btrfs_free_reserved_data_space_noquota( |
1839 | &inode->vfs_inode, | |
51773bec | 1840 | state->start, len); |
9ed74f2d | 1841 | |
0b246afa JM |
1842 | __percpu_counter_add(&fs_info->delalloc_bytes, -len, |
1843 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
1844 | spin_lock(&inode->lock); |
1845 | inode->delalloc_bytes -= len; | |
1846 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 1847 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1848 | &inode->runtime_flags)) |
eb73c1b7 | 1849 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 1850 | spin_unlock(&inode->lock); |
291d673e | 1851 | } |
a7e3b975 FM |
1852 | |
1853 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1854 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1855 | spin_lock(&inode->lock); | |
1856 | ASSERT(inode->new_delalloc_bytes >= len); | |
1857 | inode->new_delalloc_bytes -= len; | |
1858 | spin_unlock(&inode->lock); | |
1859 | } | |
291d673e CM |
1860 | } |
1861 | ||
d352ac68 CM |
1862 | /* |
1863 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1864 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1865 | * |
1866 | * return 1 if page cannot be merged to bio | |
1867 | * return 0 if page can be merged to bio | |
1868 | * return error otherwise | |
d352ac68 | 1869 | */ |
81a75f67 | 1870 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1871 | size_t size, struct bio *bio, |
1872 | unsigned long bio_flags) | |
239b14b3 | 1873 | { |
0b246afa JM |
1874 | struct inode *inode = page->mapping->host; |
1875 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1876 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1877 | u64 length = 0; |
1878 | u64 map_length; | |
239b14b3 CM |
1879 | int ret; |
1880 | ||
771ed689 CM |
1881 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1882 | return 0; | |
1883 | ||
4f024f37 | 1884 | length = bio->bi_iter.bi_size; |
239b14b3 | 1885 | map_length = length; |
0b246afa JM |
1886 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1887 | NULL, 0); | |
6f034ece LB |
1888 | if (ret < 0) |
1889 | return ret; | |
d397712b | 1890 | if (map_length < length + size) |
239b14b3 | 1891 | return 1; |
3444a972 | 1892 | return 0; |
239b14b3 CM |
1893 | } |
1894 | ||
d352ac68 CM |
1895 | /* |
1896 | * in order to insert checksums into the metadata in large chunks, | |
1897 | * we wait until bio submission time. All the pages in the bio are | |
1898 | * checksummed and sums are attached onto the ordered extent record. | |
1899 | * | |
1900 | * At IO completion time the cums attached on the ordered extent record | |
1901 | * are inserted into the btree | |
1902 | */ | |
c6100a4b | 1903 | static int __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
81a75f67 | 1904 | int mirror_num, unsigned long bio_flags, |
eaf25d93 | 1905 | u64 bio_offset) |
065631f6 | 1906 | { |
c6100a4b | 1907 | struct inode *inode = private_data; |
065631f6 | 1908 | int ret = 0; |
e015640f | 1909 | |
2ff7e61e | 1910 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1911 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1912 | return 0; |
1913 | } | |
e015640f | 1914 | |
4a69a410 CM |
1915 | /* |
1916 | * in order to insert checksums into the metadata in large chunks, | |
1917 | * we wait until bio submission time. All the pages in the bio are | |
1918 | * checksummed and sums are attached onto the ordered extent record. | |
1919 | * | |
1920 | * At IO completion time the cums attached on the ordered extent record | |
1921 | * are inserted into the btree | |
1922 | */ | |
c6100a4b | 1923 | static int __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
eaf25d93 CM |
1924 | int mirror_num, unsigned long bio_flags, |
1925 | u64 bio_offset) | |
4a69a410 | 1926 | { |
c6100a4b | 1927 | struct inode *inode = private_data; |
2ff7e61e | 1928 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
61891923 SB |
1929 | int ret; |
1930 | ||
2ff7e61e | 1931 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
4246a0b6 CH |
1932 | if (ret) { |
1933 | bio->bi_error = ret; | |
1934 | bio_endio(bio); | |
1935 | } | |
61891923 | 1936 | return ret; |
44b8bd7e CM |
1937 | } |
1938 | ||
d352ac68 | 1939 | /* |
cad321ad CM |
1940 | * extent_io.c submission hook. This does the right thing for csum calculation |
1941 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1942 | */ |
c6100a4b JB |
1943 | static int btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
1944 | int mirror_num, unsigned long bio_flags, | |
1945 | u64 bio_offset) | |
44b8bd7e | 1946 | { |
c6100a4b | 1947 | struct inode *inode = private_data; |
0b246afa | 1948 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1949 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1950 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
44b8bd7e | 1951 | int ret = 0; |
19b9bdb0 | 1952 | int skip_sum; |
b812ce28 | 1953 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1954 | |
6cbff00f | 1955 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1956 | |
70ddc553 | 1957 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 1958 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1959 | |
37226b21 | 1960 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1961 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1962 | if (ret) |
61891923 | 1963 | goto out; |
5fd02043 | 1964 | |
d20f7043 | 1965 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1966 | ret = btrfs_submit_compressed_read(inode, bio, |
1967 | mirror_num, | |
1968 | bio_flags); | |
1969 | goto out; | |
c2db1073 | 1970 | } else if (!skip_sum) { |
2ff7e61e | 1971 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1972 | if (ret) |
61891923 | 1973 | goto out; |
c2db1073 | 1974 | } |
4d1b5fb4 | 1975 | goto mapit; |
b812ce28 | 1976 | } else if (async && !skip_sum) { |
17d217fe YZ |
1977 | /* csum items have already been cloned */ |
1978 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1979 | goto mapit; | |
19b9bdb0 | 1980 | /* we're doing a write, do the async checksumming */ |
c6100a4b JB |
1981 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
1982 | bio_offset, inode, | |
0b246afa JM |
1983 | __btrfs_submit_bio_start, |
1984 | __btrfs_submit_bio_done); | |
61891923 | 1985 | goto out; |
b812ce28 | 1986 | } else if (!skip_sum) { |
2ff7e61e | 1987 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
1988 | if (ret) |
1989 | goto out; | |
19b9bdb0 CM |
1990 | } |
1991 | ||
0b86a832 | 1992 | mapit: |
2ff7e61e | 1993 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
1994 | |
1995 | out: | |
4246a0b6 CH |
1996 | if (ret < 0) { |
1997 | bio->bi_error = ret; | |
1998 | bio_endio(bio); | |
1999 | } | |
61891923 | 2000 | return ret; |
065631f6 | 2001 | } |
6885f308 | 2002 | |
d352ac68 CM |
2003 | /* |
2004 | * given a list of ordered sums record them in the inode. This happens | |
2005 | * at IO completion time based on sums calculated at bio submission time. | |
2006 | */ | |
ba1da2f4 | 2007 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2008 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2009 | { |
e6dcd2dc CM |
2010 | struct btrfs_ordered_sum *sum; |
2011 | ||
c6e30871 | 2012 | list_for_each_entry(sum, list, list) { |
39847c4d | 2013 | trans->adding_csums = 1; |
d20f7043 CM |
2014 | btrfs_csum_file_blocks(trans, |
2015 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 2016 | trans->adding_csums = 0; |
e6dcd2dc CM |
2017 | } |
2018 | return 0; | |
2019 | } | |
2020 | ||
2ac55d41 | 2021 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2022 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2023 | { |
09cbfeaf | 2024 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2025 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2026 | cached_state); |
ea8c2819 CM |
2027 | } |
2028 | ||
d352ac68 | 2029 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2030 | struct btrfs_writepage_fixup { |
2031 | struct page *page; | |
2032 | struct btrfs_work work; | |
2033 | }; | |
2034 | ||
b2950863 | 2035 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2036 | { |
2037 | struct btrfs_writepage_fixup *fixup; | |
2038 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2039 | struct extent_state *cached_state = NULL; |
247e743c CM |
2040 | struct page *page; |
2041 | struct inode *inode; | |
2042 | u64 page_start; | |
2043 | u64 page_end; | |
87826df0 | 2044 | int ret; |
247e743c CM |
2045 | |
2046 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2047 | page = fixup->page; | |
4a096752 | 2048 | again: |
247e743c CM |
2049 | lock_page(page); |
2050 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2051 | ClearPageChecked(page); | |
2052 | goto out_page; | |
2053 | } | |
2054 | ||
2055 | inode = page->mapping->host; | |
2056 | page_start = page_offset(page); | |
09cbfeaf | 2057 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2058 | |
ff13db41 | 2059 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2060 | &cached_state); |
4a096752 CM |
2061 | |
2062 | /* already ordered? We're done */ | |
8b62b72b | 2063 | if (PagePrivate2(page)) |
247e743c | 2064 | goto out; |
4a096752 | 2065 | |
a776c6fa | 2066 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2067 | PAGE_SIZE); |
4a096752 | 2068 | if (ordered) { |
2ac55d41 JB |
2069 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2070 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2071 | unlock_page(page); |
2072 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2073 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2074 | goto again; |
2075 | } | |
247e743c | 2076 | |
7cf5b976 | 2077 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
09cbfeaf | 2078 | PAGE_SIZE); |
87826df0 JM |
2079 | if (ret) { |
2080 | mapping_set_error(page->mapping, ret); | |
2081 | end_extent_writepage(page, ret, page_start, page_end); | |
2082 | ClearPageChecked(page); | |
2083 | goto out; | |
2084 | } | |
2085 | ||
ba8b04c1 QW |
2086 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
2087 | 0); | |
247e743c | 2088 | ClearPageChecked(page); |
87826df0 | 2089 | set_page_dirty(page); |
247e743c | 2090 | out: |
2ac55d41 JB |
2091 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2092 | &cached_state, GFP_NOFS); | |
247e743c CM |
2093 | out_page: |
2094 | unlock_page(page); | |
09cbfeaf | 2095 | put_page(page); |
b897abec | 2096 | kfree(fixup); |
247e743c CM |
2097 | } |
2098 | ||
2099 | /* | |
2100 | * There are a few paths in the higher layers of the kernel that directly | |
2101 | * set the page dirty bit without asking the filesystem if it is a | |
2102 | * good idea. This causes problems because we want to make sure COW | |
2103 | * properly happens and the data=ordered rules are followed. | |
2104 | * | |
c8b97818 | 2105 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2106 | * hasn't been properly setup for IO. We kick off an async process |
2107 | * to fix it up. The async helper will wait for ordered extents, set | |
2108 | * the delalloc bit and make it safe to write the page. | |
2109 | */ | |
b2950863 | 2110 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2111 | { |
2112 | struct inode *inode = page->mapping->host; | |
0b246afa | 2113 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2114 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2115 | |
8b62b72b CM |
2116 | /* this page is properly in the ordered list */ |
2117 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2118 | return 0; |
2119 | ||
2120 | if (PageChecked(page)) | |
2121 | return -EAGAIN; | |
2122 | ||
2123 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2124 | if (!fixup) | |
2125 | return -EAGAIN; | |
f421950f | 2126 | |
247e743c | 2127 | SetPageChecked(page); |
09cbfeaf | 2128 | get_page(page); |
9e0af237 LB |
2129 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2130 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2131 | fixup->page = page; |
0b246afa | 2132 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2133 | return -EBUSY; |
247e743c CM |
2134 | } |
2135 | ||
d899e052 YZ |
2136 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2137 | struct inode *inode, u64 file_pos, | |
2138 | u64 disk_bytenr, u64 disk_num_bytes, | |
2139 | u64 num_bytes, u64 ram_bytes, | |
2140 | u8 compression, u8 encryption, | |
2141 | u16 other_encoding, int extent_type) | |
2142 | { | |
2143 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2144 | struct btrfs_file_extent_item *fi; | |
2145 | struct btrfs_path *path; | |
2146 | struct extent_buffer *leaf; | |
2147 | struct btrfs_key ins; | |
1acae57b | 2148 | int extent_inserted = 0; |
d899e052 YZ |
2149 | int ret; |
2150 | ||
2151 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2152 | if (!path) |
2153 | return -ENOMEM; | |
d899e052 | 2154 | |
a1ed835e CM |
2155 | /* |
2156 | * we may be replacing one extent in the tree with another. | |
2157 | * The new extent is pinned in the extent map, and we don't want | |
2158 | * to drop it from the cache until it is completely in the btree. | |
2159 | * | |
2160 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2161 | * the caller is expected to unpin it and allow it to be merged | |
2162 | * with the others. | |
2163 | */ | |
1acae57b FDBM |
2164 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2165 | file_pos + num_bytes, NULL, 0, | |
2166 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2167 | if (ret) |
2168 | goto out; | |
d899e052 | 2169 | |
1acae57b | 2170 | if (!extent_inserted) { |
4a0cc7ca | 2171 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2172 | ins.offset = file_pos; |
2173 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2174 | ||
2175 | path->leave_spinning = 1; | |
2176 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2177 | sizeof(*fi)); | |
2178 | if (ret) | |
2179 | goto out; | |
2180 | } | |
d899e052 YZ |
2181 | leaf = path->nodes[0]; |
2182 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2183 | struct btrfs_file_extent_item); | |
2184 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2185 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2186 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2187 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2188 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2189 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2190 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2191 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2192 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2193 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2194 | |
d899e052 | 2195 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2196 | btrfs_release_path(path); |
d899e052 YZ |
2197 | |
2198 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2199 | |
2200 | ins.objectid = disk_bytenr; | |
2201 | ins.offset = disk_num_bytes; | |
2202 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
2ff7e61e | 2203 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, |
f85b7379 | 2204 | btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins); |
297d750b | 2205 | /* |
5846a3c2 QW |
2206 | * Release the reserved range from inode dirty range map, as it is |
2207 | * already moved into delayed_ref_head | |
297d750b QW |
2208 | */ |
2209 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); | |
79787eaa | 2210 | out: |
d899e052 | 2211 | btrfs_free_path(path); |
b9473439 | 2212 | |
79787eaa | 2213 | return ret; |
d899e052 YZ |
2214 | } |
2215 | ||
38c227d8 LB |
2216 | /* snapshot-aware defrag */ |
2217 | struct sa_defrag_extent_backref { | |
2218 | struct rb_node node; | |
2219 | struct old_sa_defrag_extent *old; | |
2220 | u64 root_id; | |
2221 | u64 inum; | |
2222 | u64 file_pos; | |
2223 | u64 extent_offset; | |
2224 | u64 num_bytes; | |
2225 | u64 generation; | |
2226 | }; | |
2227 | ||
2228 | struct old_sa_defrag_extent { | |
2229 | struct list_head list; | |
2230 | struct new_sa_defrag_extent *new; | |
2231 | ||
2232 | u64 extent_offset; | |
2233 | u64 bytenr; | |
2234 | u64 offset; | |
2235 | u64 len; | |
2236 | int count; | |
2237 | }; | |
2238 | ||
2239 | struct new_sa_defrag_extent { | |
2240 | struct rb_root root; | |
2241 | struct list_head head; | |
2242 | struct btrfs_path *path; | |
2243 | struct inode *inode; | |
2244 | u64 file_pos; | |
2245 | u64 len; | |
2246 | u64 bytenr; | |
2247 | u64 disk_len; | |
2248 | u8 compress_type; | |
2249 | }; | |
2250 | ||
2251 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2252 | struct sa_defrag_extent_backref *b2) | |
2253 | { | |
2254 | if (b1->root_id < b2->root_id) | |
2255 | return -1; | |
2256 | else if (b1->root_id > b2->root_id) | |
2257 | return 1; | |
2258 | ||
2259 | if (b1->inum < b2->inum) | |
2260 | return -1; | |
2261 | else if (b1->inum > b2->inum) | |
2262 | return 1; | |
2263 | ||
2264 | if (b1->file_pos < b2->file_pos) | |
2265 | return -1; | |
2266 | else if (b1->file_pos > b2->file_pos) | |
2267 | return 1; | |
2268 | ||
2269 | /* | |
2270 | * [------------------------------] ===> (a range of space) | |
2271 | * |<--->| |<---->| =============> (fs/file tree A) | |
2272 | * |<---------------------------->| ===> (fs/file tree B) | |
2273 | * | |
2274 | * A range of space can refer to two file extents in one tree while | |
2275 | * refer to only one file extent in another tree. | |
2276 | * | |
2277 | * So we may process a disk offset more than one time(two extents in A) | |
2278 | * and locate at the same extent(one extent in B), then insert two same | |
2279 | * backrefs(both refer to the extent in B). | |
2280 | */ | |
2281 | return 0; | |
2282 | } | |
2283 | ||
2284 | static void backref_insert(struct rb_root *root, | |
2285 | struct sa_defrag_extent_backref *backref) | |
2286 | { | |
2287 | struct rb_node **p = &root->rb_node; | |
2288 | struct rb_node *parent = NULL; | |
2289 | struct sa_defrag_extent_backref *entry; | |
2290 | int ret; | |
2291 | ||
2292 | while (*p) { | |
2293 | parent = *p; | |
2294 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2295 | ||
2296 | ret = backref_comp(backref, entry); | |
2297 | if (ret < 0) | |
2298 | p = &(*p)->rb_left; | |
2299 | else | |
2300 | p = &(*p)->rb_right; | |
2301 | } | |
2302 | ||
2303 | rb_link_node(&backref->node, parent, p); | |
2304 | rb_insert_color(&backref->node, root); | |
2305 | } | |
2306 | ||
2307 | /* | |
2308 | * Note the backref might has changed, and in this case we just return 0. | |
2309 | */ | |
2310 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2311 | void *ctx) | |
2312 | { | |
2313 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2314 | struct old_sa_defrag_extent *old = ctx; |
2315 | struct new_sa_defrag_extent *new = old->new; | |
2316 | struct btrfs_path *path = new->path; | |
2317 | struct btrfs_key key; | |
2318 | struct btrfs_root *root; | |
2319 | struct sa_defrag_extent_backref *backref; | |
2320 | struct extent_buffer *leaf; | |
2321 | struct inode *inode = new->inode; | |
0b246afa | 2322 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2323 | int slot; |
2324 | int ret; | |
2325 | u64 extent_offset; | |
2326 | u64 num_bytes; | |
2327 | ||
2328 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2329 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2330 | return 0; |
2331 | ||
2332 | key.objectid = root_id; | |
2333 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2334 | key.offset = (u64)-1; | |
2335 | ||
38c227d8 LB |
2336 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2337 | if (IS_ERR(root)) { | |
2338 | if (PTR_ERR(root) == -ENOENT) | |
2339 | return 0; | |
2340 | WARN_ON(1); | |
ab8d0fc4 | 2341 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2342 | inum, offset, root_id); |
2343 | return PTR_ERR(root); | |
2344 | } | |
2345 | ||
2346 | key.objectid = inum; | |
2347 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2348 | if (offset > (u64)-1 << 32) | |
2349 | key.offset = 0; | |
2350 | else | |
2351 | key.offset = offset; | |
2352 | ||
2353 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2354 | if (WARN_ON(ret < 0)) |
38c227d8 | 2355 | return ret; |
50f1319c | 2356 | ret = 0; |
38c227d8 LB |
2357 | |
2358 | while (1) { | |
2359 | cond_resched(); | |
2360 | ||
2361 | leaf = path->nodes[0]; | |
2362 | slot = path->slots[0]; | |
2363 | ||
2364 | if (slot >= btrfs_header_nritems(leaf)) { | |
2365 | ret = btrfs_next_leaf(root, path); | |
2366 | if (ret < 0) { | |
2367 | goto out; | |
2368 | } else if (ret > 0) { | |
2369 | ret = 0; | |
2370 | goto out; | |
2371 | } | |
2372 | continue; | |
2373 | } | |
2374 | ||
2375 | path->slots[0]++; | |
2376 | ||
2377 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2378 | ||
2379 | if (key.objectid > inum) | |
2380 | goto out; | |
2381 | ||
2382 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2383 | continue; | |
2384 | ||
2385 | extent = btrfs_item_ptr(leaf, slot, | |
2386 | struct btrfs_file_extent_item); | |
2387 | ||
2388 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2389 | continue; | |
2390 | ||
e68afa49 LB |
2391 | /* |
2392 | * 'offset' refers to the exact key.offset, | |
2393 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2394 | * (key.offset - extent_offset). | |
2395 | */ | |
2396 | if (key.offset != offset) | |
38c227d8 LB |
2397 | continue; |
2398 | ||
e68afa49 | 2399 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2400 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2401 | |
38c227d8 LB |
2402 | if (extent_offset >= old->extent_offset + old->offset + |
2403 | old->len || extent_offset + num_bytes <= | |
2404 | old->extent_offset + old->offset) | |
2405 | continue; | |
38c227d8 LB |
2406 | break; |
2407 | } | |
2408 | ||
2409 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2410 | if (!backref) { | |
2411 | ret = -ENOENT; | |
2412 | goto out; | |
2413 | } | |
2414 | ||
2415 | backref->root_id = root_id; | |
2416 | backref->inum = inum; | |
e68afa49 | 2417 | backref->file_pos = offset; |
38c227d8 LB |
2418 | backref->num_bytes = num_bytes; |
2419 | backref->extent_offset = extent_offset; | |
2420 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2421 | backref->old = old; | |
2422 | backref_insert(&new->root, backref); | |
2423 | old->count++; | |
2424 | out: | |
2425 | btrfs_release_path(path); | |
2426 | WARN_ON(ret); | |
2427 | return ret; | |
2428 | } | |
2429 | ||
2430 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2431 | struct new_sa_defrag_extent *new) | |
2432 | { | |
0b246afa | 2433 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2434 | struct old_sa_defrag_extent *old, *tmp; |
2435 | int ret; | |
2436 | ||
2437 | new->path = path; | |
2438 | ||
2439 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2440 | ret = iterate_inodes_from_logical(old->bytenr + |
2441 | old->extent_offset, fs_info, | |
38c227d8 LB |
2442 | path, record_one_backref, |
2443 | old); | |
4724b106 JB |
2444 | if (ret < 0 && ret != -ENOENT) |
2445 | return false; | |
38c227d8 LB |
2446 | |
2447 | /* no backref to be processed for this extent */ | |
2448 | if (!old->count) { | |
2449 | list_del(&old->list); | |
2450 | kfree(old); | |
2451 | } | |
2452 | } | |
2453 | ||
2454 | if (list_empty(&new->head)) | |
2455 | return false; | |
2456 | ||
2457 | return true; | |
2458 | } | |
2459 | ||
2460 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2461 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2462 | struct new_sa_defrag_extent *new) |
38c227d8 | 2463 | { |
116e0024 | 2464 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2465 | return 0; |
2466 | ||
2467 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2468 | return 0; | |
2469 | ||
116e0024 LB |
2470 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2471 | return 0; | |
2472 | ||
2473 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2474 | btrfs_file_extent_other_encoding(leaf, fi)) |
2475 | return 0; | |
2476 | ||
2477 | return 1; | |
2478 | } | |
2479 | ||
2480 | /* | |
2481 | * Note the backref might has changed, and in this case we just return 0. | |
2482 | */ | |
2483 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2484 | struct sa_defrag_extent_backref *prev, | |
2485 | struct sa_defrag_extent_backref *backref) | |
2486 | { | |
2487 | struct btrfs_file_extent_item *extent; | |
2488 | struct btrfs_file_extent_item *item; | |
2489 | struct btrfs_ordered_extent *ordered; | |
2490 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2491 | struct btrfs_root *root; |
2492 | struct btrfs_key key; | |
2493 | struct extent_buffer *leaf; | |
2494 | struct old_sa_defrag_extent *old = backref->old; | |
2495 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2496 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2497 | struct inode *inode; |
2498 | struct extent_state *cached = NULL; | |
2499 | int ret = 0; | |
2500 | u64 start; | |
2501 | u64 len; | |
2502 | u64 lock_start; | |
2503 | u64 lock_end; | |
2504 | bool merge = false; | |
2505 | int index; | |
2506 | ||
2507 | if (prev && prev->root_id == backref->root_id && | |
2508 | prev->inum == backref->inum && | |
2509 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2510 | merge = true; | |
2511 | ||
2512 | /* step 1: get root */ | |
2513 | key.objectid = backref->root_id; | |
2514 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2515 | key.offset = (u64)-1; | |
2516 | ||
38c227d8 LB |
2517 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2518 | ||
2519 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2520 | if (IS_ERR(root)) { | |
2521 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2522 | if (PTR_ERR(root) == -ENOENT) | |
2523 | return 0; | |
2524 | return PTR_ERR(root); | |
2525 | } | |
38c227d8 | 2526 | |
bcbba5e6 WS |
2527 | if (btrfs_root_readonly(root)) { |
2528 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2529 | return 0; | |
2530 | } | |
2531 | ||
38c227d8 LB |
2532 | /* step 2: get inode */ |
2533 | key.objectid = backref->inum; | |
2534 | key.type = BTRFS_INODE_ITEM_KEY; | |
2535 | key.offset = 0; | |
2536 | ||
2537 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2538 | if (IS_ERR(inode)) { | |
2539 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2540 | return 0; | |
2541 | } | |
2542 | ||
2543 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2544 | ||
2545 | /* step 3: relink backref */ | |
2546 | lock_start = backref->file_pos; | |
2547 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2548 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2549 | &cached); |
38c227d8 LB |
2550 | |
2551 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2552 | if (ordered) { | |
2553 | btrfs_put_ordered_extent(ordered); | |
2554 | goto out_unlock; | |
2555 | } | |
2556 | ||
2557 | trans = btrfs_join_transaction(root); | |
2558 | if (IS_ERR(trans)) { | |
2559 | ret = PTR_ERR(trans); | |
2560 | goto out_unlock; | |
2561 | } | |
2562 | ||
2563 | key.objectid = backref->inum; | |
2564 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2565 | key.offset = backref->file_pos; | |
2566 | ||
2567 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2568 | if (ret < 0) { | |
2569 | goto out_free_path; | |
2570 | } else if (ret > 0) { | |
2571 | ret = 0; | |
2572 | goto out_free_path; | |
2573 | } | |
2574 | ||
2575 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2576 | struct btrfs_file_extent_item); | |
2577 | ||
2578 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2579 | backref->generation) | |
2580 | goto out_free_path; | |
2581 | ||
2582 | btrfs_release_path(path); | |
2583 | ||
2584 | start = backref->file_pos; | |
2585 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2586 | start += old->extent_offset + old->offset - | |
2587 | backref->extent_offset; | |
2588 | ||
2589 | len = min(backref->extent_offset + backref->num_bytes, | |
2590 | old->extent_offset + old->offset + old->len); | |
2591 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2592 | ||
2593 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2594 | start + len, 1); | |
2595 | if (ret) | |
2596 | goto out_free_path; | |
2597 | again: | |
4a0cc7ca | 2598 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2599 | key.type = BTRFS_EXTENT_DATA_KEY; |
2600 | key.offset = start; | |
2601 | ||
a09a0a70 | 2602 | path->leave_spinning = 1; |
38c227d8 LB |
2603 | if (merge) { |
2604 | struct btrfs_file_extent_item *fi; | |
2605 | u64 extent_len; | |
2606 | struct btrfs_key found_key; | |
2607 | ||
3c9665df | 2608 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2609 | if (ret < 0) |
2610 | goto out_free_path; | |
2611 | ||
2612 | path->slots[0]--; | |
2613 | leaf = path->nodes[0]; | |
2614 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2615 | ||
2616 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2617 | struct btrfs_file_extent_item); | |
2618 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2619 | ||
116e0024 LB |
2620 | if (extent_len + found_key.offset == start && |
2621 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2622 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2623 | extent_len + len); | |
2624 | btrfs_mark_buffer_dirty(leaf); | |
2625 | inode_add_bytes(inode, len); | |
2626 | ||
2627 | ret = 1; | |
2628 | goto out_free_path; | |
2629 | } else { | |
2630 | merge = false; | |
2631 | btrfs_release_path(path); | |
2632 | goto again; | |
2633 | } | |
2634 | } | |
2635 | ||
2636 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2637 | sizeof(*extent)); | |
2638 | if (ret) { | |
66642832 | 2639 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2640 | goto out_free_path; |
2641 | } | |
2642 | ||
2643 | leaf = path->nodes[0]; | |
2644 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2645 | struct btrfs_file_extent_item); | |
2646 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2647 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2648 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2649 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2650 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2651 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2652 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2653 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2654 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2655 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2656 | ||
2657 | btrfs_mark_buffer_dirty(leaf); | |
2658 | inode_add_bytes(inode, len); | |
a09a0a70 | 2659 | btrfs_release_path(path); |
38c227d8 | 2660 | |
2ff7e61e | 2661 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
38c227d8 LB |
2662 | new->disk_len, 0, |
2663 | backref->root_id, backref->inum, | |
b06c4bf5 | 2664 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2665 | if (ret) { |
66642832 | 2666 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2667 | goto out_free_path; |
2668 | } | |
2669 | ||
2670 | ret = 1; | |
2671 | out_free_path: | |
2672 | btrfs_release_path(path); | |
a09a0a70 | 2673 | path->leave_spinning = 0; |
3a45bb20 | 2674 | btrfs_end_transaction(trans); |
38c227d8 LB |
2675 | out_unlock: |
2676 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2677 | &cached, GFP_NOFS); | |
2678 | iput(inode); | |
2679 | return ret; | |
2680 | } | |
2681 | ||
6f519564 LB |
2682 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2683 | { | |
2684 | struct old_sa_defrag_extent *old, *tmp; | |
2685 | ||
2686 | if (!new) | |
2687 | return; | |
2688 | ||
2689 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2690 | kfree(old); |
2691 | } | |
2692 | kfree(new); | |
2693 | } | |
2694 | ||
38c227d8 LB |
2695 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2696 | { | |
0b246afa | 2697 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2698 | struct btrfs_path *path; |
38c227d8 LB |
2699 | struct sa_defrag_extent_backref *backref; |
2700 | struct sa_defrag_extent_backref *prev = NULL; | |
2701 | struct inode *inode; | |
2702 | struct btrfs_root *root; | |
2703 | struct rb_node *node; | |
2704 | int ret; | |
2705 | ||
2706 | inode = new->inode; | |
2707 | root = BTRFS_I(inode)->root; | |
2708 | ||
2709 | path = btrfs_alloc_path(); | |
2710 | if (!path) | |
2711 | return; | |
2712 | ||
2713 | if (!record_extent_backrefs(path, new)) { | |
2714 | btrfs_free_path(path); | |
2715 | goto out; | |
2716 | } | |
2717 | btrfs_release_path(path); | |
2718 | ||
2719 | while (1) { | |
2720 | node = rb_first(&new->root); | |
2721 | if (!node) | |
2722 | break; | |
2723 | rb_erase(node, &new->root); | |
2724 | ||
2725 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2726 | ||
2727 | ret = relink_extent_backref(path, prev, backref); | |
2728 | WARN_ON(ret < 0); | |
2729 | ||
2730 | kfree(prev); | |
2731 | ||
2732 | if (ret == 1) | |
2733 | prev = backref; | |
2734 | else | |
2735 | prev = NULL; | |
2736 | cond_resched(); | |
2737 | } | |
2738 | kfree(prev); | |
2739 | ||
2740 | btrfs_free_path(path); | |
38c227d8 | 2741 | out: |
6f519564 LB |
2742 | free_sa_defrag_extent(new); |
2743 | ||
0b246afa JM |
2744 | atomic_dec(&fs_info->defrag_running); |
2745 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2746 | } |
2747 | ||
2748 | static struct new_sa_defrag_extent * | |
2749 | record_old_file_extents(struct inode *inode, | |
2750 | struct btrfs_ordered_extent *ordered) | |
2751 | { | |
0b246afa | 2752 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2753 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2754 | struct btrfs_path *path; | |
2755 | struct btrfs_key key; | |
6f519564 | 2756 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2757 | struct new_sa_defrag_extent *new; |
2758 | int ret; | |
2759 | ||
2760 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2761 | if (!new) | |
2762 | return NULL; | |
2763 | ||
2764 | new->inode = inode; | |
2765 | new->file_pos = ordered->file_offset; | |
2766 | new->len = ordered->len; | |
2767 | new->bytenr = ordered->start; | |
2768 | new->disk_len = ordered->disk_len; | |
2769 | new->compress_type = ordered->compress_type; | |
2770 | new->root = RB_ROOT; | |
2771 | INIT_LIST_HEAD(&new->head); | |
2772 | ||
2773 | path = btrfs_alloc_path(); | |
2774 | if (!path) | |
2775 | goto out_kfree; | |
2776 | ||
4a0cc7ca | 2777 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2778 | key.type = BTRFS_EXTENT_DATA_KEY; |
2779 | key.offset = new->file_pos; | |
2780 | ||
2781 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2782 | if (ret < 0) | |
2783 | goto out_free_path; | |
2784 | if (ret > 0 && path->slots[0] > 0) | |
2785 | path->slots[0]--; | |
2786 | ||
2787 | /* find out all the old extents for the file range */ | |
2788 | while (1) { | |
2789 | struct btrfs_file_extent_item *extent; | |
2790 | struct extent_buffer *l; | |
2791 | int slot; | |
2792 | u64 num_bytes; | |
2793 | u64 offset; | |
2794 | u64 end; | |
2795 | u64 disk_bytenr; | |
2796 | u64 extent_offset; | |
2797 | ||
2798 | l = path->nodes[0]; | |
2799 | slot = path->slots[0]; | |
2800 | ||
2801 | if (slot >= btrfs_header_nritems(l)) { | |
2802 | ret = btrfs_next_leaf(root, path); | |
2803 | if (ret < 0) | |
6f519564 | 2804 | goto out_free_path; |
38c227d8 LB |
2805 | else if (ret > 0) |
2806 | break; | |
2807 | continue; | |
2808 | } | |
2809 | ||
2810 | btrfs_item_key_to_cpu(l, &key, slot); | |
2811 | ||
4a0cc7ca | 2812 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2813 | break; |
2814 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2815 | break; | |
2816 | if (key.offset >= new->file_pos + new->len) | |
2817 | break; | |
2818 | ||
2819 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2820 | ||
2821 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2822 | if (key.offset + num_bytes < new->file_pos) | |
2823 | goto next; | |
2824 | ||
2825 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2826 | if (!disk_bytenr) | |
2827 | goto next; | |
2828 | ||
2829 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2830 | ||
2831 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2832 | if (!old) | |
6f519564 | 2833 | goto out_free_path; |
38c227d8 LB |
2834 | |
2835 | offset = max(new->file_pos, key.offset); | |
2836 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2837 | ||
2838 | old->bytenr = disk_bytenr; | |
2839 | old->extent_offset = extent_offset; | |
2840 | old->offset = offset - key.offset; | |
2841 | old->len = end - offset; | |
2842 | old->new = new; | |
2843 | old->count = 0; | |
2844 | list_add_tail(&old->list, &new->head); | |
2845 | next: | |
2846 | path->slots[0]++; | |
2847 | cond_resched(); | |
2848 | } | |
2849 | ||
2850 | btrfs_free_path(path); | |
0b246afa | 2851 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2852 | |
2853 | return new; | |
2854 | ||
38c227d8 LB |
2855 | out_free_path: |
2856 | btrfs_free_path(path); | |
2857 | out_kfree: | |
6f519564 | 2858 | free_sa_defrag_extent(new); |
38c227d8 LB |
2859 | return NULL; |
2860 | } | |
2861 | ||
2ff7e61e | 2862 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2863 | u64 start, u64 len) |
2864 | { | |
2865 | struct btrfs_block_group_cache *cache; | |
2866 | ||
0b246afa | 2867 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2868 | ASSERT(cache); |
2869 | ||
2870 | spin_lock(&cache->lock); | |
2871 | cache->delalloc_bytes -= len; | |
2872 | spin_unlock(&cache->lock); | |
2873 | ||
2874 | btrfs_put_block_group(cache); | |
2875 | } | |
2876 | ||
d352ac68 CM |
2877 | /* as ordered data IO finishes, this gets called so we can finish |
2878 | * an ordered extent if the range of bytes in the file it covers are | |
2879 | * fully written. | |
2880 | */ | |
5fd02043 | 2881 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2882 | { |
5fd02043 | 2883 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2884 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2885 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2886 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2887 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2888 | struct extent_state *cached_state = NULL; |
38c227d8 | 2889 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2890 | int compress_type = 0; |
77cef2ec JB |
2891 | int ret = 0; |
2892 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2893 | bool nolock; |
77cef2ec | 2894 | bool truncated = false; |
a7e3b975 FM |
2895 | bool range_locked = false; |
2896 | bool clear_new_delalloc_bytes = false; | |
2897 | ||
2898 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2899 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2900 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2901 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2902 | |
70ddc553 | 2903 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2904 | |
5fd02043 JB |
2905 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2906 | ret = -EIO; | |
2907 | goto out; | |
2908 | } | |
2909 | ||
7ab7956e NB |
2910 | btrfs_free_io_failure_record(BTRFS_I(inode), |
2911 | ordered_extent->file_offset, | |
2912 | ordered_extent->file_offset + | |
2913 | ordered_extent->len - 1); | |
f612496b | 2914 | |
77cef2ec JB |
2915 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2916 | truncated = true; | |
2917 | logical_len = ordered_extent->truncated_len; | |
2918 | /* Truncated the entire extent, don't bother adding */ | |
2919 | if (!logical_len) | |
2920 | goto out; | |
2921 | } | |
2922 | ||
c2167754 | 2923 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2924 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2925 | |
2926 | /* | |
2927 | * For mwrite(mmap + memset to write) case, we still reserve | |
2928 | * space for NOCOW range. | |
2929 | * As NOCOW won't cause a new delayed ref, just free the space | |
2930 | */ | |
2931 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, | |
2932 | ordered_extent->len); | |
6c760c07 JB |
2933 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2934 | if (nolock) | |
2935 | trans = btrfs_join_transaction_nolock(root); | |
2936 | else | |
2937 | trans = btrfs_join_transaction(root); | |
2938 | if (IS_ERR(trans)) { | |
2939 | ret = PTR_ERR(trans); | |
2940 | trans = NULL; | |
2941 | goto out; | |
c2167754 | 2942 | } |
0b246afa | 2943 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
6c760c07 JB |
2944 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2945 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2946 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2947 | goto out; |
2948 | } | |
e6dcd2dc | 2949 | |
a7e3b975 | 2950 | range_locked = true; |
2ac55d41 JB |
2951 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2952 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2953 | &cached_state); |
e6dcd2dc | 2954 | |
38c227d8 LB |
2955 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2956 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 2957 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
2958 | if (ret) { |
2959 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2960 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2961 | /* the inode is shared */ |
2962 | new = record_old_file_extents(inode, ordered_extent); | |
2963 | ||
2964 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2965 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2966 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2967 | } | |
2968 | ||
0cb59c99 | 2969 | if (nolock) |
7a7eaa40 | 2970 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2971 | else |
7a7eaa40 | 2972 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2973 | if (IS_ERR(trans)) { |
2974 | ret = PTR_ERR(trans); | |
2975 | trans = NULL; | |
a7e3b975 | 2976 | goto out; |
79787eaa | 2977 | } |
a79b7d4b | 2978 | |
0b246afa | 2979 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
c2167754 | 2980 | |
c8b97818 | 2981 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2982 | compress_type = ordered_extent->compress_type; |
d899e052 | 2983 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2984 | BUG_ON(compress_type); |
7a6d7067 | 2985 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2986 | ordered_extent->file_offset, |
2987 | ordered_extent->file_offset + | |
77cef2ec | 2988 | logical_len); |
d899e052 | 2989 | } else { |
0b246afa | 2990 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
2991 | ret = insert_reserved_file_extent(trans, inode, |
2992 | ordered_extent->file_offset, | |
2993 | ordered_extent->start, | |
2994 | ordered_extent->disk_len, | |
77cef2ec | 2995 | logical_len, logical_len, |
261507a0 | 2996 | compress_type, 0, 0, |
d899e052 | 2997 | BTRFS_FILE_EXTENT_REG); |
e570fd27 | 2998 | if (!ret) |
2ff7e61e | 2999 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3000 | ordered_extent->start, |
3001 | ordered_extent->disk_len); | |
d899e052 | 3002 | } |
5dc562c5 JB |
3003 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3004 | ordered_extent->file_offset, ordered_extent->len, | |
3005 | trans->transid); | |
79787eaa | 3006 | if (ret < 0) { |
66642832 | 3007 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3008 | goto out; |
79787eaa | 3009 | } |
2ac55d41 | 3010 | |
df9f628e | 3011 | add_pending_csums(trans, inode, &ordered_extent->list); |
e6dcd2dc | 3012 | |
6c760c07 JB |
3013 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3014 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3015 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3016 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3017 | goto out; |
1ef30be1 JB |
3018 | } |
3019 | ret = 0; | |
c2167754 | 3020 | out: |
a7e3b975 FM |
3021 | if (range_locked || clear_new_delalloc_bytes) { |
3022 | unsigned int clear_bits = 0; | |
3023 | ||
3024 | if (range_locked) | |
3025 | clear_bits |= EXTENT_LOCKED; | |
3026 | if (clear_new_delalloc_bytes) | |
3027 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3028 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3029 | ordered_extent->file_offset, | |
3030 | ordered_extent->file_offset + | |
3031 | ordered_extent->len - 1, | |
3032 | clear_bits, | |
3033 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
3034 | 0, &cached_state, GFP_NOFS); | |
3035 | } | |
3036 | ||
0b246afa | 3037 | if (root != fs_info->tree_root) |
691fa059 NB |
3038 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
3039 | ordered_extent->len); | |
a698d075 | 3040 | if (trans) |
3a45bb20 | 3041 | btrfs_end_transaction(trans); |
0cb59c99 | 3042 | |
77cef2ec JB |
3043 | if (ret || truncated) { |
3044 | u64 start, end; | |
3045 | ||
3046 | if (truncated) | |
3047 | start = ordered_extent->file_offset + logical_len; | |
3048 | else | |
3049 | start = ordered_extent->file_offset; | |
3050 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3051 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3052 | ||
3053 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3054 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3055 | |
0bec9ef5 JB |
3056 | /* |
3057 | * If the ordered extent had an IOERR or something else went | |
3058 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3059 | * back to the allocator. We only free the extent in the |
3060 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3061 | */ |
77cef2ec JB |
3062 | if ((ret || !logical_len) && |
3063 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 | 3064 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3065 | btrfs_free_reserved_extent(fs_info, |
3066 | ordered_extent->start, | |
e570fd27 | 3067 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3068 | } |
3069 | ||
3070 | ||
5fd02043 | 3071 | /* |
8bad3c02 LB |
3072 | * This needs to be done to make sure anybody waiting knows we are done |
3073 | * updating everything for this ordered extent. | |
5fd02043 JB |
3074 | */ |
3075 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3076 | ||
38c227d8 | 3077 | /* for snapshot-aware defrag */ |
6f519564 LB |
3078 | if (new) { |
3079 | if (ret) { | |
3080 | free_sa_defrag_extent(new); | |
0b246afa | 3081 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3082 | } else { |
3083 | relink_file_extents(new); | |
3084 | } | |
3085 | } | |
38c227d8 | 3086 | |
e6dcd2dc CM |
3087 | /* once for us */ |
3088 | btrfs_put_ordered_extent(ordered_extent); | |
3089 | /* once for the tree */ | |
3090 | btrfs_put_ordered_extent(ordered_extent); | |
3091 | ||
5fd02043 JB |
3092 | return ret; |
3093 | } | |
3094 | ||
3095 | static void finish_ordered_fn(struct btrfs_work *work) | |
3096 | { | |
3097 | struct btrfs_ordered_extent *ordered_extent; | |
3098 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3099 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3100 | } |
3101 | ||
c3988d63 | 3102 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3103 | struct extent_state *state, int uptodate) |
3104 | { | |
5fd02043 | 3105 | struct inode *inode = page->mapping->host; |
0b246afa | 3106 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3107 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3108 | struct btrfs_workqueue *wq; |
3109 | btrfs_work_func_t func; | |
5fd02043 | 3110 | |
1abe9b8a | 3111 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3112 | ||
8b62b72b | 3113 | ClearPagePrivate2(page); |
5fd02043 JB |
3114 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3115 | end - start + 1, uptodate)) | |
c3988d63 | 3116 | return; |
5fd02043 | 3117 | |
70ddc553 | 3118 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
0b246afa | 3119 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3120 | func = btrfs_freespace_write_helper; |
3121 | } else { | |
0b246afa | 3122 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3123 | func = btrfs_endio_write_helper; |
3124 | } | |
5fd02043 | 3125 | |
9e0af237 LB |
3126 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3127 | NULL); | |
3128 | btrfs_queue_work(wq, &ordered_extent->work); | |
211f90e6 CM |
3129 | } |
3130 | ||
dc380aea MX |
3131 | static int __readpage_endio_check(struct inode *inode, |
3132 | struct btrfs_io_bio *io_bio, | |
3133 | int icsum, struct page *page, | |
3134 | int pgoff, u64 start, size_t len) | |
3135 | { | |
3136 | char *kaddr; | |
3137 | u32 csum_expected; | |
3138 | u32 csum = ~(u32)0; | |
dc380aea MX |
3139 | |
3140 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3141 | ||
3142 | kaddr = kmap_atomic(page); | |
3143 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3144 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3145 | if (csum != csum_expected) |
3146 | goto zeroit; | |
3147 | ||
3148 | kunmap_atomic(kaddr); | |
3149 | return 0; | |
3150 | zeroit: | |
0970a22e | 3151 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
6f6b643e | 3152 | io_bio->mirror_num); |
dc380aea MX |
3153 | memset(kaddr + pgoff, 1, len); |
3154 | flush_dcache_page(page); | |
3155 | kunmap_atomic(kaddr); | |
3156 | if (csum_expected == 0) | |
3157 | return 0; | |
3158 | return -EIO; | |
3159 | } | |
3160 | ||
d352ac68 CM |
3161 | /* |
3162 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3163 | * if there's a match, we allow the bio to finish. If not, the code in |
3164 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3165 | */ |
facc8a22 MX |
3166 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3167 | u64 phy_offset, struct page *page, | |
3168 | u64 start, u64 end, int mirror) | |
07157aac | 3169 | { |
4eee4fa4 | 3170 | size_t offset = start - page_offset(page); |
07157aac | 3171 | struct inode *inode = page->mapping->host; |
d1310b2e | 3172 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3173 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3174 | |
d20f7043 CM |
3175 | if (PageChecked(page)) { |
3176 | ClearPageChecked(page); | |
dc380aea | 3177 | return 0; |
d20f7043 | 3178 | } |
6cbff00f CH |
3179 | |
3180 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3181 | return 0; |
17d217fe YZ |
3182 | |
3183 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3184 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3185 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3186 | return 0; |
17d217fe | 3187 | } |
d20f7043 | 3188 | |
facc8a22 | 3189 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3190 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3191 | start, (size_t)(end - start + 1)); | |
07157aac | 3192 | } |
b888db2b | 3193 | |
24bbcf04 YZ |
3194 | void btrfs_add_delayed_iput(struct inode *inode) |
3195 | { | |
0b246afa | 3196 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3197 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3198 | |
3199 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3200 | return; | |
3201 | ||
24bbcf04 | 3202 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3203 | if (binode->delayed_iput_count == 0) { |
3204 | ASSERT(list_empty(&binode->delayed_iput)); | |
3205 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3206 | } else { | |
3207 | binode->delayed_iput_count++; | |
3208 | } | |
24bbcf04 YZ |
3209 | spin_unlock(&fs_info->delayed_iput_lock); |
3210 | } | |
3211 | ||
2ff7e61e | 3212 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3213 | { |
24bbcf04 | 3214 | |
24bbcf04 | 3215 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3216 | while (!list_empty(&fs_info->delayed_iputs)) { |
3217 | struct btrfs_inode *inode; | |
3218 | ||
3219 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3220 | struct btrfs_inode, delayed_iput); | |
3221 | if (inode->delayed_iput_count) { | |
3222 | inode->delayed_iput_count--; | |
3223 | list_move_tail(&inode->delayed_iput, | |
3224 | &fs_info->delayed_iputs); | |
3225 | } else { | |
3226 | list_del_init(&inode->delayed_iput); | |
3227 | } | |
3228 | spin_unlock(&fs_info->delayed_iput_lock); | |
3229 | iput(&inode->vfs_inode); | |
3230 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3231 | } |
8089fe62 | 3232 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3233 | } |
3234 | ||
d68fc57b | 3235 | /* |
42b2aa86 | 3236 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3237 | * files in the subvolume, it removes orphan item and frees block_rsv |
3238 | * structure. | |
3239 | */ | |
3240 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3241 | struct btrfs_root *root) | |
3242 | { | |
0b246afa | 3243 | struct btrfs_fs_info *fs_info = root->fs_info; |
90290e19 | 3244 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3245 | int ret; |
3246 | ||
8a35d95f | 3247 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3248 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3249 | return; | |
3250 | ||
90290e19 | 3251 | spin_lock(&root->orphan_lock); |
8a35d95f | 3252 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3253 | spin_unlock(&root->orphan_lock); |
3254 | return; | |
3255 | } | |
3256 | ||
3257 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3258 | spin_unlock(&root->orphan_lock); | |
3259 | return; | |
3260 | } | |
3261 | ||
3262 | block_rsv = root->orphan_block_rsv; | |
3263 | root->orphan_block_rsv = NULL; | |
3264 | spin_unlock(&root->orphan_lock); | |
3265 | ||
27cdeb70 | 3266 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b | 3267 | btrfs_root_refs(&root->root_item) > 0) { |
0b246afa | 3268 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3269 | root->root_key.objectid); |
4ef31a45 | 3270 | if (ret) |
66642832 | 3271 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3272 | else |
27cdeb70 MX |
3273 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3274 | &root->state); | |
d68fc57b YZ |
3275 | } |
3276 | ||
90290e19 JB |
3277 | if (block_rsv) { |
3278 | WARN_ON(block_rsv->size > 0); | |
2ff7e61e | 3279 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b YZ |
3280 | } |
3281 | } | |
3282 | ||
7b128766 JB |
3283 | /* |
3284 | * This creates an orphan entry for the given inode in case something goes | |
3285 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3286 | * |
3287 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3288 | * this function. | |
7b128766 | 3289 | */ |
73f2e545 NB |
3290 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
3291 | struct btrfs_inode *inode) | |
7b128766 | 3292 | { |
73f2e545 NB |
3293 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
3294 | struct btrfs_root *root = inode->root; | |
d68fc57b YZ |
3295 | struct btrfs_block_rsv *block_rsv = NULL; |
3296 | int reserve = 0; | |
3297 | int insert = 0; | |
3298 | int ret; | |
7b128766 | 3299 | |
d68fc57b | 3300 | if (!root->orphan_block_rsv) { |
2ff7e61e JM |
3301 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
3302 | BTRFS_BLOCK_RSV_TEMP); | |
b532402e TI |
3303 | if (!block_rsv) |
3304 | return -ENOMEM; | |
d68fc57b | 3305 | } |
7b128766 | 3306 | |
d68fc57b YZ |
3307 | spin_lock(&root->orphan_lock); |
3308 | if (!root->orphan_block_rsv) { | |
3309 | root->orphan_block_rsv = block_rsv; | |
3310 | } else if (block_rsv) { | |
2ff7e61e | 3311 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b | 3312 | block_rsv = NULL; |
7b128766 | 3313 | } |
7b128766 | 3314 | |
8a35d95f | 3315 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3316 | &inode->runtime_flags)) { |
d68fc57b YZ |
3317 | #if 0 |
3318 | /* | |
3319 | * For proper ENOSPC handling, we should do orphan | |
3320 | * cleanup when mounting. But this introduces backward | |
3321 | * compatibility issue. | |
3322 | */ | |
3323 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3324 | insert = 2; | |
3325 | else | |
3326 | insert = 1; | |
3327 | #endif | |
3328 | insert = 1; | |
321f0e70 | 3329 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3330 | } |
3331 | ||
72ac3c0d | 3332 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
73f2e545 | 3333 | &inode->runtime_flags)) |
d68fc57b | 3334 | reserve = 1; |
d68fc57b | 3335 | spin_unlock(&root->orphan_lock); |
7b128766 | 3336 | |
d68fc57b YZ |
3337 | /* grab metadata reservation from transaction handle */ |
3338 | if (reserve) { | |
3339 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3340 | ASSERT(!ret); |
3341 | if (ret) { | |
3342 | atomic_dec(&root->orphan_inodes); | |
3343 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3344 | &inode->runtime_flags); |
3b6571c1 JB |
3345 | if (insert) |
3346 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
73f2e545 | 3347 | &inode->runtime_flags); |
3b6571c1 JB |
3348 | return ret; |
3349 | } | |
d68fc57b | 3350 | } |
7b128766 | 3351 | |
d68fc57b YZ |
3352 | /* insert an orphan item to track this unlinked/truncated file */ |
3353 | if (insert >= 1) { | |
73f2e545 | 3354 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3355 | if (ret) { |
703c88e0 | 3356 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3357 | if (reserve) { |
3358 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3359 | &inode->runtime_flags); |
4ef31a45 JB |
3360 | btrfs_orphan_release_metadata(inode); |
3361 | } | |
3362 | if (ret != -EEXIST) { | |
e8e7cff6 | 3363 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3364 | &inode->runtime_flags); |
66642832 | 3365 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3366 | return ret; |
3367 | } | |
79787eaa JM |
3368 | } |
3369 | ret = 0; | |
d68fc57b YZ |
3370 | } |
3371 | ||
3372 | /* insert an orphan item to track subvolume contains orphan files */ | |
3373 | if (insert >= 2) { | |
0b246afa | 3374 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3375 | root->root_key.objectid); |
79787eaa | 3376 | if (ret && ret != -EEXIST) { |
66642832 | 3377 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3378 | return ret; |
3379 | } | |
d68fc57b YZ |
3380 | } |
3381 | return 0; | |
7b128766 JB |
3382 | } |
3383 | ||
3384 | /* | |
3385 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3386 | * item for this particular inode. | |
3387 | */ | |
48a3b636 | 3388 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3389 | struct btrfs_inode *inode) |
7b128766 | 3390 | { |
3d6ae7bb | 3391 | struct btrfs_root *root = inode->root; |
d68fc57b YZ |
3392 | int delete_item = 0; |
3393 | int release_rsv = 0; | |
7b128766 JB |
3394 | int ret = 0; |
3395 | ||
d68fc57b | 3396 | spin_lock(&root->orphan_lock); |
8a35d95f | 3397 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3d6ae7bb | 3398 | &inode->runtime_flags)) |
d68fc57b | 3399 | delete_item = 1; |
7b128766 | 3400 | |
72ac3c0d | 3401 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3d6ae7bb | 3402 | &inode->runtime_flags)) |
d68fc57b | 3403 | release_rsv = 1; |
d68fc57b | 3404 | spin_unlock(&root->orphan_lock); |
7b128766 | 3405 | |
703c88e0 | 3406 | if (delete_item) { |
8a35d95f | 3407 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3408 | if (trans) |
3409 | ret = btrfs_del_orphan_item(trans, root, | |
3d6ae7bb | 3410 | btrfs_ino(inode)); |
8a35d95f | 3411 | } |
7b128766 | 3412 | |
703c88e0 FDBM |
3413 | if (release_rsv) |
3414 | btrfs_orphan_release_metadata(inode); | |
3415 | ||
4ef31a45 | 3416 | return ret; |
7b128766 JB |
3417 | } |
3418 | ||
3419 | /* | |
3420 | * this cleans up any orphans that may be left on the list from the last use | |
3421 | * of this root. | |
3422 | */ | |
66b4ffd1 | 3423 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3424 | { |
0b246afa | 3425 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3426 | struct btrfs_path *path; |
3427 | struct extent_buffer *leaf; | |
7b128766 JB |
3428 | struct btrfs_key key, found_key; |
3429 | struct btrfs_trans_handle *trans; | |
3430 | struct inode *inode; | |
8f6d7f4f | 3431 | u64 last_objectid = 0; |
7b128766 JB |
3432 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3433 | ||
d68fc57b | 3434 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3435 | return 0; |
c71bf099 YZ |
3436 | |
3437 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3438 | if (!path) { |
3439 | ret = -ENOMEM; | |
3440 | goto out; | |
3441 | } | |
e4058b54 | 3442 | path->reada = READA_BACK; |
7b128766 JB |
3443 | |
3444 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3445 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3446 | key.offset = (u64)-1; |
3447 | ||
7b128766 JB |
3448 | while (1) { |
3449 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3450 | if (ret < 0) |
3451 | goto out; | |
7b128766 JB |
3452 | |
3453 | /* | |
3454 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3455 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3456 | * find the key and see if we have stuff that matches |
3457 | */ | |
3458 | if (ret > 0) { | |
66b4ffd1 | 3459 | ret = 0; |
7b128766 JB |
3460 | if (path->slots[0] == 0) |
3461 | break; | |
3462 | path->slots[0]--; | |
3463 | } | |
3464 | ||
3465 | /* pull out the item */ | |
3466 | leaf = path->nodes[0]; | |
7b128766 JB |
3467 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3468 | ||
3469 | /* make sure the item matches what we want */ | |
3470 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3471 | break; | |
962a298f | 3472 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3473 | break; |
3474 | ||
3475 | /* release the path since we're done with it */ | |
b3b4aa74 | 3476 | btrfs_release_path(path); |
7b128766 JB |
3477 | |
3478 | /* | |
3479 | * this is where we are basically btrfs_lookup, without the | |
3480 | * crossing root thing. we store the inode number in the | |
3481 | * offset of the orphan item. | |
3482 | */ | |
8f6d7f4f JB |
3483 | |
3484 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3485 | btrfs_err(fs_info, |
3486 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3487 | ret = -EINVAL; |
3488 | goto out; | |
3489 | } | |
3490 | ||
3491 | last_objectid = found_key.offset; | |
3492 | ||
5d4f98a2 YZ |
3493 | found_key.objectid = found_key.offset; |
3494 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3495 | found_key.offset = 0; | |
0b246afa | 3496 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3497 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3498 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3499 | goto out; |
7b128766 | 3500 | |
0b246afa | 3501 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3502 | struct btrfs_root *dead_root; |
3503 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3504 | int is_dead_root = 0; | |
3505 | ||
3506 | /* | |
3507 | * this is an orphan in the tree root. Currently these | |
3508 | * could come from 2 sources: | |
3509 | * a) a snapshot deletion in progress | |
3510 | * b) a free space cache inode | |
3511 | * We need to distinguish those two, as the snapshot | |
3512 | * orphan must not get deleted. | |
3513 | * find_dead_roots already ran before us, so if this | |
3514 | * is a snapshot deletion, we should find the root | |
3515 | * in the dead_roots list | |
3516 | */ | |
3517 | spin_lock(&fs_info->trans_lock); | |
3518 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3519 | root_list) { | |
3520 | if (dead_root->root_key.objectid == | |
3521 | found_key.objectid) { | |
3522 | is_dead_root = 1; | |
3523 | break; | |
3524 | } | |
3525 | } | |
3526 | spin_unlock(&fs_info->trans_lock); | |
3527 | if (is_dead_root) { | |
3528 | /* prevent this orphan from being found again */ | |
3529 | key.offset = found_key.objectid - 1; | |
3530 | continue; | |
3531 | } | |
3532 | } | |
7b128766 | 3533 | /* |
a8c9e576 JB |
3534 | * Inode is already gone but the orphan item is still there, |
3535 | * kill the orphan item. | |
7b128766 | 3536 | */ |
67710892 | 3537 | if (ret == -ENOENT) { |
a8c9e576 | 3538 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3539 | if (IS_ERR(trans)) { |
3540 | ret = PTR_ERR(trans); | |
3541 | goto out; | |
3542 | } | |
0b246afa JM |
3543 | btrfs_debug(fs_info, "auto deleting %Lu", |
3544 | found_key.objectid); | |
a8c9e576 JB |
3545 | ret = btrfs_del_orphan_item(trans, root, |
3546 | found_key.objectid); | |
3a45bb20 | 3547 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3548 | if (ret) |
3549 | goto out; | |
7b128766 JB |
3550 | continue; |
3551 | } | |
3552 | ||
a8c9e576 JB |
3553 | /* |
3554 | * add this inode to the orphan list so btrfs_orphan_del does | |
3555 | * the proper thing when we hit it | |
3556 | */ | |
8a35d95f JB |
3557 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3558 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3559 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3560 | |
7b128766 JB |
3561 | /* if we have links, this was a truncate, lets do that */ |
3562 | if (inode->i_nlink) { | |
fae7f21c | 3563 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3564 | iput(inode); |
3565 | continue; | |
3566 | } | |
7b128766 | 3567 | nr_truncate++; |
f3fe820c JB |
3568 | |
3569 | /* 1 for the orphan item deletion. */ | |
3570 | trans = btrfs_start_transaction(root, 1); | |
3571 | if (IS_ERR(trans)) { | |
c69b26b0 | 3572 | iput(inode); |
f3fe820c JB |
3573 | ret = PTR_ERR(trans); |
3574 | goto out; | |
3575 | } | |
73f2e545 | 3576 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 3577 | btrfs_end_transaction(trans); |
c69b26b0 JB |
3578 | if (ret) { |
3579 | iput(inode); | |
f3fe820c | 3580 | goto out; |
c69b26b0 | 3581 | } |
f3fe820c | 3582 | |
66b4ffd1 | 3583 | ret = btrfs_truncate(inode); |
4a7d0f68 | 3584 | if (ret) |
3d6ae7bb | 3585 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7b128766 JB |
3586 | } else { |
3587 | nr_unlink++; | |
3588 | } | |
3589 | ||
3590 | /* this will do delete_inode and everything for us */ | |
3591 | iput(inode); | |
66b4ffd1 JB |
3592 | if (ret) |
3593 | goto out; | |
7b128766 | 3594 | } |
3254c876 MX |
3595 | /* release the path since we're done with it */ |
3596 | btrfs_release_path(path); | |
3597 | ||
d68fc57b YZ |
3598 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3599 | ||
3600 | if (root->orphan_block_rsv) | |
2ff7e61e | 3601 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
d68fc57b YZ |
3602 | (u64)-1); |
3603 | ||
27cdeb70 MX |
3604 | if (root->orphan_block_rsv || |
3605 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3606 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3607 | if (!IS_ERR(trans)) |
3a45bb20 | 3608 | btrfs_end_transaction(trans); |
d68fc57b | 3609 | } |
7b128766 JB |
3610 | |
3611 | if (nr_unlink) | |
0b246afa | 3612 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3613 | if (nr_truncate) |
0b246afa | 3614 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3615 | |
3616 | out: | |
3617 | if (ret) | |
0b246afa | 3618 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3619 | btrfs_free_path(path); |
3620 | return ret; | |
7b128766 JB |
3621 | } |
3622 | ||
46a53cca CM |
3623 | /* |
3624 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3625 | * don't find any xattrs, we know there can't be any acls. | |
3626 | * | |
3627 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3628 | */ | |
3629 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3630 | int slot, u64 objectid, |
3631 | int *first_xattr_slot) | |
46a53cca CM |
3632 | { |
3633 | u32 nritems = btrfs_header_nritems(leaf); | |
3634 | struct btrfs_key found_key; | |
f23b5a59 JB |
3635 | static u64 xattr_access = 0; |
3636 | static u64 xattr_default = 0; | |
46a53cca CM |
3637 | int scanned = 0; |
3638 | ||
f23b5a59 | 3639 | if (!xattr_access) { |
97d79299 AG |
3640 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3641 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3642 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3643 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3644 | } |
3645 | ||
46a53cca | 3646 | slot++; |
63541927 | 3647 | *first_xattr_slot = -1; |
46a53cca CM |
3648 | while (slot < nritems) { |
3649 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3650 | ||
3651 | /* we found a different objectid, there must not be acls */ | |
3652 | if (found_key.objectid != objectid) | |
3653 | return 0; | |
3654 | ||
3655 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3656 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3657 | if (*first_xattr_slot == -1) |
3658 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3659 | if (found_key.offset == xattr_access || |
3660 | found_key.offset == xattr_default) | |
3661 | return 1; | |
3662 | } | |
46a53cca CM |
3663 | |
3664 | /* | |
3665 | * we found a key greater than an xattr key, there can't | |
3666 | * be any acls later on | |
3667 | */ | |
3668 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3669 | return 0; | |
3670 | ||
3671 | slot++; | |
3672 | scanned++; | |
3673 | ||
3674 | /* | |
3675 | * it goes inode, inode backrefs, xattrs, extents, | |
3676 | * so if there are a ton of hard links to an inode there can | |
3677 | * be a lot of backrefs. Don't waste time searching too hard, | |
3678 | * this is just an optimization | |
3679 | */ | |
3680 | if (scanned >= 8) | |
3681 | break; | |
3682 | } | |
3683 | /* we hit the end of the leaf before we found an xattr or | |
3684 | * something larger than an xattr. We have to assume the inode | |
3685 | * has acls | |
3686 | */ | |
63541927 FDBM |
3687 | if (*first_xattr_slot == -1) |
3688 | *first_xattr_slot = slot; | |
46a53cca CM |
3689 | return 1; |
3690 | } | |
3691 | ||
d352ac68 CM |
3692 | /* |
3693 | * read an inode from the btree into the in-memory inode | |
3694 | */ | |
67710892 | 3695 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 | 3696 | { |
0b246afa | 3697 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 3698 | struct btrfs_path *path; |
5f39d397 | 3699 | struct extent_buffer *leaf; |
39279cc3 CM |
3700 | struct btrfs_inode_item *inode_item; |
3701 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3702 | struct btrfs_key location; | |
67de1176 | 3703 | unsigned long ptr; |
46a53cca | 3704 | int maybe_acls; |
618e21d5 | 3705 | u32 rdev; |
39279cc3 | 3706 | int ret; |
2f7e33d4 | 3707 | bool filled = false; |
63541927 | 3708 | int first_xattr_slot; |
2f7e33d4 MX |
3709 | |
3710 | ret = btrfs_fill_inode(inode, &rdev); | |
3711 | if (!ret) | |
3712 | filled = true; | |
39279cc3 CM |
3713 | |
3714 | path = btrfs_alloc_path(); | |
67710892 FM |
3715 | if (!path) { |
3716 | ret = -ENOMEM; | |
1748f843 | 3717 | goto make_bad; |
67710892 | 3718 | } |
1748f843 | 3719 | |
39279cc3 | 3720 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3721 | |
39279cc3 | 3722 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3723 | if (ret) { |
3724 | if (ret > 0) | |
3725 | ret = -ENOENT; | |
39279cc3 | 3726 | goto make_bad; |
67710892 | 3727 | } |
39279cc3 | 3728 | |
5f39d397 | 3729 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3730 | |
3731 | if (filled) | |
67de1176 | 3732 | goto cache_index; |
2f7e33d4 | 3733 | |
5f39d397 CM |
3734 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3735 | struct btrfs_inode_item); | |
5f39d397 | 3736 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3737 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3738 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3739 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3740 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3741 | |
a937b979 DS |
3742 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3743 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3744 | |
a937b979 DS |
3745 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3746 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3747 | |
a937b979 DS |
3748 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3749 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3750 | |
9cc97d64 | 3751 | BTRFS_I(inode)->i_otime.tv_sec = |
3752 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3753 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3754 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3755 | |
a76a3cd4 | 3756 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3757 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3758 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3759 | ||
6e17d30b YD |
3760 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3761 | inode->i_generation = BTRFS_I(inode)->generation; | |
3762 | inode->i_rdev = 0; | |
3763 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3764 | ||
3765 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3766 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3767 | ||
3768 | cache_index: | |
5dc562c5 JB |
3769 | /* |
3770 | * If we were modified in the current generation and evicted from memory | |
3771 | * and then re-read we need to do a full sync since we don't have any | |
3772 | * idea about which extents were modified before we were evicted from | |
3773 | * cache. | |
6e17d30b YD |
3774 | * |
3775 | * This is required for both inode re-read from disk and delayed inode | |
3776 | * in delayed_nodes_tree. | |
5dc562c5 | 3777 | */ |
0b246afa | 3778 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3779 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3780 | &BTRFS_I(inode)->runtime_flags); | |
3781 | ||
bde6c242 FM |
3782 | /* |
3783 | * We don't persist the id of the transaction where an unlink operation | |
3784 | * against the inode was last made. So here we assume the inode might | |
3785 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3786 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3787 | * between the inode and its parent if the inode is fsync'ed and the log | |
3788 | * replayed. For example, in the scenario: | |
3789 | * | |
3790 | * touch mydir/foo | |
3791 | * ln mydir/foo mydir/bar | |
3792 | * sync | |
3793 | * unlink mydir/bar | |
3794 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3795 | * xfs_io -c fsync mydir/foo | |
3796 | * <power failure> | |
3797 | * mount fs, triggers fsync log replay | |
3798 | * | |
3799 | * We must make sure that when we fsync our inode foo we also log its | |
3800 | * parent inode, otherwise after log replay the parent still has the | |
3801 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3802 | * and doesn't have an inode ref with the name "bar" anymore. | |
3803 | * | |
3804 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3805 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3806 | * transaction commits on fsync if our inode is a directory, or if our |
3807 | * inode is not a directory, logging its parent unnecessarily. | |
3808 | */ | |
3809 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3810 | ||
67de1176 MX |
3811 | path->slots[0]++; |
3812 | if (inode->i_nlink != 1 || | |
3813 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3814 | goto cache_acl; | |
3815 | ||
3816 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3817 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3818 | goto cache_acl; |
3819 | ||
3820 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3821 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3822 | struct btrfs_inode_ref *ref; | |
3823 | ||
3824 | ref = (struct btrfs_inode_ref *)ptr; | |
3825 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3826 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3827 | struct btrfs_inode_extref *extref; | |
3828 | ||
3829 | extref = (struct btrfs_inode_extref *)ptr; | |
3830 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3831 | extref); | |
3832 | } | |
2f7e33d4 | 3833 | cache_acl: |
46a53cca CM |
3834 | /* |
3835 | * try to precache a NULL acl entry for files that don't have | |
3836 | * any xattrs or acls | |
3837 | */ | |
33345d01 | 3838 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3839 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3840 | if (first_xattr_slot != -1) { |
3841 | path->slots[0] = first_xattr_slot; | |
3842 | ret = btrfs_load_inode_props(inode, path); | |
3843 | if (ret) | |
0b246afa | 3844 | btrfs_err(fs_info, |
351fd353 | 3845 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3846 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3847 | root->root_key.objectid, ret); |
3848 | } | |
3849 | btrfs_free_path(path); | |
3850 | ||
72c04902 AV |
3851 | if (!maybe_acls) |
3852 | cache_no_acl(inode); | |
46a53cca | 3853 | |
39279cc3 | 3854 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3855 | case S_IFREG: |
3856 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3857 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3858 | inode->i_fop = &btrfs_file_operations; |
3859 | inode->i_op = &btrfs_file_inode_operations; | |
3860 | break; | |
3861 | case S_IFDIR: | |
3862 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3863 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3864 | break; |
3865 | case S_IFLNK: | |
3866 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3867 | inode_nohighmem(inode); |
39279cc3 CM |
3868 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3869 | break; | |
618e21d5 | 3870 | default: |
0279b4cd | 3871 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3872 | init_special_inode(inode, inode->i_mode, rdev); |
3873 | break; | |
39279cc3 | 3874 | } |
6cbff00f CH |
3875 | |
3876 | btrfs_update_iflags(inode); | |
67710892 | 3877 | return 0; |
39279cc3 CM |
3878 | |
3879 | make_bad: | |
39279cc3 | 3880 | btrfs_free_path(path); |
39279cc3 | 3881 | make_bad_inode(inode); |
67710892 | 3882 | return ret; |
39279cc3 CM |
3883 | } |
3884 | ||
d352ac68 CM |
3885 | /* |
3886 | * given a leaf and an inode, copy the inode fields into the leaf | |
3887 | */ | |
e02119d5 CM |
3888 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3889 | struct extent_buffer *leaf, | |
5f39d397 | 3890 | struct btrfs_inode_item *item, |
39279cc3 CM |
3891 | struct inode *inode) |
3892 | { | |
51fab693 LB |
3893 | struct btrfs_map_token token; |
3894 | ||
3895 | btrfs_init_map_token(&token); | |
5f39d397 | 3896 | |
51fab693 LB |
3897 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3898 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3899 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3900 | &token); | |
3901 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3902 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3903 | |
a937b979 | 3904 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3905 | inode->i_atime.tv_sec, &token); |
a937b979 | 3906 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3907 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3908 | |
a937b979 | 3909 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3910 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3911 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3912 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3913 | |
a937b979 | 3914 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3915 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3916 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3917 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3918 | |
9cc97d64 | 3919 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3920 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3921 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3922 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3923 | ||
51fab693 LB |
3924 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3925 | &token); | |
3926 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3927 | &token); | |
3928 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3929 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3930 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3931 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3932 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3933 | } |
3934 | ||
d352ac68 CM |
3935 | /* |
3936 | * copy everything in the in-memory inode into the btree. | |
3937 | */ | |
2115133f | 3938 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3939 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3940 | { |
3941 | struct btrfs_inode_item *inode_item; | |
3942 | struct btrfs_path *path; | |
5f39d397 | 3943 | struct extent_buffer *leaf; |
39279cc3 CM |
3944 | int ret; |
3945 | ||
3946 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3947 | if (!path) |
3948 | return -ENOMEM; | |
3949 | ||
b9473439 | 3950 | path->leave_spinning = 1; |
16cdcec7 MX |
3951 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3952 | 1); | |
39279cc3 CM |
3953 | if (ret) { |
3954 | if (ret > 0) | |
3955 | ret = -ENOENT; | |
3956 | goto failed; | |
3957 | } | |
3958 | ||
5f39d397 CM |
3959 | leaf = path->nodes[0]; |
3960 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3961 | struct btrfs_inode_item); |
39279cc3 | 3962 | |
e02119d5 | 3963 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3964 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3965 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3966 | ret = 0; |
3967 | failed: | |
39279cc3 CM |
3968 | btrfs_free_path(path); |
3969 | return ret; | |
3970 | } | |
3971 | ||
2115133f CM |
3972 | /* |
3973 | * copy everything in the in-memory inode into the btree. | |
3974 | */ | |
3975 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3976 | struct btrfs_root *root, struct inode *inode) | |
3977 | { | |
0b246afa | 3978 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3979 | int ret; |
3980 | ||
3981 | /* | |
3982 | * If the inode is a free space inode, we can deadlock during commit | |
3983 | * if we put it into the delayed code. | |
3984 | * | |
3985 | * The data relocation inode should also be directly updated | |
3986 | * without delay | |
3987 | */ | |
70ddc553 | 3988 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3989 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3990 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3991 | btrfs_update_root_times(trans, root); |
3992 | ||
2115133f CM |
3993 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3994 | if (!ret) | |
3995 | btrfs_set_inode_last_trans(trans, inode); | |
3996 | return ret; | |
3997 | } | |
3998 | ||
3999 | return btrfs_update_inode_item(trans, root, inode); | |
4000 | } | |
4001 | ||
be6aef60 JB |
4002 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4003 | struct btrfs_root *root, | |
4004 | struct inode *inode) | |
2115133f CM |
4005 | { |
4006 | int ret; | |
4007 | ||
4008 | ret = btrfs_update_inode(trans, root, inode); | |
4009 | if (ret == -ENOSPC) | |
4010 | return btrfs_update_inode_item(trans, root, inode); | |
4011 | return ret; | |
4012 | } | |
4013 | ||
d352ac68 CM |
4014 | /* |
4015 | * unlink helper that gets used here in inode.c and in the tree logging | |
4016 | * recovery code. It remove a link in a directory with a given name, and | |
4017 | * also drops the back refs in the inode to the directory | |
4018 | */ | |
92986796 AV |
4019 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4020 | struct btrfs_root *root, | |
4ec5934e NB |
4021 | struct btrfs_inode *dir, |
4022 | struct btrfs_inode *inode, | |
92986796 | 4023 | const char *name, int name_len) |
39279cc3 | 4024 | { |
0b246afa | 4025 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4026 | struct btrfs_path *path; |
39279cc3 | 4027 | int ret = 0; |
5f39d397 | 4028 | struct extent_buffer *leaf; |
39279cc3 | 4029 | struct btrfs_dir_item *di; |
5f39d397 | 4030 | struct btrfs_key key; |
aec7477b | 4031 | u64 index; |
33345d01 LZ |
4032 | u64 ino = btrfs_ino(inode); |
4033 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4034 | |
4035 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4036 | if (!path) { |
4037 | ret = -ENOMEM; | |
554233a6 | 4038 | goto out; |
54aa1f4d CM |
4039 | } |
4040 | ||
b9473439 | 4041 | path->leave_spinning = 1; |
33345d01 | 4042 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4043 | name, name_len, -1); |
4044 | if (IS_ERR(di)) { | |
4045 | ret = PTR_ERR(di); | |
4046 | goto err; | |
4047 | } | |
4048 | if (!di) { | |
4049 | ret = -ENOENT; | |
4050 | goto err; | |
4051 | } | |
5f39d397 CM |
4052 | leaf = path->nodes[0]; |
4053 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4054 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4055 | if (ret) |
4056 | goto err; | |
b3b4aa74 | 4057 | btrfs_release_path(path); |
39279cc3 | 4058 | |
67de1176 MX |
4059 | /* |
4060 | * If we don't have dir index, we have to get it by looking up | |
4061 | * the inode ref, since we get the inode ref, remove it directly, | |
4062 | * it is unnecessary to do delayed deletion. | |
4063 | * | |
4064 | * But if we have dir index, needn't search inode ref to get it. | |
4065 | * Since the inode ref is close to the inode item, it is better | |
4066 | * that we delay to delete it, and just do this deletion when | |
4067 | * we update the inode item. | |
4068 | */ | |
4ec5934e | 4069 | if (inode->dir_index) { |
67de1176 MX |
4070 | ret = btrfs_delayed_delete_inode_ref(inode); |
4071 | if (!ret) { | |
4ec5934e | 4072 | index = inode->dir_index; |
67de1176 MX |
4073 | goto skip_backref; |
4074 | } | |
4075 | } | |
4076 | ||
33345d01 LZ |
4077 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4078 | dir_ino, &index); | |
aec7477b | 4079 | if (ret) { |
0b246afa | 4080 | btrfs_info(fs_info, |
c2cf52eb | 4081 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4082 | name_len, name, ino, dir_ino); |
66642832 | 4083 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4084 | goto err; |
4085 | } | |
67de1176 | 4086 | skip_backref: |
2ff7e61e | 4087 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
79787eaa | 4088 | if (ret) { |
66642832 | 4089 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4090 | goto err; |
79787eaa | 4091 | } |
39279cc3 | 4092 | |
4ec5934e NB |
4093 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4094 | dir_ino); | |
79787eaa | 4095 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4096 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4097 | goto err; |
4098 | } | |
e02119d5 | 4099 | |
4ec5934e NB |
4100 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4101 | index); | |
6418c961 CM |
4102 | if (ret == -ENOENT) |
4103 | ret = 0; | |
d4e3991b | 4104 | else if (ret) |
66642832 | 4105 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4106 | err: |
4107 | btrfs_free_path(path); | |
e02119d5 CM |
4108 | if (ret) |
4109 | goto out; | |
4110 | ||
6ef06d27 | 4111 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4112 | inode_inc_iversion(&inode->vfs_inode); |
4113 | inode_inc_iversion(&dir->vfs_inode); | |
4114 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4115 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4116 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4117 | out: |
39279cc3 CM |
4118 | return ret; |
4119 | } | |
4120 | ||
92986796 AV |
4121 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4122 | struct btrfs_root *root, | |
4ec5934e | 4123 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4124 | const char *name, int name_len) |
4125 | { | |
4126 | int ret; | |
4127 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4128 | if (!ret) { | |
4ec5934e NB |
4129 | drop_nlink(&inode->vfs_inode); |
4130 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4131 | } |
4132 | return ret; | |
4133 | } | |
39279cc3 | 4134 | |
a22285a6 YZ |
4135 | /* |
4136 | * helper to start transaction for unlink and rmdir. | |
4137 | * | |
d52be818 JB |
4138 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4139 | * if we cannot make our reservations the normal way try and see if there is | |
4140 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4141 | * allow the unlink to occur. | |
a22285a6 | 4142 | */ |
d52be818 | 4143 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4144 | { |
a22285a6 | 4145 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4146 | |
e70bea5f JB |
4147 | /* |
4148 | * 1 for the possible orphan item | |
4149 | * 1 for the dir item | |
4150 | * 1 for the dir index | |
4151 | * 1 for the inode ref | |
e70bea5f JB |
4152 | * 1 for the inode |
4153 | */ | |
8eab77ff | 4154 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4155 | } |
4156 | ||
4157 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4158 | { | |
4159 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4160 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4161 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4162 | int ret; |
a22285a6 | 4163 | |
d52be818 | 4164 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4165 | if (IS_ERR(trans)) |
4166 | return PTR_ERR(trans); | |
5f39d397 | 4167 | |
4ec5934e NB |
4168 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4169 | 0); | |
12fcfd22 | 4170 | |
4ec5934e NB |
4171 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4172 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4173 | dentry->d_name.len); | |
b532402e TI |
4174 | if (ret) |
4175 | goto out; | |
7b128766 | 4176 | |
a22285a6 | 4177 | if (inode->i_nlink == 0) { |
73f2e545 | 4178 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4179 | if (ret) |
4180 | goto out; | |
a22285a6 | 4181 | } |
7b128766 | 4182 | |
b532402e | 4183 | out: |
3a45bb20 | 4184 | btrfs_end_transaction(trans); |
2ff7e61e | 4185 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4186 | return ret; |
4187 | } | |
4188 | ||
4df27c4d YZ |
4189 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4190 | struct btrfs_root *root, | |
4191 | struct inode *dir, u64 objectid, | |
4192 | const char *name, int name_len) | |
4193 | { | |
0b246afa | 4194 | struct btrfs_fs_info *fs_info = root->fs_info; |
4df27c4d YZ |
4195 | struct btrfs_path *path; |
4196 | struct extent_buffer *leaf; | |
4197 | struct btrfs_dir_item *di; | |
4198 | struct btrfs_key key; | |
4199 | u64 index; | |
4200 | int ret; | |
4a0cc7ca | 4201 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4202 | |
4203 | path = btrfs_alloc_path(); | |
4204 | if (!path) | |
4205 | return -ENOMEM; | |
4206 | ||
33345d01 | 4207 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4208 | name, name_len, -1); |
79787eaa JM |
4209 | if (IS_ERR_OR_NULL(di)) { |
4210 | if (!di) | |
4211 | ret = -ENOENT; | |
4212 | else | |
4213 | ret = PTR_ERR(di); | |
4214 | goto out; | |
4215 | } | |
4df27c4d YZ |
4216 | |
4217 | leaf = path->nodes[0]; | |
4218 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4219 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4220 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4221 | if (ret) { |
66642832 | 4222 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4223 | goto out; |
4224 | } | |
b3b4aa74 | 4225 | btrfs_release_path(path); |
4df27c4d | 4226 | |
0b246afa JM |
4227 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
4228 | root->root_key.objectid, dir_ino, | |
4229 | &index, name, name_len); | |
4df27c4d | 4230 | if (ret < 0) { |
79787eaa | 4231 | if (ret != -ENOENT) { |
66642832 | 4232 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4233 | goto out; |
4234 | } | |
33345d01 | 4235 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4236 | name, name_len); |
79787eaa JM |
4237 | if (IS_ERR_OR_NULL(di)) { |
4238 | if (!di) | |
4239 | ret = -ENOENT; | |
4240 | else | |
4241 | ret = PTR_ERR(di); | |
66642832 | 4242 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4243 | goto out; |
4244 | } | |
4df27c4d YZ |
4245 | |
4246 | leaf = path->nodes[0]; | |
4247 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4248 | btrfs_release_path(path); |
4df27c4d YZ |
4249 | index = key.offset; |
4250 | } | |
945d8962 | 4251 | btrfs_release_path(path); |
4df27c4d | 4252 | |
e67bbbb9 | 4253 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4254 | if (ret) { |
66642832 | 4255 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4256 | goto out; |
4257 | } | |
4df27c4d | 4258 | |
6ef06d27 | 4259 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4260 | inode_inc_iversion(dir); |
c2050a45 | 4261 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4262 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4263 | if (ret) |
66642832 | 4264 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4265 | out: |
71d7aed0 | 4266 | btrfs_free_path(path); |
79787eaa | 4267 | return ret; |
4df27c4d YZ |
4268 | } |
4269 | ||
39279cc3 CM |
4270 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4271 | { | |
2b0143b5 | 4272 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4273 | int err = 0; |
39279cc3 | 4274 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4275 | struct btrfs_trans_handle *trans; |
44f714da | 4276 | u64 last_unlink_trans; |
39279cc3 | 4277 | |
b3ae244e | 4278 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4279 | return -ENOTEMPTY; |
4a0cc7ca | 4280 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
b3ae244e | 4281 | return -EPERM; |
134d4512 | 4282 | |
d52be818 | 4283 | trans = __unlink_start_trans(dir); |
a22285a6 | 4284 | if (IS_ERR(trans)) |
5df6a9f6 | 4285 | return PTR_ERR(trans); |
5df6a9f6 | 4286 | |
4a0cc7ca | 4287 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4288 | err = btrfs_unlink_subvol(trans, root, dir, |
4289 | BTRFS_I(inode)->location.objectid, | |
4290 | dentry->d_name.name, | |
4291 | dentry->d_name.len); | |
4292 | goto out; | |
4293 | } | |
4294 | ||
73f2e545 | 4295 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4296 | if (err) |
4df27c4d | 4297 | goto out; |
7b128766 | 4298 | |
44f714da FM |
4299 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4300 | ||
39279cc3 | 4301 | /* now the directory is empty */ |
4ec5934e NB |
4302 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4303 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4304 | dentry->d_name.len); | |
44f714da | 4305 | if (!err) { |
6ef06d27 | 4306 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4307 | /* |
4308 | * Propagate the last_unlink_trans value of the deleted dir to | |
4309 | * its parent directory. This is to prevent an unrecoverable | |
4310 | * log tree in the case we do something like this: | |
4311 | * 1) create dir foo | |
4312 | * 2) create snapshot under dir foo | |
4313 | * 3) delete the snapshot | |
4314 | * 4) rmdir foo | |
4315 | * 5) mkdir foo | |
4316 | * 6) fsync foo or some file inside foo | |
4317 | */ | |
4318 | if (last_unlink_trans >= trans->transid) | |
4319 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4320 | } | |
4df27c4d | 4321 | out: |
3a45bb20 | 4322 | btrfs_end_transaction(trans); |
2ff7e61e | 4323 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4324 | |
39279cc3 CM |
4325 | return err; |
4326 | } | |
4327 | ||
28f75a0e CM |
4328 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4329 | struct btrfs_root *root, | |
4330 | u64 bytes_deleted) | |
4331 | { | |
0b246afa | 4332 | struct btrfs_fs_info *fs_info = root->fs_info; |
28f75a0e CM |
4333 | int ret; |
4334 | ||
dc95f7bf JB |
4335 | /* |
4336 | * This is only used to apply pressure to the enospc system, we don't | |
4337 | * intend to use this reservation at all. | |
4338 | */ | |
2ff7e61e | 4339 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
0b246afa JM |
4340 | bytes_deleted *= fs_info->nodesize; |
4341 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
28f75a0e | 4342 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
dc95f7bf | 4343 | if (!ret) { |
0b246afa | 4344 | trace_btrfs_space_reservation(fs_info, "transaction", |
dc95f7bf JB |
4345 | trans->transid, |
4346 | bytes_deleted, 1); | |
28f75a0e | 4347 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4348 | } |
28f75a0e CM |
4349 | return ret; |
4350 | ||
4351 | } | |
4352 | ||
0305cd5f FM |
4353 | static int truncate_inline_extent(struct inode *inode, |
4354 | struct btrfs_path *path, | |
4355 | struct btrfs_key *found_key, | |
4356 | const u64 item_end, | |
4357 | const u64 new_size) | |
4358 | { | |
4359 | struct extent_buffer *leaf = path->nodes[0]; | |
4360 | int slot = path->slots[0]; | |
4361 | struct btrfs_file_extent_item *fi; | |
4362 | u32 size = (u32)(new_size - found_key->offset); | |
4363 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4364 | ||
4365 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4366 | ||
4367 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4368 | loff_t offset = new_size; | |
09cbfeaf | 4369 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4370 | |
4371 | /* | |
4372 | * Zero out the remaining of the last page of our inline extent, | |
4373 | * instead of directly truncating our inline extent here - that | |
4374 | * would be much more complex (decompressing all the data, then | |
4375 | * compressing the truncated data, which might be bigger than | |
4376 | * the size of the inline extent, resize the extent, etc). | |
4377 | * We release the path because to get the page we might need to | |
4378 | * read the extent item from disk (data not in the page cache). | |
4379 | */ | |
4380 | btrfs_release_path(path); | |
9703fefe CR |
4381 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4382 | 0); | |
0305cd5f FM |
4383 | } |
4384 | ||
4385 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4386 | size = btrfs_file_extent_calc_inline_size(size); | |
2ff7e61e | 4387 | btrfs_truncate_item(root->fs_info, path, size, 1); |
0305cd5f FM |
4388 | |
4389 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4390 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4391 | ||
4392 | return 0; | |
4393 | } | |
4394 | ||
39279cc3 CM |
4395 | /* |
4396 | * this can truncate away extent items, csum items and directory items. | |
4397 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4398 | * any higher than new_size |
39279cc3 CM |
4399 | * |
4400 | * csum items that cross the new i_size are truncated to the new size | |
4401 | * as well. | |
7b128766 JB |
4402 | * |
4403 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4404 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4405 | */ |
8082510e YZ |
4406 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4407 | struct btrfs_root *root, | |
4408 | struct inode *inode, | |
4409 | u64 new_size, u32 min_type) | |
39279cc3 | 4410 | { |
0b246afa | 4411 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4412 | struct btrfs_path *path; |
5f39d397 | 4413 | struct extent_buffer *leaf; |
39279cc3 | 4414 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4415 | struct btrfs_key key; |
4416 | struct btrfs_key found_key; | |
39279cc3 | 4417 | u64 extent_start = 0; |
db94535d | 4418 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4419 | u64 extent_offset = 0; |
39279cc3 | 4420 | u64 item_end = 0; |
c1aa4575 | 4421 | u64 last_size = new_size; |
8082510e | 4422 | u32 found_type = (u8)-1; |
39279cc3 CM |
4423 | int found_extent; |
4424 | int del_item; | |
85e21bac CM |
4425 | int pending_del_nr = 0; |
4426 | int pending_del_slot = 0; | |
179e29e4 | 4427 | int extent_type = -1; |
8082510e YZ |
4428 | int ret; |
4429 | int err = 0; | |
4a0cc7ca | 4430 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4431 | u64 bytes_deleted = 0; |
1262133b JB |
4432 | bool be_nice = 0; |
4433 | bool should_throttle = 0; | |
28f75a0e | 4434 | bool should_end = 0; |
8082510e YZ |
4435 | |
4436 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4437 | |
28ed1345 CM |
4438 | /* |
4439 | * for non-free space inodes and ref cows, we want to back off from | |
4440 | * time to time | |
4441 | */ | |
70ddc553 | 4442 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 CM |
4443 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
4444 | be_nice = 1; | |
4445 | ||
0eb0e19c MF |
4446 | path = btrfs_alloc_path(); |
4447 | if (!path) | |
4448 | return -ENOMEM; | |
e4058b54 | 4449 | path->reada = READA_BACK; |
0eb0e19c | 4450 | |
5dc562c5 JB |
4451 | /* |
4452 | * We want to drop from the next block forward in case this new size is | |
4453 | * not block aligned since we will be keeping the last block of the | |
4454 | * extent just the way it is. | |
4455 | */ | |
27cdeb70 | 4456 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4457 | root == fs_info->tree_root) |
dcdbc059 | 4458 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4459 | fs_info->sectorsize), |
da17066c | 4460 | (u64)-1, 0); |
8082510e | 4461 | |
16cdcec7 MX |
4462 | /* |
4463 | * This function is also used to drop the items in the log tree before | |
4464 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4465 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4466 | * items. | |
4467 | */ | |
4468 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4469 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4470 | |
33345d01 | 4471 | key.objectid = ino; |
39279cc3 | 4472 | key.offset = (u64)-1; |
5f39d397 CM |
4473 | key.type = (u8)-1; |
4474 | ||
85e21bac | 4475 | search_again: |
28ed1345 CM |
4476 | /* |
4477 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4478 | * up a huge file in a single leaf. Most of the time that | |
4479 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4480 | */ | |
ee22184b | 4481 | if (be_nice && bytes_deleted > SZ_32M) { |
3a45bb20 | 4482 | if (btrfs_should_end_transaction(trans)) { |
28ed1345 CM |
4483 | err = -EAGAIN; |
4484 | goto error; | |
4485 | } | |
4486 | } | |
4487 | ||
4488 | ||
b9473439 | 4489 | path->leave_spinning = 1; |
85e21bac | 4490 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4491 | if (ret < 0) { |
4492 | err = ret; | |
4493 | goto out; | |
4494 | } | |
d397712b | 4495 | |
85e21bac | 4496 | if (ret > 0) { |
e02119d5 CM |
4497 | /* there are no items in the tree for us to truncate, we're |
4498 | * done | |
4499 | */ | |
8082510e YZ |
4500 | if (path->slots[0] == 0) |
4501 | goto out; | |
85e21bac CM |
4502 | path->slots[0]--; |
4503 | } | |
4504 | ||
d397712b | 4505 | while (1) { |
39279cc3 | 4506 | fi = NULL; |
5f39d397 CM |
4507 | leaf = path->nodes[0]; |
4508 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4509 | found_type = found_key.type; |
39279cc3 | 4510 | |
33345d01 | 4511 | if (found_key.objectid != ino) |
39279cc3 | 4512 | break; |
5f39d397 | 4513 | |
85e21bac | 4514 | if (found_type < min_type) |
39279cc3 CM |
4515 | break; |
4516 | ||
5f39d397 | 4517 | item_end = found_key.offset; |
39279cc3 | 4518 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4519 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4520 | struct btrfs_file_extent_item); |
179e29e4 CM |
4521 | extent_type = btrfs_file_extent_type(leaf, fi); |
4522 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4523 | item_end += |
db94535d | 4524 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4525 | |
4526 | trace_btrfs_truncate_show_fi_regular( | |
4527 | BTRFS_I(inode), leaf, fi, | |
4528 | found_key.offset); | |
179e29e4 | 4529 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4530 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4531 | path->slots[0], fi); |
09ed2f16 LB |
4532 | |
4533 | trace_btrfs_truncate_show_fi_inline( | |
4534 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4535 | found_key.offset); | |
39279cc3 | 4536 | } |
008630c1 | 4537 | item_end--; |
39279cc3 | 4538 | } |
8082510e YZ |
4539 | if (found_type > min_type) { |
4540 | del_item = 1; | |
4541 | } else { | |
76b42abb | 4542 | if (item_end < new_size) |
b888db2b | 4543 | break; |
8082510e YZ |
4544 | if (found_key.offset >= new_size) |
4545 | del_item = 1; | |
4546 | else | |
4547 | del_item = 0; | |
39279cc3 | 4548 | } |
39279cc3 | 4549 | found_extent = 0; |
39279cc3 | 4550 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4551 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4552 | goto delete; | |
4553 | ||
7f4f6e0a JB |
4554 | if (del_item) |
4555 | last_size = found_key.offset; | |
4556 | else | |
4557 | last_size = new_size; | |
4558 | ||
179e29e4 | 4559 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4560 | u64 num_dec; |
db94535d | 4561 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4562 | if (!del_item) { |
db94535d CM |
4563 | u64 orig_num_bytes = |
4564 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4565 | extent_num_bytes = ALIGN(new_size - |
4566 | found_key.offset, | |
0b246afa | 4567 | fs_info->sectorsize); |
db94535d CM |
4568 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4569 | extent_num_bytes); | |
4570 | num_dec = (orig_num_bytes - | |
9069218d | 4571 | extent_num_bytes); |
27cdeb70 MX |
4572 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4573 | &root->state) && | |
4574 | extent_start != 0) | |
a76a3cd4 | 4575 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4576 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4577 | } else { |
db94535d CM |
4578 | extent_num_bytes = |
4579 | btrfs_file_extent_disk_num_bytes(leaf, | |
4580 | fi); | |
5d4f98a2 YZ |
4581 | extent_offset = found_key.offset - |
4582 | btrfs_file_extent_offset(leaf, fi); | |
4583 | ||
39279cc3 | 4584 | /* FIXME blocksize != 4096 */ |
9069218d | 4585 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4586 | if (extent_start != 0) { |
4587 | found_extent = 1; | |
27cdeb70 MX |
4588 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4589 | &root->state)) | |
a76a3cd4 | 4590 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4591 | } |
39279cc3 | 4592 | } |
9069218d | 4593 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4594 | /* |
4595 | * we can't truncate inline items that have had | |
4596 | * special encodings | |
4597 | */ | |
4598 | if (!del_item && | |
c8b97818 CM |
4599 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4600 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4601 | |
4602 | /* | |
0305cd5f FM |
4603 | * Need to release path in order to truncate a |
4604 | * compressed extent. So delete any accumulated | |
4605 | * extent items so far. | |
514ac8ad | 4606 | */ |
0305cd5f FM |
4607 | if (btrfs_file_extent_compression(leaf, fi) != |
4608 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4609 | err = btrfs_del_items(trans, root, path, | |
4610 | pending_del_slot, | |
4611 | pending_del_nr); | |
4612 | if (err) { | |
4613 | btrfs_abort_transaction(trans, | |
0305cd5f FM |
4614 | err); |
4615 | goto error; | |
4616 | } | |
4617 | pending_del_nr = 0; | |
4618 | } | |
4619 | ||
4620 | err = truncate_inline_extent(inode, path, | |
4621 | &found_key, | |
4622 | item_end, | |
4623 | new_size); | |
4624 | if (err) { | |
66642832 | 4625 | btrfs_abort_transaction(trans, err); |
0305cd5f FM |
4626 | goto error; |
4627 | } | |
27cdeb70 MX |
4628 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4629 | &root->state)) { | |
0305cd5f | 4630 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4631 | } |
39279cc3 | 4632 | } |
179e29e4 | 4633 | delete: |
39279cc3 | 4634 | if (del_item) { |
85e21bac CM |
4635 | if (!pending_del_nr) { |
4636 | /* no pending yet, add ourselves */ | |
4637 | pending_del_slot = path->slots[0]; | |
4638 | pending_del_nr = 1; | |
4639 | } else if (pending_del_nr && | |
4640 | path->slots[0] + 1 == pending_del_slot) { | |
4641 | /* hop on the pending chunk */ | |
4642 | pending_del_nr++; | |
4643 | pending_del_slot = path->slots[0]; | |
4644 | } else { | |
d397712b | 4645 | BUG(); |
85e21bac | 4646 | } |
39279cc3 CM |
4647 | } else { |
4648 | break; | |
4649 | } | |
28f75a0e CM |
4650 | should_throttle = 0; |
4651 | ||
27cdeb70 MX |
4652 | if (found_extent && |
4653 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4654 | root == fs_info->tree_root)) { |
b9473439 | 4655 | btrfs_set_path_blocking(path); |
28ed1345 | 4656 | bytes_deleted += extent_num_bytes; |
2ff7e61e | 4657 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
5d4f98a2 YZ |
4658 | extent_num_bytes, 0, |
4659 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4660 | ino, extent_offset); |
39279cc3 | 4661 | BUG_ON(ret); |
2ff7e61e JM |
4662 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
4663 | btrfs_async_run_delayed_refs(fs_info, | |
dd4b857a WX |
4664 | trans->delayed_ref_updates * 2, |
4665 | trans->transid, 0); | |
28f75a0e CM |
4666 | if (be_nice) { |
4667 | if (truncate_space_check(trans, root, | |
4668 | extent_num_bytes)) { | |
4669 | should_end = 1; | |
4670 | } | |
4671 | if (btrfs_should_throttle_delayed_refs(trans, | |
2ff7e61e | 4672 | fs_info)) |
28f75a0e | 4673 | should_throttle = 1; |
28f75a0e | 4674 | } |
39279cc3 | 4675 | } |
85e21bac | 4676 | |
8082510e YZ |
4677 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4678 | break; | |
4679 | ||
4680 | if (path->slots[0] == 0 || | |
1262133b | 4681 | path->slots[0] != pending_del_slot || |
28f75a0e | 4682 | should_throttle || should_end) { |
8082510e YZ |
4683 | if (pending_del_nr) { |
4684 | ret = btrfs_del_items(trans, root, path, | |
4685 | pending_del_slot, | |
4686 | pending_del_nr); | |
79787eaa | 4687 | if (ret) { |
66642832 | 4688 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4689 | goto error; |
4690 | } | |
8082510e YZ |
4691 | pending_del_nr = 0; |
4692 | } | |
b3b4aa74 | 4693 | btrfs_release_path(path); |
28f75a0e | 4694 | if (should_throttle) { |
1262133b JB |
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, |
4699 | fs_info, | |
4700 | updates * 2); | |
1262133b JB |
4701 | if (ret && !err) |
4702 | err = ret; | |
4703 | } | |
4704 | } | |
28f75a0e CM |
4705 | /* |
4706 | * if we failed to refill our space rsv, bail out | |
4707 | * and let the transaction restart | |
4708 | */ | |
4709 | if (should_end) { | |
4710 | err = -EAGAIN; | |
4711 | goto error; | |
4712 | } | |
85e21bac | 4713 | goto search_again; |
8082510e YZ |
4714 | } else { |
4715 | path->slots[0]--; | |
85e21bac | 4716 | } |
39279cc3 | 4717 | } |
8082510e | 4718 | out: |
85e21bac CM |
4719 | if (pending_del_nr) { |
4720 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4721 | pending_del_nr); | |
79787eaa | 4722 | if (ret) |
66642832 | 4723 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4724 | } |
79787eaa | 4725 | error: |
76b42abb FM |
4726 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4727 | ASSERT(last_size >= new_size); | |
4728 | if (!err && last_size > new_size) | |
4729 | last_size = new_size; | |
7f4f6e0a | 4730 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4731 | } |
28ed1345 | 4732 | |
39279cc3 | 4733 | btrfs_free_path(path); |
28ed1345 | 4734 | |
ee22184b | 4735 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4736 | unsigned long updates = trans->delayed_ref_updates; |
4737 | if (updates) { | |
4738 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4739 | ret = btrfs_run_delayed_refs(trans, fs_info, |
4740 | updates * 2); | |
28ed1345 CM |
4741 | if (ret && !err) |
4742 | err = ret; | |
4743 | } | |
4744 | } | |
8082510e | 4745 | return err; |
39279cc3 CM |
4746 | } |
4747 | ||
4748 | /* | |
9703fefe | 4749 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4750 | * @inode - inode that we're zeroing |
4751 | * @from - the offset to start zeroing | |
4752 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4753 | * offset | |
4754 | * @front - zero up to the offset instead of from the offset on | |
4755 | * | |
9703fefe | 4756 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4757 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4758 | */ |
9703fefe | 4759 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4760 | int front) |
39279cc3 | 4761 | { |
0b246afa | 4762 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4763 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4764 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4765 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4766 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4767 | char *kaddr; |
0b246afa | 4768 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4769 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4770 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4771 | struct page *page; |
3b16a4e3 | 4772 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4773 | int ret = 0; |
9703fefe CR |
4774 | u64 block_start; |
4775 | u64 block_end; | |
39279cc3 | 4776 | |
2aaa6655 JB |
4777 | if ((offset & (blocksize - 1)) == 0 && |
4778 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4779 | goto out; |
9703fefe | 4780 | |
7cf5b976 | 4781 | ret = btrfs_delalloc_reserve_space(inode, |
9703fefe | 4782 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4783 | if (ret) |
4784 | goto out; | |
39279cc3 | 4785 | |
211c17f5 | 4786 | again: |
3b16a4e3 | 4787 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4788 | if (!page) { |
7cf5b976 | 4789 | btrfs_delalloc_release_space(inode, |
9703fefe CR |
4790 | round_down(from, blocksize), |
4791 | blocksize); | |
ac6a2b36 | 4792 | ret = -ENOMEM; |
39279cc3 | 4793 | goto out; |
5d5e103a | 4794 | } |
e6dcd2dc | 4795 | |
9703fefe CR |
4796 | block_start = round_down(from, blocksize); |
4797 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4798 | |
39279cc3 | 4799 | if (!PageUptodate(page)) { |
9ebefb18 | 4800 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4801 | lock_page(page); |
211c17f5 CM |
4802 | if (page->mapping != mapping) { |
4803 | unlock_page(page); | |
09cbfeaf | 4804 | put_page(page); |
211c17f5 CM |
4805 | goto again; |
4806 | } | |
39279cc3 CM |
4807 | if (!PageUptodate(page)) { |
4808 | ret = -EIO; | |
89642229 | 4809 | goto out_unlock; |
39279cc3 CM |
4810 | } |
4811 | } | |
211c17f5 | 4812 | wait_on_page_writeback(page); |
e6dcd2dc | 4813 | |
9703fefe | 4814 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4815 | set_page_extent_mapped(page); |
4816 | ||
9703fefe | 4817 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4818 | if (ordered) { |
9703fefe | 4819 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4820 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4821 | unlock_page(page); |
09cbfeaf | 4822 | put_page(page); |
eb84ae03 | 4823 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4824 | btrfs_put_ordered_extent(ordered); |
4825 | goto again; | |
4826 | } | |
4827 | ||
9703fefe | 4828 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4829 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4830 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4831 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4832 | |
9703fefe | 4833 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4834 | &cached_state, 0); |
9ed74f2d | 4835 | if (ret) { |
9703fefe | 4836 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4837 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4838 | goto out_unlock; |
4839 | } | |
4840 | ||
9703fefe | 4841 | if (offset != blocksize) { |
2aaa6655 | 4842 | if (!len) |
9703fefe | 4843 | len = blocksize - offset; |
e6dcd2dc | 4844 | kaddr = kmap(page); |
2aaa6655 | 4845 | if (front) |
9703fefe CR |
4846 | memset(kaddr + (block_start - page_offset(page)), |
4847 | 0, offset); | |
2aaa6655 | 4848 | else |
9703fefe CR |
4849 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4850 | 0, len); | |
e6dcd2dc CM |
4851 | flush_dcache_page(page); |
4852 | kunmap(page); | |
4853 | } | |
247e743c | 4854 | ClearPageChecked(page); |
e6dcd2dc | 4855 | set_page_dirty(page); |
9703fefe | 4856 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4857 | GFP_NOFS); |
39279cc3 | 4858 | |
89642229 | 4859 | out_unlock: |
5d5e103a | 4860 | if (ret) |
9703fefe CR |
4861 | btrfs_delalloc_release_space(inode, block_start, |
4862 | blocksize); | |
39279cc3 | 4863 | unlock_page(page); |
09cbfeaf | 4864 | put_page(page); |
39279cc3 CM |
4865 | out: |
4866 | return ret; | |
4867 | } | |
4868 | ||
16e7549f JB |
4869 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4870 | u64 offset, u64 len) | |
4871 | { | |
0b246afa | 4872 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4873 | struct btrfs_trans_handle *trans; |
4874 | int ret; | |
4875 | ||
4876 | /* | |
4877 | * Still need to make sure the inode looks like it's been updated so | |
4878 | * that any holes get logged if we fsync. | |
4879 | */ | |
0b246afa JM |
4880 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4881 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4882 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4883 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4884 | return 0; | |
4885 | } | |
4886 | ||
4887 | /* | |
4888 | * 1 - for the one we're dropping | |
4889 | * 1 - for the one we're adding | |
4890 | * 1 - for updating the inode. | |
4891 | */ | |
4892 | trans = btrfs_start_transaction(root, 3); | |
4893 | if (IS_ERR(trans)) | |
4894 | return PTR_ERR(trans); | |
4895 | ||
4896 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4897 | if (ret) { | |
66642832 | 4898 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4899 | btrfs_end_transaction(trans); |
16e7549f JB |
4900 | return ret; |
4901 | } | |
4902 | ||
f85b7379 DS |
4903 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4904 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4905 | if (ret) |
66642832 | 4906 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4907 | else |
4908 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4909 | btrfs_end_transaction(trans); |
16e7549f JB |
4910 | return ret; |
4911 | } | |
4912 | ||
695a0d0d JB |
4913 | /* |
4914 | * This function puts in dummy file extents for the area we're creating a hole | |
4915 | * for. So if we are truncating this file to a larger size we need to insert | |
4916 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4917 | * the range between oldsize and size | |
4918 | */ | |
a41ad394 | 4919 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4920 | { |
0b246afa | 4921 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4922 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4923 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4924 | struct extent_map *em = NULL; |
2ac55d41 | 4925 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4926 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4927 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4928 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4929 | u64 last_byte; |
4930 | u64 cur_offset; | |
4931 | u64 hole_size; | |
9ed74f2d | 4932 | int err = 0; |
39279cc3 | 4933 | |
a71754fc | 4934 | /* |
9703fefe CR |
4935 | * If our size started in the middle of a block we need to zero out the |
4936 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4937 | * expose stale data. |
4938 | */ | |
9703fefe | 4939 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4940 | if (err) |
4941 | return err; | |
4942 | ||
9036c102 YZ |
4943 | if (size <= hole_start) |
4944 | return 0; | |
4945 | ||
9036c102 YZ |
4946 | while (1) { |
4947 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4948 | |
ff13db41 | 4949 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4950 | &cached_state); |
a776c6fa | 4951 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
fa7c1494 | 4952 | block_end - hole_start); |
9036c102 YZ |
4953 | if (!ordered) |
4954 | break; | |
2ac55d41 JB |
4955 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4956 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4957 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4958 | btrfs_put_ordered_extent(ordered); |
4959 | } | |
39279cc3 | 4960 | |
9036c102 YZ |
4961 | cur_offset = hole_start; |
4962 | while (1) { | |
fc4f21b1 | 4963 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 4964 | block_end - cur_offset, 0); |
79787eaa JM |
4965 | if (IS_ERR(em)) { |
4966 | err = PTR_ERR(em); | |
f2767956 | 4967 | em = NULL; |
79787eaa JM |
4968 | break; |
4969 | } | |
9036c102 | 4970 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4971 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 4972 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4973 | struct extent_map *hole_em; |
9036c102 | 4974 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4975 | |
16e7549f JB |
4976 | err = maybe_insert_hole(root, inode, cur_offset, |
4977 | hole_size); | |
4978 | if (err) | |
3893e33b | 4979 | break; |
dcdbc059 | 4980 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4981 | cur_offset + hole_size - 1, 0); |
4982 | hole_em = alloc_extent_map(); | |
4983 | if (!hole_em) { | |
4984 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4985 | &BTRFS_I(inode)->runtime_flags); | |
4986 | goto next; | |
4987 | } | |
4988 | hole_em->start = cur_offset; | |
4989 | hole_em->len = hole_size; | |
4990 | hole_em->orig_start = cur_offset; | |
8082510e | 4991 | |
5dc562c5 JB |
4992 | hole_em->block_start = EXTENT_MAP_HOLE; |
4993 | hole_em->block_len = 0; | |
b4939680 | 4994 | hole_em->orig_block_len = 0; |
cc95bef6 | 4995 | hole_em->ram_bytes = hole_size; |
0b246afa | 4996 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 4997 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4998 | hole_em->generation = fs_info->generation; |
8082510e | 4999 | |
5dc562c5 JB |
5000 | while (1) { |
5001 | write_lock(&em_tree->lock); | |
09a2a8f9 | 5002 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
5003 | write_unlock(&em_tree->lock); |
5004 | if (err != -EEXIST) | |
5005 | break; | |
dcdbc059 NB |
5006 | btrfs_drop_extent_cache(BTRFS_I(inode), |
5007 | cur_offset, | |
5dc562c5 JB |
5008 | cur_offset + |
5009 | hole_size - 1, 0); | |
5010 | } | |
5011 | free_extent_map(hole_em); | |
9036c102 | 5012 | } |
16e7549f | 5013 | next: |
9036c102 | 5014 | free_extent_map(em); |
a22285a6 | 5015 | em = NULL; |
9036c102 | 5016 | cur_offset = last_byte; |
8082510e | 5017 | if (cur_offset >= block_end) |
9036c102 YZ |
5018 | break; |
5019 | } | |
a22285a6 | 5020 | free_extent_map(em); |
2ac55d41 JB |
5021 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
5022 | GFP_NOFS); | |
9036c102 YZ |
5023 | return err; |
5024 | } | |
39279cc3 | 5025 | |
3972f260 | 5026 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5027 | { |
f4a2f4c5 MX |
5028 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5029 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5030 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5031 | loff_t newsize = attr->ia_size; |
5032 | int mask = attr->ia_valid; | |
8082510e YZ |
5033 | int ret; |
5034 | ||
3972f260 ES |
5035 | /* |
5036 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5037 | * special case where we need to update the times despite not having | |
5038 | * these flags set. For all other operations the VFS set these flags | |
5039 | * explicitly if it wants a timestamp update. | |
5040 | */ | |
dff6efc3 CH |
5041 | if (newsize != oldsize) { |
5042 | inode_inc_iversion(inode); | |
5043 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5044 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5045 | current_time(inode); |
dff6efc3 | 5046 | } |
3972f260 | 5047 | |
a41ad394 | 5048 | if (newsize > oldsize) { |
9ea24bbe FM |
5049 | /* |
5050 | * Don't do an expanding truncate while snapshoting is ongoing. | |
5051 | * This is to ensure the snapshot captures a fully consistent | |
5052 | * state of this file - if the snapshot captures this expanding | |
5053 | * truncation, it must capture all writes that happened before | |
5054 | * this truncation. | |
5055 | */ | |
0bc19f90 | 5056 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5057 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
5058 | if (ret) { |
5059 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 5060 | return ret; |
9ea24bbe | 5061 | } |
8082510e | 5062 | |
f4a2f4c5 | 5063 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
5064 | if (IS_ERR(trans)) { |
5065 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 5066 | return PTR_ERR(trans); |
9ea24bbe | 5067 | } |
f4a2f4c5 MX |
5068 | |
5069 | i_size_write(inode, newsize); | |
5070 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5071 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5072 | ret = btrfs_update_inode(trans, root, inode); |
9ea24bbe | 5073 | btrfs_end_write_no_snapshoting(root); |
3a45bb20 | 5074 | btrfs_end_transaction(trans); |
a41ad394 | 5075 | } else { |
8082510e | 5076 | |
a41ad394 JB |
5077 | /* |
5078 | * We're truncating a file that used to have good data down to | |
5079 | * zero. Make sure it gets into the ordered flush list so that | |
5080 | * any new writes get down to disk quickly. | |
5081 | */ | |
5082 | if (newsize == 0) | |
72ac3c0d JB |
5083 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5084 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5085 | |
f3fe820c JB |
5086 | /* |
5087 | * 1 for the orphan item we're going to add | |
5088 | * 1 for the orphan item deletion. | |
5089 | */ | |
5090 | trans = btrfs_start_transaction(root, 2); | |
5091 | if (IS_ERR(trans)) | |
5092 | return PTR_ERR(trans); | |
5093 | ||
5094 | /* | |
5095 | * We need to do this in case we fail at _any_ point during the | |
5096 | * actual truncate. Once we do the truncate_setsize we could | |
5097 | * invalidate pages which forces any outstanding ordered io to | |
5098 | * be instantly completed which will give us extents that need | |
5099 | * to be truncated. If we fail to get an orphan inode down we | |
5100 | * could have left over extents that were never meant to live, | |
01327610 | 5101 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5102 | * will be consistent. |
5103 | */ | |
73f2e545 | 5104 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 5105 | btrfs_end_transaction(trans); |
f3fe820c JB |
5106 | if (ret) |
5107 | return ret; | |
5108 | ||
a41ad394 JB |
5109 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5110 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5111 | |
5112 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
abcefb1e | 5113 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5114 | inode_dio_wait(inode); |
0b581701 | 5115 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5116 | |
a41ad394 | 5117 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5118 | if (ret && inode->i_nlink) { |
5119 | int err; | |
5120 | ||
19fd2df5 LB |
5121 | /* To get a stable disk_i_size */ |
5122 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5123 | if (err) { | |
3d6ae7bb | 5124 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
19fd2df5 LB |
5125 | return err; |
5126 | } | |
5127 | ||
7f4f6e0a JB |
5128 | /* |
5129 | * failed to truncate, disk_i_size is only adjusted down | |
5130 | * as we remove extents, so it should represent the true | |
5131 | * size of the inode, so reset the in memory size and | |
5132 | * delete our orphan entry. | |
5133 | */ | |
5134 | trans = btrfs_join_transaction(root); | |
5135 | if (IS_ERR(trans)) { | |
3d6ae7bb | 5136 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7f4f6e0a JB |
5137 | return ret; |
5138 | } | |
5139 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
3d6ae7bb | 5140 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
7f4f6e0a | 5141 | if (err) |
66642832 | 5142 | btrfs_abort_transaction(trans, err); |
3a45bb20 | 5143 | btrfs_end_transaction(trans); |
7f4f6e0a | 5144 | } |
8082510e YZ |
5145 | } |
5146 | ||
a41ad394 | 5147 | return ret; |
8082510e YZ |
5148 | } |
5149 | ||
9036c102 YZ |
5150 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5151 | { | |
2b0143b5 | 5152 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5153 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5154 | int err; |
39279cc3 | 5155 | |
b83cc969 LZ |
5156 | if (btrfs_root_readonly(root)) |
5157 | return -EROFS; | |
5158 | ||
31051c85 | 5159 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5160 | if (err) |
5161 | return err; | |
2bf5a725 | 5162 | |
5a3f23d5 | 5163 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5164 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5165 | if (err) |
5166 | return err; | |
39279cc3 | 5167 | } |
9036c102 | 5168 | |
1025774c CH |
5169 | if (attr->ia_valid) { |
5170 | setattr_copy(inode, attr); | |
0c4d2d95 | 5171 | inode_inc_iversion(inode); |
22c44fe6 | 5172 | err = btrfs_dirty_inode(inode); |
1025774c | 5173 | |
22c44fe6 | 5174 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5175 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5176 | } |
33268eaf | 5177 | |
39279cc3 CM |
5178 | return err; |
5179 | } | |
61295eb8 | 5180 | |
131e404a FDBM |
5181 | /* |
5182 | * While truncating the inode pages during eviction, we get the VFS calling | |
5183 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5184 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5185 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5186 | * extent_state structures over and over, wasting lots of time. | |
5187 | * | |
5188 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5189 | * those expensive operations on a per page basis and do only the ordered io | |
5190 | * finishing, while we release here the extent_map and extent_state structures, | |
5191 | * without the excessive merging and splitting. | |
5192 | */ | |
5193 | static void evict_inode_truncate_pages(struct inode *inode) | |
5194 | { | |
5195 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5196 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5197 | struct rb_node *node; | |
5198 | ||
5199 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5200 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5201 | |
5202 | write_lock(&map_tree->lock); | |
5203 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5204 | struct extent_map *em; | |
5205 | ||
5206 | node = rb_first(&map_tree->map); | |
5207 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5208 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5209 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5210 | remove_extent_mapping(map_tree, em); |
5211 | free_extent_map(em); | |
7064dd5c FM |
5212 | if (need_resched()) { |
5213 | write_unlock(&map_tree->lock); | |
5214 | cond_resched(); | |
5215 | write_lock(&map_tree->lock); | |
5216 | } | |
131e404a FDBM |
5217 | } |
5218 | write_unlock(&map_tree->lock); | |
5219 | ||
6ca07097 FM |
5220 | /* |
5221 | * Keep looping until we have no more ranges in the io tree. | |
5222 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5223 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5224 | * still in progress (unlocked the pages in the bio but did not yet | |
5225 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5226 | * ranges can still be locked and eviction started because before |
5227 | * submitting those bios, which are executed by a separate task (work | |
5228 | * queue kthread), inode references (inode->i_count) were not taken | |
5229 | * (which would be dropped in the end io callback of each bio). | |
5230 | * Therefore here we effectively end up waiting for those bios and | |
5231 | * anyone else holding locked ranges without having bumped the inode's | |
5232 | * reference count - if we don't do it, when they access the inode's | |
5233 | * io_tree to unlock a range it may be too late, leading to an | |
5234 | * use-after-free issue. | |
5235 | */ | |
131e404a FDBM |
5236 | spin_lock(&io_tree->lock); |
5237 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5238 | struct extent_state *state; | |
5239 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5240 | u64 start; |
5241 | u64 end; | |
131e404a FDBM |
5242 | |
5243 | node = rb_first(&io_tree->state); | |
5244 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5245 | start = state->start; |
5246 | end = state->end; | |
131e404a FDBM |
5247 | spin_unlock(&io_tree->lock); |
5248 | ||
ff13db41 | 5249 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5250 | |
5251 | /* | |
5252 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5253 | * and its reserved space won't be freed by delayed_ref. | |
5254 | * So we need to free its reserved space here. | |
5255 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5256 | * | |
5257 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5258 | */ | |
5259 | if (state->state & EXTENT_DELALLOC) | |
5260 | btrfs_qgroup_free_data(inode, start, end - start + 1); | |
5261 | ||
6ca07097 | 5262 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5263 | EXTENT_LOCKED | EXTENT_DIRTY | |
5264 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5265 | EXTENT_DEFRAG, 1, 1, | |
5266 | &cached_state, GFP_NOFS); | |
131e404a | 5267 | |
7064dd5c | 5268 | cond_resched(); |
131e404a FDBM |
5269 | spin_lock(&io_tree->lock); |
5270 | } | |
5271 | spin_unlock(&io_tree->lock); | |
5272 | } | |
5273 | ||
bd555975 | 5274 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5275 | { |
0b246afa | 5276 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5277 | struct btrfs_trans_handle *trans; |
5278 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5279 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5280 | int steal_from_global = 0; |
3d48d981 | 5281 | u64 min_size; |
39279cc3 CM |
5282 | int ret; |
5283 | ||
1abe9b8a | 5284 | trace_btrfs_inode_evict(inode); |
5285 | ||
3d48d981 NB |
5286 | if (!root) { |
5287 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5288 | return; | |
5289 | } | |
5290 | ||
0b246afa | 5291 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
3d48d981 | 5292 | |
131e404a FDBM |
5293 | evict_inode_truncate_pages(inode); |
5294 | ||
69e9c6c6 SB |
5295 | if (inode->i_nlink && |
5296 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5297 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5298 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5299 | goto no_delete; |
5300 | ||
39279cc3 | 5301 | if (is_bad_inode(inode)) { |
3d6ae7bb | 5302 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
39279cc3 CM |
5303 | goto no_delete; |
5304 | } | |
bd555975 | 5305 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5306 | if (!special_file(inode->i_mode)) |
5307 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5308 | |
7ab7956e | 5309 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5310 | |
0b246afa | 5311 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
6bf02314 | 5312 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5313 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5314 | goto no_delete; |
5315 | } | |
5316 | ||
76dda93c | 5317 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5318 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5319 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5320 | goto no_delete; |
5321 | } | |
5322 | ||
aa79021f | 5323 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
0e8c36a9 | 5324 | if (ret) { |
3d6ae7bb | 5325 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
0e8c36a9 MX |
5326 | goto no_delete; |
5327 | } | |
5328 | ||
2ff7e61e | 5329 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
4289a667 | 5330 | if (!rsv) { |
3d6ae7bb | 5331 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
4289a667 JB |
5332 | goto no_delete; |
5333 | } | |
4a338542 | 5334 | rsv->size = min_size; |
ca7e70f5 | 5335 | rsv->failfast = 1; |
0b246afa | 5336 | global_rsv = &fs_info->global_block_rsv; |
4289a667 | 5337 | |
6ef06d27 | 5338 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5339 | |
4289a667 | 5340 | /* |
8407aa46 MX |
5341 | * This is a bit simpler than btrfs_truncate since we've already |
5342 | * reserved our space for our orphan item in the unlink, so we just | |
5343 | * need to reserve some slack space in case we add bytes and update | |
5344 | * inode item when doing the truncate. | |
4289a667 | 5345 | */ |
8082510e | 5346 | while (1) { |
08e007d2 MX |
5347 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5348 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5349 | |
5350 | /* | |
5351 | * Try and steal from the global reserve since we will | |
5352 | * likely not use this space anyway, we want to try as | |
5353 | * hard as possible to get this to work. | |
5354 | */ | |
5355 | if (ret) | |
3bce876f JB |
5356 | steal_from_global++; |
5357 | else | |
5358 | steal_from_global = 0; | |
5359 | ret = 0; | |
d68fc57b | 5360 | |
3bce876f JB |
5361 | /* |
5362 | * steal_from_global == 0: we reserved stuff, hooray! | |
5363 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5364 | * steal_from_global == 2: we've committed, still not a lot of | |
5365 | * room but maybe we'll have room in the global reserve this | |
5366 | * time. | |
5367 | * steal_from_global == 3: abandon all hope! | |
5368 | */ | |
5369 | if (steal_from_global > 2) { | |
0b246afa JM |
5370 | btrfs_warn(fs_info, |
5371 | "Could not get space for a delete, will truncate on mount %d", | |
5372 | ret); | |
3d6ae7bb | 5373 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5374 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5375 | goto no_delete; |
d68fc57b | 5376 | } |
7b128766 | 5377 | |
0e8c36a9 | 5378 | trans = btrfs_join_transaction(root); |
4289a667 | 5379 | if (IS_ERR(trans)) { |
3d6ae7bb | 5380 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5381 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5382 | goto no_delete; |
d68fc57b | 5383 | } |
7b128766 | 5384 | |
3bce876f | 5385 | /* |
01327610 | 5386 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5387 | * sure there is room to do it, if not we need to commit and try |
5388 | * again. | |
5389 | */ | |
5390 | if (steal_from_global) { | |
2ff7e61e | 5391 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
3bce876f | 5392 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
25d609f8 | 5393 | min_size, 0); |
3bce876f JB |
5394 | else |
5395 | ret = -ENOSPC; | |
5396 | } | |
5397 | ||
5398 | /* | |
5399 | * Couldn't steal from the global reserve, we have too much | |
5400 | * pending stuff built up, commit the transaction and try it | |
5401 | * again. | |
5402 | */ | |
5403 | if (ret) { | |
3a45bb20 | 5404 | ret = btrfs_commit_transaction(trans); |
3bce876f | 5405 | if (ret) { |
3d6ae7bb | 5406 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5407 | btrfs_free_block_rsv(fs_info, rsv); |
3bce876f JB |
5408 | goto no_delete; |
5409 | } | |
5410 | continue; | |
5411 | } else { | |
5412 | steal_from_global = 0; | |
5413 | } | |
5414 | ||
4289a667 JB |
5415 | trans->block_rsv = rsv; |
5416 | ||
d68fc57b | 5417 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5418 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5419 | break; |
85e21bac | 5420 | |
0b246afa | 5421 | trans->block_rsv = &fs_info->trans_block_rsv; |
3a45bb20 | 5422 | btrfs_end_transaction(trans); |
8082510e | 5423 | trans = NULL; |
2ff7e61e | 5424 | btrfs_btree_balance_dirty(fs_info); |
8082510e | 5425 | } |
5f39d397 | 5426 | |
2ff7e61e | 5427 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5428 | |
4ef31a45 JB |
5429 | /* |
5430 | * Errors here aren't a big deal, it just means we leave orphan items | |
5431 | * in the tree. They will be cleaned up on the next mount. | |
5432 | */ | |
8082510e | 5433 | if (ret == 0) { |
4289a667 | 5434 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 5435 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
4ef31a45 | 5436 | } else { |
3d6ae7bb | 5437 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
8082510e | 5438 | } |
54aa1f4d | 5439 | |
0b246afa JM |
5440 | trans->block_rsv = &fs_info->trans_block_rsv; |
5441 | if (!(root == fs_info->tree_root || | |
581bb050 | 5442 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5443 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5444 | |
3a45bb20 | 5445 | btrfs_end_transaction(trans); |
2ff7e61e | 5446 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 5447 | no_delete: |
f48d1cf5 | 5448 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5449 | clear_inode(inode); |
39279cc3 CM |
5450 | } |
5451 | ||
5452 | /* | |
5453 | * this returns the key found in the dir entry in the location pointer. | |
5454 | * If no dir entries were found, location->objectid is 0. | |
5455 | */ | |
5456 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5457 | struct btrfs_key *location) | |
5458 | { | |
5459 | const char *name = dentry->d_name.name; | |
5460 | int namelen = dentry->d_name.len; | |
5461 | struct btrfs_dir_item *di; | |
5462 | struct btrfs_path *path; | |
5463 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5464 | int ret = 0; |
39279cc3 CM |
5465 | |
5466 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5467 | if (!path) |
5468 | return -ENOMEM; | |
3954401f | 5469 | |
f85b7379 DS |
5470 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5471 | name, namelen, 0); | |
0d9f7f3e Y |
5472 | if (IS_ERR(di)) |
5473 | ret = PTR_ERR(di); | |
d397712b | 5474 | |
c704005d | 5475 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5476 | goto out_err; |
d397712b | 5477 | |
5f39d397 | 5478 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5479 | out: |
39279cc3 CM |
5480 | btrfs_free_path(path); |
5481 | return ret; | |
3954401f CM |
5482 | out_err: |
5483 | location->objectid = 0; | |
5484 | goto out; | |
39279cc3 CM |
5485 | } |
5486 | ||
5487 | /* | |
5488 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5489 | * needs to be changed to reflect the root directory of the tree root. This | |
5490 | * is kind of like crossing a mount point. | |
5491 | */ | |
2ff7e61e | 5492 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5493 | struct inode *dir, |
5494 | struct dentry *dentry, | |
5495 | struct btrfs_key *location, | |
5496 | struct btrfs_root **sub_root) | |
39279cc3 | 5497 | { |
4df27c4d YZ |
5498 | struct btrfs_path *path; |
5499 | struct btrfs_root *new_root; | |
5500 | struct btrfs_root_ref *ref; | |
5501 | struct extent_buffer *leaf; | |
1d4c08e0 | 5502 | struct btrfs_key key; |
4df27c4d YZ |
5503 | int ret; |
5504 | int err = 0; | |
39279cc3 | 5505 | |
4df27c4d YZ |
5506 | path = btrfs_alloc_path(); |
5507 | if (!path) { | |
5508 | err = -ENOMEM; | |
5509 | goto out; | |
5510 | } | |
39279cc3 | 5511 | |
4df27c4d | 5512 | err = -ENOENT; |
1d4c08e0 DS |
5513 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5514 | key.type = BTRFS_ROOT_REF_KEY; | |
5515 | key.offset = location->objectid; | |
5516 | ||
0b246afa | 5517 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5518 | if (ret) { |
5519 | if (ret < 0) | |
5520 | err = ret; | |
5521 | goto out; | |
5522 | } | |
39279cc3 | 5523 | |
4df27c4d YZ |
5524 | leaf = path->nodes[0]; |
5525 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5526 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5527 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5528 | goto out; | |
39279cc3 | 5529 | |
4df27c4d YZ |
5530 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5531 | (unsigned long)(ref + 1), | |
5532 | dentry->d_name.len); | |
5533 | if (ret) | |
5534 | goto out; | |
5535 | ||
b3b4aa74 | 5536 | btrfs_release_path(path); |
4df27c4d | 5537 | |
0b246afa | 5538 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5539 | if (IS_ERR(new_root)) { |
5540 | err = PTR_ERR(new_root); | |
5541 | goto out; | |
5542 | } | |
5543 | ||
4df27c4d YZ |
5544 | *sub_root = new_root; |
5545 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5546 | location->type = BTRFS_INODE_ITEM_KEY; | |
5547 | location->offset = 0; | |
5548 | err = 0; | |
5549 | out: | |
5550 | btrfs_free_path(path); | |
5551 | return err; | |
39279cc3 CM |
5552 | } |
5553 | ||
5d4f98a2 YZ |
5554 | static void inode_tree_add(struct inode *inode) |
5555 | { | |
5556 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5557 | struct btrfs_inode *entry; | |
03e860bd NP |
5558 | struct rb_node **p; |
5559 | struct rb_node *parent; | |
cef21937 | 5560 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5561 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5562 | |
1d3382cb | 5563 | if (inode_unhashed(inode)) |
76dda93c | 5564 | return; |
e1409cef | 5565 | parent = NULL; |
5d4f98a2 | 5566 | spin_lock(&root->inode_lock); |
e1409cef | 5567 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5568 | while (*p) { |
5569 | parent = *p; | |
5570 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5571 | ||
4a0cc7ca | 5572 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5573 | p = &parent->rb_left; |
4a0cc7ca | 5574 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5575 | p = &parent->rb_right; |
5d4f98a2 YZ |
5576 | else { |
5577 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5578 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5579 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5580 | RB_CLEAR_NODE(parent); |
5581 | spin_unlock(&root->inode_lock); | |
cef21937 | 5582 | return; |
5d4f98a2 YZ |
5583 | } |
5584 | } | |
cef21937 FDBM |
5585 | rb_link_node(new, parent, p); |
5586 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5587 | spin_unlock(&root->inode_lock); |
5588 | } | |
5589 | ||
5590 | static void inode_tree_del(struct inode *inode) | |
5591 | { | |
0b246afa | 5592 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5593 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5594 | int empty = 0; |
5d4f98a2 | 5595 | |
03e860bd | 5596 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5597 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5598 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5599 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5600 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5601 | } |
03e860bd | 5602 | spin_unlock(&root->inode_lock); |
76dda93c | 5603 | |
69e9c6c6 | 5604 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5605 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5606 | spin_lock(&root->inode_lock); |
5607 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5608 | spin_unlock(&root->inode_lock); | |
5609 | if (empty) | |
5610 | btrfs_add_dead_root(root); | |
5611 | } | |
5612 | } | |
5613 | ||
143bede5 | 5614 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c | 5615 | { |
0b246afa | 5616 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c YZ |
5617 | struct rb_node *node; |
5618 | struct rb_node *prev; | |
5619 | struct btrfs_inode *entry; | |
5620 | struct inode *inode; | |
5621 | u64 objectid = 0; | |
5622 | ||
0b246afa | 5623 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
7813b3db | 5624 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
76dda93c YZ |
5625 | |
5626 | spin_lock(&root->inode_lock); | |
5627 | again: | |
5628 | node = root->inode_tree.rb_node; | |
5629 | prev = NULL; | |
5630 | while (node) { | |
5631 | prev = node; | |
5632 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5633 | ||
4a0cc7ca | 5634 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c | 5635 | node = node->rb_left; |
4a0cc7ca | 5636 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c YZ |
5637 | node = node->rb_right; |
5638 | else | |
5639 | break; | |
5640 | } | |
5641 | if (!node) { | |
5642 | while (prev) { | |
5643 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5644 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
76dda93c YZ |
5645 | node = prev; |
5646 | break; | |
5647 | } | |
5648 | prev = rb_next(prev); | |
5649 | } | |
5650 | } | |
5651 | while (node) { | |
5652 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5653 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
76dda93c YZ |
5654 | inode = igrab(&entry->vfs_inode); |
5655 | if (inode) { | |
5656 | spin_unlock(&root->inode_lock); | |
5657 | if (atomic_read(&inode->i_count) > 1) | |
5658 | d_prune_aliases(inode); | |
5659 | /* | |
45321ac5 | 5660 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5661 | * the inode cache when its usage count |
5662 | * hits zero. | |
5663 | */ | |
5664 | iput(inode); | |
5665 | cond_resched(); | |
5666 | spin_lock(&root->inode_lock); | |
5667 | goto again; | |
5668 | } | |
5669 | ||
5670 | if (cond_resched_lock(&root->inode_lock)) | |
5671 | goto again; | |
5672 | ||
5673 | node = rb_next(node); | |
5674 | } | |
5675 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5676 | } |
5677 | ||
e02119d5 CM |
5678 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5679 | { | |
5680 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5681 | inode->i_ino = args->location->objectid; |
5682 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5683 | sizeof(*args->location)); | |
e02119d5 | 5684 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5685 | return 0; |
5686 | } | |
5687 | ||
5688 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5689 | { | |
5690 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5691 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5692 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5693 | } |
5694 | ||
5d4f98a2 | 5695 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5696 | struct btrfs_key *location, |
5d4f98a2 | 5697 | struct btrfs_root *root) |
39279cc3 CM |
5698 | { |
5699 | struct inode *inode; | |
5700 | struct btrfs_iget_args args; | |
90d3e592 | 5701 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5702 | |
90d3e592 | 5703 | args.location = location; |
39279cc3 CM |
5704 | args.root = root; |
5705 | ||
778ba82b | 5706 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5707 | btrfs_init_locked_inode, |
5708 | (void *)&args); | |
5709 | return inode; | |
5710 | } | |
5711 | ||
1a54ef8c BR |
5712 | /* Get an inode object given its location and corresponding root. |
5713 | * Returns in *is_new if the inode was read from disk | |
5714 | */ | |
5715 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5716 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5717 | { |
5718 | struct inode *inode; | |
5719 | ||
90d3e592 | 5720 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5721 | if (!inode) |
5d4f98a2 | 5722 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5723 | |
5724 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5725 | int ret; |
5726 | ||
5727 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5728 | if (!is_bad_inode(inode)) { |
5729 | inode_tree_add(inode); | |
5730 | unlock_new_inode(inode); | |
5731 | if (new) | |
5732 | *new = 1; | |
5733 | } else { | |
e0b6d65b ST |
5734 | unlock_new_inode(inode); |
5735 | iput(inode); | |
67710892 FM |
5736 | ASSERT(ret < 0); |
5737 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5738 | } |
5739 | } | |
5740 | ||
1a54ef8c BR |
5741 | return inode; |
5742 | } | |
5743 | ||
4df27c4d YZ |
5744 | static struct inode *new_simple_dir(struct super_block *s, |
5745 | struct btrfs_key *key, | |
5746 | struct btrfs_root *root) | |
5747 | { | |
5748 | struct inode *inode = new_inode(s); | |
5749 | ||
5750 | if (!inode) | |
5751 | return ERR_PTR(-ENOMEM); | |
5752 | ||
4df27c4d YZ |
5753 | BTRFS_I(inode)->root = root; |
5754 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5755 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5756 | |
5757 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5758 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5759 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5760 | inode->i_fop = &simple_dir_operations; |
5761 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5762 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5763 | inode->i_atime = inode->i_mtime; |
5764 | inode->i_ctime = inode->i_mtime; | |
5765 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5766 | |
5767 | return inode; | |
5768 | } | |
5769 | ||
3de4586c | 5770 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5771 | { |
0b246afa | 5772 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5773 | struct inode *inode; |
4df27c4d | 5774 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5775 | struct btrfs_root *sub_root = root; |
5776 | struct btrfs_key location; | |
76dda93c | 5777 | int index; |
b4aff1f8 | 5778 | int ret = 0; |
39279cc3 CM |
5779 | |
5780 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5781 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5782 | |
39e3c955 | 5783 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5784 | if (ret < 0) |
5785 | return ERR_PTR(ret); | |
5f39d397 | 5786 | |
4df27c4d | 5787 | if (location.objectid == 0) |
5662344b | 5788 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5789 | |
5790 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5791 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5792 | return inode; |
5793 | } | |
5794 | ||
5795 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5796 | ||
0b246afa | 5797 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5798 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5799 | &location, &sub_root); |
5800 | if (ret < 0) { | |
5801 | if (ret != -ENOENT) | |
5802 | inode = ERR_PTR(ret); | |
5803 | else | |
5804 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5805 | } else { | |
73f73415 | 5806 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5807 | } |
0b246afa | 5808 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5809 | |
34d19bad | 5810 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5811 | down_read(&fs_info->cleanup_work_sem); |
c71bf099 | 5812 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
66b4ffd1 | 5813 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5814 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5815 | if (ret) { |
5816 | iput(inode); | |
66b4ffd1 | 5817 | inode = ERR_PTR(ret); |
01cd3367 | 5818 | } |
c71bf099 YZ |
5819 | } |
5820 | ||
3de4586c CM |
5821 | return inode; |
5822 | } | |
5823 | ||
fe15ce44 | 5824 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5825 | { |
5826 | struct btrfs_root *root; | |
2b0143b5 | 5827 | struct inode *inode = d_inode(dentry); |
76dda93c | 5828 | |
848cce0d | 5829 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5830 | inode = d_inode(dentry->d_parent); |
76dda93c | 5831 | |
848cce0d LZ |
5832 | if (inode) { |
5833 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5834 | if (btrfs_root_refs(&root->root_item) == 0) |
5835 | return 1; | |
848cce0d | 5836 | |
4a0cc7ca | 5837 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5838 | return 1; |
efefb143 | 5839 | } |
76dda93c YZ |
5840 | return 0; |
5841 | } | |
5842 | ||
b4aff1f8 JB |
5843 | static void btrfs_dentry_release(struct dentry *dentry) |
5844 | { | |
944a4515 | 5845 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5846 | } |
5847 | ||
3de4586c | 5848 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5849 | unsigned int flags) |
3de4586c | 5850 | { |
5662344b | 5851 | struct inode *inode; |
a66e7cc6 | 5852 | |
5662344b TI |
5853 | inode = btrfs_lookup_dentry(dir, dentry); |
5854 | if (IS_ERR(inode)) { | |
5855 | if (PTR_ERR(inode) == -ENOENT) | |
5856 | inode = NULL; | |
5857 | else | |
5858 | return ERR_CAST(inode); | |
5859 | } | |
5860 | ||
41d28bca | 5861 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5862 | } |
5863 | ||
16cdcec7 | 5864 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5865 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5866 | }; | |
5867 | ||
9cdda8d3 | 5868 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5869 | { |
9cdda8d3 | 5870 | struct inode *inode = file_inode(file); |
2ff7e61e | 5871 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 5872 | struct btrfs_root *root = BTRFS_I(inode)->root; |
39279cc3 CM |
5873 | struct btrfs_dir_item *di; |
5874 | struct btrfs_key key; | |
5f39d397 | 5875 | struct btrfs_key found_key; |
39279cc3 | 5876 | struct btrfs_path *path; |
16cdcec7 MX |
5877 | struct list_head ins_list; |
5878 | struct list_head del_list; | |
39279cc3 | 5879 | int ret; |
5f39d397 | 5880 | struct extent_buffer *leaf; |
39279cc3 | 5881 | int slot; |
39279cc3 CM |
5882 | unsigned char d_type; |
5883 | int over = 0; | |
5f39d397 CM |
5884 | char tmp_name[32]; |
5885 | char *name_ptr; | |
5886 | int name_len; | |
02dbfc99 | 5887 | bool put = false; |
c2951f32 | 5888 | struct btrfs_key location; |
5f39d397 | 5889 | |
9cdda8d3 AV |
5890 | if (!dir_emit_dots(file, ctx)) |
5891 | return 0; | |
5892 | ||
49593bfa | 5893 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5894 | if (!path) |
5895 | return -ENOMEM; | |
ff5714cc | 5896 | |
e4058b54 | 5897 | path->reada = READA_FORWARD; |
49593bfa | 5898 | |
c2951f32 JM |
5899 | INIT_LIST_HEAD(&ins_list); |
5900 | INIT_LIST_HEAD(&del_list); | |
5901 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5902 | |
c2951f32 | 5903 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5904 | key.offset = ctx->pos; |
4a0cc7ca | 5905 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5906 | |
39279cc3 CM |
5907 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5908 | if (ret < 0) | |
5909 | goto err; | |
49593bfa DW |
5910 | |
5911 | while (1) { | |
5f39d397 | 5912 | leaf = path->nodes[0]; |
39279cc3 | 5913 | slot = path->slots[0]; |
b9e03af0 LZ |
5914 | if (slot >= btrfs_header_nritems(leaf)) { |
5915 | ret = btrfs_next_leaf(root, path); | |
5916 | if (ret < 0) | |
5917 | goto err; | |
5918 | else if (ret > 0) | |
5919 | break; | |
5920 | continue; | |
39279cc3 | 5921 | } |
3de4586c | 5922 | |
5f39d397 CM |
5923 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5924 | ||
5925 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5926 | break; |
c2951f32 | 5927 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5928 | break; |
9cdda8d3 | 5929 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5930 | goto next; |
c2951f32 | 5931 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5932 | goto next; |
5f39d397 | 5933 | |
9cdda8d3 | 5934 | ctx->pos = found_key.offset; |
49593bfa | 5935 | |
39279cc3 | 5936 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e79a3327 | 5937 | if (verify_dir_item(fs_info, leaf, slot, di)) |
c2951f32 | 5938 | goto next; |
22a94d44 | 5939 | |
c2951f32 JM |
5940 | name_len = btrfs_dir_name_len(leaf, di); |
5941 | if (name_len <= sizeof(tmp_name)) { | |
5942 | name_ptr = tmp_name; | |
5943 | } else { | |
5944 | name_ptr = kmalloc(name_len, GFP_KERNEL); | |
5945 | if (!name_ptr) { | |
5946 | ret = -ENOMEM; | |
5947 | goto err; | |
5f39d397 | 5948 | } |
c2951f32 JM |
5949 | } |
5950 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), | |
5951 | name_len); | |
3de4586c | 5952 | |
c2951f32 JM |
5953 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
5954 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
fede766f | 5955 | |
c2951f32 JM |
5956 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
5957 | d_type); | |
5f39d397 | 5958 | |
c2951f32 JM |
5959 | if (name_ptr != tmp_name) |
5960 | kfree(name_ptr); | |
5f39d397 | 5961 | |
c2951f32 JM |
5962 | if (over) |
5963 | goto nopos; | |
d2fbb2b5 | 5964 | ctx->pos++; |
b9e03af0 LZ |
5965 | next: |
5966 | path->slots[0]++; | |
39279cc3 | 5967 | } |
49593bfa | 5968 | |
d2fbb2b5 | 5969 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5970 | if (ret) |
bc4ef759 DS |
5971 | goto nopos; |
5972 | ||
db62efbb ZB |
5973 | /* |
5974 | * Stop new entries from being returned after we return the last | |
5975 | * entry. | |
5976 | * | |
5977 | * New directory entries are assigned a strictly increasing | |
5978 | * offset. This means that new entries created during readdir | |
5979 | * are *guaranteed* to be seen in the future by that readdir. | |
5980 | * This has broken buggy programs which operate on names as | |
5981 | * they're returned by readdir. Until we re-use freed offsets | |
5982 | * we have this hack to stop new entries from being returned | |
5983 | * under the assumption that they'll never reach this huge | |
5984 | * offset. | |
5985 | * | |
5986 | * This is being careful not to overflow 32bit loff_t unless the | |
5987 | * last entry requires it because doing so has broken 32bit apps | |
5988 | * in the past. | |
5989 | */ | |
c2951f32 JM |
5990 | if (ctx->pos >= INT_MAX) |
5991 | ctx->pos = LLONG_MAX; | |
5992 | else | |
5993 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5994 | nopos: |
5995 | ret = 0; | |
5996 | err: | |
02dbfc99 OS |
5997 | if (put) |
5998 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5999 | btrfs_free_path(path); |
39279cc3 CM |
6000 | return ret; |
6001 | } | |
6002 | ||
a9185b41 | 6003 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
6004 | { |
6005 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6006 | struct btrfs_trans_handle *trans; | |
6007 | int ret = 0; | |
0af3d00b | 6008 | bool nolock = false; |
39279cc3 | 6009 | |
72ac3c0d | 6010 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
6011 | return 0; |
6012 | ||
70ddc553 NB |
6013 | if (btrfs_fs_closing(root->fs_info) && |
6014 | btrfs_is_free_space_inode(BTRFS_I(inode))) | |
82d5902d | 6015 | nolock = true; |
0af3d00b | 6016 | |
a9185b41 | 6017 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 6018 | if (nolock) |
7a7eaa40 | 6019 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 6020 | else |
7a7eaa40 | 6021 | trans = btrfs_join_transaction(root); |
3612b495 TI |
6022 | if (IS_ERR(trans)) |
6023 | return PTR_ERR(trans); | |
3a45bb20 | 6024 | ret = btrfs_commit_transaction(trans); |
39279cc3 CM |
6025 | } |
6026 | return ret; | |
6027 | } | |
6028 | ||
6029 | /* | |
54aa1f4d | 6030 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6031 | * inode changes. But, it is most likely to find the inode in cache. |
6032 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6033 | * to keep or drop this code. | |
6034 | */ | |
48a3b636 | 6035 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6036 | { |
2ff7e61e | 6037 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6038 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6039 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6040 | int ret; |
6041 | ||
72ac3c0d | 6042 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6043 | return 0; |
39279cc3 | 6044 | |
7a7eaa40 | 6045 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6046 | if (IS_ERR(trans)) |
6047 | return PTR_ERR(trans); | |
8929ecfa YZ |
6048 | |
6049 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6050 | if (ret && ret == -ENOSPC) { |
6051 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6052 | btrfs_end_transaction(trans); |
94b60442 | 6053 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6054 | if (IS_ERR(trans)) |
6055 | return PTR_ERR(trans); | |
8929ecfa | 6056 | |
94b60442 | 6057 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6058 | } |
3a45bb20 | 6059 | btrfs_end_transaction(trans); |
16cdcec7 | 6060 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6061 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6062 | |
6063 | return ret; | |
6064 | } | |
6065 | ||
6066 | /* | |
6067 | * This is a copy of file_update_time. We need this so we can return error on | |
6068 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6069 | */ | |
e41f941a JB |
6070 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6071 | int flags) | |
22c44fe6 | 6072 | { |
2bc55652 AB |
6073 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6074 | ||
6075 | if (btrfs_root_readonly(root)) | |
6076 | return -EROFS; | |
6077 | ||
e41f941a | 6078 | if (flags & S_VERSION) |
22c44fe6 | 6079 | inode_inc_iversion(inode); |
e41f941a JB |
6080 | if (flags & S_CTIME) |
6081 | inode->i_ctime = *now; | |
6082 | if (flags & S_MTIME) | |
6083 | inode->i_mtime = *now; | |
6084 | if (flags & S_ATIME) | |
6085 | inode->i_atime = *now; | |
6086 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6087 | } |
6088 | ||
d352ac68 CM |
6089 | /* |
6090 | * find the highest existing sequence number in a directory | |
6091 | * and then set the in-memory index_cnt variable to reflect | |
6092 | * free sequence numbers | |
6093 | */ | |
4c570655 | 6094 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6095 | { |
4c570655 | 6096 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6097 | struct btrfs_key key, found_key; |
6098 | struct btrfs_path *path; | |
6099 | struct extent_buffer *leaf; | |
6100 | int ret; | |
6101 | ||
4c570655 | 6102 | key.objectid = btrfs_ino(inode); |
962a298f | 6103 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6104 | key.offset = (u64)-1; |
6105 | ||
6106 | path = btrfs_alloc_path(); | |
6107 | if (!path) | |
6108 | return -ENOMEM; | |
6109 | ||
6110 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6111 | if (ret < 0) | |
6112 | goto out; | |
6113 | /* FIXME: we should be able to handle this */ | |
6114 | if (ret == 0) | |
6115 | goto out; | |
6116 | ret = 0; | |
6117 | ||
6118 | /* | |
6119 | * MAGIC NUMBER EXPLANATION: | |
6120 | * since we search a directory based on f_pos we have to start at 2 | |
6121 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6122 | * else has to start at 2 | |
6123 | */ | |
6124 | if (path->slots[0] == 0) { | |
4c570655 | 6125 | inode->index_cnt = 2; |
aec7477b JB |
6126 | goto out; |
6127 | } | |
6128 | ||
6129 | path->slots[0]--; | |
6130 | ||
6131 | leaf = path->nodes[0]; | |
6132 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6133 | ||
4c570655 | 6134 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6135 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6136 | inode->index_cnt = 2; |
aec7477b JB |
6137 | goto out; |
6138 | } | |
6139 | ||
4c570655 | 6140 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6141 | out: |
6142 | btrfs_free_path(path); | |
6143 | return ret; | |
6144 | } | |
6145 | ||
d352ac68 CM |
6146 | /* |
6147 | * helper to find a free sequence number in a given directory. This current | |
6148 | * code is very simple, later versions will do smarter things in the btree | |
6149 | */ | |
877574e2 | 6150 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6151 | { |
6152 | int ret = 0; | |
6153 | ||
877574e2 NB |
6154 | if (dir->index_cnt == (u64)-1) { |
6155 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6156 | if (ret) { |
6157 | ret = btrfs_set_inode_index_count(dir); | |
6158 | if (ret) | |
6159 | return ret; | |
6160 | } | |
aec7477b JB |
6161 | } |
6162 | ||
877574e2 NB |
6163 | *index = dir->index_cnt; |
6164 | dir->index_cnt++; | |
aec7477b JB |
6165 | |
6166 | return ret; | |
6167 | } | |
6168 | ||
b0d5d10f CM |
6169 | static int btrfs_insert_inode_locked(struct inode *inode) |
6170 | { | |
6171 | struct btrfs_iget_args args; | |
6172 | args.location = &BTRFS_I(inode)->location; | |
6173 | args.root = BTRFS_I(inode)->root; | |
6174 | ||
6175 | return insert_inode_locked4(inode, | |
6176 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6177 | btrfs_find_actor, &args); | |
6178 | } | |
6179 | ||
39279cc3 CM |
6180 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6181 | struct btrfs_root *root, | |
aec7477b | 6182 | struct inode *dir, |
9c58309d | 6183 | const char *name, int name_len, |
175a4eb7 AV |
6184 | u64 ref_objectid, u64 objectid, |
6185 | umode_t mode, u64 *index) | |
39279cc3 | 6186 | { |
0b246afa | 6187 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6188 | struct inode *inode; |
5f39d397 | 6189 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6190 | struct btrfs_key *location; |
5f39d397 | 6191 | struct btrfs_path *path; |
9c58309d CM |
6192 | struct btrfs_inode_ref *ref; |
6193 | struct btrfs_key key[2]; | |
6194 | u32 sizes[2]; | |
ef3b9af5 | 6195 | int nitems = name ? 2 : 1; |
9c58309d | 6196 | unsigned long ptr; |
39279cc3 | 6197 | int ret; |
39279cc3 | 6198 | |
5f39d397 | 6199 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6200 | if (!path) |
6201 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6202 | |
0b246afa | 6203 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6204 | if (!inode) { |
6205 | btrfs_free_path(path); | |
39279cc3 | 6206 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6207 | } |
39279cc3 | 6208 | |
5762b5c9 FM |
6209 | /* |
6210 | * O_TMPFILE, set link count to 0, so that after this point, | |
6211 | * we fill in an inode item with the correct link count. | |
6212 | */ | |
6213 | if (!name) | |
6214 | set_nlink(inode, 0); | |
6215 | ||
581bb050 LZ |
6216 | /* |
6217 | * we have to initialize this early, so we can reclaim the inode | |
6218 | * number if we fail afterwards in this function. | |
6219 | */ | |
6220 | inode->i_ino = objectid; | |
6221 | ||
ef3b9af5 | 6222 | if (dir && name) { |
1abe9b8a | 6223 | trace_btrfs_inode_request(dir); |
6224 | ||
877574e2 | 6225 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6226 | if (ret) { |
8fb27640 | 6227 | btrfs_free_path(path); |
09771430 | 6228 | iput(inode); |
aec7477b | 6229 | return ERR_PTR(ret); |
09771430 | 6230 | } |
ef3b9af5 FM |
6231 | } else if (dir) { |
6232 | *index = 0; | |
aec7477b JB |
6233 | } |
6234 | /* | |
6235 | * index_cnt is ignored for everything but a dir, | |
6236 | * btrfs_get_inode_index_count has an explanation for the magic | |
6237 | * number | |
6238 | */ | |
6239 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6240 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6241 | BTRFS_I(inode)->root = root; |
e02119d5 | 6242 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6243 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6244 | |
5dc562c5 JB |
6245 | /* |
6246 | * We could have gotten an inode number from somebody who was fsynced | |
6247 | * and then removed in this same transaction, so let's just set full | |
6248 | * sync since it will be a full sync anyway and this will blow away the | |
6249 | * old info in the log. | |
6250 | */ | |
6251 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6252 | ||
9c58309d | 6253 | key[0].objectid = objectid; |
962a298f | 6254 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6255 | key[0].offset = 0; |
6256 | ||
9c58309d | 6257 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6258 | |
6259 | if (name) { | |
6260 | /* | |
6261 | * Start new inodes with an inode_ref. This is slightly more | |
6262 | * efficient for small numbers of hard links since they will | |
6263 | * be packed into one item. Extended refs will kick in if we | |
6264 | * add more hard links than can fit in the ref item. | |
6265 | */ | |
6266 | key[1].objectid = objectid; | |
962a298f | 6267 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6268 | key[1].offset = ref_objectid; |
6269 | ||
6270 | sizes[1] = name_len + sizeof(*ref); | |
6271 | } | |
9c58309d | 6272 | |
b0d5d10f CM |
6273 | location = &BTRFS_I(inode)->location; |
6274 | location->objectid = objectid; | |
6275 | location->offset = 0; | |
962a298f | 6276 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6277 | |
6278 | ret = btrfs_insert_inode_locked(inode); | |
6279 | if (ret < 0) | |
6280 | goto fail; | |
6281 | ||
b9473439 | 6282 | path->leave_spinning = 1; |
ef3b9af5 | 6283 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6284 | if (ret != 0) |
b0d5d10f | 6285 | goto fail_unlock; |
5f39d397 | 6286 | |
ecc11fab | 6287 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6288 | inode_set_bytes(inode, 0); |
9cc97d64 | 6289 | |
c2050a45 | 6290 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6291 | inode->i_atime = inode->i_mtime; |
6292 | inode->i_ctime = inode->i_mtime; | |
6293 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6294 | ||
5f39d397 CM |
6295 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6296 | struct btrfs_inode_item); | |
b159fa28 | 6297 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6298 | sizeof(*inode_item)); |
e02119d5 | 6299 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6300 | |
ef3b9af5 FM |
6301 | if (name) { |
6302 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6303 | struct btrfs_inode_ref); | |
6304 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6305 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6306 | ptr = (unsigned long)(ref + 1); | |
6307 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6308 | } | |
9c58309d | 6309 | |
5f39d397 CM |
6310 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6311 | btrfs_free_path(path); | |
6312 | ||
6cbff00f CH |
6313 | btrfs_inherit_iflags(inode, dir); |
6314 | ||
569254b0 | 6315 | if (S_ISREG(mode)) { |
0b246afa | 6316 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6317 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6318 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6319 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6320 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6321 | } |
6322 | ||
5d4f98a2 | 6323 | inode_tree_add(inode); |
1abe9b8a | 6324 | |
6325 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6326 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6327 | |
8ea05e3a AB |
6328 | btrfs_update_root_times(trans, root); |
6329 | ||
63541927 FDBM |
6330 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6331 | if (ret) | |
0b246afa | 6332 | btrfs_err(fs_info, |
63541927 | 6333 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6334 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6335 | |
39279cc3 | 6336 | return inode; |
b0d5d10f CM |
6337 | |
6338 | fail_unlock: | |
6339 | unlock_new_inode(inode); | |
5f39d397 | 6340 | fail: |
ef3b9af5 | 6341 | if (dir && name) |
aec7477b | 6342 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6343 | btrfs_free_path(path); |
09771430 | 6344 | iput(inode); |
5f39d397 | 6345 | return ERR_PTR(ret); |
39279cc3 CM |
6346 | } |
6347 | ||
6348 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6349 | { | |
6350 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6351 | } | |
6352 | ||
d352ac68 CM |
6353 | /* |
6354 | * utility function to add 'inode' into 'parent_inode' with | |
6355 | * a give name and a given sequence number. | |
6356 | * if 'add_backref' is true, also insert a backref from the | |
6357 | * inode to the parent directory. | |
6358 | */ | |
e02119d5 | 6359 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6360 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6361 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6362 | { |
db0a669f | 6363 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
4df27c4d | 6364 | int ret = 0; |
39279cc3 | 6365 | struct btrfs_key key; |
db0a669f NB |
6366 | struct btrfs_root *root = parent_inode->root; |
6367 | u64 ino = btrfs_ino(inode); | |
6368 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6369 | |
33345d01 | 6370 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6371 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6372 | } else { |
33345d01 | 6373 | key.objectid = ino; |
962a298f | 6374 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6375 | key.offset = 0; |
6376 | } | |
6377 | ||
33345d01 | 6378 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
0b246afa JM |
6379 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
6380 | root->root_key.objectid, parent_ino, | |
6381 | index, name, name_len); | |
4df27c4d | 6382 | } else if (add_backref) { |
33345d01 LZ |
6383 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6384 | parent_ino, index); | |
4df27c4d | 6385 | } |
39279cc3 | 6386 | |
79787eaa JM |
6387 | /* Nothing to clean up yet */ |
6388 | if (ret) | |
6389 | return ret; | |
4df27c4d | 6390 | |
79787eaa JM |
6391 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6392 | parent_inode, &key, | |
db0a669f | 6393 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6394 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6395 | goto fail_dir_item; |
6396 | else if (ret) { | |
66642832 | 6397 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6398 | return ret; |
39279cc3 | 6399 | } |
79787eaa | 6400 | |
db0a669f | 6401 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6402 | name_len * 2); |
db0a669f NB |
6403 | inode_inc_iversion(&parent_inode->vfs_inode); |
6404 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6405 | current_time(&parent_inode->vfs_inode); | |
6406 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
79787eaa | 6407 | if (ret) |
66642832 | 6408 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6409 | return ret; |
fe66a05a CM |
6410 | |
6411 | fail_dir_item: | |
6412 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6413 | u64 local_index; | |
6414 | int err; | |
0b246afa JM |
6415 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
6416 | root->root_key.objectid, parent_ino, | |
6417 | &local_index, name, name_len); | |
fe66a05a CM |
6418 | |
6419 | } else if (add_backref) { | |
6420 | u64 local_index; | |
6421 | int err; | |
6422 | ||
6423 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6424 | ino, parent_ino, &local_index); | |
6425 | } | |
6426 | return ret; | |
39279cc3 CM |
6427 | } |
6428 | ||
6429 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6430 | struct btrfs_inode *dir, struct dentry *dentry, |
6431 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6432 | { |
a1b075d2 JB |
6433 | int err = btrfs_add_link(trans, dir, inode, |
6434 | dentry->d_name.name, dentry->d_name.len, | |
6435 | backref, index); | |
39279cc3 CM |
6436 | if (err > 0) |
6437 | err = -EEXIST; | |
6438 | return err; | |
6439 | } | |
6440 | ||
618e21d5 | 6441 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6442 | umode_t mode, dev_t rdev) |
618e21d5 | 6443 | { |
2ff7e61e | 6444 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6445 | struct btrfs_trans_handle *trans; |
6446 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6447 | struct inode *inode = NULL; |
618e21d5 JB |
6448 | int err; |
6449 | int drop_inode = 0; | |
6450 | u64 objectid; | |
00e4e6b3 | 6451 | u64 index = 0; |
618e21d5 | 6452 | |
9ed74f2d JB |
6453 | /* |
6454 | * 2 for inode item and ref | |
6455 | * 2 for dir items | |
6456 | * 1 for xattr if selinux is on | |
6457 | */ | |
a22285a6 YZ |
6458 | trans = btrfs_start_transaction(root, 5); |
6459 | if (IS_ERR(trans)) | |
6460 | return PTR_ERR(trans); | |
1832a6d5 | 6461 | |
581bb050 LZ |
6462 | err = btrfs_find_free_ino(root, &objectid); |
6463 | if (err) | |
6464 | goto out_unlock; | |
6465 | ||
aec7477b | 6466 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6467 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6468 | mode, &index); | |
7cf96da3 TI |
6469 | if (IS_ERR(inode)) { |
6470 | err = PTR_ERR(inode); | |
618e21d5 | 6471 | goto out_unlock; |
7cf96da3 | 6472 | } |
618e21d5 | 6473 | |
ad19db71 CS |
6474 | /* |
6475 | * If the active LSM wants to access the inode during | |
6476 | * d_instantiate it needs these. Smack checks to see | |
6477 | * if the filesystem supports xattrs by looking at the | |
6478 | * ops vector. | |
6479 | */ | |
ad19db71 | 6480 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6481 | init_special_inode(inode, inode->i_mode, rdev); |
6482 | ||
6483 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6484 | if (err) |
b0d5d10f CM |
6485 | goto out_unlock_inode; |
6486 | ||
cef415af NB |
6487 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6488 | 0, index); | |
b0d5d10f CM |
6489 | if (err) { |
6490 | goto out_unlock_inode; | |
6491 | } else { | |
1b4ab1bb | 6492 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6493 | unlock_new_inode(inode); |
08c422c2 | 6494 | d_instantiate(dentry, inode); |
618e21d5 | 6495 | } |
b0d5d10f | 6496 | |
618e21d5 | 6497 | out_unlock: |
3a45bb20 | 6498 | btrfs_end_transaction(trans); |
2ff7e61e JM |
6499 | btrfs_balance_delayed_items(fs_info); |
6500 | btrfs_btree_balance_dirty(fs_info); | |
618e21d5 JB |
6501 | if (drop_inode) { |
6502 | inode_dec_link_count(inode); | |
6503 | iput(inode); | |
6504 | } | |
618e21d5 | 6505 | return err; |
b0d5d10f CM |
6506 | |
6507 | out_unlock_inode: | |
6508 | drop_inode = 1; | |
6509 | unlock_new_inode(inode); | |
6510 | goto out_unlock; | |
6511 | ||
618e21d5 JB |
6512 | } |
6513 | ||
39279cc3 | 6514 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6515 | umode_t mode, bool excl) |
39279cc3 | 6516 | { |
2ff7e61e | 6517 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6518 | struct btrfs_trans_handle *trans; |
6519 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6520 | struct inode *inode = NULL; |
43baa579 | 6521 | int drop_inode_on_err = 0; |
a22285a6 | 6522 | int err; |
39279cc3 | 6523 | u64 objectid; |
00e4e6b3 | 6524 | u64 index = 0; |
39279cc3 | 6525 | |
9ed74f2d JB |
6526 | /* |
6527 | * 2 for inode item and ref | |
6528 | * 2 for dir items | |
6529 | * 1 for xattr if selinux is on | |
6530 | */ | |
a22285a6 YZ |
6531 | trans = btrfs_start_transaction(root, 5); |
6532 | if (IS_ERR(trans)) | |
6533 | return PTR_ERR(trans); | |
9ed74f2d | 6534 | |
581bb050 LZ |
6535 | err = btrfs_find_free_ino(root, &objectid); |
6536 | if (err) | |
6537 | goto out_unlock; | |
6538 | ||
aec7477b | 6539 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6540 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6541 | mode, &index); | |
7cf96da3 TI |
6542 | if (IS_ERR(inode)) { |
6543 | err = PTR_ERR(inode); | |
39279cc3 | 6544 | goto out_unlock; |
7cf96da3 | 6545 | } |
43baa579 | 6546 | drop_inode_on_err = 1; |
ad19db71 CS |
6547 | /* |
6548 | * If the active LSM wants to access the inode during | |
6549 | * d_instantiate it needs these. Smack checks to see | |
6550 | * if the filesystem supports xattrs by looking at the | |
6551 | * ops vector. | |
6552 | */ | |
6553 | inode->i_fop = &btrfs_file_operations; | |
6554 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6555 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6556 | |
6557 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6558 | if (err) | |
6559 | goto out_unlock_inode; | |
6560 | ||
6561 | err = btrfs_update_inode(trans, root, inode); | |
6562 | if (err) | |
6563 | goto out_unlock_inode; | |
ad19db71 | 6564 | |
cef415af NB |
6565 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6566 | 0, index); | |
39279cc3 | 6567 | if (err) |
b0d5d10f | 6568 | goto out_unlock_inode; |
43baa579 | 6569 | |
43baa579 | 6570 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6571 | unlock_new_inode(inode); |
43baa579 FB |
6572 | d_instantiate(dentry, inode); |
6573 | ||
39279cc3 | 6574 | out_unlock: |
3a45bb20 | 6575 | btrfs_end_transaction(trans); |
43baa579 | 6576 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6577 | inode_dec_link_count(inode); |
6578 | iput(inode); | |
6579 | } | |
2ff7e61e JM |
6580 | btrfs_balance_delayed_items(fs_info); |
6581 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6582 | return err; |
b0d5d10f CM |
6583 | |
6584 | out_unlock_inode: | |
6585 | unlock_new_inode(inode); | |
6586 | goto out_unlock; | |
6587 | ||
39279cc3 CM |
6588 | } |
6589 | ||
6590 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6591 | struct dentry *dentry) | |
6592 | { | |
271dba45 | 6593 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6594 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6595 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6596 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6597 | u64 index; |
39279cc3 CM |
6598 | int err; |
6599 | int drop_inode = 0; | |
6600 | ||
4a8be425 TH |
6601 | /* do not allow sys_link's with other subvols of the same device */ |
6602 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6603 | return -EXDEV; |
4a8be425 | 6604 | |
f186373f | 6605 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6606 | return -EMLINK; |
4a8be425 | 6607 | |
877574e2 | 6608 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6609 | if (err) |
6610 | goto fail; | |
6611 | ||
a22285a6 | 6612 | /* |
7e6b6465 | 6613 | * 2 items for inode and inode ref |
a22285a6 | 6614 | * 2 items for dir items |
7e6b6465 | 6615 | * 1 item for parent inode |
a22285a6 | 6616 | */ |
7e6b6465 | 6617 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6618 | if (IS_ERR(trans)) { |
6619 | err = PTR_ERR(trans); | |
271dba45 | 6620 | trans = NULL; |
a22285a6 YZ |
6621 | goto fail; |
6622 | } | |
5f39d397 | 6623 | |
67de1176 MX |
6624 | /* There are several dir indexes for this inode, clear the cache. */ |
6625 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6626 | inc_nlink(inode); |
0c4d2d95 | 6627 | inode_inc_iversion(inode); |
c2050a45 | 6628 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6629 | ihold(inode); |
e9976151 | 6630 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6631 | |
cef415af NB |
6632 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6633 | 1, index); | |
5f39d397 | 6634 | |
a5719521 | 6635 | if (err) { |
54aa1f4d | 6636 | drop_inode = 1; |
a5719521 | 6637 | } else { |
10d9f309 | 6638 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6639 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6640 | if (err) |
6641 | goto fail; | |
ef3b9af5 FM |
6642 | if (inode->i_nlink == 1) { |
6643 | /* | |
6644 | * If new hard link count is 1, it's a file created | |
6645 | * with open(2) O_TMPFILE flag. | |
6646 | */ | |
3d6ae7bb | 6647 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6648 | if (err) |
6649 | goto fail; | |
6650 | } | |
08c422c2 | 6651 | d_instantiate(dentry, inode); |
9ca5fbfb | 6652 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6653 | } |
39279cc3 | 6654 | |
2ff7e61e | 6655 | btrfs_balance_delayed_items(fs_info); |
1832a6d5 | 6656 | fail: |
271dba45 | 6657 | if (trans) |
3a45bb20 | 6658 | btrfs_end_transaction(trans); |
39279cc3 CM |
6659 | if (drop_inode) { |
6660 | inode_dec_link_count(inode); | |
6661 | iput(inode); | |
6662 | } | |
2ff7e61e | 6663 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6664 | return err; |
6665 | } | |
6666 | ||
18bb1db3 | 6667 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6668 | { |
2ff7e61e | 6669 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6670 | struct inode *inode = NULL; |
39279cc3 CM |
6671 | struct btrfs_trans_handle *trans; |
6672 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6673 | int err = 0; | |
6674 | int drop_on_err = 0; | |
b9d86667 | 6675 | u64 objectid = 0; |
00e4e6b3 | 6676 | u64 index = 0; |
39279cc3 | 6677 | |
9ed74f2d JB |
6678 | /* |
6679 | * 2 items for inode and ref | |
6680 | * 2 items for dir items | |
6681 | * 1 for xattr if selinux is on | |
6682 | */ | |
a22285a6 YZ |
6683 | trans = btrfs_start_transaction(root, 5); |
6684 | if (IS_ERR(trans)) | |
6685 | return PTR_ERR(trans); | |
39279cc3 | 6686 | |
581bb050 LZ |
6687 | err = btrfs_find_free_ino(root, &objectid); |
6688 | if (err) | |
6689 | goto out_fail; | |
6690 | ||
aec7477b | 6691 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6692 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6693 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6694 | if (IS_ERR(inode)) { |
6695 | err = PTR_ERR(inode); | |
6696 | goto out_fail; | |
6697 | } | |
5f39d397 | 6698 | |
39279cc3 | 6699 | drop_on_err = 1; |
b0d5d10f CM |
6700 | /* these must be set before we unlock the inode */ |
6701 | inode->i_op = &btrfs_dir_inode_operations; | |
6702 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6703 | |
2a7dba39 | 6704 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6705 | if (err) |
b0d5d10f | 6706 | goto out_fail_inode; |
39279cc3 | 6707 | |
6ef06d27 | 6708 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6709 | err = btrfs_update_inode(trans, root, inode); |
6710 | if (err) | |
b0d5d10f | 6711 | goto out_fail_inode; |
5f39d397 | 6712 | |
db0a669f NB |
6713 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6714 | dentry->d_name.name, | |
6715 | dentry->d_name.len, 0, index); | |
39279cc3 | 6716 | if (err) |
b0d5d10f | 6717 | goto out_fail_inode; |
5f39d397 | 6718 | |
39279cc3 | 6719 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6720 | /* |
6721 | * mkdir is special. We're unlocking after we call d_instantiate | |
6722 | * to avoid a race with nfsd calling d_instantiate. | |
6723 | */ | |
6724 | unlock_new_inode(inode); | |
39279cc3 | 6725 | drop_on_err = 0; |
39279cc3 CM |
6726 | |
6727 | out_fail: | |
3a45bb20 | 6728 | btrfs_end_transaction(trans); |
c7cfb8a5 WS |
6729 | if (drop_on_err) { |
6730 | inode_dec_link_count(inode); | |
39279cc3 | 6731 | iput(inode); |
c7cfb8a5 | 6732 | } |
2ff7e61e JM |
6733 | btrfs_balance_delayed_items(fs_info); |
6734 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6735 | return err; |
b0d5d10f CM |
6736 | |
6737 | out_fail_inode: | |
6738 | unlock_new_inode(inode); | |
6739 | goto out_fail; | |
39279cc3 CM |
6740 | } |
6741 | ||
e6c4efd8 QW |
6742 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6743 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6744 | { | |
6745 | struct rb_node *next; | |
6746 | ||
6747 | next = rb_next(&em->rb_node); | |
6748 | if (!next) | |
6749 | return NULL; | |
6750 | return container_of(next, struct extent_map, rb_node); | |
6751 | } | |
6752 | ||
6753 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6754 | { | |
6755 | struct rb_node *prev; | |
6756 | ||
6757 | prev = rb_prev(&em->rb_node); | |
6758 | if (!prev) | |
6759 | return NULL; | |
6760 | return container_of(prev, struct extent_map, rb_node); | |
6761 | } | |
6762 | ||
d352ac68 | 6763 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6764 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6765 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6766 | * the best fitted new extent into the tree. |
d352ac68 | 6767 | */ |
3b951516 CM |
6768 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6769 | struct extent_map *existing, | |
e6dcd2dc | 6770 | struct extent_map *em, |
51f395ad | 6771 | u64 map_start) |
3b951516 | 6772 | { |
e6c4efd8 QW |
6773 | struct extent_map *prev; |
6774 | struct extent_map *next; | |
6775 | u64 start; | |
6776 | u64 end; | |
3b951516 | 6777 | u64 start_diff; |
3b951516 | 6778 | |
e6dcd2dc | 6779 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6780 | |
6781 | if (existing->start > map_start) { | |
6782 | next = existing; | |
6783 | prev = prev_extent_map(next); | |
6784 | } else { | |
6785 | prev = existing; | |
6786 | next = next_extent_map(prev); | |
6787 | } | |
6788 | ||
6789 | start = prev ? extent_map_end(prev) : em->start; | |
6790 | start = max_t(u64, start, em->start); | |
6791 | end = next ? next->start : extent_map_end(em); | |
6792 | end = min_t(u64, end, extent_map_end(em)); | |
6793 | start_diff = start - em->start; | |
6794 | em->start = start; | |
6795 | em->len = end - start; | |
c8b97818 CM |
6796 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6797 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6798 | em->block_start += start_diff; |
c8b97818 CM |
6799 | em->block_len -= start_diff; |
6800 | } | |
09a2a8f9 | 6801 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6802 | } |
6803 | ||
c8b97818 | 6804 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6805 | struct page *page, |
c8b97818 CM |
6806 | size_t pg_offset, u64 extent_offset, |
6807 | struct btrfs_file_extent_item *item) | |
6808 | { | |
6809 | int ret; | |
6810 | struct extent_buffer *leaf = path->nodes[0]; | |
6811 | char *tmp; | |
6812 | size_t max_size; | |
6813 | unsigned long inline_size; | |
6814 | unsigned long ptr; | |
261507a0 | 6815 | int compress_type; |
c8b97818 CM |
6816 | |
6817 | WARN_ON(pg_offset != 0); | |
261507a0 | 6818 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6819 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6820 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6821 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6822 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6823 | if (!tmp) |
6824 | return -ENOMEM; | |
c8b97818 CM |
6825 | ptr = btrfs_file_extent_inline_start(item); |
6826 | ||
6827 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6828 | ||
09cbfeaf | 6829 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6830 | ret = btrfs_decompress(compress_type, tmp, page, |
6831 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6832 | |
6833 | /* | |
6834 | * decompression code contains a memset to fill in any space between the end | |
6835 | * of the uncompressed data and the end of max_size in case the decompressed | |
6836 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6837 | * the end of an inline extent and the beginning of the next block, so we | |
6838 | * cover that region here. | |
6839 | */ | |
6840 | ||
6841 | if (max_size + pg_offset < PAGE_SIZE) { | |
6842 | char *map = kmap(page); | |
6843 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6844 | kunmap(page); | |
6845 | } | |
c8b97818 | 6846 | kfree(tmp); |
166ae5a4 | 6847 | return ret; |
c8b97818 CM |
6848 | } |
6849 | ||
d352ac68 CM |
6850 | /* |
6851 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6852 | * the ugly parts come from merging extents from the disk with the in-ram |
6853 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6854 | * where the in-ram extents might be locked pending data=ordered completion. |
6855 | * | |
6856 | * This also copies inline extents directly into the page. | |
6857 | */ | |
fc4f21b1 NB |
6858 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
6859 | struct page *page, | |
6860 | size_t pg_offset, u64 start, u64 len, | |
6861 | int create) | |
a52d9a80 | 6862 | { |
fc4f21b1 | 6863 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
a52d9a80 CM |
6864 | int ret; |
6865 | int err = 0; | |
a52d9a80 CM |
6866 | u64 extent_start = 0; |
6867 | u64 extent_end = 0; | |
fc4f21b1 | 6868 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6869 | u32 found_type; |
f421950f | 6870 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6871 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6872 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6873 | struct extent_buffer *leaf; |
6874 | struct btrfs_key found_key; | |
a52d9a80 | 6875 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6876 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6877 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6878 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6879 | const bool new_inline = !page || create; |
a52d9a80 | 6880 | |
a52d9a80 | 6881 | again: |
890871be | 6882 | read_lock(&em_tree->lock); |
d1310b2e | 6883 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6884 | if (em) |
0b246afa | 6885 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6886 | read_unlock(&em_tree->lock); |
d1310b2e | 6887 | |
a52d9a80 | 6888 | if (em) { |
e1c4b745 CM |
6889 | if (em->start > start || em->start + em->len <= start) |
6890 | free_extent_map(em); | |
6891 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6892 | free_extent_map(em); |
6893 | else | |
6894 | goto out; | |
a52d9a80 | 6895 | } |
172ddd60 | 6896 | em = alloc_extent_map(); |
a52d9a80 | 6897 | if (!em) { |
d1310b2e CM |
6898 | err = -ENOMEM; |
6899 | goto out; | |
a52d9a80 | 6900 | } |
0b246afa | 6901 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 6902 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6903 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6904 | em->len = (u64)-1; |
c8b97818 | 6905 | em->block_len = (u64)-1; |
f421950f CM |
6906 | |
6907 | if (!path) { | |
6908 | path = btrfs_alloc_path(); | |
026fd317 JB |
6909 | if (!path) { |
6910 | err = -ENOMEM; | |
6911 | goto out; | |
6912 | } | |
6913 | /* | |
6914 | * Chances are we'll be called again, so go ahead and do | |
6915 | * readahead | |
6916 | */ | |
e4058b54 | 6917 | path->reada = READA_FORWARD; |
f421950f CM |
6918 | } |
6919 | ||
179e29e4 CM |
6920 | ret = btrfs_lookup_file_extent(trans, root, path, |
6921 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6922 | if (ret < 0) { |
6923 | err = ret; | |
6924 | goto out; | |
6925 | } | |
6926 | ||
6927 | if (ret != 0) { | |
6928 | if (path->slots[0] == 0) | |
6929 | goto not_found; | |
6930 | path->slots[0]--; | |
6931 | } | |
6932 | ||
5f39d397 CM |
6933 | leaf = path->nodes[0]; |
6934 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6935 | struct btrfs_file_extent_item); |
a52d9a80 | 6936 | /* are we inside the extent that was found? */ |
5f39d397 | 6937 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6938 | found_type = found_key.type; |
5f39d397 | 6939 | if (found_key.objectid != objectid || |
a52d9a80 | 6940 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6941 | /* |
6942 | * If we backup past the first extent we want to move forward | |
6943 | * and see if there is an extent in front of us, otherwise we'll | |
6944 | * say there is a hole for our whole search range which can | |
6945 | * cause problems. | |
6946 | */ | |
6947 | extent_end = start; | |
6948 | goto next; | |
a52d9a80 CM |
6949 | } |
6950 | ||
5f39d397 CM |
6951 | found_type = btrfs_file_extent_type(leaf, item); |
6952 | extent_start = found_key.offset; | |
d899e052 YZ |
6953 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6954 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6955 | extent_end = extent_start + |
db94535d | 6956 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
6957 | |
6958 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
6959 | extent_start); | |
9036c102 YZ |
6960 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6961 | size_t size; | |
514ac8ad | 6962 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
da17066c | 6963 | extent_end = ALIGN(extent_start + size, |
0b246afa | 6964 | fs_info->sectorsize); |
09ed2f16 LB |
6965 | |
6966 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
6967 | path->slots[0], | |
6968 | extent_start); | |
9036c102 | 6969 | } |
25a50341 | 6970 | next: |
9036c102 YZ |
6971 | if (start >= extent_end) { |
6972 | path->slots[0]++; | |
6973 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6974 | ret = btrfs_next_leaf(root, path); | |
6975 | if (ret < 0) { | |
6976 | err = ret; | |
6977 | goto out; | |
a52d9a80 | 6978 | } |
9036c102 YZ |
6979 | if (ret > 0) |
6980 | goto not_found; | |
6981 | leaf = path->nodes[0]; | |
a52d9a80 | 6982 | } |
9036c102 YZ |
6983 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6984 | if (found_key.objectid != objectid || | |
6985 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6986 | goto not_found; | |
6987 | if (start + len <= found_key.offset) | |
6988 | goto not_found; | |
e2eca69d WS |
6989 | if (start > found_key.offset) |
6990 | goto next; | |
9036c102 | 6991 | em->start = start; |
70c8a91c | 6992 | em->orig_start = start; |
9036c102 YZ |
6993 | em->len = found_key.offset - start; |
6994 | goto not_found_em; | |
6995 | } | |
6996 | ||
fc4f21b1 | 6997 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 6998 | new_inline, em); |
7ffbb598 | 6999 | |
d899e052 YZ |
7000 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7001 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
7002 | goto insert; |
7003 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 7004 | unsigned long ptr; |
a52d9a80 | 7005 | char *map; |
3326d1b0 CM |
7006 | size_t size; |
7007 | size_t extent_offset; | |
7008 | size_t copy_size; | |
a52d9a80 | 7009 | |
7ffbb598 | 7010 | if (new_inline) |
689f9346 | 7011 | goto out; |
5f39d397 | 7012 | |
514ac8ad | 7013 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 7014 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7015 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7016 | size - extent_offset); | |
3326d1b0 | 7017 | em->start = extent_start + extent_offset; |
0b246afa | 7018 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7019 | em->orig_block_len = em->len; |
70c8a91c | 7020 | em->orig_start = em->start; |
689f9346 | 7021 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 7022 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
7023 | if (btrfs_file_extent_compression(leaf, item) != |
7024 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7025 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7026 | extent_offset, item); |
166ae5a4 ZB |
7027 | if (ret) { |
7028 | err = ret; | |
7029 | goto out; | |
7030 | } | |
c8b97818 CM |
7031 | } else { |
7032 | map = kmap(page); | |
7033 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7034 | copy_size); | |
09cbfeaf | 7035 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7036 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7037 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7038 | copy_size); |
7039 | } | |
c8b97818 CM |
7040 | kunmap(page); |
7041 | } | |
179e29e4 CM |
7042 | flush_dcache_page(page); |
7043 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 7044 | BUG(); |
179e29e4 CM |
7045 | if (!trans) { |
7046 | kunmap(page); | |
7047 | free_extent_map(em); | |
7048 | em = NULL; | |
ff5714cc | 7049 | |
b3b4aa74 | 7050 | btrfs_release_path(path); |
7a7eaa40 | 7051 | trans = btrfs_join_transaction(root); |
ff5714cc | 7052 | |
3612b495 TI |
7053 | if (IS_ERR(trans)) |
7054 | return ERR_CAST(trans); | |
179e29e4 CM |
7055 | goto again; |
7056 | } | |
c8b97818 | 7057 | map = kmap(page); |
70dec807 | 7058 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 7059 | copy_size); |
c8b97818 | 7060 | kunmap(page); |
179e29e4 | 7061 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 7062 | } |
d1310b2e | 7063 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7064 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7065 | goto insert; |
a52d9a80 CM |
7066 | } |
7067 | not_found: | |
7068 | em->start = start; | |
70c8a91c | 7069 | em->orig_start = start; |
d1310b2e | 7070 | em->len = len; |
a52d9a80 | 7071 | not_found_em: |
5f39d397 | 7072 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 7073 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 7074 | insert: |
b3b4aa74 | 7075 | btrfs_release_path(path); |
d1310b2e | 7076 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7077 | btrfs_err(fs_info, |
5d163e0e JM |
7078 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7079 | em->start, em->len, start, len); | |
a52d9a80 CM |
7080 | err = -EIO; |
7081 | goto out; | |
7082 | } | |
d1310b2e CM |
7083 | |
7084 | err = 0; | |
890871be | 7085 | write_lock(&em_tree->lock); |
09a2a8f9 | 7086 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7087 | /* it is possible that someone inserted the extent into the tree |
7088 | * while we had the lock dropped. It is also possible that | |
7089 | * an overlapping map exists in the tree | |
7090 | */ | |
a52d9a80 | 7091 | if (ret == -EEXIST) { |
3b951516 | 7092 | struct extent_map *existing; |
e6dcd2dc CM |
7093 | |
7094 | ret = 0; | |
7095 | ||
e6c4efd8 QW |
7096 | existing = search_extent_mapping(em_tree, start, len); |
7097 | /* | |
7098 | * existing will always be non-NULL, since there must be | |
7099 | * extent causing the -EEXIST. | |
7100 | */ | |
8dff9c85 | 7101 | if (existing->start == em->start && |
8e2bd3b7 | 7102 | extent_map_end(existing) >= extent_map_end(em) && |
8dff9c85 CM |
7103 | em->block_start == existing->block_start) { |
7104 | /* | |
8e2bd3b7 OS |
7105 | * The existing extent map already encompasses the |
7106 | * entire extent map we tried to add. | |
8dff9c85 CM |
7107 | */ |
7108 | free_extent_map(em); | |
7109 | em = existing; | |
7110 | err = 0; | |
7111 | ||
7112 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7113 | start <= existing->start) { |
e6c4efd8 QW |
7114 | /* |
7115 | * The existing extent map is the one nearest to | |
7116 | * the [start, start + len) range which overlaps | |
7117 | */ | |
7118 | err = merge_extent_mapping(em_tree, existing, | |
7119 | em, start); | |
e1c4b745 | 7120 | free_extent_map(existing); |
e6c4efd8 | 7121 | if (err) { |
3b951516 CM |
7122 | free_extent_map(em); |
7123 | em = NULL; | |
7124 | } | |
7125 | } else { | |
7126 | free_extent_map(em); | |
7127 | em = existing; | |
e6dcd2dc | 7128 | err = 0; |
a52d9a80 | 7129 | } |
a52d9a80 | 7130 | } |
890871be | 7131 | write_unlock(&em_tree->lock); |
a52d9a80 | 7132 | out: |
1abe9b8a | 7133 | |
fc4f21b1 | 7134 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7135 | |
527afb44 | 7136 | btrfs_free_path(path); |
a52d9a80 | 7137 | if (trans) { |
3a45bb20 | 7138 | ret = btrfs_end_transaction(trans); |
d397712b | 7139 | if (!err) |
a52d9a80 CM |
7140 | err = ret; |
7141 | } | |
a52d9a80 CM |
7142 | if (err) { |
7143 | free_extent_map(em); | |
a52d9a80 CM |
7144 | return ERR_PTR(err); |
7145 | } | |
79787eaa | 7146 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7147 | return em; |
7148 | } | |
7149 | ||
fc4f21b1 NB |
7150 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
7151 | struct page *page, | |
7152 | size_t pg_offset, u64 start, u64 len, | |
7153 | int create) | |
ec29ed5b CM |
7154 | { |
7155 | struct extent_map *em; | |
7156 | struct extent_map *hole_em = NULL; | |
7157 | u64 range_start = start; | |
7158 | u64 end; | |
7159 | u64 found; | |
7160 | u64 found_end; | |
7161 | int err = 0; | |
7162 | ||
7163 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7164 | if (IS_ERR(em)) | |
7165 | return em; | |
9986277e DC |
7166 | /* |
7167 | * If our em maps to: | |
7168 | * - a hole or | |
7169 | * - a pre-alloc extent, | |
7170 | * there might actually be delalloc bytes behind it. | |
7171 | */ | |
7172 | if (em->block_start != EXTENT_MAP_HOLE && | |
7173 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7174 | return em; | |
7175 | else | |
7176 | hole_em = em; | |
ec29ed5b CM |
7177 | |
7178 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7179 | end = start + len; | |
7180 | if (end < start) | |
7181 | end = (u64)-1; | |
7182 | else | |
7183 | end -= 1; | |
7184 | ||
7185 | em = NULL; | |
7186 | ||
7187 | /* ok, we didn't find anything, lets look for delalloc */ | |
fc4f21b1 | 7188 | found = count_range_bits(&inode->io_tree, &range_start, |
ec29ed5b CM |
7189 | end, len, EXTENT_DELALLOC, 1); |
7190 | found_end = range_start + found; | |
7191 | if (found_end < range_start) | |
7192 | found_end = (u64)-1; | |
7193 | ||
7194 | /* | |
7195 | * we didn't find anything useful, return | |
7196 | * the original results from get_extent() | |
7197 | */ | |
7198 | if (range_start > end || found_end <= start) { | |
7199 | em = hole_em; | |
7200 | hole_em = NULL; | |
7201 | goto out; | |
7202 | } | |
7203 | ||
7204 | /* adjust the range_start to make sure it doesn't | |
7205 | * go backwards from the start they passed in | |
7206 | */ | |
67871254 | 7207 | range_start = max(start, range_start); |
ec29ed5b CM |
7208 | found = found_end - range_start; |
7209 | ||
7210 | if (found > 0) { | |
7211 | u64 hole_start = start; | |
7212 | u64 hole_len = len; | |
7213 | ||
172ddd60 | 7214 | em = alloc_extent_map(); |
ec29ed5b CM |
7215 | if (!em) { |
7216 | err = -ENOMEM; | |
7217 | goto out; | |
7218 | } | |
7219 | /* | |
7220 | * when btrfs_get_extent can't find anything it | |
7221 | * returns one huge hole | |
7222 | * | |
7223 | * make sure what it found really fits our range, and | |
7224 | * adjust to make sure it is based on the start from | |
7225 | * the caller | |
7226 | */ | |
7227 | if (hole_em) { | |
7228 | u64 calc_end = extent_map_end(hole_em); | |
7229 | ||
7230 | if (calc_end <= start || (hole_em->start > end)) { | |
7231 | free_extent_map(hole_em); | |
7232 | hole_em = NULL; | |
7233 | } else { | |
7234 | hole_start = max(hole_em->start, start); | |
7235 | hole_len = calc_end - hole_start; | |
7236 | } | |
7237 | } | |
7238 | em->bdev = NULL; | |
7239 | if (hole_em && range_start > hole_start) { | |
7240 | /* our hole starts before our delalloc, so we | |
7241 | * have to return just the parts of the hole | |
7242 | * that go until the delalloc starts | |
7243 | */ | |
7244 | em->len = min(hole_len, | |
7245 | range_start - hole_start); | |
7246 | em->start = hole_start; | |
7247 | em->orig_start = hole_start; | |
7248 | /* | |
7249 | * don't adjust block start at all, | |
7250 | * it is fixed at EXTENT_MAP_HOLE | |
7251 | */ | |
7252 | em->block_start = hole_em->block_start; | |
7253 | em->block_len = hole_len; | |
f9e4fb53 LB |
7254 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7255 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7256 | } else { |
7257 | em->start = range_start; | |
7258 | em->len = found; | |
7259 | em->orig_start = range_start; | |
7260 | em->block_start = EXTENT_MAP_DELALLOC; | |
7261 | em->block_len = found; | |
7262 | } | |
7263 | } else if (hole_em) { | |
7264 | return hole_em; | |
7265 | } | |
7266 | out: | |
7267 | ||
7268 | free_extent_map(hole_em); | |
7269 | if (err) { | |
7270 | free_extent_map(em); | |
7271 | return ERR_PTR(err); | |
7272 | } | |
7273 | return em; | |
7274 | } | |
7275 | ||
5f9a8a51 FM |
7276 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7277 | const u64 start, | |
7278 | const u64 len, | |
7279 | const u64 orig_start, | |
7280 | const u64 block_start, | |
7281 | const u64 block_len, | |
7282 | const u64 orig_block_len, | |
7283 | const u64 ram_bytes, | |
7284 | const int type) | |
7285 | { | |
7286 | struct extent_map *em = NULL; | |
7287 | int ret; | |
7288 | ||
5f9a8a51 | 7289 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7290 | em = create_io_em(inode, start, len, orig_start, |
7291 | block_start, block_len, orig_block_len, | |
7292 | ram_bytes, | |
7293 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7294 | type); | |
5f9a8a51 FM |
7295 | if (IS_ERR(em)) |
7296 | goto out; | |
7297 | } | |
7298 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7299 | len, block_len, type); | |
7300 | if (ret) { | |
7301 | if (em) { | |
7302 | free_extent_map(em); | |
dcdbc059 | 7303 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7304 | start + len - 1, 0); |
7305 | } | |
7306 | em = ERR_PTR(ret); | |
7307 | } | |
7308 | out: | |
5f9a8a51 FM |
7309 | |
7310 | return em; | |
7311 | } | |
7312 | ||
4b46fce2 JB |
7313 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7314 | u64 start, u64 len) | |
7315 | { | |
0b246afa | 7316 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7318 | struct extent_map *em; |
4b46fce2 JB |
7319 | struct btrfs_key ins; |
7320 | u64 alloc_hint; | |
7321 | int ret; | |
4b46fce2 | 7322 | |
4b46fce2 | 7323 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7324 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7325 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7326 | if (ret) |
7327 | return ERR_PTR(ret); | |
4b46fce2 | 7328 | |
5f9a8a51 FM |
7329 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7330 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7331 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7332 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7333 | if (IS_ERR(em)) |
2ff7e61e JM |
7334 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7335 | ins.offset, 1); | |
de0ee0ed | 7336 | |
4b46fce2 JB |
7337 | return em; |
7338 | } | |
7339 | ||
46bfbb5c CM |
7340 | /* |
7341 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7342 | * block must be cow'd | |
7343 | */ | |
00361589 | 7344 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7345 | u64 *orig_start, u64 *orig_block_len, |
7346 | u64 *ram_bytes) | |
46bfbb5c | 7347 | { |
2ff7e61e | 7348 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7349 | struct btrfs_path *path; |
7350 | int ret; | |
7351 | struct extent_buffer *leaf; | |
7352 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7353 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7354 | struct btrfs_file_extent_item *fi; |
7355 | struct btrfs_key key; | |
7356 | u64 disk_bytenr; | |
7357 | u64 backref_offset; | |
7358 | u64 extent_end; | |
7359 | u64 num_bytes; | |
7360 | int slot; | |
7361 | int found_type; | |
7ee9e440 | 7362 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7363 | |
46bfbb5c CM |
7364 | path = btrfs_alloc_path(); |
7365 | if (!path) | |
7366 | return -ENOMEM; | |
7367 | ||
f85b7379 DS |
7368 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7369 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7370 | if (ret < 0) |
7371 | goto out; | |
7372 | ||
7373 | slot = path->slots[0]; | |
7374 | if (ret == 1) { | |
7375 | if (slot == 0) { | |
7376 | /* can't find the item, must cow */ | |
7377 | ret = 0; | |
7378 | goto out; | |
7379 | } | |
7380 | slot--; | |
7381 | } | |
7382 | ret = 0; | |
7383 | leaf = path->nodes[0]; | |
7384 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7385 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7386 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7387 | /* not our file or wrong item type, must cow */ | |
7388 | goto out; | |
7389 | } | |
7390 | ||
7391 | if (key.offset > offset) { | |
7392 | /* Wrong offset, must cow */ | |
7393 | goto out; | |
7394 | } | |
7395 | ||
7396 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7397 | found_type = btrfs_file_extent_type(leaf, fi); | |
7398 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7399 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7400 | /* not a regular extent, must cow */ | |
7401 | goto out; | |
7402 | } | |
7ee9e440 JB |
7403 | |
7404 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7405 | goto out; | |
7406 | ||
e77751aa MX |
7407 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7408 | if (extent_end <= offset) | |
7409 | goto out; | |
7410 | ||
46bfbb5c | 7411 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7412 | if (disk_bytenr == 0) |
7413 | goto out; | |
7414 | ||
7415 | if (btrfs_file_extent_compression(leaf, fi) || | |
7416 | btrfs_file_extent_encryption(leaf, fi) || | |
7417 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7418 | goto out; | |
7419 | ||
46bfbb5c CM |
7420 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7421 | ||
7ee9e440 JB |
7422 | if (orig_start) { |
7423 | *orig_start = key.offset - backref_offset; | |
7424 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7425 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7426 | } | |
eb384b55 | 7427 | |
2ff7e61e | 7428 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7429 | goto out; |
7b2b7085 MX |
7430 | |
7431 | num_bytes = min(offset + *len, extent_end) - offset; | |
7432 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7433 | u64 range_end; | |
7434 | ||
da17066c JM |
7435 | range_end = round_up(offset + num_bytes, |
7436 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7437 | ret = test_range_bit(io_tree, offset, range_end, |
7438 | EXTENT_DELALLOC, 0, NULL); | |
7439 | if (ret) { | |
7440 | ret = -EAGAIN; | |
7441 | goto out; | |
7442 | } | |
7443 | } | |
7444 | ||
1bda19eb | 7445 | btrfs_release_path(path); |
46bfbb5c CM |
7446 | |
7447 | /* | |
7448 | * look for other files referencing this extent, if we | |
7449 | * find any we must cow | |
7450 | */ | |
00361589 | 7451 | |
e4c3b2dc | 7452 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7453 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7454 | if (ret) { |
7455 | ret = 0; | |
7456 | goto out; | |
7457 | } | |
46bfbb5c CM |
7458 | |
7459 | /* | |
7460 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7461 | * in this extent we are about to write. If there | |
7462 | * are any csums in that range we have to cow in order | |
7463 | * to keep the csums correct | |
7464 | */ | |
7465 | disk_bytenr += backref_offset; | |
7466 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7467 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7468 | goto out; | |
46bfbb5c CM |
7469 | /* |
7470 | * all of the above have passed, it is safe to overwrite this extent | |
7471 | * without cow | |
7472 | */ | |
eb384b55 | 7473 | *len = num_bytes; |
46bfbb5c CM |
7474 | ret = 1; |
7475 | out: | |
7476 | btrfs_free_path(path); | |
7477 | return ret; | |
7478 | } | |
7479 | ||
fc4adbff AG |
7480 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7481 | { | |
7482 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
e03733da | 7483 | bool found = false; |
fc4adbff AG |
7484 | void **pagep = NULL; |
7485 | struct page *page = NULL; | |
cc2b702c DS |
7486 | unsigned long start_idx; |
7487 | unsigned long end_idx; | |
fc4adbff | 7488 | |
09cbfeaf | 7489 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7490 | |
7491 | /* | |
7492 | * end is the last byte in the last page. end == start is legal | |
7493 | */ | |
09cbfeaf | 7494 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7495 | |
7496 | rcu_read_lock(); | |
7497 | ||
7498 | /* Most of the code in this while loop is lifted from | |
7499 | * find_get_page. It's been modified to begin searching from a | |
7500 | * page and return just the first page found in that range. If the | |
7501 | * found idx is less than or equal to the end idx then we know that | |
7502 | * a page exists. If no pages are found or if those pages are | |
7503 | * outside of the range then we're fine (yay!) */ | |
7504 | while (page == NULL && | |
7505 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7506 | page = radix_tree_deref_slot(pagep); | |
7507 | if (unlikely(!page)) | |
7508 | break; | |
7509 | ||
7510 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7511 | if (radix_tree_deref_retry(page)) { |
7512 | page = NULL; | |
fc4adbff | 7513 | continue; |
809f9016 | 7514 | } |
fc4adbff AG |
7515 | /* |
7516 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7517 | * here as an exceptional entry: so return it without | |
7518 | * attempting to raise page count. | |
7519 | */ | |
6fdef6d4 | 7520 | page = NULL; |
fc4adbff AG |
7521 | break; /* TODO: Is this relevant for this use case? */ |
7522 | } | |
7523 | ||
91405151 FM |
7524 | if (!page_cache_get_speculative(page)) { |
7525 | page = NULL; | |
fc4adbff | 7526 | continue; |
91405151 | 7527 | } |
fc4adbff AG |
7528 | |
7529 | /* | |
7530 | * Has the page moved? | |
7531 | * This is part of the lockless pagecache protocol. See | |
7532 | * include/linux/pagemap.h for details. | |
7533 | */ | |
7534 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7535 | put_page(page); |
fc4adbff AG |
7536 | page = NULL; |
7537 | } | |
7538 | } | |
7539 | ||
7540 | if (page) { | |
7541 | if (page->index <= end_idx) | |
7542 | found = true; | |
09cbfeaf | 7543 | put_page(page); |
fc4adbff AG |
7544 | } |
7545 | ||
7546 | rcu_read_unlock(); | |
7547 | return found; | |
7548 | } | |
7549 | ||
eb838e73 JB |
7550 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7551 | struct extent_state **cached_state, int writing) | |
7552 | { | |
7553 | struct btrfs_ordered_extent *ordered; | |
7554 | int ret = 0; | |
7555 | ||
7556 | while (1) { | |
7557 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7558 | cached_state); |
eb838e73 JB |
7559 | /* |
7560 | * We're concerned with the entire range that we're going to be | |
01327610 | 7561 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7562 | * extents in this range. |
7563 | */ | |
a776c6fa | 7564 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7565 | lockend - lockstart + 1); |
7566 | ||
7567 | /* | |
7568 | * We need to make sure there are no buffered pages in this | |
7569 | * range either, we could have raced between the invalidate in | |
7570 | * generic_file_direct_write and locking the extent. The | |
7571 | * invalidate needs to happen so that reads after a write do not | |
7572 | * get stale data. | |
7573 | */ | |
fc4adbff AG |
7574 | if (!ordered && |
7575 | (!writing || | |
7576 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7577 | break; |
7578 | ||
7579 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7580 | cached_state, GFP_NOFS); | |
7581 | ||
7582 | if (ordered) { | |
ade77029 FM |
7583 | /* |
7584 | * If we are doing a DIO read and the ordered extent we | |
7585 | * found is for a buffered write, we can not wait for it | |
7586 | * to complete and retry, because if we do so we can | |
7587 | * deadlock with concurrent buffered writes on page | |
7588 | * locks. This happens only if our DIO read covers more | |
7589 | * than one extent map, if at this point has already | |
7590 | * created an ordered extent for a previous extent map | |
7591 | * and locked its range in the inode's io tree, and a | |
7592 | * concurrent write against that previous extent map's | |
7593 | * range and this range started (we unlock the ranges | |
7594 | * in the io tree only when the bios complete and | |
7595 | * buffered writes always lock pages before attempting | |
7596 | * to lock range in the io tree). | |
7597 | */ | |
7598 | if (writing || | |
7599 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7600 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7601 | else | |
7602 | ret = -ENOTBLK; | |
eb838e73 JB |
7603 | btrfs_put_ordered_extent(ordered); |
7604 | } else { | |
eb838e73 | 7605 | /* |
b850ae14 FM |
7606 | * We could trigger writeback for this range (and wait |
7607 | * for it to complete) and then invalidate the pages for | |
7608 | * this range (through invalidate_inode_pages2_range()), | |
7609 | * but that can lead us to a deadlock with a concurrent | |
7610 | * call to readpages() (a buffered read or a defrag call | |
7611 | * triggered a readahead) on a page lock due to an | |
7612 | * ordered dio extent we created before but did not have | |
7613 | * yet a corresponding bio submitted (whence it can not | |
7614 | * complete), which makes readpages() wait for that | |
7615 | * ordered extent to complete while holding a lock on | |
7616 | * that page. | |
eb838e73 | 7617 | */ |
b850ae14 | 7618 | ret = -ENOTBLK; |
eb838e73 JB |
7619 | } |
7620 | ||
ade77029 FM |
7621 | if (ret) |
7622 | break; | |
7623 | ||
eb838e73 JB |
7624 | cond_resched(); |
7625 | } | |
7626 | ||
7627 | return ret; | |
7628 | } | |
7629 | ||
6f9994db LB |
7630 | /* The callers of this must take lock_extent() */ |
7631 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7632 | u64 orig_start, u64 block_start, | |
7633 | u64 block_len, u64 orig_block_len, | |
7634 | u64 ram_bytes, int compress_type, | |
7635 | int type) | |
69ffb543 JB |
7636 | { |
7637 | struct extent_map_tree *em_tree; | |
7638 | struct extent_map *em; | |
7639 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7640 | int ret; | |
7641 | ||
6f9994db LB |
7642 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7643 | type == BTRFS_ORDERED_COMPRESSED || | |
7644 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7645 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7646 | |
69ffb543 JB |
7647 | em_tree = &BTRFS_I(inode)->extent_tree; |
7648 | em = alloc_extent_map(); | |
7649 | if (!em) | |
7650 | return ERR_PTR(-ENOMEM); | |
7651 | ||
7652 | em->start = start; | |
7653 | em->orig_start = orig_start; | |
7654 | em->len = len; | |
7655 | em->block_len = block_len; | |
7656 | em->block_start = block_start; | |
7657 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7658 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7659 | em->ram_bytes = ram_bytes; |
70c8a91c | 7660 | em->generation = -1; |
69ffb543 | 7661 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7662 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7663 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7664 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7665 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7666 | em->compress_type = compress_type; | |
7667 | } | |
69ffb543 JB |
7668 | |
7669 | do { | |
dcdbc059 | 7670 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7671 | em->start + em->len - 1, 0); |
7672 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7673 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7674 | write_unlock(&em_tree->lock); |
6f9994db LB |
7675 | /* |
7676 | * The caller has taken lock_extent(), who could race with us | |
7677 | * to add em? | |
7678 | */ | |
69ffb543 JB |
7679 | } while (ret == -EEXIST); |
7680 | ||
7681 | if (ret) { | |
7682 | free_extent_map(em); | |
7683 | return ERR_PTR(ret); | |
7684 | } | |
7685 | ||
6f9994db | 7686 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7687 | return em; |
7688 | } | |
7689 | ||
9c9464cc FM |
7690 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7691 | struct btrfs_dio_data *dio_data, | |
7692 | const u64 len) | |
7693 | { | |
823bb20a | 7694 | unsigned num_extents = count_max_extents(len); |
9c9464cc | 7695 | |
9c9464cc FM |
7696 | /* |
7697 | * If we have an outstanding_extents count still set then we're | |
7698 | * within our reservation, otherwise we need to adjust our inode | |
7699 | * counter appropriately. | |
7700 | */ | |
c2931667 | 7701 | if (dio_data->outstanding_extents >= num_extents) { |
9c9464cc FM |
7702 | dio_data->outstanding_extents -= num_extents; |
7703 | } else { | |
c2931667 LB |
7704 | /* |
7705 | * If dio write length has been split due to no large enough | |
7706 | * contiguous space, we need to compensate our inode counter | |
7707 | * appropriately. | |
7708 | */ | |
7709 | u64 num_needed = num_extents - dio_data->outstanding_extents; | |
7710 | ||
9c9464cc | 7711 | spin_lock(&BTRFS_I(inode)->lock); |
c2931667 | 7712 | BTRFS_I(inode)->outstanding_extents += num_needed; |
9c9464cc FM |
7713 | spin_unlock(&BTRFS_I(inode)->lock); |
7714 | } | |
7715 | } | |
7716 | ||
4b46fce2 JB |
7717 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7718 | struct buffer_head *bh_result, int create) | |
7719 | { | |
0b246afa | 7720 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7721 | struct extent_map *em; |
eb838e73 | 7722 | struct extent_state *cached_state = NULL; |
50745b0a | 7723 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7724 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7725 | u64 lockstart, lockend; |
4b46fce2 | 7726 | u64 len = bh_result->b_size; |
eb838e73 | 7727 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7728 | int ret = 0; |
eb838e73 | 7729 | |
172a5049 | 7730 | if (create) |
3266789f | 7731 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7732 | else |
0b246afa | 7733 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7734 | |
c329861d JB |
7735 | lockstart = start; |
7736 | lockend = start + len - 1; | |
7737 | ||
e1cbbfa5 JB |
7738 | if (current->journal_info) { |
7739 | /* | |
7740 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7741 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7742 | * confused. |
7743 | */ | |
50745b0a | 7744 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7745 | current->journal_info = NULL; |
7746 | } | |
7747 | ||
eb838e73 JB |
7748 | /* |
7749 | * If this errors out it's because we couldn't invalidate pagecache for | |
7750 | * this range and we need to fallback to buffered. | |
7751 | */ | |
9c9464cc FM |
7752 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7753 | create)) { | |
7754 | ret = -ENOTBLK; | |
7755 | goto err; | |
7756 | } | |
eb838e73 | 7757 | |
fc4f21b1 | 7758 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7759 | if (IS_ERR(em)) { |
7760 | ret = PTR_ERR(em); | |
7761 | goto unlock_err; | |
7762 | } | |
4b46fce2 JB |
7763 | |
7764 | /* | |
7765 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7766 | * io. INLINE is special, and we could probably kludge it in here, but | |
7767 | * it's still buffered so for safety lets just fall back to the generic | |
7768 | * buffered path. | |
7769 | * | |
7770 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7771 | * decompress it, so there will be buffering required no matter what we | |
7772 | * do, so go ahead and fallback to buffered. | |
7773 | * | |
01327610 | 7774 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7775 | * to buffered IO. Don't blame me, this is the price we pay for using |
7776 | * the generic code. | |
7777 | */ | |
7778 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7779 | em->block_start == EXTENT_MAP_INLINE) { | |
7780 | free_extent_map(em); | |
eb838e73 JB |
7781 | ret = -ENOTBLK; |
7782 | goto unlock_err; | |
4b46fce2 JB |
7783 | } |
7784 | ||
7785 | /* Just a good old fashioned hole, return */ | |
7786 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7787 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7788 | free_extent_map(em); | |
eb838e73 | 7789 | goto unlock_err; |
4b46fce2 JB |
7790 | } |
7791 | ||
7792 | /* | |
7793 | * We don't allocate a new extent in the following cases | |
7794 | * | |
7795 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7796 | * existing extent. | |
7797 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7798 | * just use the extent. | |
7799 | * | |
7800 | */ | |
46bfbb5c | 7801 | if (!create) { |
eb838e73 JB |
7802 | len = min(len, em->len - (start - em->start)); |
7803 | lockstart = start + len; | |
7804 | goto unlock; | |
46bfbb5c | 7805 | } |
4b46fce2 JB |
7806 | |
7807 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7808 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7809 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7810 | int type; |
eb384b55 | 7811 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7812 | |
7813 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7814 | type = BTRFS_ORDERED_PREALLOC; | |
7815 | else | |
7816 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7817 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7818 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7819 | |
00361589 | 7820 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c | 7821 | &orig_block_len, &ram_bytes) == 1 && |
0b246afa | 7822 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
5f9a8a51 | 7823 | struct extent_map *em2; |
0b901916 | 7824 | |
5f9a8a51 FM |
7825 | em2 = btrfs_create_dio_extent(inode, start, len, |
7826 | orig_start, block_start, | |
7827 | len, orig_block_len, | |
7828 | ram_bytes, type); | |
0b246afa | 7829 | btrfs_dec_nocow_writers(fs_info, block_start); |
69ffb543 JB |
7830 | if (type == BTRFS_ORDERED_PREALLOC) { |
7831 | free_extent_map(em); | |
5f9a8a51 | 7832 | em = em2; |
69ffb543 | 7833 | } |
5f9a8a51 FM |
7834 | if (em2 && IS_ERR(em2)) { |
7835 | ret = PTR_ERR(em2); | |
eb838e73 | 7836 | goto unlock_err; |
46bfbb5c | 7837 | } |
18513091 WX |
7838 | /* |
7839 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7840 | * use the existing or preallocated extent, so does not | |
7841 | * need to adjust btrfs_space_info's bytes_may_use. | |
7842 | */ | |
7843 | btrfs_free_reserved_data_space_noquota(inode, | |
7844 | start, len); | |
46bfbb5c | 7845 | goto unlock; |
4b46fce2 | 7846 | } |
4b46fce2 | 7847 | } |
00361589 | 7848 | |
46bfbb5c CM |
7849 | /* |
7850 | * this will cow the extent, reset the len in case we changed | |
7851 | * it above | |
7852 | */ | |
7853 | len = bh_result->b_size; | |
70c8a91c JB |
7854 | free_extent_map(em); |
7855 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7856 | if (IS_ERR(em)) { |
7857 | ret = PTR_ERR(em); | |
7858 | goto unlock_err; | |
7859 | } | |
46bfbb5c CM |
7860 | len = min(len, em->len - (start - em->start)); |
7861 | unlock: | |
4b46fce2 JB |
7862 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7863 | inode->i_blkbits; | |
46bfbb5c | 7864 | bh_result->b_size = len; |
4b46fce2 JB |
7865 | bh_result->b_bdev = em->bdev; |
7866 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7867 | if (create) { |
7868 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7869 | set_buffer_new(bh_result); | |
7870 | ||
7871 | /* | |
7872 | * Need to update the i_size under the extent lock so buffered | |
7873 | * readers will get the updated i_size when we unlock. | |
7874 | */ | |
4aaedfb0 | 7875 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
c3473e83 | 7876 | i_size_write(inode, start + len); |
0934856d | 7877 | |
9c9464cc | 7878 | adjust_dio_outstanding_extents(inode, dio_data, len); |
50745b0a | 7879 | WARN_ON(dio_data->reserve < len); |
7880 | dio_data->reserve -= len; | |
f28a4928 | 7881 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7882 | current->journal_info = dio_data; |
c3473e83 | 7883 | } |
4b46fce2 | 7884 | |
eb838e73 JB |
7885 | /* |
7886 | * In the case of write we need to clear and unlock the entire range, | |
7887 | * in the case of read we need to unlock only the end area that we | |
7888 | * aren't using if there is any left over space. | |
7889 | */ | |
24c03fa5 | 7890 | if (lockstart < lockend) { |
0934856d MX |
7891 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7892 | lockend, unlock_bits, 1, 0, | |
7893 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7894 | } else { |
eb838e73 | 7895 | free_extent_state(cached_state); |
24c03fa5 | 7896 | } |
eb838e73 | 7897 | |
4b46fce2 JB |
7898 | free_extent_map(em); |
7899 | ||
7900 | return 0; | |
eb838e73 JB |
7901 | |
7902 | unlock_err: | |
eb838e73 JB |
7903 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7904 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7905 | err: |
50745b0a | 7906 | if (dio_data) |
7907 | current->journal_info = dio_data; | |
9c9464cc FM |
7908 | /* |
7909 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7910 | * write less data then expected, so that we don't underflow our inode's | |
7911 | * outstanding extents counter. | |
7912 | */ | |
7913 | if (create && dio_data) | |
7914 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7915 | ||
eb838e73 | 7916 | return ret; |
4b46fce2 JB |
7917 | } |
7918 | ||
8b110e39 | 7919 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
81a75f67 | 7920 | int mirror_num) |
8b110e39 | 7921 | { |
2ff7e61e | 7922 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7923 | int ret; |
7924 | ||
37226b21 | 7925 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
7926 | |
7927 | bio_get(bio); | |
7928 | ||
2ff7e61e | 7929 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 MX |
7930 | if (ret) |
7931 | goto err; | |
7932 | ||
2ff7e61e | 7933 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
8b110e39 MX |
7934 | err: |
7935 | bio_put(bio); | |
7936 | return ret; | |
7937 | } | |
7938 | ||
7939 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7940 | struct bio *failed_bio, | |
7941 | struct io_failure_record *failrec, | |
7942 | int failed_mirror) | |
7943 | { | |
ab8d0fc4 | 7944 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7945 | int num_copies; |
7946 | ||
ab8d0fc4 | 7947 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7948 | if (num_copies == 1) { |
7949 | /* | |
7950 | * we only have a single copy of the data, so don't bother with | |
7951 | * all the retry and error correction code that follows. no | |
7952 | * matter what the error is, it is very likely to persist. | |
7953 | */ | |
ab8d0fc4 JM |
7954 | btrfs_debug(fs_info, |
7955 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7956 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7957 | return 0; |
7958 | } | |
7959 | ||
7960 | failrec->failed_mirror = failed_mirror; | |
7961 | failrec->this_mirror++; | |
7962 | if (failrec->this_mirror == failed_mirror) | |
7963 | failrec->this_mirror++; | |
7964 | ||
7965 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7966 | btrfs_debug(fs_info, |
7967 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7968 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7969 | return 0; |
7970 | } | |
7971 | ||
7972 | return 1; | |
7973 | } | |
7974 | ||
7975 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
2dabb324 CR |
7976 | struct page *page, unsigned int pgoff, |
7977 | u64 start, u64 end, int failed_mirror, | |
7978 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7979 | { |
7980 | struct io_failure_record *failrec; | |
7870d082 JB |
7981 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7982 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
7983 | struct bio *bio; |
7984 | int isector; | |
70fd7614 | 7985 | int read_mode = 0; |
17347cec | 7986 | int segs; |
8b110e39 MX |
7987 | int ret; |
7988 | ||
37226b21 | 7989 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7990 | |
7991 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7992 | if (ret) | |
7993 | return ret; | |
7994 | ||
7995 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7996 | failed_mirror); | |
7997 | if (!ret) { | |
7870d082 | 7998 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
7999 | return -EIO; |
8000 | } | |
8001 | ||
17347cec LB |
8002 | segs = bio_segments(failed_bio); |
8003 | if (segs > 1 || | |
8004 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) | |
70fd7614 | 8005 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
8006 | |
8007 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
8008 | isector >>= inode->i_sb->s_blocksize_bits; | |
8009 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 8010 | pgoff, isector, repair_endio, repair_arg); |
8b110e39 | 8011 | if (!bio) { |
7870d082 | 8012 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8013 | return -EIO; |
8014 | } | |
37226b21 | 8015 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
8016 | |
8017 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
8018 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
8019 | read_mode, failrec->this_mirror, failrec->in_validation); | |
8020 | ||
81a75f67 | 8021 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8b110e39 | 8022 | if (ret) { |
7870d082 | 8023 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8024 | bio_put(bio); |
8025 | } | |
8026 | ||
8027 | return ret; | |
8028 | } | |
8029 | ||
8030 | struct btrfs_retry_complete { | |
8031 | struct completion done; | |
8032 | struct inode *inode; | |
8033 | u64 start; | |
8034 | int uptodate; | |
8035 | }; | |
8036 | ||
4246a0b6 | 8037 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
8038 | { |
8039 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 8040 | struct inode *inode = done->inode; |
8b110e39 | 8041 | struct bio_vec *bvec; |
7870d082 | 8042 | struct extent_io_tree *io_tree, *failure_tree; |
8b110e39 MX |
8043 | int i; |
8044 | ||
4246a0b6 | 8045 | if (bio->bi_error) |
8b110e39 MX |
8046 | goto end; |
8047 | ||
2dabb324 | 8048 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
8049 | io_tree = &BTRFS_I(inode)->io_tree; |
8050 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8051 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); | |
2dabb324 | 8052 | |
8b110e39 MX |
8053 | done->uptodate = 1; |
8054 | bio_for_each_segment_all(bvec, bio, i) | |
7870d082 JB |
8055 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
8056 | io_tree, done->start, bvec->bv_page, | |
8057 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
8058 | end: |
8059 | complete(&done->done); | |
8060 | bio_put(bio); | |
8061 | } | |
8062 | ||
8063 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
8064 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 8065 | { |
2dabb324 | 8066 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8067 | struct bio_vec bvec; |
8068 | struct bvec_iter iter; | |
8b110e39 | 8069 | struct btrfs_retry_complete done; |
4b46fce2 | 8070 | u64 start; |
2dabb324 CR |
8071 | unsigned int pgoff; |
8072 | u32 sectorsize; | |
8073 | int nr_sectors; | |
c1dc0896 | 8074 | int ret; |
629ebf4f | 8075 | int err = 0; |
4b46fce2 | 8076 | |
2dabb324 | 8077 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8078 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8079 | |
8b110e39 MX |
8080 | start = io_bio->logical; |
8081 | done.inode = inode; | |
17347cec | 8082 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8083 | |
17347cec LB |
8084 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8085 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8086 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
8087 | |
8088 | next_block_or_try_again: | |
8b110e39 MX |
8089 | done.uptodate = 0; |
8090 | done.start = start; | |
8091 | init_completion(&done.done); | |
8092 | ||
17347cec | 8093 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8094 | pgoff, start, start + sectorsize - 1, |
8095 | io_bio->mirror_num, | |
8096 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
8097 | if (ret) { |
8098 | err = ret; | |
8099 | goto next; | |
8100 | } | |
8b110e39 MX |
8101 | |
8102 | wait_for_completion(&done.done); | |
8103 | ||
8104 | if (!done.uptodate) { | |
8105 | /* We might have another mirror, so try again */ | |
2dabb324 | 8106 | goto next_block_or_try_again; |
8b110e39 MX |
8107 | } |
8108 | ||
629ebf4f | 8109 | next: |
2dabb324 CR |
8110 | start += sectorsize; |
8111 | ||
97bf5a55 LB |
8112 | nr_sectors--; |
8113 | if (nr_sectors) { | |
2dabb324 | 8114 | pgoff += sectorsize; |
97bf5a55 | 8115 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8116 | goto next_block_or_try_again; |
8117 | } | |
8b110e39 MX |
8118 | } |
8119 | ||
629ebf4f | 8120 | return err; |
8b110e39 MX |
8121 | } |
8122 | ||
4246a0b6 | 8123 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8124 | { |
8125 | struct btrfs_retry_complete *done = bio->bi_private; | |
8126 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
8127 | struct extent_io_tree *io_tree, *failure_tree; |
8128 | struct inode *inode = done->inode; | |
8b110e39 MX |
8129 | struct bio_vec *bvec; |
8130 | int uptodate; | |
8131 | int ret; | |
8132 | int i; | |
8133 | ||
4246a0b6 | 8134 | if (bio->bi_error) |
8b110e39 MX |
8135 | goto end; |
8136 | ||
8137 | uptodate = 1; | |
2dabb324 | 8138 | |
2dabb324 | 8139 | ASSERT(bio->bi_vcnt == 1); |
2e949b0a | 8140 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 8141 | |
7870d082 JB |
8142 | io_tree = &BTRFS_I(inode)->io_tree; |
8143 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8144 | ||
8b110e39 | 8145 | bio_for_each_segment_all(bvec, bio, i) { |
7870d082 JB |
8146 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
8147 | bvec->bv_offset, done->start, | |
8148 | bvec->bv_len); | |
8b110e39 | 8149 | if (!ret) |
7870d082 JB |
8150 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
8151 | failure_tree, io_tree, done->start, | |
8152 | bvec->bv_page, | |
8153 | btrfs_ino(BTRFS_I(inode)), | |
8154 | bvec->bv_offset); | |
8b110e39 MX |
8155 | else |
8156 | uptodate = 0; | |
8157 | } | |
8158 | ||
8159 | done->uptodate = uptodate; | |
8160 | end: | |
8161 | complete(&done->done); | |
8162 | bio_put(bio); | |
8163 | } | |
8164 | ||
8165 | static int __btrfs_subio_endio_read(struct inode *inode, | |
8166 | struct btrfs_io_bio *io_bio, int err) | |
8167 | { | |
2dabb324 | 8168 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8169 | struct bio_vec bvec; |
8170 | struct bvec_iter iter; | |
8b110e39 MX |
8171 | struct btrfs_retry_complete done; |
8172 | u64 start; | |
8173 | u64 offset = 0; | |
2dabb324 CR |
8174 | u32 sectorsize; |
8175 | int nr_sectors; | |
8176 | unsigned int pgoff; | |
8177 | int csum_pos; | |
ef7cdac1 | 8178 | bool uptodate = (err == 0); |
8b110e39 | 8179 | int ret; |
dc380aea | 8180 | |
2dabb324 | 8181 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8182 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8183 | |
8b110e39 | 8184 | err = 0; |
c1dc0896 | 8185 | start = io_bio->logical; |
8b110e39 | 8186 | done.inode = inode; |
17347cec | 8187 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8188 | |
17347cec LB |
8189 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8190 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 8191 | |
17347cec | 8192 | pgoff = bvec.bv_offset; |
2dabb324 | 8193 | next_block: |
ef7cdac1 LB |
8194 | if (uptodate) { |
8195 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8196 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8197 | bvec.bv_page, pgoff, start, sectorsize); | |
8198 | if (likely(!ret)) | |
8199 | goto next; | |
8200 | } | |
8b110e39 MX |
8201 | try_again: |
8202 | done.uptodate = 0; | |
8203 | done.start = start; | |
8204 | init_completion(&done.done); | |
8205 | ||
17347cec | 8206 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8207 | pgoff, start, start + sectorsize - 1, |
8208 | io_bio->mirror_num, | |
8209 | btrfs_retry_endio, &done); | |
8b110e39 MX |
8210 | if (ret) { |
8211 | err = ret; | |
8212 | goto next; | |
8213 | } | |
8214 | ||
8215 | wait_for_completion(&done.done); | |
8216 | ||
8217 | if (!done.uptodate) { | |
8218 | /* We might have another mirror, so try again */ | |
8219 | goto try_again; | |
8220 | } | |
8221 | next: | |
2dabb324 CR |
8222 | offset += sectorsize; |
8223 | start += sectorsize; | |
8224 | ||
8225 | ASSERT(nr_sectors); | |
8226 | ||
97bf5a55 LB |
8227 | nr_sectors--; |
8228 | if (nr_sectors) { | |
2dabb324 | 8229 | pgoff += sectorsize; |
97bf5a55 | 8230 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8231 | goto next_block; |
8232 | } | |
2c30c71b | 8233 | } |
c1dc0896 MX |
8234 | |
8235 | return err; | |
8236 | } | |
8237 | ||
8b110e39 MX |
8238 | static int btrfs_subio_endio_read(struct inode *inode, |
8239 | struct btrfs_io_bio *io_bio, int err) | |
8240 | { | |
8241 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8242 | ||
8243 | if (skip_csum) { | |
8244 | if (unlikely(err)) | |
8245 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8246 | else | |
8247 | return 0; | |
8248 | } else { | |
8249 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8250 | } | |
8251 | } | |
8252 | ||
4246a0b6 | 8253 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8254 | { |
8255 | struct btrfs_dio_private *dip = bio->bi_private; | |
8256 | struct inode *inode = dip->inode; | |
8257 | struct bio *dio_bio; | |
8258 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 8259 | int err = bio->bi_error; |
c1dc0896 | 8260 | |
e3d37fab | 8261 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) { |
8b110e39 | 8262 | err = btrfs_subio_endio_read(inode, io_bio, err); |
e3d37fab LB |
8263 | if (!err) |
8264 | bio->bi_error = 0; | |
8265 | } | |
c1dc0896 | 8266 | |
4b46fce2 | 8267 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8268 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8269 | dio_bio = dip->dio_bio; |
4b46fce2 | 8270 | |
4b46fce2 | 8271 | kfree(dip); |
c0da7aa1 | 8272 | |
1636d1d7 | 8273 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8274 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
8275 | |
8276 | if (io_bio->end_io) | |
8277 | io_bio->end_io(io_bio, err); | |
9be3395b | 8278 | bio_put(bio); |
4b46fce2 JB |
8279 | } |
8280 | ||
52427260 QW |
8281 | static void __endio_write_update_ordered(struct inode *inode, |
8282 | const u64 offset, const u64 bytes, | |
8283 | const bool uptodate) | |
4b46fce2 | 8284 | { |
0b246afa | 8285 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8286 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 QW |
8287 | struct btrfs_workqueue *wq; |
8288 | btrfs_work_func_t func; | |
14543774 FM |
8289 | u64 ordered_offset = offset; |
8290 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8291 | int ret; |
8292 | ||
52427260 QW |
8293 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
8294 | wq = fs_info->endio_freespace_worker; | |
8295 | func = btrfs_freespace_write_helper; | |
8296 | } else { | |
8297 | wq = fs_info->endio_write_workers; | |
8298 | func = btrfs_endio_write_helper; | |
8299 | } | |
8300 | ||
163cf09c CM |
8301 | again: |
8302 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8303 | &ordered_offset, | |
4246a0b6 | 8304 | ordered_bytes, |
14543774 | 8305 | uptodate); |
4b46fce2 | 8306 | if (!ret) |
163cf09c | 8307 | goto out_test; |
4b46fce2 | 8308 | |
52427260 QW |
8309 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
8310 | btrfs_queue_work(wq, &ordered->work); | |
163cf09c CM |
8311 | out_test: |
8312 | /* | |
8313 | * our bio might span multiple ordered extents. If we haven't | |
8314 | * completed the accounting for the whole dio, go back and try again | |
8315 | */ | |
14543774 FM |
8316 | if (ordered_offset < offset + bytes) { |
8317 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8318 | ordered = NULL; |
163cf09c CM |
8319 | goto again; |
8320 | } | |
14543774 FM |
8321 | } |
8322 | ||
8323 | static void btrfs_endio_direct_write(struct bio *bio) | |
8324 | { | |
8325 | struct btrfs_dio_private *dip = bio->bi_private; | |
8326 | struct bio *dio_bio = dip->dio_bio; | |
8327 | ||
52427260 QW |
8328 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
8329 | dip->bytes, !bio->bi_error); | |
4b46fce2 | 8330 | |
4b46fce2 | 8331 | kfree(dip); |
c0da7aa1 | 8332 | |
1636d1d7 | 8333 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8334 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 8335 | bio_put(bio); |
4b46fce2 JB |
8336 | } |
8337 | ||
c6100a4b | 8338 | static int __btrfs_submit_bio_start_direct_io(void *private_data, |
eaf25d93 CM |
8339 | struct bio *bio, int mirror_num, |
8340 | unsigned long bio_flags, u64 offset) | |
8341 | { | |
c6100a4b | 8342 | struct inode *inode = private_data; |
eaf25d93 | 8343 | int ret; |
2ff7e61e | 8344 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8345 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8346 | return 0; |
8347 | } | |
8348 | ||
4246a0b6 | 8349 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8350 | { |
8351 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 8352 | int err = bio->bi_error; |
e65e1535 | 8353 | |
8b110e39 MX |
8354 | if (err) |
8355 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8356 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8357 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8358 | bio->bi_opf, | |
8b110e39 MX |
8359 | (unsigned long long)bio->bi_iter.bi_sector, |
8360 | bio->bi_iter.bi_size, err); | |
8361 | ||
8362 | if (dip->subio_endio) | |
8363 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8364 | |
8365 | if (err) { | |
e65e1535 MX |
8366 | dip->errors = 1; |
8367 | ||
8368 | /* | |
8369 | * before atomic variable goto zero, we must make sure | |
8370 | * dip->errors is perceived to be set. | |
8371 | */ | |
4e857c58 | 8372 | smp_mb__before_atomic(); |
e65e1535 MX |
8373 | } |
8374 | ||
8375 | /* if there are more bios still pending for this dio, just exit */ | |
8376 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8377 | goto out; | |
8378 | ||
9be3395b | 8379 | if (dip->errors) { |
e65e1535 | 8380 | bio_io_error(dip->orig_bio); |
9be3395b | 8381 | } else { |
4246a0b6 CH |
8382 | dip->dio_bio->bi_error = 0; |
8383 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
8384 | } |
8385 | out: | |
8386 | bio_put(bio); | |
8387 | } | |
8388 | ||
2ff7e61e | 8389 | static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8390 | struct btrfs_dio_private *dip, |
8391 | struct bio *bio, | |
8392 | u64 file_offset) | |
8393 | { | |
8394 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8395 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8396 | int ret; | |
8397 | ||
8398 | /* | |
8399 | * We load all the csum data we need when we submit | |
8400 | * the first bio to reduce the csum tree search and | |
8401 | * contention. | |
8402 | */ | |
8403 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8404 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8405 | file_offset); |
8406 | if (ret) | |
8407 | return ret; | |
8408 | } | |
8409 | ||
8410 | if (bio == dip->orig_bio) | |
8411 | return 0; | |
8412 | ||
8413 | file_offset -= dip->logical_offset; | |
8414 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8415 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8416 | ||
8417 | return 0; | |
8418 | } | |
8419 | ||
e65e1535 | 8420 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
81a75f67 | 8421 | u64 file_offset, int skip_sum, |
c329861d | 8422 | int async_submit) |
e65e1535 | 8423 | { |
0b246afa | 8424 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8425 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8426 | bool write = bio_op(bio) == REQ_OP_WRITE; |
e65e1535 MX |
8427 | int ret; |
8428 | ||
b812ce28 JB |
8429 | if (async_submit) |
8430 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8431 | ||
e65e1535 | 8432 | bio_get(bio); |
5fd02043 JB |
8433 | |
8434 | if (!write) { | |
0b246afa | 8435 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8436 | if (ret) |
8437 | goto err; | |
8438 | } | |
e65e1535 | 8439 | |
1ae39938 JB |
8440 | if (skip_sum) |
8441 | goto map; | |
8442 | ||
8443 | if (write && async_submit) { | |
c6100a4b JB |
8444 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8445 | file_offset, inode, | |
0b246afa JM |
8446 | __btrfs_submit_bio_start_direct_io, |
8447 | __btrfs_submit_bio_done); | |
e65e1535 | 8448 | goto err; |
1ae39938 JB |
8449 | } else if (write) { |
8450 | /* | |
8451 | * If we aren't doing async submit, calculate the csum of the | |
8452 | * bio now. | |
8453 | */ | |
2ff7e61e | 8454 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8455 | if (ret) |
8456 | goto err; | |
23ea8e5a | 8457 | } else { |
2ff7e61e | 8458 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8459 | file_offset); |
c2db1073 TI |
8460 | if (ret) |
8461 | goto err; | |
8462 | } | |
1ae39938 | 8463 | map: |
2ff7e61e | 8464 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
e65e1535 MX |
8465 | err: |
8466 | bio_put(bio); | |
8467 | return ret; | |
8468 | } | |
8469 | ||
81a75f67 | 8470 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
e65e1535 MX |
8471 | int skip_sum) |
8472 | { | |
8473 | struct inode *inode = dip->inode; | |
0b246afa | 8474 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8475 | struct bio *bio; |
8476 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8477 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8478 | u64 file_offset = dip->logical_offset; |
e65e1535 | 8479 | u64 map_length; |
1ae39938 | 8480 | int async_submit = 0; |
725130ba LB |
8481 | u64 submit_len; |
8482 | int clone_offset = 0; | |
8483 | int clone_len; | |
5f4dc8fc | 8484 | int ret; |
e65e1535 | 8485 | |
4f024f37 | 8486 | map_length = orig_bio->bi_iter.bi_size; |
725130ba | 8487 | submit_len = map_length; |
0b246afa JM |
8488 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8489 | &map_length, NULL, 0); | |
7a5c3c9b | 8490 | if (ret) |
e65e1535 | 8491 | return -EIO; |
facc8a22 | 8492 | |
725130ba | 8493 | if (map_length >= submit_len) { |
02f57c7a | 8494 | bio = orig_bio; |
c1dc0896 | 8495 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8496 | goto submit; |
8497 | } | |
8498 | ||
53b381b3 | 8499 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8500 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8501 | async_submit = 0; |
8502 | else | |
8503 | async_submit = 1; | |
8504 | ||
725130ba LB |
8505 | /* bio split */ |
8506 | ASSERT(map_length <= INT_MAX); | |
02f57c7a | 8507 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8508 | do { |
725130ba | 8509 | clone_len = min_t(int, submit_len, map_length); |
02f57c7a | 8510 | |
725130ba LB |
8511 | /* |
8512 | * This will never fail as it's passing GPF_NOFS and | |
8513 | * the allocation is backed by btrfs_bioset. | |
8514 | */ | |
e477094f | 8515 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8516 | clone_len); |
8517 | bio->bi_private = dip; | |
8518 | bio->bi_end_io = btrfs_end_dio_bio; | |
8519 | btrfs_io_bio(bio)->logical = file_offset; | |
8520 | ||
8521 | ASSERT(submit_len >= clone_len); | |
8522 | submit_len -= clone_len; | |
8523 | if (submit_len == 0) | |
8524 | break; | |
e65e1535 | 8525 | |
725130ba LB |
8526 | /* |
8527 | * Increase the count before we submit the bio so we know | |
8528 | * the end IO handler won't happen before we increase the | |
8529 | * count. Otherwise, the dip might get freed before we're | |
8530 | * done setting it up. | |
8531 | */ | |
8532 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8533 | |
725130ba LB |
8534 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
8535 | async_submit); | |
8536 | if (ret) { | |
8537 | bio_put(bio); | |
8538 | atomic_dec(&dip->pending_bios); | |
8539 | goto out_err; | |
8540 | } | |
e65e1535 | 8541 | |
725130ba LB |
8542 | clone_offset += clone_len; |
8543 | start_sector += clone_len >> 9; | |
8544 | file_offset += clone_len; | |
5f4dc8fc | 8545 | |
725130ba LB |
8546 | map_length = submit_len; |
8547 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8548 | start_sector << 9, &map_length, NULL, 0); | |
8549 | if (ret) | |
8550 | goto out_err; | |
3c91ee69 | 8551 | } while (submit_len > 0); |
e65e1535 | 8552 | |
02f57c7a | 8553 | submit: |
81a75f67 | 8554 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
c329861d | 8555 | async_submit); |
e65e1535 MX |
8556 | if (!ret) |
8557 | return 0; | |
8558 | ||
8559 | bio_put(bio); | |
8560 | out_err: | |
8561 | dip->errors = 1; | |
8562 | /* | |
8563 | * before atomic variable goto zero, we must | |
8564 | * make sure dip->errors is perceived to be set. | |
8565 | */ | |
4e857c58 | 8566 | smp_mb__before_atomic(); |
e65e1535 MX |
8567 | if (atomic_dec_and_test(&dip->pending_bios)) |
8568 | bio_io_error(dip->orig_bio); | |
8569 | ||
8570 | /* bio_end_io() will handle error, so we needn't return it */ | |
8571 | return 0; | |
8572 | } | |
8573 | ||
8a4c1e42 MC |
8574 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8575 | loff_t file_offset) | |
4b46fce2 | 8576 | { |
61de718f | 8577 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8578 | struct bio *bio = NULL; |
8579 | struct btrfs_io_bio *io_bio; | |
4b46fce2 | 8580 | int skip_sum; |
8a4c1e42 | 8581 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8582 | int ret = 0; |
8583 | ||
8584 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8585 | ||
8b6c1d56 | 8586 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8587 | |
c1dc0896 | 8588 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8589 | if (!dip) { |
8590 | ret = -ENOMEM; | |
61de718f | 8591 | goto free_ordered; |
4b46fce2 | 8592 | } |
4b46fce2 | 8593 | |
9be3395b | 8594 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8595 | dip->inode = inode; |
8596 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8597 | dip->bytes = dio_bio->bi_iter.bi_size; |
8598 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8599 | bio->bi_private = dip; |
8600 | dip->orig_bio = bio; | |
9be3395b | 8601 | dip->dio_bio = dio_bio; |
e65e1535 | 8602 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8603 | io_bio = btrfs_io_bio(bio); |
8604 | io_bio->logical = file_offset; | |
4b46fce2 | 8605 | |
c1dc0896 | 8606 | if (write) { |
3892ac90 | 8607 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8608 | } else { |
3892ac90 | 8609 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8610 | dip->subio_endio = btrfs_subio_endio_read; |
8611 | } | |
4b46fce2 | 8612 | |
f28a4928 FM |
8613 | /* |
8614 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8615 | * even if we fail to submit a bio, because in such case we do the | |
8616 | * corresponding error handling below and it must not be done a second | |
8617 | * time by btrfs_direct_IO(). | |
8618 | */ | |
8619 | if (write) { | |
8620 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8621 | ||
8622 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8623 | dip->bytes; | |
8624 | dio_data->unsubmitted_oe_range_start = | |
8625 | dio_data->unsubmitted_oe_range_end; | |
8626 | } | |
8627 | ||
81a75f67 | 8628 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
e65e1535 | 8629 | if (!ret) |
eaf25d93 | 8630 | return; |
9be3395b | 8631 | |
3892ac90 LB |
8632 | if (io_bio->end_io) |
8633 | io_bio->end_io(io_bio, ret); | |
9be3395b | 8634 | |
4b46fce2 JB |
8635 | free_ordered: |
8636 | /* | |
61de718f FM |
8637 | * If we arrived here it means either we failed to submit the dip |
8638 | * or we either failed to clone the dio_bio or failed to allocate the | |
8639 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8640 | * call bio_endio against our io_bio so that we get proper resource | |
8641 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8642 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8643 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8644 | */ |
3892ac90 | 8645 | if (bio && dip) { |
054ec2f6 | 8646 | bio_io_error(bio); |
61de718f | 8647 | /* |
3892ac90 | 8648 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8649 | * and all the cleanup and final put for dio_bio (through |
8650 | * dio_end_io()). | |
8651 | */ | |
8652 | dip = NULL; | |
3892ac90 | 8653 | bio = NULL; |
61de718f | 8654 | } else { |
14543774 | 8655 | if (write) |
52427260 | 8656 | __endio_write_update_ordered(inode, |
14543774 FM |
8657 | file_offset, |
8658 | dio_bio->bi_iter.bi_size, | |
52427260 | 8659 | false); |
14543774 | 8660 | else |
61de718f FM |
8661 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8662 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8663 | |
4246a0b6 | 8664 | dio_bio->bi_error = -EIO; |
61de718f FM |
8665 | /* |
8666 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8667 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8668 | */ | |
8669 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8670 | } |
3892ac90 LB |
8671 | if (bio) |
8672 | bio_put(bio); | |
61de718f | 8673 | kfree(dip); |
4b46fce2 JB |
8674 | } |
8675 | ||
2ff7e61e JM |
8676 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
8677 | struct kiocb *iocb, | |
8678 | const struct iov_iter *iter, loff_t offset) | |
5a5f79b5 CM |
8679 | { |
8680 | int seg; | |
a1b75f7d | 8681 | int i; |
0b246afa | 8682 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8683 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8684 | |
8685 | if (offset & blocksize_mask) | |
8686 | goto out; | |
8687 | ||
28060d5d AV |
8688 | if (iov_iter_alignment(iter) & blocksize_mask) |
8689 | goto out; | |
a1b75f7d | 8690 | |
28060d5d | 8691 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8692 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8693 | return 0; |
8694 | /* | |
8695 | * Check to make sure we don't have duplicate iov_base's in this | |
8696 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8697 | * when reading back. | |
8698 | */ | |
8699 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8700 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8701 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8702 | goto out; |
8703 | } | |
5a5f79b5 CM |
8704 | } |
8705 | retval = 0; | |
8706 | out: | |
8707 | return retval; | |
8708 | } | |
eb838e73 | 8709 | |
c8b8e32d | 8710 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8711 | { |
4b46fce2 JB |
8712 | struct file *file = iocb->ki_filp; |
8713 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8714 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8715 | struct btrfs_dio_data dio_data = { 0 }; |
c8b8e32d | 8716 | loff_t offset = iocb->ki_pos; |
0934856d | 8717 | size_t count = 0; |
2e60a51e | 8718 | int flags = 0; |
38851cc1 MX |
8719 | bool wakeup = true; |
8720 | bool relock = false; | |
0934856d | 8721 | ssize_t ret; |
4b46fce2 | 8722 | |
2ff7e61e | 8723 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
5a5f79b5 | 8724 | return 0; |
3f7c579c | 8725 | |
fe0f07d0 | 8726 | inode_dio_begin(inode); |
4e857c58 | 8727 | smp_mb__after_atomic(); |
38851cc1 | 8728 | |
0e267c44 | 8729 | /* |
41bd9ca4 MX |
8730 | * The generic stuff only does filemap_write_and_wait_range, which |
8731 | * isn't enough if we've written compressed pages to this area, so | |
8732 | * we need to flush the dirty pages again to make absolutely sure | |
8733 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8734 | */ |
a6cbcd4a | 8735 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8736 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8737 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8738 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8739 | offset + count - 1); | |
0e267c44 | 8740 | |
6f673763 | 8741 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8742 | /* |
8743 | * If the write DIO is beyond the EOF, we need update | |
8744 | * the isize, but it is protected by i_mutex. So we can | |
8745 | * not unlock the i_mutex at this case. | |
8746 | */ | |
8747 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8748 | dio_data.overwrite = 1; |
5955102c | 8749 | inode_unlock(inode); |
38851cc1 MX |
8750 | relock = true; |
8751 | } | |
7cf5b976 | 8752 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
0934856d | 8753 | if (ret) |
38851cc1 | 8754 | goto out; |
823bb20a | 8755 | dio_data.outstanding_extents = count_max_extents(count); |
e1cbbfa5 JB |
8756 | |
8757 | /* | |
8758 | * We need to know how many extents we reserved so that we can | |
8759 | * do the accounting properly if we go over the number we | |
8760 | * originally calculated. Abuse current->journal_info for this. | |
8761 | */ | |
da17066c | 8762 | dio_data.reserve = round_up(count, |
0b246afa | 8763 | fs_info->sectorsize); |
f28a4928 FM |
8764 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8765 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8766 | current->journal_info = &dio_data; |
97dcdea0 | 8767 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8768 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8769 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8770 | inode_dio_end(inode); |
38851cc1 MX |
8771 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8772 | wakeup = false; | |
0934856d MX |
8773 | } |
8774 | ||
17f8c842 | 8775 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8776 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8777 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8778 | btrfs_submit_direct, flags); |
6f673763 | 8779 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8780 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8781 | current->journal_info = NULL; |
ddba1bfc | 8782 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8783 | if (dio_data.reserve) |
7cf5b976 QW |
8784 | btrfs_delalloc_release_space(inode, offset, |
8785 | dio_data.reserve); | |
f28a4928 FM |
8786 | /* |
8787 | * On error we might have left some ordered extents | |
8788 | * without submitting corresponding bios for them, so | |
8789 | * cleanup them up to avoid other tasks getting them | |
8790 | * and waiting for them to complete forever. | |
8791 | */ | |
8792 | if (dio_data.unsubmitted_oe_range_start < | |
8793 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8794 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8795 | dio_data.unsubmitted_oe_range_start, |
8796 | dio_data.unsubmitted_oe_range_end - | |
8797 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8798 | false); |
ddba1bfc | 8799 | } else if (ret >= 0 && (size_t)ret < count) |
7cf5b976 QW |
8800 | btrfs_delalloc_release_space(inode, offset, |
8801 | count - (size_t)ret); | |
0934856d | 8802 | } |
38851cc1 | 8803 | out: |
2e60a51e | 8804 | if (wakeup) |
fe0f07d0 | 8805 | inode_dio_end(inode); |
38851cc1 | 8806 | if (relock) |
5955102c | 8807 | inode_lock(inode); |
0934856d MX |
8808 | |
8809 | return ret; | |
16432985 CM |
8810 | } |
8811 | ||
05dadc09 TI |
8812 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8813 | ||
1506fcc8 YS |
8814 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8815 | __u64 start, __u64 len) | |
8816 | { | |
05dadc09 TI |
8817 | int ret; |
8818 | ||
8819 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8820 | if (ret) | |
8821 | return ret; | |
8822 | ||
ec29ed5b | 8823 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8824 | } |
8825 | ||
a52d9a80 | 8826 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8827 | { |
d1310b2e CM |
8828 | struct extent_io_tree *tree; |
8829 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8830 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8831 | } |
1832a6d5 | 8832 | |
a52d9a80 | 8833 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8834 | { |
d1310b2e | 8835 | struct extent_io_tree *tree; |
be7bd730 JB |
8836 | struct inode *inode = page->mapping->host; |
8837 | int ret; | |
b888db2b CM |
8838 | |
8839 | if (current->flags & PF_MEMALLOC) { | |
8840 | redirty_page_for_writepage(wbc, page); | |
8841 | unlock_page(page); | |
8842 | return 0; | |
8843 | } | |
be7bd730 JB |
8844 | |
8845 | /* | |
8846 | * If we are under memory pressure we will call this directly from the | |
8847 | * VM, we need to make sure we have the inode referenced for the ordered | |
8848 | * extent. If not just return like we didn't do anything. | |
8849 | */ | |
8850 | if (!igrab(inode)) { | |
8851 | redirty_page_for_writepage(wbc, page); | |
8852 | return AOP_WRITEPAGE_ACTIVATE; | |
8853 | } | |
d1310b2e | 8854 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8855 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8856 | btrfs_add_delayed_iput(inode); | |
8857 | return ret; | |
9ebefb18 CM |
8858 | } |
8859 | ||
48a3b636 ES |
8860 | static int btrfs_writepages(struct address_space *mapping, |
8861 | struct writeback_control *wbc) | |
b293f02e | 8862 | { |
d1310b2e | 8863 | struct extent_io_tree *tree; |
771ed689 | 8864 | |
d1310b2e | 8865 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8866 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8867 | } | |
8868 | ||
3ab2fb5a CM |
8869 | static int |
8870 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8871 | struct list_head *pages, unsigned nr_pages) | |
8872 | { | |
d1310b2e CM |
8873 | struct extent_io_tree *tree; |
8874 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8875 | return extent_readpages(tree, mapping, pages, nr_pages, |
8876 | btrfs_get_extent); | |
8877 | } | |
e6dcd2dc | 8878 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8879 | { |
d1310b2e CM |
8880 | struct extent_io_tree *tree; |
8881 | struct extent_map_tree *map; | |
a52d9a80 | 8882 | int ret; |
8c2383c3 | 8883 | |
d1310b2e CM |
8884 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8885 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8886 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8887 | if (ret == 1) { |
8888 | ClearPagePrivate(page); | |
8889 | set_page_private(page, 0); | |
09cbfeaf | 8890 | put_page(page); |
39279cc3 | 8891 | } |
a52d9a80 | 8892 | return ret; |
39279cc3 CM |
8893 | } |
8894 | ||
e6dcd2dc CM |
8895 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8896 | { | |
98509cfc CM |
8897 | if (PageWriteback(page) || PageDirty(page)) |
8898 | return 0; | |
3ba7ab22 | 8899 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8900 | } |
8901 | ||
d47992f8 LC |
8902 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8903 | unsigned int length) | |
39279cc3 | 8904 | { |
5fd02043 | 8905 | struct inode *inode = page->mapping->host; |
d1310b2e | 8906 | struct extent_io_tree *tree; |
e6dcd2dc | 8907 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8908 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8909 | u64 page_start = page_offset(page); |
09cbfeaf | 8910 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8911 | u64 start; |
8912 | u64 end; | |
131e404a | 8913 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8914 | |
8b62b72b CM |
8915 | /* |
8916 | * we have the page locked, so new writeback can't start, | |
8917 | * and the dirty bit won't be cleared while we are here. | |
8918 | * | |
8919 | * Wait for IO on this page so that we can safely clear | |
8920 | * the PagePrivate2 bit and do ordered accounting | |
8921 | */ | |
e6dcd2dc | 8922 | wait_on_page_writeback(page); |
8b62b72b | 8923 | |
5fd02043 | 8924 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8925 | if (offset) { |
8926 | btrfs_releasepage(page, GFP_NOFS); | |
8927 | return; | |
8928 | } | |
131e404a FDBM |
8929 | |
8930 | if (!inode_evicting) | |
ff13db41 | 8931 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8932 | again: |
8933 | start = page_start; | |
a776c6fa | 8934 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8935 | page_end - start + 1); |
e6dcd2dc | 8936 | if (ordered) { |
dbfdb6d1 | 8937 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8938 | /* |
8939 | * IO on this page will never be started, so we need | |
8940 | * to account for any ordered extents now | |
8941 | */ | |
131e404a | 8942 | if (!inode_evicting) |
dbfdb6d1 | 8943 | clear_extent_bit(tree, start, end, |
131e404a | 8944 | EXTENT_DIRTY | EXTENT_DELALLOC | |
a7e3b975 | 8945 | EXTENT_DELALLOC_NEW | |
131e404a FDBM |
8946 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
8947 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8948 | GFP_NOFS); | |
8b62b72b CM |
8949 | /* |
8950 | * whoever cleared the private bit is responsible | |
8951 | * for the finish_ordered_io | |
8952 | */ | |
77cef2ec JB |
8953 | if (TestClearPagePrivate2(page)) { |
8954 | struct btrfs_ordered_inode_tree *tree; | |
8955 | u64 new_len; | |
8956 | ||
8957 | tree = &BTRFS_I(inode)->ordered_tree; | |
8958 | ||
8959 | spin_lock_irq(&tree->lock); | |
8960 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8961 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8962 | if (new_len < ordered->truncated_len) |
8963 | ordered->truncated_len = new_len; | |
8964 | spin_unlock_irq(&tree->lock); | |
8965 | ||
8966 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8967 | start, |
8968 | end - start + 1, 1)) | |
77cef2ec | 8969 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8970 | } |
e6dcd2dc | 8971 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8972 | if (!inode_evicting) { |
8973 | cached_state = NULL; | |
dbfdb6d1 | 8974 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8975 | &cached_state); |
8976 | } | |
dbfdb6d1 CR |
8977 | |
8978 | start = end + 1; | |
8979 | if (start < page_end) | |
8980 | goto again; | |
131e404a FDBM |
8981 | } |
8982 | ||
b9d0b389 QW |
8983 | /* |
8984 | * Qgroup reserved space handler | |
8985 | * Page here will be either | |
8986 | * 1) Already written to disk | |
8987 | * In this case, its reserved space is released from data rsv map | |
8988 | * and will be freed by delayed_ref handler finally. | |
8989 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8990 | * space. | |
8991 | * 2) Not written to disk | |
0b34c261 GR |
8992 | * This means the reserved space should be freed here. However, |
8993 | * if a truncate invalidates the page (by clearing PageDirty) | |
8994 | * and the page is accounted for while allocating extent | |
8995 | * in btrfs_check_data_free_space() we let delayed_ref to | |
8996 | * free the entire extent. | |
b9d0b389 | 8997 | */ |
0b34c261 GR |
8998 | if (PageDirty(page)) |
8999 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); | |
131e404a FDBM |
9000 | if (!inode_evicting) { |
9001 | clear_extent_bit(tree, page_start, page_end, | |
9002 | EXTENT_LOCKED | EXTENT_DIRTY | | |
a7e3b975 FM |
9003 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
9004 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
131e404a FDBM |
9005 | &cached_state, GFP_NOFS); |
9006 | ||
9007 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 9008 | } |
e6dcd2dc | 9009 | |
4a096752 | 9010 | ClearPageChecked(page); |
9ad6b7bc | 9011 | if (PagePrivate(page)) { |
9ad6b7bc CM |
9012 | ClearPagePrivate(page); |
9013 | set_page_private(page, 0); | |
09cbfeaf | 9014 | put_page(page); |
9ad6b7bc | 9015 | } |
39279cc3 CM |
9016 | } |
9017 | ||
9ebefb18 CM |
9018 | /* |
9019 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
9020 | * called from a page fault handler when a page is first dirtied. Hence we must | |
9021 | * be careful to check for EOF conditions here. We set the page up correctly | |
9022 | * for a written page which means we get ENOSPC checking when writing into | |
9023 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
9024 | * support these features. | |
9025 | * | |
9026 | * We are not allowed to take the i_mutex here so we have to play games to | |
9027 | * protect against truncate races as the page could now be beyond EOF. Because | |
9028 | * vmtruncate() writes the inode size before removing pages, once we have the | |
9029 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
9030 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
9031 | * unlock the page. | |
9032 | */ | |
11bac800 | 9033 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 9034 | { |
c2ec175c | 9035 | struct page *page = vmf->page; |
11bac800 | 9036 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 9037 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
9038 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
9039 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 9040 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
9041 | char *kaddr; |
9042 | unsigned long zero_start; | |
9ebefb18 | 9043 | loff_t size; |
1832a6d5 | 9044 | int ret; |
9998eb70 | 9045 | int reserved = 0; |
d0b7da88 | 9046 | u64 reserved_space; |
a52d9a80 | 9047 | u64 page_start; |
e6dcd2dc | 9048 | u64 page_end; |
d0b7da88 CR |
9049 | u64 end; |
9050 | ||
09cbfeaf | 9051 | reserved_space = PAGE_SIZE; |
9ebefb18 | 9052 | |
b2b5ef5c | 9053 | sb_start_pagefault(inode->i_sb); |
df480633 | 9054 | page_start = page_offset(page); |
09cbfeaf | 9055 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 9056 | end = page_end; |
df480633 | 9057 | |
d0b7da88 CR |
9058 | /* |
9059 | * Reserving delalloc space after obtaining the page lock can lead to | |
9060 | * deadlock. For example, if a dirty page is locked by this function | |
9061 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
9062 | * dirty page write out, then the btrfs_writepage() function could | |
9063 | * end up waiting indefinitely to get a lock on the page currently | |
9064 | * being processed by btrfs_page_mkwrite() function. | |
9065 | */ | |
7cf5b976 | 9066 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
d0b7da88 | 9067 | reserved_space); |
9998eb70 | 9068 | if (!ret) { |
11bac800 | 9069 | ret = file_update_time(vmf->vma->vm_file); |
9998eb70 CM |
9070 | reserved = 1; |
9071 | } | |
56a76f82 NP |
9072 | if (ret) { |
9073 | if (ret == -ENOMEM) | |
9074 | ret = VM_FAULT_OOM; | |
9075 | else /* -ENOSPC, -EIO, etc */ | |
9076 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
9077 | if (reserved) |
9078 | goto out; | |
9079 | goto out_noreserve; | |
56a76f82 | 9080 | } |
1832a6d5 | 9081 | |
56a76f82 | 9082 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9083 | again: |
9ebefb18 | 9084 | lock_page(page); |
9ebefb18 | 9085 | size = i_size_read(inode); |
a52d9a80 | 9086 | |
9ebefb18 | 9087 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9088 | (page_start >= size)) { |
9ebefb18 CM |
9089 | /* page got truncated out from underneath us */ |
9090 | goto out_unlock; | |
9091 | } | |
e6dcd2dc CM |
9092 | wait_on_page_writeback(page); |
9093 | ||
ff13db41 | 9094 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9095 | set_page_extent_mapped(page); |
9096 | ||
eb84ae03 CM |
9097 | /* |
9098 | * we can't set the delalloc bits if there are pending ordered | |
9099 | * extents. Drop our locks and wait for them to finish | |
9100 | */ | |
a776c6fa NB |
9101 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
9102 | PAGE_SIZE); | |
e6dcd2dc | 9103 | if (ordered) { |
2ac55d41 JB |
9104 | unlock_extent_cached(io_tree, page_start, page_end, |
9105 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9106 | unlock_page(page); |
eb84ae03 | 9107 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9108 | btrfs_put_ordered_extent(ordered); |
9109 | goto again; | |
9110 | } | |
9111 | ||
09cbfeaf | 9112 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9113 | reserved_space = round_up(size - page_start, |
0b246afa | 9114 | fs_info->sectorsize); |
09cbfeaf | 9115 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
9116 | end = page_start + reserved_space - 1; |
9117 | spin_lock(&BTRFS_I(inode)->lock); | |
9118 | BTRFS_I(inode)->outstanding_extents++; | |
9119 | spin_unlock(&BTRFS_I(inode)->lock); | |
9120 | btrfs_delalloc_release_space(inode, page_start, | |
09cbfeaf | 9121 | PAGE_SIZE - reserved_space); |
d0b7da88 CR |
9122 | } |
9123 | } | |
9124 | ||
fbf19087 | 9125 | /* |
5416034f LB |
9126 | * page_mkwrite gets called when the page is firstly dirtied after it's |
9127 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9128 | * bits, thus in this case for space account reason, we still need to | |
9129 | * clear any delalloc bits within this page range since we have to | |
9130 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 9131 | */ |
d0b7da88 | 9132 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9133 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9134 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9135 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9136 | |
d0b7da88 | 9137 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9138 | &cached_state, 0); |
9ed74f2d | 9139 | if (ret) { |
2ac55d41 JB |
9140 | unlock_extent_cached(io_tree, page_start, page_end, |
9141 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9142 | ret = VM_FAULT_SIGBUS; |
9143 | goto out_unlock; | |
9144 | } | |
e6dcd2dc | 9145 | ret = 0; |
9ebefb18 CM |
9146 | |
9147 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9148 | if (page_start + PAGE_SIZE > size) |
9149 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9150 | else |
09cbfeaf | 9151 | zero_start = PAGE_SIZE; |
9ebefb18 | 9152 | |
09cbfeaf | 9153 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9154 | kaddr = kmap(page); |
09cbfeaf | 9155 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9156 | flush_dcache_page(page); |
9157 | kunmap(page); | |
9158 | } | |
247e743c | 9159 | ClearPageChecked(page); |
e6dcd2dc | 9160 | set_page_dirty(page); |
50a9b214 | 9161 | SetPageUptodate(page); |
5a3f23d5 | 9162 | |
0b246afa | 9163 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9164 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9165 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9166 | |
2ac55d41 | 9167 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9168 | |
9169 | out_unlock: | |
b2b5ef5c JK |
9170 | if (!ret) { |
9171 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 9172 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9173 | } |
9ebefb18 | 9174 | unlock_page(page); |
1832a6d5 | 9175 | out: |
d0b7da88 | 9176 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
9998eb70 | 9177 | out_noreserve: |
b2b5ef5c | 9178 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
9179 | return ret; |
9180 | } | |
9181 | ||
a41ad394 | 9182 | static int btrfs_truncate(struct inode *inode) |
39279cc3 | 9183 | { |
0b246afa | 9184 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9185 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9186 | struct btrfs_block_rsv *rsv; |
a71754fc | 9187 | int ret = 0; |
3893e33b | 9188 | int err = 0; |
39279cc3 | 9189 | struct btrfs_trans_handle *trans; |
0b246afa JM |
9190 | u64 mask = fs_info->sectorsize - 1; |
9191 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 9192 | |
0ef8b726 JB |
9193 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9194 | (u64)-1); | |
9195 | if (ret) | |
9196 | return ret; | |
39279cc3 | 9197 | |
fcb80c2a | 9198 | /* |
01327610 | 9199 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9200 | * 3 things going on here |
9201 | * | |
9202 | * 1) We need to reserve space for our orphan item and the space to | |
9203 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9204 | * orphan item because we didn't reserve space to remove it. | |
9205 | * | |
9206 | * 2) We need to reserve space to update our inode. | |
9207 | * | |
9208 | * 3) We need to have something to cache all the space that is going to | |
9209 | * be free'd up by the truncate operation, but also have some slack | |
9210 | * space reserved in case it uses space during the truncate (thank you | |
9211 | * very much snapshotting). | |
9212 | * | |
01327610 | 9213 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9214 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9215 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9216 | * doesn't end up using space reserved for updating the inode or |
9217 | * removing the orphan item. We also need to be able to stop the | |
9218 | * transaction and start a new one, which means we need to be able to | |
9219 | * update the inode several times, and we have no idea of knowing how | |
9220 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9221 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9222 | * Then there is the orphan item, which does indeed need to be held on |
9223 | * to for the whole operation, and we need nobody to touch this reserved | |
9224 | * space except the orphan code. | |
9225 | * | |
9226 | * So that leaves us with | |
9227 | * | |
9228 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9229 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9230 | * transaction reservation. | |
9231 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9232 | * updating the inode. | |
9233 | */ | |
2ff7e61e | 9234 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9235 | if (!rsv) |
9236 | return -ENOMEM; | |
4a338542 | 9237 | rsv->size = min_size; |
ca7e70f5 | 9238 | rsv->failfast = 1; |
f0cd846e | 9239 | |
907cbceb | 9240 | /* |
07127184 | 9241 | * 1 for the truncate slack space |
907cbceb JB |
9242 | * 1 for updating the inode. |
9243 | */ | |
f3fe820c | 9244 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9245 | if (IS_ERR(trans)) { |
9246 | err = PTR_ERR(trans); | |
9247 | goto out; | |
9248 | } | |
f0cd846e | 9249 | |
907cbceb | 9250 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9251 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
25d609f8 | 9252 | min_size, 0); |
fcb80c2a | 9253 | BUG_ON(ret); |
f0cd846e | 9254 | |
5dc562c5 JB |
9255 | /* |
9256 | * So if we truncate and then write and fsync we normally would just | |
9257 | * write the extents that changed, which is a problem if we need to | |
9258 | * first truncate that entire inode. So set this flag so we write out | |
9259 | * all of the extents in the inode to the sync log so we're completely | |
9260 | * safe. | |
9261 | */ | |
9262 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9263 | trans->block_rsv = rsv; |
907cbceb | 9264 | |
8082510e YZ |
9265 | while (1) { |
9266 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9267 | inode->i_size, | |
9268 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9269 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9270 | err = ret; |
8082510e | 9271 | break; |
3893e33b | 9272 | } |
39279cc3 | 9273 | |
0b246afa | 9274 | trans->block_rsv = &fs_info->trans_block_rsv; |
8082510e | 9275 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9276 | if (ret) { |
9277 | err = ret; | |
9278 | break; | |
9279 | } | |
ca7e70f5 | 9280 | |
3a45bb20 | 9281 | btrfs_end_transaction(trans); |
2ff7e61e | 9282 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9283 | |
9284 | trans = btrfs_start_transaction(root, 2); | |
9285 | if (IS_ERR(trans)) { | |
9286 | ret = err = PTR_ERR(trans); | |
9287 | trans = NULL; | |
9288 | break; | |
9289 | } | |
9290 | ||
47b5d646 | 9291 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9292 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
25d609f8 | 9293 | rsv, min_size, 0); |
ca7e70f5 JB |
9294 | BUG_ON(ret); /* shouldn't happen */ |
9295 | trans->block_rsv = rsv; | |
8082510e YZ |
9296 | } |
9297 | ||
9298 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9299 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 9300 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
3893e33b JB |
9301 | if (ret) |
9302 | err = ret; | |
8082510e YZ |
9303 | } |
9304 | ||
917c16b2 | 9305 | if (trans) { |
0b246afa | 9306 | trans->block_rsv = &fs_info->trans_block_rsv; |
917c16b2 CM |
9307 | ret = btrfs_update_inode(trans, root, inode); |
9308 | if (ret && !err) | |
9309 | err = ret; | |
7b128766 | 9310 | |
3a45bb20 | 9311 | ret = btrfs_end_transaction(trans); |
2ff7e61e | 9312 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9313 | } |
fcb80c2a | 9314 | out: |
2ff7e61e | 9315 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9316 | |
3893e33b JB |
9317 | if (ret && !err) |
9318 | err = ret; | |
a41ad394 | 9319 | |
3893e33b | 9320 | return err; |
39279cc3 CM |
9321 | } |
9322 | ||
d352ac68 CM |
9323 | /* |
9324 | * create a new subvolume directory/inode (helper for the ioctl). | |
9325 | */ | |
d2fb3437 | 9326 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9327 | struct btrfs_root *new_root, |
9328 | struct btrfs_root *parent_root, | |
9329 | u64 new_dirid) | |
39279cc3 | 9330 | { |
39279cc3 | 9331 | struct inode *inode; |
76dda93c | 9332 | int err; |
00e4e6b3 | 9333 | u64 index = 0; |
39279cc3 | 9334 | |
12fc9d09 FA |
9335 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9336 | new_dirid, new_dirid, | |
9337 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9338 | &index); | |
54aa1f4d | 9339 | if (IS_ERR(inode)) |
f46b5a66 | 9340 | return PTR_ERR(inode); |
39279cc3 CM |
9341 | inode->i_op = &btrfs_dir_inode_operations; |
9342 | inode->i_fop = &btrfs_dir_file_operations; | |
9343 | ||
bfe86848 | 9344 | set_nlink(inode, 1); |
6ef06d27 | 9345 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9346 | unlock_new_inode(inode); |
3b96362c | 9347 | |
63541927 FDBM |
9348 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9349 | if (err) | |
9350 | btrfs_err(new_root->fs_info, | |
351fd353 | 9351 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9352 | new_root->root_key.objectid, err); |
9353 | ||
76dda93c | 9354 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9355 | |
76dda93c | 9356 | iput(inode); |
ce598979 | 9357 | return err; |
39279cc3 CM |
9358 | } |
9359 | ||
39279cc3 CM |
9360 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9361 | { | |
9362 | struct btrfs_inode *ei; | |
2ead6ae7 | 9363 | struct inode *inode; |
39279cc3 CM |
9364 | |
9365 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9366 | if (!ei) | |
9367 | return NULL; | |
2ead6ae7 YZ |
9368 | |
9369 | ei->root = NULL; | |
2ead6ae7 | 9370 | ei->generation = 0; |
15ee9bc7 | 9371 | ei->last_trans = 0; |
257c62e1 | 9372 | ei->last_sub_trans = 0; |
e02119d5 | 9373 | ei->logged_trans = 0; |
2ead6ae7 | 9374 | ei->delalloc_bytes = 0; |
a7e3b975 | 9375 | ei->new_delalloc_bytes = 0; |
47059d93 | 9376 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9377 | ei->disk_i_size = 0; |
9378 | ei->flags = 0; | |
7709cde3 | 9379 | ei->csum_bytes = 0; |
2ead6ae7 | 9380 | ei->index_cnt = (u64)-1; |
67de1176 | 9381 | ei->dir_index = 0; |
2ead6ae7 | 9382 | ei->last_unlink_trans = 0; |
46d8bc34 | 9383 | ei->last_log_commit = 0; |
8089fe62 | 9384 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9385 | |
9e0baf60 JB |
9386 | spin_lock_init(&ei->lock); |
9387 | ei->outstanding_extents = 0; | |
9388 | ei->reserved_extents = 0; | |
2ead6ae7 | 9389 | |
72ac3c0d | 9390 | ei->runtime_flags = 0; |
261507a0 | 9391 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9392 | |
16cdcec7 MX |
9393 | ei->delayed_node = NULL; |
9394 | ||
9cc97d64 | 9395 | ei->i_otime.tv_sec = 0; |
9396 | ei->i_otime.tv_nsec = 0; | |
9397 | ||
2ead6ae7 | 9398 | inode = &ei->vfs_inode; |
a8067e02 | 9399 | extent_map_tree_init(&ei->extent_tree); |
c6100a4b JB |
9400 | extent_io_tree_init(&ei->io_tree, inode); |
9401 | extent_io_tree_init(&ei->io_failure_tree, inode); | |
0b32f4bb JB |
9402 | ei->io_tree.track_uptodate = 1; |
9403 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9404 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9405 | mutex_init(&ei->log_mutex); |
f248679e | 9406 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9407 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9408 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9409 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9410 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9411 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9412 | |
9413 | return inode; | |
39279cc3 CM |
9414 | } |
9415 | ||
aaedb55b JB |
9416 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9417 | void btrfs_test_destroy_inode(struct inode *inode) | |
9418 | { | |
dcdbc059 | 9419 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9420 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9421 | } | |
9422 | #endif | |
9423 | ||
fa0d7e3d NP |
9424 | static void btrfs_i_callback(struct rcu_head *head) |
9425 | { | |
9426 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9427 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9428 | } | |
9429 | ||
39279cc3 CM |
9430 | void btrfs_destroy_inode(struct inode *inode) |
9431 | { | |
0b246afa | 9432 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9433 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9434 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9435 | ||
b3d9b7a3 | 9436 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9437 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9438 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9439 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 | 9440 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9441 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9442 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9443 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9444 | |
a6dbd429 JB |
9445 | /* |
9446 | * This can happen where we create an inode, but somebody else also | |
9447 | * created the same inode and we need to destroy the one we already | |
9448 | * created. | |
9449 | */ | |
9450 | if (!root) | |
9451 | goto free; | |
9452 | ||
8a35d95f JB |
9453 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9454 | &BTRFS_I(inode)->runtime_flags)) { | |
0b246afa | 9455 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
4a0cc7ca | 9456 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 9457 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9458 | } |
7b128766 | 9459 | |
d397712b | 9460 | while (1) { |
e6dcd2dc CM |
9461 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9462 | if (!ordered) | |
9463 | break; | |
9464 | else { | |
0b246afa | 9465 | btrfs_err(fs_info, |
5d163e0e JM |
9466 | "found ordered extent %llu %llu on inode cleanup", |
9467 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9468 | btrfs_remove_ordered_extent(inode, ordered); |
9469 | btrfs_put_ordered_extent(ordered); | |
9470 | btrfs_put_ordered_extent(ordered); | |
9471 | } | |
9472 | } | |
56fa9d07 | 9473 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9474 | inode_tree_del(inode); |
dcdbc059 | 9475 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
a6dbd429 | 9476 | free: |
fa0d7e3d | 9477 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9478 | } |
9479 | ||
45321ac5 | 9480 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9481 | { |
9482 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9483 | |
6379ef9f NA |
9484 | if (root == NULL) |
9485 | return 1; | |
9486 | ||
fa6ac876 | 9487 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9488 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9489 | return 1; |
76dda93c | 9490 | else |
45321ac5 | 9491 | return generic_drop_inode(inode); |
76dda93c YZ |
9492 | } |
9493 | ||
0ee0fda0 | 9494 | static void init_once(void *foo) |
39279cc3 CM |
9495 | { |
9496 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9497 | ||
9498 | inode_init_once(&ei->vfs_inode); | |
9499 | } | |
9500 | ||
9501 | void btrfs_destroy_cachep(void) | |
9502 | { | |
8c0a8537 KS |
9503 | /* |
9504 | * Make sure all delayed rcu free inodes are flushed before we | |
9505 | * destroy cache. | |
9506 | */ | |
9507 | rcu_barrier(); | |
5598e900 KM |
9508 | kmem_cache_destroy(btrfs_inode_cachep); |
9509 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9510 | kmem_cache_destroy(btrfs_path_cachep); |
9511 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9512 | } |
9513 | ||
9514 | int btrfs_init_cachep(void) | |
9515 | { | |
837e1972 | 9516 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9517 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9518 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9519 | init_once); | |
39279cc3 CM |
9520 | if (!btrfs_inode_cachep) |
9521 | goto fail; | |
9601e3f6 | 9522 | |
837e1972 | 9523 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9524 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9525 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 | 9526 | if (!btrfs_trans_handle_cachep) |
39279cc3 | 9527 | goto fail; |
9601e3f6 | 9528 | |
837e1972 | 9529 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9530 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9531 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9532 | if (!btrfs_path_cachep) |
9533 | goto fail; | |
9601e3f6 | 9534 | |
837e1972 | 9535 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9536 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9537 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9538 | if (!btrfs_free_space_cachep) |
9539 | goto fail; | |
9540 | ||
39279cc3 CM |
9541 | return 0; |
9542 | fail: | |
9543 | btrfs_destroy_cachep(); | |
9544 | return -ENOMEM; | |
9545 | } | |
9546 | ||
a528d35e DH |
9547 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9548 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9549 | { |
df0af1a5 | 9550 | u64 delalloc_bytes; |
a528d35e | 9551 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9552 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9553 | u32 bi_flags = BTRFS_I(inode)->flags; |
9554 | ||
9555 | stat->result_mask |= STATX_BTIME; | |
9556 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9557 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9558 | if (bi_flags & BTRFS_INODE_APPEND) | |
9559 | stat->attributes |= STATX_ATTR_APPEND; | |
9560 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9561 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9562 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9563 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9564 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9565 | stat->attributes |= STATX_ATTR_NODUMP; | |
9566 | ||
9567 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9568 | STATX_ATTR_COMPRESSED | | |
9569 | STATX_ATTR_IMMUTABLE | | |
9570 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9571 | |
39279cc3 | 9572 | generic_fillattr(inode, stat); |
0ee5dc67 | 9573 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9574 | |
9575 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9576 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9577 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9578 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9579 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9580 | return 0; |
9581 | } | |
9582 | ||
cdd1fedf DF |
9583 | static int btrfs_rename_exchange(struct inode *old_dir, |
9584 | struct dentry *old_dentry, | |
9585 | struct inode *new_dir, | |
9586 | struct dentry *new_dentry) | |
9587 | { | |
0b246afa | 9588 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9589 | struct btrfs_trans_handle *trans; |
9590 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9591 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9592 | struct inode *new_inode = new_dentry->d_inode; | |
9593 | struct inode *old_inode = old_dentry->d_inode; | |
c2050a45 | 9594 | struct timespec ctime = current_time(old_inode); |
cdd1fedf | 9595 | struct dentry *parent; |
4a0cc7ca NB |
9596 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9597 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9598 | u64 old_idx = 0; |
9599 | u64 new_idx = 0; | |
9600 | u64 root_objectid; | |
9601 | int ret; | |
86e8aa0e FM |
9602 | bool root_log_pinned = false; |
9603 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9604 | |
9605 | /* we only allow rename subvolume link between subvolumes */ | |
9606 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9607 | return -EXDEV; | |
9608 | ||
9609 | /* close the race window with snapshot create/destroy ioctl */ | |
9610 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9611 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9612 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9613 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9614 | |
9615 | /* | |
9616 | * We want to reserve the absolute worst case amount of items. So if | |
9617 | * both inodes are subvols and we need to unlink them then that would | |
9618 | * require 4 item modifications, but if they are both normal inodes it | |
9619 | * would require 5 item modifications, so we'll assume their normal | |
9620 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9621 | * should cover the worst case number of items we'll modify. | |
9622 | */ | |
9623 | trans = btrfs_start_transaction(root, 12); | |
9624 | if (IS_ERR(trans)) { | |
9625 | ret = PTR_ERR(trans); | |
9626 | goto out_notrans; | |
9627 | } | |
9628 | ||
9629 | /* | |
9630 | * We need to find a free sequence number both in the source and | |
9631 | * in the destination directory for the exchange. | |
9632 | */ | |
877574e2 | 9633 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9634 | if (ret) |
9635 | goto out_fail; | |
877574e2 | 9636 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9637 | if (ret) |
9638 | goto out_fail; | |
9639 | ||
9640 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9641 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9642 | ||
9643 | /* Reference for the source. */ | |
9644 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9645 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9646 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9647 | } else { |
376e5a57 FM |
9648 | btrfs_pin_log_trans(root); |
9649 | root_log_pinned = true; | |
cdd1fedf DF |
9650 | ret = btrfs_insert_inode_ref(trans, dest, |
9651 | new_dentry->d_name.name, | |
9652 | new_dentry->d_name.len, | |
9653 | old_ino, | |
f85b7379 DS |
9654 | btrfs_ino(BTRFS_I(new_dir)), |
9655 | old_idx); | |
cdd1fedf DF |
9656 | if (ret) |
9657 | goto out_fail; | |
cdd1fedf DF |
9658 | } |
9659 | ||
9660 | /* And now for the dest. */ | |
9661 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9662 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9663 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9664 | } else { |
376e5a57 FM |
9665 | btrfs_pin_log_trans(dest); |
9666 | dest_log_pinned = true; | |
cdd1fedf DF |
9667 | ret = btrfs_insert_inode_ref(trans, root, |
9668 | old_dentry->d_name.name, | |
9669 | old_dentry->d_name.len, | |
9670 | new_ino, | |
f85b7379 DS |
9671 | btrfs_ino(BTRFS_I(old_dir)), |
9672 | new_idx); | |
cdd1fedf DF |
9673 | if (ret) |
9674 | goto out_fail; | |
cdd1fedf DF |
9675 | } |
9676 | ||
9677 | /* Update inode version and ctime/mtime. */ | |
9678 | inode_inc_iversion(old_dir); | |
9679 | inode_inc_iversion(new_dir); | |
9680 | inode_inc_iversion(old_inode); | |
9681 | inode_inc_iversion(new_inode); | |
9682 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9683 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9684 | old_inode->i_ctime = ctime; | |
9685 | new_inode->i_ctime = ctime; | |
9686 | ||
9687 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9688 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9689 | BTRFS_I(old_inode), 1); | |
9690 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9691 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9692 | } |
9693 | ||
9694 | /* src is a subvolume */ | |
9695 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9696 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9697 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9698 | root_objectid, | |
9699 | old_dentry->d_name.name, | |
9700 | old_dentry->d_name.len); | |
9701 | } else { /* src is an inode */ | |
4ec5934e NB |
9702 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9703 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9704 | old_dentry->d_name.name, |
9705 | old_dentry->d_name.len); | |
9706 | if (!ret) | |
9707 | ret = btrfs_update_inode(trans, root, old_inode); | |
9708 | } | |
9709 | if (ret) { | |
66642832 | 9710 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9711 | goto out_fail; |
9712 | } | |
9713 | ||
9714 | /* dest is a subvolume */ | |
9715 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9716 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9717 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9718 | root_objectid, | |
9719 | new_dentry->d_name.name, | |
9720 | new_dentry->d_name.len); | |
9721 | } else { /* dest is an inode */ | |
4ec5934e NB |
9722 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9723 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9724 | new_dentry->d_name.name, |
9725 | new_dentry->d_name.len); | |
9726 | if (!ret) | |
9727 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9728 | } | |
9729 | if (ret) { | |
66642832 | 9730 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9731 | goto out_fail; |
9732 | } | |
9733 | ||
db0a669f | 9734 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9735 | new_dentry->d_name.name, |
9736 | new_dentry->d_name.len, 0, old_idx); | |
9737 | if (ret) { | |
66642832 | 9738 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9739 | goto out_fail; |
9740 | } | |
9741 | ||
db0a669f | 9742 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9743 | old_dentry->d_name.name, |
9744 | old_dentry->d_name.len, 0, new_idx); | |
9745 | if (ret) { | |
66642832 | 9746 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9747 | goto out_fail; |
9748 | } | |
9749 | ||
9750 | if (old_inode->i_nlink == 1) | |
9751 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9752 | if (new_inode->i_nlink == 1) | |
9753 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9754 | ||
86e8aa0e | 9755 | if (root_log_pinned) { |
cdd1fedf | 9756 | parent = new_dentry->d_parent; |
f85b7379 DS |
9757 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9758 | parent); | |
cdd1fedf | 9759 | btrfs_end_log_trans(root); |
86e8aa0e | 9760 | root_log_pinned = false; |
cdd1fedf | 9761 | } |
86e8aa0e | 9762 | if (dest_log_pinned) { |
cdd1fedf | 9763 | parent = old_dentry->d_parent; |
f85b7379 DS |
9764 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
9765 | parent); | |
cdd1fedf | 9766 | btrfs_end_log_trans(dest); |
86e8aa0e | 9767 | dest_log_pinned = false; |
cdd1fedf DF |
9768 | } |
9769 | out_fail: | |
86e8aa0e FM |
9770 | /* |
9771 | * If we have pinned a log and an error happened, we unpin tasks | |
9772 | * trying to sync the log and force them to fallback to a transaction | |
9773 | * commit if the log currently contains any of the inodes involved in | |
9774 | * this rename operation (to ensure we do not persist a log with an | |
9775 | * inconsistent state for any of these inodes or leading to any | |
9776 | * inconsistencies when replayed). If the transaction was aborted, the | |
9777 | * abortion reason is propagated to userspace when attempting to commit | |
9778 | * the transaction. If the log does not contain any of these inodes, we | |
9779 | * allow the tasks to sync it. | |
9780 | */ | |
9781 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9782 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9783 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9784 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9785 | (new_inode && |
0f8939b8 | 9786 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9787 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9788 | |
9789 | if (root_log_pinned) { | |
9790 | btrfs_end_log_trans(root); | |
9791 | root_log_pinned = false; | |
9792 | } | |
9793 | if (dest_log_pinned) { | |
9794 | btrfs_end_log_trans(dest); | |
9795 | dest_log_pinned = false; | |
9796 | } | |
9797 | } | |
3a45bb20 | 9798 | ret = btrfs_end_transaction(trans); |
cdd1fedf DF |
9799 | out_notrans: |
9800 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9801 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9802 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9803 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9804 | |
9805 | return ret; | |
9806 | } | |
9807 | ||
9808 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9809 | struct btrfs_root *root, | |
9810 | struct inode *dir, | |
9811 | struct dentry *dentry) | |
9812 | { | |
9813 | int ret; | |
9814 | struct inode *inode; | |
9815 | u64 objectid; | |
9816 | u64 index; | |
9817 | ||
9818 | ret = btrfs_find_free_ino(root, &objectid); | |
9819 | if (ret) | |
9820 | return ret; | |
9821 | ||
9822 | inode = btrfs_new_inode(trans, root, dir, | |
9823 | dentry->d_name.name, | |
9824 | dentry->d_name.len, | |
4a0cc7ca | 9825 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9826 | objectid, |
9827 | S_IFCHR | WHITEOUT_MODE, | |
9828 | &index); | |
9829 | ||
9830 | if (IS_ERR(inode)) { | |
9831 | ret = PTR_ERR(inode); | |
9832 | return ret; | |
9833 | } | |
9834 | ||
9835 | inode->i_op = &btrfs_special_inode_operations; | |
9836 | init_special_inode(inode, inode->i_mode, | |
9837 | WHITEOUT_DEV); | |
9838 | ||
9839 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9840 | &dentry->d_name); | |
9841 | if (ret) | |
c9901618 | 9842 | goto out; |
cdd1fedf | 9843 | |
cef415af NB |
9844 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9845 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9846 | if (ret) |
c9901618 | 9847 | goto out; |
cdd1fedf DF |
9848 | |
9849 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9850 | out: |
cdd1fedf | 9851 | unlock_new_inode(inode); |
c9901618 FM |
9852 | if (ret) |
9853 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9854 | iput(inode); |
9855 | ||
c9901618 | 9856 | return ret; |
cdd1fedf DF |
9857 | } |
9858 | ||
d397712b | 9859 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9860 | struct inode *new_dir, struct dentry *new_dentry, |
9861 | unsigned int flags) | |
39279cc3 | 9862 | { |
0b246afa | 9863 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9864 | struct btrfs_trans_handle *trans; |
5062af35 | 9865 | unsigned int trans_num_items; |
39279cc3 | 9866 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9867 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9868 | struct inode *new_inode = d_inode(new_dentry); |
9869 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9870 | u64 index = 0; |
4df27c4d | 9871 | u64 root_objectid; |
39279cc3 | 9872 | int ret; |
4a0cc7ca | 9873 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9874 | bool log_pinned = false; |
39279cc3 | 9875 | |
4a0cc7ca | 9876 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9877 | return -EPERM; |
9878 | ||
4df27c4d | 9879 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9880 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9881 | return -EXDEV; |
9882 | ||
33345d01 | 9883 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9884 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9885 | return -ENOTEMPTY; |
5f39d397 | 9886 | |
4df27c4d YZ |
9887 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9888 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9889 | return -ENOTEMPTY; | |
9c52057c CM |
9890 | |
9891 | ||
9892 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9893 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9894 | new_dentry->d_name.name, |
9895 | new_dentry->d_name.len); | |
9896 | ||
9897 | if (ret) { | |
9898 | if (ret == -EEXIST) { | |
9899 | /* we shouldn't get | |
9900 | * eexist without a new_inode */ | |
fae7f21c | 9901 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9902 | return ret; |
9903 | } | |
9904 | } else { | |
9905 | /* maybe -EOVERFLOW */ | |
9906 | return ret; | |
9907 | } | |
9908 | } | |
9909 | ret = 0; | |
9910 | ||
5a3f23d5 | 9911 | /* |
8d875f95 CM |
9912 | * we're using rename to replace one file with another. Start IO on it |
9913 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9914 | */ |
8d875f95 | 9915 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9916 | filemap_flush(old_inode->i_mapping); |
9917 | ||
76dda93c | 9918 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9919 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9920 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9921 | /* |
9922 | * We want to reserve the absolute worst case amount of items. So if | |
9923 | * both inodes are subvols and we need to unlink them then that would | |
9924 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9925 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9926 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9927 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9928 | * If our rename has the whiteout flag, we need more 5 units for the |
9929 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9930 | * when selinux is enabled). | |
a22285a6 | 9931 | */ |
5062af35 FM |
9932 | trans_num_items = 11; |
9933 | if (flags & RENAME_WHITEOUT) | |
9934 | trans_num_items += 5; | |
9935 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9936 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9937 | ret = PTR_ERR(trans); |
9938 | goto out_notrans; | |
9939 | } | |
76dda93c | 9940 | |
4df27c4d YZ |
9941 | if (dest != root) |
9942 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9943 | |
877574e2 | 9944 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9945 | if (ret) |
9946 | goto out_fail; | |
5a3f23d5 | 9947 | |
67de1176 | 9948 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9949 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9950 | /* force full log commit if subvolume involved. */ |
0b246afa | 9951 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 9952 | } else { |
c4aba954 FM |
9953 | btrfs_pin_log_trans(root); |
9954 | log_pinned = true; | |
a5719521 YZ |
9955 | ret = btrfs_insert_inode_ref(trans, dest, |
9956 | new_dentry->d_name.name, | |
9957 | new_dentry->d_name.len, | |
33345d01 | 9958 | old_ino, |
4a0cc7ca | 9959 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9960 | if (ret) |
9961 | goto out_fail; | |
4df27c4d | 9962 | } |
5a3f23d5 | 9963 | |
0c4d2d95 JB |
9964 | inode_inc_iversion(old_dir); |
9965 | inode_inc_iversion(new_dir); | |
9966 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9967 | old_dir->i_ctime = old_dir->i_mtime = |
9968 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9969 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9970 | |
12fcfd22 | 9971 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9972 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9973 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9974 | |
33345d01 | 9975 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9976 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9977 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9978 | old_dentry->d_name.name, | |
9979 | old_dentry->d_name.len); | |
9980 | } else { | |
4ec5934e NB |
9981 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9982 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9983 | old_dentry->d_name.name, |
9984 | old_dentry->d_name.len); | |
9985 | if (!ret) | |
9986 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9987 | } |
79787eaa | 9988 | if (ret) { |
66642832 | 9989 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9990 | goto out_fail; |
9991 | } | |
39279cc3 CM |
9992 | |
9993 | if (new_inode) { | |
0c4d2d95 | 9994 | inode_inc_iversion(new_inode); |
c2050a45 | 9995 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9996 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
9997 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9998 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9999 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
10000 | root_objectid, | |
10001 | new_dentry->d_name.name, | |
10002 | new_dentry->d_name.len); | |
10003 | BUG_ON(new_inode->i_nlink == 0); | |
10004 | } else { | |
4ec5934e NB |
10005 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
10006 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
10007 | new_dentry->d_name.name, |
10008 | new_dentry->d_name.len); | |
10009 | } | |
4ef31a45 | 10010 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
10011 | ret = btrfs_orphan_add(trans, |
10012 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 10013 | if (ret) { |
66642832 | 10014 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10015 | goto out_fail; |
10016 | } | |
39279cc3 | 10017 | } |
aec7477b | 10018 | |
db0a669f | 10019 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 10020 | new_dentry->d_name.name, |
a5719521 | 10021 | new_dentry->d_name.len, 0, index); |
79787eaa | 10022 | if (ret) { |
66642832 | 10023 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10024 | goto out_fail; |
10025 | } | |
39279cc3 | 10026 | |
67de1176 MX |
10027 | if (old_inode->i_nlink == 1) |
10028 | BTRFS_I(old_inode)->dir_index = index; | |
10029 | ||
3dc9e8f7 | 10030 | if (log_pinned) { |
10d9f309 | 10031 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 10032 | |
f85b7379 DS |
10033 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
10034 | parent); | |
4df27c4d | 10035 | btrfs_end_log_trans(root); |
3dc9e8f7 | 10036 | log_pinned = false; |
4df27c4d | 10037 | } |
cdd1fedf DF |
10038 | |
10039 | if (flags & RENAME_WHITEOUT) { | |
10040 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10041 | old_dentry); | |
10042 | ||
10043 | if (ret) { | |
66642832 | 10044 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
10045 | goto out_fail; |
10046 | } | |
4df27c4d | 10047 | } |
39279cc3 | 10048 | out_fail: |
3dc9e8f7 FM |
10049 | /* |
10050 | * If we have pinned the log and an error happened, we unpin tasks | |
10051 | * trying to sync the log and force them to fallback to a transaction | |
10052 | * commit if the log currently contains any of the inodes involved in | |
10053 | * this rename operation (to ensure we do not persist a log with an | |
10054 | * inconsistent state for any of these inodes or leading to any | |
10055 | * inconsistencies when replayed). If the transaction was aborted, the | |
10056 | * abortion reason is propagated to userspace when attempting to commit | |
10057 | * the transaction. If the log does not contain any of these inodes, we | |
10058 | * allow the tasks to sync it. | |
10059 | */ | |
10060 | if (ret && log_pinned) { | |
0f8939b8 NB |
10061 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
10062 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10063 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 10064 | (new_inode && |
0f8939b8 | 10065 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 10066 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
10067 | |
10068 | btrfs_end_log_trans(root); | |
10069 | log_pinned = false; | |
10070 | } | |
3a45bb20 | 10071 | btrfs_end_transaction(trans); |
b44c59a8 | 10072 | out_notrans: |
33345d01 | 10073 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10074 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 10075 | |
39279cc3 CM |
10076 | return ret; |
10077 | } | |
10078 | ||
80ace85c MS |
10079 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
10080 | struct inode *new_dir, struct dentry *new_dentry, | |
10081 | unsigned int flags) | |
10082 | { | |
cdd1fedf | 10083 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
10084 | return -EINVAL; |
10085 | ||
cdd1fedf DF |
10086 | if (flags & RENAME_EXCHANGE) |
10087 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10088 | new_dentry); | |
10089 | ||
10090 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
10091 | } |
10092 | ||
8ccf6f19 MX |
10093 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
10094 | { | |
10095 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 10096 | struct inode *inode; |
8ccf6f19 MX |
10097 | |
10098 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10099 | work); | |
9f23e289 | 10100 | inode = delalloc_work->inode; |
30424601 DS |
10101 | filemap_flush(inode->i_mapping); |
10102 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10103 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10104 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10105 | |
10106 | if (delalloc_work->delay_iput) | |
9f23e289 | 10107 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10108 | else |
9f23e289 | 10109 | iput(inode); |
8ccf6f19 MX |
10110 | complete(&delalloc_work->completion); |
10111 | } | |
10112 | ||
10113 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10114 | int delay_iput) |
8ccf6f19 MX |
10115 | { |
10116 | struct btrfs_delalloc_work *work; | |
10117 | ||
100d5702 | 10118 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10119 | if (!work) |
10120 | return NULL; | |
10121 | ||
10122 | init_completion(&work->completion); | |
10123 | INIT_LIST_HEAD(&work->list); | |
10124 | work->inode = inode; | |
8ccf6f19 | 10125 | work->delay_iput = delay_iput; |
9e0af237 LB |
10126 | WARN_ON_ONCE(!inode); |
10127 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10128 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10129 | |
10130 | return work; | |
10131 | } | |
10132 | ||
10133 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10134 | { | |
10135 | wait_for_completion(&work->completion); | |
100d5702 | 10136 | kfree(work); |
8ccf6f19 MX |
10137 | } |
10138 | ||
d352ac68 CM |
10139 | /* |
10140 | * some fairly slow code that needs optimization. This walks the list | |
10141 | * of all the inodes with pending delalloc and forces them to disk. | |
10142 | */ | |
6c255e67 MX |
10143 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10144 | int nr) | |
ea8c2819 | 10145 | { |
ea8c2819 | 10146 | struct btrfs_inode *binode; |
5b21f2ed | 10147 | struct inode *inode; |
8ccf6f19 MX |
10148 | struct btrfs_delalloc_work *work, *next; |
10149 | struct list_head works; | |
1eafa6c7 | 10150 | struct list_head splice; |
8ccf6f19 | 10151 | int ret = 0; |
ea8c2819 | 10152 | |
8ccf6f19 | 10153 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10154 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10155 | |
573bfb72 | 10156 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10157 | spin_lock(&root->delalloc_lock); |
10158 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10159 | while (!list_empty(&splice)) { |
10160 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10161 | delalloc_inodes); |
1eafa6c7 | 10162 | |
eb73c1b7 MX |
10163 | list_move_tail(&binode->delalloc_inodes, |
10164 | &root->delalloc_inodes); | |
5b21f2ed | 10165 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10166 | if (!inode) { |
eb73c1b7 | 10167 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10168 | continue; |
df0af1a5 | 10169 | } |
eb73c1b7 | 10170 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10171 | |
651d494a | 10172 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10173 | if (!work) { |
f4ab9ea7 JB |
10174 | if (delay_iput) |
10175 | btrfs_add_delayed_iput(inode); | |
10176 | else | |
10177 | iput(inode); | |
1eafa6c7 | 10178 | ret = -ENOMEM; |
a1ecaabb | 10179 | goto out; |
5b21f2ed | 10180 | } |
1eafa6c7 | 10181 | list_add_tail(&work->list, &works); |
a44903ab QW |
10182 | btrfs_queue_work(root->fs_info->flush_workers, |
10183 | &work->work); | |
6c255e67 MX |
10184 | ret++; |
10185 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10186 | goto out; |
5b21f2ed | 10187 | cond_resched(); |
eb73c1b7 | 10188 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10189 | } |
eb73c1b7 | 10190 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10191 | |
a1ecaabb | 10192 | out: |
eb73c1b7 MX |
10193 | list_for_each_entry_safe(work, next, &works, list) { |
10194 | list_del_init(&work->list); | |
10195 | btrfs_wait_and_free_delalloc_work(work); | |
10196 | } | |
10197 | ||
10198 | if (!list_empty_careful(&splice)) { | |
10199 | spin_lock(&root->delalloc_lock); | |
10200 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10201 | spin_unlock(&root->delalloc_lock); | |
10202 | } | |
573bfb72 | 10203 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10204 | return ret; |
10205 | } | |
1eafa6c7 | 10206 | |
eb73c1b7 MX |
10207 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10208 | { | |
0b246afa | 10209 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10210 | int ret; |
1eafa6c7 | 10211 | |
0b246afa | 10212 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10213 | return -EROFS; |
10214 | ||
6c255e67 MX |
10215 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10216 | if (ret > 0) | |
10217 | ret = 0; | |
eb73c1b7 MX |
10218 | /* |
10219 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10220 | * we have to make sure the IO is actually started and that |
10221 | * ordered extents get created before we return | |
10222 | */ | |
0b246afa JM |
10223 | atomic_inc(&fs_info->async_submit_draining); |
10224 | while (atomic_read(&fs_info->nr_async_submits) || | |
10225 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10226 | wait_event(fs_info->async_submit_wait, | |
10227 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10228 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10229 | } | |
10230 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 MX |
10231 | return ret; |
10232 | } | |
10233 | ||
6c255e67 MX |
10234 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10235 | int nr) | |
eb73c1b7 MX |
10236 | { |
10237 | struct btrfs_root *root; | |
10238 | struct list_head splice; | |
10239 | int ret; | |
10240 | ||
2c21b4d7 | 10241 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10242 | return -EROFS; |
10243 | ||
10244 | INIT_LIST_HEAD(&splice); | |
10245 | ||
573bfb72 | 10246 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10247 | spin_lock(&fs_info->delalloc_root_lock); |
10248 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10249 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10250 | root = list_first_entry(&splice, struct btrfs_root, |
10251 | delalloc_root); | |
10252 | root = btrfs_grab_fs_root(root); | |
10253 | BUG_ON(!root); | |
10254 | list_move_tail(&root->delalloc_root, | |
10255 | &fs_info->delalloc_roots); | |
10256 | spin_unlock(&fs_info->delalloc_root_lock); | |
10257 | ||
6c255e67 | 10258 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10259 | btrfs_put_fs_root(root); |
6c255e67 | 10260 | if (ret < 0) |
eb73c1b7 MX |
10261 | goto out; |
10262 | ||
6c255e67 MX |
10263 | if (nr != -1) { |
10264 | nr -= ret; | |
10265 | WARN_ON(nr < 0); | |
10266 | } | |
eb73c1b7 | 10267 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10268 | } |
eb73c1b7 | 10269 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10270 | |
6c255e67 | 10271 | ret = 0; |
eb73c1b7 MX |
10272 | atomic_inc(&fs_info->async_submit_draining); |
10273 | while (atomic_read(&fs_info->nr_async_submits) || | |
10274 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10275 | wait_event(fs_info->async_submit_wait, | |
10276 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10277 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10278 | } | |
10279 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10280 | out: |
1eafa6c7 | 10281 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10282 | spin_lock(&fs_info->delalloc_root_lock); |
10283 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10284 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10285 | } |
573bfb72 | 10286 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10287 | return ret; |
ea8c2819 CM |
10288 | } |
10289 | ||
39279cc3 CM |
10290 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10291 | const char *symname) | |
10292 | { | |
0b246afa | 10293 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10294 | struct btrfs_trans_handle *trans; |
10295 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10296 | struct btrfs_path *path; | |
10297 | struct btrfs_key key; | |
1832a6d5 | 10298 | struct inode *inode = NULL; |
39279cc3 CM |
10299 | int err; |
10300 | int drop_inode = 0; | |
10301 | u64 objectid; | |
67871254 | 10302 | u64 index = 0; |
39279cc3 CM |
10303 | int name_len; |
10304 | int datasize; | |
5f39d397 | 10305 | unsigned long ptr; |
39279cc3 | 10306 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10307 | struct extent_buffer *leaf; |
39279cc3 | 10308 | |
f06becc4 | 10309 | name_len = strlen(symname); |
0b246afa | 10310 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10311 | return -ENAMETOOLONG; |
1832a6d5 | 10312 | |
9ed74f2d JB |
10313 | /* |
10314 | * 2 items for inode item and ref | |
10315 | * 2 items for dir items | |
9269d12b FM |
10316 | * 1 item for updating parent inode item |
10317 | * 1 item for the inline extent item | |
9ed74f2d JB |
10318 | * 1 item for xattr if selinux is on |
10319 | */ | |
9269d12b | 10320 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10321 | if (IS_ERR(trans)) |
10322 | return PTR_ERR(trans); | |
1832a6d5 | 10323 | |
581bb050 LZ |
10324 | err = btrfs_find_free_ino(root, &objectid); |
10325 | if (err) | |
10326 | goto out_unlock; | |
10327 | ||
aec7477b | 10328 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10329 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10330 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10331 | if (IS_ERR(inode)) { |
10332 | err = PTR_ERR(inode); | |
39279cc3 | 10333 | goto out_unlock; |
7cf96da3 | 10334 | } |
39279cc3 | 10335 | |
ad19db71 CS |
10336 | /* |
10337 | * If the active LSM wants to access the inode during | |
10338 | * d_instantiate it needs these. Smack checks to see | |
10339 | * if the filesystem supports xattrs by looking at the | |
10340 | * ops vector. | |
10341 | */ | |
10342 | inode->i_fop = &btrfs_file_operations; | |
10343 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10344 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10345 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10346 | ||
10347 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10348 | if (err) | |
10349 | goto out_unlock_inode; | |
ad19db71 | 10350 | |
39279cc3 | 10351 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10352 | if (!path) { |
10353 | err = -ENOMEM; | |
b0d5d10f | 10354 | goto out_unlock_inode; |
d8926bb3 | 10355 | } |
4a0cc7ca | 10356 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10357 | key.offset = 0; |
962a298f | 10358 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10359 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10360 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10361 | datasize); | |
54aa1f4d | 10362 | if (err) { |
b0839166 | 10363 | btrfs_free_path(path); |
b0d5d10f | 10364 | goto out_unlock_inode; |
54aa1f4d | 10365 | } |
5f39d397 CM |
10366 | leaf = path->nodes[0]; |
10367 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10368 | struct btrfs_file_extent_item); | |
10369 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10370 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10371 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10372 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10373 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10374 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10375 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10376 | ||
39279cc3 | 10377 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10378 | write_extent_buffer(leaf, symname, ptr, name_len); |
10379 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10380 | btrfs_free_path(path); |
5f39d397 | 10381 | |
39279cc3 | 10382 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10383 | inode_nohighmem(inode); |
39279cc3 | 10384 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10385 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10386 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10387 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10388 | /* |
10389 | * Last step, add directory indexes for our symlink inode. This is the | |
10390 | * last step to avoid extra cleanup of these indexes if an error happens | |
10391 | * elsewhere above. | |
10392 | */ | |
10393 | if (!err) | |
cef415af NB |
10394 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10395 | BTRFS_I(inode), 0, index); | |
b0d5d10f | 10396 | if (err) { |
54aa1f4d | 10397 | drop_inode = 1; |
b0d5d10f CM |
10398 | goto out_unlock_inode; |
10399 | } | |
10400 | ||
10401 | unlock_new_inode(inode); | |
10402 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10403 | |
10404 | out_unlock: | |
3a45bb20 | 10405 | btrfs_end_transaction(trans); |
39279cc3 CM |
10406 | if (drop_inode) { |
10407 | inode_dec_link_count(inode); | |
10408 | iput(inode); | |
10409 | } | |
2ff7e61e | 10410 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 10411 | return err; |
b0d5d10f CM |
10412 | |
10413 | out_unlock_inode: | |
10414 | drop_inode = 1; | |
10415 | unlock_new_inode(inode); | |
10416 | goto out_unlock; | |
39279cc3 | 10417 | } |
16432985 | 10418 | |
0af3d00b JB |
10419 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10420 | u64 start, u64 num_bytes, u64 min_size, | |
10421 | loff_t actual_len, u64 *alloc_hint, | |
10422 | struct btrfs_trans_handle *trans) | |
d899e052 | 10423 | { |
0b246afa | 10424 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10425 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10426 | struct extent_map *em; | |
d899e052 YZ |
10427 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10428 | struct btrfs_key ins; | |
d899e052 | 10429 | u64 cur_offset = start; |
55a61d1d | 10430 | u64 i_size; |
154ea289 | 10431 | u64 cur_bytes; |
0b670dc4 | 10432 | u64 last_alloc = (u64)-1; |
d899e052 | 10433 | int ret = 0; |
0af3d00b | 10434 | bool own_trans = true; |
18513091 | 10435 | u64 end = start + num_bytes - 1; |
d899e052 | 10436 | |
0af3d00b JB |
10437 | if (trans) |
10438 | own_trans = false; | |
d899e052 | 10439 | while (num_bytes > 0) { |
0af3d00b JB |
10440 | if (own_trans) { |
10441 | trans = btrfs_start_transaction(root, 3); | |
10442 | if (IS_ERR(trans)) { | |
10443 | ret = PTR_ERR(trans); | |
10444 | break; | |
10445 | } | |
5a303d5d YZ |
10446 | } |
10447 | ||
ee22184b | 10448 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10449 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10450 | /* |
10451 | * If we are severely fragmented we could end up with really | |
10452 | * small allocations, so if the allocator is returning small | |
10453 | * chunks lets make its job easier by only searching for those | |
10454 | * sized chunks. | |
10455 | */ | |
10456 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10457 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10458 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10459 | if (ret) { |
0af3d00b | 10460 | if (own_trans) |
3a45bb20 | 10461 | btrfs_end_transaction(trans); |
a22285a6 | 10462 | break; |
d899e052 | 10463 | } |
0b246afa | 10464 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10465 | |
0b670dc4 | 10466 | last_alloc = ins.offset; |
d899e052 YZ |
10467 | ret = insert_reserved_file_extent(trans, inode, |
10468 | cur_offset, ins.objectid, | |
10469 | ins.offset, ins.offset, | |
920bbbfb | 10470 | ins.offset, 0, 0, 0, |
d899e052 | 10471 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10472 | if (ret) { |
2ff7e61e | 10473 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10474 | ins.offset, 0); |
66642832 | 10475 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10476 | if (own_trans) |
3a45bb20 | 10477 | btrfs_end_transaction(trans); |
79787eaa JM |
10478 | break; |
10479 | } | |
31193213 | 10480 | |
dcdbc059 | 10481 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10482 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10483 | |
5dc562c5 JB |
10484 | em = alloc_extent_map(); |
10485 | if (!em) { | |
10486 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10487 | &BTRFS_I(inode)->runtime_flags); | |
10488 | goto next; | |
10489 | } | |
10490 | ||
10491 | em->start = cur_offset; | |
10492 | em->orig_start = cur_offset; | |
10493 | em->len = ins.offset; | |
10494 | em->block_start = ins.objectid; | |
10495 | em->block_len = ins.offset; | |
b4939680 | 10496 | em->orig_block_len = ins.offset; |
cc95bef6 | 10497 | em->ram_bytes = ins.offset; |
0b246afa | 10498 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10499 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10500 | em->generation = trans->transid; | |
10501 | ||
10502 | while (1) { | |
10503 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10504 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10505 | write_unlock(&em_tree->lock); |
10506 | if (ret != -EEXIST) | |
10507 | break; | |
dcdbc059 | 10508 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10509 | cur_offset + ins.offset - 1, |
10510 | 0); | |
10511 | } | |
10512 | free_extent_map(em); | |
10513 | next: | |
d899e052 YZ |
10514 | num_bytes -= ins.offset; |
10515 | cur_offset += ins.offset; | |
efa56464 | 10516 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10517 | |
0c4d2d95 | 10518 | inode_inc_iversion(inode); |
c2050a45 | 10519 | inode->i_ctime = current_time(inode); |
6cbff00f | 10520 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10521 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10522 | (actual_len > inode->i_size) && |
10523 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10524 | if (cur_offset > actual_len) |
55a61d1d | 10525 | i_size = actual_len; |
d1ea6a61 | 10526 | else |
55a61d1d JB |
10527 | i_size = cur_offset; |
10528 | i_size_write(inode, i_size); | |
10529 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10530 | } |
10531 | ||
d899e052 | 10532 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10533 | |
10534 | if (ret) { | |
66642832 | 10535 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10536 | if (own_trans) |
3a45bb20 | 10537 | btrfs_end_transaction(trans); |
79787eaa JM |
10538 | break; |
10539 | } | |
d899e052 | 10540 | |
0af3d00b | 10541 | if (own_trans) |
3a45bb20 | 10542 | btrfs_end_transaction(trans); |
5a303d5d | 10543 | } |
18513091 WX |
10544 | if (cur_offset < end) |
10545 | btrfs_free_reserved_data_space(inode, cur_offset, | |
10546 | end - cur_offset + 1); | |
d899e052 YZ |
10547 | return ret; |
10548 | } | |
10549 | ||
0af3d00b JB |
10550 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10551 | u64 start, u64 num_bytes, u64 min_size, | |
10552 | loff_t actual_len, u64 *alloc_hint) | |
10553 | { | |
10554 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10555 | min_size, actual_len, alloc_hint, | |
10556 | NULL); | |
10557 | } | |
10558 | ||
10559 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10560 | struct btrfs_trans_handle *trans, int mode, | |
10561 | u64 start, u64 num_bytes, u64 min_size, | |
10562 | loff_t actual_len, u64 *alloc_hint) | |
10563 | { | |
10564 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10565 | min_size, actual_len, alloc_hint, trans); | |
10566 | } | |
10567 | ||
e6dcd2dc CM |
10568 | static int btrfs_set_page_dirty(struct page *page) |
10569 | { | |
e6dcd2dc CM |
10570 | return __set_page_dirty_nobuffers(page); |
10571 | } | |
10572 | ||
10556cb2 | 10573 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10574 | { |
b83cc969 | 10575 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10576 | umode_t mode = inode->i_mode; |
b83cc969 | 10577 | |
cb6db4e5 JM |
10578 | if (mask & MAY_WRITE && |
10579 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10580 | if (btrfs_root_readonly(root)) | |
10581 | return -EROFS; | |
10582 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10583 | return -EACCES; | |
10584 | } | |
2830ba7f | 10585 | return generic_permission(inode, mask); |
fdebe2bd | 10586 | } |
39279cc3 | 10587 | |
ef3b9af5 FM |
10588 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10589 | { | |
2ff7e61e | 10590 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10591 | struct btrfs_trans_handle *trans; |
10592 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10593 | struct inode *inode = NULL; | |
10594 | u64 objectid; | |
10595 | u64 index; | |
10596 | int ret = 0; | |
10597 | ||
10598 | /* | |
10599 | * 5 units required for adding orphan entry | |
10600 | */ | |
10601 | trans = btrfs_start_transaction(root, 5); | |
10602 | if (IS_ERR(trans)) | |
10603 | return PTR_ERR(trans); | |
10604 | ||
10605 | ret = btrfs_find_free_ino(root, &objectid); | |
10606 | if (ret) | |
10607 | goto out; | |
10608 | ||
10609 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10610 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10611 | if (IS_ERR(inode)) { |
10612 | ret = PTR_ERR(inode); | |
10613 | inode = NULL; | |
10614 | goto out; | |
10615 | } | |
10616 | ||
ef3b9af5 FM |
10617 | inode->i_fop = &btrfs_file_operations; |
10618 | inode->i_op = &btrfs_file_inode_operations; | |
10619 | ||
10620 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10621 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10622 | ||
b0d5d10f CM |
10623 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10624 | if (ret) | |
10625 | goto out_inode; | |
10626 | ||
10627 | ret = btrfs_update_inode(trans, root, inode); | |
10628 | if (ret) | |
10629 | goto out_inode; | |
73f2e545 | 10630 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10631 | if (ret) |
b0d5d10f | 10632 | goto out_inode; |
ef3b9af5 | 10633 | |
5762b5c9 FM |
10634 | /* |
10635 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10636 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10637 | * through: | |
10638 | * | |
10639 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10640 | */ | |
10641 | set_nlink(inode, 1); | |
b0d5d10f | 10642 | unlock_new_inode(inode); |
ef3b9af5 FM |
10643 | d_tmpfile(dentry, inode); |
10644 | mark_inode_dirty(inode); | |
10645 | ||
10646 | out: | |
3a45bb20 | 10647 | btrfs_end_transaction(trans); |
ef3b9af5 FM |
10648 | if (ret) |
10649 | iput(inode); | |
2ff7e61e JM |
10650 | btrfs_balance_delayed_items(fs_info); |
10651 | btrfs_btree_balance_dirty(fs_info); | |
ef3b9af5 | 10652 | return ret; |
b0d5d10f CM |
10653 | |
10654 | out_inode: | |
10655 | unlock_new_inode(inode); | |
10656 | goto out; | |
10657 | ||
ef3b9af5 FM |
10658 | } |
10659 | ||
20a7db8a | 10660 | __attribute__((const)) |
9d0d1c8b | 10661 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) |
20a7db8a | 10662 | { |
9d0d1c8b | 10663 | return -EAGAIN; |
20a7db8a DS |
10664 | } |
10665 | ||
c6100a4b JB |
10666 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
10667 | { | |
10668 | struct inode *inode = private_data; | |
10669 | return btrfs_sb(inode->i_sb); | |
10670 | } | |
10671 | ||
10672 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, | |
10673 | u64 start, u64 end) | |
10674 | { | |
10675 | struct inode *inode = private_data; | |
10676 | u64 isize; | |
10677 | ||
10678 | isize = i_size_read(inode); | |
10679 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
10680 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, | |
10681 | "%s: ino %llu isize %llu odd range [%llu,%llu]", | |
10682 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); | |
10683 | } | |
10684 | } | |
10685 | ||
10686 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) | |
10687 | { | |
10688 | struct inode *inode = private_data; | |
10689 | unsigned long index = start >> PAGE_SHIFT; | |
10690 | unsigned long end_index = end >> PAGE_SHIFT; | |
10691 | struct page *page; | |
10692 | ||
10693 | while (index <= end_index) { | |
10694 | page = find_get_page(inode->i_mapping, index); | |
10695 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10696 | set_page_writeback(page); | |
10697 | put_page(page); | |
10698 | index++; | |
10699 | } | |
10700 | } | |
10701 | ||
6e1d5dcc | 10702 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10703 | .getattr = btrfs_getattr, |
39279cc3 CM |
10704 | .lookup = btrfs_lookup, |
10705 | .create = btrfs_create, | |
10706 | .unlink = btrfs_unlink, | |
10707 | .link = btrfs_link, | |
10708 | .mkdir = btrfs_mkdir, | |
10709 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10710 | .rename = btrfs_rename2, |
39279cc3 CM |
10711 | .symlink = btrfs_symlink, |
10712 | .setattr = btrfs_setattr, | |
618e21d5 | 10713 | .mknod = btrfs_mknod, |
5103e947 | 10714 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10715 | .permission = btrfs_permission, |
4e34e719 | 10716 | .get_acl = btrfs_get_acl, |
996a710d | 10717 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10718 | .update_time = btrfs_update_time, |
ef3b9af5 | 10719 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10720 | }; |
6e1d5dcc | 10721 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10722 | .lookup = btrfs_lookup, |
fdebe2bd | 10723 | .permission = btrfs_permission, |
93fd63c2 | 10724 | .update_time = btrfs_update_time, |
39279cc3 | 10725 | }; |
76dda93c | 10726 | |
828c0950 | 10727 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10728 | .llseek = generic_file_llseek, |
10729 | .read = generic_read_dir, | |
02dbfc99 | 10730 | .iterate_shared = btrfs_real_readdir, |
34287aa3 | 10731 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10732 | #ifdef CONFIG_COMPAT |
4c63c245 | 10733 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10734 | #endif |
6bf13c0c | 10735 | .release = btrfs_release_file, |
e02119d5 | 10736 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10737 | }; |
10738 | ||
20e5506b | 10739 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10740 | /* mandatory callbacks */ |
065631f6 | 10741 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac | 10742 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
4d53dddb | 10743 | .merge_bio_hook = btrfs_merge_bio_hook, |
9d0d1c8b | 10744 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
c6100a4b JB |
10745 | .tree_fs_info = iotree_fs_info, |
10746 | .set_range_writeback = btrfs_set_range_writeback, | |
4d53dddb DS |
10747 | |
10748 | /* optional callbacks */ | |
10749 | .fill_delalloc = run_delalloc_range, | |
e6dcd2dc | 10750 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10751 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10752 | .set_bit_hook = btrfs_set_bit_hook, |
10753 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10754 | .merge_extent_hook = btrfs_merge_extent_hook, |
10755 | .split_extent_hook = btrfs_split_extent_hook, | |
c6100a4b | 10756 | .check_extent_io_range = btrfs_check_extent_io_range, |
07157aac CM |
10757 | }; |
10758 | ||
35054394 CM |
10759 | /* |
10760 | * btrfs doesn't support the bmap operation because swapfiles | |
10761 | * use bmap to make a mapping of extents in the file. They assume | |
10762 | * these extents won't change over the life of the file and they | |
10763 | * use the bmap result to do IO directly to the drive. | |
10764 | * | |
10765 | * the btrfs bmap call would return logical addresses that aren't | |
10766 | * suitable for IO and they also will change frequently as COW | |
10767 | * operations happen. So, swapfile + btrfs == corruption. | |
10768 | * | |
10769 | * For now we're avoiding this by dropping bmap. | |
10770 | */ | |
7f09410b | 10771 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10772 | .readpage = btrfs_readpage, |
10773 | .writepage = btrfs_writepage, | |
b293f02e | 10774 | .writepages = btrfs_writepages, |
3ab2fb5a | 10775 | .readpages = btrfs_readpages, |
16432985 | 10776 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10777 | .invalidatepage = btrfs_invalidatepage, |
10778 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10779 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10780 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10781 | }; |
10782 | ||
7f09410b | 10783 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10784 | .readpage = btrfs_readpage, |
10785 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10786 | .invalidatepage = btrfs_invalidatepage, |
10787 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10788 | }; |
10789 | ||
6e1d5dcc | 10790 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10791 | .getattr = btrfs_getattr, |
10792 | .setattr = btrfs_setattr, | |
5103e947 | 10793 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10794 | .permission = btrfs_permission, |
1506fcc8 | 10795 | .fiemap = btrfs_fiemap, |
4e34e719 | 10796 | .get_acl = btrfs_get_acl, |
996a710d | 10797 | .set_acl = btrfs_set_acl, |
e41f941a | 10798 | .update_time = btrfs_update_time, |
39279cc3 | 10799 | }; |
6e1d5dcc | 10800 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10801 | .getattr = btrfs_getattr, |
10802 | .setattr = btrfs_setattr, | |
fdebe2bd | 10803 | .permission = btrfs_permission, |
33268eaf | 10804 | .listxattr = btrfs_listxattr, |
4e34e719 | 10805 | .get_acl = btrfs_get_acl, |
996a710d | 10806 | .set_acl = btrfs_set_acl, |
e41f941a | 10807 | .update_time = btrfs_update_time, |
618e21d5 | 10808 | }; |
6e1d5dcc | 10809 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10810 | .get_link = page_get_link, |
f209561a | 10811 | .getattr = btrfs_getattr, |
22c44fe6 | 10812 | .setattr = btrfs_setattr, |
fdebe2bd | 10813 | .permission = btrfs_permission, |
0279b4cd | 10814 | .listxattr = btrfs_listxattr, |
e41f941a | 10815 | .update_time = btrfs_update_time, |
39279cc3 | 10816 | }; |
76dda93c | 10817 | |
82d339d9 | 10818 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10819 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10820 | .d_release = btrfs_dentry_release, |
76dda93c | 10821 | }; |