]>
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; | |
74 | }; | |
75 | ||
6e1d5dcc AD |
76 | static const struct inode_operations btrfs_dir_inode_operations; |
77 | static const struct inode_operations btrfs_symlink_inode_operations; | |
78 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
79 | static const struct inode_operations btrfs_special_inode_operations; | |
80 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
81 | static const struct address_space_operations btrfs_aops; |
82 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 83 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 84 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
85 | |
86 | static struct kmem_cache *btrfs_inode_cachep; | |
87 | struct kmem_cache *btrfs_trans_handle_cachep; | |
88 | struct kmem_cache *btrfs_transaction_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); | |
70c8a91c JB |
111 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
112 | u64 len, u64 orig_start, | |
113 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
114 | u64 orig_block_len, u64 ram_bytes, |
115 | int type); | |
7b128766 | 116 | |
48a3b636 | 117 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 118 | |
6a3891c5 JB |
119 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
120 | void btrfs_test_inode_set_ops(struct inode *inode) | |
121 | { | |
122 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
123 | } | |
124 | #endif | |
125 | ||
f34f57a3 | 126 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
127 | struct inode *inode, struct inode *dir, |
128 | const struct qstr *qstr) | |
0279b4cd JO |
129 | { |
130 | int err; | |
131 | ||
f34f57a3 | 132 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 133 | if (!err) |
2a7dba39 | 134 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
135 | return err; |
136 | } | |
137 | ||
c8b97818 CM |
138 | /* |
139 | * this does all the hard work for inserting an inline extent into | |
140 | * the btree. The caller should have done a btrfs_drop_extents so that | |
141 | * no overlapping inline items exist in the btree | |
142 | */ | |
40f76580 | 143 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 144 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
145 | struct btrfs_root *root, struct inode *inode, |
146 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 147 | int compress_type, |
c8b97818 CM |
148 | struct page **compressed_pages) |
149 | { | |
c8b97818 CM |
150 | struct extent_buffer *leaf; |
151 | struct page *page = NULL; | |
152 | char *kaddr; | |
153 | unsigned long ptr; | |
154 | struct btrfs_file_extent_item *ei; | |
155 | int err = 0; | |
156 | int ret; | |
157 | size_t cur_size = size; | |
c8b97818 | 158 | unsigned long offset; |
c8b97818 | 159 | |
fe3f566c | 160 | if (compressed_size && compressed_pages) |
c8b97818 | 161 | cur_size = compressed_size; |
c8b97818 | 162 | |
1acae57b | 163 | inode_add_bytes(inode, size); |
c8b97818 | 164 | |
1acae57b FDBM |
165 | if (!extent_inserted) { |
166 | struct btrfs_key key; | |
167 | size_t datasize; | |
c8b97818 | 168 | |
4a0cc7ca | 169 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b | 170 | key.offset = start; |
962a298f | 171 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 172 | |
1acae57b FDBM |
173 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
174 | path->leave_spinning = 1; | |
175 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
176 | datasize); | |
177 | if (ret) { | |
178 | err = ret; | |
179 | goto fail; | |
180 | } | |
c8b97818 CM |
181 | } |
182 | leaf = path->nodes[0]; | |
183 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
184 | struct btrfs_file_extent_item); | |
185 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
186 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
187 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
188 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
189 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
190 | ptr = btrfs_file_extent_inline_start(ei); | |
191 | ||
261507a0 | 192 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
193 | struct page *cpage; |
194 | int i = 0; | |
d397712b | 195 | while (compressed_size > 0) { |
c8b97818 | 196 | cpage = compressed_pages[i]; |
5b050f04 | 197 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 198 | PAGE_SIZE); |
c8b97818 | 199 | |
7ac687d9 | 200 | kaddr = kmap_atomic(cpage); |
c8b97818 | 201 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 202 | kunmap_atomic(kaddr); |
c8b97818 CM |
203 | |
204 | i++; | |
205 | ptr += cur_size; | |
206 | compressed_size -= cur_size; | |
207 | } | |
208 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 209 | compress_type); |
c8b97818 CM |
210 | } else { |
211 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 212 | start >> PAGE_SHIFT); |
c8b97818 | 213 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 214 | kaddr = kmap_atomic(page); |
09cbfeaf | 215 | offset = start & (PAGE_SIZE - 1); |
c8b97818 | 216 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 217 | kunmap_atomic(kaddr); |
09cbfeaf | 218 | put_page(page); |
c8b97818 CM |
219 | } |
220 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 221 | btrfs_release_path(path); |
c8b97818 | 222 | |
c2167754 YZ |
223 | /* |
224 | * we're an inline extent, so nobody can | |
225 | * extend the file past i_size without locking | |
226 | * a page we already have locked. | |
227 | * | |
228 | * We must do any isize and inode updates | |
229 | * before we unlock the pages. Otherwise we | |
230 | * could end up racing with unlink. | |
231 | */ | |
c8b97818 | 232 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 233 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 234 | |
79787eaa | 235 | return ret; |
c8b97818 | 236 | fail: |
c8b97818 CM |
237 | return err; |
238 | } | |
239 | ||
240 | ||
241 | /* | |
242 | * conditionally insert an inline extent into the file. This | |
243 | * does the checks required to make sure the data is small enough | |
244 | * to fit as an inline extent. | |
245 | */ | |
00361589 JB |
246 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
247 | struct inode *inode, u64 start, | |
248 | u64 end, size_t compressed_size, | |
249 | int compress_type, | |
250 | struct page **compressed_pages) | |
c8b97818 | 251 | { |
0b246afa | 252 | struct btrfs_fs_info *fs_info = root->fs_info; |
00361589 | 253 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
254 | u64 isize = i_size_read(inode); |
255 | u64 actual_end = min(end + 1, isize); | |
256 | u64 inline_len = actual_end - start; | |
0b246afa | 257 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
c8b97818 CM |
258 | u64 data_len = inline_len; |
259 | int ret; | |
1acae57b FDBM |
260 | struct btrfs_path *path; |
261 | int extent_inserted = 0; | |
262 | u32 extent_item_size; | |
c8b97818 CM |
263 | |
264 | if (compressed_size) | |
265 | data_len = compressed_size; | |
266 | ||
267 | if (start > 0 || | |
0b246afa JM |
268 | actual_end > fs_info->sectorsize || |
269 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || | |
c8b97818 | 270 | (!compressed_size && |
0b246afa | 271 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
c8b97818 | 272 | end + 1 < isize || |
0b246afa | 273 | data_len > fs_info->max_inline) { |
c8b97818 CM |
274 | return 1; |
275 | } | |
276 | ||
1acae57b FDBM |
277 | path = btrfs_alloc_path(); |
278 | if (!path) | |
279 | return -ENOMEM; | |
280 | ||
00361589 | 281 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
282 | if (IS_ERR(trans)) { |
283 | btrfs_free_path(path); | |
00361589 | 284 | return PTR_ERR(trans); |
1acae57b | 285 | } |
0b246afa | 286 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
00361589 | 287 | |
1acae57b FDBM |
288 | if (compressed_size && compressed_pages) |
289 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
290 | compressed_size); | |
291 | else | |
292 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
293 | inline_len); | |
294 | ||
295 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
296 | start, aligned_end, NULL, | |
297 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 | 298 | if (ret) { |
66642832 | 299 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
300 | goto out; |
301 | } | |
c8b97818 CM |
302 | |
303 | if (isize > actual_end) | |
304 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
305 | ret = insert_inline_extent(trans, path, extent_inserted, |
306 | root, inode, start, | |
c8b97818 | 307 | inline_len, compressed_size, |
fe3f566c | 308 | compress_type, compressed_pages); |
2adcac1a | 309 | if (ret && ret != -ENOSPC) { |
66642832 | 310 | btrfs_abort_transaction(trans, ret); |
00361589 | 311 | goto out; |
2adcac1a | 312 | } else if (ret == -ENOSPC) { |
00361589 JB |
313 | ret = 1; |
314 | goto out; | |
79787eaa | 315 | } |
2adcac1a | 316 | |
bdc20e67 | 317 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 318 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 319 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 320 | out: |
94ed938a QW |
321 | /* |
322 | * Don't forget to free the reserved space, as for inlined extent | |
323 | * it won't count as data extent, free them directly here. | |
324 | * And at reserve time, it's always aligned to page size, so | |
325 | * just free one page here. | |
326 | */ | |
09cbfeaf | 327 | btrfs_qgroup_free_data(inode, 0, PAGE_SIZE); |
1acae57b | 328 | btrfs_free_path(path); |
3a45bb20 | 329 | btrfs_end_transaction(trans); |
00361589 | 330 | return ret; |
c8b97818 CM |
331 | } |
332 | ||
771ed689 CM |
333 | struct async_extent { |
334 | u64 start; | |
335 | u64 ram_size; | |
336 | u64 compressed_size; | |
337 | struct page **pages; | |
338 | unsigned long nr_pages; | |
261507a0 | 339 | int compress_type; |
771ed689 CM |
340 | struct list_head list; |
341 | }; | |
342 | ||
343 | struct async_cow { | |
344 | struct inode *inode; | |
345 | struct btrfs_root *root; | |
346 | struct page *locked_page; | |
347 | u64 start; | |
348 | u64 end; | |
349 | struct list_head extents; | |
350 | struct btrfs_work work; | |
351 | }; | |
352 | ||
353 | static noinline int add_async_extent(struct async_cow *cow, | |
354 | u64 start, u64 ram_size, | |
355 | u64 compressed_size, | |
356 | struct page **pages, | |
261507a0 LZ |
357 | unsigned long nr_pages, |
358 | int compress_type) | |
771ed689 CM |
359 | { |
360 | struct async_extent *async_extent; | |
361 | ||
362 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 363 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
364 | async_extent->start = start; |
365 | async_extent->ram_size = ram_size; | |
366 | async_extent->compressed_size = compressed_size; | |
367 | async_extent->pages = pages; | |
368 | async_extent->nr_pages = nr_pages; | |
261507a0 | 369 | async_extent->compress_type = compress_type; |
771ed689 CM |
370 | list_add_tail(&async_extent->list, &cow->extents); |
371 | return 0; | |
372 | } | |
373 | ||
f79707b0 WS |
374 | static inline int inode_need_compress(struct inode *inode) |
375 | { | |
0b246afa | 376 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f79707b0 WS |
377 | |
378 | /* force compress */ | |
0b246afa | 379 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
f79707b0 WS |
380 | return 1; |
381 | /* bad compression ratios */ | |
382 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
383 | return 0; | |
0b246afa | 384 | if (btrfs_test_opt(fs_info, COMPRESS) || |
f79707b0 WS |
385 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
386 | BTRFS_I(inode)->force_compress) | |
387 | return 1; | |
388 | return 0; | |
389 | } | |
390 | ||
26d30f85 AJ |
391 | static inline void inode_should_defrag(struct inode *inode, |
392 | u64 start, u64 end, u64 num_bytes, u64 small_write) | |
393 | { | |
394 | /* If this is a small write inside eof, kick off a defrag */ | |
395 | if (num_bytes < small_write && | |
396 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
397 | btrfs_add_inode_defrag(NULL, inode); | |
398 | } | |
399 | ||
d352ac68 | 400 | /* |
771ed689 CM |
401 | * we create compressed extents in two phases. The first |
402 | * phase compresses a range of pages that have already been | |
403 | * locked (both pages and state bits are locked). | |
c8b97818 | 404 | * |
771ed689 CM |
405 | * This is done inside an ordered work queue, and the compression |
406 | * is spread across many cpus. The actual IO submission is step | |
407 | * two, and the ordered work queue takes care of making sure that | |
408 | * happens in the same order things were put onto the queue by | |
409 | * writepages and friends. | |
c8b97818 | 410 | * |
771ed689 CM |
411 | * If this code finds it can't get good compression, it puts an |
412 | * entry onto the work queue to write the uncompressed bytes. This | |
413 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
414 | * are written in the same order that the flusher thread sent them |
415 | * down. | |
d352ac68 | 416 | */ |
c44f649e | 417 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
418 | struct page *locked_page, |
419 | u64 start, u64 end, | |
420 | struct async_cow *async_cow, | |
421 | int *num_added) | |
b888db2b | 422 | { |
0b246afa | 423 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
b888db2b | 424 | struct btrfs_root *root = BTRFS_I(inode)->root; |
db94535d | 425 | u64 num_bytes; |
0b246afa | 426 | u64 blocksize = fs_info->sectorsize; |
c8b97818 | 427 | u64 actual_end; |
42dc7bab | 428 | u64 isize = i_size_read(inode); |
e6dcd2dc | 429 | int ret = 0; |
c8b97818 CM |
430 | struct page **pages = NULL; |
431 | unsigned long nr_pages; | |
432 | unsigned long nr_pages_ret = 0; | |
433 | unsigned long total_compressed = 0; | |
434 | unsigned long total_in = 0; | |
ee22184b BL |
435 | unsigned long max_compressed = SZ_128K; |
436 | unsigned long max_uncompressed = SZ_128K; | |
c8b97818 CM |
437 | int i; |
438 | int will_compress; | |
0b246afa | 439 | int compress_type = fs_info->compress_type; |
4adaa611 | 440 | int redirty = 0; |
b888db2b | 441 | |
26d30f85 | 442 | inode_should_defrag(inode, start, end, end - start + 1, SZ_16K); |
4cb5300b | 443 | |
42dc7bab | 444 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
445 | again: |
446 | will_compress = 0; | |
09cbfeaf KS |
447 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
448 | nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE); | |
be20aa9d | 449 | |
f03d9301 CM |
450 | /* |
451 | * we don't want to send crud past the end of i_size through | |
452 | * compression, that's just a waste of CPU time. So, if the | |
453 | * end of the file is before the start of our current | |
454 | * requested range of bytes, we bail out to the uncompressed | |
455 | * cleanup code that can deal with all of this. | |
456 | * | |
457 | * It isn't really the fastest way to fix things, but this is a | |
458 | * very uncommon corner. | |
459 | */ | |
460 | if (actual_end <= start) | |
461 | goto cleanup_and_bail_uncompressed; | |
462 | ||
c8b97818 CM |
463 | total_compressed = actual_end - start; |
464 | ||
4bcbb332 SW |
465 | /* |
466 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 467 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
468 | */ |
469 | if (total_compressed <= blocksize && | |
470 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
471 | goto cleanup_and_bail_uncompressed; | |
472 | ||
c8b97818 CM |
473 | /* we want to make sure that amount of ram required to uncompress |
474 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
475 | * of a compressed extent to 128k. This is a crucial number |
476 | * because it also controls how easily we can spread reads across | |
477 | * cpus for decompression. | |
478 | * | |
479 | * We also want to make sure the amount of IO required to do | |
480 | * a random read is reasonably small, so we limit the size of | |
481 | * a compressed extent to 128k. | |
c8b97818 CM |
482 | */ |
483 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 484 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 485 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
486 | total_in = 0; |
487 | ret = 0; | |
db94535d | 488 | |
771ed689 CM |
489 | /* |
490 | * we do compression for mount -o compress and when the | |
491 | * inode has not been flagged as nocompress. This flag can | |
492 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 493 | */ |
f79707b0 | 494 | if (inode_need_compress(inode)) { |
c8b97818 | 495 | WARN_ON(pages); |
31e818fe | 496 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
497 | if (!pages) { |
498 | /* just bail out to the uncompressed code */ | |
499 | goto cont; | |
500 | } | |
c8b97818 | 501 | |
261507a0 LZ |
502 | if (BTRFS_I(inode)->force_compress) |
503 | compress_type = BTRFS_I(inode)->force_compress; | |
504 | ||
4adaa611 CM |
505 | /* |
506 | * we need to call clear_page_dirty_for_io on each | |
507 | * page in the range. Otherwise applications with the file | |
508 | * mmap'd can wander in and change the page contents while | |
509 | * we are compressing them. | |
510 | * | |
511 | * If the compression fails for any reason, we set the pages | |
512 | * dirty again later on. | |
513 | */ | |
514 | extent_range_clear_dirty_for_io(inode, start, end); | |
515 | redirty = 1; | |
261507a0 LZ |
516 | ret = btrfs_compress_pages(compress_type, |
517 | inode->i_mapping, start, | |
518 | total_compressed, pages, | |
519 | nr_pages, &nr_pages_ret, | |
520 | &total_in, | |
521 | &total_compressed, | |
522 | max_compressed); | |
c8b97818 CM |
523 | |
524 | if (!ret) { | |
525 | unsigned long offset = total_compressed & | |
09cbfeaf | 526 | (PAGE_SIZE - 1); |
c8b97818 CM |
527 | struct page *page = pages[nr_pages_ret - 1]; |
528 | char *kaddr; | |
529 | ||
530 | /* zero the tail end of the last page, we might be | |
531 | * sending it down to disk | |
532 | */ | |
533 | if (offset) { | |
7ac687d9 | 534 | kaddr = kmap_atomic(page); |
c8b97818 | 535 | memset(kaddr + offset, 0, |
09cbfeaf | 536 | PAGE_SIZE - offset); |
7ac687d9 | 537 | kunmap_atomic(kaddr); |
c8b97818 CM |
538 | } |
539 | will_compress = 1; | |
540 | } | |
541 | } | |
560f7d75 | 542 | cont: |
c8b97818 CM |
543 | if (start == 0) { |
544 | /* lets try to make an inline extent */ | |
771ed689 | 545 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 546 | /* we didn't compress the entire range, try |
771ed689 | 547 | * to make an uncompressed inline extent. |
c8b97818 | 548 | */ |
00361589 | 549 | ret = cow_file_range_inline(root, inode, start, end, |
f74670f7 | 550 | 0, BTRFS_COMPRESS_NONE, NULL); |
c8b97818 | 551 | } else { |
771ed689 | 552 | /* try making a compressed inline extent */ |
00361589 | 553 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
554 | total_compressed, |
555 | compress_type, pages); | |
c8b97818 | 556 | } |
79787eaa | 557 | if (ret <= 0) { |
151a41bc JB |
558 | unsigned long clear_flags = EXTENT_DELALLOC | |
559 | EXTENT_DEFRAG; | |
e6eb4314 FM |
560 | unsigned long page_error_op; |
561 | ||
151a41bc | 562 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 563 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 564 | |
771ed689 | 565 | /* |
79787eaa JM |
566 | * inline extent creation worked or returned error, |
567 | * we don't need to create any more async work items. | |
568 | * Unlock and free up our temp pages. | |
771ed689 | 569 | */ |
ba8b04c1 QW |
570 | extent_clear_unlock_delalloc(inode, start, end, end, |
571 | NULL, clear_flags, | |
572 | PAGE_UNLOCK | | |
c2790a2e JB |
573 | PAGE_CLEAR_DIRTY | |
574 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 575 | page_error_op | |
c2790a2e | 576 | PAGE_END_WRITEBACK); |
18513091 WX |
577 | btrfs_free_reserved_data_space_noquota(inode, start, |
578 | end - start + 1); | |
c8b97818 CM |
579 | goto free_pages_out; |
580 | } | |
581 | } | |
582 | ||
583 | if (will_compress) { | |
584 | /* | |
585 | * we aren't doing an inline extent round the compressed size | |
586 | * up to a block size boundary so the allocator does sane | |
587 | * things | |
588 | */ | |
fda2832f | 589 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
590 | |
591 | /* | |
592 | * one last check to make sure the compression is really a | |
593 | * win, compare the page count read with the blocks on disk | |
594 | */ | |
09cbfeaf | 595 | total_in = ALIGN(total_in, PAGE_SIZE); |
c8b97818 CM |
596 | if (total_compressed >= total_in) { |
597 | will_compress = 0; | |
598 | } else { | |
c8b97818 | 599 | num_bytes = total_in; |
c8bb0c8b AS |
600 | *num_added += 1; |
601 | ||
602 | /* | |
603 | * The async work queues will take care of doing actual | |
604 | * allocation on disk for these compressed pages, and | |
605 | * will submit them to the elevator. | |
606 | */ | |
607 | add_async_extent(async_cow, start, num_bytes, | |
608 | total_compressed, pages, nr_pages_ret, | |
609 | compress_type); | |
610 | ||
611 | if (start + num_bytes < end) { | |
612 | start += num_bytes; | |
613 | pages = NULL; | |
614 | cond_resched(); | |
615 | goto again; | |
616 | } | |
617 | return; | |
c8b97818 CM |
618 | } |
619 | } | |
c8bb0c8b | 620 | if (pages) { |
c8b97818 CM |
621 | /* |
622 | * the compression code ran but failed to make things smaller, | |
623 | * free any pages it allocated and our page pointer array | |
624 | */ | |
625 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 626 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 627 | put_page(pages[i]); |
c8b97818 CM |
628 | } |
629 | kfree(pages); | |
630 | pages = NULL; | |
631 | total_compressed = 0; | |
632 | nr_pages_ret = 0; | |
633 | ||
634 | /* flag the file so we don't compress in the future */ | |
0b246afa | 635 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
1e701a32 | 636 | !(BTRFS_I(inode)->force_compress)) { |
a555f810 | 637 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 638 | } |
c8b97818 | 639 | } |
f03d9301 | 640 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
641 | /* |
642 | * No compression, but we still need to write the pages in the file | |
643 | * we've been given so far. redirty the locked page if it corresponds | |
644 | * to our extent and set things up for the async work queue to run | |
645 | * cow_file_range to do the normal delalloc dance. | |
646 | */ | |
647 | if (page_offset(locked_page) >= start && | |
648 | page_offset(locked_page) <= end) | |
649 | __set_page_dirty_nobuffers(locked_page); | |
650 | /* unlocked later on in the async handlers */ | |
651 | ||
652 | if (redirty) | |
653 | extent_range_redirty_for_io(inode, start, end); | |
654 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
655 | BTRFS_COMPRESS_NONE); | |
656 | *num_added += 1; | |
3b951516 | 657 | |
c44f649e | 658 | return; |
771ed689 CM |
659 | |
660 | free_pages_out: | |
661 | for (i = 0; i < nr_pages_ret; i++) { | |
662 | WARN_ON(pages[i]->mapping); | |
09cbfeaf | 663 | put_page(pages[i]); |
771ed689 | 664 | } |
d397712b | 665 | kfree(pages); |
771ed689 | 666 | } |
771ed689 | 667 | |
40ae837b FM |
668 | static void free_async_extent_pages(struct async_extent *async_extent) |
669 | { | |
670 | int i; | |
671 | ||
672 | if (!async_extent->pages) | |
673 | return; | |
674 | ||
675 | for (i = 0; i < async_extent->nr_pages; i++) { | |
676 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 677 | put_page(async_extent->pages[i]); |
40ae837b FM |
678 | } |
679 | kfree(async_extent->pages); | |
680 | async_extent->nr_pages = 0; | |
681 | async_extent->pages = NULL; | |
771ed689 CM |
682 | } |
683 | ||
684 | /* | |
685 | * phase two of compressed writeback. This is the ordered portion | |
686 | * of the code, which only gets called in the order the work was | |
687 | * queued. We walk all the async extents created by compress_file_range | |
688 | * and send them down to the disk. | |
689 | */ | |
dec8f175 | 690 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
691 | struct async_cow *async_cow) |
692 | { | |
0b246afa | 693 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
694 | struct async_extent *async_extent; |
695 | u64 alloc_hint = 0; | |
771ed689 CM |
696 | struct btrfs_key ins; |
697 | struct extent_map *em; | |
698 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
699 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
700 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 701 | int ret = 0; |
771ed689 | 702 | |
3e04e7f1 | 703 | again: |
d397712b | 704 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
705 | async_extent = list_entry(async_cow->extents.next, |
706 | struct async_extent, list); | |
707 | list_del(&async_extent->list); | |
c8b97818 | 708 | |
771ed689 CM |
709 | io_tree = &BTRFS_I(inode)->io_tree; |
710 | ||
f5a84ee3 | 711 | retry: |
771ed689 CM |
712 | /* did the compression code fall back to uncompressed IO? */ |
713 | if (!async_extent->pages) { | |
714 | int page_started = 0; | |
715 | unsigned long nr_written = 0; | |
716 | ||
717 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 718 | async_extent->start + |
d0082371 | 719 | async_extent->ram_size - 1); |
771ed689 CM |
720 | |
721 | /* allocate blocks */ | |
f5a84ee3 JB |
722 | ret = cow_file_range(inode, async_cow->locked_page, |
723 | async_extent->start, | |
724 | async_extent->start + | |
725 | async_extent->ram_size - 1, | |
dda3245e WX |
726 | async_extent->start + |
727 | async_extent->ram_size - 1, | |
728 | &page_started, &nr_written, 0, | |
729 | NULL); | |
771ed689 | 730 | |
79787eaa JM |
731 | /* JDM XXX */ |
732 | ||
771ed689 CM |
733 | /* |
734 | * if page_started, cow_file_range inserted an | |
735 | * inline extent and took care of all the unlocking | |
736 | * and IO for us. Otherwise, we need to submit | |
737 | * all those pages down to the drive. | |
738 | */ | |
f5a84ee3 | 739 | if (!page_started && !ret) |
771ed689 CM |
740 | extent_write_locked_range(io_tree, |
741 | inode, async_extent->start, | |
d397712b | 742 | async_extent->start + |
771ed689 CM |
743 | async_extent->ram_size - 1, |
744 | btrfs_get_extent, | |
745 | WB_SYNC_ALL); | |
3e04e7f1 JB |
746 | else if (ret) |
747 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
748 | kfree(async_extent); |
749 | cond_resched(); | |
750 | continue; | |
751 | } | |
752 | ||
753 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 754 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 755 | |
18513091 | 756 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
757 | async_extent->compressed_size, |
758 | async_extent->compressed_size, | |
e570fd27 | 759 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 760 | if (ret) { |
40ae837b | 761 | free_async_extent_pages(async_extent); |
3e04e7f1 | 762 | |
fdf8e2ea JB |
763 | if (ret == -ENOSPC) { |
764 | unlock_extent(io_tree, async_extent->start, | |
765 | async_extent->start + | |
766 | async_extent->ram_size - 1); | |
ce62003f LB |
767 | |
768 | /* | |
769 | * we need to redirty the pages if we decide to | |
770 | * fallback to uncompressed IO, otherwise we | |
771 | * will not submit these pages down to lower | |
772 | * layers. | |
773 | */ | |
774 | extent_range_redirty_for_io(inode, | |
775 | async_extent->start, | |
776 | async_extent->start + | |
777 | async_extent->ram_size - 1); | |
778 | ||
79787eaa | 779 | goto retry; |
fdf8e2ea | 780 | } |
3e04e7f1 | 781 | goto out_free; |
f5a84ee3 | 782 | } |
c2167754 YZ |
783 | /* |
784 | * here we're doing allocation and writeback of the | |
785 | * compressed pages | |
786 | */ | |
787 | btrfs_drop_extent_cache(inode, async_extent->start, | |
788 | async_extent->start + | |
789 | async_extent->ram_size - 1, 0); | |
790 | ||
172ddd60 | 791 | em = alloc_extent_map(); |
b9aa55be LB |
792 | if (!em) { |
793 | ret = -ENOMEM; | |
3e04e7f1 | 794 | goto out_free_reserve; |
b9aa55be | 795 | } |
771ed689 CM |
796 | em->start = async_extent->start; |
797 | em->len = async_extent->ram_size; | |
445a6944 | 798 | em->orig_start = em->start; |
2ab28f32 JB |
799 | em->mod_start = em->start; |
800 | em->mod_len = em->len; | |
c8b97818 | 801 | |
771ed689 CM |
802 | em->block_start = ins.objectid; |
803 | em->block_len = ins.offset; | |
b4939680 | 804 | em->orig_block_len = ins.offset; |
cc95bef6 | 805 | em->ram_bytes = async_extent->ram_size; |
0b246afa | 806 | em->bdev = fs_info->fs_devices->latest_bdev; |
261507a0 | 807 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
808 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
809 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 810 | em->generation = -1; |
771ed689 | 811 | |
d397712b | 812 | while (1) { |
890871be | 813 | write_lock(&em_tree->lock); |
09a2a8f9 | 814 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 815 | write_unlock(&em_tree->lock); |
771ed689 CM |
816 | if (ret != -EEXIST) { |
817 | free_extent_map(em); | |
818 | break; | |
819 | } | |
820 | btrfs_drop_extent_cache(inode, async_extent->start, | |
821 | async_extent->start + | |
822 | async_extent->ram_size - 1, 0); | |
823 | } | |
824 | ||
3e04e7f1 JB |
825 | if (ret) |
826 | goto out_free_reserve; | |
827 | ||
261507a0 LZ |
828 | ret = btrfs_add_ordered_extent_compress(inode, |
829 | async_extent->start, | |
830 | ins.objectid, | |
831 | async_extent->ram_size, | |
832 | ins.offset, | |
833 | BTRFS_ORDERED_COMPRESSED, | |
834 | async_extent->compress_type); | |
d9f85963 FM |
835 | if (ret) { |
836 | btrfs_drop_extent_cache(inode, async_extent->start, | |
837 | async_extent->start + | |
838 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 839 | goto out_free_reserve; |
d9f85963 | 840 | } |
0b246afa | 841 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
771ed689 | 842 | |
771ed689 CM |
843 | /* |
844 | * clear dirty, set writeback and unlock the pages. | |
845 | */ | |
c2790a2e | 846 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
847 | async_extent->start + |
848 | async_extent->ram_size - 1, | |
a791e35e CM |
849 | async_extent->start + |
850 | async_extent->ram_size - 1, | |
151a41bc JB |
851 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
852 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 853 | PAGE_SET_WRITEBACK); |
771ed689 | 854 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
855 | async_extent->start, |
856 | async_extent->ram_size, | |
857 | ins.objectid, | |
858 | ins.offset, async_extent->pages, | |
859 | async_extent->nr_pages); | |
fce2a4e6 FM |
860 | if (ret) { |
861 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
862 | struct page *p = async_extent->pages[0]; | |
863 | const u64 start = async_extent->start; | |
864 | const u64 end = start + async_extent->ram_size - 1; | |
865 | ||
866 | p->mapping = inode->i_mapping; | |
867 | tree->ops->writepage_end_io_hook(p, start, end, | |
868 | NULL, 0); | |
869 | p->mapping = NULL; | |
ba8b04c1 QW |
870 | extent_clear_unlock_delalloc(inode, start, end, end, |
871 | NULL, 0, | |
fce2a4e6 FM |
872 | PAGE_END_WRITEBACK | |
873 | PAGE_SET_ERROR); | |
40ae837b | 874 | free_async_extent_pages(async_extent); |
fce2a4e6 | 875 | } |
771ed689 CM |
876 | alloc_hint = ins.objectid + ins.offset; |
877 | kfree(async_extent); | |
878 | cond_resched(); | |
879 | } | |
dec8f175 | 880 | return; |
3e04e7f1 | 881 | out_free_reserve: |
0b246afa | 882 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 883 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 884 | out_free: |
c2790a2e | 885 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
886 | async_extent->start + |
887 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
888 | async_extent->start + |
889 | async_extent->ram_size - 1, | |
c2790a2e | 890 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
891 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
892 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
893 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
894 | PAGE_SET_ERROR); | |
40ae837b | 895 | free_async_extent_pages(async_extent); |
79787eaa | 896 | kfree(async_extent); |
3e04e7f1 | 897 | goto again; |
771ed689 CM |
898 | } |
899 | ||
4b46fce2 JB |
900 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
901 | u64 num_bytes) | |
902 | { | |
903 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
904 | struct extent_map *em; | |
905 | u64 alloc_hint = 0; | |
906 | ||
907 | read_lock(&em_tree->lock); | |
908 | em = search_extent_mapping(em_tree, start, num_bytes); | |
909 | if (em) { | |
910 | /* | |
911 | * if block start isn't an actual block number then find the | |
912 | * first block in this inode and use that as a hint. If that | |
913 | * block is also bogus then just don't worry about it. | |
914 | */ | |
915 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
916 | free_extent_map(em); | |
917 | em = search_extent_mapping(em_tree, 0, 0); | |
918 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
919 | alloc_hint = em->block_start; | |
920 | if (em) | |
921 | free_extent_map(em); | |
922 | } else { | |
923 | alloc_hint = em->block_start; | |
924 | free_extent_map(em); | |
925 | } | |
926 | } | |
927 | read_unlock(&em_tree->lock); | |
928 | ||
929 | return alloc_hint; | |
930 | } | |
931 | ||
771ed689 CM |
932 | /* |
933 | * when extent_io.c finds a delayed allocation range in the file, | |
934 | * the call backs end up in this code. The basic idea is to | |
935 | * allocate extents on disk for the range, and create ordered data structs | |
936 | * in ram to track those extents. | |
937 | * | |
938 | * locked_page is the page that writepage had locked already. We use | |
939 | * it to make sure we don't do extra locks or unlocks. | |
940 | * | |
941 | * *page_started is set to one if we unlock locked_page and do everything | |
942 | * required to start IO on it. It may be clean and already done with | |
943 | * IO when we return. | |
944 | */ | |
00361589 JB |
945 | static noinline int cow_file_range(struct inode *inode, |
946 | struct page *locked_page, | |
dda3245e WX |
947 | u64 start, u64 end, u64 delalloc_end, |
948 | int *page_started, unsigned long *nr_written, | |
949 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 950 | { |
0b246afa | 951 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
953 | u64 alloc_hint = 0; |
954 | u64 num_bytes; | |
955 | unsigned long ram_size; | |
956 | u64 disk_num_bytes; | |
957 | u64 cur_alloc_size; | |
0b246afa | 958 | u64 blocksize = fs_info->sectorsize; |
771ed689 CM |
959 | struct btrfs_key ins; |
960 | struct extent_map *em; | |
961 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
962 | int ret = 0; | |
963 | ||
02ecd2c2 JB |
964 | if (btrfs_is_free_space_inode(inode)) { |
965 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
966 | ret = -EINVAL; |
967 | goto out_unlock; | |
02ecd2c2 | 968 | } |
771ed689 | 969 | |
fda2832f | 970 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
971 | num_bytes = max(blocksize, num_bytes); |
972 | disk_num_bytes = num_bytes; | |
771ed689 | 973 | |
26d30f85 | 974 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
4cb5300b | 975 | |
771ed689 CM |
976 | if (start == 0) { |
977 | /* lets try to make an inline extent */ | |
f74670f7 AJ |
978 | ret = cow_file_range_inline(root, inode, start, end, 0, |
979 | BTRFS_COMPRESS_NONE, NULL); | |
771ed689 | 980 | if (ret == 0) { |
ba8b04c1 QW |
981 | extent_clear_unlock_delalloc(inode, start, end, |
982 | delalloc_end, NULL, | |
c2790a2e | 983 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc | 984 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
985 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
986 | PAGE_END_WRITEBACK); | |
18513091 WX |
987 | btrfs_free_reserved_data_space_noquota(inode, start, |
988 | end - start + 1); | |
771ed689 | 989 | *nr_written = *nr_written + |
09cbfeaf | 990 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 991 | *page_started = 1; |
771ed689 | 992 | goto out; |
79787eaa | 993 | } else if (ret < 0) { |
79787eaa | 994 | goto out_unlock; |
771ed689 CM |
995 | } |
996 | } | |
997 | ||
998 | BUG_ON(disk_num_bytes > | |
0b246afa | 999 | btrfs_super_total_bytes(fs_info->super_copy)); |
771ed689 | 1000 | |
4b46fce2 | 1001 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
1002 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
1003 | ||
d397712b | 1004 | while (disk_num_bytes > 0) { |
a791e35e CM |
1005 | unsigned long op; |
1006 | ||
287a0ab9 | 1007 | cur_alloc_size = disk_num_bytes; |
18513091 | 1008 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
0b246afa | 1009 | fs_info->sectorsize, 0, alloc_hint, |
e570fd27 | 1010 | &ins, 1, 1); |
00361589 | 1011 | if (ret < 0) |
79787eaa | 1012 | goto out_unlock; |
d397712b | 1013 | |
172ddd60 | 1014 | em = alloc_extent_map(); |
b9aa55be LB |
1015 | if (!em) { |
1016 | ret = -ENOMEM; | |
ace68bac | 1017 | goto out_reserve; |
b9aa55be | 1018 | } |
e6dcd2dc | 1019 | em->start = start; |
445a6944 | 1020 | em->orig_start = em->start; |
771ed689 CM |
1021 | ram_size = ins.offset; |
1022 | em->len = ins.offset; | |
2ab28f32 JB |
1023 | em->mod_start = em->start; |
1024 | em->mod_len = em->len; | |
c8b97818 | 1025 | |
e6dcd2dc | 1026 | em->block_start = ins.objectid; |
c8b97818 | 1027 | em->block_len = ins.offset; |
b4939680 | 1028 | em->orig_block_len = ins.offset; |
cc95bef6 | 1029 | em->ram_bytes = ram_size; |
0b246afa | 1030 | em->bdev = fs_info->fs_devices->latest_bdev; |
7f3c74fb | 1031 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 1032 | em->generation = -1; |
c8b97818 | 1033 | |
d397712b | 1034 | while (1) { |
890871be | 1035 | write_lock(&em_tree->lock); |
09a2a8f9 | 1036 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1037 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1038 | if (ret != -EEXIST) { |
1039 | free_extent_map(em); | |
1040 | break; | |
1041 | } | |
1042 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1043 | start + ram_size - 1, 0); |
e6dcd2dc | 1044 | } |
ace68bac LB |
1045 | if (ret) |
1046 | goto out_reserve; | |
e6dcd2dc | 1047 | |
98d20f67 | 1048 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1049 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1050 | ram_size, cur_alloc_size, 0); |
ace68bac | 1051 | if (ret) |
d9f85963 | 1052 | goto out_drop_extent_cache; |
c8b97818 | 1053 | |
17d217fe YZ |
1054 | if (root->root_key.objectid == |
1055 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1056 | ret = btrfs_reloc_clone_csums(inode, start, | |
1057 | cur_alloc_size); | |
00361589 | 1058 | if (ret) |
d9f85963 | 1059 | goto out_drop_extent_cache; |
17d217fe YZ |
1060 | } |
1061 | ||
0b246afa | 1062 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
9cfa3e34 | 1063 | |
d397712b | 1064 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1065 | break; |
d397712b | 1066 | |
c8b97818 CM |
1067 | /* we're not doing compressed IO, don't unlock the first |
1068 | * page (which the caller expects to stay locked), don't | |
1069 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1070 | * |
1071 | * Do set the Private2 bit so we know this page was properly | |
1072 | * setup for writepage | |
c8b97818 | 1073 | */ |
c2790a2e JB |
1074 | op = unlock ? PAGE_UNLOCK : 0; |
1075 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1076 | |
c2790a2e | 1077 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1078 | start + ram_size - 1, |
1079 | delalloc_end, locked_page, | |
c2790a2e JB |
1080 | EXTENT_LOCKED | EXTENT_DELALLOC, |
1081 | op); | |
c8b97818 | 1082 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1083 | num_bytes -= cur_alloc_size; |
1084 | alloc_hint = ins.objectid + ins.offset; | |
1085 | start += cur_alloc_size; | |
b888db2b | 1086 | } |
79787eaa | 1087 | out: |
be20aa9d | 1088 | return ret; |
b7d5b0a8 | 1089 | |
d9f85963 FM |
1090 | out_drop_extent_cache: |
1091 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1092 | out_reserve: |
0b246afa | 1093 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
2ff7e61e | 1094 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
79787eaa | 1095 | out_unlock: |
ba8b04c1 QW |
1096 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1097 | locked_page, | |
151a41bc JB |
1098 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1099 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1100 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1101 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1102 | goto out; |
771ed689 | 1103 | } |
c8b97818 | 1104 | |
771ed689 CM |
1105 | /* |
1106 | * work queue call back to started compression on a file and pages | |
1107 | */ | |
1108 | static noinline void async_cow_start(struct btrfs_work *work) | |
1109 | { | |
1110 | struct async_cow *async_cow; | |
1111 | int num_added = 0; | |
1112 | async_cow = container_of(work, struct async_cow, work); | |
1113 | ||
1114 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1115 | async_cow->start, async_cow->end, async_cow, | |
1116 | &num_added); | |
8180ef88 | 1117 | if (num_added == 0) { |
cb77fcd8 | 1118 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1119 | async_cow->inode = NULL; |
8180ef88 | 1120 | } |
771ed689 CM |
1121 | } |
1122 | ||
1123 | /* | |
1124 | * work queue call back to submit previously compressed pages | |
1125 | */ | |
1126 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1127 | { | |
0b246afa | 1128 | struct btrfs_fs_info *fs_info; |
771ed689 CM |
1129 | struct async_cow *async_cow; |
1130 | struct btrfs_root *root; | |
1131 | unsigned long nr_pages; | |
1132 | ||
1133 | async_cow = container_of(work, struct async_cow, work); | |
1134 | ||
1135 | root = async_cow->root; | |
0b246afa | 1136 | fs_info = root->fs_info; |
09cbfeaf KS |
1137 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1138 | PAGE_SHIFT; | |
771ed689 | 1139 | |
ee863954 DS |
1140 | /* |
1141 | * atomic_sub_return implies a barrier for waitqueue_active | |
1142 | */ | |
0b246afa | 1143 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
ee22184b | 1144 | 5 * SZ_1M && |
0b246afa JM |
1145 | waitqueue_active(&fs_info->async_submit_wait)) |
1146 | wake_up(&fs_info->async_submit_wait); | |
771ed689 | 1147 | |
d397712b | 1148 | if (async_cow->inode) |
771ed689 | 1149 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1150 | } |
c8b97818 | 1151 | |
771ed689 CM |
1152 | static noinline void async_cow_free(struct btrfs_work *work) |
1153 | { | |
1154 | struct async_cow *async_cow; | |
1155 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1156 | if (async_cow->inode) |
cb77fcd8 | 1157 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1158 | kfree(async_cow); |
1159 | } | |
1160 | ||
1161 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1162 | u64 start, u64 end, int *page_started, | |
1163 | unsigned long *nr_written) | |
1164 | { | |
0b246afa | 1165 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
771ed689 CM |
1166 | struct async_cow *async_cow; |
1167 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1168 | unsigned long nr_pages; | |
1169 | u64 cur_end; | |
771ed689 | 1170 | |
a3429ab7 CM |
1171 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1172 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1173 | while (start < end) { |
771ed689 | 1174 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1175 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1176 | async_cow->inode = igrab(inode); |
771ed689 CM |
1177 | async_cow->root = root; |
1178 | async_cow->locked_page = locked_page; | |
1179 | async_cow->start = start; | |
1180 | ||
f79707b0 | 1181 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
0b246afa | 1182 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1183 | cur_end = end; |
1184 | else | |
ee22184b | 1185 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1186 | |
1187 | async_cow->end = cur_end; | |
1188 | INIT_LIST_HEAD(&async_cow->extents); | |
1189 | ||
9e0af237 LB |
1190 | btrfs_init_work(&async_cow->work, |
1191 | btrfs_delalloc_helper, | |
1192 | async_cow_start, async_cow_submit, | |
1193 | async_cow_free); | |
771ed689 | 1194 | |
09cbfeaf KS |
1195 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1196 | PAGE_SHIFT; | |
0b246afa | 1197 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
771ed689 | 1198 | |
0b246afa | 1199 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
771ed689 | 1200 | |
0b246afa JM |
1201 | while (atomic_read(&fs_info->async_submit_draining) && |
1202 | atomic_read(&fs_info->async_delalloc_pages)) { | |
1203 | wait_event(fs_info->async_submit_wait, | |
1204 | (atomic_read(&fs_info->async_delalloc_pages) == | |
1205 | 0)); | |
771ed689 CM |
1206 | } |
1207 | ||
1208 | *nr_written += nr_pages; | |
1209 | start = cur_end + 1; | |
1210 | } | |
1211 | *page_started = 1; | |
1212 | return 0; | |
be20aa9d CM |
1213 | } |
1214 | ||
2ff7e61e | 1215 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
17d217fe YZ |
1216 | u64 bytenr, u64 num_bytes) |
1217 | { | |
1218 | int ret; | |
1219 | struct btrfs_ordered_sum *sums; | |
1220 | LIST_HEAD(list); | |
1221 | ||
0b246afa | 1222 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
a2de733c | 1223 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1224 | if (ret == 0 && list_empty(&list)) |
1225 | return 0; | |
1226 | ||
1227 | while (!list_empty(&list)) { | |
1228 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1229 | list_del(&sums->list); | |
1230 | kfree(sums); | |
1231 | } | |
1232 | return 1; | |
1233 | } | |
1234 | ||
d352ac68 CM |
1235 | /* |
1236 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1237 | * of the extents that exist in the file, and COWs the file as required. | |
1238 | * | |
1239 | * If no cow copies or snapshots exist, we write directly to the existing | |
1240 | * blocks on disk | |
1241 | */ | |
7f366cfe CM |
1242 | static noinline int run_delalloc_nocow(struct inode *inode, |
1243 | struct page *locked_page, | |
771ed689 CM |
1244 | u64 start, u64 end, int *page_started, int force, |
1245 | unsigned long *nr_written) | |
be20aa9d | 1246 | { |
0b246afa | 1247 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
be20aa9d | 1248 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1249 | struct btrfs_trans_handle *trans; |
be20aa9d | 1250 | struct extent_buffer *leaf; |
be20aa9d | 1251 | struct btrfs_path *path; |
80ff3856 | 1252 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1253 | struct btrfs_key found_key; |
80ff3856 YZ |
1254 | u64 cow_start; |
1255 | u64 cur_offset; | |
1256 | u64 extent_end; | |
5d4f98a2 | 1257 | u64 extent_offset; |
80ff3856 YZ |
1258 | u64 disk_bytenr; |
1259 | u64 num_bytes; | |
b4939680 | 1260 | u64 disk_num_bytes; |
cc95bef6 | 1261 | u64 ram_bytes; |
80ff3856 | 1262 | int extent_type; |
79787eaa | 1263 | int ret, err; |
d899e052 | 1264 | int type; |
80ff3856 YZ |
1265 | int nocow; |
1266 | int check_prev = 1; | |
82d5902d | 1267 | bool nolock; |
4a0cc7ca | 1268 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
be20aa9d CM |
1269 | |
1270 | path = btrfs_alloc_path(); | |
17ca04af | 1271 | if (!path) { |
ba8b04c1 QW |
1272 | extent_clear_unlock_delalloc(inode, start, end, end, |
1273 | locked_page, | |
c2790a2e | 1274 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1275 | EXTENT_DO_ACCOUNTING | |
1276 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1277 | PAGE_CLEAR_DIRTY | |
1278 | PAGE_SET_WRITEBACK | | |
1279 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1280 | return -ENOMEM; |
17ca04af | 1281 | } |
82d5902d | 1282 | |
83eea1f1 | 1283 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1284 | |
1285 | if (nolock) | |
7a7eaa40 | 1286 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1287 | else |
7a7eaa40 | 1288 | trans = btrfs_join_transaction(root); |
ff5714cc | 1289 | |
79787eaa | 1290 | if (IS_ERR(trans)) { |
ba8b04c1 QW |
1291 | extent_clear_unlock_delalloc(inode, start, end, end, |
1292 | locked_page, | |
c2790a2e | 1293 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1294 | EXTENT_DO_ACCOUNTING | |
1295 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1296 | PAGE_CLEAR_DIRTY | |
1297 | PAGE_SET_WRITEBACK | | |
1298 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1299 | btrfs_free_path(path); |
1300 | return PTR_ERR(trans); | |
1301 | } | |
1302 | ||
0b246afa | 1303 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
be20aa9d | 1304 | |
80ff3856 YZ |
1305 | cow_start = (u64)-1; |
1306 | cur_offset = start; | |
1307 | while (1) { | |
33345d01 | 1308 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1309 | cur_offset, 0); |
d788a349 | 1310 | if (ret < 0) |
79787eaa | 1311 | goto error; |
80ff3856 YZ |
1312 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1313 | leaf = path->nodes[0]; | |
1314 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1315 | path->slots[0] - 1); | |
33345d01 | 1316 | if (found_key.objectid == ino && |
80ff3856 YZ |
1317 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1318 | path->slots[0]--; | |
1319 | } | |
1320 | check_prev = 0; | |
1321 | next_slot: | |
1322 | leaf = path->nodes[0]; | |
1323 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1324 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1325 | if (ret < 0) |
79787eaa | 1326 | goto error; |
80ff3856 YZ |
1327 | if (ret > 0) |
1328 | break; | |
1329 | leaf = path->nodes[0]; | |
1330 | } | |
be20aa9d | 1331 | |
80ff3856 YZ |
1332 | nocow = 0; |
1333 | disk_bytenr = 0; | |
17d217fe | 1334 | num_bytes = 0; |
80ff3856 YZ |
1335 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1336 | ||
1d512cb7 FM |
1337 | if (found_key.objectid > ino) |
1338 | break; | |
1339 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1340 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1341 | path->slots[0]++; | |
1342 | goto next_slot; | |
1343 | } | |
1344 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1345 | found_key.offset > end) |
1346 | break; | |
1347 | ||
1348 | if (found_key.offset > cur_offset) { | |
1349 | extent_end = found_key.offset; | |
e9061e21 | 1350 | extent_type = 0; |
80ff3856 YZ |
1351 | goto out_check; |
1352 | } | |
1353 | ||
1354 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1355 | struct btrfs_file_extent_item); | |
1356 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1357 | ||
cc95bef6 | 1358 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1359 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1360 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1361 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1362 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1363 | extent_end = found_key.offset + |
1364 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1365 | disk_num_bytes = |
1366 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1367 | if (extent_end <= start) { |
1368 | path->slots[0]++; | |
1369 | goto next_slot; | |
1370 | } | |
17d217fe YZ |
1371 | if (disk_bytenr == 0) |
1372 | goto out_check; | |
80ff3856 YZ |
1373 | if (btrfs_file_extent_compression(leaf, fi) || |
1374 | btrfs_file_extent_encryption(leaf, fi) || | |
1375 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1376 | goto out_check; | |
d899e052 YZ |
1377 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1378 | goto out_check; | |
2ff7e61e | 1379 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
80ff3856 | 1380 | goto out_check; |
33345d01 | 1381 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1382 | found_key.offset - |
1383 | extent_offset, disk_bytenr)) | |
17d217fe | 1384 | goto out_check; |
5d4f98a2 | 1385 | disk_bytenr += extent_offset; |
17d217fe YZ |
1386 | disk_bytenr += cur_offset - found_key.offset; |
1387 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1388 | /* |
1389 | * if there are pending snapshots for this root, | |
1390 | * we fall into common COW way. | |
1391 | */ | |
1392 | if (!nolock) { | |
9ea24bbe | 1393 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1394 | if (!err) |
1395 | goto out_check; | |
1396 | } | |
17d217fe YZ |
1397 | /* |
1398 | * force cow if csum exists in the range. | |
1399 | * this ensure that csum for a given extent are | |
1400 | * either valid or do not exist. | |
1401 | */ | |
2ff7e61e JM |
1402 | if (csum_exist_in_range(fs_info, disk_bytenr, |
1403 | num_bytes)) | |
17d217fe | 1404 | goto out_check; |
0b246afa | 1405 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
f78c436c | 1406 | goto out_check; |
80ff3856 YZ |
1407 | nocow = 1; |
1408 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1409 | extent_end = found_key.offset + | |
514ac8ad CM |
1410 | btrfs_file_extent_inline_len(leaf, |
1411 | path->slots[0], fi); | |
da17066c | 1412 | extent_end = ALIGN(extent_end, |
0b246afa | 1413 | fs_info->sectorsize); |
80ff3856 YZ |
1414 | } else { |
1415 | BUG_ON(1); | |
1416 | } | |
1417 | out_check: | |
1418 | if (extent_end <= start) { | |
1419 | path->slots[0]++; | |
e9894fd3 | 1420 | if (!nolock && nocow) |
9ea24bbe | 1421 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1422 | if (nocow) |
0b246afa | 1423 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
80ff3856 YZ |
1424 | goto next_slot; |
1425 | } | |
1426 | if (!nocow) { | |
1427 | if (cow_start == (u64)-1) | |
1428 | cow_start = cur_offset; | |
1429 | cur_offset = extent_end; | |
1430 | if (cur_offset > end) | |
1431 | break; | |
1432 | path->slots[0]++; | |
1433 | goto next_slot; | |
7ea394f1 YZ |
1434 | } |
1435 | ||
b3b4aa74 | 1436 | btrfs_release_path(path); |
80ff3856 | 1437 | if (cow_start != (u64)-1) { |
00361589 JB |
1438 | ret = cow_file_range(inode, locked_page, |
1439 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1440 | end, page_started, nr_written, 1, |
1441 | NULL); | |
e9894fd3 WS |
1442 | if (ret) { |
1443 | if (!nolock && nocow) | |
9ea24bbe | 1444 | btrfs_end_write_no_snapshoting(root); |
f78c436c | 1445 | if (nocow) |
0b246afa | 1446 | btrfs_dec_nocow_writers(fs_info, |
f78c436c | 1447 | disk_bytenr); |
79787eaa | 1448 | goto error; |
e9894fd3 | 1449 | } |
80ff3856 | 1450 | cow_start = (u64)-1; |
7ea394f1 | 1451 | } |
80ff3856 | 1452 | |
d899e052 YZ |
1453 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1454 | struct extent_map *em; | |
1455 | struct extent_map_tree *em_tree; | |
1456 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1457 | em = alloc_extent_map(); |
79787eaa | 1458 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1459 | em->start = cur_offset; |
70c8a91c | 1460 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1461 | em->len = num_bytes; |
1462 | em->block_len = num_bytes; | |
1463 | em->block_start = disk_bytenr; | |
b4939680 | 1464 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1465 | em->ram_bytes = ram_bytes; |
0b246afa | 1466 | em->bdev = fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1467 | em->mod_start = em->start; |
1468 | em->mod_len = em->len; | |
d899e052 | 1469 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1470 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1471 | em->generation = -1; |
d899e052 | 1472 | while (1) { |
890871be | 1473 | write_lock(&em_tree->lock); |
09a2a8f9 | 1474 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1475 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1476 | if (ret != -EEXIST) { |
1477 | free_extent_map(em); | |
1478 | break; | |
1479 | } | |
1480 | btrfs_drop_extent_cache(inode, em->start, | |
1481 | em->start + em->len - 1, 0); | |
1482 | } | |
1483 | type = BTRFS_ORDERED_PREALLOC; | |
1484 | } else { | |
1485 | type = BTRFS_ORDERED_NOCOW; | |
1486 | } | |
80ff3856 YZ |
1487 | |
1488 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1489 | num_bytes, num_bytes, type); |
f78c436c | 1490 | if (nocow) |
0b246afa | 1491 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
79787eaa | 1492 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1493 | |
efa56464 YZ |
1494 | if (root->root_key.objectid == |
1495 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1496 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1497 | num_bytes); | |
e9894fd3 WS |
1498 | if (ret) { |
1499 | if (!nolock && nocow) | |
9ea24bbe | 1500 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1501 | goto error; |
e9894fd3 | 1502 | } |
efa56464 YZ |
1503 | } |
1504 | ||
c2790a2e | 1505 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1506 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1507 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1508 | EXTENT_DELALLOC | |
1509 | EXTENT_CLEAR_DATA_RESV, | |
1510 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1511 | ||
e9894fd3 | 1512 | if (!nolock && nocow) |
9ea24bbe | 1513 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1514 | cur_offset = extent_end; |
1515 | if (cur_offset > end) | |
1516 | break; | |
be20aa9d | 1517 | } |
b3b4aa74 | 1518 | btrfs_release_path(path); |
80ff3856 | 1519 | |
17ca04af | 1520 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1521 | cow_start = cur_offset; |
17ca04af JB |
1522 | cur_offset = end; |
1523 | } | |
1524 | ||
80ff3856 | 1525 | if (cow_start != (u64)-1) { |
dda3245e WX |
1526 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1527 | page_started, nr_written, 1, NULL); | |
d788a349 | 1528 | if (ret) |
79787eaa | 1529 | goto error; |
80ff3856 YZ |
1530 | } |
1531 | ||
79787eaa | 1532 | error: |
3a45bb20 | 1533 | err = btrfs_end_transaction(trans); |
79787eaa JM |
1534 | if (!ret) |
1535 | ret = err; | |
1536 | ||
17ca04af | 1537 | if (ret && cur_offset < end) |
ba8b04c1 | 1538 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1539 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1540 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1541 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1542 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1543 | PAGE_SET_WRITEBACK | |
1544 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1545 | btrfs_free_path(path); |
79787eaa | 1546 | return ret; |
be20aa9d CM |
1547 | } |
1548 | ||
47059d93 WS |
1549 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1550 | { | |
1551 | ||
1552 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1553 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1554 | return 0; | |
1555 | ||
1556 | /* | |
1557 | * @defrag_bytes is a hint value, no spinlock held here, | |
1558 | * if is not zero, it means the file is defragging. | |
1559 | * Force cow if given extent needs to be defragged. | |
1560 | */ | |
1561 | if (BTRFS_I(inode)->defrag_bytes && | |
1562 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1563 | EXTENT_DEFRAG, 0, NULL)) | |
1564 | return 1; | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
d352ac68 CM |
1569 | /* |
1570 | * extent_io.c call back to do delayed allocation processing | |
1571 | */ | |
c8b97818 | 1572 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1573 | u64 start, u64 end, int *page_started, |
1574 | unsigned long *nr_written) | |
be20aa9d | 1575 | { |
be20aa9d | 1576 | int ret; |
47059d93 | 1577 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1578 | |
47059d93 | 1579 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1580 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1581 | page_started, 1, nr_written); |
47059d93 | 1582 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1583 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1584 | page_started, 0, nr_written); |
7816030e | 1585 | } else if (!inode_need_compress(inode)) { |
dda3245e WX |
1586 | ret = cow_file_range(inode, locked_page, start, end, end, |
1587 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1588 | } else { |
1589 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1590 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1591 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1592 | page_started, nr_written); |
7ddf5a42 | 1593 | } |
b888db2b CM |
1594 | return ret; |
1595 | } | |
1596 | ||
1bf85046 JM |
1597 | static void btrfs_split_extent_hook(struct inode *inode, |
1598 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1599 | { |
dcab6a3b JB |
1600 | u64 size; |
1601 | ||
0ca1f7ce | 1602 | /* not delalloc, ignore it */ |
9ed74f2d | 1603 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1604 | return; |
9ed74f2d | 1605 | |
dcab6a3b JB |
1606 | size = orig->end - orig->start + 1; |
1607 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
823bb20a | 1608 | u32 num_extents; |
dcab6a3b JB |
1609 | u64 new_size; |
1610 | ||
1611 | /* | |
ba117213 JB |
1612 | * See the explanation in btrfs_merge_extent_hook, the same |
1613 | * applies here, just in reverse. | |
dcab6a3b JB |
1614 | */ |
1615 | new_size = orig->end - split + 1; | |
823bb20a | 1616 | num_extents = count_max_extents(new_size); |
ba117213 | 1617 | new_size = split - orig->start; |
823bb20a DS |
1618 | num_extents += count_max_extents(new_size); |
1619 | if (count_max_extents(size) >= num_extents) | |
dcab6a3b JB |
1620 | return; |
1621 | } | |
1622 | ||
9e0baf60 JB |
1623 | spin_lock(&BTRFS_I(inode)->lock); |
1624 | BTRFS_I(inode)->outstanding_extents++; | |
1625 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1626 | } |
1627 | ||
1628 | /* | |
1629 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1630 | * extents so we can keep track of new extents that are just merged onto old | |
1631 | * extents, such as when we are doing sequential writes, so we can properly | |
1632 | * account for the metadata space we'll need. | |
1633 | */ | |
1bf85046 JM |
1634 | static void btrfs_merge_extent_hook(struct inode *inode, |
1635 | struct extent_state *new, | |
1636 | struct extent_state *other) | |
9ed74f2d | 1637 | { |
dcab6a3b | 1638 | u64 new_size, old_size; |
823bb20a | 1639 | u32 num_extents; |
dcab6a3b | 1640 | |
9ed74f2d JB |
1641 | /* not delalloc, ignore it */ |
1642 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1643 | return; |
9ed74f2d | 1644 | |
8461a3de JB |
1645 | if (new->start > other->start) |
1646 | new_size = new->end - other->start + 1; | |
1647 | else | |
1648 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1649 | |
1650 | /* we're not bigger than the max, unreserve the space and go */ | |
1651 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1652 | spin_lock(&BTRFS_I(inode)->lock); | |
1653 | BTRFS_I(inode)->outstanding_extents--; | |
1654 | spin_unlock(&BTRFS_I(inode)->lock); | |
1655 | return; | |
1656 | } | |
1657 | ||
1658 | /* | |
ba117213 JB |
1659 | * We have to add up either side to figure out how many extents were |
1660 | * accounted for before we merged into one big extent. If the number of | |
1661 | * extents we accounted for is <= the amount we need for the new range | |
1662 | * then we can return, otherwise drop. Think of it like this | |
1663 | * | |
1664 | * [ 4k][MAX_SIZE] | |
1665 | * | |
1666 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1667 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1668 | * we have 1 so they are == and we can return. But in this case | |
1669 | * | |
1670 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1671 | * | |
1672 | * Each range on their own accounts for 2 extents, but merged together | |
1673 | * they are only 3 extents worth of accounting, so we need to drop in | |
1674 | * this case. | |
dcab6a3b | 1675 | */ |
ba117213 | 1676 | old_size = other->end - other->start + 1; |
823bb20a | 1677 | num_extents = count_max_extents(old_size); |
ba117213 | 1678 | old_size = new->end - new->start + 1; |
823bb20a DS |
1679 | num_extents += count_max_extents(old_size); |
1680 | if (count_max_extents(new_size) >= num_extents) | |
dcab6a3b JB |
1681 | return; |
1682 | ||
9e0baf60 JB |
1683 | spin_lock(&BTRFS_I(inode)->lock); |
1684 | BTRFS_I(inode)->outstanding_extents--; | |
1685 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1686 | } |
1687 | ||
eb73c1b7 MX |
1688 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1689 | struct inode *inode) | |
1690 | { | |
0b246afa JM |
1691 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1692 | ||
eb73c1b7 MX |
1693 | spin_lock(&root->delalloc_lock); |
1694 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1695 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1696 | &root->delalloc_inodes); | |
1697 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1698 | &BTRFS_I(inode)->runtime_flags); | |
1699 | root->nr_delalloc_inodes++; | |
1700 | if (root->nr_delalloc_inodes == 1) { | |
0b246afa | 1701 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1702 | BUG_ON(!list_empty(&root->delalloc_root)); |
1703 | list_add_tail(&root->delalloc_root, | |
0b246afa JM |
1704 | &fs_info->delalloc_roots); |
1705 | spin_unlock(&fs_info->delalloc_root_lock); | |
eb73c1b7 MX |
1706 | } |
1707 | } | |
1708 | spin_unlock(&root->delalloc_lock); | |
1709 | } | |
1710 | ||
1711 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1712 | struct inode *inode) | |
1713 | { | |
0b246afa JM |
1714 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1715 | ||
eb73c1b7 MX |
1716 | spin_lock(&root->delalloc_lock); |
1717 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1718 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1719 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1720 | &BTRFS_I(inode)->runtime_flags); | |
1721 | root->nr_delalloc_inodes--; | |
1722 | if (!root->nr_delalloc_inodes) { | |
0b246afa | 1723 | spin_lock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1724 | BUG_ON(list_empty(&root->delalloc_root)); |
1725 | list_del_init(&root->delalloc_root); | |
0b246afa | 1726 | spin_unlock(&fs_info->delalloc_root_lock); |
eb73c1b7 MX |
1727 | } |
1728 | } | |
1729 | spin_unlock(&root->delalloc_lock); | |
1730 | } | |
1731 | ||
d352ac68 CM |
1732 | /* |
1733 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1734 | * bytes in this file, and to maintain the list of inodes that | |
1735 | * have pending delalloc work to be done. | |
1736 | */ | |
1bf85046 | 1737 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1738 | struct extent_state *state, unsigned *bits) |
291d673e | 1739 | { |
9ed74f2d | 1740 | |
0b246afa JM |
1741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
1742 | ||
47059d93 WS |
1743 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1744 | WARN_ON(1); | |
75eff68e CM |
1745 | /* |
1746 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1747 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1748 | * bit, which is only set or cleared with irqs on |
1749 | */ | |
0ca1f7ce | 1750 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1752 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1753 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1754 | |
9e0baf60 | 1755 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1756 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1757 | } else { |
1758 | spin_lock(&BTRFS_I(inode)->lock); | |
1759 | BTRFS_I(inode)->outstanding_extents++; | |
1760 | spin_unlock(&BTRFS_I(inode)->lock); | |
1761 | } | |
287a0ab9 | 1762 | |
6a3891c5 | 1763 | /* For sanity tests */ |
0b246afa | 1764 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1765 | return; |
1766 | ||
0b246afa JM |
1767 | __percpu_counter_add(&fs_info->delalloc_bytes, len, |
1768 | fs_info->delalloc_batch); | |
df0af1a5 | 1769 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1770 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1771 | if (*bits & EXTENT_DEFRAG) |
1772 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1773 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1774 | &BTRFS_I(inode)->runtime_flags)) |
1775 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1776 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1777 | } |
291d673e CM |
1778 | } |
1779 | ||
d352ac68 CM |
1780 | /* |
1781 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1782 | */ | |
1bf85046 | 1783 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1784 | struct extent_state *state, |
9ee49a04 | 1785 | unsigned *bits) |
291d673e | 1786 | { |
0b246afa | 1787 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
47059d93 | 1788 | u64 len = state->end + 1 - state->start; |
823bb20a | 1789 | u32 num_extents = count_max_extents(len); |
47059d93 WS |
1790 | |
1791 | spin_lock(&BTRFS_I(inode)->lock); | |
1792 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1793 | BTRFS_I(inode)->defrag_bytes -= len; | |
1794 | spin_unlock(&BTRFS_I(inode)->lock); | |
1795 | ||
75eff68e CM |
1796 | /* |
1797 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1798 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1799 | * bit, which is only set or cleared with irqs on |
1800 | */ | |
0ca1f7ce | 1801 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1802 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1803 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1804 | |
9e0baf60 | 1805 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1806 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1807 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1808 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1809 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1810 | spin_unlock(&BTRFS_I(inode)->lock); |
1811 | } | |
0ca1f7ce | 1812 | |
b6d08f06 JB |
1813 | /* |
1814 | * We don't reserve metadata space for space cache inodes so we | |
1815 | * don't need to call dellalloc_release_metadata if there is an | |
1816 | * error. | |
1817 | */ | |
1818 | if (*bits & EXTENT_DO_ACCOUNTING && | |
0b246afa | 1819 | root != fs_info->tree_root) |
0ca1f7ce YZ |
1820 | btrfs_delalloc_release_metadata(inode, len); |
1821 | ||
6a3891c5 | 1822 | /* For sanity tests. */ |
0b246afa | 1823 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
1824 | return; |
1825 | ||
0cb59c99 | 1826 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
18513091 WX |
1827 | && do_list && !(state->state & EXTENT_NORESERVE) |
1828 | && (*bits & (EXTENT_DO_ACCOUNTING | | |
1829 | EXTENT_CLEAR_DATA_RESV))) | |
51773bec QW |
1830 | btrfs_free_reserved_data_space_noquota(inode, |
1831 | state->start, len); | |
9ed74f2d | 1832 | |
0b246afa JM |
1833 | __percpu_counter_add(&fs_info->delalloc_bytes, -len, |
1834 | fs_info->delalloc_batch); | |
df0af1a5 | 1835 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1836 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1837 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1838 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1839 | &BTRFS_I(inode)->runtime_flags)) |
1840 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1841 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1842 | } |
291d673e CM |
1843 | } |
1844 | ||
d352ac68 CM |
1845 | /* |
1846 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1847 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1848 | * |
1849 | * return 1 if page cannot be merged to bio | |
1850 | * return 0 if page can be merged to bio | |
1851 | * return error otherwise | |
d352ac68 | 1852 | */ |
81a75f67 | 1853 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1854 | size_t size, struct bio *bio, |
1855 | unsigned long bio_flags) | |
239b14b3 | 1856 | { |
0b246afa JM |
1857 | struct inode *inode = page->mapping->host; |
1858 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4f024f37 | 1859 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1860 | u64 length = 0; |
1861 | u64 map_length; | |
239b14b3 CM |
1862 | int ret; |
1863 | ||
771ed689 CM |
1864 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1865 | return 0; | |
1866 | ||
4f024f37 | 1867 | length = bio->bi_iter.bi_size; |
239b14b3 | 1868 | map_length = length; |
0b246afa JM |
1869 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
1870 | NULL, 0); | |
6f034ece LB |
1871 | if (ret < 0) |
1872 | return ret; | |
d397712b | 1873 | if (map_length < length + size) |
239b14b3 | 1874 | return 1; |
3444a972 | 1875 | return 0; |
239b14b3 CM |
1876 | } |
1877 | ||
d352ac68 CM |
1878 | /* |
1879 | * in order to insert checksums into the metadata in large chunks, | |
1880 | * we wait until bio submission time. All the pages in the bio are | |
1881 | * checksummed and sums are attached onto the ordered extent record. | |
1882 | * | |
1883 | * At IO completion time the cums attached on the ordered extent record | |
1884 | * are inserted into the btree | |
1885 | */ | |
81a75f67 MC |
1886 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
1887 | int mirror_num, unsigned long bio_flags, | |
eaf25d93 | 1888 | u64 bio_offset) |
065631f6 | 1889 | { |
065631f6 | 1890 | int ret = 0; |
e015640f | 1891 | |
2ff7e61e | 1892 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
79787eaa | 1893 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1894 | return 0; |
1895 | } | |
e015640f | 1896 | |
4a69a410 CM |
1897 | /* |
1898 | * in order to insert checksums into the metadata in large chunks, | |
1899 | * we wait until bio submission time. All the pages in the bio are | |
1900 | * checksummed and sums are attached onto the ordered extent record. | |
1901 | * | |
1902 | * At IO completion time the cums attached on the ordered extent record | |
1903 | * are inserted into the btree | |
1904 | */ | |
81a75f67 | 1905 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
eaf25d93 CM |
1906 | int mirror_num, unsigned long bio_flags, |
1907 | u64 bio_offset) | |
4a69a410 | 1908 | { |
2ff7e61e | 1909 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
61891923 SB |
1910 | int ret; |
1911 | ||
2ff7e61e | 1912 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
4246a0b6 CH |
1913 | if (ret) { |
1914 | bio->bi_error = ret; | |
1915 | bio_endio(bio); | |
1916 | } | |
61891923 | 1917 | return ret; |
44b8bd7e CM |
1918 | } |
1919 | ||
d352ac68 | 1920 | /* |
cad321ad CM |
1921 | * extent_io.c submission hook. This does the right thing for csum calculation |
1922 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1923 | */ |
81a75f67 | 1924 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
eaf25d93 CM |
1925 | int mirror_num, unsigned long bio_flags, |
1926 | u64 bio_offset) | |
44b8bd7e | 1927 | { |
0b246afa | 1928 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
44b8bd7e | 1929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0d51e28a | 1930 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
44b8bd7e | 1931 | int ret = 0; |
19b9bdb0 | 1932 | int skip_sum; |
b812ce28 | 1933 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1934 | |
6cbff00f | 1935 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1936 | |
83eea1f1 | 1937 | if (btrfs_is_free_space_inode(inode)) |
0d51e28a | 1938 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1939 | |
37226b21 | 1940 | if (bio_op(bio) != REQ_OP_WRITE) { |
0b246afa | 1941 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
5fd02043 | 1942 | if (ret) |
61891923 | 1943 | goto out; |
5fd02043 | 1944 | |
d20f7043 | 1945 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1946 | ret = btrfs_submit_compressed_read(inode, bio, |
1947 | mirror_num, | |
1948 | bio_flags); | |
1949 | goto out; | |
c2db1073 | 1950 | } else if (!skip_sum) { |
2ff7e61e | 1951 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
c2db1073 | 1952 | if (ret) |
61891923 | 1953 | goto out; |
c2db1073 | 1954 | } |
4d1b5fb4 | 1955 | goto mapit; |
b812ce28 | 1956 | } else if (async && !skip_sum) { |
17d217fe YZ |
1957 | /* csum items have already been cloned */ |
1958 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1959 | goto mapit; | |
19b9bdb0 | 1960 | /* we're doing a write, do the async checksumming */ |
0b246afa JM |
1961 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, |
1962 | bio_flags, bio_offset, | |
1963 | __btrfs_submit_bio_start, | |
1964 | __btrfs_submit_bio_done); | |
61891923 | 1965 | goto out; |
b812ce28 | 1966 | } else if (!skip_sum) { |
2ff7e61e | 1967 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
b812ce28 JB |
1968 | if (ret) |
1969 | goto out; | |
19b9bdb0 CM |
1970 | } |
1971 | ||
0b86a832 | 1972 | mapit: |
2ff7e61e | 1973 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
61891923 SB |
1974 | |
1975 | out: | |
4246a0b6 CH |
1976 | if (ret < 0) { |
1977 | bio->bi_error = ret; | |
1978 | bio_endio(bio); | |
1979 | } | |
61891923 | 1980 | return ret; |
065631f6 | 1981 | } |
6885f308 | 1982 | |
d352ac68 CM |
1983 | /* |
1984 | * given a list of ordered sums record them in the inode. This happens | |
1985 | * at IO completion time based on sums calculated at bio submission time. | |
1986 | */ | |
ba1da2f4 | 1987 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1988 | struct inode *inode, u64 file_offset, |
1989 | struct list_head *list) | |
1990 | { | |
e6dcd2dc CM |
1991 | struct btrfs_ordered_sum *sum; |
1992 | ||
c6e30871 | 1993 | list_for_each_entry(sum, list, list) { |
39847c4d | 1994 | trans->adding_csums = 1; |
d20f7043 CM |
1995 | btrfs_csum_file_blocks(trans, |
1996 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1997 | trans->adding_csums = 0; |
e6dcd2dc CM |
1998 | } |
1999 | return 0; | |
2000 | } | |
2001 | ||
2ac55d41 | 2002 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2003 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2004 | { |
09cbfeaf | 2005 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2006 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2007 | cached_state); |
ea8c2819 CM |
2008 | } |
2009 | ||
d352ac68 | 2010 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2011 | struct btrfs_writepage_fixup { |
2012 | struct page *page; | |
2013 | struct btrfs_work work; | |
2014 | }; | |
2015 | ||
b2950863 | 2016 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2017 | { |
2018 | struct btrfs_writepage_fixup *fixup; | |
2019 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2020 | struct extent_state *cached_state = NULL; |
247e743c CM |
2021 | struct page *page; |
2022 | struct inode *inode; | |
2023 | u64 page_start; | |
2024 | u64 page_end; | |
87826df0 | 2025 | int ret; |
247e743c CM |
2026 | |
2027 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2028 | page = fixup->page; | |
4a096752 | 2029 | again: |
247e743c CM |
2030 | lock_page(page); |
2031 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2032 | ClearPageChecked(page); | |
2033 | goto out_page; | |
2034 | } | |
2035 | ||
2036 | inode = page->mapping->host; | |
2037 | page_start = page_offset(page); | |
09cbfeaf | 2038 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2039 | |
ff13db41 | 2040 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2041 | &cached_state); |
4a096752 CM |
2042 | |
2043 | /* already ordered? We're done */ | |
8b62b72b | 2044 | if (PagePrivate2(page)) |
247e743c | 2045 | goto out; |
4a096752 | 2046 | |
dbfdb6d1 | 2047 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
09cbfeaf | 2048 | PAGE_SIZE); |
4a096752 | 2049 | if (ordered) { |
2ac55d41 JB |
2050 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2051 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2052 | unlock_page(page); |
2053 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2054 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2055 | goto again; |
2056 | } | |
247e743c | 2057 | |
7cf5b976 | 2058 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
09cbfeaf | 2059 | PAGE_SIZE); |
87826df0 JM |
2060 | if (ret) { |
2061 | mapping_set_error(page->mapping, ret); | |
2062 | end_extent_writepage(page, ret, page_start, page_end); | |
2063 | ClearPageChecked(page); | |
2064 | goto out; | |
2065 | } | |
2066 | ||
ba8b04c1 QW |
2067 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
2068 | 0); | |
247e743c | 2069 | ClearPageChecked(page); |
87826df0 | 2070 | set_page_dirty(page); |
247e743c | 2071 | out: |
2ac55d41 JB |
2072 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2073 | &cached_state, GFP_NOFS); | |
247e743c CM |
2074 | out_page: |
2075 | unlock_page(page); | |
09cbfeaf | 2076 | put_page(page); |
b897abec | 2077 | kfree(fixup); |
247e743c CM |
2078 | } |
2079 | ||
2080 | /* | |
2081 | * There are a few paths in the higher layers of the kernel that directly | |
2082 | * set the page dirty bit without asking the filesystem if it is a | |
2083 | * good idea. This causes problems because we want to make sure COW | |
2084 | * properly happens and the data=ordered rules are followed. | |
2085 | * | |
c8b97818 | 2086 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2087 | * hasn't been properly setup for IO. We kick off an async process |
2088 | * to fix it up. The async helper will wait for ordered extents, set | |
2089 | * the delalloc bit and make it safe to write the page. | |
2090 | */ | |
b2950863 | 2091 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2092 | { |
2093 | struct inode *inode = page->mapping->host; | |
0b246afa | 2094 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
247e743c | 2095 | struct btrfs_writepage_fixup *fixup; |
247e743c | 2096 | |
8b62b72b CM |
2097 | /* this page is properly in the ordered list */ |
2098 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2099 | return 0; |
2100 | ||
2101 | if (PageChecked(page)) | |
2102 | return -EAGAIN; | |
2103 | ||
2104 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2105 | if (!fixup) | |
2106 | return -EAGAIN; | |
f421950f | 2107 | |
247e743c | 2108 | SetPageChecked(page); |
09cbfeaf | 2109 | get_page(page); |
9e0af237 LB |
2110 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2111 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2112 | fixup->page = page; |
0b246afa | 2113 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
87826df0 | 2114 | return -EBUSY; |
247e743c CM |
2115 | } |
2116 | ||
d899e052 YZ |
2117 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2118 | struct inode *inode, u64 file_pos, | |
2119 | u64 disk_bytenr, u64 disk_num_bytes, | |
2120 | u64 num_bytes, u64 ram_bytes, | |
2121 | u8 compression, u8 encryption, | |
2122 | u16 other_encoding, int extent_type) | |
2123 | { | |
2124 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2125 | struct btrfs_file_extent_item *fi; | |
2126 | struct btrfs_path *path; | |
2127 | struct extent_buffer *leaf; | |
2128 | struct btrfs_key ins; | |
1acae57b | 2129 | int extent_inserted = 0; |
d899e052 YZ |
2130 | int ret; |
2131 | ||
2132 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2133 | if (!path) |
2134 | return -ENOMEM; | |
d899e052 | 2135 | |
a1ed835e CM |
2136 | /* |
2137 | * we may be replacing one extent in the tree with another. | |
2138 | * The new extent is pinned in the extent map, and we don't want | |
2139 | * to drop it from the cache until it is completely in the btree. | |
2140 | * | |
2141 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2142 | * the caller is expected to unpin it and allow it to be merged | |
2143 | * with the others. | |
2144 | */ | |
1acae57b FDBM |
2145 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2146 | file_pos + num_bytes, NULL, 0, | |
2147 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2148 | if (ret) |
2149 | goto out; | |
d899e052 | 2150 | |
1acae57b | 2151 | if (!extent_inserted) { |
4a0cc7ca | 2152 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
1acae57b FDBM |
2153 | ins.offset = file_pos; |
2154 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2155 | ||
2156 | path->leave_spinning = 1; | |
2157 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2158 | sizeof(*fi)); | |
2159 | if (ret) | |
2160 | goto out; | |
2161 | } | |
d899e052 YZ |
2162 | leaf = path->nodes[0]; |
2163 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2164 | struct btrfs_file_extent_item); | |
2165 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2166 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2167 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2168 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2169 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2170 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2171 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2172 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2173 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2174 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2175 | |
d899e052 | 2176 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2177 | btrfs_release_path(path); |
d899e052 YZ |
2178 | |
2179 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2180 | |
2181 | ins.objectid = disk_bytenr; | |
2182 | ins.offset = disk_num_bytes; | |
2183 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
2ff7e61e | 2184 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, |
4a0cc7ca | 2185 | btrfs_ino(BTRFS_I(inode)), file_pos, |
2ff7e61e | 2186 | ram_bytes, &ins); |
297d750b | 2187 | /* |
5846a3c2 QW |
2188 | * Release the reserved range from inode dirty range map, as it is |
2189 | * already moved into delayed_ref_head | |
297d750b QW |
2190 | */ |
2191 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); | |
79787eaa | 2192 | out: |
d899e052 | 2193 | btrfs_free_path(path); |
b9473439 | 2194 | |
79787eaa | 2195 | return ret; |
d899e052 YZ |
2196 | } |
2197 | ||
38c227d8 LB |
2198 | /* snapshot-aware defrag */ |
2199 | struct sa_defrag_extent_backref { | |
2200 | struct rb_node node; | |
2201 | struct old_sa_defrag_extent *old; | |
2202 | u64 root_id; | |
2203 | u64 inum; | |
2204 | u64 file_pos; | |
2205 | u64 extent_offset; | |
2206 | u64 num_bytes; | |
2207 | u64 generation; | |
2208 | }; | |
2209 | ||
2210 | struct old_sa_defrag_extent { | |
2211 | struct list_head list; | |
2212 | struct new_sa_defrag_extent *new; | |
2213 | ||
2214 | u64 extent_offset; | |
2215 | u64 bytenr; | |
2216 | u64 offset; | |
2217 | u64 len; | |
2218 | int count; | |
2219 | }; | |
2220 | ||
2221 | struct new_sa_defrag_extent { | |
2222 | struct rb_root root; | |
2223 | struct list_head head; | |
2224 | struct btrfs_path *path; | |
2225 | struct inode *inode; | |
2226 | u64 file_pos; | |
2227 | u64 len; | |
2228 | u64 bytenr; | |
2229 | u64 disk_len; | |
2230 | u8 compress_type; | |
2231 | }; | |
2232 | ||
2233 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2234 | struct sa_defrag_extent_backref *b2) | |
2235 | { | |
2236 | if (b1->root_id < b2->root_id) | |
2237 | return -1; | |
2238 | else if (b1->root_id > b2->root_id) | |
2239 | return 1; | |
2240 | ||
2241 | if (b1->inum < b2->inum) | |
2242 | return -1; | |
2243 | else if (b1->inum > b2->inum) | |
2244 | return 1; | |
2245 | ||
2246 | if (b1->file_pos < b2->file_pos) | |
2247 | return -1; | |
2248 | else if (b1->file_pos > b2->file_pos) | |
2249 | return 1; | |
2250 | ||
2251 | /* | |
2252 | * [------------------------------] ===> (a range of space) | |
2253 | * |<--->| |<---->| =============> (fs/file tree A) | |
2254 | * |<---------------------------->| ===> (fs/file tree B) | |
2255 | * | |
2256 | * A range of space can refer to two file extents in one tree while | |
2257 | * refer to only one file extent in another tree. | |
2258 | * | |
2259 | * So we may process a disk offset more than one time(two extents in A) | |
2260 | * and locate at the same extent(one extent in B), then insert two same | |
2261 | * backrefs(both refer to the extent in B). | |
2262 | */ | |
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | static void backref_insert(struct rb_root *root, | |
2267 | struct sa_defrag_extent_backref *backref) | |
2268 | { | |
2269 | struct rb_node **p = &root->rb_node; | |
2270 | struct rb_node *parent = NULL; | |
2271 | struct sa_defrag_extent_backref *entry; | |
2272 | int ret; | |
2273 | ||
2274 | while (*p) { | |
2275 | parent = *p; | |
2276 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2277 | ||
2278 | ret = backref_comp(backref, entry); | |
2279 | if (ret < 0) | |
2280 | p = &(*p)->rb_left; | |
2281 | else | |
2282 | p = &(*p)->rb_right; | |
2283 | } | |
2284 | ||
2285 | rb_link_node(&backref->node, parent, p); | |
2286 | rb_insert_color(&backref->node, root); | |
2287 | } | |
2288 | ||
2289 | /* | |
2290 | * Note the backref might has changed, and in this case we just return 0. | |
2291 | */ | |
2292 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2293 | void *ctx) | |
2294 | { | |
2295 | struct btrfs_file_extent_item *extent; | |
38c227d8 LB |
2296 | struct old_sa_defrag_extent *old = ctx; |
2297 | struct new_sa_defrag_extent *new = old->new; | |
2298 | struct btrfs_path *path = new->path; | |
2299 | struct btrfs_key key; | |
2300 | struct btrfs_root *root; | |
2301 | struct sa_defrag_extent_backref *backref; | |
2302 | struct extent_buffer *leaf; | |
2303 | struct inode *inode = new->inode; | |
0b246afa | 2304 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2305 | int slot; |
2306 | int ret; | |
2307 | u64 extent_offset; | |
2308 | u64 num_bytes; | |
2309 | ||
2310 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
4a0cc7ca | 2311 | inum == btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2312 | return 0; |
2313 | ||
2314 | key.objectid = root_id; | |
2315 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2316 | key.offset = (u64)-1; | |
2317 | ||
38c227d8 LB |
2318 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
2319 | if (IS_ERR(root)) { | |
2320 | if (PTR_ERR(root) == -ENOENT) | |
2321 | return 0; | |
2322 | WARN_ON(1); | |
ab8d0fc4 | 2323 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
38c227d8 LB |
2324 | inum, offset, root_id); |
2325 | return PTR_ERR(root); | |
2326 | } | |
2327 | ||
2328 | key.objectid = inum; | |
2329 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2330 | if (offset > (u64)-1 << 32) | |
2331 | key.offset = 0; | |
2332 | else | |
2333 | key.offset = offset; | |
2334 | ||
2335 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2336 | if (WARN_ON(ret < 0)) |
38c227d8 | 2337 | return ret; |
50f1319c | 2338 | ret = 0; |
38c227d8 LB |
2339 | |
2340 | while (1) { | |
2341 | cond_resched(); | |
2342 | ||
2343 | leaf = path->nodes[0]; | |
2344 | slot = path->slots[0]; | |
2345 | ||
2346 | if (slot >= btrfs_header_nritems(leaf)) { | |
2347 | ret = btrfs_next_leaf(root, path); | |
2348 | if (ret < 0) { | |
2349 | goto out; | |
2350 | } else if (ret > 0) { | |
2351 | ret = 0; | |
2352 | goto out; | |
2353 | } | |
2354 | continue; | |
2355 | } | |
2356 | ||
2357 | path->slots[0]++; | |
2358 | ||
2359 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2360 | ||
2361 | if (key.objectid > inum) | |
2362 | goto out; | |
2363 | ||
2364 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2365 | continue; | |
2366 | ||
2367 | extent = btrfs_item_ptr(leaf, slot, | |
2368 | struct btrfs_file_extent_item); | |
2369 | ||
2370 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2371 | continue; | |
2372 | ||
e68afa49 LB |
2373 | /* |
2374 | * 'offset' refers to the exact key.offset, | |
2375 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2376 | * (key.offset - extent_offset). | |
2377 | */ | |
2378 | if (key.offset != offset) | |
38c227d8 LB |
2379 | continue; |
2380 | ||
e68afa49 | 2381 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2382 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2383 | |
38c227d8 LB |
2384 | if (extent_offset >= old->extent_offset + old->offset + |
2385 | old->len || extent_offset + num_bytes <= | |
2386 | old->extent_offset + old->offset) | |
2387 | continue; | |
38c227d8 LB |
2388 | break; |
2389 | } | |
2390 | ||
2391 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2392 | if (!backref) { | |
2393 | ret = -ENOENT; | |
2394 | goto out; | |
2395 | } | |
2396 | ||
2397 | backref->root_id = root_id; | |
2398 | backref->inum = inum; | |
e68afa49 | 2399 | backref->file_pos = offset; |
38c227d8 LB |
2400 | backref->num_bytes = num_bytes; |
2401 | backref->extent_offset = extent_offset; | |
2402 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2403 | backref->old = old; | |
2404 | backref_insert(&new->root, backref); | |
2405 | old->count++; | |
2406 | out: | |
2407 | btrfs_release_path(path); | |
2408 | WARN_ON(ret); | |
2409 | return ret; | |
2410 | } | |
2411 | ||
2412 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2413 | struct new_sa_defrag_extent *new) | |
2414 | { | |
0b246afa | 2415 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2416 | struct old_sa_defrag_extent *old, *tmp; |
2417 | int ret; | |
2418 | ||
2419 | new->path = path; | |
2420 | ||
2421 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2422 | ret = iterate_inodes_from_logical(old->bytenr + |
2423 | old->extent_offset, fs_info, | |
38c227d8 LB |
2424 | path, record_one_backref, |
2425 | old); | |
4724b106 JB |
2426 | if (ret < 0 && ret != -ENOENT) |
2427 | return false; | |
38c227d8 LB |
2428 | |
2429 | /* no backref to be processed for this extent */ | |
2430 | if (!old->count) { | |
2431 | list_del(&old->list); | |
2432 | kfree(old); | |
2433 | } | |
2434 | } | |
2435 | ||
2436 | if (list_empty(&new->head)) | |
2437 | return false; | |
2438 | ||
2439 | return true; | |
2440 | } | |
2441 | ||
2442 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2443 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2444 | struct new_sa_defrag_extent *new) |
38c227d8 | 2445 | { |
116e0024 | 2446 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2447 | return 0; |
2448 | ||
2449 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2450 | return 0; | |
2451 | ||
116e0024 LB |
2452 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2453 | return 0; | |
2454 | ||
2455 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2456 | btrfs_file_extent_other_encoding(leaf, fi)) |
2457 | return 0; | |
2458 | ||
2459 | return 1; | |
2460 | } | |
2461 | ||
2462 | /* | |
2463 | * Note the backref might has changed, and in this case we just return 0. | |
2464 | */ | |
2465 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2466 | struct sa_defrag_extent_backref *prev, | |
2467 | struct sa_defrag_extent_backref *backref) | |
2468 | { | |
2469 | struct btrfs_file_extent_item *extent; | |
2470 | struct btrfs_file_extent_item *item; | |
2471 | struct btrfs_ordered_extent *ordered; | |
2472 | struct btrfs_trans_handle *trans; | |
38c227d8 LB |
2473 | struct btrfs_root *root; |
2474 | struct btrfs_key key; | |
2475 | struct extent_buffer *leaf; | |
2476 | struct old_sa_defrag_extent *old = backref->old; | |
2477 | struct new_sa_defrag_extent *new = old->new; | |
0b246afa | 2478 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 LB |
2479 | struct inode *inode; |
2480 | struct extent_state *cached = NULL; | |
2481 | int ret = 0; | |
2482 | u64 start; | |
2483 | u64 len; | |
2484 | u64 lock_start; | |
2485 | u64 lock_end; | |
2486 | bool merge = false; | |
2487 | int index; | |
2488 | ||
2489 | if (prev && prev->root_id == backref->root_id && | |
2490 | prev->inum == backref->inum && | |
2491 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2492 | merge = true; | |
2493 | ||
2494 | /* step 1: get root */ | |
2495 | key.objectid = backref->root_id; | |
2496 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2497 | key.offset = (u64)-1; | |
2498 | ||
38c227d8 LB |
2499 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2500 | ||
2501 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2502 | if (IS_ERR(root)) { | |
2503 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2504 | if (PTR_ERR(root) == -ENOENT) | |
2505 | return 0; | |
2506 | return PTR_ERR(root); | |
2507 | } | |
38c227d8 | 2508 | |
bcbba5e6 WS |
2509 | if (btrfs_root_readonly(root)) { |
2510 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2511 | return 0; | |
2512 | } | |
2513 | ||
38c227d8 LB |
2514 | /* step 2: get inode */ |
2515 | key.objectid = backref->inum; | |
2516 | key.type = BTRFS_INODE_ITEM_KEY; | |
2517 | key.offset = 0; | |
2518 | ||
2519 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2520 | if (IS_ERR(inode)) { | |
2521 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2522 | return 0; | |
2523 | } | |
2524 | ||
2525 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2526 | ||
2527 | /* step 3: relink backref */ | |
2528 | lock_start = backref->file_pos; | |
2529 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2530 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2531 | &cached); |
38c227d8 LB |
2532 | |
2533 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2534 | if (ordered) { | |
2535 | btrfs_put_ordered_extent(ordered); | |
2536 | goto out_unlock; | |
2537 | } | |
2538 | ||
2539 | trans = btrfs_join_transaction(root); | |
2540 | if (IS_ERR(trans)) { | |
2541 | ret = PTR_ERR(trans); | |
2542 | goto out_unlock; | |
2543 | } | |
2544 | ||
2545 | key.objectid = backref->inum; | |
2546 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2547 | key.offset = backref->file_pos; | |
2548 | ||
2549 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2550 | if (ret < 0) { | |
2551 | goto out_free_path; | |
2552 | } else if (ret > 0) { | |
2553 | ret = 0; | |
2554 | goto out_free_path; | |
2555 | } | |
2556 | ||
2557 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2558 | struct btrfs_file_extent_item); | |
2559 | ||
2560 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2561 | backref->generation) | |
2562 | goto out_free_path; | |
2563 | ||
2564 | btrfs_release_path(path); | |
2565 | ||
2566 | start = backref->file_pos; | |
2567 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2568 | start += old->extent_offset + old->offset - | |
2569 | backref->extent_offset; | |
2570 | ||
2571 | len = min(backref->extent_offset + backref->num_bytes, | |
2572 | old->extent_offset + old->offset + old->len); | |
2573 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2574 | ||
2575 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2576 | start + len, 1); | |
2577 | if (ret) | |
2578 | goto out_free_path; | |
2579 | again: | |
4a0cc7ca | 2580 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2581 | key.type = BTRFS_EXTENT_DATA_KEY; |
2582 | key.offset = start; | |
2583 | ||
a09a0a70 | 2584 | path->leave_spinning = 1; |
38c227d8 LB |
2585 | if (merge) { |
2586 | struct btrfs_file_extent_item *fi; | |
2587 | u64 extent_len; | |
2588 | struct btrfs_key found_key; | |
2589 | ||
3c9665df | 2590 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2591 | if (ret < 0) |
2592 | goto out_free_path; | |
2593 | ||
2594 | path->slots[0]--; | |
2595 | leaf = path->nodes[0]; | |
2596 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2597 | ||
2598 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2599 | struct btrfs_file_extent_item); | |
2600 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2601 | ||
116e0024 LB |
2602 | if (extent_len + found_key.offset == start && |
2603 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2604 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2605 | extent_len + len); | |
2606 | btrfs_mark_buffer_dirty(leaf); | |
2607 | inode_add_bytes(inode, len); | |
2608 | ||
2609 | ret = 1; | |
2610 | goto out_free_path; | |
2611 | } else { | |
2612 | merge = false; | |
2613 | btrfs_release_path(path); | |
2614 | goto again; | |
2615 | } | |
2616 | } | |
2617 | ||
2618 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2619 | sizeof(*extent)); | |
2620 | if (ret) { | |
66642832 | 2621 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2622 | goto out_free_path; |
2623 | } | |
2624 | ||
2625 | leaf = path->nodes[0]; | |
2626 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2627 | struct btrfs_file_extent_item); | |
2628 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2629 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2630 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2631 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2632 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2633 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2634 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2635 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2636 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2637 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2638 | ||
2639 | btrfs_mark_buffer_dirty(leaf); | |
2640 | inode_add_bytes(inode, len); | |
a09a0a70 | 2641 | btrfs_release_path(path); |
38c227d8 | 2642 | |
2ff7e61e | 2643 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
38c227d8 LB |
2644 | new->disk_len, 0, |
2645 | backref->root_id, backref->inum, | |
b06c4bf5 | 2646 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2647 | if (ret) { |
66642832 | 2648 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2649 | goto out_free_path; |
2650 | } | |
2651 | ||
2652 | ret = 1; | |
2653 | out_free_path: | |
2654 | btrfs_release_path(path); | |
a09a0a70 | 2655 | path->leave_spinning = 0; |
3a45bb20 | 2656 | btrfs_end_transaction(trans); |
38c227d8 LB |
2657 | out_unlock: |
2658 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2659 | &cached, GFP_NOFS); | |
2660 | iput(inode); | |
2661 | return ret; | |
2662 | } | |
2663 | ||
6f519564 LB |
2664 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2665 | { | |
2666 | struct old_sa_defrag_extent *old, *tmp; | |
2667 | ||
2668 | if (!new) | |
2669 | return; | |
2670 | ||
2671 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2672 | kfree(old); |
2673 | } | |
2674 | kfree(new); | |
2675 | } | |
2676 | ||
38c227d8 LB |
2677 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2678 | { | |
0b246afa | 2679 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
38c227d8 | 2680 | struct btrfs_path *path; |
38c227d8 LB |
2681 | struct sa_defrag_extent_backref *backref; |
2682 | struct sa_defrag_extent_backref *prev = NULL; | |
2683 | struct inode *inode; | |
2684 | struct btrfs_root *root; | |
2685 | struct rb_node *node; | |
2686 | int ret; | |
2687 | ||
2688 | inode = new->inode; | |
2689 | root = BTRFS_I(inode)->root; | |
2690 | ||
2691 | path = btrfs_alloc_path(); | |
2692 | if (!path) | |
2693 | return; | |
2694 | ||
2695 | if (!record_extent_backrefs(path, new)) { | |
2696 | btrfs_free_path(path); | |
2697 | goto out; | |
2698 | } | |
2699 | btrfs_release_path(path); | |
2700 | ||
2701 | while (1) { | |
2702 | node = rb_first(&new->root); | |
2703 | if (!node) | |
2704 | break; | |
2705 | rb_erase(node, &new->root); | |
2706 | ||
2707 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2708 | ||
2709 | ret = relink_extent_backref(path, prev, backref); | |
2710 | WARN_ON(ret < 0); | |
2711 | ||
2712 | kfree(prev); | |
2713 | ||
2714 | if (ret == 1) | |
2715 | prev = backref; | |
2716 | else | |
2717 | prev = NULL; | |
2718 | cond_resched(); | |
2719 | } | |
2720 | kfree(prev); | |
2721 | ||
2722 | btrfs_free_path(path); | |
38c227d8 | 2723 | out: |
6f519564 LB |
2724 | free_sa_defrag_extent(new); |
2725 | ||
0b246afa JM |
2726 | atomic_dec(&fs_info->defrag_running); |
2727 | wake_up(&fs_info->transaction_wait); | |
38c227d8 LB |
2728 | } |
2729 | ||
2730 | static struct new_sa_defrag_extent * | |
2731 | record_old_file_extents(struct inode *inode, | |
2732 | struct btrfs_ordered_extent *ordered) | |
2733 | { | |
0b246afa | 2734 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
38c227d8 LB |
2735 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2736 | struct btrfs_path *path; | |
2737 | struct btrfs_key key; | |
6f519564 | 2738 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2739 | struct new_sa_defrag_extent *new; |
2740 | int ret; | |
2741 | ||
2742 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2743 | if (!new) | |
2744 | return NULL; | |
2745 | ||
2746 | new->inode = inode; | |
2747 | new->file_pos = ordered->file_offset; | |
2748 | new->len = ordered->len; | |
2749 | new->bytenr = ordered->start; | |
2750 | new->disk_len = ordered->disk_len; | |
2751 | new->compress_type = ordered->compress_type; | |
2752 | new->root = RB_ROOT; | |
2753 | INIT_LIST_HEAD(&new->head); | |
2754 | ||
2755 | path = btrfs_alloc_path(); | |
2756 | if (!path) | |
2757 | goto out_kfree; | |
2758 | ||
4a0cc7ca | 2759 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
38c227d8 LB |
2760 | key.type = BTRFS_EXTENT_DATA_KEY; |
2761 | key.offset = new->file_pos; | |
2762 | ||
2763 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2764 | if (ret < 0) | |
2765 | goto out_free_path; | |
2766 | if (ret > 0 && path->slots[0] > 0) | |
2767 | path->slots[0]--; | |
2768 | ||
2769 | /* find out all the old extents for the file range */ | |
2770 | while (1) { | |
2771 | struct btrfs_file_extent_item *extent; | |
2772 | struct extent_buffer *l; | |
2773 | int slot; | |
2774 | u64 num_bytes; | |
2775 | u64 offset; | |
2776 | u64 end; | |
2777 | u64 disk_bytenr; | |
2778 | u64 extent_offset; | |
2779 | ||
2780 | l = path->nodes[0]; | |
2781 | slot = path->slots[0]; | |
2782 | ||
2783 | if (slot >= btrfs_header_nritems(l)) { | |
2784 | ret = btrfs_next_leaf(root, path); | |
2785 | if (ret < 0) | |
6f519564 | 2786 | goto out_free_path; |
38c227d8 LB |
2787 | else if (ret > 0) |
2788 | break; | |
2789 | continue; | |
2790 | } | |
2791 | ||
2792 | btrfs_item_key_to_cpu(l, &key, slot); | |
2793 | ||
4a0cc7ca | 2794 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
38c227d8 LB |
2795 | break; |
2796 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2797 | break; | |
2798 | if (key.offset >= new->file_pos + new->len) | |
2799 | break; | |
2800 | ||
2801 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2802 | ||
2803 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2804 | if (key.offset + num_bytes < new->file_pos) | |
2805 | goto next; | |
2806 | ||
2807 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2808 | if (!disk_bytenr) | |
2809 | goto next; | |
2810 | ||
2811 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2812 | ||
2813 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2814 | if (!old) | |
6f519564 | 2815 | goto out_free_path; |
38c227d8 LB |
2816 | |
2817 | offset = max(new->file_pos, key.offset); | |
2818 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2819 | ||
2820 | old->bytenr = disk_bytenr; | |
2821 | old->extent_offset = extent_offset; | |
2822 | old->offset = offset - key.offset; | |
2823 | old->len = end - offset; | |
2824 | old->new = new; | |
2825 | old->count = 0; | |
2826 | list_add_tail(&old->list, &new->head); | |
2827 | next: | |
2828 | path->slots[0]++; | |
2829 | cond_resched(); | |
2830 | } | |
2831 | ||
2832 | btrfs_free_path(path); | |
0b246afa | 2833 | atomic_inc(&fs_info->defrag_running); |
38c227d8 LB |
2834 | |
2835 | return new; | |
2836 | ||
38c227d8 LB |
2837 | out_free_path: |
2838 | btrfs_free_path(path); | |
2839 | out_kfree: | |
6f519564 | 2840 | free_sa_defrag_extent(new); |
38c227d8 LB |
2841 | return NULL; |
2842 | } | |
2843 | ||
2ff7e61e | 2844 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
e570fd27 MX |
2845 | u64 start, u64 len) |
2846 | { | |
2847 | struct btrfs_block_group_cache *cache; | |
2848 | ||
0b246afa | 2849 | cache = btrfs_lookup_block_group(fs_info, start); |
e570fd27 MX |
2850 | ASSERT(cache); |
2851 | ||
2852 | spin_lock(&cache->lock); | |
2853 | cache->delalloc_bytes -= len; | |
2854 | spin_unlock(&cache->lock); | |
2855 | ||
2856 | btrfs_put_block_group(cache); | |
2857 | } | |
2858 | ||
d352ac68 CM |
2859 | /* as ordered data IO finishes, this gets called so we can finish |
2860 | * an ordered extent if the range of bytes in the file it covers are | |
2861 | * fully written. | |
2862 | */ | |
5fd02043 | 2863 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2864 | { |
5fd02043 | 2865 | struct inode *inode = ordered_extent->inode; |
0b246afa | 2866 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 2867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2868 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2869 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2870 | struct extent_state *cached_state = NULL; |
38c227d8 | 2871 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2872 | int compress_type = 0; |
77cef2ec JB |
2873 | int ret = 0; |
2874 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2875 | bool nolock; |
77cef2ec | 2876 | bool truncated = false; |
e6dcd2dc | 2877 | |
83eea1f1 | 2878 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2879 | |
5fd02043 JB |
2880 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2881 | ret = -EIO; | |
2882 | goto out; | |
2883 | } | |
2884 | ||
f612496b MX |
2885 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2886 | ordered_extent->file_offset + | |
2887 | ordered_extent->len - 1); | |
2888 | ||
77cef2ec JB |
2889 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2890 | truncated = true; | |
2891 | logical_len = ordered_extent->truncated_len; | |
2892 | /* Truncated the entire extent, don't bother adding */ | |
2893 | if (!logical_len) | |
2894 | goto out; | |
2895 | } | |
2896 | ||
c2167754 | 2897 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2898 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2899 | |
2900 | /* | |
2901 | * For mwrite(mmap + memset to write) case, we still reserve | |
2902 | * space for NOCOW range. | |
2903 | * As NOCOW won't cause a new delayed ref, just free the space | |
2904 | */ | |
2905 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, | |
2906 | ordered_extent->len); | |
6c760c07 JB |
2907 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2908 | if (nolock) | |
2909 | trans = btrfs_join_transaction_nolock(root); | |
2910 | else | |
2911 | trans = btrfs_join_transaction(root); | |
2912 | if (IS_ERR(trans)) { | |
2913 | ret = PTR_ERR(trans); | |
2914 | trans = NULL; | |
2915 | goto out; | |
c2167754 | 2916 | } |
0b246afa | 2917 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
6c760c07 JB |
2918 | ret = btrfs_update_inode_fallback(trans, root, inode); |
2919 | if (ret) /* -ENOMEM or corruption */ | |
66642832 | 2920 | btrfs_abort_transaction(trans, ret); |
c2167754 YZ |
2921 | goto out; |
2922 | } | |
e6dcd2dc | 2923 | |
2ac55d41 JB |
2924 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2925 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2926 | &cached_state); |
e6dcd2dc | 2927 | |
38c227d8 LB |
2928 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2929 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2930 | EXTENT_DEFRAG, 1, cached_state); | |
2931 | if (ret) { | |
2932 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2933 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2934 | /* the inode is shared */ |
2935 | new = record_old_file_extents(inode, ordered_extent); | |
2936 | ||
2937 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2938 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2939 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2940 | } | |
2941 | ||
0cb59c99 | 2942 | if (nolock) |
7a7eaa40 | 2943 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2944 | else |
7a7eaa40 | 2945 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2946 | if (IS_ERR(trans)) { |
2947 | ret = PTR_ERR(trans); | |
2948 | trans = NULL; | |
2949 | goto out_unlock; | |
2950 | } | |
a79b7d4b | 2951 | |
0b246afa | 2952 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
c2167754 | 2953 | |
c8b97818 | 2954 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2955 | compress_type = ordered_extent->compress_type; |
d899e052 | 2956 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2957 | BUG_ON(compress_type); |
920bbbfb | 2958 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2959 | ordered_extent->file_offset, |
2960 | ordered_extent->file_offset + | |
77cef2ec | 2961 | logical_len); |
d899e052 | 2962 | } else { |
0b246afa | 2963 | BUG_ON(root == fs_info->tree_root); |
d899e052 YZ |
2964 | ret = insert_reserved_file_extent(trans, inode, |
2965 | ordered_extent->file_offset, | |
2966 | ordered_extent->start, | |
2967 | ordered_extent->disk_len, | |
77cef2ec | 2968 | logical_len, logical_len, |
261507a0 | 2969 | compress_type, 0, 0, |
d899e052 | 2970 | BTRFS_FILE_EXTENT_REG); |
e570fd27 | 2971 | if (!ret) |
2ff7e61e | 2972 | btrfs_release_delalloc_bytes(fs_info, |
e570fd27 MX |
2973 | ordered_extent->start, |
2974 | ordered_extent->disk_len); | |
d899e052 | 2975 | } |
5dc562c5 JB |
2976 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2977 | ordered_extent->file_offset, ordered_extent->len, | |
2978 | trans->transid); | |
79787eaa | 2979 | if (ret < 0) { |
66642832 | 2980 | btrfs_abort_transaction(trans, ret); |
5fd02043 | 2981 | goto out_unlock; |
79787eaa | 2982 | } |
2ac55d41 | 2983 | |
e6dcd2dc CM |
2984 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2985 | &ordered_extent->list); | |
2986 | ||
6c760c07 JB |
2987 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2988 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2989 | if (ret) { /* -ENOMEM or corruption */ | |
66642832 | 2990 | btrfs_abort_transaction(trans, ret); |
6c760c07 | 2991 | goto out_unlock; |
1ef30be1 JB |
2992 | } |
2993 | ret = 0; | |
5fd02043 JB |
2994 | out_unlock: |
2995 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2996 | ordered_extent->file_offset + | |
2997 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2998 | out: |
0b246afa | 2999 | if (root != fs_info->tree_root) |
0cb59c99 | 3000 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 | 3001 | if (trans) |
3a45bb20 | 3002 | btrfs_end_transaction(trans); |
0cb59c99 | 3003 | |
77cef2ec JB |
3004 | if (ret || truncated) { |
3005 | u64 start, end; | |
3006 | ||
3007 | if (truncated) | |
3008 | start = ordered_extent->file_offset + logical_len; | |
3009 | else | |
3010 | start = ordered_extent->file_offset; | |
3011 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
3012 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
3013 | ||
3014 | /* Drop the cache for the part of the extent we didn't write. */ | |
3015 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 3016 | |
0bec9ef5 JB |
3017 | /* |
3018 | * If the ordered extent had an IOERR or something else went | |
3019 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3020 | * back to the allocator. We only free the extent in the |
3021 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3022 | */ |
77cef2ec JB |
3023 | if ((ret || !logical_len) && |
3024 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 | 3025 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2ff7e61e JM |
3026 | btrfs_free_reserved_extent(fs_info, |
3027 | ordered_extent->start, | |
e570fd27 | 3028 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3029 | } |
3030 | ||
3031 | ||
5fd02043 | 3032 | /* |
8bad3c02 LB |
3033 | * This needs to be done to make sure anybody waiting knows we are done |
3034 | * updating everything for this ordered extent. | |
5fd02043 JB |
3035 | */ |
3036 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3037 | ||
38c227d8 | 3038 | /* for snapshot-aware defrag */ |
6f519564 LB |
3039 | if (new) { |
3040 | if (ret) { | |
3041 | free_sa_defrag_extent(new); | |
0b246afa | 3042 | atomic_dec(&fs_info->defrag_running); |
6f519564 LB |
3043 | } else { |
3044 | relink_file_extents(new); | |
3045 | } | |
3046 | } | |
38c227d8 | 3047 | |
e6dcd2dc CM |
3048 | /* once for us */ |
3049 | btrfs_put_ordered_extent(ordered_extent); | |
3050 | /* once for the tree */ | |
3051 | btrfs_put_ordered_extent(ordered_extent); | |
3052 | ||
5fd02043 JB |
3053 | return ret; |
3054 | } | |
3055 | ||
3056 | static void finish_ordered_fn(struct btrfs_work *work) | |
3057 | { | |
3058 | struct btrfs_ordered_extent *ordered_extent; | |
3059 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3060 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3061 | } |
3062 | ||
b2950863 | 3063 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3064 | struct extent_state *state, int uptodate) |
3065 | { | |
5fd02043 | 3066 | struct inode *inode = page->mapping->host; |
0b246afa | 3067 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5fd02043 | 3068 | struct btrfs_ordered_extent *ordered_extent = NULL; |
9e0af237 LB |
3069 | struct btrfs_workqueue *wq; |
3070 | btrfs_work_func_t func; | |
5fd02043 | 3071 | |
1abe9b8a | 3072 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3073 | ||
8b62b72b | 3074 | ClearPagePrivate2(page); |
5fd02043 JB |
3075 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3076 | end - start + 1, uptodate)) | |
3077 | return 0; | |
3078 | ||
9e0af237 | 3079 | if (btrfs_is_free_space_inode(inode)) { |
0b246afa | 3080 | wq = fs_info->endio_freespace_worker; |
9e0af237 LB |
3081 | func = btrfs_freespace_write_helper; |
3082 | } else { | |
0b246afa | 3083 | wq = fs_info->endio_write_workers; |
9e0af237 LB |
3084 | func = btrfs_endio_write_helper; |
3085 | } | |
5fd02043 | 3086 | |
9e0af237 LB |
3087 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3088 | NULL); | |
3089 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3090 | |
3091 | return 0; | |
211f90e6 CM |
3092 | } |
3093 | ||
dc380aea MX |
3094 | static int __readpage_endio_check(struct inode *inode, |
3095 | struct btrfs_io_bio *io_bio, | |
3096 | int icsum, struct page *page, | |
3097 | int pgoff, u64 start, size_t len) | |
3098 | { | |
3099 | char *kaddr; | |
3100 | u32 csum_expected; | |
3101 | u32 csum = ~(u32)0; | |
dc380aea MX |
3102 | |
3103 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3104 | ||
3105 | kaddr = kmap_atomic(page); | |
3106 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
0b5e3daf | 3107 | btrfs_csum_final(csum, (u8 *)&csum); |
dc380aea MX |
3108 | if (csum != csum_expected) |
3109 | goto zeroit; | |
3110 | ||
3111 | kunmap_atomic(kaddr); | |
3112 | return 0; | |
3113 | zeroit: | |
94647322 DS |
3114 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
3115 | "csum failed ino %llu off %llu csum %u expected csum %u", | |
4a0cc7ca | 3116 | btrfs_ino(BTRFS_I(inode)), start, csum, csum_expected); |
dc380aea MX |
3117 | memset(kaddr + pgoff, 1, len); |
3118 | flush_dcache_page(page); | |
3119 | kunmap_atomic(kaddr); | |
3120 | if (csum_expected == 0) | |
3121 | return 0; | |
3122 | return -EIO; | |
3123 | } | |
3124 | ||
d352ac68 CM |
3125 | /* |
3126 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3127 | * if there's a match, we allow the bio to finish. If not, the code in |
3128 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3129 | */ |
facc8a22 MX |
3130 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3131 | u64 phy_offset, struct page *page, | |
3132 | u64 start, u64 end, int mirror) | |
07157aac | 3133 | { |
4eee4fa4 | 3134 | size_t offset = start - page_offset(page); |
07157aac | 3135 | struct inode *inode = page->mapping->host; |
d1310b2e | 3136 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3137 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3138 | |
d20f7043 CM |
3139 | if (PageChecked(page)) { |
3140 | ClearPageChecked(page); | |
dc380aea | 3141 | return 0; |
d20f7043 | 3142 | } |
6cbff00f CH |
3143 | |
3144 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3145 | return 0; |
17d217fe YZ |
3146 | |
3147 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3148 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3149 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3150 | return 0; |
17d217fe | 3151 | } |
d20f7043 | 3152 | |
facc8a22 | 3153 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3154 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3155 | start, (size_t)(end - start + 1)); | |
07157aac | 3156 | } |
b888db2b | 3157 | |
24bbcf04 YZ |
3158 | void btrfs_add_delayed_iput(struct inode *inode) |
3159 | { | |
0b246afa | 3160 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8089fe62 | 3161 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3162 | |
3163 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3164 | return; | |
3165 | ||
24bbcf04 | 3166 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3167 | if (binode->delayed_iput_count == 0) { |
3168 | ASSERT(list_empty(&binode->delayed_iput)); | |
3169 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3170 | } else { | |
3171 | binode->delayed_iput_count++; | |
3172 | } | |
24bbcf04 YZ |
3173 | spin_unlock(&fs_info->delayed_iput_lock); |
3174 | } | |
3175 | ||
2ff7e61e | 3176 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
24bbcf04 | 3177 | { |
24bbcf04 | 3178 | |
24bbcf04 | 3179 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3180 | while (!list_empty(&fs_info->delayed_iputs)) { |
3181 | struct btrfs_inode *inode; | |
3182 | ||
3183 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3184 | struct btrfs_inode, delayed_iput); | |
3185 | if (inode->delayed_iput_count) { | |
3186 | inode->delayed_iput_count--; | |
3187 | list_move_tail(&inode->delayed_iput, | |
3188 | &fs_info->delayed_iputs); | |
3189 | } else { | |
3190 | list_del_init(&inode->delayed_iput); | |
3191 | } | |
3192 | spin_unlock(&fs_info->delayed_iput_lock); | |
3193 | iput(&inode->vfs_inode); | |
3194 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3195 | } |
8089fe62 | 3196 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3197 | } |
3198 | ||
d68fc57b | 3199 | /* |
42b2aa86 | 3200 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3201 | * files in the subvolume, it removes orphan item and frees block_rsv |
3202 | * structure. | |
3203 | */ | |
3204 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3205 | struct btrfs_root *root) | |
3206 | { | |
0b246afa | 3207 | struct btrfs_fs_info *fs_info = root->fs_info; |
90290e19 | 3208 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3209 | int ret; |
3210 | ||
8a35d95f | 3211 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3212 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3213 | return; | |
3214 | ||
90290e19 | 3215 | spin_lock(&root->orphan_lock); |
8a35d95f | 3216 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3217 | spin_unlock(&root->orphan_lock); |
3218 | return; | |
3219 | } | |
3220 | ||
3221 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3222 | spin_unlock(&root->orphan_lock); | |
3223 | return; | |
3224 | } | |
3225 | ||
3226 | block_rsv = root->orphan_block_rsv; | |
3227 | root->orphan_block_rsv = NULL; | |
3228 | spin_unlock(&root->orphan_lock); | |
3229 | ||
27cdeb70 | 3230 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b | 3231 | btrfs_root_refs(&root->root_item) > 0) { |
0b246afa | 3232 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3233 | root->root_key.objectid); |
4ef31a45 | 3234 | if (ret) |
66642832 | 3235 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3236 | else |
27cdeb70 MX |
3237 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3238 | &root->state); | |
d68fc57b YZ |
3239 | } |
3240 | ||
90290e19 JB |
3241 | if (block_rsv) { |
3242 | WARN_ON(block_rsv->size > 0); | |
2ff7e61e | 3243 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b YZ |
3244 | } |
3245 | } | |
3246 | ||
7b128766 JB |
3247 | /* |
3248 | * This creates an orphan entry for the given inode in case something goes | |
3249 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3250 | * |
3251 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3252 | * this function. | |
7b128766 JB |
3253 | */ |
3254 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3255 | { | |
0b246afa | 3256 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7b128766 | 3257 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d68fc57b YZ |
3258 | struct btrfs_block_rsv *block_rsv = NULL; |
3259 | int reserve = 0; | |
3260 | int insert = 0; | |
3261 | int ret; | |
7b128766 | 3262 | |
d68fc57b | 3263 | if (!root->orphan_block_rsv) { |
2ff7e61e JM |
3264 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
3265 | BTRFS_BLOCK_RSV_TEMP); | |
b532402e TI |
3266 | if (!block_rsv) |
3267 | return -ENOMEM; | |
d68fc57b | 3268 | } |
7b128766 | 3269 | |
d68fc57b YZ |
3270 | spin_lock(&root->orphan_lock); |
3271 | if (!root->orphan_block_rsv) { | |
3272 | root->orphan_block_rsv = block_rsv; | |
3273 | } else if (block_rsv) { | |
2ff7e61e | 3274 | btrfs_free_block_rsv(fs_info, block_rsv); |
d68fc57b | 3275 | block_rsv = NULL; |
7b128766 | 3276 | } |
7b128766 | 3277 | |
8a35d95f JB |
3278 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3279 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3280 | #if 0 |
3281 | /* | |
3282 | * For proper ENOSPC handling, we should do orphan | |
3283 | * cleanup when mounting. But this introduces backward | |
3284 | * compatibility issue. | |
3285 | */ | |
3286 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3287 | insert = 2; | |
3288 | else | |
3289 | insert = 1; | |
3290 | #endif | |
3291 | insert = 1; | |
321f0e70 | 3292 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3293 | } |
3294 | ||
72ac3c0d JB |
3295 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3296 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3297 | reserve = 1; |
d68fc57b | 3298 | spin_unlock(&root->orphan_lock); |
7b128766 | 3299 | |
d68fc57b YZ |
3300 | /* grab metadata reservation from transaction handle */ |
3301 | if (reserve) { | |
3302 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3303 | ASSERT(!ret); |
3304 | if (ret) { | |
3305 | atomic_dec(&root->orphan_inodes); | |
3306 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3307 | &BTRFS_I(inode)->runtime_flags); | |
3308 | if (insert) | |
3309 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3310 | &BTRFS_I(inode)->runtime_flags); | |
3311 | return ret; | |
3312 | } | |
d68fc57b | 3313 | } |
7b128766 | 3314 | |
d68fc57b YZ |
3315 | /* insert an orphan item to track this unlinked/truncated file */ |
3316 | if (insert >= 1) { | |
4a0cc7ca | 3317 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(BTRFS_I(inode))); |
4ef31a45 | 3318 | if (ret) { |
703c88e0 | 3319 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3320 | if (reserve) { |
3321 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3322 | &BTRFS_I(inode)->runtime_flags); | |
3323 | btrfs_orphan_release_metadata(inode); | |
3324 | } | |
3325 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3326 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3327 | &BTRFS_I(inode)->runtime_flags); | |
66642832 | 3328 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3329 | return ret; |
3330 | } | |
79787eaa JM |
3331 | } |
3332 | ret = 0; | |
d68fc57b YZ |
3333 | } |
3334 | ||
3335 | /* insert an orphan item to track subvolume contains orphan files */ | |
3336 | if (insert >= 2) { | |
0b246afa | 3337 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
d68fc57b | 3338 | root->root_key.objectid); |
79787eaa | 3339 | if (ret && ret != -EEXIST) { |
66642832 | 3340 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3341 | return ret; |
3342 | } | |
d68fc57b YZ |
3343 | } |
3344 | return 0; | |
7b128766 JB |
3345 | } |
3346 | ||
3347 | /* | |
3348 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3349 | * item for this particular inode. | |
3350 | */ | |
48a3b636 ES |
3351 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3352 | struct inode *inode) | |
7b128766 JB |
3353 | { |
3354 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3355 | int delete_item = 0; |
3356 | int release_rsv = 0; | |
7b128766 JB |
3357 | int ret = 0; |
3358 | ||
d68fc57b | 3359 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3360 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3361 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3362 | delete_item = 1; |
7b128766 | 3363 | |
72ac3c0d JB |
3364 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3365 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3366 | release_rsv = 1; |
d68fc57b | 3367 | spin_unlock(&root->orphan_lock); |
7b128766 | 3368 | |
703c88e0 | 3369 | if (delete_item) { |
8a35d95f | 3370 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3371 | if (trans) |
3372 | ret = btrfs_del_orphan_item(trans, root, | |
4a0cc7ca | 3373 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 3374 | } |
7b128766 | 3375 | |
703c88e0 FDBM |
3376 | if (release_rsv) |
3377 | btrfs_orphan_release_metadata(inode); | |
3378 | ||
4ef31a45 | 3379 | return ret; |
7b128766 JB |
3380 | } |
3381 | ||
3382 | /* | |
3383 | * this cleans up any orphans that may be left on the list from the last use | |
3384 | * of this root. | |
3385 | */ | |
66b4ffd1 | 3386 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 | 3387 | { |
0b246afa | 3388 | struct btrfs_fs_info *fs_info = root->fs_info; |
7b128766 JB |
3389 | struct btrfs_path *path; |
3390 | struct extent_buffer *leaf; | |
7b128766 JB |
3391 | struct btrfs_key key, found_key; |
3392 | struct btrfs_trans_handle *trans; | |
3393 | struct inode *inode; | |
8f6d7f4f | 3394 | u64 last_objectid = 0; |
7b128766 JB |
3395 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3396 | ||
d68fc57b | 3397 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3398 | return 0; |
c71bf099 YZ |
3399 | |
3400 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3401 | if (!path) { |
3402 | ret = -ENOMEM; | |
3403 | goto out; | |
3404 | } | |
e4058b54 | 3405 | path->reada = READA_BACK; |
7b128766 JB |
3406 | |
3407 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3408 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3409 | key.offset = (u64)-1; |
3410 | ||
7b128766 JB |
3411 | while (1) { |
3412 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3413 | if (ret < 0) |
3414 | goto out; | |
7b128766 JB |
3415 | |
3416 | /* | |
3417 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3418 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3419 | * find the key and see if we have stuff that matches |
3420 | */ | |
3421 | if (ret > 0) { | |
66b4ffd1 | 3422 | ret = 0; |
7b128766 JB |
3423 | if (path->slots[0] == 0) |
3424 | break; | |
3425 | path->slots[0]--; | |
3426 | } | |
3427 | ||
3428 | /* pull out the item */ | |
3429 | leaf = path->nodes[0]; | |
7b128766 JB |
3430 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3431 | ||
3432 | /* make sure the item matches what we want */ | |
3433 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3434 | break; | |
962a298f | 3435 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3436 | break; |
3437 | ||
3438 | /* release the path since we're done with it */ | |
b3b4aa74 | 3439 | btrfs_release_path(path); |
7b128766 JB |
3440 | |
3441 | /* | |
3442 | * this is where we are basically btrfs_lookup, without the | |
3443 | * crossing root thing. we store the inode number in the | |
3444 | * offset of the orphan item. | |
3445 | */ | |
8f6d7f4f JB |
3446 | |
3447 | if (found_key.offset == last_objectid) { | |
0b246afa JM |
3448 | btrfs_err(fs_info, |
3449 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3450 | ret = -EINVAL; |
3451 | goto out; | |
3452 | } | |
3453 | ||
3454 | last_objectid = found_key.offset; | |
3455 | ||
5d4f98a2 YZ |
3456 | found_key.objectid = found_key.offset; |
3457 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3458 | found_key.offset = 0; | |
0b246afa | 3459 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3460 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3461 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3462 | goto out; |
7b128766 | 3463 | |
0b246afa | 3464 | if (ret == -ENOENT && root == fs_info->tree_root) { |
f8e9e0b0 AJ |
3465 | struct btrfs_root *dead_root; |
3466 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3467 | int is_dead_root = 0; | |
3468 | ||
3469 | /* | |
3470 | * this is an orphan in the tree root. Currently these | |
3471 | * could come from 2 sources: | |
3472 | * a) a snapshot deletion in progress | |
3473 | * b) a free space cache inode | |
3474 | * We need to distinguish those two, as the snapshot | |
3475 | * orphan must not get deleted. | |
3476 | * find_dead_roots already ran before us, so if this | |
3477 | * is a snapshot deletion, we should find the root | |
3478 | * in the dead_roots list | |
3479 | */ | |
3480 | spin_lock(&fs_info->trans_lock); | |
3481 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3482 | root_list) { | |
3483 | if (dead_root->root_key.objectid == | |
3484 | found_key.objectid) { | |
3485 | is_dead_root = 1; | |
3486 | break; | |
3487 | } | |
3488 | } | |
3489 | spin_unlock(&fs_info->trans_lock); | |
3490 | if (is_dead_root) { | |
3491 | /* prevent this orphan from being found again */ | |
3492 | key.offset = found_key.objectid - 1; | |
3493 | continue; | |
3494 | } | |
3495 | } | |
7b128766 | 3496 | /* |
a8c9e576 JB |
3497 | * Inode is already gone but the orphan item is still there, |
3498 | * kill the orphan item. | |
7b128766 | 3499 | */ |
67710892 | 3500 | if (ret == -ENOENT) { |
a8c9e576 | 3501 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3502 | if (IS_ERR(trans)) { |
3503 | ret = PTR_ERR(trans); | |
3504 | goto out; | |
3505 | } | |
0b246afa JM |
3506 | btrfs_debug(fs_info, "auto deleting %Lu", |
3507 | found_key.objectid); | |
a8c9e576 JB |
3508 | ret = btrfs_del_orphan_item(trans, root, |
3509 | found_key.objectid); | |
3a45bb20 | 3510 | btrfs_end_transaction(trans); |
4ef31a45 JB |
3511 | if (ret) |
3512 | goto out; | |
7b128766 JB |
3513 | continue; |
3514 | } | |
3515 | ||
a8c9e576 JB |
3516 | /* |
3517 | * add this inode to the orphan list so btrfs_orphan_del does | |
3518 | * the proper thing when we hit it | |
3519 | */ | |
8a35d95f JB |
3520 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3521 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3522 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3523 | |
7b128766 JB |
3524 | /* if we have links, this was a truncate, lets do that */ |
3525 | if (inode->i_nlink) { | |
fae7f21c | 3526 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3527 | iput(inode); |
3528 | continue; | |
3529 | } | |
7b128766 | 3530 | nr_truncate++; |
f3fe820c JB |
3531 | |
3532 | /* 1 for the orphan item deletion. */ | |
3533 | trans = btrfs_start_transaction(root, 1); | |
3534 | if (IS_ERR(trans)) { | |
c69b26b0 | 3535 | iput(inode); |
f3fe820c JB |
3536 | ret = PTR_ERR(trans); |
3537 | goto out; | |
3538 | } | |
3539 | ret = btrfs_orphan_add(trans, inode); | |
3a45bb20 | 3540 | btrfs_end_transaction(trans); |
c69b26b0 JB |
3541 | if (ret) { |
3542 | iput(inode); | |
f3fe820c | 3543 | goto out; |
c69b26b0 | 3544 | } |
f3fe820c | 3545 | |
66b4ffd1 | 3546 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3547 | if (ret) |
3548 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3549 | } else { |
3550 | nr_unlink++; | |
3551 | } | |
3552 | ||
3553 | /* this will do delete_inode and everything for us */ | |
3554 | iput(inode); | |
66b4ffd1 JB |
3555 | if (ret) |
3556 | goto out; | |
7b128766 | 3557 | } |
3254c876 MX |
3558 | /* release the path since we're done with it */ |
3559 | btrfs_release_path(path); | |
3560 | ||
d68fc57b YZ |
3561 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3562 | ||
3563 | if (root->orphan_block_rsv) | |
2ff7e61e | 3564 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
d68fc57b YZ |
3565 | (u64)-1); |
3566 | ||
27cdeb70 MX |
3567 | if (root->orphan_block_rsv || |
3568 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3569 | trans = btrfs_join_transaction(root); |
66b4ffd1 | 3570 | if (!IS_ERR(trans)) |
3a45bb20 | 3571 | btrfs_end_transaction(trans); |
d68fc57b | 3572 | } |
7b128766 JB |
3573 | |
3574 | if (nr_unlink) | |
0b246afa | 3575 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3576 | if (nr_truncate) |
0b246afa | 3577 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3578 | |
3579 | out: | |
3580 | if (ret) | |
0b246afa | 3581 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3582 | btrfs_free_path(path); |
3583 | return ret; | |
7b128766 JB |
3584 | } |
3585 | ||
46a53cca CM |
3586 | /* |
3587 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3588 | * don't find any xattrs, we know there can't be any acls. | |
3589 | * | |
3590 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3591 | */ | |
3592 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3593 | int slot, u64 objectid, |
3594 | int *first_xattr_slot) | |
46a53cca CM |
3595 | { |
3596 | u32 nritems = btrfs_header_nritems(leaf); | |
3597 | struct btrfs_key found_key; | |
f23b5a59 JB |
3598 | static u64 xattr_access = 0; |
3599 | static u64 xattr_default = 0; | |
46a53cca CM |
3600 | int scanned = 0; |
3601 | ||
f23b5a59 | 3602 | if (!xattr_access) { |
97d79299 AG |
3603 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3604 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3605 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3606 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3607 | } |
3608 | ||
46a53cca | 3609 | slot++; |
63541927 | 3610 | *first_xattr_slot = -1; |
46a53cca CM |
3611 | while (slot < nritems) { |
3612 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3613 | ||
3614 | /* we found a different objectid, there must not be acls */ | |
3615 | if (found_key.objectid != objectid) | |
3616 | return 0; | |
3617 | ||
3618 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3619 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3620 | if (*first_xattr_slot == -1) |
3621 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3622 | if (found_key.offset == xattr_access || |
3623 | found_key.offset == xattr_default) | |
3624 | return 1; | |
3625 | } | |
46a53cca CM |
3626 | |
3627 | /* | |
3628 | * we found a key greater than an xattr key, there can't | |
3629 | * be any acls later on | |
3630 | */ | |
3631 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3632 | return 0; | |
3633 | ||
3634 | slot++; | |
3635 | scanned++; | |
3636 | ||
3637 | /* | |
3638 | * it goes inode, inode backrefs, xattrs, extents, | |
3639 | * so if there are a ton of hard links to an inode there can | |
3640 | * be a lot of backrefs. Don't waste time searching too hard, | |
3641 | * this is just an optimization | |
3642 | */ | |
3643 | if (scanned >= 8) | |
3644 | break; | |
3645 | } | |
3646 | /* we hit the end of the leaf before we found an xattr or | |
3647 | * something larger than an xattr. We have to assume the inode | |
3648 | * has acls | |
3649 | */ | |
63541927 FDBM |
3650 | if (*first_xattr_slot == -1) |
3651 | *first_xattr_slot = slot; | |
46a53cca CM |
3652 | return 1; |
3653 | } | |
3654 | ||
d352ac68 CM |
3655 | /* |
3656 | * read an inode from the btree into the in-memory inode | |
3657 | */ | |
67710892 | 3658 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 | 3659 | { |
0b246afa | 3660 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 3661 | struct btrfs_path *path; |
5f39d397 | 3662 | struct extent_buffer *leaf; |
39279cc3 CM |
3663 | struct btrfs_inode_item *inode_item; |
3664 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3665 | struct btrfs_key location; | |
67de1176 | 3666 | unsigned long ptr; |
46a53cca | 3667 | int maybe_acls; |
618e21d5 | 3668 | u32 rdev; |
39279cc3 | 3669 | int ret; |
2f7e33d4 | 3670 | bool filled = false; |
63541927 | 3671 | int first_xattr_slot; |
2f7e33d4 MX |
3672 | |
3673 | ret = btrfs_fill_inode(inode, &rdev); | |
3674 | if (!ret) | |
3675 | filled = true; | |
39279cc3 CM |
3676 | |
3677 | path = btrfs_alloc_path(); | |
67710892 FM |
3678 | if (!path) { |
3679 | ret = -ENOMEM; | |
1748f843 | 3680 | goto make_bad; |
67710892 | 3681 | } |
1748f843 | 3682 | |
39279cc3 | 3683 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3684 | |
39279cc3 | 3685 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3686 | if (ret) { |
3687 | if (ret > 0) | |
3688 | ret = -ENOENT; | |
39279cc3 | 3689 | goto make_bad; |
67710892 | 3690 | } |
39279cc3 | 3691 | |
5f39d397 | 3692 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3693 | |
3694 | if (filled) | |
67de1176 | 3695 | goto cache_index; |
2f7e33d4 | 3696 | |
5f39d397 CM |
3697 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3698 | struct btrfs_inode_item); | |
5f39d397 | 3699 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3700 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3701 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3702 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3703 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3704 | |
a937b979 DS |
3705 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3706 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3707 | |
a937b979 DS |
3708 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3709 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3710 | |
a937b979 DS |
3711 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3712 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3713 | |
9cc97d64 | 3714 | BTRFS_I(inode)->i_otime.tv_sec = |
3715 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3716 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3717 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3718 | |
a76a3cd4 | 3719 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3720 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3721 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3722 | ||
6e17d30b YD |
3723 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3724 | inode->i_generation = BTRFS_I(inode)->generation; | |
3725 | inode->i_rdev = 0; | |
3726 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3727 | ||
3728 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3729 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3730 | ||
3731 | cache_index: | |
5dc562c5 JB |
3732 | /* |
3733 | * If we were modified in the current generation and evicted from memory | |
3734 | * and then re-read we need to do a full sync since we don't have any | |
3735 | * idea about which extents were modified before we were evicted from | |
3736 | * cache. | |
6e17d30b YD |
3737 | * |
3738 | * This is required for both inode re-read from disk and delayed inode | |
3739 | * in delayed_nodes_tree. | |
5dc562c5 | 3740 | */ |
0b246afa | 3741 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
5dc562c5 JB |
3742 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3743 | &BTRFS_I(inode)->runtime_flags); | |
3744 | ||
bde6c242 FM |
3745 | /* |
3746 | * We don't persist the id of the transaction where an unlink operation | |
3747 | * against the inode was last made. So here we assume the inode might | |
3748 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3749 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3750 | * between the inode and its parent if the inode is fsync'ed and the log | |
3751 | * replayed. For example, in the scenario: | |
3752 | * | |
3753 | * touch mydir/foo | |
3754 | * ln mydir/foo mydir/bar | |
3755 | * sync | |
3756 | * unlink mydir/bar | |
3757 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3758 | * xfs_io -c fsync mydir/foo | |
3759 | * <power failure> | |
3760 | * mount fs, triggers fsync log replay | |
3761 | * | |
3762 | * We must make sure that when we fsync our inode foo we also log its | |
3763 | * parent inode, otherwise after log replay the parent still has the | |
3764 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3765 | * and doesn't have an inode ref with the name "bar" anymore. | |
3766 | * | |
3767 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3768 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3769 | * transaction commits on fsync if our inode is a directory, or if our |
3770 | * inode is not a directory, logging its parent unnecessarily. | |
3771 | */ | |
3772 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3773 | ||
67de1176 MX |
3774 | path->slots[0]++; |
3775 | if (inode->i_nlink != 1 || | |
3776 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3777 | goto cache_acl; | |
3778 | ||
3779 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
4a0cc7ca | 3780 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
67de1176 MX |
3781 | goto cache_acl; |
3782 | ||
3783 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3784 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3785 | struct btrfs_inode_ref *ref; | |
3786 | ||
3787 | ref = (struct btrfs_inode_ref *)ptr; | |
3788 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3789 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3790 | struct btrfs_inode_extref *extref; | |
3791 | ||
3792 | extref = (struct btrfs_inode_extref *)ptr; | |
3793 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3794 | extref); | |
3795 | } | |
2f7e33d4 | 3796 | cache_acl: |
46a53cca CM |
3797 | /* |
3798 | * try to precache a NULL acl entry for files that don't have | |
3799 | * any xattrs or acls | |
3800 | */ | |
33345d01 | 3801 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
4a0cc7ca | 3802 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
63541927 FDBM |
3803 | if (first_xattr_slot != -1) { |
3804 | path->slots[0] = first_xattr_slot; | |
3805 | ret = btrfs_load_inode_props(inode, path); | |
3806 | if (ret) | |
0b246afa | 3807 | btrfs_err(fs_info, |
351fd353 | 3808 | "error loading props for ino %llu (root %llu): %d", |
4a0cc7ca | 3809 | btrfs_ino(BTRFS_I(inode)), |
63541927 FDBM |
3810 | root->root_key.objectid, ret); |
3811 | } | |
3812 | btrfs_free_path(path); | |
3813 | ||
72c04902 AV |
3814 | if (!maybe_acls) |
3815 | cache_no_acl(inode); | |
46a53cca | 3816 | |
39279cc3 | 3817 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3818 | case S_IFREG: |
3819 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3820 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3821 | inode->i_fop = &btrfs_file_operations; |
3822 | inode->i_op = &btrfs_file_inode_operations; | |
3823 | break; | |
3824 | case S_IFDIR: | |
3825 | inode->i_fop = &btrfs_dir_file_operations; | |
67ade058 | 3826 | inode->i_op = &btrfs_dir_inode_operations; |
39279cc3 CM |
3827 | break; |
3828 | case S_IFLNK: | |
3829 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3830 | inode_nohighmem(inode); |
39279cc3 CM |
3831 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3832 | break; | |
618e21d5 | 3833 | default: |
0279b4cd | 3834 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3835 | init_special_inode(inode, inode->i_mode, rdev); |
3836 | break; | |
39279cc3 | 3837 | } |
6cbff00f CH |
3838 | |
3839 | btrfs_update_iflags(inode); | |
67710892 | 3840 | return 0; |
39279cc3 CM |
3841 | |
3842 | make_bad: | |
39279cc3 | 3843 | btrfs_free_path(path); |
39279cc3 | 3844 | make_bad_inode(inode); |
67710892 | 3845 | return ret; |
39279cc3 CM |
3846 | } |
3847 | ||
d352ac68 CM |
3848 | /* |
3849 | * given a leaf and an inode, copy the inode fields into the leaf | |
3850 | */ | |
e02119d5 CM |
3851 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3852 | struct extent_buffer *leaf, | |
5f39d397 | 3853 | struct btrfs_inode_item *item, |
39279cc3 CM |
3854 | struct inode *inode) |
3855 | { | |
51fab693 LB |
3856 | struct btrfs_map_token token; |
3857 | ||
3858 | btrfs_init_map_token(&token); | |
5f39d397 | 3859 | |
51fab693 LB |
3860 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3861 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3862 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3863 | &token); | |
3864 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3865 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3866 | |
a937b979 | 3867 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3868 | inode->i_atime.tv_sec, &token); |
a937b979 | 3869 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3870 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3871 | |
a937b979 | 3872 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3873 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3874 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3875 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3876 | |
a937b979 | 3877 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3878 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3879 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3880 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3881 | |
9cc97d64 | 3882 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3883 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3884 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3885 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3886 | ||
51fab693 LB |
3887 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3888 | &token); | |
3889 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3890 | &token); | |
3891 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3892 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3893 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3894 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3895 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3896 | } |
3897 | ||
d352ac68 CM |
3898 | /* |
3899 | * copy everything in the in-memory inode into the btree. | |
3900 | */ | |
2115133f | 3901 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3902 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3903 | { |
3904 | struct btrfs_inode_item *inode_item; | |
3905 | struct btrfs_path *path; | |
5f39d397 | 3906 | struct extent_buffer *leaf; |
39279cc3 CM |
3907 | int ret; |
3908 | ||
3909 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3910 | if (!path) |
3911 | return -ENOMEM; | |
3912 | ||
b9473439 | 3913 | path->leave_spinning = 1; |
16cdcec7 MX |
3914 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3915 | 1); | |
39279cc3 CM |
3916 | if (ret) { |
3917 | if (ret > 0) | |
3918 | ret = -ENOENT; | |
3919 | goto failed; | |
3920 | } | |
3921 | ||
5f39d397 CM |
3922 | leaf = path->nodes[0]; |
3923 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3924 | struct btrfs_inode_item); |
39279cc3 | 3925 | |
e02119d5 | 3926 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3927 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3928 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3929 | ret = 0; |
3930 | failed: | |
39279cc3 CM |
3931 | btrfs_free_path(path); |
3932 | return ret; | |
3933 | } | |
3934 | ||
2115133f CM |
3935 | /* |
3936 | * copy everything in the in-memory inode into the btree. | |
3937 | */ | |
3938 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3939 | struct btrfs_root *root, struct inode *inode) | |
3940 | { | |
0b246afa | 3941 | struct btrfs_fs_info *fs_info = root->fs_info; |
2115133f CM |
3942 | int ret; |
3943 | ||
3944 | /* | |
3945 | * If the inode is a free space inode, we can deadlock during commit | |
3946 | * if we put it into the delayed code. | |
3947 | * | |
3948 | * The data relocation inode should also be directly updated | |
3949 | * without delay | |
3950 | */ | |
83eea1f1 | 3951 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a | 3952 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
0b246afa | 3953 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
8ea05e3a AB |
3954 | btrfs_update_root_times(trans, root); |
3955 | ||
2115133f CM |
3956 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3957 | if (!ret) | |
3958 | btrfs_set_inode_last_trans(trans, inode); | |
3959 | return ret; | |
3960 | } | |
3961 | ||
3962 | return btrfs_update_inode_item(trans, root, inode); | |
3963 | } | |
3964 | ||
be6aef60 JB |
3965 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3966 | struct btrfs_root *root, | |
3967 | struct inode *inode) | |
2115133f CM |
3968 | { |
3969 | int ret; | |
3970 | ||
3971 | ret = btrfs_update_inode(trans, root, inode); | |
3972 | if (ret == -ENOSPC) | |
3973 | return btrfs_update_inode_item(trans, root, inode); | |
3974 | return ret; | |
3975 | } | |
3976 | ||
d352ac68 CM |
3977 | /* |
3978 | * unlink helper that gets used here in inode.c and in the tree logging | |
3979 | * recovery code. It remove a link in a directory with a given name, and | |
3980 | * also drops the back refs in the inode to the directory | |
3981 | */ | |
92986796 AV |
3982 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3983 | struct btrfs_root *root, | |
3984 | struct inode *dir, struct inode *inode, | |
3985 | const char *name, int name_len) | |
39279cc3 | 3986 | { |
0b246afa | 3987 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 3988 | struct btrfs_path *path; |
39279cc3 | 3989 | int ret = 0; |
5f39d397 | 3990 | struct extent_buffer *leaf; |
39279cc3 | 3991 | struct btrfs_dir_item *di; |
5f39d397 | 3992 | struct btrfs_key key; |
aec7477b | 3993 | u64 index; |
4a0cc7ca NB |
3994 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
3995 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); | |
39279cc3 CM |
3996 | |
3997 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3998 | if (!path) { |
3999 | ret = -ENOMEM; | |
554233a6 | 4000 | goto out; |
54aa1f4d CM |
4001 | } |
4002 | ||
b9473439 | 4003 | path->leave_spinning = 1; |
33345d01 | 4004 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4005 | name, name_len, -1); |
4006 | if (IS_ERR(di)) { | |
4007 | ret = PTR_ERR(di); | |
4008 | goto err; | |
4009 | } | |
4010 | if (!di) { | |
4011 | ret = -ENOENT; | |
4012 | goto err; | |
4013 | } | |
5f39d397 CM |
4014 | leaf = path->nodes[0]; |
4015 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4016 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4017 | if (ret) |
4018 | goto err; | |
b3b4aa74 | 4019 | btrfs_release_path(path); |
39279cc3 | 4020 | |
67de1176 MX |
4021 | /* |
4022 | * If we don't have dir index, we have to get it by looking up | |
4023 | * the inode ref, since we get the inode ref, remove it directly, | |
4024 | * it is unnecessary to do delayed deletion. | |
4025 | * | |
4026 | * But if we have dir index, needn't search inode ref to get it. | |
4027 | * Since the inode ref is close to the inode item, it is better | |
4028 | * that we delay to delete it, and just do this deletion when | |
4029 | * we update the inode item. | |
4030 | */ | |
4031 | if (BTRFS_I(inode)->dir_index) { | |
e07222c7 | 4032 | ret = btrfs_delayed_delete_inode_ref(BTRFS_I(inode)); |
67de1176 MX |
4033 | if (!ret) { |
4034 | index = BTRFS_I(inode)->dir_index; | |
4035 | goto skip_backref; | |
4036 | } | |
4037 | } | |
4038 | ||
33345d01 LZ |
4039 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4040 | dir_ino, &index); | |
aec7477b | 4041 | if (ret) { |
0b246afa | 4042 | btrfs_info(fs_info, |
c2cf52eb | 4043 | "failed to delete reference to %.*s, inode %llu parent %llu", |
c1c9ff7c | 4044 | name_len, name, ino, dir_ino); |
66642832 | 4045 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4046 | goto err; |
4047 | } | |
67de1176 | 4048 | skip_backref: |
e67bbbb9 | 4049 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4050 | if (ret) { |
66642832 | 4051 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4052 | goto err; |
79787eaa | 4053 | } |
39279cc3 | 4054 | |
e02119d5 | 4055 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
a491abb2 | 4056 | BTRFS_I(inode), dir_ino); |
79787eaa | 4057 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4058 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4059 | goto err; |
4060 | } | |
e02119d5 CM |
4061 | |
4062 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
49f34d1f | 4063 | BTRFS_I(dir), index); |
6418c961 CM |
4064 | if (ret == -ENOENT) |
4065 | ret = 0; | |
d4e3991b | 4066 | else if (ret) |
66642832 | 4067 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4068 | err: |
4069 | btrfs_free_path(path); | |
e02119d5 CM |
4070 | if (ret) |
4071 | goto out; | |
4072 | ||
4073 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
4074 | inode_inc_iversion(inode); |
4075 | inode_inc_iversion(dir); | |
04b285f3 | 4076 | inode->i_ctime = dir->i_mtime = |
c2050a45 | 4077 | dir->i_ctime = current_time(inode); |
b9959295 | 4078 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4079 | out: |
39279cc3 CM |
4080 | return ret; |
4081 | } | |
4082 | ||
92986796 AV |
4083 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4084 | struct btrfs_root *root, | |
4085 | struct inode *dir, struct inode *inode, | |
4086 | const char *name, int name_len) | |
4087 | { | |
4088 | int ret; | |
4089 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4090 | if (!ret) { | |
8b558c5f | 4091 | drop_nlink(inode); |
92986796 AV |
4092 | ret = btrfs_update_inode(trans, root, inode); |
4093 | } | |
4094 | return ret; | |
4095 | } | |
39279cc3 | 4096 | |
a22285a6 YZ |
4097 | /* |
4098 | * helper to start transaction for unlink and rmdir. | |
4099 | * | |
d52be818 JB |
4100 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4101 | * if we cannot make our reservations the normal way try and see if there is | |
4102 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4103 | * allow the unlink to occur. | |
a22285a6 | 4104 | */ |
d52be818 | 4105 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4106 | { |
a22285a6 | 4107 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4108 | |
e70bea5f JB |
4109 | /* |
4110 | * 1 for the possible orphan item | |
4111 | * 1 for the dir item | |
4112 | * 1 for the dir index | |
4113 | * 1 for the inode ref | |
e70bea5f JB |
4114 | * 1 for the inode |
4115 | */ | |
8eab77ff | 4116 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4117 | } |
4118 | ||
4119 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4120 | { | |
4121 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4122 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4123 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4124 | int ret; |
a22285a6 | 4125 | |
d52be818 | 4126 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4127 | if (IS_ERR(trans)) |
4128 | return PTR_ERR(trans); | |
5f39d397 | 4129 | |
4176bdbf | 4130 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), 0); |
12fcfd22 | 4131 | |
2b0143b5 | 4132 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4133 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4134 | if (ret) |
4135 | goto out; | |
7b128766 | 4136 | |
a22285a6 | 4137 | if (inode->i_nlink == 0) { |
7b128766 | 4138 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4139 | if (ret) |
4140 | goto out; | |
a22285a6 | 4141 | } |
7b128766 | 4142 | |
b532402e | 4143 | out: |
3a45bb20 | 4144 | btrfs_end_transaction(trans); |
2ff7e61e | 4145 | btrfs_btree_balance_dirty(root->fs_info); |
39279cc3 CM |
4146 | return ret; |
4147 | } | |
4148 | ||
4df27c4d YZ |
4149 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4150 | struct btrfs_root *root, | |
4151 | struct inode *dir, u64 objectid, | |
4152 | const char *name, int name_len) | |
4153 | { | |
0b246afa | 4154 | struct btrfs_fs_info *fs_info = root->fs_info; |
4df27c4d YZ |
4155 | struct btrfs_path *path; |
4156 | struct extent_buffer *leaf; | |
4157 | struct btrfs_dir_item *di; | |
4158 | struct btrfs_key key; | |
4159 | u64 index; | |
4160 | int ret; | |
4a0cc7ca | 4161 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
4df27c4d YZ |
4162 | |
4163 | path = btrfs_alloc_path(); | |
4164 | if (!path) | |
4165 | return -ENOMEM; | |
4166 | ||
33345d01 | 4167 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4168 | name, name_len, -1); |
79787eaa JM |
4169 | if (IS_ERR_OR_NULL(di)) { |
4170 | if (!di) | |
4171 | ret = -ENOENT; | |
4172 | else | |
4173 | ret = PTR_ERR(di); | |
4174 | goto out; | |
4175 | } | |
4df27c4d YZ |
4176 | |
4177 | leaf = path->nodes[0]; | |
4178 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4179 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4180 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4181 | if (ret) { |
66642832 | 4182 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4183 | goto out; |
4184 | } | |
b3b4aa74 | 4185 | btrfs_release_path(path); |
4df27c4d | 4186 | |
0b246afa JM |
4187 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
4188 | root->root_key.objectid, dir_ino, | |
4189 | &index, name, name_len); | |
4df27c4d | 4190 | if (ret < 0) { |
79787eaa | 4191 | if (ret != -ENOENT) { |
66642832 | 4192 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4193 | goto out; |
4194 | } | |
33345d01 | 4195 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4196 | name, name_len); |
79787eaa JM |
4197 | if (IS_ERR_OR_NULL(di)) { |
4198 | if (!di) | |
4199 | ret = -ENOENT; | |
4200 | else | |
4201 | ret = PTR_ERR(di); | |
66642832 | 4202 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4203 | goto out; |
4204 | } | |
4df27c4d YZ |
4205 | |
4206 | leaf = path->nodes[0]; | |
4207 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4208 | btrfs_release_path(path); |
4df27c4d YZ |
4209 | index = key.offset; |
4210 | } | |
945d8962 | 4211 | btrfs_release_path(path); |
4df27c4d | 4212 | |
e67bbbb9 | 4213 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
79787eaa | 4214 | if (ret) { |
66642832 | 4215 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4216 | goto out; |
4217 | } | |
4df27c4d YZ |
4218 | |
4219 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4220 | inode_inc_iversion(dir); |
c2050a45 | 4221 | dir->i_mtime = dir->i_ctime = current_time(dir); |
5a24e84c | 4222 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4223 | if (ret) |
66642832 | 4224 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4225 | out: |
71d7aed0 | 4226 | btrfs_free_path(path); |
79787eaa | 4227 | return ret; |
4df27c4d YZ |
4228 | } |
4229 | ||
39279cc3 CM |
4230 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4231 | { | |
2b0143b5 | 4232 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4233 | int err = 0; |
39279cc3 | 4234 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4235 | struct btrfs_trans_handle *trans; |
44f714da | 4236 | u64 last_unlink_trans; |
39279cc3 | 4237 | |
b3ae244e | 4238 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4239 | return -ENOTEMPTY; |
4a0cc7ca | 4240 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
b3ae244e | 4241 | return -EPERM; |
134d4512 | 4242 | |
d52be818 | 4243 | trans = __unlink_start_trans(dir); |
a22285a6 | 4244 | if (IS_ERR(trans)) |
5df6a9f6 | 4245 | return PTR_ERR(trans); |
5df6a9f6 | 4246 | |
4a0cc7ca | 4247 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4248 | err = btrfs_unlink_subvol(trans, root, dir, |
4249 | BTRFS_I(inode)->location.objectid, | |
4250 | dentry->d_name.name, | |
4251 | dentry->d_name.len); | |
4252 | goto out; | |
4253 | } | |
4254 | ||
7b128766 JB |
4255 | err = btrfs_orphan_add(trans, inode); |
4256 | if (err) | |
4df27c4d | 4257 | goto out; |
7b128766 | 4258 | |
44f714da FM |
4259 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4260 | ||
39279cc3 | 4261 | /* now the directory is empty */ |
2b0143b5 | 4262 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4263 | dentry->d_name.name, dentry->d_name.len); |
44f714da | 4264 | if (!err) { |
dbe674a9 | 4265 | btrfs_i_size_write(inode, 0); |
44f714da FM |
4266 | /* |
4267 | * Propagate the last_unlink_trans value of the deleted dir to | |
4268 | * its parent directory. This is to prevent an unrecoverable | |
4269 | * log tree in the case we do something like this: | |
4270 | * 1) create dir foo | |
4271 | * 2) create snapshot under dir foo | |
4272 | * 3) delete the snapshot | |
4273 | * 4) rmdir foo | |
4274 | * 5) mkdir foo | |
4275 | * 6) fsync foo or some file inside foo | |
4276 | */ | |
4277 | if (last_unlink_trans >= trans->transid) | |
4278 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4279 | } | |
4df27c4d | 4280 | out: |
3a45bb20 | 4281 | btrfs_end_transaction(trans); |
2ff7e61e | 4282 | btrfs_btree_balance_dirty(root->fs_info); |
3954401f | 4283 | |
39279cc3 CM |
4284 | return err; |
4285 | } | |
4286 | ||
28f75a0e CM |
4287 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4288 | struct btrfs_root *root, | |
4289 | u64 bytes_deleted) | |
4290 | { | |
0b246afa | 4291 | struct btrfs_fs_info *fs_info = root->fs_info; |
28f75a0e CM |
4292 | int ret; |
4293 | ||
dc95f7bf JB |
4294 | /* |
4295 | * This is only used to apply pressure to the enospc system, we don't | |
4296 | * intend to use this reservation at all. | |
4297 | */ | |
2ff7e61e | 4298 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
0b246afa JM |
4299 | bytes_deleted *= fs_info->nodesize; |
4300 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, | |
28f75a0e | 4301 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
dc95f7bf | 4302 | if (!ret) { |
0b246afa | 4303 | trace_btrfs_space_reservation(fs_info, "transaction", |
dc95f7bf JB |
4304 | trans->transid, |
4305 | bytes_deleted, 1); | |
28f75a0e | 4306 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4307 | } |
28f75a0e CM |
4308 | return ret; |
4309 | ||
4310 | } | |
4311 | ||
0305cd5f FM |
4312 | static int truncate_inline_extent(struct inode *inode, |
4313 | struct btrfs_path *path, | |
4314 | struct btrfs_key *found_key, | |
4315 | const u64 item_end, | |
4316 | const u64 new_size) | |
4317 | { | |
4318 | struct extent_buffer *leaf = path->nodes[0]; | |
4319 | int slot = path->slots[0]; | |
4320 | struct btrfs_file_extent_item *fi; | |
4321 | u32 size = (u32)(new_size - found_key->offset); | |
4322 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4323 | ||
4324 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4325 | ||
4326 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4327 | loff_t offset = new_size; | |
09cbfeaf | 4328 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4329 | |
4330 | /* | |
4331 | * Zero out the remaining of the last page of our inline extent, | |
4332 | * instead of directly truncating our inline extent here - that | |
4333 | * would be much more complex (decompressing all the data, then | |
4334 | * compressing the truncated data, which might be bigger than | |
4335 | * the size of the inline extent, resize the extent, etc). | |
4336 | * We release the path because to get the page we might need to | |
4337 | * read the extent item from disk (data not in the page cache). | |
4338 | */ | |
4339 | btrfs_release_path(path); | |
9703fefe CR |
4340 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4341 | 0); | |
0305cd5f FM |
4342 | } |
4343 | ||
4344 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4345 | size = btrfs_file_extent_calc_inline_size(size); | |
2ff7e61e | 4346 | btrfs_truncate_item(root->fs_info, path, size, 1); |
0305cd5f FM |
4347 | |
4348 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4349 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4350 | ||
4351 | return 0; | |
4352 | } | |
4353 | ||
39279cc3 CM |
4354 | /* |
4355 | * this can truncate away extent items, csum items and directory items. | |
4356 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4357 | * any higher than new_size |
39279cc3 CM |
4358 | * |
4359 | * csum items that cross the new i_size are truncated to the new size | |
4360 | * as well. | |
7b128766 JB |
4361 | * |
4362 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4363 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4364 | */ |
8082510e YZ |
4365 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4366 | struct btrfs_root *root, | |
4367 | struct inode *inode, | |
4368 | u64 new_size, u32 min_type) | |
39279cc3 | 4369 | { |
0b246afa | 4370 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 4371 | struct btrfs_path *path; |
5f39d397 | 4372 | struct extent_buffer *leaf; |
39279cc3 | 4373 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4374 | struct btrfs_key key; |
4375 | struct btrfs_key found_key; | |
39279cc3 | 4376 | u64 extent_start = 0; |
db94535d | 4377 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4378 | u64 extent_offset = 0; |
39279cc3 | 4379 | u64 item_end = 0; |
c1aa4575 | 4380 | u64 last_size = new_size; |
8082510e | 4381 | u32 found_type = (u8)-1; |
39279cc3 CM |
4382 | int found_extent; |
4383 | int del_item; | |
85e21bac CM |
4384 | int pending_del_nr = 0; |
4385 | int pending_del_slot = 0; | |
179e29e4 | 4386 | int extent_type = -1; |
8082510e YZ |
4387 | int ret; |
4388 | int err = 0; | |
4a0cc7ca | 4389 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
28ed1345 | 4390 | u64 bytes_deleted = 0; |
1262133b JB |
4391 | bool be_nice = 0; |
4392 | bool should_throttle = 0; | |
28f75a0e | 4393 | bool should_end = 0; |
8082510e YZ |
4394 | |
4395 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4396 | |
28ed1345 CM |
4397 | /* |
4398 | * for non-free space inodes and ref cows, we want to back off from | |
4399 | * time to time | |
4400 | */ | |
4401 | if (!btrfs_is_free_space_inode(inode) && | |
4402 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4403 | be_nice = 1; | |
4404 | ||
0eb0e19c MF |
4405 | path = btrfs_alloc_path(); |
4406 | if (!path) | |
4407 | return -ENOMEM; | |
e4058b54 | 4408 | path->reada = READA_BACK; |
0eb0e19c | 4409 | |
5dc562c5 JB |
4410 | /* |
4411 | * We want to drop from the next block forward in case this new size is | |
4412 | * not block aligned since we will be keeping the last block of the | |
4413 | * extent just the way it is. | |
4414 | */ | |
27cdeb70 | 4415 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa | 4416 | root == fs_info->tree_root) |
fda2832f | 4417 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
0b246afa | 4418 | fs_info->sectorsize), |
da17066c | 4419 | (u64)-1, 0); |
8082510e | 4420 | |
16cdcec7 MX |
4421 | /* |
4422 | * This function is also used to drop the items in the log tree before | |
4423 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4424 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4425 | * items. | |
4426 | */ | |
4427 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4ccb5c72 | 4428 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
16cdcec7 | 4429 | |
33345d01 | 4430 | key.objectid = ino; |
39279cc3 | 4431 | key.offset = (u64)-1; |
5f39d397 CM |
4432 | key.type = (u8)-1; |
4433 | ||
85e21bac | 4434 | search_again: |
28ed1345 CM |
4435 | /* |
4436 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4437 | * up a huge file in a single leaf. Most of the time that | |
4438 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4439 | */ | |
ee22184b | 4440 | if (be_nice && bytes_deleted > SZ_32M) { |
3a45bb20 | 4441 | if (btrfs_should_end_transaction(trans)) { |
28ed1345 CM |
4442 | err = -EAGAIN; |
4443 | goto error; | |
4444 | } | |
4445 | } | |
4446 | ||
4447 | ||
b9473439 | 4448 | path->leave_spinning = 1; |
85e21bac | 4449 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4450 | if (ret < 0) { |
4451 | err = ret; | |
4452 | goto out; | |
4453 | } | |
d397712b | 4454 | |
85e21bac | 4455 | if (ret > 0) { |
e02119d5 CM |
4456 | /* there are no items in the tree for us to truncate, we're |
4457 | * done | |
4458 | */ | |
8082510e YZ |
4459 | if (path->slots[0] == 0) |
4460 | goto out; | |
85e21bac CM |
4461 | path->slots[0]--; |
4462 | } | |
4463 | ||
d397712b | 4464 | while (1) { |
39279cc3 | 4465 | fi = NULL; |
5f39d397 CM |
4466 | leaf = path->nodes[0]; |
4467 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4468 | found_type = found_key.type; |
39279cc3 | 4469 | |
33345d01 | 4470 | if (found_key.objectid != ino) |
39279cc3 | 4471 | break; |
5f39d397 | 4472 | |
85e21bac | 4473 | if (found_type < min_type) |
39279cc3 CM |
4474 | break; |
4475 | ||
5f39d397 | 4476 | item_end = found_key.offset; |
39279cc3 | 4477 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4478 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4479 | struct btrfs_file_extent_item); |
179e29e4 CM |
4480 | extent_type = btrfs_file_extent_type(leaf, fi); |
4481 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4482 | item_end += |
db94535d | 4483 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4484 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4485 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4486 | path->slots[0], fi); |
39279cc3 | 4487 | } |
008630c1 | 4488 | item_end--; |
39279cc3 | 4489 | } |
8082510e YZ |
4490 | if (found_type > min_type) { |
4491 | del_item = 1; | |
4492 | } else { | |
91298eec LB |
4493 | if (item_end < new_size) { |
4494 | /* | |
4495 | * With NO_HOLES mode, for the following mapping | |
4496 | * | |
4497 | * [0-4k][hole][8k-12k] | |
4498 | * | |
4499 | * if truncating isize down to 6k, it ends up | |
4500 | * isize being 8k. | |
4501 | */ | |
4502 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) | |
4503 | last_size = new_size; | |
b888db2b | 4504 | break; |
91298eec | 4505 | } |
8082510e YZ |
4506 | if (found_key.offset >= new_size) |
4507 | del_item = 1; | |
4508 | else | |
4509 | del_item = 0; | |
39279cc3 | 4510 | } |
39279cc3 | 4511 | found_extent = 0; |
39279cc3 | 4512 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4513 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4514 | goto delete; | |
4515 | ||
7f4f6e0a JB |
4516 | if (del_item) |
4517 | last_size = found_key.offset; | |
4518 | else | |
4519 | last_size = new_size; | |
4520 | ||
179e29e4 | 4521 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4522 | u64 num_dec; |
db94535d | 4523 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4524 | if (!del_item) { |
db94535d CM |
4525 | u64 orig_num_bytes = |
4526 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4527 | extent_num_bytes = ALIGN(new_size - |
4528 | found_key.offset, | |
0b246afa | 4529 | fs_info->sectorsize); |
db94535d CM |
4530 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4531 | extent_num_bytes); | |
4532 | num_dec = (orig_num_bytes - | |
9069218d | 4533 | extent_num_bytes); |
27cdeb70 MX |
4534 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4535 | &root->state) && | |
4536 | extent_start != 0) | |
a76a3cd4 | 4537 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4538 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4539 | } else { |
db94535d CM |
4540 | extent_num_bytes = |
4541 | btrfs_file_extent_disk_num_bytes(leaf, | |
4542 | fi); | |
5d4f98a2 YZ |
4543 | extent_offset = found_key.offset - |
4544 | btrfs_file_extent_offset(leaf, fi); | |
4545 | ||
39279cc3 | 4546 | /* FIXME blocksize != 4096 */ |
9069218d | 4547 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4548 | if (extent_start != 0) { |
4549 | found_extent = 1; | |
27cdeb70 MX |
4550 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4551 | &root->state)) | |
a76a3cd4 | 4552 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4553 | } |
39279cc3 | 4554 | } |
9069218d | 4555 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4556 | /* |
4557 | * we can't truncate inline items that have had | |
4558 | * special encodings | |
4559 | */ | |
4560 | if (!del_item && | |
c8b97818 CM |
4561 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4562 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4563 | |
4564 | /* | |
0305cd5f FM |
4565 | * Need to release path in order to truncate a |
4566 | * compressed extent. So delete any accumulated | |
4567 | * extent items so far. | |
514ac8ad | 4568 | */ |
0305cd5f FM |
4569 | if (btrfs_file_extent_compression(leaf, fi) != |
4570 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4571 | err = btrfs_del_items(trans, root, path, | |
4572 | pending_del_slot, | |
4573 | pending_del_nr); | |
4574 | if (err) { | |
4575 | btrfs_abort_transaction(trans, | |
0305cd5f FM |
4576 | err); |
4577 | goto error; | |
4578 | } | |
4579 | pending_del_nr = 0; | |
4580 | } | |
4581 | ||
4582 | err = truncate_inline_extent(inode, path, | |
4583 | &found_key, | |
4584 | item_end, | |
4585 | new_size); | |
4586 | if (err) { | |
66642832 | 4587 | btrfs_abort_transaction(trans, err); |
0305cd5f FM |
4588 | goto error; |
4589 | } | |
27cdeb70 MX |
4590 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4591 | &root->state)) { | |
0305cd5f | 4592 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4593 | } |
39279cc3 | 4594 | } |
179e29e4 | 4595 | delete: |
39279cc3 | 4596 | if (del_item) { |
85e21bac CM |
4597 | if (!pending_del_nr) { |
4598 | /* no pending yet, add ourselves */ | |
4599 | pending_del_slot = path->slots[0]; | |
4600 | pending_del_nr = 1; | |
4601 | } else if (pending_del_nr && | |
4602 | path->slots[0] + 1 == pending_del_slot) { | |
4603 | /* hop on the pending chunk */ | |
4604 | pending_del_nr++; | |
4605 | pending_del_slot = path->slots[0]; | |
4606 | } else { | |
d397712b | 4607 | BUG(); |
85e21bac | 4608 | } |
39279cc3 CM |
4609 | } else { |
4610 | break; | |
4611 | } | |
28f75a0e CM |
4612 | should_throttle = 0; |
4613 | ||
27cdeb70 MX |
4614 | if (found_extent && |
4615 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
0b246afa | 4616 | root == fs_info->tree_root)) { |
b9473439 | 4617 | btrfs_set_path_blocking(path); |
28ed1345 | 4618 | bytes_deleted += extent_num_bytes; |
2ff7e61e | 4619 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
5d4f98a2 YZ |
4620 | extent_num_bytes, 0, |
4621 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4622 | ino, extent_offset); |
39279cc3 | 4623 | BUG_ON(ret); |
2ff7e61e JM |
4624 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
4625 | btrfs_async_run_delayed_refs(fs_info, | |
dd4b857a WX |
4626 | trans->delayed_ref_updates * 2, |
4627 | trans->transid, 0); | |
28f75a0e CM |
4628 | if (be_nice) { |
4629 | if (truncate_space_check(trans, root, | |
4630 | extent_num_bytes)) { | |
4631 | should_end = 1; | |
4632 | } | |
4633 | if (btrfs_should_throttle_delayed_refs(trans, | |
2ff7e61e | 4634 | fs_info)) |
28f75a0e | 4635 | should_throttle = 1; |
28f75a0e | 4636 | } |
39279cc3 | 4637 | } |
85e21bac | 4638 | |
8082510e YZ |
4639 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4640 | break; | |
4641 | ||
4642 | if (path->slots[0] == 0 || | |
1262133b | 4643 | path->slots[0] != pending_del_slot || |
28f75a0e | 4644 | should_throttle || should_end) { |
8082510e YZ |
4645 | if (pending_del_nr) { |
4646 | ret = btrfs_del_items(trans, root, path, | |
4647 | pending_del_slot, | |
4648 | pending_del_nr); | |
79787eaa | 4649 | if (ret) { |
66642832 | 4650 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4651 | goto error; |
4652 | } | |
8082510e YZ |
4653 | pending_del_nr = 0; |
4654 | } | |
b3b4aa74 | 4655 | btrfs_release_path(path); |
28f75a0e | 4656 | if (should_throttle) { |
1262133b JB |
4657 | unsigned long updates = trans->delayed_ref_updates; |
4658 | if (updates) { | |
4659 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4660 | ret = btrfs_run_delayed_refs(trans, |
4661 | fs_info, | |
4662 | updates * 2); | |
1262133b JB |
4663 | if (ret && !err) |
4664 | err = ret; | |
4665 | } | |
4666 | } | |
28f75a0e CM |
4667 | /* |
4668 | * if we failed to refill our space rsv, bail out | |
4669 | * and let the transaction restart | |
4670 | */ | |
4671 | if (should_end) { | |
4672 | err = -EAGAIN; | |
4673 | goto error; | |
4674 | } | |
85e21bac | 4675 | goto search_again; |
8082510e YZ |
4676 | } else { |
4677 | path->slots[0]--; | |
85e21bac | 4678 | } |
39279cc3 | 4679 | } |
8082510e | 4680 | out: |
85e21bac CM |
4681 | if (pending_del_nr) { |
4682 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4683 | pending_del_nr); | |
79787eaa | 4684 | if (ret) |
66642832 | 4685 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4686 | } |
79787eaa | 4687 | error: |
c1aa4575 | 4688 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4689 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4690 | |
39279cc3 | 4691 | btrfs_free_path(path); |
28ed1345 | 4692 | |
ee22184b | 4693 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4694 | unsigned long updates = trans->delayed_ref_updates; |
4695 | if (updates) { | |
4696 | trans->delayed_ref_updates = 0; | |
2ff7e61e JM |
4697 | ret = btrfs_run_delayed_refs(trans, fs_info, |
4698 | updates * 2); | |
28ed1345 CM |
4699 | if (ret && !err) |
4700 | err = ret; | |
4701 | } | |
4702 | } | |
8082510e | 4703 | return err; |
39279cc3 CM |
4704 | } |
4705 | ||
4706 | /* | |
9703fefe | 4707 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4708 | * @inode - inode that we're zeroing |
4709 | * @from - the offset to start zeroing | |
4710 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4711 | * offset | |
4712 | * @front - zero up to the offset instead of from the offset on | |
4713 | * | |
9703fefe | 4714 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4715 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4716 | */ |
9703fefe | 4717 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4718 | int front) |
39279cc3 | 4719 | { |
0b246afa | 4720 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 | 4721 | struct address_space *mapping = inode->i_mapping; |
e6dcd2dc CM |
4722 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4723 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4724 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4725 | char *kaddr; |
0b246afa | 4726 | u32 blocksize = fs_info->sectorsize; |
09cbfeaf | 4727 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4728 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4729 | struct page *page; |
3b16a4e3 | 4730 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4731 | int ret = 0; |
9703fefe CR |
4732 | u64 block_start; |
4733 | u64 block_end; | |
39279cc3 | 4734 | |
2aaa6655 JB |
4735 | if ((offset & (blocksize - 1)) == 0 && |
4736 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4737 | goto out; |
9703fefe | 4738 | |
7cf5b976 | 4739 | ret = btrfs_delalloc_reserve_space(inode, |
9703fefe | 4740 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4741 | if (ret) |
4742 | goto out; | |
39279cc3 | 4743 | |
211c17f5 | 4744 | again: |
3b16a4e3 | 4745 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4746 | if (!page) { |
7cf5b976 | 4747 | btrfs_delalloc_release_space(inode, |
9703fefe CR |
4748 | round_down(from, blocksize), |
4749 | blocksize); | |
ac6a2b36 | 4750 | ret = -ENOMEM; |
39279cc3 | 4751 | goto out; |
5d5e103a | 4752 | } |
e6dcd2dc | 4753 | |
9703fefe CR |
4754 | block_start = round_down(from, blocksize); |
4755 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4756 | |
39279cc3 | 4757 | if (!PageUptodate(page)) { |
9ebefb18 | 4758 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4759 | lock_page(page); |
211c17f5 CM |
4760 | if (page->mapping != mapping) { |
4761 | unlock_page(page); | |
09cbfeaf | 4762 | put_page(page); |
211c17f5 CM |
4763 | goto again; |
4764 | } | |
39279cc3 CM |
4765 | if (!PageUptodate(page)) { |
4766 | ret = -EIO; | |
89642229 | 4767 | goto out_unlock; |
39279cc3 CM |
4768 | } |
4769 | } | |
211c17f5 | 4770 | wait_on_page_writeback(page); |
e6dcd2dc | 4771 | |
9703fefe | 4772 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4773 | set_page_extent_mapped(page); |
4774 | ||
9703fefe | 4775 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4776 | if (ordered) { |
9703fefe | 4777 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4778 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4779 | unlock_page(page); |
09cbfeaf | 4780 | put_page(page); |
eb84ae03 | 4781 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4782 | btrfs_put_ordered_extent(ordered); |
4783 | goto again; | |
4784 | } | |
4785 | ||
9703fefe | 4786 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4787 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4788 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4789 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4790 | |
9703fefe | 4791 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4792 | &cached_state, 0); |
9ed74f2d | 4793 | if (ret) { |
9703fefe | 4794 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4795 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4796 | goto out_unlock; |
4797 | } | |
4798 | ||
9703fefe | 4799 | if (offset != blocksize) { |
2aaa6655 | 4800 | if (!len) |
9703fefe | 4801 | len = blocksize - offset; |
e6dcd2dc | 4802 | kaddr = kmap(page); |
2aaa6655 | 4803 | if (front) |
9703fefe CR |
4804 | memset(kaddr + (block_start - page_offset(page)), |
4805 | 0, offset); | |
2aaa6655 | 4806 | else |
9703fefe CR |
4807 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4808 | 0, len); | |
e6dcd2dc CM |
4809 | flush_dcache_page(page); |
4810 | kunmap(page); | |
4811 | } | |
247e743c | 4812 | ClearPageChecked(page); |
e6dcd2dc | 4813 | set_page_dirty(page); |
9703fefe | 4814 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4815 | GFP_NOFS); |
39279cc3 | 4816 | |
89642229 | 4817 | out_unlock: |
5d5e103a | 4818 | if (ret) |
9703fefe CR |
4819 | btrfs_delalloc_release_space(inode, block_start, |
4820 | blocksize); | |
39279cc3 | 4821 | unlock_page(page); |
09cbfeaf | 4822 | put_page(page); |
39279cc3 CM |
4823 | out: |
4824 | return ret; | |
4825 | } | |
4826 | ||
16e7549f JB |
4827 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4828 | u64 offset, u64 len) | |
4829 | { | |
0b246afa | 4830 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
16e7549f JB |
4831 | struct btrfs_trans_handle *trans; |
4832 | int ret; | |
4833 | ||
4834 | /* | |
4835 | * Still need to make sure the inode looks like it's been updated so | |
4836 | * that any holes get logged if we fsync. | |
4837 | */ | |
0b246afa JM |
4838 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
4839 | BTRFS_I(inode)->last_trans = fs_info->generation; | |
16e7549f JB |
4840 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
4841 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4842 | return 0; | |
4843 | } | |
4844 | ||
4845 | /* | |
4846 | * 1 - for the one we're dropping | |
4847 | * 1 - for the one we're adding | |
4848 | * 1 - for updating the inode. | |
4849 | */ | |
4850 | trans = btrfs_start_transaction(root, 3); | |
4851 | if (IS_ERR(trans)) | |
4852 | return PTR_ERR(trans); | |
4853 | ||
4854 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4855 | if (ret) { | |
66642832 | 4856 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4857 | btrfs_end_transaction(trans); |
16e7549f JB |
4858 | return ret; |
4859 | } | |
4860 | ||
4a0cc7ca | 4861 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), offset, |
16e7549f JB |
4862 | 0, 0, len, 0, len, 0, 0, 0); |
4863 | if (ret) | |
66642832 | 4864 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4865 | else |
4866 | btrfs_update_inode(trans, root, inode); | |
3a45bb20 | 4867 | btrfs_end_transaction(trans); |
16e7549f JB |
4868 | return ret; |
4869 | } | |
4870 | ||
695a0d0d JB |
4871 | /* |
4872 | * This function puts in dummy file extents for the area we're creating a hole | |
4873 | * for. So if we are truncating this file to a larger size we need to insert | |
4874 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4875 | * the range between oldsize and size | |
4876 | */ | |
a41ad394 | 4877 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4878 | { |
0b246afa | 4879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
9036c102 YZ |
4880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4881 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4882 | struct extent_map *em = NULL; |
2ac55d41 | 4883 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4884 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
0b246afa JM |
4885 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
4886 | u64 block_end = ALIGN(size, fs_info->sectorsize); | |
9036c102 YZ |
4887 | u64 last_byte; |
4888 | u64 cur_offset; | |
4889 | u64 hole_size; | |
9ed74f2d | 4890 | int err = 0; |
39279cc3 | 4891 | |
a71754fc | 4892 | /* |
9703fefe CR |
4893 | * If our size started in the middle of a block we need to zero out the |
4894 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4895 | * expose stale data. |
4896 | */ | |
9703fefe | 4897 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4898 | if (err) |
4899 | return err; | |
4900 | ||
9036c102 YZ |
4901 | if (size <= hole_start) |
4902 | return 0; | |
4903 | ||
9036c102 YZ |
4904 | while (1) { |
4905 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4906 | |
ff13db41 | 4907 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4908 | &cached_state); |
fa7c1494 MX |
4909 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4910 | block_end - hole_start); | |
9036c102 YZ |
4911 | if (!ordered) |
4912 | break; | |
2ac55d41 JB |
4913 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4914 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4915 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4916 | btrfs_put_ordered_extent(ordered); |
4917 | } | |
39279cc3 | 4918 | |
9036c102 YZ |
4919 | cur_offset = hole_start; |
4920 | while (1) { | |
4921 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4922 | block_end - cur_offset, 0); | |
79787eaa JM |
4923 | if (IS_ERR(em)) { |
4924 | err = PTR_ERR(em); | |
f2767956 | 4925 | em = NULL; |
79787eaa JM |
4926 | break; |
4927 | } | |
9036c102 | 4928 | last_byte = min(extent_map_end(em), block_end); |
0b246afa | 4929 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
8082510e | 4930 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4931 | struct extent_map *hole_em; |
9036c102 | 4932 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4933 | |
16e7549f JB |
4934 | err = maybe_insert_hole(root, inode, cur_offset, |
4935 | hole_size); | |
4936 | if (err) | |
3893e33b | 4937 | break; |
5dc562c5 JB |
4938 | btrfs_drop_extent_cache(inode, cur_offset, |
4939 | cur_offset + hole_size - 1, 0); | |
4940 | hole_em = alloc_extent_map(); | |
4941 | if (!hole_em) { | |
4942 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4943 | &BTRFS_I(inode)->runtime_flags); | |
4944 | goto next; | |
4945 | } | |
4946 | hole_em->start = cur_offset; | |
4947 | hole_em->len = hole_size; | |
4948 | hole_em->orig_start = cur_offset; | |
8082510e | 4949 | |
5dc562c5 JB |
4950 | hole_em->block_start = EXTENT_MAP_HOLE; |
4951 | hole_em->block_len = 0; | |
b4939680 | 4952 | hole_em->orig_block_len = 0; |
cc95bef6 | 4953 | hole_em->ram_bytes = hole_size; |
0b246afa | 4954 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 | 4955 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
0b246afa | 4956 | hole_em->generation = fs_info->generation; |
8082510e | 4957 | |
5dc562c5 JB |
4958 | while (1) { |
4959 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4960 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4961 | write_unlock(&em_tree->lock); |
4962 | if (err != -EEXIST) | |
4963 | break; | |
4964 | btrfs_drop_extent_cache(inode, cur_offset, | |
4965 | cur_offset + | |
4966 | hole_size - 1, 0); | |
4967 | } | |
4968 | free_extent_map(hole_em); | |
9036c102 | 4969 | } |
16e7549f | 4970 | next: |
9036c102 | 4971 | free_extent_map(em); |
a22285a6 | 4972 | em = NULL; |
9036c102 | 4973 | cur_offset = last_byte; |
8082510e | 4974 | if (cur_offset >= block_end) |
9036c102 YZ |
4975 | break; |
4976 | } | |
a22285a6 | 4977 | free_extent_map(em); |
2ac55d41 JB |
4978 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4979 | GFP_NOFS); | |
9036c102 YZ |
4980 | return err; |
4981 | } | |
39279cc3 | 4982 | |
3972f260 | 4983 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4984 | { |
f4a2f4c5 MX |
4985 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4986 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4987 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4988 | loff_t newsize = attr->ia_size; |
4989 | int mask = attr->ia_valid; | |
8082510e YZ |
4990 | int ret; |
4991 | ||
3972f260 ES |
4992 | /* |
4993 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4994 | * special case where we need to update the times despite not having | |
4995 | * these flags set. For all other operations the VFS set these flags | |
4996 | * explicitly if it wants a timestamp update. | |
4997 | */ | |
dff6efc3 CH |
4998 | if (newsize != oldsize) { |
4999 | inode_inc_iversion(inode); | |
5000 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
5001 | inode->i_ctime = inode->i_mtime = | |
c2050a45 | 5002 | current_time(inode); |
dff6efc3 | 5003 | } |
3972f260 | 5004 | |
a41ad394 | 5005 | if (newsize > oldsize) { |
9ea24bbe FM |
5006 | /* |
5007 | * Don't do an expanding truncate while snapshoting is ongoing. | |
5008 | * This is to ensure the snapshot captures a fully consistent | |
5009 | * state of this file - if the snapshot captures this expanding | |
5010 | * truncation, it must capture all writes that happened before | |
5011 | * this truncation. | |
5012 | */ | |
0bc19f90 | 5013 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 5014 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
5015 | if (ret) { |
5016 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 5017 | return ret; |
9ea24bbe | 5018 | } |
8082510e | 5019 | |
f4a2f4c5 | 5020 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
5021 | if (IS_ERR(trans)) { |
5022 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 5023 | return PTR_ERR(trans); |
9ea24bbe | 5024 | } |
f4a2f4c5 MX |
5025 | |
5026 | i_size_write(inode, newsize); | |
5027 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5028 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5029 | ret = btrfs_update_inode(trans, root, inode); |
9ea24bbe | 5030 | btrfs_end_write_no_snapshoting(root); |
3a45bb20 | 5031 | btrfs_end_transaction(trans); |
a41ad394 | 5032 | } else { |
8082510e | 5033 | |
a41ad394 JB |
5034 | /* |
5035 | * We're truncating a file that used to have good data down to | |
5036 | * zero. Make sure it gets into the ordered flush list so that | |
5037 | * any new writes get down to disk quickly. | |
5038 | */ | |
5039 | if (newsize == 0) | |
72ac3c0d JB |
5040 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5041 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5042 | |
f3fe820c JB |
5043 | /* |
5044 | * 1 for the orphan item we're going to add | |
5045 | * 1 for the orphan item deletion. | |
5046 | */ | |
5047 | trans = btrfs_start_transaction(root, 2); | |
5048 | if (IS_ERR(trans)) | |
5049 | return PTR_ERR(trans); | |
5050 | ||
5051 | /* | |
5052 | * We need to do this in case we fail at _any_ point during the | |
5053 | * actual truncate. Once we do the truncate_setsize we could | |
5054 | * invalidate pages which forces any outstanding ordered io to | |
5055 | * be instantly completed which will give us extents that need | |
5056 | * to be truncated. If we fail to get an orphan inode down we | |
5057 | * could have left over extents that were never meant to live, | |
01327610 | 5058 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5059 | * will be consistent. |
5060 | */ | |
5061 | ret = btrfs_orphan_add(trans, inode); | |
3a45bb20 | 5062 | btrfs_end_transaction(trans); |
f3fe820c JB |
5063 | if (ret) |
5064 | return ret; | |
5065 | ||
a41ad394 JB |
5066 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5067 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5068 | |
5069 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
5070 | btrfs_inode_block_unlocked_dio(inode); | |
5071 | inode_dio_wait(inode); | |
5072 | btrfs_inode_resume_unlocked_dio(inode); | |
5073 | ||
a41ad394 | 5074 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5075 | if (ret && inode->i_nlink) { |
5076 | int err; | |
5077 | ||
5078 | /* | |
5079 | * failed to truncate, disk_i_size is only adjusted down | |
5080 | * as we remove extents, so it should represent the true | |
5081 | * size of the inode, so reset the in memory size and | |
5082 | * delete our orphan entry. | |
5083 | */ | |
5084 | trans = btrfs_join_transaction(root); | |
5085 | if (IS_ERR(trans)) { | |
5086 | btrfs_orphan_del(NULL, inode); | |
5087 | return ret; | |
5088 | } | |
5089 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
5090 | err = btrfs_orphan_del(trans, inode); | |
5091 | if (err) | |
66642832 | 5092 | btrfs_abort_transaction(trans, err); |
3a45bb20 | 5093 | btrfs_end_transaction(trans); |
7f4f6e0a | 5094 | } |
8082510e YZ |
5095 | } |
5096 | ||
a41ad394 | 5097 | return ret; |
8082510e YZ |
5098 | } |
5099 | ||
9036c102 YZ |
5100 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5101 | { | |
2b0143b5 | 5102 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5103 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5104 | int err; |
39279cc3 | 5105 | |
b83cc969 LZ |
5106 | if (btrfs_root_readonly(root)) |
5107 | return -EROFS; | |
5108 | ||
31051c85 | 5109 | err = setattr_prepare(dentry, attr); |
9036c102 YZ |
5110 | if (err) |
5111 | return err; | |
2bf5a725 | 5112 | |
5a3f23d5 | 5113 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5114 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5115 | if (err) |
5116 | return err; | |
39279cc3 | 5117 | } |
9036c102 | 5118 | |
1025774c CH |
5119 | if (attr->ia_valid) { |
5120 | setattr_copy(inode, attr); | |
0c4d2d95 | 5121 | inode_inc_iversion(inode); |
22c44fe6 | 5122 | err = btrfs_dirty_inode(inode); |
1025774c | 5123 | |
22c44fe6 | 5124 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5125 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5126 | } |
33268eaf | 5127 | |
39279cc3 CM |
5128 | return err; |
5129 | } | |
61295eb8 | 5130 | |
131e404a FDBM |
5131 | /* |
5132 | * While truncating the inode pages during eviction, we get the VFS calling | |
5133 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5134 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5135 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5136 | * extent_state structures over and over, wasting lots of time. | |
5137 | * | |
5138 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5139 | * those expensive operations on a per page basis and do only the ordered io | |
5140 | * finishing, while we release here the extent_map and extent_state structures, | |
5141 | * without the excessive merging and splitting. | |
5142 | */ | |
5143 | static void evict_inode_truncate_pages(struct inode *inode) | |
5144 | { | |
5145 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5146 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5147 | struct rb_node *node; | |
5148 | ||
5149 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5150 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5151 | |
5152 | write_lock(&map_tree->lock); | |
5153 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5154 | struct extent_map *em; | |
5155 | ||
5156 | node = rb_first(&map_tree->map); | |
5157 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5158 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5159 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5160 | remove_extent_mapping(map_tree, em); |
5161 | free_extent_map(em); | |
7064dd5c FM |
5162 | if (need_resched()) { |
5163 | write_unlock(&map_tree->lock); | |
5164 | cond_resched(); | |
5165 | write_lock(&map_tree->lock); | |
5166 | } | |
131e404a FDBM |
5167 | } |
5168 | write_unlock(&map_tree->lock); | |
5169 | ||
6ca07097 FM |
5170 | /* |
5171 | * Keep looping until we have no more ranges in the io tree. | |
5172 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5173 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5174 | * still in progress (unlocked the pages in the bio but did not yet | |
5175 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5176 | * ranges can still be locked and eviction started because before |
5177 | * submitting those bios, which are executed by a separate task (work | |
5178 | * queue kthread), inode references (inode->i_count) were not taken | |
5179 | * (which would be dropped in the end io callback of each bio). | |
5180 | * Therefore here we effectively end up waiting for those bios and | |
5181 | * anyone else holding locked ranges without having bumped the inode's | |
5182 | * reference count - if we don't do it, when they access the inode's | |
5183 | * io_tree to unlock a range it may be too late, leading to an | |
5184 | * use-after-free issue. | |
5185 | */ | |
131e404a FDBM |
5186 | spin_lock(&io_tree->lock); |
5187 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5188 | struct extent_state *state; | |
5189 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5190 | u64 start; |
5191 | u64 end; | |
131e404a FDBM |
5192 | |
5193 | node = rb_first(&io_tree->state); | |
5194 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5195 | start = state->start; |
5196 | end = state->end; | |
131e404a FDBM |
5197 | spin_unlock(&io_tree->lock); |
5198 | ||
ff13db41 | 5199 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5200 | |
5201 | /* | |
5202 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5203 | * and its reserved space won't be freed by delayed_ref. | |
5204 | * So we need to free its reserved space here. | |
5205 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5206 | * | |
5207 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5208 | */ | |
5209 | if (state->state & EXTENT_DELALLOC) | |
5210 | btrfs_qgroup_free_data(inode, start, end - start + 1); | |
5211 | ||
6ca07097 | 5212 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5213 | EXTENT_LOCKED | EXTENT_DIRTY | |
5214 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5215 | EXTENT_DEFRAG, 1, 1, | |
5216 | &cached_state, GFP_NOFS); | |
131e404a | 5217 | |
7064dd5c | 5218 | cond_resched(); |
131e404a FDBM |
5219 | spin_lock(&io_tree->lock); |
5220 | } | |
5221 | spin_unlock(&io_tree->lock); | |
5222 | } | |
5223 | ||
bd555975 | 5224 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 | 5225 | { |
0b246afa | 5226 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5227 | struct btrfs_trans_handle *trans; |
5228 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5229 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5230 | int steal_from_global = 0; |
3d48d981 | 5231 | u64 min_size; |
39279cc3 CM |
5232 | int ret; |
5233 | ||
1abe9b8a | 5234 | trace_btrfs_inode_evict(inode); |
5235 | ||
3d48d981 NB |
5236 | if (!root) { |
5237 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5238 | return; | |
5239 | } | |
5240 | ||
0b246afa | 5241 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
3d48d981 | 5242 | |
131e404a FDBM |
5243 | evict_inode_truncate_pages(inode); |
5244 | ||
69e9c6c6 SB |
5245 | if (inode->i_nlink && |
5246 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5247 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5248 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5249 | goto no_delete; |
5250 | ||
39279cc3 | 5251 | if (is_bad_inode(inode)) { |
7b128766 | 5252 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5253 | goto no_delete; |
5254 | } | |
bd555975 | 5255 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5256 | if (!special_file(inode->i_mode)) |
5257 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5258 | |
f612496b MX |
5259 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5260 | ||
0b246afa | 5261 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
6bf02314 | 5262 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5263 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5264 | goto no_delete; |
5265 | } | |
5266 | ||
76dda93c | 5267 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5268 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5269 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5270 | goto no_delete; |
5271 | } | |
5272 | ||
aa79021f | 5273 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
0e8c36a9 MX |
5274 | if (ret) { |
5275 | btrfs_orphan_del(NULL, inode); | |
5276 | goto no_delete; | |
5277 | } | |
5278 | ||
2ff7e61e | 5279 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5280 | if (!rsv) { |
5281 | btrfs_orphan_del(NULL, inode); | |
5282 | goto no_delete; | |
5283 | } | |
4a338542 | 5284 | rsv->size = min_size; |
ca7e70f5 | 5285 | rsv->failfast = 1; |
0b246afa | 5286 | global_rsv = &fs_info->global_block_rsv; |
4289a667 | 5287 | |
dbe674a9 | 5288 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5289 | |
4289a667 | 5290 | /* |
8407aa46 MX |
5291 | * This is a bit simpler than btrfs_truncate since we've already |
5292 | * reserved our space for our orphan item in the unlink, so we just | |
5293 | * need to reserve some slack space in case we add bytes and update | |
5294 | * inode item when doing the truncate. | |
4289a667 | 5295 | */ |
8082510e | 5296 | while (1) { |
08e007d2 MX |
5297 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5298 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5299 | |
5300 | /* | |
5301 | * Try and steal from the global reserve since we will | |
5302 | * likely not use this space anyway, we want to try as | |
5303 | * hard as possible to get this to work. | |
5304 | */ | |
5305 | if (ret) | |
3bce876f JB |
5306 | steal_from_global++; |
5307 | else | |
5308 | steal_from_global = 0; | |
5309 | ret = 0; | |
d68fc57b | 5310 | |
3bce876f JB |
5311 | /* |
5312 | * steal_from_global == 0: we reserved stuff, hooray! | |
5313 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5314 | * steal_from_global == 2: we've committed, still not a lot of | |
5315 | * room but maybe we'll have room in the global reserve this | |
5316 | * time. | |
5317 | * steal_from_global == 3: abandon all hope! | |
5318 | */ | |
5319 | if (steal_from_global > 2) { | |
0b246afa JM |
5320 | btrfs_warn(fs_info, |
5321 | "Could not get space for a delete, will truncate on mount %d", | |
5322 | ret); | |
4289a667 | 5323 | btrfs_orphan_del(NULL, inode); |
2ff7e61e | 5324 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5325 | goto no_delete; |
d68fc57b | 5326 | } |
7b128766 | 5327 | |
0e8c36a9 | 5328 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5329 | if (IS_ERR(trans)) { |
5330 | btrfs_orphan_del(NULL, inode); | |
2ff7e61e | 5331 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5332 | goto no_delete; |
d68fc57b | 5333 | } |
7b128766 | 5334 | |
3bce876f | 5335 | /* |
01327610 | 5336 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5337 | * sure there is room to do it, if not we need to commit and try |
5338 | * again. | |
5339 | */ | |
5340 | if (steal_from_global) { | |
2ff7e61e | 5341 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
3bce876f | 5342 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
25d609f8 | 5343 | min_size, 0); |
3bce876f JB |
5344 | else |
5345 | ret = -ENOSPC; | |
5346 | } | |
5347 | ||
5348 | /* | |
5349 | * Couldn't steal from the global reserve, we have too much | |
5350 | * pending stuff built up, commit the transaction and try it | |
5351 | * again. | |
5352 | */ | |
5353 | if (ret) { | |
3a45bb20 | 5354 | ret = btrfs_commit_transaction(trans); |
3bce876f JB |
5355 | if (ret) { |
5356 | btrfs_orphan_del(NULL, inode); | |
2ff7e61e | 5357 | btrfs_free_block_rsv(fs_info, rsv); |
3bce876f JB |
5358 | goto no_delete; |
5359 | } | |
5360 | continue; | |
5361 | } else { | |
5362 | steal_from_global = 0; | |
5363 | } | |
5364 | ||
4289a667 JB |
5365 | trans->block_rsv = rsv; |
5366 | ||
d68fc57b | 5367 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5368 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5369 | break; |
85e21bac | 5370 | |
0b246afa | 5371 | trans->block_rsv = &fs_info->trans_block_rsv; |
3a45bb20 | 5372 | btrfs_end_transaction(trans); |
8082510e | 5373 | trans = NULL; |
2ff7e61e | 5374 | btrfs_btree_balance_dirty(fs_info); |
8082510e | 5375 | } |
5f39d397 | 5376 | |
2ff7e61e | 5377 | btrfs_free_block_rsv(fs_info, rsv); |
4289a667 | 5378 | |
4ef31a45 JB |
5379 | /* |
5380 | * Errors here aren't a big deal, it just means we leave orphan items | |
5381 | * in the tree. They will be cleaned up on the next mount. | |
5382 | */ | |
8082510e | 5383 | if (ret == 0) { |
4289a667 | 5384 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5385 | btrfs_orphan_del(trans, inode); |
5386 | } else { | |
5387 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5388 | } |
54aa1f4d | 5389 | |
0b246afa JM |
5390 | trans->block_rsv = &fs_info->trans_block_rsv; |
5391 | if (!(root == fs_info->tree_root || | |
581bb050 | 5392 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
4a0cc7ca | 5393 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
581bb050 | 5394 | |
3a45bb20 | 5395 | btrfs_end_transaction(trans); |
2ff7e61e | 5396 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 5397 | no_delete: |
f48d1cf5 | 5398 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
dbd5768f | 5399 | clear_inode(inode); |
39279cc3 CM |
5400 | } |
5401 | ||
5402 | /* | |
5403 | * this returns the key found in the dir entry in the location pointer. | |
5404 | * If no dir entries were found, location->objectid is 0. | |
5405 | */ | |
5406 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5407 | struct btrfs_key *location) | |
5408 | { | |
5409 | const char *name = dentry->d_name.name; | |
5410 | int namelen = dentry->d_name.len; | |
5411 | struct btrfs_dir_item *di; | |
5412 | struct btrfs_path *path; | |
5413 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5414 | int ret = 0; |
39279cc3 CM |
5415 | |
5416 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5417 | if (!path) |
5418 | return -ENOMEM; | |
3954401f | 5419 | |
4a0cc7ca | 5420 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), name, |
39279cc3 | 5421 | namelen, 0); |
0d9f7f3e Y |
5422 | if (IS_ERR(di)) |
5423 | ret = PTR_ERR(di); | |
d397712b | 5424 | |
c704005d | 5425 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5426 | goto out_err; |
d397712b | 5427 | |
5f39d397 | 5428 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5429 | out: |
39279cc3 CM |
5430 | btrfs_free_path(path); |
5431 | return ret; | |
3954401f CM |
5432 | out_err: |
5433 | location->objectid = 0; | |
5434 | goto out; | |
39279cc3 CM |
5435 | } |
5436 | ||
5437 | /* | |
5438 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5439 | * needs to be changed to reflect the root directory of the tree root. This | |
5440 | * is kind of like crossing a mount point. | |
5441 | */ | |
2ff7e61e | 5442 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
4df27c4d YZ |
5443 | struct inode *dir, |
5444 | struct dentry *dentry, | |
5445 | struct btrfs_key *location, | |
5446 | struct btrfs_root **sub_root) | |
39279cc3 | 5447 | { |
4df27c4d YZ |
5448 | struct btrfs_path *path; |
5449 | struct btrfs_root *new_root; | |
5450 | struct btrfs_root_ref *ref; | |
5451 | struct extent_buffer *leaf; | |
1d4c08e0 | 5452 | struct btrfs_key key; |
4df27c4d YZ |
5453 | int ret; |
5454 | int err = 0; | |
39279cc3 | 5455 | |
4df27c4d YZ |
5456 | path = btrfs_alloc_path(); |
5457 | if (!path) { | |
5458 | err = -ENOMEM; | |
5459 | goto out; | |
5460 | } | |
39279cc3 | 5461 | |
4df27c4d | 5462 | err = -ENOENT; |
1d4c08e0 DS |
5463 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5464 | key.type = BTRFS_ROOT_REF_KEY; | |
5465 | key.offset = location->objectid; | |
5466 | ||
0b246afa | 5467 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
4df27c4d YZ |
5468 | if (ret) { |
5469 | if (ret < 0) | |
5470 | err = ret; | |
5471 | goto out; | |
5472 | } | |
39279cc3 | 5473 | |
4df27c4d YZ |
5474 | leaf = path->nodes[0]; |
5475 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
4a0cc7ca | 5476 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
4df27c4d YZ |
5477 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5478 | goto out; | |
39279cc3 | 5479 | |
4df27c4d YZ |
5480 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5481 | (unsigned long)(ref + 1), | |
5482 | dentry->d_name.len); | |
5483 | if (ret) | |
5484 | goto out; | |
5485 | ||
b3b4aa74 | 5486 | btrfs_release_path(path); |
4df27c4d | 5487 | |
0b246afa | 5488 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
4df27c4d YZ |
5489 | if (IS_ERR(new_root)) { |
5490 | err = PTR_ERR(new_root); | |
5491 | goto out; | |
5492 | } | |
5493 | ||
4df27c4d YZ |
5494 | *sub_root = new_root; |
5495 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5496 | location->type = BTRFS_INODE_ITEM_KEY; | |
5497 | location->offset = 0; | |
5498 | err = 0; | |
5499 | out: | |
5500 | btrfs_free_path(path); | |
5501 | return err; | |
39279cc3 CM |
5502 | } |
5503 | ||
5d4f98a2 YZ |
5504 | static void inode_tree_add(struct inode *inode) |
5505 | { | |
5506 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5507 | struct btrfs_inode *entry; | |
03e860bd NP |
5508 | struct rb_node **p; |
5509 | struct rb_node *parent; | |
cef21937 | 5510 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
4a0cc7ca | 5511 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 5512 | |
1d3382cb | 5513 | if (inode_unhashed(inode)) |
76dda93c | 5514 | return; |
e1409cef | 5515 | parent = NULL; |
5d4f98a2 | 5516 | spin_lock(&root->inode_lock); |
e1409cef | 5517 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5518 | while (*p) { |
5519 | parent = *p; | |
5520 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5521 | ||
4a0cc7ca | 5522 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5523 | p = &parent->rb_left; |
4a0cc7ca | 5524 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
03e860bd | 5525 | p = &parent->rb_right; |
5d4f98a2 YZ |
5526 | else { |
5527 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5528 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5529 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5530 | RB_CLEAR_NODE(parent); |
5531 | spin_unlock(&root->inode_lock); | |
cef21937 | 5532 | return; |
5d4f98a2 YZ |
5533 | } |
5534 | } | |
cef21937 FDBM |
5535 | rb_link_node(new, parent, p); |
5536 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5537 | spin_unlock(&root->inode_lock); |
5538 | } | |
5539 | ||
5540 | static void inode_tree_del(struct inode *inode) | |
5541 | { | |
0b246afa | 5542 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 5543 | struct btrfs_root *root = BTRFS_I(inode)->root; |
76dda93c | 5544 | int empty = 0; |
5d4f98a2 | 5545 | |
03e860bd | 5546 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5547 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5548 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5549 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5550 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5551 | } |
03e860bd | 5552 | spin_unlock(&root->inode_lock); |
76dda93c | 5553 | |
69e9c6c6 | 5554 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
0b246afa | 5555 | synchronize_srcu(&fs_info->subvol_srcu); |
76dda93c YZ |
5556 | spin_lock(&root->inode_lock); |
5557 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5558 | spin_unlock(&root->inode_lock); | |
5559 | if (empty) | |
5560 | btrfs_add_dead_root(root); | |
5561 | } | |
5562 | } | |
5563 | ||
143bede5 | 5564 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c | 5565 | { |
0b246afa | 5566 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c YZ |
5567 | struct rb_node *node; |
5568 | struct rb_node *prev; | |
5569 | struct btrfs_inode *entry; | |
5570 | struct inode *inode; | |
5571 | u64 objectid = 0; | |
5572 | ||
0b246afa | 5573 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
7813b3db | 5574 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
76dda93c YZ |
5575 | |
5576 | spin_lock(&root->inode_lock); | |
5577 | again: | |
5578 | node = root->inode_tree.rb_node; | |
5579 | prev = NULL; | |
5580 | while (node) { | |
5581 | prev = node; | |
5582 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5583 | ||
4a0cc7ca | 5584 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c | 5585 | node = node->rb_left; |
4a0cc7ca | 5586 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
76dda93c YZ |
5587 | node = node->rb_right; |
5588 | else | |
5589 | break; | |
5590 | } | |
5591 | if (!node) { | |
5592 | while (prev) { | |
5593 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5594 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
76dda93c YZ |
5595 | node = prev; |
5596 | break; | |
5597 | } | |
5598 | prev = rb_next(prev); | |
5599 | } | |
5600 | } | |
5601 | while (node) { | |
5602 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4a0cc7ca | 5603 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
76dda93c YZ |
5604 | inode = igrab(&entry->vfs_inode); |
5605 | if (inode) { | |
5606 | spin_unlock(&root->inode_lock); | |
5607 | if (atomic_read(&inode->i_count) > 1) | |
5608 | d_prune_aliases(inode); | |
5609 | /* | |
45321ac5 | 5610 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5611 | * the inode cache when its usage count |
5612 | * hits zero. | |
5613 | */ | |
5614 | iput(inode); | |
5615 | cond_resched(); | |
5616 | spin_lock(&root->inode_lock); | |
5617 | goto again; | |
5618 | } | |
5619 | ||
5620 | if (cond_resched_lock(&root->inode_lock)) | |
5621 | goto again; | |
5622 | ||
5623 | node = rb_next(node); | |
5624 | } | |
5625 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5626 | } |
5627 | ||
e02119d5 CM |
5628 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5629 | { | |
5630 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5631 | inode->i_ino = args->location->objectid; |
5632 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5633 | sizeof(*args->location)); | |
e02119d5 | 5634 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5635 | return 0; |
5636 | } | |
5637 | ||
5638 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5639 | { | |
5640 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5641 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5642 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5643 | } |
5644 | ||
5d4f98a2 | 5645 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5646 | struct btrfs_key *location, |
5d4f98a2 | 5647 | struct btrfs_root *root) |
39279cc3 CM |
5648 | { |
5649 | struct inode *inode; | |
5650 | struct btrfs_iget_args args; | |
90d3e592 | 5651 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5652 | |
90d3e592 | 5653 | args.location = location; |
39279cc3 CM |
5654 | args.root = root; |
5655 | ||
778ba82b | 5656 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5657 | btrfs_init_locked_inode, |
5658 | (void *)&args); | |
5659 | return inode; | |
5660 | } | |
5661 | ||
1a54ef8c BR |
5662 | /* Get an inode object given its location and corresponding root. |
5663 | * Returns in *is_new if the inode was read from disk | |
5664 | */ | |
5665 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5666 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5667 | { |
5668 | struct inode *inode; | |
5669 | ||
90d3e592 | 5670 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5671 | if (!inode) |
5d4f98a2 | 5672 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5673 | |
5674 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5675 | int ret; |
5676 | ||
5677 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5678 | if (!is_bad_inode(inode)) { |
5679 | inode_tree_add(inode); | |
5680 | unlock_new_inode(inode); | |
5681 | if (new) | |
5682 | *new = 1; | |
5683 | } else { | |
e0b6d65b ST |
5684 | unlock_new_inode(inode); |
5685 | iput(inode); | |
67710892 FM |
5686 | ASSERT(ret < 0); |
5687 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5688 | } |
5689 | } | |
5690 | ||
1a54ef8c BR |
5691 | return inode; |
5692 | } | |
5693 | ||
4df27c4d YZ |
5694 | static struct inode *new_simple_dir(struct super_block *s, |
5695 | struct btrfs_key *key, | |
5696 | struct btrfs_root *root) | |
5697 | { | |
5698 | struct inode *inode = new_inode(s); | |
5699 | ||
5700 | if (!inode) | |
5701 | return ERR_PTR(-ENOMEM); | |
5702 | ||
4df27c4d YZ |
5703 | BTRFS_I(inode)->root = root; |
5704 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5705 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5706 | |
5707 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5708 | inode->i_op = &btrfs_dir_ro_inode_operations; |
1fdf4194 | 5709 | inode->i_opflags &= ~IOP_XATTR; |
4df27c4d YZ |
5710 | inode->i_fop = &simple_dir_operations; |
5711 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
c2050a45 | 5712 | inode->i_mtime = current_time(inode); |
9cc97d64 | 5713 | inode->i_atime = inode->i_mtime; |
5714 | inode->i_ctime = inode->i_mtime; | |
5715 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5716 | |
5717 | return inode; | |
5718 | } | |
5719 | ||
3de4586c | 5720 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5721 | { |
0b246afa | 5722 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
d397712b | 5723 | struct inode *inode; |
4df27c4d | 5724 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5725 | struct btrfs_root *sub_root = root; |
5726 | struct btrfs_key location; | |
76dda93c | 5727 | int index; |
b4aff1f8 | 5728 | int ret = 0; |
39279cc3 CM |
5729 | |
5730 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5731 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5732 | |
39e3c955 | 5733 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5734 | if (ret < 0) |
5735 | return ERR_PTR(ret); | |
5f39d397 | 5736 | |
4df27c4d | 5737 | if (location.objectid == 0) |
5662344b | 5738 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5739 | |
5740 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5741 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5742 | return inode; |
5743 | } | |
5744 | ||
5745 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5746 | ||
0b246afa | 5747 | index = srcu_read_lock(&fs_info->subvol_srcu); |
2ff7e61e | 5748 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
4df27c4d YZ |
5749 | &location, &sub_root); |
5750 | if (ret < 0) { | |
5751 | if (ret != -ENOENT) | |
5752 | inode = ERR_PTR(ret); | |
5753 | else | |
5754 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5755 | } else { | |
73f73415 | 5756 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5757 | } |
0b246afa | 5758 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
76dda93c | 5759 | |
34d19bad | 5760 | if (!IS_ERR(inode) && root != sub_root) { |
0b246afa | 5761 | down_read(&fs_info->cleanup_work_sem); |
c71bf099 | 5762 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
66b4ffd1 | 5763 | ret = btrfs_orphan_cleanup(sub_root); |
0b246afa | 5764 | up_read(&fs_info->cleanup_work_sem); |
01cd3367 JB |
5765 | if (ret) { |
5766 | iput(inode); | |
66b4ffd1 | 5767 | inode = ERR_PTR(ret); |
01cd3367 | 5768 | } |
c71bf099 YZ |
5769 | } |
5770 | ||
3de4586c CM |
5771 | return inode; |
5772 | } | |
5773 | ||
fe15ce44 | 5774 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5775 | { |
5776 | struct btrfs_root *root; | |
2b0143b5 | 5777 | struct inode *inode = d_inode(dentry); |
76dda93c | 5778 | |
848cce0d | 5779 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5780 | inode = d_inode(dentry->d_parent); |
76dda93c | 5781 | |
848cce0d LZ |
5782 | if (inode) { |
5783 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5784 | if (btrfs_root_refs(&root->root_item) == 0) |
5785 | return 1; | |
848cce0d | 5786 | |
4a0cc7ca | 5787 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
848cce0d | 5788 | return 1; |
efefb143 | 5789 | } |
76dda93c YZ |
5790 | return 0; |
5791 | } | |
5792 | ||
b4aff1f8 JB |
5793 | static void btrfs_dentry_release(struct dentry *dentry) |
5794 | { | |
944a4515 | 5795 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5796 | } |
5797 | ||
3de4586c | 5798 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5799 | unsigned int flags) |
3de4586c | 5800 | { |
5662344b | 5801 | struct inode *inode; |
a66e7cc6 | 5802 | |
5662344b TI |
5803 | inode = btrfs_lookup_dentry(dir, dentry); |
5804 | if (IS_ERR(inode)) { | |
5805 | if (PTR_ERR(inode) == -ENOENT) | |
5806 | inode = NULL; | |
5807 | else | |
5808 | return ERR_CAST(inode); | |
5809 | } | |
5810 | ||
41d28bca | 5811 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5812 | } |
5813 | ||
16cdcec7 | 5814 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5815 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5816 | }; | |
5817 | ||
9cdda8d3 | 5818 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5819 | { |
9cdda8d3 | 5820 | struct inode *inode = file_inode(file); |
2ff7e61e | 5821 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5822 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5823 | struct btrfs_item *item; | |
5824 | struct btrfs_dir_item *di; | |
5825 | struct btrfs_key key; | |
5f39d397 | 5826 | struct btrfs_key found_key; |
39279cc3 | 5827 | struct btrfs_path *path; |
16cdcec7 MX |
5828 | struct list_head ins_list; |
5829 | struct list_head del_list; | |
39279cc3 | 5830 | int ret; |
5f39d397 | 5831 | struct extent_buffer *leaf; |
39279cc3 | 5832 | int slot; |
39279cc3 CM |
5833 | unsigned char d_type; |
5834 | int over = 0; | |
5f39d397 CM |
5835 | char tmp_name[32]; |
5836 | char *name_ptr; | |
5837 | int name_len; | |
02dbfc99 | 5838 | bool put = false; |
c2951f32 | 5839 | struct btrfs_key location; |
5f39d397 | 5840 | |
9cdda8d3 AV |
5841 | if (!dir_emit_dots(file, ctx)) |
5842 | return 0; | |
5843 | ||
49593bfa | 5844 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5845 | if (!path) |
5846 | return -ENOMEM; | |
ff5714cc | 5847 | |
e4058b54 | 5848 | path->reada = READA_FORWARD; |
49593bfa | 5849 | |
c2951f32 JM |
5850 | INIT_LIST_HEAD(&ins_list); |
5851 | INIT_LIST_HEAD(&del_list); | |
5852 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); | |
16cdcec7 | 5853 | |
c2951f32 | 5854 | key.type = BTRFS_DIR_INDEX_KEY; |
9cdda8d3 | 5855 | key.offset = ctx->pos; |
4a0cc7ca | 5856 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
5f39d397 | 5857 | |
39279cc3 CM |
5858 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5859 | if (ret < 0) | |
5860 | goto err; | |
49593bfa DW |
5861 | |
5862 | while (1) { | |
5f39d397 | 5863 | leaf = path->nodes[0]; |
39279cc3 | 5864 | slot = path->slots[0]; |
b9e03af0 LZ |
5865 | if (slot >= btrfs_header_nritems(leaf)) { |
5866 | ret = btrfs_next_leaf(root, path); | |
5867 | if (ret < 0) | |
5868 | goto err; | |
5869 | else if (ret > 0) | |
5870 | break; | |
5871 | continue; | |
39279cc3 | 5872 | } |
3de4586c | 5873 | |
dd3cc16b | 5874 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5875 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5876 | ||
5877 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5878 | break; |
c2951f32 | 5879 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
39279cc3 | 5880 | break; |
9cdda8d3 | 5881 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5882 | goto next; |
c2951f32 | 5883 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
16cdcec7 | 5884 | goto next; |
5f39d397 | 5885 | |
9cdda8d3 | 5886 | ctx->pos = found_key.offset; |
49593bfa | 5887 | |
39279cc3 | 5888 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
2ff7e61e | 5889 | if (verify_dir_item(fs_info, leaf, di)) |
c2951f32 | 5890 | goto next; |
22a94d44 | 5891 | |
c2951f32 JM |
5892 | name_len = btrfs_dir_name_len(leaf, di); |
5893 | if (name_len <= sizeof(tmp_name)) { | |
5894 | name_ptr = tmp_name; | |
5895 | } else { | |
5896 | name_ptr = kmalloc(name_len, GFP_KERNEL); | |
5897 | if (!name_ptr) { | |
5898 | ret = -ENOMEM; | |
5899 | goto err; | |
5f39d397 | 5900 | } |
c2951f32 JM |
5901 | } |
5902 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), | |
5903 | name_len); | |
3de4586c | 5904 | |
c2951f32 JM |
5905 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
5906 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
fede766f | 5907 | |
c2951f32 JM |
5908 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
5909 | d_type); | |
5f39d397 | 5910 | |
c2951f32 JM |
5911 | if (name_ptr != tmp_name) |
5912 | kfree(name_ptr); | |
5f39d397 | 5913 | |
c2951f32 JM |
5914 | if (over) |
5915 | goto nopos; | |
d2fbb2b5 | 5916 | ctx->pos++; |
b9e03af0 LZ |
5917 | next: |
5918 | path->slots[0]++; | |
39279cc3 | 5919 | } |
49593bfa | 5920 | |
d2fbb2b5 | 5921 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
c2951f32 | 5922 | if (ret) |
bc4ef759 DS |
5923 | goto nopos; |
5924 | ||
db62efbb ZB |
5925 | /* |
5926 | * Stop new entries from being returned after we return the last | |
5927 | * entry. | |
5928 | * | |
5929 | * New directory entries are assigned a strictly increasing | |
5930 | * offset. This means that new entries created during readdir | |
5931 | * are *guaranteed* to be seen in the future by that readdir. | |
5932 | * This has broken buggy programs which operate on names as | |
5933 | * they're returned by readdir. Until we re-use freed offsets | |
5934 | * we have this hack to stop new entries from being returned | |
5935 | * under the assumption that they'll never reach this huge | |
5936 | * offset. | |
5937 | * | |
5938 | * This is being careful not to overflow 32bit loff_t unless the | |
5939 | * last entry requires it because doing so has broken 32bit apps | |
5940 | * in the past. | |
5941 | */ | |
c2951f32 JM |
5942 | if (ctx->pos >= INT_MAX) |
5943 | ctx->pos = LLONG_MAX; | |
5944 | else | |
5945 | ctx->pos = INT_MAX; | |
39279cc3 CM |
5946 | nopos: |
5947 | ret = 0; | |
5948 | err: | |
02dbfc99 OS |
5949 | if (put) |
5950 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5951 | btrfs_free_path(path); |
39279cc3 CM |
5952 | return ret; |
5953 | } | |
5954 | ||
a9185b41 | 5955 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5956 | { |
5957 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5958 | struct btrfs_trans_handle *trans; | |
5959 | int ret = 0; | |
0af3d00b | 5960 | bool nolock = false; |
39279cc3 | 5961 | |
72ac3c0d | 5962 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5963 | return 0; |
5964 | ||
83eea1f1 | 5965 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5966 | nolock = true; |
0af3d00b | 5967 | |
a9185b41 | 5968 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5969 | if (nolock) |
7a7eaa40 | 5970 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5971 | else |
7a7eaa40 | 5972 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5973 | if (IS_ERR(trans)) |
5974 | return PTR_ERR(trans); | |
3a45bb20 | 5975 | ret = btrfs_commit_transaction(trans); |
39279cc3 CM |
5976 | } |
5977 | return ret; | |
5978 | } | |
5979 | ||
5980 | /* | |
54aa1f4d | 5981 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5982 | * inode changes. But, it is most likely to find the inode in cache. |
5983 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5984 | * to keep or drop this code. | |
5985 | */ | |
48a3b636 | 5986 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 | 5987 | { |
2ff7e61e | 5988 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 CM |
5989 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5990 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5991 | int ret; |
5992 | ||
72ac3c0d | 5993 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5994 | return 0; |
39279cc3 | 5995 | |
7a7eaa40 | 5996 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5997 | if (IS_ERR(trans)) |
5998 | return PTR_ERR(trans); | |
8929ecfa YZ |
5999 | |
6000 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6001 | if (ret && ret == -ENOSPC) { |
6002 | /* whoops, lets try again with the full transaction */ | |
3a45bb20 | 6003 | btrfs_end_transaction(trans); |
94b60442 | 6004 | trans = btrfs_start_transaction(root, 1); |
22c44fe6 JB |
6005 | if (IS_ERR(trans)) |
6006 | return PTR_ERR(trans); | |
8929ecfa | 6007 | |
94b60442 | 6008 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6009 | } |
3a45bb20 | 6010 | btrfs_end_transaction(trans); |
16cdcec7 | 6011 | if (BTRFS_I(inode)->delayed_node) |
2ff7e61e | 6012 | btrfs_balance_delayed_items(fs_info); |
22c44fe6 JB |
6013 | |
6014 | return ret; | |
6015 | } | |
6016 | ||
6017 | /* | |
6018 | * This is a copy of file_update_time. We need this so we can return error on | |
6019 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6020 | */ | |
e41f941a JB |
6021 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6022 | int flags) | |
22c44fe6 | 6023 | { |
2bc55652 AB |
6024 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6025 | ||
6026 | if (btrfs_root_readonly(root)) | |
6027 | return -EROFS; | |
6028 | ||
e41f941a | 6029 | if (flags & S_VERSION) |
22c44fe6 | 6030 | inode_inc_iversion(inode); |
e41f941a JB |
6031 | if (flags & S_CTIME) |
6032 | inode->i_ctime = *now; | |
6033 | if (flags & S_MTIME) | |
6034 | inode->i_mtime = *now; | |
6035 | if (flags & S_ATIME) | |
6036 | inode->i_atime = *now; | |
6037 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6038 | } |
6039 | ||
d352ac68 CM |
6040 | /* |
6041 | * find the highest existing sequence number in a directory | |
6042 | * and then set the in-memory index_cnt variable to reflect | |
6043 | * free sequence numbers | |
6044 | */ | |
aec7477b JB |
6045 | static int btrfs_set_inode_index_count(struct inode *inode) |
6046 | { | |
6047 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6048 | struct btrfs_key key, found_key; | |
6049 | struct btrfs_path *path; | |
6050 | struct extent_buffer *leaf; | |
6051 | int ret; | |
6052 | ||
4a0cc7ca | 6053 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
962a298f | 6054 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6055 | key.offset = (u64)-1; |
6056 | ||
6057 | path = btrfs_alloc_path(); | |
6058 | if (!path) | |
6059 | return -ENOMEM; | |
6060 | ||
6061 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6062 | if (ret < 0) | |
6063 | goto out; | |
6064 | /* FIXME: we should be able to handle this */ | |
6065 | if (ret == 0) | |
6066 | goto out; | |
6067 | ret = 0; | |
6068 | ||
6069 | /* | |
6070 | * MAGIC NUMBER EXPLANATION: | |
6071 | * since we search a directory based on f_pos we have to start at 2 | |
6072 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6073 | * else has to start at 2 | |
6074 | */ | |
6075 | if (path->slots[0] == 0) { | |
6076 | BTRFS_I(inode)->index_cnt = 2; | |
6077 | goto out; | |
6078 | } | |
6079 | ||
6080 | path->slots[0]--; | |
6081 | ||
6082 | leaf = path->nodes[0]; | |
6083 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6084 | ||
4a0cc7ca | 6085 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
962a298f | 6086 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
6087 | BTRFS_I(inode)->index_cnt = 2; |
6088 | goto out; | |
6089 | } | |
6090 | ||
6091 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
6092 | out: | |
6093 | btrfs_free_path(path); | |
6094 | return ret; | |
6095 | } | |
6096 | ||
d352ac68 CM |
6097 | /* |
6098 | * helper to find a free sequence number in a given directory. This current | |
6099 | * code is very simple, later versions will do smarter things in the btree | |
6100 | */ | |
3de4586c | 6101 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
6102 | { |
6103 | int ret = 0; | |
6104 | ||
6105 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
f5cc7b80 | 6106 | ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir)); |
16cdcec7 MX |
6107 | if (ret) { |
6108 | ret = btrfs_set_inode_index_count(dir); | |
6109 | if (ret) | |
6110 | return ret; | |
6111 | } | |
aec7477b JB |
6112 | } |
6113 | ||
00e4e6b3 | 6114 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
6115 | BTRFS_I(dir)->index_cnt++; |
6116 | ||
6117 | return ret; | |
6118 | } | |
6119 | ||
b0d5d10f CM |
6120 | static int btrfs_insert_inode_locked(struct inode *inode) |
6121 | { | |
6122 | struct btrfs_iget_args args; | |
6123 | args.location = &BTRFS_I(inode)->location; | |
6124 | args.root = BTRFS_I(inode)->root; | |
6125 | ||
6126 | return insert_inode_locked4(inode, | |
6127 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6128 | btrfs_find_actor, &args); | |
6129 | } | |
6130 | ||
39279cc3 CM |
6131 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6132 | struct btrfs_root *root, | |
aec7477b | 6133 | struct inode *dir, |
9c58309d | 6134 | const char *name, int name_len, |
175a4eb7 AV |
6135 | u64 ref_objectid, u64 objectid, |
6136 | umode_t mode, u64 *index) | |
39279cc3 | 6137 | { |
0b246afa | 6138 | struct btrfs_fs_info *fs_info = root->fs_info; |
39279cc3 | 6139 | struct inode *inode; |
5f39d397 | 6140 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6141 | struct btrfs_key *location; |
5f39d397 | 6142 | struct btrfs_path *path; |
9c58309d CM |
6143 | struct btrfs_inode_ref *ref; |
6144 | struct btrfs_key key[2]; | |
6145 | u32 sizes[2]; | |
ef3b9af5 | 6146 | int nitems = name ? 2 : 1; |
9c58309d | 6147 | unsigned long ptr; |
39279cc3 | 6148 | int ret; |
39279cc3 | 6149 | |
5f39d397 | 6150 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6151 | if (!path) |
6152 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6153 | |
0b246afa | 6154 | inode = new_inode(fs_info->sb); |
8fb27640 YS |
6155 | if (!inode) { |
6156 | btrfs_free_path(path); | |
39279cc3 | 6157 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6158 | } |
39279cc3 | 6159 | |
5762b5c9 FM |
6160 | /* |
6161 | * O_TMPFILE, set link count to 0, so that after this point, | |
6162 | * we fill in an inode item with the correct link count. | |
6163 | */ | |
6164 | if (!name) | |
6165 | set_nlink(inode, 0); | |
6166 | ||
581bb050 LZ |
6167 | /* |
6168 | * we have to initialize this early, so we can reclaim the inode | |
6169 | * number if we fail afterwards in this function. | |
6170 | */ | |
6171 | inode->i_ino = objectid; | |
6172 | ||
ef3b9af5 | 6173 | if (dir && name) { |
1abe9b8a | 6174 | trace_btrfs_inode_request(dir); |
6175 | ||
3de4586c | 6176 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6177 | if (ret) { |
8fb27640 | 6178 | btrfs_free_path(path); |
09771430 | 6179 | iput(inode); |
aec7477b | 6180 | return ERR_PTR(ret); |
09771430 | 6181 | } |
ef3b9af5 FM |
6182 | } else if (dir) { |
6183 | *index = 0; | |
aec7477b JB |
6184 | } |
6185 | /* | |
6186 | * index_cnt is ignored for everything but a dir, | |
6187 | * btrfs_get_inode_index_count has an explanation for the magic | |
6188 | * number | |
6189 | */ | |
6190 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6191 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6192 | BTRFS_I(inode)->root = root; |
e02119d5 | 6193 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6194 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6195 | |
5dc562c5 JB |
6196 | /* |
6197 | * We could have gotten an inode number from somebody who was fsynced | |
6198 | * and then removed in this same transaction, so let's just set full | |
6199 | * sync since it will be a full sync anyway and this will blow away the | |
6200 | * old info in the log. | |
6201 | */ | |
6202 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6203 | ||
9c58309d | 6204 | key[0].objectid = objectid; |
962a298f | 6205 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6206 | key[0].offset = 0; |
6207 | ||
9c58309d | 6208 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6209 | |
6210 | if (name) { | |
6211 | /* | |
6212 | * Start new inodes with an inode_ref. This is slightly more | |
6213 | * efficient for small numbers of hard links since they will | |
6214 | * be packed into one item. Extended refs will kick in if we | |
6215 | * add more hard links than can fit in the ref item. | |
6216 | */ | |
6217 | key[1].objectid = objectid; | |
962a298f | 6218 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6219 | key[1].offset = ref_objectid; |
6220 | ||
6221 | sizes[1] = name_len + sizeof(*ref); | |
6222 | } | |
9c58309d | 6223 | |
b0d5d10f CM |
6224 | location = &BTRFS_I(inode)->location; |
6225 | location->objectid = objectid; | |
6226 | location->offset = 0; | |
962a298f | 6227 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6228 | |
6229 | ret = btrfs_insert_inode_locked(inode); | |
6230 | if (ret < 0) | |
6231 | goto fail; | |
6232 | ||
b9473439 | 6233 | path->leave_spinning = 1; |
ef3b9af5 | 6234 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6235 | if (ret != 0) |
b0d5d10f | 6236 | goto fail_unlock; |
5f39d397 | 6237 | |
ecc11fab | 6238 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6239 | inode_set_bytes(inode, 0); |
9cc97d64 | 6240 | |
c2050a45 | 6241 | inode->i_mtime = current_time(inode); |
9cc97d64 | 6242 | inode->i_atime = inode->i_mtime; |
6243 | inode->i_ctime = inode->i_mtime; | |
6244 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6245 | ||
5f39d397 CM |
6246 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6247 | struct btrfs_inode_item); | |
b159fa28 | 6248 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
293f7e07 | 6249 | sizeof(*inode_item)); |
e02119d5 | 6250 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6251 | |
ef3b9af5 FM |
6252 | if (name) { |
6253 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6254 | struct btrfs_inode_ref); | |
6255 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6256 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6257 | ptr = (unsigned long)(ref + 1); | |
6258 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6259 | } | |
9c58309d | 6260 | |
5f39d397 CM |
6261 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6262 | btrfs_free_path(path); | |
6263 | ||
6cbff00f CH |
6264 | btrfs_inherit_iflags(inode, dir); |
6265 | ||
569254b0 | 6266 | if (S_ISREG(mode)) { |
0b246afa | 6267 | if (btrfs_test_opt(fs_info, NODATASUM)) |
94272164 | 6268 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
0b246afa | 6269 | if (btrfs_test_opt(fs_info, NODATACOW)) |
f2bdf9a8 JB |
6270 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6271 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6272 | } |
6273 | ||
5d4f98a2 | 6274 | inode_tree_add(inode); |
1abe9b8a | 6275 | |
6276 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6277 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6278 | |
8ea05e3a AB |
6279 | btrfs_update_root_times(trans, root); |
6280 | ||
63541927 FDBM |
6281 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6282 | if (ret) | |
0b246afa | 6283 | btrfs_err(fs_info, |
63541927 | 6284 | "error inheriting props for ino %llu (root %llu): %d", |
4a0cc7ca | 6285 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
63541927 | 6286 | |
39279cc3 | 6287 | return inode; |
b0d5d10f CM |
6288 | |
6289 | fail_unlock: | |
6290 | unlock_new_inode(inode); | |
5f39d397 | 6291 | fail: |
ef3b9af5 | 6292 | if (dir && name) |
aec7477b | 6293 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6294 | btrfs_free_path(path); |
09771430 | 6295 | iput(inode); |
5f39d397 | 6296 | return ERR_PTR(ret); |
39279cc3 CM |
6297 | } |
6298 | ||
6299 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6300 | { | |
6301 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6302 | } | |
6303 | ||
d352ac68 CM |
6304 | /* |
6305 | * utility function to add 'inode' into 'parent_inode' with | |
6306 | * a give name and a given sequence number. | |
6307 | * if 'add_backref' is true, also insert a backref from the | |
6308 | * inode to the parent directory. | |
6309 | */ | |
e02119d5 CM |
6310 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6311 | struct inode *parent_inode, struct inode *inode, | |
6312 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6313 | { |
0b246afa | 6314 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4df27c4d | 6315 | int ret = 0; |
39279cc3 | 6316 | struct btrfs_key key; |
e02119d5 | 6317 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
4a0cc7ca NB |
6318 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
6319 | u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode)); | |
5f39d397 | 6320 | |
33345d01 | 6321 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6322 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6323 | } else { | |
33345d01 | 6324 | key.objectid = ino; |
962a298f | 6325 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6326 | key.offset = 0; |
6327 | } | |
6328 | ||
33345d01 | 6329 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
0b246afa JM |
6330 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
6331 | root->root_key.objectid, parent_ino, | |
6332 | index, name, name_len); | |
4df27c4d | 6333 | } else if (add_backref) { |
33345d01 LZ |
6334 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6335 | parent_ino, index); | |
4df27c4d | 6336 | } |
39279cc3 | 6337 | |
79787eaa JM |
6338 | /* Nothing to clean up yet */ |
6339 | if (ret) | |
6340 | return ret; | |
4df27c4d | 6341 | |
79787eaa JM |
6342 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6343 | parent_inode, &key, | |
6344 | btrfs_inode_type(inode), index); | |
9c52057c | 6345 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6346 | goto fail_dir_item; |
6347 | else if (ret) { | |
66642832 | 6348 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6349 | return ret; |
39279cc3 | 6350 | } |
79787eaa JM |
6351 | |
6352 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6353 | name_len * 2); | |
0c4d2d95 | 6354 | inode_inc_iversion(parent_inode); |
04b285f3 | 6355 | parent_inode->i_mtime = parent_inode->i_ctime = |
c2050a45 | 6356 | current_time(parent_inode); |
79787eaa JM |
6357 | ret = btrfs_update_inode(trans, root, parent_inode); |
6358 | if (ret) | |
66642832 | 6359 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6360 | return ret; |
fe66a05a CM |
6361 | |
6362 | fail_dir_item: | |
6363 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6364 | u64 local_index; | |
6365 | int err; | |
0b246afa JM |
6366 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
6367 | root->root_key.objectid, parent_ino, | |
6368 | &local_index, name, name_len); | |
fe66a05a CM |
6369 | |
6370 | } else if (add_backref) { | |
6371 | u64 local_index; | |
6372 | int err; | |
6373 | ||
6374 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6375 | ino, parent_ino, &local_index); | |
6376 | } | |
6377 | return ret; | |
39279cc3 CM |
6378 | } |
6379 | ||
6380 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6381 | struct inode *dir, struct dentry *dentry, |
6382 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6383 | { |
a1b075d2 JB |
6384 | int err = btrfs_add_link(trans, dir, inode, |
6385 | dentry->d_name.name, dentry->d_name.len, | |
6386 | backref, index); | |
39279cc3 CM |
6387 | if (err > 0) |
6388 | err = -EEXIST; | |
6389 | return err; | |
6390 | } | |
6391 | ||
618e21d5 | 6392 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6393 | umode_t mode, dev_t rdev) |
618e21d5 | 6394 | { |
2ff7e61e | 6395 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
618e21d5 JB |
6396 | struct btrfs_trans_handle *trans; |
6397 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6398 | struct inode *inode = NULL; |
618e21d5 JB |
6399 | int err; |
6400 | int drop_inode = 0; | |
6401 | u64 objectid; | |
00e4e6b3 | 6402 | u64 index = 0; |
618e21d5 | 6403 | |
9ed74f2d JB |
6404 | /* |
6405 | * 2 for inode item and ref | |
6406 | * 2 for dir items | |
6407 | * 1 for xattr if selinux is on | |
6408 | */ | |
a22285a6 YZ |
6409 | trans = btrfs_start_transaction(root, 5); |
6410 | if (IS_ERR(trans)) | |
6411 | return PTR_ERR(trans); | |
1832a6d5 | 6412 | |
581bb050 LZ |
6413 | err = btrfs_find_free_ino(root, &objectid); |
6414 | if (err) | |
6415 | goto out_unlock; | |
6416 | ||
aec7477b | 6417 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
4a0cc7ca | 6418 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
d82a6f1d | 6419 | mode, &index); |
7cf96da3 TI |
6420 | if (IS_ERR(inode)) { |
6421 | err = PTR_ERR(inode); | |
618e21d5 | 6422 | goto out_unlock; |
7cf96da3 | 6423 | } |
618e21d5 | 6424 | |
ad19db71 CS |
6425 | /* |
6426 | * If the active LSM wants to access the inode during | |
6427 | * d_instantiate it needs these. Smack checks to see | |
6428 | * if the filesystem supports xattrs by looking at the | |
6429 | * ops vector. | |
6430 | */ | |
ad19db71 | 6431 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6432 | init_special_inode(inode, inode->i_mode, rdev); |
6433 | ||
6434 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6435 | if (err) |
b0d5d10f CM |
6436 | goto out_unlock_inode; |
6437 | ||
6438 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6439 | if (err) { | |
6440 | goto out_unlock_inode; | |
6441 | } else { | |
1b4ab1bb | 6442 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6443 | unlock_new_inode(inode); |
08c422c2 | 6444 | d_instantiate(dentry, inode); |
618e21d5 | 6445 | } |
b0d5d10f | 6446 | |
618e21d5 | 6447 | out_unlock: |
3a45bb20 | 6448 | btrfs_end_transaction(trans); |
2ff7e61e JM |
6449 | btrfs_balance_delayed_items(fs_info); |
6450 | btrfs_btree_balance_dirty(fs_info); | |
618e21d5 JB |
6451 | if (drop_inode) { |
6452 | inode_dec_link_count(inode); | |
6453 | iput(inode); | |
6454 | } | |
618e21d5 | 6455 | return err; |
b0d5d10f CM |
6456 | |
6457 | out_unlock_inode: | |
6458 | drop_inode = 1; | |
6459 | unlock_new_inode(inode); | |
6460 | goto out_unlock; | |
6461 | ||
618e21d5 JB |
6462 | } |
6463 | ||
39279cc3 | 6464 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6465 | umode_t mode, bool excl) |
39279cc3 | 6466 | { |
2ff7e61e | 6467 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
6468 | struct btrfs_trans_handle *trans; |
6469 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6470 | struct inode *inode = NULL; |
43baa579 | 6471 | int drop_inode_on_err = 0; |
a22285a6 | 6472 | int err; |
39279cc3 | 6473 | u64 objectid; |
00e4e6b3 | 6474 | u64 index = 0; |
39279cc3 | 6475 | |
9ed74f2d JB |
6476 | /* |
6477 | * 2 for inode item and ref | |
6478 | * 2 for dir items | |
6479 | * 1 for xattr if selinux is on | |
6480 | */ | |
a22285a6 YZ |
6481 | trans = btrfs_start_transaction(root, 5); |
6482 | if (IS_ERR(trans)) | |
6483 | return PTR_ERR(trans); | |
9ed74f2d | 6484 | |
581bb050 LZ |
6485 | err = btrfs_find_free_ino(root, &objectid); |
6486 | if (err) | |
6487 | goto out_unlock; | |
6488 | ||
aec7477b | 6489 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
4a0cc7ca | 6490 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
d82a6f1d | 6491 | mode, &index); |
7cf96da3 TI |
6492 | if (IS_ERR(inode)) { |
6493 | err = PTR_ERR(inode); | |
39279cc3 | 6494 | goto out_unlock; |
7cf96da3 | 6495 | } |
43baa579 | 6496 | drop_inode_on_err = 1; |
ad19db71 CS |
6497 | /* |
6498 | * If the active LSM wants to access the inode during | |
6499 | * d_instantiate it needs these. Smack checks to see | |
6500 | * if the filesystem supports xattrs by looking at the | |
6501 | * ops vector. | |
6502 | */ | |
6503 | inode->i_fop = &btrfs_file_operations; | |
6504 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6505 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6506 | |
6507 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6508 | if (err) | |
6509 | goto out_unlock_inode; | |
6510 | ||
6511 | err = btrfs_update_inode(trans, root, inode); | |
6512 | if (err) | |
6513 | goto out_unlock_inode; | |
ad19db71 | 6514 | |
a1b075d2 | 6515 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6516 | if (err) |
b0d5d10f | 6517 | goto out_unlock_inode; |
43baa579 | 6518 | |
43baa579 | 6519 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6520 | unlock_new_inode(inode); |
43baa579 FB |
6521 | d_instantiate(dentry, inode); |
6522 | ||
39279cc3 | 6523 | out_unlock: |
3a45bb20 | 6524 | btrfs_end_transaction(trans); |
43baa579 | 6525 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6526 | inode_dec_link_count(inode); |
6527 | iput(inode); | |
6528 | } | |
2ff7e61e JM |
6529 | btrfs_balance_delayed_items(fs_info); |
6530 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6531 | return err; |
b0d5d10f CM |
6532 | |
6533 | out_unlock_inode: | |
6534 | unlock_new_inode(inode); | |
6535 | goto out_unlock; | |
6536 | ||
39279cc3 CM |
6537 | } |
6538 | ||
6539 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6540 | struct dentry *dentry) | |
6541 | { | |
271dba45 | 6542 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6543 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6544 | struct inode *inode = d_inode(old_dentry); |
2ff7e61e | 6545 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00e4e6b3 | 6546 | u64 index; |
39279cc3 CM |
6547 | int err; |
6548 | int drop_inode = 0; | |
6549 | ||
4a8be425 TH |
6550 | /* do not allow sys_link's with other subvols of the same device */ |
6551 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6552 | return -EXDEV; |
4a8be425 | 6553 | |
f186373f | 6554 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6555 | return -EMLINK; |
4a8be425 | 6556 | |
3de4586c | 6557 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6558 | if (err) |
6559 | goto fail; | |
6560 | ||
a22285a6 | 6561 | /* |
7e6b6465 | 6562 | * 2 items for inode and inode ref |
a22285a6 | 6563 | * 2 items for dir items |
7e6b6465 | 6564 | * 1 item for parent inode |
a22285a6 | 6565 | */ |
7e6b6465 | 6566 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6567 | if (IS_ERR(trans)) { |
6568 | err = PTR_ERR(trans); | |
271dba45 | 6569 | trans = NULL; |
a22285a6 YZ |
6570 | goto fail; |
6571 | } | |
5f39d397 | 6572 | |
67de1176 MX |
6573 | /* There are several dir indexes for this inode, clear the cache. */ |
6574 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6575 | inc_nlink(inode); |
0c4d2d95 | 6576 | inode_inc_iversion(inode); |
c2050a45 | 6577 | inode->i_ctime = current_time(inode); |
7de9c6ee | 6578 | ihold(inode); |
e9976151 | 6579 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6580 | |
a1b075d2 | 6581 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6582 | |
a5719521 | 6583 | if (err) { |
54aa1f4d | 6584 | drop_inode = 1; |
a5719521 | 6585 | } else { |
10d9f309 | 6586 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6587 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6588 | if (err) |
6589 | goto fail; | |
ef3b9af5 FM |
6590 | if (inode->i_nlink == 1) { |
6591 | /* | |
6592 | * If new hard link count is 1, it's a file created | |
6593 | * with open(2) O_TMPFILE flag. | |
6594 | */ | |
6595 | err = btrfs_orphan_del(trans, inode); | |
6596 | if (err) | |
6597 | goto fail; | |
6598 | } | |
08c422c2 | 6599 | d_instantiate(dentry, inode); |
9ca5fbfb | 6600 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
a5719521 | 6601 | } |
39279cc3 | 6602 | |
2ff7e61e | 6603 | btrfs_balance_delayed_items(fs_info); |
1832a6d5 | 6604 | fail: |
271dba45 | 6605 | if (trans) |
3a45bb20 | 6606 | btrfs_end_transaction(trans); |
39279cc3 CM |
6607 | if (drop_inode) { |
6608 | inode_dec_link_count(inode); | |
6609 | iput(inode); | |
6610 | } | |
2ff7e61e | 6611 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 CM |
6612 | return err; |
6613 | } | |
6614 | ||
18bb1db3 | 6615 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6616 | { |
2ff7e61e | 6617 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
b9d86667 | 6618 | struct inode *inode = NULL; |
39279cc3 CM |
6619 | struct btrfs_trans_handle *trans; |
6620 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6621 | int err = 0; | |
6622 | int drop_on_err = 0; | |
b9d86667 | 6623 | u64 objectid = 0; |
00e4e6b3 | 6624 | u64 index = 0; |
39279cc3 | 6625 | |
9ed74f2d JB |
6626 | /* |
6627 | * 2 items for inode and ref | |
6628 | * 2 items for dir items | |
6629 | * 1 for xattr if selinux is on | |
6630 | */ | |
a22285a6 YZ |
6631 | trans = btrfs_start_transaction(root, 5); |
6632 | if (IS_ERR(trans)) | |
6633 | return PTR_ERR(trans); | |
39279cc3 | 6634 | |
581bb050 LZ |
6635 | err = btrfs_find_free_ino(root, &objectid); |
6636 | if (err) | |
6637 | goto out_fail; | |
6638 | ||
aec7477b | 6639 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
4a0cc7ca | 6640 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
d82a6f1d | 6641 | S_IFDIR | mode, &index); |
39279cc3 CM |
6642 | if (IS_ERR(inode)) { |
6643 | err = PTR_ERR(inode); | |
6644 | goto out_fail; | |
6645 | } | |
5f39d397 | 6646 | |
39279cc3 | 6647 | drop_on_err = 1; |
b0d5d10f CM |
6648 | /* these must be set before we unlock the inode */ |
6649 | inode->i_op = &btrfs_dir_inode_operations; | |
6650 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6651 | |
2a7dba39 | 6652 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6653 | if (err) |
b0d5d10f | 6654 | goto out_fail_inode; |
39279cc3 | 6655 | |
dbe674a9 | 6656 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6657 | err = btrfs_update_inode(trans, root, inode); |
6658 | if (err) | |
b0d5d10f | 6659 | goto out_fail_inode; |
5f39d397 | 6660 | |
a1b075d2 JB |
6661 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6662 | dentry->d_name.len, 0, index); | |
39279cc3 | 6663 | if (err) |
b0d5d10f | 6664 | goto out_fail_inode; |
5f39d397 | 6665 | |
39279cc3 | 6666 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6667 | /* |
6668 | * mkdir is special. We're unlocking after we call d_instantiate | |
6669 | * to avoid a race with nfsd calling d_instantiate. | |
6670 | */ | |
6671 | unlock_new_inode(inode); | |
39279cc3 | 6672 | drop_on_err = 0; |
39279cc3 CM |
6673 | |
6674 | out_fail: | |
3a45bb20 | 6675 | btrfs_end_transaction(trans); |
c7cfb8a5 WS |
6676 | if (drop_on_err) { |
6677 | inode_dec_link_count(inode); | |
39279cc3 | 6678 | iput(inode); |
c7cfb8a5 | 6679 | } |
2ff7e61e JM |
6680 | btrfs_balance_delayed_items(fs_info); |
6681 | btrfs_btree_balance_dirty(fs_info); | |
39279cc3 | 6682 | return err; |
b0d5d10f CM |
6683 | |
6684 | out_fail_inode: | |
6685 | unlock_new_inode(inode); | |
6686 | goto out_fail; | |
39279cc3 CM |
6687 | } |
6688 | ||
e6c4efd8 QW |
6689 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6690 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6691 | { | |
6692 | struct rb_node *next; | |
6693 | ||
6694 | next = rb_next(&em->rb_node); | |
6695 | if (!next) | |
6696 | return NULL; | |
6697 | return container_of(next, struct extent_map, rb_node); | |
6698 | } | |
6699 | ||
6700 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6701 | { | |
6702 | struct rb_node *prev; | |
6703 | ||
6704 | prev = rb_prev(&em->rb_node); | |
6705 | if (!prev) | |
6706 | return NULL; | |
6707 | return container_of(prev, struct extent_map, rb_node); | |
6708 | } | |
6709 | ||
d352ac68 | 6710 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6711 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6712 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6713 | * the best fitted new extent into the tree. |
d352ac68 | 6714 | */ |
3b951516 CM |
6715 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6716 | struct extent_map *existing, | |
e6dcd2dc | 6717 | struct extent_map *em, |
51f395ad | 6718 | u64 map_start) |
3b951516 | 6719 | { |
e6c4efd8 QW |
6720 | struct extent_map *prev; |
6721 | struct extent_map *next; | |
6722 | u64 start; | |
6723 | u64 end; | |
3b951516 | 6724 | u64 start_diff; |
3b951516 | 6725 | |
e6dcd2dc | 6726 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6727 | |
6728 | if (existing->start > map_start) { | |
6729 | next = existing; | |
6730 | prev = prev_extent_map(next); | |
6731 | } else { | |
6732 | prev = existing; | |
6733 | next = next_extent_map(prev); | |
6734 | } | |
6735 | ||
6736 | start = prev ? extent_map_end(prev) : em->start; | |
6737 | start = max_t(u64, start, em->start); | |
6738 | end = next ? next->start : extent_map_end(em); | |
6739 | end = min_t(u64, end, extent_map_end(em)); | |
6740 | start_diff = start - em->start; | |
6741 | em->start = start; | |
6742 | em->len = end - start; | |
c8b97818 CM |
6743 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6744 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6745 | em->block_start += start_diff; |
c8b97818 CM |
6746 | em->block_len -= start_diff; |
6747 | } | |
09a2a8f9 | 6748 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6749 | } |
6750 | ||
c8b97818 | 6751 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6752 | struct page *page, |
c8b97818 CM |
6753 | size_t pg_offset, u64 extent_offset, |
6754 | struct btrfs_file_extent_item *item) | |
6755 | { | |
6756 | int ret; | |
6757 | struct extent_buffer *leaf = path->nodes[0]; | |
6758 | char *tmp; | |
6759 | size_t max_size; | |
6760 | unsigned long inline_size; | |
6761 | unsigned long ptr; | |
261507a0 | 6762 | int compress_type; |
c8b97818 CM |
6763 | |
6764 | WARN_ON(pg_offset != 0); | |
261507a0 | 6765 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6766 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6767 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6768 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6769 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6770 | if (!tmp) |
6771 | return -ENOMEM; | |
c8b97818 CM |
6772 | ptr = btrfs_file_extent_inline_start(item); |
6773 | ||
6774 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6775 | ||
09cbfeaf | 6776 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6777 | ret = btrfs_decompress(compress_type, tmp, page, |
6778 | extent_offset, inline_size, max_size); | |
c8b97818 | 6779 | kfree(tmp); |
166ae5a4 | 6780 | return ret; |
c8b97818 CM |
6781 | } |
6782 | ||
d352ac68 CM |
6783 | /* |
6784 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6785 | * the ugly parts come from merging extents from the disk with the in-ram |
6786 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6787 | * where the in-ram extents might be locked pending data=ordered completion. |
6788 | * | |
6789 | * This also copies inline extents directly into the page. | |
6790 | */ | |
d397712b | 6791 | |
a52d9a80 | 6792 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6793 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6794 | int create) |
6795 | { | |
0b246afa | 6796 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
a52d9a80 CM |
6797 | int ret; |
6798 | int err = 0; | |
a52d9a80 CM |
6799 | u64 extent_start = 0; |
6800 | u64 extent_end = 0; | |
4a0cc7ca | 6801 | u64 objectid = btrfs_ino(BTRFS_I(inode)); |
a52d9a80 | 6802 | u32 found_type; |
f421950f | 6803 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6804 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6805 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6806 | struct extent_buffer *leaf; |
6807 | struct btrfs_key found_key; | |
a52d9a80 CM |
6808 | struct extent_map *em = NULL; |
6809 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6810 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6811 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6812 | const bool new_inline = !page || create; |
a52d9a80 | 6813 | |
a52d9a80 | 6814 | again: |
890871be | 6815 | read_lock(&em_tree->lock); |
d1310b2e | 6816 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d | 6817 | if (em) |
0b246afa | 6818 | em->bdev = fs_info->fs_devices->latest_bdev; |
890871be | 6819 | read_unlock(&em_tree->lock); |
d1310b2e | 6820 | |
a52d9a80 | 6821 | if (em) { |
e1c4b745 CM |
6822 | if (em->start > start || em->start + em->len <= start) |
6823 | free_extent_map(em); | |
6824 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6825 | free_extent_map(em); |
6826 | else | |
6827 | goto out; | |
a52d9a80 | 6828 | } |
172ddd60 | 6829 | em = alloc_extent_map(); |
a52d9a80 | 6830 | if (!em) { |
d1310b2e CM |
6831 | err = -ENOMEM; |
6832 | goto out; | |
a52d9a80 | 6833 | } |
0b246afa | 6834 | em->bdev = fs_info->fs_devices->latest_bdev; |
d1310b2e | 6835 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6836 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6837 | em->len = (u64)-1; |
c8b97818 | 6838 | em->block_len = (u64)-1; |
f421950f CM |
6839 | |
6840 | if (!path) { | |
6841 | path = btrfs_alloc_path(); | |
026fd317 JB |
6842 | if (!path) { |
6843 | err = -ENOMEM; | |
6844 | goto out; | |
6845 | } | |
6846 | /* | |
6847 | * Chances are we'll be called again, so go ahead and do | |
6848 | * readahead | |
6849 | */ | |
e4058b54 | 6850 | path->reada = READA_FORWARD; |
f421950f CM |
6851 | } |
6852 | ||
179e29e4 CM |
6853 | ret = btrfs_lookup_file_extent(trans, root, path, |
6854 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6855 | if (ret < 0) { |
6856 | err = ret; | |
6857 | goto out; | |
6858 | } | |
6859 | ||
6860 | if (ret != 0) { | |
6861 | if (path->slots[0] == 0) | |
6862 | goto not_found; | |
6863 | path->slots[0]--; | |
6864 | } | |
6865 | ||
5f39d397 CM |
6866 | leaf = path->nodes[0]; |
6867 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6868 | struct btrfs_file_extent_item); |
a52d9a80 | 6869 | /* are we inside the extent that was found? */ |
5f39d397 | 6870 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6871 | found_type = found_key.type; |
5f39d397 | 6872 | if (found_key.objectid != objectid || |
a52d9a80 | 6873 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6874 | /* |
6875 | * If we backup past the first extent we want to move forward | |
6876 | * and see if there is an extent in front of us, otherwise we'll | |
6877 | * say there is a hole for our whole search range which can | |
6878 | * cause problems. | |
6879 | */ | |
6880 | extent_end = start; | |
6881 | goto next; | |
a52d9a80 CM |
6882 | } |
6883 | ||
5f39d397 CM |
6884 | found_type = btrfs_file_extent_type(leaf, item); |
6885 | extent_start = found_key.offset; | |
d899e052 YZ |
6886 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6887 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6888 | extent_end = extent_start + |
db94535d | 6889 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6890 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6891 | size_t size; | |
514ac8ad | 6892 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
da17066c | 6893 | extent_end = ALIGN(extent_start + size, |
0b246afa | 6894 | fs_info->sectorsize); |
9036c102 | 6895 | } |
25a50341 | 6896 | next: |
9036c102 YZ |
6897 | if (start >= extent_end) { |
6898 | path->slots[0]++; | |
6899 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6900 | ret = btrfs_next_leaf(root, path); | |
6901 | if (ret < 0) { | |
6902 | err = ret; | |
6903 | goto out; | |
a52d9a80 | 6904 | } |
9036c102 YZ |
6905 | if (ret > 0) |
6906 | goto not_found; | |
6907 | leaf = path->nodes[0]; | |
a52d9a80 | 6908 | } |
9036c102 YZ |
6909 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6910 | if (found_key.objectid != objectid || | |
6911 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6912 | goto not_found; | |
6913 | if (start + len <= found_key.offset) | |
6914 | goto not_found; | |
e2eca69d WS |
6915 | if (start > found_key.offset) |
6916 | goto next; | |
9036c102 | 6917 | em->start = start; |
70c8a91c | 6918 | em->orig_start = start; |
9036c102 YZ |
6919 | em->len = found_key.offset - start; |
6920 | goto not_found_em; | |
6921 | } | |
6922 | ||
7ffbb598 FM |
6923 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6924 | ||
d899e052 YZ |
6925 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6926 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6927 | goto insert; |
6928 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6929 | unsigned long ptr; |
a52d9a80 | 6930 | char *map; |
3326d1b0 CM |
6931 | size_t size; |
6932 | size_t extent_offset; | |
6933 | size_t copy_size; | |
a52d9a80 | 6934 | |
7ffbb598 | 6935 | if (new_inline) |
689f9346 | 6936 | goto out; |
5f39d397 | 6937 | |
514ac8ad | 6938 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6939 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6940 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6941 | size - extent_offset); | |
3326d1b0 | 6942 | em->start = extent_start + extent_offset; |
0b246afa | 6943 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
b4939680 | 6944 | em->orig_block_len = em->len; |
70c8a91c | 6945 | em->orig_start = em->start; |
689f9346 | 6946 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6947 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6948 | if (btrfs_file_extent_compression(leaf, item) != |
6949 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6950 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6951 | extent_offset, item); |
166ae5a4 ZB |
6952 | if (ret) { |
6953 | err = ret; | |
6954 | goto out; | |
6955 | } | |
c8b97818 CM |
6956 | } else { |
6957 | map = kmap(page); | |
6958 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6959 | copy_size); | |
09cbfeaf | 6960 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6961 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6962 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6963 | copy_size); |
6964 | } | |
c8b97818 CM |
6965 | kunmap(page); |
6966 | } | |
179e29e4 CM |
6967 | flush_dcache_page(page); |
6968 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6969 | BUG(); |
179e29e4 CM |
6970 | if (!trans) { |
6971 | kunmap(page); | |
6972 | free_extent_map(em); | |
6973 | em = NULL; | |
ff5714cc | 6974 | |
b3b4aa74 | 6975 | btrfs_release_path(path); |
7a7eaa40 | 6976 | trans = btrfs_join_transaction(root); |
ff5714cc | 6977 | |
3612b495 TI |
6978 | if (IS_ERR(trans)) |
6979 | return ERR_CAST(trans); | |
179e29e4 CM |
6980 | goto again; |
6981 | } | |
c8b97818 | 6982 | map = kmap(page); |
70dec807 | 6983 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6984 | copy_size); |
c8b97818 | 6985 | kunmap(page); |
179e29e4 | 6986 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6987 | } |
d1310b2e | 6988 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6989 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6990 | goto insert; |
a52d9a80 CM |
6991 | } |
6992 | not_found: | |
6993 | em->start = start; | |
70c8a91c | 6994 | em->orig_start = start; |
d1310b2e | 6995 | em->len = len; |
a52d9a80 | 6996 | not_found_em: |
5f39d397 | 6997 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6998 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6999 | insert: |
b3b4aa74 | 7000 | btrfs_release_path(path); |
d1310b2e | 7001 | if (em->start > start || extent_map_end(em) <= start) { |
0b246afa | 7002 | btrfs_err(fs_info, |
5d163e0e JM |
7003 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
7004 | em->start, em->len, start, len); | |
a52d9a80 CM |
7005 | err = -EIO; |
7006 | goto out; | |
7007 | } | |
d1310b2e CM |
7008 | |
7009 | err = 0; | |
890871be | 7010 | write_lock(&em_tree->lock); |
09a2a8f9 | 7011 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7012 | /* it is possible that someone inserted the extent into the tree |
7013 | * while we had the lock dropped. It is also possible that | |
7014 | * an overlapping map exists in the tree | |
7015 | */ | |
a52d9a80 | 7016 | if (ret == -EEXIST) { |
3b951516 | 7017 | struct extent_map *existing; |
e6dcd2dc CM |
7018 | |
7019 | ret = 0; | |
7020 | ||
e6c4efd8 QW |
7021 | existing = search_extent_mapping(em_tree, start, len); |
7022 | /* | |
7023 | * existing will always be non-NULL, since there must be | |
7024 | * extent causing the -EEXIST. | |
7025 | */ | |
8dff9c85 | 7026 | if (existing->start == em->start && |
8e2bd3b7 | 7027 | extent_map_end(existing) >= extent_map_end(em) && |
8dff9c85 CM |
7028 | em->block_start == existing->block_start) { |
7029 | /* | |
8e2bd3b7 OS |
7030 | * The existing extent map already encompasses the |
7031 | * entire extent map we tried to add. | |
8dff9c85 CM |
7032 | */ |
7033 | free_extent_map(em); | |
7034 | em = existing; | |
7035 | err = 0; | |
7036 | ||
7037 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7038 | start <= existing->start) { |
e6c4efd8 QW |
7039 | /* |
7040 | * The existing extent map is the one nearest to | |
7041 | * the [start, start + len) range which overlaps | |
7042 | */ | |
7043 | err = merge_extent_mapping(em_tree, existing, | |
7044 | em, start); | |
e1c4b745 | 7045 | free_extent_map(existing); |
e6c4efd8 | 7046 | if (err) { |
3b951516 CM |
7047 | free_extent_map(em); |
7048 | em = NULL; | |
7049 | } | |
7050 | } else { | |
7051 | free_extent_map(em); | |
7052 | em = existing; | |
e6dcd2dc | 7053 | err = 0; |
a52d9a80 | 7054 | } |
a52d9a80 | 7055 | } |
890871be | 7056 | write_unlock(&em_tree->lock); |
a52d9a80 | 7057 | out: |
1abe9b8a | 7058 | |
4a0cc7ca | 7059 | trace_btrfs_get_extent(root, BTRFS_I(inode), em); |
1abe9b8a | 7060 | |
527afb44 | 7061 | btrfs_free_path(path); |
a52d9a80 | 7062 | if (trans) { |
3a45bb20 | 7063 | ret = btrfs_end_transaction(trans); |
d397712b | 7064 | if (!err) |
a52d9a80 CM |
7065 | err = ret; |
7066 | } | |
a52d9a80 CM |
7067 | if (err) { |
7068 | free_extent_map(em); | |
a52d9a80 CM |
7069 | return ERR_PTR(err); |
7070 | } | |
79787eaa | 7071 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7072 | return em; |
7073 | } | |
7074 | ||
ec29ed5b CM |
7075 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
7076 | size_t pg_offset, u64 start, u64 len, | |
7077 | int create) | |
7078 | { | |
7079 | struct extent_map *em; | |
7080 | struct extent_map *hole_em = NULL; | |
7081 | u64 range_start = start; | |
7082 | u64 end; | |
7083 | u64 found; | |
7084 | u64 found_end; | |
7085 | int err = 0; | |
7086 | ||
7087 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7088 | if (IS_ERR(em)) | |
7089 | return em; | |
7090 | if (em) { | |
7091 | /* | |
f9e4fb53 LB |
7092 | * if our em maps to |
7093 | * - a hole or | |
7094 | * - a pre-alloc extent, | |
7095 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 7096 | */ |
f9e4fb53 LB |
7097 | if (em->block_start != EXTENT_MAP_HOLE && |
7098 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
7099 | return em; |
7100 | else | |
7101 | hole_em = em; | |
7102 | } | |
7103 | ||
7104 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7105 | end = start + len; | |
7106 | if (end < start) | |
7107 | end = (u64)-1; | |
7108 | else | |
7109 | end -= 1; | |
7110 | ||
7111 | em = NULL; | |
7112 | ||
7113 | /* ok, we didn't find anything, lets look for delalloc */ | |
7114 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
7115 | end, len, EXTENT_DELALLOC, 1); | |
7116 | found_end = range_start + found; | |
7117 | if (found_end < range_start) | |
7118 | found_end = (u64)-1; | |
7119 | ||
7120 | /* | |
7121 | * we didn't find anything useful, return | |
7122 | * the original results from get_extent() | |
7123 | */ | |
7124 | if (range_start > end || found_end <= start) { | |
7125 | em = hole_em; | |
7126 | hole_em = NULL; | |
7127 | goto out; | |
7128 | } | |
7129 | ||
7130 | /* adjust the range_start to make sure it doesn't | |
7131 | * go backwards from the start they passed in | |
7132 | */ | |
67871254 | 7133 | range_start = max(start, range_start); |
ec29ed5b CM |
7134 | found = found_end - range_start; |
7135 | ||
7136 | if (found > 0) { | |
7137 | u64 hole_start = start; | |
7138 | u64 hole_len = len; | |
7139 | ||
172ddd60 | 7140 | em = alloc_extent_map(); |
ec29ed5b CM |
7141 | if (!em) { |
7142 | err = -ENOMEM; | |
7143 | goto out; | |
7144 | } | |
7145 | /* | |
7146 | * when btrfs_get_extent can't find anything it | |
7147 | * returns one huge hole | |
7148 | * | |
7149 | * make sure what it found really fits our range, and | |
7150 | * adjust to make sure it is based on the start from | |
7151 | * the caller | |
7152 | */ | |
7153 | if (hole_em) { | |
7154 | u64 calc_end = extent_map_end(hole_em); | |
7155 | ||
7156 | if (calc_end <= start || (hole_em->start > end)) { | |
7157 | free_extent_map(hole_em); | |
7158 | hole_em = NULL; | |
7159 | } else { | |
7160 | hole_start = max(hole_em->start, start); | |
7161 | hole_len = calc_end - hole_start; | |
7162 | } | |
7163 | } | |
7164 | em->bdev = NULL; | |
7165 | if (hole_em && range_start > hole_start) { | |
7166 | /* our hole starts before our delalloc, so we | |
7167 | * have to return just the parts of the hole | |
7168 | * that go until the delalloc starts | |
7169 | */ | |
7170 | em->len = min(hole_len, | |
7171 | range_start - hole_start); | |
7172 | em->start = hole_start; | |
7173 | em->orig_start = hole_start; | |
7174 | /* | |
7175 | * don't adjust block start at all, | |
7176 | * it is fixed at EXTENT_MAP_HOLE | |
7177 | */ | |
7178 | em->block_start = hole_em->block_start; | |
7179 | em->block_len = hole_len; | |
f9e4fb53 LB |
7180 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7181 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7182 | } else { |
7183 | em->start = range_start; | |
7184 | em->len = found; | |
7185 | em->orig_start = range_start; | |
7186 | em->block_start = EXTENT_MAP_DELALLOC; | |
7187 | em->block_len = found; | |
7188 | } | |
7189 | } else if (hole_em) { | |
7190 | return hole_em; | |
7191 | } | |
7192 | out: | |
7193 | ||
7194 | free_extent_map(hole_em); | |
7195 | if (err) { | |
7196 | free_extent_map(em); | |
7197 | return ERR_PTR(err); | |
7198 | } | |
7199 | return em; | |
7200 | } | |
7201 | ||
5f9a8a51 FM |
7202 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7203 | const u64 start, | |
7204 | const u64 len, | |
7205 | const u64 orig_start, | |
7206 | const u64 block_start, | |
7207 | const u64 block_len, | |
7208 | const u64 orig_block_len, | |
7209 | const u64 ram_bytes, | |
7210 | const int type) | |
7211 | { | |
7212 | struct extent_map *em = NULL; | |
7213 | int ret; | |
7214 | ||
5f9a8a51 FM |
7215 | if (type != BTRFS_ORDERED_NOCOW) { |
7216 | em = create_pinned_em(inode, start, len, orig_start, | |
7217 | block_start, block_len, orig_block_len, | |
7218 | ram_bytes, type); | |
7219 | if (IS_ERR(em)) | |
7220 | goto out; | |
7221 | } | |
7222 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7223 | len, block_len, type); | |
7224 | if (ret) { | |
7225 | if (em) { | |
7226 | free_extent_map(em); | |
7227 | btrfs_drop_extent_cache(inode, start, | |
7228 | start + len - 1, 0); | |
7229 | } | |
7230 | em = ERR_PTR(ret); | |
7231 | } | |
7232 | out: | |
5f9a8a51 FM |
7233 | |
7234 | return em; | |
7235 | } | |
7236 | ||
4b46fce2 JB |
7237 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7238 | u64 start, u64 len) | |
7239 | { | |
0b246afa | 7240 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7241 | struct btrfs_root *root = BTRFS_I(inode)->root; |
70c8a91c | 7242 | struct extent_map *em; |
4b46fce2 JB |
7243 | struct btrfs_key ins; |
7244 | u64 alloc_hint; | |
7245 | int ret; | |
4b46fce2 | 7246 | |
4b46fce2 | 7247 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
0b246afa | 7248 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
da17066c | 7249 | 0, alloc_hint, &ins, 1, 1); |
00361589 JB |
7250 | if (ret) |
7251 | return ERR_PTR(ret); | |
4b46fce2 | 7252 | |
5f9a8a51 FM |
7253 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7254 | ins.objectid, ins.offset, ins.offset, | |
7255 | ins.offset, 0); | |
0b246afa | 7256 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5f9a8a51 | 7257 | if (IS_ERR(em)) |
2ff7e61e JM |
7258 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
7259 | ins.offset, 1); | |
de0ee0ed | 7260 | |
4b46fce2 JB |
7261 | return em; |
7262 | } | |
7263 | ||
46bfbb5c CM |
7264 | /* |
7265 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7266 | * block must be cow'd | |
7267 | */ | |
00361589 | 7268 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7269 | u64 *orig_start, u64 *orig_block_len, |
7270 | u64 *ram_bytes) | |
46bfbb5c | 7271 | { |
2ff7e61e | 7272 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
00361589 | 7273 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7274 | struct btrfs_path *path; |
7275 | int ret; | |
7276 | struct extent_buffer *leaf; | |
7277 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7278 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7279 | struct btrfs_file_extent_item *fi; |
7280 | struct btrfs_key key; | |
7281 | u64 disk_bytenr; | |
7282 | u64 backref_offset; | |
7283 | u64 extent_end; | |
7284 | u64 num_bytes; | |
7285 | int slot; | |
7286 | int found_type; | |
7ee9e440 | 7287 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7288 | |
46bfbb5c CM |
7289 | path = btrfs_alloc_path(); |
7290 | if (!path) | |
7291 | return -ENOMEM; | |
7292 | ||
4a0cc7ca | 7293 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(BTRFS_I(inode)), |
46bfbb5c CM |
7294 | offset, 0); |
7295 | if (ret < 0) | |
7296 | goto out; | |
7297 | ||
7298 | slot = path->slots[0]; | |
7299 | if (ret == 1) { | |
7300 | if (slot == 0) { | |
7301 | /* can't find the item, must cow */ | |
7302 | ret = 0; | |
7303 | goto out; | |
7304 | } | |
7305 | slot--; | |
7306 | } | |
7307 | ret = 0; | |
7308 | leaf = path->nodes[0]; | |
7309 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
4a0cc7ca | 7310 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
46bfbb5c CM |
7311 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7312 | /* not our file or wrong item type, must cow */ | |
7313 | goto out; | |
7314 | } | |
7315 | ||
7316 | if (key.offset > offset) { | |
7317 | /* Wrong offset, must cow */ | |
7318 | goto out; | |
7319 | } | |
7320 | ||
7321 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7322 | found_type = btrfs_file_extent_type(leaf, fi); | |
7323 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7324 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7325 | /* not a regular extent, must cow */ | |
7326 | goto out; | |
7327 | } | |
7ee9e440 JB |
7328 | |
7329 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7330 | goto out; | |
7331 | ||
e77751aa MX |
7332 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7333 | if (extent_end <= offset) | |
7334 | goto out; | |
7335 | ||
46bfbb5c | 7336 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7337 | if (disk_bytenr == 0) |
7338 | goto out; | |
7339 | ||
7340 | if (btrfs_file_extent_compression(leaf, fi) || | |
7341 | btrfs_file_extent_encryption(leaf, fi) || | |
7342 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7343 | goto out; | |
7344 | ||
46bfbb5c CM |
7345 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7346 | ||
7ee9e440 JB |
7347 | if (orig_start) { |
7348 | *orig_start = key.offset - backref_offset; | |
7349 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7350 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7351 | } | |
eb384b55 | 7352 | |
2ff7e61e | 7353 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
46bfbb5c | 7354 | goto out; |
7b2b7085 MX |
7355 | |
7356 | num_bytes = min(offset + *len, extent_end) - offset; | |
7357 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7358 | u64 range_end; | |
7359 | ||
da17066c JM |
7360 | range_end = round_up(offset + num_bytes, |
7361 | root->fs_info->sectorsize) - 1; | |
7b2b7085 MX |
7362 | ret = test_range_bit(io_tree, offset, range_end, |
7363 | EXTENT_DELALLOC, 0, NULL); | |
7364 | if (ret) { | |
7365 | ret = -EAGAIN; | |
7366 | goto out; | |
7367 | } | |
7368 | } | |
7369 | ||
1bda19eb | 7370 | btrfs_release_path(path); |
46bfbb5c CM |
7371 | |
7372 | /* | |
7373 | * look for other files referencing this extent, if we | |
7374 | * find any we must cow | |
7375 | */ | |
00361589 JB |
7376 | trans = btrfs_join_transaction(root); |
7377 | if (IS_ERR(trans)) { | |
7378 | ret = 0; | |
46bfbb5c | 7379 | goto out; |
00361589 JB |
7380 | } |
7381 | ||
4a0cc7ca | 7382 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(BTRFS_I(inode)), |
00361589 | 7383 | key.offset - backref_offset, disk_bytenr); |
3a45bb20 | 7384 | btrfs_end_transaction(trans); |
00361589 JB |
7385 | if (ret) { |
7386 | ret = 0; | |
7387 | goto out; | |
7388 | } | |
46bfbb5c CM |
7389 | |
7390 | /* | |
7391 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7392 | * in this extent we are about to write. If there | |
7393 | * are any csums in that range we have to cow in order | |
7394 | * to keep the csums correct | |
7395 | */ | |
7396 | disk_bytenr += backref_offset; | |
7397 | disk_bytenr += offset - key.offset; | |
2ff7e61e JM |
7398 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
7399 | goto out; | |
46bfbb5c CM |
7400 | /* |
7401 | * all of the above have passed, it is safe to overwrite this extent | |
7402 | * without cow | |
7403 | */ | |
eb384b55 | 7404 | *len = num_bytes; |
46bfbb5c CM |
7405 | ret = 1; |
7406 | out: | |
7407 | btrfs_free_path(path); | |
7408 | return ret; | |
7409 | } | |
7410 | ||
fc4adbff AG |
7411 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7412 | { | |
7413 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7414 | int found = false; | |
7415 | void **pagep = NULL; | |
7416 | struct page *page = NULL; | |
7417 | int start_idx; | |
7418 | int end_idx; | |
7419 | ||
09cbfeaf | 7420 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7421 | |
7422 | /* | |
7423 | * end is the last byte in the last page. end == start is legal | |
7424 | */ | |
09cbfeaf | 7425 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7426 | |
7427 | rcu_read_lock(); | |
7428 | ||
7429 | /* Most of the code in this while loop is lifted from | |
7430 | * find_get_page. It's been modified to begin searching from a | |
7431 | * page and return just the first page found in that range. If the | |
7432 | * found idx is less than or equal to the end idx then we know that | |
7433 | * a page exists. If no pages are found or if those pages are | |
7434 | * outside of the range then we're fine (yay!) */ | |
7435 | while (page == NULL && | |
7436 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7437 | page = radix_tree_deref_slot(pagep); | |
7438 | if (unlikely(!page)) | |
7439 | break; | |
7440 | ||
7441 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7442 | if (radix_tree_deref_retry(page)) { |
7443 | page = NULL; | |
fc4adbff | 7444 | continue; |
809f9016 | 7445 | } |
fc4adbff AG |
7446 | /* |
7447 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7448 | * here as an exceptional entry: so return it without | |
7449 | * attempting to raise page count. | |
7450 | */ | |
6fdef6d4 | 7451 | page = NULL; |
fc4adbff AG |
7452 | break; /* TODO: Is this relevant for this use case? */ |
7453 | } | |
7454 | ||
91405151 FM |
7455 | if (!page_cache_get_speculative(page)) { |
7456 | page = NULL; | |
fc4adbff | 7457 | continue; |
91405151 | 7458 | } |
fc4adbff AG |
7459 | |
7460 | /* | |
7461 | * Has the page moved? | |
7462 | * This is part of the lockless pagecache protocol. See | |
7463 | * include/linux/pagemap.h for details. | |
7464 | */ | |
7465 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7466 | put_page(page); |
fc4adbff AG |
7467 | page = NULL; |
7468 | } | |
7469 | } | |
7470 | ||
7471 | if (page) { | |
7472 | if (page->index <= end_idx) | |
7473 | found = true; | |
09cbfeaf | 7474 | put_page(page); |
fc4adbff AG |
7475 | } |
7476 | ||
7477 | rcu_read_unlock(); | |
7478 | return found; | |
7479 | } | |
7480 | ||
eb838e73 JB |
7481 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7482 | struct extent_state **cached_state, int writing) | |
7483 | { | |
7484 | struct btrfs_ordered_extent *ordered; | |
7485 | int ret = 0; | |
7486 | ||
7487 | while (1) { | |
7488 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7489 | cached_state); |
eb838e73 JB |
7490 | /* |
7491 | * We're concerned with the entire range that we're going to be | |
01327610 | 7492 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7493 | * extents in this range. |
7494 | */ | |
7495 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7496 | lockend - lockstart + 1); | |
7497 | ||
7498 | /* | |
7499 | * We need to make sure there are no buffered pages in this | |
7500 | * range either, we could have raced between the invalidate in | |
7501 | * generic_file_direct_write and locking the extent. The | |
7502 | * invalidate needs to happen so that reads after a write do not | |
7503 | * get stale data. | |
7504 | */ | |
fc4adbff AG |
7505 | if (!ordered && |
7506 | (!writing || | |
7507 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7508 | break; |
7509 | ||
7510 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7511 | cached_state, GFP_NOFS); | |
7512 | ||
7513 | if (ordered) { | |
ade77029 FM |
7514 | /* |
7515 | * If we are doing a DIO read and the ordered extent we | |
7516 | * found is for a buffered write, we can not wait for it | |
7517 | * to complete and retry, because if we do so we can | |
7518 | * deadlock with concurrent buffered writes on page | |
7519 | * locks. This happens only if our DIO read covers more | |
7520 | * than one extent map, if at this point has already | |
7521 | * created an ordered extent for a previous extent map | |
7522 | * and locked its range in the inode's io tree, and a | |
7523 | * concurrent write against that previous extent map's | |
7524 | * range and this range started (we unlock the ranges | |
7525 | * in the io tree only when the bios complete and | |
7526 | * buffered writes always lock pages before attempting | |
7527 | * to lock range in the io tree). | |
7528 | */ | |
7529 | if (writing || | |
7530 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7531 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7532 | else | |
7533 | ret = -ENOTBLK; | |
eb838e73 JB |
7534 | btrfs_put_ordered_extent(ordered); |
7535 | } else { | |
eb838e73 | 7536 | /* |
b850ae14 FM |
7537 | * We could trigger writeback for this range (and wait |
7538 | * for it to complete) and then invalidate the pages for | |
7539 | * this range (through invalidate_inode_pages2_range()), | |
7540 | * but that can lead us to a deadlock with a concurrent | |
7541 | * call to readpages() (a buffered read or a defrag call | |
7542 | * triggered a readahead) on a page lock due to an | |
7543 | * ordered dio extent we created before but did not have | |
7544 | * yet a corresponding bio submitted (whence it can not | |
7545 | * complete), which makes readpages() wait for that | |
7546 | * ordered extent to complete while holding a lock on | |
7547 | * that page. | |
eb838e73 | 7548 | */ |
b850ae14 | 7549 | ret = -ENOTBLK; |
eb838e73 JB |
7550 | } |
7551 | ||
ade77029 FM |
7552 | if (ret) |
7553 | break; | |
7554 | ||
eb838e73 JB |
7555 | cond_resched(); |
7556 | } | |
7557 | ||
7558 | return ret; | |
7559 | } | |
7560 | ||
69ffb543 JB |
7561 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7562 | u64 len, u64 orig_start, | |
7563 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7564 | u64 orig_block_len, u64 ram_bytes, |
7565 | int type) | |
69ffb543 JB |
7566 | { |
7567 | struct extent_map_tree *em_tree; | |
7568 | struct extent_map *em; | |
7569 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7570 | int ret; | |
7571 | ||
7572 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7573 | em = alloc_extent_map(); | |
7574 | if (!em) | |
7575 | return ERR_PTR(-ENOMEM); | |
7576 | ||
7577 | em->start = start; | |
7578 | em->orig_start = orig_start; | |
2ab28f32 JB |
7579 | em->mod_start = start; |
7580 | em->mod_len = len; | |
69ffb543 JB |
7581 | em->len = len; |
7582 | em->block_len = block_len; | |
7583 | em->block_start = block_start; | |
7584 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7585 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7586 | em->ram_bytes = ram_bytes; |
70c8a91c | 7587 | em->generation = -1; |
69ffb543 JB |
7588 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7589 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7590 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7591 | |
7592 | do { | |
7593 | btrfs_drop_extent_cache(inode, em->start, | |
7594 | em->start + em->len - 1, 0); | |
7595 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7596 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7597 | write_unlock(&em_tree->lock); |
7598 | } while (ret == -EEXIST); | |
7599 | ||
7600 | if (ret) { | |
7601 | free_extent_map(em); | |
7602 | return ERR_PTR(ret); | |
7603 | } | |
7604 | ||
7605 | return em; | |
7606 | } | |
7607 | ||
9c9464cc FM |
7608 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7609 | struct btrfs_dio_data *dio_data, | |
7610 | const u64 len) | |
7611 | { | |
823bb20a | 7612 | unsigned num_extents = count_max_extents(len); |
9c9464cc | 7613 | |
9c9464cc FM |
7614 | /* |
7615 | * If we have an outstanding_extents count still set then we're | |
7616 | * within our reservation, otherwise we need to adjust our inode | |
7617 | * counter appropriately. | |
7618 | */ | |
c2931667 | 7619 | if (dio_data->outstanding_extents >= num_extents) { |
9c9464cc FM |
7620 | dio_data->outstanding_extents -= num_extents; |
7621 | } else { | |
c2931667 LB |
7622 | /* |
7623 | * If dio write length has been split due to no large enough | |
7624 | * contiguous space, we need to compensate our inode counter | |
7625 | * appropriately. | |
7626 | */ | |
7627 | u64 num_needed = num_extents - dio_data->outstanding_extents; | |
7628 | ||
9c9464cc | 7629 | spin_lock(&BTRFS_I(inode)->lock); |
c2931667 | 7630 | BTRFS_I(inode)->outstanding_extents += num_needed; |
9c9464cc FM |
7631 | spin_unlock(&BTRFS_I(inode)->lock); |
7632 | } | |
7633 | } | |
7634 | ||
4b46fce2 JB |
7635 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7636 | struct buffer_head *bh_result, int create) | |
7637 | { | |
0b246afa | 7638 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 7639 | struct extent_map *em; |
eb838e73 | 7640 | struct extent_state *cached_state = NULL; |
50745b0a | 7641 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7642 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7643 | u64 lockstart, lockend; |
4b46fce2 | 7644 | u64 len = bh_result->b_size; |
eb838e73 | 7645 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7646 | int ret = 0; |
eb838e73 | 7647 | |
172a5049 | 7648 | if (create) |
3266789f | 7649 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7650 | else |
0b246afa | 7651 | len = min_t(u64, len, fs_info->sectorsize); |
eb838e73 | 7652 | |
c329861d JB |
7653 | lockstart = start; |
7654 | lockend = start + len - 1; | |
7655 | ||
e1cbbfa5 JB |
7656 | if (current->journal_info) { |
7657 | /* | |
7658 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7659 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7660 | * confused. |
7661 | */ | |
50745b0a | 7662 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7663 | current->journal_info = NULL; |
7664 | } | |
7665 | ||
eb838e73 JB |
7666 | /* |
7667 | * If this errors out it's because we couldn't invalidate pagecache for | |
7668 | * this range and we need to fallback to buffered. | |
7669 | */ | |
9c9464cc FM |
7670 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7671 | create)) { | |
7672 | ret = -ENOTBLK; | |
7673 | goto err; | |
7674 | } | |
eb838e73 | 7675 | |
4b46fce2 | 7676 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7677 | if (IS_ERR(em)) { |
7678 | ret = PTR_ERR(em); | |
7679 | goto unlock_err; | |
7680 | } | |
4b46fce2 JB |
7681 | |
7682 | /* | |
7683 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7684 | * io. INLINE is special, and we could probably kludge it in here, but | |
7685 | * it's still buffered so for safety lets just fall back to the generic | |
7686 | * buffered path. | |
7687 | * | |
7688 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7689 | * decompress it, so there will be buffering required no matter what we | |
7690 | * do, so go ahead and fallback to buffered. | |
7691 | * | |
01327610 | 7692 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7693 | * to buffered IO. Don't blame me, this is the price we pay for using |
7694 | * the generic code. | |
7695 | */ | |
7696 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7697 | em->block_start == EXTENT_MAP_INLINE) { | |
7698 | free_extent_map(em); | |
eb838e73 JB |
7699 | ret = -ENOTBLK; |
7700 | goto unlock_err; | |
4b46fce2 JB |
7701 | } |
7702 | ||
7703 | /* Just a good old fashioned hole, return */ | |
7704 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7705 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7706 | free_extent_map(em); | |
eb838e73 | 7707 | goto unlock_err; |
4b46fce2 JB |
7708 | } |
7709 | ||
7710 | /* | |
7711 | * We don't allocate a new extent in the following cases | |
7712 | * | |
7713 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7714 | * existing extent. | |
7715 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7716 | * just use the extent. | |
7717 | * | |
7718 | */ | |
46bfbb5c | 7719 | if (!create) { |
eb838e73 JB |
7720 | len = min(len, em->len - (start - em->start)); |
7721 | lockstart = start + len; | |
7722 | goto unlock; | |
46bfbb5c | 7723 | } |
4b46fce2 JB |
7724 | |
7725 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7726 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7727 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7728 | int type; |
eb384b55 | 7729 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7730 | |
7731 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7732 | type = BTRFS_ORDERED_PREALLOC; | |
7733 | else | |
7734 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7735 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7736 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7737 | |
00361589 | 7738 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c | 7739 | &orig_block_len, &ram_bytes) == 1 && |
0b246afa | 7740 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
5f9a8a51 | 7741 | struct extent_map *em2; |
0b901916 | 7742 | |
5f9a8a51 FM |
7743 | em2 = btrfs_create_dio_extent(inode, start, len, |
7744 | orig_start, block_start, | |
7745 | len, orig_block_len, | |
7746 | ram_bytes, type); | |
0b246afa | 7747 | btrfs_dec_nocow_writers(fs_info, block_start); |
69ffb543 JB |
7748 | if (type == BTRFS_ORDERED_PREALLOC) { |
7749 | free_extent_map(em); | |
5f9a8a51 | 7750 | em = em2; |
69ffb543 | 7751 | } |
5f9a8a51 FM |
7752 | if (em2 && IS_ERR(em2)) { |
7753 | ret = PTR_ERR(em2); | |
eb838e73 | 7754 | goto unlock_err; |
46bfbb5c | 7755 | } |
18513091 WX |
7756 | /* |
7757 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7758 | * use the existing or preallocated extent, so does not | |
7759 | * need to adjust btrfs_space_info's bytes_may_use. | |
7760 | */ | |
7761 | btrfs_free_reserved_data_space_noquota(inode, | |
7762 | start, len); | |
46bfbb5c | 7763 | goto unlock; |
4b46fce2 | 7764 | } |
4b46fce2 | 7765 | } |
00361589 | 7766 | |
46bfbb5c CM |
7767 | /* |
7768 | * this will cow the extent, reset the len in case we changed | |
7769 | * it above | |
7770 | */ | |
7771 | len = bh_result->b_size; | |
70c8a91c JB |
7772 | free_extent_map(em); |
7773 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7774 | if (IS_ERR(em)) { |
7775 | ret = PTR_ERR(em); | |
7776 | goto unlock_err; | |
7777 | } | |
46bfbb5c CM |
7778 | len = min(len, em->len - (start - em->start)); |
7779 | unlock: | |
4b46fce2 JB |
7780 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7781 | inode->i_blkbits; | |
46bfbb5c | 7782 | bh_result->b_size = len; |
4b46fce2 JB |
7783 | bh_result->b_bdev = em->bdev; |
7784 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7785 | if (create) { |
7786 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7787 | set_buffer_new(bh_result); | |
7788 | ||
7789 | /* | |
7790 | * Need to update the i_size under the extent lock so buffered | |
7791 | * readers will get the updated i_size when we unlock. | |
7792 | */ | |
7793 | if (start + len > i_size_read(inode)) | |
7794 | i_size_write(inode, start + len); | |
0934856d | 7795 | |
9c9464cc | 7796 | adjust_dio_outstanding_extents(inode, dio_data, len); |
50745b0a | 7797 | WARN_ON(dio_data->reserve < len); |
7798 | dio_data->reserve -= len; | |
f28a4928 | 7799 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7800 | current->journal_info = dio_data; |
c3473e83 | 7801 | } |
4b46fce2 | 7802 | |
eb838e73 JB |
7803 | /* |
7804 | * In the case of write we need to clear and unlock the entire range, | |
7805 | * in the case of read we need to unlock only the end area that we | |
7806 | * aren't using if there is any left over space. | |
7807 | */ | |
24c03fa5 | 7808 | if (lockstart < lockend) { |
0934856d MX |
7809 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7810 | lockend, unlock_bits, 1, 0, | |
7811 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7812 | } else { |
eb838e73 | 7813 | free_extent_state(cached_state); |
24c03fa5 | 7814 | } |
eb838e73 | 7815 | |
4b46fce2 JB |
7816 | free_extent_map(em); |
7817 | ||
7818 | return 0; | |
eb838e73 JB |
7819 | |
7820 | unlock_err: | |
eb838e73 JB |
7821 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7822 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7823 | err: |
50745b0a | 7824 | if (dio_data) |
7825 | current->journal_info = dio_data; | |
9c9464cc FM |
7826 | /* |
7827 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7828 | * write less data then expected, so that we don't underflow our inode's | |
7829 | * outstanding extents counter. | |
7830 | */ | |
7831 | if (create && dio_data) | |
7832 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7833 | ||
eb838e73 | 7834 | return ret; |
4b46fce2 JB |
7835 | } |
7836 | ||
8b110e39 | 7837 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
81a75f67 | 7838 | int mirror_num) |
8b110e39 | 7839 | { |
2ff7e61e | 7840 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7841 | int ret; |
7842 | ||
37226b21 | 7843 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
7844 | |
7845 | bio_get(bio); | |
7846 | ||
2ff7e61e | 7847 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
8b110e39 MX |
7848 | if (ret) |
7849 | goto err; | |
7850 | ||
2ff7e61e | 7851 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
8b110e39 MX |
7852 | err: |
7853 | bio_put(bio); | |
7854 | return ret; | |
7855 | } | |
7856 | ||
7857 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7858 | struct bio *failed_bio, | |
7859 | struct io_failure_record *failrec, | |
7860 | int failed_mirror) | |
7861 | { | |
ab8d0fc4 | 7862 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8b110e39 MX |
7863 | int num_copies; |
7864 | ||
ab8d0fc4 | 7865 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
8b110e39 MX |
7866 | if (num_copies == 1) { |
7867 | /* | |
7868 | * we only have a single copy of the data, so don't bother with | |
7869 | * all the retry and error correction code that follows. no | |
7870 | * matter what the error is, it is very likely to persist. | |
7871 | */ | |
ab8d0fc4 JM |
7872 | btrfs_debug(fs_info, |
7873 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", | |
7874 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7875 | return 0; |
7876 | } | |
7877 | ||
7878 | failrec->failed_mirror = failed_mirror; | |
7879 | failrec->this_mirror++; | |
7880 | if (failrec->this_mirror == failed_mirror) | |
7881 | failrec->this_mirror++; | |
7882 | ||
7883 | if (failrec->this_mirror > num_copies) { | |
ab8d0fc4 JM |
7884 | btrfs_debug(fs_info, |
7885 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", | |
7886 | num_copies, failrec->this_mirror, failed_mirror); | |
8b110e39 MX |
7887 | return 0; |
7888 | } | |
7889 | ||
7890 | return 1; | |
7891 | } | |
7892 | ||
7893 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
2dabb324 CR |
7894 | struct page *page, unsigned int pgoff, |
7895 | u64 start, u64 end, int failed_mirror, | |
7896 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7897 | { |
7898 | struct io_failure_record *failrec; | |
7899 | struct bio *bio; | |
7900 | int isector; | |
70fd7614 | 7901 | int read_mode = 0; |
8b110e39 MX |
7902 | int ret; |
7903 | ||
37226b21 | 7904 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7905 | |
7906 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7907 | if (ret) | |
7908 | return ret; | |
7909 | ||
7910 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7911 | failed_mirror); | |
7912 | if (!ret) { | |
7913 | free_io_failure(inode, failrec); | |
7914 | return -EIO; | |
7915 | } | |
7916 | ||
2dabb324 CR |
7917 | if ((failed_bio->bi_vcnt > 1) |
7918 | || (failed_bio->bi_io_vec->bv_len | |
da17066c | 7919 | > btrfs_inode_sectorsize(inode))) |
70fd7614 | 7920 | read_mode |= REQ_FAILFAST_DEV; |
8b110e39 MX |
7921 | |
7922 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7923 | isector >>= inode->i_sb->s_blocksize_bits; | |
7924 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 7925 | pgoff, isector, repair_endio, repair_arg); |
8b110e39 MX |
7926 | if (!bio) { |
7927 | free_io_failure(inode, failrec); | |
7928 | return -EIO; | |
7929 | } | |
37226b21 | 7930 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
7931 | |
7932 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7933 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7934 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7935 | ||
81a75f67 | 7936 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8b110e39 MX |
7937 | if (ret) { |
7938 | free_io_failure(inode, failrec); | |
7939 | bio_put(bio); | |
7940 | } | |
7941 | ||
7942 | return ret; | |
7943 | } | |
7944 | ||
7945 | struct btrfs_retry_complete { | |
7946 | struct completion done; | |
7947 | struct inode *inode; | |
7948 | u64 start; | |
7949 | int uptodate; | |
7950 | }; | |
7951 | ||
4246a0b6 | 7952 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7953 | { |
7954 | struct btrfs_retry_complete *done = bio->bi_private; | |
2dabb324 | 7955 | struct inode *inode; |
8b110e39 MX |
7956 | struct bio_vec *bvec; |
7957 | int i; | |
7958 | ||
4246a0b6 | 7959 | if (bio->bi_error) |
8b110e39 MX |
7960 | goto end; |
7961 | ||
2dabb324 CR |
7962 | ASSERT(bio->bi_vcnt == 1); |
7963 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
da17066c | 7964 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
2dabb324 | 7965 | |
8b110e39 MX |
7966 | done->uptodate = 1; |
7967 | bio_for_each_segment_all(bvec, bio, i) | |
7968 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7969 | end: | |
7970 | complete(&done->done); | |
7971 | bio_put(bio); | |
7972 | } | |
7973 | ||
7974 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7975 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7976 | { |
2dabb324 | 7977 | struct btrfs_fs_info *fs_info; |
2c30c71b | 7978 | struct bio_vec *bvec; |
8b110e39 | 7979 | struct btrfs_retry_complete done; |
4b46fce2 | 7980 | u64 start; |
2dabb324 CR |
7981 | unsigned int pgoff; |
7982 | u32 sectorsize; | |
7983 | int nr_sectors; | |
2c30c71b | 7984 | int i; |
c1dc0896 | 7985 | int ret; |
4b46fce2 | 7986 | |
2dabb324 | 7987 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 7988 | sectorsize = fs_info->sectorsize; |
2dabb324 | 7989 | |
8b110e39 MX |
7990 | start = io_bio->logical; |
7991 | done.inode = inode; | |
7992 | ||
7993 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
2dabb324 CR |
7994 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
7995 | pgoff = bvec->bv_offset; | |
7996 | ||
7997 | next_block_or_try_again: | |
8b110e39 MX |
7998 | done.uptodate = 0; |
7999 | done.start = start; | |
8000 | init_completion(&done.done); | |
8001 | ||
2dabb324 CR |
8002 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
8003 | pgoff, start, start + sectorsize - 1, | |
8004 | io_bio->mirror_num, | |
8005 | btrfs_retry_endio_nocsum, &done); | |
8b110e39 MX |
8006 | if (ret) |
8007 | return ret; | |
8008 | ||
8009 | wait_for_completion(&done.done); | |
8010 | ||
8011 | if (!done.uptodate) { | |
8012 | /* We might have another mirror, so try again */ | |
2dabb324 | 8013 | goto next_block_or_try_again; |
8b110e39 MX |
8014 | } |
8015 | ||
2dabb324 CR |
8016 | start += sectorsize; |
8017 | ||
8018 | if (nr_sectors--) { | |
8019 | pgoff += sectorsize; | |
8020 | goto next_block_or_try_again; | |
8021 | } | |
8b110e39 MX |
8022 | } |
8023 | ||
8024 | return 0; | |
8025 | } | |
8026 | ||
4246a0b6 | 8027 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8028 | { |
8029 | struct btrfs_retry_complete *done = bio->bi_private; | |
8030 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
2dabb324 | 8031 | struct inode *inode; |
8b110e39 | 8032 | struct bio_vec *bvec; |
2dabb324 | 8033 | u64 start; |
8b110e39 MX |
8034 | int uptodate; |
8035 | int ret; | |
8036 | int i; | |
8037 | ||
4246a0b6 | 8038 | if (bio->bi_error) |
8b110e39 MX |
8039 | goto end; |
8040 | ||
8041 | uptodate = 1; | |
2dabb324 CR |
8042 | |
8043 | start = done->start; | |
8044 | ||
8045 | ASSERT(bio->bi_vcnt == 1); | |
8046 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
da17066c | 8047 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
2dabb324 | 8048 | |
8b110e39 MX |
8049 | bio_for_each_segment_all(bvec, bio, i) { |
8050 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
2dabb324 CR |
8051 | bvec->bv_page, bvec->bv_offset, |
8052 | done->start, bvec->bv_len); | |
8b110e39 MX |
8053 | if (!ret) |
8054 | clean_io_failure(done->inode, done->start, | |
2dabb324 | 8055 | bvec->bv_page, bvec->bv_offset); |
8b110e39 MX |
8056 | else |
8057 | uptodate = 0; | |
8058 | } | |
8059 | ||
8060 | done->uptodate = uptodate; | |
8061 | end: | |
8062 | complete(&done->done); | |
8063 | bio_put(bio); | |
8064 | } | |
8065 | ||
8066 | static int __btrfs_subio_endio_read(struct inode *inode, | |
8067 | struct btrfs_io_bio *io_bio, int err) | |
8068 | { | |
2dabb324 | 8069 | struct btrfs_fs_info *fs_info; |
8b110e39 MX |
8070 | struct bio_vec *bvec; |
8071 | struct btrfs_retry_complete done; | |
8072 | u64 start; | |
8073 | u64 offset = 0; | |
2dabb324 CR |
8074 | u32 sectorsize; |
8075 | int nr_sectors; | |
8076 | unsigned int pgoff; | |
8077 | int csum_pos; | |
8b110e39 MX |
8078 | int i; |
8079 | int ret; | |
dc380aea | 8080 | |
2dabb324 | 8081 | fs_info = BTRFS_I(inode)->root->fs_info; |
da17066c | 8082 | sectorsize = fs_info->sectorsize; |
2dabb324 | 8083 | |
8b110e39 | 8084 | err = 0; |
c1dc0896 | 8085 | start = io_bio->logical; |
8b110e39 MX |
8086 | done.inode = inode; |
8087 | ||
c1dc0896 | 8088 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
2dabb324 CR |
8089 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
8090 | ||
8091 | pgoff = bvec->bv_offset; | |
8092 | next_block: | |
8093 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8094 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8095 | bvec->bv_page, pgoff, start, | |
8096 | sectorsize); | |
8b110e39 MX |
8097 | if (likely(!ret)) |
8098 | goto next; | |
8099 | try_again: | |
8100 | done.uptodate = 0; | |
8101 | done.start = start; | |
8102 | init_completion(&done.done); | |
8103 | ||
2dabb324 CR |
8104 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
8105 | pgoff, start, start + sectorsize - 1, | |
8106 | io_bio->mirror_num, | |
8107 | btrfs_retry_endio, &done); | |
8b110e39 MX |
8108 | if (ret) { |
8109 | err = ret; | |
8110 | goto next; | |
8111 | } | |
8112 | ||
8113 | wait_for_completion(&done.done); | |
8114 | ||
8115 | if (!done.uptodate) { | |
8116 | /* We might have another mirror, so try again */ | |
8117 | goto try_again; | |
8118 | } | |
8119 | next: | |
2dabb324 CR |
8120 | offset += sectorsize; |
8121 | start += sectorsize; | |
8122 | ||
8123 | ASSERT(nr_sectors); | |
8124 | ||
8125 | if (--nr_sectors) { | |
8126 | pgoff += sectorsize; | |
8127 | goto next_block; | |
8128 | } | |
2c30c71b | 8129 | } |
c1dc0896 MX |
8130 | |
8131 | return err; | |
8132 | } | |
8133 | ||
8b110e39 MX |
8134 | static int btrfs_subio_endio_read(struct inode *inode, |
8135 | struct btrfs_io_bio *io_bio, int err) | |
8136 | { | |
8137 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8138 | ||
8139 | if (skip_csum) { | |
8140 | if (unlikely(err)) | |
8141 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8142 | else | |
8143 | return 0; | |
8144 | } else { | |
8145 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8146 | } | |
8147 | } | |
8148 | ||
4246a0b6 | 8149 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8150 | { |
8151 | struct btrfs_dio_private *dip = bio->bi_private; | |
8152 | struct inode *inode = dip->inode; | |
8153 | struct bio *dio_bio; | |
8154 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 8155 | int err = bio->bi_error; |
c1dc0896 | 8156 | |
8b110e39 MX |
8157 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8158 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 8159 | |
4b46fce2 | 8160 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8161 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8162 | dio_bio = dip->dio_bio; |
4b46fce2 | 8163 | |
4b46fce2 | 8164 | kfree(dip); |
c0da7aa1 | 8165 | |
1636d1d7 | 8166 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8167 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
8168 | |
8169 | if (io_bio->end_io) | |
8170 | io_bio->end_io(io_bio, err); | |
9be3395b | 8171 | bio_put(bio); |
4b46fce2 JB |
8172 | } |
8173 | ||
14543774 FM |
8174 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
8175 | const u64 offset, | |
8176 | const u64 bytes, | |
8177 | const int uptodate) | |
4b46fce2 | 8178 | { |
0b246afa | 8179 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4b46fce2 | 8180 | struct btrfs_ordered_extent *ordered = NULL; |
14543774 FM |
8181 | u64 ordered_offset = offset; |
8182 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8183 | int ret; |
8184 | ||
163cf09c CM |
8185 | again: |
8186 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8187 | &ordered_offset, | |
4246a0b6 | 8188 | ordered_bytes, |
14543774 | 8189 | uptodate); |
4b46fce2 | 8190 | if (!ret) |
163cf09c | 8191 | goto out_test; |
4b46fce2 | 8192 | |
9e0af237 LB |
8193 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
8194 | finish_ordered_fn, NULL, NULL); | |
0b246afa | 8195 | btrfs_queue_work(fs_info->endio_write_workers, &ordered->work); |
163cf09c CM |
8196 | out_test: |
8197 | /* | |
8198 | * our bio might span multiple ordered extents. If we haven't | |
8199 | * completed the accounting for the whole dio, go back and try again | |
8200 | */ | |
14543774 FM |
8201 | if (ordered_offset < offset + bytes) { |
8202 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8203 | ordered = NULL; |
163cf09c CM |
8204 | goto again; |
8205 | } | |
14543774 FM |
8206 | } |
8207 | ||
8208 | static void btrfs_endio_direct_write(struct bio *bio) | |
8209 | { | |
8210 | struct btrfs_dio_private *dip = bio->bi_private; | |
8211 | struct bio *dio_bio = dip->dio_bio; | |
8212 | ||
8213 | btrfs_endio_direct_write_update_ordered(dip->inode, | |
8214 | dip->logical_offset, | |
8215 | dip->bytes, | |
8216 | !bio->bi_error); | |
4b46fce2 | 8217 | |
4b46fce2 | 8218 | kfree(dip); |
c0da7aa1 | 8219 | |
1636d1d7 | 8220 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8221 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 8222 | bio_put(bio); |
4b46fce2 JB |
8223 | } |
8224 | ||
81a75f67 | 8225 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
eaf25d93 CM |
8226 | struct bio *bio, int mirror_num, |
8227 | unsigned long bio_flags, u64 offset) | |
8228 | { | |
8229 | int ret; | |
2ff7e61e | 8230 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
79787eaa | 8231 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8232 | return 0; |
8233 | } | |
8234 | ||
4246a0b6 | 8235 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8236 | { |
8237 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 8238 | int err = bio->bi_error; |
e65e1535 | 8239 | |
8b110e39 MX |
8240 | if (err) |
8241 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8242 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
4a0cc7ca | 8243 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), bio->bi_opf, |
8b110e39 MX |
8244 | (unsigned long long)bio->bi_iter.bi_sector, |
8245 | bio->bi_iter.bi_size, err); | |
8246 | ||
8247 | if (dip->subio_endio) | |
8248 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8249 | |
8250 | if (err) { | |
e65e1535 MX |
8251 | dip->errors = 1; |
8252 | ||
8253 | /* | |
8254 | * before atomic variable goto zero, we must make sure | |
8255 | * dip->errors is perceived to be set. | |
8256 | */ | |
4e857c58 | 8257 | smp_mb__before_atomic(); |
e65e1535 MX |
8258 | } |
8259 | ||
8260 | /* if there are more bios still pending for this dio, just exit */ | |
8261 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8262 | goto out; | |
8263 | ||
9be3395b | 8264 | if (dip->errors) { |
e65e1535 | 8265 | bio_io_error(dip->orig_bio); |
9be3395b | 8266 | } else { |
4246a0b6 CH |
8267 | dip->dio_bio->bi_error = 0; |
8268 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
8269 | } |
8270 | out: | |
8271 | bio_put(bio); | |
8272 | } | |
8273 | ||
8274 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
8275 | u64 first_sector, gfp_t gfp_flags) | |
8276 | { | |
da2f0f74 | 8277 | struct bio *bio; |
22365979 | 8278 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
da2f0f74 CM |
8279 | if (bio) |
8280 | bio_associate_current(bio); | |
8281 | return bio; | |
e65e1535 MX |
8282 | } |
8283 | ||
2ff7e61e | 8284 | static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
c1dc0896 MX |
8285 | struct btrfs_dio_private *dip, |
8286 | struct bio *bio, | |
8287 | u64 file_offset) | |
8288 | { | |
8289 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8290 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8291 | int ret; | |
8292 | ||
8293 | /* | |
8294 | * We load all the csum data we need when we submit | |
8295 | * the first bio to reduce the csum tree search and | |
8296 | * contention. | |
8297 | */ | |
8298 | if (dip->logical_offset == file_offset) { | |
2ff7e61e | 8299 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
c1dc0896 MX |
8300 | file_offset); |
8301 | if (ret) | |
8302 | return ret; | |
8303 | } | |
8304 | ||
8305 | if (bio == dip->orig_bio) | |
8306 | return 0; | |
8307 | ||
8308 | file_offset -= dip->logical_offset; | |
8309 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8310 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8311 | ||
8312 | return 0; | |
8313 | } | |
8314 | ||
e65e1535 | 8315 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
81a75f67 | 8316 | u64 file_offset, int skip_sum, |
c329861d | 8317 | int async_submit) |
e65e1535 | 8318 | { |
0b246afa | 8319 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
facc8a22 | 8320 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8321 | bool write = bio_op(bio) == REQ_OP_WRITE; |
e65e1535 MX |
8322 | int ret; |
8323 | ||
b812ce28 JB |
8324 | if (async_submit) |
8325 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8326 | ||
e65e1535 | 8327 | bio_get(bio); |
5fd02043 JB |
8328 | |
8329 | if (!write) { | |
0b246afa | 8330 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
5fd02043 JB |
8331 | if (ret) |
8332 | goto err; | |
8333 | } | |
e65e1535 | 8334 | |
1ae39938 JB |
8335 | if (skip_sum) |
8336 | goto map; | |
8337 | ||
8338 | if (write && async_submit) { | |
0b246afa JM |
8339 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0, |
8340 | file_offset, | |
8341 | __btrfs_submit_bio_start_direct_io, | |
8342 | __btrfs_submit_bio_done); | |
e65e1535 | 8343 | goto err; |
1ae39938 JB |
8344 | } else if (write) { |
8345 | /* | |
8346 | * If we aren't doing async submit, calculate the csum of the | |
8347 | * bio now. | |
8348 | */ | |
2ff7e61e | 8349 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
1ae39938 JB |
8350 | if (ret) |
8351 | goto err; | |
23ea8e5a | 8352 | } else { |
2ff7e61e | 8353 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
c1dc0896 | 8354 | file_offset); |
c2db1073 TI |
8355 | if (ret) |
8356 | goto err; | |
8357 | } | |
1ae39938 | 8358 | map: |
2ff7e61e | 8359 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
e65e1535 MX |
8360 | err: |
8361 | bio_put(bio); | |
8362 | return ret; | |
8363 | } | |
8364 | ||
81a75f67 | 8365 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
e65e1535 MX |
8366 | int skip_sum) |
8367 | { | |
8368 | struct inode *inode = dip->inode; | |
0b246afa | 8369 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e65e1535 | 8370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e65e1535 MX |
8371 | struct bio *bio; |
8372 | struct bio *orig_bio = dip->orig_bio; | |
6a2de22f | 8373 | struct bio_vec *bvec; |
4f024f37 | 8374 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8375 | u64 file_offset = dip->logical_offset; |
8376 | u64 submit_len = 0; | |
8377 | u64 map_length; | |
0b246afa | 8378 | u32 blocksize = fs_info->sectorsize; |
1ae39938 | 8379 | int async_submit = 0; |
5f4dc8fc CR |
8380 | int nr_sectors; |
8381 | int ret; | |
6a2de22f | 8382 | int i, j; |
e65e1535 | 8383 | |
4f024f37 | 8384 | map_length = orig_bio->bi_iter.bi_size; |
0b246afa JM |
8385 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
8386 | &map_length, NULL, 0); | |
7a5c3c9b | 8387 | if (ret) |
e65e1535 | 8388 | return -EIO; |
facc8a22 | 8389 | |
4f024f37 | 8390 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8391 | bio = orig_bio; |
c1dc0896 | 8392 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8393 | goto submit; |
8394 | } | |
8395 | ||
53b381b3 | 8396 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8397 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8398 | async_submit = 0; |
8399 | else | |
8400 | async_submit = 1; | |
8401 | ||
02f57c7a JB |
8402 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8403 | if (!bio) | |
8404 | return -ENOMEM; | |
7a5c3c9b | 8405 | |
ef295ecf | 8406 | bio->bi_opf = orig_bio->bi_opf; |
02f57c7a JB |
8407 | bio->bi_private = dip; |
8408 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8409 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8410 | atomic_inc(&dip->pending_bios); |
8411 | ||
6a2de22f | 8412 | bio_for_each_segment_all(bvec, orig_bio, j) { |
0b246afa | 8413 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
5f4dc8fc CR |
8414 | i = 0; |
8415 | next_block: | |
8416 | if (unlikely(map_length < submit_len + blocksize || | |
8417 | bio_add_page(bio, bvec->bv_page, blocksize, | |
8418 | bvec->bv_offset + (i * blocksize)) < blocksize)) { | |
e65e1535 MX |
8419 | /* |
8420 | * inc the count before we submit the bio so | |
8421 | * we know the end IO handler won't happen before | |
8422 | * we inc the count. Otherwise, the dip might get freed | |
8423 | * before we're done setting it up | |
8424 | */ | |
8425 | atomic_inc(&dip->pending_bios); | |
81a75f67 | 8426 | ret = __btrfs_submit_dio_bio(bio, inode, |
e65e1535 | 8427 | file_offset, skip_sum, |
c329861d | 8428 | async_submit); |
e65e1535 MX |
8429 | if (ret) { |
8430 | bio_put(bio); | |
8431 | atomic_dec(&dip->pending_bios); | |
8432 | goto out_err; | |
8433 | } | |
8434 | ||
e65e1535 MX |
8435 | start_sector += submit_len >> 9; |
8436 | file_offset += submit_len; | |
8437 | ||
8438 | submit_len = 0; | |
e65e1535 MX |
8439 | |
8440 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8441 | start_sector, GFP_NOFS); | |
8442 | if (!bio) | |
8443 | goto out_err; | |
ef295ecf | 8444 | bio->bi_opf = orig_bio->bi_opf; |
e65e1535 MX |
8445 | bio->bi_private = dip; |
8446 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8447 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8448 | |
4f024f37 | 8449 | map_length = orig_bio->bi_iter.bi_size; |
0b246afa | 8450 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
3ec706c8 | 8451 | start_sector << 9, |
e65e1535 MX |
8452 | &map_length, NULL, 0); |
8453 | if (ret) { | |
8454 | bio_put(bio); | |
8455 | goto out_err; | |
8456 | } | |
5f4dc8fc CR |
8457 | |
8458 | goto next_block; | |
e65e1535 | 8459 | } else { |
5f4dc8fc CR |
8460 | submit_len += blocksize; |
8461 | if (--nr_sectors) { | |
8462 | i++; | |
8463 | goto next_block; | |
8464 | } | |
e65e1535 MX |
8465 | } |
8466 | } | |
8467 | ||
02f57c7a | 8468 | submit: |
81a75f67 | 8469 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
c329861d | 8470 | async_submit); |
e65e1535 MX |
8471 | if (!ret) |
8472 | return 0; | |
8473 | ||
8474 | bio_put(bio); | |
8475 | out_err: | |
8476 | dip->errors = 1; | |
8477 | /* | |
8478 | * before atomic variable goto zero, we must | |
8479 | * make sure dip->errors is perceived to be set. | |
8480 | */ | |
4e857c58 | 8481 | smp_mb__before_atomic(); |
e65e1535 MX |
8482 | if (atomic_dec_and_test(&dip->pending_bios)) |
8483 | bio_io_error(dip->orig_bio); | |
8484 | ||
8485 | /* bio_end_io() will handle error, so we needn't return it */ | |
8486 | return 0; | |
8487 | } | |
8488 | ||
8a4c1e42 MC |
8489 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8490 | loff_t file_offset) | |
4b46fce2 | 8491 | { |
61de718f FM |
8492 | struct btrfs_dio_private *dip = NULL; |
8493 | struct bio *io_bio = NULL; | |
23ea8e5a | 8494 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8495 | int skip_sum; |
8a4c1e42 | 8496 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8497 | int ret = 0; |
8498 | ||
8499 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8500 | ||
9be3395b | 8501 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8502 | if (!io_bio) { |
8503 | ret = -ENOMEM; | |
8504 | goto free_ordered; | |
8505 | } | |
8506 | ||
c1dc0896 | 8507 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8508 | if (!dip) { |
8509 | ret = -ENOMEM; | |
61de718f | 8510 | goto free_ordered; |
4b46fce2 | 8511 | } |
4b46fce2 | 8512 | |
9be3395b | 8513 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8514 | dip->inode = inode; |
8515 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8516 | dip->bytes = dio_bio->bi_iter.bi_size; |
8517 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8518 | io_bio->bi_private = dip; |
9be3395b CM |
8519 | dip->orig_bio = io_bio; |
8520 | dip->dio_bio = dio_bio; | |
e65e1535 | 8521 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8522 | btrfs_bio = btrfs_io_bio(io_bio); |
8523 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8524 | |
c1dc0896 | 8525 | if (write) { |
9be3395b | 8526 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8527 | } else { |
9be3395b | 8528 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8529 | dip->subio_endio = btrfs_subio_endio_read; |
8530 | } | |
4b46fce2 | 8531 | |
f28a4928 FM |
8532 | /* |
8533 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8534 | * even if we fail to submit a bio, because in such case we do the | |
8535 | * corresponding error handling below and it must not be done a second | |
8536 | * time by btrfs_direct_IO(). | |
8537 | */ | |
8538 | if (write) { | |
8539 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8540 | ||
8541 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8542 | dip->bytes; | |
8543 | dio_data->unsubmitted_oe_range_start = | |
8544 | dio_data->unsubmitted_oe_range_end; | |
8545 | } | |
8546 | ||
81a75f67 | 8547 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
e65e1535 | 8548 | if (!ret) |
eaf25d93 | 8549 | return; |
9be3395b | 8550 | |
23ea8e5a MX |
8551 | if (btrfs_bio->end_io) |
8552 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8553 | |
4b46fce2 JB |
8554 | free_ordered: |
8555 | /* | |
61de718f FM |
8556 | * If we arrived here it means either we failed to submit the dip |
8557 | * or we either failed to clone the dio_bio or failed to allocate the | |
8558 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8559 | * call bio_endio against our io_bio so that we get proper resource | |
8560 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8561 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8562 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8563 | */ |
61de718f | 8564 | if (io_bio && dip) { |
4246a0b6 CH |
8565 | io_bio->bi_error = -EIO; |
8566 | bio_endio(io_bio); | |
61de718f FM |
8567 | /* |
8568 | * The end io callbacks free our dip, do the final put on io_bio | |
8569 | * and all the cleanup and final put for dio_bio (through | |
8570 | * dio_end_io()). | |
8571 | */ | |
8572 | dip = NULL; | |
8573 | io_bio = NULL; | |
8574 | } else { | |
14543774 FM |
8575 | if (write) |
8576 | btrfs_endio_direct_write_update_ordered(inode, | |
8577 | file_offset, | |
8578 | dio_bio->bi_iter.bi_size, | |
8579 | 0); | |
8580 | else | |
61de718f FM |
8581 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8582 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8583 | |
4246a0b6 | 8584 | dio_bio->bi_error = -EIO; |
61de718f FM |
8585 | /* |
8586 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8587 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8588 | */ | |
8589 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8590 | } |
61de718f FM |
8591 | if (io_bio) |
8592 | bio_put(io_bio); | |
8593 | kfree(dip); | |
4b46fce2 JB |
8594 | } |
8595 | ||
2ff7e61e JM |
8596 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
8597 | struct kiocb *iocb, | |
8598 | const struct iov_iter *iter, loff_t offset) | |
5a5f79b5 CM |
8599 | { |
8600 | int seg; | |
a1b75f7d | 8601 | int i; |
0b246afa | 8602 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
5a5f79b5 | 8603 | ssize_t retval = -EINVAL; |
5a5f79b5 CM |
8604 | |
8605 | if (offset & blocksize_mask) | |
8606 | goto out; | |
8607 | ||
28060d5d AV |
8608 | if (iov_iter_alignment(iter) & blocksize_mask) |
8609 | goto out; | |
a1b75f7d | 8610 | |
28060d5d | 8611 | /* If this is a write we don't need to check anymore */ |
cd27e455 | 8612 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
28060d5d AV |
8613 | return 0; |
8614 | /* | |
8615 | * Check to make sure we don't have duplicate iov_base's in this | |
8616 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8617 | * when reading back. | |
8618 | */ | |
8619 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8620 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8621 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8622 | goto out; |
8623 | } | |
5a5f79b5 CM |
8624 | } |
8625 | retval = 0; | |
8626 | out: | |
8627 | return retval; | |
8628 | } | |
eb838e73 | 8629 | |
c8b8e32d | 8630 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8631 | { |
4b46fce2 JB |
8632 | struct file *file = iocb->ki_filp; |
8633 | struct inode *inode = file->f_mapping->host; | |
0b246afa | 8634 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
50745b0a | 8635 | struct btrfs_dio_data dio_data = { 0 }; |
c8b8e32d | 8636 | loff_t offset = iocb->ki_pos; |
0934856d | 8637 | size_t count = 0; |
2e60a51e | 8638 | int flags = 0; |
38851cc1 MX |
8639 | bool wakeup = true; |
8640 | bool relock = false; | |
0934856d | 8641 | ssize_t ret; |
4b46fce2 | 8642 | |
2ff7e61e | 8643 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
5a5f79b5 | 8644 | return 0; |
3f7c579c | 8645 | |
fe0f07d0 | 8646 | inode_dio_begin(inode); |
4e857c58 | 8647 | smp_mb__after_atomic(); |
38851cc1 | 8648 | |
0e267c44 | 8649 | /* |
41bd9ca4 MX |
8650 | * The generic stuff only does filemap_write_and_wait_range, which |
8651 | * isn't enough if we've written compressed pages to this area, so | |
8652 | * we need to flush the dirty pages again to make absolutely sure | |
8653 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8654 | */ |
a6cbcd4a | 8655 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8656 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8657 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8658 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8659 | offset + count - 1); | |
0e267c44 | 8660 | |
6f673763 | 8661 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8662 | /* |
8663 | * If the write DIO is beyond the EOF, we need update | |
8664 | * the isize, but it is protected by i_mutex. So we can | |
8665 | * not unlock the i_mutex at this case. | |
8666 | */ | |
8667 | if (offset + count <= inode->i_size) { | |
5955102c | 8668 | inode_unlock(inode); |
38851cc1 MX |
8669 | relock = true; |
8670 | } | |
7cf5b976 | 8671 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
0934856d | 8672 | if (ret) |
38851cc1 | 8673 | goto out; |
823bb20a | 8674 | dio_data.outstanding_extents = count_max_extents(count); |
e1cbbfa5 JB |
8675 | |
8676 | /* | |
8677 | * We need to know how many extents we reserved so that we can | |
8678 | * do the accounting properly if we go over the number we | |
8679 | * originally calculated. Abuse current->journal_info for this. | |
8680 | */ | |
da17066c | 8681 | dio_data.reserve = round_up(count, |
0b246afa | 8682 | fs_info->sectorsize); |
f28a4928 FM |
8683 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8684 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8685 | current->journal_info = &dio_data; |
97dcdea0 | 8686 | down_read(&BTRFS_I(inode)->dio_sem); |
ee39b432 DS |
8687 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8688 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8689 | inode_dio_end(inode); |
38851cc1 MX |
8690 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8691 | wakeup = false; | |
0934856d MX |
8692 | } |
8693 | ||
17f8c842 | 8694 | ret = __blockdev_direct_IO(iocb, inode, |
0b246afa | 8695 | fs_info->fs_devices->latest_bdev, |
c8b8e32d | 8696 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8697 | btrfs_submit_direct, flags); |
6f673763 | 8698 | if (iov_iter_rw(iter) == WRITE) { |
97dcdea0 | 8699 | up_read(&BTRFS_I(inode)->dio_sem); |
e1cbbfa5 | 8700 | current->journal_info = NULL; |
ddba1bfc | 8701 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8702 | if (dio_data.reserve) |
7cf5b976 QW |
8703 | btrfs_delalloc_release_space(inode, offset, |
8704 | dio_data.reserve); | |
f28a4928 FM |
8705 | /* |
8706 | * On error we might have left some ordered extents | |
8707 | * without submitting corresponding bios for them, so | |
8708 | * cleanup them up to avoid other tasks getting them | |
8709 | * and waiting for them to complete forever. | |
8710 | */ | |
8711 | if (dio_data.unsubmitted_oe_range_start < | |
8712 | dio_data.unsubmitted_oe_range_end) | |
8713 | btrfs_endio_direct_write_update_ordered(inode, | |
8714 | dio_data.unsubmitted_oe_range_start, | |
8715 | dio_data.unsubmitted_oe_range_end - | |
8716 | dio_data.unsubmitted_oe_range_start, | |
8717 | 0); | |
ddba1bfc | 8718 | } else if (ret >= 0 && (size_t)ret < count) |
7cf5b976 QW |
8719 | btrfs_delalloc_release_space(inode, offset, |
8720 | count - (size_t)ret); | |
0934856d | 8721 | } |
38851cc1 | 8722 | out: |
2e60a51e | 8723 | if (wakeup) |
fe0f07d0 | 8724 | inode_dio_end(inode); |
38851cc1 | 8725 | if (relock) |
5955102c | 8726 | inode_lock(inode); |
0934856d MX |
8727 | |
8728 | return ret; | |
16432985 CM |
8729 | } |
8730 | ||
05dadc09 TI |
8731 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8732 | ||
1506fcc8 YS |
8733 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8734 | __u64 start, __u64 len) | |
8735 | { | |
05dadc09 TI |
8736 | int ret; |
8737 | ||
8738 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8739 | if (ret) | |
8740 | return ret; | |
8741 | ||
ec29ed5b | 8742 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8743 | } |
8744 | ||
a52d9a80 | 8745 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8746 | { |
d1310b2e CM |
8747 | struct extent_io_tree *tree; |
8748 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8749 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8750 | } |
1832a6d5 | 8751 | |
a52d9a80 | 8752 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8753 | { |
d1310b2e | 8754 | struct extent_io_tree *tree; |
be7bd730 JB |
8755 | struct inode *inode = page->mapping->host; |
8756 | int ret; | |
b888db2b CM |
8757 | |
8758 | if (current->flags & PF_MEMALLOC) { | |
8759 | redirty_page_for_writepage(wbc, page); | |
8760 | unlock_page(page); | |
8761 | return 0; | |
8762 | } | |
be7bd730 JB |
8763 | |
8764 | /* | |
8765 | * If we are under memory pressure we will call this directly from the | |
8766 | * VM, we need to make sure we have the inode referenced for the ordered | |
8767 | * extent. If not just return like we didn't do anything. | |
8768 | */ | |
8769 | if (!igrab(inode)) { | |
8770 | redirty_page_for_writepage(wbc, page); | |
8771 | return AOP_WRITEPAGE_ACTIVATE; | |
8772 | } | |
d1310b2e | 8773 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8774 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8775 | btrfs_add_delayed_iput(inode); | |
8776 | return ret; | |
9ebefb18 CM |
8777 | } |
8778 | ||
48a3b636 ES |
8779 | static int btrfs_writepages(struct address_space *mapping, |
8780 | struct writeback_control *wbc) | |
b293f02e | 8781 | { |
d1310b2e | 8782 | struct extent_io_tree *tree; |
771ed689 | 8783 | |
d1310b2e | 8784 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8785 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8786 | } | |
8787 | ||
3ab2fb5a CM |
8788 | static int |
8789 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8790 | struct list_head *pages, unsigned nr_pages) | |
8791 | { | |
d1310b2e CM |
8792 | struct extent_io_tree *tree; |
8793 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8794 | return extent_readpages(tree, mapping, pages, nr_pages, |
8795 | btrfs_get_extent); | |
8796 | } | |
e6dcd2dc | 8797 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8798 | { |
d1310b2e CM |
8799 | struct extent_io_tree *tree; |
8800 | struct extent_map_tree *map; | |
a52d9a80 | 8801 | int ret; |
8c2383c3 | 8802 | |
d1310b2e CM |
8803 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8804 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8805 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8806 | if (ret == 1) { |
8807 | ClearPagePrivate(page); | |
8808 | set_page_private(page, 0); | |
09cbfeaf | 8809 | put_page(page); |
39279cc3 | 8810 | } |
a52d9a80 | 8811 | return ret; |
39279cc3 CM |
8812 | } |
8813 | ||
e6dcd2dc CM |
8814 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8815 | { | |
98509cfc CM |
8816 | if (PageWriteback(page) || PageDirty(page)) |
8817 | return 0; | |
3ba7ab22 | 8818 | return __btrfs_releasepage(page, gfp_flags); |
e6dcd2dc CM |
8819 | } |
8820 | ||
d47992f8 LC |
8821 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8822 | unsigned int length) | |
39279cc3 | 8823 | { |
5fd02043 | 8824 | struct inode *inode = page->mapping->host; |
d1310b2e | 8825 | struct extent_io_tree *tree; |
e6dcd2dc | 8826 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8827 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8828 | u64 page_start = page_offset(page); |
09cbfeaf | 8829 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8830 | u64 start; |
8831 | u64 end; | |
131e404a | 8832 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8833 | |
8b62b72b CM |
8834 | /* |
8835 | * we have the page locked, so new writeback can't start, | |
8836 | * and the dirty bit won't be cleared while we are here. | |
8837 | * | |
8838 | * Wait for IO on this page so that we can safely clear | |
8839 | * the PagePrivate2 bit and do ordered accounting | |
8840 | */ | |
e6dcd2dc | 8841 | wait_on_page_writeback(page); |
8b62b72b | 8842 | |
5fd02043 | 8843 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8844 | if (offset) { |
8845 | btrfs_releasepage(page, GFP_NOFS); | |
8846 | return; | |
8847 | } | |
131e404a FDBM |
8848 | |
8849 | if (!inode_evicting) | |
ff13db41 | 8850 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8851 | again: |
8852 | start = page_start; | |
8853 | ordered = btrfs_lookup_ordered_range(inode, start, | |
8854 | page_end - start + 1); | |
e6dcd2dc | 8855 | if (ordered) { |
dbfdb6d1 | 8856 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8857 | /* |
8858 | * IO on this page will never be started, so we need | |
8859 | * to account for any ordered extents now | |
8860 | */ | |
131e404a | 8861 | if (!inode_evicting) |
dbfdb6d1 | 8862 | clear_extent_bit(tree, start, end, |
131e404a FDBM |
8863 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8864 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8865 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8866 | GFP_NOFS); | |
8b62b72b CM |
8867 | /* |
8868 | * whoever cleared the private bit is responsible | |
8869 | * for the finish_ordered_io | |
8870 | */ | |
77cef2ec JB |
8871 | if (TestClearPagePrivate2(page)) { |
8872 | struct btrfs_ordered_inode_tree *tree; | |
8873 | u64 new_len; | |
8874 | ||
8875 | tree = &BTRFS_I(inode)->ordered_tree; | |
8876 | ||
8877 | spin_lock_irq(&tree->lock); | |
8878 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8879 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8880 | if (new_len < ordered->truncated_len) |
8881 | ordered->truncated_len = new_len; | |
8882 | spin_unlock_irq(&tree->lock); | |
8883 | ||
8884 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8885 | start, |
8886 | end - start + 1, 1)) | |
77cef2ec | 8887 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8888 | } |
e6dcd2dc | 8889 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8890 | if (!inode_evicting) { |
8891 | cached_state = NULL; | |
dbfdb6d1 | 8892 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8893 | &cached_state); |
8894 | } | |
dbfdb6d1 CR |
8895 | |
8896 | start = end + 1; | |
8897 | if (start < page_end) | |
8898 | goto again; | |
131e404a FDBM |
8899 | } |
8900 | ||
b9d0b389 QW |
8901 | /* |
8902 | * Qgroup reserved space handler | |
8903 | * Page here will be either | |
8904 | * 1) Already written to disk | |
8905 | * In this case, its reserved space is released from data rsv map | |
8906 | * and will be freed by delayed_ref handler finally. | |
8907 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8908 | * space. | |
8909 | * 2) Not written to disk | |
0b34c261 GR |
8910 | * This means the reserved space should be freed here. However, |
8911 | * if a truncate invalidates the page (by clearing PageDirty) | |
8912 | * and the page is accounted for while allocating extent | |
8913 | * in btrfs_check_data_free_space() we let delayed_ref to | |
8914 | * free the entire extent. | |
b9d0b389 | 8915 | */ |
0b34c261 GR |
8916 | if (PageDirty(page)) |
8917 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); | |
131e404a FDBM |
8918 | if (!inode_evicting) { |
8919 | clear_extent_bit(tree, page_start, page_end, | |
8920 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8921 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8922 | EXTENT_DEFRAG, 1, 1, | |
8923 | &cached_state, GFP_NOFS); | |
8924 | ||
8925 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8926 | } |
e6dcd2dc | 8927 | |
4a096752 | 8928 | ClearPageChecked(page); |
9ad6b7bc | 8929 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8930 | ClearPagePrivate(page); |
8931 | set_page_private(page, 0); | |
09cbfeaf | 8932 | put_page(page); |
9ad6b7bc | 8933 | } |
39279cc3 CM |
8934 | } |
8935 | ||
9ebefb18 CM |
8936 | /* |
8937 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8938 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8939 | * be careful to check for EOF conditions here. We set the page up correctly | |
8940 | * for a written page which means we get ENOSPC checking when writing into | |
8941 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8942 | * support these features. | |
8943 | * | |
8944 | * We are not allowed to take the i_mutex here so we have to play games to | |
8945 | * protect against truncate races as the page could now be beyond EOF. Because | |
8946 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8947 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8948 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8949 | * unlock the page. | |
8950 | */ | |
c2ec175c | 8951 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8952 | { |
c2ec175c | 8953 | struct page *page = vmf->page; |
496ad9aa | 8954 | struct inode *inode = file_inode(vma->vm_file); |
0b246afa | 8955 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc CM |
8956 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8957 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8958 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8959 | char *kaddr; |
8960 | unsigned long zero_start; | |
9ebefb18 | 8961 | loff_t size; |
1832a6d5 | 8962 | int ret; |
9998eb70 | 8963 | int reserved = 0; |
d0b7da88 | 8964 | u64 reserved_space; |
a52d9a80 | 8965 | u64 page_start; |
e6dcd2dc | 8966 | u64 page_end; |
d0b7da88 CR |
8967 | u64 end; |
8968 | ||
09cbfeaf | 8969 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8970 | |
b2b5ef5c | 8971 | sb_start_pagefault(inode->i_sb); |
df480633 | 8972 | page_start = page_offset(page); |
09cbfeaf | 8973 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8974 | end = page_end; |
df480633 | 8975 | |
d0b7da88 CR |
8976 | /* |
8977 | * Reserving delalloc space after obtaining the page lock can lead to | |
8978 | * deadlock. For example, if a dirty page is locked by this function | |
8979 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8980 | * dirty page write out, then the btrfs_writepage() function could | |
8981 | * end up waiting indefinitely to get a lock on the page currently | |
8982 | * being processed by btrfs_page_mkwrite() function. | |
8983 | */ | |
7cf5b976 | 8984 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
d0b7da88 | 8985 | reserved_space); |
9998eb70 | 8986 | if (!ret) { |
e41f941a | 8987 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8988 | reserved = 1; |
8989 | } | |
56a76f82 NP |
8990 | if (ret) { |
8991 | if (ret == -ENOMEM) | |
8992 | ret = VM_FAULT_OOM; | |
8993 | else /* -ENOSPC, -EIO, etc */ | |
8994 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8995 | if (reserved) |
8996 | goto out; | |
8997 | goto out_noreserve; | |
56a76f82 | 8998 | } |
1832a6d5 | 8999 | |
56a76f82 | 9000 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9001 | again: |
9ebefb18 | 9002 | lock_page(page); |
9ebefb18 | 9003 | size = i_size_read(inode); |
a52d9a80 | 9004 | |
9ebefb18 | 9005 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9006 | (page_start >= size)) { |
9ebefb18 CM |
9007 | /* page got truncated out from underneath us */ |
9008 | goto out_unlock; | |
9009 | } | |
e6dcd2dc CM |
9010 | wait_on_page_writeback(page); |
9011 | ||
ff13db41 | 9012 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9013 | set_page_extent_mapped(page); |
9014 | ||
eb84ae03 CM |
9015 | /* |
9016 | * we can't set the delalloc bits if there are pending ordered | |
9017 | * extents. Drop our locks and wait for them to finish | |
9018 | */ | |
d0b7da88 | 9019 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
e6dcd2dc | 9020 | if (ordered) { |
2ac55d41 JB |
9021 | unlock_extent_cached(io_tree, page_start, page_end, |
9022 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9023 | unlock_page(page); |
eb84ae03 | 9024 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9025 | btrfs_put_ordered_extent(ordered); |
9026 | goto again; | |
9027 | } | |
9028 | ||
09cbfeaf | 9029 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
da17066c | 9030 | reserved_space = round_up(size - page_start, |
0b246afa | 9031 | fs_info->sectorsize); |
09cbfeaf | 9032 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
9033 | end = page_start + reserved_space - 1; |
9034 | spin_lock(&BTRFS_I(inode)->lock); | |
9035 | BTRFS_I(inode)->outstanding_extents++; | |
9036 | spin_unlock(&BTRFS_I(inode)->lock); | |
9037 | btrfs_delalloc_release_space(inode, page_start, | |
09cbfeaf | 9038 | PAGE_SIZE - reserved_space); |
d0b7da88 CR |
9039 | } |
9040 | } | |
9041 | ||
fbf19087 JB |
9042 | /* |
9043 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
9044 | * if it was already dirty, so for space accounting reasons we need to | |
9045 | * clear any delalloc bits for the range we are fixing to save. There | |
9046 | * is probably a better way to do this, but for now keep consistent with | |
9047 | * prepare_pages in the normal write path. | |
9048 | */ | |
d0b7da88 | 9049 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9050 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9051 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9052 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9053 | |
d0b7da88 | 9054 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9055 | &cached_state, 0); |
9ed74f2d | 9056 | if (ret) { |
2ac55d41 JB |
9057 | unlock_extent_cached(io_tree, page_start, page_end, |
9058 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9059 | ret = VM_FAULT_SIGBUS; |
9060 | goto out_unlock; | |
9061 | } | |
e6dcd2dc | 9062 | ret = 0; |
9ebefb18 CM |
9063 | |
9064 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9065 | if (page_start + PAGE_SIZE > size) |
9066 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9067 | else |
09cbfeaf | 9068 | zero_start = PAGE_SIZE; |
9ebefb18 | 9069 | |
09cbfeaf | 9070 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9071 | kaddr = kmap(page); |
09cbfeaf | 9072 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9073 | flush_dcache_page(page); |
9074 | kunmap(page); | |
9075 | } | |
247e743c | 9076 | ClearPageChecked(page); |
e6dcd2dc | 9077 | set_page_dirty(page); |
50a9b214 | 9078 | SetPageUptodate(page); |
5a3f23d5 | 9079 | |
0b246afa | 9080 | BTRFS_I(inode)->last_trans = fs_info->generation; |
257c62e1 | 9081 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
46d8bc34 | 9082 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9083 | |
2ac55d41 | 9084 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9085 | |
9086 | out_unlock: | |
b2b5ef5c JK |
9087 | if (!ret) { |
9088 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 9089 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9090 | } |
9ebefb18 | 9091 | unlock_page(page); |
1832a6d5 | 9092 | out: |
d0b7da88 | 9093 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
9998eb70 | 9094 | out_noreserve: |
b2b5ef5c | 9095 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
9096 | return ret; |
9097 | } | |
9098 | ||
a41ad394 | 9099 | static int btrfs_truncate(struct inode *inode) |
39279cc3 | 9100 | { |
0b246afa | 9101 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 9102 | struct btrfs_root *root = BTRFS_I(inode)->root; |
fcb80c2a | 9103 | struct btrfs_block_rsv *rsv; |
a71754fc | 9104 | int ret = 0; |
3893e33b | 9105 | int err = 0; |
39279cc3 | 9106 | struct btrfs_trans_handle *trans; |
0b246afa JM |
9107 | u64 mask = fs_info->sectorsize - 1; |
9108 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); | |
39279cc3 | 9109 | |
0ef8b726 JB |
9110 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9111 | (u64)-1); | |
9112 | if (ret) | |
9113 | return ret; | |
39279cc3 | 9114 | |
fcb80c2a | 9115 | /* |
01327610 | 9116 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9117 | * 3 things going on here |
9118 | * | |
9119 | * 1) We need to reserve space for our orphan item and the space to | |
9120 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9121 | * orphan item because we didn't reserve space to remove it. | |
9122 | * | |
9123 | * 2) We need to reserve space to update our inode. | |
9124 | * | |
9125 | * 3) We need to have something to cache all the space that is going to | |
9126 | * be free'd up by the truncate operation, but also have some slack | |
9127 | * space reserved in case it uses space during the truncate (thank you | |
9128 | * very much snapshotting). | |
9129 | * | |
01327610 | 9130 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9131 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9132 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9133 | * doesn't end up using space reserved for updating the inode or |
9134 | * removing the orphan item. We also need to be able to stop the | |
9135 | * transaction and start a new one, which means we need to be able to | |
9136 | * update the inode several times, and we have no idea of knowing how | |
9137 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9138 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9139 | * Then there is the orphan item, which does indeed need to be held on |
9140 | * to for the whole operation, and we need nobody to touch this reserved | |
9141 | * space except the orphan code. | |
9142 | * | |
9143 | * So that leaves us with | |
9144 | * | |
9145 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9146 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9147 | * transaction reservation. | |
9148 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9149 | * updating the inode. | |
9150 | */ | |
2ff7e61e | 9151 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9152 | if (!rsv) |
9153 | return -ENOMEM; | |
4a338542 | 9154 | rsv->size = min_size; |
ca7e70f5 | 9155 | rsv->failfast = 1; |
f0cd846e | 9156 | |
907cbceb | 9157 | /* |
07127184 | 9158 | * 1 for the truncate slack space |
907cbceb JB |
9159 | * 1 for updating the inode. |
9160 | */ | |
f3fe820c | 9161 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9162 | if (IS_ERR(trans)) { |
9163 | err = PTR_ERR(trans); | |
9164 | goto out; | |
9165 | } | |
f0cd846e | 9166 | |
907cbceb | 9167 | /* Migrate the slack space for the truncate to our reserve */ |
0b246afa | 9168 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
25d609f8 | 9169 | min_size, 0); |
fcb80c2a | 9170 | BUG_ON(ret); |
f0cd846e | 9171 | |
5dc562c5 JB |
9172 | /* |
9173 | * So if we truncate and then write and fsync we normally would just | |
9174 | * write the extents that changed, which is a problem if we need to | |
9175 | * first truncate that entire inode. So set this flag so we write out | |
9176 | * all of the extents in the inode to the sync log so we're completely | |
9177 | * safe. | |
9178 | */ | |
9179 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9180 | trans->block_rsv = rsv; |
907cbceb | 9181 | |
8082510e YZ |
9182 | while (1) { |
9183 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9184 | inode->i_size, | |
9185 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9186 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9187 | err = ret; |
8082510e | 9188 | break; |
3893e33b | 9189 | } |
39279cc3 | 9190 | |
0b246afa | 9191 | trans->block_rsv = &fs_info->trans_block_rsv; |
8082510e | 9192 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9193 | if (ret) { |
9194 | err = ret; | |
9195 | break; | |
9196 | } | |
ca7e70f5 | 9197 | |
3a45bb20 | 9198 | btrfs_end_transaction(trans); |
2ff7e61e | 9199 | btrfs_btree_balance_dirty(fs_info); |
ca7e70f5 JB |
9200 | |
9201 | trans = btrfs_start_transaction(root, 2); | |
9202 | if (IS_ERR(trans)) { | |
9203 | ret = err = PTR_ERR(trans); | |
9204 | trans = NULL; | |
9205 | break; | |
9206 | } | |
9207 | ||
47b5d646 | 9208 | btrfs_block_rsv_release(fs_info, rsv, -1); |
0b246afa | 9209 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
25d609f8 | 9210 | rsv, min_size, 0); |
ca7e70f5 JB |
9211 | BUG_ON(ret); /* shouldn't happen */ |
9212 | trans->block_rsv = rsv; | |
8082510e YZ |
9213 | } |
9214 | ||
9215 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9216 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 9217 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
9218 | if (ret) |
9219 | err = ret; | |
8082510e YZ |
9220 | } |
9221 | ||
917c16b2 | 9222 | if (trans) { |
0b246afa | 9223 | trans->block_rsv = &fs_info->trans_block_rsv; |
917c16b2 CM |
9224 | ret = btrfs_update_inode(trans, root, inode); |
9225 | if (ret && !err) | |
9226 | err = ret; | |
7b128766 | 9227 | |
3a45bb20 | 9228 | ret = btrfs_end_transaction(trans); |
2ff7e61e | 9229 | btrfs_btree_balance_dirty(fs_info); |
917c16b2 | 9230 | } |
fcb80c2a | 9231 | out: |
2ff7e61e | 9232 | btrfs_free_block_rsv(fs_info, rsv); |
fcb80c2a | 9233 | |
3893e33b JB |
9234 | if (ret && !err) |
9235 | err = ret; | |
a41ad394 | 9236 | |
3893e33b | 9237 | return err; |
39279cc3 CM |
9238 | } |
9239 | ||
d352ac68 CM |
9240 | /* |
9241 | * create a new subvolume directory/inode (helper for the ioctl). | |
9242 | */ | |
d2fb3437 | 9243 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9244 | struct btrfs_root *new_root, |
9245 | struct btrfs_root *parent_root, | |
9246 | u64 new_dirid) | |
39279cc3 | 9247 | { |
39279cc3 | 9248 | struct inode *inode; |
76dda93c | 9249 | int err; |
00e4e6b3 | 9250 | u64 index = 0; |
39279cc3 | 9251 | |
12fc9d09 FA |
9252 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9253 | new_dirid, new_dirid, | |
9254 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9255 | &index); | |
54aa1f4d | 9256 | if (IS_ERR(inode)) |
f46b5a66 | 9257 | return PTR_ERR(inode); |
39279cc3 CM |
9258 | inode->i_op = &btrfs_dir_inode_operations; |
9259 | inode->i_fop = &btrfs_dir_file_operations; | |
9260 | ||
bfe86848 | 9261 | set_nlink(inode, 1); |
dbe674a9 | 9262 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 9263 | unlock_new_inode(inode); |
3b96362c | 9264 | |
63541927 FDBM |
9265 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9266 | if (err) | |
9267 | btrfs_err(new_root->fs_info, | |
351fd353 | 9268 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9269 | new_root->root_key.objectid, err); |
9270 | ||
76dda93c | 9271 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9272 | |
76dda93c | 9273 | iput(inode); |
ce598979 | 9274 | return err; |
39279cc3 CM |
9275 | } |
9276 | ||
39279cc3 CM |
9277 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9278 | { | |
9279 | struct btrfs_inode *ei; | |
2ead6ae7 | 9280 | struct inode *inode; |
39279cc3 CM |
9281 | |
9282 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9283 | if (!ei) | |
9284 | return NULL; | |
2ead6ae7 YZ |
9285 | |
9286 | ei->root = NULL; | |
2ead6ae7 | 9287 | ei->generation = 0; |
15ee9bc7 | 9288 | ei->last_trans = 0; |
257c62e1 | 9289 | ei->last_sub_trans = 0; |
e02119d5 | 9290 | ei->logged_trans = 0; |
2ead6ae7 | 9291 | ei->delalloc_bytes = 0; |
47059d93 | 9292 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9293 | ei->disk_i_size = 0; |
9294 | ei->flags = 0; | |
7709cde3 | 9295 | ei->csum_bytes = 0; |
2ead6ae7 | 9296 | ei->index_cnt = (u64)-1; |
67de1176 | 9297 | ei->dir_index = 0; |
2ead6ae7 | 9298 | ei->last_unlink_trans = 0; |
46d8bc34 | 9299 | ei->last_log_commit = 0; |
8089fe62 | 9300 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9301 | |
9e0baf60 JB |
9302 | spin_lock_init(&ei->lock); |
9303 | ei->outstanding_extents = 0; | |
9304 | ei->reserved_extents = 0; | |
2ead6ae7 | 9305 | |
72ac3c0d | 9306 | ei->runtime_flags = 0; |
261507a0 | 9307 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9308 | |
16cdcec7 MX |
9309 | ei->delayed_node = NULL; |
9310 | ||
9cc97d64 | 9311 | ei->i_otime.tv_sec = 0; |
9312 | ei->i_otime.tv_nsec = 0; | |
9313 | ||
2ead6ae7 | 9314 | inode = &ei->vfs_inode; |
a8067e02 | 9315 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
9316 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
9317 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
9318 | ei->io_tree.track_uptodate = 1; |
9319 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9320 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9321 | mutex_init(&ei->log_mutex); |
f248679e | 9322 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9323 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9324 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9325 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9326 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9327 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9328 | |
9329 | return inode; | |
39279cc3 CM |
9330 | } |
9331 | ||
aaedb55b JB |
9332 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9333 | void btrfs_test_destroy_inode(struct inode *inode) | |
9334 | { | |
9335 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
9336 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9337 | } | |
9338 | #endif | |
9339 | ||
fa0d7e3d NP |
9340 | static void btrfs_i_callback(struct rcu_head *head) |
9341 | { | |
9342 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9343 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9344 | } | |
9345 | ||
39279cc3 CM |
9346 | void btrfs_destroy_inode(struct inode *inode) |
9347 | { | |
0b246afa | 9348 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
e6dcd2dc | 9349 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9350 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9351 | ||
b3d9b7a3 | 9352 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9353 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9354 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9355 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
9356 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
9357 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 9358 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9359 | |
a6dbd429 JB |
9360 | /* |
9361 | * This can happen where we create an inode, but somebody else also | |
9362 | * created the same inode and we need to destroy the one we already | |
9363 | * created. | |
9364 | */ | |
9365 | if (!root) | |
9366 | goto free; | |
9367 | ||
8a35d95f JB |
9368 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9369 | &BTRFS_I(inode)->runtime_flags)) { | |
0b246afa | 9370 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
4a0cc7ca | 9371 | btrfs_ino(BTRFS_I(inode))); |
8a35d95f | 9372 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9373 | } |
7b128766 | 9374 | |
d397712b | 9375 | while (1) { |
e6dcd2dc CM |
9376 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9377 | if (!ordered) | |
9378 | break; | |
9379 | else { | |
0b246afa | 9380 | btrfs_err(fs_info, |
5d163e0e JM |
9381 | "found ordered extent %llu %llu on inode cleanup", |
9382 | ordered->file_offset, ordered->len); | |
e6dcd2dc CM |
9383 | btrfs_remove_ordered_extent(inode, ordered); |
9384 | btrfs_put_ordered_extent(ordered); | |
9385 | btrfs_put_ordered_extent(ordered); | |
9386 | } | |
9387 | } | |
56fa9d07 | 9388 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9389 | inode_tree_del(inode); |
5b21f2ed | 9390 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9391 | free: |
fa0d7e3d | 9392 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9393 | } |
9394 | ||
45321ac5 | 9395 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9396 | { |
9397 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9398 | |
6379ef9f NA |
9399 | if (root == NULL) |
9400 | return 1; | |
9401 | ||
fa6ac876 | 9402 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9403 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9404 | return 1; |
76dda93c | 9405 | else |
45321ac5 | 9406 | return generic_drop_inode(inode); |
76dda93c YZ |
9407 | } |
9408 | ||
0ee0fda0 | 9409 | static void init_once(void *foo) |
39279cc3 CM |
9410 | { |
9411 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9412 | ||
9413 | inode_init_once(&ei->vfs_inode); | |
9414 | } | |
9415 | ||
9416 | void btrfs_destroy_cachep(void) | |
9417 | { | |
8c0a8537 KS |
9418 | /* |
9419 | * Make sure all delayed rcu free inodes are flushed before we | |
9420 | * destroy cache. | |
9421 | */ | |
9422 | rcu_barrier(); | |
5598e900 KM |
9423 | kmem_cache_destroy(btrfs_inode_cachep); |
9424 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9425 | kmem_cache_destroy(btrfs_transaction_cachep); | |
9426 | kmem_cache_destroy(btrfs_path_cachep); | |
9427 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9428 | } |
9429 | ||
9430 | int btrfs_init_cachep(void) | |
9431 | { | |
837e1972 | 9432 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9433 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9434 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9435 | init_once); | |
39279cc3 CM |
9436 | if (!btrfs_inode_cachep) |
9437 | goto fail; | |
9601e3f6 | 9438 | |
837e1972 | 9439 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9440 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9441 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9442 | if (!btrfs_trans_handle_cachep) |
9443 | goto fail; | |
9601e3f6 | 9444 | |
837e1972 | 9445 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 | 9446 | sizeof(struct btrfs_transaction), 0, |
fba4b697 | 9447 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9448 | if (!btrfs_transaction_cachep) |
9449 | goto fail; | |
9601e3f6 | 9450 | |
837e1972 | 9451 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9452 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9453 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9454 | if (!btrfs_path_cachep) |
9455 | goto fail; | |
9601e3f6 | 9456 | |
837e1972 | 9457 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9458 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9459 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9460 | if (!btrfs_free_space_cachep) |
9461 | goto fail; | |
9462 | ||
39279cc3 CM |
9463 | return 0; |
9464 | fail: | |
9465 | btrfs_destroy_cachep(); | |
9466 | return -ENOMEM; | |
9467 | } | |
9468 | ||
9469 | static int btrfs_getattr(struct vfsmount *mnt, | |
9470 | struct dentry *dentry, struct kstat *stat) | |
9471 | { | |
df0af1a5 | 9472 | u64 delalloc_bytes; |
2b0143b5 | 9473 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9474 | u32 blocksize = inode->i_sb->s_blocksize; |
9475 | ||
39279cc3 | 9476 | generic_fillattr(inode, stat); |
0ee5dc67 | 9477 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9478 | |
9479 | spin_lock(&BTRFS_I(inode)->lock); | |
9480 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9481 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9482 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9483 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9484 | return 0; |
9485 | } | |
9486 | ||
cdd1fedf DF |
9487 | static int btrfs_rename_exchange(struct inode *old_dir, |
9488 | struct dentry *old_dentry, | |
9489 | struct inode *new_dir, | |
9490 | struct dentry *new_dentry) | |
9491 | { | |
0b246afa | 9492 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
cdd1fedf DF |
9493 | struct btrfs_trans_handle *trans; |
9494 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9495 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9496 | struct inode *new_inode = new_dentry->d_inode; | |
9497 | struct inode *old_inode = old_dentry->d_inode; | |
c2050a45 | 9498 | struct timespec ctime = current_time(old_inode); |
cdd1fedf | 9499 | struct dentry *parent; |
4a0cc7ca NB |
9500 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
9501 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); | |
cdd1fedf DF |
9502 | u64 old_idx = 0; |
9503 | u64 new_idx = 0; | |
9504 | u64 root_objectid; | |
9505 | int ret; | |
86e8aa0e FM |
9506 | bool root_log_pinned = false; |
9507 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9508 | |
9509 | /* we only allow rename subvolume link between subvolumes */ | |
9510 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9511 | return -EXDEV; | |
9512 | ||
9513 | /* close the race window with snapshot create/destroy ioctl */ | |
9514 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9515 | down_read(&fs_info->subvol_sem); |
cdd1fedf | 9516 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9517 | down_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9518 | |
9519 | /* | |
9520 | * We want to reserve the absolute worst case amount of items. So if | |
9521 | * both inodes are subvols and we need to unlink them then that would | |
9522 | * require 4 item modifications, but if they are both normal inodes it | |
9523 | * would require 5 item modifications, so we'll assume their normal | |
9524 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9525 | * should cover the worst case number of items we'll modify. | |
9526 | */ | |
9527 | trans = btrfs_start_transaction(root, 12); | |
9528 | if (IS_ERR(trans)) { | |
9529 | ret = PTR_ERR(trans); | |
9530 | goto out_notrans; | |
9531 | } | |
9532 | ||
9533 | /* | |
9534 | * We need to find a free sequence number both in the source and | |
9535 | * in the destination directory for the exchange. | |
9536 | */ | |
9537 | ret = btrfs_set_inode_index(new_dir, &old_idx); | |
9538 | if (ret) | |
9539 | goto out_fail; | |
9540 | ret = btrfs_set_inode_index(old_dir, &new_idx); | |
9541 | if (ret) | |
9542 | goto out_fail; | |
9543 | ||
9544 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9545 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9546 | ||
9547 | /* Reference for the source. */ | |
9548 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9549 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9550 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9551 | } else { |
376e5a57 FM |
9552 | btrfs_pin_log_trans(root); |
9553 | root_log_pinned = true; | |
cdd1fedf DF |
9554 | ret = btrfs_insert_inode_ref(trans, dest, |
9555 | new_dentry->d_name.name, | |
9556 | new_dentry->d_name.len, | |
9557 | old_ino, | |
4a0cc7ca | 9558 | btrfs_ino(BTRFS_I(new_dir)), old_idx); |
cdd1fedf DF |
9559 | if (ret) |
9560 | goto out_fail; | |
cdd1fedf DF |
9561 | } |
9562 | ||
9563 | /* And now for the dest. */ | |
9564 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9565 | /* force full log commit if subvolume involved. */ | |
0b246afa | 9566 | btrfs_set_log_full_commit(fs_info, trans); |
cdd1fedf | 9567 | } else { |
376e5a57 FM |
9568 | btrfs_pin_log_trans(dest); |
9569 | dest_log_pinned = true; | |
cdd1fedf DF |
9570 | ret = btrfs_insert_inode_ref(trans, root, |
9571 | old_dentry->d_name.name, | |
9572 | old_dentry->d_name.len, | |
9573 | new_ino, | |
4a0cc7ca | 9574 | btrfs_ino(BTRFS_I(old_dir)), new_idx); |
cdd1fedf DF |
9575 | if (ret) |
9576 | goto out_fail; | |
cdd1fedf DF |
9577 | } |
9578 | ||
9579 | /* Update inode version and ctime/mtime. */ | |
9580 | inode_inc_iversion(old_dir); | |
9581 | inode_inc_iversion(new_dir); | |
9582 | inode_inc_iversion(old_inode); | |
9583 | inode_inc_iversion(new_inode); | |
9584 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9585 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9586 | old_inode->i_ctime = ctime; | |
9587 | new_inode->i_ctime = ctime; | |
9588 | ||
9589 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
4176bdbf NB |
9590 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), BTRFS_I(old_inode), 1); |
9591 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), BTRFS_I(new_inode), 1); | |
cdd1fedf DF |
9592 | } |
9593 | ||
9594 | /* src is a subvolume */ | |
9595 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9596 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9597 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9598 | root_objectid, | |
9599 | old_dentry->d_name.name, | |
9600 | old_dentry->d_name.len); | |
9601 | } else { /* src is an inode */ | |
9602 | ret = __btrfs_unlink_inode(trans, root, old_dir, | |
9603 | old_dentry->d_inode, | |
9604 | old_dentry->d_name.name, | |
9605 | old_dentry->d_name.len); | |
9606 | if (!ret) | |
9607 | ret = btrfs_update_inode(trans, root, old_inode); | |
9608 | } | |
9609 | if (ret) { | |
66642832 | 9610 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9611 | goto out_fail; |
9612 | } | |
9613 | ||
9614 | /* dest is a subvolume */ | |
9615 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9616 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9617 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9618 | root_objectid, | |
9619 | new_dentry->d_name.name, | |
9620 | new_dentry->d_name.len); | |
9621 | } else { /* dest is an inode */ | |
9622 | ret = __btrfs_unlink_inode(trans, dest, new_dir, | |
9623 | new_dentry->d_inode, | |
9624 | new_dentry->d_name.name, | |
9625 | new_dentry->d_name.len); | |
9626 | if (!ret) | |
9627 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9628 | } | |
9629 | if (ret) { | |
66642832 | 9630 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9631 | goto out_fail; |
9632 | } | |
9633 | ||
9634 | ret = btrfs_add_link(trans, new_dir, old_inode, | |
9635 | new_dentry->d_name.name, | |
9636 | new_dentry->d_name.len, 0, old_idx); | |
9637 | if (ret) { | |
66642832 | 9638 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9639 | goto out_fail; |
9640 | } | |
9641 | ||
9642 | ret = btrfs_add_link(trans, old_dir, new_inode, | |
9643 | old_dentry->d_name.name, | |
9644 | old_dentry->d_name.len, 0, new_idx); | |
9645 | if (ret) { | |
66642832 | 9646 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9647 | goto out_fail; |
9648 | } | |
9649 | ||
9650 | if (old_inode->i_nlink == 1) | |
9651 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9652 | if (new_inode->i_nlink == 1) | |
9653 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9654 | ||
86e8aa0e | 9655 | if (root_log_pinned) { |
cdd1fedf | 9656 | parent = new_dentry->d_parent; |
9ca5fbfb | 9657 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), parent); |
cdd1fedf | 9658 | btrfs_end_log_trans(root); |
86e8aa0e | 9659 | root_log_pinned = false; |
cdd1fedf | 9660 | } |
86e8aa0e | 9661 | if (dest_log_pinned) { |
cdd1fedf | 9662 | parent = old_dentry->d_parent; |
9ca5fbfb | 9663 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), parent); |
cdd1fedf | 9664 | btrfs_end_log_trans(dest); |
86e8aa0e | 9665 | dest_log_pinned = false; |
cdd1fedf DF |
9666 | } |
9667 | out_fail: | |
86e8aa0e FM |
9668 | /* |
9669 | * If we have pinned a log and an error happened, we unpin tasks | |
9670 | * trying to sync the log and force them to fallback to a transaction | |
9671 | * commit if the log currently contains any of the inodes involved in | |
9672 | * this rename operation (to ensure we do not persist a log with an | |
9673 | * inconsistent state for any of these inodes or leading to any | |
9674 | * inconsistencies when replayed). If the transaction was aborted, the | |
9675 | * abortion reason is propagated to userspace when attempting to commit | |
9676 | * the transaction. If the log does not contain any of these inodes, we | |
9677 | * allow the tasks to sync it. | |
9678 | */ | |
9679 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
0f8939b8 NB |
9680 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9681 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9682 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
86e8aa0e | 9683 | (new_inode && |
0f8939b8 | 9684 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9685 | btrfs_set_log_full_commit(fs_info, trans); |
86e8aa0e FM |
9686 | |
9687 | if (root_log_pinned) { | |
9688 | btrfs_end_log_trans(root); | |
9689 | root_log_pinned = false; | |
9690 | } | |
9691 | if (dest_log_pinned) { | |
9692 | btrfs_end_log_trans(dest); | |
9693 | dest_log_pinned = false; | |
9694 | } | |
9695 | } | |
3a45bb20 | 9696 | ret = btrfs_end_transaction(trans); |
cdd1fedf DF |
9697 | out_notrans: |
9698 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
0b246afa | 9699 | up_read(&fs_info->subvol_sem); |
cdd1fedf | 9700 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9701 | up_read(&fs_info->subvol_sem); |
cdd1fedf DF |
9702 | |
9703 | return ret; | |
9704 | } | |
9705 | ||
9706 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9707 | struct btrfs_root *root, | |
9708 | struct inode *dir, | |
9709 | struct dentry *dentry) | |
9710 | { | |
9711 | int ret; | |
9712 | struct inode *inode; | |
9713 | u64 objectid; | |
9714 | u64 index; | |
9715 | ||
9716 | ret = btrfs_find_free_ino(root, &objectid); | |
9717 | if (ret) | |
9718 | return ret; | |
9719 | ||
9720 | inode = btrfs_new_inode(trans, root, dir, | |
9721 | dentry->d_name.name, | |
9722 | dentry->d_name.len, | |
4a0cc7ca | 9723 | btrfs_ino(BTRFS_I(dir)), |
cdd1fedf DF |
9724 | objectid, |
9725 | S_IFCHR | WHITEOUT_MODE, | |
9726 | &index); | |
9727 | ||
9728 | if (IS_ERR(inode)) { | |
9729 | ret = PTR_ERR(inode); | |
9730 | return ret; | |
9731 | } | |
9732 | ||
9733 | inode->i_op = &btrfs_special_inode_operations; | |
9734 | init_special_inode(inode, inode->i_mode, | |
9735 | WHITEOUT_DEV); | |
9736 | ||
9737 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9738 | &dentry->d_name); | |
9739 | if (ret) | |
c9901618 | 9740 | goto out; |
cdd1fedf DF |
9741 | |
9742 | ret = btrfs_add_nondir(trans, dir, dentry, | |
9743 | inode, 0, index); | |
9744 | if (ret) | |
c9901618 | 9745 | goto out; |
cdd1fedf DF |
9746 | |
9747 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9748 | out: |
cdd1fedf | 9749 | unlock_new_inode(inode); |
c9901618 FM |
9750 | if (ret) |
9751 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9752 | iput(inode); |
9753 | ||
c9901618 | 9754 | return ret; |
cdd1fedf DF |
9755 | } |
9756 | ||
d397712b | 9757 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9758 | struct inode *new_dir, struct dentry *new_dentry, |
9759 | unsigned int flags) | |
39279cc3 | 9760 | { |
0b246afa | 9761 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
39279cc3 | 9762 | struct btrfs_trans_handle *trans; |
5062af35 | 9763 | unsigned int trans_num_items; |
39279cc3 | 9764 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9765 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9766 | struct inode *new_inode = d_inode(new_dentry); |
9767 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9768 | u64 index = 0; |
4df27c4d | 9769 | u64 root_objectid; |
39279cc3 | 9770 | int ret; |
4a0cc7ca | 9771 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
3dc9e8f7 | 9772 | bool log_pinned = false; |
39279cc3 | 9773 | |
4a0cc7ca | 9774 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9775 | return -EPERM; |
9776 | ||
4df27c4d | 9777 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9778 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9779 | return -EXDEV; |
9780 | ||
33345d01 | 9781 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
4a0cc7ca | 9782 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
39279cc3 | 9783 | return -ENOTEMPTY; |
5f39d397 | 9784 | |
4df27c4d YZ |
9785 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9786 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9787 | return -ENOTEMPTY; | |
9c52057c CM |
9788 | |
9789 | ||
9790 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9791 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9792 | new_dentry->d_name.name, |
9793 | new_dentry->d_name.len); | |
9794 | ||
9795 | if (ret) { | |
9796 | if (ret == -EEXIST) { | |
9797 | /* we shouldn't get | |
9798 | * eexist without a new_inode */ | |
fae7f21c | 9799 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9800 | return ret; |
9801 | } | |
9802 | } else { | |
9803 | /* maybe -EOVERFLOW */ | |
9804 | return ret; | |
9805 | } | |
9806 | } | |
9807 | ret = 0; | |
9808 | ||
5a3f23d5 | 9809 | /* |
8d875f95 CM |
9810 | * we're using rename to replace one file with another. Start IO on it |
9811 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9812 | */ |
8d875f95 | 9813 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9814 | filemap_flush(old_inode->i_mapping); |
9815 | ||
76dda93c | 9816 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9817 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9818 | down_read(&fs_info->subvol_sem); |
a22285a6 YZ |
9819 | /* |
9820 | * We want to reserve the absolute worst case amount of items. So if | |
9821 | * both inodes are subvols and we need to unlink them then that would | |
9822 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9823 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9824 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9825 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9826 | * If our rename has the whiteout flag, we need more 5 units for the |
9827 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9828 | * when selinux is enabled). | |
a22285a6 | 9829 | */ |
5062af35 FM |
9830 | trans_num_items = 11; |
9831 | if (flags & RENAME_WHITEOUT) | |
9832 | trans_num_items += 5; | |
9833 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9834 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9835 | ret = PTR_ERR(trans); |
9836 | goto out_notrans; | |
9837 | } | |
76dda93c | 9838 | |
4df27c4d YZ |
9839 | if (dest != root) |
9840 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9841 | |
a5719521 YZ |
9842 | ret = btrfs_set_inode_index(new_dir, &index); |
9843 | if (ret) | |
9844 | goto out_fail; | |
5a3f23d5 | 9845 | |
67de1176 | 9846 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9847 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9848 | /* force full log commit if subvolume involved. */ |
0b246afa | 9849 | btrfs_set_log_full_commit(fs_info, trans); |
4df27c4d | 9850 | } else { |
c4aba954 FM |
9851 | btrfs_pin_log_trans(root); |
9852 | log_pinned = true; | |
a5719521 YZ |
9853 | ret = btrfs_insert_inode_ref(trans, dest, |
9854 | new_dentry->d_name.name, | |
9855 | new_dentry->d_name.len, | |
33345d01 | 9856 | old_ino, |
4a0cc7ca | 9857 | btrfs_ino(BTRFS_I(new_dir)), index); |
a5719521 YZ |
9858 | if (ret) |
9859 | goto out_fail; | |
4df27c4d | 9860 | } |
5a3f23d5 | 9861 | |
0c4d2d95 JB |
9862 | inode_inc_iversion(old_dir); |
9863 | inode_inc_iversion(new_dir); | |
9864 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9865 | old_dir->i_ctime = old_dir->i_mtime = |
9866 | new_dir->i_ctime = new_dir->i_mtime = | |
c2050a45 | 9867 | old_inode->i_ctime = current_time(old_dir); |
5f39d397 | 9868 | |
12fcfd22 | 9869 | if (old_dentry->d_parent != new_dentry->d_parent) |
4176bdbf | 9870 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), BTRFS_I(old_inode), 1); |
12fcfd22 | 9871 | |
33345d01 | 9872 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9873 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9874 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9875 | old_dentry->d_name.name, | |
9876 | old_dentry->d_name.len); | |
9877 | } else { | |
92986796 | 9878 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9879 | d_inode(old_dentry), |
92986796 AV |
9880 | old_dentry->d_name.name, |
9881 | old_dentry->d_name.len); | |
9882 | if (!ret) | |
9883 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9884 | } |
79787eaa | 9885 | if (ret) { |
66642832 | 9886 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9887 | goto out_fail; |
9888 | } | |
39279cc3 CM |
9889 | |
9890 | if (new_inode) { | |
0c4d2d95 | 9891 | inode_inc_iversion(new_inode); |
c2050a45 | 9892 | new_inode->i_ctime = current_time(new_inode); |
4a0cc7ca | 9893 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
4df27c4d YZ |
9894 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9895 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9896 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9897 | root_objectid, | |
9898 | new_dentry->d_name.name, | |
9899 | new_dentry->d_name.len); | |
9900 | BUG_ON(new_inode->i_nlink == 0); | |
9901 | } else { | |
9902 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9903 | d_inode(new_dentry), |
4df27c4d YZ |
9904 | new_dentry->d_name.name, |
9905 | new_dentry->d_name.len); | |
9906 | } | |
4ef31a45 | 9907 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9908 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa | 9909 | if (ret) { |
66642832 | 9910 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9911 | goto out_fail; |
9912 | } | |
39279cc3 | 9913 | } |
aec7477b | 9914 | |
4df27c4d YZ |
9915 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9916 | new_dentry->d_name.name, | |
a5719521 | 9917 | new_dentry->d_name.len, 0, index); |
79787eaa | 9918 | if (ret) { |
66642832 | 9919 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9920 | goto out_fail; |
9921 | } | |
39279cc3 | 9922 | |
67de1176 MX |
9923 | if (old_inode->i_nlink == 1) |
9924 | BTRFS_I(old_inode)->dir_index = index; | |
9925 | ||
3dc9e8f7 | 9926 | if (log_pinned) { |
10d9f309 | 9927 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 9928 | |
9ca5fbfb | 9929 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), parent); |
4df27c4d | 9930 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9931 | log_pinned = false; |
4df27c4d | 9932 | } |
cdd1fedf DF |
9933 | |
9934 | if (flags & RENAME_WHITEOUT) { | |
9935 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9936 | old_dentry); | |
9937 | ||
9938 | if (ret) { | |
66642832 | 9939 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9940 | goto out_fail; |
9941 | } | |
4df27c4d | 9942 | } |
39279cc3 | 9943 | out_fail: |
3dc9e8f7 FM |
9944 | /* |
9945 | * If we have pinned the log and an error happened, we unpin tasks | |
9946 | * trying to sync the log and force them to fallback to a transaction | |
9947 | * commit if the log currently contains any of the inodes involved in | |
9948 | * this rename operation (to ensure we do not persist a log with an | |
9949 | * inconsistent state for any of these inodes or leading to any | |
9950 | * inconsistencies when replayed). If the transaction was aborted, the | |
9951 | * abortion reason is propagated to userspace when attempting to commit | |
9952 | * the transaction. If the log does not contain any of these inodes, we | |
9953 | * allow the tasks to sync it. | |
9954 | */ | |
9955 | if (ret && log_pinned) { | |
0f8939b8 NB |
9956 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
9957 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || | |
9958 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || | |
3dc9e8f7 | 9959 | (new_inode && |
0f8939b8 | 9960 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
0b246afa | 9961 | btrfs_set_log_full_commit(fs_info, trans); |
3dc9e8f7 FM |
9962 | |
9963 | btrfs_end_log_trans(root); | |
9964 | log_pinned = false; | |
9965 | } | |
3a45bb20 | 9966 | btrfs_end_transaction(trans); |
b44c59a8 | 9967 | out_notrans: |
33345d01 | 9968 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
0b246afa | 9969 | up_read(&fs_info->subvol_sem); |
9ed74f2d | 9970 | |
39279cc3 CM |
9971 | return ret; |
9972 | } | |
9973 | ||
80ace85c MS |
9974 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9975 | struct inode *new_dir, struct dentry *new_dentry, | |
9976 | unsigned int flags) | |
9977 | { | |
cdd1fedf | 9978 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9979 | return -EINVAL; |
9980 | ||
cdd1fedf DF |
9981 | if (flags & RENAME_EXCHANGE) |
9982 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9983 | new_dentry); | |
9984 | ||
9985 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9986 | } |
9987 | ||
8ccf6f19 MX |
9988 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9989 | { | |
9990 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9991 | struct inode *inode; |
8ccf6f19 MX |
9992 | |
9993 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9994 | work); | |
9f23e289 | 9995 | inode = delalloc_work->inode; |
30424601 DS |
9996 | filemap_flush(inode->i_mapping); |
9997 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9998 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9999 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10000 | |
10001 | if (delalloc_work->delay_iput) | |
9f23e289 | 10002 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10003 | else |
9f23e289 | 10004 | iput(inode); |
8ccf6f19 MX |
10005 | complete(&delalloc_work->completion); |
10006 | } | |
10007 | ||
10008 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10009 | int delay_iput) |
8ccf6f19 MX |
10010 | { |
10011 | struct btrfs_delalloc_work *work; | |
10012 | ||
100d5702 | 10013 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10014 | if (!work) |
10015 | return NULL; | |
10016 | ||
10017 | init_completion(&work->completion); | |
10018 | INIT_LIST_HEAD(&work->list); | |
10019 | work->inode = inode; | |
8ccf6f19 | 10020 | work->delay_iput = delay_iput; |
9e0af237 LB |
10021 | WARN_ON_ONCE(!inode); |
10022 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10023 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10024 | |
10025 | return work; | |
10026 | } | |
10027 | ||
10028 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10029 | { | |
10030 | wait_for_completion(&work->completion); | |
100d5702 | 10031 | kfree(work); |
8ccf6f19 MX |
10032 | } |
10033 | ||
d352ac68 CM |
10034 | /* |
10035 | * some fairly slow code that needs optimization. This walks the list | |
10036 | * of all the inodes with pending delalloc and forces them to disk. | |
10037 | */ | |
6c255e67 MX |
10038 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10039 | int nr) | |
ea8c2819 | 10040 | { |
ea8c2819 | 10041 | struct btrfs_inode *binode; |
5b21f2ed | 10042 | struct inode *inode; |
8ccf6f19 MX |
10043 | struct btrfs_delalloc_work *work, *next; |
10044 | struct list_head works; | |
1eafa6c7 | 10045 | struct list_head splice; |
8ccf6f19 | 10046 | int ret = 0; |
ea8c2819 | 10047 | |
8ccf6f19 | 10048 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10049 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10050 | |
573bfb72 | 10051 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10052 | spin_lock(&root->delalloc_lock); |
10053 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10054 | while (!list_empty(&splice)) { |
10055 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10056 | delalloc_inodes); |
1eafa6c7 | 10057 | |
eb73c1b7 MX |
10058 | list_move_tail(&binode->delalloc_inodes, |
10059 | &root->delalloc_inodes); | |
5b21f2ed | 10060 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10061 | if (!inode) { |
eb73c1b7 | 10062 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10063 | continue; |
df0af1a5 | 10064 | } |
eb73c1b7 | 10065 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10066 | |
651d494a | 10067 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10068 | if (!work) { |
f4ab9ea7 JB |
10069 | if (delay_iput) |
10070 | btrfs_add_delayed_iput(inode); | |
10071 | else | |
10072 | iput(inode); | |
1eafa6c7 | 10073 | ret = -ENOMEM; |
a1ecaabb | 10074 | goto out; |
5b21f2ed | 10075 | } |
1eafa6c7 | 10076 | list_add_tail(&work->list, &works); |
a44903ab QW |
10077 | btrfs_queue_work(root->fs_info->flush_workers, |
10078 | &work->work); | |
6c255e67 MX |
10079 | ret++; |
10080 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10081 | goto out; |
5b21f2ed | 10082 | cond_resched(); |
eb73c1b7 | 10083 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10084 | } |
eb73c1b7 | 10085 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10086 | |
a1ecaabb | 10087 | out: |
eb73c1b7 MX |
10088 | list_for_each_entry_safe(work, next, &works, list) { |
10089 | list_del_init(&work->list); | |
10090 | btrfs_wait_and_free_delalloc_work(work); | |
10091 | } | |
10092 | ||
10093 | if (!list_empty_careful(&splice)) { | |
10094 | spin_lock(&root->delalloc_lock); | |
10095 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10096 | spin_unlock(&root->delalloc_lock); | |
10097 | } | |
573bfb72 | 10098 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10099 | return ret; |
10100 | } | |
1eafa6c7 | 10101 | |
eb73c1b7 MX |
10102 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10103 | { | |
0b246afa | 10104 | struct btrfs_fs_info *fs_info = root->fs_info; |
eb73c1b7 | 10105 | int ret; |
1eafa6c7 | 10106 | |
0b246afa | 10107 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10108 | return -EROFS; |
10109 | ||
6c255e67 MX |
10110 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10111 | if (ret > 0) | |
10112 | ret = 0; | |
eb73c1b7 MX |
10113 | /* |
10114 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10115 | * we have to make sure the IO is actually started and that |
10116 | * ordered extents get created before we return | |
10117 | */ | |
0b246afa JM |
10118 | atomic_inc(&fs_info->async_submit_draining); |
10119 | while (atomic_read(&fs_info->nr_async_submits) || | |
10120 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10121 | wait_event(fs_info->async_submit_wait, | |
10122 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10123 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10124 | } | |
10125 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 MX |
10126 | return ret; |
10127 | } | |
10128 | ||
6c255e67 MX |
10129 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10130 | int nr) | |
eb73c1b7 MX |
10131 | { |
10132 | struct btrfs_root *root; | |
10133 | struct list_head splice; | |
10134 | int ret; | |
10135 | ||
2c21b4d7 | 10136 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10137 | return -EROFS; |
10138 | ||
10139 | INIT_LIST_HEAD(&splice); | |
10140 | ||
573bfb72 | 10141 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10142 | spin_lock(&fs_info->delalloc_root_lock); |
10143 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10144 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10145 | root = list_first_entry(&splice, struct btrfs_root, |
10146 | delalloc_root); | |
10147 | root = btrfs_grab_fs_root(root); | |
10148 | BUG_ON(!root); | |
10149 | list_move_tail(&root->delalloc_root, | |
10150 | &fs_info->delalloc_roots); | |
10151 | spin_unlock(&fs_info->delalloc_root_lock); | |
10152 | ||
6c255e67 | 10153 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10154 | btrfs_put_fs_root(root); |
6c255e67 | 10155 | if (ret < 0) |
eb73c1b7 MX |
10156 | goto out; |
10157 | ||
6c255e67 MX |
10158 | if (nr != -1) { |
10159 | nr -= ret; | |
10160 | WARN_ON(nr < 0); | |
10161 | } | |
eb73c1b7 | 10162 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10163 | } |
eb73c1b7 | 10164 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10165 | |
6c255e67 | 10166 | ret = 0; |
eb73c1b7 MX |
10167 | atomic_inc(&fs_info->async_submit_draining); |
10168 | while (atomic_read(&fs_info->nr_async_submits) || | |
10169 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10170 | wait_event(fs_info->async_submit_wait, | |
10171 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10172 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10173 | } | |
10174 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10175 | out: |
1eafa6c7 | 10176 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10177 | spin_lock(&fs_info->delalloc_root_lock); |
10178 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10179 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10180 | } |
573bfb72 | 10181 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10182 | return ret; |
ea8c2819 CM |
10183 | } |
10184 | ||
39279cc3 CM |
10185 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10186 | const char *symname) | |
10187 | { | |
0b246afa | 10188 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
39279cc3 CM |
10189 | struct btrfs_trans_handle *trans; |
10190 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10191 | struct btrfs_path *path; | |
10192 | struct btrfs_key key; | |
1832a6d5 | 10193 | struct inode *inode = NULL; |
39279cc3 CM |
10194 | int err; |
10195 | int drop_inode = 0; | |
10196 | u64 objectid; | |
67871254 | 10197 | u64 index = 0; |
39279cc3 CM |
10198 | int name_len; |
10199 | int datasize; | |
5f39d397 | 10200 | unsigned long ptr; |
39279cc3 | 10201 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10202 | struct extent_buffer *leaf; |
39279cc3 | 10203 | |
f06becc4 | 10204 | name_len = strlen(symname); |
0b246afa | 10205 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
39279cc3 | 10206 | return -ENAMETOOLONG; |
1832a6d5 | 10207 | |
9ed74f2d JB |
10208 | /* |
10209 | * 2 items for inode item and ref | |
10210 | * 2 items for dir items | |
9269d12b FM |
10211 | * 1 item for updating parent inode item |
10212 | * 1 item for the inline extent item | |
9ed74f2d JB |
10213 | * 1 item for xattr if selinux is on |
10214 | */ | |
9269d12b | 10215 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10216 | if (IS_ERR(trans)) |
10217 | return PTR_ERR(trans); | |
1832a6d5 | 10218 | |
581bb050 LZ |
10219 | err = btrfs_find_free_ino(root, &objectid); |
10220 | if (err) | |
10221 | goto out_unlock; | |
10222 | ||
aec7477b | 10223 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
4a0cc7ca | 10224 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
d82a6f1d | 10225 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
10226 | if (IS_ERR(inode)) { |
10227 | err = PTR_ERR(inode); | |
39279cc3 | 10228 | goto out_unlock; |
7cf96da3 | 10229 | } |
39279cc3 | 10230 | |
ad19db71 CS |
10231 | /* |
10232 | * If the active LSM wants to access the inode during | |
10233 | * d_instantiate it needs these. Smack checks to see | |
10234 | * if the filesystem supports xattrs by looking at the | |
10235 | * ops vector. | |
10236 | */ | |
10237 | inode->i_fop = &btrfs_file_operations; | |
10238 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10239 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10240 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10241 | ||
10242 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10243 | if (err) | |
10244 | goto out_unlock_inode; | |
ad19db71 | 10245 | |
39279cc3 | 10246 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10247 | if (!path) { |
10248 | err = -ENOMEM; | |
b0d5d10f | 10249 | goto out_unlock_inode; |
d8926bb3 | 10250 | } |
4a0cc7ca | 10251 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
39279cc3 | 10252 | key.offset = 0; |
962a298f | 10253 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10254 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10255 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10256 | datasize); | |
54aa1f4d | 10257 | if (err) { |
b0839166 | 10258 | btrfs_free_path(path); |
b0d5d10f | 10259 | goto out_unlock_inode; |
54aa1f4d | 10260 | } |
5f39d397 CM |
10261 | leaf = path->nodes[0]; |
10262 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10263 | struct btrfs_file_extent_item); | |
10264 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10265 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10266 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10267 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10268 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10269 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10270 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10271 | ||
39279cc3 | 10272 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10273 | write_extent_buffer(leaf, symname, ptr, name_len); |
10274 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10275 | btrfs_free_path(path); |
5f39d397 | 10276 | |
39279cc3 | 10277 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10278 | inode_nohighmem(inode); |
39279cc3 | 10279 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10280 | inode_set_bytes(inode, name_len); |
f06becc4 | 10281 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 10282 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10283 | /* |
10284 | * Last step, add directory indexes for our symlink inode. This is the | |
10285 | * last step to avoid extra cleanup of these indexes if an error happens | |
10286 | * elsewhere above. | |
10287 | */ | |
10288 | if (!err) | |
10289 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
b0d5d10f | 10290 | if (err) { |
54aa1f4d | 10291 | drop_inode = 1; |
b0d5d10f CM |
10292 | goto out_unlock_inode; |
10293 | } | |
10294 | ||
10295 | unlock_new_inode(inode); | |
10296 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10297 | |
10298 | out_unlock: | |
3a45bb20 | 10299 | btrfs_end_transaction(trans); |
39279cc3 CM |
10300 | if (drop_inode) { |
10301 | inode_dec_link_count(inode); | |
10302 | iput(inode); | |
10303 | } | |
2ff7e61e | 10304 | btrfs_btree_balance_dirty(fs_info); |
39279cc3 | 10305 | return err; |
b0d5d10f CM |
10306 | |
10307 | out_unlock_inode: | |
10308 | drop_inode = 1; | |
10309 | unlock_new_inode(inode); | |
10310 | goto out_unlock; | |
39279cc3 | 10311 | } |
16432985 | 10312 | |
0af3d00b JB |
10313 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10314 | u64 start, u64 num_bytes, u64 min_size, | |
10315 | loff_t actual_len, u64 *alloc_hint, | |
10316 | struct btrfs_trans_handle *trans) | |
d899e052 | 10317 | { |
0b246afa | 10318 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5dc562c5 JB |
10319 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10320 | struct extent_map *em; | |
d899e052 YZ |
10321 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10322 | struct btrfs_key ins; | |
d899e052 | 10323 | u64 cur_offset = start; |
55a61d1d | 10324 | u64 i_size; |
154ea289 | 10325 | u64 cur_bytes; |
0b670dc4 | 10326 | u64 last_alloc = (u64)-1; |
d899e052 | 10327 | int ret = 0; |
0af3d00b | 10328 | bool own_trans = true; |
18513091 | 10329 | u64 end = start + num_bytes - 1; |
d899e052 | 10330 | |
0af3d00b JB |
10331 | if (trans) |
10332 | own_trans = false; | |
d899e052 | 10333 | while (num_bytes > 0) { |
0af3d00b JB |
10334 | if (own_trans) { |
10335 | trans = btrfs_start_transaction(root, 3); | |
10336 | if (IS_ERR(trans)) { | |
10337 | ret = PTR_ERR(trans); | |
10338 | break; | |
10339 | } | |
5a303d5d YZ |
10340 | } |
10341 | ||
ee22184b | 10342 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10343 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10344 | /* |
10345 | * If we are severely fragmented we could end up with really | |
10346 | * small allocations, so if the allocator is returning small | |
10347 | * chunks lets make its job easier by only searching for those | |
10348 | * sized chunks. | |
10349 | */ | |
10350 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10351 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10352 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10353 | if (ret) { |
0af3d00b | 10354 | if (own_trans) |
3a45bb20 | 10355 | btrfs_end_transaction(trans); |
a22285a6 | 10356 | break; |
d899e052 | 10357 | } |
0b246afa | 10358 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
5a303d5d | 10359 | |
0b670dc4 | 10360 | last_alloc = ins.offset; |
d899e052 YZ |
10361 | ret = insert_reserved_file_extent(trans, inode, |
10362 | cur_offset, ins.objectid, | |
10363 | ins.offset, ins.offset, | |
920bbbfb | 10364 | ins.offset, 0, 0, 0, |
d899e052 | 10365 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10366 | if (ret) { |
2ff7e61e | 10367 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
e570fd27 | 10368 | ins.offset, 0); |
66642832 | 10369 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10370 | if (own_trans) |
3a45bb20 | 10371 | btrfs_end_transaction(trans); |
79787eaa JM |
10372 | break; |
10373 | } | |
31193213 | 10374 | |
a1ed835e CM |
10375 | btrfs_drop_extent_cache(inode, cur_offset, |
10376 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 10377 | |
5dc562c5 JB |
10378 | em = alloc_extent_map(); |
10379 | if (!em) { | |
10380 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10381 | &BTRFS_I(inode)->runtime_flags); | |
10382 | goto next; | |
10383 | } | |
10384 | ||
10385 | em->start = cur_offset; | |
10386 | em->orig_start = cur_offset; | |
10387 | em->len = ins.offset; | |
10388 | em->block_start = ins.objectid; | |
10389 | em->block_len = ins.offset; | |
b4939680 | 10390 | em->orig_block_len = ins.offset; |
cc95bef6 | 10391 | em->ram_bytes = ins.offset; |
0b246afa | 10392 | em->bdev = fs_info->fs_devices->latest_bdev; |
5dc562c5 JB |
10393 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
10394 | em->generation = trans->transid; | |
10395 | ||
10396 | while (1) { | |
10397 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10398 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10399 | write_unlock(&em_tree->lock); |
10400 | if (ret != -EEXIST) | |
10401 | break; | |
10402 | btrfs_drop_extent_cache(inode, cur_offset, | |
10403 | cur_offset + ins.offset - 1, | |
10404 | 0); | |
10405 | } | |
10406 | free_extent_map(em); | |
10407 | next: | |
d899e052 YZ |
10408 | num_bytes -= ins.offset; |
10409 | cur_offset += ins.offset; | |
efa56464 | 10410 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10411 | |
0c4d2d95 | 10412 | inode_inc_iversion(inode); |
c2050a45 | 10413 | inode->i_ctime = current_time(inode); |
6cbff00f | 10414 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10415 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10416 | (actual_len > inode->i_size) && |
10417 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10418 | if (cur_offset > actual_len) |
55a61d1d | 10419 | i_size = actual_len; |
d1ea6a61 | 10420 | else |
55a61d1d JB |
10421 | i_size = cur_offset; |
10422 | i_size_write(inode, i_size); | |
10423 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10424 | } |
10425 | ||
d899e052 | 10426 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10427 | |
10428 | if (ret) { | |
66642832 | 10429 | btrfs_abort_transaction(trans, ret); |
79787eaa | 10430 | if (own_trans) |
3a45bb20 | 10431 | btrfs_end_transaction(trans); |
79787eaa JM |
10432 | break; |
10433 | } | |
d899e052 | 10434 | |
0af3d00b | 10435 | if (own_trans) |
3a45bb20 | 10436 | btrfs_end_transaction(trans); |
5a303d5d | 10437 | } |
18513091 WX |
10438 | if (cur_offset < end) |
10439 | btrfs_free_reserved_data_space(inode, cur_offset, | |
10440 | end - cur_offset + 1); | |
d899e052 YZ |
10441 | return ret; |
10442 | } | |
10443 | ||
0af3d00b JB |
10444 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10445 | u64 start, u64 num_bytes, u64 min_size, | |
10446 | loff_t actual_len, u64 *alloc_hint) | |
10447 | { | |
10448 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10449 | min_size, actual_len, alloc_hint, | |
10450 | NULL); | |
10451 | } | |
10452 | ||
10453 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10454 | struct btrfs_trans_handle *trans, int mode, | |
10455 | u64 start, u64 num_bytes, u64 min_size, | |
10456 | loff_t actual_len, u64 *alloc_hint) | |
10457 | { | |
10458 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10459 | min_size, actual_len, alloc_hint, trans); | |
10460 | } | |
10461 | ||
e6dcd2dc CM |
10462 | static int btrfs_set_page_dirty(struct page *page) |
10463 | { | |
e6dcd2dc CM |
10464 | return __set_page_dirty_nobuffers(page); |
10465 | } | |
10466 | ||
10556cb2 | 10467 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10468 | { |
b83cc969 | 10469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10470 | umode_t mode = inode->i_mode; |
b83cc969 | 10471 | |
cb6db4e5 JM |
10472 | if (mask & MAY_WRITE && |
10473 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10474 | if (btrfs_root_readonly(root)) | |
10475 | return -EROFS; | |
10476 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10477 | return -EACCES; | |
10478 | } | |
2830ba7f | 10479 | return generic_permission(inode, mask); |
fdebe2bd | 10480 | } |
39279cc3 | 10481 | |
ef3b9af5 FM |
10482 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10483 | { | |
2ff7e61e | 10484 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
ef3b9af5 FM |
10485 | struct btrfs_trans_handle *trans; |
10486 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10487 | struct inode *inode = NULL; | |
10488 | u64 objectid; | |
10489 | u64 index; | |
10490 | int ret = 0; | |
10491 | ||
10492 | /* | |
10493 | * 5 units required for adding orphan entry | |
10494 | */ | |
10495 | trans = btrfs_start_transaction(root, 5); | |
10496 | if (IS_ERR(trans)) | |
10497 | return PTR_ERR(trans); | |
10498 | ||
10499 | ret = btrfs_find_free_ino(root, &objectid); | |
10500 | if (ret) | |
10501 | goto out; | |
10502 | ||
10503 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
4a0cc7ca | 10504 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
ef3b9af5 FM |
10505 | if (IS_ERR(inode)) { |
10506 | ret = PTR_ERR(inode); | |
10507 | inode = NULL; | |
10508 | goto out; | |
10509 | } | |
10510 | ||
ef3b9af5 FM |
10511 | inode->i_fop = &btrfs_file_operations; |
10512 | inode->i_op = &btrfs_file_inode_operations; | |
10513 | ||
10514 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10515 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10516 | ||
b0d5d10f CM |
10517 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10518 | if (ret) | |
10519 | goto out_inode; | |
10520 | ||
10521 | ret = btrfs_update_inode(trans, root, inode); | |
10522 | if (ret) | |
10523 | goto out_inode; | |
ef3b9af5 FM |
10524 | ret = btrfs_orphan_add(trans, inode); |
10525 | if (ret) | |
b0d5d10f | 10526 | goto out_inode; |
ef3b9af5 | 10527 | |
5762b5c9 FM |
10528 | /* |
10529 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10530 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10531 | * through: | |
10532 | * | |
10533 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10534 | */ | |
10535 | set_nlink(inode, 1); | |
b0d5d10f | 10536 | unlock_new_inode(inode); |
ef3b9af5 FM |
10537 | d_tmpfile(dentry, inode); |
10538 | mark_inode_dirty(inode); | |
10539 | ||
10540 | out: | |
3a45bb20 | 10541 | btrfs_end_transaction(trans); |
ef3b9af5 FM |
10542 | if (ret) |
10543 | iput(inode); | |
2ff7e61e JM |
10544 | btrfs_balance_delayed_items(fs_info); |
10545 | btrfs_btree_balance_dirty(fs_info); | |
ef3b9af5 | 10546 | return ret; |
b0d5d10f CM |
10547 | |
10548 | out_inode: | |
10549 | unlock_new_inode(inode); | |
10550 | goto out; | |
10551 | ||
ef3b9af5 FM |
10552 | } |
10553 | ||
6e1d5dcc | 10554 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10555 | .getattr = btrfs_getattr, |
39279cc3 CM |
10556 | .lookup = btrfs_lookup, |
10557 | .create = btrfs_create, | |
10558 | .unlink = btrfs_unlink, | |
10559 | .link = btrfs_link, | |
10560 | .mkdir = btrfs_mkdir, | |
10561 | .rmdir = btrfs_rmdir, | |
2773bf00 | 10562 | .rename = btrfs_rename2, |
39279cc3 CM |
10563 | .symlink = btrfs_symlink, |
10564 | .setattr = btrfs_setattr, | |
618e21d5 | 10565 | .mknod = btrfs_mknod, |
5103e947 | 10566 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10567 | .permission = btrfs_permission, |
4e34e719 | 10568 | .get_acl = btrfs_get_acl, |
996a710d | 10569 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10570 | .update_time = btrfs_update_time, |
ef3b9af5 | 10571 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10572 | }; |
6e1d5dcc | 10573 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10574 | .lookup = btrfs_lookup, |
fdebe2bd | 10575 | .permission = btrfs_permission, |
93fd63c2 | 10576 | .update_time = btrfs_update_time, |
39279cc3 | 10577 | }; |
76dda93c | 10578 | |
828c0950 | 10579 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10580 | .llseek = generic_file_llseek, |
10581 | .read = generic_read_dir, | |
02dbfc99 | 10582 | .iterate_shared = btrfs_real_readdir, |
34287aa3 | 10583 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10584 | #ifdef CONFIG_COMPAT |
4c63c245 | 10585 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10586 | #endif |
6bf13c0c | 10587 | .release = btrfs_release_file, |
e02119d5 | 10588 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10589 | }; |
10590 | ||
20e5506b | 10591 | static const struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 10592 | .fill_delalloc = run_delalloc_range, |
065631f6 | 10593 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 10594 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 10595 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 10596 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10597 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10598 | .set_bit_hook = btrfs_set_bit_hook, |
10599 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10600 | .merge_extent_hook = btrfs_merge_extent_hook, |
10601 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
10602 | }; |
10603 | ||
35054394 CM |
10604 | /* |
10605 | * btrfs doesn't support the bmap operation because swapfiles | |
10606 | * use bmap to make a mapping of extents in the file. They assume | |
10607 | * these extents won't change over the life of the file and they | |
10608 | * use the bmap result to do IO directly to the drive. | |
10609 | * | |
10610 | * the btrfs bmap call would return logical addresses that aren't | |
10611 | * suitable for IO and they also will change frequently as COW | |
10612 | * operations happen. So, swapfile + btrfs == corruption. | |
10613 | * | |
10614 | * For now we're avoiding this by dropping bmap. | |
10615 | */ | |
7f09410b | 10616 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10617 | .readpage = btrfs_readpage, |
10618 | .writepage = btrfs_writepage, | |
b293f02e | 10619 | .writepages = btrfs_writepages, |
3ab2fb5a | 10620 | .readpages = btrfs_readpages, |
16432985 | 10621 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10622 | .invalidatepage = btrfs_invalidatepage, |
10623 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10624 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10625 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10626 | }; |
10627 | ||
7f09410b | 10628 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10629 | .readpage = btrfs_readpage, |
10630 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10631 | .invalidatepage = btrfs_invalidatepage, |
10632 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10633 | }; |
10634 | ||
6e1d5dcc | 10635 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10636 | .getattr = btrfs_getattr, |
10637 | .setattr = btrfs_setattr, | |
5103e947 | 10638 | .listxattr = btrfs_listxattr, |
fdebe2bd | 10639 | .permission = btrfs_permission, |
1506fcc8 | 10640 | .fiemap = btrfs_fiemap, |
4e34e719 | 10641 | .get_acl = btrfs_get_acl, |
996a710d | 10642 | .set_acl = btrfs_set_acl, |
e41f941a | 10643 | .update_time = btrfs_update_time, |
39279cc3 | 10644 | }; |
6e1d5dcc | 10645 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10646 | .getattr = btrfs_getattr, |
10647 | .setattr = btrfs_setattr, | |
fdebe2bd | 10648 | .permission = btrfs_permission, |
33268eaf | 10649 | .listxattr = btrfs_listxattr, |
4e34e719 | 10650 | .get_acl = btrfs_get_acl, |
996a710d | 10651 | .set_acl = btrfs_set_acl, |
e41f941a | 10652 | .update_time = btrfs_update_time, |
618e21d5 | 10653 | }; |
6e1d5dcc | 10654 | static const struct inode_operations btrfs_symlink_inode_operations = { |
6b255391 | 10655 | .get_link = page_get_link, |
f209561a | 10656 | .getattr = btrfs_getattr, |
22c44fe6 | 10657 | .setattr = btrfs_setattr, |
fdebe2bd | 10658 | .permission = btrfs_permission, |
0279b4cd | 10659 | .listxattr = btrfs_listxattr, |
e41f941a | 10660 | .update_time = btrfs_update_time, |
39279cc3 | 10661 | }; |
76dda93c | 10662 | |
82d339d9 | 10663 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10664 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10665 | .d_release = btrfs_dentry_release, |
76dda93c | 10666 | }; |