]>
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 | ||
c2fcdcdf | 395 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
f79707b0 | 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 | 401 | return 1; |
eec63c65 DS |
402 | /* defrag ioctl */ |
403 | if (BTRFS_I(inode)->defrag_compress) | |
404 | return 1; | |
f79707b0 WS |
405 | /* bad compression ratios */ |
406 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
407 | return 0; | |
0b246afa | 408 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 | 409 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
b52aa8c9 | 410 | BTRFS_I(inode)->prop_compress) |
c2fcdcdf | 411 | return btrfs_compress_heuristic(inode, start, end); |
f79707b0 WS |
412 | return 0; |
413 | } | |
414 | ||
6158e1ce | 415 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
26d30f85 AJ |
416 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
417 | { | |
418 | /* If this is a small write inside eof, kick off a defrag */ | |
419 | if (num_bytes < small_write && | |
6158e1ce | 420 | (start > 0 || end + 1 < inode->disk_i_size)) |
26d30f85 AJ |
421 | btrfs_add_inode_defrag(NULL, inode); |
422 | } | |
423 | ||
d352ac68 | 424 | /* |
771ed689 CM |
425 | * we create compressed extents in two phases. The first |
426 | * phase compresses a range of pages that have already been | |
427 | * locked (both pages and state bits are locked). | |
c8b97818 | 428 | * |
771ed689 CM |
429 | * This is done inside an ordered work queue, and the compression |
430 | * is spread across many cpus. The actual IO submission is step | |
431 | * two, and the ordered work queue takes care of making sure that | |
432 | * happens in the same order things were put onto the queue by | |
433 | * writepages and friends. | |
c8b97818 | 434 | * |
771ed689 CM |
435 | * If this code finds it can't get good compression, it puts an |
436 | * entry onto the work queue to write the uncompressed bytes. This | |
437 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
438 | * are written in the same order that the flusher thread sent them |
439 | * down. | |
d352ac68 | 440 | */ |
c44f649e | 441 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
442 | struct page *locked_page, |
443 | u64 start, u64 end, | |
444 | struct async_cow *async_cow, | |
445 | int *num_added) | |
b888db2b | 446 | { |
0b246afa | 447 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
b888db2b | 448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
db94535d | 449 | u64 num_bytes; |
0b246afa | 450 | u64 blocksize = fs_info->sectorsize; |
c8b97818 | 451 | u64 actual_end; |
42dc7bab | 452 | u64 isize = i_size_read(inode); |
e6dcd2dc | 453 | int ret = 0; |
c8b97818 CM |
454 | struct page **pages = NULL; |
455 | unsigned long nr_pages; | |
c8b97818 CM |
456 | unsigned long total_compressed = 0; |
457 | unsigned long total_in = 0; | |
c8b97818 CM |
458 | int i; |
459 | int will_compress; | |
0b246afa | 460 | int compress_type = fs_info->compress_type; |
4adaa611 | 461 | int redirty = 0; |
b888db2b | 462 | |
6158e1ce NB |
463 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
464 | SZ_16K); | |
4cb5300b | 465 | |
42dc7bab | 466 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
467 | again: |
468 | will_compress = 0; | |
09cbfeaf | 469 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
069eac78 DS |
470 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
471 | nr_pages = min_t(unsigned long, nr_pages, | |
472 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); | |
be20aa9d | 473 | |
f03d9301 CM |
474 | /* |
475 | * we don't want to send crud past the end of i_size through | |
476 | * compression, that's just a waste of CPU time. So, if the | |
477 | * end of the file is before the start of our current | |
478 | * requested range of bytes, we bail out to the uncompressed | |
479 | * cleanup code that can deal with all of this. | |
480 | * | |
481 | * It isn't really the fastest way to fix things, but this is a | |
482 | * very uncommon corner. | |
483 | */ | |
484 | if (actual_end <= start) | |
485 | goto cleanup_and_bail_uncompressed; | |
486 | ||
c8b97818 CM |
487 | total_compressed = actual_end - start; |
488 | ||
4bcbb332 SW |
489 | /* |
490 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 491 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
492 | */ |
493 | if (total_compressed <= blocksize && | |
494 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
495 | goto cleanup_and_bail_uncompressed; | |
496 | ||
069eac78 DS |
497 | total_compressed = min_t(unsigned long, total_compressed, |
498 | BTRFS_MAX_UNCOMPRESSED); | |
fda2832f | 499 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 500 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
501 | total_in = 0; |
502 | ret = 0; | |
db94535d | 503 | |
771ed689 CM |
504 | /* |
505 | * we do compression for mount -o compress and when the | |
506 | * inode has not been flagged as nocompress. This flag can | |
507 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 508 | */ |
c2fcdcdf | 509 | if (inode_need_compress(inode, start, end)) { |
c8b97818 | 510 | WARN_ON(pages); |
31e818fe | 511 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
512 | if (!pages) { |
513 | /* just bail out to the uncompressed code */ | |
514 | goto cont; | |
515 | } | |
c8b97818 | 516 | |
eec63c65 DS |
517 | if (BTRFS_I(inode)->defrag_compress) |
518 | compress_type = BTRFS_I(inode)->defrag_compress; | |
519 | else if (BTRFS_I(inode)->prop_compress) | |
b52aa8c9 | 520 | compress_type = BTRFS_I(inode)->prop_compress; |
261507a0 | 521 | |
4adaa611 CM |
522 | /* |
523 | * we need to call clear_page_dirty_for_io on each | |
524 | * page in the range. Otherwise applications with the file | |
525 | * mmap'd can wander in and change the page contents while | |
526 | * we are compressing them. | |
527 | * | |
528 | * If the compression fails for any reason, we set the pages | |
529 | * dirty again later on. | |
530 | */ | |
531 | extent_range_clear_dirty_for_io(inode, start, end); | |
532 | redirty = 1; | |
261507a0 LZ |
533 | ret = btrfs_compress_pages(compress_type, |
534 | inode->i_mapping, start, | |
38c31464 | 535 | pages, |
4d3a800e | 536 | &nr_pages, |
261507a0 | 537 | &total_in, |
e5d74902 | 538 | &total_compressed); |
c8b97818 CM |
539 | |
540 | if (!ret) { | |
541 | unsigned long offset = total_compressed & | |
09cbfeaf | 542 | (PAGE_SIZE - 1); |
4d3a800e | 543 | struct page *page = pages[nr_pages - 1]; |
c8b97818 CM |
544 | char *kaddr; |
545 | ||
546 | /* zero the tail end of the last page, we might be | |
547 | * sending it down to disk | |
548 | */ | |
549 | if (offset) { | |
7ac687d9 | 550 | kaddr = kmap_atomic(page); |
c8b97818 | 551 | memset(kaddr + offset, 0, |
09cbfeaf | 552 | PAGE_SIZE - offset); |
7ac687d9 | 553 | kunmap_atomic(kaddr); |
c8b97818 CM |
554 | } |
555 | will_compress = 1; | |
556 | } | |
557 | } | |
560f7d75 | 558 | cont: |
c8b97818 CM |
559 | if (start == 0) { |
560 | /* lets try to make an inline extent */ | |
771ed689 | 561 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 562 | /* we didn't compress the entire range, try |
771ed689 | 563 | * to make an uncompressed inline extent. |
c8b97818 | 564 | */ |
00361589 | 565 | ret = cow_file_range_inline(root, inode, start, end, |
f74670f7 | 566 | 0, BTRFS_COMPRESS_NONE, NULL); |
c8b97818 | 567 | } else { |
771ed689 | 568 | /* try making a compressed inline extent */ |
00361589 | 569 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
570 | total_compressed, |
571 | compress_type, pages); | |
c8b97818 | 572 | } |
79787eaa | 573 | if (ret <= 0) { |
151a41bc | 574 | unsigned long clear_flags = EXTENT_DELALLOC | |
a7e3b975 | 575 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG; |
e6eb4314 FM |
576 | unsigned long page_error_op; |
577 | ||
151a41bc | 578 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 579 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 580 | |
771ed689 | 581 | /* |
79787eaa JM |
582 | * inline extent creation worked or returned error, |
583 | * we don't need to create any more async work items. | |
584 | * Unlock and free up our temp pages. | |
771ed689 | 585 | */ |
ba8b04c1 QW |
586 | extent_clear_unlock_delalloc(inode, start, end, end, |
587 | NULL, clear_flags, | |
588 | PAGE_UNLOCK | | |
c2790a2e JB |
589 | PAGE_CLEAR_DIRTY | |
590 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 591 | page_error_op | |
c2790a2e | 592 | PAGE_END_WRITEBACK); |
1c81ba23 FM |
593 | if (ret == 0) |
594 | btrfs_free_reserved_data_space_noquota(inode, | |
595 | start, | |
596 | end - start + 1); | |
c8b97818 CM |
597 | goto free_pages_out; |
598 | } | |
599 | } | |
600 | ||
601 | if (will_compress) { | |
602 | /* | |
603 | * we aren't doing an inline extent round the compressed size | |
604 | * up to a block size boundary so the allocator does sane | |
605 | * things | |
606 | */ | |
fda2832f | 607 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
608 | |
609 | /* | |
610 | * one last check to make sure the compression is really a | |
170607eb TT |
611 | * win, compare the page count read with the blocks on disk, |
612 | * compression must free at least one sector size | |
c8b97818 | 613 | */ |
09cbfeaf | 614 | total_in = ALIGN(total_in, PAGE_SIZE); |
170607eb | 615 | if (total_compressed + blocksize <= total_in) { |
c8b97818 | 616 | num_bytes = total_in; |
c8bb0c8b AS |
617 | *num_added += 1; |
618 | ||
619 | /* | |
620 | * The async work queues will take care of doing actual | |
621 | * allocation on disk for these compressed pages, and | |
622 | * will submit them to the elevator. | |
623 | */ | |
624 | add_async_extent(async_cow, start, num_bytes, | |
4d3a800e | 625 | total_compressed, pages, nr_pages, |
c8bb0c8b AS |
626 | compress_type); |
627 | ||
628 | if (start + num_bytes < end) { | |
629 | start += num_bytes; | |
630 | pages = NULL; | |
631 | cond_resched(); | |
632 | goto again; | |
633 | } | |
634 | return; | |
c8b97818 CM |
635 | } |
636 | } | |
c8bb0c8b | 637 | if (pages) { |
c8b97818 CM |
638 | /* |
639 | * the compression code ran but failed to make things smaller, | |
640 | * free any pages it allocated and our page pointer array | |
641 | */ | |
4d3a800e | 642 | for (i = 0; i < nr_pages; i++) { |
70b99e69 | 643 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 644 | put_page(pages[i]); |
c8b97818 CM |
645 | } |
646 | kfree(pages); | |
647 | pages = NULL; | |
648 | total_compressed = 0; | |
4d3a800e | 649 | nr_pages = 0; |
c8b97818 CM |
650 | |
651 | /* flag the file so we don't compress in the future */ | |
0b246afa | 652 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
b52aa8c9 | 653 | !(BTRFS_I(inode)->prop_compress)) { |
a555f810 | 654 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 655 | } |
c8b97818 | 656 | } |
f03d9301 | 657 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
658 | /* |
659 | * No compression, but we still need to write the pages in the file | |
660 | * we've been given so far. redirty the locked page if it corresponds | |
661 | * to our extent and set things up for the async work queue to run | |
662 | * cow_file_range to do the normal delalloc dance. | |
663 | */ | |
664 | if (page_offset(locked_page) >= start && | |
665 | page_offset(locked_page) <= end) | |
666 | __set_page_dirty_nobuffers(locked_page); | |
667 | /* unlocked later on in the async handlers */ | |
668 | ||
669 | if (redirty) | |
670 | extent_range_redirty_for_io(inode, start, end); | |
671 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
672 | BTRFS_COMPRESS_NONE); | |
673 | *num_added += 1; | |
3b951516 | 674 | |
c44f649e | 675 | return; |
771ed689 CM |
676 | |
677 | free_pages_out: | |
4d3a800e | 678 | for (i = 0; i < nr_pages; i++) { |
771ed689 | 679 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 680 | put_page(pages[i]); |
771ed689 | 681 | } |
d397712b | 682 | kfree(pages); |
771ed689 | 683 | } |
771ed689 | 684 | |
40ae837b FM |
685 | static void free_async_extent_pages(struct async_extent *async_extent) |
686 | { | |
687 | int i; | |
688 | ||
689 | if (!async_extent->pages) | |
690 | return; | |
691 | ||
692 | for (i = 0; i < async_extent->nr_pages; i++) { | |
693 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 694 | put_page(async_extent->pages[i]); |
40ae837b FM |
695 | } |
696 | kfree(async_extent->pages); | |
697 | async_extent->nr_pages = 0; | |
698 | async_extent->pages = NULL; | |
771ed689 CM |
699 | } |
700 | ||
701 | /* | |
702 | * phase two of compressed writeback. This is the ordered portion | |
703 | * of the code, which only gets called in the order the work was | |
704 | * queued. We walk all the async extents created by compress_file_range | |
705 | * and send them down to the disk. | |
706 | */ | |
dec8f175 | 707 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
708 | struct async_cow *async_cow) |
709 | { | |
0b246afa | 710 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
711 | struct async_extent *async_extent; |
712 | u64 alloc_hint = 0; | |
771ed689 CM |
713 | struct btrfs_key ins; |
714 | struct extent_map *em; | |
715 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
771ed689 | 716 | struct extent_io_tree *io_tree; |
f5a84ee3 | 717 | int ret = 0; |
771ed689 | 718 | |
3e04e7f1 | 719 | again: |
d397712b | 720 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
721 | async_extent = list_entry(async_cow->extents.next, |
722 | struct async_extent, list); | |
723 | list_del(&async_extent->list); | |
c8b97818 | 724 | |
771ed689 CM |
725 | io_tree = &BTRFS_I(inode)->io_tree; |
726 | ||
f5a84ee3 | 727 | retry: |
771ed689 CM |
728 | /* did the compression code fall back to uncompressed IO? */ |
729 | if (!async_extent->pages) { | |
730 | int page_started = 0; | |
731 | unsigned long nr_written = 0; | |
732 | ||
733 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 734 | async_extent->start + |
d0082371 | 735 | async_extent->ram_size - 1); |
771ed689 CM |
736 | |
737 | /* allocate blocks */ | |
f5a84ee3 JB |
738 | ret = cow_file_range(inode, async_cow->locked_page, |
739 | async_extent->start, | |
740 | async_extent->start + | |
741 | async_extent->ram_size - 1, | |
dda3245e WX |
742 | async_extent->start + |
743 | async_extent->ram_size - 1, | |
744 | &page_started, &nr_written, 0, | |
745 | NULL); | |
771ed689 | 746 | |
79787eaa JM |
747 | /* JDM XXX */ |
748 | ||
771ed689 CM |
749 | /* |
750 | * if page_started, cow_file_range inserted an | |
751 | * inline extent and took care of all the unlocking | |
752 | * and IO for us. Otherwise, we need to submit | |
753 | * all those pages down to the drive. | |
754 | */ | |
f5a84ee3 | 755 | if (!page_started && !ret) |
771ed689 CM |
756 | extent_write_locked_range(io_tree, |
757 | inode, async_extent->start, | |
d397712b | 758 | async_extent->start + |
771ed689 CM |
759 | async_extent->ram_size - 1, |
760 | btrfs_get_extent, | |
761 | WB_SYNC_ALL); | |
3e04e7f1 JB |
762 | else if (ret) |
763 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
764 | kfree(async_extent); |
765 | cond_resched(); | |
766 | continue; | |
767 | } | |
768 | ||
769 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 770 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 771 | |
18513091 | 772 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
773 | async_extent->compressed_size, |
774 | async_extent->compressed_size, | |
e570fd27 | 775 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 776 | if (ret) { |
40ae837b | 777 | free_async_extent_pages(async_extent); |
3e04e7f1 | 778 | |
fdf8e2ea JB |
779 | if (ret == -ENOSPC) { |
780 | unlock_extent(io_tree, async_extent->start, | |
781 | async_extent->start + | |
782 | async_extent->ram_size - 1); | |
ce62003f LB |
783 | |
784 | /* | |
785 | * we need to redirty the pages if we decide to | |
786 | * fallback to uncompressed IO, otherwise we | |
787 | * will not submit these pages down to lower | |
788 | * layers. | |
789 | */ | |
790 | extent_range_redirty_for_io(inode, | |
791 | async_extent->start, | |
792 | async_extent->start + | |
793 | async_extent->ram_size - 1); | |
794 | ||
79787eaa | 795 | goto retry; |
fdf8e2ea | 796 | } |
3e04e7f1 | 797 | goto out_free; |
f5a84ee3 | 798 | } |
c2167754 YZ |
799 | /* |
800 | * here we're doing allocation and writeback of the | |
801 | * compressed pages | |
802 | */ | |
6f9994db LB |
803 | em = create_io_em(inode, async_extent->start, |
804 | async_extent->ram_size, /* len */ | |
805 | async_extent->start, /* orig_start */ | |
806 | ins.objectid, /* block_start */ | |
807 | ins.offset, /* block_len */ | |
808 | ins.offset, /* orig_block_len */ | |
809 | async_extent->ram_size, /* ram_bytes */ | |
810 | async_extent->compress_type, | |
811 | BTRFS_ORDERED_COMPRESSED); | |
812 | if (IS_ERR(em)) | |
813 | /* ret value is not necessary due to void function */ | |
3e04e7f1 | 814 | goto out_free_reserve; |
6f9994db | 815 | free_extent_map(em); |
3e04e7f1 | 816 | |
261507a0 LZ |
817 | ret = btrfs_add_ordered_extent_compress(inode, |
818 | async_extent->start, | |
819 | ins.objectid, | |
820 | async_extent->ram_size, | |
821 | ins.offset, | |
822 | BTRFS_ORDERED_COMPRESSED, | |
823 | async_extent->compress_type); | |
d9f85963 | 824 | if (ret) { |
dcdbc059 NB |
825 | btrfs_drop_extent_cache(BTRFS_I(inode), |
826 | async_extent->start, | |
d9f85963 FM |
827 | async_extent->start + |
828 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 829 | goto out_free_reserve; |
d9f85963 | 830 | } |
0b246afa | 831 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 832 | |
771ed689 CM |
833 | /* |
834 | * clear dirty, set writeback and unlock the pages. | |
835 | */ | |
c2790a2e | 836 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
837 | async_extent->start + |
838 | async_extent->ram_size - 1, | |
a791e35e CM |
839 | async_extent->start + |
840 | async_extent->ram_size - 1, | |
151a41bc JB |
841 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
842 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 843 | PAGE_SET_WRITEBACK); |
4e4cbee9 | 844 | if (btrfs_submit_compressed_write(inode, |
d397712b CM |
845 | async_extent->start, |
846 | async_extent->ram_size, | |
847 | ins.objectid, | |
848 | ins.offset, async_extent->pages, | |
4e4cbee9 | 849 | async_extent->nr_pages)) { |
fce2a4e6 FM |
850 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
851 | struct page *p = async_extent->pages[0]; | |
852 | const u64 start = async_extent->start; | |
853 | const u64 end = start + async_extent->ram_size - 1; | |
854 | ||
855 | p->mapping = inode->i_mapping; | |
856 | tree->ops->writepage_end_io_hook(p, start, end, | |
857 | NULL, 0); | |
858 | p->mapping = NULL; | |
ba8b04c1 QW |
859 | extent_clear_unlock_delalloc(inode, start, end, end, |
860 | NULL, 0, | |
fce2a4e6 FM |
861 | PAGE_END_WRITEBACK | |
862 | PAGE_SET_ERROR); | |
40ae837b | 863 | free_async_extent_pages(async_extent); |
fce2a4e6 | 864 | } |
771ed689 CM |
865 | alloc_hint = ins.objectid + ins.offset; |
866 | kfree(async_extent); | |
867 | cond_resched(); | |
868 | } | |
dec8f175 | 869 | return; |
3e04e7f1 | 870 | out_free_reserve: |
0b246afa | 871 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 872 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 873 | out_free: |
c2790a2e | 874 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
875 | async_extent->start + |
876 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
877 | async_extent->start + |
878 | async_extent->ram_size - 1, | |
c2790a2e | 879 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 880 | EXTENT_DELALLOC_NEW | |
151a41bc JB |
881 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
882 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
883 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
884 | PAGE_SET_ERROR); | |
40ae837b | 885 | free_async_extent_pages(async_extent); |
79787eaa | 886 | kfree(async_extent); |
3e04e7f1 | 887 | goto again; |
771ed689 CM |
888 | } |
889 | ||
4b46fce2 JB |
890 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
891 | u64 num_bytes) | |
892 | { | |
893 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
894 | struct extent_map *em; | |
895 | u64 alloc_hint = 0; | |
896 | ||
897 | read_lock(&em_tree->lock); | |
898 | em = search_extent_mapping(em_tree, start, num_bytes); | |
899 | if (em) { | |
900 | /* | |
901 | * if block start isn't an actual block number then find the | |
902 | * first block in this inode and use that as a hint. If that | |
903 | * block is also bogus then just don't worry about it. | |
904 | */ | |
905 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
906 | free_extent_map(em); | |
907 | em = search_extent_mapping(em_tree, 0, 0); | |
908 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
909 | alloc_hint = em->block_start; | |
910 | if (em) | |
911 | free_extent_map(em); | |
912 | } else { | |
913 | alloc_hint = em->block_start; | |
914 | free_extent_map(em); | |
915 | } | |
916 | } | |
917 | read_unlock(&em_tree->lock); | |
918 | ||
919 | return alloc_hint; | |
920 | } | |
921 | ||
771ed689 CM |
922 | /* |
923 | * when extent_io.c finds a delayed allocation range in the file, | |
924 | * the call backs end up in this code. The basic idea is to | |
925 | * allocate extents on disk for the range, and create ordered data structs | |
926 | * in ram to track those extents. | |
927 | * | |
928 | * locked_page is the page that writepage had locked already. We use | |
929 | * it to make sure we don't do extra locks or unlocks. | |
930 | * | |
931 | * *page_started is set to one if we unlock locked_page and do everything | |
932 | * required to start IO on it. It may be clean and already done with | |
933 | * IO when we return. | |
934 | */ | |
00361589 JB |
935 | static noinline int cow_file_range(struct inode *inode, |
936 | struct page *locked_page, | |
dda3245e WX |
937 | u64 start, u64 end, u64 delalloc_end, |
938 | int *page_started, unsigned long *nr_written, | |
939 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 940 | { |
0b246afa | 941 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 942 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
943 | u64 alloc_hint = 0; |
944 | u64 num_bytes; | |
945 | unsigned long ram_size; | |
946 | u64 disk_num_bytes; | |
a315e68f | 947 | u64 cur_alloc_size = 0; |
0b246afa | 948 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
949 | struct btrfs_key ins; |
950 | struct extent_map *em; | |
a315e68f FM |
951 | unsigned clear_bits; |
952 | unsigned long page_ops; | |
953 | bool extent_reserved = false; | |
771ed689 CM |
954 | int ret = 0; |
955 | ||
70ddc553 | 956 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
02ecd2c2 | 957 | WARN_ON_ONCE(1); |
29bce2f3 JB |
958 | ret = -EINVAL; |
959 | goto out_unlock; | |
02ecd2c2 | 960 | } |
771ed689 | 961 | |
fda2832f | 962 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
963 | num_bytes = max(blocksize, num_bytes); |
964 | disk_num_bytes = num_bytes; | |
771ed689 | 965 | |
6158e1ce | 966 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
4cb5300b | 967 | |
771ed689 CM |
968 | if (start == 0) { |
969 | /* lets try to make an inline extent */ | |
f74670f7 AJ |
970 | ret = cow_file_range_inline(root, inode, start, end, 0, |
971 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 972 | if (ret == 0) { |
ba8b04c1 QW |
973 | extent_clear_unlock_delalloc(inode, start, end, |
974 | delalloc_end, NULL, | |
c2790a2e | 975 | EXTENT_LOCKED | EXTENT_DELALLOC | |
a7e3b975 | 976 | EXTENT_DELALLOC_NEW | |
151a41bc | 977 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
978 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
979 | PAGE_END_WRITEBACK); | |
18513091 WX |
980 | btrfs_free_reserved_data_space_noquota(inode, start, |
981 | end - start + 1); | |
771ed689 | 982 | *nr_written = *nr_written + |
09cbfeaf | 983 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 984 | *page_started = 1; |
771ed689 | 985 | goto out; |
79787eaa | 986 | } else if (ret < 0) { |
79787eaa | 987 | goto out_unlock; |
771ed689 CM |
988 | } |
989 | } | |
990 | ||
991 | BUG_ON(disk_num_bytes > | |
0b246afa | 992 | btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 993 | |
4b46fce2 | 994 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
dcdbc059 NB |
995 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
996 | start + num_bytes - 1, 0); | |
771ed689 | 997 | |
d397712b | 998 | while (disk_num_bytes > 0) { |
287a0ab9 | 999 | cur_alloc_size = disk_num_bytes; |
18513091 | 1000 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 1001 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 1002 | &ins, 1, 1); |
00361589 | 1003 | if (ret < 0) |
79787eaa | 1004 | goto out_unlock; |
a315e68f FM |
1005 | cur_alloc_size = ins.offset; |
1006 | extent_reserved = true; | |
d397712b | 1007 | |
771ed689 | 1008 | ram_size = ins.offset; |
6f9994db LB |
1009 | em = create_io_em(inode, start, ins.offset, /* len */ |
1010 | start, /* orig_start */ | |
1011 | ins.objectid, /* block_start */ | |
1012 | ins.offset, /* block_len */ | |
1013 | ins.offset, /* orig_block_len */ | |
1014 | ram_size, /* ram_bytes */ | |
1015 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
1af4a0aa | 1016 | BTRFS_ORDERED_REGULAR /* type */); |
6f9994db | 1017 | if (IS_ERR(em)) |
ace68bac | 1018 | goto out_reserve; |
6f9994db | 1019 | free_extent_map(em); |
e6dcd2dc | 1020 | |
e6dcd2dc | 1021 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1022 | ram_size, cur_alloc_size, 0); |
ace68bac | 1023 | if (ret) |
d9f85963 | 1024 | goto out_drop_extent_cache; |
c8b97818 | 1025 | |
17d217fe YZ |
1026 | if (root->root_key.objectid == |
1027 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1028 | ret = btrfs_reloc_clone_csums(inode, start, | |
1029 | cur_alloc_size); | |
4dbd80fb QW |
1030 | /* |
1031 | * Only drop cache here, and process as normal. | |
1032 | * | |
1033 | * We must not allow extent_clear_unlock_delalloc() | |
1034 | * at out_unlock label to free meta of this ordered | |
1035 | * extent, as its meta should be freed by | |
1036 | * btrfs_finish_ordered_io(). | |
1037 | * | |
1038 | * So we must continue until @start is increased to | |
1039 | * skip current ordered extent. | |
1040 | */ | |
00361589 | 1041 | if (ret) |
4dbd80fb QW |
1042 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
1043 | start + ram_size - 1, 0); | |
17d217fe YZ |
1044 | } |
1045 | ||
0b246afa | 1046 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1047 | |
c8b97818 CM |
1048 | /* we're not doing compressed IO, don't unlock the first |
1049 | * page (which the caller expects to stay locked), don't | |
1050 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1051 | * |
1052 | * Do set the Private2 bit so we know this page was properly | |
1053 | * setup for writepage | |
c8b97818 | 1054 | */ |
a315e68f FM |
1055 | page_ops = unlock ? PAGE_UNLOCK : 0; |
1056 | page_ops |= PAGE_SET_PRIVATE2; | |
a791e35e | 1057 | |
c2790a2e | 1058 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1059 | start + ram_size - 1, |
1060 | delalloc_end, locked_page, | |
c2790a2e | 1061 | EXTENT_LOCKED | EXTENT_DELALLOC, |
a315e68f | 1062 | page_ops); |
4dbd80fb QW |
1063 | if (disk_num_bytes < cur_alloc_size) |
1064 | disk_num_bytes = 0; | |
1065 | else | |
1066 | disk_num_bytes -= cur_alloc_size; | |
c59f8951 CM |
1067 | num_bytes -= cur_alloc_size; |
1068 | alloc_hint = ins.objectid + ins.offset; | |
1069 | start += cur_alloc_size; | |
a315e68f | 1070 | extent_reserved = false; |
4dbd80fb QW |
1071 | |
1072 | /* | |
1073 | * btrfs_reloc_clone_csums() error, since start is increased | |
1074 | * extent_clear_unlock_delalloc() at out_unlock label won't | |
1075 | * free metadata of current ordered extent, we're OK to exit. | |
1076 | */ | |
1077 | if (ret) | |
1078 | goto out_unlock; | |
b888db2b | 1079 | } |
79787eaa | 1080 | out: |
be20aa9d | 1081 | return ret; |
b7d5b0a8 | 1082 | |
d9f85963 | 1083 | out_drop_extent_cache: |
dcdbc059 | 1084 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
ace68bac | 1085 | out_reserve: |
0b246afa | 1086 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1087 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1088 | out_unlock: |
a7e3b975 FM |
1089 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
1090 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; | |
a315e68f FM |
1091 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
1092 | PAGE_END_WRITEBACK; | |
1093 | /* | |
1094 | * If we reserved an extent for our delalloc range (or a subrange) and | |
1095 | * failed to create the respective ordered extent, then it means that | |
1096 | * when we reserved the extent we decremented the extent's size from | |
1097 | * the data space_info's bytes_may_use counter and incremented the | |
1098 | * space_info's bytes_reserved counter by the same amount. We must make | |
1099 | * sure extent_clear_unlock_delalloc() does not try to decrement again | |
1100 | * the data space_info's bytes_may_use counter, therefore we do not pass | |
1101 | * it the flag EXTENT_CLEAR_DATA_RESV. | |
1102 | */ | |
1103 | if (extent_reserved) { | |
1104 | extent_clear_unlock_delalloc(inode, start, | |
1105 | start + cur_alloc_size, | |
1106 | start + cur_alloc_size, | |
1107 | locked_page, | |
1108 | clear_bits, | |
1109 | page_ops); | |
1110 | start += cur_alloc_size; | |
1111 | if (start >= end) | |
1112 | goto out; | |
1113 | } | |
ba8b04c1 QW |
1114 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1115 | locked_page, | |
a315e68f FM |
1116 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
1117 | page_ops); | |
79787eaa | 1118 | goto out; |
771ed689 | 1119 | } |
c8b97818 | 1120 | |
771ed689 CM |
1121 | /* |
1122 | * work queue call back to started compression on a file and pages | |
1123 | */ | |
1124 | static noinline void async_cow_start(struct btrfs_work *work) | |
1125 | { | |
1126 | struct async_cow *async_cow; | |
1127 | int num_added = 0; | |
1128 | async_cow = container_of(work, struct async_cow, work); | |
1129 | ||
1130 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1131 | async_cow->start, async_cow->end, async_cow, | |
1132 | &num_added); | |
8180ef88 | 1133 | if (num_added == 0) { |
cb77fcd8 | 1134 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1135 | async_cow->inode = NULL; |
8180ef88 | 1136 | } |
771ed689 CM |
1137 | } |
1138 | ||
1139 | /* | |
1140 | * work queue call back to submit previously compressed pages | |
1141 | */ | |
1142 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1143 | { | |
0b246afa | 1144 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
1145 | struct async_cow *async_cow; |
1146 | struct btrfs_root *root; | |
1147 | unsigned long nr_pages; | |
1148 | ||
1149 | async_cow = container_of(work, struct async_cow, work); | |
1150 | ||
1151 | root = async_cow->root; | |
0b246afa | 1152 | fs_info = root->fs_info; |
09cbfeaf KS |
1153 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1154 | PAGE_SHIFT; | |
771ed689 | 1155 | |
ee863954 DS |
1156 | /* |
1157 | * atomic_sub_return implies a barrier for waitqueue_active | |
1158 | */ | |
0b246afa | 1159 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
ee22184b | 1160 | 5 * SZ_1M && |
0b246afa JM |
1161 | waitqueue_active(&fs_info->async_submit_wait)) |
1162 | wake_up(&fs_info->async_submit_wait); | |
771ed689 | 1163 | |
d397712b | 1164 | if (async_cow->inode) |
771ed689 | 1165 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1166 | } |
c8b97818 | 1167 | |
771ed689 CM |
1168 | static noinline void async_cow_free(struct btrfs_work *work) |
1169 | { | |
1170 | struct async_cow *async_cow; | |
1171 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1172 | if (async_cow->inode) |
cb77fcd8 | 1173 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1174 | kfree(async_cow); |
1175 | } | |
1176 | ||
1177 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1178 | u64 start, u64 end, int *page_started, | |
1179 | unsigned long *nr_written) | |
1180 | { | |
0b246afa | 1181 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
1182 | struct async_cow *async_cow; |
1183 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1184 | unsigned long nr_pages; | |
1185 | u64 cur_end; | |
771ed689 | 1186 | |
a3429ab7 CM |
1187 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1188 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1189 | while (start < end) { |
771ed689 | 1190 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1191 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1192 | async_cow->inode = igrab(inode); |
771ed689 CM |
1193 | async_cow->root = root; |
1194 | async_cow->locked_page = locked_page; | |
1195 | async_cow->start = start; | |
1196 | ||
f79707b0 | 1197 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1198 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1199 | cur_end = end; |
1200 | else | |
ee22184b | 1201 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1202 | |
1203 | async_cow->end = cur_end; | |
1204 | INIT_LIST_HEAD(&async_cow->extents); | |
1205 | ||
9e0af237 LB |
1206 | btrfs_init_work(&async_cow->work, |
1207 | btrfs_delalloc_helper, | |
1208 | async_cow_start, async_cow_submit, | |
1209 | async_cow_free); | |
771ed689 | 1210 | |
09cbfeaf KS |
1211 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1212 | PAGE_SHIFT; | |
0b246afa | 1213 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1214 | |
0b246afa | 1215 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1216 | |
0b246afa JM |
1217 | while (atomic_read(&fs_info->async_submit_draining) && |
1218 | atomic_read(&fs_info->async_delalloc_pages)) { | |
1219 | wait_event(fs_info->async_submit_wait, | |
1220 | (atomic_read(&fs_info->async_delalloc_pages) == | |
1221 | 0)); | |
771ed689 CM |
1222 | } |
1223 | ||
1224 | *nr_written += nr_pages; | |
1225 | start = cur_end + 1; | |
1226 | } | |
1227 | *page_started = 1; | |
1228 | return 0; | |
be20aa9d CM |
1229 | } |
1230 | ||
2ff7e61e | 1231 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1232 | u64 bytenr, u64 num_bytes) |
1233 | { | |
1234 | int ret; | |
1235 | struct btrfs_ordered_sum *sums; | |
1236 | LIST_HEAD(list); | |
1237 | ||
0b246afa | 1238 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1239 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1240 | if (ret == 0 && list_empty(&list)) |
1241 | return 0; | |
1242 | ||
1243 | while (!list_empty(&list)) { | |
1244 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1245 | list_del(&sums->list); | |
1246 | kfree(sums); | |
1247 | } | |
1248 | return 1; | |
1249 | } | |
1250 | ||
d352ac68 CM |
1251 | /* |
1252 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1253 | * of the extents that exist in the file, and COWs the file as required. | |
1254 | * | |
1255 | * If no cow copies or snapshots exist, we write directly to the existing | |
1256 | * blocks on disk | |
1257 | */ | |
7f366cfe CM |
1258 | static noinline int run_delalloc_nocow(struct inode *inode, |
1259 | struct page *locked_page, | |
771ed689 CM |
1260 | u64 start, u64 end, int *page_started, int force, |
1261 | unsigned long *nr_written) | |
be20aa9d | 1262 | { |
0b246afa | 1263 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d CM |
1264 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1265 | struct extent_buffer *leaf; | |
be20aa9d | 1266 | struct btrfs_path *path; |
80ff3856 | 1267 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1268 | struct btrfs_key found_key; |
6f9994db | 1269 | struct extent_map *em; |
80ff3856 YZ |
1270 | u64 cow_start; |
1271 | u64 cur_offset; | |
1272 | u64 extent_end; | |
5d4f98a2 | 1273 | u64 extent_offset; |
80ff3856 YZ |
1274 | u64 disk_bytenr; |
1275 | u64 num_bytes; | |
b4939680 | 1276 | u64 disk_num_bytes; |
cc95bef6 | 1277 | u64 ram_bytes; |
80ff3856 | 1278 | int extent_type; |
79787eaa | 1279 | int ret, err; |
d899e052 | 1280 | int type; |
80ff3856 YZ |
1281 | int nocow; |
1282 | int check_prev = 1; | |
82d5902d | 1283 | bool nolock; |
4a0cc7ca | 1284 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1285 | |
1286 | path = btrfs_alloc_path(); | |
17ca04af | 1287 | if (!path) { |
ba8b04c1 QW |
1288 | extent_clear_unlock_delalloc(inode, start, end, end, |
1289 | locked_page, | |
c2790a2e | 1290 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1291 | EXTENT_DO_ACCOUNTING | |
1292 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1293 | PAGE_CLEAR_DIRTY | |
1294 | PAGE_SET_WRITEBACK | | |
1295 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1296 | return -ENOMEM; |
17ca04af | 1297 | } |
82d5902d | 1298 | |
70ddc553 | 1299 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
82d5902d | 1300 | |
80ff3856 YZ |
1301 | cow_start = (u64)-1; |
1302 | cur_offset = start; | |
1303 | while (1) { | |
e4c3b2dc | 1304 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
80ff3856 | 1305 | cur_offset, 0); |
d788a349 | 1306 | if (ret < 0) |
79787eaa | 1307 | goto error; |
80ff3856 YZ |
1308 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1309 | leaf = path->nodes[0]; | |
1310 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1311 | path->slots[0] - 1); | |
33345d01 | 1312 | if (found_key.objectid == ino && |
80ff3856 YZ |
1313 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1314 | path->slots[0]--; | |
1315 | } | |
1316 | check_prev = 0; | |
1317 | next_slot: | |
1318 | leaf = path->nodes[0]; | |
1319 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1320 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1321 | if (ret < 0) |
79787eaa | 1322 | goto error; |
80ff3856 YZ |
1323 | if (ret > 0) |
1324 | break; | |
1325 | leaf = path->nodes[0]; | |
1326 | } | |
be20aa9d | 1327 | |
80ff3856 YZ |
1328 | nocow = 0; |
1329 | disk_bytenr = 0; | |
17d217fe | 1330 | num_bytes = 0; |
80ff3856 YZ |
1331 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1332 | ||
1d512cb7 FM |
1333 | if (found_key.objectid > ino) |
1334 | break; | |
1335 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1336 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1337 | path->slots[0]++; | |
1338 | goto next_slot; | |
1339 | } | |
1340 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1341 | found_key.offset > end) |
1342 | break; | |
1343 | ||
1344 | if (found_key.offset > cur_offset) { | |
1345 | extent_end = found_key.offset; | |
e9061e21 | 1346 | extent_type = 0; |
80ff3856 YZ |
1347 | goto out_check; |
1348 | } | |
1349 | ||
1350 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1351 | struct btrfs_file_extent_item); | |
1352 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1353 | ||
cc95bef6 | 1354 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1355 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1356 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1357 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1358 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1359 | extent_end = found_key.offset + |
1360 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1361 | disk_num_bytes = |
1362 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1363 | if (extent_end <= start) { |
1364 | path->slots[0]++; | |
1365 | goto next_slot; | |
1366 | } | |
17d217fe YZ |
1367 | if (disk_bytenr == 0) |
1368 | goto out_check; | |
80ff3856 YZ |
1369 | if (btrfs_file_extent_compression(leaf, fi) || |
1370 | btrfs_file_extent_encryption(leaf, fi) || | |
1371 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1372 | goto out_check; | |
d899e052 YZ |
1373 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1374 | goto out_check; | |
2ff7e61e | 1375 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1376 | goto out_check; |
e4c3b2dc | 1377 | if (btrfs_cross_ref_exist(root, ino, |
5d4f98a2 YZ |
1378 | found_key.offset - |
1379 | extent_offset, disk_bytenr)) | |
17d217fe | 1380 | goto out_check; |
5d4f98a2 | 1381 | disk_bytenr += extent_offset; |
17d217fe YZ |
1382 | disk_bytenr += cur_offset - found_key.offset; |
1383 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1384 | /* |
1385 | * if there are pending snapshots for this root, | |
1386 | * we fall into common COW way. | |
1387 | */ | |
1388 | if (!nolock) { | |
ea14b57f | 1389 | err = btrfs_start_write_no_snapshotting(root); |
e9894fd3 WS |
1390 | if (!err) |
1391 | goto out_check; | |
1392 | } | |
17d217fe YZ |
1393 | /* |
1394 | * force cow if csum exists in the range. | |
1395 | * this ensure that csum for a given extent are | |
1396 | * either valid or do not exist. | |
1397 | */ | |
2ff7e61e | 1398 | if (csum_exist_in_range(fs_info, disk_bytenr, |
91e1f56a RK |
1399 | num_bytes)) { |
1400 | if (!nolock) | |
ea14b57f | 1401 | btrfs_end_write_no_snapshotting(root); |
17d217fe | 1402 | goto out_check; |
91e1f56a RK |
1403 | } |
1404 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { | |
1405 | if (!nolock) | |
ea14b57f | 1406 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1407 | goto out_check; |
91e1f56a | 1408 | } |
80ff3856 YZ |
1409 | nocow = 1; |
1410 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1411 | extent_end = found_key.offset + | |
514ac8ad CM |
1412 | btrfs_file_extent_inline_len(leaf, |
1413 | path->slots[0], fi); | |
da17066c | 1414 | extent_end = ALIGN(extent_end, |
0b246afa | 1415 | fs_info->sectorsize); |
80ff3856 YZ |
1416 | } else { |
1417 | BUG_ON(1); | |
1418 | } | |
1419 | out_check: | |
1420 | if (extent_end <= start) { | |
1421 | path->slots[0]++; | |
e9894fd3 | 1422 | if (!nolock && nocow) |
ea14b57f | 1423 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1424 | if (nocow) |
0b246afa | 1425 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1426 | goto next_slot; |
1427 | } | |
1428 | if (!nocow) { | |
1429 | if (cow_start == (u64)-1) | |
1430 | cow_start = cur_offset; | |
1431 | cur_offset = extent_end; | |
1432 | if (cur_offset > end) | |
1433 | break; | |
1434 | path->slots[0]++; | |
1435 | goto next_slot; | |
7ea394f1 YZ |
1436 | } |
1437 | ||
b3b4aa74 | 1438 | btrfs_release_path(path); |
80ff3856 | 1439 | if (cow_start != (u64)-1) { |
00361589 JB |
1440 | ret = cow_file_range(inode, locked_page, |
1441 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1442 | end, page_started, nr_written, 1, |
1443 | NULL); | |
e9894fd3 WS |
1444 | if (ret) { |
1445 | if (!nolock && nocow) | |
ea14b57f | 1446 | btrfs_end_write_no_snapshotting(root); |
f78c436c | 1447 | if (nocow) |
0b246afa | 1448 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1449 | disk_bytenr); |
79787eaa | 1450 | goto error; |
e9894fd3 | 1451 | } |
80ff3856 | 1452 | cow_start = (u64)-1; |
7ea394f1 | 1453 | } |
80ff3856 | 1454 | |
d899e052 | 1455 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
6f9994db LB |
1456 | u64 orig_start = found_key.offset - extent_offset; |
1457 | ||
1458 | em = create_io_em(inode, cur_offset, num_bytes, | |
1459 | orig_start, | |
1460 | disk_bytenr, /* block_start */ | |
1461 | num_bytes, /* block_len */ | |
1462 | disk_num_bytes, /* orig_block_len */ | |
1463 | ram_bytes, BTRFS_COMPRESS_NONE, | |
1464 | BTRFS_ORDERED_PREALLOC); | |
1465 | if (IS_ERR(em)) { | |
1466 | if (!nolock && nocow) | |
ea14b57f | 1467 | btrfs_end_write_no_snapshotting(root); |
6f9994db LB |
1468 | if (nocow) |
1469 | btrfs_dec_nocow_writers(fs_info, | |
1470 | disk_bytenr); | |
1471 | ret = PTR_ERR(em); | |
1472 | goto error; | |
d899e052 | 1473 | } |
6f9994db LB |
1474 | free_extent_map(em); |
1475 | } | |
1476 | ||
1477 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d899e052 YZ |
1478 | type = BTRFS_ORDERED_PREALLOC; |
1479 | } else { | |
1480 | type = BTRFS_ORDERED_NOCOW; | |
1481 | } | |
80ff3856 YZ |
1482 | |
1483 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1484 | num_bytes, num_bytes, type); |
f78c436c | 1485 | if (nocow) |
0b246afa | 1486 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1487 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1488 | |
efa56464 | 1489 | if (root->root_key.objectid == |
4dbd80fb QW |
1490 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
1491 | /* | |
1492 | * Error handled later, as we must prevent | |
1493 | * extent_clear_unlock_delalloc() in error handler | |
1494 | * from freeing metadata of created ordered extent. | |
1495 | */ | |
efa56464 YZ |
1496 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
1497 | num_bytes); | |
efa56464 | 1498 | |
c2790a2e | 1499 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1500 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1501 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1502 | EXTENT_DELALLOC | |
1503 | EXTENT_CLEAR_DATA_RESV, | |
1504 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1505 | ||
e9894fd3 | 1506 | if (!nolock && nocow) |
ea14b57f | 1507 | btrfs_end_write_no_snapshotting(root); |
80ff3856 | 1508 | cur_offset = extent_end; |
4dbd80fb QW |
1509 | |
1510 | /* | |
1511 | * btrfs_reloc_clone_csums() error, now we're OK to call error | |
1512 | * handler, as metadata for created ordered extent will only | |
1513 | * be freed by btrfs_finish_ordered_io(). | |
1514 | */ | |
1515 | if (ret) | |
1516 | goto error; | |
80ff3856 YZ |
1517 | if (cur_offset > end) |
1518 | break; | |
be20aa9d | 1519 | } |
b3b4aa74 | 1520 | btrfs_release_path(path); |
80ff3856 | 1521 | |
17ca04af | 1522 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1523 | cow_start = cur_offset; |
17ca04af JB |
1524 | cur_offset = end; |
1525 | } | |
1526 | ||
80ff3856 | 1527 | if (cow_start != (u64)-1) { |
dda3245e WX |
1528 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1529 | page_started, nr_written, 1, NULL); | |
d788a349 | 1530 | if (ret) |
79787eaa | 1531 | goto error; |
80ff3856 YZ |
1532 | } |
1533 | ||
79787eaa | 1534 | error: |
17ca04af | 1535 | if (ret && cur_offset < end) |
ba8b04c1 | 1536 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1537 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1538 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1539 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1540 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1541 | PAGE_SET_WRITEBACK | |
1542 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1543 | btrfs_free_path(path); |
79787eaa | 1544 | return ret; |
be20aa9d CM |
1545 | } |
1546 | ||
47059d93 WS |
1547 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1548 | { | |
1549 | ||
1550 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1551 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1552 | return 0; | |
1553 | ||
1554 | /* | |
1555 | * @defrag_bytes is a hint value, no spinlock held here, | |
1556 | * if is not zero, it means the file is defragging. | |
1557 | * Force cow if given extent needs to be defragged. | |
1558 | */ | |
1559 | if (BTRFS_I(inode)->defrag_bytes && | |
1560 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1561 | EXTENT_DEFRAG, 0, NULL)) | |
1562 | return 1; | |
1563 | ||
1564 | return 0; | |
1565 | } | |
1566 | ||
d352ac68 CM |
1567 | /* |
1568 | * extent_io.c call back to do delayed allocation processing | |
1569 | */ | |
c6100a4b | 1570 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
771ed689 CM |
1571 | u64 start, u64 end, int *page_started, |
1572 | unsigned long *nr_written) | |
be20aa9d | 1573 | { |
c6100a4b | 1574 | struct inode *inode = private_data; |
be20aa9d | 1575 | int ret; |
47059d93 | 1576 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1577 | |
47059d93 | 1578 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1579 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1580 | page_started, 1, nr_written); |
47059d93 | 1581 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1582 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1583 | page_started, 0, nr_written); |
c2fcdcdf | 1584 | } else if (!inode_need_compress(inode, start, end)) { |
dda3245e WX |
1585 | ret = cow_file_range(inode, locked_page, start, end, end, |
1586 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1587 | } else { |
1588 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1589 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1590 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1591 | page_started, nr_written); |
7ddf5a42 | 1592 | } |
52427260 QW |
1593 | if (ret) |
1594 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); | |
b888db2b CM |
1595 | return ret; |
1596 | } | |
1597 | ||
c6100a4b | 1598 | static void btrfs_split_extent_hook(void *private_data, |
1bf85046 | 1599 | struct extent_state *orig, u64 split) |
9ed74f2d | 1600 | { |
c6100a4b | 1601 | struct inode *inode = private_data; |
dcab6a3b JB |
1602 | u64 size; |
1603 | ||
0ca1f7ce | 1604 | /* not delalloc, ignore it */ |
9ed74f2d | 1605 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1606 | return; |
9ed74f2d | 1607 | |
dcab6a3b JB |
1608 | size = orig->end - orig->start + 1; |
1609 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1610 | u32 num_extents; |
dcab6a3b JB |
1611 | u64 new_size; |
1612 | ||
1613 | /* | |
ba117213 JB |
1614 | * See the explanation in btrfs_merge_extent_hook, the same |
1615 | * applies here, just in reverse. | |
dcab6a3b JB |
1616 | */ |
1617 | new_size = orig->end - split + 1; | |
823bb20a | 1618 | num_extents = count_max_extents(new_size); |
ba117213 | 1619 | new_size = split - orig->start; |
823bb20a DS |
1620 | num_extents += count_max_extents(new_size); |
1621 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1622 | return; |
1623 | } | |
1624 | ||
9e0baf60 JB |
1625 | spin_lock(&BTRFS_I(inode)->lock); |
1626 | BTRFS_I(inode)->outstanding_extents++; | |
1627 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1628 | } |
1629 | ||
1630 | /* | |
1631 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1632 | * extents so we can keep track of new extents that are just merged onto old | |
1633 | * extents, such as when we are doing sequential writes, so we can properly | |
1634 | * account for the metadata space we'll need. | |
1635 | */ | |
c6100a4b | 1636 | static void btrfs_merge_extent_hook(void *private_data, |
1bf85046 JM |
1637 | struct extent_state *new, |
1638 | struct extent_state *other) | |
9ed74f2d | 1639 | { |
c6100a4b | 1640 | struct inode *inode = private_data; |
dcab6a3b | 1641 | u64 new_size, old_size; |
823bb20a | 1642 | u32 num_extents; |
dcab6a3b | 1643 | |
9ed74f2d JB |
1644 | /* not delalloc, ignore it */ |
1645 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1646 | return; |
9ed74f2d | 1647 | |
8461a3de JB |
1648 | if (new->start > other->start) |
1649 | new_size = new->end - other->start + 1; | |
1650 | else | |
1651 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1652 | |
1653 | /* we're not bigger than the max, unreserve the space and go */ | |
1654 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1655 | spin_lock(&BTRFS_I(inode)->lock); | |
1656 | BTRFS_I(inode)->outstanding_extents--; | |
1657 | spin_unlock(&BTRFS_I(inode)->lock); | |
1658 | return; | |
1659 | } | |
1660 | ||
1661 | /* | |
ba117213 JB |
1662 | * We have to add up either side to figure out how many extents were |
1663 | * accounted for before we merged into one big extent. If the number of | |
1664 | * extents we accounted for is <= the amount we need for the new range | |
1665 | * then we can return, otherwise drop. Think of it like this | |
1666 | * | |
1667 | * [ 4k][MAX_SIZE] | |
1668 | * | |
1669 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1670 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1671 | * we have 1 so they are == and we can return. But in this case | |
1672 | * | |
1673 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1674 | * | |
1675 | * Each range on their own accounts for 2 extents, but merged together | |
1676 | * they are only 3 extents worth of accounting, so we need to drop in | |
1677 | * this case. | |
dcab6a3b | 1678 | */ |
ba117213 | 1679 | old_size = other->end - other->start + 1; |
823bb20a | 1680 | num_extents = count_max_extents(old_size); |
ba117213 | 1681 | old_size = new->end - new->start + 1; |
823bb20a DS |
1682 | num_extents += count_max_extents(old_size); |
1683 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1684 | return; |
1685 | ||
9e0baf60 JB |
1686 | spin_lock(&BTRFS_I(inode)->lock); |
1687 | BTRFS_I(inode)->outstanding_extents--; | |
1688 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1689 | } |
1690 | ||
eb73c1b7 MX |
1691 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1692 | struct inode *inode) | |
1693 | { | |
0b246afa JM |
1694 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1695 | ||
eb73c1b7 MX |
1696 | spin_lock(&root->delalloc_lock); |
1697 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1698 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1699 | &root->delalloc_inodes); | |
1700 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1701 | &BTRFS_I(inode)->runtime_flags); | |
1702 | root->nr_delalloc_inodes++; | |
1703 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1704 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1705 | BUG_ON(!list_empty(&root->delalloc_root)); |
1706 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1707 | &fs_info->delalloc_roots); |
1708 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1709 | } |
1710 | } | |
1711 | spin_unlock(&root->delalloc_lock); | |
1712 | } | |
1713 | ||
1714 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
9e3e97f4 | 1715 | struct btrfs_inode *inode) |
eb73c1b7 | 1716 | { |
9e3e97f4 | 1717 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
0b246afa | 1718 | |
eb73c1b7 | 1719 | spin_lock(&root->delalloc_lock); |
9e3e97f4 NB |
1720 | if (!list_empty(&inode->delalloc_inodes)) { |
1721 | list_del_init(&inode->delalloc_inodes); | |
eb73c1b7 | 1722 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1723 | &inode->runtime_flags); |
eb73c1b7 MX |
1724 | root->nr_delalloc_inodes--; |
1725 | if (!root->nr_delalloc_inodes) { | |
0b246afa | 1726 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1727 | BUG_ON(list_empty(&root->delalloc_root)); |
1728 | list_del_init(&root->delalloc_root); | |
0b246afa | 1729 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1730 | } |
1731 | } | |
1732 | spin_unlock(&root->delalloc_lock); | |
1733 | } | |
1734 | ||
d352ac68 CM |
1735 | /* |
1736 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1737 | * bytes in this file, and to maintain the list of inodes that | |
1738 | * have pending delalloc work to be done. | |
1739 | */ | |
c6100a4b | 1740 | static void btrfs_set_bit_hook(void *private_data, |
9ee49a04 | 1741 | struct extent_state *state, unsigned *bits) |
291d673e | 1742 | { |
c6100a4b | 1743 | struct inode *inode = private_data; |
9ed74f2d | 1744 | |
0b246afa JM |
1745 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1746 | ||
47059d93 WS |
1747 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1748 | WARN_ON(1); | |
75eff68e CM |
1749 | /* |
1750 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1751 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1752 | * bit, which is only set or cleared with irqs on |
1753 | */ | |
0ca1f7ce | 1754 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1755 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1756 | u64 len = state->end + 1 - state->start; |
70ddc553 | 1757 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
9ed74f2d | 1758 | |
9e0baf60 | 1759 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1760 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1761 | } else { |
1762 | spin_lock(&BTRFS_I(inode)->lock); | |
1763 | BTRFS_I(inode)->outstanding_extents++; | |
1764 | spin_unlock(&BTRFS_I(inode)->lock); | |
1765 | } | |
287a0ab9 | 1766 | |
6a3891c5 | 1767 | /* For sanity tests */ |
0b246afa | 1768 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1769 | return; |
1770 | ||
104b4e51 NB |
1771 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
1772 | fs_info->delalloc_batch); | |
df0af1a5 | 1773 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1774 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1775 | if (*bits & EXTENT_DEFRAG) |
1776 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1777 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1778 | &BTRFS_I(inode)->runtime_flags)) |
1779 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1780 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1781 | } |
a7e3b975 FM |
1782 | |
1783 | if (!(state->state & EXTENT_DELALLOC_NEW) && | |
1784 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1785 | spin_lock(&BTRFS_I(inode)->lock); | |
1786 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - | |
1787 | state->start; | |
1788 | spin_unlock(&BTRFS_I(inode)->lock); | |
1789 | } | |
291d673e CM |
1790 | } |
1791 | ||
d352ac68 CM |
1792 | /* |
1793 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1794 | */ | |
c6100a4b | 1795 | static void btrfs_clear_bit_hook(void *private_data, |
41074888 | 1796 | struct extent_state *state, |
9ee49a04 | 1797 | unsigned *bits) |
291d673e | 1798 | { |
c6100a4b | 1799 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
6fc0ef68 | 1800 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
47059d93 | 1801 | u64 len = state->end + 1 - state->start; |
823bb20a | 1802 | u32 num_extents = count_max_extents(len); |
47059d93 | 1803 | |
4a4b964f FM |
1804 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
1805 | spin_lock(&inode->lock); | |
6fc0ef68 | 1806 | inode->defrag_bytes -= len; |
4a4b964f FM |
1807 | spin_unlock(&inode->lock); |
1808 | } | |
47059d93 | 1809 | |
75eff68e CM |
1810 | /* |
1811 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1812 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1813 | * bit, which is only set or cleared with irqs on |
1814 | */ | |
0ca1f7ce | 1815 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
6fc0ef68 | 1816 | struct btrfs_root *root = inode->root; |
83eea1f1 | 1817 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1818 | |
9e0baf60 | 1819 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1820 | *bits &= ~EXTENT_FIRST_DELALLOC; |
a315e68f | 1821 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { |
6fc0ef68 NB |
1822 | spin_lock(&inode->lock); |
1823 | inode->outstanding_extents -= num_extents; | |
1824 | spin_unlock(&inode->lock); | |
9e0baf60 | 1825 | } |
0ca1f7ce | 1826 | |
b6d08f06 JB |
1827 | /* |
1828 | * We don't reserve metadata space for space cache inodes so we | |
1829 | * don't need to call dellalloc_release_metadata if there is an | |
1830 | * error. | |
1831 | */ | |
a315e68f | 1832 | if (*bits & EXTENT_CLEAR_META_RESV && |
0b246afa | 1833 | root != fs_info->tree_root) |
0ca1f7ce YZ |
1834 | btrfs_delalloc_release_metadata(inode, len); |
1835 | ||
6a3891c5 | 1836 | /* For sanity tests. */ |
0b246afa | 1837 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1838 | return; |
1839 | ||
a315e68f FM |
1840 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
1841 | do_list && !(state->state & EXTENT_NORESERVE) && | |
1842 | (*bits & EXTENT_CLEAR_DATA_RESV)) | |
6fc0ef68 NB |
1843 | btrfs_free_reserved_data_space_noquota( |
1844 | &inode->vfs_inode, | |
51773bec | 1845 | state->start, len); |
9ed74f2d | 1846 | |
104b4e51 NB |
1847 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
1848 | fs_info->delalloc_batch); | |
6fc0ef68 NB |
1849 | spin_lock(&inode->lock); |
1850 | inode->delalloc_bytes -= len; | |
1851 | if (do_list && inode->delalloc_bytes == 0 && | |
df0af1a5 | 1852 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
9e3e97f4 | 1853 | &inode->runtime_flags)) |
eb73c1b7 | 1854 | btrfs_del_delalloc_inode(root, inode); |
6fc0ef68 | 1855 | spin_unlock(&inode->lock); |
291d673e | 1856 | } |
a7e3b975 FM |
1857 | |
1858 | if ((state->state & EXTENT_DELALLOC_NEW) && | |
1859 | (*bits & EXTENT_DELALLOC_NEW)) { | |
1860 | spin_lock(&inode->lock); | |
1861 | ASSERT(inode->new_delalloc_bytes >= len); | |
1862 | inode->new_delalloc_bytes -= len; | |
1863 | spin_unlock(&inode->lock); | |
1864 | } | |
291d673e CM |
1865 | } |
1866 | ||
d352ac68 CM |
1867 | /* |
1868 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1869 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1870 | * |
1871 | * return 1 if page cannot be merged to bio | |
1872 | * return 0 if page can be merged to bio | |
1873 | * return error otherwise | |
d352ac68 | 1874 | */ |
81a75f67 | 1875 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1876 | size_t size, struct bio *bio, |
1877 | unsigned long bio_flags) | |
239b14b3 | 1878 | { |
0b246afa JM |
1879 | struct inode *inode = page->mapping->host; |
1880 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1881 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1882 | u64 length = 0; |
1883 | u64 map_length; | |
239b14b3 CM |
1884 | int ret; |
1885 | ||
771ed689 CM |
1886 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1887 | return 0; | |
1888 | ||
4f024f37 | 1889 | length = bio->bi_iter.bi_size; |
239b14b3 | 1890 | map_length = length; |
0b246afa JM |
1891 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1892 | NULL, 0); | |
6f034ece LB |
1893 | if (ret < 0) |
1894 | return ret; | |
d397712b | 1895 | if (map_length < length + size) |
239b14b3 | 1896 | return 1; |
3444a972 | 1897 | return 0; |
239b14b3 CM |
1898 | } |
1899 | ||
d352ac68 CM |
1900 | /* |
1901 | * in order to insert checksums into the metadata in large chunks, | |
1902 | * we wait until bio submission time. All the pages in the bio are | |
1903 | * checksummed and sums are attached onto the ordered extent record. | |
1904 | * | |
1905 | * At IO completion time the cums attached on the ordered extent record | |
1906 | * are inserted into the btree | |
1907 | */ | |
8c27cb35 | 1908 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
81a75f67 | 1909 | int mirror_num, unsigned long bio_flags, |
eaf25d93 | 1910 | u64 bio_offset) |
065631f6 | 1911 | { |
c6100a4b | 1912 | struct inode *inode = private_data; |
4e4cbee9 | 1913 | blk_status_t ret = 0; |
e015640f | 1914 | |
2ff7e61e | 1915 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1916 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1917 | return 0; |
1918 | } | |
e015640f | 1919 | |
4a69a410 CM |
1920 | /* |
1921 | * in order to insert checksums into the metadata in large chunks, | |
1922 | * we wait until bio submission time. All the pages in the bio are | |
1923 | * checksummed and sums are attached onto the ordered extent record. | |
1924 | * | |
1925 | * At IO completion time the cums attached on the ordered extent record | |
1926 | * are inserted into the btree | |
1927 | */ | |
8c27cb35 | 1928 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
eaf25d93 CM |
1929 | int mirror_num, unsigned long bio_flags, |
1930 | u64 bio_offset) | |
4a69a410 | 1931 | { |
c6100a4b | 1932 | struct inode *inode = private_data; |
2ff7e61e | 1933 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4e4cbee9 | 1934 | blk_status_t ret; |
61891923 | 1935 | |
2ff7e61e | 1936 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
4246a0b6 | 1937 | if (ret) { |
4e4cbee9 | 1938 | bio->bi_status = ret; |
4246a0b6 CH |
1939 | bio_endio(bio); |
1940 | } | |
61891923 | 1941 | return ret; |
44b8bd7e CM |
1942 | } |
1943 | ||
d352ac68 | 1944 | /* |
cad321ad CM |
1945 | * extent_io.c submission hook. This does the right thing for csum calculation |
1946 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1947 | */ |
8c27cb35 | 1948 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
c6100a4b JB |
1949 | int mirror_num, unsigned long bio_flags, |
1950 | u64 bio_offset) | |
44b8bd7e | 1951 | { |
c6100a4b | 1952 | struct inode *inode = private_data; |
0b246afa | 1953 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1954 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1955 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
4e4cbee9 | 1956 | blk_status_t ret = 0; |
19b9bdb0 | 1957 | int skip_sum; |
b812ce28 | 1958 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1959 | |
6cbff00f | 1960 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1961 | |
70ddc553 | 1962 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
0d51e28a | 1963 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1964 | |
37226b21 | 1965 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1966 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1967 | if (ret) |
61891923 | 1968 | goto out; |
5fd02043 | 1969 | |
d20f7043 | 1970 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1971 | ret = btrfs_submit_compressed_read(inode, bio, |
1972 | mirror_num, | |
1973 | bio_flags); | |
1974 | goto out; | |
c2db1073 | 1975 | } else if (!skip_sum) { |
2ff7e61e | 1976 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1977 | if (ret) |
61891923 | 1978 | goto out; |
c2db1073 | 1979 | } |
4d1b5fb4 | 1980 | goto mapit; |
b812ce28 | 1981 | } else if (async && !skip_sum) { |
17d217fe YZ |
1982 | /* csum items have already been cloned */ |
1983 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1984 | goto mapit; | |
19b9bdb0 | 1985 | /* we're doing a write, do the async checksumming */ |
c6100a4b JB |
1986 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
1987 | bio_offset, inode, | |
0b246afa JM |
1988 | __btrfs_submit_bio_start, |
1989 | __btrfs_submit_bio_done); | |
61891923 | 1990 | goto out; |
b812ce28 | 1991 | } else if (!skip_sum) { |
2ff7e61e | 1992 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
1993 | if (ret) |
1994 | goto out; | |
19b9bdb0 CM |
1995 | } |
1996 | ||
0b86a832 | 1997 | mapit: |
2ff7e61e | 1998 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
1999 | |
2000 | out: | |
4e4cbee9 CH |
2001 | if (ret) { |
2002 | bio->bi_status = ret; | |
4246a0b6 CH |
2003 | bio_endio(bio); |
2004 | } | |
61891923 | 2005 | return ret; |
065631f6 | 2006 | } |
6885f308 | 2007 | |
d352ac68 CM |
2008 | /* |
2009 | * given a list of ordered sums record them in the inode. This happens | |
2010 | * at IO completion time based on sums calculated at bio submission time. | |
2011 | */ | |
ba1da2f4 | 2012 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
df9f628e | 2013 | struct inode *inode, struct list_head *list) |
e6dcd2dc | 2014 | { |
e6dcd2dc CM |
2015 | struct btrfs_ordered_sum *sum; |
2016 | ||
c6e30871 | 2017 | list_for_each_entry(sum, list, list) { |
39847c4d | 2018 | trans->adding_csums = 1; |
d20f7043 CM |
2019 | btrfs_csum_file_blocks(trans, |
2020 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 2021 | trans->adding_csums = 0; |
e6dcd2dc CM |
2022 | } |
2023 | return 0; | |
2024 | } | |
2025 | ||
2ac55d41 | 2026 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2027 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2028 | { |
09cbfeaf | 2029 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2030 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2031 | cached_state); |
ea8c2819 CM |
2032 | } |
2033 | ||
d352ac68 | 2034 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2035 | struct btrfs_writepage_fixup { |
2036 | struct page *page; | |
2037 | struct btrfs_work work; | |
2038 | }; | |
2039 | ||
b2950863 | 2040 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2041 | { |
2042 | struct btrfs_writepage_fixup *fixup; | |
2043 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2044 | struct extent_state *cached_state = NULL; |
364ecf36 | 2045 | struct extent_changeset *data_reserved = NULL; |
247e743c CM |
2046 | struct page *page; |
2047 | struct inode *inode; | |
2048 | u64 page_start; | |
2049 | u64 page_end; | |
87826df0 | 2050 | int ret; |
247e743c CM |
2051 | |
2052 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2053 | page = fixup->page; | |
4a096752 | 2054 | again: |
247e743c CM |
2055 | lock_page(page); |
2056 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2057 | ClearPageChecked(page); | |
2058 | goto out_page; | |
2059 | } | |
2060 | ||
2061 | inode = page->mapping->host; | |
2062 | page_start = page_offset(page); | |
09cbfeaf | 2063 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2064 | |
ff13db41 | 2065 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2066 | &cached_state); |
4a096752 CM |
2067 | |
2068 | /* already ordered? We're done */ | |
8b62b72b | 2069 | if (PagePrivate2(page)) |
247e743c | 2070 | goto out; |
4a096752 | 2071 | |
a776c6fa | 2072 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
09cbfeaf | 2073 | PAGE_SIZE); |
4a096752 | 2074 | if (ordered) { |
2ac55d41 JB |
2075 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2076 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2077 | unlock_page(page); |
2078 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2079 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2080 | goto again; |
2081 | } | |
247e743c | 2082 | |
364ecf36 | 2083 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
09cbfeaf | 2084 | PAGE_SIZE); |
87826df0 JM |
2085 | if (ret) { |
2086 | mapping_set_error(page->mapping, ret); | |
2087 | end_extent_writepage(page, ret, page_start, page_end); | |
2088 | ClearPageChecked(page); | |
2089 | goto out; | |
2090 | } | |
2091 | ||
ba8b04c1 QW |
2092 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
2093 | 0); | |
247e743c | 2094 | ClearPageChecked(page); |
87826df0 | 2095 | set_page_dirty(page); |
247e743c | 2096 | out: |
2ac55d41 JB |
2097 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2098 | &cached_state, GFP_NOFS); | |
247e743c CM |
2099 | out_page: |
2100 | unlock_page(page); | |
09cbfeaf | 2101 | put_page(page); |
b897abec | 2102 | kfree(fixup); |
364ecf36 | 2103 | extent_changeset_free(data_reserved); |
247e743c CM |
2104 | } |
2105 | ||
2106 | /* | |
2107 | * There are a few paths in the higher layers of the kernel that directly | |
2108 | * set the page dirty bit without asking the filesystem if it is a | |
2109 | * good idea. This causes problems because we want to make sure COW | |
2110 | * properly happens and the data=ordered rules are followed. | |
2111 | * | |
c8b97818 | 2112 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2113 | * hasn't been properly setup for IO. We kick off an async process |
2114 | * to fix it up. The async helper will wait for ordered extents, set | |
2115 | * the delalloc bit and make it safe to write the page. | |
2116 | */ | |
b2950863 | 2117 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2118 | { |
2119 | struct inode *inode = page->mapping->host; | |
0b246afa | 2120 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2121 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2122 | |
8b62b72b CM |
2123 | /* this page is properly in the ordered list */ |
2124 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2125 | return 0; |
2126 | ||
2127 | if (PageChecked(page)) | |
2128 | return -EAGAIN; | |
2129 | ||
2130 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2131 | if (!fixup) | |
2132 | return -EAGAIN; | |
f421950f | 2133 | |
247e743c | 2134 | SetPageChecked(page); |
09cbfeaf | 2135 | get_page(page); |
9e0af237 LB |
2136 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2137 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2138 | fixup->page = page; |
0b246afa | 2139 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2140 | return -EBUSY; |
247e743c CM |
2141 | } |
2142 | ||
d899e052 YZ |
2143 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2144 | struct inode *inode, u64 file_pos, | |
2145 | u64 disk_bytenr, u64 disk_num_bytes, | |
2146 | u64 num_bytes, u64 ram_bytes, | |
2147 | u8 compression, u8 encryption, | |
2148 | u16 other_encoding, int extent_type) | |
2149 | { | |
2150 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2151 | struct btrfs_file_extent_item *fi; | |
2152 | struct btrfs_path *path; | |
2153 | struct extent_buffer *leaf; | |
2154 | struct btrfs_key ins; | |
a12b877b | 2155 | u64 qg_released; |
1acae57b | 2156 | int extent_inserted = 0; |
d899e052 YZ |
2157 | int ret; |
2158 | ||
2159 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2160 | if (!path) |
2161 | return -ENOMEM; | |
d899e052 | 2162 | |
a1ed835e CM |
2163 | /* |
2164 | * we may be replacing one extent in the tree with another. | |
2165 | * The new extent is pinned in the extent map, and we don't want | |
2166 | * to drop it from the cache until it is completely in the btree. | |
2167 | * | |
2168 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2169 | * the caller is expected to unpin it and allow it to be merged | |
2170 | * with the others. | |
2171 | */ | |
1acae57b FDBM |
2172 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2173 | file_pos + num_bytes, NULL, 0, | |
2174 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2175 | if (ret) |
2176 | goto out; | |
d899e052 | 2177 | |
1acae57b | 2178 | if (!extent_inserted) { |
4a0cc7ca | 2179 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2180 | ins.offset = file_pos; |
2181 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2182 | ||
2183 | path->leave_spinning = 1; | |
2184 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2185 | sizeof(*fi)); | |
2186 | if (ret) | |
2187 | goto out; | |
2188 | } | |
d899e052 YZ |
2189 | leaf = path->nodes[0]; |
2190 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2191 | struct btrfs_file_extent_item); | |
2192 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2193 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2194 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2195 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2196 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2197 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2198 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2199 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2200 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2201 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2202 | |
d899e052 | 2203 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2204 | btrfs_release_path(path); |
d899e052 YZ |
2205 | |
2206 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2207 | |
2208 | ins.objectid = disk_bytenr; | |
2209 | ins.offset = disk_num_bytes; | |
2210 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
a12b877b | 2211 | |
297d750b | 2212 | /* |
5846a3c2 QW |
2213 | * Release the reserved range from inode dirty range map, as it is |
2214 | * already moved into delayed_ref_head | |
297d750b | 2215 | */ |
a12b877b QW |
2216 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
2217 | if (ret < 0) | |
2218 | goto out; | |
2219 | qg_released = ret; | |
2220 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, | |
2221 | btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins); | |
79787eaa | 2222 | out: |
d899e052 | 2223 | btrfs_free_path(path); |
b9473439 | 2224 | |
79787eaa | 2225 | return ret; |
d899e052 YZ |
2226 | } |
2227 | ||
38c227d8 LB |
2228 | /* snapshot-aware defrag */ |
2229 | struct sa_defrag_extent_backref { | |
2230 | struct rb_node node; | |
2231 | struct old_sa_defrag_extent *old; | |
2232 | u64 root_id; | |
2233 | u64 inum; | |
2234 | u64 file_pos; | |
2235 | u64 extent_offset; | |
2236 | u64 num_bytes; | |
2237 | u64 generation; | |
2238 | }; | |
2239 | ||
2240 | struct old_sa_defrag_extent { | |
2241 | struct list_head list; | |
2242 | struct new_sa_defrag_extent *new; | |
2243 | ||
2244 | u64 extent_offset; | |
2245 | u64 bytenr; | |
2246 | u64 offset; | |
2247 | u64 len; | |
2248 | int count; | |
2249 | }; | |
2250 | ||
2251 | struct new_sa_defrag_extent { | |
2252 | struct rb_root root; | |
2253 | struct list_head head; | |
2254 | struct btrfs_path *path; | |
2255 | struct inode *inode; | |
2256 | u64 file_pos; | |
2257 | u64 len; | |
2258 | u64 bytenr; | |
2259 | u64 disk_len; | |
2260 | u8 compress_type; | |
2261 | }; | |
2262 | ||
2263 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2264 | struct sa_defrag_extent_backref *b2) | |
2265 | { | |
2266 | if (b1->root_id < b2->root_id) | |
2267 | return -1; | |
2268 | else if (b1->root_id > b2->root_id) | |
2269 | return 1; | |
2270 | ||
2271 | if (b1->inum < b2->inum) | |
2272 | return -1; | |
2273 | else if (b1->inum > b2->inum) | |
2274 | return 1; | |
2275 | ||
2276 | if (b1->file_pos < b2->file_pos) | |
2277 | return -1; | |
2278 | else if (b1->file_pos > b2->file_pos) | |
2279 | return 1; | |
2280 | ||
2281 | /* | |
2282 | * [------------------------------] ===> (a range of space) | |
2283 | * |<--->| |<---->| =============> (fs/file tree A) | |
2284 | * |<---------------------------->| ===> (fs/file tree B) | |
2285 | * | |
2286 | * A range of space can refer to two file extents in one tree while | |
2287 | * refer to only one file extent in another tree. | |
2288 | * | |
2289 | * So we may process a disk offset more than one time(two extents in A) | |
2290 | * and locate at the same extent(one extent in B), then insert two same | |
2291 | * backrefs(both refer to the extent in B). | |
2292 | */ | |
2293 | return 0; | |
2294 | } | |
2295 | ||
2296 | static void backref_insert(struct rb_root *root, | |
2297 | struct sa_defrag_extent_backref *backref) | |
2298 | { | |
2299 | struct rb_node **p = &root->rb_node; | |
2300 | struct rb_node *parent = NULL; | |
2301 | struct sa_defrag_extent_backref *entry; | |
2302 | int ret; | |
2303 | ||
2304 | while (*p) { | |
2305 | parent = *p; | |
2306 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2307 | ||
2308 | ret = backref_comp(backref, entry); | |
2309 | if (ret < 0) | |
2310 | p = &(*p)->rb_left; | |
2311 | else | |
2312 | p = &(*p)->rb_right; | |
2313 | } | |
2314 | ||
2315 | rb_link_node(&backref->node, parent, p); | |
2316 | rb_insert_color(&backref->node, root); | |
2317 | } | |
2318 | ||
2319 | /* | |
2320 | * Note the backref might has changed, and in this case we just return 0. | |
2321 | */ | |
2322 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2323 | void *ctx) | |
2324 | { | |
2325 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2326 | struct old_sa_defrag_extent *old = ctx; |
2327 | struct new_sa_defrag_extent *new = old->new; | |
2328 | struct btrfs_path *path = new->path; | |
2329 | struct btrfs_key key; | |
2330 | struct btrfs_root *root; | |
2331 | struct sa_defrag_extent_backref *backref; | |
2332 | struct extent_buffer *leaf; | |
2333 | struct inode *inode = new->inode; | |
0b246afa | 2334 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2335 | int slot; |
2336 | int ret; | |
2337 | u64 extent_offset; | |
2338 | u64 num_bytes; | |
2339 | ||
2340 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2341 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2342 | return 0; |
2343 | ||
2344 | key.objectid = root_id; | |
2345 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2346 | key.offset = (u64)-1; | |
2347 | ||
38c227d8 LB |
2348 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2349 | if (IS_ERR(root)) { | |
2350 | if (PTR_ERR(root) == -ENOENT) | |
2351 | return 0; | |
2352 | WARN_ON(1); | |
ab8d0fc4 | 2353 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2354 | inum, offset, root_id); |
2355 | return PTR_ERR(root); | |
2356 | } | |
2357 | ||
2358 | key.objectid = inum; | |
2359 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2360 | if (offset > (u64)-1 << 32) | |
2361 | key.offset = 0; | |
2362 | else | |
2363 | key.offset = offset; | |
2364 | ||
2365 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2366 | if (WARN_ON(ret < 0)) |
38c227d8 | 2367 | return ret; |
50f1319c | 2368 | ret = 0; |
38c227d8 LB |
2369 | |
2370 | while (1) { | |
2371 | cond_resched(); | |
2372 | ||
2373 | leaf = path->nodes[0]; | |
2374 | slot = path->slots[0]; | |
2375 | ||
2376 | if (slot >= btrfs_header_nritems(leaf)) { | |
2377 | ret = btrfs_next_leaf(root, path); | |
2378 | if (ret < 0) { | |
2379 | goto out; | |
2380 | } else if (ret > 0) { | |
2381 | ret = 0; | |
2382 | goto out; | |
2383 | } | |
2384 | continue; | |
2385 | } | |
2386 | ||
2387 | path->slots[0]++; | |
2388 | ||
2389 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2390 | ||
2391 | if (key.objectid > inum) | |
2392 | goto out; | |
2393 | ||
2394 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2395 | continue; | |
2396 | ||
2397 | extent = btrfs_item_ptr(leaf, slot, | |
2398 | struct btrfs_file_extent_item); | |
2399 | ||
2400 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2401 | continue; | |
2402 | ||
e68afa49 LB |
2403 | /* |
2404 | * 'offset' refers to the exact key.offset, | |
2405 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2406 | * (key.offset - extent_offset). | |
2407 | */ | |
2408 | if (key.offset != offset) | |
38c227d8 LB |
2409 | continue; |
2410 | ||
e68afa49 | 2411 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2412 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2413 | |
38c227d8 LB |
2414 | if (extent_offset >= old->extent_offset + old->offset + |
2415 | old->len || extent_offset + num_bytes <= | |
2416 | old->extent_offset + old->offset) | |
2417 | continue; | |
38c227d8 LB |
2418 | break; |
2419 | } | |
2420 | ||
2421 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2422 | if (!backref) { | |
2423 | ret = -ENOENT; | |
2424 | goto out; | |
2425 | } | |
2426 | ||
2427 | backref->root_id = root_id; | |
2428 | backref->inum = inum; | |
e68afa49 | 2429 | backref->file_pos = offset; |
38c227d8 LB |
2430 | backref->num_bytes = num_bytes; |
2431 | backref->extent_offset = extent_offset; | |
2432 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2433 | backref->old = old; | |
2434 | backref_insert(&new->root, backref); | |
2435 | old->count++; | |
2436 | out: | |
2437 | btrfs_release_path(path); | |
2438 | WARN_ON(ret); | |
2439 | return ret; | |
2440 | } | |
2441 | ||
2442 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2443 | struct new_sa_defrag_extent *new) | |
2444 | { | |
0b246afa | 2445 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2446 | struct old_sa_defrag_extent *old, *tmp; |
2447 | int ret; | |
2448 | ||
2449 | new->path = path; | |
2450 | ||
2451 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2452 | ret = iterate_inodes_from_logical(old->bytenr + |
2453 | old->extent_offset, fs_info, | |
38c227d8 LB |
2454 | path, record_one_backref, |
2455 | old); | |
4724b106 JB |
2456 | if (ret < 0 && ret != -ENOENT) |
2457 | return false; | |
38c227d8 LB |
2458 | |
2459 | /* no backref to be processed for this extent */ | |
2460 | if (!old->count) { | |
2461 | list_del(&old->list); | |
2462 | kfree(old); | |
2463 | } | |
2464 | } | |
2465 | ||
2466 | if (list_empty(&new->head)) | |
2467 | return false; | |
2468 | ||
2469 | return true; | |
2470 | } | |
2471 | ||
2472 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2473 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2474 | struct new_sa_defrag_extent *new) |
38c227d8 | 2475 | { |
116e0024 | 2476 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2477 | return 0; |
2478 | ||
2479 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2480 | return 0; | |
2481 | ||
116e0024 LB |
2482 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2483 | return 0; | |
2484 | ||
2485 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2486 | btrfs_file_extent_other_encoding(leaf, fi)) |
2487 | return 0; | |
2488 | ||
2489 | return 1; | |
2490 | } | |
2491 | ||
2492 | /* | |
2493 | * Note the backref might has changed, and in this case we just return 0. | |
2494 | */ | |
2495 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2496 | struct sa_defrag_extent_backref *prev, | |
2497 | struct sa_defrag_extent_backref *backref) | |
2498 | { | |
2499 | struct btrfs_file_extent_item *extent; | |
2500 | struct btrfs_file_extent_item *item; | |
2501 | struct btrfs_ordered_extent *ordered; | |
2502 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2503 | struct btrfs_root *root; |
2504 | struct btrfs_key key; | |
2505 | struct extent_buffer *leaf; | |
2506 | struct old_sa_defrag_extent *old = backref->old; | |
2507 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2508 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2509 | struct inode *inode; |
2510 | struct extent_state *cached = NULL; | |
2511 | int ret = 0; | |
2512 | u64 start; | |
2513 | u64 len; | |
2514 | u64 lock_start; | |
2515 | u64 lock_end; | |
2516 | bool merge = false; | |
2517 | int index; | |
2518 | ||
2519 | if (prev && prev->root_id == backref->root_id && | |
2520 | prev->inum == backref->inum && | |
2521 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2522 | merge = true; | |
2523 | ||
2524 | /* step 1: get root */ | |
2525 | key.objectid = backref->root_id; | |
2526 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2527 | key.offset = (u64)-1; | |
2528 | ||
38c227d8 LB |
2529 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2530 | ||
2531 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2532 | if (IS_ERR(root)) { | |
2533 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2534 | if (PTR_ERR(root) == -ENOENT) | |
2535 | return 0; | |
2536 | return PTR_ERR(root); | |
2537 | } | |
38c227d8 | 2538 | |
bcbba5e6 WS |
2539 | if (btrfs_root_readonly(root)) { |
2540 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2541 | return 0; | |
2542 | } | |
2543 | ||
38c227d8 LB |
2544 | /* step 2: get inode */ |
2545 | key.objectid = backref->inum; | |
2546 | key.type = BTRFS_INODE_ITEM_KEY; | |
2547 | key.offset = 0; | |
2548 | ||
2549 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2550 | if (IS_ERR(inode)) { | |
2551 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2552 | return 0; | |
2553 | } | |
2554 | ||
2555 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2556 | ||
2557 | /* step 3: relink backref */ | |
2558 | lock_start = backref->file_pos; | |
2559 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2560 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2561 | &cached); |
38c227d8 LB |
2562 | |
2563 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2564 | if (ordered) { | |
2565 | btrfs_put_ordered_extent(ordered); | |
2566 | goto out_unlock; | |
2567 | } | |
2568 | ||
2569 | trans = btrfs_join_transaction(root); | |
2570 | if (IS_ERR(trans)) { | |
2571 | ret = PTR_ERR(trans); | |
2572 | goto out_unlock; | |
2573 | } | |
2574 | ||
2575 | key.objectid = backref->inum; | |
2576 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2577 | key.offset = backref->file_pos; | |
2578 | ||
2579 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2580 | if (ret < 0) { | |
2581 | goto out_free_path; | |
2582 | } else if (ret > 0) { | |
2583 | ret = 0; | |
2584 | goto out_free_path; | |
2585 | } | |
2586 | ||
2587 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2588 | struct btrfs_file_extent_item); | |
2589 | ||
2590 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2591 | backref->generation) | |
2592 | goto out_free_path; | |
2593 | ||
2594 | btrfs_release_path(path); | |
2595 | ||
2596 | start = backref->file_pos; | |
2597 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2598 | start += old->extent_offset + old->offset - | |
2599 | backref->extent_offset; | |
2600 | ||
2601 | len = min(backref->extent_offset + backref->num_bytes, | |
2602 | old->extent_offset + old->offset + old->len); | |
2603 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2604 | ||
2605 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2606 | start + len, 1); | |
2607 | if (ret) | |
2608 | goto out_free_path; | |
2609 | again: | |
4a0cc7ca | 2610 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2611 | key.type = BTRFS_EXTENT_DATA_KEY; |
2612 | key.offset = start; | |
2613 | ||
a09a0a70 | 2614 | path->leave_spinning = 1; |
38c227d8 LB |
2615 | if (merge) { |
2616 | struct btrfs_file_extent_item *fi; | |
2617 | u64 extent_len; | |
2618 | struct btrfs_key found_key; | |
2619 | ||
3c9665df | 2620 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2621 | if (ret < 0) |
2622 | goto out_free_path; | |
2623 | ||
2624 | path->slots[0]--; | |
2625 | leaf = path->nodes[0]; | |
2626 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2627 | ||
2628 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2629 | struct btrfs_file_extent_item); | |
2630 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2631 | ||
116e0024 LB |
2632 | if (extent_len + found_key.offset == start && |
2633 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2634 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2635 | extent_len + len); | |
2636 | btrfs_mark_buffer_dirty(leaf); | |
2637 | inode_add_bytes(inode, len); | |
2638 | ||
2639 | ret = 1; | |
2640 | goto out_free_path; | |
2641 | } else { | |
2642 | merge = false; | |
2643 | btrfs_release_path(path); | |
2644 | goto again; | |
2645 | } | |
2646 | } | |
2647 | ||
2648 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2649 | sizeof(*extent)); | |
2650 | if (ret) { | |
66642832 | 2651 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2652 | goto out_free_path; |
2653 | } | |
2654 | ||
2655 | leaf = path->nodes[0]; | |
2656 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2657 | struct btrfs_file_extent_item); | |
2658 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2659 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2660 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2661 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2662 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2663 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2664 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2665 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2666 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2667 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2668 | ||
2669 | btrfs_mark_buffer_dirty(leaf); | |
2670 | inode_add_bytes(inode, len); | |
a09a0a70 | 2671 | btrfs_release_path(path); |
38c227d8 | 2672 | |
2ff7e61e | 2673 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
38c227d8 LB |
2674 | new->disk_len, 0, |
2675 | backref->root_id, backref->inum, | |
b06c4bf5 | 2676 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2677 | if (ret) { |
66642832 | 2678 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2679 | goto out_free_path; |
2680 | } | |
2681 | ||
2682 | ret = 1; | |
2683 | out_free_path: | |
2684 | btrfs_release_path(path); | |
a09a0a70 | 2685 | path->leave_spinning = 0; |
3a45bb20 | 2686 | btrfs_end_transaction(trans); |
38c227d8 LB |
2687 | out_unlock: |
2688 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2689 | &cached, GFP_NOFS); | |
2690 | iput(inode); | |
2691 | return ret; | |
2692 | } | |
2693 | ||
6f519564 LB |
2694 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2695 | { | |
2696 | struct old_sa_defrag_extent *old, *tmp; | |
2697 | ||
2698 | if (!new) | |
2699 | return; | |
2700 | ||
2701 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2702 | kfree(old); |
2703 | } | |
2704 | kfree(new); | |
2705 | } | |
2706 | ||
38c227d8 LB |
2707 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2708 | { | |
0b246afa | 2709 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2710 | struct btrfs_path *path; |
38c227d8 LB |
2711 | struct sa_defrag_extent_backref *backref; |
2712 | struct sa_defrag_extent_backref *prev = NULL; | |
2713 | struct inode *inode; | |
2714 | struct btrfs_root *root; | |
2715 | struct rb_node *node; | |
2716 | int ret; | |
2717 | ||
2718 | inode = new->inode; | |
2719 | root = BTRFS_I(inode)->root; | |
2720 | ||
2721 | path = btrfs_alloc_path(); | |
2722 | if (!path) | |
2723 | return; | |
2724 | ||
2725 | if (!record_extent_backrefs(path, new)) { | |
2726 | btrfs_free_path(path); | |
2727 | goto out; | |
2728 | } | |
2729 | btrfs_release_path(path); | |
2730 | ||
2731 | while (1) { | |
2732 | node = rb_first(&new->root); | |
2733 | if (!node) | |
2734 | break; | |
2735 | rb_erase(node, &new->root); | |
2736 | ||
2737 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2738 | ||
2739 | ret = relink_extent_backref(path, prev, backref); | |
2740 | WARN_ON(ret < 0); | |
2741 | ||
2742 | kfree(prev); | |
2743 | ||
2744 | if (ret == 1) | |
2745 | prev = backref; | |
2746 | else | |
2747 | prev = NULL; | |
2748 | cond_resched(); | |
2749 | } | |
2750 | kfree(prev); | |
2751 | ||
2752 | btrfs_free_path(path); | |
38c227d8 | 2753 | out: |
6f519564 LB |
2754 | free_sa_defrag_extent(new); |
2755 | ||
0b246afa JM |
2756 | atomic_dec(&fs_info->defrag_running); |
2757 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2758 | } |
2759 | ||
2760 | static struct new_sa_defrag_extent * | |
2761 | record_old_file_extents(struct inode *inode, | |
2762 | struct btrfs_ordered_extent *ordered) | |
2763 | { | |
0b246afa | 2764 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2765 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2766 | struct btrfs_path *path; | |
2767 | struct btrfs_key key; | |
6f519564 | 2768 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2769 | struct new_sa_defrag_extent *new; |
2770 | int ret; | |
2771 | ||
2772 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2773 | if (!new) | |
2774 | return NULL; | |
2775 | ||
2776 | new->inode = inode; | |
2777 | new->file_pos = ordered->file_offset; | |
2778 | new->len = ordered->len; | |
2779 | new->bytenr = ordered->start; | |
2780 | new->disk_len = ordered->disk_len; | |
2781 | new->compress_type = ordered->compress_type; | |
2782 | new->root = RB_ROOT; | |
2783 | INIT_LIST_HEAD(&new->head); | |
2784 | ||
2785 | path = btrfs_alloc_path(); | |
2786 | if (!path) | |
2787 | goto out_kfree; | |
2788 | ||
4a0cc7ca | 2789 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2790 | key.type = BTRFS_EXTENT_DATA_KEY; |
2791 | key.offset = new->file_pos; | |
2792 | ||
2793 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2794 | if (ret < 0) | |
2795 | goto out_free_path; | |
2796 | if (ret > 0 && path->slots[0] > 0) | |
2797 | path->slots[0]--; | |
2798 | ||
2799 | /* find out all the old extents for the file range */ | |
2800 | while (1) { | |
2801 | struct btrfs_file_extent_item *extent; | |
2802 | struct extent_buffer *l; | |
2803 | int slot; | |
2804 | u64 num_bytes; | |
2805 | u64 offset; | |
2806 | u64 end; | |
2807 | u64 disk_bytenr; | |
2808 | u64 extent_offset; | |
2809 | ||
2810 | l = path->nodes[0]; | |
2811 | slot = path->slots[0]; | |
2812 | ||
2813 | if (slot >= btrfs_header_nritems(l)) { | |
2814 | ret = btrfs_next_leaf(root, path); | |
2815 | if (ret < 0) | |
6f519564 | 2816 | goto out_free_path; |
38c227d8 LB |
2817 | else if (ret > 0) |
2818 | break; | |
2819 | continue; | |
2820 | } | |
2821 | ||
2822 | btrfs_item_key_to_cpu(l, &key, slot); | |
2823 | ||
4a0cc7ca | 2824 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2825 | break; |
2826 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2827 | break; | |
2828 | if (key.offset >= new->file_pos + new->len) | |
2829 | break; | |
2830 | ||
2831 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2832 | ||
2833 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2834 | if (key.offset + num_bytes < new->file_pos) | |
2835 | goto next; | |
2836 | ||
2837 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2838 | if (!disk_bytenr) | |
2839 | goto next; | |
2840 | ||
2841 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2842 | ||
2843 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2844 | if (!old) | |
6f519564 | 2845 | goto out_free_path; |
38c227d8 LB |
2846 | |
2847 | offset = max(new->file_pos, key.offset); | |
2848 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2849 | ||
2850 | old->bytenr = disk_bytenr; | |
2851 | old->extent_offset = extent_offset; | |
2852 | old->offset = offset - key.offset; | |
2853 | old->len = end - offset; | |
2854 | old->new = new; | |
2855 | old->count = 0; | |
2856 | list_add_tail(&old->list, &new->head); | |
2857 | next: | |
2858 | path->slots[0]++; | |
2859 | cond_resched(); | |
2860 | } | |
2861 | ||
2862 | btrfs_free_path(path); | |
0b246afa | 2863 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2864 | |
2865 | return new; | |
2866 | ||
38c227d8 LB |
2867 | out_free_path: |
2868 | btrfs_free_path(path); | |
2869 | out_kfree: | |
6f519564 | 2870 | free_sa_defrag_extent(new); |
38c227d8 LB |
2871 | return NULL; |
2872 | } | |
2873 | ||
2ff7e61e | 2874 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2875 | u64 start, u64 len) |
2876 | { | |
2877 | struct btrfs_block_group_cache *cache; | |
2878 | ||
0b246afa | 2879 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2880 | ASSERT(cache); |
2881 | ||
2882 | spin_lock(&cache->lock); | |
2883 | cache->delalloc_bytes -= len; | |
2884 | spin_unlock(&cache->lock); | |
2885 | ||
2886 | btrfs_put_block_group(cache); | |
2887 | } | |
2888 | ||
d352ac68 CM |
2889 | /* as ordered data IO finishes, this gets called so we can finish |
2890 | * an ordered extent if the range of bytes in the file it covers are | |
2891 | * fully written. | |
2892 | */ | |
5fd02043 | 2893 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2894 | { |
5fd02043 | 2895 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2896 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2898 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2899 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2900 | struct extent_state *cached_state = NULL; |
38c227d8 | 2901 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2902 | int compress_type = 0; |
77cef2ec JB |
2903 | int ret = 0; |
2904 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2905 | bool nolock; |
77cef2ec | 2906 | bool truncated = false; |
a7e3b975 FM |
2907 | bool range_locked = false; |
2908 | bool clear_new_delalloc_bytes = false; | |
2909 | ||
2910 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
2911 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && | |
2912 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) | |
2913 | clear_new_delalloc_bytes = true; | |
e6dcd2dc | 2914 | |
70ddc553 | 2915 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
0cb59c99 | 2916 | |
5fd02043 JB |
2917 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2918 | ret = -EIO; | |
2919 | goto out; | |
2920 | } | |
2921 | ||
7ab7956e NB |
2922 | btrfs_free_io_failure_record(BTRFS_I(inode), |
2923 | ordered_extent->file_offset, | |
2924 | ordered_extent->file_offset + | |
2925 | ordered_extent->len - 1); | |
f612496b | 2926 | |
77cef2ec JB |
2927 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2928 | truncated = true; | |
2929 | logical_len = ordered_extent->truncated_len; | |
2930 | /* Truncated the entire extent, don't bother adding */ | |
2931 | if (!logical_len) | |
2932 | goto out; | |
2933 | } | |
2934 | ||
c2167754 | 2935 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2936 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2937 | |
2938 | /* | |
2939 | * For mwrite(mmap + memset to write) case, we still reserve | |
2940 | * space for NOCOW range. | |
2941 | * As NOCOW won't cause a new delayed ref, just free the space | |
2942 | */ | |
bc42bda2 | 2943 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
94ed938a | 2944 | ordered_extent->len); |
6c760c07 JB |
2945 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2946 | if (nolock) | |
2947 | trans = btrfs_join_transaction_nolock(root); | |
2948 | else | |
2949 | trans = btrfs_join_transaction(root); | |
2950 | if (IS_ERR(trans)) { | |
2951 | ret = PTR_ERR(trans); | |
2952 | trans = NULL; | |
2953 | goto out; | |
c2167754 | 2954 | } |
0b246afa | 2955 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
6c760c07 JB |
2956 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2957 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2958 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2959 | goto out; |
2960 | } | |
e6dcd2dc | 2961 | |
a7e3b975 | 2962 | range_locked = true; |
2ac55d41 JB |
2963 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2964 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2965 | &cached_state); |
e6dcd2dc | 2966 | |
38c227d8 LB |
2967 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2968 | ordered_extent->file_offset + ordered_extent->len - 1, | |
452e62b7 | 2969 | EXTENT_DEFRAG, 0, cached_state); |
38c227d8 LB |
2970 | if (ret) { |
2971 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2972 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2973 | /* the inode is shared */ |
2974 | new = record_old_file_extents(inode, ordered_extent); | |
2975 | ||
2976 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2977 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2978 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2979 | } | |
2980 | ||
0cb59c99 | 2981 | if (nolock) |
7a7eaa40 | 2982 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2983 | else |
7a7eaa40 | 2984 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2985 | if (IS_ERR(trans)) { |
2986 | ret = PTR_ERR(trans); | |
2987 | trans = NULL; | |
a7e3b975 | 2988 | goto out; |
79787eaa | 2989 | } |
a79b7d4b | 2990 | |
0b246afa | 2991 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
c2167754 | 2992 | |
c8b97818 | 2993 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2994 | compress_type = ordered_extent->compress_type; |
d899e052 | 2995 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2996 | BUG_ON(compress_type); |
7a6d7067 | 2997 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
d899e052 YZ |
2998 | ordered_extent->file_offset, |
2999 | ordered_extent->file_offset + | |
77cef2ec | 3000 | logical_len); |
d899e052 | 3001 | } else { |
0b246afa | 3002 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
3003 | ret = insert_reserved_file_extent(trans, inode, |
3004 | ordered_extent->file_offset, | |
3005 | ordered_extent->start, | |
3006 | ordered_extent->disk_len, | |
77cef2ec | 3007 | logical_len, logical_len, |
261507a0 | 3008 | compress_type, 0, 0, |
d899e052 | 3009 | BTRFS_FILE_EXTENT_REG); |
e570fd27 | 3010 | if (!ret) |
2ff7e61e | 3011 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
3012 | ordered_extent->start, |
3013 | ordered_extent->disk_len); | |
d899e052 | 3014 | } |
5dc562c5 JB |
3015 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
3016 | ordered_extent->file_offset, ordered_extent->len, | |
3017 | trans->transid); | |
79787eaa | 3018 | if (ret < 0) { |
66642832 | 3019 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3020 | goto out; |
79787eaa | 3021 | } |
2ac55d41 | 3022 | |
df9f628e | 3023 | add_pending_csums(trans, inode, &ordered_extent->list); |
e6dcd2dc | 3024 | |
6c760c07 JB |
3025 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
3026 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
3027 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 3028 | btrfs_abort_transaction(trans, ret); |
a7e3b975 | 3029 | goto out; |
1ef30be1 JB |
3030 | } |
3031 | ret = 0; | |
c2167754 | 3032 | out: |
a7e3b975 FM |
3033 | if (range_locked || clear_new_delalloc_bytes) { |
3034 | unsigned int clear_bits = 0; | |
3035 | ||
3036 | if (range_locked) | |
3037 | clear_bits |= EXTENT_LOCKED; | |
3038 | if (clear_new_delalloc_bytes) | |
3039 | clear_bits |= EXTENT_DELALLOC_NEW; | |
3040 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | |
3041 | ordered_extent->file_offset, | |
3042 | ordered_extent->file_offset + | |
3043 | ordered_extent->len - 1, | |
3044 | clear_bits, | |
3045 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, | |
3046 | 0, &cached_state, GFP_NOFS); | |
3047 | } | |
3048 | ||
0b246afa | 3049 | if (root != fs_info->tree_root) |
691fa059 NB |
3050 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
3051 | ordered_extent->len); | |
a698d075 | 3052 | if (trans) |
3a45bb20 | 3053 | btrfs_end_transaction(trans); |
0cb59c99 | 3054 | |
77cef2ec JB |
3055 | if (ret || truncated) { |
3056 | u64 start, end; | |
3057 | ||
3058 | if (truncated) | |
3059 | start = ordered_extent->file_offset + logical_len; | |
3060 | else | |
3061 | start = ordered_extent->file_offset; | |
3062 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3063 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3064 | ||
3065 | /* Drop the cache for the part of the extent we didn't write. */ | |
dcdbc059 | 3066 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
5fd02043 | 3067 | |
0bec9ef5 JB |
3068 | /* |
3069 | * If the ordered extent had an IOERR or something else went | |
3070 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3071 | * back to the allocator. We only free the extent in the |
3072 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3073 | */ |
77cef2ec JB |
3074 | if ((ret || !logical_len) && |
3075 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 | 3076 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3077 | btrfs_free_reserved_extent(fs_info, |
3078 | ordered_extent->start, | |
e570fd27 | 3079 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3080 | } |
3081 | ||
3082 | ||
5fd02043 | 3083 | /* |
8bad3c02 LB |
3084 | * This needs to be done to make sure anybody waiting knows we are done |
3085 | * updating everything for this ordered extent. | |
5fd02043 JB |
3086 | */ |
3087 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3088 | ||
38c227d8 | 3089 | /* for snapshot-aware defrag */ |
6f519564 LB |
3090 | if (new) { |
3091 | if (ret) { | |
3092 | free_sa_defrag_extent(new); | |
0b246afa | 3093 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3094 | } else { |
3095 | relink_file_extents(new); | |
3096 | } | |
3097 | } | |
38c227d8 | 3098 | |
e6dcd2dc CM |
3099 | /* once for us */ |
3100 | btrfs_put_ordered_extent(ordered_extent); | |
3101 | /* once for the tree */ | |
3102 | btrfs_put_ordered_extent(ordered_extent); | |
3103 | ||
5fd02043 JB |
3104 | return ret; |
3105 | } | |
3106 | ||
3107 | static void finish_ordered_fn(struct btrfs_work *work) | |
3108 | { | |
3109 | struct btrfs_ordered_extent *ordered_extent; | |
3110 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3111 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3112 | } |
3113 | ||
c3988d63 | 3114 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3115 | struct extent_state *state, int uptodate) |
3116 | { | |
5fd02043 | 3117 | struct inode *inode = page->mapping->host; |
0b246afa | 3118 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3119 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3120 | struct btrfs_workqueue *wq; |
3121 | btrfs_work_func_t func; | |
5fd02043 | 3122 | |
1abe9b8a | 3123 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3124 | ||
8b62b72b | 3125 | ClearPagePrivate2(page); |
5fd02043 JB |
3126 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3127 | end - start + 1, uptodate)) | |
c3988d63 | 3128 | return; |
5fd02043 | 3129 | |
70ddc553 | 3130 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
0b246afa | 3131 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3132 | func = btrfs_freespace_write_helper; |
3133 | } else { | |
0b246afa | 3134 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3135 | func = btrfs_endio_write_helper; |
3136 | } | |
5fd02043 | 3137 | |
9e0af237 LB |
3138 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3139 | NULL); | |
3140 | btrfs_queue_work(wq, &ordered_extent->work); | |
211f90e6 CM |
3141 | } |
3142 | ||
dc380aea MX |
3143 | static int __readpage_endio_check(struct inode *inode, |
3144 | struct btrfs_io_bio *io_bio, | |
3145 | int icsum, struct page *page, | |
3146 | int pgoff, u64 start, size_t len) | |
3147 | { | |
3148 | char *kaddr; | |
3149 | u32 csum_expected; | |
3150 | u32 csum = ~(u32)0; | |
dc380aea MX |
3151 | |
3152 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3153 | ||
3154 | kaddr = kmap_atomic(page); | |
3155 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3156 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3157 | if (csum != csum_expected) |
3158 | goto zeroit; | |
3159 | ||
3160 | kunmap_atomic(kaddr); | |
3161 | return 0; | |
3162 | zeroit: | |
0970a22e | 3163 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
6f6b643e | 3164 | io_bio->mirror_num); |
dc380aea MX |
3165 | memset(kaddr + pgoff, 1, len); |
3166 | flush_dcache_page(page); | |
3167 | kunmap_atomic(kaddr); | |
dc380aea MX |
3168 | return -EIO; |
3169 | } | |
3170 | ||
d352ac68 CM |
3171 | /* |
3172 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3173 | * if there's a match, we allow the bio to finish. If not, the code in |
3174 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3175 | */ |
facc8a22 MX |
3176 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3177 | u64 phy_offset, struct page *page, | |
3178 | u64 start, u64 end, int mirror) | |
07157aac | 3179 | { |
4eee4fa4 | 3180 | size_t offset = start - page_offset(page); |
07157aac | 3181 | struct inode *inode = page->mapping->host; |
d1310b2e | 3182 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3183 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3184 | |
d20f7043 CM |
3185 | if (PageChecked(page)) { |
3186 | ClearPageChecked(page); | |
dc380aea | 3187 | return 0; |
d20f7043 | 3188 | } |
6cbff00f CH |
3189 | |
3190 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3191 | return 0; |
17d217fe YZ |
3192 | |
3193 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3194 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3195 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3196 | return 0; |
17d217fe | 3197 | } |
d20f7043 | 3198 | |
facc8a22 | 3199 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3200 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3201 | start, (size_t)(end - start + 1)); | |
07157aac | 3202 | } |
b888db2b | 3203 | |
24bbcf04 YZ |
3204 | void btrfs_add_delayed_iput(struct inode *inode) |
3205 | { | |
0b246afa | 3206 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3207 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3208 | |
3209 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3210 | return; | |
3211 | ||
24bbcf04 | 3212 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3213 | if (binode->delayed_iput_count == 0) { |
3214 | ASSERT(list_empty(&binode->delayed_iput)); | |
3215 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3216 | } else { | |
3217 | binode->delayed_iput_count++; | |
3218 | } | |
24bbcf04 YZ |
3219 | spin_unlock(&fs_info->delayed_iput_lock); |
3220 | } | |
3221 | ||
2ff7e61e | 3222 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3223 | { |
24bbcf04 | 3224 | |
24bbcf04 | 3225 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3226 | while (!list_empty(&fs_info->delayed_iputs)) { |
3227 | struct btrfs_inode *inode; | |
3228 | ||
3229 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3230 | struct btrfs_inode, delayed_iput); | |
3231 | if (inode->delayed_iput_count) { | |
3232 | inode->delayed_iput_count--; | |
3233 | list_move_tail(&inode->delayed_iput, | |
3234 | &fs_info->delayed_iputs); | |
3235 | } else { | |
3236 | list_del_init(&inode->delayed_iput); | |
3237 | } | |
3238 | spin_unlock(&fs_info->delayed_iput_lock); | |
3239 | iput(&inode->vfs_inode); | |
3240 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3241 | } |
8089fe62 | 3242 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3243 | } |
3244 | ||
d68fc57b | 3245 | /* |
42b2aa86 | 3246 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3247 | * files in the subvolume, it removes orphan item and frees block_rsv |
3248 | * structure. | |
3249 | */ | |
3250 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3251 | struct btrfs_root *root) | |
3252 | { | |
0b246afa | 3253 | struct btrfs_fs_info *fs_info = root->fs_info; |
90290e19 | 3254 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3255 | int ret; |
3256 | ||
8a35d95f | 3257 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3258 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3259 | return; | |
3260 | ||
90290e19 | 3261 | spin_lock(&root->orphan_lock); |
8a35d95f | 3262 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3263 | spin_unlock(&root->orphan_lock); |
3264 | return; | |
3265 | } | |
3266 | ||
3267 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3268 | spin_unlock(&root->orphan_lock); | |
3269 | return; | |
3270 | } | |
3271 | ||
3272 | block_rsv = root->orphan_block_rsv; | |
3273 | root->orphan_block_rsv = NULL; | |
3274 | spin_unlock(&root->orphan_lock); | |
3275 | ||
27cdeb70 | 3276 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b | 3277 | btrfs_root_refs(&root->root_item) > 0) { |
0b246afa | 3278 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3279 | root->root_key.objectid); |
4ef31a45 | 3280 | if (ret) |
66642832 | 3281 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3282 | else |
27cdeb70 MX |
3283 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3284 | &root->state); | |
d68fc57b YZ |
3285 | } |
3286 | ||
90290e19 JB |
3287 | if (block_rsv) { |
3288 | WARN_ON(block_rsv->size > 0); | |
2ff7e61e | 3289 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b YZ |
3290 | } |
3291 | } | |
3292 | ||
7b128766 JB |
3293 | /* |
3294 | * This creates an orphan entry for the given inode in case something goes | |
3295 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3296 | * |
3297 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3298 | * this function. | |
7b128766 | 3299 | */ |
73f2e545 NB |
3300 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
3301 | struct btrfs_inode *inode) | |
7b128766 | 3302 | { |
73f2e545 NB |
3303 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
3304 | struct btrfs_root *root = inode->root; | |
d68fc57b YZ |
3305 | struct btrfs_block_rsv *block_rsv = NULL; |
3306 | int reserve = 0; | |
3307 | int insert = 0; | |
3308 | int ret; | |
7b128766 | 3309 | |
d68fc57b | 3310 | if (!root->orphan_block_rsv) { |
2ff7e61e JM |
3311 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
3312 | BTRFS_BLOCK_RSV_TEMP); | |
b532402e TI |
3313 | if (!block_rsv) |
3314 | return -ENOMEM; | |
d68fc57b | 3315 | } |
7b128766 | 3316 | |
d68fc57b YZ |
3317 | spin_lock(&root->orphan_lock); |
3318 | if (!root->orphan_block_rsv) { | |
3319 | root->orphan_block_rsv = block_rsv; | |
3320 | } else if (block_rsv) { | |
2ff7e61e | 3321 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b | 3322 | block_rsv = NULL; |
7b128766 | 3323 | } |
7b128766 | 3324 | |
8a35d95f | 3325 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3326 | &inode->runtime_flags)) { |
d68fc57b YZ |
3327 | #if 0 |
3328 | /* | |
3329 | * For proper ENOSPC handling, we should do orphan | |
3330 | * cleanup when mounting. But this introduces backward | |
3331 | * compatibility issue. | |
3332 | */ | |
3333 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3334 | insert = 2; | |
3335 | else | |
3336 | insert = 1; | |
3337 | #endif | |
3338 | insert = 1; | |
321f0e70 | 3339 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3340 | } |
3341 | ||
72ac3c0d | 3342 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
73f2e545 | 3343 | &inode->runtime_flags)) |
d68fc57b | 3344 | reserve = 1; |
d68fc57b | 3345 | spin_unlock(&root->orphan_lock); |
7b128766 | 3346 | |
d68fc57b YZ |
3347 | /* grab metadata reservation from transaction handle */ |
3348 | if (reserve) { | |
3349 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3350 | ASSERT(!ret); |
3351 | if (ret) { | |
3352 | atomic_dec(&root->orphan_inodes); | |
3353 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3354 | &inode->runtime_flags); |
3b6571c1 JB |
3355 | if (insert) |
3356 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
73f2e545 | 3357 | &inode->runtime_flags); |
3b6571c1 JB |
3358 | return ret; |
3359 | } | |
d68fc57b | 3360 | } |
7b128766 | 3361 | |
d68fc57b YZ |
3362 | /* insert an orphan item to track this unlinked/truncated file */ |
3363 | if (insert >= 1) { | |
73f2e545 | 3364 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3365 | if (ret) { |
703c88e0 | 3366 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3367 | if (reserve) { |
3368 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
73f2e545 | 3369 | &inode->runtime_flags); |
4ef31a45 JB |
3370 | btrfs_orphan_release_metadata(inode); |
3371 | } | |
3372 | if (ret != -EEXIST) { | |
e8e7cff6 | 3373 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
73f2e545 | 3374 | &inode->runtime_flags); |
66642832 | 3375 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3376 | return ret; |
3377 | } | |
79787eaa JM |
3378 | } |
3379 | ret = 0; | |
d68fc57b YZ |
3380 | } |
3381 | ||
3382 | /* insert an orphan item to track subvolume contains orphan files */ | |
3383 | if (insert >= 2) { | |
0b246afa | 3384 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3385 | root->root_key.objectid); |
79787eaa | 3386 | if (ret && ret != -EEXIST) { |
66642832 | 3387 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3388 | return ret; |
3389 | } | |
d68fc57b YZ |
3390 | } |
3391 | return 0; | |
7b128766 JB |
3392 | } |
3393 | ||
3394 | /* | |
3395 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3396 | * item for this particular inode. | |
3397 | */ | |
48a3b636 | 3398 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3d6ae7bb | 3399 | struct btrfs_inode *inode) |
7b128766 | 3400 | { |
3d6ae7bb | 3401 | struct btrfs_root *root = inode->root; |
d68fc57b YZ |
3402 | int delete_item = 0; |
3403 | int release_rsv = 0; | |
7b128766 JB |
3404 | int ret = 0; |
3405 | ||
d68fc57b | 3406 | spin_lock(&root->orphan_lock); |
8a35d95f | 3407 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3d6ae7bb | 3408 | &inode->runtime_flags)) |
d68fc57b | 3409 | delete_item = 1; |
7b128766 | 3410 | |
72ac3c0d | 3411 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3d6ae7bb | 3412 | &inode->runtime_flags)) |
d68fc57b | 3413 | release_rsv = 1; |
d68fc57b | 3414 | spin_unlock(&root->orphan_lock); |
7b128766 | 3415 | |
703c88e0 | 3416 | if (delete_item) { |
8a35d95f | 3417 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3418 | if (trans) |
3419 | ret = btrfs_del_orphan_item(trans, root, | |
3d6ae7bb | 3420 | btrfs_ino(inode)); |
8a35d95f | 3421 | } |
7b128766 | 3422 | |
703c88e0 FDBM |
3423 | if (release_rsv) |
3424 | btrfs_orphan_release_metadata(inode); | |
3425 | ||
4ef31a45 | 3426 | return ret; |
7b128766 JB |
3427 | } |
3428 | ||
3429 | /* | |
3430 | * this cleans up any orphans that may be left on the list from the last use | |
3431 | * of this root. | |
3432 | */ | |
66b4ffd1 | 3433 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3434 | { |
0b246afa | 3435 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3436 | struct btrfs_path *path; |
3437 | struct extent_buffer *leaf; | |
7b128766 JB |
3438 | struct btrfs_key key, found_key; |
3439 | struct btrfs_trans_handle *trans; | |
3440 | struct inode *inode; | |
8f6d7f4f | 3441 | u64 last_objectid = 0; |
7b128766 JB |
3442 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3443 | ||
d68fc57b | 3444 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3445 | return 0; |
c71bf099 YZ |
3446 | |
3447 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3448 | if (!path) { |
3449 | ret = -ENOMEM; | |
3450 | goto out; | |
3451 | } | |
e4058b54 | 3452 | path->reada = READA_BACK; |
7b128766 JB |
3453 | |
3454 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3455 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3456 | key.offset = (u64)-1; |
3457 | ||
7b128766 JB |
3458 | while (1) { |
3459 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3460 | if (ret < 0) |
3461 | goto out; | |
7b128766 JB |
3462 | |
3463 | /* | |
3464 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3465 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3466 | * find the key and see if we have stuff that matches |
3467 | */ | |
3468 | if (ret > 0) { | |
66b4ffd1 | 3469 | ret = 0; |
7b128766 JB |
3470 | if (path->slots[0] == 0) |
3471 | break; | |
3472 | path->slots[0]--; | |
3473 | } | |
3474 | ||
3475 | /* pull out the item */ | |
3476 | leaf = path->nodes[0]; | |
7b128766 JB |
3477 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3478 | ||
3479 | /* make sure the item matches what we want */ | |
3480 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3481 | break; | |
962a298f | 3482 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3483 | break; |
3484 | ||
3485 | /* release the path since we're done with it */ | |
b3b4aa74 | 3486 | btrfs_release_path(path); |
7b128766 JB |
3487 | |
3488 | /* | |
3489 | * this is where we are basically btrfs_lookup, without the | |
3490 | * crossing root thing. we store the inode number in the | |
3491 | * offset of the orphan item. | |
3492 | */ | |
8f6d7f4f JB |
3493 | |
3494 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3495 | btrfs_err(fs_info, |
3496 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3497 | ret = -EINVAL; |
3498 | goto out; | |
3499 | } | |
3500 | ||
3501 | last_objectid = found_key.offset; | |
3502 | ||
5d4f98a2 YZ |
3503 | found_key.objectid = found_key.offset; |
3504 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3505 | found_key.offset = 0; | |
0b246afa | 3506 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3507 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3508 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3509 | goto out; |
7b128766 | 3510 | |
0b246afa | 3511 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3512 | struct btrfs_root *dead_root; |
3513 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3514 | int is_dead_root = 0; | |
3515 | ||
3516 | /* | |
3517 | * this is an orphan in the tree root. Currently these | |
3518 | * could come from 2 sources: | |
3519 | * a) a snapshot deletion in progress | |
3520 | * b) a free space cache inode | |
3521 | * We need to distinguish those two, as the snapshot | |
3522 | * orphan must not get deleted. | |
3523 | * find_dead_roots already ran before us, so if this | |
3524 | * is a snapshot deletion, we should find the root | |
3525 | * in the dead_roots list | |
3526 | */ | |
3527 | spin_lock(&fs_info->trans_lock); | |
3528 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3529 | root_list) { | |
3530 | if (dead_root->root_key.objectid == | |
3531 | found_key.objectid) { | |
3532 | is_dead_root = 1; | |
3533 | break; | |
3534 | } | |
3535 | } | |
3536 | spin_unlock(&fs_info->trans_lock); | |
3537 | if (is_dead_root) { | |
3538 | /* prevent this orphan from being found again */ | |
3539 | key.offset = found_key.objectid - 1; | |
3540 | continue; | |
3541 | } | |
3542 | } | |
7b128766 | 3543 | /* |
a8c9e576 JB |
3544 | * Inode is already gone but the orphan item is still there, |
3545 | * kill the orphan item. | |
7b128766 | 3546 | */ |
67710892 | 3547 | if (ret == -ENOENT) { |
a8c9e576 | 3548 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3549 | if (IS_ERR(trans)) { |
3550 | ret = PTR_ERR(trans); | |
3551 | goto out; | |
3552 | } | |
0b246afa JM |
3553 | btrfs_debug(fs_info, "auto deleting %Lu", |
3554 | found_key.objectid); | |
a8c9e576 JB |
3555 | ret = btrfs_del_orphan_item(trans, root, |
3556 | found_key.objectid); | |
3a45bb20 | 3557 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3558 | if (ret) |
3559 | goto out; | |
7b128766 JB |
3560 | continue; |
3561 | } | |
3562 | ||
a8c9e576 JB |
3563 | /* |
3564 | * add this inode to the orphan list so btrfs_orphan_del does | |
3565 | * the proper thing when we hit it | |
3566 | */ | |
8a35d95f JB |
3567 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3568 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3569 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3570 | |
7b128766 JB |
3571 | /* if we have links, this was a truncate, lets do that */ |
3572 | if (inode->i_nlink) { | |
fae7f21c | 3573 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3574 | iput(inode); |
3575 | continue; | |
3576 | } | |
7b128766 | 3577 | nr_truncate++; |
f3fe820c JB |
3578 | |
3579 | /* 1 for the orphan item deletion. */ | |
3580 | trans = btrfs_start_transaction(root, 1); | |
3581 | if (IS_ERR(trans)) { | |
c69b26b0 | 3582 | iput(inode); |
f3fe820c JB |
3583 | ret = PTR_ERR(trans); |
3584 | goto out; | |
3585 | } | |
73f2e545 | 3586 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 3587 | btrfs_end_transaction(trans); |
c69b26b0 JB |
3588 | if (ret) { |
3589 | iput(inode); | |
f3fe820c | 3590 | goto out; |
c69b26b0 | 3591 | } |
f3fe820c | 3592 | |
66b4ffd1 | 3593 | ret = btrfs_truncate(inode); |
4a7d0f68 | 3594 | if (ret) |
3d6ae7bb | 3595 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7b128766 JB |
3596 | } else { |
3597 | nr_unlink++; | |
3598 | } | |
3599 | ||
3600 | /* this will do delete_inode and everything for us */ | |
3601 | iput(inode); | |
66b4ffd1 JB |
3602 | if (ret) |
3603 | goto out; | |
7b128766 | 3604 | } |
3254c876 MX |
3605 | /* release the path since we're done with it */ |
3606 | btrfs_release_path(path); | |
3607 | ||
d68fc57b YZ |
3608 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3609 | ||
3610 | if (root->orphan_block_rsv) | |
2ff7e61e | 3611 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
d68fc57b YZ |
3612 | (u64)-1); |
3613 | ||
27cdeb70 MX |
3614 | if (root->orphan_block_rsv || |
3615 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3616 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3617 | if (!IS_ERR(trans)) |
3a45bb20 | 3618 | btrfs_end_transaction(trans); |
d68fc57b | 3619 | } |
7b128766 JB |
3620 | |
3621 | if (nr_unlink) | |
0b246afa | 3622 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3623 | if (nr_truncate) |
0b246afa | 3624 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3625 | |
3626 | out: | |
3627 | if (ret) | |
0b246afa | 3628 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3629 | btrfs_free_path(path); |
3630 | return ret; | |
7b128766 JB |
3631 | } |
3632 | ||
46a53cca CM |
3633 | /* |
3634 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3635 | * don't find any xattrs, we know there can't be any acls. | |
3636 | * | |
3637 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3638 | */ | |
3639 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3640 | int slot, u64 objectid, |
3641 | int *first_xattr_slot) | |
46a53cca CM |
3642 | { |
3643 | u32 nritems = btrfs_header_nritems(leaf); | |
3644 | struct btrfs_key found_key; | |
f23b5a59 JB |
3645 | static u64 xattr_access = 0; |
3646 | static u64 xattr_default = 0; | |
46a53cca CM |
3647 | int scanned = 0; |
3648 | ||
f23b5a59 | 3649 | if (!xattr_access) { |
97d79299 AG |
3650 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3651 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3652 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3653 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3654 | } |
3655 | ||
46a53cca | 3656 | slot++; |
63541927 | 3657 | *first_xattr_slot = -1; |
46a53cca CM |
3658 | while (slot < nritems) { |
3659 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3660 | ||
3661 | /* we found a different objectid, there must not be acls */ | |
3662 | if (found_key.objectid != objectid) | |
3663 | return 0; | |
3664 | ||
3665 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3666 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3667 | if (*first_xattr_slot == -1) |
3668 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3669 | if (found_key.offset == xattr_access || |
3670 | found_key.offset == xattr_default) | |
3671 | return 1; | |
3672 | } | |
46a53cca CM |
3673 | |
3674 | /* | |
3675 | * we found a key greater than an xattr key, there can't | |
3676 | * be any acls later on | |
3677 | */ | |
3678 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3679 | return 0; | |
3680 | ||
3681 | slot++; | |
3682 | scanned++; | |
3683 | ||
3684 | /* | |
3685 | * it goes inode, inode backrefs, xattrs, extents, | |
3686 | * so if there are a ton of hard links to an inode there can | |
3687 | * be a lot of backrefs. Don't waste time searching too hard, | |
3688 | * this is just an optimization | |
3689 | */ | |
3690 | if (scanned >= 8) | |
3691 | break; | |
3692 | } | |
3693 | /* we hit the end of the leaf before we found an xattr or | |
3694 | * something larger than an xattr. We have to assume the inode | |
3695 | * has acls | |
3696 | */ | |
63541927 FDBM |
3697 | if (*first_xattr_slot == -1) |
3698 | *first_xattr_slot = slot; | |
46a53cca CM |
3699 | return 1; |
3700 | } | |
3701 | ||
d352ac68 CM |
3702 | /* |
3703 | * read an inode from the btree into the in-memory inode | |
3704 | */ | |
67710892 | 3705 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 | 3706 | { |
0b246afa | 3707 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 3708 | struct btrfs_path *path; |
5f39d397 | 3709 | struct extent_buffer *leaf; |
39279cc3 CM |
3710 | struct btrfs_inode_item *inode_item; |
3711 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3712 | struct btrfs_key location; | |
67de1176 | 3713 | unsigned long ptr; |
46a53cca | 3714 | int maybe_acls; |
618e21d5 | 3715 | u32 rdev; |
39279cc3 | 3716 | int ret; |
2f7e33d4 | 3717 | bool filled = false; |
63541927 | 3718 | int first_xattr_slot; |
2f7e33d4 MX |
3719 | |
3720 | ret = btrfs_fill_inode(inode, &rdev); | |
3721 | if (!ret) | |
3722 | filled = true; | |
39279cc3 CM |
3723 | |
3724 | path = btrfs_alloc_path(); | |
67710892 FM |
3725 | if (!path) { |
3726 | ret = -ENOMEM; | |
1748f843 | 3727 | goto make_bad; |
67710892 | 3728 | } |
1748f843 | 3729 | |
39279cc3 | 3730 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3731 | |
39279cc3 | 3732 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3733 | if (ret) { |
3734 | if (ret > 0) | |
3735 | ret = -ENOENT; | |
39279cc3 | 3736 | goto make_bad; |
67710892 | 3737 | } |
39279cc3 | 3738 | |
5f39d397 | 3739 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3740 | |
3741 | if (filled) | |
67de1176 | 3742 | goto cache_index; |
2f7e33d4 | 3743 | |
5f39d397 CM |
3744 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3745 | struct btrfs_inode_item); | |
5f39d397 | 3746 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3747 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3748 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3749 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
6ef06d27 | 3750 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3751 | |
a937b979 DS |
3752 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3753 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3754 | |
a937b979 DS |
3755 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3756 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3757 | |
a937b979 DS |
3758 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3759 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3760 | |
9cc97d64 | 3761 | BTRFS_I(inode)->i_otime.tv_sec = |
3762 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3763 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3764 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3765 | |
a76a3cd4 | 3766 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3767 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3768 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3769 | ||
6e17d30b YD |
3770 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3771 | inode->i_generation = BTRFS_I(inode)->generation; | |
3772 | inode->i_rdev = 0; | |
3773 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3774 | ||
3775 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3776 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3777 | ||
3778 | cache_index: | |
5dc562c5 JB |
3779 | /* |
3780 | * If we were modified in the current generation and evicted from memory | |
3781 | * and then re-read we need to do a full sync since we don't have any | |
3782 | * idea about which extents were modified before we were evicted from | |
3783 | * cache. | |
6e17d30b YD |
3784 | * |
3785 | * This is required for both inode re-read from disk and delayed inode | |
3786 | * in delayed_nodes_tree. | |
5dc562c5 | 3787 | */ |
0b246afa | 3788 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3789 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3790 | &BTRFS_I(inode)->runtime_flags); | |
3791 | ||
bde6c242 FM |
3792 | /* |
3793 | * We don't persist the id of the transaction where an unlink operation | |
3794 | * against the inode was last made. So here we assume the inode might | |
3795 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3796 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3797 | * between the inode and its parent if the inode is fsync'ed and the log | |
3798 | * replayed. For example, in the scenario: | |
3799 | * | |
3800 | * touch mydir/foo | |
3801 | * ln mydir/foo mydir/bar | |
3802 | * sync | |
3803 | * unlink mydir/bar | |
3804 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3805 | * xfs_io -c fsync mydir/foo | |
3806 | * <power failure> | |
3807 | * mount fs, triggers fsync log replay | |
3808 | * | |
3809 | * We must make sure that when we fsync our inode foo we also log its | |
3810 | * parent inode, otherwise after log replay the parent still has the | |
3811 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3812 | * and doesn't have an inode ref with the name "bar" anymore. | |
3813 | * | |
3814 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3815 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3816 | * transaction commits on fsync if our inode is a directory, or if our |
3817 | * inode is not a directory, logging its parent unnecessarily. | |
3818 | */ | |
3819 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3820 | ||
67de1176 MX |
3821 | path->slots[0]++; |
3822 | if (inode->i_nlink != 1 || | |
3823 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3824 | goto cache_acl; | |
3825 | ||
3826 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3827 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3828 | goto cache_acl; |
3829 | ||
3830 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3831 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3832 | struct btrfs_inode_ref *ref; | |
3833 | ||
3834 | ref = (struct btrfs_inode_ref *)ptr; | |
3835 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3836 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3837 | struct btrfs_inode_extref *extref; | |
3838 | ||
3839 | extref = (struct btrfs_inode_extref *)ptr; | |
3840 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3841 | extref); | |
3842 | } | |
2f7e33d4 | 3843 | cache_acl: |
46a53cca CM |
3844 | /* |
3845 | * try to precache a NULL acl entry for files that don't have | |
3846 | * any xattrs or acls | |
3847 | */ | |
33345d01 | 3848 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
f85b7379 | 3849 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3850 | if (first_xattr_slot != -1) { |
3851 | path->slots[0] = first_xattr_slot; | |
3852 | ret = btrfs_load_inode_props(inode, path); | |
3853 | if (ret) | |
0b246afa | 3854 | btrfs_err(fs_info, |
351fd353 | 3855 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3856 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3857 | root->root_key.objectid, ret); |
3858 | } | |
3859 | btrfs_free_path(path); | |
3860 | ||
72c04902 AV |
3861 | if (!maybe_acls) |
3862 | cache_no_acl(inode); | |
46a53cca | 3863 | |
39279cc3 | 3864 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3865 | case S_IFREG: |
3866 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3867 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3868 | inode->i_fop = &btrfs_file_operations; |
3869 | inode->i_op = &btrfs_file_inode_operations; | |
3870 | break; | |
3871 | case S_IFDIR: | |
3872 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3873 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3874 | break; |
3875 | case S_IFLNK: | |
3876 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3877 | inode_nohighmem(inode); |
39279cc3 CM |
3878 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3879 | break; | |
618e21d5 | 3880 | default: |
0279b4cd | 3881 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3882 | init_special_inode(inode, inode->i_mode, rdev); |
3883 | break; | |
39279cc3 | 3884 | } |
6cbff00f CH |
3885 | |
3886 | btrfs_update_iflags(inode); | |
67710892 | 3887 | return 0; |
39279cc3 CM |
3888 | |
3889 | make_bad: | |
39279cc3 | 3890 | btrfs_free_path(path); |
39279cc3 | 3891 | make_bad_inode(inode); |
67710892 | 3892 | return ret; |
39279cc3 CM |
3893 | } |
3894 | ||
d352ac68 CM |
3895 | /* |
3896 | * given a leaf and an inode, copy the inode fields into the leaf | |
3897 | */ | |
e02119d5 CM |
3898 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3899 | struct extent_buffer *leaf, | |
5f39d397 | 3900 | struct btrfs_inode_item *item, |
39279cc3 CM |
3901 | struct inode *inode) |
3902 | { | |
51fab693 LB |
3903 | struct btrfs_map_token token; |
3904 | ||
3905 | btrfs_init_map_token(&token); | |
5f39d397 | 3906 | |
51fab693 LB |
3907 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3908 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3909 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3910 | &token); | |
3911 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3912 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3913 | |
a937b979 | 3914 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3915 | inode->i_atime.tv_sec, &token); |
a937b979 | 3916 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3917 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3918 | |
a937b979 | 3919 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3920 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3921 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3922 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3923 | |
a937b979 | 3924 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3925 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3926 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3927 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3928 | |
9cc97d64 | 3929 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3930 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3931 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3932 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3933 | ||
51fab693 LB |
3934 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3935 | &token); | |
3936 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3937 | &token); | |
3938 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3939 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3940 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3941 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3942 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3943 | } |
3944 | ||
d352ac68 CM |
3945 | /* |
3946 | * copy everything in the in-memory inode into the btree. | |
3947 | */ | |
2115133f | 3948 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3949 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3950 | { |
3951 | struct btrfs_inode_item *inode_item; | |
3952 | struct btrfs_path *path; | |
5f39d397 | 3953 | struct extent_buffer *leaf; |
39279cc3 CM |
3954 | int ret; |
3955 | ||
3956 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3957 | if (!path) |
3958 | return -ENOMEM; | |
3959 | ||
b9473439 | 3960 | path->leave_spinning = 1; |
16cdcec7 MX |
3961 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3962 | 1); | |
39279cc3 CM |
3963 | if (ret) { |
3964 | if (ret > 0) | |
3965 | ret = -ENOENT; | |
3966 | goto failed; | |
3967 | } | |
3968 | ||
5f39d397 CM |
3969 | leaf = path->nodes[0]; |
3970 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3971 | struct btrfs_inode_item); |
39279cc3 | 3972 | |
e02119d5 | 3973 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3974 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3975 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3976 | ret = 0; |
3977 | failed: | |
39279cc3 CM |
3978 | btrfs_free_path(path); |
3979 | return ret; | |
3980 | } | |
3981 | ||
2115133f CM |
3982 | /* |
3983 | * copy everything in the in-memory inode into the btree. | |
3984 | */ | |
3985 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3986 | struct btrfs_root *root, struct inode *inode) | |
3987 | { | |
0b246afa | 3988 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3989 | int ret; |
3990 | ||
3991 | /* | |
3992 | * If the inode is a free space inode, we can deadlock during commit | |
3993 | * if we put it into the delayed code. | |
3994 | * | |
3995 | * The data relocation inode should also be directly updated | |
3996 | * without delay | |
3997 | */ | |
70ddc553 | 3998 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
1d52c78a | 3999 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 4000 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
4001 | btrfs_update_root_times(trans, root); |
4002 | ||
2115133f CM |
4003 | ret = btrfs_delayed_update_inode(trans, root, inode); |
4004 | if (!ret) | |
4005 | btrfs_set_inode_last_trans(trans, inode); | |
4006 | return ret; | |
4007 | } | |
4008 | ||
4009 | return btrfs_update_inode_item(trans, root, inode); | |
4010 | } | |
4011 | ||
be6aef60 JB |
4012 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
4013 | struct btrfs_root *root, | |
4014 | struct inode *inode) | |
2115133f CM |
4015 | { |
4016 | int ret; | |
4017 | ||
4018 | ret = btrfs_update_inode(trans, root, inode); | |
4019 | if (ret == -ENOSPC) | |
4020 | return btrfs_update_inode_item(trans, root, inode); | |
4021 | return ret; | |
4022 | } | |
4023 | ||
d352ac68 CM |
4024 | /* |
4025 | * unlink helper that gets used here in inode.c and in the tree logging | |
4026 | * recovery code. It remove a link in a directory with a given name, and | |
4027 | * also drops the back refs in the inode to the directory | |
4028 | */ | |
92986796 AV |
4029 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4030 | struct btrfs_root *root, | |
4ec5934e NB |
4031 | struct btrfs_inode *dir, |
4032 | struct btrfs_inode *inode, | |
92986796 | 4033 | const char *name, int name_len) |
39279cc3 | 4034 | { |
0b246afa | 4035 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4036 | struct btrfs_path *path; |
39279cc3 | 4037 | int ret = 0; |
5f39d397 | 4038 | struct extent_buffer *leaf; |
39279cc3 | 4039 | struct btrfs_dir_item *di; |
5f39d397 | 4040 | struct btrfs_key key; |
aec7477b | 4041 | u64 index; |
33345d01 LZ |
4042 | u64 ino = btrfs_ino(inode); |
4043 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
4044 | |
4045 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
4046 | if (!path) { |
4047 | ret = -ENOMEM; | |
554233a6 | 4048 | goto out; |
54aa1f4d CM |
4049 | } |
4050 | ||
b9473439 | 4051 | path->leave_spinning = 1; |
33345d01 | 4052 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4053 | name, name_len, -1); |
4054 | if (IS_ERR(di)) { | |
4055 | ret = PTR_ERR(di); | |
4056 | goto err; | |
4057 | } | |
4058 | if (!di) { | |
4059 | ret = -ENOENT; | |
4060 | goto err; | |
4061 | } | |
5f39d397 CM |
4062 | leaf = path->nodes[0]; |
4063 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4064 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4065 | if (ret) |
4066 | goto err; | |
b3b4aa74 | 4067 | btrfs_release_path(path); |
39279cc3 | 4068 | |
67de1176 MX |
4069 | /* |
4070 | * If we don't have dir index, we have to get it by looking up | |
4071 | * the inode ref, since we get the inode ref, remove it directly, | |
4072 | * it is unnecessary to do delayed deletion. | |
4073 | * | |
4074 | * But if we have dir index, needn't search inode ref to get it. | |
4075 | * Since the inode ref is close to the inode item, it is better | |
4076 | * that we delay to delete it, and just do this deletion when | |
4077 | * we update the inode item. | |
4078 | */ | |
4ec5934e | 4079 | if (inode->dir_index) { |
67de1176 MX |
4080 | ret = btrfs_delayed_delete_inode_ref(inode); |
4081 | if (!ret) { | |
4ec5934e | 4082 | index = inode->dir_index; |
67de1176 MX |
4083 | goto skip_backref; |
4084 | } | |
4085 | } | |
4086 | ||
33345d01 LZ |
4087 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4088 | dir_ino, &index); | |
aec7477b | 4089 | if (ret) { |
0b246afa | 4090 | btrfs_info(fs_info, |
c2cf52eb | 4091 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4092 | name_len, name, ino, dir_ino); |
66642832 | 4093 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4094 | goto err; |
4095 | } | |
67de1176 | 4096 | skip_backref: |
2ff7e61e | 4097 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
79787eaa | 4098 | if (ret) { |
66642832 | 4099 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4100 | goto err; |
79787eaa | 4101 | } |
39279cc3 | 4102 | |
4ec5934e NB |
4103 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
4104 | dir_ino); | |
79787eaa | 4105 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4106 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4107 | goto err; |
4108 | } | |
e02119d5 | 4109 | |
4ec5934e NB |
4110 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
4111 | index); | |
6418c961 CM |
4112 | if (ret == -ENOENT) |
4113 | ret = 0; | |
d4e3991b | 4114 | else if (ret) |
66642832 | 4115 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4116 | err: |
4117 | btrfs_free_path(path); | |
e02119d5 CM |
4118 | if (ret) |
4119 | goto out; | |
4120 | ||
6ef06d27 | 4121 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
4ec5934e NB |
4122 | inode_inc_iversion(&inode->vfs_inode); |
4123 | inode_inc_iversion(&dir->vfs_inode); | |
4124 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = | |
4125 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); | |
4126 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); | |
e02119d5 | 4127 | out: |
39279cc3 CM |
4128 | return ret; |
4129 | } | |
4130 | ||
92986796 AV |
4131 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4132 | struct btrfs_root *root, | |
4ec5934e | 4133 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
92986796 AV |
4134 | const char *name, int name_len) |
4135 | { | |
4136 | int ret; | |
4137 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4138 | if (!ret) { | |
4ec5934e NB |
4139 | drop_nlink(&inode->vfs_inode); |
4140 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); | |
92986796 AV |
4141 | } |
4142 | return ret; | |
4143 | } | |
39279cc3 | 4144 | |
a22285a6 YZ |
4145 | /* |
4146 | * helper to start transaction for unlink and rmdir. | |
4147 | * | |
d52be818 JB |
4148 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4149 | * if we cannot make our reservations the normal way try and see if there is | |
4150 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4151 | * allow the unlink to occur. | |
a22285a6 | 4152 | */ |
d52be818 | 4153 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4154 | { |
a22285a6 | 4155 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4156 | |
e70bea5f JB |
4157 | /* |
4158 | * 1 for the possible orphan item | |
4159 | * 1 for the dir item | |
4160 | * 1 for the dir index | |
4161 | * 1 for the inode ref | |
e70bea5f JB |
4162 | * 1 for the inode |
4163 | */ | |
8eab77ff | 4164 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4165 | } |
4166 | ||
4167 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4168 | { | |
4169 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4170 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4171 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4172 | int ret; |
a22285a6 | 4173 | |
d52be818 | 4174 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4175 | if (IS_ERR(trans)) |
4176 | return PTR_ERR(trans); | |
5f39d397 | 4177 | |
4ec5934e NB |
4178 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
4179 | 0); | |
12fcfd22 | 4180 | |
4ec5934e NB |
4181 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4182 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4183 | dentry->d_name.len); | |
b532402e TI |
4184 | if (ret) |
4185 | goto out; | |
7b128766 | 4186 | |
a22285a6 | 4187 | if (inode->i_nlink == 0) { |
73f2e545 | 4188 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
b532402e TI |
4189 | if (ret) |
4190 | goto out; | |
a22285a6 | 4191 | } |
7b128766 | 4192 | |
b532402e | 4193 | out: |
3a45bb20 | 4194 | btrfs_end_transaction(trans); |
2ff7e61e | 4195 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4196 | return ret; |
4197 | } | |
4198 | ||
4df27c4d YZ |
4199 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4200 | struct btrfs_root *root, | |
4201 | struct inode *dir, u64 objectid, | |
4202 | const char *name, int name_len) | |
4203 | { | |
0b246afa | 4204 | struct btrfs_fs_info *fs_info = root->fs_info; |
4df27c4d YZ |
4205 | struct btrfs_path *path; |
4206 | struct extent_buffer *leaf; | |
4207 | struct btrfs_dir_item *di; | |
4208 | struct btrfs_key key; | |
4209 | u64 index; | |
4210 | int ret; | |
4a0cc7ca | 4211 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4212 | |
4213 | path = btrfs_alloc_path(); | |
4214 | if (!path) | |
4215 | return -ENOMEM; | |
4216 | ||
33345d01 | 4217 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4218 | name, name_len, -1); |
79787eaa JM |
4219 | if (IS_ERR_OR_NULL(di)) { |
4220 | if (!di) | |
4221 | ret = -ENOENT; | |
4222 | else | |
4223 | ret = PTR_ERR(di); | |
4224 | goto out; | |
4225 | } | |
4df27c4d YZ |
4226 | |
4227 | leaf = path->nodes[0]; | |
4228 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4229 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4230 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4231 | if (ret) { |
66642832 | 4232 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4233 | goto out; |
4234 | } | |
b3b4aa74 | 4235 | btrfs_release_path(path); |
4df27c4d | 4236 | |
0b246afa JM |
4237 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
4238 | root->root_key.objectid, dir_ino, | |
4239 | &index, name, name_len); | |
4df27c4d | 4240 | if (ret < 0) { |
79787eaa | 4241 | if (ret != -ENOENT) { |
66642832 | 4242 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4243 | goto out; |
4244 | } | |
33345d01 | 4245 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4246 | name, name_len); |
79787eaa JM |
4247 | if (IS_ERR_OR_NULL(di)) { |
4248 | if (!di) | |
4249 | ret = -ENOENT; | |
4250 | else | |
4251 | ret = PTR_ERR(di); | |
66642832 | 4252 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4253 | goto out; |
4254 | } | |
4df27c4d YZ |
4255 | |
4256 | leaf = path->nodes[0]; | |
4257 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4258 | btrfs_release_path(path); |
4df27c4d YZ |
4259 | index = key.offset; |
4260 | } | |
945d8962 | 4261 | btrfs_release_path(path); |
4df27c4d | 4262 | |
e67bbbb9 | 4263 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4264 | if (ret) { |
66642832 | 4265 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4266 | goto out; |
4267 | } | |
4df27c4d | 4268 | |
6ef06d27 | 4269 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
0c4d2d95 | 4270 | inode_inc_iversion(dir); |
c2050a45 | 4271 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4272 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4273 | if (ret) |
66642832 | 4274 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4275 | out: |
71d7aed0 | 4276 | btrfs_free_path(path); |
79787eaa | 4277 | return ret; |
4df27c4d YZ |
4278 | } |
4279 | ||
39279cc3 CM |
4280 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4281 | { | |
2b0143b5 | 4282 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4283 | int err = 0; |
39279cc3 | 4284 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4285 | struct btrfs_trans_handle *trans; |
44f714da | 4286 | u64 last_unlink_trans; |
39279cc3 | 4287 | |
b3ae244e | 4288 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4289 | return -ENOTEMPTY; |
4a0cc7ca | 4290 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
b3ae244e | 4291 | return -EPERM; |
134d4512 | 4292 | |
d52be818 | 4293 | trans = __unlink_start_trans(dir); |
a22285a6 | 4294 | if (IS_ERR(trans)) |
5df6a9f6 | 4295 | return PTR_ERR(trans); |
5df6a9f6 | 4296 | |
4a0cc7ca | 4297 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4298 | err = btrfs_unlink_subvol(trans, root, dir, |
4299 | BTRFS_I(inode)->location.objectid, | |
4300 | dentry->d_name.name, | |
4301 | dentry->d_name.len); | |
4302 | goto out; | |
4303 | } | |
4304 | ||
73f2e545 | 4305 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
7b128766 | 4306 | if (err) |
4df27c4d | 4307 | goto out; |
7b128766 | 4308 | |
44f714da FM |
4309 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4310 | ||
39279cc3 | 4311 | /* now the directory is empty */ |
4ec5934e NB |
4312 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
4313 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, | |
4314 | dentry->d_name.len); | |
44f714da | 4315 | if (!err) { |
6ef06d27 | 4316 | btrfs_i_size_write(BTRFS_I(inode), 0); |
44f714da FM |
4317 | /* |
4318 | * Propagate the last_unlink_trans value of the deleted dir to | |
4319 | * its parent directory. This is to prevent an unrecoverable | |
4320 | * log tree in the case we do something like this: | |
4321 | * 1) create dir foo | |
4322 | * 2) create snapshot under dir foo | |
4323 | * 3) delete the snapshot | |
4324 | * 4) rmdir foo | |
4325 | * 5) mkdir foo | |
4326 | * 6) fsync foo or some file inside foo | |
4327 | */ | |
4328 | if (last_unlink_trans >= trans->transid) | |
4329 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4330 | } | |
4df27c4d | 4331 | out: |
3a45bb20 | 4332 | btrfs_end_transaction(trans); |
2ff7e61e | 4333 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4334 | |
39279cc3 CM |
4335 | return err; |
4336 | } | |
4337 | ||
28f75a0e CM |
4338 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4339 | struct btrfs_root *root, | |
4340 | u64 bytes_deleted) | |
4341 | { | |
0b246afa | 4342 | struct btrfs_fs_info *fs_info = root->fs_info; |
28f75a0e CM |
4343 | int ret; |
4344 | ||
dc95f7bf JB |
4345 | /* |
4346 | * This is only used to apply pressure to the enospc system, we don't | |
4347 | * intend to use this reservation at all. | |
4348 | */ | |
2ff7e61e | 4349 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
0b246afa JM |
4350 | bytes_deleted *= fs_info->nodesize; |
4351 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
28f75a0e | 4352 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
dc95f7bf | 4353 | if (!ret) { |
0b246afa | 4354 | trace_btrfs_space_reservation(fs_info, "transaction", |
dc95f7bf JB |
4355 | trans->transid, |
4356 | bytes_deleted, 1); | |
28f75a0e | 4357 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4358 | } |
28f75a0e CM |
4359 | return ret; |
4360 | ||
4361 | } | |
4362 | ||
0305cd5f FM |
4363 | static int truncate_inline_extent(struct inode *inode, |
4364 | struct btrfs_path *path, | |
4365 | struct btrfs_key *found_key, | |
4366 | const u64 item_end, | |
4367 | const u64 new_size) | |
4368 | { | |
4369 | struct extent_buffer *leaf = path->nodes[0]; | |
4370 | int slot = path->slots[0]; | |
4371 | struct btrfs_file_extent_item *fi; | |
4372 | u32 size = (u32)(new_size - found_key->offset); | |
4373 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4374 | ||
4375 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4376 | ||
4377 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4378 | loff_t offset = new_size; | |
09cbfeaf | 4379 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4380 | |
4381 | /* | |
4382 | * Zero out the remaining of the last page of our inline extent, | |
4383 | * instead of directly truncating our inline extent here - that | |
4384 | * would be much more complex (decompressing all the data, then | |
4385 | * compressing the truncated data, which might be bigger than | |
4386 | * the size of the inline extent, resize the extent, etc). | |
4387 | * We release the path because to get the page we might need to | |
4388 | * read the extent item from disk (data not in the page cache). | |
4389 | */ | |
4390 | btrfs_release_path(path); | |
9703fefe CR |
4391 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4392 | 0); | |
0305cd5f FM |
4393 | } |
4394 | ||
4395 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4396 | size = btrfs_file_extent_calc_inline_size(size); | |
2ff7e61e | 4397 | btrfs_truncate_item(root->fs_info, path, size, 1); |
0305cd5f FM |
4398 | |
4399 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4400 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4401 | ||
4402 | return 0; | |
4403 | } | |
4404 | ||
39279cc3 CM |
4405 | /* |
4406 | * this can truncate away extent items, csum items and directory items. | |
4407 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4408 | * any higher than new_size |
39279cc3 CM |
4409 | * |
4410 | * csum items that cross the new i_size are truncated to the new size | |
4411 | * as well. | |
7b128766 JB |
4412 | * |
4413 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4414 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4415 | */ |
8082510e YZ |
4416 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4417 | struct btrfs_root *root, | |
4418 | struct inode *inode, | |
4419 | u64 new_size, u32 min_type) | |
39279cc3 | 4420 | { |
0b246afa | 4421 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4422 | struct btrfs_path *path; |
5f39d397 | 4423 | struct extent_buffer *leaf; |
39279cc3 | 4424 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4425 | struct btrfs_key key; |
4426 | struct btrfs_key found_key; | |
39279cc3 | 4427 | u64 extent_start = 0; |
db94535d | 4428 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4429 | u64 extent_offset = 0; |
39279cc3 | 4430 | u64 item_end = 0; |
c1aa4575 | 4431 | u64 last_size = new_size; |
8082510e | 4432 | u32 found_type = (u8)-1; |
39279cc3 CM |
4433 | int found_extent; |
4434 | int del_item; | |
85e21bac CM |
4435 | int pending_del_nr = 0; |
4436 | int pending_del_slot = 0; | |
179e29e4 | 4437 | int extent_type = -1; |
8082510e YZ |
4438 | int ret; |
4439 | int err = 0; | |
4a0cc7ca | 4440 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4441 | u64 bytes_deleted = 0; |
1262133b JB |
4442 | bool be_nice = 0; |
4443 | bool should_throttle = 0; | |
28f75a0e | 4444 | bool should_end = 0; |
8082510e YZ |
4445 | |
4446 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4447 | |
28ed1345 CM |
4448 | /* |
4449 | * for non-free space inodes and ref cows, we want to back off from | |
4450 | * time to time | |
4451 | */ | |
70ddc553 | 4452 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
28ed1345 CM |
4453 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
4454 | be_nice = 1; | |
4455 | ||
0eb0e19c MF |
4456 | path = btrfs_alloc_path(); |
4457 | if (!path) | |
4458 | return -ENOMEM; | |
e4058b54 | 4459 | path->reada = READA_BACK; |
0eb0e19c | 4460 | |
5dc562c5 JB |
4461 | /* |
4462 | * We want to drop from the next block forward in case this new size is | |
4463 | * not block aligned since we will be keeping the last block of the | |
4464 | * extent just the way it is. | |
4465 | */ | |
27cdeb70 | 4466 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4467 | root == fs_info->tree_root) |
dcdbc059 | 4468 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
0b246afa | 4469 | fs_info->sectorsize), |
da17066c | 4470 | (u64)-1, 0); |
8082510e | 4471 | |
16cdcec7 MX |
4472 | /* |
4473 | * This function is also used to drop the items in the log tree before | |
4474 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4475 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4476 | * items. | |
4477 | */ | |
4478 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4479 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4480 | |
33345d01 | 4481 | key.objectid = ino; |
39279cc3 | 4482 | key.offset = (u64)-1; |
5f39d397 CM |
4483 | key.type = (u8)-1; |
4484 | ||
85e21bac | 4485 | search_again: |
28ed1345 CM |
4486 | /* |
4487 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4488 | * up a huge file in a single leaf. Most of the time that | |
4489 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4490 | */ | |
ee22184b | 4491 | if (be_nice && bytes_deleted > SZ_32M) { |
3a45bb20 | 4492 | if (btrfs_should_end_transaction(trans)) { |
28ed1345 CM |
4493 | err = -EAGAIN; |
4494 | goto error; | |
4495 | } | |
4496 | } | |
4497 | ||
4498 | ||
b9473439 | 4499 | path->leave_spinning = 1; |
85e21bac | 4500 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4501 | if (ret < 0) { |
4502 | err = ret; | |
4503 | goto out; | |
4504 | } | |
d397712b | 4505 | |
85e21bac | 4506 | if (ret > 0) { |
e02119d5 CM |
4507 | /* there are no items in the tree for us to truncate, we're |
4508 | * done | |
4509 | */ | |
8082510e YZ |
4510 | if (path->slots[0] == 0) |
4511 | goto out; | |
85e21bac CM |
4512 | path->slots[0]--; |
4513 | } | |
4514 | ||
d397712b | 4515 | while (1) { |
39279cc3 | 4516 | fi = NULL; |
5f39d397 CM |
4517 | leaf = path->nodes[0]; |
4518 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4519 | found_type = found_key.type; |
39279cc3 | 4520 | |
33345d01 | 4521 | if (found_key.objectid != ino) |
39279cc3 | 4522 | break; |
5f39d397 | 4523 | |
85e21bac | 4524 | if (found_type < min_type) |
39279cc3 CM |
4525 | break; |
4526 | ||
5f39d397 | 4527 | item_end = found_key.offset; |
39279cc3 | 4528 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4529 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4530 | struct btrfs_file_extent_item); |
179e29e4 CM |
4531 | extent_type = btrfs_file_extent_type(leaf, fi); |
4532 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4533 | item_end += |
db94535d | 4534 | btrfs_file_extent_num_bytes(leaf, fi); |
09ed2f16 LB |
4535 | |
4536 | trace_btrfs_truncate_show_fi_regular( | |
4537 | BTRFS_I(inode), leaf, fi, | |
4538 | found_key.offset); | |
179e29e4 | 4539 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4540 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4541 | path->slots[0], fi); |
09ed2f16 LB |
4542 | |
4543 | trace_btrfs_truncate_show_fi_inline( | |
4544 | BTRFS_I(inode), leaf, fi, path->slots[0], | |
4545 | found_key.offset); | |
39279cc3 | 4546 | } |
008630c1 | 4547 | item_end--; |
39279cc3 | 4548 | } |
8082510e YZ |
4549 | if (found_type > min_type) { |
4550 | del_item = 1; | |
4551 | } else { | |
76b42abb | 4552 | if (item_end < new_size) |
b888db2b | 4553 | break; |
8082510e YZ |
4554 | if (found_key.offset >= new_size) |
4555 | del_item = 1; | |
4556 | else | |
4557 | del_item = 0; | |
39279cc3 | 4558 | } |
39279cc3 | 4559 | found_extent = 0; |
39279cc3 | 4560 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4561 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4562 | goto delete; | |
4563 | ||
7f4f6e0a JB |
4564 | if (del_item) |
4565 | last_size = found_key.offset; | |
4566 | else | |
4567 | last_size = new_size; | |
4568 | ||
179e29e4 | 4569 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4570 | u64 num_dec; |
db94535d | 4571 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4572 | if (!del_item) { |
db94535d CM |
4573 | u64 orig_num_bytes = |
4574 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4575 | extent_num_bytes = ALIGN(new_size - |
4576 | found_key.offset, | |
0b246afa | 4577 | fs_info->sectorsize); |
db94535d CM |
4578 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4579 | extent_num_bytes); | |
4580 | num_dec = (orig_num_bytes - | |
9069218d | 4581 | extent_num_bytes); |
27cdeb70 MX |
4582 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4583 | &root->state) && | |
4584 | extent_start != 0) | |
a76a3cd4 | 4585 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4586 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4587 | } else { |
db94535d CM |
4588 | extent_num_bytes = |
4589 | btrfs_file_extent_disk_num_bytes(leaf, | |
4590 | fi); | |
5d4f98a2 YZ |
4591 | extent_offset = found_key.offset - |
4592 | btrfs_file_extent_offset(leaf, fi); | |
4593 | ||
39279cc3 | 4594 | /* FIXME blocksize != 4096 */ |
9069218d | 4595 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4596 | if (extent_start != 0) { |
4597 | found_extent = 1; | |
27cdeb70 MX |
4598 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4599 | &root->state)) | |
a76a3cd4 | 4600 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4601 | } |
39279cc3 | 4602 | } |
9069218d | 4603 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4604 | /* |
4605 | * we can't truncate inline items that have had | |
4606 | * special encodings | |
4607 | */ | |
4608 | if (!del_item && | |
c8b97818 CM |
4609 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4610 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4611 | |
4612 | /* | |
0305cd5f FM |
4613 | * Need to release path in order to truncate a |
4614 | * compressed extent. So delete any accumulated | |
4615 | * extent items so far. | |
514ac8ad | 4616 | */ |
0305cd5f FM |
4617 | if (btrfs_file_extent_compression(leaf, fi) != |
4618 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4619 | err = btrfs_del_items(trans, root, path, | |
4620 | pending_del_slot, | |
4621 | pending_del_nr); | |
4622 | if (err) { | |
4623 | btrfs_abort_transaction(trans, | |
0305cd5f FM |
4624 | err); |
4625 | goto error; | |
4626 | } | |
4627 | pending_del_nr = 0; | |
4628 | } | |
4629 | ||
4630 | err = truncate_inline_extent(inode, path, | |
4631 | &found_key, | |
4632 | item_end, | |
4633 | new_size); | |
4634 | if (err) { | |
66642832 | 4635 | btrfs_abort_transaction(trans, err); |
0305cd5f FM |
4636 | goto error; |
4637 | } | |
27cdeb70 MX |
4638 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4639 | &root->state)) { | |
0305cd5f | 4640 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4641 | } |
39279cc3 | 4642 | } |
179e29e4 | 4643 | delete: |
39279cc3 | 4644 | if (del_item) { |
85e21bac CM |
4645 | if (!pending_del_nr) { |
4646 | /* no pending yet, add ourselves */ | |
4647 | pending_del_slot = path->slots[0]; | |
4648 | pending_del_nr = 1; | |
4649 | } else if (pending_del_nr && | |
4650 | path->slots[0] + 1 == pending_del_slot) { | |
4651 | /* hop on the pending chunk */ | |
4652 | pending_del_nr++; | |
4653 | pending_del_slot = path->slots[0]; | |
4654 | } else { | |
d397712b | 4655 | BUG(); |
85e21bac | 4656 | } |
39279cc3 CM |
4657 | } else { |
4658 | break; | |
4659 | } | |
28f75a0e CM |
4660 | should_throttle = 0; |
4661 | ||
27cdeb70 MX |
4662 | if (found_extent && |
4663 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4664 | root == fs_info->tree_root)) { |
b9473439 | 4665 | btrfs_set_path_blocking(path); |
28ed1345 | 4666 | bytes_deleted += extent_num_bytes; |
2ff7e61e | 4667 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
5d4f98a2 YZ |
4668 | extent_num_bytes, 0, |
4669 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4670 | ino, extent_offset); |
39279cc3 | 4671 | BUG_ON(ret); |
2ff7e61e JM |
4672 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
4673 | btrfs_async_run_delayed_refs(fs_info, | |
dd4b857a WX |
4674 | trans->delayed_ref_updates * 2, |
4675 | trans->transid, 0); | |
28f75a0e CM |
4676 | if (be_nice) { |
4677 | if (truncate_space_check(trans, root, | |
4678 | extent_num_bytes)) { | |
4679 | should_end = 1; | |
4680 | } | |
4681 | if (btrfs_should_throttle_delayed_refs(trans, | |
2ff7e61e | 4682 | fs_info)) |
28f75a0e | 4683 | should_throttle = 1; |
28f75a0e | 4684 | } |
39279cc3 | 4685 | } |
85e21bac | 4686 | |
8082510e YZ |
4687 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4688 | break; | |
4689 | ||
4690 | if (path->slots[0] == 0 || | |
1262133b | 4691 | path->slots[0] != pending_del_slot || |
28f75a0e | 4692 | should_throttle || should_end) { |
8082510e YZ |
4693 | if (pending_del_nr) { |
4694 | ret = btrfs_del_items(trans, root, path, | |
4695 | pending_del_slot, | |
4696 | pending_del_nr); | |
79787eaa | 4697 | if (ret) { |
66642832 | 4698 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4699 | goto error; |
4700 | } | |
8082510e YZ |
4701 | pending_del_nr = 0; |
4702 | } | |
b3b4aa74 | 4703 | btrfs_release_path(path); |
28f75a0e | 4704 | if (should_throttle) { |
1262133b JB |
4705 | unsigned long updates = trans->delayed_ref_updates; |
4706 | if (updates) { | |
4707 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4708 | ret = btrfs_run_delayed_refs(trans, |
4709 | fs_info, | |
4710 | updates * 2); | |
1262133b JB |
4711 | if (ret && !err) |
4712 | err = ret; | |
4713 | } | |
4714 | } | |
28f75a0e CM |
4715 | /* |
4716 | * if we failed to refill our space rsv, bail out | |
4717 | * and let the transaction restart | |
4718 | */ | |
4719 | if (should_end) { | |
4720 | err = -EAGAIN; | |
4721 | goto error; | |
4722 | } | |
85e21bac | 4723 | goto search_again; |
8082510e YZ |
4724 | } else { |
4725 | path->slots[0]--; | |
85e21bac | 4726 | } |
39279cc3 | 4727 | } |
8082510e | 4728 | out: |
85e21bac CM |
4729 | if (pending_del_nr) { |
4730 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4731 | pending_del_nr); | |
79787eaa | 4732 | if (ret) |
66642832 | 4733 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4734 | } |
79787eaa | 4735 | error: |
76b42abb FM |
4736 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
4737 | ASSERT(last_size >= new_size); | |
4738 | if (!err && last_size > new_size) | |
4739 | last_size = new_size; | |
7f4f6e0a | 4740 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
76b42abb | 4741 | } |
28ed1345 | 4742 | |
39279cc3 | 4743 | btrfs_free_path(path); |
28ed1345 | 4744 | |
ee22184b | 4745 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4746 | unsigned long updates = trans->delayed_ref_updates; |
4747 | if (updates) { | |
4748 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4749 | ret = btrfs_run_delayed_refs(trans, fs_info, |
4750 | updates * 2); | |
28ed1345 CM |
4751 | if (ret && !err) |
4752 | err = ret; | |
4753 | } | |
4754 | } | |
8082510e | 4755 | return err; |
39279cc3 CM |
4756 | } |
4757 | ||
4758 | /* | |
9703fefe | 4759 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4760 | * @inode - inode that we're zeroing |
4761 | * @from - the offset to start zeroing | |
4762 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4763 | * offset | |
4764 | * @front - zero up to the offset instead of from the offset on | |
4765 | * | |
9703fefe | 4766 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4767 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4768 | */ |
9703fefe | 4769 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4770 | int front) |
39279cc3 | 4771 | { |
0b246afa | 4772 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4773 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4774 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4775 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4776 | struct extent_state *cached_state = NULL; |
364ecf36 | 4777 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc | 4778 | char *kaddr; |
0b246afa | 4779 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4780 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4781 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4782 | struct page *page; |
3b16a4e3 | 4783 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4784 | int ret = 0; |
9703fefe CR |
4785 | u64 block_start; |
4786 | u64 block_end; | |
39279cc3 | 4787 | |
2aaa6655 JB |
4788 | if ((offset & (blocksize - 1)) == 0 && |
4789 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4790 | goto out; |
9703fefe | 4791 | |
364ecf36 | 4792 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
9703fefe | 4793 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4794 | if (ret) |
4795 | goto out; | |
39279cc3 | 4796 | |
211c17f5 | 4797 | again: |
3b16a4e3 | 4798 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4799 | if (!page) { |
bc42bda2 | 4800 | btrfs_delalloc_release_space(inode, data_reserved, |
9703fefe CR |
4801 | round_down(from, blocksize), |
4802 | blocksize); | |
ac6a2b36 | 4803 | ret = -ENOMEM; |
39279cc3 | 4804 | goto out; |
5d5e103a | 4805 | } |
e6dcd2dc | 4806 | |
9703fefe CR |
4807 | block_start = round_down(from, blocksize); |
4808 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4809 | |
39279cc3 | 4810 | if (!PageUptodate(page)) { |
9ebefb18 | 4811 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4812 | lock_page(page); |
211c17f5 CM |
4813 | if (page->mapping != mapping) { |
4814 | unlock_page(page); | |
09cbfeaf | 4815 | put_page(page); |
211c17f5 CM |
4816 | goto again; |
4817 | } | |
39279cc3 CM |
4818 | if (!PageUptodate(page)) { |
4819 | ret = -EIO; | |
89642229 | 4820 | goto out_unlock; |
39279cc3 CM |
4821 | } |
4822 | } | |
211c17f5 | 4823 | wait_on_page_writeback(page); |
e6dcd2dc | 4824 | |
9703fefe | 4825 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4826 | set_page_extent_mapped(page); |
4827 | ||
9703fefe | 4828 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4829 | if (ordered) { |
9703fefe | 4830 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4831 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4832 | unlock_page(page); |
09cbfeaf | 4833 | put_page(page); |
eb84ae03 | 4834 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4835 | btrfs_put_ordered_extent(ordered); |
4836 | goto again; | |
4837 | } | |
4838 | ||
9703fefe | 4839 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4840 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4841 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4842 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4843 | |
9703fefe | 4844 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4845 | &cached_state, 0); |
9ed74f2d | 4846 | if (ret) { |
9703fefe | 4847 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4848 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4849 | goto out_unlock; |
4850 | } | |
4851 | ||
9703fefe | 4852 | if (offset != blocksize) { |
2aaa6655 | 4853 | if (!len) |
9703fefe | 4854 | len = blocksize - offset; |
e6dcd2dc | 4855 | kaddr = kmap(page); |
2aaa6655 | 4856 | if (front) |
9703fefe CR |
4857 | memset(kaddr + (block_start - page_offset(page)), |
4858 | 0, offset); | |
2aaa6655 | 4859 | else |
9703fefe CR |
4860 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4861 | 0, len); | |
e6dcd2dc CM |
4862 | flush_dcache_page(page); |
4863 | kunmap(page); | |
4864 | } | |
247e743c | 4865 | ClearPageChecked(page); |
e6dcd2dc | 4866 | set_page_dirty(page); |
9703fefe | 4867 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4868 | GFP_NOFS); |
39279cc3 | 4869 | |
89642229 | 4870 | out_unlock: |
5d5e103a | 4871 | if (ret) |
bc42bda2 | 4872 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
9703fefe | 4873 | blocksize); |
39279cc3 | 4874 | unlock_page(page); |
09cbfeaf | 4875 | put_page(page); |
39279cc3 | 4876 | out: |
364ecf36 | 4877 | extent_changeset_free(data_reserved); |
39279cc3 CM |
4878 | return ret; |
4879 | } | |
4880 | ||
16e7549f JB |
4881 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4882 | u64 offset, u64 len) | |
4883 | { | |
0b246afa | 4884 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4885 | struct btrfs_trans_handle *trans; |
4886 | int ret; | |
4887 | ||
4888 | /* | |
4889 | * Still need to make sure the inode looks like it's been updated so | |
4890 | * that any holes get logged if we fsync. | |
4891 | */ | |
0b246afa JM |
4892 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4893 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4894 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4895 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4896 | return 0; | |
4897 | } | |
4898 | ||
4899 | /* | |
4900 | * 1 - for the one we're dropping | |
4901 | * 1 - for the one we're adding | |
4902 | * 1 - for updating the inode. | |
4903 | */ | |
4904 | trans = btrfs_start_transaction(root, 3); | |
4905 | if (IS_ERR(trans)) | |
4906 | return PTR_ERR(trans); | |
4907 | ||
4908 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4909 | if (ret) { | |
66642832 | 4910 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4911 | btrfs_end_transaction(trans); |
16e7549f JB |
4912 | return ret; |
4913 | } | |
4914 | ||
f85b7379 DS |
4915 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
4916 | offset, 0, 0, len, 0, len, 0, 0, 0); | |
16e7549f | 4917 | if (ret) |
66642832 | 4918 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4919 | else |
4920 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4921 | btrfs_end_transaction(trans); |
16e7549f JB |
4922 | return ret; |
4923 | } | |
4924 | ||
695a0d0d JB |
4925 | /* |
4926 | * This function puts in dummy file extents for the area we're creating a hole | |
4927 | * for. So if we are truncating this file to a larger size we need to insert | |
4928 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4929 | * the range between oldsize and size | |
4930 | */ | |
a41ad394 | 4931 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4932 | { |
0b246afa | 4933 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4934 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4935 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4936 | struct extent_map *em = NULL; |
2ac55d41 | 4937 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4938 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4939 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4940 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4941 | u64 last_byte; |
4942 | u64 cur_offset; | |
4943 | u64 hole_size; | |
9ed74f2d | 4944 | int err = 0; |
39279cc3 | 4945 | |
a71754fc | 4946 | /* |
9703fefe CR |
4947 | * If our size started in the middle of a block we need to zero out the |
4948 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4949 | * expose stale data. |
4950 | */ | |
9703fefe | 4951 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4952 | if (err) |
4953 | return err; | |
4954 | ||
9036c102 YZ |
4955 | if (size <= hole_start) |
4956 | return 0; | |
4957 | ||
9036c102 YZ |
4958 | while (1) { |
4959 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4960 | |
ff13db41 | 4961 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4962 | &cached_state); |
a776c6fa | 4963 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
fa7c1494 | 4964 | block_end - hole_start); |
9036c102 YZ |
4965 | if (!ordered) |
4966 | break; | |
2ac55d41 JB |
4967 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4968 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4969 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4970 | btrfs_put_ordered_extent(ordered); |
4971 | } | |
39279cc3 | 4972 | |
9036c102 YZ |
4973 | cur_offset = hole_start; |
4974 | while (1) { | |
fc4f21b1 | 4975 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
9036c102 | 4976 | block_end - cur_offset, 0); |
79787eaa JM |
4977 | if (IS_ERR(em)) { |
4978 | err = PTR_ERR(em); | |
f2767956 | 4979 | em = NULL; |
79787eaa JM |
4980 | break; |
4981 | } | |
9036c102 | 4982 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4983 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 4984 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4985 | struct extent_map *hole_em; |
9036c102 | 4986 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4987 | |
16e7549f JB |
4988 | err = maybe_insert_hole(root, inode, cur_offset, |
4989 | hole_size); | |
4990 | if (err) | |
3893e33b | 4991 | break; |
dcdbc059 | 4992 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
4993 | cur_offset + hole_size - 1, 0); |
4994 | hole_em = alloc_extent_map(); | |
4995 | if (!hole_em) { | |
4996 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4997 | &BTRFS_I(inode)->runtime_flags); | |
4998 | goto next; | |
4999 | } | |
5000 | hole_em->start = cur_offset; | |
5001 | hole_em->len = hole_size; | |
5002 | hole_em->orig_start = cur_offset; | |
8082510e | 5003 | |
5dc562c5 JB |
5004 | hole_em->block_start = EXTENT_MAP_HOLE; |
5005 | hole_em->block_len = 0; | |
b4939680 | 5006 | hole_em->orig_block_len = 0; |
cc95bef6 | 5007 | hole_em->ram_bytes = hole_size; |
0b246afa | 5008 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 5009 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 5010 | hole_em->generation = fs_info->generation; |
8082510e | 5011 | |
5dc562c5 JB |
5012 | while (1) { |
5013 | write_lock(&em_tree->lock); | |
09a2a8f9 | 5014 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
5015 | write_unlock(&em_tree->lock); |
5016 | if (err != -EEXIST) | |
5017 | break; | |
dcdbc059 NB |
5018 | btrfs_drop_extent_cache(BTRFS_I(inode), |
5019 | cur_offset, | |
5dc562c5 JB |
5020 | cur_offset + |
5021 | hole_size - 1, 0); | |
5022 | } | |
5023 | free_extent_map(hole_em); | |
9036c102 | 5024 | } |
16e7549f | 5025 | next: |
9036c102 | 5026 | free_extent_map(em); |
a22285a6 | 5027 | em = NULL; |
9036c102 | 5028 | cur_offset = last_byte; |
8082510e | 5029 | if (cur_offset >= block_end) |
9036c102 YZ |
5030 | break; |
5031 | } | |
a22285a6 | 5032 | free_extent_map(em); |
2ac55d41 JB |
5033 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
5034 | GFP_NOFS); | |
9036c102 YZ |
5035 | return err; |
5036 | } | |
39279cc3 | 5037 | |
3972f260 | 5038 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 5039 | { |
f4a2f4c5 MX |
5040 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5041 | struct btrfs_trans_handle *trans; | |
a41ad394 | 5042 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
5043 | loff_t newsize = attr->ia_size; |
5044 | int mask = attr->ia_valid; | |
8082510e YZ |
5045 | int ret; |
5046 | ||
3972f260 ES |
5047 | /* |
5048 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
5049 | * special case where we need to update the times despite not having | |
5050 | * these flags set. For all other operations the VFS set these flags | |
5051 | * explicitly if it wants a timestamp update. | |
5052 | */ | |
dff6efc3 CH |
5053 | if (newsize != oldsize) { |
5054 | inode_inc_iversion(inode); | |
5055 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5056 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5057 | current_time(inode); |
dff6efc3 | 5058 | } |
3972f260 | 5059 | |
a41ad394 | 5060 | if (newsize > oldsize) { |
9ea24bbe | 5061 | /* |
ea14b57f | 5062 | * Don't do an expanding truncate while snapshotting is ongoing. |
9ea24bbe FM |
5063 | * This is to ensure the snapshot captures a fully consistent |
5064 | * state of this file - if the snapshot captures this expanding | |
5065 | * truncation, it must capture all writes that happened before | |
5066 | * this truncation. | |
5067 | */ | |
0bc19f90 | 5068 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5069 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe | 5070 | if (ret) { |
ea14b57f | 5071 | btrfs_end_write_no_snapshotting(root); |
8082510e | 5072 | return ret; |
9ea24bbe | 5073 | } |
8082510e | 5074 | |
f4a2f4c5 | 5075 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe | 5076 | if (IS_ERR(trans)) { |
ea14b57f | 5077 | btrfs_end_write_no_snapshotting(root); |
f4a2f4c5 | 5078 | return PTR_ERR(trans); |
9ea24bbe | 5079 | } |
f4a2f4c5 MX |
5080 | |
5081 | i_size_write(inode, newsize); | |
5082 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5083 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5084 | ret = btrfs_update_inode(trans, root, inode); |
ea14b57f | 5085 | btrfs_end_write_no_snapshotting(root); |
3a45bb20 | 5086 | btrfs_end_transaction(trans); |
a41ad394 | 5087 | } else { |
8082510e | 5088 | |
a41ad394 JB |
5089 | /* |
5090 | * We're truncating a file that used to have good data down to | |
5091 | * zero. Make sure it gets into the ordered flush list so that | |
5092 | * any new writes get down to disk quickly. | |
5093 | */ | |
5094 | if (newsize == 0) | |
72ac3c0d JB |
5095 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5096 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5097 | |
f3fe820c JB |
5098 | /* |
5099 | * 1 for the orphan item we're going to add | |
5100 | * 1 for the orphan item deletion. | |
5101 | */ | |
5102 | trans = btrfs_start_transaction(root, 2); | |
5103 | if (IS_ERR(trans)) | |
5104 | return PTR_ERR(trans); | |
5105 | ||
5106 | /* | |
5107 | * We need to do this in case we fail at _any_ point during the | |
5108 | * actual truncate. Once we do the truncate_setsize we could | |
5109 | * invalidate pages which forces any outstanding ordered io to | |
5110 | * be instantly completed which will give us extents that need | |
5111 | * to be truncated. If we fail to get an orphan inode down we | |
5112 | * could have left over extents that were never meant to live, | |
01327610 | 5113 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5114 | * will be consistent. |
5115 | */ | |
73f2e545 | 5116 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
3a45bb20 | 5117 | btrfs_end_transaction(trans); |
f3fe820c JB |
5118 | if (ret) |
5119 | return ret; | |
5120 | ||
a41ad394 JB |
5121 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5122 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5123 | |
5124 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
abcefb1e | 5125 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5126 | inode_dio_wait(inode); |
0b581701 | 5127 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
2e60a51e | 5128 | |
a41ad394 | 5129 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5130 | if (ret && inode->i_nlink) { |
5131 | int err; | |
5132 | ||
19fd2df5 LB |
5133 | /* To get a stable disk_i_size */ |
5134 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5135 | if (err) { | |
3d6ae7bb | 5136 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
19fd2df5 LB |
5137 | return err; |
5138 | } | |
5139 | ||
7f4f6e0a JB |
5140 | /* |
5141 | * failed to truncate, disk_i_size is only adjusted down | |
5142 | * as we remove extents, so it should represent the true | |
5143 | * size of the inode, so reset the in memory size and | |
5144 | * delete our orphan entry. | |
5145 | */ | |
5146 | trans = btrfs_join_transaction(root); | |
5147 | if (IS_ERR(trans)) { | |
3d6ae7bb | 5148 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
7f4f6e0a JB |
5149 | return ret; |
5150 | } | |
5151 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
3d6ae7bb | 5152 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
7f4f6e0a | 5153 | if (err) |
66642832 | 5154 | btrfs_abort_transaction(trans, err); |
3a45bb20 | 5155 | btrfs_end_transaction(trans); |
7f4f6e0a | 5156 | } |
8082510e YZ |
5157 | } |
5158 | ||
a41ad394 | 5159 | return ret; |
8082510e YZ |
5160 | } |
5161 | ||
9036c102 YZ |
5162 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5163 | { | |
2b0143b5 | 5164 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5165 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5166 | int err; |
39279cc3 | 5167 | |
b83cc969 LZ |
5168 | if (btrfs_root_readonly(root)) |
5169 | return -EROFS; | |
5170 | ||
31051c85 | 5171 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5172 | if (err) |
5173 | return err; | |
2bf5a725 | 5174 | |
5a3f23d5 | 5175 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5176 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5177 | if (err) |
5178 | return err; | |
39279cc3 | 5179 | } |
9036c102 | 5180 | |
1025774c CH |
5181 | if (attr->ia_valid) { |
5182 | setattr_copy(inode, attr); | |
0c4d2d95 | 5183 | inode_inc_iversion(inode); |
22c44fe6 | 5184 | err = btrfs_dirty_inode(inode); |
1025774c | 5185 | |
22c44fe6 | 5186 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5187 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5188 | } |
33268eaf | 5189 | |
39279cc3 CM |
5190 | return err; |
5191 | } | |
61295eb8 | 5192 | |
131e404a FDBM |
5193 | /* |
5194 | * While truncating the inode pages during eviction, we get the VFS calling | |
5195 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5196 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5197 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5198 | * extent_state structures over and over, wasting lots of time. | |
5199 | * | |
5200 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5201 | * those expensive operations on a per page basis and do only the ordered io | |
5202 | * finishing, while we release here the extent_map and extent_state structures, | |
5203 | * without the excessive merging and splitting. | |
5204 | */ | |
5205 | static void evict_inode_truncate_pages(struct inode *inode) | |
5206 | { | |
5207 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5208 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5209 | struct rb_node *node; | |
5210 | ||
5211 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5212 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5213 | |
5214 | write_lock(&map_tree->lock); | |
5215 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5216 | struct extent_map *em; | |
5217 | ||
5218 | node = rb_first(&map_tree->map); | |
5219 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5220 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5221 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5222 | remove_extent_mapping(map_tree, em); |
5223 | free_extent_map(em); | |
7064dd5c FM |
5224 | if (need_resched()) { |
5225 | write_unlock(&map_tree->lock); | |
5226 | cond_resched(); | |
5227 | write_lock(&map_tree->lock); | |
5228 | } | |
131e404a FDBM |
5229 | } |
5230 | write_unlock(&map_tree->lock); | |
5231 | ||
6ca07097 FM |
5232 | /* |
5233 | * Keep looping until we have no more ranges in the io tree. | |
5234 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5235 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5236 | * still in progress (unlocked the pages in the bio but did not yet | |
5237 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5238 | * ranges can still be locked and eviction started because before |
5239 | * submitting those bios, which are executed by a separate task (work | |
5240 | * queue kthread), inode references (inode->i_count) were not taken | |
5241 | * (which would be dropped in the end io callback of each bio). | |
5242 | * Therefore here we effectively end up waiting for those bios and | |
5243 | * anyone else holding locked ranges without having bumped the inode's | |
5244 | * reference count - if we don't do it, when they access the inode's | |
5245 | * io_tree to unlock a range it may be too late, leading to an | |
5246 | * use-after-free issue. | |
5247 | */ | |
131e404a FDBM |
5248 | spin_lock(&io_tree->lock); |
5249 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5250 | struct extent_state *state; | |
5251 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5252 | u64 start; |
5253 | u64 end; | |
131e404a FDBM |
5254 | |
5255 | node = rb_first(&io_tree->state); | |
5256 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5257 | start = state->start; |
5258 | end = state->end; | |
131e404a FDBM |
5259 | spin_unlock(&io_tree->lock); |
5260 | ||
ff13db41 | 5261 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5262 | |
5263 | /* | |
5264 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5265 | * and its reserved space won't be freed by delayed_ref. | |
5266 | * So we need to free its reserved space here. | |
5267 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5268 | * | |
5269 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5270 | */ | |
5271 | if (state->state & EXTENT_DELALLOC) | |
bc42bda2 | 5272 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
b9d0b389 | 5273 | |
6ca07097 | 5274 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5275 | EXTENT_LOCKED | EXTENT_DIRTY | |
5276 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5277 | EXTENT_DEFRAG, 1, 1, | |
5278 | &cached_state, GFP_NOFS); | |
131e404a | 5279 | |
7064dd5c | 5280 | cond_resched(); |
131e404a FDBM |
5281 | spin_lock(&io_tree->lock); |
5282 | } | |
5283 | spin_unlock(&io_tree->lock); | |
5284 | } | |
5285 | ||
bd555975 | 5286 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5287 | { |
0b246afa | 5288 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5289 | struct btrfs_trans_handle *trans; |
5290 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5291 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5292 | int steal_from_global = 0; |
3d48d981 | 5293 | u64 min_size; |
39279cc3 CM |
5294 | int ret; |
5295 | ||
1abe9b8a | 5296 | trace_btrfs_inode_evict(inode); |
5297 | ||
3d48d981 NB |
5298 | if (!root) { |
5299 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5300 | return; | |
5301 | } | |
5302 | ||
0b246afa | 5303 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
3d48d981 | 5304 | |
131e404a FDBM |
5305 | evict_inode_truncate_pages(inode); |
5306 | ||
69e9c6c6 SB |
5307 | if (inode->i_nlink && |
5308 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5309 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
70ddc553 | 5310 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
bd555975 AV |
5311 | goto no_delete; |
5312 | ||
39279cc3 | 5313 | if (is_bad_inode(inode)) { |
3d6ae7bb | 5314 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
39279cc3 CM |
5315 | goto no_delete; |
5316 | } | |
bd555975 | 5317 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5318 | if (!special_file(inode->i_mode)) |
5319 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5320 | |
7ab7956e | 5321 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
f612496b | 5322 | |
0b246afa | 5323 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
6bf02314 | 5324 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5325 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5326 | goto no_delete; |
5327 | } | |
5328 | ||
76dda93c | 5329 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5330 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5331 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5332 | goto no_delete; |
5333 | } | |
5334 | ||
aa79021f | 5335 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
0e8c36a9 | 5336 | if (ret) { |
3d6ae7bb | 5337 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
0e8c36a9 MX |
5338 | goto no_delete; |
5339 | } | |
5340 | ||
2ff7e61e | 5341 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
4289a667 | 5342 | if (!rsv) { |
3d6ae7bb | 5343 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
4289a667 JB |
5344 | goto no_delete; |
5345 | } | |
4a338542 | 5346 | rsv->size = min_size; |
ca7e70f5 | 5347 | rsv->failfast = 1; |
0b246afa | 5348 | global_rsv = &fs_info->global_block_rsv; |
4289a667 | 5349 | |
6ef06d27 | 5350 | btrfs_i_size_write(BTRFS_I(inode), 0); |
5f39d397 | 5351 | |
4289a667 | 5352 | /* |
8407aa46 MX |
5353 | * This is a bit simpler than btrfs_truncate since we've already |
5354 | * reserved our space for our orphan item in the unlink, so we just | |
5355 | * need to reserve some slack space in case we add bytes and update | |
5356 | * inode item when doing the truncate. | |
4289a667 | 5357 | */ |
8082510e | 5358 | while (1) { |
08e007d2 MX |
5359 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5360 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5361 | |
5362 | /* | |
5363 | * Try and steal from the global reserve since we will | |
5364 | * likely not use this space anyway, we want to try as | |
5365 | * hard as possible to get this to work. | |
5366 | */ | |
5367 | if (ret) | |
3bce876f JB |
5368 | steal_from_global++; |
5369 | else | |
5370 | steal_from_global = 0; | |
5371 | ret = 0; | |
d68fc57b | 5372 | |
3bce876f JB |
5373 | /* |
5374 | * steal_from_global == 0: we reserved stuff, hooray! | |
5375 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5376 | * steal_from_global == 2: we've committed, still not a lot of | |
5377 | * room but maybe we'll have room in the global reserve this | |
5378 | * time. | |
5379 | * steal_from_global == 3: abandon all hope! | |
5380 | */ | |
5381 | if (steal_from_global > 2) { | |
0b246afa JM |
5382 | btrfs_warn(fs_info, |
5383 | "Could not get space for a delete, will truncate on mount %d", | |
5384 | ret); | |
3d6ae7bb | 5385 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5386 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5387 | goto no_delete; |
d68fc57b | 5388 | } |
7b128766 | 5389 | |
0e8c36a9 | 5390 | trans = btrfs_join_transaction(root); |
4289a667 | 5391 | if (IS_ERR(trans)) { |
3d6ae7bb | 5392 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5393 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5394 | goto no_delete; |
d68fc57b | 5395 | } |
7b128766 | 5396 | |
3bce876f | 5397 | /* |
01327610 | 5398 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5399 | * sure there is room to do it, if not we need to commit and try |
5400 | * again. | |
5401 | */ | |
5402 | if (steal_from_global) { | |
2ff7e61e | 5403 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
3bce876f | 5404 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
25d609f8 | 5405 | min_size, 0); |
3bce876f JB |
5406 | else |
5407 | ret = -ENOSPC; | |
5408 | } | |
5409 | ||
5410 | /* | |
5411 | * Couldn't steal from the global reserve, we have too much | |
5412 | * pending stuff built up, commit the transaction and try it | |
5413 | * again. | |
5414 | */ | |
5415 | if (ret) { | |
3a45bb20 | 5416 | ret = btrfs_commit_transaction(trans); |
3bce876f | 5417 | if (ret) { |
3d6ae7bb | 5418 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
2ff7e61e | 5419 | btrfs_free_block_rsv(fs_info, rsv); |
3bce876f JB |
5420 | goto no_delete; |
5421 | } | |
5422 | continue; | |
5423 | } else { | |
5424 | steal_from_global = 0; | |
5425 | } | |
5426 | ||
4289a667 JB |
5427 | trans->block_rsv = rsv; |
5428 | ||
d68fc57b | 5429 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5430 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5431 | break; |
85e21bac | 5432 | |
0b246afa | 5433 | trans->block_rsv = &fs_info->trans_block_rsv; |
3a45bb20 | 5434 | btrfs_end_transaction(trans); |
8082510e | 5435 | trans = NULL; |
2ff7e61e | 5436 | btrfs_btree_balance_dirty(fs_info); |
8082510e | 5437 | } |
5f39d397 | 5438 | |
2ff7e61e | 5439 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5440 | |
4ef31a45 JB |
5441 | /* |
5442 | * Errors here aren't a big deal, it just means we leave orphan items | |
5443 | * in the tree. They will be cleaned up on the next mount. | |
5444 | */ | |
8082510e | 5445 | if (ret == 0) { |
4289a667 | 5446 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 5447 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
4ef31a45 | 5448 | } else { |
3d6ae7bb | 5449 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
8082510e | 5450 | } |
54aa1f4d | 5451 | |
0b246afa JM |
5452 | trans->block_rsv = &fs_info->trans_block_rsv; |
5453 | if (!(root == fs_info->tree_root || | |
581bb050 | 5454 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5455 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5456 | |
3a45bb20 | 5457 | btrfs_end_transaction(trans); |
2ff7e61e | 5458 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 5459 | no_delete: |
f48d1cf5 | 5460 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5461 | clear_inode(inode); |
39279cc3 CM |
5462 | } |
5463 | ||
5464 | /* | |
5465 | * this returns the key found in the dir entry in the location pointer. | |
5466 | * If no dir entries were found, location->objectid is 0. | |
5467 | */ | |
5468 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5469 | struct btrfs_key *location) | |
5470 | { | |
5471 | const char *name = dentry->d_name.name; | |
5472 | int namelen = dentry->d_name.len; | |
5473 | struct btrfs_dir_item *di; | |
5474 | struct btrfs_path *path; | |
5475 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5476 | int ret = 0; |
39279cc3 CM |
5477 | |
5478 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5479 | if (!path) |
5480 | return -ENOMEM; | |
3954401f | 5481 | |
f85b7379 DS |
5482 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
5483 | name, namelen, 0); | |
0d9f7f3e Y |
5484 | if (IS_ERR(di)) |
5485 | ret = PTR_ERR(di); | |
d397712b | 5486 | |
c704005d | 5487 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5488 | goto out_err; |
d397712b | 5489 | |
5f39d397 | 5490 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5491 | out: |
39279cc3 CM |
5492 | btrfs_free_path(path); |
5493 | return ret; | |
3954401f CM |
5494 | out_err: |
5495 | location->objectid = 0; | |
5496 | goto out; | |
39279cc3 CM |
5497 | } |
5498 | ||
5499 | /* | |
5500 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5501 | * needs to be changed to reflect the root directory of the tree root. This | |
5502 | * is kind of like crossing a mount point. | |
5503 | */ | |
2ff7e61e | 5504 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5505 | struct inode *dir, |
5506 | struct dentry *dentry, | |
5507 | struct btrfs_key *location, | |
5508 | struct btrfs_root **sub_root) | |
39279cc3 | 5509 | { |
4df27c4d YZ |
5510 | struct btrfs_path *path; |
5511 | struct btrfs_root *new_root; | |
5512 | struct btrfs_root_ref *ref; | |
5513 | struct extent_buffer *leaf; | |
1d4c08e0 | 5514 | struct btrfs_key key; |
4df27c4d YZ |
5515 | int ret; |
5516 | int err = 0; | |
39279cc3 | 5517 | |
4df27c4d YZ |
5518 | path = btrfs_alloc_path(); |
5519 | if (!path) { | |
5520 | err = -ENOMEM; | |
5521 | goto out; | |
5522 | } | |
39279cc3 | 5523 | |
4df27c4d | 5524 | err = -ENOENT; |
1d4c08e0 DS |
5525 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5526 | key.type = BTRFS_ROOT_REF_KEY; | |
5527 | key.offset = location->objectid; | |
5528 | ||
0b246afa | 5529 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5530 | if (ret) { |
5531 | if (ret < 0) | |
5532 | err = ret; | |
5533 | goto out; | |
5534 | } | |
39279cc3 | 5535 | |
4df27c4d YZ |
5536 | leaf = path->nodes[0]; |
5537 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5538 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5539 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5540 | goto out; | |
39279cc3 | 5541 | |
4df27c4d YZ |
5542 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5543 | (unsigned long)(ref + 1), | |
5544 | dentry->d_name.len); | |
5545 | if (ret) | |
5546 | goto out; | |
5547 | ||
b3b4aa74 | 5548 | btrfs_release_path(path); |
4df27c4d | 5549 | |
0b246afa | 5550 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5551 | if (IS_ERR(new_root)) { |
5552 | err = PTR_ERR(new_root); | |
5553 | goto out; | |
5554 | } | |
5555 | ||
4df27c4d YZ |
5556 | *sub_root = new_root; |
5557 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5558 | location->type = BTRFS_INODE_ITEM_KEY; | |
5559 | location->offset = 0; | |
5560 | err = 0; | |
5561 | out: | |
5562 | btrfs_free_path(path); | |
5563 | return err; | |
39279cc3 CM |
5564 | } |
5565 | ||
5d4f98a2 YZ |
5566 | static void inode_tree_add(struct inode *inode) |
5567 | { | |
5568 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5569 | struct btrfs_inode *entry; | |
03e860bd FNP |
5570 | struct rb_node **p; |
5571 | struct rb_node *parent; | |
cef21937 | 5572 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5573 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5574 | |
1d3382cb | 5575 | if (inode_unhashed(inode)) |
76dda93c | 5576 | return; |
e1409cef | 5577 | parent = NULL; |
5d4f98a2 | 5578 | spin_lock(&root->inode_lock); |
e1409cef | 5579 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5580 | while (*p) { |
5581 | parent = *p; | |
5582 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5583 | ||
4a0cc7ca | 5584 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5585 | p = &parent->rb_left; |
4a0cc7ca | 5586 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5587 | p = &parent->rb_right; |
5d4f98a2 YZ |
5588 | else { |
5589 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5590 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5591 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5592 | RB_CLEAR_NODE(parent); |
5593 | spin_unlock(&root->inode_lock); | |
cef21937 | 5594 | return; |
5d4f98a2 YZ |
5595 | } |
5596 | } | |
cef21937 FDBM |
5597 | rb_link_node(new, parent, p); |
5598 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5599 | spin_unlock(&root->inode_lock); |
5600 | } | |
5601 | ||
5602 | static void inode_tree_del(struct inode *inode) | |
5603 | { | |
0b246afa | 5604 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5605 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5606 | int empty = 0; |
5d4f98a2 | 5607 | |
03e860bd | 5608 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5609 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5610 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5611 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5612 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5613 | } |
03e860bd | 5614 | spin_unlock(&root->inode_lock); |
76dda93c | 5615 | |
69e9c6c6 | 5616 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5617 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5618 | spin_lock(&root->inode_lock); |
5619 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5620 | spin_unlock(&root->inode_lock); | |
5621 | if (empty) | |
5622 | btrfs_add_dead_root(root); | |
5623 | } | |
5624 | } | |
5625 | ||
143bede5 | 5626 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c | 5627 | { |
0b246afa | 5628 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c YZ |
5629 | struct rb_node *node; |
5630 | struct rb_node *prev; | |
5631 | struct btrfs_inode *entry; | |
5632 | struct inode *inode; | |
5633 | u64 objectid = 0; | |
5634 | ||
0b246afa | 5635 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
7813b3db | 5636 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
76dda93c YZ |
5637 | |
5638 | spin_lock(&root->inode_lock); | |
5639 | again: | |
5640 | node = root->inode_tree.rb_node; | |
5641 | prev = NULL; | |
5642 | while (node) { | |
5643 | prev = node; | |
5644 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5645 | ||
4a0cc7ca | 5646 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c | 5647 | node = node->rb_left; |
4a0cc7ca | 5648 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c YZ |
5649 | node = node->rb_right; |
5650 | else | |
5651 | break; | |
5652 | } | |
5653 | if (!node) { | |
5654 | while (prev) { | |
5655 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5656 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
76dda93c YZ |
5657 | node = prev; |
5658 | break; | |
5659 | } | |
5660 | prev = rb_next(prev); | |
5661 | } | |
5662 | } | |
5663 | while (node) { | |
5664 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5665 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
76dda93c YZ |
5666 | inode = igrab(&entry->vfs_inode); |
5667 | if (inode) { | |
5668 | spin_unlock(&root->inode_lock); | |
5669 | if (atomic_read(&inode->i_count) > 1) | |
5670 | d_prune_aliases(inode); | |
5671 | /* | |
45321ac5 | 5672 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5673 | * the inode cache when its usage count |
5674 | * hits zero. | |
5675 | */ | |
5676 | iput(inode); | |
5677 | cond_resched(); | |
5678 | spin_lock(&root->inode_lock); | |
5679 | goto again; | |
5680 | } | |
5681 | ||
5682 | if (cond_resched_lock(&root->inode_lock)) | |
5683 | goto again; | |
5684 | ||
5685 | node = rb_next(node); | |
5686 | } | |
5687 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5688 | } |
5689 | ||
e02119d5 CM |
5690 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5691 | { | |
5692 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5693 | inode->i_ino = args->location->objectid; |
5694 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5695 | sizeof(*args->location)); | |
e02119d5 | 5696 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5697 | return 0; |
5698 | } | |
5699 | ||
5700 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5701 | { | |
5702 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5703 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5704 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5705 | } |
5706 | ||
5d4f98a2 | 5707 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5708 | struct btrfs_key *location, |
5d4f98a2 | 5709 | struct btrfs_root *root) |
39279cc3 CM |
5710 | { |
5711 | struct inode *inode; | |
5712 | struct btrfs_iget_args args; | |
90d3e592 | 5713 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5714 | |
90d3e592 | 5715 | args.location = location; |
39279cc3 CM |
5716 | args.root = root; |
5717 | ||
778ba82b | 5718 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5719 | btrfs_init_locked_inode, |
5720 | (void *)&args); | |
5721 | return inode; | |
5722 | } | |
5723 | ||
1a54ef8c BR |
5724 | /* Get an inode object given its location and corresponding root. |
5725 | * Returns in *is_new if the inode was read from disk | |
5726 | */ | |
5727 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5728 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5729 | { |
5730 | struct inode *inode; | |
5731 | ||
90d3e592 | 5732 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5733 | if (!inode) |
5d4f98a2 | 5734 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5735 | |
5736 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5737 | int ret; |
5738 | ||
5739 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5740 | if (!is_bad_inode(inode)) { |
5741 | inode_tree_add(inode); | |
5742 | unlock_new_inode(inode); | |
5743 | if (new) | |
5744 | *new = 1; | |
5745 | } else { | |
e0b6d65b ST |
5746 | unlock_new_inode(inode); |
5747 | iput(inode); | |
67710892 FM |
5748 | ASSERT(ret < 0); |
5749 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5750 | } |
5751 | } | |
5752 | ||
1a54ef8c BR |
5753 | return inode; |
5754 | } | |
5755 | ||
4df27c4d YZ |
5756 | static struct inode *new_simple_dir(struct super_block *s, |
5757 | struct btrfs_key *key, | |
5758 | struct btrfs_root *root) | |
5759 | { | |
5760 | struct inode *inode = new_inode(s); | |
5761 | ||
5762 | if (!inode) | |
5763 | return ERR_PTR(-ENOMEM); | |
5764 | ||
4df27c4d YZ |
5765 | BTRFS_I(inode)->root = root; |
5766 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5767 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5768 | |
5769 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5770 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5771 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5772 | inode->i_fop = &simple_dir_operations; |
5773 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5774 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5775 | inode->i_atime = inode->i_mtime; |
5776 | inode->i_ctime = inode->i_mtime; | |
5777 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5778 | |
5779 | return inode; | |
5780 | } | |
5781 | ||
3de4586c | 5782 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5783 | { |
0b246afa | 5784 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5785 | struct inode *inode; |
4df27c4d | 5786 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5787 | struct btrfs_root *sub_root = root; |
5788 | struct btrfs_key location; | |
76dda93c | 5789 | int index; |
b4aff1f8 | 5790 | int ret = 0; |
39279cc3 CM |
5791 | |
5792 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5793 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5794 | |
39e3c955 | 5795 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5796 | if (ret < 0) |
5797 | return ERR_PTR(ret); | |
5f39d397 | 5798 | |
4df27c4d | 5799 | if (location.objectid == 0) |
5662344b | 5800 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5801 | |
5802 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5803 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5804 | return inode; |
5805 | } | |
5806 | ||
5807 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5808 | ||
0b246afa | 5809 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5810 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5811 | &location, &sub_root); |
5812 | if (ret < 0) { | |
5813 | if (ret != -ENOENT) | |
5814 | inode = ERR_PTR(ret); | |
5815 | else | |
5816 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5817 | } else { | |
73f73415 | 5818 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5819 | } |
0b246afa | 5820 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5821 | |
34d19bad | 5822 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5823 | down_read(&fs_info->cleanup_work_sem); |
bc98a42c | 5824 | if (!sb_rdonly(inode->i_sb)) |
66b4ffd1 | 5825 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5826 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5827 | if (ret) { |
5828 | iput(inode); | |
66b4ffd1 | 5829 | inode = ERR_PTR(ret); |
01cd3367 | 5830 | } |
c71bf099 YZ |
5831 | } |
5832 | ||
3de4586c CM |
5833 | return inode; |
5834 | } | |
5835 | ||
fe15ce44 | 5836 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5837 | { |
5838 | struct btrfs_root *root; | |
2b0143b5 | 5839 | struct inode *inode = d_inode(dentry); |
76dda93c | 5840 | |
848cce0d | 5841 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5842 | inode = d_inode(dentry->d_parent); |
76dda93c | 5843 | |
848cce0d LZ |
5844 | if (inode) { |
5845 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5846 | if (btrfs_root_refs(&root->root_item) == 0) |
5847 | return 1; | |
848cce0d | 5848 | |
4a0cc7ca | 5849 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5850 | return 1; |
efefb143 | 5851 | } |
76dda93c YZ |
5852 | return 0; |
5853 | } | |
5854 | ||
b4aff1f8 JB |
5855 | static void btrfs_dentry_release(struct dentry *dentry) |
5856 | { | |
944a4515 | 5857 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5858 | } |
5859 | ||
3de4586c | 5860 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5861 | unsigned int flags) |
3de4586c | 5862 | { |
5662344b | 5863 | struct inode *inode; |
a66e7cc6 | 5864 | |
5662344b TI |
5865 | inode = btrfs_lookup_dentry(dir, dentry); |
5866 | if (IS_ERR(inode)) { | |
5867 | if (PTR_ERR(inode) == -ENOENT) | |
5868 | inode = NULL; | |
5869 | else | |
5870 | return ERR_CAST(inode); | |
5871 | } | |
5872 | ||
41d28bca | 5873 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5874 | } |
5875 | ||
16cdcec7 | 5876 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5877 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5878 | }; | |
5879 | ||
23b5ec74 JB |
5880 | /* |
5881 | * All this infrastructure exists because dir_emit can fault, and we are holding | |
5882 | * the tree lock when doing readdir. For now just allocate a buffer and copy | |
5883 | * our information into that, and then dir_emit from the buffer. This is | |
5884 | * similar to what NFS does, only we don't keep the buffer around in pagecache | |
5885 | * because I'm afraid I'll mess that up. Long term we need to make filldir do | |
5886 | * copy_to_user_inatomic so we don't have to worry about page faulting under the | |
5887 | * tree lock. | |
5888 | */ | |
5889 | static int btrfs_opendir(struct inode *inode, struct file *file) | |
5890 | { | |
5891 | struct btrfs_file_private *private; | |
5892 | ||
5893 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); | |
5894 | if (!private) | |
5895 | return -ENOMEM; | |
5896 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
5897 | if (!private->filldir_buf) { | |
5898 | kfree(private); | |
5899 | return -ENOMEM; | |
5900 | } | |
5901 | file->private_data = private; | |
5902 | return 0; | |
5903 | } | |
5904 | ||
5905 | struct dir_entry { | |
5906 | u64 ino; | |
5907 | u64 offset; | |
5908 | unsigned type; | |
5909 | int name_len; | |
5910 | }; | |
5911 | ||
5912 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) | |
5913 | { | |
5914 | while (entries--) { | |
5915 | struct dir_entry *entry = addr; | |
5916 | char *name = (char *)(entry + 1); | |
5917 | ||
5918 | ctx->pos = entry->offset; | |
5919 | if (!dir_emit(ctx, name, entry->name_len, entry->ino, | |
5920 | entry->type)) | |
5921 | return 1; | |
5922 | addr += sizeof(struct dir_entry) + entry->name_len; | |
5923 | ctx->pos++; | |
5924 | } | |
5925 | return 0; | |
5926 | } | |
5927 | ||
9cdda8d3 | 5928 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5929 | { |
9cdda8d3 | 5930 | struct inode *inode = file_inode(file); |
2ff7e61e | 5931 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 5932 | struct btrfs_root *root = BTRFS_I(inode)->root; |
23b5ec74 | 5933 | struct btrfs_file_private *private = file->private_data; |
39279cc3 CM |
5934 | struct btrfs_dir_item *di; |
5935 | struct btrfs_key key; | |
5f39d397 | 5936 | struct btrfs_key found_key; |
39279cc3 | 5937 | struct btrfs_path *path; |
23b5ec74 | 5938 | void *addr; |
16cdcec7 MX |
5939 | struct list_head ins_list; |
5940 | struct list_head del_list; | |
39279cc3 | 5941 | int ret; |
5f39d397 | 5942 | struct extent_buffer *leaf; |
39279cc3 | 5943 | int slot; |
5f39d397 CM |
5944 | char *name_ptr; |
5945 | int name_len; | |
23b5ec74 JB |
5946 | int entries = 0; |
5947 | int total_len = 0; | |
02dbfc99 | 5948 | bool put = false; |
c2951f32 | 5949 | struct btrfs_key location; |
5f39d397 | 5950 | |
9cdda8d3 AV |
5951 | if (!dir_emit_dots(file, ctx)) |
5952 | return 0; | |
5953 | ||
49593bfa | 5954 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5955 | if (!path) |
5956 | return -ENOMEM; | |
ff5714cc | 5957 | |
23b5ec74 | 5958 | addr = private->filldir_buf; |
e4058b54 | 5959 | path->reada = READA_FORWARD; |
49593bfa | 5960 | |
c2951f32 JM |
5961 | INIT_LIST_HEAD(&ins_list); |
5962 | INIT_LIST_HEAD(&del_list); | |
5963 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5964 | |
23b5ec74 | 5965 | again: |
c2951f32 | 5966 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5967 | key.offset = ctx->pos; |
4a0cc7ca | 5968 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5969 | |
39279cc3 CM |
5970 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5971 | if (ret < 0) | |
5972 | goto err; | |
49593bfa DW |
5973 | |
5974 | while (1) { | |
23b5ec74 JB |
5975 | struct dir_entry *entry; |
5976 | ||
5f39d397 | 5977 | leaf = path->nodes[0]; |
39279cc3 | 5978 | slot = path->slots[0]; |
b9e03af0 LZ |
5979 | if (slot >= btrfs_header_nritems(leaf)) { |
5980 | ret = btrfs_next_leaf(root, path); | |
5981 | if (ret < 0) | |
5982 | goto err; | |
5983 | else if (ret > 0) | |
5984 | break; | |
5985 | continue; | |
39279cc3 | 5986 | } |
3de4586c | 5987 | |
5f39d397 CM |
5988 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5989 | ||
5990 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5991 | break; |
c2951f32 | 5992 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5993 | break; |
9cdda8d3 | 5994 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5995 | goto next; |
c2951f32 | 5996 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5997 | goto next; |
39279cc3 | 5998 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e79a3327 | 5999 | if (verify_dir_item(fs_info, leaf, slot, di)) |
c2951f32 | 6000 | goto next; |
22a94d44 | 6001 | |
c2951f32 | 6002 | name_len = btrfs_dir_name_len(leaf, di); |
23b5ec74 JB |
6003 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
6004 | PAGE_SIZE) { | |
6005 | btrfs_release_path(path); | |
6006 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6007 | if (ret) | |
6008 | goto nopos; | |
6009 | addr = private->filldir_buf; | |
6010 | entries = 0; | |
6011 | total_len = 0; | |
6012 | goto again; | |
c2951f32 | 6013 | } |
23b5ec74 JB |
6014 | |
6015 | entry = addr; | |
6016 | entry->name_len = name_len; | |
6017 | name_ptr = (char *)(entry + 1); | |
c2951f32 JM |
6018 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
6019 | name_len); | |
23b5ec74 | 6020 | entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
c2951f32 | 6021 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
23b5ec74 JB |
6022 | entry->ino = location.objectid; |
6023 | entry->offset = found_key.offset; | |
6024 | entries++; | |
6025 | addr += sizeof(struct dir_entry) + name_len; | |
6026 | total_len += sizeof(struct dir_entry) + name_len; | |
b9e03af0 LZ |
6027 | next: |
6028 | path->slots[0]++; | |
39279cc3 | 6029 | } |
23b5ec74 JB |
6030 | btrfs_release_path(path); |
6031 | ||
6032 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); | |
6033 | if (ret) | |
6034 | goto nopos; | |
49593bfa | 6035 | |
d2fbb2b5 | 6036 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 6037 | if (ret) |
bc4ef759 DS |
6038 | goto nopos; |
6039 | ||
db62efbb ZB |
6040 | /* |
6041 | * Stop new entries from being returned after we return the last | |
6042 | * entry. | |
6043 | * | |
6044 | * New directory entries are assigned a strictly increasing | |
6045 | * offset. This means that new entries created during readdir | |
6046 | * are *guaranteed* to be seen in the future by that readdir. | |
6047 | * This has broken buggy programs which operate on names as | |
6048 | * they're returned by readdir. Until we re-use freed offsets | |
6049 | * we have this hack to stop new entries from being returned | |
6050 | * under the assumption that they'll never reach this huge | |
6051 | * offset. | |
6052 | * | |
6053 | * This is being careful not to overflow 32bit loff_t unless the | |
6054 | * last entry requires it because doing so has broken 32bit apps | |
6055 | * in the past. | |
6056 | */ | |
c2951f32 JM |
6057 | if (ctx->pos >= INT_MAX) |
6058 | ctx->pos = LLONG_MAX; | |
6059 | else | |
6060 | ctx->pos = INT_MAX; | |
39279cc3 CM |
6061 | nopos: |
6062 | ret = 0; | |
6063 | err: | |
02dbfc99 OS |
6064 | if (put) |
6065 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 6066 | btrfs_free_path(path); |
39279cc3 CM |
6067 | return ret; |
6068 | } | |
6069 | ||
a9185b41 | 6070 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
6071 | { |
6072 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6073 | struct btrfs_trans_handle *trans; | |
6074 | int ret = 0; | |
0af3d00b | 6075 | bool nolock = false; |
39279cc3 | 6076 | |
72ac3c0d | 6077 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
6078 | return 0; |
6079 | ||
70ddc553 NB |
6080 | if (btrfs_fs_closing(root->fs_info) && |
6081 | btrfs_is_free_space_inode(BTRFS_I(inode))) | |
82d5902d | 6082 | nolock = true; |
0af3d00b | 6083 | |
a9185b41 | 6084 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 6085 | if (nolock) |
7a7eaa40 | 6086 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 6087 | else |
7a7eaa40 | 6088 | trans = btrfs_join_transaction(root); |
3612b495 TI |
6089 | if (IS_ERR(trans)) |
6090 | return PTR_ERR(trans); | |
3a45bb20 | 6091 | ret = btrfs_commit_transaction(trans); |
39279cc3 CM |
6092 | } |
6093 | return ret; | |
6094 | } | |
6095 | ||
6096 | /* | |
54aa1f4d | 6097 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6098 | * inode changes. But, it is most likely to find the inode in cache. |
6099 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6100 | * to keep or drop this code. | |
6101 | */ | |
48a3b636 | 6102 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 6103 | { |
2ff7e61e | 6104 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
6105 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6106 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6107 | int ret; |
6108 | ||
72ac3c0d | 6109 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6110 | return 0; |
39279cc3 | 6111 | |
7a7eaa40 | 6112 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6113 | if (IS_ERR(trans)) |
6114 | return PTR_ERR(trans); | |
8929ecfa YZ |
6115 | |
6116 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6117 | if (ret && ret == -ENOSPC) { |
6118 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6119 | btrfs_end_transaction(trans); |
94b60442 | 6120 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6121 | if (IS_ERR(trans)) |
6122 | return PTR_ERR(trans); | |
8929ecfa | 6123 | |
94b60442 | 6124 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6125 | } |
3a45bb20 | 6126 | btrfs_end_transaction(trans); |
16cdcec7 | 6127 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6128 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6129 | |
6130 | return ret; | |
6131 | } | |
6132 | ||
6133 | /* | |
6134 | * This is a copy of file_update_time. We need this so we can return error on | |
6135 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6136 | */ | |
e41f941a JB |
6137 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6138 | int flags) | |
22c44fe6 | 6139 | { |
2bc55652 AB |
6140 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6141 | ||
6142 | if (btrfs_root_readonly(root)) | |
6143 | return -EROFS; | |
6144 | ||
e41f941a | 6145 | if (flags & S_VERSION) |
22c44fe6 | 6146 | inode_inc_iversion(inode); |
e41f941a JB |
6147 | if (flags & S_CTIME) |
6148 | inode->i_ctime = *now; | |
6149 | if (flags & S_MTIME) | |
6150 | inode->i_mtime = *now; | |
6151 | if (flags & S_ATIME) | |
6152 | inode->i_atime = *now; | |
6153 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6154 | } |
6155 | ||
d352ac68 CM |
6156 | /* |
6157 | * find the highest existing sequence number in a directory | |
6158 | * and then set the in-memory index_cnt variable to reflect | |
6159 | * free sequence numbers | |
6160 | */ | |
4c570655 | 6161 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
aec7477b | 6162 | { |
4c570655 | 6163 | struct btrfs_root *root = inode->root; |
aec7477b JB |
6164 | struct btrfs_key key, found_key; |
6165 | struct btrfs_path *path; | |
6166 | struct extent_buffer *leaf; | |
6167 | int ret; | |
6168 | ||
4c570655 | 6169 | key.objectid = btrfs_ino(inode); |
962a298f | 6170 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6171 | key.offset = (u64)-1; |
6172 | ||
6173 | path = btrfs_alloc_path(); | |
6174 | if (!path) | |
6175 | return -ENOMEM; | |
6176 | ||
6177 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6178 | if (ret < 0) | |
6179 | goto out; | |
6180 | /* FIXME: we should be able to handle this */ | |
6181 | if (ret == 0) | |
6182 | goto out; | |
6183 | ret = 0; | |
6184 | ||
6185 | /* | |
6186 | * MAGIC NUMBER EXPLANATION: | |
6187 | * since we search a directory based on f_pos we have to start at 2 | |
6188 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6189 | * else has to start at 2 | |
6190 | */ | |
6191 | if (path->slots[0] == 0) { | |
4c570655 | 6192 | inode->index_cnt = 2; |
aec7477b JB |
6193 | goto out; |
6194 | } | |
6195 | ||
6196 | path->slots[0]--; | |
6197 | ||
6198 | leaf = path->nodes[0]; | |
6199 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6200 | ||
4c570655 | 6201 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6202 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
4c570655 | 6203 | inode->index_cnt = 2; |
aec7477b JB |
6204 | goto out; |
6205 | } | |
6206 | ||
4c570655 | 6207 | inode->index_cnt = found_key.offset + 1; |
aec7477b JB |
6208 | out: |
6209 | btrfs_free_path(path); | |
6210 | return ret; | |
6211 | } | |
6212 | ||
d352ac68 CM |
6213 | /* |
6214 | * helper to find a free sequence number in a given directory. This current | |
6215 | * code is very simple, later versions will do smarter things in the btree | |
6216 | */ | |
877574e2 | 6217 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
aec7477b JB |
6218 | { |
6219 | int ret = 0; | |
6220 | ||
877574e2 NB |
6221 | if (dir->index_cnt == (u64)-1) { |
6222 | ret = btrfs_inode_delayed_dir_index_count(dir); | |
16cdcec7 MX |
6223 | if (ret) { |
6224 | ret = btrfs_set_inode_index_count(dir); | |
6225 | if (ret) | |
6226 | return ret; | |
6227 | } | |
aec7477b JB |
6228 | } |
6229 | ||
877574e2 NB |
6230 | *index = dir->index_cnt; |
6231 | dir->index_cnt++; | |
aec7477b JB |
6232 | |
6233 | return ret; | |
6234 | } | |
6235 | ||
b0d5d10f CM |
6236 | static int btrfs_insert_inode_locked(struct inode *inode) |
6237 | { | |
6238 | struct btrfs_iget_args args; | |
6239 | args.location = &BTRFS_I(inode)->location; | |
6240 | args.root = BTRFS_I(inode)->root; | |
6241 | ||
6242 | return insert_inode_locked4(inode, | |
6243 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6244 | btrfs_find_actor, &args); | |
6245 | } | |
6246 | ||
19aee8de AJ |
6247 | /* |
6248 | * Inherit flags from the parent inode. | |
6249 | * | |
6250 | * Currently only the compression flags and the cow flags are inherited. | |
6251 | */ | |
6252 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
6253 | { | |
6254 | unsigned int flags; | |
6255 | ||
6256 | if (!dir) | |
6257 | return; | |
6258 | ||
6259 | flags = BTRFS_I(dir)->flags; | |
6260 | ||
6261 | if (flags & BTRFS_INODE_NOCOMPRESS) { | |
6262 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
6263 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
6264 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
6265 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
6266 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
6267 | } | |
6268 | ||
6269 | if (flags & BTRFS_INODE_NODATACOW) { | |
6270 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6271 | if (S_ISREG(inode->i_mode)) | |
6272 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
6273 | } | |
6274 | ||
6275 | btrfs_update_iflags(inode); | |
6276 | } | |
6277 | ||
39279cc3 CM |
6278 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6279 | struct btrfs_root *root, | |
aec7477b | 6280 | struct inode *dir, |
9c58309d | 6281 | const char *name, int name_len, |
175a4eb7 AV |
6282 | u64 ref_objectid, u64 objectid, |
6283 | umode_t mode, u64 *index) | |
39279cc3 | 6284 | { |
0b246afa | 6285 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6286 | struct inode *inode; |
5f39d397 | 6287 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6288 | struct btrfs_key *location; |
5f39d397 | 6289 | struct btrfs_path *path; |
9c58309d CM |
6290 | struct btrfs_inode_ref *ref; |
6291 | struct btrfs_key key[2]; | |
6292 | u32 sizes[2]; | |
ef3b9af5 | 6293 | int nitems = name ? 2 : 1; |
9c58309d | 6294 | unsigned long ptr; |
39279cc3 | 6295 | int ret; |
39279cc3 | 6296 | |
5f39d397 | 6297 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6298 | if (!path) |
6299 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6300 | |
0b246afa | 6301 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6302 | if (!inode) { |
6303 | btrfs_free_path(path); | |
39279cc3 | 6304 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6305 | } |
39279cc3 | 6306 | |
5762b5c9 FM |
6307 | /* |
6308 | * O_TMPFILE, set link count to 0, so that after this point, | |
6309 | * we fill in an inode item with the correct link count. | |
6310 | */ | |
6311 | if (!name) | |
6312 | set_nlink(inode, 0); | |
6313 | ||
581bb050 LZ |
6314 | /* |
6315 | * we have to initialize this early, so we can reclaim the inode | |
6316 | * number if we fail afterwards in this function. | |
6317 | */ | |
6318 | inode->i_ino = objectid; | |
6319 | ||
ef3b9af5 | 6320 | if (dir && name) { |
1abe9b8a | 6321 | trace_btrfs_inode_request(dir); |
6322 | ||
877574e2 | 6323 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
09771430 | 6324 | if (ret) { |
8fb27640 | 6325 | btrfs_free_path(path); |
09771430 | 6326 | iput(inode); |
aec7477b | 6327 | return ERR_PTR(ret); |
09771430 | 6328 | } |
ef3b9af5 FM |
6329 | } else if (dir) { |
6330 | *index = 0; | |
aec7477b JB |
6331 | } |
6332 | /* | |
6333 | * index_cnt is ignored for everything but a dir, | |
6334 | * btrfs_get_inode_index_count has an explanation for the magic | |
6335 | * number | |
6336 | */ | |
6337 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6338 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6339 | BTRFS_I(inode)->root = root; |
e02119d5 | 6340 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6341 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6342 | |
5dc562c5 JB |
6343 | /* |
6344 | * We could have gotten an inode number from somebody who was fsynced | |
6345 | * and then removed in this same transaction, so let's just set full | |
6346 | * sync since it will be a full sync anyway and this will blow away the | |
6347 | * old info in the log. | |
6348 | */ | |
6349 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6350 | ||
9c58309d | 6351 | key[0].objectid = objectid; |
962a298f | 6352 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6353 | key[0].offset = 0; |
6354 | ||
9c58309d | 6355 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6356 | |
6357 | if (name) { | |
6358 | /* | |
6359 | * Start new inodes with an inode_ref. This is slightly more | |
6360 | * efficient for small numbers of hard links since they will | |
6361 | * be packed into one item. Extended refs will kick in if we | |
6362 | * add more hard links than can fit in the ref item. | |
6363 | */ | |
6364 | key[1].objectid = objectid; | |
962a298f | 6365 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6366 | key[1].offset = ref_objectid; |
6367 | ||
6368 | sizes[1] = name_len + sizeof(*ref); | |
6369 | } | |
9c58309d | 6370 | |
b0d5d10f CM |
6371 | location = &BTRFS_I(inode)->location; |
6372 | location->objectid = objectid; | |
6373 | location->offset = 0; | |
962a298f | 6374 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6375 | |
6376 | ret = btrfs_insert_inode_locked(inode); | |
6377 | if (ret < 0) | |
6378 | goto fail; | |
6379 | ||
b9473439 | 6380 | path->leave_spinning = 1; |
ef3b9af5 | 6381 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6382 | if (ret != 0) |
b0d5d10f | 6383 | goto fail_unlock; |
5f39d397 | 6384 | |
ecc11fab | 6385 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6386 | inode_set_bytes(inode, 0); |
9cc97d64 | 6387 | |
c2050a45 | 6388 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6389 | inode->i_atime = inode->i_mtime; |
6390 | inode->i_ctime = inode->i_mtime; | |
6391 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6392 | ||
5f39d397 CM |
6393 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6394 | struct btrfs_inode_item); | |
b159fa28 | 6395 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6396 | sizeof(*inode_item)); |
e02119d5 | 6397 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6398 | |
ef3b9af5 FM |
6399 | if (name) { |
6400 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6401 | struct btrfs_inode_ref); | |
6402 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6403 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6404 | ptr = (unsigned long)(ref + 1); | |
6405 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6406 | } | |
9c58309d | 6407 | |
5f39d397 CM |
6408 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6409 | btrfs_free_path(path); | |
6410 | ||
6cbff00f CH |
6411 | btrfs_inherit_iflags(inode, dir); |
6412 | ||
569254b0 | 6413 | if (S_ISREG(mode)) { |
0b246afa | 6414 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6415 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6416 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6417 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6418 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6419 | } |
6420 | ||
5d4f98a2 | 6421 | inode_tree_add(inode); |
1abe9b8a | 6422 | |
6423 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6424 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6425 | |
8ea05e3a AB |
6426 | btrfs_update_root_times(trans, root); |
6427 | ||
63541927 FDBM |
6428 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6429 | if (ret) | |
0b246afa | 6430 | btrfs_err(fs_info, |
63541927 | 6431 | "error inheriting props for ino %llu (root %llu): %d", |
f85b7379 | 6432 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6433 | |
39279cc3 | 6434 | return inode; |
b0d5d10f CM |
6435 | |
6436 | fail_unlock: | |
6437 | unlock_new_inode(inode); | |
5f39d397 | 6438 | fail: |
ef3b9af5 | 6439 | if (dir && name) |
aec7477b | 6440 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6441 | btrfs_free_path(path); |
09771430 | 6442 | iput(inode); |
5f39d397 | 6443 | return ERR_PTR(ret); |
39279cc3 CM |
6444 | } |
6445 | ||
6446 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6447 | { | |
6448 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6449 | } | |
6450 | ||
d352ac68 CM |
6451 | /* |
6452 | * utility function to add 'inode' into 'parent_inode' with | |
6453 | * a give name and a given sequence number. | |
6454 | * if 'add_backref' is true, also insert a backref from the | |
6455 | * inode to the parent directory. | |
6456 | */ | |
e02119d5 | 6457 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
db0a669f | 6458 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
e02119d5 | 6459 | const char *name, int name_len, int add_backref, u64 index) |
39279cc3 | 6460 | { |
db0a669f | 6461 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
4df27c4d | 6462 | int ret = 0; |
39279cc3 | 6463 | struct btrfs_key key; |
db0a669f NB |
6464 | struct btrfs_root *root = parent_inode->root; |
6465 | u64 ino = btrfs_ino(inode); | |
6466 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6467 | |
33345d01 | 6468 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
db0a669f | 6469 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
4df27c4d | 6470 | } else { |
33345d01 | 6471 | key.objectid = ino; |
962a298f | 6472 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6473 | key.offset = 0; |
6474 | } | |
6475 | ||
33345d01 | 6476 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
0b246afa JM |
6477 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
6478 | root->root_key.objectid, parent_ino, | |
6479 | index, name, name_len); | |
4df27c4d | 6480 | } else if (add_backref) { |
33345d01 LZ |
6481 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6482 | parent_ino, index); | |
4df27c4d | 6483 | } |
39279cc3 | 6484 | |
79787eaa JM |
6485 | /* Nothing to clean up yet */ |
6486 | if (ret) | |
6487 | return ret; | |
4df27c4d | 6488 | |
79787eaa JM |
6489 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6490 | parent_inode, &key, | |
db0a669f | 6491 | btrfs_inode_type(&inode->vfs_inode), index); |
9c52057c | 6492 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6493 | goto fail_dir_item; |
6494 | else if (ret) { | |
66642832 | 6495 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6496 | return ret; |
39279cc3 | 6497 | } |
79787eaa | 6498 | |
db0a669f | 6499 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
79787eaa | 6500 | name_len * 2); |
db0a669f NB |
6501 | inode_inc_iversion(&parent_inode->vfs_inode); |
6502 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = | |
6503 | current_time(&parent_inode->vfs_inode); | |
6504 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); | |
79787eaa | 6505 | if (ret) |
66642832 | 6506 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6507 | return ret; |
fe66a05a CM |
6508 | |
6509 | fail_dir_item: | |
6510 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6511 | u64 local_index; | |
6512 | int err; | |
0b246afa JM |
6513 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
6514 | root->root_key.objectid, parent_ino, | |
6515 | &local_index, name, name_len); | |
fe66a05a CM |
6516 | |
6517 | } else if (add_backref) { | |
6518 | u64 local_index; | |
6519 | int err; | |
6520 | ||
6521 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6522 | ino, parent_ino, &local_index); | |
6523 | } | |
6524 | return ret; | |
39279cc3 CM |
6525 | } |
6526 | ||
6527 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
cef415af NB |
6528 | struct btrfs_inode *dir, struct dentry *dentry, |
6529 | struct btrfs_inode *inode, int backref, u64 index) | |
39279cc3 | 6530 | { |
a1b075d2 JB |
6531 | int err = btrfs_add_link(trans, dir, inode, |
6532 | dentry->d_name.name, dentry->d_name.len, | |
6533 | backref, index); | |
39279cc3 CM |
6534 | if (err > 0) |
6535 | err = -EEXIST; | |
6536 | return err; | |
6537 | } | |
6538 | ||
618e21d5 | 6539 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6540 | umode_t mode, dev_t rdev) |
618e21d5 | 6541 | { |
2ff7e61e | 6542 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6543 | struct btrfs_trans_handle *trans; |
6544 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6545 | struct inode *inode = NULL; |
618e21d5 JB |
6546 | int err; |
6547 | int drop_inode = 0; | |
6548 | u64 objectid; | |
00e4e6b3 | 6549 | u64 index = 0; |
618e21d5 | 6550 | |
9ed74f2d JB |
6551 | /* |
6552 | * 2 for inode item and ref | |
6553 | * 2 for dir items | |
6554 | * 1 for xattr if selinux is on | |
6555 | */ | |
a22285a6 YZ |
6556 | trans = btrfs_start_transaction(root, 5); |
6557 | if (IS_ERR(trans)) | |
6558 | return PTR_ERR(trans); | |
1832a6d5 | 6559 | |
581bb050 LZ |
6560 | err = btrfs_find_free_ino(root, &objectid); |
6561 | if (err) | |
6562 | goto out_unlock; | |
6563 | ||
aec7477b | 6564 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6565 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6566 | mode, &index); | |
7cf96da3 TI |
6567 | if (IS_ERR(inode)) { |
6568 | err = PTR_ERR(inode); | |
618e21d5 | 6569 | goto out_unlock; |
7cf96da3 | 6570 | } |
618e21d5 | 6571 | |
ad19db71 CS |
6572 | /* |
6573 | * If the active LSM wants to access the inode during | |
6574 | * d_instantiate it needs these. Smack checks to see | |
6575 | * if the filesystem supports xattrs by looking at the | |
6576 | * ops vector. | |
6577 | */ | |
ad19db71 | 6578 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6579 | init_special_inode(inode, inode->i_mode, rdev); |
6580 | ||
6581 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6582 | if (err) |
b0d5d10f CM |
6583 | goto out_unlock_inode; |
6584 | ||
cef415af NB |
6585 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6586 | 0, index); | |
b0d5d10f CM |
6587 | if (err) { |
6588 | goto out_unlock_inode; | |
6589 | } else { | |
1b4ab1bb | 6590 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6591 | unlock_new_inode(inode); |
08c422c2 | 6592 | d_instantiate(dentry, inode); |
618e21d5 | 6593 | } |
b0d5d10f | 6594 | |
618e21d5 | 6595 | out_unlock: |
3a45bb20 | 6596 | btrfs_end_transaction(trans); |
2ff7e61e JM |
6597 | btrfs_balance_delayed_items(fs_info); |
6598 | btrfs_btree_balance_dirty(fs_info); | |
618e21d5 JB |
6599 | if (drop_inode) { |
6600 | inode_dec_link_count(inode); | |
6601 | iput(inode); | |
6602 | } | |
618e21d5 | 6603 | return err; |
b0d5d10f CM |
6604 | |
6605 | out_unlock_inode: | |
6606 | drop_inode = 1; | |
6607 | unlock_new_inode(inode); | |
6608 | goto out_unlock; | |
6609 | ||
618e21d5 JB |
6610 | } |
6611 | ||
39279cc3 | 6612 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6613 | umode_t mode, bool excl) |
39279cc3 | 6614 | { |
2ff7e61e | 6615 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6616 | struct btrfs_trans_handle *trans; |
6617 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6618 | struct inode *inode = NULL; |
43baa579 | 6619 | int drop_inode_on_err = 0; |
a22285a6 | 6620 | int err; |
39279cc3 | 6621 | u64 objectid; |
00e4e6b3 | 6622 | u64 index = 0; |
39279cc3 | 6623 | |
9ed74f2d JB |
6624 | /* |
6625 | * 2 for inode item and ref | |
6626 | * 2 for dir items | |
6627 | * 1 for xattr if selinux is on | |
6628 | */ | |
a22285a6 YZ |
6629 | trans = btrfs_start_transaction(root, 5); |
6630 | if (IS_ERR(trans)) | |
6631 | return PTR_ERR(trans); | |
9ed74f2d | 6632 | |
581bb050 LZ |
6633 | err = btrfs_find_free_ino(root, &objectid); |
6634 | if (err) | |
6635 | goto out_unlock; | |
6636 | ||
aec7477b | 6637 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6638 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6639 | mode, &index); | |
7cf96da3 TI |
6640 | if (IS_ERR(inode)) { |
6641 | err = PTR_ERR(inode); | |
39279cc3 | 6642 | goto out_unlock; |
7cf96da3 | 6643 | } |
43baa579 | 6644 | drop_inode_on_err = 1; |
ad19db71 CS |
6645 | /* |
6646 | * If the active LSM wants to access the inode during | |
6647 | * d_instantiate it needs these. Smack checks to see | |
6648 | * if the filesystem supports xattrs by looking at the | |
6649 | * ops vector. | |
6650 | */ | |
6651 | inode->i_fop = &btrfs_file_operations; | |
6652 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6653 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6654 | |
6655 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6656 | if (err) | |
6657 | goto out_unlock_inode; | |
6658 | ||
6659 | err = btrfs_update_inode(trans, root, inode); | |
6660 | if (err) | |
6661 | goto out_unlock_inode; | |
ad19db71 | 6662 | |
cef415af NB |
6663 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6664 | 0, index); | |
39279cc3 | 6665 | if (err) |
b0d5d10f | 6666 | goto out_unlock_inode; |
43baa579 | 6667 | |
43baa579 | 6668 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6669 | unlock_new_inode(inode); |
43baa579 FB |
6670 | d_instantiate(dentry, inode); |
6671 | ||
39279cc3 | 6672 | out_unlock: |
3a45bb20 | 6673 | btrfs_end_transaction(trans); |
43baa579 | 6674 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6675 | inode_dec_link_count(inode); |
6676 | iput(inode); | |
6677 | } | |
2ff7e61e JM |
6678 | btrfs_balance_delayed_items(fs_info); |
6679 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6680 | return err; |
b0d5d10f CM |
6681 | |
6682 | out_unlock_inode: | |
6683 | unlock_new_inode(inode); | |
6684 | goto out_unlock; | |
6685 | ||
39279cc3 CM |
6686 | } |
6687 | ||
6688 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6689 | struct dentry *dentry) | |
6690 | { | |
271dba45 | 6691 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6692 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6693 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6694 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6695 | u64 index; |
39279cc3 CM |
6696 | int err; |
6697 | int drop_inode = 0; | |
6698 | ||
4a8be425 TH |
6699 | /* do not allow sys_link's with other subvols of the same device */ |
6700 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6701 | return -EXDEV; |
4a8be425 | 6702 | |
f186373f | 6703 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6704 | return -EMLINK; |
4a8be425 | 6705 | |
877574e2 | 6706 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
aec7477b JB |
6707 | if (err) |
6708 | goto fail; | |
6709 | ||
a22285a6 | 6710 | /* |
7e6b6465 | 6711 | * 2 items for inode and inode ref |
a22285a6 | 6712 | * 2 items for dir items |
7e6b6465 | 6713 | * 1 item for parent inode |
a22285a6 | 6714 | */ |
7e6b6465 | 6715 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6716 | if (IS_ERR(trans)) { |
6717 | err = PTR_ERR(trans); | |
271dba45 | 6718 | trans = NULL; |
a22285a6 YZ |
6719 | goto fail; |
6720 | } | |
5f39d397 | 6721 | |
67de1176 MX |
6722 | /* There are several dir indexes for this inode, clear the cache. */ |
6723 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6724 | inc_nlink(inode); |
0c4d2d95 | 6725 | inode_inc_iversion(inode); |
c2050a45 | 6726 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6727 | ihold(inode); |
e9976151 | 6728 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6729 | |
cef415af NB |
6730 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
6731 | 1, index); | |
5f39d397 | 6732 | |
a5719521 | 6733 | if (err) { |
54aa1f4d | 6734 | drop_inode = 1; |
a5719521 | 6735 | } else { |
10d9f309 | 6736 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6737 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6738 | if (err) |
6739 | goto fail; | |
ef3b9af5 FM |
6740 | if (inode->i_nlink == 1) { |
6741 | /* | |
6742 | * If new hard link count is 1, it's a file created | |
6743 | * with open(2) O_TMPFILE flag. | |
6744 | */ | |
3d6ae7bb | 6745 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
ef3b9af5 FM |
6746 | if (err) |
6747 | goto fail; | |
6748 | } | |
08c422c2 | 6749 | d_instantiate(dentry, inode); |
9ca5fbfb | 6750 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6751 | } |
39279cc3 | 6752 | |
2ff7e61e | 6753 | btrfs_balance_delayed_items(fs_info); |
1832a6d5 | 6754 | fail: |
271dba45 | 6755 | if (trans) |
3a45bb20 | 6756 | btrfs_end_transaction(trans); |
39279cc3 CM |
6757 | if (drop_inode) { |
6758 | inode_dec_link_count(inode); | |
6759 | iput(inode); | |
6760 | } | |
2ff7e61e | 6761 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6762 | return err; |
6763 | } | |
6764 | ||
18bb1db3 | 6765 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6766 | { |
2ff7e61e | 6767 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6768 | struct inode *inode = NULL; |
39279cc3 CM |
6769 | struct btrfs_trans_handle *trans; |
6770 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6771 | int err = 0; | |
6772 | int drop_on_err = 0; | |
b9d86667 | 6773 | u64 objectid = 0; |
00e4e6b3 | 6774 | u64 index = 0; |
39279cc3 | 6775 | |
9ed74f2d JB |
6776 | /* |
6777 | * 2 items for inode and ref | |
6778 | * 2 items for dir items | |
6779 | * 1 for xattr if selinux is on | |
6780 | */ | |
a22285a6 YZ |
6781 | trans = btrfs_start_transaction(root, 5); |
6782 | if (IS_ERR(trans)) | |
6783 | return PTR_ERR(trans); | |
39279cc3 | 6784 | |
581bb050 LZ |
6785 | err = btrfs_find_free_ino(root, &objectid); |
6786 | if (err) | |
6787 | goto out_fail; | |
6788 | ||
aec7477b | 6789 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
6790 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
6791 | S_IFDIR | mode, &index); | |
39279cc3 CM |
6792 | if (IS_ERR(inode)) { |
6793 | err = PTR_ERR(inode); | |
6794 | goto out_fail; | |
6795 | } | |
5f39d397 | 6796 | |
39279cc3 | 6797 | drop_on_err = 1; |
b0d5d10f CM |
6798 | /* these must be set before we unlock the inode */ |
6799 | inode->i_op = &btrfs_dir_inode_operations; | |
6800 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6801 | |
2a7dba39 | 6802 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6803 | if (err) |
b0d5d10f | 6804 | goto out_fail_inode; |
39279cc3 | 6805 | |
6ef06d27 | 6806 | btrfs_i_size_write(BTRFS_I(inode), 0); |
39279cc3 CM |
6807 | err = btrfs_update_inode(trans, root, inode); |
6808 | if (err) | |
b0d5d10f | 6809 | goto out_fail_inode; |
5f39d397 | 6810 | |
db0a669f NB |
6811 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
6812 | dentry->d_name.name, | |
6813 | dentry->d_name.len, 0, index); | |
39279cc3 | 6814 | if (err) |
b0d5d10f | 6815 | goto out_fail_inode; |
5f39d397 | 6816 | |
39279cc3 | 6817 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6818 | /* |
6819 | * mkdir is special. We're unlocking after we call d_instantiate | |
6820 | * to avoid a race with nfsd calling d_instantiate. | |
6821 | */ | |
6822 | unlock_new_inode(inode); | |
39279cc3 | 6823 | drop_on_err = 0; |
39279cc3 CM |
6824 | |
6825 | out_fail: | |
3a45bb20 | 6826 | btrfs_end_transaction(trans); |
c7cfb8a5 WS |
6827 | if (drop_on_err) { |
6828 | inode_dec_link_count(inode); | |
39279cc3 | 6829 | iput(inode); |
c7cfb8a5 | 6830 | } |
2ff7e61e JM |
6831 | btrfs_balance_delayed_items(fs_info); |
6832 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6833 | return err; |
b0d5d10f CM |
6834 | |
6835 | out_fail_inode: | |
6836 | unlock_new_inode(inode); | |
6837 | goto out_fail; | |
39279cc3 CM |
6838 | } |
6839 | ||
e6c4efd8 QW |
6840 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6841 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6842 | { | |
6843 | struct rb_node *next; | |
6844 | ||
6845 | next = rb_next(&em->rb_node); | |
6846 | if (!next) | |
6847 | return NULL; | |
6848 | return container_of(next, struct extent_map, rb_node); | |
6849 | } | |
6850 | ||
6851 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6852 | { | |
6853 | struct rb_node *prev; | |
6854 | ||
6855 | prev = rb_prev(&em->rb_node); | |
6856 | if (!prev) | |
6857 | return NULL; | |
6858 | return container_of(prev, struct extent_map, rb_node); | |
6859 | } | |
6860 | ||
d352ac68 | 6861 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6862 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6863 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6864 | * the best fitted new extent into the tree. |
d352ac68 | 6865 | */ |
3b951516 CM |
6866 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6867 | struct extent_map *existing, | |
e6dcd2dc | 6868 | struct extent_map *em, |
51f395ad | 6869 | u64 map_start) |
3b951516 | 6870 | { |
e6c4efd8 QW |
6871 | struct extent_map *prev; |
6872 | struct extent_map *next; | |
6873 | u64 start; | |
6874 | u64 end; | |
3b951516 | 6875 | u64 start_diff; |
3b951516 | 6876 | |
e6dcd2dc | 6877 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6878 | |
6879 | if (existing->start > map_start) { | |
6880 | next = existing; | |
6881 | prev = prev_extent_map(next); | |
6882 | } else { | |
6883 | prev = existing; | |
6884 | next = next_extent_map(prev); | |
6885 | } | |
6886 | ||
6887 | start = prev ? extent_map_end(prev) : em->start; | |
6888 | start = max_t(u64, start, em->start); | |
6889 | end = next ? next->start : extent_map_end(em); | |
6890 | end = min_t(u64, end, extent_map_end(em)); | |
6891 | start_diff = start - em->start; | |
6892 | em->start = start; | |
6893 | em->len = end - start; | |
c8b97818 CM |
6894 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6895 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6896 | em->block_start += start_diff; |
c8b97818 CM |
6897 | em->block_len -= start_diff; |
6898 | } | |
09a2a8f9 | 6899 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6900 | } |
6901 | ||
c8b97818 | 6902 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6903 | struct page *page, |
c8b97818 CM |
6904 | size_t pg_offset, u64 extent_offset, |
6905 | struct btrfs_file_extent_item *item) | |
6906 | { | |
6907 | int ret; | |
6908 | struct extent_buffer *leaf = path->nodes[0]; | |
6909 | char *tmp; | |
6910 | size_t max_size; | |
6911 | unsigned long inline_size; | |
6912 | unsigned long ptr; | |
261507a0 | 6913 | int compress_type; |
c8b97818 CM |
6914 | |
6915 | WARN_ON(pg_offset != 0); | |
261507a0 | 6916 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6917 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6918 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6919 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6920 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6921 | if (!tmp) |
6922 | return -ENOMEM; | |
c8b97818 CM |
6923 | ptr = btrfs_file_extent_inline_start(item); |
6924 | ||
6925 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6926 | ||
09cbfeaf | 6927 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6928 | ret = btrfs_decompress(compress_type, tmp, page, |
6929 | extent_offset, inline_size, max_size); | |
e1699d2d ZB |
6930 | |
6931 | /* | |
6932 | * decompression code contains a memset to fill in any space between the end | |
6933 | * of the uncompressed data and the end of max_size in case the decompressed | |
6934 | * data ends up shorter than ram_bytes. That doesn't cover the hole between | |
6935 | * the end of an inline extent and the beginning of the next block, so we | |
6936 | * cover that region here. | |
6937 | */ | |
6938 | ||
6939 | if (max_size + pg_offset < PAGE_SIZE) { | |
6940 | char *map = kmap(page); | |
6941 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); | |
6942 | kunmap(page); | |
6943 | } | |
c8b97818 | 6944 | kfree(tmp); |
166ae5a4 | 6945 | return ret; |
c8b97818 CM |
6946 | } |
6947 | ||
d352ac68 CM |
6948 | /* |
6949 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6950 | * the ugly parts come from merging extents from the disk with the in-ram |
6951 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6952 | * where the in-ram extents might be locked pending data=ordered completion. |
6953 | * | |
6954 | * This also copies inline extents directly into the page. | |
6955 | */ | |
fc4f21b1 NB |
6956 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
6957 | struct page *page, | |
6958 | size_t pg_offset, u64 start, u64 len, | |
6959 | int create) | |
a52d9a80 | 6960 | { |
fc4f21b1 | 6961 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
a52d9a80 CM |
6962 | int ret; |
6963 | int err = 0; | |
a52d9a80 CM |
6964 | u64 extent_start = 0; |
6965 | u64 extent_end = 0; | |
fc4f21b1 | 6966 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6967 | u32 found_type; |
f421950f | 6968 | struct btrfs_path *path = NULL; |
fc4f21b1 | 6969 | struct btrfs_root *root = inode->root; |
a52d9a80 | 6970 | struct btrfs_file_extent_item *item; |
5f39d397 CM |
6971 | struct extent_buffer *leaf; |
6972 | struct btrfs_key found_key; | |
a52d9a80 | 6973 | struct extent_map *em = NULL; |
fc4f21b1 NB |
6974 | struct extent_map_tree *em_tree = &inode->extent_tree; |
6975 | struct extent_io_tree *io_tree = &inode->io_tree; | |
a52d9a80 | 6976 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6977 | const bool new_inline = !page || create; |
a52d9a80 | 6978 | |
a52d9a80 | 6979 | again: |
890871be | 6980 | read_lock(&em_tree->lock); |
d1310b2e | 6981 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6982 | if (em) |
0b246afa | 6983 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6984 | read_unlock(&em_tree->lock); |
d1310b2e | 6985 | |
a52d9a80 | 6986 | if (em) { |
e1c4b745 CM |
6987 | if (em->start > start || em->start + em->len <= start) |
6988 | free_extent_map(em); | |
6989 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6990 | free_extent_map(em); |
6991 | else | |
6992 | goto out; | |
a52d9a80 | 6993 | } |
172ddd60 | 6994 | em = alloc_extent_map(); |
a52d9a80 | 6995 | if (!em) { |
d1310b2e CM |
6996 | err = -ENOMEM; |
6997 | goto out; | |
a52d9a80 | 6998 | } |
0b246afa | 6999 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 7000 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 7001 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 7002 | em->len = (u64)-1; |
c8b97818 | 7003 | em->block_len = (u64)-1; |
f421950f CM |
7004 | |
7005 | if (!path) { | |
7006 | path = btrfs_alloc_path(); | |
026fd317 JB |
7007 | if (!path) { |
7008 | err = -ENOMEM; | |
7009 | goto out; | |
7010 | } | |
7011 | /* | |
7012 | * Chances are we'll be called again, so go ahead and do | |
7013 | * readahead | |
7014 | */ | |
e4058b54 | 7015 | path->reada = READA_FORWARD; |
f421950f CM |
7016 | } |
7017 | ||
179e29e4 CM |
7018 | ret = btrfs_lookup_file_extent(trans, root, path, |
7019 | objectid, start, trans != NULL); | |
a52d9a80 CM |
7020 | if (ret < 0) { |
7021 | err = ret; | |
7022 | goto out; | |
7023 | } | |
7024 | ||
7025 | if (ret != 0) { | |
7026 | if (path->slots[0] == 0) | |
7027 | goto not_found; | |
7028 | path->slots[0]--; | |
7029 | } | |
7030 | ||
5f39d397 CM |
7031 | leaf = path->nodes[0]; |
7032 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 7033 | struct btrfs_file_extent_item); |
a52d9a80 | 7034 | /* are we inside the extent that was found? */ |
5f39d397 | 7035 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 7036 | found_type = found_key.type; |
5f39d397 | 7037 | if (found_key.objectid != objectid || |
a52d9a80 | 7038 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
7039 | /* |
7040 | * If we backup past the first extent we want to move forward | |
7041 | * and see if there is an extent in front of us, otherwise we'll | |
7042 | * say there is a hole for our whole search range which can | |
7043 | * cause problems. | |
7044 | */ | |
7045 | extent_end = start; | |
7046 | goto next; | |
a52d9a80 CM |
7047 | } |
7048 | ||
5f39d397 CM |
7049 | found_type = btrfs_file_extent_type(leaf, item); |
7050 | extent_start = found_key.offset; | |
d899e052 YZ |
7051 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7052 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 7053 | extent_end = extent_start + |
db94535d | 7054 | btrfs_file_extent_num_bytes(leaf, item); |
09ed2f16 LB |
7055 | |
7056 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, | |
7057 | extent_start); | |
9036c102 YZ |
7058 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
7059 | size_t size; | |
514ac8ad | 7060 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
da17066c | 7061 | extent_end = ALIGN(extent_start + size, |
0b246afa | 7062 | fs_info->sectorsize); |
09ed2f16 LB |
7063 | |
7064 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, | |
7065 | path->slots[0], | |
7066 | extent_start); | |
9036c102 | 7067 | } |
25a50341 | 7068 | next: |
9036c102 YZ |
7069 | if (start >= extent_end) { |
7070 | path->slots[0]++; | |
7071 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
7072 | ret = btrfs_next_leaf(root, path); | |
7073 | if (ret < 0) { | |
7074 | err = ret; | |
7075 | goto out; | |
a52d9a80 | 7076 | } |
9036c102 YZ |
7077 | if (ret > 0) |
7078 | goto not_found; | |
7079 | leaf = path->nodes[0]; | |
a52d9a80 | 7080 | } |
9036c102 YZ |
7081 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
7082 | if (found_key.objectid != objectid || | |
7083 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
7084 | goto not_found; | |
7085 | if (start + len <= found_key.offset) | |
7086 | goto not_found; | |
e2eca69d WS |
7087 | if (start > found_key.offset) |
7088 | goto next; | |
9036c102 | 7089 | em->start = start; |
70c8a91c | 7090 | em->orig_start = start; |
9036c102 YZ |
7091 | em->len = found_key.offset - start; |
7092 | goto not_found_em; | |
7093 | } | |
7094 | ||
fc4f21b1 | 7095 | btrfs_extent_item_to_extent_map(inode, path, item, |
9cdc5124 | 7096 | new_inline, em); |
7ffbb598 | 7097 | |
d899e052 YZ |
7098 | if (found_type == BTRFS_FILE_EXTENT_REG || |
7099 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
7100 | goto insert; |
7101 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 7102 | unsigned long ptr; |
a52d9a80 | 7103 | char *map; |
3326d1b0 CM |
7104 | size_t size; |
7105 | size_t extent_offset; | |
7106 | size_t copy_size; | |
a52d9a80 | 7107 | |
7ffbb598 | 7108 | if (new_inline) |
689f9346 | 7109 | goto out; |
5f39d397 | 7110 | |
514ac8ad | 7111 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 7112 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
7113 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
7114 | size - extent_offset); | |
3326d1b0 | 7115 | em->start = extent_start + extent_offset; |
0b246afa | 7116 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 7117 | em->orig_block_len = em->len; |
70c8a91c | 7118 | em->orig_start = em->start; |
689f9346 | 7119 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 7120 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
7121 | if (btrfs_file_extent_compression(leaf, item) != |
7122 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 7123 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 7124 | extent_offset, item); |
166ae5a4 ZB |
7125 | if (ret) { |
7126 | err = ret; | |
7127 | goto out; | |
7128 | } | |
c8b97818 CM |
7129 | } else { |
7130 | map = kmap(page); | |
7131 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
7132 | copy_size); | |
09cbfeaf | 7133 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 7134 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 7135 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
7136 | copy_size); |
7137 | } | |
c8b97818 CM |
7138 | kunmap(page); |
7139 | } | |
179e29e4 CM |
7140 | flush_dcache_page(page); |
7141 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 7142 | BUG(); |
179e29e4 CM |
7143 | if (!trans) { |
7144 | kunmap(page); | |
7145 | free_extent_map(em); | |
7146 | em = NULL; | |
ff5714cc | 7147 | |
b3b4aa74 | 7148 | btrfs_release_path(path); |
7a7eaa40 | 7149 | trans = btrfs_join_transaction(root); |
ff5714cc | 7150 | |
3612b495 TI |
7151 | if (IS_ERR(trans)) |
7152 | return ERR_CAST(trans); | |
179e29e4 CM |
7153 | goto again; |
7154 | } | |
c8b97818 | 7155 | map = kmap(page); |
70dec807 | 7156 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 7157 | copy_size); |
c8b97818 | 7158 | kunmap(page); |
179e29e4 | 7159 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 7160 | } |
d1310b2e | 7161 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7162 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7163 | goto insert; |
a52d9a80 CM |
7164 | } |
7165 | not_found: | |
7166 | em->start = start; | |
70c8a91c | 7167 | em->orig_start = start; |
d1310b2e | 7168 | em->len = len; |
a52d9a80 | 7169 | not_found_em: |
5f39d397 | 7170 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 7171 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 7172 | insert: |
b3b4aa74 | 7173 | btrfs_release_path(path); |
d1310b2e | 7174 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7175 | btrfs_err(fs_info, |
5d163e0e JM |
7176 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7177 | em->start, em->len, start, len); | |
a52d9a80 CM |
7178 | err = -EIO; |
7179 | goto out; | |
7180 | } | |
d1310b2e CM |
7181 | |
7182 | err = 0; | |
890871be | 7183 | write_lock(&em_tree->lock); |
09a2a8f9 | 7184 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7185 | /* it is possible that someone inserted the extent into the tree |
7186 | * while we had the lock dropped. It is also possible that | |
7187 | * an overlapping map exists in the tree | |
7188 | */ | |
a52d9a80 | 7189 | if (ret == -EEXIST) { |
3b951516 | 7190 | struct extent_map *existing; |
e6dcd2dc CM |
7191 | |
7192 | ret = 0; | |
7193 | ||
e6c4efd8 QW |
7194 | existing = search_extent_mapping(em_tree, start, len); |
7195 | /* | |
7196 | * existing will always be non-NULL, since there must be | |
7197 | * extent causing the -EEXIST. | |
7198 | */ | |
8dff9c85 | 7199 | if (existing->start == em->start && |
8e2bd3b7 | 7200 | extent_map_end(existing) >= extent_map_end(em) && |
8dff9c85 CM |
7201 | em->block_start == existing->block_start) { |
7202 | /* | |
8e2bd3b7 OS |
7203 | * The existing extent map already encompasses the |
7204 | * entire extent map we tried to add. | |
8dff9c85 CM |
7205 | */ |
7206 | free_extent_map(em); | |
7207 | em = existing; | |
7208 | err = 0; | |
7209 | ||
7210 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7211 | start <= existing->start) { |
e6c4efd8 QW |
7212 | /* |
7213 | * The existing extent map is the one nearest to | |
7214 | * the [start, start + len) range which overlaps | |
7215 | */ | |
7216 | err = merge_extent_mapping(em_tree, existing, | |
7217 | em, start); | |
e1c4b745 | 7218 | free_extent_map(existing); |
e6c4efd8 | 7219 | if (err) { |
3b951516 CM |
7220 | free_extent_map(em); |
7221 | em = NULL; | |
7222 | } | |
7223 | } else { | |
7224 | free_extent_map(em); | |
7225 | em = existing; | |
e6dcd2dc | 7226 | err = 0; |
a52d9a80 | 7227 | } |
a52d9a80 | 7228 | } |
890871be | 7229 | write_unlock(&em_tree->lock); |
a52d9a80 | 7230 | out: |
1abe9b8a | 7231 | |
fc4f21b1 | 7232 | trace_btrfs_get_extent(root, inode, em); |
1abe9b8a | 7233 | |
527afb44 | 7234 | btrfs_free_path(path); |
a52d9a80 | 7235 | if (trans) { |
3a45bb20 | 7236 | ret = btrfs_end_transaction(trans); |
d397712b | 7237 | if (!err) |
a52d9a80 CM |
7238 | err = ret; |
7239 | } | |
a52d9a80 CM |
7240 | if (err) { |
7241 | free_extent_map(em); | |
a52d9a80 CM |
7242 | return ERR_PTR(err); |
7243 | } | |
79787eaa | 7244 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7245 | return em; |
7246 | } | |
7247 | ||
fc4f21b1 NB |
7248 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
7249 | struct page *page, | |
7250 | size_t pg_offset, u64 start, u64 len, | |
7251 | int create) | |
ec29ed5b CM |
7252 | { |
7253 | struct extent_map *em; | |
7254 | struct extent_map *hole_em = NULL; | |
7255 | u64 range_start = start; | |
7256 | u64 end; | |
7257 | u64 found; | |
7258 | u64 found_end; | |
7259 | int err = 0; | |
7260 | ||
7261 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7262 | if (IS_ERR(em)) | |
7263 | return em; | |
9986277e DC |
7264 | /* |
7265 | * If our em maps to: | |
7266 | * - a hole or | |
7267 | * - a pre-alloc extent, | |
7268 | * there might actually be delalloc bytes behind it. | |
7269 | */ | |
7270 | if (em->block_start != EXTENT_MAP_HOLE && | |
7271 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7272 | return em; | |
7273 | else | |
7274 | hole_em = em; | |
ec29ed5b CM |
7275 | |
7276 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7277 | end = start + len; | |
7278 | if (end < start) | |
7279 | end = (u64)-1; | |
7280 | else | |
7281 | end -= 1; | |
7282 | ||
7283 | em = NULL; | |
7284 | ||
7285 | /* ok, we didn't find anything, lets look for delalloc */ | |
fc4f21b1 | 7286 | found = count_range_bits(&inode->io_tree, &range_start, |
ec29ed5b CM |
7287 | end, len, EXTENT_DELALLOC, 1); |
7288 | found_end = range_start + found; | |
7289 | if (found_end < range_start) | |
7290 | found_end = (u64)-1; | |
7291 | ||
7292 | /* | |
7293 | * we didn't find anything useful, return | |
7294 | * the original results from get_extent() | |
7295 | */ | |
7296 | if (range_start > end || found_end <= start) { | |
7297 | em = hole_em; | |
7298 | hole_em = NULL; | |
7299 | goto out; | |
7300 | } | |
7301 | ||
7302 | /* adjust the range_start to make sure it doesn't | |
7303 | * go backwards from the start they passed in | |
7304 | */ | |
67871254 | 7305 | range_start = max(start, range_start); |
ec29ed5b CM |
7306 | found = found_end - range_start; |
7307 | ||
7308 | if (found > 0) { | |
7309 | u64 hole_start = start; | |
7310 | u64 hole_len = len; | |
7311 | ||
172ddd60 | 7312 | em = alloc_extent_map(); |
ec29ed5b CM |
7313 | if (!em) { |
7314 | err = -ENOMEM; | |
7315 | goto out; | |
7316 | } | |
7317 | /* | |
7318 | * when btrfs_get_extent can't find anything it | |
7319 | * returns one huge hole | |
7320 | * | |
7321 | * make sure what it found really fits our range, and | |
7322 | * adjust to make sure it is based on the start from | |
7323 | * the caller | |
7324 | */ | |
7325 | if (hole_em) { | |
7326 | u64 calc_end = extent_map_end(hole_em); | |
7327 | ||
7328 | if (calc_end <= start || (hole_em->start > end)) { | |
7329 | free_extent_map(hole_em); | |
7330 | hole_em = NULL; | |
7331 | } else { | |
7332 | hole_start = max(hole_em->start, start); | |
7333 | hole_len = calc_end - hole_start; | |
7334 | } | |
7335 | } | |
7336 | em->bdev = NULL; | |
7337 | if (hole_em && range_start > hole_start) { | |
7338 | /* our hole starts before our delalloc, so we | |
7339 | * have to return just the parts of the hole | |
7340 | * that go until the delalloc starts | |
7341 | */ | |
7342 | em->len = min(hole_len, | |
7343 | range_start - hole_start); | |
7344 | em->start = hole_start; | |
7345 | em->orig_start = hole_start; | |
7346 | /* | |
7347 | * don't adjust block start at all, | |
7348 | * it is fixed at EXTENT_MAP_HOLE | |
7349 | */ | |
7350 | em->block_start = hole_em->block_start; | |
7351 | em->block_len = hole_len; | |
f9e4fb53 LB |
7352 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7353 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7354 | } else { |
7355 | em->start = range_start; | |
7356 | em->len = found; | |
7357 | em->orig_start = range_start; | |
7358 | em->block_start = EXTENT_MAP_DELALLOC; | |
7359 | em->block_len = found; | |
7360 | } | |
7361 | } else if (hole_em) { | |
7362 | return hole_em; | |
7363 | } | |
7364 | out: | |
7365 | ||
7366 | free_extent_map(hole_em); | |
7367 | if (err) { | |
7368 | free_extent_map(em); | |
7369 | return ERR_PTR(err); | |
7370 | } | |
7371 | return em; | |
7372 | } | |
7373 | ||
5f9a8a51 FM |
7374 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7375 | const u64 start, | |
7376 | const u64 len, | |
7377 | const u64 orig_start, | |
7378 | const u64 block_start, | |
7379 | const u64 block_len, | |
7380 | const u64 orig_block_len, | |
7381 | const u64 ram_bytes, | |
7382 | const int type) | |
7383 | { | |
7384 | struct extent_map *em = NULL; | |
7385 | int ret; | |
7386 | ||
5f9a8a51 | 7387 | if (type != BTRFS_ORDERED_NOCOW) { |
6f9994db LB |
7388 | em = create_io_em(inode, start, len, orig_start, |
7389 | block_start, block_len, orig_block_len, | |
7390 | ram_bytes, | |
7391 | BTRFS_COMPRESS_NONE, /* compress_type */ | |
7392 | type); | |
5f9a8a51 FM |
7393 | if (IS_ERR(em)) |
7394 | goto out; | |
7395 | } | |
7396 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7397 | len, block_len, type); | |
7398 | if (ret) { | |
7399 | if (em) { | |
7400 | free_extent_map(em); | |
dcdbc059 | 7401 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
5f9a8a51 FM |
7402 | start + len - 1, 0); |
7403 | } | |
7404 | em = ERR_PTR(ret); | |
7405 | } | |
7406 | out: | |
5f9a8a51 FM |
7407 | |
7408 | return em; | |
7409 | } | |
7410 | ||
4b46fce2 JB |
7411 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7412 | u64 start, u64 len) | |
7413 | { | |
0b246afa | 7414 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7415 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7416 | struct extent_map *em; |
4b46fce2 JB |
7417 | struct btrfs_key ins; |
7418 | u64 alloc_hint; | |
7419 | int ret; | |
4b46fce2 | 7420 | |
4b46fce2 | 7421 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7422 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7423 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7424 | if (ret) |
7425 | return ERR_PTR(ret); | |
4b46fce2 | 7426 | |
5f9a8a51 FM |
7427 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7428 | ins.objectid, ins.offset, ins.offset, | |
6288d6ea | 7429 | ins.offset, BTRFS_ORDERED_REGULAR); |
0b246afa | 7430 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7431 | if (IS_ERR(em)) |
2ff7e61e JM |
7432 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7433 | ins.offset, 1); | |
de0ee0ed | 7434 | |
4b46fce2 JB |
7435 | return em; |
7436 | } | |
7437 | ||
46bfbb5c CM |
7438 | /* |
7439 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7440 | * block must be cow'd | |
7441 | */ | |
00361589 | 7442 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7443 | u64 *orig_start, u64 *orig_block_len, |
7444 | u64 *ram_bytes) | |
46bfbb5c | 7445 | { |
2ff7e61e | 7446 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
46bfbb5c CM |
7447 | struct btrfs_path *path; |
7448 | int ret; | |
7449 | struct extent_buffer *leaf; | |
7450 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7451 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7452 | struct btrfs_file_extent_item *fi; |
7453 | struct btrfs_key key; | |
7454 | u64 disk_bytenr; | |
7455 | u64 backref_offset; | |
7456 | u64 extent_end; | |
7457 | u64 num_bytes; | |
7458 | int slot; | |
7459 | int found_type; | |
7ee9e440 | 7460 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7461 | |
46bfbb5c CM |
7462 | path = btrfs_alloc_path(); |
7463 | if (!path) | |
7464 | return -ENOMEM; | |
7465 | ||
f85b7379 DS |
7466 | ret = btrfs_lookup_file_extent(NULL, root, path, |
7467 | btrfs_ino(BTRFS_I(inode)), offset, 0); | |
46bfbb5c CM |
7468 | if (ret < 0) |
7469 | goto out; | |
7470 | ||
7471 | slot = path->slots[0]; | |
7472 | if (ret == 1) { | |
7473 | if (slot == 0) { | |
7474 | /* can't find the item, must cow */ | |
7475 | ret = 0; | |
7476 | goto out; | |
7477 | } | |
7478 | slot--; | |
7479 | } | |
7480 | ret = 0; | |
7481 | leaf = path->nodes[0]; | |
7482 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7483 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7484 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7485 | /* not our file or wrong item type, must cow */ | |
7486 | goto out; | |
7487 | } | |
7488 | ||
7489 | if (key.offset > offset) { | |
7490 | /* Wrong offset, must cow */ | |
7491 | goto out; | |
7492 | } | |
7493 | ||
7494 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7495 | found_type = btrfs_file_extent_type(leaf, fi); | |
7496 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7497 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7498 | /* not a regular extent, must cow */ | |
7499 | goto out; | |
7500 | } | |
7ee9e440 JB |
7501 | |
7502 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7503 | goto out; | |
7504 | ||
e77751aa MX |
7505 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7506 | if (extent_end <= offset) | |
7507 | goto out; | |
7508 | ||
46bfbb5c | 7509 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7510 | if (disk_bytenr == 0) |
7511 | goto out; | |
7512 | ||
7513 | if (btrfs_file_extent_compression(leaf, fi) || | |
7514 | btrfs_file_extent_encryption(leaf, fi) || | |
7515 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7516 | goto out; | |
7517 | ||
46bfbb5c CM |
7518 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7519 | ||
7ee9e440 JB |
7520 | if (orig_start) { |
7521 | *orig_start = key.offset - backref_offset; | |
7522 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7523 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7524 | } | |
eb384b55 | 7525 | |
2ff7e61e | 7526 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7527 | goto out; |
7b2b7085 MX |
7528 | |
7529 | num_bytes = min(offset + *len, extent_end) - offset; | |
7530 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7531 | u64 range_end; | |
7532 | ||
da17066c JM |
7533 | range_end = round_up(offset + num_bytes, |
7534 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7535 | ret = test_range_bit(io_tree, offset, range_end, |
7536 | EXTENT_DELALLOC, 0, NULL); | |
7537 | if (ret) { | |
7538 | ret = -EAGAIN; | |
7539 | goto out; | |
7540 | } | |
7541 | } | |
7542 | ||
1bda19eb | 7543 | btrfs_release_path(path); |
46bfbb5c CM |
7544 | |
7545 | /* | |
7546 | * look for other files referencing this extent, if we | |
7547 | * find any we must cow | |
7548 | */ | |
00361589 | 7549 | |
e4c3b2dc | 7550 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7551 | key.offset - backref_offset, disk_bytenr); |
00361589 JB |
7552 | if (ret) { |
7553 | ret = 0; | |
7554 | goto out; | |
7555 | } | |
46bfbb5c CM |
7556 | |
7557 | /* | |
7558 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7559 | * in this extent we are about to write. If there | |
7560 | * are any csums in that range we have to cow in order | |
7561 | * to keep the csums correct | |
7562 | */ | |
7563 | disk_bytenr += backref_offset; | |
7564 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7565 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7566 | goto out; | |
46bfbb5c CM |
7567 | /* |
7568 | * all of the above have passed, it is safe to overwrite this extent | |
7569 | * without cow | |
7570 | */ | |
eb384b55 | 7571 | *len = num_bytes; |
46bfbb5c CM |
7572 | ret = 1; |
7573 | out: | |
7574 | btrfs_free_path(path); | |
7575 | return ret; | |
7576 | } | |
7577 | ||
fc4adbff AG |
7578 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7579 | { | |
7580 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
e03733da | 7581 | bool found = false; |
fc4adbff AG |
7582 | void **pagep = NULL; |
7583 | struct page *page = NULL; | |
cc2b702c DS |
7584 | unsigned long start_idx; |
7585 | unsigned long end_idx; | |
fc4adbff | 7586 | |
09cbfeaf | 7587 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7588 | |
7589 | /* | |
7590 | * end is the last byte in the last page. end == start is legal | |
7591 | */ | |
09cbfeaf | 7592 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7593 | |
7594 | rcu_read_lock(); | |
7595 | ||
7596 | /* Most of the code in this while loop is lifted from | |
7597 | * find_get_page. It's been modified to begin searching from a | |
7598 | * page and return just the first page found in that range. If the | |
7599 | * found idx is less than or equal to the end idx then we know that | |
7600 | * a page exists. If no pages are found or if those pages are | |
7601 | * outside of the range then we're fine (yay!) */ | |
7602 | while (page == NULL && | |
7603 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7604 | page = radix_tree_deref_slot(pagep); | |
7605 | if (unlikely(!page)) | |
7606 | break; | |
7607 | ||
7608 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7609 | if (radix_tree_deref_retry(page)) { |
7610 | page = NULL; | |
fc4adbff | 7611 | continue; |
809f9016 | 7612 | } |
fc4adbff AG |
7613 | /* |
7614 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7615 | * here as an exceptional entry: so return it without | |
7616 | * attempting to raise page count. | |
7617 | */ | |
6fdef6d4 | 7618 | page = NULL; |
fc4adbff AG |
7619 | break; /* TODO: Is this relevant for this use case? */ |
7620 | } | |
7621 | ||
91405151 FM |
7622 | if (!page_cache_get_speculative(page)) { |
7623 | page = NULL; | |
fc4adbff | 7624 | continue; |
91405151 | 7625 | } |
fc4adbff AG |
7626 | |
7627 | /* | |
7628 | * Has the page moved? | |
7629 | * This is part of the lockless pagecache protocol. See | |
7630 | * include/linux/pagemap.h for details. | |
7631 | */ | |
7632 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7633 | put_page(page); |
fc4adbff AG |
7634 | page = NULL; |
7635 | } | |
7636 | } | |
7637 | ||
7638 | if (page) { | |
7639 | if (page->index <= end_idx) | |
7640 | found = true; | |
09cbfeaf | 7641 | put_page(page); |
fc4adbff AG |
7642 | } |
7643 | ||
7644 | rcu_read_unlock(); | |
7645 | return found; | |
7646 | } | |
7647 | ||
eb838e73 JB |
7648 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7649 | struct extent_state **cached_state, int writing) | |
7650 | { | |
7651 | struct btrfs_ordered_extent *ordered; | |
7652 | int ret = 0; | |
7653 | ||
7654 | while (1) { | |
7655 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7656 | cached_state); |
eb838e73 JB |
7657 | /* |
7658 | * We're concerned with the entire range that we're going to be | |
01327610 | 7659 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7660 | * extents in this range. |
7661 | */ | |
a776c6fa | 7662 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
eb838e73 JB |
7663 | lockend - lockstart + 1); |
7664 | ||
7665 | /* | |
7666 | * We need to make sure there are no buffered pages in this | |
7667 | * range either, we could have raced between the invalidate in | |
7668 | * generic_file_direct_write and locking the extent. The | |
7669 | * invalidate needs to happen so that reads after a write do not | |
7670 | * get stale data. | |
7671 | */ | |
fc4adbff AG |
7672 | if (!ordered && |
7673 | (!writing || | |
7674 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7675 | break; |
7676 | ||
7677 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7678 | cached_state, GFP_NOFS); | |
7679 | ||
7680 | if (ordered) { | |
ade77029 FM |
7681 | /* |
7682 | * If we are doing a DIO read and the ordered extent we | |
7683 | * found is for a buffered write, we can not wait for it | |
7684 | * to complete and retry, because if we do so we can | |
7685 | * deadlock with concurrent buffered writes on page | |
7686 | * locks. This happens only if our DIO read covers more | |
7687 | * than one extent map, if at this point has already | |
7688 | * created an ordered extent for a previous extent map | |
7689 | * and locked its range in the inode's io tree, and a | |
7690 | * concurrent write against that previous extent map's | |
7691 | * range and this range started (we unlock the ranges | |
7692 | * in the io tree only when the bios complete and | |
7693 | * buffered writes always lock pages before attempting | |
7694 | * to lock range in the io tree). | |
7695 | */ | |
7696 | if (writing || | |
7697 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7698 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7699 | else | |
7700 | ret = -ENOTBLK; | |
eb838e73 JB |
7701 | btrfs_put_ordered_extent(ordered); |
7702 | } else { | |
eb838e73 | 7703 | /* |
b850ae14 FM |
7704 | * We could trigger writeback for this range (and wait |
7705 | * for it to complete) and then invalidate the pages for | |
7706 | * this range (through invalidate_inode_pages2_range()), | |
7707 | * but that can lead us to a deadlock with a concurrent | |
7708 | * call to readpages() (a buffered read or a defrag call | |
7709 | * triggered a readahead) on a page lock due to an | |
7710 | * ordered dio extent we created before but did not have | |
7711 | * yet a corresponding bio submitted (whence it can not | |
7712 | * complete), which makes readpages() wait for that | |
7713 | * ordered extent to complete while holding a lock on | |
7714 | * that page. | |
eb838e73 | 7715 | */ |
b850ae14 | 7716 | ret = -ENOTBLK; |
eb838e73 JB |
7717 | } |
7718 | ||
ade77029 FM |
7719 | if (ret) |
7720 | break; | |
7721 | ||
eb838e73 JB |
7722 | cond_resched(); |
7723 | } | |
7724 | ||
7725 | return ret; | |
7726 | } | |
7727 | ||
6f9994db LB |
7728 | /* The callers of this must take lock_extent() */ |
7729 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, | |
7730 | u64 orig_start, u64 block_start, | |
7731 | u64 block_len, u64 orig_block_len, | |
7732 | u64 ram_bytes, int compress_type, | |
7733 | int type) | |
69ffb543 JB |
7734 | { |
7735 | struct extent_map_tree *em_tree; | |
7736 | struct extent_map *em; | |
7737 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7738 | int ret; | |
7739 | ||
6f9994db LB |
7740 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
7741 | type == BTRFS_ORDERED_COMPRESSED || | |
7742 | type == BTRFS_ORDERED_NOCOW || | |
1af4a0aa | 7743 | type == BTRFS_ORDERED_REGULAR); |
6f9994db | 7744 | |
69ffb543 JB |
7745 | em_tree = &BTRFS_I(inode)->extent_tree; |
7746 | em = alloc_extent_map(); | |
7747 | if (!em) | |
7748 | return ERR_PTR(-ENOMEM); | |
7749 | ||
7750 | em->start = start; | |
7751 | em->orig_start = orig_start; | |
7752 | em->len = len; | |
7753 | em->block_len = block_len; | |
7754 | em->block_start = block_start; | |
7755 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7756 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7757 | em->ram_bytes = ram_bytes; |
70c8a91c | 7758 | em->generation = -1; |
69ffb543 | 7759 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1af4a0aa | 7760 | if (type == BTRFS_ORDERED_PREALLOC) { |
b11e234d | 7761 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
1af4a0aa | 7762 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
6f9994db LB |
7763 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
7764 | em->compress_type = compress_type; | |
7765 | } | |
69ffb543 JB |
7766 | |
7767 | do { | |
dcdbc059 | 7768 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
69ffb543 JB |
7769 | em->start + em->len - 1, 0); |
7770 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7771 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 | 7772 | write_unlock(&em_tree->lock); |
6f9994db LB |
7773 | /* |
7774 | * The caller has taken lock_extent(), who could race with us | |
7775 | * to add em? | |
7776 | */ | |
69ffb543 JB |
7777 | } while (ret == -EEXIST); |
7778 | ||
7779 | if (ret) { | |
7780 | free_extent_map(em); | |
7781 | return ERR_PTR(ret); | |
7782 | } | |
7783 | ||
6f9994db | 7784 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
69ffb543 JB |
7785 | return em; |
7786 | } | |
7787 | ||
9c9464cc FM |
7788 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7789 | struct btrfs_dio_data *dio_data, | |
7790 | const u64 len) | |
7791 | { | |
823bb20a | 7792 | unsigned num_extents = count_max_extents(len); |
9c9464cc | 7793 | |
9c9464cc FM |
7794 | /* |
7795 | * If we have an outstanding_extents count still set then we're | |
7796 | * within our reservation, otherwise we need to adjust our inode | |
7797 | * counter appropriately. | |
7798 | */ | |
c2931667 | 7799 | if (dio_data->outstanding_extents >= num_extents) { |
9c9464cc FM |
7800 | dio_data->outstanding_extents -= num_extents; |
7801 | } else { | |
c2931667 LB |
7802 | /* |
7803 | * If dio write length has been split due to no large enough | |
7804 | * contiguous space, we need to compensate our inode counter | |
7805 | * appropriately. | |
7806 | */ | |
7807 | u64 num_needed = num_extents - dio_data->outstanding_extents; | |
7808 | ||
9c9464cc | 7809 | spin_lock(&BTRFS_I(inode)->lock); |
c2931667 | 7810 | BTRFS_I(inode)->outstanding_extents += num_needed; |
9c9464cc FM |
7811 | spin_unlock(&BTRFS_I(inode)->lock); |
7812 | } | |
7813 | } | |
7814 | ||
4b46fce2 JB |
7815 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7816 | struct buffer_head *bh_result, int create) | |
7817 | { | |
0b246afa | 7818 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7819 | struct extent_map *em; |
eb838e73 | 7820 | struct extent_state *cached_state = NULL; |
50745b0a | 7821 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7822 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7823 | u64 lockstart, lockend; |
4b46fce2 | 7824 | u64 len = bh_result->b_size; |
eb838e73 | 7825 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7826 | int ret = 0; |
eb838e73 | 7827 | |
172a5049 | 7828 | if (create) |
3266789f | 7829 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7830 | else |
0b246afa | 7831 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7832 | |
c329861d JB |
7833 | lockstart = start; |
7834 | lockend = start + len - 1; | |
7835 | ||
e1cbbfa5 JB |
7836 | if (current->journal_info) { |
7837 | /* | |
7838 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7839 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7840 | * confused. |
7841 | */ | |
50745b0a | 7842 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7843 | current->journal_info = NULL; |
7844 | } | |
7845 | ||
eb838e73 JB |
7846 | /* |
7847 | * If this errors out it's because we couldn't invalidate pagecache for | |
7848 | * this range and we need to fallback to buffered. | |
7849 | */ | |
9c9464cc FM |
7850 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7851 | create)) { | |
7852 | ret = -ENOTBLK; | |
7853 | goto err; | |
7854 | } | |
eb838e73 | 7855 | |
fc4f21b1 | 7856 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
eb838e73 JB |
7857 | if (IS_ERR(em)) { |
7858 | ret = PTR_ERR(em); | |
7859 | goto unlock_err; | |
7860 | } | |
4b46fce2 JB |
7861 | |
7862 | /* | |
7863 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7864 | * io. INLINE is special, and we could probably kludge it in here, but | |
7865 | * it's still buffered so for safety lets just fall back to the generic | |
7866 | * buffered path. | |
7867 | * | |
7868 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7869 | * decompress it, so there will be buffering required no matter what we | |
7870 | * do, so go ahead and fallback to buffered. | |
7871 | * | |
01327610 | 7872 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7873 | * to buffered IO. Don't blame me, this is the price we pay for using |
7874 | * the generic code. | |
7875 | */ | |
7876 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7877 | em->block_start == EXTENT_MAP_INLINE) { | |
7878 | free_extent_map(em); | |
eb838e73 JB |
7879 | ret = -ENOTBLK; |
7880 | goto unlock_err; | |
4b46fce2 JB |
7881 | } |
7882 | ||
7883 | /* Just a good old fashioned hole, return */ | |
7884 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7885 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7886 | free_extent_map(em); | |
eb838e73 | 7887 | goto unlock_err; |
4b46fce2 JB |
7888 | } |
7889 | ||
7890 | /* | |
7891 | * We don't allocate a new extent in the following cases | |
7892 | * | |
7893 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7894 | * existing extent. | |
7895 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7896 | * just use the extent. | |
7897 | * | |
7898 | */ | |
46bfbb5c | 7899 | if (!create) { |
eb838e73 JB |
7900 | len = min(len, em->len - (start - em->start)); |
7901 | lockstart = start + len; | |
7902 | goto unlock; | |
46bfbb5c | 7903 | } |
4b46fce2 JB |
7904 | |
7905 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7906 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7907 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7908 | int type; |
eb384b55 | 7909 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7910 | |
7911 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7912 | type = BTRFS_ORDERED_PREALLOC; | |
7913 | else | |
7914 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7915 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7916 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7917 | |
00361589 | 7918 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c | 7919 | &orig_block_len, &ram_bytes) == 1 && |
0b246afa | 7920 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
5f9a8a51 | 7921 | struct extent_map *em2; |
0b901916 | 7922 | |
5f9a8a51 FM |
7923 | em2 = btrfs_create_dio_extent(inode, start, len, |
7924 | orig_start, block_start, | |
7925 | len, orig_block_len, | |
7926 | ram_bytes, type); | |
0b246afa | 7927 | btrfs_dec_nocow_writers(fs_info, block_start); |
69ffb543 JB |
7928 | if (type == BTRFS_ORDERED_PREALLOC) { |
7929 | free_extent_map(em); | |
5f9a8a51 | 7930 | em = em2; |
69ffb543 | 7931 | } |
5f9a8a51 FM |
7932 | if (em2 && IS_ERR(em2)) { |
7933 | ret = PTR_ERR(em2); | |
eb838e73 | 7934 | goto unlock_err; |
46bfbb5c | 7935 | } |
18513091 WX |
7936 | /* |
7937 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7938 | * use the existing or preallocated extent, so does not | |
7939 | * need to adjust btrfs_space_info's bytes_may_use. | |
7940 | */ | |
7941 | btrfs_free_reserved_data_space_noquota(inode, | |
7942 | start, len); | |
46bfbb5c | 7943 | goto unlock; |
4b46fce2 | 7944 | } |
4b46fce2 | 7945 | } |
00361589 | 7946 | |
46bfbb5c CM |
7947 | /* |
7948 | * this will cow the extent, reset the len in case we changed | |
7949 | * it above | |
7950 | */ | |
7951 | len = bh_result->b_size; | |
70c8a91c JB |
7952 | free_extent_map(em); |
7953 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7954 | if (IS_ERR(em)) { |
7955 | ret = PTR_ERR(em); | |
7956 | goto unlock_err; | |
7957 | } | |
46bfbb5c CM |
7958 | len = min(len, em->len - (start - em->start)); |
7959 | unlock: | |
4b46fce2 JB |
7960 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7961 | inode->i_blkbits; | |
46bfbb5c | 7962 | bh_result->b_size = len; |
4b46fce2 JB |
7963 | bh_result->b_bdev = em->bdev; |
7964 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7965 | if (create) { |
7966 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7967 | set_buffer_new(bh_result); | |
7968 | ||
7969 | /* | |
7970 | * Need to update the i_size under the extent lock so buffered | |
7971 | * readers will get the updated i_size when we unlock. | |
7972 | */ | |
4aaedfb0 | 7973 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
c3473e83 | 7974 | i_size_write(inode, start + len); |
0934856d | 7975 | |
9c9464cc | 7976 | adjust_dio_outstanding_extents(inode, dio_data, len); |
50745b0a | 7977 | WARN_ON(dio_data->reserve < len); |
7978 | dio_data->reserve -= len; | |
f28a4928 | 7979 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7980 | current->journal_info = dio_data; |
c3473e83 | 7981 | } |
4b46fce2 | 7982 | |
eb838e73 JB |
7983 | /* |
7984 | * In the case of write we need to clear and unlock the entire range, | |
7985 | * in the case of read we need to unlock only the end area that we | |
7986 | * aren't using if there is any left over space. | |
7987 | */ | |
24c03fa5 | 7988 | if (lockstart < lockend) { |
0934856d MX |
7989 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7990 | lockend, unlock_bits, 1, 0, | |
7991 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7992 | } else { |
eb838e73 | 7993 | free_extent_state(cached_state); |
24c03fa5 | 7994 | } |
eb838e73 | 7995 | |
4b46fce2 JB |
7996 | free_extent_map(em); |
7997 | ||
7998 | return 0; | |
eb838e73 JB |
7999 | |
8000 | unlock_err: | |
eb838e73 JB |
8001 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
8002 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 8003 | err: |
50745b0a | 8004 | if (dio_data) |
8005 | current->journal_info = dio_data; | |
9c9464cc FM |
8006 | /* |
8007 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
8008 | * write less data then expected, so that we don't underflow our inode's | |
8009 | * outstanding extents counter. | |
8010 | */ | |
8011 | if (create && dio_data) | |
8012 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
8013 | ||
eb838e73 | 8014 | return ret; |
4b46fce2 JB |
8015 | } |
8016 | ||
58efbc9f OS |
8017 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
8018 | struct bio *bio, | |
8019 | int mirror_num) | |
8b110e39 | 8020 | { |
2ff7e61e | 8021 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
58efbc9f | 8022 | blk_status_t ret; |
8b110e39 | 8023 | |
37226b21 | 8024 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
8025 | |
8026 | bio_get(bio); | |
8027 | ||
2ff7e61e | 8028 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 MX |
8029 | if (ret) |
8030 | goto err; | |
8031 | ||
2ff7e61e | 8032 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
8b110e39 MX |
8033 | err: |
8034 | bio_put(bio); | |
8035 | return ret; | |
8036 | } | |
8037 | ||
8038 | static int btrfs_check_dio_repairable(struct inode *inode, | |
8039 | struct bio *failed_bio, | |
8040 | struct io_failure_record *failrec, | |
8041 | int failed_mirror) | |
8042 | { | |
ab8d0fc4 | 8043 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
8044 | int num_copies; |
8045 | ||
ab8d0fc4 | 8046 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
8047 | if (num_copies == 1) { |
8048 | /* | |
8049 | * we only have a single copy of the data, so don't bother with | |
8050 | * all the retry and error correction code that follows. no | |
8051 | * matter what the error is, it is very likely to persist. | |
8052 | */ | |
ab8d0fc4 JM |
8053 | btrfs_debug(fs_info, |
8054 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
8055 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
8056 | return 0; |
8057 | } | |
8058 | ||
8059 | failrec->failed_mirror = failed_mirror; | |
8060 | failrec->this_mirror++; | |
8061 | if (failrec->this_mirror == failed_mirror) | |
8062 | failrec->this_mirror++; | |
8063 | ||
8064 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
8065 | btrfs_debug(fs_info, |
8066 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
8067 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
8068 | return 0; |
8069 | } | |
8070 | ||
8071 | return 1; | |
8072 | } | |
8073 | ||
58efbc9f OS |
8074 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
8075 | struct page *page, unsigned int pgoff, | |
8076 | u64 start, u64 end, int failed_mirror, | |
8077 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
8078 | { |
8079 | struct io_failure_record *failrec; | |
7870d082 JB |
8080 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8081 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8b110e39 MX |
8082 | struct bio *bio; |
8083 | int isector; | |
f1c77c55 | 8084 | unsigned int read_mode = 0; |
17347cec | 8085 | int segs; |
8b110e39 | 8086 | int ret; |
58efbc9f | 8087 | blk_status_t status; |
8b110e39 | 8088 | |
37226b21 | 8089 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
8090 | |
8091 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
8092 | if (ret) | |
58efbc9f | 8093 | return errno_to_blk_status(ret); |
8b110e39 MX |
8094 | |
8095 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
8096 | failed_mirror); | |
8097 | if (!ret) { | |
7870d082 | 8098 | free_io_failure(failure_tree, io_tree, failrec); |
58efbc9f | 8099 | return BLK_STS_IOERR; |
8b110e39 MX |
8100 | } |
8101 | ||
17347cec LB |
8102 | segs = bio_segments(failed_bio); |
8103 | if (segs > 1 || | |
8104 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) | |
70fd7614 | 8105 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
8106 | |
8107 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
8108 | isector >>= inode->i_sb->s_blocksize_bits; | |
8109 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 8110 | pgoff, isector, repair_endio, repair_arg); |
37226b21 | 8111 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
8112 | |
8113 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
913e1535 | 8114 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
8b110e39 MX |
8115 | read_mode, failrec->this_mirror, failrec->in_validation); |
8116 | ||
58efbc9f OS |
8117 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8118 | if (status) { | |
7870d082 | 8119 | free_io_failure(failure_tree, io_tree, failrec); |
8b110e39 MX |
8120 | bio_put(bio); |
8121 | } | |
8122 | ||
58efbc9f | 8123 | return status; |
8b110e39 MX |
8124 | } |
8125 | ||
8126 | struct btrfs_retry_complete { | |
8127 | struct completion done; | |
8128 | struct inode *inode; | |
8129 | u64 start; | |
8130 | int uptodate; | |
8131 | }; | |
8132 | ||
4246a0b6 | 8133 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
8134 | { |
8135 | struct btrfs_retry_complete *done = bio->bi_private; | |
7870d082 | 8136 | struct inode *inode = done->inode; |
8b110e39 | 8137 | struct bio_vec *bvec; |
7870d082 | 8138 | struct extent_io_tree *io_tree, *failure_tree; |
8b110e39 MX |
8139 | int i; |
8140 | ||
4e4cbee9 | 8141 | if (bio->bi_status) |
8b110e39 MX |
8142 | goto end; |
8143 | ||
2dabb324 | 8144 | ASSERT(bio->bi_vcnt == 1); |
7870d082 JB |
8145 | io_tree = &BTRFS_I(inode)->io_tree; |
8146 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8147 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); | |
2dabb324 | 8148 | |
8b110e39 | 8149 | done->uptodate = 1; |
c09abff8 | 8150 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8151 | bio_for_each_segment_all(bvec, bio, i) |
7870d082 JB |
8152 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
8153 | io_tree, done->start, bvec->bv_page, | |
8154 | btrfs_ino(BTRFS_I(inode)), 0); | |
8b110e39 MX |
8155 | end: |
8156 | complete(&done->done); | |
8157 | bio_put(bio); | |
8158 | } | |
8159 | ||
58efbc9f OS |
8160 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
8161 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 8162 | { |
2dabb324 | 8163 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8164 | struct bio_vec bvec; |
8165 | struct bvec_iter iter; | |
8b110e39 | 8166 | struct btrfs_retry_complete done; |
4b46fce2 | 8167 | u64 start; |
2dabb324 CR |
8168 | unsigned int pgoff; |
8169 | u32 sectorsize; | |
8170 | int nr_sectors; | |
58efbc9f OS |
8171 | blk_status_t ret; |
8172 | blk_status_t err = BLK_STS_OK; | |
4b46fce2 | 8173 | |
2dabb324 | 8174 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8175 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8176 | |
8b110e39 MX |
8177 | start = io_bio->logical; |
8178 | done.inode = inode; | |
17347cec | 8179 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8180 | |
17347cec LB |
8181 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8182 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
8183 | pgoff = bvec.bv_offset; | |
2dabb324 CR |
8184 | |
8185 | next_block_or_try_again: | |
8b110e39 MX |
8186 | done.uptodate = 0; |
8187 | done.start = start; | |
8188 | init_completion(&done.done); | |
8189 | ||
17347cec | 8190 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
2dabb324 CR |
8191 | pgoff, start, start + sectorsize - 1, |
8192 | io_bio->mirror_num, | |
8193 | btrfs_retry_endio_nocsum, &done); | |
629ebf4f LB |
8194 | if (ret) { |
8195 | err = ret; | |
8196 | goto next; | |
8197 | } | |
8b110e39 | 8198 | |
9c17f6cd | 8199 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8200 | |
8201 | if (!done.uptodate) { | |
8202 | /* We might have another mirror, so try again */ | |
2dabb324 | 8203 | goto next_block_or_try_again; |
8b110e39 MX |
8204 | } |
8205 | ||
629ebf4f | 8206 | next: |
2dabb324 CR |
8207 | start += sectorsize; |
8208 | ||
97bf5a55 LB |
8209 | nr_sectors--; |
8210 | if (nr_sectors) { | |
2dabb324 | 8211 | pgoff += sectorsize; |
97bf5a55 | 8212 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8213 | goto next_block_or_try_again; |
8214 | } | |
8b110e39 MX |
8215 | } |
8216 | ||
629ebf4f | 8217 | return err; |
8b110e39 MX |
8218 | } |
8219 | ||
4246a0b6 | 8220 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8221 | { |
8222 | struct btrfs_retry_complete *done = bio->bi_private; | |
8223 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7870d082 JB |
8224 | struct extent_io_tree *io_tree, *failure_tree; |
8225 | struct inode *inode = done->inode; | |
8b110e39 MX |
8226 | struct bio_vec *bvec; |
8227 | int uptodate; | |
8228 | int ret; | |
8229 | int i; | |
8230 | ||
4e4cbee9 | 8231 | if (bio->bi_status) |
8b110e39 MX |
8232 | goto end; |
8233 | ||
8234 | uptodate = 1; | |
2dabb324 | 8235 | |
2dabb324 | 8236 | ASSERT(bio->bi_vcnt == 1); |
2e949b0a | 8237 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
2dabb324 | 8238 | |
7870d082 JB |
8239 | io_tree = &BTRFS_I(inode)->io_tree; |
8240 | failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
8241 | ||
c09abff8 | 8242 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
8b110e39 | 8243 | bio_for_each_segment_all(bvec, bio, i) { |
7870d082 JB |
8244 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
8245 | bvec->bv_offset, done->start, | |
8246 | bvec->bv_len); | |
8b110e39 | 8247 | if (!ret) |
7870d082 JB |
8248 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
8249 | failure_tree, io_tree, done->start, | |
8250 | bvec->bv_page, | |
8251 | btrfs_ino(BTRFS_I(inode)), | |
8252 | bvec->bv_offset); | |
8b110e39 MX |
8253 | else |
8254 | uptodate = 0; | |
8255 | } | |
8256 | ||
8257 | done->uptodate = uptodate; | |
8258 | end: | |
8259 | complete(&done->done); | |
8260 | bio_put(bio); | |
8261 | } | |
8262 | ||
4e4cbee9 CH |
8263 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
8264 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 | 8265 | { |
2dabb324 | 8266 | struct btrfs_fs_info *fs_info; |
17347cec LB |
8267 | struct bio_vec bvec; |
8268 | struct bvec_iter iter; | |
8b110e39 MX |
8269 | struct btrfs_retry_complete done; |
8270 | u64 start; | |
8271 | u64 offset = 0; | |
2dabb324 CR |
8272 | u32 sectorsize; |
8273 | int nr_sectors; | |
8274 | unsigned int pgoff; | |
8275 | int csum_pos; | |
ef7cdac1 | 8276 | bool uptodate = (err == 0); |
8b110e39 | 8277 | int ret; |
58efbc9f | 8278 | blk_status_t status; |
dc380aea | 8279 | |
2dabb324 | 8280 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8281 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8282 | |
58efbc9f | 8283 | err = BLK_STS_OK; |
c1dc0896 | 8284 | start = io_bio->logical; |
8b110e39 | 8285 | done.inode = inode; |
17347cec | 8286 | io_bio->bio.bi_iter = io_bio->iter; |
8b110e39 | 8287 | |
17347cec LB |
8288 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
8289 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); | |
2dabb324 | 8290 | |
17347cec | 8291 | pgoff = bvec.bv_offset; |
2dabb324 | 8292 | next_block: |
ef7cdac1 LB |
8293 | if (uptodate) { |
8294 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8295 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8296 | bvec.bv_page, pgoff, start, sectorsize); | |
8297 | if (likely(!ret)) | |
8298 | goto next; | |
8299 | } | |
8b110e39 MX |
8300 | try_again: |
8301 | done.uptodate = 0; | |
8302 | done.start = start; | |
8303 | init_completion(&done.done); | |
8304 | ||
58efbc9f OS |
8305 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
8306 | pgoff, start, start + sectorsize - 1, | |
8307 | io_bio->mirror_num, btrfs_retry_endio, | |
8308 | &done); | |
8309 | if (status) { | |
8310 | err = status; | |
8b110e39 MX |
8311 | goto next; |
8312 | } | |
8313 | ||
9c17f6cd | 8314 | wait_for_completion_io(&done.done); |
8b110e39 MX |
8315 | |
8316 | if (!done.uptodate) { | |
8317 | /* We might have another mirror, so try again */ | |
8318 | goto try_again; | |
8319 | } | |
8320 | next: | |
2dabb324 CR |
8321 | offset += sectorsize; |
8322 | start += sectorsize; | |
8323 | ||
8324 | ASSERT(nr_sectors); | |
8325 | ||
97bf5a55 LB |
8326 | nr_sectors--; |
8327 | if (nr_sectors) { | |
2dabb324 | 8328 | pgoff += sectorsize; |
97bf5a55 | 8329 | ASSERT(pgoff < PAGE_SIZE); |
2dabb324 CR |
8330 | goto next_block; |
8331 | } | |
2c30c71b | 8332 | } |
c1dc0896 MX |
8333 | |
8334 | return err; | |
8335 | } | |
8336 | ||
4e4cbee9 CH |
8337 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
8338 | struct btrfs_io_bio *io_bio, blk_status_t err) | |
8b110e39 MX |
8339 | { |
8340 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8341 | ||
8342 | if (skip_csum) { | |
8343 | if (unlikely(err)) | |
8344 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8345 | else | |
58efbc9f | 8346 | return BLK_STS_OK; |
8b110e39 MX |
8347 | } else { |
8348 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8349 | } | |
8350 | } | |
8351 | ||
4246a0b6 | 8352 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8353 | { |
8354 | struct btrfs_dio_private *dip = bio->bi_private; | |
8355 | struct inode *inode = dip->inode; | |
8356 | struct bio *dio_bio; | |
8357 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4e4cbee9 | 8358 | blk_status_t err = bio->bi_status; |
c1dc0896 | 8359 | |
e3d37fab | 8360 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) { |
8b110e39 | 8361 | err = btrfs_subio_endio_read(inode, io_bio, err); |
e3d37fab | 8362 | if (!err) |
8c27cb35 | 8363 | bio->bi_status = 0; |
e3d37fab | 8364 | } |
c1dc0896 | 8365 | |
4b46fce2 | 8366 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8367 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8368 | dio_bio = dip->dio_bio; |
4b46fce2 | 8369 | |
4b46fce2 | 8370 | kfree(dip); |
c0da7aa1 | 8371 | |
4e4cbee9 | 8372 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8373 | dio_end_io(dio_bio); |
23ea8e5a MX |
8374 | |
8375 | if (io_bio->end_io) | |
4e4cbee9 | 8376 | io_bio->end_io(io_bio, blk_status_to_errno(err)); |
9be3395b | 8377 | bio_put(bio); |
4b46fce2 JB |
8378 | } |
8379 | ||
52427260 QW |
8380 | static void __endio_write_update_ordered(struct inode *inode, |
8381 | const u64 offset, const u64 bytes, | |
8382 | const bool uptodate) | |
4b46fce2 | 8383 | { |
0b246afa | 8384 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8385 | struct btrfs_ordered_extent *ordered = NULL; |
52427260 QW |
8386 | struct btrfs_workqueue *wq; |
8387 | btrfs_work_func_t func; | |
14543774 FM |
8388 | u64 ordered_offset = offset; |
8389 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8390 | int ret; |
8391 | ||
52427260 QW |
8392 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
8393 | wq = fs_info->endio_freespace_worker; | |
8394 | func = btrfs_freespace_write_helper; | |
8395 | } else { | |
8396 | wq = fs_info->endio_write_workers; | |
8397 | func = btrfs_endio_write_helper; | |
8398 | } | |
8399 | ||
163cf09c CM |
8400 | again: |
8401 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8402 | &ordered_offset, | |
4246a0b6 | 8403 | ordered_bytes, |
14543774 | 8404 | uptodate); |
4b46fce2 | 8405 | if (!ret) |
163cf09c | 8406 | goto out_test; |
4b46fce2 | 8407 | |
52427260 QW |
8408 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
8409 | btrfs_queue_work(wq, &ordered->work); | |
163cf09c CM |
8410 | out_test: |
8411 | /* | |
8412 | * our bio might span multiple ordered extents. If we haven't | |
8413 | * completed the accounting for the whole dio, go back and try again | |
8414 | */ | |
14543774 FM |
8415 | if (ordered_offset < offset + bytes) { |
8416 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8417 | ordered = NULL; |
163cf09c CM |
8418 | goto again; |
8419 | } | |
14543774 FM |
8420 | } |
8421 | ||
8422 | static void btrfs_endio_direct_write(struct bio *bio) | |
8423 | { | |
8424 | struct btrfs_dio_private *dip = bio->bi_private; | |
8425 | struct bio *dio_bio = dip->dio_bio; | |
8426 | ||
52427260 | 8427 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
4e4cbee9 | 8428 | dip->bytes, !bio->bi_status); |
4b46fce2 | 8429 | |
4b46fce2 | 8430 | kfree(dip); |
c0da7aa1 | 8431 | |
4e4cbee9 | 8432 | dio_bio->bi_status = bio->bi_status; |
4055351c | 8433 | dio_end_io(dio_bio); |
9be3395b | 8434 | bio_put(bio); |
4b46fce2 JB |
8435 | } |
8436 | ||
8c27cb35 | 8437 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, |
eaf25d93 CM |
8438 | struct bio *bio, int mirror_num, |
8439 | unsigned long bio_flags, u64 offset) | |
8440 | { | |
c6100a4b | 8441 | struct inode *inode = private_data; |
4e4cbee9 | 8442 | blk_status_t ret; |
2ff7e61e | 8443 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8444 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8445 | return 0; |
8446 | } | |
8447 | ||
4246a0b6 | 8448 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8449 | { |
8450 | struct btrfs_dio_private *dip = bio->bi_private; | |
4e4cbee9 | 8451 | blk_status_t err = bio->bi_status; |
e65e1535 | 8452 | |
8b110e39 MX |
8453 | if (err) |
8454 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8455 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
f85b7379 DS |
8456 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
8457 | bio->bi_opf, | |
8b110e39 MX |
8458 | (unsigned long long)bio->bi_iter.bi_sector, |
8459 | bio->bi_iter.bi_size, err); | |
8460 | ||
8461 | if (dip->subio_endio) | |
8462 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8463 | |
8464 | if (err) { | |
e65e1535 MX |
8465 | dip->errors = 1; |
8466 | ||
8467 | /* | |
8468 | * before atomic variable goto zero, we must make sure | |
8469 | * dip->errors is perceived to be set. | |
8470 | */ | |
4e857c58 | 8471 | smp_mb__before_atomic(); |
e65e1535 MX |
8472 | } |
8473 | ||
8474 | /* if there are more bios still pending for this dio, just exit */ | |
8475 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8476 | goto out; | |
8477 | ||
9be3395b | 8478 | if (dip->errors) { |
e65e1535 | 8479 | bio_io_error(dip->orig_bio); |
9be3395b | 8480 | } else { |
4e4cbee9 | 8481 | dip->dio_bio->bi_status = 0; |
4246a0b6 | 8482 | bio_endio(dip->orig_bio); |
e65e1535 MX |
8483 | } |
8484 | out: | |
8485 | bio_put(bio); | |
8486 | } | |
8487 | ||
4e4cbee9 | 8488 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8489 | struct btrfs_dio_private *dip, |
8490 | struct bio *bio, | |
8491 | u64 file_offset) | |
8492 | { | |
8493 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8494 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
4e4cbee9 | 8495 | blk_status_t ret; |
c1dc0896 MX |
8496 | |
8497 | /* | |
8498 | * We load all the csum data we need when we submit | |
8499 | * the first bio to reduce the csum tree search and | |
8500 | * contention. | |
8501 | */ | |
8502 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8503 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8504 | file_offset); |
8505 | if (ret) | |
8506 | return ret; | |
8507 | } | |
8508 | ||
8509 | if (bio == dip->orig_bio) | |
8510 | return 0; | |
8511 | ||
8512 | file_offset -= dip->logical_offset; | |
8513 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8514 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8515 | ||
8516 | return 0; | |
8517 | } | |
8518 | ||
58efbc9f OS |
8519 | static inline blk_status_t |
8520 | __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset, | |
66ba772e | 8521 | int async_submit) |
e65e1535 | 8522 | { |
0b246afa | 8523 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8524 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8525 | bool write = bio_op(bio) == REQ_OP_WRITE; |
4e4cbee9 | 8526 | blk_status_t ret; |
e65e1535 | 8527 | |
b812ce28 JB |
8528 | if (async_submit) |
8529 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8530 | ||
e65e1535 | 8531 | bio_get(bio); |
5fd02043 JB |
8532 | |
8533 | if (!write) { | |
0b246afa | 8534 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8535 | if (ret) |
8536 | goto err; | |
8537 | } | |
e65e1535 | 8538 | |
e6961cac | 8539 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
1ae39938 JB |
8540 | goto map; |
8541 | ||
8542 | if (write && async_submit) { | |
c6100a4b JB |
8543 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
8544 | file_offset, inode, | |
0b246afa JM |
8545 | __btrfs_submit_bio_start_direct_io, |
8546 | __btrfs_submit_bio_done); | |
e65e1535 | 8547 | goto err; |
1ae39938 JB |
8548 | } else if (write) { |
8549 | /* | |
8550 | * If we aren't doing async submit, calculate the csum of the | |
8551 | * bio now. | |
8552 | */ | |
2ff7e61e | 8553 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8554 | if (ret) |
8555 | goto err; | |
23ea8e5a | 8556 | } else { |
2ff7e61e | 8557 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8558 | file_offset); |
c2db1073 TI |
8559 | if (ret) |
8560 | goto err; | |
8561 | } | |
1ae39938 | 8562 | map: |
2ff7e61e | 8563 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
e65e1535 MX |
8564 | err: |
8565 | bio_put(bio); | |
8566 | return ret; | |
8567 | } | |
8568 | ||
e6961cac | 8569 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
e65e1535 MX |
8570 | { |
8571 | struct inode *inode = dip->inode; | |
0b246afa | 8572 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 MX |
8573 | struct bio *bio; |
8574 | struct bio *orig_bio = dip->orig_bio; | |
4f024f37 | 8575 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 | 8576 | u64 file_offset = dip->logical_offset; |
e65e1535 | 8577 | u64 map_length; |
1ae39938 | 8578 | int async_submit = 0; |
725130ba LB |
8579 | u64 submit_len; |
8580 | int clone_offset = 0; | |
8581 | int clone_len; | |
5f4dc8fc | 8582 | int ret; |
58efbc9f | 8583 | blk_status_t status; |
e65e1535 | 8584 | |
4f024f37 | 8585 | map_length = orig_bio->bi_iter.bi_size; |
725130ba | 8586 | submit_len = map_length; |
0b246afa JM |
8587 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8588 | &map_length, NULL, 0); | |
7a5c3c9b | 8589 | if (ret) |
e65e1535 | 8590 | return -EIO; |
facc8a22 | 8591 | |
725130ba | 8592 | if (map_length >= submit_len) { |
02f57c7a | 8593 | bio = orig_bio; |
c1dc0896 | 8594 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8595 | goto submit; |
8596 | } | |
8597 | ||
53b381b3 | 8598 | /* async crcs make it difficult to collect full stripe writes. */ |
1b86826d | 8599 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8600 | async_submit = 0; |
8601 | else | |
8602 | async_submit = 1; | |
8603 | ||
725130ba LB |
8604 | /* bio split */ |
8605 | ASSERT(map_length <= INT_MAX); | |
02f57c7a | 8606 | atomic_inc(&dip->pending_bios); |
3c91ee69 | 8607 | do { |
725130ba | 8608 | clone_len = min_t(int, submit_len, map_length); |
02f57c7a | 8609 | |
725130ba LB |
8610 | /* |
8611 | * This will never fail as it's passing GPF_NOFS and | |
8612 | * the allocation is backed by btrfs_bioset. | |
8613 | */ | |
e477094f | 8614 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
725130ba LB |
8615 | clone_len); |
8616 | bio->bi_private = dip; | |
8617 | bio->bi_end_io = btrfs_end_dio_bio; | |
8618 | btrfs_io_bio(bio)->logical = file_offset; | |
8619 | ||
8620 | ASSERT(submit_len >= clone_len); | |
8621 | submit_len -= clone_len; | |
8622 | if (submit_len == 0) | |
8623 | break; | |
e65e1535 | 8624 | |
725130ba LB |
8625 | /* |
8626 | * Increase the count before we submit the bio so we know | |
8627 | * the end IO handler won't happen before we increase the | |
8628 | * count. Otherwise, the dip might get freed before we're | |
8629 | * done setting it up. | |
8630 | */ | |
8631 | atomic_inc(&dip->pending_bios); | |
e65e1535 | 8632 | |
66ba772e | 8633 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, |
58efbc9f OS |
8634 | async_submit); |
8635 | if (status) { | |
725130ba LB |
8636 | bio_put(bio); |
8637 | atomic_dec(&dip->pending_bios); | |
8638 | goto out_err; | |
8639 | } | |
e65e1535 | 8640 | |
725130ba LB |
8641 | clone_offset += clone_len; |
8642 | start_sector += clone_len >> 9; | |
8643 | file_offset += clone_len; | |
5f4dc8fc | 8644 | |
725130ba LB |
8645 | map_length = submit_len; |
8646 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), | |
8647 | start_sector << 9, &map_length, NULL, 0); | |
8648 | if (ret) | |
8649 | goto out_err; | |
3c91ee69 | 8650 | } while (submit_len > 0); |
e65e1535 | 8651 | |
02f57c7a | 8652 | submit: |
66ba772e | 8653 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
58efbc9f | 8654 | if (!status) |
e65e1535 MX |
8655 | return 0; |
8656 | ||
8657 | bio_put(bio); | |
8658 | out_err: | |
8659 | dip->errors = 1; | |
8660 | /* | |
8661 | * before atomic variable goto zero, we must | |
8662 | * make sure dip->errors is perceived to be set. | |
8663 | */ | |
4e857c58 | 8664 | smp_mb__before_atomic(); |
e65e1535 MX |
8665 | if (atomic_dec_and_test(&dip->pending_bios)) |
8666 | bio_io_error(dip->orig_bio); | |
8667 | ||
8668 | /* bio_end_io() will handle error, so we needn't return it */ | |
8669 | return 0; | |
8670 | } | |
8671 | ||
8a4c1e42 MC |
8672 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8673 | loff_t file_offset) | |
4b46fce2 | 8674 | { |
61de718f | 8675 | struct btrfs_dio_private *dip = NULL; |
3892ac90 LB |
8676 | struct bio *bio = NULL; |
8677 | struct btrfs_io_bio *io_bio; | |
8a4c1e42 | 8678 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8679 | int ret = 0; |
8680 | ||
8b6c1d56 | 8681 | bio = btrfs_bio_clone(dio_bio); |
9be3395b | 8682 | |
c1dc0896 | 8683 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8684 | if (!dip) { |
8685 | ret = -ENOMEM; | |
61de718f | 8686 | goto free_ordered; |
4b46fce2 | 8687 | } |
4b46fce2 | 8688 | |
9be3395b | 8689 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8690 | dip->inode = inode; |
8691 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8692 | dip->bytes = dio_bio->bi_iter.bi_size; |
8693 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
3892ac90 LB |
8694 | bio->bi_private = dip; |
8695 | dip->orig_bio = bio; | |
9be3395b | 8696 | dip->dio_bio = dio_bio; |
e65e1535 | 8697 | atomic_set(&dip->pending_bios, 0); |
3892ac90 LB |
8698 | io_bio = btrfs_io_bio(bio); |
8699 | io_bio->logical = file_offset; | |
4b46fce2 | 8700 | |
c1dc0896 | 8701 | if (write) { |
3892ac90 | 8702 | bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8703 | } else { |
3892ac90 | 8704 | bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8705 | dip->subio_endio = btrfs_subio_endio_read; |
8706 | } | |
4b46fce2 | 8707 | |
f28a4928 FM |
8708 | /* |
8709 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8710 | * even if we fail to submit a bio, because in such case we do the | |
8711 | * corresponding error handling below and it must not be done a second | |
8712 | * time by btrfs_direct_IO(). | |
8713 | */ | |
8714 | if (write) { | |
8715 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8716 | ||
8717 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8718 | dip->bytes; | |
8719 | dio_data->unsubmitted_oe_range_start = | |
8720 | dio_data->unsubmitted_oe_range_end; | |
8721 | } | |
8722 | ||
e6961cac | 8723 | ret = btrfs_submit_direct_hook(dip); |
e65e1535 | 8724 | if (!ret) |
eaf25d93 | 8725 | return; |
9be3395b | 8726 | |
3892ac90 LB |
8727 | if (io_bio->end_io) |
8728 | io_bio->end_io(io_bio, ret); | |
9be3395b | 8729 | |
4b46fce2 JB |
8730 | free_ordered: |
8731 | /* | |
61de718f FM |
8732 | * If we arrived here it means either we failed to submit the dip |
8733 | * or we either failed to clone the dio_bio or failed to allocate the | |
8734 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8735 | * call bio_endio against our io_bio so that we get proper resource | |
8736 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8737 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8738 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8739 | */ |
3892ac90 | 8740 | if (bio && dip) { |
054ec2f6 | 8741 | bio_io_error(bio); |
61de718f | 8742 | /* |
3892ac90 | 8743 | * The end io callbacks free our dip, do the final put on bio |
61de718f FM |
8744 | * and all the cleanup and final put for dio_bio (through |
8745 | * dio_end_io()). | |
8746 | */ | |
8747 | dip = NULL; | |
3892ac90 | 8748 | bio = NULL; |
61de718f | 8749 | } else { |
14543774 | 8750 | if (write) |
52427260 | 8751 | __endio_write_update_ordered(inode, |
14543774 FM |
8752 | file_offset, |
8753 | dio_bio->bi_iter.bi_size, | |
52427260 | 8754 | false); |
14543774 | 8755 | else |
61de718f FM |
8756 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8757 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8758 | |
4e4cbee9 | 8759 | dio_bio->bi_status = BLK_STS_IOERR; |
61de718f FM |
8760 | /* |
8761 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8762 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8763 | */ | |
4055351c | 8764 | dio_end_io(dio_bio); |
4b46fce2 | 8765 | } |
3892ac90 LB |
8766 | if (bio) |
8767 | bio_put(bio); | |
61de718f | 8768 | kfree(dip); |
4b46fce2 JB |
8769 | } |
8770 | ||
2ff7e61e JM |
8771 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
8772 | struct kiocb *iocb, | |
8773 | const struct iov_iter *iter, loff_t offset) | |
5a5f79b5 CM |
8774 | { |
8775 | int seg; | |
a1b75f7d | 8776 | int i; |
0b246afa | 8777 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8778 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8779 | |
8780 | if (offset & blocksize_mask) | |
8781 | goto out; | |
8782 | ||
28060d5d AV |
8783 | if (iov_iter_alignment(iter) & blocksize_mask) |
8784 | goto out; | |
a1b75f7d | 8785 | |
28060d5d | 8786 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8787 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8788 | return 0; |
8789 | /* | |
8790 | * Check to make sure we don't have duplicate iov_base's in this | |
8791 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8792 | * when reading back. | |
8793 | */ | |
8794 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8795 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8796 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8797 | goto out; |
8798 | } | |
5a5f79b5 CM |
8799 | } |
8800 | retval = 0; | |
8801 | out: | |
8802 | return retval; | |
8803 | } | |
eb838e73 | 8804 | |
c8b8e32d | 8805 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8806 | { |
4b46fce2 JB |
8807 | struct file *file = iocb->ki_filp; |
8808 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8809 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8810 | struct btrfs_dio_data dio_data = { 0 }; |
364ecf36 | 8811 | struct extent_changeset *data_reserved = NULL; |
c8b8e32d | 8812 | loff_t offset = iocb->ki_pos; |
0934856d | 8813 | size_t count = 0; |
2e60a51e | 8814 | int flags = 0; |
38851cc1 MX |
8815 | bool wakeup = true; |
8816 | bool relock = false; | |
0934856d | 8817 | ssize_t ret; |
4b46fce2 | 8818 | |
2ff7e61e | 8819 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
5a5f79b5 | 8820 | return 0; |
3f7c579c | 8821 | |
fe0f07d0 | 8822 | inode_dio_begin(inode); |
38851cc1 | 8823 | |
0e267c44 | 8824 | /* |
41bd9ca4 MX |
8825 | * The generic stuff only does filemap_write_and_wait_range, which |
8826 | * isn't enough if we've written compressed pages to this area, so | |
8827 | * we need to flush the dirty pages again to make absolutely sure | |
8828 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8829 | */ |
a6cbcd4a | 8830 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8831 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8832 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8833 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8834 | offset + count - 1); | |
0e267c44 | 8835 | |
6f673763 | 8836 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8837 | /* |
8838 | * If the write DIO is beyond the EOF, we need update | |
8839 | * the isize, but it is protected by i_mutex. So we can | |
8840 | * not unlock the i_mutex at this case. | |
8841 | */ | |
8842 | if (offset + count <= inode->i_size) { | |
4aaedfb0 | 8843 | dio_data.overwrite = 1; |
5955102c | 8844 | inode_unlock(inode); |
38851cc1 | 8845 | relock = true; |
edf064e7 GR |
8846 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
8847 | ret = -EAGAIN; | |
8848 | goto out; | |
38851cc1 | 8849 | } |
364ecf36 QW |
8850 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
8851 | offset, count); | |
0934856d | 8852 | if (ret) |
38851cc1 | 8853 | goto out; |
823bb20a | 8854 | dio_data.outstanding_extents = count_max_extents(count); |
e1cbbfa5 JB |
8855 | |
8856 | /* | |
8857 | * We need to know how many extents we reserved so that we can | |
8858 | * do the accounting properly if we go over the number we | |
8859 | * originally calculated. Abuse current->journal_info for this. | |
8860 | */ | |
da17066c | 8861 | dio_data.reserve = round_up(count, |
0b246afa | 8862 | fs_info->sectorsize); |
f28a4928 FM |
8863 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8864 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8865 | current->journal_info = &dio_data; |
97dcdea0 | 8866 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8867 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8868 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8869 | inode_dio_end(inode); |
38851cc1 MX |
8870 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8871 | wakeup = false; | |
0934856d MX |
8872 | } |
8873 | ||
17f8c842 | 8874 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8875 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8876 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8877 | btrfs_submit_direct, flags); |
6f673763 | 8878 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8879 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8880 | current->journal_info = NULL; |
ddba1bfc | 8881 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8882 | if (dio_data.reserve) |
bc42bda2 QW |
8883 | btrfs_delalloc_release_space(inode, data_reserved, |
8884 | offset, dio_data.reserve); | |
f28a4928 FM |
8885 | /* |
8886 | * On error we might have left some ordered extents | |
8887 | * without submitting corresponding bios for them, so | |
8888 | * cleanup them up to avoid other tasks getting them | |
8889 | * and waiting for them to complete forever. | |
8890 | */ | |
8891 | if (dio_data.unsubmitted_oe_range_start < | |
8892 | dio_data.unsubmitted_oe_range_end) | |
52427260 | 8893 | __endio_write_update_ordered(inode, |
f28a4928 FM |
8894 | dio_data.unsubmitted_oe_range_start, |
8895 | dio_data.unsubmitted_oe_range_end - | |
8896 | dio_data.unsubmitted_oe_range_start, | |
52427260 | 8897 | false); |
ddba1bfc | 8898 | } else if (ret >= 0 && (size_t)ret < count) |
bc42bda2 QW |
8899 | btrfs_delalloc_release_space(inode, data_reserved, |
8900 | offset, count - (size_t)ret); | |
0934856d | 8901 | } |
38851cc1 | 8902 | out: |
2e60a51e | 8903 | if (wakeup) |
fe0f07d0 | 8904 | inode_dio_end(inode); |
38851cc1 | 8905 | if (relock) |
5955102c | 8906 | inode_lock(inode); |
0934856d | 8907 | |
364ecf36 | 8908 | extent_changeset_free(data_reserved); |
0934856d | 8909 | return ret; |
16432985 CM |
8910 | } |
8911 | ||
05dadc09 TI |
8912 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8913 | ||
1506fcc8 YS |
8914 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8915 | __u64 start, __u64 len) | |
8916 | { | |
05dadc09 TI |
8917 | int ret; |
8918 | ||
8919 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8920 | if (ret) | |
8921 | return ret; | |
8922 | ||
ec29ed5b | 8923 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8924 | } |
8925 | ||
a52d9a80 | 8926 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8927 | { |
d1310b2e CM |
8928 | struct extent_io_tree *tree; |
8929 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8930 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8931 | } |
1832a6d5 | 8932 | |
a52d9a80 | 8933 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8934 | { |
d1310b2e | 8935 | struct extent_io_tree *tree; |
be7bd730 JB |
8936 | struct inode *inode = page->mapping->host; |
8937 | int ret; | |
b888db2b CM |
8938 | |
8939 | if (current->flags & PF_MEMALLOC) { | |
8940 | redirty_page_for_writepage(wbc, page); | |
8941 | unlock_page(page); | |
8942 | return 0; | |
8943 | } | |
be7bd730 JB |
8944 | |
8945 | /* | |
8946 | * If we are under memory pressure we will call this directly from the | |
8947 | * VM, we need to make sure we have the inode referenced for the ordered | |
8948 | * extent. If not just return like we didn't do anything. | |
8949 | */ | |
8950 | if (!igrab(inode)) { | |
8951 | redirty_page_for_writepage(wbc, page); | |
8952 | return AOP_WRITEPAGE_ACTIVATE; | |
8953 | } | |
d1310b2e | 8954 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8955 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8956 | btrfs_add_delayed_iput(inode); | |
8957 | return ret; | |
9ebefb18 CM |
8958 | } |
8959 | ||
48a3b636 ES |
8960 | static int btrfs_writepages(struct address_space *mapping, |
8961 | struct writeback_control *wbc) | |
b293f02e | 8962 | { |
d1310b2e | 8963 | struct extent_io_tree *tree; |
771ed689 | 8964 | |
d1310b2e | 8965 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8966 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8967 | } | |
8968 | ||
3ab2fb5a CM |
8969 | static int |
8970 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8971 | struct list_head *pages, unsigned nr_pages) | |
8972 | { | |
d1310b2e CM |
8973 | struct extent_io_tree *tree; |
8974 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8975 | return extent_readpages(tree, mapping, pages, nr_pages, |
8976 | btrfs_get_extent); | |
8977 | } | |
e6dcd2dc | 8978 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8979 | { |
d1310b2e CM |
8980 | struct extent_io_tree *tree; |
8981 | struct extent_map_tree *map; | |
a52d9a80 | 8982 | int ret; |
8c2383c3 | 8983 | |
d1310b2e CM |
8984 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8985 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8986 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8987 | if (ret == 1) { |
8988 | ClearPagePrivate(page); | |
8989 | set_page_private(page, 0); | |
09cbfeaf | 8990 | put_page(page); |
39279cc3 | 8991 | } |
a52d9a80 | 8992 | return ret; |
39279cc3 CM |
8993 | } |
8994 | ||
e6dcd2dc CM |
8995 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8996 | { | |
98509cfc CM |
8997 | if (PageWriteback(page) || PageDirty(page)) |
8998 | return 0; | |
3ba7ab22 | 8999 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
9000 | } |
9001 | ||
d47992f8 LC |
9002 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
9003 | unsigned int length) | |
39279cc3 | 9004 | { |
5fd02043 | 9005 | struct inode *inode = page->mapping->host; |
d1310b2e | 9006 | struct extent_io_tree *tree; |
e6dcd2dc | 9007 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 9008 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 9009 | u64 page_start = page_offset(page); |
09cbfeaf | 9010 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
9011 | u64 start; |
9012 | u64 end; | |
131e404a | 9013 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 9014 | |
8b62b72b CM |
9015 | /* |
9016 | * we have the page locked, so new writeback can't start, | |
9017 | * and the dirty bit won't be cleared while we are here. | |
9018 | * | |
9019 | * Wait for IO on this page so that we can safely clear | |
9020 | * the PagePrivate2 bit and do ordered accounting | |
9021 | */ | |
e6dcd2dc | 9022 | wait_on_page_writeback(page); |
8b62b72b | 9023 | |
5fd02043 | 9024 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
9025 | if (offset) { |
9026 | btrfs_releasepage(page, GFP_NOFS); | |
9027 | return; | |
9028 | } | |
131e404a FDBM |
9029 | |
9030 | if (!inode_evicting) | |
ff13db41 | 9031 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
9032 | again: |
9033 | start = page_start; | |
a776c6fa | 9034 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
dbfdb6d1 | 9035 | page_end - start + 1); |
e6dcd2dc | 9036 | if (ordered) { |
dbfdb6d1 | 9037 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
9038 | /* |
9039 | * IO on this page will never be started, so we need | |
9040 | * to account for any ordered extents now | |
9041 | */ | |
131e404a | 9042 | if (!inode_evicting) |
dbfdb6d1 | 9043 | clear_extent_bit(tree, start, end, |
131e404a | 9044 | EXTENT_DIRTY | EXTENT_DELALLOC | |
a7e3b975 | 9045 | EXTENT_DELALLOC_NEW | |
131e404a FDBM |
9046 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
9047 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
9048 | GFP_NOFS); | |
8b62b72b CM |
9049 | /* |
9050 | * whoever cleared the private bit is responsible | |
9051 | * for the finish_ordered_io | |
9052 | */ | |
77cef2ec JB |
9053 | if (TestClearPagePrivate2(page)) { |
9054 | struct btrfs_ordered_inode_tree *tree; | |
9055 | u64 new_len; | |
9056 | ||
9057 | tree = &BTRFS_I(inode)->ordered_tree; | |
9058 | ||
9059 | spin_lock_irq(&tree->lock); | |
9060 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 9061 | new_len = start - ordered->file_offset; |
77cef2ec JB |
9062 | if (new_len < ordered->truncated_len) |
9063 | ordered->truncated_len = new_len; | |
9064 | spin_unlock_irq(&tree->lock); | |
9065 | ||
9066 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
9067 | start, |
9068 | end - start + 1, 1)) | |
77cef2ec | 9069 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 9070 | } |
e6dcd2dc | 9071 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
9072 | if (!inode_evicting) { |
9073 | cached_state = NULL; | |
dbfdb6d1 | 9074 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
9075 | &cached_state); |
9076 | } | |
dbfdb6d1 CR |
9077 | |
9078 | start = end + 1; | |
9079 | if (start < page_end) | |
9080 | goto again; | |
131e404a FDBM |
9081 | } |
9082 | ||
b9d0b389 QW |
9083 | /* |
9084 | * Qgroup reserved space handler | |
9085 | * Page here will be either | |
9086 | * 1) Already written to disk | |
9087 | * In this case, its reserved space is released from data rsv map | |
9088 | * and will be freed by delayed_ref handler finally. | |
9089 | * So even we call qgroup_free_data(), it won't decrease reserved | |
9090 | * space. | |
9091 | * 2) Not written to disk | |
0b34c261 GR |
9092 | * This means the reserved space should be freed here. However, |
9093 | * if a truncate invalidates the page (by clearing PageDirty) | |
9094 | * and the page is accounted for while allocating extent | |
9095 | * in btrfs_check_data_free_space() we let delayed_ref to | |
9096 | * free the entire extent. | |
b9d0b389 | 9097 | */ |
0b34c261 | 9098 | if (PageDirty(page)) |
bc42bda2 | 9099 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
131e404a FDBM |
9100 | if (!inode_evicting) { |
9101 | clear_extent_bit(tree, page_start, page_end, | |
9102 | EXTENT_LOCKED | EXTENT_DIRTY | | |
a7e3b975 FM |
9103 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
9104 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, | |
131e404a FDBM |
9105 | &cached_state, GFP_NOFS); |
9106 | ||
9107 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 9108 | } |
e6dcd2dc | 9109 | |
4a096752 | 9110 | ClearPageChecked(page); |
9ad6b7bc | 9111 | if (PagePrivate(page)) { |
9ad6b7bc CM |
9112 | ClearPagePrivate(page); |
9113 | set_page_private(page, 0); | |
09cbfeaf | 9114 | put_page(page); |
9ad6b7bc | 9115 | } |
39279cc3 CM |
9116 | } |
9117 | ||
9ebefb18 CM |
9118 | /* |
9119 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
9120 | * called from a page fault handler when a page is first dirtied. Hence we must | |
9121 | * be careful to check for EOF conditions here. We set the page up correctly | |
9122 | * for a written page which means we get ENOSPC checking when writing into | |
9123 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
9124 | * support these features. | |
9125 | * | |
9126 | * We are not allowed to take the i_mutex here so we have to play games to | |
9127 | * protect against truncate races as the page could now be beyond EOF. Because | |
9128 | * vmtruncate() writes the inode size before removing pages, once we have the | |
9129 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
9130 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
9131 | * unlock the page. | |
9132 | */ | |
11bac800 | 9133 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
9ebefb18 | 9134 | { |
c2ec175c | 9135 | struct page *page = vmf->page; |
11bac800 | 9136 | struct inode *inode = file_inode(vmf->vma->vm_file); |
0b246afa | 9137 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
9138 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
9139 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 9140 | struct extent_state *cached_state = NULL; |
364ecf36 | 9141 | struct extent_changeset *data_reserved = NULL; |
e6dcd2dc CM |
9142 | char *kaddr; |
9143 | unsigned long zero_start; | |
9ebefb18 | 9144 | loff_t size; |
1832a6d5 | 9145 | int ret; |
9998eb70 | 9146 | int reserved = 0; |
d0b7da88 | 9147 | u64 reserved_space; |
a52d9a80 | 9148 | u64 page_start; |
e6dcd2dc | 9149 | u64 page_end; |
d0b7da88 CR |
9150 | u64 end; |
9151 | ||
09cbfeaf | 9152 | reserved_space = PAGE_SIZE; |
9ebefb18 | 9153 | |
b2b5ef5c | 9154 | sb_start_pagefault(inode->i_sb); |
df480633 | 9155 | page_start = page_offset(page); |
09cbfeaf | 9156 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 9157 | end = page_end; |
df480633 | 9158 | |
d0b7da88 CR |
9159 | /* |
9160 | * Reserving delalloc space after obtaining the page lock can lead to | |
9161 | * deadlock. For example, if a dirty page is locked by this function | |
9162 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
9163 | * dirty page write out, then the btrfs_writepage() function could | |
9164 | * end up waiting indefinitely to get a lock on the page currently | |
9165 | * being processed by btrfs_page_mkwrite() function. | |
9166 | */ | |
364ecf36 | 9167 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
d0b7da88 | 9168 | reserved_space); |
9998eb70 | 9169 | if (!ret) { |
11bac800 | 9170 | ret = file_update_time(vmf->vma->vm_file); |
9998eb70 CM |
9171 | reserved = 1; |
9172 | } | |
56a76f82 NP |
9173 | if (ret) { |
9174 | if (ret == -ENOMEM) | |
9175 | ret = VM_FAULT_OOM; | |
9176 | else /* -ENOSPC, -EIO, etc */ | |
9177 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
9178 | if (reserved) |
9179 | goto out; | |
9180 | goto out_noreserve; | |
56a76f82 | 9181 | } |
1832a6d5 | 9182 | |
56a76f82 | 9183 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9184 | again: |
9ebefb18 | 9185 | lock_page(page); |
9ebefb18 | 9186 | size = i_size_read(inode); |
a52d9a80 | 9187 | |
9ebefb18 | 9188 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9189 | (page_start >= size)) { |
9ebefb18 CM |
9190 | /* page got truncated out from underneath us */ |
9191 | goto out_unlock; | |
9192 | } | |
e6dcd2dc CM |
9193 | wait_on_page_writeback(page); |
9194 | ||
ff13db41 | 9195 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9196 | set_page_extent_mapped(page); |
9197 | ||
eb84ae03 CM |
9198 | /* |
9199 | * we can't set the delalloc bits if there are pending ordered | |
9200 | * extents. Drop our locks and wait for them to finish | |
9201 | */ | |
a776c6fa NB |
9202 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
9203 | PAGE_SIZE); | |
e6dcd2dc | 9204 | if (ordered) { |
2ac55d41 JB |
9205 | unlock_extent_cached(io_tree, page_start, page_end, |
9206 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9207 | unlock_page(page); |
eb84ae03 | 9208 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9209 | btrfs_put_ordered_extent(ordered); |
9210 | goto again; | |
9211 | } | |
9212 | ||
09cbfeaf | 9213 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9214 | reserved_space = round_up(size - page_start, |
0b246afa | 9215 | fs_info->sectorsize); |
09cbfeaf | 9216 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
9217 | end = page_start + reserved_space - 1; |
9218 | spin_lock(&BTRFS_I(inode)->lock); | |
9219 | BTRFS_I(inode)->outstanding_extents++; | |
9220 | spin_unlock(&BTRFS_I(inode)->lock); | |
bc42bda2 QW |
9221 | btrfs_delalloc_release_space(inode, data_reserved, |
9222 | page_start, PAGE_SIZE - reserved_space); | |
d0b7da88 CR |
9223 | } |
9224 | } | |
9225 | ||
fbf19087 | 9226 | /* |
5416034f LB |
9227 | * page_mkwrite gets called when the page is firstly dirtied after it's |
9228 | * faulted in, but write(2) could also dirty a page and set delalloc | |
9229 | * bits, thus in this case for space account reason, we still need to | |
9230 | * clear any delalloc bits within this page range since we have to | |
9231 | * reserve data&meta space before lock_page() (see above comments). | |
fbf19087 | 9232 | */ |
d0b7da88 | 9233 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9234 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9235 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9236 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9237 | |
d0b7da88 | 9238 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9239 | &cached_state, 0); |
9ed74f2d | 9240 | if (ret) { |
2ac55d41 JB |
9241 | unlock_extent_cached(io_tree, page_start, page_end, |
9242 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9243 | ret = VM_FAULT_SIGBUS; |
9244 | goto out_unlock; | |
9245 | } | |
e6dcd2dc | 9246 | ret = 0; |
9ebefb18 CM |
9247 | |
9248 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9249 | if (page_start + PAGE_SIZE > size) |
9250 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9251 | else |
09cbfeaf | 9252 | zero_start = PAGE_SIZE; |
9ebefb18 | 9253 | |
09cbfeaf | 9254 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9255 | kaddr = kmap(page); |
09cbfeaf | 9256 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9257 | flush_dcache_page(page); |
9258 | kunmap(page); | |
9259 | } | |
247e743c | 9260 | ClearPageChecked(page); |
e6dcd2dc | 9261 | set_page_dirty(page); |
50a9b214 | 9262 | SetPageUptodate(page); |
5a3f23d5 | 9263 | |
0b246afa | 9264 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9265 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9266 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9267 | |
2ac55d41 | 9268 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9269 | |
9270 | out_unlock: | |
b2b5ef5c JK |
9271 | if (!ret) { |
9272 | sb_end_pagefault(inode->i_sb); | |
364ecf36 | 9273 | extent_changeset_free(data_reserved); |
50a9b214 | 9274 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9275 | } |
9ebefb18 | 9276 | unlock_page(page); |
1832a6d5 | 9277 | out: |
bc42bda2 QW |
9278 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
9279 | reserved_space); | |
9998eb70 | 9280 | out_noreserve: |
b2b5ef5c | 9281 | sb_end_pagefault(inode->i_sb); |
364ecf36 | 9282 | extent_changeset_free(data_reserved); |
9ebefb18 CM |
9283 | return ret; |
9284 | } | |
9285 | ||
a41ad394 | 9286 | static int btrfs_truncate(struct inode *inode) |
39279cc3 | 9287 | { |
0b246afa | 9288 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9289 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9290 | struct btrfs_block_rsv *rsv; |
a71754fc | 9291 | int ret = 0; |
3893e33b | 9292 | int err = 0; |
39279cc3 | 9293 | struct btrfs_trans_handle *trans; |
0b246afa JM |
9294 | u64 mask = fs_info->sectorsize - 1; |
9295 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 9296 | |
0ef8b726 JB |
9297 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9298 | (u64)-1); | |
9299 | if (ret) | |
9300 | return ret; | |
39279cc3 | 9301 | |
fcb80c2a | 9302 | /* |
01327610 | 9303 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9304 | * 3 things going on here |
9305 | * | |
9306 | * 1) We need to reserve space for our orphan item and the space to | |
9307 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9308 | * orphan item because we didn't reserve space to remove it. | |
9309 | * | |
9310 | * 2) We need to reserve space to update our inode. | |
9311 | * | |
9312 | * 3) We need to have something to cache all the space that is going to | |
9313 | * be free'd up by the truncate operation, but also have some slack | |
9314 | * space reserved in case it uses space during the truncate (thank you | |
9315 | * very much snapshotting). | |
9316 | * | |
01327610 | 9317 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9318 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9319 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9320 | * doesn't end up using space reserved for updating the inode or |
9321 | * removing the orphan item. We also need to be able to stop the | |
9322 | * transaction and start a new one, which means we need to be able to | |
9323 | * update the inode several times, and we have no idea of knowing how | |
9324 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9325 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9326 | * Then there is the orphan item, which does indeed need to be held on |
9327 | * to for the whole operation, and we need nobody to touch this reserved | |
9328 | * space except the orphan code. | |
9329 | * | |
9330 | * So that leaves us with | |
9331 | * | |
9332 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9333 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9334 | * transaction reservation. | |
9335 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9336 | * updating the inode. | |
9337 | */ | |
2ff7e61e | 9338 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9339 | if (!rsv) |
9340 | return -ENOMEM; | |
4a338542 | 9341 | rsv->size = min_size; |
ca7e70f5 | 9342 | rsv->failfast = 1; |
f0cd846e | 9343 | |
907cbceb | 9344 | /* |
07127184 | 9345 | * 1 for the truncate slack space |
907cbceb JB |
9346 | * 1 for updating the inode. |
9347 | */ | |
f3fe820c | 9348 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9349 | if (IS_ERR(trans)) { |
9350 | err = PTR_ERR(trans); | |
9351 | goto out; | |
9352 | } | |
f0cd846e | 9353 | |
907cbceb | 9354 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9355 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
25d609f8 | 9356 | min_size, 0); |
fcb80c2a | 9357 | BUG_ON(ret); |
f0cd846e | 9358 | |
5dc562c5 JB |
9359 | /* |
9360 | * So if we truncate and then write and fsync we normally would just | |
9361 | * write the extents that changed, which is a problem if we need to | |
9362 | * first truncate that entire inode. So set this flag so we write out | |
9363 | * all of the extents in the inode to the sync log so we're completely | |
9364 | * safe. | |
9365 | */ | |
9366 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9367 | trans->block_rsv = rsv; |
907cbceb | 9368 | |
8082510e YZ |
9369 | while (1) { |
9370 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9371 | inode->i_size, | |
9372 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9373 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9374 | err = ret; |
8082510e | 9375 | break; |
3893e33b | 9376 | } |
39279cc3 | 9377 | |
0b246afa | 9378 | trans->block_rsv = &fs_info->trans_block_rsv; |
8082510e | 9379 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9380 | if (ret) { |
9381 | err = ret; | |
9382 | break; | |
9383 | } | |
ca7e70f5 | 9384 | |
3a45bb20 | 9385 | btrfs_end_transaction(trans); |
2ff7e61e | 9386 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9387 | |
9388 | trans = btrfs_start_transaction(root, 2); | |
9389 | if (IS_ERR(trans)) { | |
9390 | ret = err = PTR_ERR(trans); | |
9391 | trans = NULL; | |
9392 | break; | |
9393 | } | |
9394 | ||
47b5d646 | 9395 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9396 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
25d609f8 | 9397 | rsv, min_size, 0); |
ca7e70f5 JB |
9398 | BUG_ON(ret); /* shouldn't happen */ |
9399 | trans->block_rsv = rsv; | |
8082510e YZ |
9400 | } |
9401 | ||
9402 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9403 | trans->block_rsv = root->orphan_block_rsv; |
3d6ae7bb | 9404 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
3893e33b JB |
9405 | if (ret) |
9406 | err = ret; | |
8082510e YZ |
9407 | } |
9408 | ||
917c16b2 | 9409 | if (trans) { |
0b246afa | 9410 | trans->block_rsv = &fs_info->trans_block_rsv; |
917c16b2 CM |
9411 | ret = btrfs_update_inode(trans, root, inode); |
9412 | if (ret && !err) | |
9413 | err = ret; | |
7b128766 | 9414 | |
3a45bb20 | 9415 | ret = btrfs_end_transaction(trans); |
2ff7e61e | 9416 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9417 | } |
fcb80c2a | 9418 | out: |
2ff7e61e | 9419 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9420 | |
3893e33b JB |
9421 | if (ret && !err) |
9422 | err = ret; | |
a41ad394 | 9423 | |
3893e33b | 9424 | return err; |
39279cc3 CM |
9425 | } |
9426 | ||
d352ac68 CM |
9427 | /* |
9428 | * create a new subvolume directory/inode (helper for the ioctl). | |
9429 | */ | |
d2fb3437 | 9430 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9431 | struct btrfs_root *new_root, |
9432 | struct btrfs_root *parent_root, | |
9433 | u64 new_dirid) | |
39279cc3 | 9434 | { |
39279cc3 | 9435 | struct inode *inode; |
76dda93c | 9436 | int err; |
00e4e6b3 | 9437 | u64 index = 0; |
39279cc3 | 9438 | |
12fc9d09 FA |
9439 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9440 | new_dirid, new_dirid, | |
9441 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9442 | &index); | |
54aa1f4d | 9443 | if (IS_ERR(inode)) |
f46b5a66 | 9444 | return PTR_ERR(inode); |
39279cc3 CM |
9445 | inode->i_op = &btrfs_dir_inode_operations; |
9446 | inode->i_fop = &btrfs_dir_file_operations; | |
9447 | ||
bfe86848 | 9448 | set_nlink(inode, 1); |
6ef06d27 | 9449 | btrfs_i_size_write(BTRFS_I(inode), 0); |
b0d5d10f | 9450 | unlock_new_inode(inode); |
3b96362c | 9451 | |
63541927 FDBM |
9452 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9453 | if (err) | |
9454 | btrfs_err(new_root->fs_info, | |
351fd353 | 9455 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9456 | new_root->root_key.objectid, err); |
9457 | ||
76dda93c | 9458 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9459 | |
76dda93c | 9460 | iput(inode); |
ce598979 | 9461 | return err; |
39279cc3 CM |
9462 | } |
9463 | ||
39279cc3 CM |
9464 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9465 | { | |
9466 | struct btrfs_inode *ei; | |
2ead6ae7 | 9467 | struct inode *inode; |
39279cc3 CM |
9468 | |
9469 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9470 | if (!ei) | |
9471 | return NULL; | |
2ead6ae7 YZ |
9472 | |
9473 | ei->root = NULL; | |
2ead6ae7 | 9474 | ei->generation = 0; |
15ee9bc7 | 9475 | ei->last_trans = 0; |
257c62e1 | 9476 | ei->last_sub_trans = 0; |
e02119d5 | 9477 | ei->logged_trans = 0; |
2ead6ae7 | 9478 | ei->delalloc_bytes = 0; |
a7e3b975 | 9479 | ei->new_delalloc_bytes = 0; |
47059d93 | 9480 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9481 | ei->disk_i_size = 0; |
9482 | ei->flags = 0; | |
7709cde3 | 9483 | ei->csum_bytes = 0; |
2ead6ae7 | 9484 | ei->index_cnt = (u64)-1; |
67de1176 | 9485 | ei->dir_index = 0; |
2ead6ae7 | 9486 | ei->last_unlink_trans = 0; |
46d8bc34 | 9487 | ei->last_log_commit = 0; |
8089fe62 | 9488 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9489 | |
9e0baf60 JB |
9490 | spin_lock_init(&ei->lock); |
9491 | ei->outstanding_extents = 0; | |
9492 | ei->reserved_extents = 0; | |
2ead6ae7 | 9493 | |
72ac3c0d | 9494 | ei->runtime_flags = 0; |
b52aa8c9 | 9495 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
eec63c65 | 9496 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9497 | |
16cdcec7 MX |
9498 | ei->delayed_node = NULL; |
9499 | ||
9cc97d64 | 9500 | ei->i_otime.tv_sec = 0; |
9501 | ei->i_otime.tv_nsec = 0; | |
9502 | ||
2ead6ae7 | 9503 | inode = &ei->vfs_inode; |
a8067e02 | 9504 | extent_map_tree_init(&ei->extent_tree); |
c6100a4b JB |
9505 | extent_io_tree_init(&ei->io_tree, inode); |
9506 | extent_io_tree_init(&ei->io_failure_tree, inode); | |
0b32f4bb JB |
9507 | ei->io_tree.track_uptodate = 1; |
9508 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9509 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9510 | mutex_init(&ei->log_mutex); |
f248679e | 9511 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9512 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9513 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9514 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9515 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9516 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9517 | |
9518 | return inode; | |
39279cc3 CM |
9519 | } |
9520 | ||
aaedb55b JB |
9521 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9522 | void btrfs_test_destroy_inode(struct inode *inode) | |
9523 | { | |
dcdbc059 | 9524 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
aaedb55b JB |
9525 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9526 | } | |
9527 | #endif | |
9528 | ||
fa0d7e3d NP |
9529 | static void btrfs_i_callback(struct rcu_head *head) |
9530 | { | |
9531 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9532 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9533 | } | |
9534 | ||
39279cc3 CM |
9535 | void btrfs_destroy_inode(struct inode *inode) |
9536 | { | |
0b246afa | 9537 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9538 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9539 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9540 | ||
b3d9b7a3 | 9541 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9542 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9543 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9544 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 | 9545 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
a7e3b975 | 9546 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
7709cde3 | 9547 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
47059d93 | 9548 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9549 | |
a6dbd429 JB |
9550 | /* |
9551 | * This can happen where we create an inode, but somebody else also | |
9552 | * created the same inode and we need to destroy the one we already | |
9553 | * created. | |
9554 | */ | |
9555 | if (!root) | |
9556 | goto free; | |
9557 | ||
8a35d95f JB |
9558 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9559 | &BTRFS_I(inode)->runtime_flags)) { | |
0b246afa | 9560 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
4a0cc7ca | 9561 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 9562 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9563 | } |
7b128766 | 9564 | |
d397712b | 9565 | while (1) { |
e6dcd2dc CM |
9566 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9567 | if (!ordered) | |
9568 | break; | |
9569 | else { | |
0b246afa | 9570 | btrfs_err(fs_info, |
5d163e0e JM |
9571 | "found ordered extent %llu %llu on inode cleanup", |
9572 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9573 | btrfs_remove_ordered_extent(inode, ordered); |
9574 | btrfs_put_ordered_extent(ordered); | |
9575 | btrfs_put_ordered_extent(ordered); | |
9576 | } | |
9577 | } | |
56fa9d07 | 9578 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9579 | inode_tree_del(inode); |
dcdbc059 | 9580 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
a6dbd429 | 9581 | free: |
fa0d7e3d | 9582 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9583 | } |
9584 | ||
45321ac5 | 9585 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9586 | { |
9587 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9588 | |
6379ef9f NA |
9589 | if (root == NULL) |
9590 | return 1; | |
9591 | ||
fa6ac876 | 9592 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9593 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9594 | return 1; |
76dda93c | 9595 | else |
45321ac5 | 9596 | return generic_drop_inode(inode); |
76dda93c YZ |
9597 | } |
9598 | ||
0ee0fda0 | 9599 | static void init_once(void *foo) |
39279cc3 CM |
9600 | { |
9601 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9602 | ||
9603 | inode_init_once(&ei->vfs_inode); | |
9604 | } | |
9605 | ||
9606 | void btrfs_destroy_cachep(void) | |
9607 | { | |
8c0a8537 KS |
9608 | /* |
9609 | * Make sure all delayed rcu free inodes are flushed before we | |
9610 | * destroy cache. | |
9611 | */ | |
9612 | rcu_barrier(); | |
5598e900 KM |
9613 | kmem_cache_destroy(btrfs_inode_cachep); |
9614 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
5598e900 KM |
9615 | kmem_cache_destroy(btrfs_path_cachep); |
9616 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9617 | } |
9618 | ||
9619 | int btrfs_init_cachep(void) | |
9620 | { | |
837e1972 | 9621 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9622 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9623 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9624 | init_once); | |
39279cc3 CM |
9625 | if (!btrfs_inode_cachep) |
9626 | goto fail; | |
9601e3f6 | 9627 | |
837e1972 | 9628 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9629 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9630 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9631 | if (!btrfs_trans_handle_cachep) |
9632 | goto fail; | |
9601e3f6 | 9633 | |
837e1972 | 9634 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9635 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9636 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9637 | if (!btrfs_path_cachep) |
9638 | goto fail; | |
9601e3f6 | 9639 | |
837e1972 | 9640 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9641 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9642 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9643 | if (!btrfs_free_space_cachep) |
9644 | goto fail; | |
9645 | ||
39279cc3 CM |
9646 | return 0; |
9647 | fail: | |
9648 | btrfs_destroy_cachep(); | |
9649 | return -ENOMEM; | |
9650 | } | |
9651 | ||
a528d35e DH |
9652 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
9653 | u32 request_mask, unsigned int flags) | |
39279cc3 | 9654 | { |
df0af1a5 | 9655 | u64 delalloc_bytes; |
a528d35e | 9656 | struct inode *inode = d_inode(path->dentry); |
fadc0d8b | 9657 | u32 blocksize = inode->i_sb->s_blocksize; |
04a87e34 YS |
9658 | u32 bi_flags = BTRFS_I(inode)->flags; |
9659 | ||
9660 | stat->result_mask |= STATX_BTIME; | |
9661 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; | |
9662 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; | |
9663 | if (bi_flags & BTRFS_INODE_APPEND) | |
9664 | stat->attributes |= STATX_ATTR_APPEND; | |
9665 | if (bi_flags & BTRFS_INODE_COMPRESS) | |
9666 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
9667 | if (bi_flags & BTRFS_INODE_IMMUTABLE) | |
9668 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
9669 | if (bi_flags & BTRFS_INODE_NODUMP) | |
9670 | stat->attributes |= STATX_ATTR_NODUMP; | |
9671 | ||
9672 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
9673 | STATX_ATTR_COMPRESSED | | |
9674 | STATX_ATTR_IMMUTABLE | | |
9675 | STATX_ATTR_NODUMP); | |
fadc0d8b | 9676 | |
39279cc3 | 9677 | generic_fillattr(inode, stat); |
0ee5dc67 | 9678 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9679 | |
9680 | spin_lock(&BTRFS_I(inode)->lock); | |
a7e3b975 | 9681 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
df0af1a5 | 9682 | spin_unlock(&BTRFS_I(inode)->lock); |
fadc0d8b | 9683 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9684 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9685 | return 0; |
9686 | } | |
9687 | ||
cdd1fedf DF |
9688 | static int btrfs_rename_exchange(struct inode *old_dir, |
9689 | struct dentry *old_dentry, | |
9690 | struct inode *new_dir, | |
9691 | struct dentry *new_dentry) | |
9692 | { | |
0b246afa | 9693 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9694 | struct btrfs_trans_handle *trans; |
9695 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9696 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9697 | struct inode *new_inode = new_dentry->d_inode; | |
9698 | struct inode *old_inode = old_dentry->d_inode; | |
c2050a45 | 9699 | struct timespec ctime = current_time(old_inode); |
cdd1fedf | 9700 | struct dentry *parent; |
4a0cc7ca NB |
9701 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9702 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9703 | u64 old_idx = 0; |
9704 | u64 new_idx = 0; | |
9705 | u64 root_objectid; | |
9706 | int ret; | |
86e8aa0e FM |
9707 | bool root_log_pinned = false; |
9708 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9709 | |
9710 | /* we only allow rename subvolume link between subvolumes */ | |
9711 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9712 | return -EXDEV; | |
9713 | ||
9714 | /* close the race window with snapshot create/destroy ioctl */ | |
9715 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9716 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9717 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9718 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9719 | |
9720 | /* | |
9721 | * We want to reserve the absolute worst case amount of items. So if | |
9722 | * both inodes are subvols and we need to unlink them then that would | |
9723 | * require 4 item modifications, but if they are both normal inodes it | |
9724 | * would require 5 item modifications, so we'll assume their normal | |
9725 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9726 | * should cover the worst case number of items we'll modify. | |
9727 | */ | |
9728 | trans = btrfs_start_transaction(root, 12); | |
9729 | if (IS_ERR(trans)) { | |
9730 | ret = PTR_ERR(trans); | |
9731 | goto out_notrans; | |
9732 | } | |
9733 | ||
9734 | /* | |
9735 | * We need to find a free sequence number both in the source and | |
9736 | * in the destination directory for the exchange. | |
9737 | */ | |
877574e2 | 9738 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
cdd1fedf DF |
9739 | if (ret) |
9740 | goto out_fail; | |
877574e2 | 9741 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
cdd1fedf DF |
9742 | if (ret) |
9743 | goto out_fail; | |
9744 | ||
9745 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9746 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9747 | ||
9748 | /* Reference for the source. */ | |
9749 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9750 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9751 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9752 | } else { |
376e5a57 FM |
9753 | btrfs_pin_log_trans(root); |
9754 | root_log_pinned = true; | |
cdd1fedf DF |
9755 | ret = btrfs_insert_inode_ref(trans, dest, |
9756 | new_dentry->d_name.name, | |
9757 | new_dentry->d_name.len, | |
9758 | old_ino, | |
f85b7379 DS |
9759 | btrfs_ino(BTRFS_I(new_dir)), |
9760 | old_idx); | |
cdd1fedf DF |
9761 | if (ret) |
9762 | goto out_fail; | |
cdd1fedf DF |
9763 | } |
9764 | ||
9765 | /* And now for the dest. */ | |
9766 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9767 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9768 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9769 | } else { |
376e5a57 FM |
9770 | btrfs_pin_log_trans(dest); |
9771 | dest_log_pinned = true; | |
cdd1fedf DF |
9772 | ret = btrfs_insert_inode_ref(trans, root, |
9773 | old_dentry->d_name.name, | |
9774 | old_dentry->d_name.len, | |
9775 | new_ino, | |
f85b7379 DS |
9776 | btrfs_ino(BTRFS_I(old_dir)), |
9777 | new_idx); | |
cdd1fedf DF |
9778 | if (ret) |
9779 | goto out_fail; | |
cdd1fedf DF |
9780 | } |
9781 | ||
9782 | /* Update inode version and ctime/mtime. */ | |
9783 | inode_inc_iversion(old_dir); | |
9784 | inode_inc_iversion(new_dir); | |
9785 | inode_inc_iversion(old_inode); | |
9786 | inode_inc_iversion(new_inode); | |
9787 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9788 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9789 | old_inode->i_ctime = ctime; | |
9790 | new_inode->i_ctime = ctime; | |
9791 | ||
9792 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
f85b7379 DS |
9793 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
9794 | BTRFS_I(old_inode), 1); | |
9795 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), | |
9796 | BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9797 | } |
9798 | ||
9799 | /* src is a subvolume */ | |
9800 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9801 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9802 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9803 | root_objectid, | |
9804 | old_dentry->d_name.name, | |
9805 | old_dentry->d_name.len); | |
9806 | } else { /* src is an inode */ | |
4ec5934e NB |
9807 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
9808 | BTRFS_I(old_dentry->d_inode), | |
cdd1fedf DF |
9809 | old_dentry->d_name.name, |
9810 | old_dentry->d_name.len); | |
9811 | if (!ret) | |
9812 | ret = btrfs_update_inode(trans, root, old_inode); | |
9813 | } | |
9814 | if (ret) { | |
66642832 | 9815 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9816 | goto out_fail; |
9817 | } | |
9818 | ||
9819 | /* dest is a subvolume */ | |
9820 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9821 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9822 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9823 | root_objectid, | |
9824 | new_dentry->d_name.name, | |
9825 | new_dentry->d_name.len); | |
9826 | } else { /* dest is an inode */ | |
4ec5934e NB |
9827 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
9828 | BTRFS_I(new_dentry->d_inode), | |
cdd1fedf DF |
9829 | new_dentry->d_name.name, |
9830 | new_dentry->d_name.len); | |
9831 | if (!ret) | |
9832 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9833 | } | |
9834 | if (ret) { | |
66642832 | 9835 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9836 | goto out_fail; |
9837 | } | |
9838 | ||
db0a669f | 9839 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
cdd1fedf DF |
9840 | new_dentry->d_name.name, |
9841 | new_dentry->d_name.len, 0, old_idx); | |
9842 | if (ret) { | |
66642832 | 9843 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9844 | goto out_fail; |
9845 | } | |
9846 | ||
db0a669f | 9847 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
cdd1fedf DF |
9848 | old_dentry->d_name.name, |
9849 | old_dentry->d_name.len, 0, new_idx); | |
9850 | if (ret) { | |
66642832 | 9851 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9852 | goto out_fail; |
9853 | } | |
9854 | ||
9855 | if (old_inode->i_nlink == 1) | |
9856 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9857 | if (new_inode->i_nlink == 1) | |
9858 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9859 | ||
86e8aa0e | 9860 | if (root_log_pinned) { |
cdd1fedf | 9861 | parent = new_dentry->d_parent; |
f85b7379 DS |
9862 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
9863 | parent); | |
cdd1fedf | 9864 | btrfs_end_log_trans(root); |
86e8aa0e | 9865 | root_log_pinned = false; |
cdd1fedf | 9866 | } |
86e8aa0e | 9867 | if (dest_log_pinned) { |
cdd1fedf | 9868 | parent = old_dentry->d_parent; |
f85b7379 DS |
9869 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
9870 | parent); | |
cdd1fedf | 9871 | btrfs_end_log_trans(dest); |
86e8aa0e | 9872 | dest_log_pinned = false; |
cdd1fedf DF |
9873 | } |
9874 | out_fail: | |
86e8aa0e FM |
9875 | /* |
9876 | * If we have pinned a log and an error happened, we unpin tasks | |
9877 | * trying to sync the log and force them to fallback to a transaction | |
9878 | * commit if the log currently contains any of the inodes involved in | |
9879 | * this rename operation (to ensure we do not persist a log with an | |
9880 | * inconsistent state for any of these inodes or leading to any | |
9881 | * inconsistencies when replayed). If the transaction was aborted, the | |
9882 | * abortion reason is propagated to userspace when attempting to commit | |
9883 | * the transaction. If the log does not contain any of these inodes, we | |
9884 | * allow the tasks to sync it. | |
9885 | */ | |
9886 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9887 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9888 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9889 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9890 | (new_inode && |
0f8939b8 | 9891 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9892 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9893 | |
9894 | if (root_log_pinned) { | |
9895 | btrfs_end_log_trans(root); | |
9896 | root_log_pinned = false; | |
9897 | } | |
9898 | if (dest_log_pinned) { | |
9899 | btrfs_end_log_trans(dest); | |
9900 | dest_log_pinned = false; | |
9901 | } | |
9902 | } | |
3a45bb20 | 9903 | ret = btrfs_end_transaction(trans); |
cdd1fedf DF |
9904 | out_notrans: |
9905 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9906 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9907 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9908 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9909 | |
9910 | return ret; | |
9911 | } | |
9912 | ||
9913 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9914 | struct btrfs_root *root, | |
9915 | struct inode *dir, | |
9916 | struct dentry *dentry) | |
9917 | { | |
9918 | int ret; | |
9919 | struct inode *inode; | |
9920 | u64 objectid; | |
9921 | u64 index; | |
9922 | ||
9923 | ret = btrfs_find_free_ino(root, &objectid); | |
9924 | if (ret) | |
9925 | return ret; | |
9926 | ||
9927 | inode = btrfs_new_inode(trans, root, dir, | |
9928 | dentry->d_name.name, | |
9929 | dentry->d_name.len, | |
4a0cc7ca | 9930 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9931 | objectid, |
9932 | S_IFCHR | WHITEOUT_MODE, | |
9933 | &index); | |
9934 | ||
9935 | if (IS_ERR(inode)) { | |
9936 | ret = PTR_ERR(inode); | |
9937 | return ret; | |
9938 | } | |
9939 | ||
9940 | inode->i_op = &btrfs_special_inode_operations; | |
9941 | init_special_inode(inode, inode->i_mode, | |
9942 | WHITEOUT_DEV); | |
9943 | ||
9944 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9945 | &dentry->d_name); | |
9946 | if (ret) | |
c9901618 | 9947 | goto out; |
cdd1fedf | 9948 | |
cef415af NB |
9949 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
9950 | BTRFS_I(inode), 0, index); | |
cdd1fedf | 9951 | if (ret) |
c9901618 | 9952 | goto out; |
cdd1fedf DF |
9953 | |
9954 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9955 | out: |
cdd1fedf | 9956 | unlock_new_inode(inode); |
c9901618 FM |
9957 | if (ret) |
9958 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9959 | iput(inode); |
9960 | ||
c9901618 | 9961 | return ret; |
cdd1fedf DF |
9962 | } |
9963 | ||
d397712b | 9964 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9965 | struct inode *new_dir, struct dentry *new_dentry, |
9966 | unsigned int flags) | |
39279cc3 | 9967 | { |
0b246afa | 9968 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9969 | struct btrfs_trans_handle *trans; |
5062af35 | 9970 | unsigned int trans_num_items; |
39279cc3 | 9971 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9972 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9973 | struct inode *new_inode = d_inode(new_dentry); |
9974 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9975 | u64 index = 0; |
4df27c4d | 9976 | u64 root_objectid; |
39279cc3 | 9977 | int ret; |
4a0cc7ca | 9978 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9979 | bool log_pinned = false; |
39279cc3 | 9980 | |
4a0cc7ca | 9981 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9982 | return -EPERM; |
9983 | ||
4df27c4d | 9984 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9985 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9986 | return -EXDEV; |
9987 | ||
33345d01 | 9988 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9989 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9990 | return -ENOTEMPTY; |
5f39d397 | 9991 | |
4df27c4d YZ |
9992 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9993 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9994 | return -ENOTEMPTY; | |
9c52057c CM |
9995 | |
9996 | ||
9997 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9998 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9999 | new_dentry->d_name.name, |
10000 | new_dentry->d_name.len); | |
10001 | ||
10002 | if (ret) { | |
10003 | if (ret == -EEXIST) { | |
10004 | /* we shouldn't get | |
10005 | * eexist without a new_inode */ | |
fae7f21c | 10006 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
10007 | return ret; |
10008 | } | |
10009 | } else { | |
10010 | /* maybe -EOVERFLOW */ | |
10011 | return ret; | |
10012 | } | |
10013 | } | |
10014 | ret = 0; | |
10015 | ||
5a3f23d5 | 10016 | /* |
8d875f95 CM |
10017 | * we're using rename to replace one file with another. Start IO on it |
10018 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 10019 | */ |
8d875f95 | 10020 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
10021 | filemap_flush(old_inode->i_mapping); |
10022 | ||
76dda93c | 10023 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 10024 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10025 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
10026 | /* |
10027 | * We want to reserve the absolute worst case amount of items. So if | |
10028 | * both inodes are subvols and we need to unlink them then that would | |
10029 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 10030 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
10031 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
10032 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
10033 | * If our rename has the whiteout flag, we need more 5 units for the |
10034 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
10035 | * when selinux is enabled). | |
a22285a6 | 10036 | */ |
5062af35 FM |
10037 | trans_num_items = 11; |
10038 | if (flags & RENAME_WHITEOUT) | |
10039 | trans_num_items += 5; | |
10040 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 10041 | if (IS_ERR(trans)) { |
cdd1fedf DF |
10042 | ret = PTR_ERR(trans); |
10043 | goto out_notrans; | |
10044 | } | |
76dda93c | 10045 | |
4df27c4d YZ |
10046 | if (dest != root) |
10047 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 10048 | |
877574e2 | 10049 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
a5719521 YZ |
10050 | if (ret) |
10051 | goto out_fail; | |
5a3f23d5 | 10052 | |
67de1176 | 10053 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 10054 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 10055 | /* force full log commit if subvolume involved. */ |
0b246afa | 10056 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 10057 | } else { |
c4aba954 FM |
10058 | btrfs_pin_log_trans(root); |
10059 | log_pinned = true; | |
a5719521 YZ |
10060 | ret = btrfs_insert_inode_ref(trans, dest, |
10061 | new_dentry->d_name.name, | |
10062 | new_dentry->d_name.len, | |
33345d01 | 10063 | old_ino, |
4a0cc7ca | 10064 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
10065 | if (ret) |
10066 | goto out_fail; | |
4df27c4d | 10067 | } |
5a3f23d5 | 10068 | |
0c4d2d95 JB |
10069 | inode_inc_iversion(old_dir); |
10070 | inode_inc_iversion(new_dir); | |
10071 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
10072 | old_dir->i_ctime = old_dir->i_mtime = |
10073 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 10074 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 10075 | |
12fcfd22 | 10076 | if (old_dentry->d_parent != new_dentry->d_parent) |
f85b7379 DS |
10077 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
10078 | BTRFS_I(old_inode), 1); | |
12fcfd22 | 10079 | |
33345d01 | 10080 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
10081 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
10082 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
10083 | old_dentry->d_name.name, | |
10084 | old_dentry->d_name.len); | |
10085 | } else { | |
4ec5934e NB |
10086 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
10087 | BTRFS_I(d_inode(old_dentry)), | |
92986796 AV |
10088 | old_dentry->d_name.name, |
10089 | old_dentry->d_name.len); | |
10090 | if (!ret) | |
10091 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 10092 | } |
79787eaa | 10093 | if (ret) { |
66642832 | 10094 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10095 | goto out_fail; |
10096 | } | |
39279cc3 CM |
10097 | |
10098 | if (new_inode) { | |
0c4d2d95 | 10099 | inode_inc_iversion(new_inode); |
c2050a45 | 10100 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 10101 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
10102 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
10103 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
10104 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
10105 | root_objectid, | |
10106 | new_dentry->d_name.name, | |
10107 | new_dentry->d_name.len); | |
10108 | BUG_ON(new_inode->i_nlink == 0); | |
10109 | } else { | |
4ec5934e NB |
10110 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
10111 | BTRFS_I(d_inode(new_dentry)), | |
4df27c4d YZ |
10112 | new_dentry->d_name.name, |
10113 | new_dentry->d_name.len); | |
10114 | } | |
4ef31a45 | 10115 | if (!ret && new_inode->i_nlink == 0) |
73f2e545 NB |
10116 | ret = btrfs_orphan_add(trans, |
10117 | BTRFS_I(d_inode(new_dentry))); | |
79787eaa | 10118 | if (ret) { |
66642832 | 10119 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10120 | goto out_fail; |
10121 | } | |
39279cc3 | 10122 | } |
aec7477b | 10123 | |
db0a669f | 10124 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
4df27c4d | 10125 | new_dentry->d_name.name, |
a5719521 | 10126 | new_dentry->d_name.len, 0, index); |
79787eaa | 10127 | if (ret) { |
66642832 | 10128 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10129 | goto out_fail; |
10130 | } | |
39279cc3 | 10131 | |
67de1176 MX |
10132 | if (old_inode->i_nlink == 1) |
10133 | BTRFS_I(old_inode)->dir_index = index; | |
10134 | ||
3dc9e8f7 | 10135 | if (log_pinned) { |
10d9f309 | 10136 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 10137 | |
f85b7379 DS |
10138 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
10139 | parent); | |
4df27c4d | 10140 | btrfs_end_log_trans(root); |
3dc9e8f7 | 10141 | log_pinned = false; |
4df27c4d | 10142 | } |
cdd1fedf DF |
10143 | |
10144 | if (flags & RENAME_WHITEOUT) { | |
10145 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
10146 | old_dentry); | |
10147 | ||
10148 | if (ret) { | |
66642832 | 10149 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
10150 | goto out_fail; |
10151 | } | |
4df27c4d | 10152 | } |
39279cc3 | 10153 | out_fail: |
3dc9e8f7 FM |
10154 | /* |
10155 | * If we have pinned the log and an error happened, we unpin tasks | |
10156 | * trying to sync the log and force them to fallback to a transaction | |
10157 | * commit if the log currently contains any of the inodes involved in | |
10158 | * this rename operation (to ensure we do not persist a log with an | |
10159 | * inconsistent state for any of these inodes or leading to any | |
10160 | * inconsistencies when replayed). If the transaction was aborted, the | |
10161 | * abortion reason is propagated to userspace when attempting to commit | |
10162 | * the transaction. If the log does not contain any of these inodes, we | |
10163 | * allow the tasks to sync it. | |
10164 | */ | |
10165 | if (ret && log_pinned) { | |
0f8939b8 NB |
10166 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
10167 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
10168 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 10169 | (new_inode && |
0f8939b8 | 10170 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 10171 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
10172 | |
10173 | btrfs_end_log_trans(root); | |
10174 | log_pinned = false; | |
10175 | } | |
3a45bb20 | 10176 | btrfs_end_transaction(trans); |
b44c59a8 | 10177 | out_notrans: |
33345d01 | 10178 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 10179 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 10180 | |
39279cc3 CM |
10181 | return ret; |
10182 | } | |
10183 | ||
80ace85c MS |
10184 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
10185 | struct inode *new_dir, struct dentry *new_dentry, | |
10186 | unsigned int flags) | |
10187 | { | |
cdd1fedf | 10188 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
10189 | return -EINVAL; |
10190 | ||
cdd1fedf DF |
10191 | if (flags & RENAME_EXCHANGE) |
10192 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
10193 | new_dentry); | |
10194 | ||
10195 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
10196 | } |
10197 | ||
8ccf6f19 MX |
10198 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
10199 | { | |
10200 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 10201 | struct inode *inode; |
8ccf6f19 MX |
10202 | |
10203 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10204 | work); | |
9f23e289 | 10205 | inode = delalloc_work->inode; |
30424601 DS |
10206 | filemap_flush(inode->i_mapping); |
10207 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10208 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10209 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10210 | |
10211 | if (delalloc_work->delay_iput) | |
9f23e289 | 10212 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10213 | else |
9f23e289 | 10214 | iput(inode); |
8ccf6f19 MX |
10215 | complete(&delalloc_work->completion); |
10216 | } | |
10217 | ||
10218 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10219 | int delay_iput) |
8ccf6f19 MX |
10220 | { |
10221 | struct btrfs_delalloc_work *work; | |
10222 | ||
100d5702 | 10223 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10224 | if (!work) |
10225 | return NULL; | |
10226 | ||
10227 | init_completion(&work->completion); | |
10228 | INIT_LIST_HEAD(&work->list); | |
10229 | work->inode = inode; | |
8ccf6f19 | 10230 | work->delay_iput = delay_iput; |
9e0af237 LB |
10231 | WARN_ON_ONCE(!inode); |
10232 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10233 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10234 | |
10235 | return work; | |
10236 | } | |
10237 | ||
10238 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10239 | { | |
10240 | wait_for_completion(&work->completion); | |
100d5702 | 10241 | kfree(work); |
8ccf6f19 MX |
10242 | } |
10243 | ||
d352ac68 CM |
10244 | /* |
10245 | * some fairly slow code that needs optimization. This walks the list | |
10246 | * of all the inodes with pending delalloc and forces them to disk. | |
10247 | */ | |
6c255e67 MX |
10248 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10249 | int nr) | |
ea8c2819 | 10250 | { |
ea8c2819 | 10251 | struct btrfs_inode *binode; |
5b21f2ed | 10252 | struct inode *inode; |
8ccf6f19 MX |
10253 | struct btrfs_delalloc_work *work, *next; |
10254 | struct list_head works; | |
1eafa6c7 | 10255 | struct list_head splice; |
8ccf6f19 | 10256 | int ret = 0; |
ea8c2819 | 10257 | |
8ccf6f19 | 10258 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10259 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10260 | |
573bfb72 | 10261 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10262 | spin_lock(&root->delalloc_lock); |
10263 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10264 | while (!list_empty(&splice)) { |
10265 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10266 | delalloc_inodes); |
1eafa6c7 | 10267 | |
eb73c1b7 MX |
10268 | list_move_tail(&binode->delalloc_inodes, |
10269 | &root->delalloc_inodes); | |
5b21f2ed | 10270 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10271 | if (!inode) { |
eb73c1b7 | 10272 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10273 | continue; |
df0af1a5 | 10274 | } |
eb73c1b7 | 10275 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10276 | |
651d494a | 10277 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10278 | if (!work) { |
f4ab9ea7 JB |
10279 | if (delay_iput) |
10280 | btrfs_add_delayed_iput(inode); | |
10281 | else | |
10282 | iput(inode); | |
1eafa6c7 | 10283 | ret = -ENOMEM; |
a1ecaabb | 10284 | goto out; |
5b21f2ed | 10285 | } |
1eafa6c7 | 10286 | list_add_tail(&work->list, &works); |
a44903ab QW |
10287 | btrfs_queue_work(root->fs_info->flush_workers, |
10288 | &work->work); | |
6c255e67 MX |
10289 | ret++; |
10290 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10291 | goto out; |
5b21f2ed | 10292 | cond_resched(); |
eb73c1b7 | 10293 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10294 | } |
eb73c1b7 | 10295 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10296 | |
a1ecaabb | 10297 | out: |
eb73c1b7 MX |
10298 | list_for_each_entry_safe(work, next, &works, list) { |
10299 | list_del_init(&work->list); | |
10300 | btrfs_wait_and_free_delalloc_work(work); | |
10301 | } | |
10302 | ||
10303 | if (!list_empty_careful(&splice)) { | |
10304 | spin_lock(&root->delalloc_lock); | |
10305 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10306 | spin_unlock(&root->delalloc_lock); | |
10307 | } | |
573bfb72 | 10308 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10309 | return ret; |
10310 | } | |
1eafa6c7 | 10311 | |
eb73c1b7 MX |
10312 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10313 | { | |
0b246afa | 10314 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10315 | int ret; |
1eafa6c7 | 10316 | |
0b246afa | 10317 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10318 | return -EROFS; |
10319 | ||
6c255e67 MX |
10320 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10321 | if (ret > 0) | |
10322 | ret = 0; | |
eb73c1b7 MX |
10323 | /* |
10324 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10325 | * we have to make sure the IO is actually started and that |
10326 | * ordered extents get created before we return | |
10327 | */ | |
0b246afa JM |
10328 | atomic_inc(&fs_info->async_submit_draining); |
10329 | while (atomic_read(&fs_info->nr_async_submits) || | |
10330 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10331 | wait_event(fs_info->async_submit_wait, | |
10332 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10333 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10334 | } | |
10335 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 MX |
10336 | return ret; |
10337 | } | |
10338 | ||
6c255e67 MX |
10339 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10340 | int nr) | |
eb73c1b7 MX |
10341 | { |
10342 | struct btrfs_root *root; | |
10343 | struct list_head splice; | |
10344 | int ret; | |
10345 | ||
2c21b4d7 | 10346 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10347 | return -EROFS; |
10348 | ||
10349 | INIT_LIST_HEAD(&splice); | |
10350 | ||
573bfb72 | 10351 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10352 | spin_lock(&fs_info->delalloc_root_lock); |
10353 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10354 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10355 | root = list_first_entry(&splice, struct btrfs_root, |
10356 | delalloc_root); | |
10357 | root = btrfs_grab_fs_root(root); | |
10358 | BUG_ON(!root); | |
10359 | list_move_tail(&root->delalloc_root, | |
10360 | &fs_info->delalloc_roots); | |
10361 | spin_unlock(&fs_info->delalloc_root_lock); | |
10362 | ||
6c255e67 | 10363 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10364 | btrfs_put_fs_root(root); |
6c255e67 | 10365 | if (ret < 0) |
eb73c1b7 MX |
10366 | goto out; |
10367 | ||
6c255e67 MX |
10368 | if (nr != -1) { |
10369 | nr -= ret; | |
10370 | WARN_ON(nr < 0); | |
10371 | } | |
eb73c1b7 | 10372 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10373 | } |
eb73c1b7 | 10374 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10375 | |
6c255e67 | 10376 | ret = 0; |
eb73c1b7 MX |
10377 | atomic_inc(&fs_info->async_submit_draining); |
10378 | while (atomic_read(&fs_info->nr_async_submits) || | |
10379 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10380 | wait_event(fs_info->async_submit_wait, | |
10381 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10382 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10383 | } | |
10384 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10385 | out: |
1eafa6c7 | 10386 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10387 | spin_lock(&fs_info->delalloc_root_lock); |
10388 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10389 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10390 | } |
573bfb72 | 10391 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10392 | return ret; |
ea8c2819 CM |
10393 | } |
10394 | ||
39279cc3 CM |
10395 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10396 | const char *symname) | |
10397 | { | |
0b246afa | 10398 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10399 | struct btrfs_trans_handle *trans; |
10400 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10401 | struct btrfs_path *path; | |
10402 | struct btrfs_key key; | |
1832a6d5 | 10403 | struct inode *inode = NULL; |
39279cc3 CM |
10404 | int err; |
10405 | int drop_inode = 0; | |
10406 | u64 objectid; | |
67871254 | 10407 | u64 index = 0; |
39279cc3 CM |
10408 | int name_len; |
10409 | int datasize; | |
5f39d397 | 10410 | unsigned long ptr; |
39279cc3 | 10411 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10412 | struct extent_buffer *leaf; |
39279cc3 | 10413 | |
f06becc4 | 10414 | name_len = strlen(symname); |
0b246afa | 10415 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10416 | return -ENAMETOOLONG; |
1832a6d5 | 10417 | |
9ed74f2d JB |
10418 | /* |
10419 | * 2 items for inode item and ref | |
10420 | * 2 items for dir items | |
9269d12b FM |
10421 | * 1 item for updating parent inode item |
10422 | * 1 item for the inline extent item | |
9ed74f2d JB |
10423 | * 1 item for xattr if selinux is on |
10424 | */ | |
9269d12b | 10425 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10426 | if (IS_ERR(trans)) |
10427 | return PTR_ERR(trans); | |
1832a6d5 | 10428 | |
581bb050 LZ |
10429 | err = btrfs_find_free_ino(root, &objectid); |
10430 | if (err) | |
10431 | goto out_unlock; | |
10432 | ||
aec7477b | 10433 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
f85b7379 DS |
10434 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
10435 | objectid, S_IFLNK|S_IRWXUGO, &index); | |
7cf96da3 TI |
10436 | if (IS_ERR(inode)) { |
10437 | err = PTR_ERR(inode); | |
39279cc3 | 10438 | goto out_unlock; |
7cf96da3 | 10439 | } |
39279cc3 | 10440 | |
ad19db71 CS |
10441 | /* |
10442 | * If the active LSM wants to access the inode during | |
10443 | * d_instantiate it needs these. Smack checks to see | |
10444 | * if the filesystem supports xattrs by looking at the | |
10445 | * ops vector. | |
10446 | */ | |
10447 | inode->i_fop = &btrfs_file_operations; | |
10448 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10449 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10450 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10451 | ||
10452 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10453 | if (err) | |
10454 | goto out_unlock_inode; | |
ad19db71 | 10455 | |
39279cc3 | 10456 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10457 | if (!path) { |
10458 | err = -ENOMEM; | |
b0d5d10f | 10459 | goto out_unlock_inode; |
d8926bb3 | 10460 | } |
4a0cc7ca | 10461 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10462 | key.offset = 0; |
962a298f | 10463 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10464 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10465 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10466 | datasize); | |
54aa1f4d | 10467 | if (err) { |
b0839166 | 10468 | btrfs_free_path(path); |
b0d5d10f | 10469 | goto out_unlock_inode; |
54aa1f4d | 10470 | } |
5f39d397 CM |
10471 | leaf = path->nodes[0]; |
10472 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10473 | struct btrfs_file_extent_item); | |
10474 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10475 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10476 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10477 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10478 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10479 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10480 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10481 | ||
39279cc3 | 10482 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10483 | write_extent_buffer(leaf, symname, ptr, name_len); |
10484 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10485 | btrfs_free_path(path); |
5f39d397 | 10486 | |
39279cc3 | 10487 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10488 | inode_nohighmem(inode); |
39279cc3 | 10489 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10490 | inode_set_bytes(inode, name_len); |
6ef06d27 | 10491 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
54aa1f4d | 10492 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10493 | /* |
10494 | * Last step, add directory indexes for our symlink inode. This is the | |
10495 | * last step to avoid extra cleanup of these indexes if an error happens | |
10496 | * elsewhere above. | |
10497 | */ | |
10498 | if (!err) | |
cef415af NB |
10499 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
10500 | BTRFS_I(inode), 0, index); | |
b0d5d10f | 10501 | if (err) { |
54aa1f4d | 10502 | drop_inode = 1; |
b0d5d10f CM |
10503 | goto out_unlock_inode; |
10504 | } | |
10505 | ||
10506 | unlock_new_inode(inode); | |
10507 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10508 | |
10509 | out_unlock: | |
3a45bb20 | 10510 | btrfs_end_transaction(trans); |
39279cc3 CM |
10511 | if (drop_inode) { |
10512 | inode_dec_link_count(inode); | |
10513 | iput(inode); | |
10514 | } | |
2ff7e61e | 10515 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 10516 | return err; |
b0d5d10f CM |
10517 | |
10518 | out_unlock_inode: | |
10519 | drop_inode = 1; | |
10520 | unlock_new_inode(inode); | |
10521 | goto out_unlock; | |
39279cc3 | 10522 | } |
16432985 | 10523 | |
0af3d00b JB |
10524 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10525 | u64 start, u64 num_bytes, u64 min_size, | |
10526 | loff_t actual_len, u64 *alloc_hint, | |
10527 | struct btrfs_trans_handle *trans) | |
d899e052 | 10528 | { |
0b246afa | 10529 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10530 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10531 | struct extent_map *em; | |
d899e052 YZ |
10532 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10533 | struct btrfs_key ins; | |
d899e052 | 10534 | u64 cur_offset = start; |
55a61d1d | 10535 | u64 i_size; |
154ea289 | 10536 | u64 cur_bytes; |
0b670dc4 | 10537 | u64 last_alloc = (u64)-1; |
d899e052 | 10538 | int ret = 0; |
0af3d00b | 10539 | bool own_trans = true; |
18513091 | 10540 | u64 end = start + num_bytes - 1; |
d899e052 | 10541 | |
0af3d00b JB |
10542 | if (trans) |
10543 | own_trans = false; | |
d899e052 | 10544 | while (num_bytes > 0) { |
0af3d00b JB |
10545 | if (own_trans) { |
10546 | trans = btrfs_start_transaction(root, 3); | |
10547 | if (IS_ERR(trans)) { | |
10548 | ret = PTR_ERR(trans); | |
10549 | break; | |
10550 | } | |
5a303d5d YZ |
10551 | } |
10552 | ||
ee22184b | 10553 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10554 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10555 | /* |
10556 | * If we are severely fragmented we could end up with really | |
10557 | * small allocations, so if the allocator is returning small | |
10558 | * chunks lets make its job easier by only searching for those | |
10559 | * sized chunks. | |
10560 | */ | |
10561 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10562 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10563 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10564 | if (ret) { |
0af3d00b | 10565 | if (own_trans) |
3a45bb20 | 10566 | btrfs_end_transaction(trans); |
a22285a6 | 10567 | break; |
d899e052 | 10568 | } |
0b246afa | 10569 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10570 | |
0b670dc4 | 10571 | last_alloc = ins.offset; |
d899e052 YZ |
10572 | ret = insert_reserved_file_extent(trans, inode, |
10573 | cur_offset, ins.objectid, | |
10574 | ins.offset, ins.offset, | |
920bbbfb | 10575 | ins.offset, 0, 0, 0, |
d899e052 | 10576 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10577 | if (ret) { |
2ff7e61e | 10578 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10579 | ins.offset, 0); |
66642832 | 10580 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10581 | if (own_trans) |
3a45bb20 | 10582 | btrfs_end_transaction(trans); |
79787eaa JM |
10583 | break; |
10584 | } | |
31193213 | 10585 | |
dcdbc059 | 10586 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
a1ed835e | 10587 | cur_offset + ins.offset -1, 0); |
5a303d5d | 10588 | |
5dc562c5 JB |
10589 | em = alloc_extent_map(); |
10590 | if (!em) { | |
10591 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10592 | &BTRFS_I(inode)->runtime_flags); | |
10593 | goto next; | |
10594 | } | |
10595 | ||
10596 | em->start = cur_offset; | |
10597 | em->orig_start = cur_offset; | |
10598 | em->len = ins.offset; | |
10599 | em->block_start = ins.objectid; | |
10600 | em->block_len = ins.offset; | |
b4939680 | 10601 | em->orig_block_len = ins.offset; |
cc95bef6 | 10602 | em->ram_bytes = ins.offset; |
0b246afa | 10603 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10604 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10605 | em->generation = trans->transid; | |
10606 | ||
10607 | while (1) { | |
10608 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10609 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10610 | write_unlock(&em_tree->lock); |
10611 | if (ret != -EEXIST) | |
10612 | break; | |
dcdbc059 | 10613 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
5dc562c5 JB |
10614 | cur_offset + ins.offset - 1, |
10615 | 0); | |
10616 | } | |
10617 | free_extent_map(em); | |
10618 | next: | |
d899e052 YZ |
10619 | num_bytes -= ins.offset; |
10620 | cur_offset += ins.offset; | |
efa56464 | 10621 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10622 | |
0c4d2d95 | 10623 | inode_inc_iversion(inode); |
c2050a45 | 10624 | inode->i_ctime = current_time(inode); |
6cbff00f | 10625 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10626 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10627 | (actual_len > inode->i_size) && |
10628 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10629 | if (cur_offset > actual_len) |
55a61d1d | 10630 | i_size = actual_len; |
d1ea6a61 | 10631 | else |
55a61d1d JB |
10632 | i_size = cur_offset; |
10633 | i_size_write(inode, i_size); | |
10634 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10635 | } |
10636 | ||
d899e052 | 10637 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10638 | |
10639 | if (ret) { | |
66642832 | 10640 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10641 | if (own_trans) |
3a45bb20 | 10642 | btrfs_end_transaction(trans); |
79787eaa JM |
10643 | break; |
10644 | } | |
d899e052 | 10645 | |
0af3d00b | 10646 | if (own_trans) |
3a45bb20 | 10647 | btrfs_end_transaction(trans); |
5a303d5d | 10648 | } |
18513091 | 10649 | if (cur_offset < end) |
bc42bda2 | 10650 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
18513091 | 10651 | end - cur_offset + 1); |
d899e052 YZ |
10652 | return ret; |
10653 | } | |
10654 | ||
0af3d00b JB |
10655 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10656 | u64 start, u64 num_bytes, u64 min_size, | |
10657 | loff_t actual_len, u64 *alloc_hint) | |
10658 | { | |
10659 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10660 | min_size, actual_len, alloc_hint, | |
10661 | NULL); | |
10662 | } | |
10663 | ||
10664 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10665 | struct btrfs_trans_handle *trans, int mode, | |
10666 | u64 start, u64 num_bytes, u64 min_size, | |
10667 | loff_t actual_len, u64 *alloc_hint) | |
10668 | { | |
10669 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10670 | min_size, actual_len, alloc_hint, trans); | |
10671 | } | |
10672 | ||
e6dcd2dc CM |
10673 | static int btrfs_set_page_dirty(struct page *page) |
10674 | { | |
e6dcd2dc CM |
10675 | return __set_page_dirty_nobuffers(page); |
10676 | } | |
10677 | ||
10556cb2 | 10678 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10679 | { |
b83cc969 | 10680 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10681 | umode_t mode = inode->i_mode; |
b83cc969 | 10682 | |
cb6db4e5 JM |
10683 | if (mask & MAY_WRITE && |
10684 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10685 | if (btrfs_root_readonly(root)) | |
10686 | return -EROFS; | |
10687 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10688 | return -EACCES; | |
10689 | } | |
2830ba7f | 10690 | return generic_permission(inode, mask); |
fdebe2bd | 10691 | } |
39279cc3 | 10692 | |
ef3b9af5 FM |
10693 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10694 | { | |
2ff7e61e | 10695 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10696 | struct btrfs_trans_handle *trans; |
10697 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10698 | struct inode *inode = NULL; | |
10699 | u64 objectid; | |
10700 | u64 index; | |
10701 | int ret = 0; | |
10702 | ||
10703 | /* | |
10704 | * 5 units required for adding orphan entry | |
10705 | */ | |
10706 | trans = btrfs_start_transaction(root, 5); | |
10707 | if (IS_ERR(trans)) | |
10708 | return PTR_ERR(trans); | |
10709 | ||
10710 | ret = btrfs_find_free_ino(root, &objectid); | |
10711 | if (ret) | |
10712 | goto out; | |
10713 | ||
10714 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
f85b7379 | 10715 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10716 | if (IS_ERR(inode)) { |
10717 | ret = PTR_ERR(inode); | |
10718 | inode = NULL; | |
10719 | goto out; | |
10720 | } | |
10721 | ||
ef3b9af5 FM |
10722 | inode->i_fop = &btrfs_file_operations; |
10723 | inode->i_op = &btrfs_file_inode_operations; | |
10724 | ||
10725 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10726 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10727 | ||
b0d5d10f CM |
10728 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10729 | if (ret) | |
10730 | goto out_inode; | |
10731 | ||
10732 | ret = btrfs_update_inode(trans, root, inode); | |
10733 | if (ret) | |
10734 | goto out_inode; | |
73f2e545 | 10735 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
ef3b9af5 | 10736 | if (ret) |
b0d5d10f | 10737 | goto out_inode; |
ef3b9af5 | 10738 | |
5762b5c9 FM |
10739 | /* |
10740 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10741 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10742 | * through: | |
10743 | * | |
10744 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10745 | */ | |
10746 | set_nlink(inode, 1); | |
b0d5d10f | 10747 | unlock_new_inode(inode); |
ef3b9af5 FM |
10748 | d_tmpfile(dentry, inode); |
10749 | mark_inode_dirty(inode); | |
10750 | ||
10751 | out: | |
3a45bb20 | 10752 | btrfs_end_transaction(trans); |
ef3b9af5 FM |
10753 | if (ret) |
10754 | iput(inode); | |
2ff7e61e JM |
10755 | btrfs_balance_delayed_items(fs_info); |
10756 | btrfs_btree_balance_dirty(fs_info); | |
ef3b9af5 | 10757 | return ret; |
b0d5d10f CM |
10758 | |
10759 | out_inode: | |
10760 | unlock_new_inode(inode); | |
10761 | goto out; | |
10762 | ||
ef3b9af5 FM |
10763 | } |
10764 | ||
20a7db8a | 10765 | __attribute__((const)) |
9d0d1c8b | 10766 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) |
20a7db8a | 10767 | { |
9d0d1c8b | 10768 | return -EAGAIN; |
20a7db8a DS |
10769 | } |
10770 | ||
c6100a4b JB |
10771 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
10772 | { | |
10773 | struct inode *inode = private_data; | |
10774 | return btrfs_sb(inode->i_sb); | |
10775 | } | |
10776 | ||
10777 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, | |
10778 | u64 start, u64 end) | |
10779 | { | |
10780 | struct inode *inode = private_data; | |
10781 | u64 isize; | |
10782 | ||
10783 | isize = i_size_read(inode); | |
10784 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
10785 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, | |
10786 | "%s: ino %llu isize %llu odd range [%llu,%llu]", | |
10787 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); | |
10788 | } | |
10789 | } | |
10790 | ||
10791 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) | |
10792 | { | |
10793 | struct inode *inode = private_data; | |
10794 | unsigned long index = start >> PAGE_SHIFT; | |
10795 | unsigned long end_index = end >> PAGE_SHIFT; | |
10796 | struct page *page; | |
10797 | ||
10798 | while (index <= end_index) { | |
10799 | page = find_get_page(inode->i_mapping, index); | |
10800 | ASSERT(page); /* Pages should be in the extent_io_tree */ | |
10801 | set_page_writeback(page); | |
10802 | put_page(page); | |
10803 | index++; | |
10804 | } | |
10805 | } | |
10806 | ||
6e1d5dcc | 10807 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10808 | .getattr = btrfs_getattr, |
39279cc3 CM |
10809 | .lookup = btrfs_lookup, |
10810 | .create = btrfs_create, | |
10811 | .unlink = btrfs_unlink, | |
10812 | .link = btrfs_link, | |
10813 | .mkdir = btrfs_mkdir, | |
10814 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10815 | .rename = btrfs_rename2, |
39279cc3 CM |
10816 | .symlink = btrfs_symlink, |
10817 | .setattr = btrfs_setattr, | |
618e21d5 | 10818 | .mknod = btrfs_mknod, |
5103e947 | 10819 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10820 | .permission = btrfs_permission, |
4e34e719 | 10821 | .get_acl = btrfs_get_acl, |
996a710d | 10822 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10823 | .update_time = btrfs_update_time, |
ef3b9af5 | 10824 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10825 | }; |
6e1d5dcc | 10826 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10827 | .lookup = btrfs_lookup, |
fdebe2bd | 10828 | .permission = btrfs_permission, |
93fd63c2 | 10829 | .update_time = btrfs_update_time, |
39279cc3 | 10830 | }; |
76dda93c | 10831 | |
828c0950 | 10832 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10833 | .llseek = generic_file_llseek, |
10834 | .read = generic_read_dir, | |
02dbfc99 | 10835 | .iterate_shared = btrfs_real_readdir, |
23b5ec74 | 10836 | .open = btrfs_opendir, |
34287aa3 | 10837 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10838 | #ifdef CONFIG_COMPAT |
4c63c245 | 10839 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10840 | #endif |
6bf13c0c | 10841 | .release = btrfs_release_file, |
e02119d5 | 10842 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10843 | }; |
10844 | ||
20e5506b | 10845 | static const struct extent_io_ops btrfs_extent_io_ops = { |
4d53dddb | 10846 | /* mandatory callbacks */ |
065631f6 | 10847 | .submit_bio_hook = btrfs_submit_bio_hook, |
07157aac | 10848 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
4d53dddb | 10849 | .merge_bio_hook = btrfs_merge_bio_hook, |
9d0d1c8b | 10850 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
c6100a4b JB |
10851 | .tree_fs_info = iotree_fs_info, |
10852 | .set_range_writeback = btrfs_set_range_writeback, | |
4d53dddb DS |
10853 | |
10854 | /* optional callbacks */ | |
10855 | .fill_delalloc = run_delalloc_range, | |
e6dcd2dc | 10856 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10857 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10858 | .set_bit_hook = btrfs_set_bit_hook, |
10859 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10860 | .merge_extent_hook = btrfs_merge_extent_hook, |
10861 | .split_extent_hook = btrfs_split_extent_hook, | |
c6100a4b | 10862 | .check_extent_io_range = btrfs_check_extent_io_range, |
07157aac CM |
10863 | }; |
10864 | ||
35054394 CM |
10865 | /* |
10866 | * btrfs doesn't support the bmap operation because swapfiles | |
10867 | * use bmap to make a mapping of extents in the file. They assume | |
10868 | * these extents won't change over the life of the file and they | |
10869 | * use the bmap result to do IO directly to the drive. | |
10870 | * | |
10871 | * the btrfs bmap call would return logical addresses that aren't | |
10872 | * suitable for IO and they also will change frequently as COW | |
10873 | * operations happen. So, swapfile + btrfs == corruption. | |
10874 | * | |
10875 | * For now we're avoiding this by dropping bmap. | |
10876 | */ | |
7f09410b | 10877 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10878 | .readpage = btrfs_readpage, |
10879 | .writepage = btrfs_writepage, | |
b293f02e | 10880 | .writepages = btrfs_writepages, |
3ab2fb5a | 10881 | .readpages = btrfs_readpages, |
16432985 | 10882 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10883 | .invalidatepage = btrfs_invalidatepage, |
10884 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10885 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10886 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10887 | }; |
10888 | ||
7f09410b | 10889 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10890 | .readpage = btrfs_readpage, |
10891 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10892 | .invalidatepage = btrfs_invalidatepage, |
10893 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10894 | }; |
10895 | ||
6e1d5dcc | 10896 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10897 | .getattr = btrfs_getattr, |
10898 | .setattr = btrfs_setattr, | |
5103e947 | 10899 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10900 | .permission = btrfs_permission, |
1506fcc8 | 10901 | .fiemap = btrfs_fiemap, |
4e34e719 | 10902 | .get_acl = btrfs_get_acl, |
996a710d | 10903 | .set_acl = btrfs_set_acl, |
e41f941a | 10904 | .update_time = btrfs_update_time, |
39279cc3 | 10905 | }; |
6e1d5dcc | 10906 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10907 | .getattr = btrfs_getattr, |
10908 | .setattr = btrfs_setattr, | |
fdebe2bd | 10909 | .permission = btrfs_permission, |
33268eaf | 10910 | .listxattr = btrfs_listxattr, |
4e34e719 | 10911 | .get_acl = btrfs_get_acl, |
996a710d | 10912 | .set_acl = btrfs_set_acl, |
e41f941a | 10913 | .update_time = btrfs_update_time, |
618e21d5 | 10914 | }; |
6e1d5dcc | 10915 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10916 | .get_link = page_get_link, |
f209561a | 10917 | .getattr = btrfs_getattr, |
22c44fe6 | 10918 | .setattr = btrfs_setattr, |
fdebe2bd | 10919 | .permission = btrfs_permission, |
0279b4cd | 10920 | .listxattr = btrfs_listxattr, |
e41f941a | 10921 | .update_time = btrfs_update_time, |
39279cc3 | 10922 | }; |
76dda93c | 10923 | |
82d339d9 | 10924 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10925 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10926 | .d_release = btrfs_dentry_release, |
76dda93c | 10927 | }; |