]>
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> | |
33 | #include <linux/statfs.h> | |
34 | #include <linux/compat.h> | |
9ebefb18 | 35 | #include <linux/bit_spinlock.h> |
5103e947 | 36 | #include <linux/xattr.h> |
33268eaf | 37 | #include <linux/posix_acl.h> |
d899e052 | 38 | #include <linux/falloc.h> |
5a0e3ad6 | 39 | #include <linux/slab.h> |
7a36ddec | 40 | #include <linux/ratelimit.h> |
22c44fe6 | 41 | #include <linux/mount.h> |
55e301fd | 42 | #include <linux/btrfs.h> |
53b381b3 | 43 | #include <linux/blkdev.h> |
f23b5a59 | 44 | #include <linux/posix_acl_xattr.h> |
e2e40f2c | 45 | #include <linux/uio.h> |
39279cc3 CM |
46 | #include "ctree.h" |
47 | #include "disk-io.h" | |
48 | #include "transaction.h" | |
49 | #include "btrfs_inode.h" | |
39279cc3 | 50 | #include "print-tree.h" |
e6dcd2dc | 51 | #include "ordered-data.h" |
95819c05 | 52 | #include "xattr.h" |
e02119d5 | 53 | #include "tree-log.h" |
4a54c8c1 | 54 | #include "volumes.h" |
c8b97818 | 55 | #include "compression.h" |
b4ce94de | 56 | #include "locking.h" |
dc89e982 | 57 | #include "free-space-cache.h" |
581bb050 | 58 | #include "inode-map.h" |
38c227d8 | 59 | #include "backref.h" |
f23b5a59 | 60 | #include "hash.h" |
63541927 | 61 | #include "props.h" |
31193213 | 62 | #include "qgroup.h" |
dda3245e | 63 | #include "dedupe.h" |
39279cc3 CM |
64 | |
65 | struct btrfs_iget_args { | |
90d3e592 | 66 | struct btrfs_key *location; |
39279cc3 CM |
67 | struct btrfs_root *root; |
68 | }; | |
69 | ||
f28a4928 FM |
70 | struct btrfs_dio_data { |
71 | u64 outstanding_extents; | |
72 | u64 reserve; | |
73 | u64 unsubmitted_oe_range_start; | |
74 | u64 unsubmitted_oe_range_end; | |
75 | }; | |
76 | ||
6e1d5dcc AD |
77 | static const struct inode_operations btrfs_dir_inode_operations; |
78 | static const struct inode_operations btrfs_symlink_inode_operations; | |
79 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
80 | static const struct inode_operations btrfs_special_inode_operations; | |
81 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
82 | static const struct address_space_operations btrfs_aops; |
83 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 84 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
86 | |
87 | static struct kmem_cache *btrfs_inode_cachep; | |
88 | struct kmem_cache *btrfs_trans_handle_cachep; | |
89 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 90 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 91 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
92 | |
93 | #define S_SHIFT 12 | |
4d4ab6d6 | 94 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
39279cc3 CM |
95 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
96 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
97 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
98 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
99 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
100 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
101 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
102 | }; | |
103 | ||
3972f260 | 104 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 105 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 106 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
107 | static noinline int cow_file_range(struct inode *inode, |
108 | struct page *locked_page, | |
dda3245e WX |
109 | u64 start, u64 end, u64 delalloc_end, |
110 | int *page_started, unsigned long *nr_written, | |
111 | int unlock, struct btrfs_dedupe_hash *hash); | |
70c8a91c JB |
112 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
113 | u64 len, u64 orig_start, | |
114 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
115 | u64 orig_block_len, u64 ram_bytes, |
116 | int type); | |
7b128766 | 117 | |
48a3b636 | 118 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 119 | |
6a3891c5 JB |
120 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
121 | void btrfs_test_inode_set_ops(struct inode *inode) | |
122 | { | |
123 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
124 | } | |
125 | #endif | |
126 | ||
f34f57a3 | 127 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
128 | struct inode *inode, struct inode *dir, |
129 | const struct qstr *qstr) | |
0279b4cd JO |
130 | { |
131 | int err; | |
132 | ||
f34f57a3 | 133 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 134 | if (!err) |
2a7dba39 | 135 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
136 | return err; |
137 | } | |
138 | ||
c8b97818 CM |
139 | /* |
140 | * this does all the hard work for inserting an inline extent into | |
141 | * the btree. The caller should have done a btrfs_drop_extents so that | |
142 | * no overlapping inline items exist in the btree | |
143 | */ | |
40f76580 | 144 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 145 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
146 | struct btrfs_root *root, struct inode *inode, |
147 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 148 | int compress_type, |
c8b97818 CM |
149 | struct page **compressed_pages) |
150 | { | |
c8b97818 CM |
151 | struct extent_buffer *leaf; |
152 | struct page *page = NULL; | |
153 | char *kaddr; | |
154 | unsigned long ptr; | |
155 | struct btrfs_file_extent_item *ei; | |
156 | int err = 0; | |
157 | int ret; | |
158 | size_t cur_size = size; | |
c8b97818 | 159 | unsigned long offset; |
c8b97818 | 160 | |
fe3f566c | 161 | if (compressed_size && compressed_pages) |
c8b97818 | 162 | cur_size = compressed_size; |
c8b97818 | 163 | |
1acae57b | 164 | inode_add_bytes(inode, size); |
c8b97818 | 165 | |
1acae57b FDBM |
166 | if (!extent_inserted) { |
167 | struct btrfs_key key; | |
168 | size_t datasize; | |
c8b97818 | 169 | |
1acae57b FDBM |
170 | key.objectid = btrfs_ino(inode); |
171 | key.offset = start; | |
962a298f | 172 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 173 | |
1acae57b FDBM |
174 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
175 | path->leave_spinning = 1; | |
176 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
177 | datasize); | |
178 | if (ret) { | |
179 | err = ret; | |
180 | goto fail; | |
181 | } | |
c8b97818 CM |
182 | } |
183 | leaf = path->nodes[0]; | |
184 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
185 | struct btrfs_file_extent_item); | |
186 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
187 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
188 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
189 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
190 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
191 | ptr = btrfs_file_extent_inline_start(ei); | |
192 | ||
261507a0 | 193 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
194 | struct page *cpage; |
195 | int i = 0; | |
d397712b | 196 | while (compressed_size > 0) { |
c8b97818 | 197 | cpage = compressed_pages[i]; |
5b050f04 | 198 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 199 | PAGE_SIZE); |
c8b97818 | 200 | |
7ac687d9 | 201 | kaddr = kmap_atomic(cpage); |
c8b97818 | 202 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 203 | kunmap_atomic(kaddr); |
c8b97818 CM |
204 | |
205 | i++; | |
206 | ptr += cur_size; | |
207 | compressed_size -= cur_size; | |
208 | } | |
209 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 210 | compress_type); |
c8b97818 CM |
211 | } else { |
212 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 213 | start >> PAGE_SHIFT); |
c8b97818 | 214 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 215 | kaddr = kmap_atomic(page); |
09cbfeaf | 216 | offset = start & (PAGE_SIZE - 1); |
c8b97818 | 217 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 218 | kunmap_atomic(kaddr); |
09cbfeaf | 219 | put_page(page); |
c8b97818 CM |
220 | } |
221 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 222 | btrfs_release_path(path); |
c8b97818 | 223 | |
c2167754 YZ |
224 | /* |
225 | * we're an inline extent, so nobody can | |
226 | * extend the file past i_size without locking | |
227 | * a page we already have locked. | |
228 | * | |
229 | * We must do any isize and inode updates | |
230 | * before we unlock the pages. Otherwise we | |
231 | * could end up racing with unlink. | |
232 | */ | |
c8b97818 | 233 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 234 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 235 | |
79787eaa | 236 | return ret; |
c8b97818 | 237 | fail: |
c8b97818 CM |
238 | return err; |
239 | } | |
240 | ||
241 | ||
242 | /* | |
243 | * conditionally insert an inline extent into the file. This | |
244 | * does the checks required to make sure the data is small enough | |
245 | * to fit as an inline extent. | |
246 | */ | |
00361589 JB |
247 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
248 | struct inode *inode, u64 start, | |
249 | u64 end, size_t compressed_size, | |
250 | int compress_type, | |
251 | struct page **compressed_pages) | |
c8b97818 | 252 | { |
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; | |
fda2832f | 257 | u64 aligned_end = ALIGN(end, root->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 || | |
0c29ba99 | 268 | actual_end > root->sectorsize || |
354877be | 269 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
c8b97818 CM |
270 | (!compressed_size && |
271 | (actual_end & (root->sectorsize - 1)) == 0) || | |
272 | end + 1 < isize || | |
273 | data_len > root->fs_info->max_inline) { | |
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 | } |
00361589 JB |
286 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
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); |
00361589 JB |
329 | btrfs_end_transaction(trans, root); |
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 | { | |
376 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
377 | ||
378 | /* force compress */ | |
3cdde224 | 379 | if (btrfs_test_opt(root->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; | |
3cdde224 | 384 | if (btrfs_test_opt(root->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 | ||
d352ac68 | 391 | /* |
771ed689 CM |
392 | * we create compressed extents in two phases. The first |
393 | * phase compresses a range of pages that have already been | |
394 | * locked (both pages and state bits are locked). | |
c8b97818 | 395 | * |
771ed689 CM |
396 | * This is done inside an ordered work queue, and the compression |
397 | * is spread across many cpus. The actual IO submission is step | |
398 | * two, and the ordered work queue takes care of making sure that | |
399 | * happens in the same order things were put onto the queue by | |
400 | * writepages and friends. | |
c8b97818 | 401 | * |
771ed689 CM |
402 | * If this code finds it can't get good compression, it puts an |
403 | * entry onto the work queue to write the uncompressed bytes. This | |
404 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
405 | * are written in the same order that the flusher thread sent them |
406 | * down. | |
d352ac68 | 407 | */ |
c44f649e | 408 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
409 | struct page *locked_page, |
410 | u64 start, u64 end, | |
411 | struct async_cow *async_cow, | |
412 | int *num_added) | |
b888db2b CM |
413 | { |
414 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 415 | u64 num_bytes; |
db94535d | 416 | u64 blocksize = root->sectorsize; |
c8b97818 | 417 | u64 actual_end; |
42dc7bab | 418 | u64 isize = i_size_read(inode); |
e6dcd2dc | 419 | int ret = 0; |
c8b97818 CM |
420 | struct page **pages = NULL; |
421 | unsigned long nr_pages; | |
422 | unsigned long nr_pages_ret = 0; | |
423 | unsigned long total_compressed = 0; | |
424 | unsigned long total_in = 0; | |
ee22184b BL |
425 | unsigned long max_compressed = SZ_128K; |
426 | unsigned long max_uncompressed = SZ_128K; | |
c8b97818 CM |
427 | int i; |
428 | int will_compress; | |
261507a0 | 429 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 430 | int redirty = 0; |
b888db2b | 431 | |
4cb13e5d | 432 | /* if this is a small write inside eof, kick off a defrag */ |
ee22184b | 433 | if ((end - start + 1) < SZ_16K && |
4cb13e5d | 434 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
4cb5300b CM |
435 | btrfs_add_inode_defrag(NULL, inode); |
436 | ||
42dc7bab | 437 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
438 | again: |
439 | will_compress = 0; | |
09cbfeaf KS |
440 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
441 | nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE); | |
be20aa9d | 442 | |
f03d9301 CM |
443 | /* |
444 | * we don't want to send crud past the end of i_size through | |
445 | * compression, that's just a waste of CPU time. So, if the | |
446 | * end of the file is before the start of our current | |
447 | * requested range of bytes, we bail out to the uncompressed | |
448 | * cleanup code that can deal with all of this. | |
449 | * | |
450 | * It isn't really the fastest way to fix things, but this is a | |
451 | * very uncommon corner. | |
452 | */ | |
453 | if (actual_end <= start) | |
454 | goto cleanup_and_bail_uncompressed; | |
455 | ||
c8b97818 CM |
456 | total_compressed = actual_end - start; |
457 | ||
4bcbb332 SW |
458 | /* |
459 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 460 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
461 | */ |
462 | if (total_compressed <= blocksize && | |
463 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
464 | goto cleanup_and_bail_uncompressed; | |
465 | ||
c8b97818 CM |
466 | /* we want to make sure that amount of ram required to uncompress |
467 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
468 | * of a compressed extent to 128k. This is a crucial number |
469 | * because it also controls how easily we can spread reads across | |
470 | * cpus for decompression. | |
471 | * | |
472 | * We also want to make sure the amount of IO required to do | |
473 | * a random read is reasonably small, so we limit the size of | |
474 | * a compressed extent to 128k. | |
c8b97818 CM |
475 | */ |
476 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 477 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 478 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
479 | total_in = 0; |
480 | ret = 0; | |
db94535d | 481 | |
771ed689 CM |
482 | /* |
483 | * we do compression for mount -o compress and when the | |
484 | * inode has not been flagged as nocompress. This flag can | |
485 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 486 | */ |
f79707b0 | 487 | if (inode_need_compress(inode)) { |
c8b97818 | 488 | WARN_ON(pages); |
31e818fe | 489 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
490 | if (!pages) { |
491 | /* just bail out to the uncompressed code */ | |
492 | goto cont; | |
493 | } | |
c8b97818 | 494 | |
261507a0 LZ |
495 | if (BTRFS_I(inode)->force_compress) |
496 | compress_type = BTRFS_I(inode)->force_compress; | |
497 | ||
4adaa611 CM |
498 | /* |
499 | * we need to call clear_page_dirty_for_io on each | |
500 | * page in the range. Otherwise applications with the file | |
501 | * mmap'd can wander in and change the page contents while | |
502 | * we are compressing them. | |
503 | * | |
504 | * If the compression fails for any reason, we set the pages | |
505 | * dirty again later on. | |
506 | */ | |
507 | extent_range_clear_dirty_for_io(inode, start, end); | |
508 | redirty = 1; | |
261507a0 LZ |
509 | ret = btrfs_compress_pages(compress_type, |
510 | inode->i_mapping, start, | |
511 | total_compressed, pages, | |
512 | nr_pages, &nr_pages_ret, | |
513 | &total_in, | |
514 | &total_compressed, | |
515 | max_compressed); | |
c8b97818 CM |
516 | |
517 | if (!ret) { | |
518 | unsigned long offset = total_compressed & | |
09cbfeaf | 519 | (PAGE_SIZE - 1); |
c8b97818 CM |
520 | struct page *page = pages[nr_pages_ret - 1]; |
521 | char *kaddr; | |
522 | ||
523 | /* zero the tail end of the last page, we might be | |
524 | * sending it down to disk | |
525 | */ | |
526 | if (offset) { | |
7ac687d9 | 527 | kaddr = kmap_atomic(page); |
c8b97818 | 528 | memset(kaddr + offset, 0, |
09cbfeaf | 529 | PAGE_SIZE - offset); |
7ac687d9 | 530 | kunmap_atomic(kaddr); |
c8b97818 CM |
531 | } |
532 | will_compress = 1; | |
533 | } | |
534 | } | |
560f7d75 | 535 | cont: |
c8b97818 CM |
536 | if (start == 0) { |
537 | /* lets try to make an inline extent */ | |
771ed689 | 538 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 539 | /* we didn't compress the entire range, try |
771ed689 | 540 | * to make an uncompressed inline extent. |
c8b97818 | 541 | */ |
00361589 JB |
542 | ret = cow_file_range_inline(root, inode, start, end, |
543 | 0, 0, NULL); | |
c8b97818 | 544 | } else { |
771ed689 | 545 | /* try making a compressed inline extent */ |
00361589 | 546 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
547 | total_compressed, |
548 | compress_type, pages); | |
c8b97818 | 549 | } |
79787eaa | 550 | if (ret <= 0) { |
151a41bc JB |
551 | unsigned long clear_flags = EXTENT_DELALLOC | |
552 | EXTENT_DEFRAG; | |
e6eb4314 FM |
553 | unsigned long page_error_op; |
554 | ||
151a41bc | 555 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 556 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 557 | |
771ed689 | 558 | /* |
79787eaa JM |
559 | * inline extent creation worked or returned error, |
560 | * we don't need to create any more async work items. | |
561 | * Unlock and free up our temp pages. | |
771ed689 | 562 | */ |
ba8b04c1 QW |
563 | extent_clear_unlock_delalloc(inode, start, end, end, |
564 | NULL, clear_flags, | |
565 | PAGE_UNLOCK | | |
c2790a2e JB |
566 | PAGE_CLEAR_DIRTY | |
567 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 568 | page_error_op | |
c2790a2e | 569 | PAGE_END_WRITEBACK); |
18513091 WX |
570 | btrfs_free_reserved_data_space_noquota(inode, start, |
571 | end - start + 1); | |
c8b97818 CM |
572 | goto free_pages_out; |
573 | } | |
574 | } | |
575 | ||
576 | if (will_compress) { | |
577 | /* | |
578 | * we aren't doing an inline extent round the compressed size | |
579 | * up to a block size boundary so the allocator does sane | |
580 | * things | |
581 | */ | |
fda2832f | 582 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
583 | |
584 | /* | |
585 | * one last check to make sure the compression is really a | |
586 | * win, compare the page count read with the blocks on disk | |
587 | */ | |
09cbfeaf | 588 | total_in = ALIGN(total_in, PAGE_SIZE); |
c8b97818 CM |
589 | if (total_compressed >= total_in) { |
590 | will_compress = 0; | |
591 | } else { | |
c8b97818 | 592 | num_bytes = total_in; |
c8bb0c8b AS |
593 | *num_added += 1; |
594 | ||
595 | /* | |
596 | * The async work queues will take care of doing actual | |
597 | * allocation on disk for these compressed pages, and | |
598 | * will submit them to the elevator. | |
599 | */ | |
600 | add_async_extent(async_cow, start, num_bytes, | |
601 | total_compressed, pages, nr_pages_ret, | |
602 | compress_type); | |
603 | ||
604 | if (start + num_bytes < end) { | |
605 | start += num_bytes; | |
606 | pages = NULL; | |
607 | cond_resched(); | |
608 | goto again; | |
609 | } | |
610 | return; | |
c8b97818 CM |
611 | } |
612 | } | |
c8bb0c8b | 613 | if (pages) { |
c8b97818 CM |
614 | /* |
615 | * the compression code ran but failed to make things smaller, | |
616 | * free any pages it allocated and our page pointer array | |
617 | */ | |
618 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 619 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 620 | put_page(pages[i]); |
c8b97818 CM |
621 | } |
622 | kfree(pages); | |
623 | pages = NULL; | |
624 | total_compressed = 0; | |
625 | nr_pages_ret = 0; | |
626 | ||
627 | /* flag the file so we don't compress in the future */ | |
3cdde224 | 628 | if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) && |
1e701a32 | 629 | !(BTRFS_I(inode)->force_compress)) { |
a555f810 | 630 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 631 | } |
c8b97818 | 632 | } |
f03d9301 | 633 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
634 | /* |
635 | * No compression, but we still need to write the pages in the file | |
636 | * we've been given so far. redirty the locked page if it corresponds | |
637 | * to our extent and set things up for the async work queue to run | |
638 | * cow_file_range to do the normal delalloc dance. | |
639 | */ | |
640 | if (page_offset(locked_page) >= start && | |
641 | page_offset(locked_page) <= end) | |
642 | __set_page_dirty_nobuffers(locked_page); | |
643 | /* unlocked later on in the async handlers */ | |
644 | ||
645 | if (redirty) | |
646 | extent_range_redirty_for_io(inode, start, end); | |
647 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
648 | BTRFS_COMPRESS_NONE); | |
649 | *num_added += 1; | |
3b951516 | 650 | |
c44f649e | 651 | return; |
771ed689 CM |
652 | |
653 | free_pages_out: | |
654 | for (i = 0; i < nr_pages_ret; i++) { | |
655 | WARN_ON(pages[i]->mapping); | |
09cbfeaf | 656 | put_page(pages[i]); |
771ed689 | 657 | } |
d397712b | 658 | kfree(pages); |
771ed689 | 659 | } |
771ed689 | 660 | |
40ae837b FM |
661 | static void free_async_extent_pages(struct async_extent *async_extent) |
662 | { | |
663 | int i; | |
664 | ||
665 | if (!async_extent->pages) | |
666 | return; | |
667 | ||
668 | for (i = 0; i < async_extent->nr_pages; i++) { | |
669 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 670 | put_page(async_extent->pages[i]); |
40ae837b FM |
671 | } |
672 | kfree(async_extent->pages); | |
673 | async_extent->nr_pages = 0; | |
674 | async_extent->pages = NULL; | |
771ed689 CM |
675 | } |
676 | ||
677 | /* | |
678 | * phase two of compressed writeback. This is the ordered portion | |
679 | * of the code, which only gets called in the order the work was | |
680 | * queued. We walk all the async extents created by compress_file_range | |
681 | * and send them down to the disk. | |
682 | */ | |
dec8f175 | 683 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
684 | struct async_cow *async_cow) |
685 | { | |
686 | struct async_extent *async_extent; | |
687 | u64 alloc_hint = 0; | |
771ed689 CM |
688 | struct btrfs_key ins; |
689 | struct extent_map *em; | |
690 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
691 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
692 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 693 | int ret = 0; |
771ed689 | 694 | |
3e04e7f1 | 695 | again: |
d397712b | 696 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
697 | async_extent = list_entry(async_cow->extents.next, |
698 | struct async_extent, list); | |
699 | list_del(&async_extent->list); | |
c8b97818 | 700 | |
771ed689 CM |
701 | io_tree = &BTRFS_I(inode)->io_tree; |
702 | ||
f5a84ee3 | 703 | retry: |
771ed689 CM |
704 | /* did the compression code fall back to uncompressed IO? */ |
705 | if (!async_extent->pages) { | |
706 | int page_started = 0; | |
707 | unsigned long nr_written = 0; | |
708 | ||
709 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 710 | async_extent->start + |
d0082371 | 711 | async_extent->ram_size - 1); |
771ed689 CM |
712 | |
713 | /* allocate blocks */ | |
f5a84ee3 JB |
714 | ret = cow_file_range(inode, async_cow->locked_page, |
715 | async_extent->start, | |
716 | async_extent->start + | |
717 | async_extent->ram_size - 1, | |
dda3245e WX |
718 | async_extent->start + |
719 | async_extent->ram_size - 1, | |
720 | &page_started, &nr_written, 0, | |
721 | NULL); | |
771ed689 | 722 | |
79787eaa JM |
723 | /* JDM XXX */ |
724 | ||
771ed689 CM |
725 | /* |
726 | * if page_started, cow_file_range inserted an | |
727 | * inline extent and took care of all the unlocking | |
728 | * and IO for us. Otherwise, we need to submit | |
729 | * all those pages down to the drive. | |
730 | */ | |
f5a84ee3 | 731 | if (!page_started && !ret) |
771ed689 CM |
732 | extent_write_locked_range(io_tree, |
733 | inode, async_extent->start, | |
d397712b | 734 | async_extent->start + |
771ed689 CM |
735 | async_extent->ram_size - 1, |
736 | btrfs_get_extent, | |
737 | WB_SYNC_ALL); | |
3e04e7f1 JB |
738 | else if (ret) |
739 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
740 | kfree(async_extent); |
741 | cond_resched(); | |
742 | continue; | |
743 | } | |
744 | ||
745 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 746 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 747 | |
18513091 | 748 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
771ed689 CM |
749 | async_extent->compressed_size, |
750 | async_extent->compressed_size, | |
e570fd27 | 751 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 752 | if (ret) { |
40ae837b | 753 | free_async_extent_pages(async_extent); |
3e04e7f1 | 754 | |
fdf8e2ea JB |
755 | if (ret == -ENOSPC) { |
756 | unlock_extent(io_tree, async_extent->start, | |
757 | async_extent->start + | |
758 | async_extent->ram_size - 1); | |
ce62003f LB |
759 | |
760 | /* | |
761 | * we need to redirty the pages if we decide to | |
762 | * fallback to uncompressed IO, otherwise we | |
763 | * will not submit these pages down to lower | |
764 | * layers. | |
765 | */ | |
766 | extent_range_redirty_for_io(inode, | |
767 | async_extent->start, | |
768 | async_extent->start + | |
769 | async_extent->ram_size - 1); | |
770 | ||
79787eaa | 771 | goto retry; |
fdf8e2ea | 772 | } |
3e04e7f1 | 773 | goto out_free; |
f5a84ee3 | 774 | } |
c2167754 YZ |
775 | /* |
776 | * here we're doing allocation and writeback of the | |
777 | * compressed pages | |
778 | */ | |
779 | btrfs_drop_extent_cache(inode, async_extent->start, | |
780 | async_extent->start + | |
781 | async_extent->ram_size - 1, 0); | |
782 | ||
172ddd60 | 783 | em = alloc_extent_map(); |
b9aa55be LB |
784 | if (!em) { |
785 | ret = -ENOMEM; | |
3e04e7f1 | 786 | goto out_free_reserve; |
b9aa55be | 787 | } |
771ed689 CM |
788 | em->start = async_extent->start; |
789 | em->len = async_extent->ram_size; | |
445a6944 | 790 | em->orig_start = em->start; |
2ab28f32 JB |
791 | em->mod_start = em->start; |
792 | em->mod_len = em->len; | |
c8b97818 | 793 | |
771ed689 CM |
794 | em->block_start = ins.objectid; |
795 | em->block_len = ins.offset; | |
b4939680 | 796 | em->orig_block_len = ins.offset; |
cc95bef6 | 797 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 798 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 799 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
800 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
801 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 802 | em->generation = -1; |
771ed689 | 803 | |
d397712b | 804 | while (1) { |
890871be | 805 | write_lock(&em_tree->lock); |
09a2a8f9 | 806 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 807 | write_unlock(&em_tree->lock); |
771ed689 CM |
808 | if (ret != -EEXIST) { |
809 | free_extent_map(em); | |
810 | break; | |
811 | } | |
812 | btrfs_drop_extent_cache(inode, async_extent->start, | |
813 | async_extent->start + | |
814 | async_extent->ram_size - 1, 0); | |
815 | } | |
816 | ||
3e04e7f1 JB |
817 | if (ret) |
818 | goto out_free_reserve; | |
819 | ||
261507a0 LZ |
820 | ret = btrfs_add_ordered_extent_compress(inode, |
821 | async_extent->start, | |
822 | ins.objectid, | |
823 | async_extent->ram_size, | |
824 | ins.offset, | |
825 | BTRFS_ORDERED_COMPRESSED, | |
826 | async_extent->compress_type); | |
d9f85963 FM |
827 | if (ret) { |
828 | btrfs_drop_extent_cache(inode, async_extent->start, | |
829 | async_extent->start + | |
830 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 831 | goto out_free_reserve; |
d9f85963 | 832 | } |
9cfa3e34 | 833 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
771ed689 | 834 | |
771ed689 CM |
835 | /* |
836 | * clear dirty, set writeback and unlock the pages. | |
837 | */ | |
c2790a2e | 838 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
839 | async_extent->start + |
840 | async_extent->ram_size - 1, | |
a791e35e CM |
841 | async_extent->start + |
842 | async_extent->ram_size - 1, | |
151a41bc JB |
843 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
844 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 845 | PAGE_SET_WRITEBACK); |
771ed689 | 846 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
847 | async_extent->start, |
848 | async_extent->ram_size, | |
849 | ins.objectid, | |
850 | ins.offset, async_extent->pages, | |
851 | async_extent->nr_pages); | |
fce2a4e6 FM |
852 | if (ret) { |
853 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
854 | struct page *p = async_extent->pages[0]; | |
855 | const u64 start = async_extent->start; | |
856 | const u64 end = start + async_extent->ram_size - 1; | |
857 | ||
858 | p->mapping = inode->i_mapping; | |
859 | tree->ops->writepage_end_io_hook(p, start, end, | |
860 | NULL, 0); | |
861 | p->mapping = NULL; | |
ba8b04c1 QW |
862 | extent_clear_unlock_delalloc(inode, start, end, end, |
863 | NULL, 0, | |
fce2a4e6 FM |
864 | PAGE_END_WRITEBACK | |
865 | PAGE_SET_ERROR); | |
40ae837b | 866 | free_async_extent_pages(async_extent); |
fce2a4e6 | 867 | } |
771ed689 CM |
868 | alloc_hint = ins.objectid + ins.offset; |
869 | kfree(async_extent); | |
870 | cond_resched(); | |
871 | } | |
dec8f175 | 872 | return; |
3e04e7f1 | 873 | out_free_reserve: |
9cfa3e34 | 874 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
e570fd27 | 875 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 876 | out_free: |
c2790a2e | 877 | extent_clear_unlock_delalloc(inode, async_extent->start, |
ba8b04c1 QW |
878 | async_extent->start + |
879 | async_extent->ram_size - 1, | |
3e04e7f1 JB |
880 | async_extent->start + |
881 | async_extent->ram_size - 1, | |
c2790a2e | 882 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
883 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
884 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
885 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
886 | PAGE_SET_ERROR); | |
40ae837b | 887 | free_async_extent_pages(async_extent); |
79787eaa | 888 | kfree(async_extent); |
3e04e7f1 | 889 | goto again; |
771ed689 CM |
890 | } |
891 | ||
4b46fce2 JB |
892 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
893 | u64 num_bytes) | |
894 | { | |
895 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
896 | struct extent_map *em; | |
897 | u64 alloc_hint = 0; | |
898 | ||
899 | read_lock(&em_tree->lock); | |
900 | em = search_extent_mapping(em_tree, start, num_bytes); | |
901 | if (em) { | |
902 | /* | |
903 | * if block start isn't an actual block number then find the | |
904 | * first block in this inode and use that as a hint. If that | |
905 | * block is also bogus then just don't worry about it. | |
906 | */ | |
907 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
908 | free_extent_map(em); | |
909 | em = search_extent_mapping(em_tree, 0, 0); | |
910 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
911 | alloc_hint = em->block_start; | |
912 | if (em) | |
913 | free_extent_map(em); | |
914 | } else { | |
915 | alloc_hint = em->block_start; | |
916 | free_extent_map(em); | |
917 | } | |
918 | } | |
919 | read_unlock(&em_tree->lock); | |
920 | ||
921 | return alloc_hint; | |
922 | } | |
923 | ||
771ed689 CM |
924 | /* |
925 | * when extent_io.c finds a delayed allocation range in the file, | |
926 | * the call backs end up in this code. The basic idea is to | |
927 | * allocate extents on disk for the range, and create ordered data structs | |
928 | * in ram to track those extents. | |
929 | * | |
930 | * locked_page is the page that writepage had locked already. We use | |
931 | * it to make sure we don't do extra locks or unlocks. | |
932 | * | |
933 | * *page_started is set to one if we unlock locked_page and do everything | |
934 | * required to start IO on it. It may be clean and already done with | |
935 | * IO when we return. | |
936 | */ | |
00361589 JB |
937 | static noinline int cow_file_range(struct inode *inode, |
938 | struct page *locked_page, | |
dda3245e WX |
939 | u64 start, u64 end, u64 delalloc_end, |
940 | int *page_started, unsigned long *nr_written, | |
941 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 942 | { |
00361589 | 943 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
944 | u64 alloc_hint = 0; |
945 | u64 num_bytes; | |
946 | unsigned long ram_size; | |
947 | u64 disk_num_bytes; | |
948 | u64 cur_alloc_size; | |
949 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
950 | struct btrfs_key ins; |
951 | struct extent_map *em; | |
952 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
953 | int ret = 0; | |
954 | ||
02ecd2c2 JB |
955 | if (btrfs_is_free_space_inode(inode)) { |
956 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
957 | ret = -EINVAL; |
958 | goto out_unlock; | |
02ecd2c2 | 959 | } |
771ed689 | 960 | |
fda2832f | 961 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
962 | num_bytes = max(blocksize, num_bytes); |
963 | disk_num_bytes = num_bytes; | |
771ed689 | 964 | |
4cb5300b | 965 | /* if this is a small write inside eof, kick off defrag */ |
ee22184b | 966 | if (num_bytes < SZ_64K && |
4cb13e5d | 967 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
00361589 | 968 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 969 | |
771ed689 CM |
970 | if (start == 0) { |
971 | /* lets try to make an inline extent */ | |
00361589 JB |
972 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
973 | NULL); | |
771ed689 | 974 | if (ret == 0) { |
ba8b04c1 QW |
975 | extent_clear_unlock_delalloc(inode, start, end, |
976 | delalloc_end, NULL, | |
c2790a2e | 977 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc | 978 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
979 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
980 | PAGE_END_WRITEBACK); | |
18513091 WX |
981 | btrfs_free_reserved_data_space_noquota(inode, start, |
982 | end - start + 1); | |
771ed689 | 983 | *nr_written = *nr_written + |
09cbfeaf | 984 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 985 | *page_started = 1; |
771ed689 | 986 | goto out; |
79787eaa | 987 | } else if (ret < 0) { |
79787eaa | 988 | goto out_unlock; |
771ed689 CM |
989 | } |
990 | } | |
991 | ||
992 | BUG_ON(disk_num_bytes > | |
6c41761f | 993 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 994 | |
4b46fce2 | 995 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
996 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
997 | ||
d397712b | 998 | while (disk_num_bytes > 0) { |
a791e35e CM |
999 | unsigned long op; |
1000 | ||
287a0ab9 | 1001 | cur_alloc_size = disk_num_bytes; |
18513091 | 1002 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
771ed689 | 1003 | root->sectorsize, 0, alloc_hint, |
e570fd27 | 1004 | &ins, 1, 1); |
00361589 | 1005 | if (ret < 0) |
79787eaa | 1006 | goto out_unlock; |
d397712b | 1007 | |
172ddd60 | 1008 | em = alloc_extent_map(); |
b9aa55be LB |
1009 | if (!em) { |
1010 | ret = -ENOMEM; | |
ace68bac | 1011 | goto out_reserve; |
b9aa55be | 1012 | } |
e6dcd2dc | 1013 | em->start = start; |
445a6944 | 1014 | em->orig_start = em->start; |
771ed689 CM |
1015 | ram_size = ins.offset; |
1016 | em->len = ins.offset; | |
2ab28f32 JB |
1017 | em->mod_start = em->start; |
1018 | em->mod_len = em->len; | |
c8b97818 | 1019 | |
e6dcd2dc | 1020 | em->block_start = ins.objectid; |
c8b97818 | 1021 | em->block_len = ins.offset; |
b4939680 | 1022 | em->orig_block_len = ins.offset; |
cc95bef6 | 1023 | em->ram_bytes = ram_size; |
e6dcd2dc | 1024 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 1025 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 1026 | em->generation = -1; |
c8b97818 | 1027 | |
d397712b | 1028 | while (1) { |
890871be | 1029 | write_lock(&em_tree->lock); |
09a2a8f9 | 1030 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1031 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1032 | if (ret != -EEXIST) { |
1033 | free_extent_map(em); | |
1034 | break; | |
1035 | } | |
1036 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1037 | start + ram_size - 1, 0); |
e6dcd2dc | 1038 | } |
ace68bac LB |
1039 | if (ret) |
1040 | goto out_reserve; | |
e6dcd2dc | 1041 | |
98d20f67 | 1042 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1043 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1044 | ram_size, cur_alloc_size, 0); |
ace68bac | 1045 | if (ret) |
d9f85963 | 1046 | goto out_drop_extent_cache; |
c8b97818 | 1047 | |
17d217fe YZ |
1048 | if (root->root_key.objectid == |
1049 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1050 | ret = btrfs_reloc_clone_csums(inode, start, | |
1051 | cur_alloc_size); | |
00361589 | 1052 | if (ret) |
d9f85963 | 1053 | goto out_drop_extent_cache; |
17d217fe YZ |
1054 | } |
1055 | ||
9cfa3e34 FM |
1056 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
1057 | ||
d397712b | 1058 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1059 | break; |
d397712b | 1060 | |
c8b97818 CM |
1061 | /* we're not doing compressed IO, don't unlock the first |
1062 | * page (which the caller expects to stay locked), don't | |
1063 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1064 | * |
1065 | * Do set the Private2 bit so we know this page was properly | |
1066 | * setup for writepage | |
c8b97818 | 1067 | */ |
c2790a2e JB |
1068 | op = unlock ? PAGE_UNLOCK : 0; |
1069 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1070 | |
c2790a2e | 1071 | extent_clear_unlock_delalloc(inode, start, |
ba8b04c1 QW |
1072 | start + ram_size - 1, |
1073 | delalloc_end, locked_page, | |
c2790a2e JB |
1074 | EXTENT_LOCKED | EXTENT_DELALLOC, |
1075 | op); | |
c8b97818 | 1076 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1077 | num_bytes -= cur_alloc_size; |
1078 | alloc_hint = ins.objectid + ins.offset; | |
1079 | start += cur_alloc_size; | |
b888db2b | 1080 | } |
79787eaa | 1081 | out: |
be20aa9d | 1082 | return ret; |
b7d5b0a8 | 1083 | |
d9f85963 FM |
1084 | out_drop_extent_cache: |
1085 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1086 | out_reserve: |
9cfa3e34 | 1087 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
e570fd27 | 1088 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 1089 | out_unlock: |
ba8b04c1 QW |
1090 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
1091 | locked_page, | |
151a41bc JB |
1092 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1093 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1094 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1095 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1096 | goto out; |
771ed689 | 1097 | } |
c8b97818 | 1098 | |
771ed689 CM |
1099 | /* |
1100 | * work queue call back to started compression on a file and pages | |
1101 | */ | |
1102 | static noinline void async_cow_start(struct btrfs_work *work) | |
1103 | { | |
1104 | struct async_cow *async_cow; | |
1105 | int num_added = 0; | |
1106 | async_cow = container_of(work, struct async_cow, work); | |
1107 | ||
1108 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1109 | async_cow->start, async_cow->end, async_cow, | |
1110 | &num_added); | |
8180ef88 | 1111 | if (num_added == 0) { |
cb77fcd8 | 1112 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1113 | async_cow->inode = NULL; |
8180ef88 | 1114 | } |
771ed689 CM |
1115 | } |
1116 | ||
1117 | /* | |
1118 | * work queue call back to submit previously compressed pages | |
1119 | */ | |
1120 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1121 | { | |
1122 | struct async_cow *async_cow; | |
1123 | struct btrfs_root *root; | |
1124 | unsigned long nr_pages; | |
1125 | ||
1126 | async_cow = container_of(work, struct async_cow, work); | |
1127 | ||
1128 | root = async_cow->root; | |
09cbfeaf KS |
1129 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1130 | PAGE_SHIFT; | |
771ed689 | 1131 | |
ee863954 DS |
1132 | /* |
1133 | * atomic_sub_return implies a barrier for waitqueue_active | |
1134 | */ | |
66657b31 | 1135 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
ee22184b | 1136 | 5 * SZ_1M && |
771ed689 CM |
1137 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1138 | wake_up(&root->fs_info->async_submit_wait); | |
1139 | ||
d397712b | 1140 | if (async_cow->inode) |
771ed689 | 1141 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1142 | } |
c8b97818 | 1143 | |
771ed689 CM |
1144 | static noinline void async_cow_free(struct btrfs_work *work) |
1145 | { | |
1146 | struct async_cow *async_cow; | |
1147 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1148 | if (async_cow->inode) |
cb77fcd8 | 1149 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1150 | kfree(async_cow); |
1151 | } | |
1152 | ||
1153 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1154 | u64 start, u64 end, int *page_started, | |
1155 | unsigned long *nr_written) | |
1156 | { | |
1157 | struct async_cow *async_cow; | |
1158 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1159 | unsigned long nr_pages; | |
1160 | u64 cur_end; | |
ee22184b | 1161 | int limit = 10 * SZ_1M; |
771ed689 | 1162 | |
a3429ab7 CM |
1163 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1164 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1165 | while (start < end) { |
771ed689 | 1166 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1167 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1168 | async_cow->inode = igrab(inode); |
771ed689 CM |
1169 | async_cow->root = root; |
1170 | async_cow->locked_page = locked_page; | |
1171 | async_cow->start = start; | |
1172 | ||
f79707b0 | 1173 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
3cdde224 | 1174 | !btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1175 | cur_end = end; |
1176 | else | |
ee22184b | 1177 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1178 | |
1179 | async_cow->end = cur_end; | |
1180 | INIT_LIST_HEAD(&async_cow->extents); | |
1181 | ||
9e0af237 LB |
1182 | btrfs_init_work(&async_cow->work, |
1183 | btrfs_delalloc_helper, | |
1184 | async_cow_start, async_cow_submit, | |
1185 | async_cow_free); | |
771ed689 | 1186 | |
09cbfeaf KS |
1187 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1188 | PAGE_SHIFT; | |
771ed689 CM |
1189 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
1190 | ||
afe3d242 QW |
1191 | btrfs_queue_work(root->fs_info->delalloc_workers, |
1192 | &async_cow->work); | |
771ed689 CM |
1193 | |
1194 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1195 | wait_event(root->fs_info->async_submit_wait, | |
1196 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1197 | limit)); | |
1198 | } | |
1199 | ||
d397712b | 1200 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1201 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1202 | wait_event(root->fs_info->async_submit_wait, | |
1203 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1204 | 0)); | |
1205 | } | |
1206 | ||
1207 | *nr_written += nr_pages; | |
1208 | start = cur_end + 1; | |
1209 | } | |
1210 | *page_started = 1; | |
1211 | return 0; | |
be20aa9d CM |
1212 | } |
1213 | ||
d397712b | 1214 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1215 | u64 bytenr, u64 num_bytes) |
1216 | { | |
1217 | int ret; | |
1218 | struct btrfs_ordered_sum *sums; | |
1219 | LIST_HEAD(list); | |
1220 | ||
07d400a6 | 1221 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1222 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1223 | if (ret == 0 && list_empty(&list)) |
1224 | return 0; | |
1225 | ||
1226 | while (!list_empty(&list)) { | |
1227 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1228 | list_del(&sums->list); | |
1229 | kfree(sums); | |
1230 | } | |
1231 | return 1; | |
1232 | } | |
1233 | ||
d352ac68 CM |
1234 | /* |
1235 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1236 | * of the extents that exist in the file, and COWs the file as required. | |
1237 | * | |
1238 | * If no cow copies or snapshots exist, we write directly to the existing | |
1239 | * blocks on disk | |
1240 | */ | |
7f366cfe CM |
1241 | static noinline int run_delalloc_nocow(struct inode *inode, |
1242 | struct page *locked_page, | |
771ed689 CM |
1243 | u64 start, u64 end, int *page_started, int force, |
1244 | unsigned long *nr_written) | |
be20aa9d | 1245 | { |
be20aa9d | 1246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1247 | struct btrfs_trans_handle *trans; |
be20aa9d | 1248 | struct extent_buffer *leaf; |
be20aa9d | 1249 | struct btrfs_path *path; |
80ff3856 | 1250 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1251 | struct btrfs_key found_key; |
80ff3856 YZ |
1252 | u64 cow_start; |
1253 | u64 cur_offset; | |
1254 | u64 extent_end; | |
5d4f98a2 | 1255 | u64 extent_offset; |
80ff3856 YZ |
1256 | u64 disk_bytenr; |
1257 | u64 num_bytes; | |
b4939680 | 1258 | u64 disk_num_bytes; |
cc95bef6 | 1259 | u64 ram_bytes; |
80ff3856 | 1260 | int extent_type; |
79787eaa | 1261 | int ret, err; |
d899e052 | 1262 | int type; |
80ff3856 YZ |
1263 | int nocow; |
1264 | int check_prev = 1; | |
82d5902d | 1265 | bool nolock; |
33345d01 | 1266 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1267 | |
1268 | path = btrfs_alloc_path(); | |
17ca04af | 1269 | if (!path) { |
ba8b04c1 QW |
1270 | extent_clear_unlock_delalloc(inode, start, end, end, |
1271 | locked_page, | |
c2790a2e | 1272 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1273 | EXTENT_DO_ACCOUNTING | |
1274 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1275 | PAGE_CLEAR_DIRTY | |
1276 | PAGE_SET_WRITEBACK | | |
1277 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1278 | return -ENOMEM; |
17ca04af | 1279 | } |
82d5902d | 1280 | |
83eea1f1 | 1281 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1282 | |
1283 | if (nolock) | |
7a7eaa40 | 1284 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1285 | else |
7a7eaa40 | 1286 | trans = btrfs_join_transaction(root); |
ff5714cc | 1287 | |
79787eaa | 1288 | if (IS_ERR(trans)) { |
ba8b04c1 QW |
1289 | extent_clear_unlock_delalloc(inode, start, end, end, |
1290 | locked_page, | |
c2790a2e | 1291 | EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
1292 | EXTENT_DO_ACCOUNTING | |
1293 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1294 | PAGE_CLEAR_DIRTY | |
1295 | PAGE_SET_WRITEBACK | | |
1296 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1297 | btrfs_free_path(path); |
1298 | return PTR_ERR(trans); | |
1299 | } | |
1300 | ||
74b21075 | 1301 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1302 | |
80ff3856 YZ |
1303 | cow_start = (u64)-1; |
1304 | cur_offset = start; | |
1305 | while (1) { | |
33345d01 | 1306 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1307 | cur_offset, 0); |
d788a349 | 1308 | if (ret < 0) |
79787eaa | 1309 | goto error; |
80ff3856 YZ |
1310 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1311 | leaf = path->nodes[0]; | |
1312 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1313 | path->slots[0] - 1); | |
33345d01 | 1314 | if (found_key.objectid == ino && |
80ff3856 YZ |
1315 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1316 | path->slots[0]--; | |
1317 | } | |
1318 | check_prev = 0; | |
1319 | next_slot: | |
1320 | leaf = path->nodes[0]; | |
1321 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1322 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1323 | if (ret < 0) |
79787eaa | 1324 | goto error; |
80ff3856 YZ |
1325 | if (ret > 0) |
1326 | break; | |
1327 | leaf = path->nodes[0]; | |
1328 | } | |
be20aa9d | 1329 | |
80ff3856 YZ |
1330 | nocow = 0; |
1331 | disk_bytenr = 0; | |
17d217fe | 1332 | num_bytes = 0; |
80ff3856 YZ |
1333 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1334 | ||
1d512cb7 FM |
1335 | if (found_key.objectid > ino) |
1336 | break; | |
1337 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1338 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1339 | path->slots[0]++; | |
1340 | goto next_slot; | |
1341 | } | |
1342 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1343 | found_key.offset > end) |
1344 | break; | |
1345 | ||
1346 | if (found_key.offset > cur_offset) { | |
1347 | extent_end = found_key.offset; | |
e9061e21 | 1348 | extent_type = 0; |
80ff3856 YZ |
1349 | goto out_check; |
1350 | } | |
1351 | ||
1352 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1353 | struct btrfs_file_extent_item); | |
1354 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1355 | ||
cc95bef6 | 1356 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1357 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1358 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1359 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1360 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1361 | extent_end = found_key.offset + |
1362 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1363 | disk_num_bytes = |
1364 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1365 | if (extent_end <= start) { |
1366 | path->slots[0]++; | |
1367 | goto next_slot; | |
1368 | } | |
17d217fe YZ |
1369 | if (disk_bytenr == 0) |
1370 | goto out_check; | |
80ff3856 YZ |
1371 | if (btrfs_file_extent_compression(leaf, fi) || |
1372 | btrfs_file_extent_encryption(leaf, fi) || | |
1373 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1374 | goto out_check; | |
d899e052 YZ |
1375 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1376 | goto out_check; | |
d2fb3437 | 1377 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1378 | goto out_check; |
33345d01 | 1379 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1380 | found_key.offset - |
1381 | extent_offset, disk_bytenr)) | |
17d217fe | 1382 | goto out_check; |
5d4f98a2 | 1383 | disk_bytenr += extent_offset; |
17d217fe YZ |
1384 | disk_bytenr += cur_offset - found_key.offset; |
1385 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1386 | /* |
1387 | * if there are pending snapshots for this root, | |
1388 | * we fall into common COW way. | |
1389 | */ | |
1390 | if (!nolock) { | |
9ea24bbe | 1391 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1392 | if (!err) |
1393 | goto out_check; | |
1394 | } | |
17d217fe YZ |
1395 | /* |
1396 | * force cow if csum exists in the range. | |
1397 | * this ensure that csum for a given extent are | |
1398 | * either valid or do not exist. | |
1399 | */ | |
1400 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1401 | goto out_check; | |
f78c436c FM |
1402 | if (!btrfs_inc_nocow_writers(root->fs_info, |
1403 | disk_bytenr)) | |
1404 | goto out_check; | |
80ff3856 YZ |
1405 | nocow = 1; |
1406 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1407 | extent_end = found_key.offset + | |
514ac8ad CM |
1408 | btrfs_file_extent_inline_len(leaf, |
1409 | path->slots[0], fi); | |
80ff3856 YZ |
1410 | extent_end = ALIGN(extent_end, root->sectorsize); |
1411 | } else { | |
1412 | BUG_ON(1); | |
1413 | } | |
1414 | out_check: | |
1415 | if (extent_end <= start) { | |
1416 | path->slots[0]++; | |
e9894fd3 | 1417 | if (!nolock && nocow) |
9ea24bbe | 1418 | btrfs_end_write_no_snapshoting(root); |
f78c436c FM |
1419 | if (nocow) |
1420 | btrfs_dec_nocow_writers(root->fs_info, | |
1421 | disk_bytenr); | |
80ff3856 YZ |
1422 | goto next_slot; |
1423 | } | |
1424 | if (!nocow) { | |
1425 | if (cow_start == (u64)-1) | |
1426 | cow_start = cur_offset; | |
1427 | cur_offset = extent_end; | |
1428 | if (cur_offset > end) | |
1429 | break; | |
1430 | path->slots[0]++; | |
1431 | goto next_slot; | |
7ea394f1 YZ |
1432 | } |
1433 | ||
b3b4aa74 | 1434 | btrfs_release_path(path); |
80ff3856 | 1435 | if (cow_start != (u64)-1) { |
00361589 JB |
1436 | ret = cow_file_range(inode, locked_page, |
1437 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1438 | end, page_started, nr_written, 1, |
1439 | NULL); | |
e9894fd3 WS |
1440 | if (ret) { |
1441 | if (!nolock && nocow) | |
9ea24bbe | 1442 | btrfs_end_write_no_snapshoting(root); |
f78c436c FM |
1443 | if (nocow) |
1444 | btrfs_dec_nocow_writers(root->fs_info, | |
1445 | disk_bytenr); | |
79787eaa | 1446 | goto error; |
e9894fd3 | 1447 | } |
80ff3856 | 1448 | cow_start = (u64)-1; |
7ea394f1 | 1449 | } |
80ff3856 | 1450 | |
d899e052 YZ |
1451 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1452 | struct extent_map *em; | |
1453 | struct extent_map_tree *em_tree; | |
1454 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1455 | em = alloc_extent_map(); |
79787eaa | 1456 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1457 | em->start = cur_offset; |
70c8a91c | 1458 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1459 | em->len = num_bytes; |
1460 | em->block_len = num_bytes; | |
1461 | em->block_start = disk_bytenr; | |
b4939680 | 1462 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1463 | em->ram_bytes = ram_bytes; |
d899e052 | 1464 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1465 | em->mod_start = em->start; |
1466 | em->mod_len = em->len; | |
d899e052 | 1467 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1468 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1469 | em->generation = -1; |
d899e052 | 1470 | while (1) { |
890871be | 1471 | write_lock(&em_tree->lock); |
09a2a8f9 | 1472 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1473 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1474 | if (ret != -EEXIST) { |
1475 | free_extent_map(em); | |
1476 | break; | |
1477 | } | |
1478 | btrfs_drop_extent_cache(inode, em->start, | |
1479 | em->start + em->len - 1, 0); | |
1480 | } | |
1481 | type = BTRFS_ORDERED_PREALLOC; | |
1482 | } else { | |
1483 | type = BTRFS_ORDERED_NOCOW; | |
1484 | } | |
80ff3856 YZ |
1485 | |
1486 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1487 | num_bytes, num_bytes, type); |
f78c436c FM |
1488 | if (nocow) |
1489 | btrfs_dec_nocow_writers(root->fs_info, disk_bytenr); | |
79787eaa | 1490 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1491 | |
efa56464 YZ |
1492 | if (root->root_key.objectid == |
1493 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1494 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1495 | num_bytes); | |
e9894fd3 WS |
1496 | if (ret) { |
1497 | if (!nolock && nocow) | |
9ea24bbe | 1498 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1499 | goto error; |
e9894fd3 | 1500 | } |
efa56464 YZ |
1501 | } |
1502 | ||
c2790a2e | 1503 | extent_clear_unlock_delalloc(inode, cur_offset, |
ba8b04c1 | 1504 | cur_offset + num_bytes - 1, end, |
c2790a2e | 1505 | locked_page, EXTENT_LOCKED | |
18513091 WX |
1506 | EXTENT_DELALLOC | |
1507 | EXTENT_CLEAR_DATA_RESV, | |
1508 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); | |
1509 | ||
e9894fd3 | 1510 | if (!nolock && nocow) |
9ea24bbe | 1511 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1512 | cur_offset = extent_end; |
1513 | if (cur_offset > end) | |
1514 | break; | |
be20aa9d | 1515 | } |
b3b4aa74 | 1516 | btrfs_release_path(path); |
80ff3856 | 1517 | |
17ca04af | 1518 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1519 | cow_start = cur_offset; |
17ca04af JB |
1520 | cur_offset = end; |
1521 | } | |
1522 | ||
80ff3856 | 1523 | if (cow_start != (u64)-1) { |
dda3245e WX |
1524 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1525 | page_started, nr_written, 1, NULL); | |
d788a349 | 1526 | if (ret) |
79787eaa | 1527 | goto error; |
80ff3856 YZ |
1528 | } |
1529 | ||
79787eaa | 1530 | error: |
a698d075 | 1531 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1532 | if (!ret) |
1533 | ret = err; | |
1534 | ||
17ca04af | 1535 | if (ret && cur_offset < end) |
ba8b04c1 | 1536 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
c2790a2e | 1537 | locked_page, EXTENT_LOCKED | |
151a41bc JB |
1538 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1539 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1540 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1541 | PAGE_SET_WRITEBACK | |
1542 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1543 | btrfs_free_path(path); |
79787eaa | 1544 | return ret; |
be20aa9d CM |
1545 | } |
1546 | ||
47059d93 WS |
1547 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1548 | { | |
1549 | ||
1550 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1551 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1552 | return 0; | |
1553 | ||
1554 | /* | |
1555 | * @defrag_bytes is a hint value, no spinlock held here, | |
1556 | * if is not zero, it means the file is defragging. | |
1557 | * Force cow if given extent needs to be defragged. | |
1558 | */ | |
1559 | if (BTRFS_I(inode)->defrag_bytes && | |
1560 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1561 | EXTENT_DEFRAG, 0, NULL)) | |
1562 | return 1; | |
1563 | ||
1564 | return 0; | |
1565 | } | |
1566 | ||
d352ac68 CM |
1567 | /* |
1568 | * extent_io.c call back to do delayed allocation processing | |
1569 | */ | |
c8b97818 | 1570 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1571 | u64 start, u64 end, int *page_started, |
1572 | unsigned long *nr_written) | |
be20aa9d | 1573 | { |
be20aa9d | 1574 | int ret; |
47059d93 | 1575 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1576 | |
47059d93 | 1577 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1578 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1579 | page_started, 1, nr_written); |
47059d93 | 1580 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1581 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1582 | page_started, 0, nr_written); |
7816030e | 1583 | } else if (!inode_need_compress(inode)) { |
dda3245e WX |
1584 | ret = cow_file_range(inode, locked_page, start, end, end, |
1585 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1586 | } else { |
1587 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1588 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1589 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1590 | page_started, nr_written); |
7ddf5a42 | 1591 | } |
b888db2b CM |
1592 | return ret; |
1593 | } | |
1594 | ||
1bf85046 JM |
1595 | static void btrfs_split_extent_hook(struct inode *inode, |
1596 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1597 | { |
dcab6a3b JB |
1598 | u64 size; |
1599 | ||
0ca1f7ce | 1600 | /* not delalloc, ignore it */ |
9ed74f2d | 1601 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1602 | return; |
9ed74f2d | 1603 | |
dcab6a3b JB |
1604 | size = orig->end - orig->start + 1; |
1605 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1606 | u64 num_extents; | |
1607 | u64 new_size; | |
1608 | ||
1609 | /* | |
ba117213 JB |
1610 | * See the explanation in btrfs_merge_extent_hook, the same |
1611 | * applies here, just in reverse. | |
dcab6a3b JB |
1612 | */ |
1613 | new_size = orig->end - split + 1; | |
ba117213 | 1614 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
dcab6a3b | 1615 | BTRFS_MAX_EXTENT_SIZE); |
ba117213 JB |
1616 | new_size = split - orig->start; |
1617 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1618 | BTRFS_MAX_EXTENT_SIZE); | |
1619 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1620 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) | |
dcab6a3b JB |
1621 | return; |
1622 | } | |
1623 | ||
9e0baf60 JB |
1624 | spin_lock(&BTRFS_I(inode)->lock); |
1625 | BTRFS_I(inode)->outstanding_extents++; | |
1626 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1627 | } |
1628 | ||
1629 | /* | |
1630 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1631 | * extents so we can keep track of new extents that are just merged onto old | |
1632 | * extents, such as when we are doing sequential writes, so we can properly | |
1633 | * account for the metadata space we'll need. | |
1634 | */ | |
1bf85046 JM |
1635 | static void btrfs_merge_extent_hook(struct inode *inode, |
1636 | struct extent_state *new, | |
1637 | struct extent_state *other) | |
9ed74f2d | 1638 | { |
dcab6a3b JB |
1639 | u64 new_size, old_size; |
1640 | u64 num_extents; | |
1641 | ||
9ed74f2d JB |
1642 | /* not delalloc, ignore it */ |
1643 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1644 | return; |
9ed74f2d | 1645 | |
8461a3de JB |
1646 | if (new->start > other->start) |
1647 | new_size = new->end - other->start + 1; | |
1648 | else | |
1649 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1650 | |
1651 | /* we're not bigger than the max, unreserve the space and go */ | |
1652 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1653 | spin_lock(&BTRFS_I(inode)->lock); | |
1654 | BTRFS_I(inode)->outstanding_extents--; | |
1655 | spin_unlock(&BTRFS_I(inode)->lock); | |
1656 | return; | |
1657 | } | |
1658 | ||
1659 | /* | |
ba117213 JB |
1660 | * We have to add up either side to figure out how many extents were |
1661 | * accounted for before we merged into one big extent. If the number of | |
1662 | * extents we accounted for is <= the amount we need for the new range | |
1663 | * then we can return, otherwise drop. Think of it like this | |
1664 | * | |
1665 | * [ 4k][MAX_SIZE] | |
1666 | * | |
1667 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1668 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1669 | * we have 1 so they are == and we can return. But in this case | |
1670 | * | |
1671 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1672 | * | |
1673 | * Each range on their own accounts for 2 extents, but merged together | |
1674 | * they are only 3 extents worth of accounting, so we need to drop in | |
1675 | * this case. | |
dcab6a3b | 1676 | */ |
ba117213 | 1677 | old_size = other->end - other->start + 1; |
dcab6a3b JB |
1678 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
1679 | BTRFS_MAX_EXTENT_SIZE); | |
ba117213 JB |
1680 | old_size = new->end - new->start + 1; |
1681 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1682 | BTRFS_MAX_EXTENT_SIZE); | |
1683 | ||
dcab6a3b | 1684 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
ba117213 | 1685 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
dcab6a3b JB |
1686 | return; |
1687 | ||
9e0baf60 JB |
1688 | spin_lock(&BTRFS_I(inode)->lock); |
1689 | BTRFS_I(inode)->outstanding_extents--; | |
1690 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1691 | } |
1692 | ||
eb73c1b7 MX |
1693 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1694 | struct inode *inode) | |
1695 | { | |
1696 | spin_lock(&root->delalloc_lock); | |
1697 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1698 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1699 | &root->delalloc_inodes); | |
1700 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1701 | &BTRFS_I(inode)->runtime_flags); | |
1702 | root->nr_delalloc_inodes++; | |
1703 | if (root->nr_delalloc_inodes == 1) { | |
1704 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1705 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1706 | list_add_tail(&root->delalloc_root, | |
1707 | &root->fs_info->delalloc_roots); | |
1708 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1709 | } | |
1710 | } | |
1711 | spin_unlock(&root->delalloc_lock); | |
1712 | } | |
1713 | ||
1714 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1715 | struct inode *inode) | |
1716 | { | |
1717 | spin_lock(&root->delalloc_lock); | |
1718 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1719 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1720 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1721 | &BTRFS_I(inode)->runtime_flags); | |
1722 | root->nr_delalloc_inodes--; | |
1723 | if (!root->nr_delalloc_inodes) { | |
1724 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1725 | BUG_ON(list_empty(&root->delalloc_root)); | |
1726 | list_del_init(&root->delalloc_root); | |
1727 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1728 | } | |
1729 | } | |
1730 | spin_unlock(&root->delalloc_lock); | |
1731 | } | |
1732 | ||
d352ac68 CM |
1733 | /* |
1734 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1735 | * bytes in this file, and to maintain the list of inodes that | |
1736 | * have pending delalloc work to be done. | |
1737 | */ | |
1bf85046 | 1738 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1739 | struct extent_state *state, unsigned *bits) |
291d673e | 1740 | { |
9ed74f2d | 1741 | |
47059d93 WS |
1742 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1743 | WARN_ON(1); | |
75eff68e CM |
1744 | /* |
1745 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1746 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1747 | * bit, which is only set or cleared with irqs on |
1748 | */ | |
0ca1f7ce | 1749 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1750 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1751 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1752 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1753 | |
9e0baf60 | 1754 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1755 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1756 | } else { |
1757 | spin_lock(&BTRFS_I(inode)->lock); | |
1758 | BTRFS_I(inode)->outstanding_extents++; | |
1759 | spin_unlock(&BTRFS_I(inode)->lock); | |
1760 | } | |
287a0ab9 | 1761 | |
6a3891c5 | 1762 | /* For sanity tests */ |
f5ee5c9a | 1763 | if (btrfs_is_testing(root->fs_info)) |
6a3891c5 JB |
1764 | return; |
1765 | ||
963d678b MX |
1766 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1767 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1768 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1769 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1770 | if (*bits & EXTENT_DEFRAG) |
1771 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1772 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1773 | &BTRFS_I(inode)->runtime_flags)) |
1774 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1775 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1776 | } |
291d673e CM |
1777 | } |
1778 | ||
d352ac68 CM |
1779 | /* |
1780 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1781 | */ | |
1bf85046 | 1782 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1783 | struct extent_state *state, |
9ee49a04 | 1784 | unsigned *bits) |
291d673e | 1785 | { |
47059d93 | 1786 | u64 len = state->end + 1 - state->start; |
dcab6a3b JB |
1787 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
1788 | BTRFS_MAX_EXTENT_SIZE); | |
47059d93 WS |
1789 | |
1790 | spin_lock(&BTRFS_I(inode)->lock); | |
1791 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1792 | BTRFS_I(inode)->defrag_bytes -= len; | |
1793 | spin_unlock(&BTRFS_I(inode)->lock); | |
1794 | ||
75eff68e CM |
1795 | /* |
1796 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1797 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1798 | * bit, which is only set or cleared with irqs on |
1799 | */ | |
0ca1f7ce | 1800 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1802 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1803 | |
9e0baf60 | 1804 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1805 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1806 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1807 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1808 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1809 | spin_unlock(&BTRFS_I(inode)->lock); |
1810 | } | |
0ca1f7ce | 1811 | |
b6d08f06 JB |
1812 | /* |
1813 | * We don't reserve metadata space for space cache inodes so we | |
1814 | * don't need to call dellalloc_release_metadata if there is an | |
1815 | * error. | |
1816 | */ | |
1817 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1818 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1819 | btrfs_delalloc_release_metadata(inode, len); |
1820 | ||
6a3891c5 | 1821 | /* For sanity tests. */ |
f5ee5c9a | 1822 | if (btrfs_is_testing(root->fs_info)) |
6a3891c5 JB |
1823 | return; |
1824 | ||
0cb59c99 | 1825 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
18513091 WX |
1826 | && do_list && !(state->state & EXTENT_NORESERVE) |
1827 | && (*bits & (EXTENT_DO_ACCOUNTING | | |
1828 | EXTENT_CLEAR_DATA_RESV))) | |
51773bec QW |
1829 | btrfs_free_reserved_data_space_noquota(inode, |
1830 | state->start, len); | |
9ed74f2d | 1831 | |
963d678b MX |
1832 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1833 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1834 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1835 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1836 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1837 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1838 | &BTRFS_I(inode)->runtime_flags)) |
1839 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1840 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1841 | } |
291d673e CM |
1842 | } |
1843 | ||
d352ac68 CM |
1844 | /* |
1845 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1846 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1847 | * |
1848 | * return 1 if page cannot be merged to bio | |
1849 | * return 0 if page can be merged to bio | |
1850 | * return error otherwise | |
d352ac68 | 1851 | */ |
81a75f67 | 1852 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
1853 | size_t size, struct bio *bio, |
1854 | unsigned long bio_flags) | |
239b14b3 CM |
1855 | { |
1856 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
4f024f37 | 1857 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1858 | u64 length = 0; |
1859 | u64 map_length; | |
239b14b3 CM |
1860 | int ret; |
1861 | ||
771ed689 CM |
1862 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1863 | return 0; | |
1864 | ||
4f024f37 | 1865 | length = bio->bi_iter.bi_size; |
239b14b3 | 1866 | map_length = length; |
b3d3fa51 | 1867 | ret = btrfs_map_block(root->fs_info, bio_op(bio), logical, |
f188591e | 1868 | &map_length, NULL, 0); |
6f034ece LB |
1869 | if (ret < 0) |
1870 | return ret; | |
d397712b | 1871 | if (map_length < length + size) |
239b14b3 | 1872 | return 1; |
3444a972 | 1873 | return 0; |
239b14b3 CM |
1874 | } |
1875 | ||
d352ac68 CM |
1876 | /* |
1877 | * in order to insert checksums into the metadata in large chunks, | |
1878 | * we wait until bio submission time. All the pages in the bio are | |
1879 | * checksummed and sums are attached onto the ordered extent record. | |
1880 | * | |
1881 | * At IO completion time the cums attached on the ordered extent record | |
1882 | * are inserted into the btree | |
1883 | */ | |
81a75f67 MC |
1884 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
1885 | int mirror_num, unsigned long bio_flags, | |
eaf25d93 | 1886 | u64 bio_offset) |
065631f6 | 1887 | { |
065631f6 | 1888 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1889 | int ret = 0; |
e015640f | 1890 | |
d20f7043 | 1891 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1892 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1893 | return 0; |
1894 | } | |
e015640f | 1895 | |
4a69a410 CM |
1896 | /* |
1897 | * in order to insert checksums into the metadata in large chunks, | |
1898 | * we wait until bio submission time. All the pages in the bio are | |
1899 | * checksummed and sums are attached onto the ordered extent record. | |
1900 | * | |
1901 | * At IO completion time the cums attached on the ordered extent record | |
1902 | * are inserted into the btree | |
1903 | */ | |
81a75f67 | 1904 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
eaf25d93 CM |
1905 | int mirror_num, unsigned long bio_flags, |
1906 | u64 bio_offset) | |
4a69a410 CM |
1907 | { |
1908 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1909 | int ret; |
1910 | ||
81a75f67 | 1911 | ret = btrfs_map_bio(root, bio, mirror_num, 1); |
4246a0b6 CH |
1912 | if (ret) { |
1913 | bio->bi_error = ret; | |
1914 | bio_endio(bio); | |
1915 | } | |
61891923 | 1916 | return ret; |
44b8bd7e CM |
1917 | } |
1918 | ||
d352ac68 | 1919 | /* |
cad321ad CM |
1920 | * extent_io.c submission hook. This does the right thing for csum calculation |
1921 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1922 | */ |
81a75f67 | 1923 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
eaf25d93 CM |
1924 | int mirror_num, unsigned long bio_flags, |
1925 | u64 bio_offset) | |
44b8bd7e CM |
1926 | { |
1927 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
0d51e28a | 1928 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
44b8bd7e | 1929 | int ret = 0; |
19b9bdb0 | 1930 | int skip_sum; |
b812ce28 | 1931 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1932 | |
6cbff00f | 1933 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1934 | |
83eea1f1 | 1935 | if (btrfs_is_free_space_inode(inode)) |
0d51e28a | 1936 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1937 | |
37226b21 | 1938 | if (bio_op(bio) != REQ_OP_WRITE) { |
5fd02043 JB |
1939 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1940 | if (ret) | |
61891923 | 1941 | goto out; |
5fd02043 | 1942 | |
d20f7043 | 1943 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1944 | ret = btrfs_submit_compressed_read(inode, bio, |
1945 | mirror_num, | |
1946 | bio_flags); | |
1947 | goto out; | |
c2db1073 TI |
1948 | } else if (!skip_sum) { |
1949 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1950 | if (ret) | |
61891923 | 1951 | goto out; |
c2db1073 | 1952 | } |
4d1b5fb4 | 1953 | goto mapit; |
b812ce28 | 1954 | } else if (async && !skip_sum) { |
17d217fe YZ |
1955 | /* csum items have already been cloned */ |
1956 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1957 | goto mapit; | |
19b9bdb0 | 1958 | /* we're doing a write, do the async checksumming */ |
61891923 | 1959 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
81a75f67 | 1960 | inode, bio, mirror_num, |
eaf25d93 CM |
1961 | bio_flags, bio_offset, |
1962 | __btrfs_submit_bio_start, | |
4a69a410 | 1963 | __btrfs_submit_bio_done); |
61891923 | 1964 | goto out; |
b812ce28 JB |
1965 | } else if (!skip_sum) { |
1966 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1967 | if (ret) | |
1968 | goto out; | |
19b9bdb0 CM |
1969 | } |
1970 | ||
0b86a832 | 1971 | mapit: |
81a75f67 | 1972 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
61891923 SB |
1973 | |
1974 | out: | |
4246a0b6 CH |
1975 | if (ret < 0) { |
1976 | bio->bi_error = ret; | |
1977 | bio_endio(bio); | |
1978 | } | |
61891923 | 1979 | return ret; |
065631f6 | 1980 | } |
6885f308 | 1981 | |
d352ac68 CM |
1982 | /* |
1983 | * given a list of ordered sums record them in the inode. This happens | |
1984 | * at IO completion time based on sums calculated at bio submission time. | |
1985 | */ | |
ba1da2f4 | 1986 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1987 | struct inode *inode, u64 file_offset, |
1988 | struct list_head *list) | |
1989 | { | |
e6dcd2dc CM |
1990 | struct btrfs_ordered_sum *sum; |
1991 | ||
c6e30871 | 1992 | list_for_each_entry(sum, list, list) { |
39847c4d | 1993 | trans->adding_csums = 1; |
d20f7043 CM |
1994 | btrfs_csum_file_blocks(trans, |
1995 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1996 | trans->adding_csums = 0; |
e6dcd2dc CM |
1997 | } |
1998 | return 0; | |
1999 | } | |
2000 | ||
2ac55d41 | 2001 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
ba8b04c1 | 2002 | struct extent_state **cached_state, int dedupe) |
ea8c2819 | 2003 | { |
09cbfeaf | 2004 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 2005 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 2006 | cached_state); |
ea8c2819 CM |
2007 | } |
2008 | ||
d352ac68 | 2009 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
2010 | struct btrfs_writepage_fixup { |
2011 | struct page *page; | |
2012 | struct btrfs_work work; | |
2013 | }; | |
2014 | ||
b2950863 | 2015 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
2016 | { |
2017 | struct btrfs_writepage_fixup *fixup; | |
2018 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2019 | struct extent_state *cached_state = NULL; |
247e743c CM |
2020 | struct page *page; |
2021 | struct inode *inode; | |
2022 | u64 page_start; | |
2023 | u64 page_end; | |
87826df0 | 2024 | int ret; |
247e743c CM |
2025 | |
2026 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2027 | page = fixup->page; | |
4a096752 | 2028 | again: |
247e743c CM |
2029 | lock_page(page); |
2030 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2031 | ClearPageChecked(page); | |
2032 | goto out_page; | |
2033 | } | |
2034 | ||
2035 | inode = page->mapping->host; | |
2036 | page_start = page_offset(page); | |
09cbfeaf | 2037 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2038 | |
ff13db41 | 2039 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2040 | &cached_state); |
4a096752 CM |
2041 | |
2042 | /* already ordered? We're done */ | |
8b62b72b | 2043 | if (PagePrivate2(page)) |
247e743c | 2044 | goto out; |
4a096752 | 2045 | |
dbfdb6d1 | 2046 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
09cbfeaf | 2047 | PAGE_SIZE); |
4a096752 | 2048 | if (ordered) { |
2ac55d41 JB |
2049 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2050 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2051 | unlock_page(page); |
2052 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2053 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2054 | goto again; |
2055 | } | |
247e743c | 2056 | |
7cf5b976 | 2057 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
09cbfeaf | 2058 | PAGE_SIZE); |
87826df0 JM |
2059 | if (ret) { |
2060 | mapping_set_error(page->mapping, ret); | |
2061 | end_extent_writepage(page, ret, page_start, page_end); | |
2062 | ClearPageChecked(page); | |
2063 | goto out; | |
2064 | } | |
2065 | ||
ba8b04c1 QW |
2066 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
2067 | 0); | |
247e743c | 2068 | ClearPageChecked(page); |
87826df0 | 2069 | set_page_dirty(page); |
247e743c | 2070 | out: |
2ac55d41 JB |
2071 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2072 | &cached_state, GFP_NOFS); | |
247e743c CM |
2073 | out_page: |
2074 | unlock_page(page); | |
09cbfeaf | 2075 | put_page(page); |
b897abec | 2076 | kfree(fixup); |
247e743c CM |
2077 | } |
2078 | ||
2079 | /* | |
2080 | * There are a few paths in the higher layers of the kernel that directly | |
2081 | * set the page dirty bit without asking the filesystem if it is a | |
2082 | * good idea. This causes problems because we want to make sure COW | |
2083 | * properly happens and the data=ordered rules are followed. | |
2084 | * | |
c8b97818 | 2085 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2086 | * hasn't been properly setup for IO. We kick off an async process |
2087 | * to fix it up. The async helper will wait for ordered extents, set | |
2088 | * the delalloc bit and make it safe to write the page. | |
2089 | */ | |
b2950863 | 2090 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2091 | { |
2092 | struct inode *inode = page->mapping->host; | |
2093 | struct btrfs_writepage_fixup *fixup; | |
2094 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2095 | |
8b62b72b CM |
2096 | /* this page is properly in the ordered list */ |
2097 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2098 | return 0; |
2099 | ||
2100 | if (PageChecked(page)) | |
2101 | return -EAGAIN; | |
2102 | ||
2103 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2104 | if (!fixup) | |
2105 | return -EAGAIN; | |
f421950f | 2106 | |
247e743c | 2107 | SetPageChecked(page); |
09cbfeaf | 2108 | get_page(page); |
9e0af237 LB |
2109 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2110 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2111 | fixup->page = page; |
dc6e3209 | 2112 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2113 | return -EBUSY; |
247e743c CM |
2114 | } |
2115 | ||
d899e052 YZ |
2116 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2117 | struct inode *inode, u64 file_pos, | |
2118 | u64 disk_bytenr, u64 disk_num_bytes, | |
2119 | u64 num_bytes, u64 ram_bytes, | |
2120 | u8 compression, u8 encryption, | |
2121 | u16 other_encoding, int extent_type) | |
2122 | { | |
2123 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2124 | struct btrfs_file_extent_item *fi; | |
2125 | struct btrfs_path *path; | |
2126 | struct extent_buffer *leaf; | |
2127 | struct btrfs_key ins; | |
1acae57b | 2128 | int extent_inserted = 0; |
d899e052 YZ |
2129 | int ret; |
2130 | ||
2131 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2132 | if (!path) |
2133 | return -ENOMEM; | |
d899e052 | 2134 | |
a1ed835e CM |
2135 | /* |
2136 | * we may be replacing one extent in the tree with another. | |
2137 | * The new extent is pinned in the extent map, and we don't want | |
2138 | * to drop it from the cache until it is completely in the btree. | |
2139 | * | |
2140 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2141 | * the caller is expected to unpin it and allow it to be merged | |
2142 | * with the others. | |
2143 | */ | |
1acae57b FDBM |
2144 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2145 | file_pos + num_bytes, NULL, 0, | |
2146 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2147 | if (ret) |
2148 | goto out; | |
d899e052 | 2149 | |
1acae57b FDBM |
2150 | if (!extent_inserted) { |
2151 | ins.objectid = btrfs_ino(inode); | |
2152 | ins.offset = file_pos; | |
2153 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2154 | ||
2155 | path->leave_spinning = 1; | |
2156 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2157 | sizeof(*fi)); | |
2158 | if (ret) | |
2159 | goto out; | |
2160 | } | |
d899e052 YZ |
2161 | leaf = path->nodes[0]; |
2162 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2163 | struct btrfs_file_extent_item); | |
2164 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2165 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2166 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2167 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2168 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2169 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2170 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2171 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2172 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2173 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2174 | |
d899e052 | 2175 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2176 | btrfs_release_path(path); |
d899e052 YZ |
2177 | |
2178 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2179 | |
2180 | ins.objectid = disk_bytenr; | |
2181 | ins.offset = disk_num_bytes; | |
2182 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2183 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2184 | root->root_key.objectid, | |
5846a3c2 QW |
2185 | btrfs_ino(inode), file_pos, |
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; | |
2296 | struct btrfs_fs_info *fs_info; | |
2297 | struct old_sa_defrag_extent *old = ctx; | |
2298 | struct new_sa_defrag_extent *new = old->new; | |
2299 | struct btrfs_path *path = new->path; | |
2300 | struct btrfs_key key; | |
2301 | struct btrfs_root *root; | |
2302 | struct sa_defrag_extent_backref *backref; | |
2303 | struct extent_buffer *leaf; | |
2304 | struct inode *inode = new->inode; | |
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 && | |
2311 | inum == btrfs_ino(inode)) | |
2312 | return 0; | |
2313 | ||
2314 | key.objectid = root_id; | |
2315 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2316 | key.offset = (u64)-1; | |
2317 | ||
2318 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2319 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2320 | if (IS_ERR(root)) { | |
2321 | if (PTR_ERR(root) == -ENOENT) | |
2322 | return 0; | |
2323 | WARN_ON(1); | |
2324 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2325 | inum, offset, root_id); | |
2326 | return PTR_ERR(root); | |
2327 | } | |
2328 | ||
2329 | key.objectid = inum; | |
2330 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2331 | if (offset > (u64)-1 << 32) | |
2332 | key.offset = 0; | |
2333 | else | |
2334 | key.offset = offset; | |
2335 | ||
2336 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2337 | if (WARN_ON(ret < 0)) |
38c227d8 | 2338 | return ret; |
50f1319c | 2339 | ret = 0; |
38c227d8 LB |
2340 | |
2341 | while (1) { | |
2342 | cond_resched(); | |
2343 | ||
2344 | leaf = path->nodes[0]; | |
2345 | slot = path->slots[0]; | |
2346 | ||
2347 | if (slot >= btrfs_header_nritems(leaf)) { | |
2348 | ret = btrfs_next_leaf(root, path); | |
2349 | if (ret < 0) { | |
2350 | goto out; | |
2351 | } else if (ret > 0) { | |
2352 | ret = 0; | |
2353 | goto out; | |
2354 | } | |
2355 | continue; | |
2356 | } | |
2357 | ||
2358 | path->slots[0]++; | |
2359 | ||
2360 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2361 | ||
2362 | if (key.objectid > inum) | |
2363 | goto out; | |
2364 | ||
2365 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2366 | continue; | |
2367 | ||
2368 | extent = btrfs_item_ptr(leaf, slot, | |
2369 | struct btrfs_file_extent_item); | |
2370 | ||
2371 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2372 | continue; | |
2373 | ||
e68afa49 LB |
2374 | /* |
2375 | * 'offset' refers to the exact key.offset, | |
2376 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2377 | * (key.offset - extent_offset). | |
2378 | */ | |
2379 | if (key.offset != offset) | |
38c227d8 LB |
2380 | continue; |
2381 | ||
e68afa49 | 2382 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2383 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2384 | |
38c227d8 LB |
2385 | if (extent_offset >= old->extent_offset + old->offset + |
2386 | old->len || extent_offset + num_bytes <= | |
2387 | old->extent_offset + old->offset) | |
2388 | continue; | |
38c227d8 LB |
2389 | break; |
2390 | } | |
2391 | ||
2392 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2393 | if (!backref) { | |
2394 | ret = -ENOENT; | |
2395 | goto out; | |
2396 | } | |
2397 | ||
2398 | backref->root_id = root_id; | |
2399 | backref->inum = inum; | |
e68afa49 | 2400 | backref->file_pos = offset; |
38c227d8 LB |
2401 | backref->num_bytes = num_bytes; |
2402 | backref->extent_offset = extent_offset; | |
2403 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2404 | backref->old = old; | |
2405 | backref_insert(&new->root, backref); | |
2406 | old->count++; | |
2407 | out: | |
2408 | btrfs_release_path(path); | |
2409 | WARN_ON(ret); | |
2410 | return ret; | |
2411 | } | |
2412 | ||
2413 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2414 | struct new_sa_defrag_extent *new) | |
2415 | { | |
2416 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2417 | struct old_sa_defrag_extent *old, *tmp; | |
2418 | int ret; | |
2419 | ||
2420 | new->path = path; | |
2421 | ||
2422 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2423 | ret = iterate_inodes_from_logical(old->bytenr + |
2424 | old->extent_offset, fs_info, | |
38c227d8 LB |
2425 | path, record_one_backref, |
2426 | old); | |
4724b106 JB |
2427 | if (ret < 0 && ret != -ENOENT) |
2428 | return false; | |
38c227d8 LB |
2429 | |
2430 | /* no backref to be processed for this extent */ | |
2431 | if (!old->count) { | |
2432 | list_del(&old->list); | |
2433 | kfree(old); | |
2434 | } | |
2435 | } | |
2436 | ||
2437 | if (list_empty(&new->head)) | |
2438 | return false; | |
2439 | ||
2440 | return true; | |
2441 | } | |
2442 | ||
2443 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2444 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2445 | struct new_sa_defrag_extent *new) |
38c227d8 | 2446 | { |
116e0024 | 2447 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2448 | return 0; |
2449 | ||
2450 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2451 | return 0; | |
2452 | ||
116e0024 LB |
2453 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2454 | return 0; | |
2455 | ||
2456 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2457 | btrfs_file_extent_other_encoding(leaf, fi)) |
2458 | return 0; | |
2459 | ||
2460 | return 1; | |
2461 | } | |
2462 | ||
2463 | /* | |
2464 | * Note the backref might has changed, and in this case we just return 0. | |
2465 | */ | |
2466 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2467 | struct sa_defrag_extent_backref *prev, | |
2468 | struct sa_defrag_extent_backref *backref) | |
2469 | { | |
2470 | struct btrfs_file_extent_item *extent; | |
2471 | struct btrfs_file_extent_item *item; | |
2472 | struct btrfs_ordered_extent *ordered; | |
2473 | struct btrfs_trans_handle *trans; | |
2474 | struct btrfs_fs_info *fs_info; | |
2475 | struct btrfs_root *root; | |
2476 | struct btrfs_key key; | |
2477 | struct extent_buffer *leaf; | |
2478 | struct old_sa_defrag_extent *old = backref->old; | |
2479 | struct new_sa_defrag_extent *new = old->new; | |
2480 | struct inode *src_inode = new->inode; | |
2481 | struct inode *inode; | |
2482 | struct extent_state *cached = NULL; | |
2483 | int ret = 0; | |
2484 | u64 start; | |
2485 | u64 len; | |
2486 | u64 lock_start; | |
2487 | u64 lock_end; | |
2488 | bool merge = false; | |
2489 | int index; | |
2490 | ||
2491 | if (prev && prev->root_id == backref->root_id && | |
2492 | prev->inum == backref->inum && | |
2493 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2494 | merge = true; | |
2495 | ||
2496 | /* step 1: get root */ | |
2497 | key.objectid = backref->root_id; | |
2498 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2499 | key.offset = (u64)-1; | |
2500 | ||
2501 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2502 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2503 | ||
2504 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2505 | if (IS_ERR(root)) { | |
2506 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2507 | if (PTR_ERR(root) == -ENOENT) | |
2508 | return 0; | |
2509 | return PTR_ERR(root); | |
2510 | } | |
38c227d8 | 2511 | |
bcbba5e6 WS |
2512 | if (btrfs_root_readonly(root)) { |
2513 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2514 | return 0; | |
2515 | } | |
2516 | ||
38c227d8 LB |
2517 | /* step 2: get inode */ |
2518 | key.objectid = backref->inum; | |
2519 | key.type = BTRFS_INODE_ITEM_KEY; | |
2520 | key.offset = 0; | |
2521 | ||
2522 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2523 | if (IS_ERR(inode)) { | |
2524 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2525 | return 0; | |
2526 | } | |
2527 | ||
2528 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2529 | ||
2530 | /* step 3: relink backref */ | |
2531 | lock_start = backref->file_pos; | |
2532 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2533 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2534 | &cached); |
38c227d8 LB |
2535 | |
2536 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2537 | if (ordered) { | |
2538 | btrfs_put_ordered_extent(ordered); | |
2539 | goto out_unlock; | |
2540 | } | |
2541 | ||
2542 | trans = btrfs_join_transaction(root); | |
2543 | if (IS_ERR(trans)) { | |
2544 | ret = PTR_ERR(trans); | |
2545 | goto out_unlock; | |
2546 | } | |
2547 | ||
2548 | key.objectid = backref->inum; | |
2549 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2550 | key.offset = backref->file_pos; | |
2551 | ||
2552 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2553 | if (ret < 0) { | |
2554 | goto out_free_path; | |
2555 | } else if (ret > 0) { | |
2556 | ret = 0; | |
2557 | goto out_free_path; | |
2558 | } | |
2559 | ||
2560 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2561 | struct btrfs_file_extent_item); | |
2562 | ||
2563 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2564 | backref->generation) | |
2565 | goto out_free_path; | |
2566 | ||
2567 | btrfs_release_path(path); | |
2568 | ||
2569 | start = backref->file_pos; | |
2570 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2571 | start += old->extent_offset + old->offset - | |
2572 | backref->extent_offset; | |
2573 | ||
2574 | len = min(backref->extent_offset + backref->num_bytes, | |
2575 | old->extent_offset + old->offset + old->len); | |
2576 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2577 | ||
2578 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2579 | start + len, 1); | |
2580 | if (ret) | |
2581 | goto out_free_path; | |
2582 | again: | |
2583 | key.objectid = btrfs_ino(inode); | |
2584 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2585 | key.offset = start; | |
2586 | ||
a09a0a70 | 2587 | path->leave_spinning = 1; |
38c227d8 LB |
2588 | if (merge) { |
2589 | struct btrfs_file_extent_item *fi; | |
2590 | u64 extent_len; | |
2591 | struct btrfs_key found_key; | |
2592 | ||
3c9665df | 2593 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2594 | if (ret < 0) |
2595 | goto out_free_path; | |
2596 | ||
2597 | path->slots[0]--; | |
2598 | leaf = path->nodes[0]; | |
2599 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2600 | ||
2601 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2602 | struct btrfs_file_extent_item); | |
2603 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2604 | ||
116e0024 LB |
2605 | if (extent_len + found_key.offset == start && |
2606 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2607 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2608 | extent_len + len); | |
2609 | btrfs_mark_buffer_dirty(leaf); | |
2610 | inode_add_bytes(inode, len); | |
2611 | ||
2612 | ret = 1; | |
2613 | goto out_free_path; | |
2614 | } else { | |
2615 | merge = false; | |
2616 | btrfs_release_path(path); | |
2617 | goto again; | |
2618 | } | |
2619 | } | |
2620 | ||
2621 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2622 | sizeof(*extent)); | |
2623 | if (ret) { | |
66642832 | 2624 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2625 | goto out_free_path; |
2626 | } | |
2627 | ||
2628 | leaf = path->nodes[0]; | |
2629 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2630 | struct btrfs_file_extent_item); | |
2631 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2632 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2633 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2634 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2635 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2636 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2637 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2638 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2639 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2640 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2641 | ||
2642 | btrfs_mark_buffer_dirty(leaf); | |
2643 | inode_add_bytes(inode, len); | |
a09a0a70 | 2644 | btrfs_release_path(path); |
38c227d8 LB |
2645 | |
2646 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2647 | new->disk_len, 0, | |
2648 | backref->root_id, backref->inum, | |
b06c4bf5 | 2649 | new->file_pos); /* start - extent_offset */ |
38c227d8 | 2650 | if (ret) { |
66642832 | 2651 | btrfs_abort_transaction(trans, ret); |
38c227d8 LB |
2652 | goto out_free_path; |
2653 | } | |
2654 | ||
2655 | ret = 1; | |
2656 | out_free_path: | |
2657 | btrfs_release_path(path); | |
a09a0a70 | 2658 | path->leave_spinning = 0; |
38c227d8 LB |
2659 | btrfs_end_transaction(trans, root); |
2660 | out_unlock: | |
2661 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2662 | &cached, GFP_NOFS); | |
2663 | iput(inode); | |
2664 | return ret; | |
2665 | } | |
2666 | ||
6f519564 LB |
2667 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2668 | { | |
2669 | struct old_sa_defrag_extent *old, *tmp; | |
2670 | ||
2671 | if (!new) | |
2672 | return; | |
2673 | ||
2674 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2675 | kfree(old); |
2676 | } | |
2677 | kfree(new); | |
2678 | } | |
2679 | ||
38c227d8 LB |
2680 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2681 | { | |
2682 | struct btrfs_path *path; | |
38c227d8 LB |
2683 | struct sa_defrag_extent_backref *backref; |
2684 | struct sa_defrag_extent_backref *prev = NULL; | |
2685 | struct inode *inode; | |
2686 | struct btrfs_root *root; | |
2687 | struct rb_node *node; | |
2688 | int ret; | |
2689 | ||
2690 | inode = new->inode; | |
2691 | root = BTRFS_I(inode)->root; | |
2692 | ||
2693 | path = btrfs_alloc_path(); | |
2694 | if (!path) | |
2695 | return; | |
2696 | ||
2697 | if (!record_extent_backrefs(path, new)) { | |
2698 | btrfs_free_path(path); | |
2699 | goto out; | |
2700 | } | |
2701 | btrfs_release_path(path); | |
2702 | ||
2703 | while (1) { | |
2704 | node = rb_first(&new->root); | |
2705 | if (!node) | |
2706 | break; | |
2707 | rb_erase(node, &new->root); | |
2708 | ||
2709 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2710 | ||
2711 | ret = relink_extent_backref(path, prev, backref); | |
2712 | WARN_ON(ret < 0); | |
2713 | ||
2714 | kfree(prev); | |
2715 | ||
2716 | if (ret == 1) | |
2717 | prev = backref; | |
2718 | else | |
2719 | prev = NULL; | |
2720 | cond_resched(); | |
2721 | } | |
2722 | kfree(prev); | |
2723 | ||
2724 | btrfs_free_path(path); | |
38c227d8 | 2725 | out: |
6f519564 LB |
2726 | free_sa_defrag_extent(new); |
2727 | ||
38c227d8 LB |
2728 | atomic_dec(&root->fs_info->defrag_running); |
2729 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2730 | } |
2731 | ||
2732 | static struct new_sa_defrag_extent * | |
2733 | record_old_file_extents(struct inode *inode, | |
2734 | struct btrfs_ordered_extent *ordered) | |
2735 | { | |
2736 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2737 | struct btrfs_path *path; | |
2738 | struct btrfs_key key; | |
6f519564 | 2739 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2740 | struct new_sa_defrag_extent *new; |
2741 | int ret; | |
2742 | ||
2743 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2744 | if (!new) | |
2745 | return NULL; | |
2746 | ||
2747 | new->inode = inode; | |
2748 | new->file_pos = ordered->file_offset; | |
2749 | new->len = ordered->len; | |
2750 | new->bytenr = ordered->start; | |
2751 | new->disk_len = ordered->disk_len; | |
2752 | new->compress_type = ordered->compress_type; | |
2753 | new->root = RB_ROOT; | |
2754 | INIT_LIST_HEAD(&new->head); | |
2755 | ||
2756 | path = btrfs_alloc_path(); | |
2757 | if (!path) | |
2758 | goto out_kfree; | |
2759 | ||
2760 | key.objectid = btrfs_ino(inode); | |
2761 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2762 | key.offset = new->file_pos; | |
2763 | ||
2764 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2765 | if (ret < 0) | |
2766 | goto out_free_path; | |
2767 | if (ret > 0 && path->slots[0] > 0) | |
2768 | path->slots[0]--; | |
2769 | ||
2770 | /* find out all the old extents for the file range */ | |
2771 | while (1) { | |
2772 | struct btrfs_file_extent_item *extent; | |
2773 | struct extent_buffer *l; | |
2774 | int slot; | |
2775 | u64 num_bytes; | |
2776 | u64 offset; | |
2777 | u64 end; | |
2778 | u64 disk_bytenr; | |
2779 | u64 extent_offset; | |
2780 | ||
2781 | l = path->nodes[0]; | |
2782 | slot = path->slots[0]; | |
2783 | ||
2784 | if (slot >= btrfs_header_nritems(l)) { | |
2785 | ret = btrfs_next_leaf(root, path); | |
2786 | if (ret < 0) | |
6f519564 | 2787 | goto out_free_path; |
38c227d8 LB |
2788 | else if (ret > 0) |
2789 | break; | |
2790 | continue; | |
2791 | } | |
2792 | ||
2793 | btrfs_item_key_to_cpu(l, &key, slot); | |
2794 | ||
2795 | if (key.objectid != btrfs_ino(inode)) | |
2796 | break; | |
2797 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2798 | break; | |
2799 | if (key.offset >= new->file_pos + new->len) | |
2800 | break; | |
2801 | ||
2802 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2803 | ||
2804 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2805 | if (key.offset + num_bytes < new->file_pos) | |
2806 | goto next; | |
2807 | ||
2808 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2809 | if (!disk_bytenr) | |
2810 | goto next; | |
2811 | ||
2812 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2813 | ||
2814 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2815 | if (!old) | |
6f519564 | 2816 | goto out_free_path; |
38c227d8 LB |
2817 | |
2818 | offset = max(new->file_pos, key.offset); | |
2819 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2820 | ||
2821 | old->bytenr = disk_bytenr; | |
2822 | old->extent_offset = extent_offset; | |
2823 | old->offset = offset - key.offset; | |
2824 | old->len = end - offset; | |
2825 | old->new = new; | |
2826 | old->count = 0; | |
2827 | list_add_tail(&old->list, &new->head); | |
2828 | next: | |
2829 | path->slots[0]++; | |
2830 | cond_resched(); | |
2831 | } | |
2832 | ||
2833 | btrfs_free_path(path); | |
2834 | atomic_inc(&root->fs_info->defrag_running); | |
2835 | ||
2836 | return new; | |
2837 | ||
38c227d8 LB |
2838 | out_free_path: |
2839 | btrfs_free_path(path); | |
2840 | out_kfree: | |
6f519564 | 2841 | free_sa_defrag_extent(new); |
38c227d8 LB |
2842 | return NULL; |
2843 | } | |
2844 | ||
e570fd27 MX |
2845 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2846 | u64 start, u64 len) | |
2847 | { | |
2848 | struct btrfs_block_group_cache *cache; | |
2849 | ||
2850 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2851 | ASSERT(cache); | |
2852 | ||
2853 | spin_lock(&cache->lock); | |
2854 | cache->delalloc_bytes -= len; | |
2855 | spin_unlock(&cache->lock); | |
2856 | ||
2857 | btrfs_put_block_group(cache); | |
2858 | } | |
2859 | ||
d352ac68 CM |
2860 | /* as ordered data IO finishes, this gets called so we can finish |
2861 | * an ordered extent if the range of bytes in the file it covers are | |
2862 | * fully written. | |
2863 | */ | |
5fd02043 | 2864 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2865 | { |
5fd02043 | 2866 | struct inode *inode = ordered_extent->inode; |
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 | } |
6c760c07 JB |
2917 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
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 | |
0ca1f7ce | 2952 | trans->block_rsv = &root->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 { |
0af3d00b | 2963 | BUG_ON(root == 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 MX |
2971 | if (!ret) |
2972 | btrfs_release_delalloc_bytes(root, | |
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: |
5b0e95bf | 2999 | if (root != root->fs_info->tree_root) |
0cb59c99 | 3000 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
3001 | if (trans) |
3002 | btrfs_end_transaction(trans, root); | |
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 JB |
3025 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
3026 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 3027 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3028 | } |
3029 | ||
3030 | ||
5fd02043 | 3031 | /* |
8bad3c02 LB |
3032 | * This needs to be done to make sure anybody waiting knows we are done |
3033 | * updating everything for this ordered extent. | |
5fd02043 JB |
3034 | */ |
3035 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3036 | ||
38c227d8 | 3037 | /* for snapshot-aware defrag */ |
6f519564 LB |
3038 | if (new) { |
3039 | if (ret) { | |
3040 | free_sa_defrag_extent(new); | |
3041 | atomic_dec(&root->fs_info->defrag_running); | |
3042 | } else { | |
3043 | relink_file_extents(new); | |
3044 | } | |
3045 | } | |
38c227d8 | 3046 | |
e6dcd2dc CM |
3047 | /* once for us */ |
3048 | btrfs_put_ordered_extent(ordered_extent); | |
3049 | /* once for the tree */ | |
3050 | btrfs_put_ordered_extent(ordered_extent); | |
3051 | ||
5fd02043 JB |
3052 | return ret; |
3053 | } | |
3054 | ||
3055 | static void finish_ordered_fn(struct btrfs_work *work) | |
3056 | { | |
3057 | struct btrfs_ordered_extent *ordered_extent; | |
3058 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3059 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3060 | } |
3061 | ||
b2950863 | 3062 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3063 | struct extent_state *state, int uptodate) |
3064 | { | |
5fd02043 JB |
3065 | struct inode *inode = page->mapping->host; |
3066 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3067 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
3068 | struct btrfs_workqueue *wq; |
3069 | btrfs_work_func_t func; | |
5fd02043 | 3070 | |
1abe9b8a | 3071 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3072 | ||
8b62b72b | 3073 | ClearPagePrivate2(page); |
5fd02043 JB |
3074 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3075 | end - start + 1, uptodate)) | |
3076 | return 0; | |
3077 | ||
9e0af237 LB |
3078 | if (btrfs_is_free_space_inode(inode)) { |
3079 | wq = root->fs_info->endio_freespace_worker; | |
3080 | func = btrfs_freespace_write_helper; | |
3081 | } else { | |
3082 | wq = root->fs_info->endio_write_workers; | |
3083 | func = btrfs_endio_write_helper; | |
3084 | } | |
5fd02043 | 3085 | |
9e0af237 LB |
3086 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3087 | NULL); | |
3088 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3089 | |
3090 | return 0; | |
211f90e6 CM |
3091 | } |
3092 | ||
dc380aea MX |
3093 | static int __readpage_endio_check(struct inode *inode, |
3094 | struct btrfs_io_bio *io_bio, | |
3095 | int icsum, struct page *page, | |
3096 | int pgoff, u64 start, size_t len) | |
3097 | { | |
3098 | char *kaddr; | |
3099 | u32 csum_expected; | |
3100 | u32 csum = ~(u32)0; | |
dc380aea MX |
3101 | |
3102 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3103 | ||
3104 | kaddr = kmap_atomic(page); | |
3105 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3106 | btrfs_csum_final(csum, (char *)&csum); | |
3107 | if (csum != csum_expected) | |
3108 | goto zeroit; | |
3109 | ||
3110 | kunmap_atomic(kaddr); | |
3111 | return 0; | |
3112 | zeroit: | |
94647322 DS |
3113 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
3114 | "csum failed ino %llu off %llu csum %u expected csum %u", | |
dc380aea MX |
3115 | btrfs_ino(inode), start, csum, csum_expected); |
3116 | memset(kaddr + pgoff, 1, len); | |
3117 | flush_dcache_page(page); | |
3118 | kunmap_atomic(kaddr); | |
3119 | if (csum_expected == 0) | |
3120 | return 0; | |
3121 | return -EIO; | |
3122 | } | |
3123 | ||
d352ac68 CM |
3124 | /* |
3125 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3126 | * if there's a match, we allow the bio to finish. If not, the code in |
3127 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3128 | */ |
facc8a22 MX |
3129 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3130 | u64 phy_offset, struct page *page, | |
3131 | u64 start, u64 end, int mirror) | |
07157aac | 3132 | { |
4eee4fa4 | 3133 | size_t offset = start - page_offset(page); |
07157aac | 3134 | struct inode *inode = page->mapping->host; |
d1310b2e | 3135 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3136 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3137 | |
d20f7043 CM |
3138 | if (PageChecked(page)) { |
3139 | ClearPageChecked(page); | |
dc380aea | 3140 | return 0; |
d20f7043 | 3141 | } |
6cbff00f CH |
3142 | |
3143 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3144 | return 0; |
17d217fe YZ |
3145 | |
3146 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3147 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3148 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3149 | return 0; |
17d217fe | 3150 | } |
d20f7043 | 3151 | |
facc8a22 | 3152 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3153 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3154 | start, (size_t)(end - start + 1)); | |
07157aac | 3155 | } |
b888db2b | 3156 | |
24bbcf04 YZ |
3157 | void btrfs_add_delayed_iput(struct inode *inode) |
3158 | { | |
3159 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
8089fe62 | 3160 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3161 | |
3162 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3163 | return; | |
3164 | ||
24bbcf04 | 3165 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3166 | if (binode->delayed_iput_count == 0) { |
3167 | ASSERT(list_empty(&binode->delayed_iput)); | |
3168 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3169 | } else { | |
3170 | binode->delayed_iput_count++; | |
3171 | } | |
24bbcf04 YZ |
3172 | spin_unlock(&fs_info->delayed_iput_lock); |
3173 | } | |
3174 | ||
3175 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3176 | { | |
24bbcf04 | 3177 | struct btrfs_fs_info *fs_info = root->fs_info; |
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 | { | |
90290e19 | 3207 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3208 | int ret; |
3209 | ||
8a35d95f | 3210 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3211 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3212 | return; | |
3213 | ||
90290e19 | 3214 | spin_lock(&root->orphan_lock); |
8a35d95f | 3215 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3216 | spin_unlock(&root->orphan_lock); |
3217 | return; | |
3218 | } | |
3219 | ||
3220 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3221 | spin_unlock(&root->orphan_lock); | |
3222 | return; | |
3223 | } | |
3224 | ||
3225 | block_rsv = root->orphan_block_rsv; | |
3226 | root->orphan_block_rsv = NULL; | |
3227 | spin_unlock(&root->orphan_lock); | |
3228 | ||
27cdeb70 | 3229 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3230 | btrfs_root_refs(&root->root_item) > 0) { |
3231 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3232 | root->root_key.objectid); | |
4ef31a45 | 3233 | if (ret) |
66642832 | 3234 | btrfs_abort_transaction(trans, ret); |
4ef31a45 | 3235 | else |
27cdeb70 MX |
3236 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3237 | &root->state); | |
d68fc57b YZ |
3238 | } |
3239 | ||
90290e19 JB |
3240 | if (block_rsv) { |
3241 | WARN_ON(block_rsv->size > 0); | |
3242 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3243 | } |
3244 | } | |
3245 | ||
7b128766 JB |
3246 | /* |
3247 | * This creates an orphan entry for the given inode in case something goes | |
3248 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3249 | * |
3250 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3251 | * this function. | |
7b128766 JB |
3252 | */ |
3253 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3254 | { | |
3255 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3256 | struct btrfs_block_rsv *block_rsv = NULL; |
3257 | int reserve = 0; | |
3258 | int insert = 0; | |
3259 | int ret; | |
7b128766 | 3260 | |
d68fc57b | 3261 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3262 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3263 | if (!block_rsv) |
3264 | return -ENOMEM; | |
d68fc57b | 3265 | } |
7b128766 | 3266 | |
d68fc57b YZ |
3267 | spin_lock(&root->orphan_lock); |
3268 | if (!root->orphan_block_rsv) { | |
3269 | root->orphan_block_rsv = block_rsv; | |
3270 | } else if (block_rsv) { | |
3271 | btrfs_free_block_rsv(root, block_rsv); | |
3272 | block_rsv = NULL; | |
7b128766 | 3273 | } |
7b128766 | 3274 | |
8a35d95f JB |
3275 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3276 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3277 | #if 0 |
3278 | /* | |
3279 | * For proper ENOSPC handling, we should do orphan | |
3280 | * cleanup when mounting. But this introduces backward | |
3281 | * compatibility issue. | |
3282 | */ | |
3283 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3284 | insert = 2; | |
3285 | else | |
3286 | insert = 1; | |
3287 | #endif | |
3288 | insert = 1; | |
321f0e70 | 3289 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3290 | } |
3291 | ||
72ac3c0d JB |
3292 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3293 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3294 | reserve = 1; |
d68fc57b | 3295 | spin_unlock(&root->orphan_lock); |
7b128766 | 3296 | |
d68fc57b YZ |
3297 | /* grab metadata reservation from transaction handle */ |
3298 | if (reserve) { | |
3299 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3300 | ASSERT(!ret); |
3301 | if (ret) { | |
3302 | atomic_dec(&root->orphan_inodes); | |
3303 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3304 | &BTRFS_I(inode)->runtime_flags); | |
3305 | if (insert) | |
3306 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3307 | &BTRFS_I(inode)->runtime_flags); | |
3308 | return ret; | |
3309 | } | |
d68fc57b | 3310 | } |
7b128766 | 3311 | |
d68fc57b YZ |
3312 | /* insert an orphan item to track this unlinked/truncated file */ |
3313 | if (insert >= 1) { | |
33345d01 | 3314 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3315 | if (ret) { |
703c88e0 | 3316 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3317 | if (reserve) { |
3318 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3319 | &BTRFS_I(inode)->runtime_flags); | |
3320 | btrfs_orphan_release_metadata(inode); | |
3321 | } | |
3322 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3323 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3324 | &BTRFS_I(inode)->runtime_flags); | |
66642832 | 3325 | btrfs_abort_transaction(trans, ret); |
4ef31a45 JB |
3326 | return ret; |
3327 | } | |
79787eaa JM |
3328 | } |
3329 | ret = 0; | |
d68fc57b YZ |
3330 | } |
3331 | ||
3332 | /* insert an orphan item to track subvolume contains orphan files */ | |
3333 | if (insert >= 2) { | |
3334 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3335 | root->root_key.objectid); | |
79787eaa | 3336 | if (ret && ret != -EEXIST) { |
66642832 | 3337 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
3338 | return ret; |
3339 | } | |
d68fc57b YZ |
3340 | } |
3341 | return 0; | |
7b128766 JB |
3342 | } |
3343 | ||
3344 | /* | |
3345 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3346 | * item for this particular inode. | |
3347 | */ | |
48a3b636 ES |
3348 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3349 | struct inode *inode) | |
7b128766 JB |
3350 | { |
3351 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3352 | int delete_item = 0; |
3353 | int release_rsv = 0; | |
7b128766 JB |
3354 | int ret = 0; |
3355 | ||
d68fc57b | 3356 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3357 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3358 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3359 | delete_item = 1; |
7b128766 | 3360 | |
72ac3c0d JB |
3361 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3362 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3363 | release_rsv = 1; |
d68fc57b | 3364 | spin_unlock(&root->orphan_lock); |
7b128766 | 3365 | |
703c88e0 | 3366 | if (delete_item) { |
8a35d95f | 3367 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3368 | if (trans) |
3369 | ret = btrfs_del_orphan_item(trans, root, | |
3370 | btrfs_ino(inode)); | |
8a35d95f | 3371 | } |
7b128766 | 3372 | |
703c88e0 FDBM |
3373 | if (release_rsv) |
3374 | btrfs_orphan_release_metadata(inode); | |
3375 | ||
4ef31a45 | 3376 | return ret; |
7b128766 JB |
3377 | } |
3378 | ||
3379 | /* | |
3380 | * this cleans up any orphans that may be left on the list from the last use | |
3381 | * of this root. | |
3382 | */ | |
66b4ffd1 | 3383 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3384 | { |
3385 | struct btrfs_path *path; | |
3386 | struct extent_buffer *leaf; | |
7b128766 JB |
3387 | struct btrfs_key key, found_key; |
3388 | struct btrfs_trans_handle *trans; | |
3389 | struct inode *inode; | |
8f6d7f4f | 3390 | u64 last_objectid = 0; |
7b128766 JB |
3391 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3392 | ||
d68fc57b | 3393 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3394 | return 0; |
c71bf099 YZ |
3395 | |
3396 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3397 | if (!path) { |
3398 | ret = -ENOMEM; | |
3399 | goto out; | |
3400 | } | |
e4058b54 | 3401 | path->reada = READA_BACK; |
7b128766 JB |
3402 | |
3403 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3404 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3405 | key.offset = (u64)-1; |
3406 | ||
7b128766 JB |
3407 | while (1) { |
3408 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3409 | if (ret < 0) |
3410 | goto out; | |
7b128766 JB |
3411 | |
3412 | /* | |
3413 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3414 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3415 | * find the key and see if we have stuff that matches |
3416 | */ | |
3417 | if (ret > 0) { | |
66b4ffd1 | 3418 | ret = 0; |
7b128766 JB |
3419 | if (path->slots[0] == 0) |
3420 | break; | |
3421 | path->slots[0]--; | |
3422 | } | |
3423 | ||
3424 | /* pull out the item */ | |
3425 | leaf = path->nodes[0]; | |
7b128766 JB |
3426 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3427 | ||
3428 | /* make sure the item matches what we want */ | |
3429 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3430 | break; | |
962a298f | 3431 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3432 | break; |
3433 | ||
3434 | /* release the path since we're done with it */ | |
b3b4aa74 | 3435 | btrfs_release_path(path); |
7b128766 JB |
3436 | |
3437 | /* | |
3438 | * this is where we are basically btrfs_lookup, without the | |
3439 | * crossing root thing. we store the inode number in the | |
3440 | * offset of the orphan item. | |
3441 | */ | |
8f6d7f4f JB |
3442 | |
3443 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3444 | btrfs_err(root->fs_info, |
3445 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3446 | ret = -EINVAL; |
3447 | goto out; | |
3448 | } | |
3449 | ||
3450 | last_objectid = found_key.offset; | |
3451 | ||
5d4f98a2 YZ |
3452 | found_key.objectid = found_key.offset; |
3453 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3454 | found_key.offset = 0; | |
73f73415 | 3455 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3456 | ret = PTR_ERR_OR_ZERO(inode); |
67710892 | 3457 | if (ret && ret != -ENOENT) |
66b4ffd1 | 3458 | goto out; |
7b128766 | 3459 | |
67710892 | 3460 | if (ret == -ENOENT && root == root->fs_info->tree_root) { |
f8e9e0b0 AJ |
3461 | struct btrfs_root *dead_root; |
3462 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3463 | int is_dead_root = 0; | |
3464 | ||
3465 | /* | |
3466 | * this is an orphan in the tree root. Currently these | |
3467 | * could come from 2 sources: | |
3468 | * a) a snapshot deletion in progress | |
3469 | * b) a free space cache inode | |
3470 | * We need to distinguish those two, as the snapshot | |
3471 | * orphan must not get deleted. | |
3472 | * find_dead_roots already ran before us, so if this | |
3473 | * is a snapshot deletion, we should find the root | |
3474 | * in the dead_roots list | |
3475 | */ | |
3476 | spin_lock(&fs_info->trans_lock); | |
3477 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3478 | root_list) { | |
3479 | if (dead_root->root_key.objectid == | |
3480 | found_key.objectid) { | |
3481 | is_dead_root = 1; | |
3482 | break; | |
3483 | } | |
3484 | } | |
3485 | spin_unlock(&fs_info->trans_lock); | |
3486 | if (is_dead_root) { | |
3487 | /* prevent this orphan from being found again */ | |
3488 | key.offset = found_key.objectid - 1; | |
3489 | continue; | |
3490 | } | |
3491 | } | |
7b128766 | 3492 | /* |
a8c9e576 JB |
3493 | * Inode is already gone but the orphan item is still there, |
3494 | * kill the orphan item. | |
7b128766 | 3495 | */ |
67710892 | 3496 | if (ret == -ENOENT) { |
a8c9e576 | 3497 | trans = btrfs_start_transaction(root, 1); |
66b4ffd1 JB |
3498 | if (IS_ERR(trans)) { |
3499 | ret = PTR_ERR(trans); | |
3500 | goto out; | |
3501 | } | |
c2cf52eb SK |
3502 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3503 | found_key.objectid); | |
a8c9e576 JB |
3504 | ret = btrfs_del_orphan_item(trans, root, |
3505 | found_key.objectid); | |
5b21f2ed | 3506 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3507 | if (ret) |
3508 | goto out; | |
7b128766 JB |
3509 | continue; |
3510 | } | |
3511 | ||
a8c9e576 JB |
3512 | /* |
3513 | * add this inode to the orphan list so btrfs_orphan_del does | |
3514 | * the proper thing when we hit it | |
3515 | */ | |
8a35d95f JB |
3516 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3517 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3518 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3519 | |
7b128766 JB |
3520 | /* if we have links, this was a truncate, lets do that */ |
3521 | if (inode->i_nlink) { | |
fae7f21c | 3522 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3523 | iput(inode); |
3524 | continue; | |
3525 | } | |
7b128766 | 3526 | nr_truncate++; |
f3fe820c JB |
3527 | |
3528 | /* 1 for the orphan item deletion. */ | |
3529 | trans = btrfs_start_transaction(root, 1); | |
3530 | if (IS_ERR(trans)) { | |
c69b26b0 | 3531 | iput(inode); |
f3fe820c JB |
3532 | ret = PTR_ERR(trans); |
3533 | goto out; | |
3534 | } | |
3535 | ret = btrfs_orphan_add(trans, inode); | |
3536 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3537 | if (ret) { |
3538 | iput(inode); | |
f3fe820c | 3539 | goto out; |
c69b26b0 | 3540 | } |
f3fe820c | 3541 | |
66b4ffd1 | 3542 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3543 | if (ret) |
3544 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3545 | } else { |
3546 | nr_unlink++; | |
3547 | } | |
3548 | ||
3549 | /* this will do delete_inode and everything for us */ | |
3550 | iput(inode); | |
66b4ffd1 JB |
3551 | if (ret) |
3552 | goto out; | |
7b128766 | 3553 | } |
3254c876 MX |
3554 | /* release the path since we're done with it */ |
3555 | btrfs_release_path(path); | |
3556 | ||
d68fc57b YZ |
3557 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3558 | ||
3559 | if (root->orphan_block_rsv) | |
3560 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3561 | (u64)-1); | |
3562 | ||
27cdeb70 MX |
3563 | if (root->orphan_block_rsv || |
3564 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3565 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3566 | if (!IS_ERR(trans)) |
3567 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3568 | } |
7b128766 JB |
3569 | |
3570 | if (nr_unlink) | |
4884b476 | 3571 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3572 | if (nr_truncate) |
4884b476 | 3573 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3574 | |
3575 | out: | |
3576 | if (ret) | |
68b663d1 | 3577 | btrfs_err(root->fs_info, |
c2cf52eb | 3578 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3579 | btrfs_free_path(path); |
3580 | return ret; | |
7b128766 JB |
3581 | } |
3582 | ||
46a53cca CM |
3583 | /* |
3584 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3585 | * don't find any xattrs, we know there can't be any acls. | |
3586 | * | |
3587 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3588 | */ | |
3589 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3590 | int slot, u64 objectid, |
3591 | int *first_xattr_slot) | |
46a53cca CM |
3592 | { |
3593 | u32 nritems = btrfs_header_nritems(leaf); | |
3594 | struct btrfs_key found_key; | |
f23b5a59 JB |
3595 | static u64 xattr_access = 0; |
3596 | static u64 xattr_default = 0; | |
46a53cca CM |
3597 | int scanned = 0; |
3598 | ||
f23b5a59 | 3599 | if (!xattr_access) { |
97d79299 AG |
3600 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3601 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3602 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3603 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3604 | } |
3605 | ||
46a53cca | 3606 | slot++; |
63541927 | 3607 | *first_xattr_slot = -1; |
46a53cca CM |
3608 | while (slot < nritems) { |
3609 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3610 | ||
3611 | /* we found a different objectid, there must not be acls */ | |
3612 | if (found_key.objectid != objectid) | |
3613 | return 0; | |
3614 | ||
3615 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3616 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3617 | if (*first_xattr_slot == -1) |
3618 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3619 | if (found_key.offset == xattr_access || |
3620 | found_key.offset == xattr_default) | |
3621 | return 1; | |
3622 | } | |
46a53cca CM |
3623 | |
3624 | /* | |
3625 | * we found a key greater than an xattr key, there can't | |
3626 | * be any acls later on | |
3627 | */ | |
3628 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3629 | return 0; | |
3630 | ||
3631 | slot++; | |
3632 | scanned++; | |
3633 | ||
3634 | /* | |
3635 | * it goes inode, inode backrefs, xattrs, extents, | |
3636 | * so if there are a ton of hard links to an inode there can | |
3637 | * be a lot of backrefs. Don't waste time searching too hard, | |
3638 | * this is just an optimization | |
3639 | */ | |
3640 | if (scanned >= 8) | |
3641 | break; | |
3642 | } | |
3643 | /* we hit the end of the leaf before we found an xattr or | |
3644 | * something larger than an xattr. We have to assume the inode | |
3645 | * has acls | |
3646 | */ | |
63541927 FDBM |
3647 | if (*first_xattr_slot == -1) |
3648 | *first_xattr_slot = slot; | |
46a53cca CM |
3649 | return 1; |
3650 | } | |
3651 | ||
d352ac68 CM |
3652 | /* |
3653 | * read an inode from the btree into the in-memory inode | |
3654 | */ | |
67710892 | 3655 | static int btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3656 | { |
3657 | struct btrfs_path *path; | |
5f39d397 | 3658 | struct extent_buffer *leaf; |
39279cc3 CM |
3659 | struct btrfs_inode_item *inode_item; |
3660 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3661 | struct btrfs_key location; | |
67de1176 | 3662 | unsigned long ptr; |
46a53cca | 3663 | int maybe_acls; |
618e21d5 | 3664 | u32 rdev; |
39279cc3 | 3665 | int ret; |
2f7e33d4 | 3666 | bool filled = false; |
63541927 | 3667 | int first_xattr_slot; |
2f7e33d4 MX |
3668 | |
3669 | ret = btrfs_fill_inode(inode, &rdev); | |
3670 | if (!ret) | |
3671 | filled = true; | |
39279cc3 CM |
3672 | |
3673 | path = btrfs_alloc_path(); | |
67710892 FM |
3674 | if (!path) { |
3675 | ret = -ENOMEM; | |
1748f843 | 3676 | goto make_bad; |
67710892 | 3677 | } |
1748f843 | 3678 | |
39279cc3 | 3679 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3680 | |
39279cc3 | 3681 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
67710892 FM |
3682 | if (ret) { |
3683 | if (ret > 0) | |
3684 | ret = -ENOENT; | |
39279cc3 | 3685 | goto make_bad; |
67710892 | 3686 | } |
39279cc3 | 3687 | |
5f39d397 | 3688 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3689 | |
3690 | if (filled) | |
67de1176 | 3691 | goto cache_index; |
2f7e33d4 | 3692 | |
5f39d397 CM |
3693 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3694 | struct btrfs_inode_item); | |
5f39d397 | 3695 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3696 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3697 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3698 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3699 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3700 | |
a937b979 DS |
3701 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3702 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3703 | |
a937b979 DS |
3704 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3705 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3706 | |
a937b979 DS |
3707 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3708 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3709 | |
9cc97d64 | 3710 | BTRFS_I(inode)->i_otime.tv_sec = |
3711 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3712 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3713 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3714 | |
a76a3cd4 | 3715 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3716 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3717 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3718 | ||
6e17d30b YD |
3719 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3720 | inode->i_generation = BTRFS_I(inode)->generation; | |
3721 | inode->i_rdev = 0; | |
3722 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3723 | ||
3724 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3725 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3726 | ||
3727 | cache_index: | |
5dc562c5 JB |
3728 | /* |
3729 | * If we were modified in the current generation and evicted from memory | |
3730 | * and then re-read we need to do a full sync since we don't have any | |
3731 | * idea about which extents were modified before we were evicted from | |
3732 | * cache. | |
6e17d30b YD |
3733 | * |
3734 | * This is required for both inode re-read from disk and delayed inode | |
3735 | * in delayed_nodes_tree. | |
5dc562c5 JB |
3736 | */ |
3737 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3738 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3739 | &BTRFS_I(inode)->runtime_flags); | |
3740 | ||
bde6c242 FM |
3741 | /* |
3742 | * We don't persist the id of the transaction where an unlink operation | |
3743 | * against the inode was last made. So here we assume the inode might | |
3744 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3745 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3746 | * between the inode and its parent if the inode is fsync'ed and the log | |
3747 | * replayed. For example, in the scenario: | |
3748 | * | |
3749 | * touch mydir/foo | |
3750 | * ln mydir/foo mydir/bar | |
3751 | * sync | |
3752 | * unlink mydir/bar | |
3753 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3754 | * xfs_io -c fsync mydir/foo | |
3755 | * <power failure> | |
3756 | * mount fs, triggers fsync log replay | |
3757 | * | |
3758 | * We must make sure that when we fsync our inode foo we also log its | |
3759 | * parent inode, otherwise after log replay the parent still has the | |
3760 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3761 | * and doesn't have an inode ref with the name "bar" anymore. | |
3762 | * | |
3763 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3764 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3765 | * transaction commits on fsync if our inode is a directory, or if our |
3766 | * inode is not a directory, logging its parent unnecessarily. | |
3767 | */ | |
3768 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3769 | ||
67de1176 MX |
3770 | path->slots[0]++; |
3771 | if (inode->i_nlink != 1 || | |
3772 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3773 | goto cache_acl; | |
3774 | ||
3775 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3776 | if (location.objectid != btrfs_ino(inode)) | |
3777 | goto cache_acl; | |
3778 | ||
3779 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3780 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3781 | struct btrfs_inode_ref *ref; | |
3782 | ||
3783 | ref = (struct btrfs_inode_ref *)ptr; | |
3784 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3785 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3786 | struct btrfs_inode_extref *extref; | |
3787 | ||
3788 | extref = (struct btrfs_inode_extref *)ptr; | |
3789 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3790 | extref); | |
3791 | } | |
2f7e33d4 | 3792 | cache_acl: |
46a53cca CM |
3793 | /* |
3794 | * try to precache a NULL acl entry for files that don't have | |
3795 | * any xattrs or acls | |
3796 | */ | |
33345d01 | 3797 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3798 | btrfs_ino(inode), &first_xattr_slot); |
3799 | if (first_xattr_slot != -1) { | |
3800 | path->slots[0] = first_xattr_slot; | |
3801 | ret = btrfs_load_inode_props(inode, path); | |
3802 | if (ret) | |
3803 | btrfs_err(root->fs_info, | |
351fd353 | 3804 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3805 | btrfs_ino(inode), |
3806 | root->root_key.objectid, ret); | |
3807 | } | |
3808 | btrfs_free_path(path); | |
3809 | ||
72c04902 AV |
3810 | if (!maybe_acls) |
3811 | cache_no_acl(inode); | |
46a53cca | 3812 | |
39279cc3 | 3813 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3814 | case S_IFREG: |
3815 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3816 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3817 | inode->i_fop = &btrfs_file_operations; |
3818 | inode->i_op = &btrfs_file_inode_operations; | |
3819 | break; | |
3820 | case S_IFDIR: | |
3821 | inode->i_fop = &btrfs_dir_file_operations; | |
3822 | if (root == root->fs_info->tree_root) | |
3823 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3824 | else | |
3825 | inode->i_op = &btrfs_dir_inode_operations; | |
3826 | break; | |
3827 | case S_IFLNK: | |
3828 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3829 | inode_nohighmem(inode); |
39279cc3 CM |
3830 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3831 | break; | |
618e21d5 | 3832 | default: |
0279b4cd | 3833 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3834 | init_special_inode(inode, inode->i_mode, rdev); |
3835 | break; | |
39279cc3 | 3836 | } |
6cbff00f CH |
3837 | |
3838 | btrfs_update_iflags(inode); | |
67710892 | 3839 | return 0; |
39279cc3 CM |
3840 | |
3841 | make_bad: | |
39279cc3 | 3842 | btrfs_free_path(path); |
39279cc3 | 3843 | make_bad_inode(inode); |
67710892 | 3844 | return ret; |
39279cc3 CM |
3845 | } |
3846 | ||
d352ac68 CM |
3847 | /* |
3848 | * given a leaf and an inode, copy the inode fields into the leaf | |
3849 | */ | |
e02119d5 CM |
3850 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3851 | struct extent_buffer *leaf, | |
5f39d397 | 3852 | struct btrfs_inode_item *item, |
39279cc3 CM |
3853 | struct inode *inode) |
3854 | { | |
51fab693 LB |
3855 | struct btrfs_map_token token; |
3856 | ||
3857 | btrfs_init_map_token(&token); | |
5f39d397 | 3858 | |
51fab693 LB |
3859 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3860 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3861 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3862 | &token); | |
3863 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3864 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3865 | |
a937b979 | 3866 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3867 | inode->i_atime.tv_sec, &token); |
a937b979 | 3868 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3869 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3870 | |
a937b979 | 3871 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3872 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3873 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3874 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3875 | |
a937b979 | 3876 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3877 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3878 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3879 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3880 | |
9cc97d64 | 3881 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3882 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3883 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3884 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3885 | ||
51fab693 LB |
3886 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3887 | &token); | |
3888 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3889 | &token); | |
3890 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3891 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3892 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3893 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3894 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3895 | } |
3896 | ||
d352ac68 CM |
3897 | /* |
3898 | * copy everything in the in-memory inode into the btree. | |
3899 | */ | |
2115133f | 3900 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3901 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3902 | { |
3903 | struct btrfs_inode_item *inode_item; | |
3904 | struct btrfs_path *path; | |
5f39d397 | 3905 | struct extent_buffer *leaf; |
39279cc3 CM |
3906 | int ret; |
3907 | ||
3908 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3909 | if (!path) |
3910 | return -ENOMEM; | |
3911 | ||
b9473439 | 3912 | path->leave_spinning = 1; |
16cdcec7 MX |
3913 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3914 | 1); | |
39279cc3 CM |
3915 | if (ret) { |
3916 | if (ret > 0) | |
3917 | ret = -ENOENT; | |
3918 | goto failed; | |
3919 | } | |
3920 | ||
5f39d397 CM |
3921 | leaf = path->nodes[0]; |
3922 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3923 | struct btrfs_inode_item); |
39279cc3 | 3924 | |
e02119d5 | 3925 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3926 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3927 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3928 | ret = 0; |
3929 | failed: | |
39279cc3 CM |
3930 | btrfs_free_path(path); |
3931 | return ret; | |
3932 | } | |
3933 | ||
2115133f CM |
3934 | /* |
3935 | * copy everything in the in-memory inode into the btree. | |
3936 | */ | |
3937 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3938 | struct btrfs_root *root, struct inode *inode) | |
3939 | { | |
3940 | int ret; | |
3941 | ||
3942 | /* | |
3943 | * If the inode is a free space inode, we can deadlock during commit | |
3944 | * if we put it into the delayed code. | |
3945 | * | |
3946 | * The data relocation inode should also be directly updated | |
3947 | * without delay | |
3948 | */ | |
83eea1f1 | 3949 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a | 3950 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
afcdd129 | 3951 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
8ea05e3a AB |
3952 | btrfs_update_root_times(trans, root); |
3953 | ||
2115133f CM |
3954 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3955 | if (!ret) | |
3956 | btrfs_set_inode_last_trans(trans, inode); | |
3957 | return ret; | |
3958 | } | |
3959 | ||
3960 | return btrfs_update_inode_item(trans, root, inode); | |
3961 | } | |
3962 | ||
be6aef60 JB |
3963 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3964 | struct btrfs_root *root, | |
3965 | struct inode *inode) | |
2115133f CM |
3966 | { |
3967 | int ret; | |
3968 | ||
3969 | ret = btrfs_update_inode(trans, root, inode); | |
3970 | if (ret == -ENOSPC) | |
3971 | return btrfs_update_inode_item(trans, root, inode); | |
3972 | return ret; | |
3973 | } | |
3974 | ||
d352ac68 CM |
3975 | /* |
3976 | * unlink helper that gets used here in inode.c and in the tree logging | |
3977 | * recovery code. It remove a link in a directory with a given name, and | |
3978 | * also drops the back refs in the inode to the directory | |
3979 | */ | |
92986796 AV |
3980 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3981 | struct btrfs_root *root, | |
3982 | struct inode *dir, struct inode *inode, | |
3983 | const char *name, int name_len) | |
39279cc3 CM |
3984 | { |
3985 | struct btrfs_path *path; | |
39279cc3 | 3986 | int ret = 0; |
5f39d397 | 3987 | struct extent_buffer *leaf; |
39279cc3 | 3988 | struct btrfs_dir_item *di; |
5f39d397 | 3989 | struct btrfs_key key; |
aec7477b | 3990 | u64 index; |
33345d01 LZ |
3991 | u64 ino = btrfs_ino(inode); |
3992 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3993 | |
3994 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3995 | if (!path) { |
3996 | ret = -ENOMEM; | |
554233a6 | 3997 | goto out; |
54aa1f4d CM |
3998 | } |
3999 | ||
b9473439 | 4000 | path->leave_spinning = 1; |
33345d01 | 4001 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
4002 | name, name_len, -1); |
4003 | if (IS_ERR(di)) { | |
4004 | ret = PTR_ERR(di); | |
4005 | goto err; | |
4006 | } | |
4007 | if (!di) { | |
4008 | ret = -ENOENT; | |
4009 | goto err; | |
4010 | } | |
5f39d397 CM |
4011 | leaf = path->nodes[0]; |
4012 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 4013 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
4014 | if (ret) |
4015 | goto err; | |
b3b4aa74 | 4016 | btrfs_release_path(path); |
39279cc3 | 4017 | |
67de1176 MX |
4018 | /* |
4019 | * If we don't have dir index, we have to get it by looking up | |
4020 | * the inode ref, since we get the inode ref, remove it directly, | |
4021 | * it is unnecessary to do delayed deletion. | |
4022 | * | |
4023 | * But if we have dir index, needn't search inode ref to get it. | |
4024 | * Since the inode ref is close to the inode item, it is better | |
4025 | * that we delay to delete it, and just do this deletion when | |
4026 | * we update the inode item. | |
4027 | */ | |
4028 | if (BTRFS_I(inode)->dir_index) { | |
4029 | ret = btrfs_delayed_delete_inode_ref(inode); | |
4030 | if (!ret) { | |
4031 | index = BTRFS_I(inode)->dir_index; | |
4032 | goto skip_backref; | |
4033 | } | |
4034 | } | |
4035 | ||
33345d01 LZ |
4036 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4037 | dir_ino, &index); | |
aec7477b | 4038 | if (ret) { |
c2cf52eb SK |
4039 | btrfs_info(root->fs_info, |
4040 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 4041 | name_len, name, ino, dir_ino); |
66642832 | 4042 | btrfs_abort_transaction(trans, ret); |
aec7477b JB |
4043 | goto err; |
4044 | } | |
67de1176 | 4045 | skip_backref: |
16cdcec7 | 4046 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa | 4047 | if (ret) { |
66642832 | 4048 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 4049 | goto err; |
79787eaa | 4050 | } |
39279cc3 | 4051 | |
e02119d5 | 4052 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 4053 | inode, dir_ino); |
79787eaa | 4054 | if (ret != 0 && ret != -ENOENT) { |
66642832 | 4055 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4056 | goto err; |
4057 | } | |
e02119d5 CM |
4058 | |
4059 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
4060 | dir, index); | |
6418c961 CM |
4061 | if (ret == -ENOENT) |
4062 | ret = 0; | |
d4e3991b | 4063 | else if (ret) |
66642832 | 4064 | btrfs_abort_transaction(trans, ret); |
39279cc3 CM |
4065 | err: |
4066 | btrfs_free_path(path); | |
e02119d5 CM |
4067 | if (ret) |
4068 | goto out; | |
4069 | ||
4070 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
4071 | inode_inc_iversion(inode); |
4072 | inode_inc_iversion(dir); | |
04b285f3 DD |
4073 | inode->i_ctime = dir->i_mtime = |
4074 | dir->i_ctime = current_fs_time(inode->i_sb); | |
b9959295 | 4075 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4076 | out: |
39279cc3 CM |
4077 | return ret; |
4078 | } | |
4079 | ||
92986796 AV |
4080 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4081 | struct btrfs_root *root, | |
4082 | struct inode *dir, struct inode *inode, | |
4083 | const char *name, int name_len) | |
4084 | { | |
4085 | int ret; | |
4086 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4087 | if (!ret) { | |
8b558c5f | 4088 | drop_nlink(inode); |
92986796 AV |
4089 | ret = btrfs_update_inode(trans, root, inode); |
4090 | } | |
4091 | return ret; | |
4092 | } | |
39279cc3 | 4093 | |
a22285a6 YZ |
4094 | /* |
4095 | * helper to start transaction for unlink and rmdir. | |
4096 | * | |
d52be818 JB |
4097 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4098 | * if we cannot make our reservations the normal way try and see if there is | |
4099 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4100 | * allow the unlink to occur. | |
a22285a6 | 4101 | */ |
d52be818 | 4102 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4103 | { |
a22285a6 | 4104 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4105 | |
e70bea5f JB |
4106 | /* |
4107 | * 1 for the possible orphan item | |
4108 | * 1 for the dir item | |
4109 | * 1 for the dir index | |
4110 | * 1 for the inode ref | |
e70bea5f JB |
4111 | * 1 for the inode |
4112 | */ | |
8eab77ff | 4113 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4114 | } |
4115 | ||
4116 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4117 | { | |
4118 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4119 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4120 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4121 | int ret; |
a22285a6 | 4122 | |
d52be818 | 4123 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4124 | if (IS_ERR(trans)) |
4125 | return PTR_ERR(trans); | |
5f39d397 | 4126 | |
2b0143b5 | 4127 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
12fcfd22 | 4128 | |
2b0143b5 | 4129 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4130 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4131 | if (ret) |
4132 | goto out; | |
7b128766 | 4133 | |
a22285a6 | 4134 | if (inode->i_nlink == 0) { |
7b128766 | 4135 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4136 | if (ret) |
4137 | goto out; | |
a22285a6 | 4138 | } |
7b128766 | 4139 | |
b532402e | 4140 | out: |
d52be818 | 4141 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4142 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4143 | return ret; |
4144 | } | |
4145 | ||
4df27c4d YZ |
4146 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4147 | struct btrfs_root *root, | |
4148 | struct inode *dir, u64 objectid, | |
4149 | const char *name, int name_len) | |
4150 | { | |
4151 | struct btrfs_path *path; | |
4152 | struct extent_buffer *leaf; | |
4153 | struct btrfs_dir_item *di; | |
4154 | struct btrfs_key key; | |
4155 | u64 index; | |
4156 | int ret; | |
33345d01 | 4157 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4158 | |
4159 | path = btrfs_alloc_path(); | |
4160 | if (!path) | |
4161 | return -ENOMEM; | |
4162 | ||
33345d01 | 4163 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4164 | name, name_len, -1); |
79787eaa JM |
4165 | if (IS_ERR_OR_NULL(di)) { |
4166 | if (!di) | |
4167 | ret = -ENOENT; | |
4168 | else | |
4169 | ret = PTR_ERR(di); | |
4170 | goto out; | |
4171 | } | |
4df27c4d YZ |
4172 | |
4173 | leaf = path->nodes[0]; | |
4174 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4175 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4176 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa | 4177 | if (ret) { |
66642832 | 4178 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4179 | goto out; |
4180 | } | |
b3b4aa74 | 4181 | btrfs_release_path(path); |
4df27c4d YZ |
4182 | |
4183 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4184 | objectid, root->root_key.objectid, | |
33345d01 | 4185 | dir_ino, &index, name, name_len); |
4df27c4d | 4186 | if (ret < 0) { |
79787eaa | 4187 | if (ret != -ENOENT) { |
66642832 | 4188 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4189 | goto out; |
4190 | } | |
33345d01 | 4191 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4192 | name, name_len); |
79787eaa JM |
4193 | if (IS_ERR_OR_NULL(di)) { |
4194 | if (!di) | |
4195 | ret = -ENOENT; | |
4196 | else | |
4197 | ret = PTR_ERR(di); | |
66642832 | 4198 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4199 | goto out; |
4200 | } | |
4df27c4d YZ |
4201 | |
4202 | leaf = path->nodes[0]; | |
4203 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4204 | btrfs_release_path(path); |
4df27c4d YZ |
4205 | index = key.offset; |
4206 | } | |
945d8962 | 4207 | btrfs_release_path(path); |
4df27c4d | 4208 | |
16cdcec7 | 4209 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa | 4210 | if (ret) { |
66642832 | 4211 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4212 | goto out; |
4213 | } | |
4df27c4d YZ |
4214 | |
4215 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4216 | inode_inc_iversion(dir); |
04b285f3 | 4217 | dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb); |
5a24e84c | 4218 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa | 4219 | if (ret) |
66642832 | 4220 | btrfs_abort_transaction(trans, ret); |
79787eaa | 4221 | out: |
71d7aed0 | 4222 | btrfs_free_path(path); |
79787eaa | 4223 | return ret; |
4df27c4d YZ |
4224 | } |
4225 | ||
39279cc3 CM |
4226 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4227 | { | |
2b0143b5 | 4228 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4229 | int err = 0; |
39279cc3 | 4230 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4231 | struct btrfs_trans_handle *trans; |
44f714da | 4232 | u64 last_unlink_trans; |
39279cc3 | 4233 | |
b3ae244e | 4234 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4235 | return -ENOTEMPTY; |
b3ae244e DS |
4236 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4237 | return -EPERM; | |
134d4512 | 4238 | |
d52be818 | 4239 | trans = __unlink_start_trans(dir); |
a22285a6 | 4240 | if (IS_ERR(trans)) |
5df6a9f6 | 4241 | return PTR_ERR(trans); |
5df6a9f6 | 4242 | |
33345d01 | 4243 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4244 | err = btrfs_unlink_subvol(trans, root, dir, |
4245 | BTRFS_I(inode)->location.objectid, | |
4246 | dentry->d_name.name, | |
4247 | dentry->d_name.len); | |
4248 | goto out; | |
4249 | } | |
4250 | ||
7b128766 JB |
4251 | err = btrfs_orphan_add(trans, inode); |
4252 | if (err) | |
4df27c4d | 4253 | goto out; |
7b128766 | 4254 | |
44f714da FM |
4255 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
4256 | ||
39279cc3 | 4257 | /* now the directory is empty */ |
2b0143b5 | 4258 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4259 | dentry->d_name.name, dentry->d_name.len); |
44f714da | 4260 | if (!err) { |
dbe674a9 | 4261 | btrfs_i_size_write(inode, 0); |
44f714da FM |
4262 | /* |
4263 | * Propagate the last_unlink_trans value of the deleted dir to | |
4264 | * its parent directory. This is to prevent an unrecoverable | |
4265 | * log tree in the case we do something like this: | |
4266 | * 1) create dir foo | |
4267 | * 2) create snapshot under dir foo | |
4268 | * 3) delete the snapshot | |
4269 | * 4) rmdir foo | |
4270 | * 5) mkdir foo | |
4271 | * 6) fsync foo or some file inside foo | |
4272 | */ | |
4273 | if (last_unlink_trans >= trans->transid) | |
4274 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; | |
4275 | } | |
4df27c4d | 4276 | out: |
d52be818 | 4277 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4278 | btrfs_btree_balance_dirty(root); |
3954401f | 4279 | |
39279cc3 CM |
4280 | return err; |
4281 | } | |
4282 | ||
28f75a0e CM |
4283 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4284 | struct btrfs_root *root, | |
4285 | u64 bytes_deleted) | |
4286 | { | |
4287 | int ret; | |
4288 | ||
dc95f7bf JB |
4289 | /* |
4290 | * This is only used to apply pressure to the enospc system, we don't | |
4291 | * intend to use this reservation at all. | |
4292 | */ | |
28f75a0e | 4293 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
dc95f7bf | 4294 | bytes_deleted *= root->nodesize; |
28f75a0e CM |
4295 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
4296 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
dc95f7bf JB |
4297 | if (!ret) { |
4298 | trace_btrfs_space_reservation(root->fs_info, "transaction", | |
4299 | trans->transid, | |
4300 | bytes_deleted, 1); | |
28f75a0e | 4301 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4302 | } |
28f75a0e CM |
4303 | return ret; |
4304 | ||
4305 | } | |
4306 | ||
0305cd5f FM |
4307 | static int truncate_inline_extent(struct inode *inode, |
4308 | struct btrfs_path *path, | |
4309 | struct btrfs_key *found_key, | |
4310 | const u64 item_end, | |
4311 | const u64 new_size) | |
4312 | { | |
4313 | struct extent_buffer *leaf = path->nodes[0]; | |
4314 | int slot = path->slots[0]; | |
4315 | struct btrfs_file_extent_item *fi; | |
4316 | u32 size = (u32)(new_size - found_key->offset); | |
4317 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4318 | ||
4319 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4320 | ||
4321 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4322 | loff_t offset = new_size; | |
09cbfeaf | 4323 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4324 | |
4325 | /* | |
4326 | * Zero out the remaining of the last page of our inline extent, | |
4327 | * instead of directly truncating our inline extent here - that | |
4328 | * would be much more complex (decompressing all the data, then | |
4329 | * compressing the truncated data, which might be bigger than | |
4330 | * the size of the inline extent, resize the extent, etc). | |
4331 | * We release the path because to get the page we might need to | |
4332 | * read the extent item from disk (data not in the page cache). | |
4333 | */ | |
4334 | btrfs_release_path(path); | |
9703fefe CR |
4335 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4336 | 0); | |
0305cd5f FM |
4337 | } |
4338 | ||
4339 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4340 | size = btrfs_file_extent_calc_inline_size(size); | |
4341 | btrfs_truncate_item(root, path, size, 1); | |
4342 | ||
4343 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4344 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4345 | ||
4346 | return 0; | |
4347 | } | |
4348 | ||
39279cc3 CM |
4349 | /* |
4350 | * this can truncate away extent items, csum items and directory items. | |
4351 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4352 | * any higher than new_size |
39279cc3 CM |
4353 | * |
4354 | * csum items that cross the new i_size are truncated to the new size | |
4355 | * as well. | |
7b128766 JB |
4356 | * |
4357 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4358 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4359 | */ |
8082510e YZ |
4360 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4361 | struct btrfs_root *root, | |
4362 | struct inode *inode, | |
4363 | u64 new_size, u32 min_type) | |
39279cc3 | 4364 | { |
39279cc3 | 4365 | struct btrfs_path *path; |
5f39d397 | 4366 | struct extent_buffer *leaf; |
39279cc3 | 4367 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4368 | struct btrfs_key key; |
4369 | struct btrfs_key found_key; | |
39279cc3 | 4370 | u64 extent_start = 0; |
db94535d | 4371 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4372 | u64 extent_offset = 0; |
39279cc3 | 4373 | u64 item_end = 0; |
c1aa4575 | 4374 | u64 last_size = new_size; |
8082510e | 4375 | u32 found_type = (u8)-1; |
39279cc3 CM |
4376 | int found_extent; |
4377 | int del_item; | |
85e21bac CM |
4378 | int pending_del_nr = 0; |
4379 | int pending_del_slot = 0; | |
179e29e4 | 4380 | int extent_type = -1; |
8082510e YZ |
4381 | int ret; |
4382 | int err = 0; | |
33345d01 | 4383 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4384 | u64 bytes_deleted = 0; |
1262133b JB |
4385 | bool be_nice = 0; |
4386 | bool should_throttle = 0; | |
28f75a0e | 4387 | bool should_end = 0; |
8082510e YZ |
4388 | |
4389 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4390 | |
28ed1345 CM |
4391 | /* |
4392 | * for non-free space inodes and ref cows, we want to back off from | |
4393 | * time to time | |
4394 | */ | |
4395 | if (!btrfs_is_free_space_inode(inode) && | |
4396 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4397 | be_nice = 1; | |
4398 | ||
0eb0e19c MF |
4399 | path = btrfs_alloc_path(); |
4400 | if (!path) | |
4401 | return -ENOMEM; | |
e4058b54 | 4402 | path->reada = READA_BACK; |
0eb0e19c | 4403 | |
5dc562c5 JB |
4404 | /* |
4405 | * We want to drop from the next block forward in case this new size is | |
4406 | * not block aligned since we will be keeping the last block of the | |
4407 | * extent just the way it is. | |
4408 | */ | |
27cdeb70 MX |
4409 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4410 | root == root->fs_info->tree_root) | |
fda2832f QW |
4411 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4412 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4413 | |
16cdcec7 MX |
4414 | /* |
4415 | * This function is also used to drop the items in the log tree before | |
4416 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4417 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4418 | * items. | |
4419 | */ | |
4420 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4421 | btrfs_kill_delayed_inode_items(inode); | |
4422 | ||
33345d01 | 4423 | key.objectid = ino; |
39279cc3 | 4424 | key.offset = (u64)-1; |
5f39d397 CM |
4425 | key.type = (u8)-1; |
4426 | ||
85e21bac | 4427 | search_again: |
28ed1345 CM |
4428 | /* |
4429 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4430 | * up a huge file in a single leaf. Most of the time that | |
4431 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4432 | */ | |
ee22184b | 4433 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4434 | if (btrfs_should_end_transaction(trans, root)) { |
4435 | err = -EAGAIN; | |
4436 | goto error; | |
4437 | } | |
4438 | } | |
4439 | ||
4440 | ||
b9473439 | 4441 | path->leave_spinning = 1; |
85e21bac | 4442 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4443 | if (ret < 0) { |
4444 | err = ret; | |
4445 | goto out; | |
4446 | } | |
d397712b | 4447 | |
85e21bac | 4448 | if (ret > 0) { |
e02119d5 CM |
4449 | /* there are no items in the tree for us to truncate, we're |
4450 | * done | |
4451 | */ | |
8082510e YZ |
4452 | if (path->slots[0] == 0) |
4453 | goto out; | |
85e21bac CM |
4454 | path->slots[0]--; |
4455 | } | |
4456 | ||
d397712b | 4457 | while (1) { |
39279cc3 | 4458 | fi = NULL; |
5f39d397 CM |
4459 | leaf = path->nodes[0]; |
4460 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4461 | found_type = found_key.type; |
39279cc3 | 4462 | |
33345d01 | 4463 | if (found_key.objectid != ino) |
39279cc3 | 4464 | break; |
5f39d397 | 4465 | |
85e21bac | 4466 | if (found_type < min_type) |
39279cc3 CM |
4467 | break; |
4468 | ||
5f39d397 | 4469 | item_end = found_key.offset; |
39279cc3 | 4470 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4471 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4472 | struct btrfs_file_extent_item); |
179e29e4 CM |
4473 | extent_type = btrfs_file_extent_type(leaf, fi); |
4474 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4475 | item_end += |
db94535d | 4476 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4477 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4478 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4479 | path->slots[0], fi); |
39279cc3 | 4480 | } |
008630c1 | 4481 | item_end--; |
39279cc3 | 4482 | } |
8082510e YZ |
4483 | if (found_type > min_type) { |
4484 | del_item = 1; | |
4485 | } else { | |
4486 | if (item_end < new_size) | |
b888db2b | 4487 | break; |
8082510e YZ |
4488 | if (found_key.offset >= new_size) |
4489 | del_item = 1; | |
4490 | else | |
4491 | del_item = 0; | |
39279cc3 | 4492 | } |
39279cc3 | 4493 | found_extent = 0; |
39279cc3 | 4494 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4495 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4496 | goto delete; | |
4497 | ||
7f4f6e0a JB |
4498 | if (del_item) |
4499 | last_size = found_key.offset; | |
4500 | else | |
4501 | last_size = new_size; | |
4502 | ||
179e29e4 | 4503 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4504 | u64 num_dec; |
db94535d | 4505 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4506 | if (!del_item) { |
db94535d CM |
4507 | u64 orig_num_bytes = |
4508 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4509 | extent_num_bytes = ALIGN(new_size - |
4510 | found_key.offset, | |
4511 | root->sectorsize); | |
db94535d CM |
4512 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4513 | extent_num_bytes); | |
4514 | num_dec = (orig_num_bytes - | |
9069218d | 4515 | extent_num_bytes); |
27cdeb70 MX |
4516 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4517 | &root->state) && | |
4518 | extent_start != 0) | |
a76a3cd4 | 4519 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4520 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4521 | } else { |
db94535d CM |
4522 | extent_num_bytes = |
4523 | btrfs_file_extent_disk_num_bytes(leaf, | |
4524 | fi); | |
5d4f98a2 YZ |
4525 | extent_offset = found_key.offset - |
4526 | btrfs_file_extent_offset(leaf, fi); | |
4527 | ||
39279cc3 | 4528 | /* FIXME blocksize != 4096 */ |
9069218d | 4529 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4530 | if (extent_start != 0) { |
4531 | found_extent = 1; | |
27cdeb70 MX |
4532 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4533 | &root->state)) | |
a76a3cd4 | 4534 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4535 | } |
39279cc3 | 4536 | } |
9069218d | 4537 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4538 | /* |
4539 | * we can't truncate inline items that have had | |
4540 | * special encodings | |
4541 | */ | |
4542 | if (!del_item && | |
c8b97818 CM |
4543 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4544 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4545 | |
4546 | /* | |
0305cd5f FM |
4547 | * Need to release path in order to truncate a |
4548 | * compressed extent. So delete any accumulated | |
4549 | * extent items so far. | |
514ac8ad | 4550 | */ |
0305cd5f FM |
4551 | if (btrfs_file_extent_compression(leaf, fi) != |
4552 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4553 | err = btrfs_del_items(trans, root, path, | |
4554 | pending_del_slot, | |
4555 | pending_del_nr); | |
4556 | if (err) { | |
4557 | btrfs_abort_transaction(trans, | |
0305cd5f FM |
4558 | err); |
4559 | goto error; | |
4560 | } | |
4561 | pending_del_nr = 0; | |
4562 | } | |
4563 | ||
4564 | err = truncate_inline_extent(inode, path, | |
4565 | &found_key, | |
4566 | item_end, | |
4567 | new_size); | |
4568 | if (err) { | |
66642832 | 4569 | btrfs_abort_transaction(trans, err); |
0305cd5f FM |
4570 | goto error; |
4571 | } | |
27cdeb70 MX |
4572 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4573 | &root->state)) { | |
0305cd5f | 4574 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4575 | } |
39279cc3 | 4576 | } |
179e29e4 | 4577 | delete: |
39279cc3 | 4578 | if (del_item) { |
85e21bac CM |
4579 | if (!pending_del_nr) { |
4580 | /* no pending yet, add ourselves */ | |
4581 | pending_del_slot = path->slots[0]; | |
4582 | pending_del_nr = 1; | |
4583 | } else if (pending_del_nr && | |
4584 | path->slots[0] + 1 == pending_del_slot) { | |
4585 | /* hop on the pending chunk */ | |
4586 | pending_del_nr++; | |
4587 | pending_del_slot = path->slots[0]; | |
4588 | } else { | |
d397712b | 4589 | BUG(); |
85e21bac | 4590 | } |
39279cc3 CM |
4591 | } else { |
4592 | break; | |
4593 | } | |
28f75a0e CM |
4594 | should_throttle = 0; |
4595 | ||
27cdeb70 MX |
4596 | if (found_extent && |
4597 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4598 | root == root->fs_info->tree_root)) { | |
b9473439 | 4599 | btrfs_set_path_blocking(path); |
28ed1345 | 4600 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4601 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4602 | extent_num_bytes, 0, |
4603 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4604 | ino, extent_offset); |
39279cc3 | 4605 | BUG_ON(ret); |
1262133b | 4606 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 | 4607 | btrfs_async_run_delayed_refs(root, |
31b9655f | 4608 | trans->transid, |
28ed1345 | 4609 | trans->delayed_ref_updates * 2, 0); |
28f75a0e CM |
4610 | if (be_nice) { |
4611 | if (truncate_space_check(trans, root, | |
4612 | extent_num_bytes)) { | |
4613 | should_end = 1; | |
4614 | } | |
4615 | if (btrfs_should_throttle_delayed_refs(trans, | |
4616 | root)) { | |
4617 | should_throttle = 1; | |
4618 | } | |
4619 | } | |
39279cc3 | 4620 | } |
85e21bac | 4621 | |
8082510e YZ |
4622 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4623 | break; | |
4624 | ||
4625 | if (path->slots[0] == 0 || | |
1262133b | 4626 | path->slots[0] != pending_del_slot || |
28f75a0e | 4627 | should_throttle || should_end) { |
8082510e YZ |
4628 | if (pending_del_nr) { |
4629 | ret = btrfs_del_items(trans, root, path, | |
4630 | pending_del_slot, | |
4631 | pending_del_nr); | |
79787eaa | 4632 | if (ret) { |
66642832 | 4633 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
4634 | goto error; |
4635 | } | |
8082510e YZ |
4636 | pending_del_nr = 0; |
4637 | } | |
b3b4aa74 | 4638 | btrfs_release_path(path); |
28f75a0e | 4639 | if (should_throttle) { |
1262133b JB |
4640 | unsigned long updates = trans->delayed_ref_updates; |
4641 | if (updates) { | |
4642 | trans->delayed_ref_updates = 0; | |
4643 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4644 | if (ret && !err) | |
4645 | err = ret; | |
4646 | } | |
4647 | } | |
28f75a0e CM |
4648 | /* |
4649 | * if we failed to refill our space rsv, bail out | |
4650 | * and let the transaction restart | |
4651 | */ | |
4652 | if (should_end) { | |
4653 | err = -EAGAIN; | |
4654 | goto error; | |
4655 | } | |
85e21bac | 4656 | goto search_again; |
8082510e YZ |
4657 | } else { |
4658 | path->slots[0]--; | |
85e21bac | 4659 | } |
39279cc3 | 4660 | } |
8082510e | 4661 | out: |
85e21bac CM |
4662 | if (pending_del_nr) { |
4663 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4664 | pending_del_nr); | |
79787eaa | 4665 | if (ret) |
66642832 | 4666 | btrfs_abort_transaction(trans, ret); |
85e21bac | 4667 | } |
79787eaa | 4668 | error: |
c1aa4575 | 4669 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4670 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4671 | |
39279cc3 | 4672 | btrfs_free_path(path); |
28ed1345 | 4673 | |
ee22184b | 4674 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4675 | unsigned long updates = trans->delayed_ref_updates; |
4676 | if (updates) { | |
4677 | trans->delayed_ref_updates = 0; | |
4678 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4679 | if (ret && !err) | |
4680 | err = ret; | |
4681 | } | |
4682 | } | |
8082510e | 4683 | return err; |
39279cc3 CM |
4684 | } |
4685 | ||
4686 | /* | |
9703fefe | 4687 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4688 | * @inode - inode that we're zeroing |
4689 | * @from - the offset to start zeroing | |
4690 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4691 | * offset | |
4692 | * @front - zero up to the offset instead of from the offset on | |
4693 | * | |
9703fefe | 4694 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4695 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4696 | */ |
9703fefe | 4697 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4698 | int front) |
39279cc3 | 4699 | { |
2aaa6655 | 4700 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4701 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4702 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4703 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4704 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4705 | char *kaddr; |
db94535d | 4706 | u32 blocksize = root->sectorsize; |
09cbfeaf | 4707 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4708 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4709 | struct page *page; |
3b16a4e3 | 4710 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4711 | int ret = 0; |
9703fefe CR |
4712 | u64 block_start; |
4713 | u64 block_end; | |
39279cc3 | 4714 | |
2aaa6655 JB |
4715 | if ((offset & (blocksize - 1)) == 0 && |
4716 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4717 | goto out; |
9703fefe | 4718 | |
7cf5b976 | 4719 | ret = btrfs_delalloc_reserve_space(inode, |
9703fefe | 4720 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4721 | if (ret) |
4722 | goto out; | |
39279cc3 | 4723 | |
211c17f5 | 4724 | again: |
3b16a4e3 | 4725 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4726 | if (!page) { |
7cf5b976 | 4727 | btrfs_delalloc_release_space(inode, |
9703fefe CR |
4728 | round_down(from, blocksize), |
4729 | blocksize); | |
ac6a2b36 | 4730 | ret = -ENOMEM; |
39279cc3 | 4731 | goto out; |
5d5e103a | 4732 | } |
e6dcd2dc | 4733 | |
9703fefe CR |
4734 | block_start = round_down(from, blocksize); |
4735 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4736 | |
39279cc3 | 4737 | if (!PageUptodate(page)) { |
9ebefb18 | 4738 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4739 | lock_page(page); |
211c17f5 CM |
4740 | if (page->mapping != mapping) { |
4741 | unlock_page(page); | |
09cbfeaf | 4742 | put_page(page); |
211c17f5 CM |
4743 | goto again; |
4744 | } | |
39279cc3 CM |
4745 | if (!PageUptodate(page)) { |
4746 | ret = -EIO; | |
89642229 | 4747 | goto out_unlock; |
39279cc3 CM |
4748 | } |
4749 | } | |
211c17f5 | 4750 | wait_on_page_writeback(page); |
e6dcd2dc | 4751 | |
9703fefe | 4752 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4753 | set_page_extent_mapped(page); |
4754 | ||
9703fefe | 4755 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4756 | if (ordered) { |
9703fefe | 4757 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4758 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4759 | unlock_page(page); |
09cbfeaf | 4760 | put_page(page); |
eb84ae03 | 4761 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4762 | btrfs_put_ordered_extent(ordered); |
4763 | goto again; | |
4764 | } | |
4765 | ||
9703fefe | 4766 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4767 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4768 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4769 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4770 | |
9703fefe | 4771 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
ba8b04c1 | 4772 | &cached_state, 0); |
9ed74f2d | 4773 | if (ret) { |
9703fefe | 4774 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4775 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4776 | goto out_unlock; |
4777 | } | |
4778 | ||
9703fefe | 4779 | if (offset != blocksize) { |
2aaa6655 | 4780 | if (!len) |
9703fefe | 4781 | len = blocksize - offset; |
e6dcd2dc | 4782 | kaddr = kmap(page); |
2aaa6655 | 4783 | if (front) |
9703fefe CR |
4784 | memset(kaddr + (block_start - page_offset(page)), |
4785 | 0, offset); | |
2aaa6655 | 4786 | else |
9703fefe CR |
4787 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4788 | 0, len); | |
e6dcd2dc CM |
4789 | flush_dcache_page(page); |
4790 | kunmap(page); | |
4791 | } | |
247e743c | 4792 | ClearPageChecked(page); |
e6dcd2dc | 4793 | set_page_dirty(page); |
9703fefe | 4794 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4795 | GFP_NOFS); |
39279cc3 | 4796 | |
89642229 | 4797 | out_unlock: |
5d5e103a | 4798 | if (ret) |
9703fefe CR |
4799 | btrfs_delalloc_release_space(inode, block_start, |
4800 | blocksize); | |
39279cc3 | 4801 | unlock_page(page); |
09cbfeaf | 4802 | put_page(page); |
39279cc3 CM |
4803 | out: |
4804 | return ret; | |
4805 | } | |
4806 | ||
16e7549f JB |
4807 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4808 | u64 offset, u64 len) | |
4809 | { | |
4810 | struct btrfs_trans_handle *trans; | |
4811 | int ret; | |
4812 | ||
4813 | /* | |
4814 | * Still need to make sure the inode looks like it's been updated so | |
4815 | * that any holes get logged if we fsync. | |
4816 | */ | |
4817 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4818 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4819 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4820 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4821 | return 0; | |
4822 | } | |
4823 | ||
4824 | /* | |
4825 | * 1 - for the one we're dropping | |
4826 | * 1 - for the one we're adding | |
4827 | * 1 - for updating the inode. | |
4828 | */ | |
4829 | trans = btrfs_start_transaction(root, 3); | |
4830 | if (IS_ERR(trans)) | |
4831 | return PTR_ERR(trans); | |
4832 | ||
4833 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4834 | if (ret) { | |
66642832 | 4835 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4836 | btrfs_end_transaction(trans, root); |
4837 | return ret; | |
4838 | } | |
4839 | ||
4840 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4841 | 0, 0, len, 0, len, 0, 0, 0); | |
4842 | if (ret) | |
66642832 | 4843 | btrfs_abort_transaction(trans, ret); |
16e7549f JB |
4844 | else |
4845 | btrfs_update_inode(trans, root, inode); | |
4846 | btrfs_end_transaction(trans, root); | |
4847 | return ret; | |
4848 | } | |
4849 | ||
695a0d0d JB |
4850 | /* |
4851 | * This function puts in dummy file extents for the area we're creating a hole | |
4852 | * for. So if we are truncating this file to a larger size we need to insert | |
4853 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4854 | * the range between oldsize and size | |
4855 | */ | |
a41ad394 | 4856 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4857 | { |
9036c102 YZ |
4858 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4859 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4860 | struct extent_map *em = NULL; |
2ac55d41 | 4861 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4862 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4863 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4864 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4865 | u64 last_byte; |
4866 | u64 cur_offset; | |
4867 | u64 hole_size; | |
9ed74f2d | 4868 | int err = 0; |
39279cc3 | 4869 | |
a71754fc | 4870 | /* |
9703fefe CR |
4871 | * If our size started in the middle of a block we need to zero out the |
4872 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4873 | * expose stale data. |
4874 | */ | |
9703fefe | 4875 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4876 | if (err) |
4877 | return err; | |
4878 | ||
9036c102 YZ |
4879 | if (size <= hole_start) |
4880 | return 0; | |
4881 | ||
9036c102 YZ |
4882 | while (1) { |
4883 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4884 | |
ff13db41 | 4885 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4886 | &cached_state); |
fa7c1494 MX |
4887 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4888 | block_end - hole_start); | |
9036c102 YZ |
4889 | if (!ordered) |
4890 | break; | |
2ac55d41 JB |
4891 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4892 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4893 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4894 | btrfs_put_ordered_extent(ordered); |
4895 | } | |
39279cc3 | 4896 | |
9036c102 YZ |
4897 | cur_offset = hole_start; |
4898 | while (1) { | |
4899 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4900 | block_end - cur_offset, 0); | |
79787eaa JM |
4901 | if (IS_ERR(em)) { |
4902 | err = PTR_ERR(em); | |
f2767956 | 4903 | em = NULL; |
79787eaa JM |
4904 | break; |
4905 | } | |
9036c102 | 4906 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4907 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4908 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4909 | struct extent_map *hole_em; |
9036c102 | 4910 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4911 | |
16e7549f JB |
4912 | err = maybe_insert_hole(root, inode, cur_offset, |
4913 | hole_size); | |
4914 | if (err) | |
3893e33b | 4915 | break; |
5dc562c5 JB |
4916 | btrfs_drop_extent_cache(inode, cur_offset, |
4917 | cur_offset + hole_size - 1, 0); | |
4918 | hole_em = alloc_extent_map(); | |
4919 | if (!hole_em) { | |
4920 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4921 | &BTRFS_I(inode)->runtime_flags); | |
4922 | goto next; | |
4923 | } | |
4924 | hole_em->start = cur_offset; | |
4925 | hole_em->len = hole_size; | |
4926 | hole_em->orig_start = cur_offset; | |
8082510e | 4927 | |
5dc562c5 JB |
4928 | hole_em->block_start = EXTENT_MAP_HOLE; |
4929 | hole_em->block_len = 0; | |
b4939680 | 4930 | hole_em->orig_block_len = 0; |
cc95bef6 | 4931 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4932 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4933 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4934 | hole_em->generation = root->fs_info->generation; |
8082510e | 4935 | |
5dc562c5 JB |
4936 | while (1) { |
4937 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4938 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4939 | write_unlock(&em_tree->lock); |
4940 | if (err != -EEXIST) | |
4941 | break; | |
4942 | btrfs_drop_extent_cache(inode, cur_offset, | |
4943 | cur_offset + | |
4944 | hole_size - 1, 0); | |
4945 | } | |
4946 | free_extent_map(hole_em); | |
9036c102 | 4947 | } |
16e7549f | 4948 | next: |
9036c102 | 4949 | free_extent_map(em); |
a22285a6 | 4950 | em = NULL; |
9036c102 | 4951 | cur_offset = last_byte; |
8082510e | 4952 | if (cur_offset >= block_end) |
9036c102 YZ |
4953 | break; |
4954 | } | |
a22285a6 | 4955 | free_extent_map(em); |
2ac55d41 JB |
4956 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4957 | GFP_NOFS); | |
9036c102 YZ |
4958 | return err; |
4959 | } | |
39279cc3 | 4960 | |
3972f260 | 4961 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4962 | { |
f4a2f4c5 MX |
4963 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4964 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4965 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4966 | loff_t newsize = attr->ia_size; |
4967 | int mask = attr->ia_valid; | |
8082510e YZ |
4968 | int ret; |
4969 | ||
3972f260 ES |
4970 | /* |
4971 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4972 | * special case where we need to update the times despite not having | |
4973 | * these flags set. For all other operations the VFS set these flags | |
4974 | * explicitly if it wants a timestamp update. | |
4975 | */ | |
dff6efc3 CH |
4976 | if (newsize != oldsize) { |
4977 | inode_inc_iversion(inode); | |
4978 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4979 | inode->i_ctime = inode->i_mtime = | |
4980 | current_fs_time(inode->i_sb); | |
4981 | } | |
3972f260 | 4982 | |
a41ad394 | 4983 | if (newsize > oldsize) { |
9ea24bbe FM |
4984 | /* |
4985 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4986 | * This is to ensure the snapshot captures a fully consistent | |
4987 | * state of this file - if the snapshot captures this expanding | |
4988 | * truncation, it must capture all writes that happened before | |
4989 | * this truncation. | |
4990 | */ | |
0bc19f90 | 4991 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 4992 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4993 | if (ret) { |
4994 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4995 | return ret; |
9ea24bbe | 4996 | } |
8082510e | 4997 | |
f4a2f4c5 | 4998 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4999 | if (IS_ERR(trans)) { |
5000 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 5001 | return PTR_ERR(trans); |
9ea24bbe | 5002 | } |
f4a2f4c5 MX |
5003 | |
5004 | i_size_write(inode, newsize); | |
5005 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 5006 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 5007 | ret = btrfs_update_inode(trans, root, inode); |
9ea24bbe | 5008 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 5009 | btrfs_end_transaction(trans, root); |
a41ad394 | 5010 | } else { |
8082510e | 5011 | |
a41ad394 JB |
5012 | /* |
5013 | * We're truncating a file that used to have good data down to | |
5014 | * zero. Make sure it gets into the ordered flush list so that | |
5015 | * any new writes get down to disk quickly. | |
5016 | */ | |
5017 | if (newsize == 0) | |
72ac3c0d JB |
5018 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
5019 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 5020 | |
f3fe820c JB |
5021 | /* |
5022 | * 1 for the orphan item we're going to add | |
5023 | * 1 for the orphan item deletion. | |
5024 | */ | |
5025 | trans = btrfs_start_transaction(root, 2); | |
5026 | if (IS_ERR(trans)) | |
5027 | return PTR_ERR(trans); | |
5028 | ||
5029 | /* | |
5030 | * We need to do this in case we fail at _any_ point during the | |
5031 | * actual truncate. Once we do the truncate_setsize we could | |
5032 | * invalidate pages which forces any outstanding ordered io to | |
5033 | * be instantly completed which will give us extents that need | |
5034 | * to be truncated. If we fail to get an orphan inode down we | |
5035 | * could have left over extents that were never meant to live, | |
01327610 | 5036 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
5037 | * will be consistent. |
5038 | */ | |
5039 | ret = btrfs_orphan_add(trans, inode); | |
5040 | btrfs_end_transaction(trans, root); | |
5041 | if (ret) | |
5042 | return ret; | |
5043 | ||
a41ad394 JB |
5044 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5045 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5046 | |
5047 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
5048 | btrfs_inode_block_unlocked_dio(inode); | |
5049 | inode_dio_wait(inode); | |
5050 | btrfs_inode_resume_unlocked_dio(inode); | |
5051 | ||
a41ad394 | 5052 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5053 | if (ret && inode->i_nlink) { |
5054 | int err; | |
5055 | ||
5056 | /* | |
5057 | * failed to truncate, disk_i_size is only adjusted down | |
5058 | * as we remove extents, so it should represent the true | |
5059 | * size of the inode, so reset the in memory size and | |
5060 | * delete our orphan entry. | |
5061 | */ | |
5062 | trans = btrfs_join_transaction(root); | |
5063 | if (IS_ERR(trans)) { | |
5064 | btrfs_orphan_del(NULL, inode); | |
5065 | return ret; | |
5066 | } | |
5067 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
5068 | err = btrfs_orphan_del(trans, inode); | |
5069 | if (err) | |
66642832 | 5070 | btrfs_abort_transaction(trans, err); |
7f4f6e0a JB |
5071 | btrfs_end_transaction(trans, root); |
5072 | } | |
8082510e YZ |
5073 | } |
5074 | ||
a41ad394 | 5075 | return ret; |
8082510e YZ |
5076 | } |
5077 | ||
9036c102 YZ |
5078 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5079 | { | |
2b0143b5 | 5080 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5081 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5082 | int err; |
39279cc3 | 5083 | |
b83cc969 LZ |
5084 | if (btrfs_root_readonly(root)) |
5085 | return -EROFS; | |
5086 | ||
9036c102 YZ |
5087 | err = inode_change_ok(inode, attr); |
5088 | if (err) | |
5089 | return err; | |
2bf5a725 | 5090 | |
5a3f23d5 | 5091 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5092 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5093 | if (err) |
5094 | return err; | |
39279cc3 | 5095 | } |
9036c102 | 5096 | |
1025774c CH |
5097 | if (attr->ia_valid) { |
5098 | setattr_copy(inode, attr); | |
0c4d2d95 | 5099 | inode_inc_iversion(inode); |
22c44fe6 | 5100 | err = btrfs_dirty_inode(inode); |
1025774c | 5101 | |
22c44fe6 | 5102 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5103 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5104 | } |
33268eaf | 5105 | |
39279cc3 CM |
5106 | return err; |
5107 | } | |
61295eb8 | 5108 | |
131e404a FDBM |
5109 | /* |
5110 | * While truncating the inode pages during eviction, we get the VFS calling | |
5111 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5112 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5113 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5114 | * extent_state structures over and over, wasting lots of time. | |
5115 | * | |
5116 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5117 | * those expensive operations on a per page basis and do only the ordered io | |
5118 | * finishing, while we release here the extent_map and extent_state structures, | |
5119 | * without the excessive merging and splitting. | |
5120 | */ | |
5121 | static void evict_inode_truncate_pages(struct inode *inode) | |
5122 | { | |
5123 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5124 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5125 | struct rb_node *node; | |
5126 | ||
5127 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5128 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5129 | |
5130 | write_lock(&map_tree->lock); | |
5131 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5132 | struct extent_map *em; | |
5133 | ||
5134 | node = rb_first(&map_tree->map); | |
5135 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5136 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5137 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5138 | remove_extent_mapping(map_tree, em); |
5139 | free_extent_map(em); | |
7064dd5c FM |
5140 | if (need_resched()) { |
5141 | write_unlock(&map_tree->lock); | |
5142 | cond_resched(); | |
5143 | write_lock(&map_tree->lock); | |
5144 | } | |
131e404a FDBM |
5145 | } |
5146 | write_unlock(&map_tree->lock); | |
5147 | ||
6ca07097 FM |
5148 | /* |
5149 | * Keep looping until we have no more ranges in the io tree. | |
5150 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5151 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5152 | * still in progress (unlocked the pages in the bio but did not yet | |
5153 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5154 | * ranges can still be locked and eviction started because before |
5155 | * submitting those bios, which are executed by a separate task (work | |
5156 | * queue kthread), inode references (inode->i_count) were not taken | |
5157 | * (which would be dropped in the end io callback of each bio). | |
5158 | * Therefore here we effectively end up waiting for those bios and | |
5159 | * anyone else holding locked ranges without having bumped the inode's | |
5160 | * reference count - if we don't do it, when they access the inode's | |
5161 | * io_tree to unlock a range it may be too late, leading to an | |
5162 | * use-after-free issue. | |
5163 | */ | |
131e404a FDBM |
5164 | spin_lock(&io_tree->lock); |
5165 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5166 | struct extent_state *state; | |
5167 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5168 | u64 start; |
5169 | u64 end; | |
131e404a FDBM |
5170 | |
5171 | node = rb_first(&io_tree->state); | |
5172 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5173 | start = state->start; |
5174 | end = state->end; | |
131e404a FDBM |
5175 | spin_unlock(&io_tree->lock); |
5176 | ||
ff13db41 | 5177 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5178 | |
5179 | /* | |
5180 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5181 | * and its reserved space won't be freed by delayed_ref. | |
5182 | * So we need to free its reserved space here. | |
5183 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5184 | * | |
5185 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5186 | */ | |
5187 | if (state->state & EXTENT_DELALLOC) | |
5188 | btrfs_qgroup_free_data(inode, start, end - start + 1); | |
5189 | ||
6ca07097 | 5190 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5191 | EXTENT_LOCKED | EXTENT_DIRTY | |
5192 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5193 | EXTENT_DEFRAG, 1, 1, | |
5194 | &cached_state, GFP_NOFS); | |
131e404a | 5195 | |
7064dd5c | 5196 | cond_resched(); |
131e404a FDBM |
5197 | spin_lock(&io_tree->lock); |
5198 | } | |
5199 | spin_unlock(&io_tree->lock); | |
5200 | } | |
5201 | ||
bd555975 | 5202 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5203 | { |
5204 | struct btrfs_trans_handle *trans; | |
5205 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5206 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5207 | int steal_from_global = 0; |
3d48d981 | 5208 | u64 min_size; |
39279cc3 CM |
5209 | int ret; |
5210 | ||
1abe9b8a | 5211 | trace_btrfs_inode_evict(inode); |
5212 | ||
3d48d981 NB |
5213 | if (!root) { |
5214 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5215 | return; | |
5216 | } | |
5217 | ||
5218 | min_size = btrfs_calc_trunc_metadata_size(root, 1); | |
5219 | ||
131e404a FDBM |
5220 | evict_inode_truncate_pages(inode); |
5221 | ||
69e9c6c6 SB |
5222 | if (inode->i_nlink && |
5223 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5224 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5225 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5226 | goto no_delete; |
5227 | ||
39279cc3 | 5228 | if (is_bad_inode(inode)) { |
7b128766 | 5229 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5230 | goto no_delete; |
5231 | } | |
bd555975 | 5232 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5233 | if (!special_file(inode->i_mode)) |
5234 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5235 | |
f612496b MX |
5236 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5237 | ||
afcdd129 | 5238 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
6bf02314 | 5239 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5240 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5241 | goto no_delete; |
5242 | } | |
5243 | ||
76dda93c | 5244 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5245 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5246 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5247 | goto no_delete; |
5248 | } | |
5249 | ||
0e8c36a9 MX |
5250 | ret = btrfs_commit_inode_delayed_inode(inode); |
5251 | if (ret) { | |
5252 | btrfs_orphan_del(NULL, inode); | |
5253 | goto no_delete; | |
5254 | } | |
5255 | ||
66d8f3dd | 5256 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5257 | if (!rsv) { |
5258 | btrfs_orphan_del(NULL, inode); | |
5259 | goto no_delete; | |
5260 | } | |
4a338542 | 5261 | rsv->size = min_size; |
ca7e70f5 | 5262 | rsv->failfast = 1; |
726c35fa | 5263 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5264 | |
dbe674a9 | 5265 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5266 | |
4289a667 | 5267 | /* |
8407aa46 MX |
5268 | * This is a bit simpler than btrfs_truncate since we've already |
5269 | * reserved our space for our orphan item in the unlink, so we just | |
5270 | * need to reserve some slack space in case we add bytes and update | |
5271 | * inode item when doing the truncate. | |
4289a667 | 5272 | */ |
8082510e | 5273 | while (1) { |
08e007d2 MX |
5274 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5275 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5276 | |
5277 | /* | |
5278 | * Try and steal from the global reserve since we will | |
5279 | * likely not use this space anyway, we want to try as | |
5280 | * hard as possible to get this to work. | |
5281 | */ | |
5282 | if (ret) | |
3bce876f JB |
5283 | steal_from_global++; |
5284 | else | |
5285 | steal_from_global = 0; | |
5286 | ret = 0; | |
d68fc57b | 5287 | |
3bce876f JB |
5288 | /* |
5289 | * steal_from_global == 0: we reserved stuff, hooray! | |
5290 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5291 | * steal_from_global == 2: we've committed, still not a lot of | |
5292 | * room but maybe we'll have room in the global reserve this | |
5293 | * time. | |
5294 | * steal_from_global == 3: abandon all hope! | |
5295 | */ | |
5296 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5297 | btrfs_warn(root->fs_info, |
5298 | "Could not get space for a delete, will truncate on mount %d", | |
5299 | ret); | |
4289a667 JB |
5300 | btrfs_orphan_del(NULL, inode); |
5301 | btrfs_free_block_rsv(root, rsv); | |
5302 | goto no_delete; | |
d68fc57b | 5303 | } |
7b128766 | 5304 | |
0e8c36a9 | 5305 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5306 | if (IS_ERR(trans)) { |
5307 | btrfs_orphan_del(NULL, inode); | |
5308 | btrfs_free_block_rsv(root, rsv); | |
5309 | goto no_delete; | |
d68fc57b | 5310 | } |
7b128766 | 5311 | |
3bce876f | 5312 | /* |
01327610 | 5313 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5314 | * sure there is room to do it, if not we need to commit and try |
5315 | * again. | |
5316 | */ | |
5317 | if (steal_from_global) { | |
5318 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5319 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
25d609f8 | 5320 | min_size, 0); |
3bce876f JB |
5321 | else |
5322 | ret = -ENOSPC; | |
5323 | } | |
5324 | ||
5325 | /* | |
5326 | * Couldn't steal from the global reserve, we have too much | |
5327 | * pending stuff built up, commit the transaction and try it | |
5328 | * again. | |
5329 | */ | |
5330 | if (ret) { | |
5331 | ret = btrfs_commit_transaction(trans, root); | |
5332 | if (ret) { | |
5333 | btrfs_orphan_del(NULL, inode); | |
5334 | btrfs_free_block_rsv(root, rsv); | |
5335 | goto no_delete; | |
5336 | } | |
5337 | continue; | |
5338 | } else { | |
5339 | steal_from_global = 0; | |
5340 | } | |
5341 | ||
4289a667 JB |
5342 | trans->block_rsv = rsv; |
5343 | ||
d68fc57b | 5344 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5345 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5346 | break; |
85e21bac | 5347 | |
8407aa46 | 5348 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5349 | btrfs_end_transaction(trans, root); |
5350 | trans = NULL; | |
b53d3f5d | 5351 | btrfs_btree_balance_dirty(root); |
8082510e | 5352 | } |
5f39d397 | 5353 | |
4289a667 JB |
5354 | btrfs_free_block_rsv(root, rsv); |
5355 | ||
4ef31a45 JB |
5356 | /* |
5357 | * Errors here aren't a big deal, it just means we leave orphan items | |
5358 | * in the tree. They will be cleaned up on the next mount. | |
5359 | */ | |
8082510e | 5360 | if (ret == 0) { |
4289a667 | 5361 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5362 | btrfs_orphan_del(trans, inode); |
5363 | } else { | |
5364 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5365 | } |
54aa1f4d | 5366 | |
4289a667 | 5367 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5368 | if (!(root == root->fs_info->tree_root || |
5369 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5370 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5371 | |
54aa1f4d | 5372 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5373 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5374 | no_delete: |
89042e5a | 5375 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5376 | clear_inode(inode); |
39279cc3 CM |
5377 | } |
5378 | ||
5379 | /* | |
5380 | * this returns the key found in the dir entry in the location pointer. | |
5381 | * If no dir entries were found, location->objectid is 0. | |
5382 | */ | |
5383 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5384 | struct btrfs_key *location) | |
5385 | { | |
5386 | const char *name = dentry->d_name.name; | |
5387 | int namelen = dentry->d_name.len; | |
5388 | struct btrfs_dir_item *di; | |
5389 | struct btrfs_path *path; | |
5390 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5391 | int ret = 0; |
39279cc3 CM |
5392 | |
5393 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5394 | if (!path) |
5395 | return -ENOMEM; | |
3954401f | 5396 | |
33345d01 | 5397 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5398 | namelen, 0); |
0d9f7f3e Y |
5399 | if (IS_ERR(di)) |
5400 | ret = PTR_ERR(di); | |
d397712b | 5401 | |
c704005d | 5402 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5403 | goto out_err; |
d397712b | 5404 | |
5f39d397 | 5405 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5406 | out: |
39279cc3 CM |
5407 | btrfs_free_path(path); |
5408 | return ret; | |
3954401f CM |
5409 | out_err: |
5410 | location->objectid = 0; | |
5411 | goto out; | |
39279cc3 CM |
5412 | } |
5413 | ||
5414 | /* | |
5415 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5416 | * needs to be changed to reflect the root directory of the tree root. This | |
5417 | * is kind of like crossing a mount point. | |
5418 | */ | |
5419 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5420 | struct inode *dir, |
5421 | struct dentry *dentry, | |
5422 | struct btrfs_key *location, | |
5423 | struct btrfs_root **sub_root) | |
39279cc3 | 5424 | { |
4df27c4d YZ |
5425 | struct btrfs_path *path; |
5426 | struct btrfs_root *new_root; | |
5427 | struct btrfs_root_ref *ref; | |
5428 | struct extent_buffer *leaf; | |
1d4c08e0 | 5429 | struct btrfs_key key; |
4df27c4d YZ |
5430 | int ret; |
5431 | int err = 0; | |
39279cc3 | 5432 | |
4df27c4d YZ |
5433 | path = btrfs_alloc_path(); |
5434 | if (!path) { | |
5435 | err = -ENOMEM; | |
5436 | goto out; | |
5437 | } | |
39279cc3 | 5438 | |
4df27c4d | 5439 | err = -ENOENT; |
1d4c08e0 DS |
5440 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5441 | key.type = BTRFS_ROOT_REF_KEY; | |
5442 | key.offset = location->objectid; | |
5443 | ||
5444 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5445 | 0, 0); | |
4df27c4d YZ |
5446 | if (ret) { |
5447 | if (ret < 0) | |
5448 | err = ret; | |
5449 | goto out; | |
5450 | } | |
39279cc3 | 5451 | |
4df27c4d YZ |
5452 | leaf = path->nodes[0]; |
5453 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5454 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5455 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5456 | goto out; | |
39279cc3 | 5457 | |
4df27c4d YZ |
5458 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5459 | (unsigned long)(ref + 1), | |
5460 | dentry->d_name.len); | |
5461 | if (ret) | |
5462 | goto out; | |
5463 | ||
b3b4aa74 | 5464 | btrfs_release_path(path); |
4df27c4d YZ |
5465 | |
5466 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5467 | if (IS_ERR(new_root)) { | |
5468 | err = PTR_ERR(new_root); | |
5469 | goto out; | |
5470 | } | |
5471 | ||
4df27c4d YZ |
5472 | *sub_root = new_root; |
5473 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5474 | location->type = BTRFS_INODE_ITEM_KEY; | |
5475 | location->offset = 0; | |
5476 | err = 0; | |
5477 | out: | |
5478 | btrfs_free_path(path); | |
5479 | return err; | |
39279cc3 CM |
5480 | } |
5481 | ||
5d4f98a2 YZ |
5482 | static void inode_tree_add(struct inode *inode) |
5483 | { | |
5484 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5485 | struct btrfs_inode *entry; | |
03e860bd FNP |
5486 | struct rb_node **p; |
5487 | struct rb_node *parent; | |
cef21937 | 5488 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5489 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5490 | |
1d3382cb | 5491 | if (inode_unhashed(inode)) |
76dda93c | 5492 | return; |
e1409cef | 5493 | parent = NULL; |
5d4f98a2 | 5494 | spin_lock(&root->inode_lock); |
e1409cef | 5495 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5496 | while (*p) { |
5497 | parent = *p; | |
5498 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5499 | ||
33345d01 | 5500 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5501 | p = &parent->rb_left; |
33345d01 | 5502 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5503 | p = &parent->rb_right; |
5d4f98a2 YZ |
5504 | else { |
5505 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5506 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5507 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5508 | RB_CLEAR_NODE(parent); |
5509 | spin_unlock(&root->inode_lock); | |
cef21937 | 5510 | return; |
5d4f98a2 YZ |
5511 | } |
5512 | } | |
cef21937 FDBM |
5513 | rb_link_node(new, parent, p); |
5514 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5515 | spin_unlock(&root->inode_lock); |
5516 | } | |
5517 | ||
5518 | static void inode_tree_del(struct inode *inode) | |
5519 | { | |
5520 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5521 | int empty = 0; |
5d4f98a2 | 5522 | |
03e860bd | 5523 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5524 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5525 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5526 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5527 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5528 | } |
03e860bd | 5529 | spin_unlock(&root->inode_lock); |
76dda93c | 5530 | |
69e9c6c6 | 5531 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5532 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5533 | spin_lock(&root->inode_lock); | |
5534 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5535 | spin_unlock(&root->inode_lock); | |
5536 | if (empty) | |
5537 | btrfs_add_dead_root(root); | |
5538 | } | |
5539 | } | |
5540 | ||
143bede5 | 5541 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5542 | { |
5543 | struct rb_node *node; | |
5544 | struct rb_node *prev; | |
5545 | struct btrfs_inode *entry; | |
5546 | struct inode *inode; | |
5547 | u64 objectid = 0; | |
5548 | ||
7813b3db LB |
5549 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5550 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5551 | |
5552 | spin_lock(&root->inode_lock); | |
5553 | again: | |
5554 | node = root->inode_tree.rb_node; | |
5555 | prev = NULL; | |
5556 | while (node) { | |
5557 | prev = node; | |
5558 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5559 | ||
33345d01 | 5560 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5561 | node = node->rb_left; |
33345d01 | 5562 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5563 | node = node->rb_right; |
5564 | else | |
5565 | break; | |
5566 | } | |
5567 | if (!node) { | |
5568 | while (prev) { | |
5569 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5570 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5571 | node = prev; |
5572 | break; | |
5573 | } | |
5574 | prev = rb_next(prev); | |
5575 | } | |
5576 | } | |
5577 | while (node) { | |
5578 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5579 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5580 | inode = igrab(&entry->vfs_inode); |
5581 | if (inode) { | |
5582 | spin_unlock(&root->inode_lock); | |
5583 | if (atomic_read(&inode->i_count) > 1) | |
5584 | d_prune_aliases(inode); | |
5585 | /* | |
45321ac5 | 5586 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5587 | * the inode cache when its usage count |
5588 | * hits zero. | |
5589 | */ | |
5590 | iput(inode); | |
5591 | cond_resched(); | |
5592 | spin_lock(&root->inode_lock); | |
5593 | goto again; | |
5594 | } | |
5595 | ||
5596 | if (cond_resched_lock(&root->inode_lock)) | |
5597 | goto again; | |
5598 | ||
5599 | node = rb_next(node); | |
5600 | } | |
5601 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5602 | } |
5603 | ||
e02119d5 CM |
5604 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5605 | { | |
5606 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5607 | inode->i_ino = args->location->objectid; |
5608 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5609 | sizeof(*args->location)); | |
e02119d5 | 5610 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5611 | return 0; |
5612 | } | |
5613 | ||
5614 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5615 | { | |
5616 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5617 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5618 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5619 | } |
5620 | ||
5d4f98a2 | 5621 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5622 | struct btrfs_key *location, |
5d4f98a2 | 5623 | struct btrfs_root *root) |
39279cc3 CM |
5624 | { |
5625 | struct inode *inode; | |
5626 | struct btrfs_iget_args args; | |
90d3e592 | 5627 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5628 | |
90d3e592 | 5629 | args.location = location; |
39279cc3 CM |
5630 | args.root = root; |
5631 | ||
778ba82b | 5632 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5633 | btrfs_init_locked_inode, |
5634 | (void *)&args); | |
5635 | return inode; | |
5636 | } | |
5637 | ||
1a54ef8c BR |
5638 | /* Get an inode object given its location and corresponding root. |
5639 | * Returns in *is_new if the inode was read from disk | |
5640 | */ | |
5641 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5642 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5643 | { |
5644 | struct inode *inode; | |
5645 | ||
90d3e592 | 5646 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5647 | if (!inode) |
5d4f98a2 | 5648 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5649 | |
5650 | if (inode->i_state & I_NEW) { | |
67710892 FM |
5651 | int ret; |
5652 | ||
5653 | ret = btrfs_read_locked_inode(inode); | |
1748f843 MF |
5654 | if (!is_bad_inode(inode)) { |
5655 | inode_tree_add(inode); | |
5656 | unlock_new_inode(inode); | |
5657 | if (new) | |
5658 | *new = 1; | |
5659 | } else { | |
e0b6d65b ST |
5660 | unlock_new_inode(inode); |
5661 | iput(inode); | |
67710892 FM |
5662 | ASSERT(ret < 0); |
5663 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); | |
1748f843 MF |
5664 | } |
5665 | } | |
5666 | ||
1a54ef8c BR |
5667 | return inode; |
5668 | } | |
5669 | ||
4df27c4d YZ |
5670 | static struct inode *new_simple_dir(struct super_block *s, |
5671 | struct btrfs_key *key, | |
5672 | struct btrfs_root *root) | |
5673 | { | |
5674 | struct inode *inode = new_inode(s); | |
5675 | ||
5676 | if (!inode) | |
5677 | return ERR_PTR(-ENOMEM); | |
5678 | ||
4df27c4d YZ |
5679 | BTRFS_I(inode)->root = root; |
5680 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5681 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5682 | |
5683 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5684 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5685 | inode->i_fop = &simple_dir_operations; |
5686 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
04b285f3 | 5687 | inode->i_mtime = current_fs_time(inode->i_sb); |
9cc97d64 | 5688 | inode->i_atime = inode->i_mtime; |
5689 | inode->i_ctime = inode->i_mtime; | |
5690 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5691 | |
5692 | return inode; | |
5693 | } | |
5694 | ||
3de4586c | 5695 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5696 | { |
d397712b | 5697 | struct inode *inode; |
4df27c4d | 5698 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5699 | struct btrfs_root *sub_root = root; |
5700 | struct btrfs_key location; | |
76dda93c | 5701 | int index; |
b4aff1f8 | 5702 | int ret = 0; |
39279cc3 CM |
5703 | |
5704 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5705 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5706 | |
39e3c955 | 5707 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5708 | if (ret < 0) |
5709 | return ERR_PTR(ret); | |
5f39d397 | 5710 | |
4df27c4d | 5711 | if (location.objectid == 0) |
5662344b | 5712 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5713 | |
5714 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5715 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5716 | return inode; |
5717 | } | |
5718 | ||
5719 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5720 | ||
76dda93c | 5721 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5722 | ret = fixup_tree_root_location(root, dir, dentry, |
5723 | &location, &sub_root); | |
5724 | if (ret < 0) { | |
5725 | if (ret != -ENOENT) | |
5726 | inode = ERR_PTR(ret); | |
5727 | else | |
5728 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5729 | } else { | |
73f73415 | 5730 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5731 | } |
76dda93c YZ |
5732 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5733 | ||
34d19bad | 5734 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5735 | down_read(&root->fs_info->cleanup_work_sem); |
5736 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5737 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5738 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5739 | if (ret) { |
5740 | iput(inode); | |
66b4ffd1 | 5741 | inode = ERR_PTR(ret); |
01cd3367 | 5742 | } |
c71bf099 YZ |
5743 | } |
5744 | ||
3de4586c CM |
5745 | return inode; |
5746 | } | |
5747 | ||
fe15ce44 | 5748 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5749 | { |
5750 | struct btrfs_root *root; | |
2b0143b5 | 5751 | struct inode *inode = d_inode(dentry); |
76dda93c | 5752 | |
848cce0d | 5753 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5754 | inode = d_inode(dentry->d_parent); |
76dda93c | 5755 | |
848cce0d LZ |
5756 | if (inode) { |
5757 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5758 | if (btrfs_root_refs(&root->root_item) == 0) |
5759 | return 1; | |
848cce0d LZ |
5760 | |
5761 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5762 | return 1; | |
efefb143 | 5763 | } |
76dda93c YZ |
5764 | return 0; |
5765 | } | |
5766 | ||
b4aff1f8 JB |
5767 | static void btrfs_dentry_release(struct dentry *dentry) |
5768 | { | |
944a4515 | 5769 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5770 | } |
5771 | ||
3de4586c | 5772 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5773 | unsigned int flags) |
3de4586c | 5774 | { |
5662344b | 5775 | struct inode *inode; |
a66e7cc6 | 5776 | |
5662344b TI |
5777 | inode = btrfs_lookup_dentry(dir, dentry); |
5778 | if (IS_ERR(inode)) { | |
5779 | if (PTR_ERR(inode) == -ENOENT) | |
5780 | inode = NULL; | |
5781 | else | |
5782 | return ERR_CAST(inode); | |
5783 | } | |
5784 | ||
41d28bca | 5785 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5786 | } |
5787 | ||
16cdcec7 | 5788 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5789 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5790 | }; | |
5791 | ||
9cdda8d3 | 5792 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5793 | { |
9cdda8d3 | 5794 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5795 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5796 | struct btrfs_item *item; | |
5797 | struct btrfs_dir_item *di; | |
5798 | struct btrfs_key key; | |
5f39d397 | 5799 | struct btrfs_key found_key; |
39279cc3 | 5800 | struct btrfs_path *path; |
16cdcec7 MX |
5801 | struct list_head ins_list; |
5802 | struct list_head del_list; | |
39279cc3 | 5803 | int ret; |
5f39d397 | 5804 | struct extent_buffer *leaf; |
39279cc3 | 5805 | int slot; |
39279cc3 CM |
5806 | unsigned char d_type; |
5807 | int over = 0; | |
5808 | u32 di_cur; | |
5809 | u32 di_total; | |
5810 | u32 di_len; | |
5811 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5812 | char tmp_name[32]; |
5813 | char *name_ptr; | |
5814 | int name_len; | |
9cdda8d3 | 5815 | int is_curr = 0; /* ctx->pos points to the current index? */ |
bc4ef759 | 5816 | bool emitted; |
02dbfc99 | 5817 | bool put = false; |
39279cc3 CM |
5818 | |
5819 | /* FIXME, use a real flag for deciding about the key type */ | |
5820 | if (root->fs_info->tree_root == root) | |
5821 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5822 | |
9cdda8d3 AV |
5823 | if (!dir_emit_dots(file, ctx)) |
5824 | return 0; | |
5825 | ||
49593bfa | 5826 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5827 | if (!path) |
5828 | return -ENOMEM; | |
ff5714cc | 5829 | |
e4058b54 | 5830 | path->reada = READA_FORWARD; |
49593bfa | 5831 | |
16cdcec7 MX |
5832 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5833 | INIT_LIST_HEAD(&ins_list); | |
5834 | INIT_LIST_HEAD(&del_list); | |
02dbfc99 OS |
5835 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, |
5836 | &del_list); | |
16cdcec7 MX |
5837 | } |
5838 | ||
962a298f | 5839 | key.type = key_type; |
9cdda8d3 | 5840 | key.offset = ctx->pos; |
33345d01 | 5841 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5842 | |
39279cc3 CM |
5843 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5844 | if (ret < 0) | |
5845 | goto err; | |
49593bfa | 5846 | |
bc4ef759 | 5847 | emitted = false; |
49593bfa | 5848 | while (1) { |
5f39d397 | 5849 | leaf = path->nodes[0]; |
39279cc3 | 5850 | slot = path->slots[0]; |
b9e03af0 LZ |
5851 | if (slot >= btrfs_header_nritems(leaf)) { |
5852 | ret = btrfs_next_leaf(root, path); | |
5853 | if (ret < 0) | |
5854 | goto err; | |
5855 | else if (ret > 0) | |
5856 | break; | |
5857 | continue; | |
39279cc3 | 5858 | } |
3de4586c | 5859 | |
dd3cc16b | 5860 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5861 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5862 | ||
5863 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5864 | break; |
962a298f | 5865 | if (found_key.type != key_type) |
39279cc3 | 5866 | break; |
9cdda8d3 | 5867 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5868 | goto next; |
16cdcec7 MX |
5869 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5870 | btrfs_should_delete_dir_index(&del_list, | |
5871 | found_key.offset)) | |
5872 | goto next; | |
5f39d397 | 5873 | |
9cdda8d3 | 5874 | ctx->pos = found_key.offset; |
16cdcec7 | 5875 | is_curr = 1; |
49593bfa | 5876 | |
39279cc3 CM |
5877 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5878 | di_cur = 0; | |
5f39d397 | 5879 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5880 | |
5881 | while (di_cur < di_total) { | |
5f39d397 CM |
5882 | struct btrfs_key location; |
5883 | ||
22a94d44 JB |
5884 | if (verify_dir_item(root, leaf, di)) |
5885 | break; | |
5886 | ||
5f39d397 | 5887 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5888 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5889 | name_ptr = tmp_name; |
5890 | } else { | |
49e350a4 | 5891 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
49593bfa DW |
5892 | if (!name_ptr) { |
5893 | ret = -ENOMEM; | |
5894 | goto err; | |
5895 | } | |
5f39d397 CM |
5896 | } |
5897 | read_extent_buffer(leaf, name_ptr, | |
5898 | (unsigned long)(di + 1), name_len); | |
5899 | ||
5900 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5901 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5902 | |
fede766f | 5903 | |
3de4586c | 5904 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5905 | * skip it. |
5906 | * | |
5907 | * In contrast to old kernels, we insert the snapshot's | |
5908 | * dir item and dir index after it has been created, so | |
5909 | * we won't find a reference to our own snapshot. We | |
5910 | * still keep the following code for backward | |
5911 | * compatibility. | |
3de4586c CM |
5912 | */ |
5913 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5914 | location.objectid == root->root_key.objectid) { | |
5915 | over = 0; | |
5916 | goto skip; | |
5917 | } | |
9cdda8d3 AV |
5918 | over = !dir_emit(ctx, name_ptr, name_len, |
5919 | location.objectid, d_type); | |
5f39d397 | 5920 | |
3de4586c | 5921 | skip: |
5f39d397 CM |
5922 | if (name_ptr != tmp_name) |
5923 | kfree(name_ptr); | |
5924 | ||
39279cc3 CM |
5925 | if (over) |
5926 | goto nopos; | |
bc4ef759 | 5927 | emitted = true; |
5103e947 | 5928 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5929 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5930 | di_cur += di_len; |
5931 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5932 | } | |
b9e03af0 LZ |
5933 | next: |
5934 | path->slots[0]++; | |
39279cc3 | 5935 | } |
49593bfa | 5936 | |
16cdcec7 MX |
5937 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5938 | if (is_curr) | |
9cdda8d3 | 5939 | ctx->pos++; |
bc4ef759 | 5940 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted); |
16cdcec7 MX |
5941 | if (ret) |
5942 | goto nopos; | |
5943 | } | |
5944 | ||
bc4ef759 DS |
5945 | /* |
5946 | * If we haven't emitted any dir entry, we must not touch ctx->pos as | |
5947 | * it was was set to the termination value in previous call. We assume | |
5948 | * that "." and ".." were emitted if we reach this point and set the | |
5949 | * termination value as well for an empty directory. | |
5950 | */ | |
5951 | if (ctx->pos > 2 && !emitted) | |
5952 | goto nopos; | |
5953 | ||
49593bfa | 5954 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5955 | ctx->pos++; |
5956 | ||
5957 | /* | |
5958 | * Stop new entries from being returned after we return the last | |
5959 | * entry. | |
5960 | * | |
5961 | * New directory entries are assigned a strictly increasing | |
5962 | * offset. This means that new entries created during readdir | |
5963 | * are *guaranteed* to be seen in the future by that readdir. | |
5964 | * This has broken buggy programs which operate on names as | |
5965 | * they're returned by readdir. Until we re-use freed offsets | |
5966 | * we have this hack to stop new entries from being returned | |
5967 | * under the assumption that they'll never reach this huge | |
5968 | * offset. | |
5969 | * | |
5970 | * This is being careful not to overflow 32bit loff_t unless the | |
5971 | * last entry requires it because doing so has broken 32bit apps | |
5972 | * in the past. | |
5973 | */ | |
5974 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5975 | if (ctx->pos >= INT_MAX) | |
5976 | ctx->pos = LLONG_MAX; | |
5977 | else | |
5978 | ctx->pos = INT_MAX; | |
5979 | } | |
39279cc3 CM |
5980 | nopos: |
5981 | ret = 0; | |
5982 | err: | |
02dbfc99 OS |
5983 | if (put) |
5984 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5985 | btrfs_free_path(path); |
39279cc3 CM |
5986 | return ret; |
5987 | } | |
5988 | ||
a9185b41 | 5989 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5990 | { |
5991 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5992 | struct btrfs_trans_handle *trans; | |
5993 | int ret = 0; | |
0af3d00b | 5994 | bool nolock = false; |
39279cc3 | 5995 | |
72ac3c0d | 5996 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5997 | return 0; |
5998 | ||
83eea1f1 | 5999 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 6000 | nolock = true; |
0af3d00b | 6001 | |
a9185b41 | 6002 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 6003 | if (nolock) |
7a7eaa40 | 6004 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 6005 | else |
7a7eaa40 | 6006 | trans = btrfs_join_transaction(root); |
3612b495 TI |
6007 | if (IS_ERR(trans)) |
6008 | return PTR_ERR(trans); | |
a698d075 | 6009 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
6010 | } |
6011 | return ret; | |
6012 | } | |
6013 | ||
6014 | /* | |
54aa1f4d | 6015 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
6016 | * inode changes. But, it is most likely to find the inode in cache. |
6017 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
6018 | * to keep or drop this code. | |
6019 | */ | |
48a3b636 | 6020 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
6021 | { |
6022 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6023 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
6024 | int ret; |
6025 | ||
72ac3c0d | 6026 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 6027 | return 0; |
39279cc3 | 6028 | |
7a7eaa40 | 6029 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
6030 | if (IS_ERR(trans)) |
6031 | return PTR_ERR(trans); | |
8929ecfa YZ |
6032 | |
6033 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
6034 | if (ret && ret == -ENOSPC) { |
6035 | /* whoops, lets try again with the full transaction */ | |
6036 | btrfs_end_transaction(trans, root); | |
6037 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
6038 | if (IS_ERR(trans)) |
6039 | return PTR_ERR(trans); | |
8929ecfa | 6040 | |
94b60442 | 6041 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6042 | } |
39279cc3 | 6043 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
6044 | if (BTRFS_I(inode)->delayed_node) |
6045 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
6046 | |
6047 | return ret; | |
6048 | } | |
6049 | ||
6050 | /* | |
6051 | * This is a copy of file_update_time. We need this so we can return error on | |
6052 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6053 | */ | |
e41f941a JB |
6054 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6055 | int flags) | |
22c44fe6 | 6056 | { |
2bc55652 AB |
6057 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6058 | ||
6059 | if (btrfs_root_readonly(root)) | |
6060 | return -EROFS; | |
6061 | ||
e41f941a | 6062 | if (flags & S_VERSION) |
22c44fe6 | 6063 | inode_inc_iversion(inode); |
e41f941a JB |
6064 | if (flags & S_CTIME) |
6065 | inode->i_ctime = *now; | |
6066 | if (flags & S_MTIME) | |
6067 | inode->i_mtime = *now; | |
6068 | if (flags & S_ATIME) | |
6069 | inode->i_atime = *now; | |
6070 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6071 | } |
6072 | ||
d352ac68 CM |
6073 | /* |
6074 | * find the highest existing sequence number in a directory | |
6075 | * and then set the in-memory index_cnt variable to reflect | |
6076 | * free sequence numbers | |
6077 | */ | |
aec7477b JB |
6078 | static int btrfs_set_inode_index_count(struct inode *inode) |
6079 | { | |
6080 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6081 | struct btrfs_key key, found_key; | |
6082 | struct btrfs_path *path; | |
6083 | struct extent_buffer *leaf; | |
6084 | int ret; | |
6085 | ||
33345d01 | 6086 | key.objectid = btrfs_ino(inode); |
962a298f | 6087 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6088 | key.offset = (u64)-1; |
6089 | ||
6090 | path = btrfs_alloc_path(); | |
6091 | if (!path) | |
6092 | return -ENOMEM; | |
6093 | ||
6094 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6095 | if (ret < 0) | |
6096 | goto out; | |
6097 | /* FIXME: we should be able to handle this */ | |
6098 | if (ret == 0) | |
6099 | goto out; | |
6100 | ret = 0; | |
6101 | ||
6102 | /* | |
6103 | * MAGIC NUMBER EXPLANATION: | |
6104 | * since we search a directory based on f_pos we have to start at 2 | |
6105 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6106 | * else has to start at 2 | |
6107 | */ | |
6108 | if (path->slots[0] == 0) { | |
6109 | BTRFS_I(inode)->index_cnt = 2; | |
6110 | goto out; | |
6111 | } | |
6112 | ||
6113 | path->slots[0]--; | |
6114 | ||
6115 | leaf = path->nodes[0]; | |
6116 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6117 | ||
33345d01 | 6118 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6119 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
6120 | BTRFS_I(inode)->index_cnt = 2; |
6121 | goto out; | |
6122 | } | |
6123 | ||
6124 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
6125 | out: | |
6126 | btrfs_free_path(path); | |
6127 | return ret; | |
6128 | } | |
6129 | ||
d352ac68 CM |
6130 | /* |
6131 | * helper to find a free sequence number in a given directory. This current | |
6132 | * code is very simple, later versions will do smarter things in the btree | |
6133 | */ | |
3de4586c | 6134 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
6135 | { |
6136 | int ret = 0; | |
6137 | ||
6138 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
6139 | ret = btrfs_inode_delayed_dir_index_count(dir); |
6140 | if (ret) { | |
6141 | ret = btrfs_set_inode_index_count(dir); | |
6142 | if (ret) | |
6143 | return ret; | |
6144 | } | |
aec7477b JB |
6145 | } |
6146 | ||
00e4e6b3 | 6147 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
6148 | BTRFS_I(dir)->index_cnt++; |
6149 | ||
6150 | return ret; | |
6151 | } | |
6152 | ||
b0d5d10f CM |
6153 | static int btrfs_insert_inode_locked(struct inode *inode) |
6154 | { | |
6155 | struct btrfs_iget_args args; | |
6156 | args.location = &BTRFS_I(inode)->location; | |
6157 | args.root = BTRFS_I(inode)->root; | |
6158 | ||
6159 | return insert_inode_locked4(inode, | |
6160 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6161 | btrfs_find_actor, &args); | |
6162 | } | |
6163 | ||
39279cc3 CM |
6164 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6165 | struct btrfs_root *root, | |
aec7477b | 6166 | struct inode *dir, |
9c58309d | 6167 | const char *name, int name_len, |
175a4eb7 AV |
6168 | u64 ref_objectid, u64 objectid, |
6169 | umode_t mode, u64 *index) | |
39279cc3 CM |
6170 | { |
6171 | struct inode *inode; | |
5f39d397 | 6172 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6173 | struct btrfs_key *location; |
5f39d397 | 6174 | struct btrfs_path *path; |
9c58309d CM |
6175 | struct btrfs_inode_ref *ref; |
6176 | struct btrfs_key key[2]; | |
6177 | u32 sizes[2]; | |
ef3b9af5 | 6178 | int nitems = name ? 2 : 1; |
9c58309d | 6179 | unsigned long ptr; |
39279cc3 | 6180 | int ret; |
39279cc3 | 6181 | |
5f39d397 | 6182 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6183 | if (!path) |
6184 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6185 | |
39279cc3 | 6186 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
6187 | if (!inode) { |
6188 | btrfs_free_path(path); | |
39279cc3 | 6189 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6190 | } |
39279cc3 | 6191 | |
5762b5c9 FM |
6192 | /* |
6193 | * O_TMPFILE, set link count to 0, so that after this point, | |
6194 | * we fill in an inode item with the correct link count. | |
6195 | */ | |
6196 | if (!name) | |
6197 | set_nlink(inode, 0); | |
6198 | ||
581bb050 LZ |
6199 | /* |
6200 | * we have to initialize this early, so we can reclaim the inode | |
6201 | * number if we fail afterwards in this function. | |
6202 | */ | |
6203 | inode->i_ino = objectid; | |
6204 | ||
ef3b9af5 | 6205 | if (dir && name) { |
1abe9b8a | 6206 | trace_btrfs_inode_request(dir); |
6207 | ||
3de4586c | 6208 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6209 | if (ret) { |
8fb27640 | 6210 | btrfs_free_path(path); |
09771430 | 6211 | iput(inode); |
aec7477b | 6212 | return ERR_PTR(ret); |
09771430 | 6213 | } |
ef3b9af5 FM |
6214 | } else if (dir) { |
6215 | *index = 0; | |
aec7477b JB |
6216 | } |
6217 | /* | |
6218 | * index_cnt is ignored for everything but a dir, | |
6219 | * btrfs_get_inode_index_count has an explanation for the magic | |
6220 | * number | |
6221 | */ | |
6222 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6223 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6224 | BTRFS_I(inode)->root = root; |
e02119d5 | 6225 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6226 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6227 | |
5dc562c5 JB |
6228 | /* |
6229 | * We could have gotten an inode number from somebody who was fsynced | |
6230 | * and then removed in this same transaction, so let's just set full | |
6231 | * sync since it will be a full sync anyway and this will blow away the | |
6232 | * old info in the log. | |
6233 | */ | |
6234 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6235 | ||
9c58309d | 6236 | key[0].objectid = objectid; |
962a298f | 6237 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6238 | key[0].offset = 0; |
6239 | ||
9c58309d | 6240 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6241 | |
6242 | if (name) { | |
6243 | /* | |
6244 | * Start new inodes with an inode_ref. This is slightly more | |
6245 | * efficient for small numbers of hard links since they will | |
6246 | * be packed into one item. Extended refs will kick in if we | |
6247 | * add more hard links than can fit in the ref item. | |
6248 | */ | |
6249 | key[1].objectid = objectid; | |
962a298f | 6250 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6251 | key[1].offset = ref_objectid; |
6252 | ||
6253 | sizes[1] = name_len + sizeof(*ref); | |
6254 | } | |
9c58309d | 6255 | |
b0d5d10f CM |
6256 | location = &BTRFS_I(inode)->location; |
6257 | location->objectid = objectid; | |
6258 | location->offset = 0; | |
962a298f | 6259 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6260 | |
6261 | ret = btrfs_insert_inode_locked(inode); | |
6262 | if (ret < 0) | |
6263 | goto fail; | |
6264 | ||
b9473439 | 6265 | path->leave_spinning = 1; |
ef3b9af5 | 6266 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6267 | if (ret != 0) |
b0d5d10f | 6268 | goto fail_unlock; |
5f39d397 | 6269 | |
ecc11fab | 6270 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6271 | inode_set_bytes(inode, 0); |
9cc97d64 | 6272 | |
04b285f3 | 6273 | inode->i_mtime = current_fs_time(inode->i_sb); |
9cc97d64 | 6274 | inode->i_atime = inode->i_mtime; |
6275 | inode->i_ctime = inode->i_mtime; | |
6276 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6277 | ||
5f39d397 CM |
6278 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6279 | struct btrfs_inode_item); | |
293f7e07 LZ |
6280 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6281 | sizeof(*inode_item)); | |
e02119d5 | 6282 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6283 | |
ef3b9af5 FM |
6284 | if (name) { |
6285 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6286 | struct btrfs_inode_ref); | |
6287 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6288 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6289 | ptr = (unsigned long)(ref + 1); | |
6290 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6291 | } | |
9c58309d | 6292 | |
5f39d397 CM |
6293 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6294 | btrfs_free_path(path); | |
6295 | ||
6cbff00f CH |
6296 | btrfs_inherit_iflags(inode, dir); |
6297 | ||
569254b0 | 6298 | if (S_ISREG(mode)) { |
3cdde224 | 6299 | if (btrfs_test_opt(root->fs_info, NODATASUM)) |
94272164 | 6300 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
3cdde224 | 6301 | if (btrfs_test_opt(root->fs_info, NODATACOW)) |
f2bdf9a8 JB |
6302 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6303 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6304 | } |
6305 | ||
5d4f98a2 | 6306 | inode_tree_add(inode); |
1abe9b8a | 6307 | |
6308 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6309 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6310 | |
8ea05e3a AB |
6311 | btrfs_update_root_times(trans, root); |
6312 | ||
63541927 FDBM |
6313 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6314 | if (ret) | |
6315 | btrfs_err(root->fs_info, | |
6316 | "error inheriting props for ino %llu (root %llu): %d", | |
6317 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6318 | ||
39279cc3 | 6319 | return inode; |
b0d5d10f CM |
6320 | |
6321 | fail_unlock: | |
6322 | unlock_new_inode(inode); | |
5f39d397 | 6323 | fail: |
ef3b9af5 | 6324 | if (dir && name) |
aec7477b | 6325 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6326 | btrfs_free_path(path); |
09771430 | 6327 | iput(inode); |
5f39d397 | 6328 | return ERR_PTR(ret); |
39279cc3 CM |
6329 | } |
6330 | ||
6331 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6332 | { | |
6333 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6334 | } | |
6335 | ||
d352ac68 CM |
6336 | /* |
6337 | * utility function to add 'inode' into 'parent_inode' with | |
6338 | * a give name and a given sequence number. | |
6339 | * if 'add_backref' is true, also insert a backref from the | |
6340 | * inode to the parent directory. | |
6341 | */ | |
e02119d5 CM |
6342 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6343 | struct inode *parent_inode, struct inode *inode, | |
6344 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6345 | { |
4df27c4d | 6346 | int ret = 0; |
39279cc3 | 6347 | struct btrfs_key key; |
e02119d5 | 6348 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6349 | u64 ino = btrfs_ino(inode); |
6350 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6351 | |
33345d01 | 6352 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6353 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6354 | } else { | |
33345d01 | 6355 | key.objectid = ino; |
962a298f | 6356 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6357 | key.offset = 0; |
6358 | } | |
6359 | ||
33345d01 | 6360 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6361 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6362 | key.objectid, root->root_key.objectid, | |
33345d01 | 6363 | parent_ino, index, name, name_len); |
4df27c4d | 6364 | } else if (add_backref) { |
33345d01 LZ |
6365 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6366 | parent_ino, index); | |
4df27c4d | 6367 | } |
39279cc3 | 6368 | |
79787eaa JM |
6369 | /* Nothing to clean up yet */ |
6370 | if (ret) | |
6371 | return ret; | |
4df27c4d | 6372 | |
79787eaa JM |
6373 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6374 | parent_inode, &key, | |
6375 | btrfs_inode_type(inode), index); | |
9c52057c | 6376 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6377 | goto fail_dir_item; |
6378 | else if (ret) { | |
66642832 | 6379 | btrfs_abort_transaction(trans, ret); |
79787eaa | 6380 | return ret; |
39279cc3 | 6381 | } |
79787eaa JM |
6382 | |
6383 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6384 | name_len * 2); | |
0c4d2d95 | 6385 | inode_inc_iversion(parent_inode); |
04b285f3 DD |
6386 | parent_inode->i_mtime = parent_inode->i_ctime = |
6387 | current_fs_time(parent_inode->i_sb); | |
79787eaa JM |
6388 | ret = btrfs_update_inode(trans, root, parent_inode); |
6389 | if (ret) | |
66642832 | 6390 | btrfs_abort_transaction(trans, ret); |
39279cc3 | 6391 | return ret; |
fe66a05a CM |
6392 | |
6393 | fail_dir_item: | |
6394 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6395 | u64 local_index; | |
6396 | int err; | |
6397 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6398 | key.objectid, root->root_key.objectid, | |
6399 | parent_ino, &local_index, name, name_len); | |
6400 | ||
6401 | } else if (add_backref) { | |
6402 | u64 local_index; | |
6403 | int err; | |
6404 | ||
6405 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6406 | ino, parent_ino, &local_index); | |
6407 | } | |
6408 | return ret; | |
39279cc3 CM |
6409 | } |
6410 | ||
6411 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6412 | struct inode *dir, struct dentry *dentry, |
6413 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6414 | { |
a1b075d2 JB |
6415 | int err = btrfs_add_link(trans, dir, inode, |
6416 | dentry->d_name.name, dentry->d_name.len, | |
6417 | backref, index); | |
39279cc3 CM |
6418 | if (err > 0) |
6419 | err = -EEXIST; | |
6420 | return err; | |
6421 | } | |
6422 | ||
618e21d5 | 6423 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6424 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6425 | { |
6426 | struct btrfs_trans_handle *trans; | |
6427 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6428 | struct inode *inode = NULL; |
618e21d5 JB |
6429 | int err; |
6430 | int drop_inode = 0; | |
6431 | u64 objectid; | |
00e4e6b3 | 6432 | u64 index = 0; |
618e21d5 | 6433 | |
9ed74f2d JB |
6434 | /* |
6435 | * 2 for inode item and ref | |
6436 | * 2 for dir items | |
6437 | * 1 for xattr if selinux is on | |
6438 | */ | |
a22285a6 YZ |
6439 | trans = btrfs_start_transaction(root, 5); |
6440 | if (IS_ERR(trans)) | |
6441 | return PTR_ERR(trans); | |
1832a6d5 | 6442 | |
581bb050 LZ |
6443 | err = btrfs_find_free_ino(root, &objectid); |
6444 | if (err) | |
6445 | goto out_unlock; | |
6446 | ||
aec7477b | 6447 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6448 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6449 | mode, &index); |
7cf96da3 TI |
6450 | if (IS_ERR(inode)) { |
6451 | err = PTR_ERR(inode); | |
618e21d5 | 6452 | goto out_unlock; |
7cf96da3 | 6453 | } |
618e21d5 | 6454 | |
ad19db71 CS |
6455 | /* |
6456 | * If the active LSM wants to access the inode during | |
6457 | * d_instantiate it needs these. Smack checks to see | |
6458 | * if the filesystem supports xattrs by looking at the | |
6459 | * ops vector. | |
6460 | */ | |
ad19db71 | 6461 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6462 | init_special_inode(inode, inode->i_mode, rdev); |
6463 | ||
6464 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6465 | if (err) |
b0d5d10f CM |
6466 | goto out_unlock_inode; |
6467 | ||
6468 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6469 | if (err) { | |
6470 | goto out_unlock_inode; | |
6471 | } else { | |
1b4ab1bb | 6472 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6473 | unlock_new_inode(inode); |
08c422c2 | 6474 | d_instantiate(dentry, inode); |
618e21d5 | 6475 | } |
b0d5d10f | 6476 | |
618e21d5 | 6477 | out_unlock: |
7ad85bb7 | 6478 | btrfs_end_transaction(trans, root); |
c581afc8 | 6479 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6480 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6481 | if (drop_inode) { |
6482 | inode_dec_link_count(inode); | |
6483 | iput(inode); | |
6484 | } | |
618e21d5 | 6485 | return err; |
b0d5d10f CM |
6486 | |
6487 | out_unlock_inode: | |
6488 | drop_inode = 1; | |
6489 | unlock_new_inode(inode); | |
6490 | goto out_unlock; | |
6491 | ||
618e21d5 JB |
6492 | } |
6493 | ||
39279cc3 | 6494 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6495 | umode_t mode, bool excl) |
39279cc3 CM |
6496 | { |
6497 | struct btrfs_trans_handle *trans; | |
6498 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6499 | struct inode *inode = NULL; |
43baa579 | 6500 | int drop_inode_on_err = 0; |
a22285a6 | 6501 | int err; |
39279cc3 | 6502 | u64 objectid; |
00e4e6b3 | 6503 | u64 index = 0; |
39279cc3 | 6504 | |
9ed74f2d JB |
6505 | /* |
6506 | * 2 for inode item and ref | |
6507 | * 2 for dir items | |
6508 | * 1 for xattr if selinux is on | |
6509 | */ | |
a22285a6 YZ |
6510 | trans = btrfs_start_transaction(root, 5); |
6511 | if (IS_ERR(trans)) | |
6512 | return PTR_ERR(trans); | |
9ed74f2d | 6513 | |
581bb050 LZ |
6514 | err = btrfs_find_free_ino(root, &objectid); |
6515 | if (err) | |
6516 | goto out_unlock; | |
6517 | ||
aec7477b | 6518 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6519 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6520 | mode, &index); |
7cf96da3 TI |
6521 | if (IS_ERR(inode)) { |
6522 | err = PTR_ERR(inode); | |
39279cc3 | 6523 | goto out_unlock; |
7cf96da3 | 6524 | } |
43baa579 | 6525 | drop_inode_on_err = 1; |
ad19db71 CS |
6526 | /* |
6527 | * If the active LSM wants to access the inode during | |
6528 | * d_instantiate it needs these. Smack checks to see | |
6529 | * if the filesystem supports xattrs by looking at the | |
6530 | * ops vector. | |
6531 | */ | |
6532 | inode->i_fop = &btrfs_file_operations; | |
6533 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6534 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6535 | |
6536 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6537 | if (err) | |
6538 | goto out_unlock_inode; | |
6539 | ||
6540 | err = btrfs_update_inode(trans, root, inode); | |
6541 | if (err) | |
6542 | goto out_unlock_inode; | |
ad19db71 | 6543 | |
a1b075d2 | 6544 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6545 | if (err) |
b0d5d10f | 6546 | goto out_unlock_inode; |
43baa579 | 6547 | |
43baa579 | 6548 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6549 | unlock_new_inode(inode); |
43baa579 FB |
6550 | d_instantiate(dentry, inode); |
6551 | ||
39279cc3 | 6552 | out_unlock: |
7ad85bb7 | 6553 | btrfs_end_transaction(trans, root); |
43baa579 | 6554 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6555 | inode_dec_link_count(inode); |
6556 | iput(inode); | |
6557 | } | |
c581afc8 | 6558 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6559 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6560 | return err; |
b0d5d10f CM |
6561 | |
6562 | out_unlock_inode: | |
6563 | unlock_new_inode(inode); | |
6564 | goto out_unlock; | |
6565 | ||
39279cc3 CM |
6566 | } |
6567 | ||
6568 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6569 | struct dentry *dentry) | |
6570 | { | |
271dba45 | 6571 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6572 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6573 | struct inode *inode = d_inode(old_dentry); |
00e4e6b3 | 6574 | u64 index; |
39279cc3 CM |
6575 | int err; |
6576 | int drop_inode = 0; | |
6577 | ||
4a8be425 TH |
6578 | /* do not allow sys_link's with other subvols of the same device */ |
6579 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6580 | return -EXDEV; |
4a8be425 | 6581 | |
f186373f | 6582 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6583 | return -EMLINK; |
4a8be425 | 6584 | |
3de4586c | 6585 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6586 | if (err) |
6587 | goto fail; | |
6588 | ||
a22285a6 | 6589 | /* |
7e6b6465 | 6590 | * 2 items for inode and inode ref |
a22285a6 | 6591 | * 2 items for dir items |
7e6b6465 | 6592 | * 1 item for parent inode |
a22285a6 | 6593 | */ |
7e6b6465 | 6594 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6595 | if (IS_ERR(trans)) { |
6596 | err = PTR_ERR(trans); | |
271dba45 | 6597 | trans = NULL; |
a22285a6 YZ |
6598 | goto fail; |
6599 | } | |
5f39d397 | 6600 | |
67de1176 MX |
6601 | /* There are several dir indexes for this inode, clear the cache. */ |
6602 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6603 | inc_nlink(inode); |
0c4d2d95 | 6604 | inode_inc_iversion(inode); |
04b285f3 | 6605 | inode->i_ctime = current_fs_time(inode->i_sb); |
7de9c6ee | 6606 | ihold(inode); |
e9976151 | 6607 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6608 | |
a1b075d2 | 6609 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6610 | |
a5719521 | 6611 | if (err) { |
54aa1f4d | 6612 | drop_inode = 1; |
a5719521 | 6613 | } else { |
10d9f309 | 6614 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6615 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6616 | if (err) |
6617 | goto fail; | |
ef3b9af5 FM |
6618 | if (inode->i_nlink == 1) { |
6619 | /* | |
6620 | * If new hard link count is 1, it's a file created | |
6621 | * with open(2) O_TMPFILE flag. | |
6622 | */ | |
6623 | err = btrfs_orphan_del(trans, inode); | |
6624 | if (err) | |
6625 | goto fail; | |
6626 | } | |
08c422c2 | 6627 | d_instantiate(dentry, inode); |
6a912213 | 6628 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6629 | } |
39279cc3 | 6630 | |
c581afc8 | 6631 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6632 | fail: |
271dba45 FM |
6633 | if (trans) |
6634 | btrfs_end_transaction(trans, root); | |
39279cc3 CM |
6635 | if (drop_inode) { |
6636 | inode_dec_link_count(inode); | |
6637 | iput(inode); | |
6638 | } | |
b53d3f5d | 6639 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6640 | return err; |
6641 | } | |
6642 | ||
18bb1db3 | 6643 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6644 | { |
b9d86667 | 6645 | struct inode *inode = NULL; |
39279cc3 CM |
6646 | struct btrfs_trans_handle *trans; |
6647 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6648 | int err = 0; | |
6649 | int drop_on_err = 0; | |
b9d86667 | 6650 | u64 objectid = 0; |
00e4e6b3 | 6651 | u64 index = 0; |
39279cc3 | 6652 | |
9ed74f2d JB |
6653 | /* |
6654 | * 2 items for inode and ref | |
6655 | * 2 items for dir items | |
6656 | * 1 for xattr if selinux is on | |
6657 | */ | |
a22285a6 YZ |
6658 | trans = btrfs_start_transaction(root, 5); |
6659 | if (IS_ERR(trans)) | |
6660 | return PTR_ERR(trans); | |
39279cc3 | 6661 | |
581bb050 LZ |
6662 | err = btrfs_find_free_ino(root, &objectid); |
6663 | if (err) | |
6664 | goto out_fail; | |
6665 | ||
aec7477b | 6666 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6667 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6668 | S_IFDIR | mode, &index); |
39279cc3 CM |
6669 | if (IS_ERR(inode)) { |
6670 | err = PTR_ERR(inode); | |
6671 | goto out_fail; | |
6672 | } | |
5f39d397 | 6673 | |
39279cc3 | 6674 | drop_on_err = 1; |
b0d5d10f CM |
6675 | /* these must be set before we unlock the inode */ |
6676 | inode->i_op = &btrfs_dir_inode_operations; | |
6677 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6678 | |
2a7dba39 | 6679 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6680 | if (err) |
b0d5d10f | 6681 | goto out_fail_inode; |
39279cc3 | 6682 | |
dbe674a9 | 6683 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6684 | err = btrfs_update_inode(trans, root, inode); |
6685 | if (err) | |
b0d5d10f | 6686 | goto out_fail_inode; |
5f39d397 | 6687 | |
a1b075d2 JB |
6688 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6689 | dentry->d_name.len, 0, index); | |
39279cc3 | 6690 | if (err) |
b0d5d10f | 6691 | goto out_fail_inode; |
5f39d397 | 6692 | |
39279cc3 | 6693 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6694 | /* |
6695 | * mkdir is special. We're unlocking after we call d_instantiate | |
6696 | * to avoid a race with nfsd calling d_instantiate. | |
6697 | */ | |
6698 | unlock_new_inode(inode); | |
39279cc3 | 6699 | drop_on_err = 0; |
39279cc3 CM |
6700 | |
6701 | out_fail: | |
7ad85bb7 | 6702 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6703 | if (drop_on_err) { |
6704 | inode_dec_link_count(inode); | |
39279cc3 | 6705 | iput(inode); |
c7cfb8a5 | 6706 | } |
c581afc8 | 6707 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6708 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6709 | return err; |
b0d5d10f CM |
6710 | |
6711 | out_fail_inode: | |
6712 | unlock_new_inode(inode); | |
6713 | goto out_fail; | |
39279cc3 CM |
6714 | } |
6715 | ||
e6c4efd8 QW |
6716 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6717 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6718 | { | |
6719 | struct rb_node *next; | |
6720 | ||
6721 | next = rb_next(&em->rb_node); | |
6722 | if (!next) | |
6723 | return NULL; | |
6724 | return container_of(next, struct extent_map, rb_node); | |
6725 | } | |
6726 | ||
6727 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6728 | { | |
6729 | struct rb_node *prev; | |
6730 | ||
6731 | prev = rb_prev(&em->rb_node); | |
6732 | if (!prev) | |
6733 | return NULL; | |
6734 | return container_of(prev, struct extent_map, rb_node); | |
6735 | } | |
6736 | ||
d352ac68 | 6737 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6738 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6739 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6740 | * the best fitted new extent into the tree. |
d352ac68 | 6741 | */ |
3b951516 CM |
6742 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6743 | struct extent_map *existing, | |
e6dcd2dc | 6744 | struct extent_map *em, |
51f395ad | 6745 | u64 map_start) |
3b951516 | 6746 | { |
e6c4efd8 QW |
6747 | struct extent_map *prev; |
6748 | struct extent_map *next; | |
6749 | u64 start; | |
6750 | u64 end; | |
3b951516 | 6751 | u64 start_diff; |
3b951516 | 6752 | |
e6dcd2dc | 6753 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6754 | |
6755 | if (existing->start > map_start) { | |
6756 | next = existing; | |
6757 | prev = prev_extent_map(next); | |
6758 | } else { | |
6759 | prev = existing; | |
6760 | next = next_extent_map(prev); | |
6761 | } | |
6762 | ||
6763 | start = prev ? extent_map_end(prev) : em->start; | |
6764 | start = max_t(u64, start, em->start); | |
6765 | end = next ? next->start : extent_map_end(em); | |
6766 | end = min_t(u64, end, extent_map_end(em)); | |
6767 | start_diff = start - em->start; | |
6768 | em->start = start; | |
6769 | em->len = end - start; | |
c8b97818 CM |
6770 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6771 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6772 | em->block_start += start_diff; |
c8b97818 CM |
6773 | em->block_len -= start_diff; |
6774 | } | |
09a2a8f9 | 6775 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6776 | } |
6777 | ||
c8b97818 | 6778 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6779 | struct page *page, |
c8b97818 CM |
6780 | size_t pg_offset, u64 extent_offset, |
6781 | struct btrfs_file_extent_item *item) | |
6782 | { | |
6783 | int ret; | |
6784 | struct extent_buffer *leaf = path->nodes[0]; | |
6785 | char *tmp; | |
6786 | size_t max_size; | |
6787 | unsigned long inline_size; | |
6788 | unsigned long ptr; | |
261507a0 | 6789 | int compress_type; |
c8b97818 CM |
6790 | |
6791 | WARN_ON(pg_offset != 0); | |
261507a0 | 6792 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6793 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6794 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6795 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6796 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6797 | if (!tmp) |
6798 | return -ENOMEM; | |
c8b97818 CM |
6799 | ptr = btrfs_file_extent_inline_start(item); |
6800 | ||
6801 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6802 | ||
09cbfeaf | 6803 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6804 | ret = btrfs_decompress(compress_type, tmp, page, |
6805 | extent_offset, inline_size, max_size); | |
c8b97818 | 6806 | kfree(tmp); |
166ae5a4 | 6807 | return ret; |
c8b97818 CM |
6808 | } |
6809 | ||
d352ac68 CM |
6810 | /* |
6811 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6812 | * the ugly parts come from merging extents from the disk with the in-ram |
6813 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6814 | * where the in-ram extents might be locked pending data=ordered completion. |
6815 | * | |
6816 | * This also copies inline extents directly into the page. | |
6817 | */ | |
d397712b | 6818 | |
a52d9a80 | 6819 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6820 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6821 | int create) |
6822 | { | |
6823 | int ret; | |
6824 | int err = 0; | |
a52d9a80 CM |
6825 | u64 extent_start = 0; |
6826 | u64 extent_end = 0; | |
33345d01 | 6827 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6828 | u32 found_type; |
f421950f | 6829 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6830 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6831 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6832 | struct extent_buffer *leaf; |
6833 | struct btrfs_key found_key; | |
a52d9a80 CM |
6834 | struct extent_map *em = NULL; |
6835 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6836 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6837 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6838 | const bool new_inline = !page || create; |
a52d9a80 | 6839 | |
a52d9a80 | 6840 | again: |
890871be | 6841 | read_lock(&em_tree->lock); |
d1310b2e | 6842 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6843 | if (em) |
6844 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6845 | read_unlock(&em_tree->lock); |
d1310b2e | 6846 | |
a52d9a80 | 6847 | if (em) { |
e1c4b745 CM |
6848 | if (em->start > start || em->start + em->len <= start) |
6849 | free_extent_map(em); | |
6850 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6851 | free_extent_map(em); |
6852 | else | |
6853 | goto out; | |
a52d9a80 | 6854 | } |
172ddd60 | 6855 | em = alloc_extent_map(); |
a52d9a80 | 6856 | if (!em) { |
d1310b2e CM |
6857 | err = -ENOMEM; |
6858 | goto out; | |
a52d9a80 | 6859 | } |
e6dcd2dc | 6860 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6861 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6862 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6863 | em->len = (u64)-1; |
c8b97818 | 6864 | em->block_len = (u64)-1; |
f421950f CM |
6865 | |
6866 | if (!path) { | |
6867 | path = btrfs_alloc_path(); | |
026fd317 JB |
6868 | if (!path) { |
6869 | err = -ENOMEM; | |
6870 | goto out; | |
6871 | } | |
6872 | /* | |
6873 | * Chances are we'll be called again, so go ahead and do | |
6874 | * readahead | |
6875 | */ | |
e4058b54 | 6876 | path->reada = READA_FORWARD; |
f421950f CM |
6877 | } |
6878 | ||
179e29e4 CM |
6879 | ret = btrfs_lookup_file_extent(trans, root, path, |
6880 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6881 | if (ret < 0) { |
6882 | err = ret; | |
6883 | goto out; | |
6884 | } | |
6885 | ||
6886 | if (ret != 0) { | |
6887 | if (path->slots[0] == 0) | |
6888 | goto not_found; | |
6889 | path->slots[0]--; | |
6890 | } | |
6891 | ||
5f39d397 CM |
6892 | leaf = path->nodes[0]; |
6893 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6894 | struct btrfs_file_extent_item); |
a52d9a80 | 6895 | /* are we inside the extent that was found? */ |
5f39d397 | 6896 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6897 | found_type = found_key.type; |
5f39d397 | 6898 | if (found_key.objectid != objectid || |
a52d9a80 | 6899 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6900 | /* |
6901 | * If we backup past the first extent we want to move forward | |
6902 | * and see if there is an extent in front of us, otherwise we'll | |
6903 | * say there is a hole for our whole search range which can | |
6904 | * cause problems. | |
6905 | */ | |
6906 | extent_end = start; | |
6907 | goto next; | |
a52d9a80 CM |
6908 | } |
6909 | ||
5f39d397 CM |
6910 | found_type = btrfs_file_extent_type(leaf, item); |
6911 | extent_start = found_key.offset; | |
d899e052 YZ |
6912 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6913 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6914 | extent_end = extent_start + |
db94535d | 6915 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6916 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6917 | size_t size; | |
514ac8ad | 6918 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6919 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6920 | } |
25a50341 | 6921 | next: |
9036c102 YZ |
6922 | if (start >= extent_end) { |
6923 | path->slots[0]++; | |
6924 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6925 | ret = btrfs_next_leaf(root, path); | |
6926 | if (ret < 0) { | |
6927 | err = ret; | |
6928 | goto out; | |
a52d9a80 | 6929 | } |
9036c102 YZ |
6930 | if (ret > 0) |
6931 | goto not_found; | |
6932 | leaf = path->nodes[0]; | |
a52d9a80 | 6933 | } |
9036c102 YZ |
6934 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6935 | if (found_key.objectid != objectid || | |
6936 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6937 | goto not_found; | |
6938 | if (start + len <= found_key.offset) | |
6939 | goto not_found; | |
e2eca69d WS |
6940 | if (start > found_key.offset) |
6941 | goto next; | |
9036c102 | 6942 | em->start = start; |
70c8a91c | 6943 | em->orig_start = start; |
9036c102 YZ |
6944 | em->len = found_key.offset - start; |
6945 | goto not_found_em; | |
6946 | } | |
6947 | ||
7ffbb598 FM |
6948 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6949 | ||
d899e052 YZ |
6950 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6951 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6952 | goto insert; |
6953 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6954 | unsigned long ptr; |
a52d9a80 | 6955 | char *map; |
3326d1b0 CM |
6956 | size_t size; |
6957 | size_t extent_offset; | |
6958 | size_t copy_size; | |
a52d9a80 | 6959 | |
7ffbb598 | 6960 | if (new_inline) |
689f9346 | 6961 | goto out; |
5f39d397 | 6962 | |
514ac8ad | 6963 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6964 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6965 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6966 | size - extent_offset); | |
3326d1b0 | 6967 | em->start = extent_start + extent_offset; |
fda2832f | 6968 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6969 | em->orig_block_len = em->len; |
70c8a91c | 6970 | em->orig_start = em->start; |
689f9346 | 6971 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6972 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6973 | if (btrfs_file_extent_compression(leaf, item) != |
6974 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6975 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6976 | extent_offset, item); |
166ae5a4 ZB |
6977 | if (ret) { |
6978 | err = ret; | |
6979 | goto out; | |
6980 | } | |
c8b97818 CM |
6981 | } else { |
6982 | map = kmap(page); | |
6983 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6984 | copy_size); | |
09cbfeaf | 6985 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6986 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6987 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6988 | copy_size); |
6989 | } | |
c8b97818 CM |
6990 | kunmap(page); |
6991 | } | |
179e29e4 CM |
6992 | flush_dcache_page(page); |
6993 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6994 | BUG(); |
179e29e4 CM |
6995 | if (!trans) { |
6996 | kunmap(page); | |
6997 | free_extent_map(em); | |
6998 | em = NULL; | |
ff5714cc | 6999 | |
b3b4aa74 | 7000 | btrfs_release_path(path); |
7a7eaa40 | 7001 | trans = btrfs_join_transaction(root); |
ff5714cc | 7002 | |
3612b495 TI |
7003 | if (IS_ERR(trans)) |
7004 | return ERR_CAST(trans); | |
179e29e4 CM |
7005 | goto again; |
7006 | } | |
c8b97818 | 7007 | map = kmap(page); |
70dec807 | 7008 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 7009 | copy_size); |
c8b97818 | 7010 | kunmap(page); |
179e29e4 | 7011 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 7012 | } |
d1310b2e | 7013 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 7014 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 7015 | goto insert; |
a52d9a80 CM |
7016 | } |
7017 | not_found: | |
7018 | em->start = start; | |
70c8a91c | 7019 | em->orig_start = start; |
d1310b2e | 7020 | em->len = len; |
a52d9a80 | 7021 | not_found_em: |
5f39d397 | 7022 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 7023 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 7024 | insert: |
b3b4aa74 | 7025 | btrfs_release_path(path); |
d1310b2e | 7026 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 7027 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 7028 | em->start, em->len, start, len); |
a52d9a80 CM |
7029 | err = -EIO; |
7030 | goto out; | |
7031 | } | |
d1310b2e CM |
7032 | |
7033 | err = 0; | |
890871be | 7034 | write_lock(&em_tree->lock); |
09a2a8f9 | 7035 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
7036 | /* it is possible that someone inserted the extent into the tree |
7037 | * while we had the lock dropped. It is also possible that | |
7038 | * an overlapping map exists in the tree | |
7039 | */ | |
a52d9a80 | 7040 | if (ret == -EEXIST) { |
3b951516 | 7041 | struct extent_map *existing; |
e6dcd2dc CM |
7042 | |
7043 | ret = 0; | |
7044 | ||
e6c4efd8 QW |
7045 | existing = search_extent_mapping(em_tree, start, len); |
7046 | /* | |
7047 | * existing will always be non-NULL, since there must be | |
7048 | * extent causing the -EEXIST. | |
7049 | */ | |
8dff9c85 CM |
7050 | if (existing->start == em->start && |
7051 | extent_map_end(existing) == extent_map_end(em) && | |
7052 | em->block_start == existing->block_start) { | |
7053 | /* | |
7054 | * these two extents are the same, it happens | |
7055 | * with inlines especially | |
7056 | */ | |
7057 | free_extent_map(em); | |
7058 | em = existing; | |
7059 | err = 0; | |
7060 | ||
7061 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7062 | start <= existing->start) { |
e6c4efd8 QW |
7063 | /* |
7064 | * The existing extent map is the one nearest to | |
7065 | * the [start, start + len) range which overlaps | |
7066 | */ | |
7067 | err = merge_extent_mapping(em_tree, existing, | |
7068 | em, start); | |
e1c4b745 | 7069 | free_extent_map(existing); |
e6c4efd8 | 7070 | if (err) { |
3b951516 CM |
7071 | free_extent_map(em); |
7072 | em = NULL; | |
7073 | } | |
7074 | } else { | |
7075 | free_extent_map(em); | |
7076 | em = existing; | |
e6dcd2dc | 7077 | err = 0; |
a52d9a80 | 7078 | } |
a52d9a80 | 7079 | } |
890871be | 7080 | write_unlock(&em_tree->lock); |
a52d9a80 | 7081 | out: |
1abe9b8a | 7082 | |
4cd8587c | 7083 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 7084 | |
527afb44 | 7085 | btrfs_free_path(path); |
a52d9a80 CM |
7086 | if (trans) { |
7087 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 7088 | if (!err) |
a52d9a80 CM |
7089 | err = ret; |
7090 | } | |
a52d9a80 CM |
7091 | if (err) { |
7092 | free_extent_map(em); | |
a52d9a80 CM |
7093 | return ERR_PTR(err); |
7094 | } | |
79787eaa | 7095 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7096 | return em; |
7097 | } | |
7098 | ||
ec29ed5b CM |
7099 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
7100 | size_t pg_offset, u64 start, u64 len, | |
7101 | int create) | |
7102 | { | |
7103 | struct extent_map *em; | |
7104 | struct extent_map *hole_em = NULL; | |
7105 | u64 range_start = start; | |
7106 | u64 end; | |
7107 | u64 found; | |
7108 | u64 found_end; | |
7109 | int err = 0; | |
7110 | ||
7111 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7112 | if (IS_ERR(em)) | |
7113 | return em; | |
7114 | if (em) { | |
7115 | /* | |
f9e4fb53 LB |
7116 | * if our em maps to |
7117 | * - a hole or | |
7118 | * - a pre-alloc extent, | |
7119 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 7120 | */ |
f9e4fb53 LB |
7121 | if (em->block_start != EXTENT_MAP_HOLE && |
7122 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
7123 | return em; |
7124 | else | |
7125 | hole_em = em; | |
7126 | } | |
7127 | ||
7128 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7129 | end = start + len; | |
7130 | if (end < start) | |
7131 | end = (u64)-1; | |
7132 | else | |
7133 | end -= 1; | |
7134 | ||
7135 | em = NULL; | |
7136 | ||
7137 | /* ok, we didn't find anything, lets look for delalloc */ | |
7138 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
7139 | end, len, EXTENT_DELALLOC, 1); | |
7140 | found_end = range_start + found; | |
7141 | if (found_end < range_start) | |
7142 | found_end = (u64)-1; | |
7143 | ||
7144 | /* | |
7145 | * we didn't find anything useful, return | |
7146 | * the original results from get_extent() | |
7147 | */ | |
7148 | if (range_start > end || found_end <= start) { | |
7149 | em = hole_em; | |
7150 | hole_em = NULL; | |
7151 | goto out; | |
7152 | } | |
7153 | ||
7154 | /* adjust the range_start to make sure it doesn't | |
7155 | * go backwards from the start they passed in | |
7156 | */ | |
67871254 | 7157 | range_start = max(start, range_start); |
ec29ed5b CM |
7158 | found = found_end - range_start; |
7159 | ||
7160 | if (found > 0) { | |
7161 | u64 hole_start = start; | |
7162 | u64 hole_len = len; | |
7163 | ||
172ddd60 | 7164 | em = alloc_extent_map(); |
ec29ed5b CM |
7165 | if (!em) { |
7166 | err = -ENOMEM; | |
7167 | goto out; | |
7168 | } | |
7169 | /* | |
7170 | * when btrfs_get_extent can't find anything it | |
7171 | * returns one huge hole | |
7172 | * | |
7173 | * make sure what it found really fits our range, and | |
7174 | * adjust to make sure it is based on the start from | |
7175 | * the caller | |
7176 | */ | |
7177 | if (hole_em) { | |
7178 | u64 calc_end = extent_map_end(hole_em); | |
7179 | ||
7180 | if (calc_end <= start || (hole_em->start > end)) { | |
7181 | free_extent_map(hole_em); | |
7182 | hole_em = NULL; | |
7183 | } else { | |
7184 | hole_start = max(hole_em->start, start); | |
7185 | hole_len = calc_end - hole_start; | |
7186 | } | |
7187 | } | |
7188 | em->bdev = NULL; | |
7189 | if (hole_em && range_start > hole_start) { | |
7190 | /* our hole starts before our delalloc, so we | |
7191 | * have to return just the parts of the hole | |
7192 | * that go until the delalloc starts | |
7193 | */ | |
7194 | em->len = min(hole_len, | |
7195 | range_start - hole_start); | |
7196 | em->start = hole_start; | |
7197 | em->orig_start = hole_start; | |
7198 | /* | |
7199 | * don't adjust block start at all, | |
7200 | * it is fixed at EXTENT_MAP_HOLE | |
7201 | */ | |
7202 | em->block_start = hole_em->block_start; | |
7203 | em->block_len = hole_len; | |
f9e4fb53 LB |
7204 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7205 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7206 | } else { |
7207 | em->start = range_start; | |
7208 | em->len = found; | |
7209 | em->orig_start = range_start; | |
7210 | em->block_start = EXTENT_MAP_DELALLOC; | |
7211 | em->block_len = found; | |
7212 | } | |
7213 | } else if (hole_em) { | |
7214 | return hole_em; | |
7215 | } | |
7216 | out: | |
7217 | ||
7218 | free_extent_map(hole_em); | |
7219 | if (err) { | |
7220 | free_extent_map(em); | |
7221 | return ERR_PTR(err); | |
7222 | } | |
7223 | return em; | |
7224 | } | |
7225 | ||
5f9a8a51 FM |
7226 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7227 | const u64 start, | |
7228 | const u64 len, | |
7229 | const u64 orig_start, | |
7230 | const u64 block_start, | |
7231 | const u64 block_len, | |
7232 | const u64 orig_block_len, | |
7233 | const u64 ram_bytes, | |
7234 | const int type) | |
7235 | { | |
7236 | struct extent_map *em = NULL; | |
7237 | int ret; | |
7238 | ||
7239 | down_read(&BTRFS_I(inode)->dio_sem); | |
7240 | if (type != BTRFS_ORDERED_NOCOW) { | |
7241 | em = create_pinned_em(inode, start, len, orig_start, | |
7242 | block_start, block_len, orig_block_len, | |
7243 | ram_bytes, type); | |
7244 | if (IS_ERR(em)) | |
7245 | goto out; | |
7246 | } | |
7247 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7248 | len, block_len, type); | |
7249 | if (ret) { | |
7250 | if (em) { | |
7251 | free_extent_map(em); | |
7252 | btrfs_drop_extent_cache(inode, start, | |
7253 | start + len - 1, 0); | |
7254 | } | |
7255 | em = ERR_PTR(ret); | |
7256 | } | |
7257 | out: | |
7258 | up_read(&BTRFS_I(inode)->dio_sem); | |
7259 | ||
7260 | return em; | |
7261 | } | |
7262 | ||
4b46fce2 JB |
7263 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7264 | u64 start, u64 len) | |
7265 | { | |
7266 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7267 | struct extent_map *em; |
4b46fce2 JB |
7268 | struct btrfs_key ins; |
7269 | u64 alloc_hint; | |
7270 | int ret; | |
4b46fce2 | 7271 | |
4b46fce2 | 7272 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
18513091 | 7273 | ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0, |
e570fd27 | 7274 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7275 | if (ret) |
7276 | return ERR_PTR(ret); | |
4b46fce2 | 7277 | |
5f9a8a51 FM |
7278 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7279 | ins.objectid, ins.offset, ins.offset, | |
7280 | ins.offset, 0); | |
9cfa3e34 | 7281 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
5f9a8a51 | 7282 | if (IS_ERR(em)) |
e570fd27 | 7283 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
de0ee0ed | 7284 | |
4b46fce2 JB |
7285 | return em; |
7286 | } | |
7287 | ||
46bfbb5c CM |
7288 | /* |
7289 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7290 | * block must be cow'd | |
7291 | */ | |
00361589 | 7292 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7293 | u64 *orig_start, u64 *orig_block_len, |
7294 | u64 *ram_bytes) | |
46bfbb5c | 7295 | { |
00361589 | 7296 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7297 | struct btrfs_path *path; |
7298 | int ret; | |
7299 | struct extent_buffer *leaf; | |
7300 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7301 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7302 | struct btrfs_file_extent_item *fi; |
7303 | struct btrfs_key key; | |
7304 | u64 disk_bytenr; | |
7305 | u64 backref_offset; | |
7306 | u64 extent_end; | |
7307 | u64 num_bytes; | |
7308 | int slot; | |
7309 | int found_type; | |
7ee9e440 | 7310 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7311 | |
46bfbb5c CM |
7312 | path = btrfs_alloc_path(); |
7313 | if (!path) | |
7314 | return -ENOMEM; | |
7315 | ||
00361589 | 7316 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7317 | offset, 0); |
7318 | if (ret < 0) | |
7319 | goto out; | |
7320 | ||
7321 | slot = path->slots[0]; | |
7322 | if (ret == 1) { | |
7323 | if (slot == 0) { | |
7324 | /* can't find the item, must cow */ | |
7325 | ret = 0; | |
7326 | goto out; | |
7327 | } | |
7328 | slot--; | |
7329 | } | |
7330 | ret = 0; | |
7331 | leaf = path->nodes[0]; | |
7332 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7333 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7334 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7335 | /* not our file or wrong item type, must cow */ | |
7336 | goto out; | |
7337 | } | |
7338 | ||
7339 | if (key.offset > offset) { | |
7340 | /* Wrong offset, must cow */ | |
7341 | goto out; | |
7342 | } | |
7343 | ||
7344 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7345 | found_type = btrfs_file_extent_type(leaf, fi); | |
7346 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7347 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7348 | /* not a regular extent, must cow */ | |
7349 | goto out; | |
7350 | } | |
7ee9e440 JB |
7351 | |
7352 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7353 | goto out; | |
7354 | ||
e77751aa MX |
7355 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7356 | if (extent_end <= offset) | |
7357 | goto out; | |
7358 | ||
46bfbb5c | 7359 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7360 | if (disk_bytenr == 0) |
7361 | goto out; | |
7362 | ||
7363 | if (btrfs_file_extent_compression(leaf, fi) || | |
7364 | btrfs_file_extent_encryption(leaf, fi) || | |
7365 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7366 | goto out; | |
7367 | ||
46bfbb5c CM |
7368 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7369 | ||
7ee9e440 JB |
7370 | if (orig_start) { |
7371 | *orig_start = key.offset - backref_offset; | |
7372 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7373 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7374 | } | |
eb384b55 | 7375 | |
46bfbb5c CM |
7376 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7377 | goto out; | |
7b2b7085 MX |
7378 | |
7379 | num_bytes = min(offset + *len, extent_end) - offset; | |
7380 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7381 | u64 range_end; | |
7382 | ||
7383 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7384 | ret = test_range_bit(io_tree, offset, range_end, | |
7385 | EXTENT_DELALLOC, 0, NULL); | |
7386 | if (ret) { | |
7387 | ret = -EAGAIN; | |
7388 | goto out; | |
7389 | } | |
7390 | } | |
7391 | ||
1bda19eb | 7392 | btrfs_release_path(path); |
46bfbb5c CM |
7393 | |
7394 | /* | |
7395 | * look for other files referencing this extent, if we | |
7396 | * find any we must cow | |
7397 | */ | |
00361589 JB |
7398 | trans = btrfs_join_transaction(root); |
7399 | if (IS_ERR(trans)) { | |
7400 | ret = 0; | |
46bfbb5c | 7401 | goto out; |
00361589 JB |
7402 | } |
7403 | ||
7404 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7405 | key.offset - backref_offset, disk_bytenr); | |
7406 | btrfs_end_transaction(trans, root); | |
7407 | if (ret) { | |
7408 | ret = 0; | |
7409 | goto out; | |
7410 | } | |
46bfbb5c CM |
7411 | |
7412 | /* | |
7413 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7414 | * in this extent we are about to write. If there | |
7415 | * are any csums in that range we have to cow in order | |
7416 | * to keep the csums correct | |
7417 | */ | |
7418 | disk_bytenr += backref_offset; | |
7419 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7420 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7421 | goto out; | |
7422 | /* | |
7423 | * all of the above have passed, it is safe to overwrite this extent | |
7424 | * without cow | |
7425 | */ | |
eb384b55 | 7426 | *len = num_bytes; |
46bfbb5c CM |
7427 | ret = 1; |
7428 | out: | |
7429 | btrfs_free_path(path); | |
7430 | return ret; | |
7431 | } | |
7432 | ||
fc4adbff AG |
7433 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7434 | { | |
7435 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7436 | int found = false; | |
7437 | void **pagep = NULL; | |
7438 | struct page *page = NULL; | |
7439 | int start_idx; | |
7440 | int end_idx; | |
7441 | ||
09cbfeaf | 7442 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7443 | |
7444 | /* | |
7445 | * end is the last byte in the last page. end == start is legal | |
7446 | */ | |
09cbfeaf | 7447 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7448 | |
7449 | rcu_read_lock(); | |
7450 | ||
7451 | /* Most of the code in this while loop is lifted from | |
7452 | * find_get_page. It's been modified to begin searching from a | |
7453 | * page and return just the first page found in that range. If the | |
7454 | * found idx is less than or equal to the end idx then we know that | |
7455 | * a page exists. If no pages are found or if those pages are | |
7456 | * outside of the range then we're fine (yay!) */ | |
7457 | while (page == NULL && | |
7458 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7459 | page = radix_tree_deref_slot(pagep); | |
7460 | if (unlikely(!page)) | |
7461 | break; | |
7462 | ||
7463 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7464 | if (radix_tree_deref_retry(page)) { |
7465 | page = NULL; | |
fc4adbff | 7466 | continue; |
809f9016 | 7467 | } |
fc4adbff AG |
7468 | /* |
7469 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7470 | * here as an exceptional entry: so return it without | |
7471 | * attempting to raise page count. | |
7472 | */ | |
6fdef6d4 | 7473 | page = NULL; |
fc4adbff AG |
7474 | break; /* TODO: Is this relevant for this use case? */ |
7475 | } | |
7476 | ||
91405151 FM |
7477 | if (!page_cache_get_speculative(page)) { |
7478 | page = NULL; | |
fc4adbff | 7479 | continue; |
91405151 | 7480 | } |
fc4adbff AG |
7481 | |
7482 | /* | |
7483 | * Has the page moved? | |
7484 | * This is part of the lockless pagecache protocol. See | |
7485 | * include/linux/pagemap.h for details. | |
7486 | */ | |
7487 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7488 | put_page(page); |
fc4adbff AG |
7489 | page = NULL; |
7490 | } | |
7491 | } | |
7492 | ||
7493 | if (page) { | |
7494 | if (page->index <= end_idx) | |
7495 | found = true; | |
09cbfeaf | 7496 | put_page(page); |
fc4adbff AG |
7497 | } |
7498 | ||
7499 | rcu_read_unlock(); | |
7500 | return found; | |
7501 | } | |
7502 | ||
eb838e73 JB |
7503 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7504 | struct extent_state **cached_state, int writing) | |
7505 | { | |
7506 | struct btrfs_ordered_extent *ordered; | |
7507 | int ret = 0; | |
7508 | ||
7509 | while (1) { | |
7510 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7511 | cached_state); |
eb838e73 JB |
7512 | /* |
7513 | * We're concerned with the entire range that we're going to be | |
01327610 | 7514 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7515 | * extents in this range. |
7516 | */ | |
7517 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7518 | lockend - lockstart + 1); | |
7519 | ||
7520 | /* | |
7521 | * We need to make sure there are no buffered pages in this | |
7522 | * range either, we could have raced between the invalidate in | |
7523 | * generic_file_direct_write and locking the extent. The | |
7524 | * invalidate needs to happen so that reads after a write do not | |
7525 | * get stale data. | |
7526 | */ | |
fc4adbff AG |
7527 | if (!ordered && |
7528 | (!writing || | |
7529 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7530 | break; |
7531 | ||
7532 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7533 | cached_state, GFP_NOFS); | |
7534 | ||
7535 | if (ordered) { | |
ade77029 FM |
7536 | /* |
7537 | * If we are doing a DIO read and the ordered extent we | |
7538 | * found is for a buffered write, we can not wait for it | |
7539 | * to complete and retry, because if we do so we can | |
7540 | * deadlock with concurrent buffered writes on page | |
7541 | * locks. This happens only if our DIO read covers more | |
7542 | * than one extent map, if at this point has already | |
7543 | * created an ordered extent for a previous extent map | |
7544 | * and locked its range in the inode's io tree, and a | |
7545 | * concurrent write against that previous extent map's | |
7546 | * range and this range started (we unlock the ranges | |
7547 | * in the io tree only when the bios complete and | |
7548 | * buffered writes always lock pages before attempting | |
7549 | * to lock range in the io tree). | |
7550 | */ | |
7551 | if (writing || | |
7552 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7553 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7554 | else | |
7555 | ret = -ENOTBLK; | |
eb838e73 JB |
7556 | btrfs_put_ordered_extent(ordered); |
7557 | } else { | |
eb838e73 | 7558 | /* |
b850ae14 FM |
7559 | * We could trigger writeback for this range (and wait |
7560 | * for it to complete) and then invalidate the pages for | |
7561 | * this range (through invalidate_inode_pages2_range()), | |
7562 | * but that can lead us to a deadlock with a concurrent | |
7563 | * call to readpages() (a buffered read or a defrag call | |
7564 | * triggered a readahead) on a page lock due to an | |
7565 | * ordered dio extent we created before but did not have | |
7566 | * yet a corresponding bio submitted (whence it can not | |
7567 | * complete), which makes readpages() wait for that | |
7568 | * ordered extent to complete while holding a lock on | |
7569 | * that page. | |
eb838e73 | 7570 | */ |
b850ae14 | 7571 | ret = -ENOTBLK; |
eb838e73 JB |
7572 | } |
7573 | ||
ade77029 FM |
7574 | if (ret) |
7575 | break; | |
7576 | ||
eb838e73 JB |
7577 | cond_resched(); |
7578 | } | |
7579 | ||
7580 | return ret; | |
7581 | } | |
7582 | ||
69ffb543 JB |
7583 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7584 | u64 len, u64 orig_start, | |
7585 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7586 | u64 orig_block_len, u64 ram_bytes, |
7587 | int type) | |
69ffb543 JB |
7588 | { |
7589 | struct extent_map_tree *em_tree; | |
7590 | struct extent_map *em; | |
7591 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7592 | int ret; | |
7593 | ||
7594 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7595 | em = alloc_extent_map(); | |
7596 | if (!em) | |
7597 | return ERR_PTR(-ENOMEM); | |
7598 | ||
7599 | em->start = start; | |
7600 | em->orig_start = orig_start; | |
2ab28f32 JB |
7601 | em->mod_start = start; |
7602 | em->mod_len = len; | |
69ffb543 JB |
7603 | em->len = len; |
7604 | em->block_len = block_len; | |
7605 | em->block_start = block_start; | |
7606 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7607 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7608 | em->ram_bytes = ram_bytes; |
70c8a91c | 7609 | em->generation = -1; |
69ffb543 JB |
7610 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7611 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7612 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7613 | |
7614 | do { | |
7615 | btrfs_drop_extent_cache(inode, em->start, | |
7616 | em->start + em->len - 1, 0); | |
7617 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7618 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7619 | write_unlock(&em_tree->lock); |
7620 | } while (ret == -EEXIST); | |
7621 | ||
7622 | if (ret) { | |
7623 | free_extent_map(em); | |
7624 | return ERR_PTR(ret); | |
7625 | } | |
7626 | ||
7627 | return em; | |
7628 | } | |
7629 | ||
9c9464cc FM |
7630 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7631 | struct btrfs_dio_data *dio_data, | |
7632 | const u64 len) | |
7633 | { | |
7634 | unsigned num_extents; | |
7635 | ||
7636 | num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1, | |
7637 | BTRFS_MAX_EXTENT_SIZE); | |
7638 | /* | |
7639 | * If we have an outstanding_extents count still set then we're | |
7640 | * within our reservation, otherwise we need to adjust our inode | |
7641 | * counter appropriately. | |
7642 | */ | |
7643 | if (dio_data->outstanding_extents) { | |
7644 | dio_data->outstanding_extents -= num_extents; | |
7645 | } else { | |
7646 | spin_lock(&BTRFS_I(inode)->lock); | |
7647 | BTRFS_I(inode)->outstanding_extents += num_extents; | |
7648 | spin_unlock(&BTRFS_I(inode)->lock); | |
7649 | } | |
7650 | } | |
7651 | ||
4b46fce2 JB |
7652 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7653 | struct buffer_head *bh_result, int create) | |
7654 | { | |
7655 | struct extent_map *em; | |
7656 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7657 | struct extent_state *cached_state = NULL; |
50745b0a | 7658 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7659 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7660 | u64 lockstart, lockend; |
4b46fce2 | 7661 | u64 len = bh_result->b_size; |
eb838e73 | 7662 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7663 | int ret = 0; |
eb838e73 | 7664 | |
172a5049 | 7665 | if (create) |
3266789f | 7666 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7667 | else |
c329861d | 7668 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7669 | |
c329861d JB |
7670 | lockstart = start; |
7671 | lockend = start + len - 1; | |
7672 | ||
e1cbbfa5 JB |
7673 | if (current->journal_info) { |
7674 | /* | |
7675 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7676 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7677 | * confused. |
7678 | */ | |
50745b0a | 7679 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7680 | current->journal_info = NULL; |
7681 | } | |
7682 | ||
eb838e73 JB |
7683 | /* |
7684 | * If this errors out it's because we couldn't invalidate pagecache for | |
7685 | * this range and we need to fallback to buffered. | |
7686 | */ | |
9c9464cc FM |
7687 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7688 | create)) { | |
7689 | ret = -ENOTBLK; | |
7690 | goto err; | |
7691 | } | |
eb838e73 | 7692 | |
4b46fce2 | 7693 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7694 | if (IS_ERR(em)) { |
7695 | ret = PTR_ERR(em); | |
7696 | goto unlock_err; | |
7697 | } | |
4b46fce2 JB |
7698 | |
7699 | /* | |
7700 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7701 | * io. INLINE is special, and we could probably kludge it in here, but | |
7702 | * it's still buffered so for safety lets just fall back to the generic | |
7703 | * buffered path. | |
7704 | * | |
7705 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7706 | * decompress it, so there will be buffering required no matter what we | |
7707 | * do, so go ahead and fallback to buffered. | |
7708 | * | |
01327610 | 7709 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7710 | * to buffered IO. Don't blame me, this is the price we pay for using |
7711 | * the generic code. | |
7712 | */ | |
7713 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7714 | em->block_start == EXTENT_MAP_INLINE) { | |
7715 | free_extent_map(em); | |
eb838e73 JB |
7716 | ret = -ENOTBLK; |
7717 | goto unlock_err; | |
4b46fce2 JB |
7718 | } |
7719 | ||
7720 | /* Just a good old fashioned hole, return */ | |
7721 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7722 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7723 | free_extent_map(em); | |
eb838e73 | 7724 | goto unlock_err; |
4b46fce2 JB |
7725 | } |
7726 | ||
7727 | /* | |
7728 | * We don't allocate a new extent in the following cases | |
7729 | * | |
7730 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7731 | * existing extent. | |
7732 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7733 | * just use the extent. | |
7734 | * | |
7735 | */ | |
46bfbb5c | 7736 | if (!create) { |
eb838e73 JB |
7737 | len = min(len, em->len - (start - em->start)); |
7738 | lockstart = start + len; | |
7739 | goto unlock; | |
46bfbb5c | 7740 | } |
4b46fce2 JB |
7741 | |
7742 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7743 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7744 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7745 | int type; |
eb384b55 | 7746 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7747 | |
7748 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7749 | type = BTRFS_ORDERED_PREALLOC; | |
7750 | else | |
7751 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7752 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7753 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7754 | |
00361589 | 7755 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c FM |
7756 | &orig_block_len, &ram_bytes) == 1 && |
7757 | btrfs_inc_nocow_writers(root->fs_info, block_start)) { | |
5f9a8a51 | 7758 | struct extent_map *em2; |
0b901916 | 7759 | |
5f9a8a51 FM |
7760 | em2 = btrfs_create_dio_extent(inode, start, len, |
7761 | orig_start, block_start, | |
7762 | len, orig_block_len, | |
7763 | ram_bytes, type); | |
f78c436c | 7764 | btrfs_dec_nocow_writers(root->fs_info, block_start); |
69ffb543 JB |
7765 | if (type == BTRFS_ORDERED_PREALLOC) { |
7766 | free_extent_map(em); | |
5f9a8a51 | 7767 | em = em2; |
69ffb543 | 7768 | } |
5f9a8a51 FM |
7769 | if (em2 && IS_ERR(em2)) { |
7770 | ret = PTR_ERR(em2); | |
eb838e73 | 7771 | goto unlock_err; |
46bfbb5c | 7772 | } |
18513091 WX |
7773 | /* |
7774 | * For inode marked NODATACOW or extent marked PREALLOC, | |
7775 | * use the existing or preallocated extent, so does not | |
7776 | * need to adjust btrfs_space_info's bytes_may_use. | |
7777 | */ | |
7778 | btrfs_free_reserved_data_space_noquota(inode, | |
7779 | start, len); | |
46bfbb5c | 7780 | goto unlock; |
4b46fce2 | 7781 | } |
4b46fce2 | 7782 | } |
00361589 | 7783 | |
46bfbb5c CM |
7784 | /* |
7785 | * this will cow the extent, reset the len in case we changed | |
7786 | * it above | |
7787 | */ | |
7788 | len = bh_result->b_size; | |
70c8a91c JB |
7789 | free_extent_map(em); |
7790 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7791 | if (IS_ERR(em)) { |
7792 | ret = PTR_ERR(em); | |
7793 | goto unlock_err; | |
7794 | } | |
46bfbb5c CM |
7795 | len = min(len, em->len - (start - em->start)); |
7796 | unlock: | |
4b46fce2 JB |
7797 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7798 | inode->i_blkbits; | |
46bfbb5c | 7799 | bh_result->b_size = len; |
4b46fce2 JB |
7800 | bh_result->b_bdev = em->bdev; |
7801 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7802 | if (create) { |
7803 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7804 | set_buffer_new(bh_result); | |
7805 | ||
7806 | /* | |
7807 | * Need to update the i_size under the extent lock so buffered | |
7808 | * readers will get the updated i_size when we unlock. | |
7809 | */ | |
7810 | if (start + len > i_size_read(inode)) | |
7811 | i_size_write(inode, start + len); | |
0934856d | 7812 | |
9c9464cc | 7813 | adjust_dio_outstanding_extents(inode, dio_data, len); |
50745b0a | 7814 | WARN_ON(dio_data->reserve < len); |
7815 | dio_data->reserve -= len; | |
f28a4928 | 7816 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7817 | current->journal_info = dio_data; |
c3473e83 | 7818 | } |
4b46fce2 | 7819 | |
eb838e73 JB |
7820 | /* |
7821 | * In the case of write we need to clear and unlock the entire range, | |
7822 | * in the case of read we need to unlock only the end area that we | |
7823 | * aren't using if there is any left over space. | |
7824 | */ | |
24c03fa5 | 7825 | if (lockstart < lockend) { |
0934856d MX |
7826 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7827 | lockend, unlock_bits, 1, 0, | |
7828 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7829 | } else { |
eb838e73 | 7830 | free_extent_state(cached_state); |
24c03fa5 | 7831 | } |
eb838e73 | 7832 | |
4b46fce2 JB |
7833 | free_extent_map(em); |
7834 | ||
7835 | return 0; | |
eb838e73 JB |
7836 | |
7837 | unlock_err: | |
eb838e73 JB |
7838 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7839 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7840 | err: |
50745b0a | 7841 | if (dio_data) |
7842 | current->journal_info = dio_data; | |
9c9464cc FM |
7843 | /* |
7844 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7845 | * write less data then expected, so that we don't underflow our inode's | |
7846 | * outstanding extents counter. | |
7847 | */ | |
7848 | if (create && dio_data) | |
7849 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7850 | ||
eb838e73 | 7851 | return ret; |
4b46fce2 JB |
7852 | } |
7853 | ||
8b110e39 | 7854 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
81a75f67 | 7855 | int mirror_num) |
8b110e39 MX |
7856 | { |
7857 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7858 | int ret; | |
7859 | ||
37226b21 | 7860 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
8b110e39 MX |
7861 | |
7862 | bio_get(bio); | |
7863 | ||
7864 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7865 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7866 | if (ret) | |
7867 | goto err; | |
7868 | ||
81a75f67 | 7869 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
8b110e39 MX |
7870 | err: |
7871 | bio_put(bio); | |
7872 | return ret; | |
7873 | } | |
7874 | ||
7875 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7876 | struct bio *failed_bio, | |
7877 | struct io_failure_record *failrec, | |
7878 | int failed_mirror) | |
7879 | { | |
7880 | int num_copies; | |
7881 | ||
7882 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7883 | failrec->logical, failrec->len); | |
7884 | if (num_copies == 1) { | |
7885 | /* | |
7886 | * we only have a single copy of the data, so don't bother with | |
7887 | * all the retry and error correction code that follows. no | |
7888 | * matter what the error is, it is very likely to persist. | |
7889 | */ | |
7890 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7891 | num_copies, failrec->this_mirror, failed_mirror); | |
7892 | return 0; | |
7893 | } | |
7894 | ||
7895 | failrec->failed_mirror = failed_mirror; | |
7896 | failrec->this_mirror++; | |
7897 | if (failrec->this_mirror == failed_mirror) | |
7898 | failrec->this_mirror++; | |
7899 | ||
7900 | if (failrec->this_mirror > num_copies) { | |
7901 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7902 | num_copies, failrec->this_mirror, failed_mirror); | |
7903 | return 0; | |
7904 | } | |
7905 | ||
7906 | return 1; | |
7907 | } | |
7908 | ||
7909 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
2dabb324 CR |
7910 | struct page *page, unsigned int pgoff, |
7911 | u64 start, u64 end, int failed_mirror, | |
7912 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7913 | { |
7914 | struct io_failure_record *failrec; | |
7915 | struct bio *bio; | |
7916 | int isector; | |
7917 | int read_mode; | |
7918 | int ret; | |
7919 | ||
37226b21 | 7920 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
8b110e39 MX |
7921 | |
7922 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7923 | if (ret) | |
7924 | return ret; | |
7925 | ||
7926 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7927 | failed_mirror); | |
7928 | if (!ret) { | |
7929 | free_io_failure(inode, failrec); | |
7930 | return -EIO; | |
7931 | } | |
7932 | ||
2dabb324 CR |
7933 | if ((failed_bio->bi_vcnt > 1) |
7934 | || (failed_bio->bi_io_vec->bv_len | |
7935 | > BTRFS_I(inode)->root->sectorsize)) | |
8b110e39 MX |
7936 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
7937 | else | |
7938 | read_mode = READ_SYNC; | |
7939 | ||
7940 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7941 | isector >>= inode->i_sb->s_blocksize_bits; | |
7942 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 7943 | pgoff, isector, repair_endio, repair_arg); |
8b110e39 MX |
7944 | if (!bio) { |
7945 | free_io_failure(inode, failrec); | |
7946 | return -EIO; | |
7947 | } | |
37226b21 | 7948 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
8b110e39 MX |
7949 | |
7950 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7951 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7952 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7953 | ||
81a75f67 | 7954 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
8b110e39 MX |
7955 | if (ret) { |
7956 | free_io_failure(inode, failrec); | |
7957 | bio_put(bio); | |
7958 | } | |
7959 | ||
7960 | return ret; | |
7961 | } | |
7962 | ||
7963 | struct btrfs_retry_complete { | |
7964 | struct completion done; | |
7965 | struct inode *inode; | |
7966 | u64 start; | |
7967 | int uptodate; | |
7968 | }; | |
7969 | ||
4246a0b6 | 7970 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7971 | { |
7972 | struct btrfs_retry_complete *done = bio->bi_private; | |
2dabb324 | 7973 | struct inode *inode; |
8b110e39 MX |
7974 | struct bio_vec *bvec; |
7975 | int i; | |
7976 | ||
4246a0b6 | 7977 | if (bio->bi_error) |
8b110e39 MX |
7978 | goto end; |
7979 | ||
2dabb324 CR |
7980 | ASSERT(bio->bi_vcnt == 1); |
7981 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
7982 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); | |
7983 | ||
8b110e39 MX |
7984 | done->uptodate = 1; |
7985 | bio_for_each_segment_all(bvec, bio, i) | |
7986 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7987 | end: | |
7988 | complete(&done->done); | |
7989 | bio_put(bio); | |
7990 | } | |
7991 | ||
7992 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7993 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7994 | { |
2dabb324 | 7995 | struct btrfs_fs_info *fs_info; |
2c30c71b | 7996 | struct bio_vec *bvec; |
8b110e39 | 7997 | struct btrfs_retry_complete done; |
4b46fce2 | 7998 | u64 start; |
2dabb324 CR |
7999 | unsigned int pgoff; |
8000 | u32 sectorsize; | |
8001 | int nr_sectors; | |
2c30c71b | 8002 | int i; |
c1dc0896 | 8003 | int ret; |
4b46fce2 | 8004 | |
2dabb324 CR |
8005 | fs_info = BTRFS_I(inode)->root->fs_info; |
8006 | sectorsize = BTRFS_I(inode)->root->sectorsize; | |
8007 | ||
8b110e39 MX |
8008 | start = io_bio->logical; |
8009 | done.inode = inode; | |
8010 | ||
8011 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
2dabb324 CR |
8012 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
8013 | pgoff = bvec->bv_offset; | |
8014 | ||
8015 | next_block_or_try_again: | |
8b110e39 MX |
8016 | done.uptodate = 0; |
8017 | done.start = start; | |
8018 | init_completion(&done.done); | |
8019 | ||
2dabb324 CR |
8020 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
8021 | pgoff, start, start + sectorsize - 1, | |
8022 | io_bio->mirror_num, | |
8023 | btrfs_retry_endio_nocsum, &done); | |
8b110e39 MX |
8024 | if (ret) |
8025 | return ret; | |
8026 | ||
8027 | wait_for_completion(&done.done); | |
8028 | ||
8029 | if (!done.uptodate) { | |
8030 | /* We might have another mirror, so try again */ | |
2dabb324 | 8031 | goto next_block_or_try_again; |
8b110e39 MX |
8032 | } |
8033 | ||
2dabb324 CR |
8034 | start += sectorsize; |
8035 | ||
8036 | if (nr_sectors--) { | |
8037 | pgoff += sectorsize; | |
8038 | goto next_block_or_try_again; | |
8039 | } | |
8b110e39 MX |
8040 | } |
8041 | ||
8042 | return 0; | |
8043 | } | |
8044 | ||
4246a0b6 | 8045 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
8046 | { |
8047 | struct btrfs_retry_complete *done = bio->bi_private; | |
8048 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
2dabb324 | 8049 | struct inode *inode; |
8b110e39 | 8050 | struct bio_vec *bvec; |
2dabb324 | 8051 | u64 start; |
8b110e39 MX |
8052 | int uptodate; |
8053 | int ret; | |
8054 | int i; | |
8055 | ||
4246a0b6 | 8056 | if (bio->bi_error) |
8b110e39 MX |
8057 | goto end; |
8058 | ||
8059 | uptodate = 1; | |
2dabb324 CR |
8060 | |
8061 | start = done->start; | |
8062 | ||
8063 | ASSERT(bio->bi_vcnt == 1); | |
8064 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
8065 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); | |
8066 | ||
8b110e39 MX |
8067 | bio_for_each_segment_all(bvec, bio, i) { |
8068 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
2dabb324 CR |
8069 | bvec->bv_page, bvec->bv_offset, |
8070 | done->start, bvec->bv_len); | |
8b110e39 MX |
8071 | if (!ret) |
8072 | clean_io_failure(done->inode, done->start, | |
2dabb324 | 8073 | bvec->bv_page, bvec->bv_offset); |
8b110e39 MX |
8074 | else |
8075 | uptodate = 0; | |
8076 | } | |
8077 | ||
8078 | done->uptodate = uptodate; | |
8079 | end: | |
8080 | complete(&done->done); | |
8081 | bio_put(bio); | |
8082 | } | |
8083 | ||
8084 | static int __btrfs_subio_endio_read(struct inode *inode, | |
8085 | struct btrfs_io_bio *io_bio, int err) | |
8086 | { | |
2dabb324 | 8087 | struct btrfs_fs_info *fs_info; |
8b110e39 MX |
8088 | struct bio_vec *bvec; |
8089 | struct btrfs_retry_complete done; | |
8090 | u64 start; | |
8091 | u64 offset = 0; | |
2dabb324 CR |
8092 | u32 sectorsize; |
8093 | int nr_sectors; | |
8094 | unsigned int pgoff; | |
8095 | int csum_pos; | |
8b110e39 MX |
8096 | int i; |
8097 | int ret; | |
dc380aea | 8098 | |
2dabb324 CR |
8099 | fs_info = BTRFS_I(inode)->root->fs_info; |
8100 | sectorsize = BTRFS_I(inode)->root->sectorsize; | |
8101 | ||
8b110e39 | 8102 | err = 0; |
c1dc0896 | 8103 | start = io_bio->logical; |
8b110e39 MX |
8104 | done.inode = inode; |
8105 | ||
c1dc0896 | 8106 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
2dabb324 CR |
8107 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
8108 | ||
8109 | pgoff = bvec->bv_offset; | |
8110 | next_block: | |
8111 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8112 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8113 | bvec->bv_page, pgoff, start, | |
8114 | sectorsize); | |
8b110e39 MX |
8115 | if (likely(!ret)) |
8116 | goto next; | |
8117 | try_again: | |
8118 | done.uptodate = 0; | |
8119 | done.start = start; | |
8120 | init_completion(&done.done); | |
8121 | ||
2dabb324 CR |
8122 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
8123 | pgoff, start, start + sectorsize - 1, | |
8124 | io_bio->mirror_num, | |
8125 | btrfs_retry_endio, &done); | |
8b110e39 MX |
8126 | if (ret) { |
8127 | err = ret; | |
8128 | goto next; | |
8129 | } | |
8130 | ||
8131 | wait_for_completion(&done.done); | |
8132 | ||
8133 | if (!done.uptodate) { | |
8134 | /* We might have another mirror, so try again */ | |
8135 | goto try_again; | |
8136 | } | |
8137 | next: | |
2dabb324 CR |
8138 | offset += sectorsize; |
8139 | start += sectorsize; | |
8140 | ||
8141 | ASSERT(nr_sectors); | |
8142 | ||
8143 | if (--nr_sectors) { | |
8144 | pgoff += sectorsize; | |
8145 | goto next_block; | |
8146 | } | |
2c30c71b | 8147 | } |
c1dc0896 MX |
8148 | |
8149 | return err; | |
8150 | } | |
8151 | ||
8b110e39 MX |
8152 | static int btrfs_subio_endio_read(struct inode *inode, |
8153 | struct btrfs_io_bio *io_bio, int err) | |
8154 | { | |
8155 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8156 | ||
8157 | if (skip_csum) { | |
8158 | if (unlikely(err)) | |
8159 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8160 | else | |
8161 | return 0; | |
8162 | } else { | |
8163 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8164 | } | |
8165 | } | |
8166 | ||
4246a0b6 | 8167 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8168 | { |
8169 | struct btrfs_dio_private *dip = bio->bi_private; | |
8170 | struct inode *inode = dip->inode; | |
8171 | struct bio *dio_bio; | |
8172 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 8173 | int err = bio->bi_error; |
c1dc0896 | 8174 | |
8b110e39 MX |
8175 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8176 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 8177 | |
4b46fce2 | 8178 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8179 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8180 | dio_bio = dip->dio_bio; |
4b46fce2 | 8181 | |
4b46fce2 | 8182 | kfree(dip); |
c0da7aa1 | 8183 | |
1636d1d7 | 8184 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8185 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
8186 | |
8187 | if (io_bio->end_io) | |
8188 | io_bio->end_io(io_bio, err); | |
9be3395b | 8189 | bio_put(bio); |
4b46fce2 JB |
8190 | } |
8191 | ||
14543774 FM |
8192 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
8193 | const u64 offset, | |
8194 | const u64 bytes, | |
8195 | const int uptodate) | |
4b46fce2 | 8196 | { |
4b46fce2 | 8197 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4b46fce2 | 8198 | struct btrfs_ordered_extent *ordered = NULL; |
14543774 FM |
8199 | u64 ordered_offset = offset; |
8200 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8201 | int ret; |
8202 | ||
163cf09c CM |
8203 | again: |
8204 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8205 | &ordered_offset, | |
4246a0b6 | 8206 | ordered_bytes, |
14543774 | 8207 | uptodate); |
4b46fce2 | 8208 | if (!ret) |
163cf09c | 8209 | goto out_test; |
4b46fce2 | 8210 | |
9e0af237 LB |
8211 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
8212 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
8213 | btrfs_queue_work(root->fs_info->endio_write_workers, |
8214 | &ordered->work); | |
163cf09c CM |
8215 | out_test: |
8216 | /* | |
8217 | * our bio might span multiple ordered extents. If we haven't | |
8218 | * completed the accounting for the whole dio, go back and try again | |
8219 | */ | |
14543774 FM |
8220 | if (ordered_offset < offset + bytes) { |
8221 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8222 | ordered = NULL; |
163cf09c CM |
8223 | goto again; |
8224 | } | |
14543774 FM |
8225 | } |
8226 | ||
8227 | static void btrfs_endio_direct_write(struct bio *bio) | |
8228 | { | |
8229 | struct btrfs_dio_private *dip = bio->bi_private; | |
8230 | struct bio *dio_bio = dip->dio_bio; | |
8231 | ||
8232 | btrfs_endio_direct_write_update_ordered(dip->inode, | |
8233 | dip->logical_offset, | |
8234 | dip->bytes, | |
8235 | !bio->bi_error); | |
4b46fce2 | 8236 | |
4b46fce2 | 8237 | kfree(dip); |
c0da7aa1 | 8238 | |
1636d1d7 | 8239 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8240 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 8241 | bio_put(bio); |
4b46fce2 JB |
8242 | } |
8243 | ||
81a75f67 | 8244 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
eaf25d93 CM |
8245 | struct bio *bio, int mirror_num, |
8246 | unsigned long bio_flags, u64 offset) | |
8247 | { | |
8248 | int ret; | |
8249 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8250 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 8251 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8252 | return 0; |
8253 | } | |
8254 | ||
4246a0b6 | 8255 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8256 | { |
8257 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 8258 | int err = bio->bi_error; |
e65e1535 | 8259 | |
8b110e39 MX |
8260 | if (err) |
8261 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
6296b960 | 8262 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
1eff9d32 | 8263 | btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf, |
8b110e39 MX |
8264 | (unsigned long long)bio->bi_iter.bi_sector, |
8265 | bio->bi_iter.bi_size, err); | |
8266 | ||
8267 | if (dip->subio_endio) | |
8268 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8269 | |
8270 | if (err) { | |
e65e1535 MX |
8271 | dip->errors = 1; |
8272 | ||
8273 | /* | |
8274 | * before atomic variable goto zero, we must make sure | |
8275 | * dip->errors is perceived to be set. | |
8276 | */ | |
4e857c58 | 8277 | smp_mb__before_atomic(); |
e65e1535 MX |
8278 | } |
8279 | ||
8280 | /* if there are more bios still pending for this dio, just exit */ | |
8281 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8282 | goto out; | |
8283 | ||
9be3395b | 8284 | if (dip->errors) { |
e65e1535 | 8285 | bio_io_error(dip->orig_bio); |
9be3395b | 8286 | } else { |
4246a0b6 CH |
8287 | dip->dio_bio->bi_error = 0; |
8288 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
8289 | } |
8290 | out: | |
8291 | bio_put(bio); | |
8292 | } | |
8293 | ||
8294 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
8295 | u64 first_sector, gfp_t gfp_flags) | |
8296 | { | |
da2f0f74 | 8297 | struct bio *bio; |
22365979 | 8298 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
da2f0f74 CM |
8299 | if (bio) |
8300 | bio_associate_current(bio); | |
8301 | return bio; | |
e65e1535 MX |
8302 | } |
8303 | ||
c1dc0896 MX |
8304 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
8305 | struct inode *inode, | |
8306 | struct btrfs_dio_private *dip, | |
8307 | struct bio *bio, | |
8308 | u64 file_offset) | |
8309 | { | |
8310 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8311 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8312 | int ret; | |
8313 | ||
8314 | /* | |
8315 | * We load all the csum data we need when we submit | |
8316 | * the first bio to reduce the csum tree search and | |
8317 | * contention. | |
8318 | */ | |
8319 | if (dip->logical_offset == file_offset) { | |
8320 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
8321 | file_offset); | |
8322 | if (ret) | |
8323 | return ret; | |
8324 | } | |
8325 | ||
8326 | if (bio == dip->orig_bio) | |
8327 | return 0; | |
8328 | ||
8329 | file_offset -= dip->logical_offset; | |
8330 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8331 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8332 | ||
8333 | return 0; | |
8334 | } | |
8335 | ||
e65e1535 | 8336 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
81a75f67 | 8337 | u64 file_offset, int skip_sum, |
c329861d | 8338 | int async_submit) |
e65e1535 | 8339 | { |
facc8a22 | 8340 | struct btrfs_dio_private *dip = bio->bi_private; |
37226b21 | 8341 | bool write = bio_op(bio) == REQ_OP_WRITE; |
e65e1535 MX |
8342 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8343 | int ret; | |
8344 | ||
b812ce28 JB |
8345 | if (async_submit) |
8346 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8347 | ||
e65e1535 | 8348 | bio_get(bio); |
5fd02043 JB |
8349 | |
8350 | if (!write) { | |
bfebd8b5 DS |
8351 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8352 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8353 | if (ret) |
8354 | goto err; | |
8355 | } | |
e65e1535 | 8356 | |
1ae39938 JB |
8357 | if (skip_sum) |
8358 | goto map; | |
8359 | ||
8360 | if (write && async_submit) { | |
e65e1535 | 8361 | ret = btrfs_wq_submit_bio(root->fs_info, |
81a75f67 | 8362 | inode, bio, 0, 0, file_offset, |
e65e1535 MX |
8363 | __btrfs_submit_bio_start_direct_io, |
8364 | __btrfs_submit_bio_done); | |
8365 | goto err; | |
1ae39938 JB |
8366 | } else if (write) { |
8367 | /* | |
8368 | * If we aren't doing async submit, calculate the csum of the | |
8369 | * bio now. | |
8370 | */ | |
8371 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8372 | if (ret) | |
8373 | goto err; | |
23ea8e5a | 8374 | } else { |
c1dc0896 MX |
8375 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8376 | file_offset); | |
c2db1073 TI |
8377 | if (ret) |
8378 | goto err; | |
8379 | } | |
1ae39938 | 8380 | map: |
81a75f67 | 8381 | ret = btrfs_map_bio(root, bio, 0, async_submit); |
e65e1535 MX |
8382 | err: |
8383 | bio_put(bio); | |
8384 | return ret; | |
8385 | } | |
8386 | ||
81a75f67 | 8387 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
e65e1535 MX |
8388 | int skip_sum) |
8389 | { | |
8390 | struct inode *inode = dip->inode; | |
8391 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8392 | struct bio *bio; |
8393 | struct bio *orig_bio = dip->orig_bio; | |
8394 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8395 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8396 | u64 file_offset = dip->logical_offset; |
8397 | u64 submit_len = 0; | |
8398 | u64 map_length; | |
5f4dc8fc | 8399 | u32 blocksize = root->sectorsize; |
1ae39938 | 8400 | int async_submit = 0; |
5f4dc8fc CR |
8401 | int nr_sectors; |
8402 | int ret; | |
8403 | int i; | |
e65e1535 | 8404 | |
4f024f37 | 8405 | map_length = orig_bio->bi_iter.bi_size; |
37226b21 MC |
8406 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
8407 | start_sector << 9, &map_length, NULL, 0); | |
7a5c3c9b | 8408 | if (ret) |
e65e1535 | 8409 | return -EIO; |
facc8a22 | 8410 | |
4f024f37 | 8411 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8412 | bio = orig_bio; |
c1dc0896 | 8413 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8414 | goto submit; |
8415 | } | |
8416 | ||
53b381b3 | 8417 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8418 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8419 | async_submit = 0; |
8420 | else | |
8421 | async_submit = 1; | |
8422 | ||
02f57c7a JB |
8423 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8424 | if (!bio) | |
8425 | return -ENOMEM; | |
7a5c3c9b | 8426 | |
1eff9d32 | 8427 | bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf); |
02f57c7a JB |
8428 | bio->bi_private = dip; |
8429 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8430 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8431 | atomic_inc(&dip->pending_bios); |
8432 | ||
e65e1535 | 8433 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
5f4dc8fc CR |
8434 | nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len); |
8435 | i = 0; | |
8436 | next_block: | |
8437 | if (unlikely(map_length < submit_len + blocksize || | |
8438 | bio_add_page(bio, bvec->bv_page, blocksize, | |
8439 | bvec->bv_offset + (i * blocksize)) < blocksize)) { | |
e65e1535 MX |
8440 | /* |
8441 | * inc the count before we submit the bio so | |
8442 | * we know the end IO handler won't happen before | |
8443 | * we inc the count. Otherwise, the dip might get freed | |
8444 | * before we're done setting it up | |
8445 | */ | |
8446 | atomic_inc(&dip->pending_bios); | |
81a75f67 | 8447 | ret = __btrfs_submit_dio_bio(bio, inode, |
e65e1535 | 8448 | file_offset, skip_sum, |
c329861d | 8449 | async_submit); |
e65e1535 MX |
8450 | if (ret) { |
8451 | bio_put(bio); | |
8452 | atomic_dec(&dip->pending_bios); | |
8453 | goto out_err; | |
8454 | } | |
8455 | ||
e65e1535 MX |
8456 | start_sector += submit_len >> 9; |
8457 | file_offset += submit_len; | |
8458 | ||
8459 | submit_len = 0; | |
e65e1535 MX |
8460 | |
8461 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8462 | start_sector, GFP_NOFS); | |
8463 | if (!bio) | |
8464 | goto out_err; | |
1eff9d32 | 8465 | bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf); |
e65e1535 MX |
8466 | bio->bi_private = dip; |
8467 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8468 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8469 | |
4f024f37 | 8470 | map_length = orig_bio->bi_iter.bi_size; |
37226b21 | 8471 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
3ec706c8 | 8472 | start_sector << 9, |
e65e1535 MX |
8473 | &map_length, NULL, 0); |
8474 | if (ret) { | |
8475 | bio_put(bio); | |
8476 | goto out_err; | |
8477 | } | |
5f4dc8fc CR |
8478 | |
8479 | goto next_block; | |
e65e1535 | 8480 | } else { |
5f4dc8fc CR |
8481 | submit_len += blocksize; |
8482 | if (--nr_sectors) { | |
8483 | i++; | |
8484 | goto next_block; | |
8485 | } | |
e65e1535 MX |
8486 | bvec++; |
8487 | } | |
8488 | } | |
8489 | ||
02f57c7a | 8490 | submit: |
81a75f67 | 8491 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
c329861d | 8492 | async_submit); |
e65e1535 MX |
8493 | if (!ret) |
8494 | return 0; | |
8495 | ||
8496 | bio_put(bio); | |
8497 | out_err: | |
8498 | dip->errors = 1; | |
8499 | /* | |
8500 | * before atomic variable goto zero, we must | |
8501 | * make sure dip->errors is perceived to be set. | |
8502 | */ | |
4e857c58 | 8503 | smp_mb__before_atomic(); |
e65e1535 MX |
8504 | if (atomic_dec_and_test(&dip->pending_bios)) |
8505 | bio_io_error(dip->orig_bio); | |
8506 | ||
8507 | /* bio_end_io() will handle error, so we needn't return it */ | |
8508 | return 0; | |
8509 | } | |
8510 | ||
8a4c1e42 MC |
8511 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
8512 | loff_t file_offset) | |
4b46fce2 | 8513 | { |
61de718f FM |
8514 | struct btrfs_dio_private *dip = NULL; |
8515 | struct bio *io_bio = NULL; | |
23ea8e5a | 8516 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8517 | int skip_sum; |
8a4c1e42 | 8518 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
4b46fce2 JB |
8519 | int ret = 0; |
8520 | ||
8521 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8522 | ||
9be3395b | 8523 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8524 | if (!io_bio) { |
8525 | ret = -ENOMEM; | |
8526 | goto free_ordered; | |
8527 | } | |
8528 | ||
c1dc0896 | 8529 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8530 | if (!dip) { |
8531 | ret = -ENOMEM; | |
61de718f | 8532 | goto free_ordered; |
4b46fce2 | 8533 | } |
4b46fce2 | 8534 | |
9be3395b | 8535 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8536 | dip->inode = inode; |
8537 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8538 | dip->bytes = dio_bio->bi_iter.bi_size; |
8539 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8540 | io_bio->bi_private = dip; |
9be3395b CM |
8541 | dip->orig_bio = io_bio; |
8542 | dip->dio_bio = dio_bio; | |
e65e1535 | 8543 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8544 | btrfs_bio = btrfs_io_bio(io_bio); |
8545 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8546 | |
c1dc0896 | 8547 | if (write) { |
9be3395b | 8548 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8549 | } else { |
9be3395b | 8550 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8551 | dip->subio_endio = btrfs_subio_endio_read; |
8552 | } | |
4b46fce2 | 8553 | |
f28a4928 FM |
8554 | /* |
8555 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8556 | * even if we fail to submit a bio, because in such case we do the | |
8557 | * corresponding error handling below and it must not be done a second | |
8558 | * time by btrfs_direct_IO(). | |
8559 | */ | |
8560 | if (write) { | |
8561 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8562 | ||
8563 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8564 | dip->bytes; | |
8565 | dio_data->unsubmitted_oe_range_start = | |
8566 | dio_data->unsubmitted_oe_range_end; | |
8567 | } | |
8568 | ||
81a75f67 | 8569 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
e65e1535 | 8570 | if (!ret) |
eaf25d93 | 8571 | return; |
9be3395b | 8572 | |
23ea8e5a MX |
8573 | if (btrfs_bio->end_io) |
8574 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8575 | |
4b46fce2 JB |
8576 | free_ordered: |
8577 | /* | |
61de718f FM |
8578 | * If we arrived here it means either we failed to submit the dip |
8579 | * or we either failed to clone the dio_bio or failed to allocate the | |
8580 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8581 | * call bio_endio against our io_bio so that we get proper resource | |
8582 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8583 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8584 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8585 | */ |
61de718f | 8586 | if (io_bio && dip) { |
4246a0b6 CH |
8587 | io_bio->bi_error = -EIO; |
8588 | bio_endio(io_bio); | |
61de718f FM |
8589 | /* |
8590 | * The end io callbacks free our dip, do the final put on io_bio | |
8591 | * and all the cleanup and final put for dio_bio (through | |
8592 | * dio_end_io()). | |
8593 | */ | |
8594 | dip = NULL; | |
8595 | io_bio = NULL; | |
8596 | } else { | |
14543774 FM |
8597 | if (write) |
8598 | btrfs_endio_direct_write_update_ordered(inode, | |
8599 | file_offset, | |
8600 | dio_bio->bi_iter.bi_size, | |
8601 | 0); | |
8602 | else | |
61de718f FM |
8603 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8604 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8605 | |
4246a0b6 | 8606 | dio_bio->bi_error = -EIO; |
61de718f FM |
8607 | /* |
8608 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8609 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8610 | */ | |
8611 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8612 | } |
61de718f FM |
8613 | if (io_bio) |
8614 | bio_put(io_bio); | |
8615 | kfree(dip); | |
4b46fce2 JB |
8616 | } |
8617 | ||
6f673763 | 8618 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
28060d5d | 8619 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8620 | { |
8621 | int seg; | |
a1b75f7d | 8622 | int i; |
5a5f79b5 CM |
8623 | unsigned blocksize_mask = root->sectorsize - 1; |
8624 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8625 | |
8626 | if (offset & blocksize_mask) | |
8627 | goto out; | |
8628 | ||
28060d5d AV |
8629 | if (iov_iter_alignment(iter) & blocksize_mask) |
8630 | goto out; | |
a1b75f7d | 8631 | |
28060d5d | 8632 | /* If this is a write we don't need to check anymore */ |
6f673763 | 8633 | if (iov_iter_rw(iter) == WRITE) |
28060d5d AV |
8634 | return 0; |
8635 | /* | |
8636 | * Check to make sure we don't have duplicate iov_base's in this | |
8637 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8638 | * when reading back. | |
8639 | */ | |
8640 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8641 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8642 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8643 | goto out; |
8644 | } | |
5a5f79b5 CM |
8645 | } |
8646 | retval = 0; | |
8647 | out: | |
8648 | return retval; | |
8649 | } | |
eb838e73 | 8650 | |
c8b8e32d | 8651 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8652 | { |
4b46fce2 JB |
8653 | struct file *file = iocb->ki_filp; |
8654 | struct inode *inode = file->f_mapping->host; | |
50745b0a | 8655 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8656 | struct btrfs_dio_data dio_data = { 0 }; | |
c8b8e32d | 8657 | loff_t offset = iocb->ki_pos; |
0934856d | 8658 | size_t count = 0; |
2e60a51e | 8659 | int flags = 0; |
38851cc1 MX |
8660 | bool wakeup = true; |
8661 | bool relock = false; | |
0934856d | 8662 | ssize_t ret; |
4b46fce2 | 8663 | |
6f673763 | 8664 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
5a5f79b5 | 8665 | return 0; |
3f7c579c | 8666 | |
fe0f07d0 | 8667 | inode_dio_begin(inode); |
4e857c58 | 8668 | smp_mb__after_atomic(); |
38851cc1 | 8669 | |
0e267c44 | 8670 | /* |
41bd9ca4 MX |
8671 | * The generic stuff only does filemap_write_and_wait_range, which |
8672 | * isn't enough if we've written compressed pages to this area, so | |
8673 | * we need to flush the dirty pages again to make absolutely sure | |
8674 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8675 | */ |
a6cbcd4a | 8676 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8677 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8678 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8679 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8680 | offset + count - 1); | |
0e267c44 | 8681 | |
6f673763 | 8682 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8683 | /* |
8684 | * If the write DIO is beyond the EOF, we need update | |
8685 | * the isize, but it is protected by i_mutex. So we can | |
8686 | * not unlock the i_mutex at this case. | |
8687 | */ | |
8688 | if (offset + count <= inode->i_size) { | |
5955102c | 8689 | inode_unlock(inode); |
38851cc1 MX |
8690 | relock = true; |
8691 | } | |
7cf5b976 | 8692 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
0934856d | 8693 | if (ret) |
38851cc1 | 8694 | goto out; |
50745b0a | 8695 | dio_data.outstanding_extents = div64_u64(count + |
e1cbbfa5 JB |
8696 | BTRFS_MAX_EXTENT_SIZE - 1, |
8697 | BTRFS_MAX_EXTENT_SIZE); | |
8698 | ||
8699 | /* | |
8700 | * We need to know how many extents we reserved so that we can | |
8701 | * do the accounting properly if we go over the number we | |
8702 | * originally calculated. Abuse current->journal_info for this. | |
8703 | */ | |
50745b0a | 8704 | dio_data.reserve = round_up(count, root->sectorsize); |
f28a4928 FM |
8705 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8706 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8707 | current->journal_info = &dio_data; |
ee39b432 DS |
8708 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8709 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8710 | inode_dio_end(inode); |
38851cc1 MX |
8711 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8712 | wakeup = false; | |
0934856d MX |
8713 | } |
8714 | ||
17f8c842 OS |
8715 | ret = __blockdev_direct_IO(iocb, inode, |
8716 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
c8b8e32d | 8717 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8718 | btrfs_submit_direct, flags); |
6f673763 | 8719 | if (iov_iter_rw(iter) == WRITE) { |
e1cbbfa5 | 8720 | current->journal_info = NULL; |
ddba1bfc | 8721 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8722 | if (dio_data.reserve) |
7cf5b976 QW |
8723 | btrfs_delalloc_release_space(inode, offset, |
8724 | dio_data.reserve); | |
f28a4928 FM |
8725 | /* |
8726 | * On error we might have left some ordered extents | |
8727 | * without submitting corresponding bios for them, so | |
8728 | * cleanup them up to avoid other tasks getting them | |
8729 | * and waiting for them to complete forever. | |
8730 | */ | |
8731 | if (dio_data.unsubmitted_oe_range_start < | |
8732 | dio_data.unsubmitted_oe_range_end) | |
8733 | btrfs_endio_direct_write_update_ordered(inode, | |
8734 | dio_data.unsubmitted_oe_range_start, | |
8735 | dio_data.unsubmitted_oe_range_end - | |
8736 | dio_data.unsubmitted_oe_range_start, | |
8737 | 0); | |
ddba1bfc | 8738 | } else if (ret >= 0 && (size_t)ret < count) |
7cf5b976 QW |
8739 | btrfs_delalloc_release_space(inode, offset, |
8740 | count - (size_t)ret); | |
0934856d | 8741 | } |
38851cc1 | 8742 | out: |
2e60a51e | 8743 | if (wakeup) |
fe0f07d0 | 8744 | inode_dio_end(inode); |
38851cc1 | 8745 | if (relock) |
5955102c | 8746 | inode_lock(inode); |
0934856d MX |
8747 | |
8748 | return ret; | |
16432985 CM |
8749 | } |
8750 | ||
05dadc09 TI |
8751 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8752 | ||
1506fcc8 YS |
8753 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8754 | __u64 start, __u64 len) | |
8755 | { | |
05dadc09 TI |
8756 | int ret; |
8757 | ||
8758 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8759 | if (ret) | |
8760 | return ret; | |
8761 | ||
ec29ed5b | 8762 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8763 | } |
8764 | ||
a52d9a80 | 8765 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8766 | { |
d1310b2e CM |
8767 | struct extent_io_tree *tree; |
8768 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8769 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8770 | } |
1832a6d5 | 8771 | |
a52d9a80 | 8772 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8773 | { |
d1310b2e | 8774 | struct extent_io_tree *tree; |
be7bd730 JB |
8775 | struct inode *inode = page->mapping->host; |
8776 | int ret; | |
b888db2b CM |
8777 | |
8778 | if (current->flags & PF_MEMALLOC) { | |
8779 | redirty_page_for_writepage(wbc, page); | |
8780 | unlock_page(page); | |
8781 | return 0; | |
8782 | } | |
be7bd730 JB |
8783 | |
8784 | /* | |
8785 | * If we are under memory pressure we will call this directly from the | |
8786 | * VM, we need to make sure we have the inode referenced for the ordered | |
8787 | * extent. If not just return like we didn't do anything. | |
8788 | */ | |
8789 | if (!igrab(inode)) { | |
8790 | redirty_page_for_writepage(wbc, page); | |
8791 | return AOP_WRITEPAGE_ACTIVATE; | |
8792 | } | |
d1310b2e | 8793 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8794 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8795 | btrfs_add_delayed_iput(inode); | |
8796 | return ret; | |
9ebefb18 CM |
8797 | } |
8798 | ||
48a3b636 ES |
8799 | static int btrfs_writepages(struct address_space *mapping, |
8800 | struct writeback_control *wbc) | |
b293f02e | 8801 | { |
d1310b2e | 8802 | struct extent_io_tree *tree; |
771ed689 | 8803 | |
d1310b2e | 8804 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8805 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8806 | } | |
8807 | ||
3ab2fb5a CM |
8808 | static int |
8809 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8810 | struct list_head *pages, unsigned nr_pages) | |
8811 | { | |
d1310b2e CM |
8812 | struct extent_io_tree *tree; |
8813 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8814 | return extent_readpages(tree, mapping, pages, nr_pages, |
8815 | btrfs_get_extent); | |
8816 | } | |
e6dcd2dc | 8817 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8818 | { |
d1310b2e CM |
8819 | struct extent_io_tree *tree; |
8820 | struct extent_map_tree *map; | |
a52d9a80 | 8821 | int ret; |
8c2383c3 | 8822 | |
d1310b2e CM |
8823 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8824 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8825 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8826 | if (ret == 1) { |
8827 | ClearPagePrivate(page); | |
8828 | set_page_private(page, 0); | |
09cbfeaf | 8829 | put_page(page); |
39279cc3 | 8830 | } |
a52d9a80 | 8831 | return ret; |
39279cc3 CM |
8832 | } |
8833 | ||
e6dcd2dc CM |
8834 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8835 | { | |
98509cfc CM |
8836 | if (PageWriteback(page) || PageDirty(page)) |
8837 | return 0; | |
b335b003 | 8838 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8839 | } |
8840 | ||
d47992f8 LC |
8841 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8842 | unsigned int length) | |
39279cc3 | 8843 | { |
5fd02043 | 8844 | struct inode *inode = page->mapping->host; |
d1310b2e | 8845 | struct extent_io_tree *tree; |
e6dcd2dc | 8846 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8847 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8848 | u64 page_start = page_offset(page); |
09cbfeaf | 8849 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8850 | u64 start; |
8851 | u64 end; | |
131e404a | 8852 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8853 | |
8b62b72b CM |
8854 | /* |
8855 | * we have the page locked, so new writeback can't start, | |
8856 | * and the dirty bit won't be cleared while we are here. | |
8857 | * | |
8858 | * Wait for IO on this page so that we can safely clear | |
8859 | * the PagePrivate2 bit and do ordered accounting | |
8860 | */ | |
e6dcd2dc | 8861 | wait_on_page_writeback(page); |
8b62b72b | 8862 | |
5fd02043 | 8863 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8864 | if (offset) { |
8865 | btrfs_releasepage(page, GFP_NOFS); | |
8866 | return; | |
8867 | } | |
131e404a FDBM |
8868 | |
8869 | if (!inode_evicting) | |
ff13db41 | 8870 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8871 | again: |
8872 | start = page_start; | |
8873 | ordered = btrfs_lookup_ordered_range(inode, start, | |
8874 | page_end - start + 1); | |
e6dcd2dc | 8875 | if (ordered) { |
dbfdb6d1 | 8876 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8877 | /* |
8878 | * IO on this page will never be started, so we need | |
8879 | * to account for any ordered extents now | |
8880 | */ | |
131e404a | 8881 | if (!inode_evicting) |
dbfdb6d1 | 8882 | clear_extent_bit(tree, start, end, |
131e404a FDBM |
8883 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8884 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8885 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8886 | GFP_NOFS); | |
8b62b72b CM |
8887 | /* |
8888 | * whoever cleared the private bit is responsible | |
8889 | * for the finish_ordered_io | |
8890 | */ | |
77cef2ec JB |
8891 | if (TestClearPagePrivate2(page)) { |
8892 | struct btrfs_ordered_inode_tree *tree; | |
8893 | u64 new_len; | |
8894 | ||
8895 | tree = &BTRFS_I(inode)->ordered_tree; | |
8896 | ||
8897 | spin_lock_irq(&tree->lock); | |
8898 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8899 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8900 | if (new_len < ordered->truncated_len) |
8901 | ordered->truncated_len = new_len; | |
8902 | spin_unlock_irq(&tree->lock); | |
8903 | ||
8904 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8905 | start, |
8906 | end - start + 1, 1)) | |
77cef2ec | 8907 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8908 | } |
e6dcd2dc | 8909 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8910 | if (!inode_evicting) { |
8911 | cached_state = NULL; | |
dbfdb6d1 | 8912 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8913 | &cached_state); |
8914 | } | |
dbfdb6d1 CR |
8915 | |
8916 | start = end + 1; | |
8917 | if (start < page_end) | |
8918 | goto again; | |
131e404a FDBM |
8919 | } |
8920 | ||
b9d0b389 QW |
8921 | /* |
8922 | * Qgroup reserved space handler | |
8923 | * Page here will be either | |
8924 | * 1) Already written to disk | |
8925 | * In this case, its reserved space is released from data rsv map | |
8926 | * and will be freed by delayed_ref handler finally. | |
8927 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8928 | * space. | |
8929 | * 2) Not written to disk | |
8930 | * This means the reserved space should be freed here. | |
8931 | */ | |
09cbfeaf | 8932 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
131e404a FDBM |
8933 | if (!inode_evicting) { |
8934 | clear_extent_bit(tree, page_start, page_end, | |
8935 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8936 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8937 | EXTENT_DEFRAG, 1, 1, | |
8938 | &cached_state, GFP_NOFS); | |
8939 | ||
8940 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8941 | } |
e6dcd2dc | 8942 | |
4a096752 | 8943 | ClearPageChecked(page); |
9ad6b7bc | 8944 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8945 | ClearPagePrivate(page); |
8946 | set_page_private(page, 0); | |
09cbfeaf | 8947 | put_page(page); |
9ad6b7bc | 8948 | } |
39279cc3 CM |
8949 | } |
8950 | ||
9ebefb18 CM |
8951 | /* |
8952 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8953 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8954 | * be careful to check for EOF conditions here. We set the page up correctly | |
8955 | * for a written page which means we get ENOSPC checking when writing into | |
8956 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8957 | * support these features. | |
8958 | * | |
8959 | * We are not allowed to take the i_mutex here so we have to play games to | |
8960 | * protect against truncate races as the page could now be beyond EOF. Because | |
8961 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8962 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8963 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8964 | * unlock the page. | |
8965 | */ | |
c2ec175c | 8966 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8967 | { |
c2ec175c | 8968 | struct page *page = vmf->page; |
496ad9aa | 8969 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8970 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8971 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8972 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8973 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8974 | char *kaddr; |
8975 | unsigned long zero_start; | |
9ebefb18 | 8976 | loff_t size; |
1832a6d5 | 8977 | int ret; |
9998eb70 | 8978 | int reserved = 0; |
d0b7da88 | 8979 | u64 reserved_space; |
a52d9a80 | 8980 | u64 page_start; |
e6dcd2dc | 8981 | u64 page_end; |
d0b7da88 CR |
8982 | u64 end; |
8983 | ||
09cbfeaf | 8984 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8985 | |
b2b5ef5c | 8986 | sb_start_pagefault(inode->i_sb); |
df480633 | 8987 | page_start = page_offset(page); |
09cbfeaf | 8988 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8989 | end = page_end; |
df480633 | 8990 | |
d0b7da88 CR |
8991 | /* |
8992 | * Reserving delalloc space after obtaining the page lock can lead to | |
8993 | * deadlock. For example, if a dirty page is locked by this function | |
8994 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8995 | * dirty page write out, then the btrfs_writepage() function could | |
8996 | * end up waiting indefinitely to get a lock on the page currently | |
8997 | * being processed by btrfs_page_mkwrite() function. | |
8998 | */ | |
7cf5b976 | 8999 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
d0b7da88 | 9000 | reserved_space); |
9998eb70 | 9001 | if (!ret) { |
e41f941a | 9002 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
9003 | reserved = 1; |
9004 | } | |
56a76f82 NP |
9005 | if (ret) { |
9006 | if (ret == -ENOMEM) | |
9007 | ret = VM_FAULT_OOM; | |
9008 | else /* -ENOSPC, -EIO, etc */ | |
9009 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
9010 | if (reserved) |
9011 | goto out; | |
9012 | goto out_noreserve; | |
56a76f82 | 9013 | } |
1832a6d5 | 9014 | |
56a76f82 | 9015 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 9016 | again: |
9ebefb18 | 9017 | lock_page(page); |
9ebefb18 | 9018 | size = i_size_read(inode); |
a52d9a80 | 9019 | |
9ebefb18 | 9020 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 9021 | (page_start >= size)) { |
9ebefb18 CM |
9022 | /* page got truncated out from underneath us */ |
9023 | goto out_unlock; | |
9024 | } | |
e6dcd2dc CM |
9025 | wait_on_page_writeback(page); |
9026 | ||
ff13db41 | 9027 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
9028 | set_page_extent_mapped(page); |
9029 | ||
eb84ae03 CM |
9030 | /* |
9031 | * we can't set the delalloc bits if there are pending ordered | |
9032 | * extents. Drop our locks and wait for them to finish | |
9033 | */ | |
d0b7da88 | 9034 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
e6dcd2dc | 9035 | if (ordered) { |
2ac55d41 JB |
9036 | unlock_extent_cached(io_tree, page_start, page_end, |
9037 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 9038 | unlock_page(page); |
eb84ae03 | 9039 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
9040 | btrfs_put_ordered_extent(ordered); |
9041 | goto again; | |
9042 | } | |
9043 | ||
09cbfeaf | 9044 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
d0b7da88 | 9045 | reserved_space = round_up(size - page_start, root->sectorsize); |
09cbfeaf | 9046 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
9047 | end = page_start + reserved_space - 1; |
9048 | spin_lock(&BTRFS_I(inode)->lock); | |
9049 | BTRFS_I(inode)->outstanding_extents++; | |
9050 | spin_unlock(&BTRFS_I(inode)->lock); | |
9051 | btrfs_delalloc_release_space(inode, page_start, | |
09cbfeaf | 9052 | PAGE_SIZE - reserved_space); |
d0b7da88 CR |
9053 | } |
9054 | } | |
9055 | ||
fbf19087 JB |
9056 | /* |
9057 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
9058 | * if it was already dirty, so for space accounting reasons we need to | |
9059 | * clear any delalloc bits for the range we are fixing to save. There | |
9060 | * is probably a better way to do this, but for now keep consistent with | |
9061 | * prepare_pages in the normal write path. | |
9062 | */ | |
d0b7da88 | 9063 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9064 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9065 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9066 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9067 | |
d0b7da88 | 9068 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
ba8b04c1 | 9069 | &cached_state, 0); |
9ed74f2d | 9070 | if (ret) { |
2ac55d41 JB |
9071 | unlock_extent_cached(io_tree, page_start, page_end, |
9072 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9073 | ret = VM_FAULT_SIGBUS; |
9074 | goto out_unlock; | |
9075 | } | |
e6dcd2dc | 9076 | ret = 0; |
9ebefb18 CM |
9077 | |
9078 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9079 | if (page_start + PAGE_SIZE > size) |
9080 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9081 | else |
09cbfeaf | 9082 | zero_start = PAGE_SIZE; |
9ebefb18 | 9083 | |
09cbfeaf | 9084 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9085 | kaddr = kmap(page); |
09cbfeaf | 9086 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9087 | flush_dcache_page(page); |
9088 | kunmap(page); | |
9089 | } | |
247e743c | 9090 | ClearPageChecked(page); |
e6dcd2dc | 9091 | set_page_dirty(page); |
50a9b214 | 9092 | SetPageUptodate(page); |
5a3f23d5 | 9093 | |
257c62e1 CM |
9094 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
9095 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 9096 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9097 | |
2ac55d41 | 9098 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9099 | |
9100 | out_unlock: | |
b2b5ef5c JK |
9101 | if (!ret) { |
9102 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 9103 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9104 | } |
9ebefb18 | 9105 | unlock_page(page); |
1832a6d5 | 9106 | out: |
d0b7da88 | 9107 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
9998eb70 | 9108 | out_noreserve: |
b2b5ef5c | 9109 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
9110 | return ret; |
9111 | } | |
9112 | ||
a41ad394 | 9113 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
9114 | { |
9115 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 9116 | struct btrfs_block_rsv *rsv; |
a71754fc | 9117 | int ret = 0; |
3893e33b | 9118 | int err = 0; |
39279cc3 | 9119 | struct btrfs_trans_handle *trans; |
dbe674a9 | 9120 | u64 mask = root->sectorsize - 1; |
07127184 | 9121 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 9122 | |
0ef8b726 JB |
9123 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9124 | (u64)-1); | |
9125 | if (ret) | |
9126 | return ret; | |
39279cc3 | 9127 | |
fcb80c2a | 9128 | /* |
01327610 | 9129 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9130 | * 3 things going on here |
9131 | * | |
9132 | * 1) We need to reserve space for our orphan item and the space to | |
9133 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9134 | * orphan item because we didn't reserve space to remove it. | |
9135 | * | |
9136 | * 2) We need to reserve space to update our inode. | |
9137 | * | |
9138 | * 3) We need to have something to cache all the space that is going to | |
9139 | * be free'd up by the truncate operation, but also have some slack | |
9140 | * space reserved in case it uses space during the truncate (thank you | |
9141 | * very much snapshotting). | |
9142 | * | |
01327610 | 9143 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9144 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9145 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9146 | * doesn't end up using space reserved for updating the inode or |
9147 | * removing the orphan item. We also need to be able to stop the | |
9148 | * transaction and start a new one, which means we need to be able to | |
9149 | * update the inode several times, and we have no idea of knowing how | |
9150 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9151 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9152 | * Then there is the orphan item, which does indeed need to be held on |
9153 | * to for the whole operation, and we need nobody to touch this reserved | |
9154 | * space except the orphan code. | |
9155 | * | |
9156 | * So that leaves us with | |
9157 | * | |
9158 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9159 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9160 | * transaction reservation. | |
9161 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9162 | * updating the inode. | |
9163 | */ | |
66d8f3dd | 9164 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9165 | if (!rsv) |
9166 | return -ENOMEM; | |
4a338542 | 9167 | rsv->size = min_size; |
ca7e70f5 | 9168 | rsv->failfast = 1; |
f0cd846e | 9169 | |
907cbceb | 9170 | /* |
07127184 | 9171 | * 1 for the truncate slack space |
907cbceb JB |
9172 | * 1 for updating the inode. |
9173 | */ | |
f3fe820c | 9174 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9175 | if (IS_ERR(trans)) { |
9176 | err = PTR_ERR(trans); | |
9177 | goto out; | |
9178 | } | |
f0cd846e | 9179 | |
907cbceb JB |
9180 | /* Migrate the slack space for the truncate to our reserve */ |
9181 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
25d609f8 | 9182 | min_size, 0); |
fcb80c2a | 9183 | BUG_ON(ret); |
f0cd846e | 9184 | |
5dc562c5 JB |
9185 | /* |
9186 | * So if we truncate and then write and fsync we normally would just | |
9187 | * write the extents that changed, which is a problem if we need to | |
9188 | * first truncate that entire inode. So set this flag so we write out | |
9189 | * all of the extents in the inode to the sync log so we're completely | |
9190 | * safe. | |
9191 | */ | |
9192 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9193 | trans->block_rsv = rsv; |
907cbceb | 9194 | |
8082510e YZ |
9195 | while (1) { |
9196 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9197 | inode->i_size, | |
9198 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9199 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9200 | err = ret; |
8082510e | 9201 | break; |
3893e33b | 9202 | } |
39279cc3 | 9203 | |
fcb80c2a | 9204 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 9205 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9206 | if (ret) { |
9207 | err = ret; | |
9208 | break; | |
9209 | } | |
ca7e70f5 | 9210 | |
8082510e | 9211 | btrfs_end_transaction(trans, root); |
b53d3f5d | 9212 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
9213 | |
9214 | trans = btrfs_start_transaction(root, 2); | |
9215 | if (IS_ERR(trans)) { | |
9216 | ret = err = PTR_ERR(trans); | |
9217 | trans = NULL; | |
9218 | break; | |
9219 | } | |
9220 | ||
9221 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
25d609f8 | 9222 | rsv, min_size, 0); |
ca7e70f5 JB |
9223 | BUG_ON(ret); /* shouldn't happen */ |
9224 | trans->block_rsv = rsv; | |
8082510e YZ |
9225 | } |
9226 | ||
9227 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9228 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 9229 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
9230 | if (ret) |
9231 | err = ret; | |
8082510e YZ |
9232 | } |
9233 | ||
917c16b2 CM |
9234 | if (trans) { |
9235 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
9236 | ret = btrfs_update_inode(trans, root, inode); | |
9237 | if (ret && !err) | |
9238 | err = ret; | |
7b128766 | 9239 | |
7ad85bb7 | 9240 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 9241 | btrfs_btree_balance_dirty(root); |
917c16b2 | 9242 | } |
fcb80c2a JB |
9243 | out: |
9244 | btrfs_free_block_rsv(root, rsv); | |
9245 | ||
3893e33b JB |
9246 | if (ret && !err) |
9247 | err = ret; | |
a41ad394 | 9248 | |
3893e33b | 9249 | return err; |
39279cc3 CM |
9250 | } |
9251 | ||
d352ac68 CM |
9252 | /* |
9253 | * create a new subvolume directory/inode (helper for the ioctl). | |
9254 | */ | |
d2fb3437 | 9255 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9256 | struct btrfs_root *new_root, |
9257 | struct btrfs_root *parent_root, | |
9258 | u64 new_dirid) | |
39279cc3 | 9259 | { |
39279cc3 | 9260 | struct inode *inode; |
76dda93c | 9261 | int err; |
00e4e6b3 | 9262 | u64 index = 0; |
39279cc3 | 9263 | |
12fc9d09 FA |
9264 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9265 | new_dirid, new_dirid, | |
9266 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9267 | &index); | |
54aa1f4d | 9268 | if (IS_ERR(inode)) |
f46b5a66 | 9269 | return PTR_ERR(inode); |
39279cc3 CM |
9270 | inode->i_op = &btrfs_dir_inode_operations; |
9271 | inode->i_fop = &btrfs_dir_file_operations; | |
9272 | ||
bfe86848 | 9273 | set_nlink(inode, 1); |
dbe674a9 | 9274 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 9275 | unlock_new_inode(inode); |
3b96362c | 9276 | |
63541927 FDBM |
9277 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9278 | if (err) | |
9279 | btrfs_err(new_root->fs_info, | |
351fd353 | 9280 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9281 | new_root->root_key.objectid, err); |
9282 | ||
76dda93c | 9283 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9284 | |
76dda93c | 9285 | iput(inode); |
ce598979 | 9286 | return err; |
39279cc3 CM |
9287 | } |
9288 | ||
39279cc3 CM |
9289 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9290 | { | |
9291 | struct btrfs_inode *ei; | |
2ead6ae7 | 9292 | struct inode *inode; |
39279cc3 CM |
9293 | |
9294 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9295 | if (!ei) | |
9296 | return NULL; | |
2ead6ae7 YZ |
9297 | |
9298 | ei->root = NULL; | |
2ead6ae7 | 9299 | ei->generation = 0; |
15ee9bc7 | 9300 | ei->last_trans = 0; |
257c62e1 | 9301 | ei->last_sub_trans = 0; |
e02119d5 | 9302 | ei->logged_trans = 0; |
2ead6ae7 | 9303 | ei->delalloc_bytes = 0; |
47059d93 | 9304 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9305 | ei->disk_i_size = 0; |
9306 | ei->flags = 0; | |
7709cde3 | 9307 | ei->csum_bytes = 0; |
2ead6ae7 | 9308 | ei->index_cnt = (u64)-1; |
67de1176 | 9309 | ei->dir_index = 0; |
2ead6ae7 | 9310 | ei->last_unlink_trans = 0; |
46d8bc34 | 9311 | ei->last_log_commit = 0; |
8089fe62 | 9312 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9313 | |
9e0baf60 JB |
9314 | spin_lock_init(&ei->lock); |
9315 | ei->outstanding_extents = 0; | |
9316 | ei->reserved_extents = 0; | |
2ead6ae7 | 9317 | |
72ac3c0d | 9318 | ei->runtime_flags = 0; |
261507a0 | 9319 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9320 | |
16cdcec7 MX |
9321 | ei->delayed_node = NULL; |
9322 | ||
9cc97d64 | 9323 | ei->i_otime.tv_sec = 0; |
9324 | ei->i_otime.tv_nsec = 0; | |
9325 | ||
2ead6ae7 | 9326 | inode = &ei->vfs_inode; |
a8067e02 | 9327 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
9328 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
9329 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
9330 | ei->io_tree.track_uptodate = 1; |
9331 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9332 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9333 | mutex_init(&ei->log_mutex); |
f248679e | 9334 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9335 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9336 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9337 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9338 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9339 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9340 | |
9341 | return inode; | |
39279cc3 CM |
9342 | } |
9343 | ||
aaedb55b JB |
9344 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9345 | void btrfs_test_destroy_inode(struct inode *inode) | |
9346 | { | |
9347 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
9348 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9349 | } | |
9350 | #endif | |
9351 | ||
fa0d7e3d NP |
9352 | static void btrfs_i_callback(struct rcu_head *head) |
9353 | { | |
9354 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9355 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9356 | } | |
9357 | ||
39279cc3 CM |
9358 | void btrfs_destroy_inode(struct inode *inode) |
9359 | { | |
e6dcd2dc | 9360 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9361 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9362 | ||
b3d9b7a3 | 9363 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9364 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9365 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9366 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
9367 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
9368 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 9369 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9370 | |
a6dbd429 JB |
9371 | /* |
9372 | * This can happen where we create an inode, but somebody else also | |
9373 | * created the same inode and we need to destroy the one we already | |
9374 | * created. | |
9375 | */ | |
9376 | if (!root) | |
9377 | goto free; | |
9378 | ||
8a35d95f JB |
9379 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9380 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 9381 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 9382 | btrfs_ino(inode)); |
8a35d95f | 9383 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9384 | } |
7b128766 | 9385 | |
d397712b | 9386 | while (1) { |
e6dcd2dc CM |
9387 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9388 | if (!ordered) | |
9389 | break; | |
9390 | else { | |
c2cf52eb | 9391 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 9392 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
9393 | btrfs_remove_ordered_extent(inode, ordered); |
9394 | btrfs_put_ordered_extent(ordered); | |
9395 | btrfs_put_ordered_extent(ordered); | |
9396 | } | |
9397 | } | |
56fa9d07 | 9398 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9399 | inode_tree_del(inode); |
5b21f2ed | 9400 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9401 | free: |
fa0d7e3d | 9402 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9403 | } |
9404 | ||
45321ac5 | 9405 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9406 | { |
9407 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9408 | |
6379ef9f NA |
9409 | if (root == NULL) |
9410 | return 1; | |
9411 | ||
fa6ac876 | 9412 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9413 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9414 | return 1; |
76dda93c | 9415 | else |
45321ac5 | 9416 | return generic_drop_inode(inode); |
76dda93c YZ |
9417 | } |
9418 | ||
0ee0fda0 | 9419 | static void init_once(void *foo) |
39279cc3 CM |
9420 | { |
9421 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9422 | ||
9423 | inode_init_once(&ei->vfs_inode); | |
9424 | } | |
9425 | ||
9426 | void btrfs_destroy_cachep(void) | |
9427 | { | |
8c0a8537 KS |
9428 | /* |
9429 | * Make sure all delayed rcu free inodes are flushed before we | |
9430 | * destroy cache. | |
9431 | */ | |
9432 | rcu_barrier(); | |
5598e900 KM |
9433 | kmem_cache_destroy(btrfs_inode_cachep); |
9434 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9435 | kmem_cache_destroy(btrfs_transaction_cachep); | |
9436 | kmem_cache_destroy(btrfs_path_cachep); | |
9437 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9438 | } |
9439 | ||
9440 | int btrfs_init_cachep(void) | |
9441 | { | |
837e1972 | 9442 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9443 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9444 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9445 | init_once); | |
39279cc3 CM |
9446 | if (!btrfs_inode_cachep) |
9447 | goto fail; | |
9601e3f6 | 9448 | |
837e1972 | 9449 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9450 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9451 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9452 | if (!btrfs_trans_handle_cachep) |
9453 | goto fail; | |
9601e3f6 | 9454 | |
837e1972 | 9455 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 | 9456 | sizeof(struct btrfs_transaction), 0, |
fba4b697 | 9457 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9458 | if (!btrfs_transaction_cachep) |
9459 | goto fail; | |
9601e3f6 | 9460 | |
837e1972 | 9461 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9462 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9463 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9464 | if (!btrfs_path_cachep) |
9465 | goto fail; | |
9601e3f6 | 9466 | |
837e1972 | 9467 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9468 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9469 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9470 | if (!btrfs_free_space_cachep) |
9471 | goto fail; | |
9472 | ||
39279cc3 CM |
9473 | return 0; |
9474 | fail: | |
9475 | btrfs_destroy_cachep(); | |
9476 | return -ENOMEM; | |
9477 | } | |
9478 | ||
9479 | static int btrfs_getattr(struct vfsmount *mnt, | |
9480 | struct dentry *dentry, struct kstat *stat) | |
9481 | { | |
df0af1a5 | 9482 | u64 delalloc_bytes; |
2b0143b5 | 9483 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9484 | u32 blocksize = inode->i_sb->s_blocksize; |
9485 | ||
39279cc3 | 9486 | generic_fillattr(inode, stat); |
0ee5dc67 | 9487 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9488 | |
9489 | spin_lock(&BTRFS_I(inode)->lock); | |
9490 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9491 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9492 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9493 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9494 | return 0; |
9495 | } | |
9496 | ||
cdd1fedf DF |
9497 | static int btrfs_rename_exchange(struct inode *old_dir, |
9498 | struct dentry *old_dentry, | |
9499 | struct inode *new_dir, | |
9500 | struct dentry *new_dentry) | |
9501 | { | |
9502 | struct btrfs_trans_handle *trans; | |
9503 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9504 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9505 | struct inode *new_inode = new_dentry->d_inode; | |
9506 | struct inode *old_inode = old_dentry->d_inode; | |
9507 | struct timespec ctime = CURRENT_TIME; | |
9508 | struct dentry *parent; | |
9509 | u64 old_ino = btrfs_ino(old_inode); | |
9510 | u64 new_ino = btrfs_ino(new_inode); | |
9511 | u64 old_idx = 0; | |
9512 | u64 new_idx = 0; | |
9513 | u64 root_objectid; | |
9514 | int ret; | |
86e8aa0e FM |
9515 | bool root_log_pinned = false; |
9516 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9517 | |
9518 | /* we only allow rename subvolume link between subvolumes */ | |
9519 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9520 | return -EXDEV; | |
9521 | ||
9522 | /* close the race window with snapshot create/destroy ioctl */ | |
9523 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9524 | down_read(&root->fs_info->subvol_sem); | |
9525 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9526 | down_read(&dest->fs_info->subvol_sem); | |
9527 | ||
9528 | /* | |
9529 | * We want to reserve the absolute worst case amount of items. So if | |
9530 | * both inodes are subvols and we need to unlink them then that would | |
9531 | * require 4 item modifications, but if they are both normal inodes it | |
9532 | * would require 5 item modifications, so we'll assume their normal | |
9533 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9534 | * should cover the worst case number of items we'll modify. | |
9535 | */ | |
9536 | trans = btrfs_start_transaction(root, 12); | |
9537 | if (IS_ERR(trans)) { | |
9538 | ret = PTR_ERR(trans); | |
9539 | goto out_notrans; | |
9540 | } | |
9541 | ||
9542 | /* | |
9543 | * We need to find a free sequence number both in the source and | |
9544 | * in the destination directory for the exchange. | |
9545 | */ | |
9546 | ret = btrfs_set_inode_index(new_dir, &old_idx); | |
9547 | if (ret) | |
9548 | goto out_fail; | |
9549 | ret = btrfs_set_inode_index(old_dir, &new_idx); | |
9550 | if (ret) | |
9551 | goto out_fail; | |
9552 | ||
9553 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9554 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9555 | ||
9556 | /* Reference for the source. */ | |
9557 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9558 | /* force full log commit if subvolume involved. */ | |
9559 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9560 | } else { | |
376e5a57 FM |
9561 | btrfs_pin_log_trans(root); |
9562 | root_log_pinned = true; | |
cdd1fedf DF |
9563 | ret = btrfs_insert_inode_ref(trans, dest, |
9564 | new_dentry->d_name.name, | |
9565 | new_dentry->d_name.len, | |
9566 | old_ino, | |
9567 | btrfs_ino(new_dir), old_idx); | |
9568 | if (ret) | |
9569 | goto out_fail; | |
cdd1fedf DF |
9570 | } |
9571 | ||
9572 | /* And now for the dest. */ | |
9573 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9574 | /* force full log commit if subvolume involved. */ | |
9575 | btrfs_set_log_full_commit(dest->fs_info, trans); | |
9576 | } else { | |
376e5a57 FM |
9577 | btrfs_pin_log_trans(dest); |
9578 | dest_log_pinned = true; | |
cdd1fedf DF |
9579 | ret = btrfs_insert_inode_ref(trans, root, |
9580 | old_dentry->d_name.name, | |
9581 | old_dentry->d_name.len, | |
9582 | new_ino, | |
9583 | btrfs_ino(old_dir), new_idx); | |
9584 | if (ret) | |
9585 | goto out_fail; | |
cdd1fedf DF |
9586 | } |
9587 | ||
9588 | /* Update inode version and ctime/mtime. */ | |
9589 | inode_inc_iversion(old_dir); | |
9590 | inode_inc_iversion(new_dir); | |
9591 | inode_inc_iversion(old_inode); | |
9592 | inode_inc_iversion(new_inode); | |
9593 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9594 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9595 | old_inode->i_ctime = ctime; | |
9596 | new_inode->i_ctime = ctime; | |
9597 | ||
9598 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
9599 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9600 | btrfs_record_unlink_dir(trans, new_dir, new_inode, 1); | |
9601 | } | |
9602 | ||
9603 | /* src is a subvolume */ | |
9604 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9605 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9606 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9607 | root_objectid, | |
9608 | old_dentry->d_name.name, | |
9609 | old_dentry->d_name.len); | |
9610 | } else { /* src is an inode */ | |
9611 | ret = __btrfs_unlink_inode(trans, root, old_dir, | |
9612 | old_dentry->d_inode, | |
9613 | old_dentry->d_name.name, | |
9614 | old_dentry->d_name.len); | |
9615 | if (!ret) | |
9616 | ret = btrfs_update_inode(trans, root, old_inode); | |
9617 | } | |
9618 | if (ret) { | |
66642832 | 9619 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9620 | goto out_fail; |
9621 | } | |
9622 | ||
9623 | /* dest is a subvolume */ | |
9624 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9625 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9626 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9627 | root_objectid, | |
9628 | new_dentry->d_name.name, | |
9629 | new_dentry->d_name.len); | |
9630 | } else { /* dest is an inode */ | |
9631 | ret = __btrfs_unlink_inode(trans, dest, new_dir, | |
9632 | new_dentry->d_inode, | |
9633 | new_dentry->d_name.name, | |
9634 | new_dentry->d_name.len); | |
9635 | if (!ret) | |
9636 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9637 | } | |
9638 | if (ret) { | |
66642832 | 9639 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9640 | goto out_fail; |
9641 | } | |
9642 | ||
9643 | ret = btrfs_add_link(trans, new_dir, old_inode, | |
9644 | new_dentry->d_name.name, | |
9645 | new_dentry->d_name.len, 0, old_idx); | |
9646 | if (ret) { | |
66642832 | 9647 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9648 | goto out_fail; |
9649 | } | |
9650 | ||
9651 | ret = btrfs_add_link(trans, old_dir, new_inode, | |
9652 | old_dentry->d_name.name, | |
9653 | old_dentry->d_name.len, 0, new_idx); | |
9654 | if (ret) { | |
66642832 | 9655 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9656 | goto out_fail; |
9657 | } | |
9658 | ||
9659 | if (old_inode->i_nlink == 1) | |
9660 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9661 | if (new_inode->i_nlink == 1) | |
9662 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9663 | ||
86e8aa0e | 9664 | if (root_log_pinned) { |
cdd1fedf DF |
9665 | parent = new_dentry->d_parent; |
9666 | btrfs_log_new_name(trans, old_inode, old_dir, parent); | |
9667 | btrfs_end_log_trans(root); | |
86e8aa0e | 9668 | root_log_pinned = false; |
cdd1fedf | 9669 | } |
86e8aa0e | 9670 | if (dest_log_pinned) { |
cdd1fedf DF |
9671 | parent = old_dentry->d_parent; |
9672 | btrfs_log_new_name(trans, new_inode, new_dir, parent); | |
9673 | btrfs_end_log_trans(dest); | |
86e8aa0e | 9674 | dest_log_pinned = false; |
cdd1fedf DF |
9675 | } |
9676 | out_fail: | |
86e8aa0e FM |
9677 | /* |
9678 | * If we have pinned a log and an error happened, we unpin tasks | |
9679 | * trying to sync the log and force them to fallback to a transaction | |
9680 | * commit if the log currently contains any of the inodes involved in | |
9681 | * this rename operation (to ensure we do not persist a log with an | |
9682 | * inconsistent state for any of these inodes or leading to any | |
9683 | * inconsistencies when replayed). If the transaction was aborted, the | |
9684 | * abortion reason is propagated to userspace when attempting to commit | |
9685 | * the transaction. If the log does not contain any of these inodes, we | |
9686 | * allow the tasks to sync it. | |
9687 | */ | |
9688 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
9689 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || | |
9690 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || | |
9691 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || | |
9692 | (new_inode && | |
9693 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) | |
9694 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9695 | ||
9696 | if (root_log_pinned) { | |
9697 | btrfs_end_log_trans(root); | |
9698 | root_log_pinned = false; | |
9699 | } | |
9700 | if (dest_log_pinned) { | |
9701 | btrfs_end_log_trans(dest); | |
9702 | dest_log_pinned = false; | |
9703 | } | |
9704 | } | |
cdd1fedf DF |
9705 | ret = btrfs_end_transaction(trans, root); |
9706 | out_notrans: | |
9707 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9708 | up_read(&dest->fs_info->subvol_sem); | |
9709 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9710 | up_read(&root->fs_info->subvol_sem); | |
9711 | ||
9712 | return ret; | |
9713 | } | |
9714 | ||
9715 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9716 | struct btrfs_root *root, | |
9717 | struct inode *dir, | |
9718 | struct dentry *dentry) | |
9719 | { | |
9720 | int ret; | |
9721 | struct inode *inode; | |
9722 | u64 objectid; | |
9723 | u64 index; | |
9724 | ||
9725 | ret = btrfs_find_free_ino(root, &objectid); | |
9726 | if (ret) | |
9727 | return ret; | |
9728 | ||
9729 | inode = btrfs_new_inode(trans, root, dir, | |
9730 | dentry->d_name.name, | |
9731 | dentry->d_name.len, | |
9732 | btrfs_ino(dir), | |
9733 | objectid, | |
9734 | S_IFCHR | WHITEOUT_MODE, | |
9735 | &index); | |
9736 | ||
9737 | if (IS_ERR(inode)) { | |
9738 | ret = PTR_ERR(inode); | |
9739 | return ret; | |
9740 | } | |
9741 | ||
9742 | inode->i_op = &btrfs_special_inode_operations; | |
9743 | init_special_inode(inode, inode->i_mode, | |
9744 | WHITEOUT_DEV); | |
9745 | ||
9746 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9747 | &dentry->d_name); | |
9748 | if (ret) | |
c9901618 | 9749 | goto out; |
cdd1fedf DF |
9750 | |
9751 | ret = btrfs_add_nondir(trans, dir, dentry, | |
9752 | inode, 0, index); | |
9753 | if (ret) | |
c9901618 | 9754 | goto out; |
cdd1fedf DF |
9755 | |
9756 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9757 | out: |
cdd1fedf | 9758 | unlock_new_inode(inode); |
c9901618 FM |
9759 | if (ret) |
9760 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9761 | iput(inode); |
9762 | ||
c9901618 | 9763 | return ret; |
cdd1fedf DF |
9764 | } |
9765 | ||
d397712b | 9766 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9767 | struct inode *new_dir, struct dentry *new_dentry, |
9768 | unsigned int flags) | |
39279cc3 CM |
9769 | { |
9770 | struct btrfs_trans_handle *trans; | |
5062af35 | 9771 | unsigned int trans_num_items; |
39279cc3 | 9772 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9773 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9774 | struct inode *new_inode = d_inode(new_dentry); |
9775 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9776 | u64 index = 0; |
4df27c4d | 9777 | u64 root_objectid; |
39279cc3 | 9778 | int ret; |
33345d01 | 9779 | u64 old_ino = btrfs_ino(old_inode); |
3dc9e8f7 | 9780 | bool log_pinned = false; |
39279cc3 | 9781 | |
33345d01 | 9782 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9783 | return -EPERM; |
9784 | ||
4df27c4d | 9785 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9786 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9787 | return -EXDEV; |
9788 | ||
33345d01 LZ |
9789 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9790 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9791 | return -ENOTEMPTY; |
5f39d397 | 9792 | |
4df27c4d YZ |
9793 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9794 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9795 | return -ENOTEMPTY; | |
9c52057c CM |
9796 | |
9797 | ||
9798 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9799 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9800 | new_dentry->d_name.name, |
9801 | new_dentry->d_name.len); | |
9802 | ||
9803 | if (ret) { | |
9804 | if (ret == -EEXIST) { | |
9805 | /* we shouldn't get | |
9806 | * eexist without a new_inode */ | |
fae7f21c | 9807 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9808 | return ret; |
9809 | } | |
9810 | } else { | |
9811 | /* maybe -EOVERFLOW */ | |
9812 | return ret; | |
9813 | } | |
9814 | } | |
9815 | ret = 0; | |
9816 | ||
5a3f23d5 | 9817 | /* |
8d875f95 CM |
9818 | * we're using rename to replace one file with another. Start IO on it |
9819 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9820 | */ |
8d875f95 | 9821 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9822 | filemap_flush(old_inode->i_mapping); |
9823 | ||
76dda93c | 9824 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9825 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9826 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9827 | /* |
9828 | * We want to reserve the absolute worst case amount of items. So if | |
9829 | * both inodes are subvols and we need to unlink them then that would | |
9830 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9831 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9832 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9833 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9834 | * If our rename has the whiteout flag, we need more 5 units for the |
9835 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9836 | * when selinux is enabled). | |
a22285a6 | 9837 | */ |
5062af35 FM |
9838 | trans_num_items = 11; |
9839 | if (flags & RENAME_WHITEOUT) | |
9840 | trans_num_items += 5; | |
9841 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9842 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9843 | ret = PTR_ERR(trans); |
9844 | goto out_notrans; | |
9845 | } | |
76dda93c | 9846 | |
4df27c4d YZ |
9847 | if (dest != root) |
9848 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9849 | |
a5719521 YZ |
9850 | ret = btrfs_set_inode_index(new_dir, &index); |
9851 | if (ret) | |
9852 | goto out_fail; | |
5a3f23d5 | 9853 | |
67de1176 | 9854 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9855 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9856 | /* force full log commit if subvolume involved. */ |
995946dd | 9857 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9858 | } else { |
c4aba954 FM |
9859 | btrfs_pin_log_trans(root); |
9860 | log_pinned = true; | |
a5719521 YZ |
9861 | ret = btrfs_insert_inode_ref(trans, dest, |
9862 | new_dentry->d_name.name, | |
9863 | new_dentry->d_name.len, | |
33345d01 LZ |
9864 | old_ino, |
9865 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9866 | if (ret) |
9867 | goto out_fail; | |
4df27c4d | 9868 | } |
5a3f23d5 | 9869 | |
0c4d2d95 JB |
9870 | inode_inc_iversion(old_dir); |
9871 | inode_inc_iversion(new_dir); | |
9872 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9873 | old_dir->i_ctime = old_dir->i_mtime = |
9874 | new_dir->i_ctime = new_dir->i_mtime = | |
9875 | old_inode->i_ctime = current_fs_time(old_dir->i_sb); | |
5f39d397 | 9876 | |
12fcfd22 CM |
9877 | if (old_dentry->d_parent != new_dentry->d_parent) |
9878 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9879 | ||
33345d01 | 9880 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9881 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9882 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9883 | old_dentry->d_name.name, | |
9884 | old_dentry->d_name.len); | |
9885 | } else { | |
92986796 | 9886 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9887 | d_inode(old_dentry), |
92986796 AV |
9888 | old_dentry->d_name.name, |
9889 | old_dentry->d_name.len); | |
9890 | if (!ret) | |
9891 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9892 | } |
79787eaa | 9893 | if (ret) { |
66642832 | 9894 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9895 | goto out_fail; |
9896 | } | |
39279cc3 CM |
9897 | |
9898 | if (new_inode) { | |
0c4d2d95 | 9899 | inode_inc_iversion(new_inode); |
04b285f3 | 9900 | new_inode->i_ctime = current_fs_time(new_inode->i_sb); |
33345d01 | 9901 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9902 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9903 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9904 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9905 | root_objectid, | |
9906 | new_dentry->d_name.name, | |
9907 | new_dentry->d_name.len); | |
9908 | BUG_ON(new_inode->i_nlink == 0); | |
9909 | } else { | |
9910 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9911 | d_inode(new_dentry), |
4df27c4d YZ |
9912 | new_dentry->d_name.name, |
9913 | new_dentry->d_name.len); | |
9914 | } | |
4ef31a45 | 9915 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9916 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa | 9917 | if (ret) { |
66642832 | 9918 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9919 | goto out_fail; |
9920 | } | |
39279cc3 | 9921 | } |
aec7477b | 9922 | |
4df27c4d YZ |
9923 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9924 | new_dentry->d_name.name, | |
a5719521 | 9925 | new_dentry->d_name.len, 0, index); |
79787eaa | 9926 | if (ret) { |
66642832 | 9927 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9928 | goto out_fail; |
9929 | } | |
39279cc3 | 9930 | |
67de1176 MX |
9931 | if (old_inode->i_nlink == 1) |
9932 | BTRFS_I(old_inode)->dir_index = index; | |
9933 | ||
3dc9e8f7 | 9934 | if (log_pinned) { |
10d9f309 | 9935 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 9936 | |
6a912213 | 9937 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d | 9938 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9939 | log_pinned = false; |
4df27c4d | 9940 | } |
cdd1fedf DF |
9941 | |
9942 | if (flags & RENAME_WHITEOUT) { | |
9943 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9944 | old_dentry); | |
9945 | ||
9946 | if (ret) { | |
66642832 | 9947 | btrfs_abort_transaction(trans, ret); |
cdd1fedf DF |
9948 | goto out_fail; |
9949 | } | |
4df27c4d | 9950 | } |
39279cc3 | 9951 | out_fail: |
3dc9e8f7 FM |
9952 | /* |
9953 | * If we have pinned the log and an error happened, we unpin tasks | |
9954 | * trying to sync the log and force them to fallback to a transaction | |
9955 | * commit if the log currently contains any of the inodes involved in | |
9956 | * this rename operation (to ensure we do not persist a log with an | |
9957 | * inconsistent state for any of these inodes or leading to any | |
9958 | * inconsistencies when replayed). If the transaction was aborted, the | |
9959 | * abortion reason is propagated to userspace when attempting to commit | |
9960 | * the transaction. If the log does not contain any of these inodes, we | |
9961 | * allow the tasks to sync it. | |
9962 | */ | |
9963 | if (ret && log_pinned) { | |
9964 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || | |
9965 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || | |
9966 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || | |
9967 | (new_inode && | |
9968 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) | |
9969 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9970 | ||
9971 | btrfs_end_log_trans(root); | |
9972 | log_pinned = false; | |
9973 | } | |
7ad85bb7 | 9974 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9975 | out_notrans: |
33345d01 | 9976 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9977 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9978 | |
39279cc3 CM |
9979 | return ret; |
9980 | } | |
9981 | ||
80ace85c MS |
9982 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9983 | struct inode *new_dir, struct dentry *new_dentry, | |
9984 | unsigned int flags) | |
9985 | { | |
cdd1fedf | 9986 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9987 | return -EINVAL; |
9988 | ||
cdd1fedf DF |
9989 | if (flags & RENAME_EXCHANGE) |
9990 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9991 | new_dentry); | |
9992 | ||
9993 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9994 | } |
9995 | ||
8ccf6f19 MX |
9996 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9997 | { | |
9998 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9999 | struct inode *inode; |
8ccf6f19 MX |
10000 | |
10001 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
10002 | work); | |
9f23e289 | 10003 | inode = delalloc_work->inode; |
30424601 DS |
10004 | filemap_flush(inode->i_mapping); |
10005 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
10006 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 10007 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
10008 | |
10009 | if (delalloc_work->delay_iput) | |
9f23e289 | 10010 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 10011 | else |
9f23e289 | 10012 | iput(inode); |
8ccf6f19 MX |
10013 | complete(&delalloc_work->completion); |
10014 | } | |
10015 | ||
10016 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 10017 | int delay_iput) |
8ccf6f19 MX |
10018 | { |
10019 | struct btrfs_delalloc_work *work; | |
10020 | ||
100d5702 | 10021 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
10022 | if (!work) |
10023 | return NULL; | |
10024 | ||
10025 | init_completion(&work->completion); | |
10026 | INIT_LIST_HEAD(&work->list); | |
10027 | work->inode = inode; | |
8ccf6f19 | 10028 | work->delay_iput = delay_iput; |
9e0af237 LB |
10029 | WARN_ON_ONCE(!inode); |
10030 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
10031 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
10032 | |
10033 | return work; | |
10034 | } | |
10035 | ||
10036 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
10037 | { | |
10038 | wait_for_completion(&work->completion); | |
100d5702 | 10039 | kfree(work); |
8ccf6f19 MX |
10040 | } |
10041 | ||
d352ac68 CM |
10042 | /* |
10043 | * some fairly slow code that needs optimization. This walks the list | |
10044 | * of all the inodes with pending delalloc and forces them to disk. | |
10045 | */ | |
6c255e67 MX |
10046 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
10047 | int nr) | |
ea8c2819 | 10048 | { |
ea8c2819 | 10049 | struct btrfs_inode *binode; |
5b21f2ed | 10050 | struct inode *inode; |
8ccf6f19 MX |
10051 | struct btrfs_delalloc_work *work, *next; |
10052 | struct list_head works; | |
1eafa6c7 | 10053 | struct list_head splice; |
8ccf6f19 | 10054 | int ret = 0; |
ea8c2819 | 10055 | |
8ccf6f19 | 10056 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10057 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10058 | |
573bfb72 | 10059 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10060 | spin_lock(&root->delalloc_lock); |
10061 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10062 | while (!list_empty(&splice)) { |
10063 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10064 | delalloc_inodes); |
1eafa6c7 | 10065 | |
eb73c1b7 MX |
10066 | list_move_tail(&binode->delalloc_inodes, |
10067 | &root->delalloc_inodes); | |
5b21f2ed | 10068 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10069 | if (!inode) { |
eb73c1b7 | 10070 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10071 | continue; |
df0af1a5 | 10072 | } |
eb73c1b7 | 10073 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10074 | |
651d494a | 10075 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10076 | if (!work) { |
f4ab9ea7 JB |
10077 | if (delay_iput) |
10078 | btrfs_add_delayed_iput(inode); | |
10079 | else | |
10080 | iput(inode); | |
1eafa6c7 | 10081 | ret = -ENOMEM; |
a1ecaabb | 10082 | goto out; |
5b21f2ed | 10083 | } |
1eafa6c7 | 10084 | list_add_tail(&work->list, &works); |
a44903ab QW |
10085 | btrfs_queue_work(root->fs_info->flush_workers, |
10086 | &work->work); | |
6c255e67 MX |
10087 | ret++; |
10088 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10089 | goto out; |
5b21f2ed | 10090 | cond_resched(); |
eb73c1b7 | 10091 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10092 | } |
eb73c1b7 | 10093 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10094 | |
a1ecaabb | 10095 | out: |
eb73c1b7 MX |
10096 | list_for_each_entry_safe(work, next, &works, list) { |
10097 | list_del_init(&work->list); | |
10098 | btrfs_wait_and_free_delalloc_work(work); | |
10099 | } | |
10100 | ||
10101 | if (!list_empty_careful(&splice)) { | |
10102 | spin_lock(&root->delalloc_lock); | |
10103 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10104 | spin_unlock(&root->delalloc_lock); | |
10105 | } | |
573bfb72 | 10106 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10107 | return ret; |
10108 | } | |
1eafa6c7 | 10109 | |
eb73c1b7 MX |
10110 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10111 | { | |
10112 | int ret; | |
1eafa6c7 | 10113 | |
2c21b4d7 | 10114 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
10115 | return -EROFS; |
10116 | ||
6c255e67 MX |
10117 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10118 | if (ret > 0) | |
10119 | ret = 0; | |
eb73c1b7 MX |
10120 | /* |
10121 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10122 | * we have to make sure the IO is actually started and that |
10123 | * ordered extents get created before we return | |
10124 | */ | |
10125 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 10126 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 10127 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 10128 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
10129 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
10130 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
10131 | } |
10132 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
10133 | return ret; |
10134 | } | |
10135 | ||
6c255e67 MX |
10136 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10137 | int nr) | |
eb73c1b7 MX |
10138 | { |
10139 | struct btrfs_root *root; | |
10140 | struct list_head splice; | |
10141 | int ret; | |
10142 | ||
2c21b4d7 | 10143 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10144 | return -EROFS; |
10145 | ||
10146 | INIT_LIST_HEAD(&splice); | |
10147 | ||
573bfb72 | 10148 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10149 | spin_lock(&fs_info->delalloc_root_lock); |
10150 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10151 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10152 | root = list_first_entry(&splice, struct btrfs_root, |
10153 | delalloc_root); | |
10154 | root = btrfs_grab_fs_root(root); | |
10155 | BUG_ON(!root); | |
10156 | list_move_tail(&root->delalloc_root, | |
10157 | &fs_info->delalloc_roots); | |
10158 | spin_unlock(&fs_info->delalloc_root_lock); | |
10159 | ||
6c255e67 | 10160 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10161 | btrfs_put_fs_root(root); |
6c255e67 | 10162 | if (ret < 0) |
eb73c1b7 MX |
10163 | goto out; |
10164 | ||
6c255e67 MX |
10165 | if (nr != -1) { |
10166 | nr -= ret; | |
10167 | WARN_ON(nr < 0); | |
10168 | } | |
eb73c1b7 | 10169 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10170 | } |
eb73c1b7 | 10171 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10172 | |
6c255e67 | 10173 | ret = 0; |
eb73c1b7 MX |
10174 | atomic_inc(&fs_info->async_submit_draining); |
10175 | while (atomic_read(&fs_info->nr_async_submits) || | |
10176 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10177 | wait_event(fs_info->async_submit_wait, | |
10178 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10179 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10180 | } | |
10181 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10182 | out: |
1eafa6c7 | 10183 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10184 | spin_lock(&fs_info->delalloc_root_lock); |
10185 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10186 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10187 | } |
573bfb72 | 10188 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10189 | return ret; |
ea8c2819 CM |
10190 | } |
10191 | ||
39279cc3 CM |
10192 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10193 | const char *symname) | |
10194 | { | |
10195 | struct btrfs_trans_handle *trans; | |
10196 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10197 | struct btrfs_path *path; | |
10198 | struct btrfs_key key; | |
1832a6d5 | 10199 | struct inode *inode = NULL; |
39279cc3 CM |
10200 | int err; |
10201 | int drop_inode = 0; | |
10202 | u64 objectid; | |
67871254 | 10203 | u64 index = 0; |
39279cc3 CM |
10204 | int name_len; |
10205 | int datasize; | |
5f39d397 | 10206 | unsigned long ptr; |
39279cc3 | 10207 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10208 | struct extent_buffer *leaf; |
39279cc3 | 10209 | |
f06becc4 | 10210 | name_len = strlen(symname); |
39279cc3 CM |
10211 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
10212 | return -ENAMETOOLONG; | |
1832a6d5 | 10213 | |
9ed74f2d JB |
10214 | /* |
10215 | * 2 items for inode item and ref | |
10216 | * 2 items for dir items | |
9269d12b FM |
10217 | * 1 item for updating parent inode item |
10218 | * 1 item for the inline extent item | |
9ed74f2d JB |
10219 | * 1 item for xattr if selinux is on |
10220 | */ | |
9269d12b | 10221 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10222 | if (IS_ERR(trans)) |
10223 | return PTR_ERR(trans); | |
1832a6d5 | 10224 | |
581bb050 LZ |
10225 | err = btrfs_find_free_ino(root, &objectid); |
10226 | if (err) | |
10227 | goto out_unlock; | |
10228 | ||
aec7477b | 10229 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 10230 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 10231 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
10232 | if (IS_ERR(inode)) { |
10233 | err = PTR_ERR(inode); | |
39279cc3 | 10234 | goto out_unlock; |
7cf96da3 | 10235 | } |
39279cc3 | 10236 | |
ad19db71 CS |
10237 | /* |
10238 | * If the active LSM wants to access the inode during | |
10239 | * d_instantiate it needs these. Smack checks to see | |
10240 | * if the filesystem supports xattrs by looking at the | |
10241 | * ops vector. | |
10242 | */ | |
10243 | inode->i_fop = &btrfs_file_operations; | |
10244 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10245 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10246 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10247 | ||
10248 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10249 | if (err) | |
10250 | goto out_unlock_inode; | |
ad19db71 | 10251 | |
39279cc3 | 10252 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10253 | if (!path) { |
10254 | err = -ENOMEM; | |
b0d5d10f | 10255 | goto out_unlock_inode; |
d8926bb3 | 10256 | } |
33345d01 | 10257 | key.objectid = btrfs_ino(inode); |
39279cc3 | 10258 | key.offset = 0; |
962a298f | 10259 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10260 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10261 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10262 | datasize); | |
54aa1f4d | 10263 | if (err) { |
b0839166 | 10264 | btrfs_free_path(path); |
b0d5d10f | 10265 | goto out_unlock_inode; |
54aa1f4d | 10266 | } |
5f39d397 CM |
10267 | leaf = path->nodes[0]; |
10268 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10269 | struct btrfs_file_extent_item); | |
10270 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10271 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10272 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10273 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10274 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10275 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10276 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10277 | ||
39279cc3 | 10278 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10279 | write_extent_buffer(leaf, symname, ptr, name_len); |
10280 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10281 | btrfs_free_path(path); |
5f39d397 | 10282 | |
39279cc3 | 10283 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10284 | inode_nohighmem(inode); |
39279cc3 | 10285 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10286 | inode_set_bytes(inode, name_len); |
f06becc4 | 10287 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 10288 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10289 | /* |
10290 | * Last step, add directory indexes for our symlink inode. This is the | |
10291 | * last step to avoid extra cleanup of these indexes if an error happens | |
10292 | * elsewhere above. | |
10293 | */ | |
10294 | if (!err) | |
10295 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
b0d5d10f | 10296 | if (err) { |
54aa1f4d | 10297 | drop_inode = 1; |
b0d5d10f CM |
10298 | goto out_unlock_inode; |
10299 | } | |
10300 | ||
10301 | unlock_new_inode(inode); | |
10302 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10303 | |
10304 | out_unlock: | |
7ad85bb7 | 10305 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
10306 | if (drop_inode) { |
10307 | inode_dec_link_count(inode); | |
10308 | iput(inode); | |
10309 | } | |
b53d3f5d | 10310 | btrfs_btree_balance_dirty(root); |
39279cc3 | 10311 | return err; |
b0d5d10f CM |
10312 | |
10313 | out_unlock_inode: | |
10314 | drop_inode = 1; | |
10315 | unlock_new_inode(inode); | |
10316 | goto out_unlock; | |
39279cc3 | 10317 | } |
16432985 | 10318 | |
0af3d00b JB |
10319 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10320 | u64 start, u64 num_bytes, u64 min_size, | |
10321 | loff_t actual_len, u64 *alloc_hint, | |
10322 | struct btrfs_trans_handle *trans) | |
d899e052 | 10323 | { |
5dc562c5 JB |
10324 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10325 | struct extent_map *em; | |
d899e052 YZ |
10326 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10327 | struct btrfs_key ins; | |
d899e052 | 10328 | u64 cur_offset = start; |
55a61d1d | 10329 | u64 i_size; |
154ea289 | 10330 | u64 cur_bytes; |
0b670dc4 | 10331 | u64 last_alloc = (u64)-1; |
d899e052 | 10332 | int ret = 0; |
0af3d00b | 10333 | bool own_trans = true; |
18513091 | 10334 | u64 end = start + num_bytes - 1; |
d899e052 | 10335 | |
0af3d00b JB |
10336 | if (trans) |
10337 | own_trans = false; | |
d899e052 | 10338 | while (num_bytes > 0) { |
0af3d00b JB |
10339 | if (own_trans) { |
10340 | trans = btrfs_start_transaction(root, 3); | |
10341 | if (IS_ERR(trans)) { | |
10342 | ret = PTR_ERR(trans); | |
10343 | break; | |
10344 | } | |
5a303d5d YZ |
10345 | } |
10346 | ||
ee22184b | 10347 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10348 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10349 | /* |
10350 | * If we are severely fragmented we could end up with really | |
10351 | * small allocations, so if the allocator is returning small | |
10352 | * chunks lets make its job easier by only searching for those | |
10353 | * sized chunks. | |
10354 | */ | |
10355 | cur_bytes = min(cur_bytes, last_alloc); | |
18513091 WX |
10356 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
10357 | min_size, 0, *alloc_hint, &ins, 1, 0); | |
5a303d5d | 10358 | if (ret) { |
0af3d00b JB |
10359 | if (own_trans) |
10360 | btrfs_end_transaction(trans, root); | |
a22285a6 | 10361 | break; |
d899e052 | 10362 | } |
9cfa3e34 | 10363 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
5a303d5d | 10364 | |
0b670dc4 | 10365 | last_alloc = ins.offset; |
d899e052 YZ |
10366 | ret = insert_reserved_file_extent(trans, inode, |
10367 | cur_offset, ins.objectid, | |
10368 | ins.offset, ins.offset, | |
920bbbfb | 10369 | ins.offset, 0, 0, 0, |
d899e052 | 10370 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10371 | if (ret) { |
857cc2fc | 10372 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 10373 | ins.offset, 0); |
66642832 | 10374 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10375 | if (own_trans) |
10376 | btrfs_end_transaction(trans, root); | |
10377 | break; | |
10378 | } | |
31193213 | 10379 | |
a1ed835e CM |
10380 | btrfs_drop_extent_cache(inode, cur_offset, |
10381 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 10382 | |
5dc562c5 JB |
10383 | em = alloc_extent_map(); |
10384 | if (!em) { | |
10385 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10386 | &BTRFS_I(inode)->runtime_flags); | |
10387 | goto next; | |
10388 | } | |
10389 | ||
10390 | em->start = cur_offset; | |
10391 | em->orig_start = cur_offset; | |
10392 | em->len = ins.offset; | |
10393 | em->block_start = ins.objectid; | |
10394 | em->block_len = ins.offset; | |
b4939680 | 10395 | em->orig_block_len = ins.offset; |
cc95bef6 | 10396 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
10397 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
10398 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
10399 | em->generation = trans->transid; | |
10400 | ||
10401 | while (1) { | |
10402 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10403 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10404 | write_unlock(&em_tree->lock); |
10405 | if (ret != -EEXIST) | |
10406 | break; | |
10407 | btrfs_drop_extent_cache(inode, cur_offset, | |
10408 | cur_offset + ins.offset - 1, | |
10409 | 0); | |
10410 | } | |
10411 | free_extent_map(em); | |
10412 | next: | |
d899e052 YZ |
10413 | num_bytes -= ins.offset; |
10414 | cur_offset += ins.offset; | |
efa56464 | 10415 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10416 | |
0c4d2d95 | 10417 | inode_inc_iversion(inode); |
04b285f3 | 10418 | inode->i_ctime = current_fs_time(inode->i_sb); |
6cbff00f | 10419 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10420 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10421 | (actual_len > inode->i_size) && |
10422 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10423 | if (cur_offset > actual_len) |
55a61d1d | 10424 | i_size = actual_len; |
d1ea6a61 | 10425 | else |
55a61d1d JB |
10426 | i_size = cur_offset; |
10427 | i_size_write(inode, i_size); | |
10428 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10429 | } |
10430 | ||
d899e052 | 10431 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10432 | |
10433 | if (ret) { | |
66642832 | 10434 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
10435 | if (own_trans) |
10436 | btrfs_end_transaction(trans, root); | |
10437 | break; | |
10438 | } | |
d899e052 | 10439 | |
0af3d00b JB |
10440 | if (own_trans) |
10441 | btrfs_end_transaction(trans, root); | |
5a303d5d | 10442 | } |
18513091 WX |
10443 | if (cur_offset < end) |
10444 | btrfs_free_reserved_data_space(inode, cur_offset, | |
10445 | end - cur_offset + 1); | |
d899e052 YZ |
10446 | return ret; |
10447 | } | |
10448 | ||
0af3d00b JB |
10449 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10450 | u64 start, u64 num_bytes, u64 min_size, | |
10451 | loff_t actual_len, u64 *alloc_hint) | |
10452 | { | |
10453 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10454 | min_size, actual_len, alloc_hint, | |
10455 | NULL); | |
10456 | } | |
10457 | ||
10458 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10459 | struct btrfs_trans_handle *trans, int mode, | |
10460 | u64 start, u64 num_bytes, u64 min_size, | |
10461 | loff_t actual_len, u64 *alloc_hint) | |
10462 | { | |
10463 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10464 | min_size, actual_len, alloc_hint, trans); | |
10465 | } | |
10466 | ||
e6dcd2dc CM |
10467 | static int btrfs_set_page_dirty(struct page *page) |
10468 | { | |
e6dcd2dc CM |
10469 | return __set_page_dirty_nobuffers(page); |
10470 | } | |
10471 | ||
10556cb2 | 10472 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10473 | { |
b83cc969 | 10474 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10475 | umode_t mode = inode->i_mode; |
b83cc969 | 10476 | |
cb6db4e5 JM |
10477 | if (mask & MAY_WRITE && |
10478 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10479 | if (btrfs_root_readonly(root)) | |
10480 | return -EROFS; | |
10481 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10482 | return -EACCES; | |
10483 | } | |
2830ba7f | 10484 | return generic_permission(inode, mask); |
fdebe2bd | 10485 | } |
39279cc3 | 10486 | |
ef3b9af5 FM |
10487 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10488 | { | |
10489 | struct btrfs_trans_handle *trans; | |
10490 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10491 | struct inode *inode = NULL; | |
10492 | u64 objectid; | |
10493 | u64 index; | |
10494 | int ret = 0; | |
10495 | ||
10496 | /* | |
10497 | * 5 units required for adding orphan entry | |
10498 | */ | |
10499 | trans = btrfs_start_transaction(root, 5); | |
10500 | if (IS_ERR(trans)) | |
10501 | return PTR_ERR(trans); | |
10502 | ||
10503 | ret = btrfs_find_free_ino(root, &objectid); | |
10504 | if (ret) | |
10505 | goto out; | |
10506 | ||
10507 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
10508 | btrfs_ino(dir), objectid, mode, &index); | |
10509 | if (IS_ERR(inode)) { | |
10510 | ret = PTR_ERR(inode); | |
10511 | inode = NULL; | |
10512 | goto out; | |
10513 | } | |
10514 | ||
ef3b9af5 FM |
10515 | inode->i_fop = &btrfs_file_operations; |
10516 | inode->i_op = &btrfs_file_inode_operations; | |
10517 | ||
10518 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10519 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10520 | ||
b0d5d10f CM |
10521 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10522 | if (ret) | |
10523 | goto out_inode; | |
10524 | ||
10525 | ret = btrfs_update_inode(trans, root, inode); | |
10526 | if (ret) | |
10527 | goto out_inode; | |
ef3b9af5 FM |
10528 | ret = btrfs_orphan_add(trans, inode); |
10529 | if (ret) | |
b0d5d10f | 10530 | goto out_inode; |
ef3b9af5 | 10531 | |
5762b5c9 FM |
10532 | /* |
10533 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10534 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10535 | * through: | |
10536 | * | |
10537 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10538 | */ | |
10539 | set_nlink(inode, 1); | |
b0d5d10f | 10540 | unlock_new_inode(inode); |
ef3b9af5 FM |
10541 | d_tmpfile(dentry, inode); |
10542 | mark_inode_dirty(inode); | |
10543 | ||
10544 | out: | |
10545 | btrfs_end_transaction(trans, root); | |
10546 | if (ret) | |
10547 | iput(inode); | |
10548 | btrfs_balance_delayed_items(root); | |
10549 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 10550 | return ret; |
b0d5d10f CM |
10551 | |
10552 | out_inode: | |
10553 | unlock_new_inode(inode); | |
10554 | goto out; | |
10555 | ||
ef3b9af5 FM |
10556 | } |
10557 | ||
b38ef71c FM |
10558 | /* Inspired by filemap_check_errors() */ |
10559 | int btrfs_inode_check_errors(struct inode *inode) | |
10560 | { | |
10561 | int ret = 0; | |
10562 | ||
10563 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
10564 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
10565 | ret = -ENOSPC; | |
10566 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
10567 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
10568 | ret = -EIO; | |
10569 | ||
10570 | return ret; | |
10571 | } | |
10572 | ||
6e1d5dcc | 10573 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10574 | .getattr = btrfs_getattr, |
39279cc3 CM |
10575 | .lookup = btrfs_lookup, |
10576 | .create = btrfs_create, | |
10577 | .unlink = btrfs_unlink, | |
10578 | .link = btrfs_link, | |
10579 | .mkdir = btrfs_mkdir, | |
10580 | .rmdir = btrfs_rmdir, | |
80ace85c | 10581 | .rename2 = btrfs_rename2, |
39279cc3 CM |
10582 | .symlink = btrfs_symlink, |
10583 | .setattr = btrfs_setattr, | |
618e21d5 | 10584 | .mknod = btrfs_mknod, |
e0d46f5c | 10585 | .setxattr = generic_setxattr, |
9172abbc | 10586 | .getxattr = generic_getxattr, |
5103e947 | 10587 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10588 | .removexattr = generic_removexattr, |
fdebe2bd | 10589 | .permission = btrfs_permission, |
4e34e719 | 10590 | .get_acl = btrfs_get_acl, |
996a710d | 10591 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10592 | .update_time = btrfs_update_time, |
ef3b9af5 | 10593 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10594 | }; |
6e1d5dcc | 10595 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10596 | .lookup = btrfs_lookup, |
fdebe2bd | 10597 | .permission = btrfs_permission, |
4e34e719 | 10598 | .get_acl = btrfs_get_acl, |
996a710d | 10599 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10600 | .update_time = btrfs_update_time, |
39279cc3 | 10601 | }; |
76dda93c | 10602 | |
828c0950 | 10603 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10604 | .llseek = generic_file_llseek, |
10605 | .read = generic_read_dir, | |
02dbfc99 | 10606 | .iterate_shared = btrfs_real_readdir, |
34287aa3 | 10607 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10608 | #ifdef CONFIG_COMPAT |
4c63c245 | 10609 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10610 | #endif |
6bf13c0c | 10611 | .release = btrfs_release_file, |
e02119d5 | 10612 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10613 | }; |
10614 | ||
20e5506b | 10615 | static const struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 10616 | .fill_delalloc = run_delalloc_range, |
065631f6 | 10617 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 10618 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 10619 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 10620 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10621 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10622 | .set_bit_hook = btrfs_set_bit_hook, |
10623 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10624 | .merge_extent_hook = btrfs_merge_extent_hook, |
10625 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
10626 | }; |
10627 | ||
35054394 CM |
10628 | /* |
10629 | * btrfs doesn't support the bmap operation because swapfiles | |
10630 | * use bmap to make a mapping of extents in the file. They assume | |
10631 | * these extents won't change over the life of the file and they | |
10632 | * use the bmap result to do IO directly to the drive. | |
10633 | * | |
10634 | * the btrfs bmap call would return logical addresses that aren't | |
10635 | * suitable for IO and they also will change frequently as COW | |
10636 | * operations happen. So, swapfile + btrfs == corruption. | |
10637 | * | |
10638 | * For now we're avoiding this by dropping bmap. | |
10639 | */ | |
7f09410b | 10640 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10641 | .readpage = btrfs_readpage, |
10642 | .writepage = btrfs_writepage, | |
b293f02e | 10643 | .writepages = btrfs_writepages, |
3ab2fb5a | 10644 | .readpages = btrfs_readpages, |
16432985 | 10645 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10646 | .invalidatepage = btrfs_invalidatepage, |
10647 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10648 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10649 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10650 | }; |
10651 | ||
7f09410b | 10652 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10653 | .readpage = btrfs_readpage, |
10654 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10655 | .invalidatepage = btrfs_invalidatepage, |
10656 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10657 | }; |
10658 | ||
6e1d5dcc | 10659 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10660 | .getattr = btrfs_getattr, |
10661 | .setattr = btrfs_setattr, | |
e0d46f5c | 10662 | .setxattr = generic_setxattr, |
9172abbc | 10663 | .getxattr = generic_getxattr, |
5103e947 | 10664 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10665 | .removexattr = generic_removexattr, |
fdebe2bd | 10666 | .permission = btrfs_permission, |
1506fcc8 | 10667 | .fiemap = btrfs_fiemap, |
4e34e719 | 10668 | .get_acl = btrfs_get_acl, |
996a710d | 10669 | .set_acl = btrfs_set_acl, |
e41f941a | 10670 | .update_time = btrfs_update_time, |
39279cc3 | 10671 | }; |
6e1d5dcc | 10672 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10673 | .getattr = btrfs_getattr, |
10674 | .setattr = btrfs_setattr, | |
fdebe2bd | 10675 | .permission = btrfs_permission, |
e0d46f5c | 10676 | .setxattr = generic_setxattr, |
9172abbc | 10677 | .getxattr = generic_getxattr, |
33268eaf | 10678 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10679 | .removexattr = generic_removexattr, |
4e34e719 | 10680 | .get_acl = btrfs_get_acl, |
996a710d | 10681 | .set_acl = btrfs_set_acl, |
e41f941a | 10682 | .update_time = btrfs_update_time, |
618e21d5 | 10683 | }; |
6e1d5dcc | 10684 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 | 10685 | .readlink = generic_readlink, |
6b255391 | 10686 | .get_link = page_get_link, |
f209561a | 10687 | .getattr = btrfs_getattr, |
22c44fe6 | 10688 | .setattr = btrfs_setattr, |
fdebe2bd | 10689 | .permission = btrfs_permission, |
e0d46f5c | 10690 | .setxattr = generic_setxattr, |
9172abbc | 10691 | .getxattr = generic_getxattr, |
0279b4cd | 10692 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10693 | .removexattr = generic_removexattr, |
e41f941a | 10694 | .update_time = btrfs_update_time, |
39279cc3 | 10695 | }; |
76dda93c | 10696 | |
82d339d9 | 10697 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10698 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10699 | .d_release = btrfs_dentry_release, |
76dda93c | 10700 | }; |