]>
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 | */ | |
bc42bda2 | 348 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
1acae57b | 349 | btrfs_free_path(path); |
3a45bb20 | 350 | btrfs_end_transaction(trans); |
00361589 | 351 | return ret; |
c8b97818 CM |
352 | } |
353 | ||
771ed689 CM |
354 | struct async_extent { |
355 | u64 start; | |
356 | u64 ram_size; | |
357 | u64 compressed_size; | |
358 | struct page **pages; | |
359 | unsigned long nr_pages; | |
261507a0 | 360 | int compress_type; |
771ed689 CM |
361 | struct list_head list; |
362 | }; | |
363 | ||
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); |
4e4cbee9 | 839 | if (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, | |
4e4cbee9 | 844 | async_extent->nr_pages)) { |
fce2a4e6 FM |
845 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
846 | struct page *p = async_extent->pages[0]; | |
847 | const u64 start = async_extent->start; | |
848 | const u64 end = start + async_extent->ram_size - 1; | |
849 | ||
850 | p->mapping = inode->i_mapping; | |
851 | tree->ops->writepage_end_io_hook(p, start, end, | |
852 | NULL, 0); | |
853 | p->mapping = NULL; | |
ba8b04c1 QW |
854 | extent_clear_unlock_delalloc(inode, start, end, end, |
855 | NULL, 0, | |
fce2a4e6 FM |
856 | PAGE_END_WRITEBACK | |
857 | PAGE_SET_ERROR); | |
40ae837b | 858 | free_async_extent_pages(async_extent); |
fce2a4e6 | 859 | } |
771ed689 CM |
860 | alloc_hint = ins.objectid + ins.offset; |
861 | kfree(async_extent); | |
862 | cond_resched(); | |
863 | } | |
dec8f175 | 864 | return; |
3e04e7f1 | 865 | out_free_reserve: |
0b246afa | 866 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 867 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 868 | out_free: |
c2790a2e | 869 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
870 | async_extent->start + |
871 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
872 | async_extent->start + |
873 | async_extent->ram_size - 1, | |
c2790a2e | 874 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 875 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
876 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
877 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
878 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
879 | PAGE_SET_ERROR); | |
40ae837b | 880 | free_async_extent_pages(async_extent); |
79787eaa | 881 | kfree(async_extent); |
3e04e7f1 | 882 | goto again; |
771ed689 CM |
883 | } |
884 | ||
4b46fce2 JB |
885 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
886 | u64 num_bytes) | |
887 | { | |
888 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
889 | struct extent_map *em; | |
890 | u64 alloc_hint = 0; | |
891 | ||
892 | read_lock(&em_tree->lock); | |
893 | em = search_extent_mapping(em_tree, start, num_bytes); | |
894 | if (em) { | |
895 | /* | |
896 | * if block start isn't an actual block number then find the | |
897 | * first block in this inode and use that as a hint. If that | |
898 | * block is also bogus then just don't worry about it. | |
899 | */ | |
900 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
901 | free_extent_map(em); | |
902 | em = search_extent_mapping(em_tree, 0, 0); | |
903 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
904 | alloc_hint = em->block_start; | |
905 | if (em) | |
906 | free_extent_map(em); | |
907 | } else { | |
908 | alloc_hint = em->block_start; | |
909 | free_extent_map(em); | |
910 | } | |
911 | } | |
912 | read_unlock(&em_tree->lock); | |
913 | ||
914 | return alloc_hint; | |
915 | } | |
916 | ||
771ed689 CM |
917 | /* |
918 | * when extent_io.c finds a delayed allocation range in the file, | |
919 | * the call backs end up in this code. The basic idea is to | |
920 | * allocate extents on disk for the range, and create ordered data structs | |
921 | * in ram to track those extents. | |
922 | * | |
923 | * locked_page is the page that writepage had locked already. We use | |
924 | * it to make sure we don't do extra locks or unlocks. | |
925 | * | |
926 | * *page_started is set to one if we unlock locked_page and do everything | |
927 | * required to start IO on it. It may be clean and already done with | |
928 | * IO when we return. | |
929 | */ | |
00361589 JB |
930 | static noinline int cow_file_range(struct inode *inode, |
931 | struct page *locked_page, | |
dda3245e WX |
932 | u64 start, u64 end, u64 delalloc_end, |
933 | int *page_started, unsigned long *nr_written, | |
934 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 935 | { |
0b246afa | 936 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 937 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
938 | u64 alloc_hint = 0; |
939 | u64 num_bytes; | |
940 | unsigned long ram_size; | |
941 | u64 disk_num_bytes; | |
a315e68f | 942 | u64 cur_alloc_size = 0; |
0b246afa | 943 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
944 | struct btrfs_key ins; |
945 | struct extent_map *em; | |
a315e68f FM |
946 | unsigned clear_bits; |
947 | unsigned long page_ops; | |
948 | bool extent_reserved = false; | |
771ed689 CM |
949 | int ret = 0; |
950 | ||
70ddc553 | 951 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 952 | WARN_ON_ONCE(1); |
29bce2f3 JB |
953 | ret = -EINVAL; |
954 | goto out_unlock; | |
02ecd2c2 | 955 | } |
771ed689 | 956 | |
fda2832f | 957 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
958 | num_bytes = max(blocksize, num_bytes); |
959 | disk_num_bytes = num_bytes; | |
771ed689 | 960 | |
6158e1ce | 961 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 962 | |
771ed689 CM |
963 | if (start == 0) { |
964 | /* lets try to make an inline extent */ | |
f74670f7 AJ |
965 | ret = cow_file_range_inline(root, inode, start, end, 0, |
966 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 967 | if (ret == 0) { |
ba8b04c1 QW |
968 | extent_clear_unlock_delalloc(inode, start, end, |
969 | delalloc_end, NULL, | |
c2790a2e | 970 | EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 971 | EXTENT_DELALLOC_NEW | |
151a41bc | 972 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
973 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
974 | PAGE_END_WRITEBACK); | |
18513091 WX |
975 | btrfs_free_reserved_data_space_noquota(inode, start, |
976 | end - start + 1); | |
771ed689 | 977 | *nr_written = *nr_written + |
09cbfeaf | 978 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 979 | *page_started = 1; |
771ed689 | 980 | goto out; |
79787eaa | 981 | } else if (ret < 0) { |
79787eaa | 982 | goto out_unlock; |
771ed689 CM |
983 | } |
984 | } | |
985 | ||
986 | BUG_ON(disk_num_bytes > | |
0b246afa | 987 | btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 988 | |
4b46fce2 | 989 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
990 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
991 | start + num_bytes - 1, 0); | |
771ed689 | 992 | |
d397712b | 993 | while (disk_num_bytes > 0) { |
287a0ab9 | 994 | cur_alloc_size = disk_num_bytes; |
18513091 | 995 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 996 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 997 | &ins, 1, 1); |
00361589 | 998 | if (ret < 0) |
79787eaa | 999 | goto out_unlock; |
a315e68f FM |
1000 | cur_alloc_size = ins.offset; |
1001 | extent_reserved = true; | |
d397712b | 1002 | |
771ed689 | 1003 | ram_size = ins.offset; |
6f9994db LB |
1004 | em = create_io_em(inode, start, ins.offset, /* len */ |
1005 | start, /* orig_start */ | |
1006 | ins.objectid, /* block_start */ | |
1007 | ins.offset, /* block_len */ | |
1008 | ins.offset, /* orig_block_len */ | |
1009 | ram_size, /* ram_bytes */ | |
1010 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1011 | BTRFS_ORDERED_REGULAR /* type */); |
6f9994db | 1012 | if (IS_ERR(em)) |
ace68bac | 1013 | goto out_reserve; |
6f9994db | 1014 | free_extent_map(em); |
e6dcd2dc | 1015 | |
e6dcd2dc | 1016 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1017 | ram_size, cur_alloc_size, 0); |
ace68bac | 1018 | if (ret) |
d9f85963 | 1019 | goto out_drop_extent_cache; |
c8b97818 | 1020 | |
17d217fe YZ |
1021 | if (root->root_key.objectid == |
1022 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1023 | ret = btrfs_reloc_clone_csums(inode, start, | |
1024 | cur_alloc_size); | |
4dbd80fb QW |
1025 | /* |
1026 | * Only drop cache here, and process as normal. | |
1027 | * | |
1028 | * We must not allow extent_clear_unlock_delalloc() | |
1029 | * at out_unlock label to free meta of this ordered | |
1030 | * extent, as its meta should be freed by | |
1031 | * btrfs_finish_ordered_io(). | |
1032 | * | |
1033 | * So we must continue until @start is increased to | |
1034 | * skip current ordered extent. | |
1035 | */ | |
00361589 | 1036 | if (ret) |
4dbd80fb QW |
1037 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1038 | start + ram_size - 1, 0); | |
17d217fe YZ |
1039 | } |
1040 | ||
0b246afa | 1041 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1042 | |
c8b97818 CM |
1043 | /* we're not doing compressed IO, don't unlock the first |
1044 | * page (which the caller expects to stay locked), don't | |
1045 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1046 | * |
1047 | * Do set the Private2 bit so we know this page was properly | |
1048 | * setup for writepage | |
c8b97818 | 1049 | */ |
a315e68f FM |
1050 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1051 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1052 | |
c2790a2e | 1053 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1054 | start + ram_size - 1, |
1055 | delalloc_end, locked_page, | |
c2790a2e | 1056 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1057 | page_ops); |
4dbd80fb QW |
1058 | if (disk_num_bytes < cur_alloc_size) |
1059 | disk_num_bytes = 0; | |
1060 | else | |
1061 | disk_num_bytes -= cur_alloc_size; | |
c59f8951 CM |
1062 | num_bytes -= cur_alloc_size; |
1063 | alloc_hint = ins.objectid + ins.offset; | |
1064 | start += cur_alloc_size; | |
a315e68f | 1065 | extent_reserved = false; |
4dbd80fb QW |
1066 | |
1067 | /* | |
1068 | * btrfs_reloc_clone_csums() error, since start is increased | |
1069 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1070 | * free metadata of current ordered extent, we're OK to exit. | |
1071 | */ | |
1072 | if (ret) | |
1073 | goto out_unlock; | |
b888db2b | 1074 | } |
79787eaa | 1075 | out: |
be20aa9d | 1076 | return ret; |
b7d5b0a8 | 1077 | |
d9f85963 | 1078 | out_drop_extent_cache: |
dcdbc059 | 1079 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1080 | out_reserve: |
0b246afa | 1081 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1082 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1083 | out_unlock: |
a7e3b975 FM |
1084 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1085 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1086 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1087 | PAGE_END_WRITEBACK; | |
1088 | /* | |
1089 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1090 | * failed to create the respective ordered extent, then it means that | |
1091 | * when we reserved the extent we decremented the extent's size from | |
1092 | * the data space_info's bytes_may_use counter and incremented the | |
1093 | * space_info's bytes_reserved counter by the same amount. We must make | |
1094 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1095 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1096 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1097 | */ | |
1098 | if (extent_reserved) { | |
1099 | extent_clear_unlock_delalloc(inode, start, | |
1100 | start + cur_alloc_size, | |
1101 | start + cur_alloc_size, | |
1102 | locked_page, | |
1103 | clear_bits, | |
1104 | page_ops); | |
1105 | start += cur_alloc_size; | |
1106 | if (start >= end) | |
1107 | goto out; | |
1108 | } | |
ba8b04c1 QW |
1109 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1110 | locked_page, | |
a315e68f FM |
1111 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1112 | page_ops); | |
79787eaa | 1113 | goto out; |
771ed689 | 1114 | } |
c8b97818 | 1115 | |
771ed689 CM |
1116 | /* |
1117 | * work queue call back to started compression on a file and pages | |
1118 | */ | |
1119 | static noinline void async_cow_start(struct btrfs_work *work) | |
1120 | { | |
1121 | struct async_cow *async_cow; | |
1122 | int num_added = 0; | |
1123 | async_cow = container_of(work, struct async_cow, work); | |
1124 | ||
1125 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1126 | async_cow->start, async_cow->end, async_cow, | |
1127 | &num_added); | |
8180ef88 | 1128 | if (num_added == 0) { |
cb77fcd8 | 1129 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1130 | async_cow->inode = NULL; |
8180ef88 | 1131 | } |
771ed689 CM |
1132 | } |
1133 | ||
1134 | /* | |
1135 | * work queue call back to submit previously compressed pages | |
1136 | */ | |
1137 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1138 | { | |
0b246afa | 1139 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
1140 | struct async_cow *async_cow; |
1141 | struct btrfs_root *root; | |
1142 | unsigned long nr_pages; | |
1143 | ||
1144 | async_cow = container_of(work, struct async_cow, work); | |
1145 | ||
1146 | root = async_cow->root; | |
0b246afa | 1147 | fs_info = root->fs_info; |
09cbfeaf KS |
1148 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1149 | PAGE_SHIFT; | |
771ed689 | 1150 | |
ee863954 DS |
1151 | /* |
1152 | * atomic_sub_return implies a barrier for waitqueue_active | |
1153 | */ | |
0b246afa | 1154 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
ee22184b | 1155 | 5 * SZ_1M && |
0b246afa JM |
1156 | waitqueue_active(&fs_info->async_submit_wait)) |
1157 | wake_up(&fs_info->async_submit_wait); | |
771ed689 | 1158 | |
d397712b | 1159 | if (async_cow->inode) |
771ed689 | 1160 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1161 | } |
c8b97818 | 1162 | |
771ed689 CM |
1163 | static noinline void async_cow_free(struct btrfs_work *work) |
1164 | { | |
1165 | struct async_cow *async_cow; | |
1166 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1167 | if (async_cow->inode) |
cb77fcd8 | 1168 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1169 | kfree(async_cow); |
1170 | } | |
1171 | ||
1172 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1173 | u64 start, u64 end, int *page_started, | |
1174 | unsigned long *nr_written) | |
1175 | { | |
0b246afa | 1176 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
1177 | struct async_cow *async_cow; |
1178 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1179 | unsigned long nr_pages; | |
1180 | u64 cur_end; | |
771ed689 | 1181 | |
a3429ab7 CM |
1182 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1183 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1184 | while (start < end) { |
771ed689 | 1185 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1186 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1187 | async_cow->inode = igrab(inode); |
771ed689 CM |
1188 | async_cow->root = root; |
1189 | async_cow->locked_page = locked_page; | |
1190 | async_cow->start = start; | |
1191 | ||
f79707b0 | 1192 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1193 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1194 | cur_end = end; |
1195 | else | |
ee22184b | 1196 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1197 | |
1198 | async_cow->end = cur_end; | |
1199 | INIT_LIST_HEAD(&async_cow->extents); | |
1200 | ||
9e0af237 LB |
1201 | btrfs_init_work(&async_cow->work, |
1202 | btrfs_delalloc_helper, | |
1203 | async_cow_start, async_cow_submit, | |
1204 | async_cow_free); | |
771ed689 | 1205 | |
09cbfeaf KS |
1206 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1207 | PAGE_SHIFT; | |
0b246afa | 1208 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1209 | |
0b246afa | 1210 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1211 | |
0b246afa JM |
1212 | while (atomic_read(&fs_info->async_submit_draining) && |
1213 | atomic_read(&fs_info->async_delalloc_pages)) { | |
1214 | wait_event(fs_info->async_submit_wait, | |
1215 | (atomic_read(&fs_info->async_delalloc_pages) == | |
1216 | 0)); | |
771ed689 CM |
1217 | } |
1218 | ||
1219 | *nr_written += nr_pages; | |
1220 | start = cur_end + 1; | |
1221 | } | |
1222 | *page_started = 1; | |
1223 | return 0; | |
be20aa9d CM |
1224 | } |
1225 | ||
2ff7e61e | 1226 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1227 | u64 bytenr, u64 num_bytes) |
1228 | { | |
1229 | int ret; | |
1230 | struct btrfs_ordered_sum *sums; | |
1231 | LIST_HEAD(list); | |
1232 | ||
0b246afa | 1233 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1234 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1235 | if (ret == 0 && list_empty(&list)) |
1236 | return 0; | |
1237 | ||
1238 | while (!list_empty(&list)) { | |
1239 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1240 | list_del(&sums->list); | |
1241 | kfree(sums); | |
1242 | } | |
1243 | return 1; | |
1244 | } | |
1245 | ||
d352ac68 CM |
1246 | /* |
1247 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1248 | * of the extents that exist in the file, and COWs the file as required. | |
1249 | * | |
1250 | * If no cow copies or snapshots exist, we write directly to the existing | |
1251 | * blocks on disk | |
1252 | */ | |
7f366cfe CM |
1253 | static noinline int run_delalloc_nocow(struct inode *inode, |
1254 | struct page *locked_page, | |
771ed689 CM |
1255 | u64 start, u64 end, int *page_started, int force, |
1256 | unsigned long *nr_written) | |
be20aa9d | 1257 | { |
0b246afa | 1258 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d CM |
1259 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1260 | struct extent_buffer *leaf; | |
be20aa9d | 1261 | struct btrfs_path *path; |
80ff3856 | 1262 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1263 | struct btrfs_key found_key; |
6f9994db | 1264 | struct extent_map *em; |
80ff3856 YZ |
1265 | u64 cow_start; |
1266 | u64 cur_offset; | |
1267 | u64 extent_end; | |
5d4f98a2 | 1268 | u64 extent_offset; |
80ff3856 YZ |
1269 | u64 disk_bytenr; |
1270 | u64 num_bytes; | |
b4939680 | 1271 | u64 disk_num_bytes; |
cc95bef6 | 1272 | u64 ram_bytes; |
80ff3856 | 1273 | int extent_type; |
79787eaa | 1274 | int ret, err; |
d899e052 | 1275 | int type; |
80ff3856 YZ |
1276 | int nocow; |
1277 | int check_prev = 1; | |
82d5902d | 1278 | bool nolock; |
4a0cc7ca | 1279 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1280 | |
1281 | path = btrfs_alloc_path(); | |
17ca04af | 1282 | if (!path) { |
ba8b04c1 QW |
1283 | extent_clear_unlock_delalloc(inode, start, end, end, |
1284 | locked_page, | |
c2790a2e | 1285 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1286 | EXTENT_DO_ACCOUNTING | |
1287 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1288 | PAGE_CLEAR_DIRTY | |
1289 | PAGE_SET_WRITEBACK | | |
1290 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1291 | return -ENOMEM; |
17ca04af | 1292 | } |
82d5902d | 1293 | |
70ddc553 | 1294 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
82d5902d | 1295 | |
80ff3856 YZ |
1296 | cow_start = (u64)-1; |
1297 | cur_offset = start; | |
1298 | while (1) { | |
e4c3b2dc | 1299 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1300 | cur_offset, 0); |
d788a349 | 1301 | if (ret < 0) |
79787eaa | 1302 | goto error; |
80ff3856 YZ |
1303 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1304 | leaf = path->nodes[0]; | |
1305 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1306 | path->slots[0] - 1); | |
33345d01 | 1307 | if (found_key.objectid == ino && |
80ff3856 YZ |
1308 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1309 | path->slots[0]--; | |
1310 | } | |
1311 | check_prev = 0; | |
1312 | next_slot: | |
1313 | leaf = path->nodes[0]; | |
1314 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1315 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1316 | if (ret < 0) |
79787eaa | 1317 | goto error; |
80ff3856 YZ |
1318 | if (ret > 0) |
1319 | break; | |
1320 | leaf = path->nodes[0]; | |
1321 | } | |
be20aa9d | 1322 | |
80ff3856 YZ |
1323 | nocow = 0; |
1324 | disk_bytenr = 0; | |
17d217fe | 1325 | num_bytes = 0; |
80ff3856 YZ |
1326 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1327 | ||
1d512cb7 FM |
1328 | if (found_key.objectid > ino) |
1329 | break; | |
1330 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1331 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1332 | path->slots[0]++; | |
1333 | goto next_slot; | |
1334 | } | |
1335 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1336 | found_key.offset > end) |
1337 | break; | |
1338 | ||
1339 | if (found_key.offset > cur_offset) { | |
1340 | extent_end = found_key.offset; | |
e9061e21 | 1341 | extent_type = 0; |
80ff3856 YZ |
1342 | goto out_check; |
1343 | } | |
1344 | ||
1345 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1346 | struct btrfs_file_extent_item); | |
1347 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1348 | ||
cc95bef6 | 1349 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1350 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1351 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1352 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1353 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1354 | extent_end = found_key.offset + |
1355 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1356 | disk_num_bytes = |
1357 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1358 | if (extent_end <= start) { |
1359 | path->slots[0]++; | |
1360 | goto next_slot; | |
1361 | } | |
17d217fe YZ |
1362 | if (disk_bytenr == 0) |
1363 | goto out_check; | |
80ff3856 YZ |
1364 | if (btrfs_file_extent_compression(leaf, fi) || |
1365 | btrfs_file_extent_encryption(leaf, fi) || | |
1366 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1367 | goto out_check; | |
d899e052 YZ |
1368 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1369 | goto out_check; | |
2ff7e61e | 1370 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1371 | goto out_check; |
e4c3b2dc | 1372 | if (btrfs_cross_ref_exist(root, ino, |
5d4f98a2 YZ |
1373 | found_key.offset - |
1374 | extent_offset, disk_bytenr)) | |
17d217fe | 1375 | goto out_check; |
5d4f98a2 | 1376 | disk_bytenr += extent_offset; |
17d217fe YZ |
1377 | disk_bytenr += cur_offset - found_key.offset; |
1378 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1379 | /* |
1380 | * if there are pending snapshots for this root, | |
1381 | * we fall into common COW way. | |
1382 | */ | |
1383 | if (!nolock) { | |
9ea24bbe | 1384 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1385 | if (!err) |
1386 | goto out_check; | |
1387 | } | |
17d217fe YZ |
1388 | /* |
1389 | * force cow if csum exists in the range. | |
1390 | * this ensure that csum for a given extent are | |
1391 | * either valid or do not exist. | |
1392 | */ | |
2ff7e61e | 1393 | if (csum_exist_in_range(fs_info, disk_bytenr, |
91e1f56a RK |
1394 | num_bytes)) { |
1395 | if (!nolock) | |
1396 | btrfs_end_write_no_snapshoting(root); | |
17d217fe | 1397 | goto out_check; |
91e1f56a RK |
1398 | } |
1399 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { | |
1400 | if (!nolock) | |
1401 | btrfs_end_write_no_snapshoting(root); | |
f78c436c | 1402 | goto out_check; |
91e1f56a | 1403 | } |
80ff3856 YZ |
1404 | nocow = 1; |
1405 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1406 | extent_end = found_key.offset + | |
514ac8ad CM |
1407 | btrfs_file_extent_inline_len(leaf, |
1408 | path->slots[0], fi); | |
da17066c | 1409 | extent_end = ALIGN(extent_end, |
0b246afa | 1410 | fs_info->sectorsize); |
80ff3856 YZ |
1411 | } else { |
1412 | BUG_ON(1); | |
1413 | } | |
1414 | out_check: | |
1415 | if (extent_end <= start) { | |
1416 | path->slots[0]++; | |
e9894fd3 | 1417 | if (!nolock && nocow) |
9ea24bbe | 1418 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1419 | if (nocow) |
0b246afa | 1420 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1421 | goto next_slot; |
1422 | } | |
1423 | if (!nocow) { | |
1424 | if (cow_start == (u64)-1) | |
1425 | cow_start = cur_offset; | |
1426 | cur_offset = extent_end; | |
1427 | if (cur_offset > end) | |
1428 | break; | |
1429 | path->slots[0]++; | |
1430 | goto next_slot; | |
7ea394f1 YZ |
1431 | } |
1432 | ||
b3b4aa74 | 1433 | btrfs_release_path(path); |
80ff3856 | 1434 | if (cow_start != (u64)-1) { |
00361589 JB |
1435 | ret = cow_file_range(inode, locked_page, |
1436 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1437 | end, page_started, nr_written, 1, |
1438 | NULL); | |
e9894fd3 WS |
1439 | if (ret) { |
1440 | if (!nolock && nocow) | |
9ea24bbe | 1441 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1442 | if (nocow) |
0b246afa | 1443 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1444 | disk_bytenr); |
79787eaa | 1445 | goto error; |
e9894fd3 | 1446 | } |
80ff3856 | 1447 | cow_start = (u64)-1; |
7ea394f1 | 1448 | } |
80ff3856 | 1449 | |
d899e052 | 1450 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db LB |
1451 | u64 orig_start = found_key.offset - extent_offset; |
1452 | ||
1453 | em = create_io_em(inode, cur_offset, num_bytes, | |
1454 | orig_start, | |
1455 | disk_bytenr, /* block_start */ | |
1456 | num_bytes, /* block_len */ | |
1457 | disk_num_bytes, /* orig_block_len */ | |
1458 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1459 | BTRFS_ORDERED_PREALLOC); | |
1460 | if (IS_ERR(em)) { | |
1461 | if (!nolock && nocow) | |
1462 | btrfs_end_write_no_snapshoting(root); | |
1463 | if (nocow) | |
1464 | btrfs_dec_nocow_writers(fs_info, | |
1465 | disk_bytenr); | |
1466 | ret = PTR_ERR(em); | |
1467 | goto error; | |
d899e052 | 1468 | } |
6f9994db LB |
1469 | free_extent_map(em); |
1470 | } | |
1471 | ||
1472 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d899e052 YZ |
1473 | type = BTRFS_ORDERED_PREALLOC; |
1474 | } else { | |
1475 | type = BTRFS_ORDERED_NOCOW; | |
1476 | } | |
80ff3856 YZ |
1477 | |
1478 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1479 | num_bytes, num_bytes, type); |
f78c436c | 1480 | if (nocow) |
0b246afa | 1481 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1482 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1483 | |
efa56464 | 1484 | if (root->root_key.objectid == |
4dbd80fb QW |
1485 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1486 | /* | |
1487 | * Error handled later, as we must prevent | |
1488 | * extent_clear_unlock_delalloc() in error handler | |
1489 | * from freeing metadata of created ordered extent. | |
1490 | */ | |
efa56464 YZ |
1491 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1492 | num_bytes); | |
efa56464 | 1493 | |
c2790a2e | 1494 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1495 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1496 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1497 | EXTENT_DELALLOC | |
1498 | EXTENT_CLEAR_DATA_RESV, | |
1499 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1500 | ||
e9894fd3 | 1501 | if (!nolock && nocow) |
9ea24bbe | 1502 | btrfs_end_write_no_snapshoting(root); |
80ff3856 | 1503 | cur_offset = extent_end; |
4dbd80fb QW |
1504 | |
1505 | /* | |
1506 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1507 | * handler, as metadata for created ordered extent will only | |
1508 | * be freed by btrfs_finish_ordered_io(). | |
1509 | */ | |
1510 | if (ret) | |
1511 | goto error; | |
80ff3856 YZ |
1512 | if (cur_offset > end) |
1513 | break; | |
be20aa9d | 1514 | } |
b3b4aa74 | 1515 | btrfs_release_path(path); |
80ff3856 | 1516 | |
17ca04af | 1517 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1518 | cow_start = cur_offset; |
17ca04af JB |
1519 | cur_offset = end; |
1520 | } | |
1521 | ||
80ff3856 | 1522 | if (cow_start != (u64)-1) { |
dda3245e WX |
1523 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1524 | page_started, nr_written, 1, NULL); | |
d788a349 | 1525 | if (ret) |
79787eaa | 1526 | goto error; |
80ff3856 YZ |
1527 | } |
1528 | ||
79787eaa | 1529 | error: |
17ca04af | 1530 | if (ret && cur_offset < end) |
ba8b04c1 | 1531 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1532 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1533 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1534 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1535 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1536 | PAGE_SET_WRITEBACK | |
1537 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1538 | btrfs_free_path(path); |
79787eaa | 1539 | return ret; |
be20aa9d CM |
1540 | } |
1541 | ||
47059d93 WS |
1542 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1543 | { | |
1544 | ||
1545 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1546 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1547 | return 0; | |
1548 | ||
1549 | /* | |
1550 | * @defrag_bytes is a hint value, no spinlock held here, | |
1551 | * if is not zero, it means the file is defragging. | |
1552 | * Force cow if given extent needs to be defragged. | |
1553 | */ | |
1554 | if (BTRFS_I(inode)->defrag_bytes && | |
1555 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1556 | EXTENT_DEFRAG, 0, NULL)) | |
1557 | return 1; | |
1558 | ||
1559 | return 0; | |
1560 | } | |
1561 | ||
d352ac68 CM |
1562 | /* |
1563 | * extent_io.c call back to do delayed allocation processing | |
1564 | */ | |
c6100a4b | 1565 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
771ed689 CM |
1566 | u64 start, u64 end, int *page_started, |
1567 | unsigned long *nr_written) | |
be20aa9d | 1568 | { |
c6100a4b | 1569 | struct inode *inode = private_data; |
be20aa9d | 1570 | int ret; |
47059d93 | 1571 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1572 | |
47059d93 | 1573 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1574 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1575 | page_started, 1, nr_written); |
47059d93 | 1576 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1577 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1578 | page_started, 0, nr_written); |
7816030e | 1579 | } else if (!inode_need_compress(inode)) { |
dda3245e WX |
1580 | ret = cow_file_range(inode, locked_page, start, end, end, |
1581 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1582 | } else { |
1583 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1584 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1585 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1586 | page_started, nr_written); |
7ddf5a42 | 1587 | } |
52427260 QW |
1588 | if (ret) |
1589 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); | |
b888db2b CM |
1590 | return ret; |
1591 | } | |
1592 | ||
c6100a4b | 1593 | static void btrfs_split_extent_hook(void *private_data, |
1bf85046 | 1594 | struct extent_state *orig, u64 split) |
9ed74f2d | 1595 | { |
c6100a4b | 1596 | struct inode *inode = private_data; |
dcab6a3b JB |
1597 | u64 size; |
1598 | ||
0ca1f7ce | 1599 | /* not delalloc, ignore it */ |
9ed74f2d | 1600 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1601 | return; |
9ed74f2d | 1602 | |
dcab6a3b JB |
1603 | size = orig->end - orig->start + 1; |
1604 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1605 | u32 num_extents; |
dcab6a3b JB |
1606 | u64 new_size; |
1607 | ||
1608 | /* | |
ba117213 JB |
1609 | * See the explanation in btrfs_merge_extent_hook, the same |
1610 | * applies here, just in reverse. | |
dcab6a3b JB |
1611 | */ |
1612 | new_size = orig->end - split + 1; | |
823bb20a | 1613 | num_extents = count_max_extents(new_size); |
ba117213 | 1614 | new_size = split - orig->start; |
823bb20a DS |
1615 | num_extents += count_max_extents(new_size); |
1616 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1617 | return; |
1618 | } | |
1619 | ||
9e0baf60 JB |
1620 | spin_lock(&BTRFS_I(inode)->lock); |
1621 | BTRFS_I(inode)->outstanding_extents++; | |
1622 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1623 | } |
1624 | ||
1625 | /* | |
1626 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1627 | * extents so we can keep track of new extents that are just merged onto old | |
1628 | * extents, such as when we are doing sequential writes, so we can properly | |
1629 | * account for the metadata space we'll need. | |
1630 | */ | |
c6100a4b | 1631 | static void btrfs_merge_extent_hook(void *private_data, |
1bf85046 JM |
1632 | struct extent_state *new, |
1633 | struct extent_state *other) | |
9ed74f2d | 1634 | { |
c6100a4b | 1635 | struct inode *inode = private_data; |
dcab6a3b | 1636 | u64 new_size, old_size; |
823bb20a | 1637 | u32 num_extents; |
dcab6a3b | 1638 | |
9ed74f2d JB |
1639 | /* not delalloc, ignore it */ |
1640 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1641 | return; |
9ed74f2d | 1642 | |
8461a3de JB |
1643 | if (new->start > other->start) |
1644 | new_size = new->end - other->start + 1; | |
1645 | else | |
1646 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1647 | |
1648 | /* we're not bigger than the max, unreserve the space and go */ | |
1649 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1650 | spin_lock(&BTRFS_I(inode)->lock); | |
1651 | BTRFS_I(inode)->outstanding_extents--; | |
1652 | spin_unlock(&BTRFS_I(inode)->lock); | |
1653 | return; | |
1654 | } | |
1655 | ||
1656 | /* | |
ba117213 JB |
1657 | * We have to add up either side to figure out how many extents were |
1658 | * accounted for before we merged into one big extent. If the number of | |
1659 | * extents we accounted for is <= the amount we need for the new range | |
1660 | * then we can return, otherwise drop. Think of it like this | |
1661 | * | |
1662 | * [ 4k][MAX_SIZE] | |
1663 | * | |
1664 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1665 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1666 | * we have 1 so they are == and we can return. But in this case | |
1667 | * | |
1668 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1669 | * | |
1670 | * Each range on their own accounts for 2 extents, but merged together | |
1671 | * they are only 3 extents worth of accounting, so we need to drop in | |
1672 | * this case. | |
dcab6a3b | 1673 | */ |
ba117213 | 1674 | old_size = other->end - other->start + 1; |
823bb20a | 1675 | num_extents = count_max_extents(old_size); |
ba117213 | 1676 | old_size = new->end - new->start + 1; |
823bb20a DS |
1677 | num_extents += count_max_extents(old_size); |
1678 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1679 | return; |
1680 | ||
9e0baf60 JB |
1681 | spin_lock(&BTRFS_I(inode)->lock); |
1682 | BTRFS_I(inode)->outstanding_extents--; | |
1683 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1684 | } |
1685 | ||
eb73c1b7 MX |
1686 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1687 | struct inode *inode) | |
1688 | { | |
0b246afa JM |
1689 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1690 | ||
eb73c1b7 MX |
1691 | spin_lock(&root->delalloc_lock); |
1692 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1693 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1694 | &root->delalloc_inodes); | |
1695 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1696 | &BTRFS_I(inode)->runtime_flags); | |
1697 | root->nr_delalloc_inodes++; | |
1698 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1699 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1700 | BUG_ON(!list_empty(&root->delalloc_root)); |
1701 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1702 | &fs_info->delalloc_roots); |
1703 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1704 | } |
1705 | } | |
1706 | spin_unlock(&root->delalloc_lock); | |
1707 | } | |
1708 | ||
1709 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
9e3e97f4 | 1710 | struct btrfs_inode *inode) |
eb73c1b7 | 1711 | { |
9e3e97f4 | 1712 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
0b246afa | 1713 | |
eb73c1b7 | 1714 | spin_lock(&root->delalloc_lock); |
9e3e97f4 NB |
1715 | if (!list_empty(&inode->delalloc_inodes)) { |
1716 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1717 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1718 | &inode->runtime_flags); |
eb73c1b7 MX |
1719 | root->nr_delalloc_inodes--; |
1720 | if (!root->nr_delalloc_inodes) { | |
0b246afa | 1721 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1722 | BUG_ON(list_empty(&root->delalloc_root)); |
1723 | list_del_init(&root->delalloc_root); | |
0b246afa | 1724 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1725 | } |
1726 | } | |
1727 | spin_unlock(&root->delalloc_lock); | |
1728 | } | |
1729 | ||
d352ac68 CM |
1730 | /* |
1731 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1732 | * bytes in this file, and to maintain the list of inodes that | |
1733 | * have pending delalloc work to be done. | |
1734 | */ | |
c6100a4b | 1735 | static void btrfs_set_bit_hook(void *private_data, |
9ee49a04 | 1736 | struct extent_state *state, unsigned *bits) |
291d673e | 1737 | { |
c6100a4b | 1738 | struct inode *inode = private_data; |
9ed74f2d | 1739 | |
0b246afa JM |
1740 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1741 | ||
47059d93 WS |
1742 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1743 | WARN_ON(1); | |
75eff68e CM |
1744 | /* |
1745 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1746 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1747 | * bit, which is only set or cleared with irqs on |
1748 | */ | |
0ca1f7ce | 1749 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1750 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1751 | u64 len = state->end + 1 - state->start; |
70ddc553 | 1752 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1753 | |
9e0baf60 | 1754 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1755 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1756 | } else { |
1757 | spin_lock(&BTRFS_I(inode)->lock); | |
1758 | BTRFS_I(inode)->outstanding_extents++; | |
1759 | spin_unlock(&BTRFS_I(inode)->lock); | |
1760 | } | |
287a0ab9 | 1761 | |
6a3891c5 | 1762 | /* For sanity tests */ |
0b246afa | 1763 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1764 | return; |
1765 | ||
104b4e51 NB |
1766 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
1767 | fs_info->delalloc_batch); | |
df0af1a5 | 1768 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1769 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1770 | if (*bits & EXTENT_DEFRAG) |
1771 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1772 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1773 | &BTRFS_I(inode)->runtime_flags)) |
1774 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1775 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1776 | } |
a7e3b975 FM |
1777 | |
1778 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1779 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1780 | spin_lock(&BTRFS_I(inode)->lock); | |
1781 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1782 | state->start; | |
1783 | spin_unlock(&BTRFS_I(inode)->lock); | |
1784 | } | |
291d673e CM |
1785 | } |
1786 | ||
d352ac68 CM |
1787 | /* |
1788 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1789 | */ | |
c6100a4b | 1790 | static void btrfs_clear_bit_hook(void *private_data, |
41074888 | 1791 | struct extent_state *state, |
9ee49a04 | 1792 | unsigned *bits) |
291d673e | 1793 | { |
c6100a4b | 1794 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
6fc0ef68 | 1795 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
47059d93 | 1796 | u64 len = state->end + 1 - state->start; |
823bb20a | 1797 | u32 num_extents = count_max_extents(len); |
47059d93 | 1798 | |
6fc0ef68 | 1799 | spin_lock(&inode->lock); |
47059d93 | 1800 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
6fc0ef68 NB |
1801 | inode->defrag_bytes -= len; |
1802 | spin_unlock(&inode->lock); | |
47059d93 | 1803 | |
75eff68e CM |
1804 | /* |
1805 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1806 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1807 | * bit, which is only set or cleared with irqs on |
1808 | */ | |
0ca1f7ce | 1809 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1810 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1811 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1812 | |
9e0baf60 | 1813 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1814 | *bits &= ~EXTENT_FIRST_DELALLOC; |
a315e68f | 1815 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { |
6fc0ef68 NB |
1816 | spin_lock(&inode->lock); |
1817 | inode->outstanding_extents -= num_extents; | |
1818 | spin_unlock(&inode->lock); | |
9e0baf60 | 1819 | } |
0ca1f7ce | 1820 | |
b6d08f06 JB |
1821 | /* |
1822 | * We don't reserve metadata space for space cache inodes so we | |
1823 | * don't need to call dellalloc_release_metadata if there is an | |
1824 | * error. | |
1825 | */ | |
a315e68f | 1826 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1827 | root != fs_info->tree_root) |
0ca1f7ce YZ |
1828 | btrfs_delalloc_release_metadata(inode, len); |
1829 | ||
6a3891c5 | 1830 | /* For sanity tests. */ |
0b246afa | 1831 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1832 | return; |
1833 | ||
a315e68f FM |
1834 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
1835 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1836 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
1837 | btrfs_free_reserved_data_space_noquota( |
1838 | &inode->vfs_inode, | |
51773bec | 1839 | state->start, len); |
9ed74f2d | 1840 | |
104b4e51 NB |
1841 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
1842 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
1843 | spin_lock(&inode->lock); |
1844 | inode->delalloc_bytes -= len; | |
1845 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 1846 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1847 | &inode->runtime_flags)) |
eb73c1b7 | 1848 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 1849 | spin_unlock(&inode->lock); |
291d673e | 1850 | } |
a7e3b975 FM |
1851 | |
1852 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1853 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1854 | spin_lock(&inode->lock); | |
1855 | ASSERT(inode->new_delalloc_bytes >= len); | |
1856 | inode->new_delalloc_bytes -= len; | |
1857 | spin_unlock(&inode->lock); | |
1858 | } | |
291d673e CM |
1859 | } |
1860 | ||
d352ac68 CM |
1861 | /* |
1862 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1863 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1864 | * |
1865 | * return 1 if page cannot be merged to bio | |
1866 | * return 0 if page can be merged to bio | |
1867 | * return error otherwise | |
d352ac68 | 1868 | */ |
81a75f67 | 1869 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1870 | size_t size, struct bio *bio, |
1871 | unsigned long bio_flags) | |
239b14b3 | 1872 | { |
0b246afa JM |
1873 | struct inode *inode = page->mapping->host; |
1874 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1875 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1876 | u64 length = 0; |
1877 | u64 map_length; | |
239b14b3 CM |
1878 | int ret; |
1879 | ||
771ed689 CM |
1880 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1881 | return 0; | |
1882 | ||
4f024f37 | 1883 | length = bio->bi_iter.bi_size; |
239b14b3 | 1884 | map_length = length; |
0b246afa JM |
1885 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1886 | NULL, 0); | |
6f034ece LB |
1887 | if (ret < 0) |
1888 | return ret; | |
d397712b | 1889 | if (map_length < length + size) |
239b14b3 | 1890 | return 1; |
3444a972 | 1891 | return 0; |
239b14b3 CM |
1892 | } |
1893 | ||
d352ac68 CM |
1894 | /* |
1895 | * in order to insert checksums into the metadata in large chunks, | |
1896 | * we wait until bio submission time. All the pages in the bio are | |
1897 | * checksummed and sums are attached onto the ordered extent record. | |
1898 | * | |
1899 | * At IO completion time the cums attached on the ordered extent record | |
1900 | * are inserted into the btree | |
1901 | */ | |
8c27cb35 | 1902 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
81a75f67 | 1903 | int mirror_num, unsigned long bio_flags, |
eaf25d93 | 1904 | u64 bio_offset) |
065631f6 | 1905 | { |
c6100a4b | 1906 | struct inode *inode = private_data; |
4e4cbee9 | 1907 | blk_status_t ret = 0; |
e015640f | 1908 | |
2ff7e61e | 1909 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1910 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1911 | return 0; |
1912 | } | |
e015640f | 1913 | |
4a69a410 CM |
1914 | /* |
1915 | * in order to insert checksums into the metadata in large chunks, | |
1916 | * we wait until bio submission time. All the pages in the bio are | |
1917 | * checksummed and sums are attached onto the ordered extent record. | |
1918 | * | |
1919 | * At IO completion time the cums attached on the ordered extent record | |
1920 | * are inserted into the btree | |
1921 | */ | |
8c27cb35 | 1922 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
eaf25d93 CM |
1923 | int mirror_num, unsigned long bio_flags, |
1924 | u64 bio_offset) | |
4a69a410 | 1925 | { |
c6100a4b | 1926 | struct inode *inode = private_data; |
2ff7e61e | 1927 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4e4cbee9 | 1928 | blk_status_t ret; |
61891923 | 1929 | |
2ff7e61e | 1930 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
4246a0b6 | 1931 | if (ret) { |
4e4cbee9 | 1932 | bio->bi_status = ret; |
4246a0b6 CH |
1933 | bio_endio(bio); |
1934 | } | |
61891923 | 1935 | return ret; |
44b8bd7e CM |
1936 | } |
1937 | ||
d352ac68 | 1938 | /* |
cad321ad CM |
1939 | * extent_io.c submission hook. This does the right thing for csum calculation |
1940 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1941 | */ |
8c27cb35 | 1942 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
c6100a4b JB |
1943 | int mirror_num, unsigned long bio_flags, |
1944 | u64 bio_offset) | |
44b8bd7e | 1945 | { |
c6100a4b | 1946 | struct inode *inode = private_data; |
0b246afa | 1947 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1948 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1949 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 1950 | blk_status_t ret = 0; |
19b9bdb0 | 1951 | int skip_sum; |
b812ce28 | 1952 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1953 | |
6cbff00f | 1954 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1955 | |
70ddc553 | 1956 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 1957 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1958 | |
37226b21 | 1959 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1960 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1961 | if (ret) |
61891923 | 1962 | goto out; |
5fd02043 | 1963 | |
d20f7043 | 1964 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1965 | ret = btrfs_submit_compressed_read(inode, bio, |
1966 | mirror_num, | |
1967 | bio_flags); | |
1968 | goto out; | |
c2db1073 | 1969 | } else if (!skip_sum) { |
2ff7e61e | 1970 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1971 | if (ret) |
61891923 | 1972 | goto out; |
c2db1073 | 1973 | } |
4d1b5fb4 | 1974 | goto mapit; |
b812ce28 | 1975 | } else if (async && !skip_sum) { |
17d217fe YZ |
1976 | /* csum items have already been cloned */ |
1977 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1978 | goto mapit; | |
19b9bdb0 | 1979 | /* we're doing a write, do the async checksumming */ |
c6100a4b JB |
1980 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
1981 | bio_offset, inode, | |
0b246afa JM |
1982 | __btrfs_submit_bio_start, |
1983 | __btrfs_submit_bio_done); | |
61891923 | 1984 | goto out; |
b812ce28 | 1985 | } else if (!skip_sum) { |
2ff7e61e | 1986 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
1987 | if (ret) |
1988 | goto out; | |
19b9bdb0 CM |
1989 | } |
1990 | ||
0b86a832 | 1991 | mapit: |
2ff7e61e | 1992 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
1993 | |
1994 | out: | |
4e4cbee9 CH |
1995 | if (ret) { |
1996 | bio->bi_status = ret; | |
4246a0b6 CH |
1997 | bio_endio(bio); |
1998 | } | |
61891923 | 1999 | return ret; |
065631f6 | 2000 | } |
6885f308 | 2001 | |
d352ac68 CM |
2002 | /* |
2003 | * given a list of ordered sums record them in the inode. This happens | |
2004 | * at IO completion time based on sums calculated at bio submission time. | |
2005 | */ | |
ba1da2f4 | 2006 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2007 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2008 | { |
e6dcd2dc CM |
2009 | struct btrfs_ordered_sum *sum; |
2010 | ||
c6e30871 | 2011 | list_for_each_entry(sum, list, list) { |
39847c4d | 2012 | trans->adding_csums = 1; |
d20f7043 CM |
2013 | btrfs_csum_file_blocks(trans, |
2014 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 2015 | trans->adding_csums = 0; |
e6dcd2dc CM |
2016 | } |
2017 | return 0; | |
2018 | } | |
2019 | ||
2ac55d41 | 2020 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2021 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2022 | { |
09cbfeaf | 2023 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2024 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2025 | cached_state); |
ea8c2819 CM |
2026 | } |
2027 | ||
d352ac68 | 2028 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2029 | struct btrfs_writepage_fixup { |
2030 | struct page *page; | |
2031 | struct btrfs_work work; | |
2032 | }; | |
2033 | ||
b2950863 | 2034 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2035 | { |
2036 | struct btrfs_writepage_fixup *fixup; | |
2037 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2038 | struct extent_state *cached_state = NULL; |
364ecf36 | 2039 | struct extent_changeset *data_reserved = 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 | |
364ecf36 | 2077 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 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); |
364ecf36 | 2097 | extent_changeset_free(data_reserved); |
247e743c CM |
2098 | } |
2099 | ||
2100 | /* | |
2101 | * There are a few paths in the higher layers of the kernel that directly | |
2102 | * set the page dirty bit without asking the filesystem if it is a | |
2103 | * good idea. This causes problems because we want to make sure COW | |
2104 | * properly happens and the data=ordered rules are followed. | |
2105 | * | |
c8b97818 | 2106 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2107 | * hasn't been properly setup for IO. We kick off an async process |
2108 | * to fix it up. The async helper will wait for ordered extents, set | |
2109 | * the delalloc bit and make it safe to write the page. | |
2110 | */ | |
b2950863 | 2111 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2112 | { |
2113 | struct inode *inode = page->mapping->host; | |
0b246afa | 2114 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2115 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2116 | |
8b62b72b CM |
2117 | /* this page is properly in the ordered list */ |
2118 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2119 | return 0; |
2120 | ||
2121 | if (PageChecked(page)) | |
2122 | return -EAGAIN; | |
2123 | ||
2124 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2125 | if (!fixup) | |
2126 | return -EAGAIN; | |
f421950f | 2127 | |
247e743c | 2128 | SetPageChecked(page); |
09cbfeaf | 2129 | get_page(page); |
9e0af237 LB |
2130 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2131 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2132 | fixup->page = page; |
0b246afa | 2133 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2134 | return -EBUSY; |
247e743c CM |
2135 | } |
2136 | ||
d899e052 YZ |
2137 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2138 | struct inode *inode, u64 file_pos, | |
2139 | u64 disk_bytenr, u64 disk_num_bytes, | |
2140 | u64 num_bytes, u64 ram_bytes, | |
2141 | u8 compression, u8 encryption, | |
2142 | u16 other_encoding, int extent_type) | |
2143 | { | |
2144 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2145 | struct btrfs_file_extent_item *fi; | |
2146 | struct btrfs_path *path; | |
2147 | struct extent_buffer *leaf; | |
2148 | struct btrfs_key ins; | |
a12b877b | 2149 | u64 qg_released; |
1acae57b | 2150 | int extent_inserted = 0; |
d899e052 YZ |
2151 | int ret; |
2152 | ||
2153 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2154 | if (!path) |
2155 | return -ENOMEM; | |
d899e052 | 2156 | |
a1ed835e CM |
2157 | /* |
2158 | * we may be replacing one extent in the tree with another. | |
2159 | * The new extent is pinned in the extent map, and we don't want | |
2160 | * to drop it from the cache until it is completely in the btree. | |
2161 | * | |
2162 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2163 | * the caller is expected to unpin it and allow it to be merged | |
2164 | * with the others. | |
2165 | */ | |
1acae57b FDBM |
2166 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2167 | file_pos + num_bytes, NULL, 0, | |
2168 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2169 | if (ret) |
2170 | goto out; | |
d899e052 | 2171 | |
1acae57b | 2172 | if (!extent_inserted) { |
4a0cc7ca | 2173 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2174 | ins.offset = file_pos; |
2175 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2176 | ||
2177 | path->leave_spinning = 1; | |
2178 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2179 | sizeof(*fi)); | |
2180 | if (ret) | |
2181 | goto out; | |
2182 | } | |
d899e052 YZ |
2183 | leaf = path->nodes[0]; |
2184 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2185 | struct btrfs_file_extent_item); | |
2186 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2187 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2188 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2189 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2190 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2191 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2192 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2193 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2194 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2195 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2196 | |
d899e052 | 2197 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2198 | btrfs_release_path(path); |
d899e052 YZ |
2199 | |
2200 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2201 | |
2202 | ins.objectid = disk_bytenr; | |
2203 | ins.offset = disk_num_bytes; | |
2204 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2205 | |
297d750b | 2206 | /* |
5846a3c2 QW |
2207 | * Release the reserved range from inode dirty range map, as it is |
2208 | * already moved into delayed_ref_head | |
297d750b | 2209 | */ |
a12b877b QW |
2210 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
2211 | if (ret < 0) | |
2212 | goto out; | |
2213 | qg_released = ret; | |
2214 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, | |
2215 | btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins); | |
79787eaa | 2216 | out: |
d899e052 | 2217 | btrfs_free_path(path); |
b9473439 | 2218 | |
79787eaa | 2219 | return ret; |
d899e052 YZ |
2220 | } |
2221 | ||
38c227d8 LB |
2222 | /* snapshot-aware defrag */ |
2223 | struct sa_defrag_extent_backref { | |
2224 | struct rb_node node; | |
2225 | struct old_sa_defrag_extent *old; | |
2226 | u64 root_id; | |
2227 | u64 inum; | |
2228 | u64 file_pos; | |
2229 | u64 extent_offset; | |
2230 | u64 num_bytes; | |
2231 | u64 generation; | |
2232 | }; | |
2233 | ||
2234 | struct old_sa_defrag_extent { | |
2235 | struct list_head list; | |
2236 | struct new_sa_defrag_extent *new; | |
2237 | ||
2238 | u64 extent_offset; | |
2239 | u64 bytenr; | |
2240 | u64 offset; | |
2241 | u64 len; | |
2242 | int count; | |
2243 | }; | |
2244 | ||
2245 | struct new_sa_defrag_extent { | |
2246 | struct rb_root root; | |
2247 | struct list_head head; | |
2248 | struct btrfs_path *path; | |
2249 | struct inode *inode; | |
2250 | u64 file_pos; | |
2251 | u64 len; | |
2252 | u64 bytenr; | |
2253 | u64 disk_len; | |
2254 | u8 compress_type; | |
2255 | }; | |
2256 | ||
2257 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2258 | struct sa_defrag_extent_backref *b2) | |
2259 | { | |
2260 | if (b1->root_id < b2->root_id) | |
2261 | return -1; | |
2262 | else if (b1->root_id > b2->root_id) | |
2263 | return 1; | |
2264 | ||
2265 | if (b1->inum < b2->inum) | |
2266 | return -1; | |
2267 | else if (b1->inum > b2->inum) | |
2268 | return 1; | |
2269 | ||
2270 | if (b1->file_pos < b2->file_pos) | |
2271 | return -1; | |
2272 | else if (b1->file_pos > b2->file_pos) | |
2273 | return 1; | |
2274 | ||
2275 | /* | |
2276 | * [------------------------------] ===> (a range of space) | |
2277 | * |<--->| |<---->| =============> (fs/file tree A) | |
2278 | * |<---------------------------->| ===> (fs/file tree B) | |
2279 | * | |
2280 | * A range of space can refer to two file extents in one tree while | |
2281 | * refer to only one file extent in another tree. | |
2282 | * | |
2283 | * So we may process a disk offset more than one time(two extents in A) | |
2284 | * and locate at the same extent(one extent in B), then insert two same | |
2285 | * backrefs(both refer to the extent in B). | |
2286 | */ | |
2287 | return 0; | |
2288 | } | |
2289 | ||
2290 | static void backref_insert(struct rb_root *root, | |
2291 | struct sa_defrag_extent_backref *backref) | |
2292 | { | |
2293 | struct rb_node **p = &root->rb_node; | |
2294 | struct rb_node *parent = NULL; | |
2295 | struct sa_defrag_extent_backref *entry; | |
2296 | int ret; | |
2297 | ||
2298 | while (*p) { | |
2299 | parent = *p; | |
2300 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2301 | ||
2302 | ret = backref_comp(backref, entry); | |
2303 | if (ret < 0) | |
2304 | p = &(*p)->rb_left; | |
2305 | else | |
2306 | p = &(*p)->rb_right; | |
2307 | } | |
2308 | ||
2309 | rb_link_node(&backref->node, parent, p); | |
2310 | rb_insert_color(&backref->node, root); | |
2311 | } | |
2312 | ||
2313 | /* | |
2314 | * Note the backref might has changed, and in this case we just return 0. | |
2315 | */ | |
2316 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2317 | void *ctx) | |
2318 | { | |
2319 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2320 | struct old_sa_defrag_extent *old = ctx; |
2321 | struct new_sa_defrag_extent *new = old->new; | |
2322 | struct btrfs_path *path = new->path; | |
2323 | struct btrfs_key key; | |
2324 | struct btrfs_root *root; | |
2325 | struct sa_defrag_extent_backref *backref; | |
2326 | struct extent_buffer *leaf; | |
2327 | struct inode *inode = new->inode; | |
0b246afa | 2328 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2329 | int slot; |
2330 | int ret; | |
2331 | u64 extent_offset; | |
2332 | u64 num_bytes; | |
2333 | ||
2334 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2335 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2336 | return 0; |
2337 | ||
2338 | key.objectid = root_id; | |
2339 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2340 | key.offset = (u64)-1; | |
2341 | ||
38c227d8 LB |
2342 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2343 | if (IS_ERR(root)) { | |
2344 | if (PTR_ERR(root) == -ENOENT) | |
2345 | return 0; | |
2346 | WARN_ON(1); | |
ab8d0fc4 | 2347 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2348 | inum, offset, root_id); |
2349 | return PTR_ERR(root); | |
2350 | } | |
2351 | ||
2352 | key.objectid = inum; | |
2353 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2354 | if (offset > (u64)-1 << 32) | |
2355 | key.offset = 0; | |
2356 | else | |
2357 | key.offset = offset; | |
2358 | ||
2359 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2360 | if (WARN_ON(ret < 0)) |
38c227d8 | 2361 | return ret; |
50f1319c | 2362 | ret = 0; |
38c227d8 LB |
2363 | |
2364 | while (1) { | |
2365 | cond_resched(); | |
2366 | ||
2367 | leaf = path->nodes[0]; | |
2368 | slot = path->slots[0]; | |
2369 | ||
2370 | if (slot >= btrfs_header_nritems(leaf)) { | |
2371 | ret = btrfs_next_leaf(root, path); | |
2372 | if (ret < 0) { | |
2373 | goto out; | |
2374 | } else if (ret > 0) { | |
2375 | ret = 0; | |
2376 | goto out; | |
2377 | } | |
2378 | continue; | |
2379 | } | |
2380 | ||
2381 | path->slots[0]++; | |
2382 | ||
2383 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2384 | ||
2385 | if (key.objectid > inum) | |
2386 | goto out; | |
2387 | ||
2388 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2389 | continue; | |
2390 | ||
2391 | extent = btrfs_item_ptr(leaf, slot, | |
2392 | struct btrfs_file_extent_item); | |
2393 | ||
2394 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2395 | continue; | |
2396 | ||
e68afa49 LB |
2397 | /* |
2398 | * 'offset' refers to the exact key.offset, | |
2399 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2400 | * (key.offset - extent_offset). | |
2401 | */ | |
2402 | if (key.offset != offset) | |
38c227d8 LB |
2403 | continue; |
2404 | ||
e68afa49 | 2405 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2406 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2407 | |
38c227d8 LB |
2408 | if (extent_offset >= old->extent_offset + old->offset + |
2409 | old->len || extent_offset + num_bytes <= | |
2410 | old->extent_offset + old->offset) | |
2411 | continue; | |
38c227d8 LB |
2412 | break; |
2413 | } | |
2414 | ||
2415 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2416 | if (!backref) { | |
2417 | ret = -ENOENT; | |
2418 | goto out; | |
2419 | } | |
2420 | ||
2421 | backref->root_id = root_id; | |
2422 | backref->inum = inum; | |
e68afa49 | 2423 | backref->file_pos = offset; |
38c227d8 LB |
2424 | backref->num_bytes = num_bytes; |
2425 | backref->extent_offset = extent_offset; | |
2426 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2427 | backref->old = old; | |
2428 | backref_insert(&new->root, backref); | |
2429 | old->count++; | |
2430 | out: | |
2431 | btrfs_release_path(path); | |
2432 | WARN_ON(ret); | |
2433 | return ret; | |
2434 | } | |
2435 | ||
2436 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2437 | struct new_sa_defrag_extent *new) | |
2438 | { | |
0b246afa | 2439 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2440 | struct old_sa_defrag_extent *old, *tmp; |
2441 | int ret; | |
2442 | ||
2443 | new->path = path; | |
2444 | ||
2445 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2446 | ret = iterate_inodes_from_logical(old->bytenr + |
2447 | old->extent_offset, fs_info, | |
38c227d8 LB |
2448 | path, record_one_backref, |
2449 | old); | |
4724b106 JB |
2450 | if (ret < 0 && ret != -ENOENT) |
2451 | return false; | |
38c227d8 LB |
2452 | |
2453 | /* no backref to be processed for this extent */ | |
2454 | if (!old->count) { | |
2455 | list_del(&old->list); | |
2456 | kfree(old); | |
2457 | } | |
2458 | } | |
2459 | ||
2460 | if (list_empty(&new->head)) | |
2461 | return false; | |
2462 | ||
2463 | return true; | |
2464 | } | |
2465 | ||
2466 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2467 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2468 | struct new_sa_defrag_extent *new) |
38c227d8 | 2469 | { |
116e0024 | 2470 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2471 | return 0; |
2472 | ||
2473 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2474 | return 0; | |
2475 | ||
116e0024 LB |
2476 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2477 | return 0; | |
2478 | ||
2479 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2480 | btrfs_file_extent_other_encoding(leaf, fi)) |
2481 | return 0; | |
2482 | ||
2483 | return 1; | |
2484 | } | |
2485 | ||
2486 | /* | |
2487 | * Note the backref might has changed, and in this case we just return 0. | |
2488 | */ | |
2489 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2490 | struct sa_defrag_extent_backref *prev, | |
2491 | struct sa_defrag_extent_backref *backref) | |
2492 | { | |
2493 | struct btrfs_file_extent_item *extent; | |
2494 | struct btrfs_file_extent_item *item; | |
2495 | struct btrfs_ordered_extent *ordered; | |
2496 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2497 | struct btrfs_root *root; |
2498 | struct btrfs_key key; | |
2499 | struct extent_buffer *leaf; | |
2500 | struct old_sa_defrag_extent *old = backref->old; | |
2501 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2502 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2503 | struct inode *inode; |
2504 | struct extent_state *cached = NULL; | |
2505 | int ret = 0; | |
2506 | u64 start; | |
2507 | u64 len; | |
2508 | u64 lock_start; | |
2509 | u64 lock_end; | |
2510 | bool merge = false; | |
2511 | int index; | |
2512 | ||
2513 | if (prev && prev->root_id == backref->root_id && | |
2514 | prev->inum == backref->inum && | |
2515 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2516 | merge = true; | |
2517 | ||
2518 | /* step 1: get root */ | |
2519 | key.objectid = backref->root_id; | |
2520 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2521 | key.offset = (u64)-1; | |
2522 | ||
38c227d8 LB |
2523 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2524 | ||
2525 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2526 | if (IS_ERR(root)) { | |
2527 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2528 | if (PTR_ERR(root) == -ENOENT) | |
2529 | return 0; | |
2530 | return PTR_ERR(root); | |
2531 | } | |
38c227d8 | 2532 | |
bcbba5e6 WS |
2533 | if (btrfs_root_readonly(root)) { |
2534 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2535 | return 0; | |
2536 | } | |
2537 | ||
38c227d8 LB |
2538 | /* step 2: get inode */ |
2539 | key.objectid = backref->inum; | |
2540 | key.type = BTRFS_INODE_ITEM_KEY; | |
2541 | key.offset = 0; | |
2542 | ||
2543 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2544 | if (IS_ERR(inode)) { | |
2545 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2546 | return 0; | |
2547 | } | |
2548 | ||
2549 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2550 | ||
2551 | /* step 3: relink backref */ | |
2552 | lock_start = backref->file_pos; | |
2553 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2554 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2555 | &cached); |
38c227d8 LB |
2556 | |
2557 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2558 | if (ordered) { | |
2559 | btrfs_put_ordered_extent(ordered); | |
2560 | goto out_unlock; | |
2561 | } | |
2562 | ||
2563 | trans = btrfs_join_transaction(root); | |
2564 | if (IS_ERR(trans)) { | |
2565 | ret = PTR_ERR(trans); | |
2566 | goto out_unlock; | |
2567 | } | |
2568 | ||
2569 | key.objectid = backref->inum; | |
2570 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2571 | key.offset = backref->file_pos; | |
2572 | ||
2573 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2574 | if (ret < 0) { | |
2575 | goto out_free_path; | |
2576 | } else if (ret > 0) { | |
2577 | ret = 0; | |
2578 | goto out_free_path; | |
2579 | } | |
2580 | ||
2581 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2582 | struct btrfs_file_extent_item); | |
2583 | ||
2584 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2585 | backref->generation) | |
2586 | goto out_free_path; | |
2587 | ||
2588 | btrfs_release_path(path); | |
2589 | ||
2590 | start = backref->file_pos; | |
2591 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2592 | start += old->extent_offset + old->offset - | |
2593 | backref->extent_offset; | |
2594 | ||
2595 | len = min(backref->extent_offset + backref->num_bytes, | |
2596 | old->extent_offset + old->offset + old->len); | |
2597 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2598 | ||
2599 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2600 | start + len, 1); | |
2601 | if (ret) | |
2602 | goto out_free_path; | |
2603 | again: | |
4a0cc7ca | 2604 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2605 | key.type = BTRFS_EXTENT_DATA_KEY; |
2606 | key.offset = start; | |
2607 | ||
a09a0a70 | 2608 | path->leave_spinning = 1; |
38c227d8 LB |
2609 | if (merge) { |
2610 | struct btrfs_file_extent_item *fi; | |
2611 | u64 extent_len; | |
2612 | struct btrfs_key found_key; | |
2613 | ||
3c9665df | 2614 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2615 | if (ret < 0) |
2616 | goto out_free_path; | |
2617 | ||
2618 | path->slots[0]--; | |
2619 | leaf = path->nodes[0]; | |
2620 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2621 | ||
2622 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2623 | struct btrfs_file_extent_item); | |
2624 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2625 | ||
116e0024 LB |
2626 | if (extent_len + found_key.offset == start && |
2627 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2628 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2629 | extent_len + len); | |
2630 | btrfs_mark_buffer_dirty(leaf); | |
2631 | inode_add_bytes(inode, len); | |
2632 | ||
2633 | ret = 1; | |
2634 | goto out_free_path; | |
2635 | } else { | |
2636 | merge = false; | |
2637 | btrfs_release_path(path); | |
2638 | goto again; | |
2639 | } | |
2640 | } | |
2641 | ||
2642 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2643 | sizeof(*extent)); | |
2644 | if (ret) { | |
66642832 | 2645 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2646 | goto out_free_path; |
2647 | } | |
2648 | ||
2649 | leaf = path->nodes[0]; | |
2650 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2651 | struct btrfs_file_extent_item); | |
2652 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2653 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2654 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2655 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2656 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2657 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2658 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2659 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2660 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2661 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2662 | ||
2663 | btrfs_mark_buffer_dirty(leaf); | |
2664 | inode_add_bytes(inode, len); | |
a09a0a70 | 2665 | btrfs_release_path(path); |
38c227d8 | 2666 | |
2ff7e61e | 2667 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
38c227d8 LB |
2668 | new->disk_len, 0, |
2669 | backref->root_id, backref->inum, | |
b06c4bf5 | 2670 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2671 | if (ret) { |
66642832 | 2672 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2673 | goto out_free_path; |
2674 | } | |
2675 | ||
2676 | ret = 1; | |
2677 | out_free_path: | |
2678 | btrfs_release_path(path); | |
a09a0a70 | 2679 | path->leave_spinning = 0; |
3a45bb20 | 2680 | btrfs_end_transaction(trans); |
38c227d8 LB |
2681 | out_unlock: |
2682 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2683 | &cached, GFP_NOFS); | |
2684 | iput(inode); | |
2685 | return ret; | |
2686 | } | |
2687 | ||
6f519564 LB |
2688 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2689 | { | |
2690 | struct old_sa_defrag_extent *old, *tmp; | |
2691 | ||
2692 | if (!new) | |
2693 | return; | |
2694 | ||
2695 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2696 | kfree(old); |
2697 | } | |
2698 | kfree(new); | |
2699 | } | |
2700 | ||
38c227d8 LB |
2701 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2702 | { | |
0b246afa | 2703 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2704 | struct btrfs_path *path; |
38c227d8 LB |
2705 | struct sa_defrag_extent_backref *backref; |
2706 | struct sa_defrag_extent_backref *prev = NULL; | |
2707 | struct inode *inode; | |
2708 | struct btrfs_root *root; | |
2709 | struct rb_node *node; | |
2710 | int ret; | |
2711 | ||
2712 | inode = new->inode; | |
2713 | root = BTRFS_I(inode)->root; | |
2714 | ||
2715 | path = btrfs_alloc_path(); | |
2716 | if (!path) | |
2717 | return; | |
2718 | ||
2719 | if (!record_extent_backrefs(path, new)) { | |
2720 | btrfs_free_path(path); | |
2721 | goto out; | |
2722 | } | |
2723 | btrfs_release_path(path); | |
2724 | ||
2725 | while (1) { | |
2726 | node = rb_first(&new->root); | |
2727 | if (!node) | |
2728 | break; | |
2729 | rb_erase(node, &new->root); | |
2730 | ||
2731 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2732 | ||
2733 | ret = relink_extent_backref(path, prev, backref); | |
2734 | WARN_ON(ret < 0); | |
2735 | ||
2736 | kfree(prev); | |
2737 | ||
2738 | if (ret == 1) | |
2739 | prev = backref; | |
2740 | else | |
2741 | prev = NULL; | |
2742 | cond_resched(); | |
2743 | } | |
2744 | kfree(prev); | |
2745 | ||
2746 | btrfs_free_path(path); | |
38c227d8 | 2747 | out: |
6f519564 LB |
2748 | free_sa_defrag_extent(new); |
2749 | ||
0b246afa JM |
2750 | atomic_dec(&fs_info->defrag_running); |
2751 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2752 | } |
2753 | ||
2754 | static struct new_sa_defrag_extent * | |
2755 | record_old_file_extents(struct inode *inode, | |
2756 | struct btrfs_ordered_extent *ordered) | |
2757 | { | |
0b246afa | 2758 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2759 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2760 | struct btrfs_path *path; | |
2761 | struct btrfs_key key; | |
6f519564 | 2762 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2763 | struct new_sa_defrag_extent *new; |
2764 | int ret; | |
2765 | ||
2766 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2767 | if (!new) | |
2768 | return NULL; | |
2769 | ||
2770 | new->inode = inode; | |
2771 | new->file_pos = ordered->file_offset; | |
2772 | new->len = ordered->len; | |
2773 | new->bytenr = ordered->start; | |
2774 | new->disk_len = ordered->disk_len; | |
2775 | new->compress_type = ordered->compress_type; | |
2776 | new->root = RB_ROOT; | |
2777 | INIT_LIST_HEAD(&new->head); | |
2778 | ||
2779 | path = btrfs_alloc_path(); | |
2780 | if (!path) | |
2781 | goto out_kfree; | |
2782 | ||
4a0cc7ca | 2783 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2784 | key.type = BTRFS_EXTENT_DATA_KEY; |
2785 | key.offset = new->file_pos; | |
2786 | ||
2787 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2788 | if (ret < 0) | |
2789 | goto out_free_path; | |
2790 | if (ret > 0 && path->slots[0] > 0) | |
2791 | path->slots[0]--; | |
2792 | ||
2793 | /* find out all the old extents for the file range */ | |
2794 | while (1) { | |
2795 | struct btrfs_file_extent_item *extent; | |
2796 | struct extent_buffer *l; | |
2797 | int slot; | |
2798 | u64 num_bytes; | |
2799 | u64 offset; | |
2800 | u64 end; | |
2801 | u64 disk_bytenr; | |
2802 | u64 extent_offset; | |
2803 | ||
2804 | l = path->nodes[0]; | |
2805 | slot = path->slots[0]; | |
2806 | ||
2807 | if (slot >= btrfs_header_nritems(l)) { | |
2808 | ret = btrfs_next_leaf(root, path); | |
2809 | if (ret < 0) | |
6f519564 | 2810 | goto out_free_path; |
38c227d8 LB |
2811 | else if (ret > 0) |
2812 | break; | |
2813 | continue; | |
2814 | } | |
2815 | ||
2816 | btrfs_item_key_to_cpu(l, &key, slot); | |
2817 | ||
4a0cc7ca | 2818 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2819 | break; |
2820 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2821 | break; | |
2822 | if (key.offset >= new->file_pos + new->len) | |
2823 | break; | |
2824 | ||
2825 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2826 | ||
2827 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2828 | if (key.offset + num_bytes < new->file_pos) | |
2829 | goto next; | |
2830 | ||
2831 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2832 | if (!disk_bytenr) | |
2833 | goto next; | |
2834 | ||
2835 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2836 | ||
2837 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2838 | if (!old) | |
6f519564 | 2839 | goto out_free_path; |
38c227d8 LB |
2840 | |
2841 | offset = max(new->file_pos, key.offset); | |
2842 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2843 | ||
2844 | old->bytenr = disk_bytenr; | |
2845 | old->extent_offset = extent_offset; | |
2846 | old->offset = offset - key.offset; | |
2847 | old->len = end - offset; | |
2848 | old->new = new; | |
2849 | old->count = 0; | |
2850 | list_add_tail(&old->list, &new->head); | |
2851 | next: | |
2852 | path->slots[0]++; | |
2853 | cond_resched(); | |
2854 | } | |
2855 | ||
2856 | btrfs_free_path(path); | |
0b246afa | 2857 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2858 | |
2859 | return new; | |
2860 | ||
38c227d8 LB |
2861 | out_free_path: |
2862 | btrfs_free_path(path); | |
2863 | out_kfree: | |
6f519564 | 2864 | free_sa_defrag_extent(new); |
38c227d8 LB |
2865 | return NULL; |
2866 | } | |
2867 | ||
2ff7e61e | 2868 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2869 | u64 start, u64 len) |
2870 | { | |
2871 | struct btrfs_block_group_cache *cache; | |
2872 | ||
0b246afa | 2873 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2874 | ASSERT(cache); |
2875 | ||
2876 | spin_lock(&cache->lock); | |
2877 | cache->delalloc_bytes -= len; | |
2878 | spin_unlock(&cache->lock); | |
2879 | ||
2880 | btrfs_put_block_group(cache); | |
2881 | } | |
2882 | ||
d352ac68 CM |
2883 | /* as ordered data IO finishes, this gets called so we can finish |
2884 | * an ordered extent if the range of bytes in the file it covers are | |
2885 | * fully written. | |
2886 | */ | |
5fd02043 | 2887 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2888 | { |
5fd02043 | 2889 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2890 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2892 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2893 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2894 | struct extent_state *cached_state = NULL; |
38c227d8 | 2895 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2896 | int compress_type = 0; |
77cef2ec JB |
2897 | int ret = 0; |
2898 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2899 | bool nolock; |
77cef2ec | 2900 | bool truncated = false; |
a7e3b975 FM |
2901 | bool range_locked = false; |
2902 | bool clear_new_delalloc_bytes = false; | |
2903 | ||
2904 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2905 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2906 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2907 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2908 | |
70ddc553 | 2909 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2910 | |
5fd02043 JB |
2911 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2912 | ret = -EIO; | |
2913 | goto out; | |
2914 | } | |
2915 | ||
7ab7956e NB |
2916 | btrfs_free_io_failure_record(BTRFS_I(inode), |
2917 | ordered_extent->file_offset, | |
2918 | ordered_extent->file_offset + | |
2919 | ordered_extent->len - 1); | |
f612496b | 2920 | |
77cef2ec JB |
2921 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2922 | truncated = true; | |
2923 | logical_len = ordered_extent->truncated_len; | |
2924 | /* Truncated the entire extent, don't bother adding */ | |
2925 | if (!logical_len) | |
2926 | goto out; | |
2927 | } | |
2928 | ||
c2167754 | 2929 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2930 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2931 | |
2932 | /* | |
2933 | * For mwrite(mmap + memset to write) case, we still reserve | |
2934 | * space for NOCOW range. | |
2935 | * As NOCOW won't cause a new delayed ref, just free the space | |
2936 | */ | |
bc42bda2 | 2937 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
94ed938a | 2938 | ordered_extent->len); |
6c760c07 JB |
2939 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2940 | if (nolock) | |
2941 | trans = btrfs_join_transaction_nolock(root); | |
2942 | else | |
2943 | trans = btrfs_join_transaction(root); | |
2944 | if (IS_ERR(trans)) { | |
2945 | ret = PTR_ERR(trans); | |
2946 | trans = NULL; | |
2947 | goto out; | |
c2167754 | 2948 | } |
0b246afa | 2949 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
6c760c07 JB |
2950 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2951 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2952 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2953 | goto out; |
2954 | } | |
e6dcd2dc | 2955 | |
a7e3b975 | 2956 | range_locked = true; |
2ac55d41 JB |
2957 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2958 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2959 | &cached_state); |
e6dcd2dc | 2960 | |
38c227d8 LB |
2961 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2962 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 2963 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
2964 | if (ret) { |
2965 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2966 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2967 | /* the inode is shared */ |
2968 | new = record_old_file_extents(inode, ordered_extent); | |
2969 | ||
2970 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2971 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2972 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2973 | } | |
2974 | ||
0cb59c99 | 2975 | if (nolock) |
7a7eaa40 | 2976 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2977 | else |
7a7eaa40 | 2978 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2979 | if (IS_ERR(trans)) { |
2980 | ret = PTR_ERR(trans); | |
2981 | trans = NULL; | |
a7e3b975 | 2982 | goto out; |
79787eaa | 2983 | } |
a79b7d4b | 2984 | |
0b246afa | 2985 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
c2167754 | 2986 | |
c8b97818 | 2987 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2988 | compress_type = ordered_extent->compress_type; |
d899e052 | 2989 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2990 | BUG_ON(compress_type); |
7a6d7067 | 2991 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2992 | ordered_extent->file_offset, |
2993 | ordered_extent->file_offset + | |
77cef2ec | 2994 | logical_len); |
d899e052 | 2995 | } else { |
0b246afa | 2996 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
2997 | ret = insert_reserved_file_extent(trans, inode, |
2998 | ordered_extent->file_offset, | |
2999 | ordered_extent->start, | |
3000 | ordered_extent->disk_len, | |
77cef2ec | 3001 | logical_len, logical_len, |
261507a0 | 3002 | compress_type, 0, 0, |
d899e052 | 3003 | BTRFS_FILE_EXTENT_REG); |
e570fd27 | 3004 | if (!ret) |
2ff7e61e | 3005 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3006 | ordered_extent->start, |
3007 | ordered_extent->disk_len); | |
d899e052 | 3008 | } |
5dc562c5 JB |
3009 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3010 | ordered_extent->file_offset, ordered_extent->len, | |
3011 | trans->transid); | |
79787eaa | 3012 | if (ret < 0) { |
66642832 | 3013 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3014 | goto out; |
79787eaa | 3015 | } |
2ac55d41 | 3016 | |
df9f628e | 3017 | add_pending_csums(trans, inode, &ordered_extent->list); |
e6dcd2dc | 3018 | |
6c760c07 JB |
3019 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3020 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3021 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3022 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3023 | goto out; |
1ef30be1 JB |
3024 | } |
3025 | ret = 0; | |
c2167754 | 3026 | out: |
a7e3b975 FM |
3027 | if (range_locked || clear_new_delalloc_bytes) { |
3028 | unsigned int clear_bits = 0; | |
3029 | ||
3030 | if (range_locked) | |
3031 | clear_bits |= EXTENT_LOCKED; | |
3032 | if (clear_new_delalloc_bytes) | |
3033 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3034 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3035 | ordered_extent->file_offset, | |
3036 | ordered_extent->file_offset + | |
3037 | ordered_extent->len - 1, | |
3038 | clear_bits, | |
3039 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
3040 | 0, &cached_state, GFP_NOFS); | |
3041 | } | |
3042 | ||
0b246afa | 3043 | if (root != fs_info->tree_root) |
691fa059 NB |
3044 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
3045 | ordered_extent->len); | |
a698d075 | 3046 | if (trans) |
3a45bb20 | 3047 | btrfs_end_transaction(trans); |
0cb59c99 | 3048 | |
77cef2ec JB |
3049 | if (ret || truncated) { |
3050 | u64 start, end; | |
3051 | ||
3052 | if (truncated) | |
3053 | start = ordered_extent->file_offset + logical_len; | |
3054 | else | |
3055 | start = ordered_extent->file_offset; | |
3056 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3057 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3058 | ||
3059 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3060 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3061 | |
0bec9ef5 JB |
3062 | /* |
3063 | * If the ordered extent had an IOERR or something else went | |
3064 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3065 | * back to the allocator. We only free the extent in the |
3066 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3067 | */ |
77cef2ec JB |
3068 | if ((ret || !logical_len) && |
3069 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 | 3070 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3071 | btrfs_free_reserved_extent(fs_info, |
3072 | ordered_extent->start, | |
e570fd27 | 3073 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3074 | } |
3075 | ||
3076 | ||
5fd02043 | 3077 | /* |
8bad3c02 LB |
3078 | * This needs to be done to make sure anybody waiting knows we are done |
3079 | * updating everything for this ordered extent. | |
5fd02043 JB |
3080 | */ |
3081 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3082 | ||
38c227d8 | 3083 | /* for snapshot-aware defrag */ |
6f519564 LB |
3084 | if (new) { |
3085 | if (ret) { | |
3086 | free_sa_defrag_extent(new); | |
0b246afa | 3087 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3088 | } else { |
3089 | relink_file_extents(new); | |
3090 | } | |
3091 | } | |
38c227d8 | 3092 | |
e6dcd2dc CM |
3093 | /* once for us */ |
3094 | btrfs_put_ordered_extent(ordered_extent); | |
3095 | /* once for the tree */ | |
3096 | btrfs_put_ordered_extent(ordered_extent); | |
3097 | ||
5fd02043 JB |
3098 | return ret; |
3099 | } | |
3100 | ||
3101 | static void finish_ordered_fn(struct btrfs_work *work) | |
3102 | { | |
3103 | struct btrfs_ordered_extent *ordered_extent; | |
3104 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3105 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3106 | } |
3107 | ||
c3988d63 | 3108 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3109 | struct extent_state *state, int uptodate) |
3110 | { | |
5fd02043 | 3111 | struct inode *inode = page->mapping->host; |
0b246afa | 3112 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3113 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3114 | struct btrfs_workqueue *wq; |
3115 | btrfs_work_func_t func; | |
5fd02043 | 3116 | |
1abe9b8a | 3117 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3118 | ||
8b62b72b | 3119 | ClearPagePrivate2(page); |
5fd02043 JB |
3120 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3121 | end - start + 1, uptodate)) | |
c3988d63 | 3122 | return; |
5fd02043 | 3123 | |
70ddc553 | 3124 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
0b246afa | 3125 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3126 | func = btrfs_freespace_write_helper; |
3127 | } else { | |
0b246afa | 3128 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3129 | func = btrfs_endio_write_helper; |
3130 | } | |
5fd02043 | 3131 | |
9e0af237 LB |
3132 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3133 | NULL); | |
3134 | btrfs_queue_work(wq, &ordered_extent->work); | |
211f90e6 CM |
3135 | } |
3136 | ||
dc380aea MX |
3137 | static int __readpage_endio_check(struct inode *inode, |
3138 | struct btrfs_io_bio *io_bio, | |
3139 | int icsum, struct page *page, | |
3140 | int pgoff, u64 start, size_t len) | |
3141 | { | |
3142 | char *kaddr; | |
3143 | u32 csum_expected; | |
3144 | u32 csum = ~(u32)0; | |
dc380aea MX |
3145 | |
3146 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3147 | ||
3148 | kaddr = kmap_atomic(page); | |
3149 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3150 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3151 | if (csum != csum_expected) |
3152 | goto zeroit; | |
3153 | ||
3154 | kunmap_atomic(kaddr); | |
3155 | return 0; | |
3156 | zeroit: | |
0970a22e | 3157 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
6f6b643e | 3158 | io_bio->mirror_num); |
dc380aea MX |
3159 | memset(kaddr + pgoff, 1, len); |
3160 | flush_dcache_page(page); | |
3161 | kunmap_atomic(kaddr); | |
dc380aea MX |
3162 | return -EIO; |
3163 | } | |
3164 | ||
d352ac68 CM |
3165 | /* |
3166 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3167 | * if there's a match, we allow the bio to finish. If not, the code in |
3168 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3169 | */ |
facc8a22 MX |
3170 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3171 | u64 phy_offset, struct page *page, | |
3172 | u64 start, u64 end, int mirror) | |
07157aac | 3173 | { |
4eee4fa4 | 3174 | size_t offset = start - page_offset(page); |
07157aac | 3175 | struct inode *inode = page->mapping->host; |
d1310b2e | 3176 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3177 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3178 | |
d20f7043 CM |
3179 | if (PageChecked(page)) { |
3180 | ClearPageChecked(page); | |
dc380aea | 3181 | return 0; |
d20f7043 | 3182 | } |
6cbff00f CH |
3183 | |
3184 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3185 | return 0; |
17d217fe YZ |
3186 | |
3187 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3188 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3189 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3190 | return 0; |
17d217fe | 3191 | } |
d20f7043 | 3192 | |
facc8a22 | 3193 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3194 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3195 | start, (size_t)(end - start + 1)); | |
07157aac | 3196 | } |
b888db2b | 3197 | |
24bbcf04 YZ |
3198 | void btrfs_add_delayed_iput(struct inode *inode) |
3199 | { | |
0b246afa | 3200 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3201 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3202 | |
3203 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3204 | return; | |
3205 | ||
24bbcf04 | 3206 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3207 | if (binode->delayed_iput_count == 0) { |
3208 | ASSERT(list_empty(&binode->delayed_iput)); | |
3209 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3210 | } else { | |
3211 | binode->delayed_iput_count++; | |
3212 | } | |
24bbcf04 YZ |
3213 | spin_unlock(&fs_info->delayed_iput_lock); |
3214 | } | |
3215 | ||
2ff7e61e | 3216 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3217 | { |
24bbcf04 | 3218 | |
24bbcf04 | 3219 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3220 | while (!list_empty(&fs_info->delayed_iputs)) { |
3221 | struct btrfs_inode *inode; | |
3222 | ||
3223 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3224 | struct btrfs_inode, delayed_iput); | |
3225 | if (inode->delayed_iput_count) { | |
3226 | inode->delayed_iput_count--; | |
3227 | list_move_tail(&inode->delayed_iput, | |
3228 | &fs_info->delayed_iputs); | |
3229 | } else { | |
3230 | list_del_init(&inode->delayed_iput); | |
3231 | } | |
3232 | spin_unlock(&fs_info->delayed_iput_lock); | |
3233 | iput(&inode->vfs_inode); | |
3234 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3235 | } |
8089fe62 | 3236 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3237 | } |
3238 | ||
d68fc57b | 3239 | /* |
42b2aa86 | 3240 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3241 | * files in the subvolume, it removes orphan item and frees block_rsv |
3242 | * structure. | |
3243 | */ | |
3244 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3245 | struct btrfs_root *root) | |
3246 | { | |
0b246afa | 3247 | struct btrfs_fs_info *fs_info = root->fs_info; |
90290e19 | 3248 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3249 | int ret; |
3250 | ||
8a35d95f | 3251 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3252 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3253 | return; | |
3254 | ||
90290e19 | 3255 | spin_lock(&root->orphan_lock); |
8a35d95f | 3256 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3257 | spin_unlock(&root->orphan_lock); |
3258 | return; | |
3259 | } | |
3260 | ||
3261 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3262 | spin_unlock(&root->orphan_lock); | |
3263 | return; | |
3264 | } | |
3265 | ||
3266 | block_rsv = root->orphan_block_rsv; | |
3267 | root->orphan_block_rsv = NULL; | |
3268 | spin_unlock(&root->orphan_lock); | |
3269 | ||
27cdeb70 | 3270 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b | 3271 | btrfs_root_refs(&root->root_item) > 0) { |
0b246afa | 3272 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3273 | root->root_key.objectid); |
4ef31a45 | 3274 | if (ret) |
66642832 | 3275 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3276 | else |
27cdeb70 MX |
3277 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3278 | &root->state); | |
d68fc57b YZ |
3279 | } |
3280 | ||
90290e19 JB |
3281 | if (block_rsv) { |
3282 | WARN_ON(block_rsv->size > 0); | |
2ff7e61e | 3283 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b YZ |
3284 | } |
3285 | } | |
3286 | ||
7b128766 JB |
3287 | /* |
3288 | * This creates an orphan entry for the given inode in case something goes | |
3289 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3290 | * |
3291 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3292 | * this function. | |
7b128766 | 3293 | */ |
73f2e545 NB |
3294 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
3295 | struct btrfs_inode *inode) | |
7b128766 | 3296 | { |
73f2e545 NB |
3297 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
3298 | struct btrfs_root *root = inode->root; | |
d68fc57b YZ |
3299 | struct btrfs_block_rsv *block_rsv = NULL; |
3300 | int reserve = 0; | |
3301 | int insert = 0; | |
3302 | int ret; | |
7b128766 | 3303 | |
d68fc57b | 3304 | if (!root->orphan_block_rsv) { |
2ff7e61e JM |
3305 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
3306 | BTRFS_BLOCK_RSV_TEMP); | |
b532402e TI |
3307 | if (!block_rsv) |
3308 | return -ENOMEM; | |
d68fc57b | 3309 | } |
7b128766 | 3310 | |
d68fc57b YZ |
3311 | spin_lock(&root->orphan_lock); |
3312 | if (!root->orphan_block_rsv) { | |
3313 | root->orphan_block_rsv = block_rsv; | |
3314 | } else if (block_rsv) { | |
2ff7e61e | 3315 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b | 3316 | block_rsv = NULL; |
7b128766 | 3317 | } |
7b128766 | 3318 | |
8a35d95f | 3319 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3320 | &inode->runtime_flags)) { |
d68fc57b YZ |
3321 | #if 0 |
3322 | /* | |
3323 | * For proper ENOSPC handling, we should do orphan | |
3324 | * cleanup when mounting. But this introduces backward | |
3325 | * compatibility issue. | |
3326 | */ | |
3327 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3328 | insert = 2; | |
3329 | else | |
3330 | insert = 1; | |
3331 | #endif | |
3332 | insert = 1; | |
321f0e70 | 3333 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3334 | } |
3335 | ||
72ac3c0d | 3336 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
73f2e545 | 3337 | &inode->runtime_flags)) |
d68fc57b | 3338 | reserve = 1; |
d68fc57b | 3339 | spin_unlock(&root->orphan_lock); |
7b128766 | 3340 | |
d68fc57b YZ |
3341 | /* grab metadata reservation from transaction handle */ |
3342 | if (reserve) { | |
3343 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3344 | ASSERT(!ret); |
3345 | if (ret) { | |
3346 | atomic_dec(&root->orphan_inodes); | |
3347 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3348 | &inode->runtime_flags); |
3b6571c1 JB |
3349 | if (insert) |
3350 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
73f2e545 | 3351 | &inode->runtime_flags); |
3b6571c1 JB |
3352 | return ret; |
3353 | } | |
d68fc57b | 3354 | } |
7b128766 | 3355 | |
d68fc57b YZ |
3356 | /* insert an orphan item to track this unlinked/truncated file */ |
3357 | if (insert >= 1) { | |
73f2e545 | 3358 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3359 | if (ret) { |
703c88e0 | 3360 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3361 | if (reserve) { |
3362 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3363 | &inode->runtime_flags); |
4ef31a45 JB |
3364 | btrfs_orphan_release_metadata(inode); |
3365 | } | |
3366 | if (ret != -EEXIST) { | |
e8e7cff6 | 3367 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3368 | &inode->runtime_flags); |
66642832 | 3369 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3370 | return ret; |
3371 | } | |
79787eaa JM |
3372 | } |
3373 | ret = 0; | |
d68fc57b YZ |
3374 | } |
3375 | ||
3376 | /* insert an orphan item to track subvolume contains orphan files */ | |
3377 | if (insert >= 2) { | |
0b246afa | 3378 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3379 | root->root_key.objectid); |
79787eaa | 3380 | if (ret && ret != -EEXIST) { |
66642832 | 3381 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3382 | return ret; |
3383 | } | |
d68fc57b YZ |
3384 | } |
3385 | return 0; | |
7b128766 JB |
3386 | } |
3387 | ||
3388 | /* | |
3389 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3390 | * item for this particular inode. | |
3391 | */ | |
48a3b636 | 3392 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3393 | struct btrfs_inode *inode) |
7b128766 | 3394 | { |
3d6ae7bb | 3395 | struct btrfs_root *root = inode->root; |
d68fc57b YZ |
3396 | int delete_item = 0; |
3397 | int release_rsv = 0; | |
7b128766 JB |
3398 | int ret = 0; |
3399 | ||
d68fc57b | 3400 | spin_lock(&root->orphan_lock); |
8a35d95f | 3401 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3d6ae7bb | 3402 | &inode->runtime_flags)) |
d68fc57b | 3403 | delete_item = 1; |
7b128766 | 3404 | |
72ac3c0d | 3405 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3d6ae7bb | 3406 | &inode->runtime_flags)) |
d68fc57b | 3407 | release_rsv = 1; |
d68fc57b | 3408 | spin_unlock(&root->orphan_lock); |
7b128766 | 3409 | |
703c88e0 | 3410 | if (delete_item) { |
8a35d95f | 3411 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3412 | if (trans) |
3413 | ret = btrfs_del_orphan_item(trans, root, | |
3d6ae7bb | 3414 | btrfs_ino(inode)); |
8a35d95f | 3415 | } |
7b128766 | 3416 | |
703c88e0 FDBM |
3417 | if (release_rsv) |
3418 | btrfs_orphan_release_metadata(inode); | |
3419 | ||
4ef31a45 | 3420 | return ret; |
7b128766 JB |
3421 | } |
3422 | ||
3423 | /* | |
3424 | * this cleans up any orphans that may be left on the list from the last use | |
3425 | * of this root. | |
3426 | */ | |
66b4ffd1 | 3427 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3428 | { |
0b246afa | 3429 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3430 | struct btrfs_path *path; |
3431 | struct extent_buffer *leaf; | |
7b128766 JB |
3432 | struct btrfs_key key, found_key; |
3433 | struct btrfs_trans_handle *trans; | |
3434 | struct inode *inode; | |
8f6d7f4f | 3435 | u64 last_objectid = 0; |
7b128766 JB |
3436 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3437 | ||
d68fc57b | 3438 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3439 | return 0; |
c71bf099 YZ |
3440 | |
3441 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3442 | if (!path) { |
3443 | ret = -ENOMEM; | |
3444 | goto out; | |
3445 | } | |
e4058b54 | 3446 | path->reada = READA_BACK; |
7b128766 JB |
3447 | |
3448 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3449 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3450 | key.offset = (u64)-1; |
3451 | ||
7b128766 JB |
3452 | while (1) { |
3453 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3454 | if (ret < 0) |
3455 | goto out; | |
7b128766 JB |
3456 | |
3457 | /* | |
3458 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3459 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3460 | * find the key and see if we have stuff that matches |
3461 | */ | |
3462 | if (ret > 0) { | |
66b4ffd1 | 3463 | ret = 0; |
7b128766 JB |
3464 | if (path->slots[0] == 0) |
3465 | break; | |
3466 | path->slots[0]--; | |
3467 | } | |
3468 | ||
3469 | /* pull out the item */ | |
3470 | leaf = path->nodes[0]; | |
7b128766 JB |
3471 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3472 | ||
3473 | /* make sure the item matches what we want */ | |
3474 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3475 | break; | |
962a298f | 3476 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3477 | break; |
3478 | ||
3479 | /* release the path since we're done with it */ | |
b3b4aa74 | 3480 | btrfs_release_path(path); |
7b128766 JB |
3481 | |
3482 | /* | |
3483 | * this is where we are basically btrfs_lookup, without the | |
3484 | * crossing root thing. we store the inode number in the | |
3485 | * offset of the orphan item. | |
3486 | */ | |
8f6d7f4f JB |
3487 | |
3488 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3489 | btrfs_err(fs_info, |
3490 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3491 | ret = -EINVAL; |
3492 | goto out; | |
3493 | } | |
3494 | ||
3495 | last_objectid = found_key.offset; | |
3496 | ||
5d4f98a2 YZ |
3497 | found_key.objectid = found_key.offset; |
3498 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3499 | found_key.offset = 0; | |
0b246afa | 3500 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3501 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3502 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3503 | goto out; |
7b128766 | 3504 | |
0b246afa | 3505 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3506 | struct btrfs_root *dead_root; |
3507 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3508 | int is_dead_root = 0; | |
3509 | ||
3510 | /* | |
3511 | * this is an orphan in the tree root. Currently these | |
3512 | * could come from 2 sources: | |
3513 | * a) a snapshot deletion in progress | |
3514 | * b) a free space cache inode | |
3515 | * We need to distinguish those two, as the snapshot | |
3516 | * orphan must not get deleted. | |
3517 | * find_dead_roots already ran before us, so if this | |
3518 | * is a snapshot deletion, we should find the root | |
3519 | * in the dead_roots list | |
3520 | */ | |
3521 | spin_lock(&fs_info->trans_lock); | |
3522 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3523 | root_list) { | |
3524 | if (dead_root->root_key.objectid == | |
3525 | found_key.objectid) { | |
3526 | is_dead_root = 1; | |
3527 | break; | |
3528 | } | |
3529 | } | |
3530 | spin_unlock(&fs_info->trans_lock); | |
3531 | if (is_dead_root) { | |
3532 | /* prevent this orphan from being found again */ | |
3533 | key.offset = found_key.objectid - 1; | |
3534 | continue; | |
3535 | } | |
3536 | } | |
7b128766 | 3537 | /* |
a8c9e576 JB |
3538 | * Inode is already gone but the orphan item is still there, |
3539 | * kill the orphan item. | |
7b128766 | 3540 | */ |
67710892 | 3541 | if (ret == -ENOENT) { |
a8c9e576 | 3542 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3543 | if (IS_ERR(trans)) { |
3544 | ret = PTR_ERR(trans); | |
3545 | goto out; | |
3546 | } | |
0b246afa JM |
3547 | btrfs_debug(fs_info, "auto deleting %Lu", |
3548 | found_key.objectid); | |
a8c9e576 JB |
3549 | ret = btrfs_del_orphan_item(trans, root, |
3550 | found_key.objectid); | |
3a45bb20 | 3551 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3552 | if (ret) |
3553 | goto out; | |
7b128766 JB |
3554 | continue; |
3555 | } | |
3556 | ||
a8c9e576 JB |
3557 | /* |
3558 | * add this inode to the orphan list so btrfs_orphan_del does | |
3559 | * the proper thing when we hit it | |
3560 | */ | |
8a35d95f JB |
3561 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3562 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3563 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3564 | |
7b128766 JB |
3565 | /* if we have links, this was a truncate, lets do that */ |
3566 | if (inode->i_nlink) { | |
fae7f21c | 3567 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3568 | iput(inode); |
3569 | continue; | |
3570 | } | |
7b128766 | 3571 | nr_truncate++; |
f3fe820c JB |
3572 | |
3573 | /* 1 for the orphan item deletion. */ | |
3574 | trans = btrfs_start_transaction(root, 1); | |
3575 | if (IS_ERR(trans)) { | |
c69b26b0 | 3576 | iput(inode); |
f3fe820c JB |
3577 | ret = PTR_ERR(trans); |
3578 | goto out; | |
3579 | } | |
73f2e545 | 3580 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 3581 | btrfs_end_transaction(trans); |
c69b26b0 JB |
3582 | if (ret) { |
3583 | iput(inode); | |
f3fe820c | 3584 | goto out; |
c69b26b0 | 3585 | } |
f3fe820c | 3586 | |
66b4ffd1 | 3587 | ret = btrfs_truncate(inode); |
4a7d0f68 | 3588 | if (ret) |
3d6ae7bb | 3589 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7b128766 JB |
3590 | } else { |
3591 | nr_unlink++; | |
3592 | } | |
3593 | ||
3594 | /* this will do delete_inode and everything for us */ | |
3595 | iput(inode); | |
66b4ffd1 JB |
3596 | if (ret) |
3597 | goto out; | |
7b128766 | 3598 | } |
3254c876 MX |
3599 | /* release the path since we're done with it */ |
3600 | btrfs_release_path(path); | |
3601 | ||
d68fc57b YZ |
3602 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3603 | ||
3604 | if (root->orphan_block_rsv) | |
2ff7e61e | 3605 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
d68fc57b YZ |
3606 | (u64)-1); |
3607 | ||
27cdeb70 MX |
3608 | if (root->orphan_block_rsv || |
3609 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3610 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3611 | if (!IS_ERR(trans)) |
3a45bb20 | 3612 | btrfs_end_transaction(trans); |
d68fc57b | 3613 | } |
7b128766 JB |
3614 | |
3615 | if (nr_unlink) | |
0b246afa | 3616 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3617 | if (nr_truncate) |
0b246afa | 3618 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3619 | |
3620 | out: | |
3621 | if (ret) | |
0b246afa | 3622 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3623 | btrfs_free_path(path); |
3624 | return ret; | |
7b128766 JB |
3625 | } |
3626 | ||
46a53cca CM |
3627 | /* |
3628 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3629 | * don't find any xattrs, we know there can't be any acls. | |
3630 | * | |
3631 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3632 | */ | |
3633 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3634 | int slot, u64 objectid, |
3635 | int *first_xattr_slot) | |
46a53cca CM |
3636 | { |
3637 | u32 nritems = btrfs_header_nritems(leaf); | |
3638 | struct btrfs_key found_key; | |
f23b5a59 JB |
3639 | static u64 xattr_access = 0; |
3640 | static u64 xattr_default = 0; | |
46a53cca CM |
3641 | int scanned = 0; |
3642 | ||
f23b5a59 | 3643 | if (!xattr_access) { |
97d79299 AG |
3644 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3645 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3646 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3647 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3648 | } |
3649 | ||
46a53cca | 3650 | slot++; |
63541927 | 3651 | *first_xattr_slot = -1; |
46a53cca CM |
3652 | while (slot < nritems) { |
3653 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3654 | ||
3655 | /* we found a different objectid, there must not be acls */ | |
3656 | if (found_key.objectid != objectid) | |
3657 | return 0; | |
3658 | ||
3659 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3660 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3661 | if (*first_xattr_slot == -1) |
3662 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3663 | if (found_key.offset == xattr_access || |
3664 | found_key.offset == xattr_default) | |
3665 | return 1; | |
3666 | } | |
46a53cca CM |
3667 | |
3668 | /* | |
3669 | * we found a key greater than an xattr key, there can't | |
3670 | * be any acls later on | |
3671 | */ | |
3672 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3673 | return 0; | |
3674 | ||
3675 | slot++; | |
3676 | scanned++; | |
3677 | ||
3678 | /* | |
3679 | * it goes inode, inode backrefs, xattrs, extents, | |
3680 | * so if there are a ton of hard links to an inode there can | |
3681 | * be a lot of backrefs. Don't waste time searching too hard, | |
3682 | * this is just an optimization | |
3683 | */ | |
3684 | if (scanned >= 8) | |
3685 | break; | |
3686 | } | |
3687 | /* we hit the end of the leaf before we found an xattr or | |
3688 | * something larger than an xattr. We have to assume the inode | |
3689 | * has acls | |
3690 | */ | |
63541927 FDBM |
3691 | if (*first_xattr_slot == -1) |
3692 | *first_xattr_slot = slot; | |
46a53cca CM |
3693 | return 1; |
3694 | } | |
3695 | ||
d352ac68 CM |
3696 | /* |
3697 | * read an inode from the btree into the in-memory inode | |
3698 | */ | |
67710892 | 3699 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 | 3700 | { |
0b246afa | 3701 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 3702 | struct btrfs_path *path; |
5f39d397 | 3703 | struct extent_buffer *leaf; |
39279cc3 CM |
3704 | struct btrfs_inode_item *inode_item; |
3705 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3706 | struct btrfs_key location; | |
67de1176 | 3707 | unsigned long ptr; |
46a53cca | 3708 | int maybe_acls; |
618e21d5 | 3709 | u32 rdev; |
39279cc3 | 3710 | int ret; |
2f7e33d4 | 3711 | bool filled = false; |
63541927 | 3712 | int first_xattr_slot; |
2f7e33d4 MX |
3713 | |
3714 | ret = btrfs_fill_inode(inode, &rdev); | |
3715 | if (!ret) | |
3716 | filled = true; | |
39279cc3 CM |
3717 | |
3718 | path = btrfs_alloc_path(); | |
67710892 FM |
3719 | if (!path) { |
3720 | ret = -ENOMEM; | |
1748f843 | 3721 | goto make_bad; |
67710892 | 3722 | } |
1748f843 | 3723 | |
39279cc3 | 3724 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3725 | |
39279cc3 | 3726 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3727 | if (ret) { |
3728 | if (ret > 0) | |
3729 | ret = -ENOENT; | |
39279cc3 | 3730 | goto make_bad; |
67710892 | 3731 | } |
39279cc3 | 3732 | |
5f39d397 | 3733 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3734 | |
3735 | if (filled) | |
67de1176 | 3736 | goto cache_index; |
2f7e33d4 | 3737 | |
5f39d397 CM |
3738 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3739 | struct btrfs_inode_item); | |
5f39d397 | 3740 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3741 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3742 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3743 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3744 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3745 | |
a937b979 DS |
3746 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3747 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3748 | |
a937b979 DS |
3749 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3750 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3751 | |
a937b979 DS |
3752 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3753 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3754 | |
9cc97d64 | 3755 | BTRFS_I(inode)->i_otime.tv_sec = |
3756 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3757 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3758 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3759 | |
a76a3cd4 | 3760 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3761 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3762 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3763 | ||
6e17d30b YD |
3764 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3765 | inode->i_generation = BTRFS_I(inode)->generation; | |
3766 | inode->i_rdev = 0; | |
3767 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3768 | ||
3769 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3770 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3771 | ||
3772 | cache_index: | |
5dc562c5 JB |
3773 | /* |
3774 | * If we were modified in the current generation and evicted from memory | |
3775 | * and then re-read we need to do a full sync since we don't have any | |
3776 | * idea about which extents were modified before we were evicted from | |
3777 | * cache. | |
6e17d30b YD |
3778 | * |
3779 | * This is required for both inode re-read from disk and delayed inode | |
3780 | * in delayed_nodes_tree. | |
5dc562c5 | 3781 | */ |
0b246afa | 3782 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3783 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3784 | &BTRFS_I(inode)->runtime_flags); | |
3785 | ||
bde6c242 FM |
3786 | /* |
3787 | * We don't persist the id of the transaction where an unlink operation | |
3788 | * against the inode was last made. So here we assume the inode might | |
3789 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3790 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3791 | * between the inode and its parent if the inode is fsync'ed and the log | |
3792 | * replayed. For example, in the scenario: | |
3793 | * | |
3794 | * touch mydir/foo | |
3795 | * ln mydir/foo mydir/bar | |
3796 | * sync | |
3797 | * unlink mydir/bar | |
3798 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3799 | * xfs_io -c fsync mydir/foo | |
3800 | * <power failure> | |
3801 | * mount fs, triggers fsync log replay | |
3802 | * | |
3803 | * We must make sure that when we fsync our inode foo we also log its | |
3804 | * parent inode, otherwise after log replay the parent still has the | |
3805 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3806 | * and doesn't have an inode ref with the name "bar" anymore. | |
3807 | * | |
3808 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3809 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3810 | * transaction commits on fsync if our inode is a directory, or if our |
3811 | * inode is not a directory, logging its parent unnecessarily. | |
3812 | */ | |
3813 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3814 | ||
67de1176 MX |
3815 | path->slots[0]++; |
3816 | if (inode->i_nlink != 1 || | |
3817 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3818 | goto cache_acl; | |
3819 | ||
3820 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3821 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3822 | goto cache_acl; |
3823 | ||
3824 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3825 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3826 | struct btrfs_inode_ref *ref; | |
3827 | ||
3828 | ref = (struct btrfs_inode_ref *)ptr; | |
3829 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3830 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3831 | struct btrfs_inode_extref *extref; | |
3832 | ||
3833 | extref = (struct btrfs_inode_extref *)ptr; | |
3834 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3835 | extref); | |
3836 | } | |
2f7e33d4 | 3837 | cache_acl: |
46a53cca CM |
3838 | /* |
3839 | * try to precache a NULL acl entry for files that don't have | |
3840 | * any xattrs or acls | |
3841 | */ | |
33345d01 | 3842 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3843 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3844 | if (first_xattr_slot != -1) { |
3845 | path->slots[0] = first_xattr_slot; | |
3846 | ret = btrfs_load_inode_props(inode, path); | |
3847 | if (ret) | |
0b246afa | 3848 | btrfs_err(fs_info, |
351fd353 | 3849 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3850 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3851 | root->root_key.objectid, ret); |
3852 | } | |
3853 | btrfs_free_path(path); | |
3854 | ||
72c04902 AV |
3855 | if (!maybe_acls) |
3856 | cache_no_acl(inode); | |
46a53cca | 3857 | |
39279cc3 | 3858 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3859 | case S_IFREG: |
3860 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3861 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3862 | inode->i_fop = &btrfs_file_operations; |
3863 | inode->i_op = &btrfs_file_inode_operations; | |
3864 | break; | |
3865 | case S_IFDIR: | |
3866 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3867 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3868 | break; |
3869 | case S_IFLNK: | |
3870 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3871 | inode_nohighmem(inode); |
39279cc3 CM |
3872 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3873 | break; | |
618e21d5 | 3874 | default: |
0279b4cd | 3875 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3876 | init_special_inode(inode, inode->i_mode, rdev); |
3877 | break; | |
39279cc3 | 3878 | } |
6cbff00f CH |
3879 | |
3880 | btrfs_update_iflags(inode); | |
67710892 | 3881 | return 0; |
39279cc3 CM |
3882 | |
3883 | make_bad: | |
39279cc3 | 3884 | btrfs_free_path(path); |
39279cc3 | 3885 | make_bad_inode(inode); |
67710892 | 3886 | return ret; |
39279cc3 CM |
3887 | } |
3888 | ||
d352ac68 CM |
3889 | /* |
3890 | * given a leaf and an inode, copy the inode fields into the leaf | |
3891 | */ | |
e02119d5 CM |
3892 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3893 | struct extent_buffer *leaf, | |
5f39d397 | 3894 | struct btrfs_inode_item *item, |
39279cc3 CM |
3895 | struct inode *inode) |
3896 | { | |
51fab693 LB |
3897 | struct btrfs_map_token token; |
3898 | ||
3899 | btrfs_init_map_token(&token); | |
5f39d397 | 3900 | |
51fab693 LB |
3901 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3902 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3903 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3904 | &token); | |
3905 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3906 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3907 | |
a937b979 | 3908 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3909 | inode->i_atime.tv_sec, &token); |
a937b979 | 3910 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3911 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3912 | |
a937b979 | 3913 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3914 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3915 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3916 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3917 | |
a937b979 | 3918 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3919 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3920 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3921 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3922 | |
9cc97d64 | 3923 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3924 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3925 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3926 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3927 | ||
51fab693 LB |
3928 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3929 | &token); | |
3930 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3931 | &token); | |
3932 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3933 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3934 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3935 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3936 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3937 | } |
3938 | ||
d352ac68 CM |
3939 | /* |
3940 | * copy everything in the in-memory inode into the btree. | |
3941 | */ | |
2115133f | 3942 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3943 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3944 | { |
3945 | struct btrfs_inode_item *inode_item; | |
3946 | struct btrfs_path *path; | |
5f39d397 | 3947 | struct extent_buffer *leaf; |
39279cc3 CM |
3948 | int ret; |
3949 | ||
3950 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3951 | if (!path) |
3952 | return -ENOMEM; | |
3953 | ||
b9473439 | 3954 | path->leave_spinning = 1; |
16cdcec7 MX |
3955 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3956 | 1); | |
39279cc3 CM |
3957 | if (ret) { |
3958 | if (ret > 0) | |
3959 | ret = -ENOENT; | |
3960 | goto failed; | |
3961 | } | |
3962 | ||
5f39d397 CM |
3963 | leaf = path->nodes[0]; |
3964 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3965 | struct btrfs_inode_item); |
39279cc3 | 3966 | |
e02119d5 | 3967 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3968 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3969 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3970 | ret = 0; |
3971 | failed: | |
39279cc3 CM |
3972 | btrfs_free_path(path); |
3973 | return ret; | |
3974 | } | |
3975 | ||
2115133f CM |
3976 | /* |
3977 | * copy everything in the in-memory inode into the btree. | |
3978 | */ | |
3979 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3980 | struct btrfs_root *root, struct inode *inode) | |
3981 | { | |
0b246afa | 3982 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3983 | int ret; |
3984 | ||
3985 | /* | |
3986 | * If the inode is a free space inode, we can deadlock during commit | |
3987 | * if we put it into the delayed code. | |
3988 | * | |
3989 | * The data relocation inode should also be directly updated | |
3990 | * without delay | |
3991 | */ | |
70ddc553 | 3992 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3993 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3994 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3995 | btrfs_update_root_times(trans, root); |
3996 | ||
2115133f CM |
3997 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3998 | if (!ret) | |
3999 | btrfs_set_inode_last_trans(trans, inode); | |
4000 | return ret; | |
4001 | } | |
4002 | ||
4003 | return btrfs_update_inode_item(trans, root, inode); | |
4004 | } | |
4005 | ||
be6aef60 JB |
4006 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4007 | struct btrfs_root *root, | |
4008 | struct inode *inode) | |
2115133f CM |
4009 | { |
4010 | int ret; | |
4011 | ||
4012 | ret = btrfs_update_inode(trans, root, inode); | |
4013 | if (ret == -ENOSPC) | |
4014 | return btrfs_update_inode_item(trans, root, inode); | |
4015 | return ret; | |
4016 | } | |
4017 | ||
d352ac68 CM |
4018 | /* |
4019 | * unlink helper that gets used here in inode.c and in the tree logging | |
4020 | * recovery code. It remove a link in a directory with a given name, and | |
4021 | * also drops the back refs in the inode to the directory | |
4022 | */ | |
92986796 AV |
4023 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4024 | struct btrfs_root *root, | |
4ec5934e NB |
4025 | struct btrfs_inode *dir, |
4026 | struct btrfs_inode *inode, | |
92986796 | 4027 | const char *name, int name_len) |
39279cc3 | 4028 | { |
0b246afa | 4029 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4030 | struct btrfs_path *path; |
39279cc3 | 4031 | int ret = 0; |
5f39d397 | 4032 | struct extent_buffer *leaf; |
39279cc3 | 4033 | struct btrfs_dir_item *di; |
5f39d397 | 4034 | struct btrfs_key key; |
aec7477b | 4035 | u64 index; |
33345d01 LZ |
4036 | u64 ino = btrfs_ino(inode); |
4037 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4038 | |
4039 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4040 | if (!path) { |
4041 | ret = -ENOMEM; | |
554233a6 | 4042 | goto out; |
54aa1f4d CM |
4043 | } |
4044 | ||
b9473439 | 4045 | path->leave_spinning = 1; |
33345d01 | 4046 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4047 | name, name_len, -1); |
4048 | if (IS_ERR(di)) { | |
4049 | ret = PTR_ERR(di); | |
4050 | goto err; | |
4051 | } | |
4052 | if (!di) { | |
4053 | ret = -ENOENT; | |
4054 | goto err; | |
4055 | } | |
5f39d397 CM |
4056 | leaf = path->nodes[0]; |
4057 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4058 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4059 | if (ret) |
4060 | goto err; | |
b3b4aa74 | 4061 | btrfs_release_path(path); |
39279cc3 | 4062 | |
67de1176 MX |
4063 | /* |
4064 | * If we don't have dir index, we have to get it by looking up | |
4065 | * the inode ref, since we get the inode ref, remove it directly, | |
4066 | * it is unnecessary to do delayed deletion. | |
4067 | * | |
4068 | * But if we have dir index, needn't search inode ref to get it. | |
4069 | * Since the inode ref is close to the inode item, it is better | |
4070 | * that we delay to delete it, and just do this deletion when | |
4071 | * we update the inode item. | |
4072 | */ | |
4ec5934e | 4073 | if (inode->dir_index) { |
67de1176 MX |
4074 | ret = btrfs_delayed_delete_inode_ref(inode); |
4075 | if (!ret) { | |
4ec5934e | 4076 | index = inode->dir_index; |
67de1176 MX |
4077 | goto skip_backref; |
4078 | } | |
4079 | } | |
4080 | ||
33345d01 LZ |
4081 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4082 | dir_ino, &index); | |
aec7477b | 4083 | if (ret) { |
0b246afa | 4084 | btrfs_info(fs_info, |
c2cf52eb | 4085 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4086 | name_len, name, ino, dir_ino); |
66642832 | 4087 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4088 | goto err; |
4089 | } | |
67de1176 | 4090 | skip_backref: |
2ff7e61e | 4091 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
79787eaa | 4092 | if (ret) { |
66642832 | 4093 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4094 | goto err; |
79787eaa | 4095 | } |
39279cc3 | 4096 | |
4ec5934e NB |
4097 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4098 | dir_ino); | |
79787eaa | 4099 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4100 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4101 | goto err; |
4102 | } | |
e02119d5 | 4103 | |
4ec5934e NB |
4104 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4105 | index); | |
6418c961 CM |
4106 | if (ret == -ENOENT) |
4107 | ret = 0; | |
d4e3991b | 4108 | else if (ret) |
66642832 | 4109 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4110 | err: |
4111 | btrfs_free_path(path); | |
e02119d5 CM |
4112 | if (ret) |
4113 | goto out; | |
4114 | ||
6ef06d27 | 4115 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4116 | inode_inc_iversion(&inode->vfs_inode); |
4117 | inode_inc_iversion(&dir->vfs_inode); | |
4118 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4119 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4120 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4121 | out: |
39279cc3 CM |
4122 | return ret; |
4123 | } | |
4124 | ||
92986796 AV |
4125 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4126 | struct btrfs_root *root, | |
4ec5934e | 4127 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4128 | const char *name, int name_len) |
4129 | { | |
4130 | int ret; | |
4131 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4132 | if (!ret) { | |
4ec5934e NB |
4133 | drop_nlink(&inode->vfs_inode); |
4134 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4135 | } |
4136 | return ret; | |
4137 | } | |
39279cc3 | 4138 | |
a22285a6 YZ |
4139 | /* |
4140 | * helper to start transaction for unlink and rmdir. | |
4141 | * | |
d52be818 JB |
4142 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4143 | * if we cannot make our reservations the normal way try and see if there is | |
4144 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4145 | * allow the unlink to occur. | |
a22285a6 | 4146 | */ |
d52be818 | 4147 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4148 | { |
a22285a6 | 4149 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4150 | |
e70bea5f JB |
4151 | /* |
4152 | * 1 for the possible orphan item | |
4153 | * 1 for the dir item | |
4154 | * 1 for the dir index | |
4155 | * 1 for the inode ref | |
e70bea5f JB |
4156 | * 1 for the inode |
4157 | */ | |
8eab77ff | 4158 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4159 | } |
4160 | ||
4161 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4162 | { | |
4163 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4164 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4165 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4166 | int ret; |
a22285a6 | 4167 | |
d52be818 | 4168 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4169 | if (IS_ERR(trans)) |
4170 | return PTR_ERR(trans); | |
5f39d397 | 4171 | |
4ec5934e NB |
4172 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4173 | 0); | |
12fcfd22 | 4174 | |
4ec5934e NB |
4175 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4176 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4177 | dentry->d_name.len); | |
b532402e TI |
4178 | if (ret) |
4179 | goto out; | |
7b128766 | 4180 | |
a22285a6 | 4181 | if (inode->i_nlink == 0) { |
73f2e545 | 4182 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4183 | if (ret) |
4184 | goto out; | |
a22285a6 | 4185 | } |
7b128766 | 4186 | |
b532402e | 4187 | out: |
3a45bb20 | 4188 | btrfs_end_transaction(trans); |
2ff7e61e | 4189 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4190 | return ret; |
4191 | } | |
4192 | ||
4df27c4d YZ |
4193 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4194 | struct btrfs_root *root, | |
4195 | struct inode *dir, u64 objectid, | |
4196 | const char *name, int name_len) | |
4197 | { | |
0b246afa | 4198 | struct btrfs_fs_info *fs_info = root->fs_info; |
4df27c4d YZ |
4199 | struct btrfs_path *path; |
4200 | struct extent_buffer *leaf; | |
4201 | struct btrfs_dir_item *di; | |
4202 | struct btrfs_key key; | |
4203 | u64 index; | |
4204 | int ret; | |
4a0cc7ca | 4205 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4206 | |
4207 | path = btrfs_alloc_path(); | |
4208 | if (!path) | |
4209 | return -ENOMEM; | |
4210 | ||
33345d01 | 4211 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4212 | name, name_len, -1); |
79787eaa JM |
4213 | if (IS_ERR_OR_NULL(di)) { |
4214 | if (!di) | |
4215 | ret = -ENOENT; | |
4216 | else | |
4217 | ret = PTR_ERR(di); | |
4218 | goto out; | |
4219 | } | |
4df27c4d YZ |
4220 | |
4221 | leaf = path->nodes[0]; | |
4222 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4223 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4224 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4225 | if (ret) { |
66642832 | 4226 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4227 | goto out; |
4228 | } | |
b3b4aa74 | 4229 | btrfs_release_path(path); |
4df27c4d | 4230 | |
0b246afa JM |
4231 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
4232 | root->root_key.objectid, dir_ino, | |
4233 | &index, name, name_len); | |
4df27c4d | 4234 | if (ret < 0) { |
79787eaa | 4235 | if (ret != -ENOENT) { |
66642832 | 4236 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4237 | goto out; |
4238 | } | |
33345d01 | 4239 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4240 | name, name_len); |
79787eaa JM |
4241 | if (IS_ERR_OR_NULL(di)) { |
4242 | if (!di) | |
4243 | ret = -ENOENT; | |
4244 | else | |
4245 | ret = PTR_ERR(di); | |
66642832 | 4246 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4247 | goto out; |
4248 | } | |
4df27c4d YZ |
4249 | |
4250 | leaf = path->nodes[0]; | |
4251 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4252 | btrfs_release_path(path); |
4df27c4d YZ |
4253 | index = key.offset; |
4254 | } | |
945d8962 | 4255 | btrfs_release_path(path); |
4df27c4d | 4256 | |
e67bbbb9 | 4257 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4258 | if (ret) { |
66642832 | 4259 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4260 | goto out; |
4261 | } | |
4df27c4d | 4262 | |
6ef06d27 | 4263 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4264 | inode_inc_iversion(dir); |
c2050a45 | 4265 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4266 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4267 | if (ret) |
66642832 | 4268 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4269 | out: |
71d7aed0 | 4270 | btrfs_free_path(path); |
79787eaa | 4271 | return ret; |
4df27c4d YZ |
4272 | } |
4273 | ||
39279cc3 CM |
4274 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4275 | { | |
2b0143b5 | 4276 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4277 | int err = 0; |
39279cc3 | 4278 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4279 | struct btrfs_trans_handle *trans; |
44f714da | 4280 | u64 last_unlink_trans; |
39279cc3 | 4281 | |
b3ae244e | 4282 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4283 | return -ENOTEMPTY; |
4a0cc7ca | 4284 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
b3ae244e | 4285 | return -EPERM; |
134d4512 | 4286 | |
d52be818 | 4287 | trans = __unlink_start_trans(dir); |
a22285a6 | 4288 | if (IS_ERR(trans)) |
5df6a9f6 | 4289 | return PTR_ERR(trans); |
5df6a9f6 | 4290 | |
4a0cc7ca | 4291 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4292 | err = btrfs_unlink_subvol(trans, root, dir, |
4293 | BTRFS_I(inode)->location.objectid, | |
4294 | dentry->d_name.name, | |
4295 | dentry->d_name.len); | |
4296 | goto out; | |
4297 | } | |
4298 | ||
73f2e545 | 4299 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4300 | if (err) |
4df27c4d | 4301 | goto out; |
7b128766 | 4302 | |
44f714da FM |
4303 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4304 | ||
39279cc3 | 4305 | /* now the directory is empty */ |
4ec5934e NB |
4306 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4307 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4308 | dentry->d_name.len); | |
44f714da | 4309 | if (!err) { |
6ef06d27 | 4310 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4311 | /* |
4312 | * Propagate the last_unlink_trans value of the deleted dir to | |
4313 | * its parent directory. This is to prevent an unrecoverable | |
4314 | * log tree in the case we do something like this: | |
4315 | * 1) create dir foo | |
4316 | * 2) create snapshot under dir foo | |
4317 | * 3) delete the snapshot | |
4318 | * 4) rmdir foo | |
4319 | * 5) mkdir foo | |
4320 | * 6) fsync foo or some file inside foo | |
4321 | */ | |
4322 | if (last_unlink_trans >= trans->transid) | |
4323 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4324 | } | |
4df27c4d | 4325 | out: |
3a45bb20 | 4326 | btrfs_end_transaction(trans); |
2ff7e61e | 4327 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4328 | |
39279cc3 CM |
4329 | return err; |
4330 | } | |
4331 | ||
28f75a0e CM |
4332 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4333 | struct btrfs_root *root, | |
4334 | u64 bytes_deleted) | |
4335 | { | |
0b246afa | 4336 | struct btrfs_fs_info *fs_info = root->fs_info; |
28f75a0e CM |
4337 | int ret; |
4338 | ||
dc95f7bf JB |
4339 | /* |
4340 | * This is only used to apply pressure to the enospc system, we don't | |
4341 | * intend to use this reservation at all. | |
4342 | */ | |
2ff7e61e | 4343 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
0b246afa JM |
4344 | bytes_deleted *= fs_info->nodesize; |
4345 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
28f75a0e | 4346 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
dc95f7bf | 4347 | if (!ret) { |
0b246afa | 4348 | trace_btrfs_space_reservation(fs_info, "transaction", |
dc95f7bf JB |
4349 | trans->transid, |
4350 | bytes_deleted, 1); | |
28f75a0e | 4351 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4352 | } |
28f75a0e CM |
4353 | return ret; |
4354 | ||
4355 | } | |
4356 | ||
0305cd5f FM |
4357 | static int truncate_inline_extent(struct inode *inode, |
4358 | struct btrfs_path *path, | |
4359 | struct btrfs_key *found_key, | |
4360 | const u64 item_end, | |
4361 | const u64 new_size) | |
4362 | { | |
4363 | struct extent_buffer *leaf = path->nodes[0]; | |
4364 | int slot = path->slots[0]; | |
4365 | struct btrfs_file_extent_item *fi; | |
4366 | u32 size = (u32)(new_size - found_key->offset); | |
4367 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4368 | ||
4369 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4370 | ||
4371 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4372 | loff_t offset = new_size; | |
09cbfeaf | 4373 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4374 | |
4375 | /* | |
4376 | * Zero out the remaining of the last page of our inline extent, | |
4377 | * instead of directly truncating our inline extent here - that | |
4378 | * would be much more complex (decompressing all the data, then | |
4379 | * compressing the truncated data, which might be bigger than | |
4380 | * the size of the inline extent, resize the extent, etc). | |
4381 | * We release the path because to get the page we might need to | |
4382 | * read the extent item from disk (data not in the page cache). | |
4383 | */ | |
4384 | btrfs_release_path(path); | |
9703fefe CR |
4385 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4386 | 0); | |
0305cd5f FM |
4387 | } |
4388 | ||
4389 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4390 | size = btrfs_file_extent_calc_inline_size(size); | |
2ff7e61e | 4391 | btrfs_truncate_item(root->fs_info, path, size, 1); |
0305cd5f FM |
4392 | |
4393 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4394 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4395 | ||
4396 | return 0; | |
4397 | } | |
4398 | ||
39279cc3 CM |
4399 | /* |
4400 | * this can truncate away extent items, csum items and directory items. | |
4401 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4402 | * any higher than new_size |
39279cc3 CM |
4403 | * |
4404 | * csum items that cross the new i_size are truncated to the new size | |
4405 | * as well. | |
7b128766 JB |
4406 | * |
4407 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4408 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4409 | */ |
8082510e YZ |
4410 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4411 | struct btrfs_root *root, | |
4412 | struct inode *inode, | |
4413 | u64 new_size, u32 min_type) | |
39279cc3 | 4414 | { |
0b246afa | 4415 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4416 | struct btrfs_path *path; |
5f39d397 | 4417 | struct extent_buffer *leaf; |
39279cc3 | 4418 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4419 | struct btrfs_key key; |
4420 | struct btrfs_key found_key; | |
39279cc3 | 4421 | u64 extent_start = 0; |
db94535d | 4422 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4423 | u64 extent_offset = 0; |
39279cc3 | 4424 | u64 item_end = 0; |
c1aa4575 | 4425 | u64 last_size = new_size; |
8082510e | 4426 | u32 found_type = (u8)-1; |
39279cc3 CM |
4427 | int found_extent; |
4428 | int del_item; | |
85e21bac CM |
4429 | int pending_del_nr = 0; |
4430 | int pending_del_slot = 0; | |
179e29e4 | 4431 | int extent_type = -1; |
8082510e YZ |
4432 | int ret; |
4433 | int err = 0; | |
4a0cc7ca | 4434 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4435 | u64 bytes_deleted = 0; |
1262133b JB |
4436 | bool be_nice = 0; |
4437 | bool should_throttle = 0; | |
28f75a0e | 4438 | bool should_end = 0; |
8082510e YZ |
4439 | |
4440 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4441 | |
28ed1345 CM |
4442 | /* |
4443 | * for non-free space inodes and ref cows, we want to back off from | |
4444 | * time to time | |
4445 | */ | |
70ddc553 | 4446 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 CM |
4447 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
4448 | be_nice = 1; | |
4449 | ||
0eb0e19c MF |
4450 | path = btrfs_alloc_path(); |
4451 | if (!path) | |
4452 | return -ENOMEM; | |
e4058b54 | 4453 | path->reada = READA_BACK; |
0eb0e19c | 4454 | |
5dc562c5 JB |
4455 | /* |
4456 | * We want to drop from the next block forward in case this new size is | |
4457 | * not block aligned since we will be keeping the last block of the | |
4458 | * extent just the way it is. | |
4459 | */ | |
27cdeb70 | 4460 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4461 | root == fs_info->tree_root) |
dcdbc059 | 4462 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4463 | fs_info->sectorsize), |
da17066c | 4464 | (u64)-1, 0); |
8082510e | 4465 | |
16cdcec7 MX |
4466 | /* |
4467 | * This function is also used to drop the items in the log tree before | |
4468 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4469 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4470 | * items. | |
4471 | */ | |
4472 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4473 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4474 | |
33345d01 | 4475 | key.objectid = ino; |
39279cc3 | 4476 | key.offset = (u64)-1; |
5f39d397 CM |
4477 | key.type = (u8)-1; |
4478 | ||
85e21bac | 4479 | search_again: |
28ed1345 CM |
4480 | /* |
4481 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4482 | * up a huge file in a single leaf. Most of the time that | |
4483 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4484 | */ | |
ee22184b | 4485 | if (be_nice && bytes_deleted > SZ_32M) { |
3a45bb20 | 4486 | if (btrfs_should_end_transaction(trans)) { |
28ed1345 CM |
4487 | err = -EAGAIN; |
4488 | goto error; | |
4489 | } | |
4490 | } | |
4491 | ||
4492 | ||
b9473439 | 4493 | path->leave_spinning = 1; |
85e21bac | 4494 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4495 | if (ret < 0) { |
4496 | err = ret; | |
4497 | goto out; | |
4498 | } | |
d397712b | 4499 | |
85e21bac | 4500 | if (ret > 0) { |
e02119d5 CM |
4501 | /* there are no items in the tree for us to truncate, we're |
4502 | * done | |
4503 | */ | |
8082510e YZ |
4504 | if (path->slots[0] == 0) |
4505 | goto out; | |
85e21bac CM |
4506 | path->slots[0]--; |
4507 | } | |
4508 | ||
d397712b | 4509 | while (1) { |
39279cc3 | 4510 | fi = NULL; |
5f39d397 CM |
4511 | leaf = path->nodes[0]; |
4512 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4513 | found_type = found_key.type; |
39279cc3 | 4514 | |
33345d01 | 4515 | if (found_key.objectid != ino) |
39279cc3 | 4516 | break; |
5f39d397 | 4517 | |
85e21bac | 4518 | if (found_type < min_type) |
39279cc3 CM |
4519 | break; |
4520 | ||
5f39d397 | 4521 | item_end = found_key.offset; |
39279cc3 | 4522 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4523 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4524 | struct btrfs_file_extent_item); |
179e29e4 CM |
4525 | extent_type = btrfs_file_extent_type(leaf, fi); |
4526 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4527 | item_end += |
db94535d | 4528 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4529 | |
4530 | trace_btrfs_truncate_show_fi_regular( | |
4531 | BTRFS_I(inode), leaf, fi, | |
4532 | found_key.offset); | |
179e29e4 | 4533 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4534 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4535 | path->slots[0], fi); |
09ed2f16 LB |
4536 | |
4537 | trace_btrfs_truncate_show_fi_inline( | |
4538 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4539 | found_key.offset); | |
39279cc3 | 4540 | } |
008630c1 | 4541 | item_end--; |
39279cc3 | 4542 | } |
8082510e YZ |
4543 | if (found_type > min_type) { |
4544 | del_item = 1; | |
4545 | } else { | |
76b42abb | 4546 | if (item_end < new_size) |
b888db2b | 4547 | break; |
8082510e YZ |
4548 | if (found_key.offset >= new_size) |
4549 | del_item = 1; | |
4550 | else | |
4551 | del_item = 0; | |
39279cc3 | 4552 | } |
39279cc3 | 4553 | found_extent = 0; |
39279cc3 | 4554 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4555 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4556 | goto delete; | |
4557 | ||
7f4f6e0a JB |
4558 | if (del_item) |
4559 | last_size = found_key.offset; | |
4560 | else | |
4561 | last_size = new_size; | |
4562 | ||
179e29e4 | 4563 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4564 | u64 num_dec; |
db94535d | 4565 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4566 | if (!del_item) { |
db94535d CM |
4567 | u64 orig_num_bytes = |
4568 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4569 | extent_num_bytes = ALIGN(new_size - |
4570 | found_key.offset, | |
0b246afa | 4571 | fs_info->sectorsize); |
db94535d CM |
4572 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4573 | extent_num_bytes); | |
4574 | num_dec = (orig_num_bytes - | |
9069218d | 4575 | extent_num_bytes); |
27cdeb70 MX |
4576 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4577 | &root->state) && | |
4578 | extent_start != 0) | |
a76a3cd4 | 4579 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4580 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4581 | } else { |
db94535d CM |
4582 | extent_num_bytes = |
4583 | btrfs_file_extent_disk_num_bytes(leaf, | |
4584 | fi); | |
5d4f98a2 YZ |
4585 | extent_offset = found_key.offset - |
4586 | btrfs_file_extent_offset(leaf, fi); | |
4587 | ||
39279cc3 | 4588 | /* FIXME blocksize != 4096 */ |
9069218d | 4589 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4590 | if (extent_start != 0) { |
4591 | found_extent = 1; | |
27cdeb70 MX |
4592 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4593 | &root->state)) | |
a76a3cd4 | 4594 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4595 | } |
39279cc3 | 4596 | } |
9069218d | 4597 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4598 | /* |
4599 | * we can't truncate inline items that have had | |
4600 | * special encodings | |
4601 | */ | |
4602 | if (!del_item && | |
c8b97818 CM |
4603 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4604 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4605 | |
4606 | /* | |
0305cd5f FM |
4607 | * Need to release path in order to truncate a |
4608 | * compressed extent. So delete any accumulated | |
4609 | * extent items so far. | |
514ac8ad | 4610 | */ |
0305cd5f FM |
4611 | if (btrfs_file_extent_compression(leaf, fi) != |
4612 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4613 | err = btrfs_del_items(trans, root, path, | |
4614 | pending_del_slot, | |
4615 | pending_del_nr); | |
4616 | if (err) { | |
4617 | btrfs_abort_transaction(trans, | |
0305cd5f FM |
4618 | err); |
4619 | goto error; | |
4620 | } | |
4621 | pending_del_nr = 0; | |
4622 | } | |
4623 | ||
4624 | err = truncate_inline_extent(inode, path, | |
4625 | &found_key, | |
4626 | item_end, | |
4627 | new_size); | |
4628 | if (err) { | |
66642832 | 4629 | btrfs_abort_transaction(trans, err); |
0305cd5f FM |
4630 | goto error; |
4631 | } | |
27cdeb70 MX |
4632 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4633 | &root->state)) { | |
0305cd5f | 4634 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4635 | } |
39279cc3 | 4636 | } |
179e29e4 | 4637 | delete: |
39279cc3 | 4638 | if (del_item) { |
85e21bac CM |
4639 | if (!pending_del_nr) { |
4640 | /* no pending yet, add ourselves */ | |
4641 | pending_del_slot = path->slots[0]; | |
4642 | pending_del_nr = 1; | |
4643 | } else if (pending_del_nr && | |
4644 | path->slots[0] + 1 == pending_del_slot) { | |
4645 | /* hop on the pending chunk */ | |
4646 | pending_del_nr++; | |
4647 | pending_del_slot = path->slots[0]; | |
4648 | } else { | |
d397712b | 4649 | BUG(); |
85e21bac | 4650 | } |
39279cc3 CM |
4651 | } else { |
4652 | break; | |
4653 | } | |
28f75a0e CM |
4654 | should_throttle = 0; |
4655 | ||
27cdeb70 MX |
4656 | if (found_extent && |
4657 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4658 | root == fs_info->tree_root)) { |
b9473439 | 4659 | btrfs_set_path_blocking(path); |
28ed1345 | 4660 | bytes_deleted += extent_num_bytes; |
2ff7e61e | 4661 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
5d4f98a2 YZ |
4662 | extent_num_bytes, 0, |
4663 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4664 | ino, extent_offset); |
39279cc3 | 4665 | BUG_ON(ret); |
2ff7e61e JM |
4666 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
4667 | btrfs_async_run_delayed_refs(fs_info, | |
dd4b857a WX |
4668 | trans->delayed_ref_updates * 2, |
4669 | trans->transid, 0); | |
28f75a0e CM |
4670 | if (be_nice) { |
4671 | if (truncate_space_check(trans, root, | |
4672 | extent_num_bytes)) { | |
4673 | should_end = 1; | |
4674 | } | |
4675 | if (btrfs_should_throttle_delayed_refs(trans, | |
2ff7e61e | 4676 | fs_info)) |
28f75a0e | 4677 | should_throttle = 1; |
28f75a0e | 4678 | } |
39279cc3 | 4679 | } |
85e21bac | 4680 | |
8082510e YZ |
4681 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4682 | break; | |
4683 | ||
4684 | if (path->slots[0] == 0 || | |
1262133b | 4685 | path->slots[0] != pending_del_slot || |
28f75a0e | 4686 | should_throttle || should_end) { |
8082510e YZ |
4687 | if (pending_del_nr) { |
4688 | ret = btrfs_del_items(trans, root, path, | |
4689 | pending_del_slot, | |
4690 | pending_del_nr); | |
79787eaa | 4691 | if (ret) { |
66642832 | 4692 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4693 | goto error; |
4694 | } | |
8082510e YZ |
4695 | pending_del_nr = 0; |
4696 | } | |
b3b4aa74 | 4697 | btrfs_release_path(path); |
28f75a0e | 4698 | if (should_throttle) { |
1262133b JB |
4699 | unsigned long updates = trans->delayed_ref_updates; |
4700 | if (updates) { | |
4701 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4702 | ret = btrfs_run_delayed_refs(trans, |
4703 | fs_info, | |
4704 | updates * 2); | |
1262133b JB |
4705 | if (ret && !err) |
4706 | err = ret; | |
4707 | } | |
4708 | } | |
28f75a0e CM |
4709 | /* |
4710 | * if we failed to refill our space rsv, bail out | |
4711 | * and let the transaction restart | |
4712 | */ | |
4713 | if (should_end) { | |
4714 | err = -EAGAIN; | |
4715 | goto error; | |
4716 | } | |
85e21bac | 4717 | goto search_again; |
8082510e YZ |
4718 | } else { |
4719 | path->slots[0]--; | |
85e21bac | 4720 | } |
39279cc3 | 4721 | } |
8082510e | 4722 | out: |
85e21bac CM |
4723 | if (pending_del_nr) { |
4724 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4725 | pending_del_nr); | |
79787eaa | 4726 | if (ret) |
66642832 | 4727 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4728 | } |
79787eaa | 4729 | error: |
76b42abb FM |
4730 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4731 | ASSERT(last_size >= new_size); | |
4732 | if (!err && last_size > new_size) | |
4733 | last_size = new_size; | |
7f4f6e0a | 4734 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4735 | } |
28ed1345 | 4736 | |
39279cc3 | 4737 | btrfs_free_path(path); |
28ed1345 | 4738 | |
ee22184b | 4739 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4740 | unsigned long updates = trans->delayed_ref_updates; |
4741 | if (updates) { | |
4742 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4743 | ret = btrfs_run_delayed_refs(trans, fs_info, |
4744 | updates * 2); | |
28ed1345 CM |
4745 | if (ret && !err) |
4746 | err = ret; | |
4747 | } | |
4748 | } | |
8082510e | 4749 | return err; |
39279cc3 CM |
4750 | } |
4751 | ||
4752 | /* | |
9703fefe | 4753 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4754 | * @inode - inode that we're zeroing |
4755 | * @from - the offset to start zeroing | |
4756 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4757 | * offset | |
4758 | * @front - zero up to the offset instead of from the offset on | |
4759 | * | |
9703fefe | 4760 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4761 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4762 | */ |
9703fefe | 4763 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4764 | int front) |
39279cc3 | 4765 | { |
0b246afa | 4766 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4767 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4768 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4769 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4770 | struct extent_state *cached_state = NULL; |
364ecf36 | 4771 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4772 | char *kaddr; |
0b246afa | 4773 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4774 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4775 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4776 | struct page *page; |
3b16a4e3 | 4777 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4778 | int ret = 0; |
9703fefe CR |
4779 | u64 block_start; |
4780 | u64 block_end; | |
39279cc3 | 4781 | |
2aaa6655 JB |
4782 | if ((offset & (blocksize - 1)) == 0 && |
4783 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4784 | goto out; |
9703fefe | 4785 | |
364ecf36 | 4786 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
9703fefe | 4787 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4788 | if (ret) |
4789 | goto out; | |
39279cc3 | 4790 | |
211c17f5 | 4791 | again: |
3b16a4e3 | 4792 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4793 | if (!page) { |
bc42bda2 | 4794 | btrfs_delalloc_release_space(inode, data_reserved, |
9703fefe CR |
4795 | round_down(from, blocksize), |
4796 | blocksize); | |
ac6a2b36 | 4797 | ret = -ENOMEM; |
39279cc3 | 4798 | goto out; |
5d5e103a | 4799 | } |
e6dcd2dc | 4800 | |
9703fefe CR |
4801 | block_start = round_down(from, blocksize); |
4802 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4803 | |
39279cc3 | 4804 | if (!PageUptodate(page)) { |
9ebefb18 | 4805 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4806 | lock_page(page); |
211c17f5 CM |
4807 | if (page->mapping != mapping) { |
4808 | unlock_page(page); | |
09cbfeaf | 4809 | put_page(page); |
211c17f5 CM |
4810 | goto again; |
4811 | } | |
39279cc3 CM |
4812 | if (!PageUptodate(page)) { |
4813 | ret = -EIO; | |
89642229 | 4814 | goto out_unlock; |
39279cc3 CM |
4815 | } |
4816 | } | |
211c17f5 | 4817 | wait_on_page_writeback(page); |
e6dcd2dc | 4818 | |
9703fefe | 4819 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4820 | set_page_extent_mapped(page); |
4821 | ||
9703fefe | 4822 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4823 | if (ordered) { |
9703fefe | 4824 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4825 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4826 | unlock_page(page); |
09cbfeaf | 4827 | put_page(page); |
eb84ae03 | 4828 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4829 | btrfs_put_ordered_extent(ordered); |
4830 | goto again; | |
4831 | } | |
4832 | ||
9703fefe | 4833 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4834 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4835 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4836 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4837 | |
9703fefe | 4838 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4839 | &cached_state, 0); |
9ed74f2d | 4840 | if (ret) { |
9703fefe | 4841 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4842 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4843 | goto out_unlock; |
4844 | } | |
4845 | ||
9703fefe | 4846 | if (offset != blocksize) { |
2aaa6655 | 4847 | if (!len) |
9703fefe | 4848 | len = blocksize - offset; |
e6dcd2dc | 4849 | kaddr = kmap(page); |
2aaa6655 | 4850 | if (front) |
9703fefe CR |
4851 | memset(kaddr + (block_start - page_offset(page)), |
4852 | 0, offset); | |
2aaa6655 | 4853 | else |
9703fefe CR |
4854 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4855 | 0, len); | |
e6dcd2dc CM |
4856 | flush_dcache_page(page); |
4857 | kunmap(page); | |
4858 | } | |
247e743c | 4859 | ClearPageChecked(page); |
e6dcd2dc | 4860 | set_page_dirty(page); |
9703fefe | 4861 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4862 | GFP_NOFS); |
39279cc3 | 4863 | |
89642229 | 4864 | out_unlock: |
5d5e103a | 4865 | if (ret) |
bc42bda2 | 4866 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
9703fefe | 4867 | blocksize); |
39279cc3 | 4868 | unlock_page(page); |
09cbfeaf | 4869 | put_page(page); |
39279cc3 | 4870 | out: |
364ecf36 | 4871 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4872 | return ret; |
4873 | } | |
4874 | ||
16e7549f JB |
4875 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4876 | u64 offset, u64 len) | |
4877 | { | |
0b246afa | 4878 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4879 | struct btrfs_trans_handle *trans; |
4880 | int ret; | |
4881 | ||
4882 | /* | |
4883 | * Still need to make sure the inode looks like it's been updated so | |
4884 | * that any holes get logged if we fsync. | |
4885 | */ | |
0b246afa JM |
4886 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4887 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4888 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4889 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4890 | return 0; | |
4891 | } | |
4892 | ||
4893 | /* | |
4894 | * 1 - for the one we're dropping | |
4895 | * 1 - for the one we're adding | |
4896 | * 1 - for updating the inode. | |
4897 | */ | |
4898 | trans = btrfs_start_transaction(root, 3); | |
4899 | if (IS_ERR(trans)) | |
4900 | return PTR_ERR(trans); | |
4901 | ||
4902 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4903 | if (ret) { | |
66642832 | 4904 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4905 | btrfs_end_transaction(trans); |
16e7549f JB |
4906 | return ret; |
4907 | } | |
4908 | ||
f85b7379 DS |
4909 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4910 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4911 | if (ret) |
66642832 | 4912 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4913 | else |
4914 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4915 | btrfs_end_transaction(trans); |
16e7549f JB |
4916 | return ret; |
4917 | } | |
4918 | ||
695a0d0d JB |
4919 | /* |
4920 | * This function puts in dummy file extents for the area we're creating a hole | |
4921 | * for. So if we are truncating this file to a larger size we need to insert | |
4922 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4923 | * the range between oldsize and size | |
4924 | */ | |
a41ad394 | 4925 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4926 | { |
0b246afa | 4927 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4928 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4929 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4930 | struct extent_map *em = NULL; |
2ac55d41 | 4931 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4932 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4933 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4934 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4935 | u64 last_byte; |
4936 | u64 cur_offset; | |
4937 | u64 hole_size; | |
9ed74f2d | 4938 | int err = 0; |
39279cc3 | 4939 | |
a71754fc | 4940 | /* |
9703fefe CR |
4941 | * If our size started in the middle of a block we need to zero out the |
4942 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4943 | * expose stale data. |
4944 | */ | |
9703fefe | 4945 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4946 | if (err) |
4947 | return err; | |
4948 | ||
9036c102 YZ |
4949 | if (size <= hole_start) |
4950 | return 0; | |
4951 | ||
9036c102 YZ |
4952 | while (1) { |
4953 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4954 | |
ff13db41 | 4955 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4956 | &cached_state); |
a776c6fa | 4957 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
fa7c1494 | 4958 | block_end - hole_start); |
9036c102 YZ |
4959 | if (!ordered) |
4960 | break; | |
2ac55d41 JB |
4961 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4962 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4963 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4964 | btrfs_put_ordered_extent(ordered); |
4965 | } | |
39279cc3 | 4966 | |
9036c102 YZ |
4967 | cur_offset = hole_start; |
4968 | while (1) { | |
fc4f21b1 | 4969 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 4970 | block_end - cur_offset, 0); |
79787eaa JM |
4971 | if (IS_ERR(em)) { |
4972 | err = PTR_ERR(em); | |
f2767956 | 4973 | em = NULL; |
79787eaa JM |
4974 | break; |
4975 | } | |
9036c102 | 4976 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4977 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 4978 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4979 | struct extent_map *hole_em; |
9036c102 | 4980 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4981 | |
16e7549f JB |
4982 | err = maybe_insert_hole(root, inode, cur_offset, |
4983 | hole_size); | |
4984 | if (err) | |
3893e33b | 4985 | break; |
dcdbc059 | 4986 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4987 | cur_offset + hole_size - 1, 0); |
4988 | hole_em = alloc_extent_map(); | |
4989 | if (!hole_em) { | |
4990 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4991 | &BTRFS_I(inode)->runtime_flags); | |
4992 | goto next; | |
4993 | } | |
4994 | hole_em->start = cur_offset; | |
4995 | hole_em->len = hole_size; | |
4996 | hole_em->orig_start = cur_offset; | |
8082510e | 4997 | |
5dc562c5 JB |
4998 | hole_em->block_start = EXTENT_MAP_HOLE; |
4999 | hole_em->block_len = 0; | |
b4939680 | 5000 | hole_em->orig_block_len = 0; |
cc95bef6 | 5001 | hole_em->ram_bytes = hole_size; |
0b246afa | 5002 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 5003 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 5004 | hole_em->generation = fs_info->generation; |
8082510e | 5005 | |
5dc562c5 JB |
5006 | while (1) { |
5007 | write_lock(&em_tree->lock); | |
09a2a8f9 | 5008 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
5009 | write_unlock(&em_tree->lock); |
5010 | if (err != -EEXIST) | |
5011 | break; | |
dcdbc059 NB |
5012 | btrfs_drop_extent_cache(BTRFS_I(inode), |
5013 | cur_offset, | |
5dc562c5 JB |
5014 | cur_offset + |
5015 | hole_size - 1, 0); | |
5016 | } | |
5017 | free_extent_map(hole_em); | |
9036c102 | 5018 | } |
16e7549f | 5019 | next: |
9036c102 | 5020 | free_extent_map(em); |
a22285a6 | 5021 | em = NULL; |
9036c102 | 5022 | cur_offset = last_byte; |
8082510e | 5023 | if (cur_offset >= block_end) |
9036c102 YZ |
5024 | break; |
5025 | } | |
a22285a6 | 5026 | free_extent_map(em); |
2ac55d41 JB |
5027 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
5028 | GFP_NOFS); | |
9036c102 YZ |
5029 | return err; |
5030 | } | |
39279cc3 | 5031 | |
3972f260 | 5032 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5033 | { |
f4a2f4c5 MX |
5034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5035 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5036 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5037 | loff_t newsize = attr->ia_size; |
5038 | int mask = attr->ia_valid; | |
8082510e YZ |
5039 | int ret; |
5040 | ||
3972f260 ES |
5041 | /* |
5042 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5043 | * special case where we need to update the times despite not having | |
5044 | * these flags set. For all other operations the VFS set these flags | |
5045 | * explicitly if it wants a timestamp update. | |
5046 | */ | |
dff6efc3 CH |
5047 | if (newsize != oldsize) { |
5048 | inode_inc_iversion(inode); | |
5049 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5050 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5051 | current_time(inode); |
dff6efc3 | 5052 | } |
3972f260 | 5053 | |
a41ad394 | 5054 | if (newsize > oldsize) { |
9ea24bbe FM |
5055 | /* |
5056 | * Don't do an expanding truncate while snapshoting is ongoing. | |
5057 | * This is to ensure the snapshot captures a fully consistent | |
5058 | * state of this file - if the snapshot captures this expanding | |
5059 | * truncation, it must capture all writes that happened before | |
5060 | * this truncation. | |
5061 | */ | |
0bc19f90 | 5062 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5063 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
5064 | if (ret) { |
5065 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 5066 | return ret; |
9ea24bbe | 5067 | } |
8082510e | 5068 | |
f4a2f4c5 | 5069 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
5070 | if (IS_ERR(trans)) { |
5071 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 5072 | return PTR_ERR(trans); |
9ea24bbe | 5073 | } |
f4a2f4c5 MX |
5074 | |
5075 | i_size_write(inode, newsize); | |
5076 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5077 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5078 | ret = btrfs_update_inode(trans, root, inode); |
9ea24bbe | 5079 | btrfs_end_write_no_snapshoting(root); |
3a45bb20 | 5080 | btrfs_end_transaction(trans); |
a41ad394 | 5081 | } else { |
8082510e | 5082 | |
a41ad394 JB |
5083 | /* |
5084 | * We're truncating a file that used to have good data down to | |
5085 | * zero. Make sure it gets into the ordered flush list so that | |
5086 | * any new writes get down to disk quickly. | |
5087 | */ | |
5088 | if (newsize == 0) | |
72ac3c0d JB |
5089 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5090 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5091 | |
f3fe820c JB |
5092 | /* |
5093 | * 1 for the orphan item we're going to add | |
5094 | * 1 for the orphan item deletion. | |
5095 | */ | |
5096 | trans = btrfs_start_transaction(root, 2); | |
5097 | if (IS_ERR(trans)) | |
5098 | return PTR_ERR(trans); | |
5099 | ||
5100 | /* | |
5101 | * We need to do this in case we fail at _any_ point during the | |
5102 | * actual truncate. Once we do the truncate_setsize we could | |
5103 | * invalidate pages which forces any outstanding ordered io to | |
5104 | * be instantly completed which will give us extents that need | |
5105 | * to be truncated. If we fail to get an orphan inode down we | |
5106 | * could have left over extents that were never meant to live, | |
01327610 | 5107 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5108 | * will be consistent. |
5109 | */ | |
73f2e545 | 5110 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 5111 | btrfs_end_transaction(trans); |
f3fe820c JB |
5112 | if (ret) |
5113 | return ret; | |
5114 | ||
a41ad394 JB |
5115 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5116 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5117 | |
5118 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
abcefb1e | 5119 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5120 | inode_dio_wait(inode); |
0b581701 | 5121 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5122 | |
a41ad394 | 5123 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5124 | if (ret && inode->i_nlink) { |
5125 | int err; | |
5126 | ||
19fd2df5 LB |
5127 | /* To get a stable disk_i_size */ |
5128 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5129 | if (err) { | |
3d6ae7bb | 5130 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
19fd2df5 LB |
5131 | return err; |
5132 | } | |
5133 | ||
7f4f6e0a JB |
5134 | /* |
5135 | * failed to truncate, disk_i_size is only adjusted down | |
5136 | * as we remove extents, so it should represent the true | |
5137 | * size of the inode, so reset the in memory size and | |
5138 | * delete our orphan entry. | |
5139 | */ | |
5140 | trans = btrfs_join_transaction(root); | |
5141 | if (IS_ERR(trans)) { | |
3d6ae7bb | 5142 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7f4f6e0a JB |
5143 | return ret; |
5144 | } | |
5145 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
3d6ae7bb | 5146 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
7f4f6e0a | 5147 | if (err) |
66642832 | 5148 | btrfs_abort_transaction(trans, err); |
3a45bb20 | 5149 | btrfs_end_transaction(trans); |
7f4f6e0a | 5150 | } |
8082510e YZ |
5151 | } |
5152 | ||
a41ad394 | 5153 | return ret; |
8082510e YZ |
5154 | } |
5155 | ||
9036c102 YZ |
5156 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5157 | { | |
2b0143b5 | 5158 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5159 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5160 | int err; |
39279cc3 | 5161 | |
b83cc969 LZ |
5162 | if (btrfs_root_readonly(root)) |
5163 | return -EROFS; | |
5164 | ||
31051c85 | 5165 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5166 | if (err) |
5167 | return err; | |
2bf5a725 | 5168 | |
5a3f23d5 | 5169 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5170 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5171 | if (err) |
5172 | return err; | |
39279cc3 | 5173 | } |
9036c102 | 5174 | |
1025774c CH |
5175 | if (attr->ia_valid) { |
5176 | setattr_copy(inode, attr); | |
0c4d2d95 | 5177 | inode_inc_iversion(inode); |
22c44fe6 | 5178 | err = btrfs_dirty_inode(inode); |
1025774c | 5179 | |
22c44fe6 | 5180 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5181 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5182 | } |
33268eaf | 5183 | |
39279cc3 CM |
5184 | return err; |
5185 | } | |
61295eb8 | 5186 | |
131e404a FDBM |
5187 | /* |
5188 | * While truncating the inode pages during eviction, we get the VFS calling | |
5189 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5190 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5191 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5192 | * extent_state structures over and over, wasting lots of time. | |
5193 | * | |
5194 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5195 | * those expensive operations on a per page basis and do only the ordered io | |
5196 | * finishing, while we release here the extent_map and extent_state structures, | |
5197 | * without the excessive merging and splitting. | |
5198 | */ | |
5199 | static void evict_inode_truncate_pages(struct inode *inode) | |
5200 | { | |
5201 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5202 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5203 | struct rb_node *node; | |
5204 | ||
5205 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5206 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5207 | |
5208 | write_lock(&map_tree->lock); | |
5209 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5210 | struct extent_map *em; | |
5211 | ||
5212 | node = rb_first(&map_tree->map); | |
5213 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5214 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5215 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5216 | remove_extent_mapping(map_tree, em); |
5217 | free_extent_map(em); | |
7064dd5c FM |
5218 | if (need_resched()) { |
5219 | write_unlock(&map_tree->lock); | |
5220 | cond_resched(); | |
5221 | write_lock(&map_tree->lock); | |
5222 | } | |
131e404a FDBM |
5223 | } |
5224 | write_unlock(&map_tree->lock); | |
5225 | ||
6ca07097 FM |
5226 | /* |
5227 | * Keep looping until we have no more ranges in the io tree. | |
5228 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5229 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5230 | * still in progress (unlocked the pages in the bio but did not yet | |
5231 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5232 | * ranges can still be locked and eviction started because before |
5233 | * submitting those bios, which are executed by a separate task (work | |
5234 | * queue kthread), inode references (inode->i_count) were not taken | |
5235 | * (which would be dropped in the end io callback of each bio). | |
5236 | * Therefore here we effectively end up waiting for those bios and | |
5237 | * anyone else holding locked ranges without having bumped the inode's | |
5238 | * reference count - if we don't do it, when they access the inode's | |
5239 | * io_tree to unlock a range it may be too late, leading to an | |
5240 | * use-after-free issue. | |
5241 | */ | |
131e404a FDBM |
5242 | spin_lock(&io_tree->lock); |
5243 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5244 | struct extent_state *state; | |
5245 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5246 | u64 start; |
5247 | u64 end; | |
131e404a FDBM |
5248 | |
5249 | node = rb_first(&io_tree->state); | |
5250 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5251 | start = state->start; |
5252 | end = state->end; | |
131e404a FDBM |
5253 | spin_unlock(&io_tree->lock); |
5254 | ||
ff13db41 | 5255 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5256 | |
5257 | /* | |
5258 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5259 | * and its reserved space won't be freed by delayed_ref. | |
5260 | * So we need to free its reserved space here. | |
5261 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5262 | * | |
5263 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5264 | */ | |
5265 | if (state->state & EXTENT_DELALLOC) | |
bc42bda2 | 5266 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
b9d0b389 | 5267 | |
6ca07097 | 5268 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5269 | EXTENT_LOCKED | EXTENT_DIRTY | |
5270 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5271 | EXTENT_DEFRAG, 1, 1, | |
5272 | &cached_state, GFP_NOFS); | |
131e404a | 5273 | |
7064dd5c | 5274 | cond_resched(); |
131e404a FDBM |
5275 | spin_lock(&io_tree->lock); |
5276 | } | |
5277 | spin_unlock(&io_tree->lock); | |
5278 | } | |
5279 | ||
bd555975 | 5280 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5281 | { |
0b246afa | 5282 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5283 | struct btrfs_trans_handle *trans; |
5284 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5285 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5286 | int steal_from_global = 0; |
3d48d981 | 5287 | u64 min_size; |
39279cc3 CM |
5288 | int ret; |
5289 | ||
1abe9b8a | 5290 | trace_btrfs_inode_evict(inode); |
5291 | ||
3d48d981 NB |
5292 | if (!root) { |
5293 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5294 | return; | |
5295 | } | |
5296 | ||
0b246afa | 5297 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
3d48d981 | 5298 | |
131e404a FDBM |
5299 | evict_inode_truncate_pages(inode); |
5300 | ||
69e9c6c6 SB |
5301 | if (inode->i_nlink && |
5302 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5303 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5304 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5305 | goto no_delete; |
5306 | ||
39279cc3 | 5307 | if (is_bad_inode(inode)) { |
3d6ae7bb | 5308 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
39279cc3 CM |
5309 | goto no_delete; |
5310 | } | |
bd555975 | 5311 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5312 | if (!special_file(inode->i_mode)) |
5313 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5314 | |
7ab7956e | 5315 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5316 | |
0b246afa | 5317 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
6bf02314 | 5318 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5319 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5320 | goto no_delete; |
5321 | } | |
5322 | ||
76dda93c | 5323 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5324 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5325 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5326 | goto no_delete; |
5327 | } | |
5328 | ||
aa79021f | 5329 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
0e8c36a9 | 5330 | if (ret) { |
3d6ae7bb | 5331 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
0e8c36a9 MX |
5332 | goto no_delete; |
5333 | } | |
5334 | ||
2ff7e61e | 5335 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
4289a667 | 5336 | if (!rsv) { |
3d6ae7bb | 5337 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
4289a667 JB |
5338 | goto no_delete; |
5339 | } | |
4a338542 | 5340 | rsv->size = min_size; |
ca7e70f5 | 5341 | rsv->failfast = 1; |
0b246afa | 5342 | global_rsv = &fs_info->global_block_rsv; |
4289a667 | 5343 | |
6ef06d27 | 5344 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5345 | |
4289a667 | 5346 | /* |
8407aa46 MX |
5347 | * This is a bit simpler than btrfs_truncate since we've already |
5348 | * reserved our space for our orphan item in the unlink, so we just | |
5349 | * need to reserve some slack space in case we add bytes and update | |
5350 | * inode item when doing the truncate. | |
4289a667 | 5351 | */ |
8082510e | 5352 | while (1) { |
08e007d2 MX |
5353 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5354 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5355 | |
5356 | /* | |
5357 | * Try and steal from the global reserve since we will | |
5358 | * likely not use this space anyway, we want to try as | |
5359 | * hard as possible to get this to work. | |
5360 | */ | |
5361 | if (ret) | |
3bce876f JB |
5362 | steal_from_global++; |
5363 | else | |
5364 | steal_from_global = 0; | |
5365 | ret = 0; | |
d68fc57b | 5366 | |
3bce876f JB |
5367 | /* |
5368 | * steal_from_global == 0: we reserved stuff, hooray! | |
5369 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5370 | * steal_from_global == 2: we've committed, still not a lot of | |
5371 | * room but maybe we'll have room in the global reserve this | |
5372 | * time. | |
5373 | * steal_from_global == 3: abandon all hope! | |
5374 | */ | |
5375 | if (steal_from_global > 2) { | |
0b246afa JM |
5376 | btrfs_warn(fs_info, |
5377 | "Could not get space for a delete, will truncate on mount %d", | |
5378 | ret); | |
3d6ae7bb | 5379 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5380 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5381 | goto no_delete; |
d68fc57b | 5382 | } |
7b128766 | 5383 | |
0e8c36a9 | 5384 | trans = btrfs_join_transaction(root); |
4289a667 | 5385 | if (IS_ERR(trans)) { |
3d6ae7bb | 5386 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5387 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5388 | goto no_delete; |
d68fc57b | 5389 | } |
7b128766 | 5390 | |
3bce876f | 5391 | /* |
01327610 | 5392 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5393 | * sure there is room to do it, if not we need to commit and try |
5394 | * again. | |
5395 | */ | |
5396 | if (steal_from_global) { | |
2ff7e61e | 5397 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
3bce876f | 5398 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
25d609f8 | 5399 | min_size, 0); |
3bce876f JB |
5400 | else |
5401 | ret = -ENOSPC; | |
5402 | } | |
5403 | ||
5404 | /* | |
5405 | * Couldn't steal from the global reserve, we have too much | |
5406 | * pending stuff built up, commit the transaction and try it | |
5407 | * again. | |
5408 | */ | |
5409 | if (ret) { | |
3a45bb20 | 5410 | ret = btrfs_commit_transaction(trans); |
3bce876f | 5411 | if (ret) { |
3d6ae7bb | 5412 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5413 | btrfs_free_block_rsv(fs_info, rsv); |
3bce876f JB |
5414 | goto no_delete; |
5415 | } | |
5416 | continue; | |
5417 | } else { | |
5418 | steal_from_global = 0; | |
5419 | } | |
5420 | ||
4289a667 JB |
5421 | trans->block_rsv = rsv; |
5422 | ||
d68fc57b | 5423 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5424 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5425 | break; |
85e21bac | 5426 | |
0b246afa | 5427 | trans->block_rsv = &fs_info->trans_block_rsv; |
3a45bb20 | 5428 | btrfs_end_transaction(trans); |
8082510e | 5429 | trans = NULL; |
2ff7e61e | 5430 | btrfs_btree_balance_dirty(fs_info); |
8082510e | 5431 | } |
5f39d397 | 5432 | |
2ff7e61e | 5433 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5434 | |
4ef31a45 JB |
5435 | /* |
5436 | * Errors here aren't a big deal, it just means we leave orphan items | |
5437 | * in the tree. They will be cleaned up on the next mount. | |
5438 | */ | |
8082510e | 5439 | if (ret == 0) { |
4289a667 | 5440 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 5441 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
4ef31a45 | 5442 | } else { |
3d6ae7bb | 5443 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
8082510e | 5444 | } |
54aa1f4d | 5445 | |
0b246afa JM |
5446 | trans->block_rsv = &fs_info->trans_block_rsv; |
5447 | if (!(root == fs_info->tree_root || | |
581bb050 | 5448 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5449 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5450 | |
3a45bb20 | 5451 | btrfs_end_transaction(trans); |
2ff7e61e | 5452 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 5453 | no_delete: |
f48d1cf5 | 5454 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5455 | clear_inode(inode); |
39279cc3 CM |
5456 | } |
5457 | ||
5458 | /* | |
5459 | * this returns the key found in the dir entry in the location pointer. | |
5460 | * If no dir entries were found, location->objectid is 0. | |
5461 | */ | |
5462 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5463 | struct btrfs_key *location) | |
5464 | { | |
5465 | const char *name = dentry->d_name.name; | |
5466 | int namelen = dentry->d_name.len; | |
5467 | struct btrfs_dir_item *di; | |
5468 | struct btrfs_path *path; | |
5469 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5470 | int ret = 0; |
39279cc3 CM |
5471 | |
5472 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5473 | if (!path) |
5474 | return -ENOMEM; | |
3954401f | 5475 | |
f85b7379 DS |
5476 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5477 | name, namelen, 0); | |
0d9f7f3e Y |
5478 | if (IS_ERR(di)) |
5479 | ret = PTR_ERR(di); | |
d397712b | 5480 | |
c704005d | 5481 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5482 | goto out_err; |
d397712b | 5483 | |
5f39d397 | 5484 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5485 | out: |
39279cc3 CM |
5486 | btrfs_free_path(path); |
5487 | return ret; | |
3954401f CM |
5488 | out_err: |
5489 | location->objectid = 0; | |
5490 | goto out; | |
39279cc3 CM |
5491 | } |
5492 | ||
5493 | /* | |
5494 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5495 | * needs to be changed to reflect the root directory of the tree root. This | |
5496 | * is kind of like crossing a mount point. | |
5497 | */ | |
2ff7e61e | 5498 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5499 | struct inode *dir, |
5500 | struct dentry *dentry, | |
5501 | struct btrfs_key *location, | |
5502 | struct btrfs_root **sub_root) | |
39279cc3 | 5503 | { |
4df27c4d YZ |
5504 | struct btrfs_path *path; |
5505 | struct btrfs_root *new_root; | |
5506 | struct btrfs_root_ref *ref; | |
5507 | struct extent_buffer *leaf; | |
1d4c08e0 | 5508 | struct btrfs_key key; |
4df27c4d YZ |
5509 | int ret; |
5510 | int err = 0; | |
39279cc3 | 5511 | |
4df27c4d YZ |
5512 | path = btrfs_alloc_path(); |
5513 | if (!path) { | |
5514 | err = -ENOMEM; | |
5515 | goto out; | |
5516 | } | |
39279cc3 | 5517 | |
4df27c4d | 5518 | err = -ENOENT; |
1d4c08e0 DS |
5519 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5520 | key.type = BTRFS_ROOT_REF_KEY; | |
5521 | key.offset = location->objectid; | |
5522 | ||
0b246afa | 5523 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5524 | if (ret) { |
5525 | if (ret < 0) | |
5526 | err = ret; | |
5527 | goto out; | |
5528 | } | |
39279cc3 | 5529 | |
4df27c4d YZ |
5530 | leaf = path->nodes[0]; |
5531 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5532 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5533 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5534 | goto out; | |
39279cc3 | 5535 | |
4df27c4d YZ |
5536 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5537 | (unsigned long)(ref + 1), | |
5538 | dentry->d_name.len); | |
5539 | if (ret) | |
5540 | goto out; | |
5541 | ||
b3b4aa74 | 5542 | btrfs_release_path(path); |
4df27c4d | 5543 | |
0b246afa | 5544 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5545 | if (IS_ERR(new_root)) { |
5546 | err = PTR_ERR(new_root); | |
5547 | goto out; | |
5548 | } | |
5549 | ||
4df27c4d YZ |
5550 | *sub_root = new_root; |
5551 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5552 | location->type = BTRFS_INODE_ITEM_KEY; | |
5553 | location->offset = 0; | |
5554 | err = 0; | |
5555 | out: | |
5556 | btrfs_free_path(path); | |
5557 | return err; | |
39279cc3 CM |
5558 | } |
5559 | ||
5d4f98a2 YZ |
5560 | static void inode_tree_add(struct inode *inode) |
5561 | { | |
5562 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5563 | struct btrfs_inode *entry; | |
03e860bd NP |
5564 | struct rb_node **p; |
5565 | struct rb_node *parent; | |
cef21937 | 5566 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5567 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5568 | |
1d3382cb | 5569 | if (inode_unhashed(inode)) |
76dda93c | 5570 | return; |
e1409cef | 5571 | parent = NULL; |
5d4f98a2 | 5572 | spin_lock(&root->inode_lock); |
e1409cef | 5573 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5574 | while (*p) { |
5575 | parent = *p; | |
5576 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5577 | ||
4a0cc7ca | 5578 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5579 | p = &parent->rb_left; |
4a0cc7ca | 5580 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5581 | p = &parent->rb_right; |
5d4f98a2 YZ |
5582 | else { |
5583 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5584 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5585 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5586 | RB_CLEAR_NODE(parent); |
5587 | spin_unlock(&root->inode_lock); | |
cef21937 | 5588 | return; |
5d4f98a2 YZ |
5589 | } |
5590 | } | |
cef21937 FDBM |
5591 | rb_link_node(new, parent, p); |
5592 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5593 | spin_unlock(&root->inode_lock); |
5594 | } | |
5595 | ||
5596 | static void inode_tree_del(struct inode *inode) | |
5597 | { | |
0b246afa | 5598 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5599 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5600 | int empty = 0; |
5d4f98a2 | 5601 | |
03e860bd | 5602 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5603 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5604 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5605 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5606 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5607 | } |
03e860bd | 5608 | spin_unlock(&root->inode_lock); |
76dda93c | 5609 | |
69e9c6c6 | 5610 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5611 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5612 | spin_lock(&root->inode_lock); |
5613 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5614 | spin_unlock(&root->inode_lock); | |
5615 | if (empty) | |
5616 | btrfs_add_dead_root(root); | |
5617 | } | |
5618 | } | |
5619 | ||
143bede5 | 5620 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c | 5621 | { |
0b246afa | 5622 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c YZ |
5623 | struct rb_node *node; |
5624 | struct rb_node *prev; | |
5625 | struct btrfs_inode *entry; | |
5626 | struct inode *inode; | |
5627 | u64 objectid = 0; | |
5628 | ||
0b246afa | 5629 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
7813b3db | 5630 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
76dda93c YZ |
5631 | |
5632 | spin_lock(&root->inode_lock); | |
5633 | again: | |
5634 | node = root->inode_tree.rb_node; | |
5635 | prev = NULL; | |
5636 | while (node) { | |
5637 | prev = node; | |
5638 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5639 | ||
4a0cc7ca | 5640 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c | 5641 | node = node->rb_left; |
4a0cc7ca | 5642 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c YZ |
5643 | node = node->rb_right; |
5644 | else | |
5645 | break; | |
5646 | } | |
5647 | if (!node) { | |
5648 | while (prev) { | |
5649 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5650 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
76dda93c YZ |
5651 | node = prev; |
5652 | break; | |
5653 | } | |
5654 | prev = rb_next(prev); | |
5655 | } | |
5656 | } | |
5657 | while (node) { | |
5658 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5659 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
76dda93c YZ |
5660 | inode = igrab(&entry->vfs_inode); |
5661 | if (inode) { | |
5662 | spin_unlock(&root->inode_lock); | |
5663 | if (atomic_read(&inode->i_count) > 1) | |
5664 | d_prune_aliases(inode); | |
5665 | /* | |
45321ac5 | 5666 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5667 | * the inode cache when its usage count |
5668 | * hits zero. | |
5669 | */ | |
5670 | iput(inode); | |
5671 | cond_resched(); | |
5672 | spin_lock(&root->inode_lock); | |
5673 | goto again; | |
5674 | } | |
5675 | ||
5676 | if (cond_resched_lock(&root->inode_lock)) | |
5677 | goto again; | |
5678 | ||
5679 | node = rb_next(node); | |
5680 | } | |
5681 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5682 | } |
5683 | ||
e02119d5 CM |
5684 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5685 | { | |
5686 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5687 | inode->i_ino = args->location->objectid; |
5688 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5689 | sizeof(*args->location)); | |
e02119d5 | 5690 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5691 | return 0; |
5692 | } | |
5693 | ||
5694 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5695 | { | |
5696 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5697 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5698 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5699 | } |
5700 | ||
5d4f98a2 | 5701 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5702 | struct btrfs_key *location, |
5d4f98a2 | 5703 | struct btrfs_root *root) |
39279cc3 CM |
5704 | { |
5705 | struct inode *inode; | |
5706 | struct btrfs_iget_args args; | |
90d3e592 | 5707 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5708 | |
90d3e592 | 5709 | args.location = location; |
39279cc3 CM |
5710 | args.root = root; |
5711 | ||
778ba82b | 5712 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5713 | btrfs_init_locked_inode, |
5714 | (void *)&args); | |
5715 | return inode; | |
5716 | } | |
5717 | ||
1a54ef8c BR |
5718 | /* Get an inode object given its location and corresponding root. |
5719 | * Returns in *is_new if the inode was read from disk | |
5720 | */ | |
5721 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5722 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5723 | { |
5724 | struct inode *inode; | |
5725 | ||
90d3e592 | 5726 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5727 | if (!inode) |
5d4f98a2 | 5728 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5729 | |
5730 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5731 | int ret; |
5732 | ||
5733 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5734 | if (!is_bad_inode(inode)) { |
5735 | inode_tree_add(inode); | |
5736 | unlock_new_inode(inode); | |
5737 | if (new) | |
5738 | *new = 1; | |
5739 | } else { | |
e0b6d65b ST |
5740 | unlock_new_inode(inode); |
5741 | iput(inode); | |
67710892 FM |
5742 | ASSERT(ret < 0); |
5743 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5744 | } |
5745 | } | |
5746 | ||
1a54ef8c BR |
5747 | return inode; |
5748 | } | |
5749 | ||
4df27c4d YZ |
5750 | static struct inode *new_simple_dir(struct super_block *s, |
5751 | struct btrfs_key *key, | |
5752 | struct btrfs_root *root) | |
5753 | { | |
5754 | struct inode *inode = new_inode(s); | |
5755 | ||
5756 | if (!inode) | |
5757 | return ERR_PTR(-ENOMEM); | |
5758 | ||
4df27c4d YZ |
5759 | BTRFS_I(inode)->root = root; |
5760 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5761 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5762 | |
5763 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5764 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5765 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5766 | inode->i_fop = &simple_dir_operations; |
5767 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5768 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5769 | inode->i_atime = inode->i_mtime; |
5770 | inode->i_ctime = inode->i_mtime; | |
5771 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5772 | |
5773 | return inode; | |
5774 | } | |
5775 | ||
3de4586c | 5776 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5777 | { |
0b246afa | 5778 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5779 | struct inode *inode; |
4df27c4d | 5780 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5781 | struct btrfs_root *sub_root = root; |
5782 | struct btrfs_key location; | |
76dda93c | 5783 | int index; |
b4aff1f8 | 5784 | int ret = 0; |
39279cc3 CM |
5785 | |
5786 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5787 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5788 | |
39e3c955 | 5789 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5790 | if (ret < 0) |
5791 | return ERR_PTR(ret); | |
5f39d397 | 5792 | |
4df27c4d | 5793 | if (location.objectid == 0) |
5662344b | 5794 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5795 | |
5796 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5797 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5798 | return inode; |
5799 | } | |
5800 | ||
5801 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5802 | ||
0b246afa | 5803 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5804 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5805 | &location, &sub_root); |
5806 | if (ret < 0) { | |
5807 | if (ret != -ENOENT) | |
5808 | inode = ERR_PTR(ret); | |
5809 | else | |
5810 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5811 | } else { | |
73f73415 | 5812 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5813 | } |
0b246afa | 5814 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5815 | |
34d19bad | 5816 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5817 | down_read(&fs_info->cleanup_work_sem); |
c71bf099 | 5818 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
66b4ffd1 | 5819 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5820 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5821 | if (ret) { |
5822 | iput(inode); | |
66b4ffd1 | 5823 | inode = ERR_PTR(ret); |
01cd3367 | 5824 | } |
c71bf099 YZ |
5825 | } |
5826 | ||
3de4586c CM |
5827 | return inode; |
5828 | } | |
5829 | ||
fe15ce44 | 5830 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5831 | { |
5832 | struct btrfs_root *root; | |
2b0143b5 | 5833 | struct inode *inode = d_inode(dentry); |
76dda93c | 5834 | |
848cce0d | 5835 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5836 | inode = d_inode(dentry->d_parent); |
76dda93c | 5837 | |
848cce0d LZ |
5838 | if (inode) { |
5839 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5840 | if (btrfs_root_refs(&root->root_item) == 0) |
5841 | return 1; | |
848cce0d | 5842 | |
4a0cc7ca | 5843 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5844 | return 1; |
efefb143 | 5845 | } |
76dda93c YZ |
5846 | return 0; |
5847 | } | |
5848 | ||
b4aff1f8 JB |
5849 | static void btrfs_dentry_release(struct dentry *dentry) |
5850 | { | |
944a4515 | 5851 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5852 | } |
5853 | ||
3de4586c | 5854 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5855 | unsigned int flags) |
3de4586c | 5856 | { |
5662344b | 5857 | struct inode *inode; |
a66e7cc6 | 5858 | |
5662344b TI |
5859 | inode = btrfs_lookup_dentry(dir, dentry); |
5860 | if (IS_ERR(inode)) { | |
5861 | if (PTR_ERR(inode) == -ENOENT) | |
5862 | inode = NULL; | |
5863 | else | |
5864 | return ERR_CAST(inode); | |
5865 | } | |
5866 | ||
41d28bca | 5867 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5868 | } |
5869 | ||
16cdcec7 | 5870 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5871 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5872 | }; | |
5873 | ||
9cdda8d3 | 5874 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5875 | { |
9cdda8d3 | 5876 | struct inode *inode = file_inode(file); |
2ff7e61e | 5877 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 5878 | struct btrfs_root *root = BTRFS_I(inode)->root; |
39279cc3 CM |
5879 | struct btrfs_dir_item *di; |
5880 | struct btrfs_key key; | |
5f39d397 | 5881 | struct btrfs_key found_key; |
39279cc3 | 5882 | struct btrfs_path *path; |
16cdcec7 MX |
5883 | struct list_head ins_list; |
5884 | struct list_head del_list; | |
39279cc3 | 5885 | int ret; |
5f39d397 | 5886 | struct extent_buffer *leaf; |
39279cc3 | 5887 | int slot; |
39279cc3 CM |
5888 | unsigned char d_type; |
5889 | int over = 0; | |
5f39d397 CM |
5890 | char tmp_name[32]; |
5891 | char *name_ptr; | |
5892 | int name_len; | |
02dbfc99 | 5893 | bool put = false; |
c2951f32 | 5894 | struct btrfs_key location; |
5f39d397 | 5895 | |
9cdda8d3 AV |
5896 | if (!dir_emit_dots(file, ctx)) |
5897 | return 0; | |
5898 | ||
49593bfa | 5899 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5900 | if (!path) |
5901 | return -ENOMEM; | |
ff5714cc | 5902 | |
e4058b54 | 5903 | path->reada = READA_FORWARD; |
49593bfa | 5904 | |
c2951f32 JM |
5905 | INIT_LIST_HEAD(&ins_list); |
5906 | INIT_LIST_HEAD(&del_list); | |
5907 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5908 | |
c2951f32 | 5909 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5910 | key.offset = ctx->pos; |
4a0cc7ca | 5911 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5912 | |
39279cc3 CM |
5913 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5914 | if (ret < 0) | |
5915 | goto err; | |
49593bfa DW |
5916 | |
5917 | while (1) { | |
5f39d397 | 5918 | leaf = path->nodes[0]; |
39279cc3 | 5919 | slot = path->slots[0]; |
b9e03af0 LZ |
5920 | if (slot >= btrfs_header_nritems(leaf)) { |
5921 | ret = btrfs_next_leaf(root, path); | |
5922 | if (ret < 0) | |
5923 | goto err; | |
5924 | else if (ret > 0) | |
5925 | break; | |
5926 | continue; | |
39279cc3 | 5927 | } |
3de4586c | 5928 | |
5f39d397 CM |
5929 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5930 | ||
5931 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5932 | break; |
c2951f32 | 5933 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5934 | break; |
9cdda8d3 | 5935 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5936 | goto next; |
c2951f32 | 5937 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5938 | goto next; |
5f39d397 | 5939 | |
9cdda8d3 | 5940 | ctx->pos = found_key.offset; |
49593bfa | 5941 | |
39279cc3 | 5942 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e79a3327 | 5943 | if (verify_dir_item(fs_info, leaf, slot, di)) |
c2951f32 | 5944 | goto next; |
22a94d44 | 5945 | |
c2951f32 JM |
5946 | name_len = btrfs_dir_name_len(leaf, di); |
5947 | if (name_len <= sizeof(tmp_name)) { | |
5948 | name_ptr = tmp_name; | |
5949 | } else { | |
5950 | name_ptr = kmalloc(name_len, GFP_KERNEL); | |
5951 | if (!name_ptr) { | |
5952 | ret = -ENOMEM; | |
5953 | goto err; | |
5f39d397 | 5954 | } |
c2951f32 JM |
5955 | } |
5956 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), | |
5957 | name_len); | |
3de4586c | 5958 | |
c2951f32 JM |
5959 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
5960 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
fede766f | 5961 | |
c2951f32 JM |
5962 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
5963 | d_type); | |
5f39d397 | 5964 | |
c2951f32 JM |
5965 | if (name_ptr != tmp_name) |
5966 | kfree(name_ptr); | |
5f39d397 | 5967 | |
c2951f32 JM |
5968 | if (over) |
5969 | goto nopos; | |
d2fbb2b5 | 5970 | ctx->pos++; |
b9e03af0 LZ |
5971 | next: |
5972 | path->slots[0]++; | |
39279cc3 | 5973 | } |
49593bfa | 5974 | |
d2fbb2b5 | 5975 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5976 | if (ret) |
bc4ef759 DS |
5977 | goto nopos; |
5978 | ||
db62efbb ZB |
5979 | /* |
5980 | * Stop new entries from being returned after we return the last | |
5981 | * entry. | |
5982 | * | |
5983 | * New directory entries are assigned a strictly increasing | |
5984 | * offset. This means that new entries created during readdir | |
5985 | * are *guaranteed* to be seen in the future by that readdir. | |
5986 | * This has broken buggy programs which operate on names as | |
5987 | * they're returned by readdir. Until we re-use freed offsets | |
5988 | * we have this hack to stop new entries from being returned | |
5989 | * under the assumption that they'll never reach this huge | |
5990 | * offset. | |
5991 | * | |
5992 | * This is being careful not to overflow 32bit loff_t unless the | |
5993 | * last entry requires it because doing so has broken 32bit apps | |
5994 | * in the past. | |
5995 | */ | |
c2951f32 JM |
5996 | if (ctx->pos >= INT_MAX) |
5997 | ctx->pos = LLONG_MAX; | |
5998 | else | |
5999 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6000 | nopos: |
6001 | ret = 0; | |
6002 | err: | |
02dbfc99 OS |
6003 | if (put) |
6004 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6005 | btrfs_free_path(path); |
39279cc3 CM |
6006 | return ret; |
6007 | } | |
6008 | ||
a9185b41 | 6009 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
6010 | { |
6011 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6012 | struct btrfs_trans_handle *trans; | |
6013 | int ret = 0; | |
0af3d00b | 6014 | bool nolock = false; |
39279cc3 | 6015 | |
72ac3c0d | 6016 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
6017 | return 0; |
6018 | ||
70ddc553 NB |
6019 | if (btrfs_fs_closing(root->fs_info) && |
6020 | btrfs_is_free_space_inode(BTRFS_I(inode))) | |
82d5902d | 6021 | nolock = true; |
0af3d00b | 6022 | |
a9185b41 | 6023 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 6024 | if (nolock) |
7a7eaa40 | 6025 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 6026 | else |
7a7eaa40 | 6027 | trans = btrfs_join_transaction(root); |
3612b495 TI |
6028 | if (IS_ERR(trans)) |
6029 | return PTR_ERR(trans); | |
3a45bb20 | 6030 | ret = btrfs_commit_transaction(trans); |
39279cc3 CM |
6031 | } |
6032 | return ret; | |
6033 | } | |
6034 | ||
6035 | /* | |
54aa1f4d | 6036 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6037 | * inode changes. But, it is most likely to find the inode in cache. |
6038 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6039 | * to keep or drop this code. | |
6040 | */ | |
48a3b636 | 6041 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6042 | { |
2ff7e61e | 6043 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6044 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6045 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6046 | int ret; |
6047 | ||
72ac3c0d | 6048 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6049 | return 0; |
39279cc3 | 6050 | |
7a7eaa40 | 6051 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6052 | if (IS_ERR(trans)) |
6053 | return PTR_ERR(trans); | |
8929ecfa YZ |
6054 | |
6055 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6056 | if (ret && ret == -ENOSPC) { |
6057 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6058 | btrfs_end_transaction(trans); |
94b60442 | 6059 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6060 | if (IS_ERR(trans)) |
6061 | return PTR_ERR(trans); | |
8929ecfa | 6062 | |
94b60442 | 6063 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6064 | } |
3a45bb20 | 6065 | btrfs_end_transaction(trans); |
16cdcec7 | 6066 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6067 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6068 | |
6069 | return ret; | |
6070 | } | |
6071 | ||
6072 | /* | |
6073 | * This is a copy of file_update_time. We need this so we can return error on | |
6074 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6075 | */ | |
e41f941a JB |
6076 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6077 | int flags) | |
22c44fe6 | 6078 | { |
2bc55652 AB |
6079 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6080 | ||
6081 | if (btrfs_root_readonly(root)) | |
6082 | return -EROFS; | |
6083 | ||
e41f941a | 6084 | if (flags & S_VERSION) |
22c44fe6 | 6085 | inode_inc_iversion(inode); |
e41f941a JB |
6086 | if (flags & S_CTIME) |
6087 | inode->i_ctime = *now; | |
6088 | if (flags & S_MTIME) | |
6089 | inode->i_mtime = *now; | |
6090 | if (flags & S_ATIME) | |
6091 | inode->i_atime = *now; | |
6092 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6093 | } |
6094 | ||
d352ac68 CM |
6095 | /* |
6096 | * find the highest existing sequence number in a directory | |
6097 | * and then set the in-memory index_cnt variable to reflect | |
6098 | * free sequence numbers | |
6099 | */ | |
4c570655 | 6100 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6101 | { |
4c570655 | 6102 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6103 | struct btrfs_key key, found_key; |
6104 | struct btrfs_path *path; | |
6105 | struct extent_buffer *leaf; | |
6106 | int ret; | |
6107 | ||
4c570655 | 6108 | key.objectid = btrfs_ino(inode); |
962a298f | 6109 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6110 | key.offset = (u64)-1; |
6111 | ||
6112 | path = btrfs_alloc_path(); | |
6113 | if (!path) | |
6114 | return -ENOMEM; | |
6115 | ||
6116 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6117 | if (ret < 0) | |
6118 | goto out; | |
6119 | /* FIXME: we should be able to handle this */ | |
6120 | if (ret == 0) | |
6121 | goto out; | |
6122 | ret = 0; | |
6123 | ||
6124 | /* | |
6125 | * MAGIC NUMBER EXPLANATION: | |
6126 | * since we search a directory based on f_pos we have to start at 2 | |
6127 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6128 | * else has to start at 2 | |
6129 | */ | |
6130 | if (path->slots[0] == 0) { | |
4c570655 | 6131 | inode->index_cnt = 2; |
aec7477b JB |
6132 | goto out; |
6133 | } | |
6134 | ||
6135 | path->slots[0]--; | |
6136 | ||
6137 | leaf = path->nodes[0]; | |
6138 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6139 | ||
4c570655 | 6140 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6141 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6142 | inode->index_cnt = 2; |
aec7477b JB |
6143 | goto out; |
6144 | } | |
6145 | ||
4c570655 | 6146 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6147 | out: |
6148 | btrfs_free_path(path); | |
6149 | return ret; | |
6150 | } | |
6151 | ||
d352ac68 CM |
6152 | /* |
6153 | * helper to find a free sequence number in a given directory. This current | |
6154 | * code is very simple, later versions will do smarter things in the btree | |
6155 | */ | |
877574e2 | 6156 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6157 | { |
6158 | int ret = 0; | |
6159 | ||
877574e2 NB |
6160 | if (dir->index_cnt == (u64)-1) { |
6161 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6162 | if (ret) { |
6163 | ret = btrfs_set_inode_index_count(dir); | |
6164 | if (ret) | |
6165 | return ret; | |
6166 | } | |
aec7477b JB |
6167 | } |
6168 | ||
877574e2 NB |
6169 | *index = dir->index_cnt; |
6170 | dir->index_cnt++; | |
aec7477b JB |
6171 | |
6172 | return ret; | |
6173 | } | |
6174 | ||
b0d5d10f CM |
6175 | static int btrfs_insert_inode_locked(struct inode *inode) |
6176 | { | |
6177 | struct btrfs_iget_args args; | |
6178 | args.location = &BTRFS_I(inode)->location; | |
6179 | args.root = BTRFS_I(inode)->root; | |
6180 | ||
6181 | return insert_inode_locked4(inode, | |
6182 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6183 | btrfs_find_actor, &args); | |
6184 | } | |
6185 | ||
39279cc3 CM |
6186 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6187 | struct btrfs_root *root, | |
aec7477b | 6188 | struct inode *dir, |
9c58309d | 6189 | const char *name, int name_len, |
175a4eb7 AV |
6190 | u64 ref_objectid, u64 objectid, |
6191 | umode_t mode, u64 *index) | |
39279cc3 | 6192 | { |
0b246afa | 6193 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6194 | struct inode *inode; |
5f39d397 | 6195 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6196 | struct btrfs_key *location; |
5f39d397 | 6197 | struct btrfs_path *path; |
9c58309d CM |
6198 | struct btrfs_inode_ref *ref; |
6199 | struct btrfs_key key[2]; | |
6200 | u32 sizes[2]; | |
ef3b9af5 | 6201 | int nitems = name ? 2 : 1; |
9c58309d | 6202 | unsigned long ptr; |
39279cc3 | 6203 | int ret; |
39279cc3 | 6204 | |
5f39d397 | 6205 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6206 | if (!path) |
6207 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6208 | |
0b246afa | 6209 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6210 | if (!inode) { |
6211 | btrfs_free_path(path); | |
39279cc3 | 6212 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6213 | } |
39279cc3 | 6214 | |
5762b5c9 FM |
6215 | /* |
6216 | * O_TMPFILE, set link count to 0, so that after this point, | |
6217 | * we fill in an inode item with the correct link count. | |
6218 | */ | |
6219 | if (!name) | |
6220 | set_nlink(inode, 0); | |
6221 | ||
581bb050 LZ |
6222 | /* |
6223 | * we have to initialize this early, so we can reclaim the inode | |
6224 | * number if we fail afterwards in this function. | |
6225 | */ | |
6226 | inode->i_ino = objectid; | |
6227 | ||
ef3b9af5 | 6228 | if (dir && name) { |
1abe9b8a | 6229 | trace_btrfs_inode_request(dir); |
6230 | ||
877574e2 | 6231 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6232 | if (ret) { |
8fb27640 | 6233 | btrfs_free_path(path); |
09771430 | 6234 | iput(inode); |
aec7477b | 6235 | return ERR_PTR(ret); |
09771430 | 6236 | } |
ef3b9af5 FM |
6237 | } else if (dir) { |
6238 | *index = 0; | |
aec7477b JB |
6239 | } |
6240 | /* | |
6241 | * index_cnt is ignored for everything but a dir, | |
6242 | * btrfs_get_inode_index_count has an explanation for the magic | |
6243 | * number | |
6244 | */ | |
6245 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6246 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6247 | BTRFS_I(inode)->root = root; |
e02119d5 | 6248 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6249 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6250 | |
5dc562c5 JB |
6251 | /* |
6252 | * We could have gotten an inode number from somebody who was fsynced | |
6253 | * and then removed in this same transaction, so let's just set full | |
6254 | * sync since it will be a full sync anyway and this will blow away the | |
6255 | * old info in the log. | |
6256 | */ | |
6257 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6258 | ||
9c58309d | 6259 | key[0].objectid = objectid; |
962a298f | 6260 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6261 | key[0].offset = 0; |
6262 | ||
9c58309d | 6263 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6264 | |
6265 | if (name) { | |
6266 | /* | |
6267 | * Start new inodes with an inode_ref. This is slightly more | |
6268 | * efficient for small numbers of hard links since they will | |
6269 | * be packed into one item. Extended refs will kick in if we | |
6270 | * add more hard links than can fit in the ref item. | |
6271 | */ | |
6272 | key[1].objectid = objectid; | |
962a298f | 6273 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6274 | key[1].offset = ref_objectid; |
6275 | ||
6276 | sizes[1] = name_len + sizeof(*ref); | |
6277 | } | |
9c58309d | 6278 | |
b0d5d10f CM |
6279 | location = &BTRFS_I(inode)->location; |
6280 | location->objectid = objectid; | |
6281 | location->offset = 0; | |
962a298f | 6282 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6283 | |
6284 | ret = btrfs_insert_inode_locked(inode); | |
6285 | if (ret < 0) | |
6286 | goto fail; | |
6287 | ||
b9473439 | 6288 | path->leave_spinning = 1; |
ef3b9af5 | 6289 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6290 | if (ret != 0) |
b0d5d10f | 6291 | goto fail_unlock; |
5f39d397 | 6292 | |
ecc11fab | 6293 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6294 | inode_set_bytes(inode, 0); |
9cc97d64 | 6295 | |
c2050a45 | 6296 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6297 | inode->i_atime = inode->i_mtime; |
6298 | inode->i_ctime = inode->i_mtime; | |
6299 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6300 | ||
5f39d397 CM |
6301 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6302 | struct btrfs_inode_item); | |
b159fa28 | 6303 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6304 | sizeof(*inode_item)); |
e02119d5 | 6305 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6306 | |
ef3b9af5 FM |
6307 | if (name) { |
6308 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6309 | struct btrfs_inode_ref); | |
6310 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6311 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6312 | ptr = (unsigned long)(ref + 1); | |
6313 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6314 | } | |
9c58309d | 6315 | |
5f39d397 CM |
6316 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6317 | btrfs_free_path(path); | |
6318 | ||
6cbff00f CH |
6319 | btrfs_inherit_iflags(inode, dir); |
6320 | ||
569254b0 | 6321 | if (S_ISREG(mode)) { |
0b246afa | 6322 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6323 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6324 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6325 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6326 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6327 | } |
6328 | ||
5d4f98a2 | 6329 | inode_tree_add(inode); |
1abe9b8a | 6330 | |
6331 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6332 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6333 | |
8ea05e3a AB |
6334 | btrfs_update_root_times(trans, root); |
6335 | ||
63541927 FDBM |
6336 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6337 | if (ret) | |
0b246afa | 6338 | btrfs_err(fs_info, |
63541927 | 6339 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6340 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6341 | |
39279cc3 | 6342 | return inode; |
b0d5d10f CM |
6343 | |
6344 | fail_unlock: | |
6345 | unlock_new_inode(inode); | |
5f39d397 | 6346 | fail: |
ef3b9af5 | 6347 | if (dir && name) |
aec7477b | 6348 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6349 | btrfs_free_path(path); |
09771430 | 6350 | iput(inode); |
5f39d397 | 6351 | return ERR_PTR(ret); |
39279cc3 CM |
6352 | } |
6353 | ||
6354 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6355 | { | |
6356 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6357 | } | |
6358 | ||
d352ac68 CM |
6359 | /* |
6360 | * utility function to add 'inode' into 'parent_inode' with | |
6361 | * a give name and a given sequence number. | |
6362 | * if 'add_backref' is true, also insert a backref from the | |
6363 | * inode to the parent directory. | |
6364 | */ | |
e02119d5 | 6365 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6366 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6367 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6368 | { |
db0a669f | 6369 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
4df27c4d | 6370 | int ret = 0; |
39279cc3 | 6371 | struct btrfs_key key; |
db0a669f NB |
6372 | struct btrfs_root *root = parent_inode->root; |
6373 | u64 ino = btrfs_ino(inode); | |
6374 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6375 | |
33345d01 | 6376 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6377 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6378 | } else { |
33345d01 | 6379 | key.objectid = ino; |
962a298f | 6380 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6381 | key.offset = 0; |
6382 | } | |
6383 | ||
33345d01 | 6384 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
0b246afa JM |
6385 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
6386 | root->root_key.objectid, parent_ino, | |
6387 | index, name, name_len); | |
4df27c4d | 6388 | } else if (add_backref) { |
33345d01 LZ |
6389 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6390 | parent_ino, index); | |
4df27c4d | 6391 | } |
39279cc3 | 6392 | |
79787eaa JM |
6393 | /* Nothing to clean up yet */ |
6394 | if (ret) | |
6395 | return ret; | |
4df27c4d | 6396 | |
79787eaa JM |
6397 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6398 | parent_inode, &key, | |
db0a669f | 6399 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6400 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6401 | goto fail_dir_item; |
6402 | else if (ret) { | |
66642832 | 6403 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6404 | return ret; |
39279cc3 | 6405 | } |
79787eaa | 6406 | |
db0a669f | 6407 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6408 | name_len * 2); |
db0a669f NB |
6409 | inode_inc_iversion(&parent_inode->vfs_inode); |
6410 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6411 | current_time(&parent_inode->vfs_inode); | |
6412 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
79787eaa | 6413 | if (ret) |
66642832 | 6414 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6415 | return ret; |
fe66a05a CM |
6416 | |
6417 | fail_dir_item: | |
6418 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6419 | u64 local_index; | |
6420 | int err; | |
0b246afa JM |
6421 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
6422 | root->root_key.objectid, parent_ino, | |
6423 | &local_index, name, name_len); | |
fe66a05a CM |
6424 | |
6425 | } else if (add_backref) { | |
6426 | u64 local_index; | |
6427 | int err; | |
6428 | ||
6429 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6430 | ino, parent_ino, &local_index); | |
6431 | } | |
6432 | return ret; | |
39279cc3 CM |
6433 | } |
6434 | ||
6435 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6436 | struct btrfs_inode *dir, struct dentry *dentry, |
6437 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6438 | { |
a1b075d2 JB |
6439 | int err = btrfs_add_link(trans, dir, inode, |
6440 | dentry->d_name.name, dentry->d_name.len, | |
6441 | backref, index); | |
39279cc3 CM |
6442 | if (err > 0) |
6443 | err = -EEXIST; | |
6444 | return err; | |
6445 | } | |
6446 | ||
618e21d5 | 6447 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6448 | umode_t mode, dev_t rdev) |
618e21d5 | 6449 | { |
2ff7e61e | 6450 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6451 | struct btrfs_trans_handle *trans; |
6452 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6453 | struct inode *inode = NULL; |
618e21d5 JB |
6454 | int err; |
6455 | int drop_inode = 0; | |
6456 | u64 objectid; | |
00e4e6b3 | 6457 | u64 index = 0; |
618e21d5 | 6458 | |
9ed74f2d JB |
6459 | /* |
6460 | * 2 for inode item and ref | |
6461 | * 2 for dir items | |
6462 | * 1 for xattr if selinux is on | |
6463 | */ | |
a22285a6 YZ |
6464 | trans = btrfs_start_transaction(root, 5); |
6465 | if (IS_ERR(trans)) | |
6466 | return PTR_ERR(trans); | |
1832a6d5 | 6467 | |
581bb050 LZ |
6468 | err = btrfs_find_free_ino(root, &objectid); |
6469 | if (err) | |
6470 | goto out_unlock; | |
6471 | ||
aec7477b | 6472 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6473 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6474 | mode, &index); | |
7cf96da3 TI |
6475 | if (IS_ERR(inode)) { |
6476 | err = PTR_ERR(inode); | |
618e21d5 | 6477 | goto out_unlock; |
7cf96da3 | 6478 | } |
618e21d5 | 6479 | |
ad19db71 CS |
6480 | /* |
6481 | * If the active LSM wants to access the inode during | |
6482 | * d_instantiate it needs these. Smack checks to see | |
6483 | * if the filesystem supports xattrs by looking at the | |
6484 | * ops vector. | |
6485 | */ | |
ad19db71 | 6486 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6487 | init_special_inode(inode, inode->i_mode, rdev); |
6488 | ||
6489 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6490 | if (err) |
b0d5d10f CM |
6491 | goto out_unlock_inode; |
6492 | ||
cef415af NB |
6493 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6494 | 0, index); | |
b0d5d10f CM |
6495 | if (err) { |
6496 | goto out_unlock_inode; | |
6497 | } else { | |
1b4ab1bb | 6498 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6499 | unlock_new_inode(inode); |
08c422c2 | 6500 | d_instantiate(dentry, inode); |
618e21d5 | 6501 | } |
b0d5d10f | 6502 | |
618e21d5 | 6503 | out_unlock: |
3a45bb20 | 6504 | btrfs_end_transaction(trans); |
2ff7e61e JM |
6505 | btrfs_balance_delayed_items(fs_info); |
6506 | btrfs_btree_balance_dirty(fs_info); | |
618e21d5 JB |
6507 | if (drop_inode) { |
6508 | inode_dec_link_count(inode); | |
6509 | iput(inode); | |
6510 | } | |
618e21d5 | 6511 | return err; |
b0d5d10f CM |
6512 | |
6513 | out_unlock_inode: | |
6514 | drop_inode = 1; | |
6515 | unlock_new_inode(inode); | |
6516 | goto out_unlock; | |
6517 | ||
618e21d5 JB |
6518 | } |
6519 | ||
39279cc3 | 6520 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6521 | umode_t mode, bool excl) |
39279cc3 | 6522 | { |
2ff7e61e | 6523 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6524 | struct btrfs_trans_handle *trans; |
6525 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6526 | struct inode *inode = NULL; |
43baa579 | 6527 | int drop_inode_on_err = 0; |
a22285a6 | 6528 | int err; |
39279cc3 | 6529 | u64 objectid; |
00e4e6b3 | 6530 | u64 index = 0; |
39279cc3 | 6531 | |
9ed74f2d JB |
6532 | /* |
6533 | * 2 for inode item and ref | |
6534 | * 2 for dir items | |
6535 | * 1 for xattr if selinux is on | |
6536 | */ | |
a22285a6 YZ |
6537 | trans = btrfs_start_transaction(root, 5); |
6538 | if (IS_ERR(trans)) | |
6539 | return PTR_ERR(trans); | |
9ed74f2d | 6540 | |
581bb050 LZ |
6541 | err = btrfs_find_free_ino(root, &objectid); |
6542 | if (err) | |
6543 | goto out_unlock; | |
6544 | ||
aec7477b | 6545 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6546 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6547 | mode, &index); | |
7cf96da3 TI |
6548 | if (IS_ERR(inode)) { |
6549 | err = PTR_ERR(inode); | |
39279cc3 | 6550 | goto out_unlock; |
7cf96da3 | 6551 | } |
43baa579 | 6552 | drop_inode_on_err = 1; |
ad19db71 CS |
6553 | /* |
6554 | * If the active LSM wants to access the inode during | |
6555 | * d_instantiate it needs these. Smack checks to see | |
6556 | * if the filesystem supports xattrs by looking at the | |
6557 | * ops vector. | |
6558 | */ | |
6559 | inode->i_fop = &btrfs_file_operations; | |
6560 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6561 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6562 | |
6563 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6564 | if (err) | |
6565 | goto out_unlock_inode; | |
6566 | ||
6567 | err = btrfs_update_inode(trans, root, inode); | |
6568 | if (err) | |
6569 | goto out_unlock_inode; | |
ad19db71 | 6570 | |
cef415af NB |
6571 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6572 | 0, index); | |
39279cc3 | 6573 | if (err) |
b0d5d10f | 6574 | goto out_unlock_inode; |
43baa579 | 6575 | |
43baa579 | 6576 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6577 | unlock_new_inode(inode); |
43baa579 FB |
6578 | d_instantiate(dentry, inode); |
6579 | ||
39279cc3 | 6580 | out_unlock: |
3a45bb20 | 6581 | btrfs_end_transaction(trans); |
43baa579 | 6582 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6583 | inode_dec_link_count(inode); |
6584 | iput(inode); | |
6585 | } | |
2ff7e61e JM |
6586 | btrfs_balance_delayed_items(fs_info); |
6587 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6588 | return err; |
b0d5d10f CM |
6589 | |
6590 | out_unlock_inode: | |
6591 | unlock_new_inode(inode); | |
6592 | goto out_unlock; | |
6593 | ||
39279cc3 CM |
6594 | } |
6595 | ||
6596 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6597 | struct dentry *dentry) | |
6598 | { | |
271dba45 | 6599 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6600 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6601 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6602 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6603 | u64 index; |
39279cc3 CM |
6604 | int err; |
6605 | int drop_inode = 0; | |
6606 | ||
4a8be425 TH |
6607 | /* do not allow sys_link's with other subvols of the same device */ |
6608 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6609 | return -EXDEV; |
4a8be425 | 6610 | |
f186373f | 6611 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6612 | return -EMLINK; |
4a8be425 | 6613 | |
877574e2 | 6614 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6615 | if (err) |
6616 | goto fail; | |
6617 | ||
a22285a6 | 6618 | /* |
7e6b6465 | 6619 | * 2 items for inode and inode ref |
a22285a6 | 6620 | * 2 items for dir items |
7e6b6465 | 6621 | * 1 item for parent inode |
a22285a6 | 6622 | */ |
7e6b6465 | 6623 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6624 | if (IS_ERR(trans)) { |
6625 | err = PTR_ERR(trans); | |
271dba45 | 6626 | trans = NULL; |
a22285a6 YZ |
6627 | goto fail; |
6628 | } | |
5f39d397 | 6629 | |
67de1176 MX |
6630 | /* There are several dir indexes for this inode, clear the cache. */ |
6631 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6632 | inc_nlink(inode); |
0c4d2d95 | 6633 | inode_inc_iversion(inode); |
c2050a45 | 6634 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6635 | ihold(inode); |
e9976151 | 6636 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6637 | |
cef415af NB |
6638 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6639 | 1, index); | |
5f39d397 | 6640 | |
a5719521 | 6641 | if (err) { |
54aa1f4d | 6642 | drop_inode = 1; |
a5719521 | 6643 | } else { |
10d9f309 | 6644 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6645 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6646 | if (err) |
6647 | goto fail; | |
ef3b9af5 FM |
6648 | if (inode->i_nlink == 1) { |
6649 | /* | |
6650 | * If new hard link count is 1, it's a file created | |
6651 | * with open(2) O_TMPFILE flag. | |
6652 | */ | |
3d6ae7bb | 6653 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6654 | if (err) |
6655 | goto fail; | |
6656 | } | |
08c422c2 | 6657 | d_instantiate(dentry, inode); |
9ca5fbfb | 6658 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6659 | } |
39279cc3 | 6660 | |
2ff7e61e | 6661 | btrfs_balance_delayed_items(fs_info); |
1832a6d5 | 6662 | fail: |
271dba45 | 6663 | if (trans) |
3a45bb20 | 6664 | btrfs_end_transaction(trans); |
39279cc3 CM |
6665 | if (drop_inode) { |
6666 | inode_dec_link_count(inode); | |
6667 | iput(inode); | |
6668 | } | |
2ff7e61e | 6669 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6670 | return err; |
6671 | } | |
6672 | ||
18bb1db3 | 6673 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6674 | { |
2ff7e61e | 6675 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6676 | struct inode *inode = NULL; |
39279cc3 CM |
6677 | struct btrfs_trans_handle *trans; |
6678 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6679 | int err = 0; | |
6680 | int drop_on_err = 0; | |
b9d86667 | 6681 | u64 objectid = 0; |
00e4e6b3 | 6682 | u64 index = 0; |
39279cc3 | 6683 | |
9ed74f2d JB |
6684 | /* |
6685 | * 2 items for inode and ref | |
6686 | * 2 items for dir items | |
6687 | * 1 for xattr if selinux is on | |
6688 | */ | |
a22285a6 YZ |
6689 | trans = btrfs_start_transaction(root, 5); |
6690 | if (IS_ERR(trans)) | |
6691 | return PTR_ERR(trans); | |
39279cc3 | 6692 | |
581bb050 LZ |
6693 | err = btrfs_find_free_ino(root, &objectid); |
6694 | if (err) | |
6695 | goto out_fail; | |
6696 | ||
aec7477b | 6697 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6698 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6699 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6700 | if (IS_ERR(inode)) { |
6701 | err = PTR_ERR(inode); | |
6702 | goto out_fail; | |
6703 | } | |
5f39d397 | 6704 | |
39279cc3 | 6705 | drop_on_err = 1; |
b0d5d10f CM |
6706 | /* these must be set before we unlock the inode */ |
6707 | inode->i_op = &btrfs_dir_inode_operations; | |
6708 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6709 | |
2a7dba39 | 6710 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6711 | if (err) |
b0d5d10f | 6712 | goto out_fail_inode; |
39279cc3 | 6713 | |
6ef06d27 | 6714 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6715 | err = btrfs_update_inode(trans, root, inode); |
6716 | if (err) | |
b0d5d10f | 6717 | goto out_fail_inode; |
5f39d397 | 6718 | |
db0a669f NB |
6719 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6720 | dentry->d_name.name, | |
6721 | dentry->d_name.len, 0, index); | |
39279cc3 | 6722 | if (err) |
b0d5d10f | 6723 | goto out_fail_inode; |
5f39d397 | 6724 | |
39279cc3 | 6725 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6726 | /* |
6727 | * mkdir is special. We're unlocking after we call d_instantiate | |
6728 | * to avoid a race with nfsd calling d_instantiate. | |
6729 | */ | |
6730 | unlock_new_inode(inode); | |
39279cc3 | 6731 | drop_on_err = 0; |
39279cc3 CM |
6732 | |
6733 | out_fail: | |
3a45bb20 | 6734 | btrfs_end_transaction(trans); |
c7cfb8a5 WS |
6735 | if (drop_on_err) { |
6736 | inode_dec_link_count(inode); | |
39279cc3 | 6737 | iput(inode); |
c7cfb8a5 | 6738 | } |
2ff7e61e JM |
6739 | btrfs_balance_delayed_items(fs_info); |
6740 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6741 | return err; |
b0d5d10f CM |
6742 | |
6743 | out_fail_inode: | |
6744 | unlock_new_inode(inode); | |
6745 | goto out_fail; | |
39279cc3 CM |
6746 | } |
6747 | ||
e6c4efd8 QW |
6748 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6749 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6750 | { | |
6751 | struct rb_node *next; | |
6752 | ||
6753 | next = rb_next(&em->rb_node); | |
6754 | if (!next) | |
6755 | return NULL; | |
6756 | return container_of(next, struct extent_map, rb_node); | |
6757 | } | |
6758 | ||
6759 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6760 | { | |
6761 | struct rb_node *prev; | |
6762 | ||
6763 | prev = rb_prev(&em->rb_node); | |
6764 | if (!prev) | |
6765 | return NULL; | |
6766 | return container_of(prev, struct extent_map, rb_node); | |
6767 | } | |
6768 | ||
d352ac68 | 6769 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6770 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6771 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6772 | * the best fitted new extent into the tree. |
d352ac68 | 6773 | */ |
3b951516 CM |
6774 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6775 | struct extent_map *existing, | |
e6dcd2dc | 6776 | struct extent_map *em, |
51f395ad | 6777 | u64 map_start) |
3b951516 | 6778 | { |
e6c4efd8 QW |
6779 | struct extent_map *prev; |
6780 | struct extent_map *next; | |
6781 | u64 start; | |
6782 | u64 end; | |
3b951516 | 6783 | u64 start_diff; |
3b951516 | 6784 | |
e6dcd2dc | 6785 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6786 | |
6787 | if (existing->start > map_start) { | |
6788 | next = existing; | |
6789 | prev = prev_extent_map(next); | |
6790 | } else { | |
6791 | prev = existing; | |
6792 | next = next_extent_map(prev); | |
6793 | } | |
6794 | ||
6795 | start = prev ? extent_map_end(prev) : em->start; | |
6796 | start = max_t(u64, start, em->start); | |
6797 | end = next ? next->start : extent_map_end(em); | |
6798 | end = min_t(u64, end, extent_map_end(em)); | |
6799 | start_diff = start - em->start; | |
6800 | em->start = start; | |
6801 | em->len = end - start; | |
c8b97818 CM |
6802 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6803 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6804 | em->block_start += start_diff; |
c8b97818 CM |
6805 | em->block_len -= start_diff; |
6806 | } | |
09a2a8f9 | 6807 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6808 | } |
6809 | ||
c8b97818 | 6810 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6811 | struct page *page, |
c8b97818 CM |
6812 | size_t pg_offset, u64 extent_offset, |
6813 | struct btrfs_file_extent_item *item) | |
6814 | { | |
6815 | int ret; | |
6816 | struct extent_buffer *leaf = path->nodes[0]; | |
6817 | char *tmp; | |
6818 | size_t max_size; | |
6819 | unsigned long inline_size; | |
6820 | unsigned long ptr; | |
261507a0 | 6821 | int compress_type; |
c8b97818 CM |
6822 | |
6823 | WARN_ON(pg_offset != 0); | |
261507a0 | 6824 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6825 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6826 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6827 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6828 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6829 | if (!tmp) |
6830 | return -ENOMEM; | |
c8b97818 CM |
6831 | ptr = btrfs_file_extent_inline_start(item); |
6832 | ||
6833 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6834 | ||
09cbfeaf | 6835 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6836 | ret = btrfs_decompress(compress_type, tmp, page, |
6837 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6838 | |
6839 | /* | |
6840 | * decompression code contains a memset to fill in any space between the end | |
6841 | * of the uncompressed data and the end of max_size in case the decompressed | |
6842 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6843 | * the end of an inline extent and the beginning of the next block, so we | |
6844 | * cover that region here. | |
6845 | */ | |
6846 | ||
6847 | if (max_size + pg_offset < PAGE_SIZE) { | |
6848 | char *map = kmap(page); | |
6849 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6850 | kunmap(page); | |
6851 | } | |
c8b97818 | 6852 | kfree(tmp); |
166ae5a4 | 6853 | return ret; |
c8b97818 CM |
6854 | } |
6855 | ||
d352ac68 CM |
6856 | /* |
6857 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6858 | * the ugly parts come from merging extents from the disk with the in-ram |
6859 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6860 | * where the in-ram extents might be locked pending data=ordered completion. |
6861 | * | |
6862 | * This also copies inline extents directly into the page. | |
6863 | */ | |
fc4f21b1 NB |
6864 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
6865 | struct page *page, | |
6866 | size_t pg_offset, u64 start, u64 len, | |
6867 | int create) | |
a52d9a80 | 6868 | { |
fc4f21b1 | 6869 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
a52d9a80 CM |
6870 | int ret; |
6871 | int err = 0; | |
a52d9a80 CM |
6872 | u64 extent_start = 0; |
6873 | u64 extent_end = 0; | |
fc4f21b1 | 6874 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6875 | u32 found_type; |
f421950f | 6876 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6877 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6878 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6879 | struct extent_buffer *leaf; |
6880 | struct btrfs_key found_key; | |
a52d9a80 | 6881 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6882 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6883 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6884 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6885 | const bool new_inline = !page || create; |
a52d9a80 | 6886 | |
a52d9a80 | 6887 | again: |
890871be | 6888 | read_lock(&em_tree->lock); |
d1310b2e | 6889 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6890 | if (em) |
0b246afa | 6891 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6892 | read_unlock(&em_tree->lock); |
d1310b2e | 6893 | |
a52d9a80 | 6894 | if (em) { |
e1c4b745 CM |
6895 | if (em->start > start || em->start + em->len <= start) |
6896 | free_extent_map(em); | |
6897 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6898 | free_extent_map(em); |
6899 | else | |
6900 | goto out; | |
a52d9a80 | 6901 | } |
172ddd60 | 6902 | em = alloc_extent_map(); |
a52d9a80 | 6903 | if (!em) { |
d1310b2e CM |
6904 | err = -ENOMEM; |
6905 | goto out; | |
a52d9a80 | 6906 | } |
0b246afa | 6907 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 6908 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6909 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6910 | em->len = (u64)-1; |
c8b97818 | 6911 | em->block_len = (u64)-1; |
f421950f CM |
6912 | |
6913 | if (!path) { | |
6914 | path = btrfs_alloc_path(); | |
026fd317 JB |
6915 | if (!path) { |
6916 | err = -ENOMEM; | |
6917 | goto out; | |
6918 | } | |
6919 | /* | |
6920 | * Chances are we'll be called again, so go ahead and do | |
6921 | * readahead | |
6922 | */ | |
e4058b54 | 6923 | path->reada = READA_FORWARD; |
f421950f CM |
6924 | } |
6925 | ||
179e29e4 CM |
6926 | ret = btrfs_lookup_file_extent(trans, root, path, |
6927 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6928 | if (ret < 0) { |
6929 | err = ret; | |
6930 | goto out; | |
6931 | } | |
6932 | ||
6933 | if (ret != 0) { | |
6934 | if (path->slots[0] == 0) | |
6935 | goto not_found; | |
6936 | path->slots[0]--; | |
6937 | } | |
6938 | ||
5f39d397 CM |
6939 | leaf = path->nodes[0]; |
6940 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6941 | struct btrfs_file_extent_item); |
a52d9a80 | 6942 | /* are we inside the extent that was found? */ |
5f39d397 | 6943 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6944 | found_type = found_key.type; |
5f39d397 | 6945 | if (found_key.objectid != objectid || |
a52d9a80 | 6946 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6947 | /* |
6948 | * If we backup past the first extent we want to move forward | |
6949 | * and see if there is an extent in front of us, otherwise we'll | |
6950 | * say there is a hole for our whole search range which can | |
6951 | * cause problems. | |
6952 | */ | |
6953 | extent_end = start; | |
6954 | goto next; | |
a52d9a80 CM |
6955 | } |
6956 | ||
5f39d397 CM |
6957 | found_type = btrfs_file_extent_type(leaf, item); |
6958 | extent_start = found_key.offset; | |
d899e052 YZ |
6959 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6960 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6961 | extent_end = extent_start + |
db94535d | 6962 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
6963 | |
6964 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
6965 | extent_start); | |
9036c102 YZ |
6966 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6967 | size_t size; | |
514ac8ad | 6968 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
da17066c | 6969 | extent_end = ALIGN(extent_start + size, |
0b246afa | 6970 | fs_info->sectorsize); |
09ed2f16 LB |
6971 | |
6972 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
6973 | path->slots[0], | |
6974 | extent_start); | |
9036c102 | 6975 | } |
25a50341 | 6976 | next: |
9036c102 YZ |
6977 | if (start >= extent_end) { |
6978 | path->slots[0]++; | |
6979 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6980 | ret = btrfs_next_leaf(root, path); | |
6981 | if (ret < 0) { | |
6982 | err = ret; | |
6983 | goto out; | |
a52d9a80 | 6984 | } |
9036c102 YZ |
6985 | if (ret > 0) |
6986 | goto not_found; | |
6987 | leaf = path->nodes[0]; | |
a52d9a80 | 6988 | } |
9036c102 YZ |
6989 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6990 | if (found_key.objectid != objectid || | |
6991 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6992 | goto not_found; | |
6993 | if (start + len <= found_key.offset) | |
6994 | goto not_found; | |
e2eca69d WS |
6995 | if (start > found_key.offset) |
6996 | goto next; | |
9036c102 | 6997 | em->start = start; |
70c8a91c | 6998 | em->orig_start = start; |
9036c102 YZ |
6999 | em->len = found_key.offset - start; |
7000 | goto not_found_em; | |
7001 | } | |
7002 | ||
fc4f21b1 | 7003 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 7004 | new_inline, em); |
7ffbb598 | 7005 | |
d899e052 YZ |
7006 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7007 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
7008 | goto insert; |
7009 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 7010 | unsigned long ptr; |
a52d9a80 | 7011 | char *map; |
3326d1b0 CM |
7012 | size_t size; |
7013 | size_t extent_offset; | |
7014 | size_t copy_size; | |
a52d9a80 | 7015 | |
7ffbb598 | 7016 | if (new_inline) |
689f9346 | 7017 | goto out; |
5f39d397 | 7018 | |
514ac8ad | 7019 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 7020 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7021 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7022 | size - extent_offset); | |
3326d1b0 | 7023 | em->start = extent_start + extent_offset; |
0b246afa | 7024 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7025 | em->orig_block_len = em->len; |
70c8a91c | 7026 | em->orig_start = em->start; |
689f9346 | 7027 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 7028 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
7029 | if (btrfs_file_extent_compression(leaf, item) != |
7030 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7031 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7032 | extent_offset, item); |
166ae5a4 ZB |
7033 | if (ret) { |
7034 | err = ret; | |
7035 | goto out; | |
7036 | } | |
c8b97818 CM |
7037 | } else { |
7038 | map = kmap(page); | |
7039 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7040 | copy_size); | |
09cbfeaf | 7041 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7042 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7043 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7044 | copy_size); |
7045 | } | |
c8b97818 CM |
7046 | kunmap(page); |
7047 | } | |
179e29e4 CM |
7048 | flush_dcache_page(page); |
7049 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 7050 | BUG(); |
179e29e4 CM |
7051 | if (!trans) { |
7052 | kunmap(page); | |
7053 | free_extent_map(em); | |
7054 | em = NULL; | |
ff5714cc | 7055 | |
b3b4aa74 | 7056 | btrfs_release_path(path); |
7a7eaa40 | 7057 | trans = btrfs_join_transaction(root); |
ff5714cc | 7058 | |
3612b495 TI |
7059 | if (IS_ERR(trans)) |
7060 | return ERR_CAST(trans); | |
179e29e4 CM |
7061 | goto again; |
7062 | } | |
c8b97818 | 7063 | map = kmap(page); |
70dec807 | 7064 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 7065 | copy_size); |
c8b97818 | 7066 | kunmap(page); |
179e29e4 | 7067 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 7068 | } |
d1310b2e | 7069 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7070 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7071 | goto insert; |
a52d9a80 CM |
7072 | } |
7073 | not_found: | |
7074 | em->start = start; | |
70c8a91c | 7075 | em->orig_start = start; |
d1310b2e | 7076 | em->len = len; |
a52d9a80 | 7077 | not_found_em: |
5f39d397 | 7078 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 7079 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 7080 | insert: |
b3b4aa74 | 7081 | btrfs_release_path(path); |
d1310b2e | 7082 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7083 | btrfs_err(fs_info, |
5d163e0e JM |
7084 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7085 | em->start, em->len, start, len); | |
a52d9a80 CM |
7086 | err = -EIO; |
7087 | goto out; | |
7088 | } | |
d1310b2e CM |
7089 | |
7090 | err = 0; | |
890871be | 7091 | write_lock(&em_tree->lock); |
09a2a8f9 | 7092 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7093 | /* it is possible that someone inserted the extent into the tree |
7094 | * while we had the lock dropped. It is also possible that | |
7095 | * an overlapping map exists in the tree | |
7096 | */ | |
a52d9a80 | 7097 | if (ret == -EEXIST) { |
3b951516 | 7098 | struct extent_map *existing; |
e6dcd2dc CM |
7099 | |
7100 | ret = 0; | |
7101 | ||
e6c4efd8 QW |
7102 | existing = search_extent_mapping(em_tree, start, len); |
7103 | /* | |
7104 | * existing will always be non-NULL, since there must be | |
7105 | * extent causing the -EEXIST. | |
7106 | */ | |
8dff9c85 | 7107 | if (existing->start == em->start && |
8e2bd3b7 | 7108 | extent_map_end(existing) >= extent_map_end(em) && |
8dff9c85 CM |
7109 | em->block_start == existing->block_start) { |
7110 | /* | |
8e2bd3b7 OS |
7111 | * The existing extent map already encompasses the |
7112 | * entire extent map we tried to add. | |
8dff9c85 CM |
7113 | */ |
7114 | free_extent_map(em); | |
7115 | em = existing; | |
7116 | err = 0; | |
7117 | ||
7118 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7119 | start <= existing->start) { |
e6c4efd8 QW |
7120 | /* |
7121 | * The existing extent map is the one nearest to | |
7122 | * the [start, start + len) range which overlaps | |
7123 | */ | |
7124 | err = merge_extent_mapping(em_tree, existing, | |
7125 | em, start); | |
e1c4b745 | 7126 | free_extent_map(existing); |
e6c4efd8 | 7127 | if (err) { |
3b951516 CM |
7128 | free_extent_map(em); |
7129 | em = NULL; | |
7130 | } | |
7131 | } else { | |
7132 | free_extent_map(em); | |
7133 | em = existing; | |
e6dcd2dc | 7134 | err = 0; |
a52d9a80 | 7135 | } |
a52d9a80 | 7136 | } |
890871be | 7137 | write_unlock(&em_tree->lock); |
a52d9a80 | 7138 | out: |
1abe9b8a | 7139 | |
fc4f21b1 | 7140 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7141 | |
527afb44 | 7142 | btrfs_free_path(path); |
a52d9a80 | 7143 | if (trans) { |
3a45bb20 | 7144 | ret = btrfs_end_transaction(trans); |
d397712b | 7145 | if (!err) |
a52d9a80 CM |
7146 | err = ret; |
7147 | } | |
a52d9a80 CM |
7148 | if (err) { |
7149 | free_extent_map(em); | |
a52d9a80 CM |
7150 | return ERR_PTR(err); |
7151 | } | |
79787eaa | 7152 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7153 | return em; |
7154 | } | |
7155 | ||
fc4f21b1 NB |
7156 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
7157 | struct page *page, | |
7158 | size_t pg_offset, u64 start, u64 len, | |
7159 | int create) | |
ec29ed5b CM |
7160 | { |
7161 | struct extent_map *em; | |
7162 | struct extent_map *hole_em = NULL; | |
7163 | u64 range_start = start; | |
7164 | u64 end; | |
7165 | u64 found; | |
7166 | u64 found_end; | |
7167 | int err = 0; | |
7168 | ||
7169 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7170 | if (IS_ERR(em)) | |
7171 | return em; | |
9986277e DC |
7172 | /* |
7173 | * If our em maps to: | |
7174 | * - a hole or | |
7175 | * - a pre-alloc extent, | |
7176 | * there might actually be delalloc bytes behind it. | |
7177 | */ | |
7178 | if (em->block_start != EXTENT_MAP_HOLE && | |
7179 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7180 | return em; | |
7181 | else | |
7182 | hole_em = em; | |
ec29ed5b CM |
7183 | |
7184 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7185 | end = start + len; | |
7186 | if (end < start) | |
7187 | end = (u64)-1; | |
7188 | else | |
7189 | end -= 1; | |
7190 | ||
7191 | em = NULL; | |
7192 | ||
7193 | /* ok, we didn't find anything, lets look for delalloc */ | |
fc4f21b1 | 7194 | found = count_range_bits(&inode->io_tree, &range_start, |
ec29ed5b CM |
7195 | end, len, EXTENT_DELALLOC, 1); |
7196 | found_end = range_start + found; | |
7197 | if (found_end < range_start) | |
7198 | found_end = (u64)-1; | |
7199 | ||
7200 | /* | |
7201 | * we didn't find anything useful, return | |
7202 | * the original results from get_extent() | |
7203 | */ | |
7204 | if (range_start > end || found_end <= start) { | |
7205 | em = hole_em; | |
7206 | hole_em = NULL; | |
7207 | goto out; | |
7208 | } | |
7209 | ||
7210 | /* adjust the range_start to make sure it doesn't | |
7211 | * go backwards from the start they passed in | |
7212 | */ | |
67871254 | 7213 | range_start = max(start, range_start); |
ec29ed5b CM |
7214 | found = found_end - range_start; |
7215 | ||
7216 | if (found > 0) { | |
7217 | u64 hole_start = start; | |
7218 | u64 hole_len = len; | |
7219 | ||
172ddd60 | 7220 | em = alloc_extent_map(); |
ec29ed5b CM |
7221 | if (!em) { |
7222 | err = -ENOMEM; | |
7223 | goto out; | |
7224 | } | |
7225 | /* | |
7226 | * when btrfs_get_extent can't find anything it | |
7227 | * returns one huge hole | |
7228 | * | |
7229 | * make sure what it found really fits our range, and | |
7230 | * adjust to make sure it is based on the start from | |
7231 | * the caller | |
7232 | */ | |
7233 | if (hole_em) { | |
7234 | u64 calc_end = extent_map_end(hole_em); | |
7235 | ||
7236 | if (calc_end <= start || (hole_em->start > end)) { | |
7237 | free_extent_map(hole_em); | |
7238 | hole_em = NULL; | |
7239 | } else { | |
7240 | hole_start = max(hole_em->start, start); | |
7241 | hole_len = calc_end - hole_start; | |
7242 | } | |
7243 | } | |
7244 | em->bdev = NULL; | |
7245 | if (hole_em && range_start > hole_start) { | |
7246 | /* our hole starts before our delalloc, so we | |
7247 | * have to return just the parts of the hole | |
7248 | * that go until the delalloc starts | |
7249 | */ | |
7250 | em->len = min(hole_len, | |
7251 | range_start - hole_start); | |
7252 | em->start = hole_start; | |
7253 | em->orig_start = hole_start; | |
7254 | /* | |
7255 | * don't adjust block start at all, | |
7256 | * it is fixed at EXTENT_MAP_HOLE | |
7257 | */ | |
7258 | em->block_start = hole_em->block_start; | |
7259 | em->block_len = hole_len; | |
f9e4fb53 LB |
7260 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7261 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7262 | } else { |
7263 | em->start = range_start; | |
7264 | em->len = found; | |
7265 | em->orig_start = range_start; | |
7266 | em->block_start = EXTENT_MAP_DELALLOC; | |
7267 | em->block_len = found; | |
7268 | } | |
7269 | } else if (hole_em) { | |
7270 | return hole_em; | |
7271 | } | |
7272 | out: | |
7273 | ||
7274 | free_extent_map(hole_em); | |
7275 | if (err) { | |
7276 | free_extent_map(em); | |
7277 | return ERR_PTR(err); | |
7278 | } | |
7279 | return em; | |
7280 | } | |
7281 | ||
5f9a8a51 FM |
7282 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7283 | const u64 start, | |
7284 | const u64 len, | |
7285 | const u64 orig_start, | |
7286 | const u64 block_start, | |
7287 | const u64 block_len, | |
7288 | const u64 orig_block_len, | |
7289 | const u64 ram_bytes, | |
7290 | const int type) | |
7291 | { | |
7292 | struct extent_map *em = NULL; | |
7293 | int ret; | |
7294 | ||
5f9a8a51 | 7295 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7296 | em = create_io_em(inode, start, len, orig_start, |
7297 | block_start, block_len, orig_block_len, | |
7298 | ram_bytes, | |
7299 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7300 | type); | |
5f9a8a51 FM |
7301 | if (IS_ERR(em)) |
7302 | goto out; | |
7303 | } | |
7304 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7305 | len, block_len, type); | |
7306 | if (ret) { | |
7307 | if (em) { | |
7308 | free_extent_map(em); | |
dcdbc059 | 7309 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7310 | start + len - 1, 0); |
7311 | } | |
7312 | em = ERR_PTR(ret); | |
7313 | } | |
7314 | out: | |
5f9a8a51 FM |
7315 | |
7316 | return em; | |
7317 | } | |
7318 | ||
4b46fce2 JB |
7319 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7320 | u64 start, u64 len) | |
7321 | { | |
0b246afa | 7322 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7323 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7324 | struct extent_map *em; |
4b46fce2 JB |
7325 | struct btrfs_key ins; |
7326 | u64 alloc_hint; | |
7327 | int ret; | |
4b46fce2 | 7328 | |
4b46fce2 | 7329 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7330 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7331 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7332 | if (ret) |
7333 | return ERR_PTR(ret); | |
4b46fce2 | 7334 | |
5f9a8a51 FM |
7335 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7336 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7337 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7338 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7339 | if (IS_ERR(em)) |
2ff7e61e JM |
7340 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7341 | ins.offset, 1); | |
de0ee0ed | 7342 | |
4b46fce2 JB |
7343 | return em; |
7344 | } | |
7345 | ||
46bfbb5c CM |
7346 | /* |
7347 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7348 | * block must be cow'd | |
7349 | */ | |
00361589 | 7350 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7351 | u64 *orig_start, u64 *orig_block_len, |
7352 | u64 *ram_bytes) | |
46bfbb5c | 7353 | { |
2ff7e61e | 7354 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7355 | struct btrfs_path *path; |
7356 | int ret; | |
7357 | struct extent_buffer *leaf; | |
7358 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7359 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7360 | struct btrfs_file_extent_item *fi; |
7361 | struct btrfs_key key; | |
7362 | u64 disk_bytenr; | |
7363 | u64 backref_offset; | |
7364 | u64 extent_end; | |
7365 | u64 num_bytes; | |
7366 | int slot; | |
7367 | int found_type; | |
7ee9e440 | 7368 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7369 | |
46bfbb5c CM |
7370 | path = btrfs_alloc_path(); |
7371 | if (!path) | |
7372 | return -ENOMEM; | |
7373 | ||
f85b7379 DS |
7374 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7375 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7376 | if (ret < 0) |
7377 | goto out; | |
7378 | ||
7379 | slot = path->slots[0]; | |
7380 | if (ret == 1) { | |
7381 | if (slot == 0) { | |
7382 | /* can't find the item, must cow */ | |
7383 | ret = 0; | |
7384 | goto out; | |
7385 | } | |
7386 | slot--; | |
7387 | } | |
7388 | ret = 0; | |
7389 | leaf = path->nodes[0]; | |
7390 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7391 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7392 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7393 | /* not our file or wrong item type, must cow */ | |
7394 | goto out; | |
7395 | } | |
7396 | ||
7397 | if (key.offset > offset) { | |
7398 | /* Wrong offset, must cow */ | |
7399 | goto out; | |
7400 | } | |
7401 | ||
7402 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7403 | found_type = btrfs_file_extent_type(leaf, fi); | |
7404 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7405 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7406 | /* not a regular extent, must cow */ | |
7407 | goto out; | |
7408 | } | |
7ee9e440 JB |
7409 | |
7410 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7411 | goto out; | |
7412 | ||
e77751aa MX |
7413 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7414 | if (extent_end <= offset) | |
7415 | goto out; | |
7416 | ||
46bfbb5c | 7417 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7418 | if (disk_bytenr == 0) |
7419 | goto out; | |
7420 | ||
7421 | if (btrfs_file_extent_compression(leaf, fi) || | |
7422 | btrfs_file_extent_encryption(leaf, fi) || | |
7423 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7424 | goto out; | |
7425 | ||
46bfbb5c CM |
7426 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7427 | ||
7ee9e440 JB |
7428 | if (orig_start) { |
7429 | *orig_start = key.offset - backref_offset; | |
7430 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7431 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7432 | } | |
eb384b55 | 7433 | |
2ff7e61e | 7434 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7435 | goto out; |
7b2b7085 MX |
7436 | |
7437 | num_bytes = min(offset + *len, extent_end) - offset; | |
7438 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7439 | u64 range_end; | |
7440 | ||
da17066c JM |
7441 | range_end = round_up(offset + num_bytes, |
7442 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7443 | ret = test_range_bit(io_tree, offset, range_end, |
7444 | EXTENT_DELALLOC, 0, NULL); | |
7445 | if (ret) { | |
7446 | ret = -EAGAIN; | |
7447 | goto out; | |
7448 | } | |
7449 | } | |
7450 | ||
1bda19eb | 7451 | btrfs_release_path(path); |
46bfbb5c CM |
7452 | |
7453 | /* | |
7454 | * look for other files referencing this extent, if we | |
7455 | * find any we must cow | |
7456 | */ | |
00361589 | 7457 | |
e4c3b2dc | 7458 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7459 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7460 | if (ret) { |
7461 | ret = 0; | |
7462 | goto out; | |
7463 | } | |
46bfbb5c CM |
7464 | |
7465 | /* | |
7466 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7467 | * in this extent we are about to write. If there | |
7468 | * are any csums in that range we have to cow in order | |
7469 | * to keep the csums correct | |
7470 | */ | |
7471 | disk_bytenr += backref_offset; | |
7472 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7473 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7474 | goto out; | |
46bfbb5c CM |
7475 | /* |
7476 | * all of the above have passed, it is safe to overwrite this extent | |
7477 | * without cow | |
7478 | */ | |
eb384b55 | 7479 | *len = num_bytes; |
46bfbb5c CM |
7480 | ret = 1; |
7481 | out: | |
7482 | btrfs_free_path(path); | |
7483 | return ret; | |
7484 | } | |
7485 | ||
fc4adbff AG |
7486 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7487 | { | |
7488 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
e03733da | 7489 | bool found = false; |
fc4adbff AG |
7490 | void **pagep = NULL; |
7491 | struct page *page = NULL; | |
cc2b702c DS |
7492 | unsigned long start_idx; |
7493 | unsigned long end_idx; | |
fc4adbff | 7494 | |
09cbfeaf | 7495 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7496 | |
7497 | /* | |
7498 | * end is the last byte in the last page. end == start is legal | |
7499 | */ | |
09cbfeaf | 7500 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7501 | |
7502 | rcu_read_lock(); | |
7503 | ||
7504 | /* Most of the code in this while loop is lifted from | |
7505 | * find_get_page. It's been modified to begin searching from a | |
7506 | * page and return just the first page found in that range. If the | |
7507 | * found idx is less than or equal to the end idx then we know that | |
7508 | * a page exists. If no pages are found or if those pages are | |
7509 | * outside of the range then we're fine (yay!) */ | |
7510 | while (page == NULL && | |
7511 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7512 | page = radix_tree_deref_slot(pagep); | |
7513 | if (unlikely(!page)) | |
7514 | break; | |
7515 | ||
7516 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7517 | if (radix_tree_deref_retry(page)) { |
7518 | page = NULL; | |
fc4adbff | 7519 | continue; |
809f9016 | 7520 | } |
fc4adbff AG |
7521 | /* |
7522 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7523 | * here as an exceptional entry: so return it without | |
7524 | * attempting to raise page count. | |
7525 | */ | |
6fdef6d4 | 7526 | page = NULL; |
fc4adbff AG |
7527 | break; /* TODO: Is this relevant for this use case? */ |
7528 | } | |
7529 | ||
91405151 FM |
7530 | if (!page_cache_get_speculative(page)) { |
7531 | page = NULL; | |
fc4adbff | 7532 | continue; |
91405151 | 7533 | } |
fc4adbff AG |
7534 | |
7535 | /* | |
7536 | * Has the page moved? | |
7537 | * This is part of the lockless pagecache protocol. See | |
7538 | * include/linux/pagemap.h for details. | |
7539 | */ | |
7540 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7541 | put_page(page); |
fc4adbff AG |
7542 | page = NULL; |
7543 | } | |
7544 | } | |
7545 | ||
7546 | if (page) { | |
7547 | if (page->index <= end_idx) | |
7548 | found = true; | |
09cbfeaf | 7549 | put_page(page); |
fc4adbff AG |
7550 | } |
7551 | ||
7552 | rcu_read_unlock(); | |
7553 | return found; | |
7554 | } | |
7555 | ||
eb838e73 JB |
7556 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7557 | struct extent_state **cached_state, int writing) | |
7558 | { | |
7559 | struct btrfs_ordered_extent *ordered; | |
7560 | int ret = 0; | |
7561 | ||
7562 | while (1) { | |
7563 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7564 | cached_state); |
eb838e73 JB |
7565 | /* |
7566 | * We're concerned with the entire range that we're going to be | |
01327610 | 7567 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7568 | * extents in this range. |
7569 | */ | |
a776c6fa | 7570 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7571 | lockend - lockstart + 1); |
7572 | ||
7573 | /* | |
7574 | * We need to make sure there are no buffered pages in this | |
7575 | * range either, we could have raced between the invalidate in | |
7576 | * generic_file_direct_write and locking the extent. The | |
7577 | * invalidate needs to happen so that reads after a write do not | |
7578 | * get stale data. | |
7579 | */ | |
fc4adbff AG |
7580 | if (!ordered && |
7581 | (!writing || | |
7582 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7583 | break; |
7584 | ||
7585 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7586 | cached_state, GFP_NOFS); | |
7587 | ||
7588 | if (ordered) { | |
ade77029 FM |
7589 | /* |
7590 | * If we are doing a DIO read and the ordered extent we | |
7591 | * found is for a buffered write, we can not wait for it | |
7592 | * to complete and retry, because if we do so we can | |
7593 | * deadlock with concurrent buffered writes on page | |
7594 | * locks. This happens only if our DIO read covers more | |
7595 | * than one extent map, if at this point has already | |
7596 | * created an ordered extent for a previous extent map | |
7597 | * and locked its range in the inode's io tree, and a | |
7598 | * concurrent write against that previous extent map's | |
7599 | * range and this range started (we unlock the ranges | |
7600 | * in the io tree only when the bios complete and | |
7601 | * buffered writes always lock pages before attempting | |
7602 | * to lock range in the io tree). | |
7603 | */ | |
7604 | if (writing || | |
7605 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7606 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7607 | else | |
7608 | ret = -ENOTBLK; | |
eb838e73 JB |
7609 | btrfs_put_ordered_extent(ordered); |
7610 | } else { | |
eb838e73 | 7611 | /* |
b850ae14 FM |
7612 | * We could trigger writeback for this range (and wait |
7613 | * for it to complete) and then invalidate the pages for | |
7614 | * this range (through invalidate_inode_pages2_range()), | |
7615 | * but that can lead us to a deadlock with a concurrent | |
7616 | * call to readpages() (a buffered read or a defrag call | |
7617 | * triggered a readahead) on a page lock due to an | |
7618 | * ordered dio extent we created before but did not have | |
7619 | * yet a corresponding bio submitted (whence it can not | |
7620 | * complete), which makes readpages() wait for that | |
7621 | * ordered extent to complete while holding a lock on | |
7622 | * that page. | |
eb838e73 | 7623 | */ |
b850ae14 | 7624 | ret = -ENOTBLK; |
eb838e73 JB |
7625 | } |
7626 | ||
ade77029 FM |
7627 | if (ret) |
7628 | break; | |
7629 | ||
eb838e73 JB |
7630 | cond_resched(); |
7631 | } | |
7632 | ||
7633 | return ret; | |
7634 | } | |
7635 | ||
6f9994db LB |
7636 | /* The callers of this must take lock_extent() */ |
7637 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7638 | u64 orig_start, u64 block_start, | |
7639 | u64 block_len, u64 orig_block_len, | |
7640 | u64 ram_bytes, int compress_type, | |
7641 | int type) | |
69ffb543 JB |
7642 | { |
7643 | struct extent_map_tree *em_tree; | |
7644 | struct extent_map *em; | |
7645 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7646 | int ret; | |
7647 | ||
6f9994db LB |
7648 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7649 | type == BTRFS_ORDERED_COMPRESSED || | |
7650 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7651 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7652 | |
69ffb543 JB |
7653 | em_tree = &BTRFS_I(inode)->extent_tree; |
7654 | em = alloc_extent_map(); | |
7655 | if (!em) | |
7656 | return ERR_PTR(-ENOMEM); | |
7657 | ||
7658 | em->start = start; | |
7659 | em->orig_start = orig_start; | |
7660 | em->len = len; | |
7661 | em->block_len = block_len; | |
7662 | em->block_start = block_start; | |
7663 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7664 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7665 | em->ram_bytes = ram_bytes; |
70c8a91c | 7666 | em->generation = -1; |
69ffb543 | 7667 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7668 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7669 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7670 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7671 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7672 | em->compress_type = compress_type; | |
7673 | } | |
69ffb543 JB |
7674 | |
7675 | do { | |
dcdbc059 | 7676 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7677 | em->start + em->len - 1, 0); |
7678 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7679 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7680 | write_unlock(&em_tree->lock); |
6f9994db LB |
7681 | /* |
7682 | * The caller has taken lock_extent(), who could race with us | |
7683 | * to add em? | |
7684 | */ | |
69ffb543 JB |
7685 | } while (ret == -EEXIST); |
7686 | ||
7687 | if (ret) { | |
7688 | free_extent_map(em); | |
7689 | return ERR_PTR(ret); | |
7690 | } | |
7691 | ||
6f9994db | 7692 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7693 | return em; |
7694 | } | |
7695 | ||
9c9464cc FM |
7696 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7697 | struct btrfs_dio_data *dio_data, | |
7698 | const u64 len) | |
7699 | { | |
823bb20a | 7700 | unsigned num_extents = count_max_extents(len); |
9c9464cc | 7701 | |
9c9464cc FM |
7702 | /* |
7703 | * If we have an outstanding_extents count still set then we're | |
7704 | * within our reservation, otherwise we need to adjust our inode | |
7705 | * counter appropriately. | |
7706 | */ | |
c2931667 | 7707 | if (dio_data->outstanding_extents >= num_extents) { |
9c9464cc FM |
7708 | dio_data->outstanding_extents -= num_extents; |
7709 | } else { | |
c2931667 LB |
7710 | /* |
7711 | * If dio write length has been split due to no large enough | |
7712 | * contiguous space, we need to compensate our inode counter | |
7713 | * appropriately. | |
7714 | */ | |
7715 | u64 num_needed = num_extents - dio_data->outstanding_extents; | |
7716 | ||
9c9464cc | 7717 | spin_lock(&BTRFS_I(inode)->lock); |
c2931667 | 7718 | BTRFS_I(inode)->outstanding_extents += num_needed; |
9c9464cc FM |
7719 | spin_unlock(&BTRFS_I(inode)->lock); |
7720 | } | |
7721 | } | |
7722 | ||
4b46fce2 JB |
7723 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7724 | struct buffer_head *bh_result, int create) | |
7725 | { | |
0b246afa | 7726 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7727 | struct extent_map *em; |
eb838e73 | 7728 | struct extent_state *cached_state = NULL; |
50745b0a | 7729 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7730 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7731 | u64 lockstart, lockend; |
4b46fce2 | 7732 | u64 len = bh_result->b_size; |
eb838e73 | 7733 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7734 | int ret = 0; |
eb838e73 | 7735 | |
172a5049 | 7736 | if (create) |
3266789f | 7737 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7738 | else |
0b246afa | 7739 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7740 | |
c329861d JB |
7741 | lockstart = start; |
7742 | lockend = start + len - 1; | |
7743 | ||
e1cbbfa5 JB |
7744 | if (current->journal_info) { |
7745 | /* | |
7746 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7747 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7748 | * confused. |
7749 | */ | |
50745b0a | 7750 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7751 | current->journal_info = NULL; |
7752 | } | |
7753 | ||
eb838e73 JB |
7754 | /* |
7755 | * If this errors out it's because we couldn't invalidate pagecache for | |
7756 | * this range and we need to fallback to buffered. | |
7757 | */ | |
9c9464cc FM |
7758 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7759 | create)) { | |
7760 | ret = -ENOTBLK; | |
7761 | goto err; | |
7762 | } | |
eb838e73 | 7763 | |
fc4f21b1 | 7764 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7765 | if (IS_ERR(em)) { |
7766 | ret = PTR_ERR(em); | |
7767 | goto unlock_err; | |
7768 | } | |
4b46fce2 JB |
7769 | |
7770 | /* | |
7771 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7772 | * io. INLINE is special, and we could probably kludge it in here, but | |
7773 | * it's still buffered so for safety lets just fall back to the generic | |
7774 | * buffered path. | |
7775 | * | |
7776 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7777 | * decompress it, so there will be buffering required no matter what we | |
7778 | * do, so go ahead and fallback to buffered. | |
7779 | * | |
01327610 | 7780 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7781 | * to buffered IO. Don't blame me, this is the price we pay for using |
7782 | * the generic code. | |
7783 | */ | |
7784 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7785 | em->block_start == EXTENT_MAP_INLINE) { | |
7786 | free_extent_map(em); | |
eb838e73 JB |
7787 | ret = -ENOTBLK; |
7788 | goto unlock_err; | |
4b46fce2 JB |
7789 | } |
7790 | ||
7791 | /* Just a good old fashioned hole, return */ | |
7792 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7793 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7794 | free_extent_map(em); | |
eb838e73 | 7795 | goto unlock_err; |
4b46fce2 JB |
7796 | } |
7797 | ||
7798 | /* | |
7799 | * We don't allocate a new extent in the following cases | |
7800 | * | |
7801 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7802 | * existing extent. | |
7803 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7804 | * just use the extent. | |
7805 | * | |
7806 | */ | |
46bfbb5c | 7807 | if (!create) { |
eb838e73 JB |
7808 | len = min(len, em->len - (start - em->start)); |
7809 | lockstart = start + len; | |
7810 | goto unlock; | |
46bfbb5c | 7811 | } |
4b46fce2 JB |
7812 | |
7813 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7814 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7815 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7816 | int type; |
eb384b55 | 7817 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7818 | |
7819 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7820 | type = BTRFS_ORDERED_PREALLOC; | |
7821 | else | |
7822 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7823 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7824 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7825 | |
00361589 | 7826 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c | 7827 | &orig_block_len, &ram_bytes) == 1 && |
0b246afa | 7828 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
5f9a8a51 | 7829 | struct extent_map *em2; |
0b901916 | 7830 | |
5f9a8a51 FM |
7831 | em2 = btrfs_create_dio_extent(inode, start, len, |
7832 | orig_start, block_start, | |
7833 | len, orig_block_len, | |
7834 | ram_bytes, type); | |
0b246afa | 7835 | btrfs_dec_nocow_writers(fs_info, block_start); |
69ffb543 JB |
7836 | if (type == BTRFS_ORDERED_PREALLOC) { |
7837 | free_extent_map(em); | |
5f9a8a51 | 7838 | em = em2; |
69ffb543 | 7839 | } |
5f9a8a51 FM |
7840 | if (em2 && IS_ERR(em2)) { |
7841 | ret = PTR_ERR(em2); | |
eb838e73 | 7842 | goto unlock_err; |
46bfbb5c | 7843 | } |
18513091 WX |
7844 | /* |
7845 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7846 | * use the existing or preallocated extent, so does not | |
7847 | * need to adjust btrfs_space_info's bytes_may_use. | |
7848 | */ | |
7849 | btrfs_free_reserved_data_space_noquota(inode, | |
7850 | start, len); | |
46bfbb5c | 7851 | goto unlock; |
4b46fce2 | 7852 | } |
4b46fce2 | 7853 | } |
00361589 | 7854 | |
46bfbb5c CM |
7855 | /* |
7856 | * this will cow the extent, reset the len in case we changed | |
7857 | * it above | |
7858 | */ | |
7859 | len = bh_result->b_size; | |
70c8a91c JB |
7860 | free_extent_map(em); |
7861 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7862 | if (IS_ERR(em)) { |
7863 | ret = PTR_ERR(em); | |
7864 | goto unlock_err; | |
7865 | } | |
46bfbb5c CM |
7866 | len = min(len, em->len - (start - em->start)); |
7867 | unlock: | |
4b46fce2 JB |
7868 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7869 | inode->i_blkbits; | |
46bfbb5c | 7870 | bh_result->b_size = len; |
4b46fce2 JB |
7871 | bh_result->b_bdev = em->bdev; |
7872 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7873 | if (create) { |
7874 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7875 | set_buffer_new(bh_result); | |
7876 | ||
7877 | /* | |
7878 | * Need to update the i_size under the extent lock so buffered | |
7879 | * readers will get the updated i_size when we unlock. | |
7880 | */ | |
4aaedfb0 | 7881 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
c3473e83 | 7882 | i_size_write(inode, start + len); |
0934856d | 7883 | |
9c9464cc | 7884 | adjust_dio_outstanding_extents(inode, dio_data, len); |
50745b0a | 7885 | WARN_ON(dio_data->reserve < len); |
7886 | dio_data->reserve -= len; | |
f28a4928 | 7887 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7888 | current->journal_info = dio_data; |
c3473e83 | 7889 | } |
4b46fce2 | 7890 | |
eb838e73 JB |
7891 | /* |
7892 | * In the case of write we need to clear and unlock the entire range, | |
7893 | * in the case of read we need to unlock only the end area that we | |
7894 | * aren't using if there is any left over space. | |
7895 | */ | |
24c03fa5 | 7896 | if (lockstart < lockend) { |
0934856d MX |
7897 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7898 | lockend, unlock_bits, 1, 0, | |
7899 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7900 | } else { |
eb838e73 | 7901 | free_extent_state(cached_state); |
24c03fa5 | 7902 | } |
eb838e73 | 7903 | |
4b46fce2 JB |
7904 | free_extent_map(em); |
7905 | ||
7906 | return 0; | |
eb838e73 JB |
7907 | |
7908 | unlock_err: | |
eb838e73 JB |
7909 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7910 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7911 | err: |
50745b0a | 7912 | if (dio_data) |
7913 | current->journal_info = dio_data; | |
9c9464cc FM |
7914 | /* |
7915 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7916 | * write less data then expected, so that we don't underflow our inode's | |
7917 | * outstanding extents counter. | |
7918 | */ | |
7919 | if (create && dio_data) | |
7920 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7921 | ||
eb838e73 | 7922 | return ret; |
4b46fce2 JB |
7923 | } |
7924 | ||
8b110e39 | 7925 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
81a75f67 | 7926 | int mirror_num) |
8b110e39 | 7927 | { |
2ff7e61e | 7928 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7929 | int ret; |
7930 | ||
37226b21 | 7931 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
7932 | |
7933 | bio_get(bio); | |
7934 | ||
2ff7e61e | 7935 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 MX |
7936 | if (ret) |
7937 | goto err; | |
7938 | ||
2ff7e61e | 7939 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
8b110e39 MX |
7940 | err: |
7941 | bio_put(bio); | |
7942 | return ret; | |
7943 | } | |
7944 | ||
7945 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7946 | struct bio *failed_bio, | |
7947 | struct io_failure_record *failrec, | |
7948 | int failed_mirror) | |
7949 | { | |
ab8d0fc4 | 7950 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7951 | int num_copies; |
7952 | ||
ab8d0fc4 | 7953 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7954 | if (num_copies == 1) { |
7955 | /* | |
7956 | * we only have a single copy of the data, so don't bother with | |
7957 | * all the retry and error correction code that follows. no | |
7958 | * matter what the error is, it is very likely to persist. | |
7959 | */ | |
ab8d0fc4 JM |
7960 | btrfs_debug(fs_info, |
7961 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7962 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7963 | return 0; |
7964 | } | |
7965 | ||
7966 | failrec->failed_mirror = failed_mirror; | |
7967 | failrec->this_mirror++; | |
7968 | if (failrec->this_mirror == failed_mirror) | |
7969 | failrec->this_mirror++; | |
7970 | ||
7971 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7972 | btrfs_debug(fs_info, |
7973 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7974 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7975 | return 0; |
7976 | } | |
7977 | ||
7978 | return 1; | |
7979 | } | |
7980 | ||
7981 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
2dabb324 CR |
7982 | struct page *page, unsigned int pgoff, |
7983 | u64 start, u64 end, int failed_mirror, | |
7984 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7985 | { |
7986 | struct io_failure_record *failrec; | |
7870d082 JB |
7987 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7988 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
7989 | struct bio *bio; |
7990 | int isector; | |
70fd7614 | 7991 | int read_mode = 0; |
17347cec | 7992 | int segs; |
8b110e39 MX |
7993 | int ret; |
7994 | ||
37226b21 | 7995 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7996 | |
7997 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7998 | if (ret) | |
7999 | return ret; | |
8000 | ||
8001 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
8002 | failed_mirror); | |
8003 | if (!ret) { | |
7870d082 | 8004 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8005 | return -EIO; |
8006 | } | |
8007 | ||
17347cec LB |
8008 | segs = bio_segments(failed_bio); |
8009 | if (segs > 1 || | |
8010 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) | |
70fd7614 | 8011 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
8012 | |
8013 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
8014 | isector >>= inode->i_sb->s_blocksize_bits; | |
8015 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 8016 | pgoff, isector, repair_endio, repair_arg); |
37226b21 | 8017 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
8018 | |
8019 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
913e1535 | 8020 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
8b110e39 MX |
8021 | read_mode, failrec->this_mirror, failrec->in_validation); |
8022 | ||
81a75f67 | 8023 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8b110e39 | 8024 | if (ret) { |
7870d082 | 8025 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8026 | bio_put(bio); |
8027 | } | |
8028 | ||
8029 | return ret; | |
8030 | } | |
8031 | ||
8032 | struct btrfs_retry_complete { | |
8033 | struct completion done; | |
8034 | struct inode *inode; | |
8035 | u64 start; | |
8036 | int uptodate; | |
8037 | }; | |
8038 | ||
4246a0b6 | 8039 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
8040 | { |
8041 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 8042 | struct inode *inode = done->inode; |
8b110e39 | 8043 | struct bio_vec *bvec; |
7870d082 | 8044 | struct extent_io_tree *io_tree, *failure_tree; |
8b110e39 MX |
8045 | int i; |
8046 | ||
4e4cbee9 | 8047 | if (bio->bi_status) |
8b110e39 MX |
8048 | goto end; |
8049 | ||
2dabb324 | 8050 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
8051 | io_tree = &BTRFS_I(inode)->io_tree; |
8052 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8053 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); | |
2dabb324 | 8054 | |
8b110e39 | 8055 | done->uptodate = 1; |
c09abff8 | 8056 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8057 | bio_for_each_segment_all(bvec, bio, i) |
7870d082 JB |
8058 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
8059 | io_tree, done->start, bvec->bv_page, | |
8060 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
8061 | end: |
8062 | complete(&done->done); | |
8063 | bio_put(bio); | |
8064 | } | |
8065 | ||
8066 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
8067 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 8068 | { |
2dabb324 | 8069 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8070 | struct bio_vec bvec; |
8071 | struct bvec_iter iter; | |
8b110e39 | 8072 | struct btrfs_retry_complete done; |
4b46fce2 | 8073 | u64 start; |
2dabb324 CR |
8074 | unsigned int pgoff; |
8075 | u32 sectorsize; | |
8076 | int nr_sectors; | |
c1dc0896 | 8077 | int ret; |
629ebf4f | 8078 | int err = 0; |
4b46fce2 | 8079 | |
2dabb324 | 8080 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8081 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8082 | |
8b110e39 MX |
8083 | start = io_bio->logical; |
8084 | done.inode = inode; | |
17347cec | 8085 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8086 | |
17347cec LB |
8087 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8088 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8089 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
8090 | |
8091 | next_block_or_try_again: | |
8b110e39 MX |
8092 | done.uptodate = 0; |
8093 | done.start = start; | |
8094 | init_completion(&done.done); | |
8095 | ||
17347cec | 8096 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8097 | pgoff, start, start + sectorsize - 1, |
8098 | io_bio->mirror_num, | |
8099 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
8100 | if (ret) { |
8101 | err = ret; | |
8102 | goto next; | |
8103 | } | |
8b110e39 MX |
8104 | |
8105 | wait_for_completion(&done.done); | |
8106 | ||
8107 | if (!done.uptodate) { | |
8108 | /* We might have another mirror, so try again */ | |
2dabb324 | 8109 | goto next_block_or_try_again; |
8b110e39 MX |
8110 | } |
8111 | ||
629ebf4f | 8112 | next: |
2dabb324 CR |
8113 | start += sectorsize; |
8114 | ||
97bf5a55 LB |
8115 | nr_sectors--; |
8116 | if (nr_sectors) { | |
2dabb324 | 8117 | pgoff += sectorsize; |
97bf5a55 | 8118 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8119 | goto next_block_or_try_again; |
8120 | } | |
8b110e39 MX |
8121 | } |
8122 | ||
629ebf4f | 8123 | return err; |
8b110e39 MX |
8124 | } |
8125 | ||
4246a0b6 | 8126 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8127 | { |
8128 | struct btrfs_retry_complete *done = bio->bi_private; | |
8129 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
8130 | struct extent_io_tree *io_tree, *failure_tree; |
8131 | struct inode *inode = done->inode; | |
8b110e39 MX |
8132 | struct bio_vec *bvec; |
8133 | int uptodate; | |
8134 | int ret; | |
8135 | int i; | |
8136 | ||
4e4cbee9 | 8137 | if (bio->bi_status) |
8b110e39 MX |
8138 | goto end; |
8139 | ||
8140 | uptodate = 1; | |
2dabb324 | 8141 | |
2dabb324 | 8142 | ASSERT(bio->bi_vcnt == 1); |
2e949b0a | 8143 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 8144 | |
7870d082 JB |
8145 | io_tree = &BTRFS_I(inode)->io_tree; |
8146 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8147 | ||
c09abff8 | 8148 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8149 | bio_for_each_segment_all(bvec, bio, i) { |
7870d082 JB |
8150 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
8151 | bvec->bv_offset, done->start, | |
8152 | bvec->bv_len); | |
8b110e39 | 8153 | if (!ret) |
7870d082 JB |
8154 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
8155 | failure_tree, io_tree, done->start, | |
8156 | bvec->bv_page, | |
8157 | btrfs_ino(BTRFS_I(inode)), | |
8158 | bvec->bv_offset); | |
8b110e39 MX |
8159 | else |
8160 | uptodate = 0; | |
8161 | } | |
8162 | ||
8163 | done->uptodate = uptodate; | |
8164 | end: | |
8165 | complete(&done->done); | |
8166 | bio_put(bio); | |
8167 | } | |
8168 | ||
4e4cbee9 CH |
8169 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
8170 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 | 8171 | { |
2dabb324 | 8172 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8173 | struct bio_vec bvec; |
8174 | struct bvec_iter iter; | |
8b110e39 MX |
8175 | struct btrfs_retry_complete done; |
8176 | u64 start; | |
8177 | u64 offset = 0; | |
2dabb324 CR |
8178 | u32 sectorsize; |
8179 | int nr_sectors; | |
8180 | unsigned int pgoff; | |
8181 | int csum_pos; | |
ef7cdac1 | 8182 | bool uptodate = (err == 0); |
8b110e39 | 8183 | int ret; |
dc380aea | 8184 | |
2dabb324 | 8185 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8186 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8187 | |
8b110e39 | 8188 | err = 0; |
c1dc0896 | 8189 | start = io_bio->logical; |
8b110e39 | 8190 | done.inode = inode; |
17347cec | 8191 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8192 | |
17347cec LB |
8193 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8194 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 8195 | |
17347cec | 8196 | pgoff = bvec.bv_offset; |
2dabb324 | 8197 | next_block: |
ef7cdac1 LB |
8198 | if (uptodate) { |
8199 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8200 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8201 | bvec.bv_page, pgoff, start, sectorsize); | |
8202 | if (likely(!ret)) | |
8203 | goto next; | |
8204 | } | |
8b110e39 MX |
8205 | try_again: |
8206 | done.uptodate = 0; | |
8207 | done.start = start; | |
8208 | init_completion(&done.done); | |
8209 | ||
17347cec | 8210 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8211 | pgoff, start, start + sectorsize - 1, |
8212 | io_bio->mirror_num, | |
8213 | btrfs_retry_endio, &done); | |
8b110e39 | 8214 | if (ret) { |
4e4cbee9 | 8215 | err = errno_to_blk_status(ret); |
8b110e39 MX |
8216 | goto next; |
8217 | } | |
8218 | ||
8219 | wait_for_completion(&done.done); | |
8220 | ||
8221 | if (!done.uptodate) { | |
8222 | /* We might have another mirror, so try again */ | |
8223 | goto try_again; | |
8224 | } | |
8225 | next: | |
2dabb324 CR |
8226 | offset += sectorsize; |
8227 | start += sectorsize; | |
8228 | ||
8229 | ASSERT(nr_sectors); | |
8230 | ||
97bf5a55 LB |
8231 | nr_sectors--; |
8232 | if (nr_sectors) { | |
2dabb324 | 8233 | pgoff += sectorsize; |
97bf5a55 | 8234 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8235 | goto next_block; |
8236 | } | |
2c30c71b | 8237 | } |
c1dc0896 MX |
8238 | |
8239 | return err; | |
8240 | } | |
8241 | ||
4e4cbee9 CH |
8242 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
8243 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 MX |
8244 | { |
8245 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8246 | ||
8247 | if (skip_csum) { | |
8248 | if (unlikely(err)) | |
8249 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8250 | else | |
8251 | return 0; | |
8252 | } else { | |
8253 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8254 | } | |
8255 | } | |
8256 | ||
4246a0b6 | 8257 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8258 | { |
8259 | struct btrfs_dio_private *dip = bio->bi_private; | |
8260 | struct inode *inode = dip->inode; | |
8261 | struct bio *dio_bio; | |
8262 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4e4cbee9 | 8263 | blk_status_t err = bio->bi_status; |
c1dc0896 | 8264 | |
e3d37fab | 8265 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) { |
8b110e39 | 8266 | err = btrfs_subio_endio_read(inode, io_bio, err); |
e3d37fab | 8267 | if (!err) |
8c27cb35 | 8268 | bio->bi_status = 0; |
e3d37fab | 8269 | } |
c1dc0896 | 8270 | |
4b46fce2 | 8271 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8272 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8273 | dio_bio = dip->dio_bio; |
4b46fce2 | 8274 | |
4b46fce2 | 8275 | kfree(dip); |
c0da7aa1 | 8276 | |
4e4cbee9 | 8277 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8278 | dio_end_io(dio_bio); |
23ea8e5a MX |
8279 | |
8280 | if (io_bio->end_io) | |
4e4cbee9 | 8281 | io_bio->end_io(io_bio, blk_status_to_errno(err)); |
9be3395b | 8282 | bio_put(bio); |
4b46fce2 JB |
8283 | } |
8284 | ||
52427260 QW |
8285 | static void __endio_write_update_ordered(struct inode *inode, |
8286 | const u64 offset, const u64 bytes, | |
8287 | const bool uptodate) | |
4b46fce2 | 8288 | { |
0b246afa | 8289 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8290 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 QW |
8291 | struct btrfs_workqueue *wq; |
8292 | btrfs_work_func_t func; | |
14543774 FM |
8293 | u64 ordered_offset = offset; |
8294 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8295 | int ret; |
8296 | ||
52427260 QW |
8297 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
8298 | wq = fs_info->endio_freespace_worker; | |
8299 | func = btrfs_freespace_write_helper; | |
8300 | } else { | |
8301 | wq = fs_info->endio_write_workers; | |
8302 | func = btrfs_endio_write_helper; | |
8303 | } | |
8304 | ||
163cf09c CM |
8305 | again: |
8306 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8307 | &ordered_offset, | |
4246a0b6 | 8308 | ordered_bytes, |
14543774 | 8309 | uptodate); |
4b46fce2 | 8310 | if (!ret) |
163cf09c | 8311 | goto out_test; |
4b46fce2 | 8312 | |
52427260 QW |
8313 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
8314 | btrfs_queue_work(wq, &ordered->work); | |
163cf09c CM |
8315 | out_test: |
8316 | /* | |
8317 | * our bio might span multiple ordered extents. If we haven't | |
8318 | * completed the accounting for the whole dio, go back and try again | |
8319 | */ | |
14543774 FM |
8320 | if (ordered_offset < offset + bytes) { |
8321 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8322 | ordered = NULL; |
163cf09c CM |
8323 | goto again; |
8324 | } | |
14543774 FM |
8325 | } |
8326 | ||
8327 | static void btrfs_endio_direct_write(struct bio *bio) | |
8328 | { | |
8329 | struct btrfs_dio_private *dip = bio->bi_private; | |
8330 | struct bio *dio_bio = dip->dio_bio; | |
8331 | ||
52427260 | 8332 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
4e4cbee9 | 8333 | dip->bytes, !bio->bi_status); |
4b46fce2 | 8334 | |
4b46fce2 | 8335 | kfree(dip); |
c0da7aa1 | 8336 | |
4e4cbee9 | 8337 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8338 | dio_end_io(dio_bio); |
9be3395b | 8339 | bio_put(bio); |
4b46fce2 JB |
8340 | } |
8341 | ||
8c27cb35 | 8342 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, |
eaf25d93 CM |
8343 | struct bio *bio, int mirror_num, |
8344 | unsigned long bio_flags, u64 offset) | |
8345 | { | |
c6100a4b | 8346 | struct inode *inode = private_data; |
4e4cbee9 | 8347 | blk_status_t ret; |
2ff7e61e | 8348 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8349 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8350 | return 0; |
8351 | } | |
8352 | ||
4246a0b6 | 8353 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8354 | { |
8355 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 8356 | blk_status_t err = bio->bi_status; |
e65e1535 | 8357 | |
8b110e39 MX |
8358 | if (err) |
8359 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8360 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8361 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8362 | bio->bi_opf, | |
8b110e39 MX |
8363 | (unsigned long long)bio->bi_iter.bi_sector, |
8364 | bio->bi_iter.bi_size, err); | |
8365 | ||
8366 | if (dip->subio_endio) | |
8367 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8368 | |
8369 | if (err) { | |
e65e1535 MX |
8370 | dip->errors = 1; |
8371 | ||
8372 | /* | |
8373 | * before atomic variable goto zero, we must make sure | |
8374 | * dip->errors is perceived to be set. | |
8375 | */ | |
4e857c58 | 8376 | smp_mb__before_atomic(); |
e65e1535 MX |
8377 | } |
8378 | ||
8379 | /* if there are more bios still pending for this dio, just exit */ | |
8380 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8381 | goto out; | |
8382 | ||
9be3395b | 8383 | if (dip->errors) { |
e65e1535 | 8384 | bio_io_error(dip->orig_bio); |
9be3395b | 8385 | } else { |
4e4cbee9 | 8386 | dip->dio_bio->bi_status = 0; |
4246a0b6 | 8387 | bio_endio(dip->orig_bio); |
e65e1535 MX |
8388 | } |
8389 | out: | |
8390 | bio_put(bio); | |
8391 | } | |
8392 | ||
4e4cbee9 | 8393 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8394 | struct btrfs_dio_private *dip, |
8395 | struct bio *bio, | |
8396 | u64 file_offset) | |
8397 | { | |
8398 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8399 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
4e4cbee9 | 8400 | blk_status_t ret; |
c1dc0896 MX |
8401 | |
8402 | /* | |
8403 | * We load all the csum data we need when we submit | |
8404 | * the first bio to reduce the csum tree search and | |
8405 | * contention. | |
8406 | */ | |
8407 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8408 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8409 | file_offset); |
8410 | if (ret) | |
8411 | return ret; | |
8412 | } | |
8413 | ||
8414 | if (bio == dip->orig_bio) | |
8415 | return 0; | |
8416 | ||
8417 | file_offset -= dip->logical_offset; | |
8418 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8419 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8420 | ||
8421 | return 0; | |
8422 | } | |
8423 | ||
e65e1535 | 8424 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
81a75f67 | 8425 | u64 file_offset, int skip_sum, |
c329861d | 8426 | int async_submit) |
e65e1535 | 8427 | { |
0b246afa | 8428 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8429 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8430 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 8431 | blk_status_t ret; |
e65e1535 | 8432 | |
b812ce28 JB |
8433 | if (async_submit) |
8434 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8435 | ||
e65e1535 | 8436 | bio_get(bio); |
5fd02043 JB |
8437 | |
8438 | if (!write) { | |
0b246afa | 8439 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8440 | if (ret) |
8441 | goto err; | |
8442 | } | |
e65e1535 | 8443 | |
1ae39938 JB |
8444 | if (skip_sum) |
8445 | goto map; | |
8446 | ||
8447 | if (write && async_submit) { | |
c6100a4b JB |
8448 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8449 | file_offset, inode, | |
0b246afa JM |
8450 | __btrfs_submit_bio_start_direct_io, |
8451 | __btrfs_submit_bio_done); | |
e65e1535 | 8452 | goto err; |
1ae39938 JB |
8453 | } else if (write) { |
8454 | /* | |
8455 | * If we aren't doing async submit, calculate the csum of the | |
8456 | * bio now. | |
8457 | */ | |
2ff7e61e | 8458 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8459 | if (ret) |
8460 | goto err; | |
23ea8e5a | 8461 | } else { |
2ff7e61e | 8462 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8463 | file_offset); |
c2db1073 TI |
8464 | if (ret) |
8465 | goto err; | |
8466 | } | |
1ae39938 | 8467 | map: |
2ff7e61e | 8468 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
e65e1535 MX |
8469 | err: |
8470 | bio_put(bio); | |
8471 | return ret; | |
8472 | } | |
8473 | ||
81a75f67 | 8474 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
e65e1535 MX |
8475 | int skip_sum) |
8476 | { | |
8477 | struct inode *inode = dip->inode; | |
0b246afa | 8478 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8479 | struct bio *bio; |
8480 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8481 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8482 | u64 file_offset = dip->logical_offset; |
e65e1535 | 8483 | u64 map_length; |
1ae39938 | 8484 | int async_submit = 0; |
725130ba LB |
8485 | u64 submit_len; |
8486 | int clone_offset = 0; | |
8487 | int clone_len; | |
5f4dc8fc | 8488 | int ret; |
e65e1535 | 8489 | |
4f024f37 | 8490 | map_length = orig_bio->bi_iter.bi_size; |
725130ba | 8491 | submit_len = map_length; |
0b246afa JM |
8492 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8493 | &map_length, NULL, 0); | |
7a5c3c9b | 8494 | if (ret) |
e65e1535 | 8495 | return -EIO; |
facc8a22 | 8496 | |
725130ba | 8497 | if (map_length >= submit_len) { |
02f57c7a | 8498 | bio = orig_bio; |
c1dc0896 | 8499 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8500 | goto submit; |
8501 | } | |
8502 | ||
53b381b3 | 8503 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8504 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8505 | async_submit = 0; |
8506 | else | |
8507 | async_submit = 1; | |
8508 | ||
725130ba LB |
8509 | /* bio split */ |
8510 | ASSERT(map_length <= INT_MAX); | |
02f57c7a | 8511 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8512 | do { |
725130ba | 8513 | clone_len = min_t(int, submit_len, map_length); |
02f57c7a | 8514 | |
725130ba LB |
8515 | /* |
8516 | * This will never fail as it's passing GPF_NOFS and | |
8517 | * the allocation is backed by btrfs_bioset. | |
8518 | */ | |
e477094f | 8519 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8520 | clone_len); |
8521 | bio->bi_private = dip; | |
8522 | bio->bi_end_io = btrfs_end_dio_bio; | |
8523 | btrfs_io_bio(bio)->logical = file_offset; | |
8524 | ||
8525 | ASSERT(submit_len >= clone_len); | |
8526 | submit_len -= clone_len; | |
8527 | if (submit_len == 0) | |
8528 | break; | |
e65e1535 | 8529 | |
725130ba LB |
8530 | /* |
8531 | * Increase the count before we submit the bio so we know | |
8532 | * the end IO handler won't happen before we increase the | |
8533 | * count. Otherwise, the dip might get freed before we're | |
8534 | * done setting it up. | |
8535 | */ | |
8536 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8537 | |
725130ba LB |
8538 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
8539 | async_submit); | |
8540 | if (ret) { | |
8541 | bio_put(bio); | |
8542 | atomic_dec(&dip->pending_bios); | |
8543 | goto out_err; | |
8544 | } | |
e65e1535 | 8545 | |
725130ba LB |
8546 | clone_offset += clone_len; |
8547 | start_sector += clone_len >> 9; | |
8548 | file_offset += clone_len; | |
5f4dc8fc | 8549 | |
725130ba LB |
8550 | map_length = submit_len; |
8551 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8552 | start_sector << 9, &map_length, NULL, 0); | |
8553 | if (ret) | |
8554 | goto out_err; | |
3c91ee69 | 8555 | } while (submit_len > 0); |
e65e1535 | 8556 | |
02f57c7a | 8557 | submit: |
81a75f67 | 8558 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
c329861d | 8559 | async_submit); |
e65e1535 MX |
8560 | if (!ret) |
8561 | return 0; | |
8562 | ||
8563 | bio_put(bio); | |
8564 | out_err: | |
8565 | dip->errors = 1; | |
8566 | /* | |
8567 | * before atomic variable goto zero, we must | |
8568 | * make sure dip->errors is perceived to be set. | |
8569 | */ | |
4e857c58 | 8570 | smp_mb__before_atomic(); |
e65e1535 MX |
8571 | if (atomic_dec_and_test(&dip->pending_bios)) |
8572 | bio_io_error(dip->orig_bio); | |
8573 | ||
8574 | /* bio_end_io() will handle error, so we needn't return it */ | |
8575 | return 0; | |
8576 | } | |
8577 | ||
8a4c1e42 MC |
8578 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8579 | loff_t file_offset) | |
4b46fce2 | 8580 | { |
61de718f | 8581 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8582 | struct bio *bio = NULL; |
8583 | struct btrfs_io_bio *io_bio; | |
4b46fce2 | 8584 | int skip_sum; |
8a4c1e42 | 8585 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8586 | int ret = 0; |
8587 | ||
8588 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8589 | ||
8b6c1d56 | 8590 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8591 | |
c1dc0896 | 8592 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8593 | if (!dip) { |
8594 | ret = -ENOMEM; | |
61de718f | 8595 | goto free_ordered; |
4b46fce2 | 8596 | } |
4b46fce2 | 8597 | |
9be3395b | 8598 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8599 | dip->inode = inode; |
8600 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8601 | dip->bytes = dio_bio->bi_iter.bi_size; |
8602 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8603 | bio->bi_private = dip; |
8604 | dip->orig_bio = bio; | |
9be3395b | 8605 | dip->dio_bio = dio_bio; |
e65e1535 | 8606 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8607 | io_bio = btrfs_io_bio(bio); |
8608 | io_bio->logical = file_offset; | |
4b46fce2 | 8609 | |
c1dc0896 | 8610 | if (write) { |
3892ac90 | 8611 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8612 | } else { |
3892ac90 | 8613 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8614 | dip->subio_endio = btrfs_subio_endio_read; |
8615 | } | |
4b46fce2 | 8616 | |
f28a4928 FM |
8617 | /* |
8618 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8619 | * even if we fail to submit a bio, because in such case we do the | |
8620 | * corresponding error handling below and it must not be done a second | |
8621 | * time by btrfs_direct_IO(). | |
8622 | */ | |
8623 | if (write) { | |
8624 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8625 | ||
8626 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8627 | dip->bytes; | |
8628 | dio_data->unsubmitted_oe_range_start = | |
8629 | dio_data->unsubmitted_oe_range_end; | |
8630 | } | |
8631 | ||
81a75f67 | 8632 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
e65e1535 | 8633 | if (!ret) |
eaf25d93 | 8634 | return; |
9be3395b | 8635 | |
3892ac90 LB |
8636 | if (io_bio->end_io) |
8637 | io_bio->end_io(io_bio, ret); | |
9be3395b | 8638 | |
4b46fce2 JB |
8639 | free_ordered: |
8640 | /* | |
61de718f FM |
8641 | * If we arrived here it means either we failed to submit the dip |
8642 | * or we either failed to clone the dio_bio or failed to allocate the | |
8643 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8644 | * call bio_endio against our io_bio so that we get proper resource | |
8645 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8646 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8647 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8648 | */ |
3892ac90 | 8649 | if (bio && dip) { |
054ec2f6 | 8650 | bio_io_error(bio); |
61de718f | 8651 | /* |
3892ac90 | 8652 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8653 | * and all the cleanup and final put for dio_bio (through |
8654 | * dio_end_io()). | |
8655 | */ | |
8656 | dip = NULL; | |
3892ac90 | 8657 | bio = NULL; |
61de718f | 8658 | } else { |
14543774 | 8659 | if (write) |
52427260 | 8660 | __endio_write_update_ordered(inode, |
14543774 FM |
8661 | file_offset, |
8662 | dio_bio->bi_iter.bi_size, | |
52427260 | 8663 | false); |
14543774 | 8664 | else |
61de718f FM |
8665 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8666 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8667 | |
4e4cbee9 | 8668 | dio_bio->bi_status = BLK_STS_IOERR; |
61de718f FM |
8669 | /* |
8670 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8671 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8672 | */ | |
4055351c | 8673 | dio_end_io(dio_bio); |
4b46fce2 | 8674 | } |
3892ac90 LB |
8675 | if (bio) |
8676 | bio_put(bio); | |
61de718f | 8677 | kfree(dip); |
4b46fce2 JB |
8678 | } |
8679 | ||
2ff7e61e JM |
8680 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
8681 | struct kiocb *iocb, | |
8682 | const struct iov_iter *iter, loff_t offset) | |
5a5f79b5 CM |
8683 | { |
8684 | int seg; | |
a1b75f7d | 8685 | int i; |
0b246afa | 8686 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8687 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8688 | |
8689 | if (offset & blocksize_mask) | |
8690 | goto out; | |
8691 | ||
28060d5d AV |
8692 | if (iov_iter_alignment(iter) & blocksize_mask) |
8693 | goto out; | |
a1b75f7d | 8694 | |
28060d5d | 8695 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8696 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8697 | return 0; |
8698 | /* | |
8699 | * Check to make sure we don't have duplicate iov_base's in this | |
8700 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8701 | * when reading back. | |
8702 | */ | |
8703 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8704 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8705 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8706 | goto out; |
8707 | } | |
5a5f79b5 CM |
8708 | } |
8709 | retval = 0; | |
8710 | out: | |
8711 | return retval; | |
8712 | } | |
eb838e73 | 8713 | |
c8b8e32d | 8714 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8715 | { |
4b46fce2 JB |
8716 | struct file *file = iocb->ki_filp; |
8717 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8718 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8719 | struct btrfs_dio_data dio_data = { 0 }; |
364ecf36 | 8720 | struct extent_changeset *data_reserved = NULL; |
c8b8e32d | 8721 | loff_t offset = iocb->ki_pos; |
0934856d | 8722 | size_t count = 0; |
2e60a51e | 8723 | int flags = 0; |
38851cc1 MX |
8724 | bool wakeup = true; |
8725 | bool relock = false; | |
0934856d | 8726 | ssize_t ret; |
4b46fce2 | 8727 | |
2ff7e61e | 8728 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
5a5f79b5 | 8729 | return 0; |
3f7c579c | 8730 | |
fe0f07d0 | 8731 | inode_dio_begin(inode); |
4e857c58 | 8732 | smp_mb__after_atomic(); |
38851cc1 | 8733 | |
0e267c44 | 8734 | /* |
41bd9ca4 MX |
8735 | * The generic stuff only does filemap_write_and_wait_range, which |
8736 | * isn't enough if we've written compressed pages to this area, so | |
8737 | * we need to flush the dirty pages again to make absolutely sure | |
8738 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8739 | */ |
a6cbcd4a | 8740 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8741 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8742 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8743 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8744 | offset + count - 1); | |
0e267c44 | 8745 | |
6f673763 | 8746 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8747 | /* |
8748 | * If the write DIO is beyond the EOF, we need update | |
8749 | * the isize, but it is protected by i_mutex. So we can | |
8750 | * not unlock the i_mutex at this case. | |
8751 | */ | |
8752 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8753 | dio_data.overwrite = 1; |
5955102c | 8754 | inode_unlock(inode); |
38851cc1 | 8755 | relock = true; |
edf064e7 GR |
8756 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
8757 | ret = -EAGAIN; | |
8758 | goto out; | |
38851cc1 | 8759 | } |
364ecf36 QW |
8760 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8761 | offset, count); | |
0934856d | 8762 | if (ret) |
38851cc1 | 8763 | goto out; |
823bb20a | 8764 | dio_data.outstanding_extents = count_max_extents(count); |
e1cbbfa5 JB |
8765 | |
8766 | /* | |
8767 | * We need to know how many extents we reserved so that we can | |
8768 | * do the accounting properly if we go over the number we | |
8769 | * originally calculated. Abuse current->journal_info for this. | |
8770 | */ | |
da17066c | 8771 | dio_data.reserve = round_up(count, |
0b246afa | 8772 | fs_info->sectorsize); |
f28a4928 FM |
8773 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8774 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8775 | current->journal_info = &dio_data; |
97dcdea0 | 8776 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8777 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8778 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8779 | inode_dio_end(inode); |
38851cc1 MX |
8780 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8781 | wakeup = false; | |
0934856d MX |
8782 | } |
8783 | ||
17f8c842 | 8784 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8785 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8786 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8787 | btrfs_submit_direct, flags); |
6f673763 | 8788 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8789 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8790 | current->journal_info = NULL; |
ddba1bfc | 8791 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8792 | if (dio_data.reserve) |
bc42bda2 QW |
8793 | btrfs_delalloc_release_space(inode, data_reserved, |
8794 | offset, dio_data.reserve); | |
f28a4928 FM |
8795 | /* |
8796 | * On error we might have left some ordered extents | |
8797 | * without submitting corresponding bios for them, so | |
8798 | * cleanup them up to avoid other tasks getting them | |
8799 | * and waiting for them to complete forever. | |
8800 | */ | |
8801 | if (dio_data.unsubmitted_oe_range_start < | |
8802 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8803 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8804 | dio_data.unsubmitted_oe_range_start, |
8805 | dio_data.unsubmitted_oe_range_end - | |
8806 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8807 | false); |
ddba1bfc | 8808 | } else if (ret >= 0 && (size_t)ret < count) |
bc42bda2 QW |
8809 | btrfs_delalloc_release_space(inode, data_reserved, |
8810 | offset, count - (size_t)ret); | |
0934856d | 8811 | } |
38851cc1 | 8812 | out: |
2e60a51e | 8813 | if (wakeup) |
fe0f07d0 | 8814 | inode_dio_end(inode); |
38851cc1 | 8815 | if (relock) |
5955102c | 8816 | inode_lock(inode); |
0934856d | 8817 | |
364ecf36 | 8818 | extent_changeset_free(data_reserved); |
0934856d | 8819 | return ret; |
16432985 CM |
8820 | } |
8821 | ||
05dadc09 TI |
8822 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8823 | ||
1506fcc8 YS |
8824 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8825 | __u64 start, __u64 len) | |
8826 | { | |
05dadc09 TI |
8827 | int ret; |
8828 | ||
8829 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8830 | if (ret) | |
8831 | return ret; | |
8832 | ||
ec29ed5b | 8833 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8834 | } |
8835 | ||
a52d9a80 | 8836 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8837 | { |
d1310b2e CM |
8838 | struct extent_io_tree *tree; |
8839 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8840 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8841 | } |
1832a6d5 | 8842 | |
a52d9a80 | 8843 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8844 | { |
d1310b2e | 8845 | struct extent_io_tree *tree; |
be7bd730 JB |
8846 | struct inode *inode = page->mapping->host; |
8847 | int ret; | |
b888db2b CM |
8848 | |
8849 | if (current->flags & PF_MEMALLOC) { | |
8850 | redirty_page_for_writepage(wbc, page); | |
8851 | unlock_page(page); | |
8852 | return 0; | |
8853 | } | |
be7bd730 JB |
8854 | |
8855 | /* | |
8856 | * If we are under memory pressure we will call this directly from the | |
8857 | * VM, we need to make sure we have the inode referenced for the ordered | |
8858 | * extent. If not just return like we didn't do anything. | |
8859 | */ | |
8860 | if (!igrab(inode)) { | |
8861 | redirty_page_for_writepage(wbc, page); | |
8862 | return AOP_WRITEPAGE_ACTIVATE; | |
8863 | } | |
d1310b2e | 8864 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8865 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8866 | btrfs_add_delayed_iput(inode); | |
8867 | return ret; | |
9ebefb18 CM |
8868 | } |
8869 | ||
48a3b636 ES |
8870 | static int btrfs_writepages(struct address_space *mapping, |
8871 | struct writeback_control *wbc) | |
b293f02e | 8872 | { |
d1310b2e | 8873 | struct extent_io_tree *tree; |
771ed689 | 8874 | |
d1310b2e | 8875 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8876 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8877 | } | |
8878 | ||
3ab2fb5a CM |
8879 | static int |
8880 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8881 | struct list_head *pages, unsigned nr_pages) | |
8882 | { | |
d1310b2e CM |
8883 | struct extent_io_tree *tree; |
8884 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8885 | return extent_readpages(tree, mapping, pages, nr_pages, |
8886 | btrfs_get_extent); | |
8887 | } | |
e6dcd2dc | 8888 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8889 | { |
d1310b2e CM |
8890 | struct extent_io_tree *tree; |
8891 | struct extent_map_tree *map; | |
a52d9a80 | 8892 | int ret; |
8c2383c3 | 8893 | |
d1310b2e CM |
8894 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8895 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8896 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8897 | if (ret == 1) { |
8898 | ClearPagePrivate(page); | |
8899 | set_page_private(page, 0); | |
09cbfeaf | 8900 | put_page(page); |
39279cc3 | 8901 | } |
a52d9a80 | 8902 | return ret; |
39279cc3 CM |
8903 | } |
8904 | ||
e6dcd2dc CM |
8905 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8906 | { | |
98509cfc CM |
8907 | if (PageWriteback(page) || PageDirty(page)) |
8908 | return 0; | |
3ba7ab22 | 8909 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8910 | } |
8911 | ||
d47992f8 LC |
8912 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8913 | unsigned int length) | |
39279cc3 | 8914 | { |
5fd02043 | 8915 | struct inode *inode = page->mapping->host; |
d1310b2e | 8916 | struct extent_io_tree *tree; |
e6dcd2dc | 8917 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8918 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8919 | u64 page_start = page_offset(page); |
09cbfeaf | 8920 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8921 | u64 start; |
8922 | u64 end; | |
131e404a | 8923 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8924 | |
8b62b72b CM |
8925 | /* |
8926 | * we have the page locked, so new writeback can't start, | |
8927 | * and the dirty bit won't be cleared while we are here. | |
8928 | * | |
8929 | * Wait for IO on this page so that we can safely clear | |
8930 | * the PagePrivate2 bit and do ordered accounting | |
8931 | */ | |
e6dcd2dc | 8932 | wait_on_page_writeback(page); |
8b62b72b | 8933 | |
5fd02043 | 8934 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8935 | if (offset) { |
8936 | btrfs_releasepage(page, GFP_NOFS); | |
8937 | return; | |
8938 | } | |
131e404a FDBM |
8939 | |
8940 | if (!inode_evicting) | |
ff13db41 | 8941 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8942 | again: |
8943 | start = page_start; | |
a776c6fa | 8944 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 8945 | page_end - start + 1); |
e6dcd2dc | 8946 | if (ordered) { |
dbfdb6d1 | 8947 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8948 | /* |
8949 | * IO on this page will never be started, so we need | |
8950 | * to account for any ordered extents now | |
8951 | */ | |
131e404a | 8952 | if (!inode_evicting) |
dbfdb6d1 | 8953 | clear_extent_bit(tree, start, end, |
131e404a | 8954 | EXTENT_DIRTY | EXTENT_DELALLOC | |
a7e3b975 | 8955 | EXTENT_DELALLOC_NEW | |
131e404a FDBM |
8956 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
8957 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8958 | GFP_NOFS); | |
8b62b72b CM |
8959 | /* |
8960 | * whoever cleared the private bit is responsible | |
8961 | * for the finish_ordered_io | |
8962 | */ | |
77cef2ec JB |
8963 | if (TestClearPagePrivate2(page)) { |
8964 | struct btrfs_ordered_inode_tree *tree; | |
8965 | u64 new_len; | |
8966 | ||
8967 | tree = &BTRFS_I(inode)->ordered_tree; | |
8968 | ||
8969 | spin_lock_irq(&tree->lock); | |
8970 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8971 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8972 | if (new_len < ordered->truncated_len) |
8973 | ordered->truncated_len = new_len; | |
8974 | spin_unlock_irq(&tree->lock); | |
8975 | ||
8976 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8977 | start, |
8978 | end - start + 1, 1)) | |
77cef2ec | 8979 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8980 | } |
e6dcd2dc | 8981 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8982 | if (!inode_evicting) { |
8983 | cached_state = NULL; | |
dbfdb6d1 | 8984 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8985 | &cached_state); |
8986 | } | |
dbfdb6d1 CR |
8987 | |
8988 | start = end + 1; | |
8989 | if (start < page_end) | |
8990 | goto again; | |
131e404a FDBM |
8991 | } |
8992 | ||
b9d0b389 QW |
8993 | /* |
8994 | * Qgroup reserved space handler | |
8995 | * Page here will be either | |
8996 | * 1) Already written to disk | |
8997 | * In this case, its reserved space is released from data rsv map | |
8998 | * and will be freed by delayed_ref handler finally. | |
8999 | * So even we call qgroup_free_data(), it won't decrease reserved | |
9000 | * space. | |
9001 | * 2) Not written to disk | |
0b34c261 GR |
9002 | * This means the reserved space should be freed here. However, |
9003 | * if a truncate invalidates the page (by clearing PageDirty) | |
9004 | * and the page is accounted for while allocating extent | |
9005 | * in btrfs_check_data_free_space() we let delayed_ref to | |
9006 | * free the entire extent. | |
b9d0b389 | 9007 | */ |
0b34c261 | 9008 | if (PageDirty(page)) |
bc42bda2 | 9009 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a FDBM |
9010 | if (!inode_evicting) { |
9011 | clear_extent_bit(tree, page_start, page_end, | |
9012 | EXTENT_LOCKED | EXTENT_DIRTY | | |
a7e3b975 FM |
9013 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
9014 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
131e404a FDBM |
9015 | &cached_state, GFP_NOFS); |
9016 | ||
9017 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 9018 | } |
e6dcd2dc | 9019 | |
4a096752 | 9020 | ClearPageChecked(page); |
9ad6b7bc | 9021 | if (PagePrivate(page)) { |
9ad6b7bc CM |
9022 | ClearPagePrivate(page); |
9023 | set_page_private(page, 0); | |
09cbfeaf | 9024 | put_page(page); |
9ad6b7bc | 9025 | } |
39279cc3 CM |
9026 | } |
9027 | ||
9ebefb18 CM |
9028 | /* |
9029 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
9030 | * called from a page fault handler when a page is first dirtied. Hence we must | |
9031 | * be careful to check for EOF conditions here. We set the page up correctly | |
9032 | * for a written page which means we get ENOSPC checking when writing into | |
9033 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
9034 | * support these features. | |
9035 | * | |
9036 | * We are not allowed to take the i_mutex here so we have to play games to | |
9037 | * protect against truncate races as the page could now be beyond EOF. Because | |
9038 | * vmtruncate() writes the inode size before removing pages, once we have the | |
9039 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
9040 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
9041 | * unlock the page. | |
9042 | */ | |
11bac800 | 9043 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 9044 | { |
c2ec175c | 9045 | struct page *page = vmf->page; |
11bac800 | 9046 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 9047 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
9048 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
9049 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 9050 | struct extent_state *cached_state = NULL; |
364ecf36 | 9051 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
9052 | char *kaddr; |
9053 | unsigned long zero_start; | |
9ebefb18 | 9054 | loff_t size; |
1832a6d5 | 9055 | int ret; |
9998eb70 | 9056 | int reserved = 0; |
d0b7da88 | 9057 | u64 reserved_space; |
a52d9a80 | 9058 | u64 page_start; |
e6dcd2dc | 9059 | u64 page_end; |
d0b7da88 CR |
9060 | u64 end; |
9061 | ||
09cbfeaf | 9062 | reserved_space = PAGE_SIZE; |
9ebefb18 | 9063 | |
b2b5ef5c | 9064 | sb_start_pagefault(inode->i_sb); |
df480633 | 9065 | page_start = page_offset(page); |
09cbfeaf | 9066 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 9067 | end = page_end; |
df480633 | 9068 | |
d0b7da88 CR |
9069 | /* |
9070 | * Reserving delalloc space after obtaining the page lock can lead to | |
9071 | * deadlock. For example, if a dirty page is locked by this function | |
9072 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
9073 | * dirty page write out, then the btrfs_writepage() function could | |
9074 | * end up waiting indefinitely to get a lock on the page currently | |
9075 | * being processed by btrfs_page_mkwrite() function. | |
9076 | */ | |
364ecf36 | 9077 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 9078 | reserved_space); |
9998eb70 | 9079 | if (!ret) { |
11bac800 | 9080 | ret = file_update_time(vmf->vma->vm_file); |
9998eb70 CM |
9081 | reserved = 1; |
9082 | } | |
56a76f82 NP |
9083 | if (ret) { |
9084 | if (ret == -ENOMEM) | |
9085 | ret = VM_FAULT_OOM; | |
9086 | else /* -ENOSPC, -EIO, etc */ | |
9087 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
9088 | if (reserved) |
9089 | goto out; | |
9090 | goto out_noreserve; | |
56a76f82 | 9091 | } |
1832a6d5 | 9092 | |
56a76f82 | 9093 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9094 | again: |
9ebefb18 | 9095 | lock_page(page); |
9ebefb18 | 9096 | size = i_size_read(inode); |
a52d9a80 | 9097 | |
9ebefb18 | 9098 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9099 | (page_start >= size)) { |
9ebefb18 CM |
9100 | /* page got truncated out from underneath us */ |
9101 | goto out_unlock; | |
9102 | } | |
e6dcd2dc CM |
9103 | wait_on_page_writeback(page); |
9104 | ||
ff13db41 | 9105 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9106 | set_page_extent_mapped(page); |
9107 | ||
eb84ae03 CM |
9108 | /* |
9109 | * we can't set the delalloc bits if there are pending ordered | |
9110 | * extents. Drop our locks and wait for them to finish | |
9111 | */ | |
a776c6fa NB |
9112 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
9113 | PAGE_SIZE); | |
e6dcd2dc | 9114 | if (ordered) { |
2ac55d41 JB |
9115 | unlock_extent_cached(io_tree, page_start, page_end, |
9116 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9117 | unlock_page(page); |
eb84ae03 | 9118 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9119 | btrfs_put_ordered_extent(ordered); |
9120 | goto again; | |
9121 | } | |
9122 | ||
09cbfeaf | 9123 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9124 | reserved_space = round_up(size - page_start, |
0b246afa | 9125 | fs_info->sectorsize); |
09cbfeaf | 9126 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
9127 | end = page_start + reserved_space - 1; |
9128 | spin_lock(&BTRFS_I(inode)->lock); | |
9129 | BTRFS_I(inode)->outstanding_extents++; | |
9130 | spin_unlock(&BTRFS_I(inode)->lock); | |
bc42bda2 QW |
9131 | btrfs_delalloc_release_space(inode, data_reserved, |
9132 | page_start, PAGE_SIZE - reserved_space); | |
d0b7da88 CR |
9133 | } |
9134 | } | |
9135 | ||
fbf19087 | 9136 | /* |
5416034f LB |
9137 | * page_mkwrite gets called when the page is firstly dirtied after it's |
9138 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9139 | * bits, thus in this case for space account reason, we still need to | |
9140 | * clear any delalloc bits within this page range since we have to | |
9141 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 9142 | */ |
d0b7da88 | 9143 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9144 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9145 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9146 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9147 | |
d0b7da88 | 9148 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9149 | &cached_state, 0); |
9ed74f2d | 9150 | if (ret) { |
2ac55d41 JB |
9151 | unlock_extent_cached(io_tree, page_start, page_end, |
9152 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9153 | ret = VM_FAULT_SIGBUS; |
9154 | goto out_unlock; | |
9155 | } | |
e6dcd2dc | 9156 | ret = 0; |
9ebefb18 CM |
9157 | |
9158 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9159 | if (page_start + PAGE_SIZE > size) |
9160 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9161 | else |
09cbfeaf | 9162 | zero_start = PAGE_SIZE; |
9ebefb18 | 9163 | |
09cbfeaf | 9164 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9165 | kaddr = kmap(page); |
09cbfeaf | 9166 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9167 | flush_dcache_page(page); |
9168 | kunmap(page); | |
9169 | } | |
247e743c | 9170 | ClearPageChecked(page); |
e6dcd2dc | 9171 | set_page_dirty(page); |
50a9b214 | 9172 | SetPageUptodate(page); |
5a3f23d5 | 9173 | |
0b246afa | 9174 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9175 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9176 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9177 | |
2ac55d41 | 9178 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9179 | |
9180 | out_unlock: | |
b2b5ef5c JK |
9181 | if (!ret) { |
9182 | sb_end_pagefault(inode->i_sb); | |
364ecf36 | 9183 | extent_changeset_free(data_reserved); |
50a9b214 | 9184 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9185 | } |
9ebefb18 | 9186 | unlock_page(page); |
1832a6d5 | 9187 | out: |
bc42bda2 QW |
9188 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
9189 | reserved_space); | |
9998eb70 | 9190 | out_noreserve: |
b2b5ef5c | 9191 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9192 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
9193 | return ret; |
9194 | } | |
9195 | ||
a41ad394 | 9196 | static int btrfs_truncate(struct inode *inode) |
39279cc3 | 9197 | { |
0b246afa | 9198 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9199 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9200 | struct btrfs_block_rsv *rsv; |
a71754fc | 9201 | int ret = 0; |
3893e33b | 9202 | int err = 0; |
39279cc3 | 9203 | struct btrfs_trans_handle *trans; |
0b246afa JM |
9204 | u64 mask = fs_info->sectorsize - 1; |
9205 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 9206 | |
0ef8b726 JB |
9207 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9208 | (u64)-1); | |
9209 | if (ret) | |
9210 | return ret; | |
39279cc3 | 9211 | |
fcb80c2a | 9212 | /* |
01327610 | 9213 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9214 | * 3 things going on here |
9215 | * | |
9216 | * 1) We need to reserve space for our orphan item and the space to | |
9217 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9218 | * orphan item because we didn't reserve space to remove it. | |
9219 | * | |
9220 | * 2) We need to reserve space to update our inode. | |
9221 | * | |
9222 | * 3) We need to have something to cache all the space that is going to | |
9223 | * be free'd up by the truncate operation, but also have some slack | |
9224 | * space reserved in case it uses space during the truncate (thank you | |
9225 | * very much snapshotting). | |
9226 | * | |
01327610 | 9227 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9228 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9229 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9230 | * doesn't end up using space reserved for updating the inode or |
9231 | * removing the orphan item. We also need to be able to stop the | |
9232 | * transaction and start a new one, which means we need to be able to | |
9233 | * update the inode several times, and we have no idea of knowing how | |
9234 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9235 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9236 | * Then there is the orphan item, which does indeed need to be held on |
9237 | * to for the whole operation, and we need nobody to touch this reserved | |
9238 | * space except the orphan code. | |
9239 | * | |
9240 | * So that leaves us with | |
9241 | * | |
9242 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9243 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9244 | * transaction reservation. | |
9245 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9246 | * updating the inode. | |
9247 | */ | |
2ff7e61e | 9248 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9249 | if (!rsv) |
9250 | return -ENOMEM; | |
4a338542 | 9251 | rsv->size = min_size; |
ca7e70f5 | 9252 | rsv->failfast = 1; |
f0cd846e | 9253 | |
907cbceb | 9254 | /* |
07127184 | 9255 | * 1 for the truncate slack space |
907cbceb JB |
9256 | * 1 for updating the inode. |
9257 | */ | |
f3fe820c | 9258 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9259 | if (IS_ERR(trans)) { |
9260 | err = PTR_ERR(trans); | |
9261 | goto out; | |
9262 | } | |
f0cd846e | 9263 | |
907cbceb | 9264 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9265 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
25d609f8 | 9266 | min_size, 0); |
fcb80c2a | 9267 | BUG_ON(ret); |
f0cd846e | 9268 | |
5dc562c5 JB |
9269 | /* |
9270 | * So if we truncate and then write and fsync we normally would just | |
9271 | * write the extents that changed, which is a problem if we need to | |
9272 | * first truncate that entire inode. So set this flag so we write out | |
9273 | * all of the extents in the inode to the sync log so we're completely | |
9274 | * safe. | |
9275 | */ | |
9276 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9277 | trans->block_rsv = rsv; |
907cbceb | 9278 | |
8082510e YZ |
9279 | while (1) { |
9280 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9281 | inode->i_size, | |
9282 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9283 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9284 | err = ret; |
8082510e | 9285 | break; |
3893e33b | 9286 | } |
39279cc3 | 9287 | |
0b246afa | 9288 | trans->block_rsv = &fs_info->trans_block_rsv; |
8082510e | 9289 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9290 | if (ret) { |
9291 | err = ret; | |
9292 | break; | |
9293 | } | |
ca7e70f5 | 9294 | |
3a45bb20 | 9295 | btrfs_end_transaction(trans); |
2ff7e61e | 9296 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9297 | |
9298 | trans = btrfs_start_transaction(root, 2); | |
9299 | if (IS_ERR(trans)) { | |
9300 | ret = err = PTR_ERR(trans); | |
9301 | trans = NULL; | |
9302 | break; | |
9303 | } | |
9304 | ||
47b5d646 | 9305 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9306 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
25d609f8 | 9307 | rsv, min_size, 0); |
ca7e70f5 JB |
9308 | BUG_ON(ret); /* shouldn't happen */ |
9309 | trans->block_rsv = rsv; | |
8082510e YZ |
9310 | } |
9311 | ||
9312 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9313 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 9314 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
3893e33b JB |
9315 | if (ret) |
9316 | err = ret; | |
8082510e YZ |
9317 | } |
9318 | ||
917c16b2 | 9319 | if (trans) { |
0b246afa | 9320 | trans->block_rsv = &fs_info->trans_block_rsv; |
917c16b2 CM |
9321 | ret = btrfs_update_inode(trans, root, inode); |
9322 | if (ret && !err) | |
9323 | err = ret; | |
7b128766 | 9324 | |
3a45bb20 | 9325 | ret = btrfs_end_transaction(trans); |
2ff7e61e | 9326 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9327 | } |
fcb80c2a | 9328 | out: |
2ff7e61e | 9329 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9330 | |
3893e33b JB |
9331 | if (ret && !err) |
9332 | err = ret; | |
a41ad394 | 9333 | |
3893e33b | 9334 | return err; |
39279cc3 CM |
9335 | } |
9336 | ||
d352ac68 CM |
9337 | /* |
9338 | * create a new subvolume directory/inode (helper for the ioctl). | |
9339 | */ | |
d2fb3437 | 9340 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9341 | struct btrfs_root *new_root, |
9342 | struct btrfs_root *parent_root, | |
9343 | u64 new_dirid) | |
39279cc3 | 9344 | { |
39279cc3 | 9345 | struct inode *inode; |
76dda93c | 9346 | int err; |
00e4e6b3 | 9347 | u64 index = 0; |
39279cc3 | 9348 | |
12fc9d09 FA |
9349 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9350 | new_dirid, new_dirid, | |
9351 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9352 | &index); | |
54aa1f4d | 9353 | if (IS_ERR(inode)) |
f46b5a66 | 9354 | return PTR_ERR(inode); |
39279cc3 CM |
9355 | inode->i_op = &btrfs_dir_inode_operations; |
9356 | inode->i_fop = &btrfs_dir_file_operations; | |
9357 | ||
bfe86848 | 9358 | set_nlink(inode, 1); |
6ef06d27 | 9359 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9360 | unlock_new_inode(inode); |
3b96362c | 9361 | |
63541927 FDBM |
9362 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9363 | if (err) | |
9364 | btrfs_err(new_root->fs_info, | |
351fd353 | 9365 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9366 | new_root->root_key.objectid, err); |
9367 | ||
76dda93c | 9368 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9369 | |
76dda93c | 9370 | iput(inode); |
ce598979 | 9371 | return err; |
39279cc3 CM |
9372 | } |
9373 | ||
39279cc3 CM |
9374 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9375 | { | |
9376 | struct btrfs_inode *ei; | |
2ead6ae7 | 9377 | struct inode *inode; |
39279cc3 CM |
9378 | |
9379 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9380 | if (!ei) | |
9381 | return NULL; | |
2ead6ae7 YZ |
9382 | |
9383 | ei->root = NULL; | |
2ead6ae7 | 9384 | ei->generation = 0; |
15ee9bc7 | 9385 | ei->last_trans = 0; |
257c62e1 | 9386 | ei->last_sub_trans = 0; |
e02119d5 | 9387 | ei->logged_trans = 0; |
2ead6ae7 | 9388 | ei->delalloc_bytes = 0; |
a7e3b975 | 9389 | ei->new_delalloc_bytes = 0; |
47059d93 | 9390 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9391 | ei->disk_i_size = 0; |
9392 | ei->flags = 0; | |
7709cde3 | 9393 | ei->csum_bytes = 0; |
2ead6ae7 | 9394 | ei->index_cnt = (u64)-1; |
67de1176 | 9395 | ei->dir_index = 0; |
2ead6ae7 | 9396 | ei->last_unlink_trans = 0; |
46d8bc34 | 9397 | ei->last_log_commit = 0; |
8089fe62 | 9398 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9399 | |
9e0baf60 JB |
9400 | spin_lock_init(&ei->lock); |
9401 | ei->outstanding_extents = 0; | |
9402 | ei->reserved_extents = 0; | |
2ead6ae7 | 9403 | |
72ac3c0d | 9404 | ei->runtime_flags = 0; |
261507a0 | 9405 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9406 | |
16cdcec7 MX |
9407 | ei->delayed_node = NULL; |
9408 | ||
9cc97d64 | 9409 | ei->i_otime.tv_sec = 0; |
9410 | ei->i_otime.tv_nsec = 0; | |
9411 | ||
2ead6ae7 | 9412 | inode = &ei->vfs_inode; |
a8067e02 | 9413 | extent_map_tree_init(&ei->extent_tree); |
c6100a4b JB |
9414 | extent_io_tree_init(&ei->io_tree, inode); |
9415 | extent_io_tree_init(&ei->io_failure_tree, inode); | |
0b32f4bb JB |
9416 | ei->io_tree.track_uptodate = 1; |
9417 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9418 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9419 | mutex_init(&ei->log_mutex); |
f248679e | 9420 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9421 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9422 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9423 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9424 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9425 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9426 | |
9427 | return inode; | |
39279cc3 CM |
9428 | } |
9429 | ||
aaedb55b JB |
9430 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9431 | void btrfs_test_destroy_inode(struct inode *inode) | |
9432 | { | |
dcdbc059 | 9433 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9434 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9435 | } | |
9436 | #endif | |
9437 | ||
fa0d7e3d NP |
9438 | static void btrfs_i_callback(struct rcu_head *head) |
9439 | { | |
9440 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9441 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9442 | } | |
9443 | ||
39279cc3 CM |
9444 | void btrfs_destroy_inode(struct inode *inode) |
9445 | { | |
0b246afa | 9446 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9447 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9449 | ||
b3d9b7a3 | 9450 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9451 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9452 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9453 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 | 9454 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9455 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9456 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9457 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9458 | |
a6dbd429 JB |
9459 | /* |
9460 | * This can happen where we create an inode, but somebody else also | |
9461 | * created the same inode and we need to destroy the one we already | |
9462 | * created. | |
9463 | */ | |
9464 | if (!root) | |
9465 | goto free; | |
9466 | ||
8a35d95f JB |
9467 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9468 | &BTRFS_I(inode)->runtime_flags)) { | |
0b246afa | 9469 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
4a0cc7ca | 9470 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 9471 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9472 | } |
7b128766 | 9473 | |
d397712b | 9474 | while (1) { |
e6dcd2dc CM |
9475 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9476 | if (!ordered) | |
9477 | break; | |
9478 | else { | |
0b246afa | 9479 | btrfs_err(fs_info, |
5d163e0e JM |
9480 | "found ordered extent %llu %llu on inode cleanup", |
9481 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9482 | btrfs_remove_ordered_extent(inode, ordered); |
9483 | btrfs_put_ordered_extent(ordered); | |
9484 | btrfs_put_ordered_extent(ordered); | |
9485 | } | |
9486 | } | |
56fa9d07 | 9487 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9488 | inode_tree_del(inode); |
dcdbc059 | 9489 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
a6dbd429 | 9490 | free: |
fa0d7e3d | 9491 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9492 | } |
9493 | ||
45321ac5 | 9494 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9495 | { |
9496 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9497 | |
6379ef9f NA |
9498 | if (root == NULL) |
9499 | return 1; | |
9500 | ||
fa6ac876 | 9501 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9502 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9503 | return 1; |
76dda93c | 9504 | else |
45321ac5 | 9505 | return generic_drop_inode(inode); |
76dda93c YZ |
9506 | } |
9507 | ||
0ee0fda0 | 9508 | static void init_once(void *foo) |
39279cc3 CM |
9509 | { |
9510 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9511 | ||
9512 | inode_init_once(&ei->vfs_inode); | |
9513 | } | |
9514 | ||
9515 | void btrfs_destroy_cachep(void) | |
9516 | { | |
8c0a8537 KS |
9517 | /* |
9518 | * Make sure all delayed rcu free inodes are flushed before we | |
9519 | * destroy cache. | |
9520 | */ | |
9521 | rcu_barrier(); | |
5598e900 KM |
9522 | kmem_cache_destroy(btrfs_inode_cachep); |
9523 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9524 | kmem_cache_destroy(btrfs_path_cachep); |
9525 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9526 | } |
9527 | ||
9528 | int btrfs_init_cachep(void) | |
9529 | { | |
837e1972 | 9530 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9531 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9532 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9533 | init_once); | |
39279cc3 CM |
9534 | if (!btrfs_inode_cachep) |
9535 | goto fail; | |
9601e3f6 | 9536 | |
837e1972 | 9537 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9538 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9539 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9540 | if (!btrfs_trans_handle_cachep) |
9541 | goto fail; | |
9601e3f6 | 9542 | |
837e1972 | 9543 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9544 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9545 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9546 | if (!btrfs_path_cachep) |
9547 | goto fail; | |
9601e3f6 | 9548 | |
837e1972 | 9549 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9550 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9551 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9552 | if (!btrfs_free_space_cachep) |
9553 | goto fail; | |
9554 | ||
39279cc3 CM |
9555 | return 0; |
9556 | fail: | |
9557 | btrfs_destroy_cachep(); | |
9558 | return -ENOMEM; | |
9559 | } | |
9560 | ||
a528d35e DH |
9561 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9562 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9563 | { |
df0af1a5 | 9564 | u64 delalloc_bytes; |
a528d35e | 9565 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9566 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9567 | u32 bi_flags = BTRFS_I(inode)->flags; |
9568 | ||
9569 | stat->result_mask |= STATX_BTIME; | |
9570 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9571 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9572 | if (bi_flags & BTRFS_INODE_APPEND) | |
9573 | stat->attributes |= STATX_ATTR_APPEND; | |
9574 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9575 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9576 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9577 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9578 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9579 | stat->attributes |= STATX_ATTR_NODUMP; | |
9580 | ||
9581 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9582 | STATX_ATTR_COMPRESSED | | |
9583 | STATX_ATTR_IMMUTABLE | | |
9584 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9585 | |
39279cc3 | 9586 | generic_fillattr(inode, stat); |
0ee5dc67 | 9587 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9588 | |
9589 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9590 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9591 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9592 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9593 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9594 | return 0; |
9595 | } | |
9596 | ||
cdd1fedf DF |
9597 | static int btrfs_rename_exchange(struct inode *old_dir, |
9598 | struct dentry *old_dentry, | |
9599 | struct inode *new_dir, | |
9600 | struct dentry *new_dentry) | |
9601 | { | |
0b246afa | 9602 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9603 | struct btrfs_trans_handle *trans; |
9604 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9605 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9606 | struct inode *new_inode = new_dentry->d_inode; | |
9607 | struct inode *old_inode = old_dentry->d_inode; | |
c2050a45 | 9608 | struct timespec ctime = current_time(old_inode); |
cdd1fedf | 9609 | struct dentry *parent; |
4a0cc7ca NB |
9610 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9611 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9612 | u64 old_idx = 0; |
9613 | u64 new_idx = 0; | |
9614 | u64 root_objectid; | |
9615 | int ret; | |
86e8aa0e FM |
9616 | bool root_log_pinned = false; |
9617 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9618 | |
9619 | /* we only allow rename subvolume link between subvolumes */ | |
9620 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9621 | return -EXDEV; | |
9622 | ||
9623 | /* close the race window with snapshot create/destroy ioctl */ | |
9624 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9625 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9626 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9627 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9628 | |
9629 | /* | |
9630 | * We want to reserve the absolute worst case amount of items. So if | |
9631 | * both inodes are subvols and we need to unlink them then that would | |
9632 | * require 4 item modifications, but if they are both normal inodes it | |
9633 | * would require 5 item modifications, so we'll assume their normal | |
9634 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9635 | * should cover the worst case number of items we'll modify. | |
9636 | */ | |
9637 | trans = btrfs_start_transaction(root, 12); | |
9638 | if (IS_ERR(trans)) { | |
9639 | ret = PTR_ERR(trans); | |
9640 | goto out_notrans; | |
9641 | } | |
9642 | ||
9643 | /* | |
9644 | * We need to find a free sequence number both in the source and | |
9645 | * in the destination directory for the exchange. | |
9646 | */ | |
877574e2 | 9647 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9648 | if (ret) |
9649 | goto out_fail; | |
877574e2 | 9650 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9651 | if (ret) |
9652 | goto out_fail; | |
9653 | ||
9654 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9655 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9656 | ||
9657 | /* Reference for the source. */ | |
9658 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9659 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9660 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9661 | } else { |
376e5a57 FM |
9662 | btrfs_pin_log_trans(root); |
9663 | root_log_pinned = true; | |
cdd1fedf DF |
9664 | ret = btrfs_insert_inode_ref(trans, dest, |
9665 | new_dentry->d_name.name, | |
9666 | new_dentry->d_name.len, | |
9667 | old_ino, | |
f85b7379 DS |
9668 | btrfs_ino(BTRFS_I(new_dir)), |
9669 | old_idx); | |
cdd1fedf DF |
9670 | if (ret) |
9671 | goto out_fail; | |
cdd1fedf DF |
9672 | } |
9673 | ||
9674 | /* And now for the dest. */ | |
9675 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9676 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9677 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9678 | } else { |
376e5a57 FM |
9679 | btrfs_pin_log_trans(dest); |
9680 | dest_log_pinned = true; | |
cdd1fedf DF |
9681 | ret = btrfs_insert_inode_ref(trans, root, |
9682 | old_dentry->d_name.name, | |
9683 | old_dentry->d_name.len, | |
9684 | new_ino, | |
f85b7379 DS |
9685 | btrfs_ino(BTRFS_I(old_dir)), |
9686 | new_idx); | |
cdd1fedf DF |
9687 | if (ret) |
9688 | goto out_fail; | |
cdd1fedf DF |
9689 | } |
9690 | ||
9691 | /* Update inode version and ctime/mtime. */ | |
9692 | inode_inc_iversion(old_dir); | |
9693 | inode_inc_iversion(new_dir); | |
9694 | inode_inc_iversion(old_inode); | |
9695 | inode_inc_iversion(new_inode); | |
9696 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9697 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9698 | old_inode->i_ctime = ctime; | |
9699 | new_inode->i_ctime = ctime; | |
9700 | ||
9701 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9702 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9703 | BTRFS_I(old_inode), 1); | |
9704 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9705 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9706 | } |
9707 | ||
9708 | /* src is a subvolume */ | |
9709 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9710 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9711 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9712 | root_objectid, | |
9713 | old_dentry->d_name.name, | |
9714 | old_dentry->d_name.len); | |
9715 | } else { /* src is an inode */ | |
4ec5934e NB |
9716 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9717 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9718 | old_dentry->d_name.name, |
9719 | old_dentry->d_name.len); | |
9720 | if (!ret) | |
9721 | ret = btrfs_update_inode(trans, root, old_inode); | |
9722 | } | |
9723 | if (ret) { | |
66642832 | 9724 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9725 | goto out_fail; |
9726 | } | |
9727 | ||
9728 | /* dest is a subvolume */ | |
9729 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9730 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9731 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9732 | root_objectid, | |
9733 | new_dentry->d_name.name, | |
9734 | new_dentry->d_name.len); | |
9735 | } else { /* dest is an inode */ | |
4ec5934e NB |
9736 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9737 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9738 | new_dentry->d_name.name, |
9739 | new_dentry->d_name.len); | |
9740 | if (!ret) | |
9741 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9742 | } | |
9743 | if (ret) { | |
66642832 | 9744 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9745 | goto out_fail; |
9746 | } | |
9747 | ||
db0a669f | 9748 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9749 | new_dentry->d_name.name, |
9750 | new_dentry->d_name.len, 0, old_idx); | |
9751 | if (ret) { | |
66642832 | 9752 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9753 | goto out_fail; |
9754 | } | |
9755 | ||
db0a669f | 9756 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9757 | old_dentry->d_name.name, |
9758 | old_dentry->d_name.len, 0, new_idx); | |
9759 | if (ret) { | |
66642832 | 9760 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9761 | goto out_fail; |
9762 | } | |
9763 | ||
9764 | if (old_inode->i_nlink == 1) | |
9765 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9766 | if (new_inode->i_nlink == 1) | |
9767 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9768 | ||
86e8aa0e | 9769 | if (root_log_pinned) { |
cdd1fedf | 9770 | parent = new_dentry->d_parent; |
f85b7379 DS |
9771 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9772 | parent); | |
cdd1fedf | 9773 | btrfs_end_log_trans(root); |
86e8aa0e | 9774 | root_log_pinned = false; |
cdd1fedf | 9775 | } |
86e8aa0e | 9776 | if (dest_log_pinned) { |
cdd1fedf | 9777 | parent = old_dentry->d_parent; |
f85b7379 DS |
9778 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
9779 | parent); | |
cdd1fedf | 9780 | btrfs_end_log_trans(dest); |
86e8aa0e | 9781 | dest_log_pinned = false; |
cdd1fedf DF |
9782 | } |
9783 | out_fail: | |
86e8aa0e FM |
9784 | /* |
9785 | * If we have pinned a log and an error happened, we unpin tasks | |
9786 | * trying to sync the log and force them to fallback to a transaction | |
9787 | * commit if the log currently contains any of the inodes involved in | |
9788 | * this rename operation (to ensure we do not persist a log with an | |
9789 | * inconsistent state for any of these inodes or leading to any | |
9790 | * inconsistencies when replayed). If the transaction was aborted, the | |
9791 | * abortion reason is propagated to userspace when attempting to commit | |
9792 | * the transaction. If the log does not contain any of these inodes, we | |
9793 | * allow the tasks to sync it. | |
9794 | */ | |
9795 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9796 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9797 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9798 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9799 | (new_inode && |
0f8939b8 | 9800 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9801 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9802 | |
9803 | if (root_log_pinned) { | |
9804 | btrfs_end_log_trans(root); | |
9805 | root_log_pinned = false; | |
9806 | } | |
9807 | if (dest_log_pinned) { | |
9808 | btrfs_end_log_trans(dest); | |
9809 | dest_log_pinned = false; | |
9810 | } | |
9811 | } | |
3a45bb20 | 9812 | ret = btrfs_end_transaction(trans); |
cdd1fedf DF |
9813 | out_notrans: |
9814 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9815 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9816 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9817 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9818 | |
9819 | return ret; | |
9820 | } | |
9821 | ||
9822 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9823 | struct btrfs_root *root, | |
9824 | struct inode *dir, | |
9825 | struct dentry *dentry) | |
9826 | { | |
9827 | int ret; | |
9828 | struct inode *inode; | |
9829 | u64 objectid; | |
9830 | u64 index; | |
9831 | ||
9832 | ret = btrfs_find_free_ino(root, &objectid); | |
9833 | if (ret) | |
9834 | return ret; | |
9835 | ||
9836 | inode = btrfs_new_inode(trans, root, dir, | |
9837 | dentry->d_name.name, | |
9838 | dentry->d_name.len, | |
4a0cc7ca | 9839 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9840 | objectid, |
9841 | S_IFCHR | WHITEOUT_MODE, | |
9842 | &index); | |
9843 | ||
9844 | if (IS_ERR(inode)) { | |
9845 | ret = PTR_ERR(inode); | |
9846 | return ret; | |
9847 | } | |
9848 | ||
9849 | inode->i_op = &btrfs_special_inode_operations; | |
9850 | init_special_inode(inode, inode->i_mode, | |
9851 | WHITEOUT_DEV); | |
9852 | ||
9853 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9854 | &dentry->d_name); | |
9855 | if (ret) | |
c9901618 | 9856 | goto out; |
cdd1fedf | 9857 | |
cef415af NB |
9858 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9859 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9860 | if (ret) |
c9901618 | 9861 | goto out; |
cdd1fedf DF |
9862 | |
9863 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9864 | out: |
cdd1fedf | 9865 | unlock_new_inode(inode); |
c9901618 FM |
9866 | if (ret) |
9867 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9868 | iput(inode); |
9869 | ||
c9901618 | 9870 | return ret; |
cdd1fedf DF |
9871 | } |
9872 | ||
d397712b | 9873 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9874 | struct inode *new_dir, struct dentry *new_dentry, |
9875 | unsigned int flags) | |
39279cc3 | 9876 | { |
0b246afa | 9877 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9878 | struct btrfs_trans_handle *trans; |
5062af35 | 9879 | unsigned int trans_num_items; |
39279cc3 | 9880 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9881 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9882 | struct inode *new_inode = d_inode(new_dentry); |
9883 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9884 | u64 index = 0; |
4df27c4d | 9885 | u64 root_objectid; |
39279cc3 | 9886 | int ret; |
4a0cc7ca | 9887 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9888 | bool log_pinned = false; |
39279cc3 | 9889 | |
4a0cc7ca | 9890 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9891 | return -EPERM; |
9892 | ||
4df27c4d | 9893 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9894 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9895 | return -EXDEV; |
9896 | ||
33345d01 | 9897 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9898 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9899 | return -ENOTEMPTY; |
5f39d397 | 9900 | |
4df27c4d YZ |
9901 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9902 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9903 | return -ENOTEMPTY; | |
9c52057c CM |
9904 | |
9905 | ||
9906 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9907 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9908 | new_dentry->d_name.name, |
9909 | new_dentry->d_name.len); | |
9910 | ||
9911 | if (ret) { | |
9912 | if (ret == -EEXIST) { | |
9913 | /* we shouldn't get | |
9914 | * eexist without a new_inode */ | |
fae7f21c | 9915 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9916 | return ret; |
9917 | } | |
9918 | } else { | |
9919 | /* maybe -EOVERFLOW */ | |
9920 | return ret; | |
9921 | } | |
9922 | } | |
9923 | ret = 0; | |
9924 | ||
5a3f23d5 | 9925 | /* |
8d875f95 CM |
9926 | * we're using rename to replace one file with another. Start IO on it |
9927 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9928 | */ |
8d875f95 | 9929 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9930 | filemap_flush(old_inode->i_mapping); |
9931 | ||
76dda93c | 9932 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9933 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9934 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9935 | /* |
9936 | * We want to reserve the absolute worst case amount of items. So if | |
9937 | * both inodes are subvols and we need to unlink them then that would | |
9938 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9939 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9940 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9941 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9942 | * If our rename has the whiteout flag, we need more 5 units for the |
9943 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9944 | * when selinux is enabled). | |
a22285a6 | 9945 | */ |
5062af35 FM |
9946 | trans_num_items = 11; |
9947 | if (flags & RENAME_WHITEOUT) | |
9948 | trans_num_items += 5; | |
9949 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9950 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9951 | ret = PTR_ERR(trans); |
9952 | goto out_notrans; | |
9953 | } | |
76dda93c | 9954 | |
4df27c4d YZ |
9955 | if (dest != root) |
9956 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9957 | |
877574e2 | 9958 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
9959 | if (ret) |
9960 | goto out_fail; | |
5a3f23d5 | 9961 | |
67de1176 | 9962 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9963 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9964 | /* force full log commit if subvolume involved. */ |
0b246afa | 9965 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 9966 | } else { |
c4aba954 FM |
9967 | btrfs_pin_log_trans(root); |
9968 | log_pinned = true; | |
a5719521 YZ |
9969 | ret = btrfs_insert_inode_ref(trans, dest, |
9970 | new_dentry->d_name.name, | |
9971 | new_dentry->d_name.len, | |
33345d01 | 9972 | old_ino, |
4a0cc7ca | 9973 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9974 | if (ret) |
9975 | goto out_fail; | |
4df27c4d | 9976 | } |
5a3f23d5 | 9977 | |
0c4d2d95 JB |
9978 | inode_inc_iversion(old_dir); |
9979 | inode_inc_iversion(new_dir); | |
9980 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9981 | old_dir->i_ctime = old_dir->i_mtime = |
9982 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9983 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9984 | |
12fcfd22 | 9985 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
9986 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9987 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 9988 | |
33345d01 | 9989 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9990 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9991 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9992 | old_dentry->d_name.name, | |
9993 | old_dentry->d_name.len); | |
9994 | } else { | |
4ec5934e NB |
9995 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9996 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
9997 | old_dentry->d_name.name, |
9998 | old_dentry->d_name.len); | |
9999 | if (!ret) | |
10000 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 10001 | } |
79787eaa | 10002 | if (ret) { |
66642832 | 10003 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10004 | goto out_fail; |
10005 | } | |
39279cc3 CM |
10006 | |
10007 | if (new_inode) { | |
0c4d2d95 | 10008 | inode_inc_iversion(new_inode); |
c2050a45 | 10009 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 10010 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
10011 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
10012 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
10013 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
10014 | root_objectid, | |
10015 | new_dentry->d_name.name, | |
10016 | new_dentry->d_name.len); | |
10017 | BUG_ON(new_inode->i_nlink == 0); | |
10018 | } else { | |
4ec5934e NB |
10019 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
10020 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
10021 | new_dentry->d_name.name, |
10022 | new_dentry->d_name.len); | |
10023 | } | |
4ef31a45 | 10024 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
10025 | ret = btrfs_orphan_add(trans, |
10026 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 10027 | if (ret) { |
66642832 | 10028 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10029 | goto out_fail; |
10030 | } | |
39279cc3 | 10031 | } |
aec7477b | 10032 | |
db0a669f | 10033 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 10034 | new_dentry->d_name.name, |
a5719521 | 10035 | new_dentry->d_name.len, 0, index); |
79787eaa | 10036 | if (ret) { |
66642832 | 10037 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10038 | goto out_fail; |
10039 | } | |
39279cc3 | 10040 | |
67de1176 MX |
10041 | if (old_inode->i_nlink == 1) |
10042 | BTRFS_I(old_inode)->dir_index = index; | |
10043 | ||
3dc9e8f7 | 10044 | if (log_pinned) { |
10d9f309 | 10045 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 10046 | |
f85b7379 DS |
10047 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
10048 | parent); | |
4df27c4d | 10049 | btrfs_end_log_trans(root); |
3dc9e8f7 | 10050 | log_pinned = false; |
4df27c4d | 10051 | } |
cdd1fedf DF |
10052 | |
10053 | if (flags & RENAME_WHITEOUT) { | |
10054 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10055 | old_dentry); | |
10056 | ||
10057 | if (ret) { | |
66642832 | 10058 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
10059 | goto out_fail; |
10060 | } | |
4df27c4d | 10061 | } |
39279cc3 | 10062 | out_fail: |
3dc9e8f7 FM |
10063 | /* |
10064 | * If we have pinned the log and an error happened, we unpin tasks | |
10065 | * trying to sync the log and force them to fallback to a transaction | |
10066 | * commit if the log currently contains any of the inodes involved in | |
10067 | * this rename operation (to ensure we do not persist a log with an | |
10068 | * inconsistent state for any of these inodes or leading to any | |
10069 | * inconsistencies when replayed). If the transaction was aborted, the | |
10070 | * abortion reason is propagated to userspace when attempting to commit | |
10071 | * the transaction. If the log does not contain any of these inodes, we | |
10072 | * allow the tasks to sync it. | |
10073 | */ | |
10074 | if (ret && log_pinned) { | |
0f8939b8 NB |
10075 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
10076 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10077 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 10078 | (new_inode && |
0f8939b8 | 10079 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 10080 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
10081 | |
10082 | btrfs_end_log_trans(root); | |
10083 | log_pinned = false; | |
10084 | } | |
3a45bb20 | 10085 | btrfs_end_transaction(trans); |
b44c59a8 | 10086 | out_notrans: |
33345d01 | 10087 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10088 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 10089 | |
39279cc3 CM |
10090 | return ret; |
10091 | } | |
10092 | ||
80ace85c MS |
10093 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
10094 | struct inode *new_dir, struct dentry *new_dentry, | |
10095 | unsigned int flags) | |
10096 | { | |
cdd1fedf | 10097 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
10098 | return -EINVAL; |
10099 | ||
cdd1fedf DF |
10100 | if (flags & RENAME_EXCHANGE) |
10101 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10102 | new_dentry); | |
10103 | ||
10104 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
10105 | } |
10106 | ||
8ccf6f19 MX |
10107 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
10108 | { | |
10109 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 10110 | struct inode *inode; |
8ccf6f19 MX |
10111 | |
10112 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10113 | work); | |
9f23e289 | 10114 | inode = delalloc_work->inode; |
30424601 DS |
10115 | filemap_flush(inode->i_mapping); |
10116 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10117 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10118 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10119 | |
10120 | if (delalloc_work->delay_iput) | |
9f23e289 | 10121 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10122 | else |
9f23e289 | 10123 | iput(inode); |
8ccf6f19 MX |
10124 | complete(&delalloc_work->completion); |
10125 | } | |
10126 | ||
10127 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10128 | int delay_iput) |
8ccf6f19 MX |
10129 | { |
10130 | struct btrfs_delalloc_work *work; | |
10131 | ||
100d5702 | 10132 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10133 | if (!work) |
10134 | return NULL; | |
10135 | ||
10136 | init_completion(&work->completion); | |
10137 | INIT_LIST_HEAD(&work->list); | |
10138 | work->inode = inode; | |
8ccf6f19 | 10139 | work->delay_iput = delay_iput; |
9e0af237 LB |
10140 | WARN_ON_ONCE(!inode); |
10141 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10142 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10143 | |
10144 | return work; | |
10145 | } | |
10146 | ||
10147 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10148 | { | |
10149 | wait_for_completion(&work->completion); | |
100d5702 | 10150 | kfree(work); |
8ccf6f19 MX |
10151 | } |
10152 | ||
d352ac68 CM |
10153 | /* |
10154 | * some fairly slow code that needs optimization. This walks the list | |
10155 | * of all the inodes with pending delalloc and forces them to disk. | |
10156 | */ | |
6c255e67 MX |
10157 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10158 | int nr) | |
ea8c2819 | 10159 | { |
ea8c2819 | 10160 | struct btrfs_inode *binode; |
5b21f2ed | 10161 | struct inode *inode; |
8ccf6f19 MX |
10162 | struct btrfs_delalloc_work *work, *next; |
10163 | struct list_head works; | |
1eafa6c7 | 10164 | struct list_head splice; |
8ccf6f19 | 10165 | int ret = 0; |
ea8c2819 | 10166 | |
8ccf6f19 | 10167 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10168 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10169 | |
573bfb72 | 10170 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10171 | spin_lock(&root->delalloc_lock); |
10172 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10173 | while (!list_empty(&splice)) { |
10174 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10175 | delalloc_inodes); |
1eafa6c7 | 10176 | |
eb73c1b7 MX |
10177 | list_move_tail(&binode->delalloc_inodes, |
10178 | &root->delalloc_inodes); | |
5b21f2ed | 10179 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10180 | if (!inode) { |
eb73c1b7 | 10181 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10182 | continue; |
df0af1a5 | 10183 | } |
eb73c1b7 | 10184 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10185 | |
651d494a | 10186 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10187 | if (!work) { |
f4ab9ea7 JB |
10188 | if (delay_iput) |
10189 | btrfs_add_delayed_iput(inode); | |
10190 | else | |
10191 | iput(inode); | |
1eafa6c7 | 10192 | ret = -ENOMEM; |
a1ecaabb | 10193 | goto out; |
5b21f2ed | 10194 | } |
1eafa6c7 | 10195 | list_add_tail(&work->list, &works); |
a44903ab QW |
10196 | btrfs_queue_work(root->fs_info->flush_workers, |
10197 | &work->work); | |
6c255e67 MX |
10198 | ret++; |
10199 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10200 | goto out; |
5b21f2ed | 10201 | cond_resched(); |
eb73c1b7 | 10202 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10203 | } |
eb73c1b7 | 10204 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10205 | |
a1ecaabb | 10206 | out: |
eb73c1b7 MX |
10207 | list_for_each_entry_safe(work, next, &works, list) { |
10208 | list_del_init(&work->list); | |
10209 | btrfs_wait_and_free_delalloc_work(work); | |
10210 | } | |
10211 | ||
10212 | if (!list_empty_careful(&splice)) { | |
10213 | spin_lock(&root->delalloc_lock); | |
10214 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10215 | spin_unlock(&root->delalloc_lock); | |
10216 | } | |
573bfb72 | 10217 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10218 | return ret; |
10219 | } | |
1eafa6c7 | 10220 | |
eb73c1b7 MX |
10221 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10222 | { | |
0b246afa | 10223 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10224 | int ret; |
1eafa6c7 | 10225 | |
0b246afa | 10226 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10227 | return -EROFS; |
10228 | ||
6c255e67 MX |
10229 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10230 | if (ret > 0) | |
10231 | ret = 0; | |
eb73c1b7 MX |
10232 | /* |
10233 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10234 | * we have to make sure the IO is actually started and that |
10235 | * ordered extents get created before we return | |
10236 | */ | |
0b246afa JM |
10237 | atomic_inc(&fs_info->async_submit_draining); |
10238 | while (atomic_read(&fs_info->nr_async_submits) || | |
10239 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10240 | wait_event(fs_info->async_submit_wait, | |
10241 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10242 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10243 | } | |
10244 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 MX |
10245 | return ret; |
10246 | } | |
10247 | ||
6c255e67 MX |
10248 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10249 | int nr) | |
eb73c1b7 MX |
10250 | { |
10251 | struct btrfs_root *root; | |
10252 | struct list_head splice; | |
10253 | int ret; | |
10254 | ||
2c21b4d7 | 10255 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10256 | return -EROFS; |
10257 | ||
10258 | INIT_LIST_HEAD(&splice); | |
10259 | ||
573bfb72 | 10260 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10261 | spin_lock(&fs_info->delalloc_root_lock); |
10262 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10263 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10264 | root = list_first_entry(&splice, struct btrfs_root, |
10265 | delalloc_root); | |
10266 | root = btrfs_grab_fs_root(root); | |
10267 | BUG_ON(!root); | |
10268 | list_move_tail(&root->delalloc_root, | |
10269 | &fs_info->delalloc_roots); | |
10270 | spin_unlock(&fs_info->delalloc_root_lock); | |
10271 | ||
6c255e67 | 10272 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10273 | btrfs_put_fs_root(root); |
6c255e67 | 10274 | if (ret < 0) |
eb73c1b7 MX |
10275 | goto out; |
10276 | ||
6c255e67 MX |
10277 | if (nr != -1) { |
10278 | nr -= ret; | |
10279 | WARN_ON(nr < 0); | |
10280 | } | |
eb73c1b7 | 10281 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10282 | } |
eb73c1b7 | 10283 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10284 | |
6c255e67 | 10285 | ret = 0; |
eb73c1b7 MX |
10286 | atomic_inc(&fs_info->async_submit_draining); |
10287 | while (atomic_read(&fs_info->nr_async_submits) || | |
10288 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10289 | wait_event(fs_info->async_submit_wait, | |
10290 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10291 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10292 | } | |
10293 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10294 | out: |
1eafa6c7 | 10295 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10296 | spin_lock(&fs_info->delalloc_root_lock); |
10297 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10298 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10299 | } |
573bfb72 | 10300 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10301 | return ret; |
ea8c2819 CM |
10302 | } |
10303 | ||
39279cc3 CM |
10304 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10305 | const char *symname) | |
10306 | { | |
0b246afa | 10307 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10308 | struct btrfs_trans_handle *trans; |
10309 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10310 | struct btrfs_path *path; | |
10311 | struct btrfs_key key; | |
1832a6d5 | 10312 | struct inode *inode = NULL; |
39279cc3 CM |
10313 | int err; |
10314 | int drop_inode = 0; | |
10315 | u64 objectid; | |
67871254 | 10316 | u64 index = 0; |
39279cc3 CM |
10317 | int name_len; |
10318 | int datasize; | |
5f39d397 | 10319 | unsigned long ptr; |
39279cc3 | 10320 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10321 | struct extent_buffer *leaf; |
39279cc3 | 10322 | |
f06becc4 | 10323 | name_len = strlen(symname); |
0b246afa | 10324 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10325 | return -ENAMETOOLONG; |
1832a6d5 | 10326 | |
9ed74f2d JB |
10327 | /* |
10328 | * 2 items for inode item and ref | |
10329 | * 2 items for dir items | |
9269d12b FM |
10330 | * 1 item for updating parent inode item |
10331 | * 1 item for the inline extent item | |
9ed74f2d JB |
10332 | * 1 item for xattr if selinux is on |
10333 | */ | |
9269d12b | 10334 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10335 | if (IS_ERR(trans)) |
10336 | return PTR_ERR(trans); | |
1832a6d5 | 10337 | |
581bb050 LZ |
10338 | err = btrfs_find_free_ino(root, &objectid); |
10339 | if (err) | |
10340 | goto out_unlock; | |
10341 | ||
aec7477b | 10342 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10343 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10344 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10345 | if (IS_ERR(inode)) { |
10346 | err = PTR_ERR(inode); | |
39279cc3 | 10347 | goto out_unlock; |
7cf96da3 | 10348 | } |
39279cc3 | 10349 | |
ad19db71 CS |
10350 | /* |
10351 | * If the active LSM wants to access the inode during | |
10352 | * d_instantiate it needs these. Smack checks to see | |
10353 | * if the filesystem supports xattrs by looking at the | |
10354 | * ops vector. | |
10355 | */ | |
10356 | inode->i_fop = &btrfs_file_operations; | |
10357 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10358 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10359 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10360 | ||
10361 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10362 | if (err) | |
10363 | goto out_unlock_inode; | |
ad19db71 | 10364 | |
39279cc3 | 10365 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10366 | if (!path) { |
10367 | err = -ENOMEM; | |
b0d5d10f | 10368 | goto out_unlock_inode; |
d8926bb3 | 10369 | } |
4a0cc7ca | 10370 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10371 | key.offset = 0; |
962a298f | 10372 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10373 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10374 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10375 | datasize); | |
54aa1f4d | 10376 | if (err) { |
b0839166 | 10377 | btrfs_free_path(path); |
b0d5d10f | 10378 | goto out_unlock_inode; |
54aa1f4d | 10379 | } |
5f39d397 CM |
10380 | leaf = path->nodes[0]; |
10381 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10382 | struct btrfs_file_extent_item); | |
10383 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10384 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10385 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10386 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10387 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10388 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10389 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10390 | ||
39279cc3 | 10391 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10392 | write_extent_buffer(leaf, symname, ptr, name_len); |
10393 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10394 | btrfs_free_path(path); |
5f39d397 | 10395 | |
39279cc3 | 10396 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10397 | inode_nohighmem(inode); |
39279cc3 | 10398 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10399 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10400 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10401 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10402 | /* |
10403 | * Last step, add directory indexes for our symlink inode. This is the | |
10404 | * last step to avoid extra cleanup of these indexes if an error happens | |
10405 | * elsewhere above. | |
10406 | */ | |
10407 | if (!err) | |
cef415af NB |
10408 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10409 | BTRFS_I(inode), 0, index); | |
b0d5d10f | 10410 | if (err) { |
54aa1f4d | 10411 | drop_inode = 1; |
b0d5d10f CM |
10412 | goto out_unlock_inode; |
10413 | } | |
10414 | ||
10415 | unlock_new_inode(inode); | |
10416 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10417 | |
10418 | out_unlock: | |
3a45bb20 | 10419 | btrfs_end_transaction(trans); |
39279cc3 CM |
10420 | if (drop_inode) { |
10421 | inode_dec_link_count(inode); | |
10422 | iput(inode); | |
10423 | } | |
2ff7e61e | 10424 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 10425 | return err; |
b0d5d10f CM |
10426 | |
10427 | out_unlock_inode: | |
10428 | drop_inode = 1; | |
10429 | unlock_new_inode(inode); | |
10430 | goto out_unlock; | |
39279cc3 | 10431 | } |
16432985 | 10432 | |
0af3d00b JB |
10433 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10434 | u64 start, u64 num_bytes, u64 min_size, | |
10435 | loff_t actual_len, u64 *alloc_hint, | |
10436 | struct btrfs_trans_handle *trans) | |
d899e052 | 10437 | { |
0b246afa | 10438 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10439 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10440 | struct extent_map *em; | |
d899e052 YZ |
10441 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10442 | struct btrfs_key ins; | |
d899e052 | 10443 | u64 cur_offset = start; |
55a61d1d | 10444 | u64 i_size; |
154ea289 | 10445 | u64 cur_bytes; |
0b670dc4 | 10446 | u64 last_alloc = (u64)-1; |
d899e052 | 10447 | int ret = 0; |
0af3d00b | 10448 | bool own_trans = true; |
18513091 | 10449 | u64 end = start + num_bytes - 1; |
d899e052 | 10450 | |
0af3d00b JB |
10451 | if (trans) |
10452 | own_trans = false; | |
d899e052 | 10453 | while (num_bytes > 0) { |
0af3d00b JB |
10454 | if (own_trans) { |
10455 | trans = btrfs_start_transaction(root, 3); | |
10456 | if (IS_ERR(trans)) { | |
10457 | ret = PTR_ERR(trans); | |
10458 | break; | |
10459 | } | |
5a303d5d YZ |
10460 | } |
10461 | ||
ee22184b | 10462 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10463 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10464 | /* |
10465 | * If we are severely fragmented we could end up with really | |
10466 | * small allocations, so if the allocator is returning small | |
10467 | * chunks lets make its job easier by only searching for those | |
10468 | * sized chunks. | |
10469 | */ | |
10470 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10471 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10472 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10473 | if (ret) { |
0af3d00b | 10474 | if (own_trans) |
3a45bb20 | 10475 | btrfs_end_transaction(trans); |
a22285a6 | 10476 | break; |
d899e052 | 10477 | } |
0b246afa | 10478 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10479 | |
0b670dc4 | 10480 | last_alloc = ins.offset; |
d899e052 YZ |
10481 | ret = insert_reserved_file_extent(trans, inode, |
10482 | cur_offset, ins.objectid, | |
10483 | ins.offset, ins.offset, | |
920bbbfb | 10484 | ins.offset, 0, 0, 0, |
d899e052 | 10485 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10486 | if (ret) { |
2ff7e61e | 10487 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10488 | ins.offset, 0); |
66642832 | 10489 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10490 | if (own_trans) |
3a45bb20 | 10491 | btrfs_end_transaction(trans); |
79787eaa JM |
10492 | break; |
10493 | } | |
31193213 | 10494 | |
dcdbc059 | 10495 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10496 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10497 | |
5dc562c5 JB |
10498 | em = alloc_extent_map(); |
10499 | if (!em) { | |
10500 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10501 | &BTRFS_I(inode)->runtime_flags); | |
10502 | goto next; | |
10503 | } | |
10504 | ||
10505 | em->start = cur_offset; | |
10506 | em->orig_start = cur_offset; | |
10507 | em->len = ins.offset; | |
10508 | em->block_start = ins.objectid; | |
10509 | em->block_len = ins.offset; | |
b4939680 | 10510 | em->orig_block_len = ins.offset; |
cc95bef6 | 10511 | em->ram_bytes = ins.offset; |
0b246afa | 10512 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10513 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10514 | em->generation = trans->transid; | |
10515 | ||
10516 | while (1) { | |
10517 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10518 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10519 | write_unlock(&em_tree->lock); |
10520 | if (ret != -EEXIST) | |
10521 | break; | |
dcdbc059 | 10522 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10523 | cur_offset + ins.offset - 1, |
10524 | 0); | |
10525 | } | |
10526 | free_extent_map(em); | |
10527 | next: | |
d899e052 YZ |
10528 | num_bytes -= ins.offset; |
10529 | cur_offset += ins.offset; | |
efa56464 | 10530 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10531 | |
0c4d2d95 | 10532 | inode_inc_iversion(inode); |
c2050a45 | 10533 | inode->i_ctime = current_time(inode); |
6cbff00f | 10534 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10535 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10536 | (actual_len > inode->i_size) && |
10537 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10538 | if (cur_offset > actual_len) |
55a61d1d | 10539 | i_size = actual_len; |
d1ea6a61 | 10540 | else |
55a61d1d JB |
10541 | i_size = cur_offset; |
10542 | i_size_write(inode, i_size); | |
10543 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10544 | } |
10545 | ||
d899e052 | 10546 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10547 | |
10548 | if (ret) { | |
66642832 | 10549 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10550 | if (own_trans) |
3a45bb20 | 10551 | btrfs_end_transaction(trans); |
79787eaa JM |
10552 | break; |
10553 | } | |
d899e052 | 10554 | |
0af3d00b | 10555 | if (own_trans) |
3a45bb20 | 10556 | btrfs_end_transaction(trans); |
5a303d5d | 10557 | } |
18513091 | 10558 | if (cur_offset < end) |
bc42bda2 | 10559 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
18513091 | 10560 | end - cur_offset + 1); |
d899e052 YZ |
10561 | return ret; |
10562 | } | |
10563 | ||
0af3d00b JB |
10564 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10565 | u64 start, u64 num_bytes, u64 min_size, | |
10566 | loff_t actual_len, u64 *alloc_hint) | |
10567 | { | |
10568 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10569 | min_size, actual_len, alloc_hint, | |
10570 | NULL); | |
10571 | } | |
10572 | ||
10573 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10574 | struct btrfs_trans_handle *trans, int mode, | |
10575 | u64 start, u64 num_bytes, u64 min_size, | |
10576 | loff_t actual_len, u64 *alloc_hint) | |
10577 | { | |
10578 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10579 | min_size, actual_len, alloc_hint, trans); | |
10580 | } | |
10581 | ||
e6dcd2dc CM |
10582 | static int btrfs_set_page_dirty(struct page *page) |
10583 | { | |
e6dcd2dc CM |
10584 | return __set_page_dirty_nobuffers(page); |
10585 | } | |
10586 | ||
10556cb2 | 10587 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10588 | { |
b83cc969 | 10589 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10590 | umode_t mode = inode->i_mode; |
b83cc969 | 10591 | |
cb6db4e5 JM |
10592 | if (mask & MAY_WRITE && |
10593 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10594 | if (btrfs_root_readonly(root)) | |
10595 | return -EROFS; | |
10596 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10597 | return -EACCES; | |
10598 | } | |
2830ba7f | 10599 | return generic_permission(inode, mask); |
fdebe2bd | 10600 | } |
39279cc3 | 10601 | |
ef3b9af5 FM |
10602 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10603 | { | |
2ff7e61e | 10604 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10605 | struct btrfs_trans_handle *trans; |
10606 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10607 | struct inode *inode = NULL; | |
10608 | u64 objectid; | |
10609 | u64 index; | |
10610 | int ret = 0; | |
10611 | ||
10612 | /* | |
10613 | * 5 units required for adding orphan entry | |
10614 | */ | |
10615 | trans = btrfs_start_transaction(root, 5); | |
10616 | if (IS_ERR(trans)) | |
10617 | return PTR_ERR(trans); | |
10618 | ||
10619 | ret = btrfs_find_free_ino(root, &objectid); | |
10620 | if (ret) | |
10621 | goto out; | |
10622 | ||
10623 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10624 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10625 | if (IS_ERR(inode)) { |
10626 | ret = PTR_ERR(inode); | |
10627 | inode = NULL; | |
10628 | goto out; | |
10629 | } | |
10630 | ||
ef3b9af5 FM |
10631 | inode->i_fop = &btrfs_file_operations; |
10632 | inode->i_op = &btrfs_file_inode_operations; | |
10633 | ||
10634 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10635 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10636 | ||
b0d5d10f CM |
10637 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10638 | if (ret) | |
10639 | goto out_inode; | |
10640 | ||
10641 | ret = btrfs_update_inode(trans, root, inode); | |
10642 | if (ret) | |
10643 | goto out_inode; | |
73f2e545 | 10644 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10645 | if (ret) |
b0d5d10f | 10646 | goto out_inode; |
ef3b9af5 | 10647 | |
5762b5c9 FM |
10648 | /* |
10649 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10650 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10651 | * through: | |
10652 | * | |
10653 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10654 | */ | |
10655 | set_nlink(inode, 1); | |
b0d5d10f | 10656 | unlock_new_inode(inode); |
ef3b9af5 FM |
10657 | d_tmpfile(dentry, inode); |
10658 | mark_inode_dirty(inode); | |
10659 | ||
10660 | out: | |
3a45bb20 | 10661 | btrfs_end_transaction(trans); |
ef3b9af5 FM |
10662 | if (ret) |
10663 | iput(inode); | |
2ff7e61e JM |
10664 | btrfs_balance_delayed_items(fs_info); |
10665 | btrfs_btree_balance_dirty(fs_info); | |
ef3b9af5 | 10666 | return ret; |
b0d5d10f CM |
10667 | |
10668 | out_inode: | |
10669 | unlock_new_inode(inode); | |
10670 | goto out; | |
10671 | ||
ef3b9af5 FM |
10672 | } |
10673 | ||
20a7db8a | 10674 | __attribute__((const)) |
9d0d1c8b | 10675 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) |
20a7db8a | 10676 | { |
9d0d1c8b | 10677 | return -EAGAIN; |
20a7db8a DS |
10678 | } |
10679 | ||
c6100a4b JB |
10680 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
10681 | { | |
10682 | struct inode *inode = private_data; | |
10683 | return btrfs_sb(inode->i_sb); | |
10684 | } | |
10685 | ||
10686 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, | |
10687 | u64 start, u64 end) | |
10688 | { | |
10689 | struct inode *inode = private_data; | |
10690 | u64 isize; | |
10691 | ||
10692 | isize = i_size_read(inode); | |
10693 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
10694 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, | |
10695 | "%s: ino %llu isize %llu odd range [%llu,%llu]", | |
10696 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); | |
10697 | } | |
10698 | } | |
10699 | ||
10700 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) | |
10701 | { | |
10702 | struct inode *inode = private_data; | |
10703 | unsigned long index = start >> PAGE_SHIFT; | |
10704 | unsigned long end_index = end >> PAGE_SHIFT; | |
10705 | struct page *page; | |
10706 | ||
10707 | while (index <= end_index) { | |
10708 | page = find_get_page(inode->i_mapping, index); | |
10709 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10710 | set_page_writeback(page); | |
10711 | put_page(page); | |
10712 | index++; | |
10713 | } | |
10714 | } | |
10715 | ||
6e1d5dcc | 10716 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10717 | .getattr = btrfs_getattr, |
39279cc3 CM |
10718 | .lookup = btrfs_lookup, |
10719 | .create = btrfs_create, | |
10720 | .unlink = btrfs_unlink, | |
10721 | .link = btrfs_link, | |
10722 | .mkdir = btrfs_mkdir, | |
10723 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10724 | .rename = btrfs_rename2, |
39279cc3 CM |
10725 | .symlink = btrfs_symlink, |
10726 | .setattr = btrfs_setattr, | |
618e21d5 | 10727 | .mknod = btrfs_mknod, |
5103e947 | 10728 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10729 | .permission = btrfs_permission, |
4e34e719 | 10730 | .get_acl = btrfs_get_acl, |
996a710d | 10731 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10732 | .update_time = btrfs_update_time, |
ef3b9af5 | 10733 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10734 | }; |
6e1d5dcc | 10735 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10736 | .lookup = btrfs_lookup, |
fdebe2bd | 10737 | .permission = btrfs_permission, |
93fd63c2 | 10738 | .update_time = btrfs_update_time, |
39279cc3 | 10739 | }; |
76dda93c | 10740 | |
828c0950 | 10741 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10742 | .llseek = generic_file_llseek, |
10743 | .read = generic_read_dir, | |
02dbfc99 | 10744 | .iterate_shared = btrfs_real_readdir, |
34287aa3 | 10745 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10746 | #ifdef CONFIG_COMPAT |
4c63c245 | 10747 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10748 | #endif |
6bf13c0c | 10749 | .release = btrfs_release_file, |
e02119d5 | 10750 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10751 | }; |
10752 | ||
20e5506b | 10753 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10754 | /* mandatory callbacks */ |
065631f6 | 10755 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac | 10756 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
4d53dddb | 10757 | .merge_bio_hook = btrfs_merge_bio_hook, |
9d0d1c8b | 10758 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
c6100a4b JB |
10759 | .tree_fs_info = iotree_fs_info, |
10760 | .set_range_writeback = btrfs_set_range_writeback, | |
4d53dddb DS |
10761 | |
10762 | /* optional callbacks */ | |
10763 | .fill_delalloc = run_delalloc_range, | |
e6dcd2dc | 10764 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10765 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10766 | .set_bit_hook = btrfs_set_bit_hook, |
10767 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10768 | .merge_extent_hook = btrfs_merge_extent_hook, |
10769 | .split_extent_hook = btrfs_split_extent_hook, | |
c6100a4b | 10770 | .check_extent_io_range = btrfs_check_extent_io_range, |
07157aac CM |
10771 | }; |
10772 | ||
35054394 CM |
10773 | /* |
10774 | * btrfs doesn't support the bmap operation because swapfiles | |
10775 | * use bmap to make a mapping of extents in the file. They assume | |
10776 | * these extents won't change over the life of the file and they | |
10777 | * use the bmap result to do IO directly to the drive. | |
10778 | * | |
10779 | * the btrfs bmap call would return logical addresses that aren't | |
10780 | * suitable for IO and they also will change frequently as COW | |
10781 | * operations happen. So, swapfile + btrfs == corruption. | |
10782 | * | |
10783 | * For now we're avoiding this by dropping bmap. | |
10784 | */ | |
7f09410b | 10785 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10786 | .readpage = btrfs_readpage, |
10787 | .writepage = btrfs_writepage, | |
b293f02e | 10788 | .writepages = btrfs_writepages, |
3ab2fb5a | 10789 | .readpages = btrfs_readpages, |
16432985 | 10790 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10791 | .invalidatepage = btrfs_invalidatepage, |
10792 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10793 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10794 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10795 | }; |
10796 | ||
7f09410b | 10797 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10798 | .readpage = btrfs_readpage, |
10799 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10800 | .invalidatepage = btrfs_invalidatepage, |
10801 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10802 | }; |
10803 | ||
6e1d5dcc | 10804 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10805 | .getattr = btrfs_getattr, |
10806 | .setattr = btrfs_setattr, | |
5103e947 | 10807 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10808 | .permission = btrfs_permission, |
1506fcc8 | 10809 | .fiemap = btrfs_fiemap, |
4e34e719 | 10810 | .get_acl = btrfs_get_acl, |
996a710d | 10811 | .set_acl = btrfs_set_acl, |
e41f941a | 10812 | .update_time = btrfs_update_time, |
39279cc3 | 10813 | }; |
6e1d5dcc | 10814 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10815 | .getattr = btrfs_getattr, |
10816 | .setattr = btrfs_setattr, | |
fdebe2bd | 10817 | .permission = btrfs_permission, |
33268eaf | 10818 | .listxattr = btrfs_listxattr, |
4e34e719 | 10819 | .get_acl = btrfs_get_acl, |
996a710d | 10820 | .set_acl = btrfs_set_acl, |
e41f941a | 10821 | .update_time = btrfs_update_time, |
618e21d5 | 10822 | }; |
6e1d5dcc | 10823 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10824 | .get_link = page_get_link, |
f209561a | 10825 | .getattr = btrfs_getattr, |
22c44fe6 | 10826 | .setattr = btrfs_setattr, |
fdebe2bd | 10827 | .permission = btrfs_permission, |
0279b4cd | 10828 | .listxattr = btrfs_listxattr, |
e41f941a | 10829 | .update_time = btrfs_update_time, |
39279cc3 | 10830 | }; |
76dda93c | 10831 | |
82d339d9 | 10832 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10833 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10834 | .d_release = btrfs_dentry_release, |
76dda93c | 10835 | }; |