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> | |
a27bb332 | 35 | #include <linux/aio.h> |
9ebefb18 | 36 | #include <linux/bit_spinlock.h> |
5103e947 | 37 | #include <linux/xattr.h> |
33268eaf | 38 | #include <linux/posix_acl.h> |
d899e052 | 39 | #include <linux/falloc.h> |
5a0e3ad6 | 40 | #include <linux/slab.h> |
7a36ddec | 41 | #include <linux/ratelimit.h> |
22c44fe6 | 42 | #include <linux/mount.h> |
55e301fd | 43 | #include <linux/btrfs.h> |
53b381b3 | 44 | #include <linux/blkdev.h> |
f23b5a59 | 45 | #include <linux/posix_acl_xattr.h> |
4a3d1caf | 46 | #include <linux/uio.h> |
39279cc3 CM |
47 | #include "ctree.h" |
48 | #include "disk-io.h" | |
49 | #include "transaction.h" | |
50 | #include "btrfs_inode.h" | |
39279cc3 | 51 | #include "print-tree.h" |
e6dcd2dc | 52 | #include "ordered-data.h" |
95819c05 | 53 | #include "xattr.h" |
e02119d5 | 54 | #include "tree-log.h" |
4a54c8c1 | 55 | #include "volumes.h" |
c8b97818 | 56 | #include "compression.h" |
b4ce94de | 57 | #include "locking.h" |
dc89e982 | 58 | #include "free-space-cache.h" |
581bb050 | 59 | #include "inode-map.h" |
38c227d8 | 60 | #include "backref.h" |
f23b5a59 | 61 | #include "hash.h" |
63541927 | 62 | #include "props.h" |
31193213 | 63 | #include "qgroup.h" |
39279cc3 CM |
64 | |
65 | struct btrfs_iget_args { | |
90d3e592 | 66 | struct btrfs_key *location; |
39279cc3 CM |
67 | struct btrfs_root *root; |
68 | }; | |
69 | ||
6e1d5dcc AD |
70 | static const struct inode_operations btrfs_dir_inode_operations; |
71 | static const struct inode_operations btrfs_symlink_inode_operations; | |
72 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
73 | static const struct inode_operations btrfs_special_inode_operations; | |
74 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
75 | static const struct address_space_operations btrfs_aops; |
76 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 77 | static const struct file_operations btrfs_dir_file_operations; |
d1310b2e | 78 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
79 | |
80 | static struct kmem_cache *btrfs_inode_cachep; | |
8ccf6f19 | 81 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
39279cc3 CM |
82 | struct kmem_cache *btrfs_trans_handle_cachep; |
83 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 84 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 85 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
86 | |
87 | #define S_SHIFT 12 | |
88 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
89 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
90 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
91 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
92 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
93 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
94 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
95 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
96 | }; | |
97 | ||
3972f260 | 98 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 99 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 100 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
101 | static noinline int cow_file_range(struct inode *inode, |
102 | struct page *locked_page, | |
103 | u64 start, u64 end, int *page_started, | |
104 | unsigned long *nr_written, int unlock); | |
70c8a91c JB |
105 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
106 | u64 len, u64 orig_start, | |
107 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
108 | u64 orig_block_len, u64 ram_bytes, |
109 | int type); | |
7b128766 | 110 | |
48a3b636 | 111 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 112 | |
6a3891c5 JB |
113 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
114 | void btrfs_test_inode_set_ops(struct inode *inode) | |
115 | { | |
116 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
117 | } | |
118 | #endif | |
119 | ||
f34f57a3 | 120 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
121 | struct inode *inode, struct inode *dir, |
122 | const struct qstr *qstr) | |
0279b4cd JO |
123 | { |
124 | int err; | |
125 | ||
f34f57a3 | 126 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 127 | if (!err) |
2a7dba39 | 128 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
129 | return err; |
130 | } | |
131 | ||
c8b97818 CM |
132 | /* |
133 | * this does all the hard work for inserting an inline extent into | |
134 | * the btree. The caller should have done a btrfs_drop_extents so that | |
135 | * no overlapping inline items exist in the btree | |
136 | */ | |
40f76580 | 137 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 138 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
139 | struct btrfs_root *root, struct inode *inode, |
140 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 141 | int compress_type, |
c8b97818 CM |
142 | struct page **compressed_pages) |
143 | { | |
c8b97818 CM |
144 | struct extent_buffer *leaf; |
145 | struct page *page = NULL; | |
146 | char *kaddr; | |
147 | unsigned long ptr; | |
148 | struct btrfs_file_extent_item *ei; | |
149 | int err = 0; | |
150 | int ret; | |
151 | size_t cur_size = size; | |
c8b97818 | 152 | unsigned long offset; |
c8b97818 | 153 | |
fe3f566c | 154 | if (compressed_size && compressed_pages) |
c8b97818 | 155 | cur_size = compressed_size; |
c8b97818 | 156 | |
1acae57b | 157 | inode_add_bytes(inode, size); |
c8b97818 | 158 | |
1acae57b FDBM |
159 | if (!extent_inserted) { |
160 | struct btrfs_key key; | |
161 | size_t datasize; | |
c8b97818 | 162 | |
1acae57b FDBM |
163 | key.objectid = btrfs_ino(inode); |
164 | key.offset = start; | |
962a298f | 165 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 166 | |
1acae57b FDBM |
167 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
168 | path->leave_spinning = 1; | |
169 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
170 | datasize); | |
171 | if (ret) { | |
172 | err = ret; | |
173 | goto fail; | |
174 | } | |
c8b97818 CM |
175 | } |
176 | leaf = path->nodes[0]; | |
177 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
178 | struct btrfs_file_extent_item); | |
179 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
180 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
181 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
182 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
183 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
184 | ptr = btrfs_file_extent_inline_start(ei); | |
185 | ||
261507a0 | 186 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
187 | struct page *cpage; |
188 | int i = 0; | |
d397712b | 189 | while (compressed_size > 0) { |
c8b97818 | 190 | cpage = compressed_pages[i]; |
5b050f04 | 191 | cur_size = min_t(unsigned long, compressed_size, |
c8b97818 CM |
192 | PAGE_CACHE_SIZE); |
193 | ||
7ac687d9 | 194 | kaddr = kmap_atomic(cpage); |
c8b97818 | 195 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 196 | kunmap_atomic(kaddr); |
c8b97818 CM |
197 | |
198 | i++; | |
199 | ptr += cur_size; | |
200 | compressed_size -= cur_size; | |
201 | } | |
202 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 203 | compress_type); |
c8b97818 CM |
204 | } else { |
205 | page = find_get_page(inode->i_mapping, | |
206 | start >> PAGE_CACHE_SHIFT); | |
207 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
7ac687d9 | 208 | kaddr = kmap_atomic(page); |
c8b97818 CM |
209 | offset = start & (PAGE_CACHE_SIZE - 1); |
210 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
7ac687d9 | 211 | kunmap_atomic(kaddr); |
c8b97818 CM |
212 | page_cache_release(page); |
213 | } | |
214 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 215 | btrfs_release_path(path); |
c8b97818 | 216 | |
c2167754 YZ |
217 | /* |
218 | * we're an inline extent, so nobody can | |
219 | * extend the file past i_size without locking | |
220 | * a page we already have locked. | |
221 | * | |
222 | * We must do any isize and inode updates | |
223 | * before we unlock the pages. Otherwise we | |
224 | * could end up racing with unlink. | |
225 | */ | |
c8b97818 | 226 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 227 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 228 | |
79787eaa | 229 | return ret; |
c8b97818 | 230 | fail: |
c8b97818 CM |
231 | return err; |
232 | } | |
233 | ||
234 | ||
235 | /* | |
236 | * conditionally insert an inline extent into the file. This | |
237 | * does the checks required to make sure the data is small enough | |
238 | * to fit as an inline extent. | |
239 | */ | |
00361589 JB |
240 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
241 | struct inode *inode, u64 start, | |
242 | u64 end, size_t compressed_size, | |
243 | int compress_type, | |
244 | struct page **compressed_pages) | |
c8b97818 | 245 | { |
00361589 | 246 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
247 | u64 isize = i_size_read(inode); |
248 | u64 actual_end = min(end + 1, isize); | |
249 | u64 inline_len = actual_end - start; | |
fda2832f | 250 | u64 aligned_end = ALIGN(end, root->sectorsize); |
c8b97818 CM |
251 | u64 data_len = inline_len; |
252 | int ret; | |
1acae57b FDBM |
253 | struct btrfs_path *path; |
254 | int extent_inserted = 0; | |
255 | u32 extent_item_size; | |
c8b97818 CM |
256 | |
257 | if (compressed_size) | |
258 | data_len = compressed_size; | |
259 | ||
260 | if (start > 0 || | |
354877be WS |
261 | actual_end > PAGE_CACHE_SIZE || |
262 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || | |
c8b97818 CM |
263 | (!compressed_size && |
264 | (actual_end & (root->sectorsize - 1)) == 0) || | |
265 | end + 1 < isize || | |
266 | data_len > root->fs_info->max_inline) { | |
267 | return 1; | |
268 | } | |
269 | ||
1acae57b FDBM |
270 | path = btrfs_alloc_path(); |
271 | if (!path) | |
272 | return -ENOMEM; | |
273 | ||
00361589 | 274 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
275 | if (IS_ERR(trans)) { |
276 | btrfs_free_path(path); | |
00361589 | 277 | return PTR_ERR(trans); |
1acae57b | 278 | } |
00361589 JB |
279 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
280 | ||
1acae57b FDBM |
281 | if (compressed_size && compressed_pages) |
282 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
283 | compressed_size); | |
284 | else | |
285 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
286 | inline_len); | |
287 | ||
288 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
289 | start, aligned_end, NULL, | |
290 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 JB |
291 | if (ret) { |
292 | btrfs_abort_transaction(trans, root, ret); | |
293 | goto out; | |
294 | } | |
c8b97818 CM |
295 | |
296 | if (isize > actual_end) | |
297 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
298 | ret = insert_inline_extent(trans, path, extent_inserted, |
299 | root, inode, start, | |
c8b97818 | 300 | inline_len, compressed_size, |
fe3f566c | 301 | compress_type, compressed_pages); |
2adcac1a | 302 | if (ret && ret != -ENOSPC) { |
79787eaa | 303 | btrfs_abort_transaction(trans, root, ret); |
00361589 | 304 | goto out; |
2adcac1a | 305 | } else if (ret == -ENOSPC) { |
00361589 JB |
306 | ret = 1; |
307 | goto out; | |
79787eaa | 308 | } |
2adcac1a | 309 | |
bdc20e67 | 310 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 311 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 312 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 313 | out: |
1acae57b | 314 | btrfs_free_path(path); |
00361589 JB |
315 | btrfs_end_transaction(trans, root); |
316 | return ret; | |
c8b97818 CM |
317 | } |
318 | ||
771ed689 CM |
319 | struct async_extent { |
320 | u64 start; | |
321 | u64 ram_size; | |
322 | u64 compressed_size; | |
323 | struct page **pages; | |
324 | unsigned long nr_pages; | |
261507a0 | 325 | int compress_type; |
771ed689 CM |
326 | struct list_head list; |
327 | }; | |
328 | ||
329 | struct async_cow { | |
330 | struct inode *inode; | |
331 | struct btrfs_root *root; | |
332 | struct page *locked_page; | |
333 | u64 start; | |
334 | u64 end; | |
335 | struct list_head extents; | |
336 | struct btrfs_work work; | |
337 | }; | |
338 | ||
339 | static noinline int add_async_extent(struct async_cow *cow, | |
340 | u64 start, u64 ram_size, | |
341 | u64 compressed_size, | |
342 | struct page **pages, | |
261507a0 LZ |
343 | unsigned long nr_pages, |
344 | int compress_type) | |
771ed689 CM |
345 | { |
346 | struct async_extent *async_extent; | |
347 | ||
348 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 349 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
350 | async_extent->start = start; |
351 | async_extent->ram_size = ram_size; | |
352 | async_extent->compressed_size = compressed_size; | |
353 | async_extent->pages = pages; | |
354 | async_extent->nr_pages = nr_pages; | |
261507a0 | 355 | async_extent->compress_type = compress_type; |
771ed689 CM |
356 | list_add_tail(&async_extent->list, &cow->extents); |
357 | return 0; | |
358 | } | |
359 | ||
f79707b0 WS |
360 | static inline int inode_need_compress(struct inode *inode) |
361 | { | |
362 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
363 | ||
364 | /* force compress */ | |
365 | if (btrfs_test_opt(root, FORCE_COMPRESS)) | |
366 | return 1; | |
367 | /* bad compression ratios */ | |
368 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
369 | return 0; | |
370 | if (btrfs_test_opt(root, COMPRESS) || | |
371 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || | |
372 | BTRFS_I(inode)->force_compress) | |
373 | return 1; | |
374 | return 0; | |
375 | } | |
376 | ||
d352ac68 | 377 | /* |
771ed689 CM |
378 | * we create compressed extents in two phases. The first |
379 | * phase compresses a range of pages that have already been | |
380 | * locked (both pages and state bits are locked). | |
c8b97818 | 381 | * |
771ed689 CM |
382 | * This is done inside an ordered work queue, and the compression |
383 | * is spread across many cpus. The actual IO submission is step | |
384 | * two, and the ordered work queue takes care of making sure that | |
385 | * happens in the same order things were put onto the queue by | |
386 | * writepages and friends. | |
c8b97818 | 387 | * |
771ed689 CM |
388 | * If this code finds it can't get good compression, it puts an |
389 | * entry onto the work queue to write the uncompressed bytes. This | |
390 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
391 | * are written in the same order that the flusher thread sent them |
392 | * down. | |
d352ac68 | 393 | */ |
c44f649e | 394 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
395 | struct page *locked_page, |
396 | u64 start, u64 end, | |
397 | struct async_cow *async_cow, | |
398 | int *num_added) | |
b888db2b CM |
399 | { |
400 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 401 | u64 num_bytes; |
db94535d | 402 | u64 blocksize = root->sectorsize; |
c8b97818 | 403 | u64 actual_end; |
42dc7bab | 404 | u64 isize = i_size_read(inode); |
e6dcd2dc | 405 | int ret = 0; |
c8b97818 CM |
406 | struct page **pages = NULL; |
407 | unsigned long nr_pages; | |
408 | unsigned long nr_pages_ret = 0; | |
409 | unsigned long total_compressed = 0; | |
410 | unsigned long total_in = 0; | |
411 | unsigned long max_compressed = 128 * 1024; | |
771ed689 | 412 | unsigned long max_uncompressed = 128 * 1024; |
c8b97818 CM |
413 | int i; |
414 | int will_compress; | |
261507a0 | 415 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 416 | int redirty = 0; |
b888db2b | 417 | |
4cb13e5d LB |
418 | /* if this is a small write inside eof, kick off a defrag */ |
419 | if ((end - start + 1) < 16 * 1024 && | |
420 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
4cb5300b CM |
421 | btrfs_add_inode_defrag(NULL, inode); |
422 | ||
42dc7bab | 423 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
424 | again: |
425 | will_compress = 0; | |
426 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; | |
427 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); | |
be20aa9d | 428 | |
f03d9301 CM |
429 | /* |
430 | * we don't want to send crud past the end of i_size through | |
431 | * compression, that's just a waste of CPU time. So, if the | |
432 | * end of the file is before the start of our current | |
433 | * requested range of bytes, we bail out to the uncompressed | |
434 | * cleanup code that can deal with all of this. | |
435 | * | |
436 | * It isn't really the fastest way to fix things, but this is a | |
437 | * very uncommon corner. | |
438 | */ | |
439 | if (actual_end <= start) | |
440 | goto cleanup_and_bail_uncompressed; | |
441 | ||
c8b97818 CM |
442 | total_compressed = actual_end - start; |
443 | ||
4bcbb332 SW |
444 | /* |
445 | * skip compression for a small file range(<=blocksize) that | |
446 | * isn't an inline extent, since it dosen't save disk space at all. | |
447 | */ | |
448 | if (total_compressed <= blocksize && | |
449 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
450 | goto cleanup_and_bail_uncompressed; | |
451 | ||
c8b97818 CM |
452 | /* we want to make sure that amount of ram required to uncompress |
453 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
454 | * of a compressed extent to 128k. This is a crucial number |
455 | * because it also controls how easily we can spread reads across | |
456 | * cpus for decompression. | |
457 | * | |
458 | * We also want to make sure the amount of IO required to do | |
459 | * a random read is reasonably small, so we limit the size of | |
460 | * a compressed extent to 128k. | |
c8b97818 CM |
461 | */ |
462 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 463 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 464 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
465 | total_in = 0; |
466 | ret = 0; | |
db94535d | 467 | |
771ed689 CM |
468 | /* |
469 | * we do compression for mount -o compress and when the | |
470 | * inode has not been flagged as nocompress. This flag can | |
471 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 472 | */ |
f79707b0 | 473 | if (inode_need_compress(inode)) { |
c8b97818 | 474 | WARN_ON(pages); |
31e818fe | 475 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
476 | if (!pages) { |
477 | /* just bail out to the uncompressed code */ | |
478 | goto cont; | |
479 | } | |
c8b97818 | 480 | |
261507a0 LZ |
481 | if (BTRFS_I(inode)->force_compress) |
482 | compress_type = BTRFS_I(inode)->force_compress; | |
483 | ||
4adaa611 CM |
484 | /* |
485 | * we need to call clear_page_dirty_for_io on each | |
486 | * page in the range. Otherwise applications with the file | |
487 | * mmap'd can wander in and change the page contents while | |
488 | * we are compressing them. | |
489 | * | |
490 | * If the compression fails for any reason, we set the pages | |
491 | * dirty again later on. | |
492 | */ | |
493 | extent_range_clear_dirty_for_io(inode, start, end); | |
494 | redirty = 1; | |
261507a0 LZ |
495 | ret = btrfs_compress_pages(compress_type, |
496 | inode->i_mapping, start, | |
497 | total_compressed, pages, | |
498 | nr_pages, &nr_pages_ret, | |
499 | &total_in, | |
500 | &total_compressed, | |
501 | max_compressed); | |
c8b97818 CM |
502 | |
503 | if (!ret) { | |
504 | unsigned long offset = total_compressed & | |
505 | (PAGE_CACHE_SIZE - 1); | |
506 | struct page *page = pages[nr_pages_ret - 1]; | |
507 | char *kaddr; | |
508 | ||
509 | /* zero the tail end of the last page, we might be | |
510 | * sending it down to disk | |
511 | */ | |
512 | if (offset) { | |
7ac687d9 | 513 | kaddr = kmap_atomic(page); |
c8b97818 CM |
514 | memset(kaddr + offset, 0, |
515 | PAGE_CACHE_SIZE - offset); | |
7ac687d9 | 516 | kunmap_atomic(kaddr); |
c8b97818 CM |
517 | } |
518 | will_compress = 1; | |
519 | } | |
520 | } | |
560f7d75 | 521 | cont: |
c8b97818 CM |
522 | if (start == 0) { |
523 | /* lets try to make an inline extent */ | |
771ed689 | 524 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 525 | /* we didn't compress the entire range, try |
771ed689 | 526 | * to make an uncompressed inline extent. |
c8b97818 | 527 | */ |
00361589 JB |
528 | ret = cow_file_range_inline(root, inode, start, end, |
529 | 0, 0, NULL); | |
c8b97818 | 530 | } else { |
771ed689 | 531 | /* try making a compressed inline extent */ |
00361589 | 532 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
533 | total_compressed, |
534 | compress_type, pages); | |
c8b97818 | 535 | } |
79787eaa | 536 | if (ret <= 0) { |
151a41bc JB |
537 | unsigned long clear_flags = EXTENT_DELALLOC | |
538 | EXTENT_DEFRAG; | |
e6eb4314 FM |
539 | unsigned long page_error_op; |
540 | ||
151a41bc | 541 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 542 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 543 | |
771ed689 | 544 | /* |
79787eaa JM |
545 | * inline extent creation worked or returned error, |
546 | * we don't need to create any more async work items. | |
547 | * Unlock and free up our temp pages. | |
771ed689 | 548 | */ |
c2790a2e | 549 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
151a41bc | 550 | clear_flags, PAGE_UNLOCK | |
c2790a2e JB |
551 | PAGE_CLEAR_DIRTY | |
552 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 553 | page_error_op | |
c2790a2e | 554 | PAGE_END_WRITEBACK); |
c8b97818 CM |
555 | goto free_pages_out; |
556 | } | |
557 | } | |
558 | ||
559 | if (will_compress) { | |
560 | /* | |
561 | * we aren't doing an inline extent round the compressed size | |
562 | * up to a block size boundary so the allocator does sane | |
563 | * things | |
564 | */ | |
fda2832f | 565 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
566 | |
567 | /* | |
568 | * one last check to make sure the compression is really a | |
569 | * win, compare the page count read with the blocks on disk | |
570 | */ | |
fda2832f | 571 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
c8b97818 CM |
572 | if (total_compressed >= total_in) { |
573 | will_compress = 0; | |
574 | } else { | |
c8b97818 CM |
575 | num_bytes = total_in; |
576 | } | |
577 | } | |
578 | if (!will_compress && pages) { | |
579 | /* | |
580 | * the compression code ran but failed to make things smaller, | |
581 | * free any pages it allocated and our page pointer array | |
582 | */ | |
583 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 584 | WARN_ON(pages[i]->mapping); |
c8b97818 CM |
585 | page_cache_release(pages[i]); |
586 | } | |
587 | kfree(pages); | |
588 | pages = NULL; | |
589 | total_compressed = 0; | |
590 | nr_pages_ret = 0; | |
591 | ||
592 | /* flag the file so we don't compress in the future */ | |
1e701a32 CM |
593 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
594 | !(BTRFS_I(inode)->force_compress)) { | |
a555f810 | 595 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 596 | } |
c8b97818 | 597 | } |
771ed689 CM |
598 | if (will_compress) { |
599 | *num_added += 1; | |
c8b97818 | 600 | |
771ed689 CM |
601 | /* the async work queues will take care of doing actual |
602 | * allocation on disk for these compressed pages, | |
603 | * and will submit them to the elevator. | |
604 | */ | |
605 | add_async_extent(async_cow, start, num_bytes, | |
261507a0 LZ |
606 | total_compressed, pages, nr_pages_ret, |
607 | compress_type); | |
179e29e4 | 608 | |
24ae6365 | 609 | if (start + num_bytes < end) { |
771ed689 CM |
610 | start += num_bytes; |
611 | pages = NULL; | |
612 | cond_resched(); | |
613 | goto again; | |
614 | } | |
615 | } else { | |
f03d9301 | 616 | cleanup_and_bail_uncompressed: |
771ed689 CM |
617 | /* |
618 | * No compression, but we still need to write the pages in | |
619 | * the file we've been given so far. redirty the locked | |
620 | * page if it corresponds to our extent and set things up | |
621 | * for the async work queue to run cow_file_range to do | |
622 | * the normal delalloc dance | |
623 | */ | |
624 | if (page_offset(locked_page) >= start && | |
625 | page_offset(locked_page) <= end) { | |
626 | __set_page_dirty_nobuffers(locked_page); | |
627 | /* unlocked later on in the async handlers */ | |
628 | } | |
4adaa611 CM |
629 | if (redirty) |
630 | extent_range_redirty_for_io(inode, start, end); | |
261507a0 LZ |
631 | add_async_extent(async_cow, start, end - start + 1, |
632 | 0, NULL, 0, BTRFS_COMPRESS_NONE); | |
771ed689 CM |
633 | *num_added += 1; |
634 | } | |
3b951516 | 635 | |
c44f649e | 636 | return; |
771ed689 CM |
637 | |
638 | free_pages_out: | |
639 | for (i = 0; i < nr_pages_ret; i++) { | |
640 | WARN_ON(pages[i]->mapping); | |
641 | page_cache_release(pages[i]); | |
642 | } | |
d397712b | 643 | kfree(pages); |
771ed689 | 644 | } |
771ed689 | 645 | |
40ae837b FM |
646 | static void free_async_extent_pages(struct async_extent *async_extent) |
647 | { | |
648 | int i; | |
649 | ||
650 | if (!async_extent->pages) | |
651 | return; | |
652 | ||
653 | for (i = 0; i < async_extent->nr_pages; i++) { | |
654 | WARN_ON(async_extent->pages[i]->mapping); | |
655 | page_cache_release(async_extent->pages[i]); | |
656 | } | |
657 | kfree(async_extent->pages); | |
658 | async_extent->nr_pages = 0; | |
659 | async_extent->pages = NULL; | |
771ed689 CM |
660 | } |
661 | ||
662 | /* | |
663 | * phase two of compressed writeback. This is the ordered portion | |
664 | * of the code, which only gets called in the order the work was | |
665 | * queued. We walk all the async extents created by compress_file_range | |
666 | * and send them down to the disk. | |
667 | */ | |
dec8f175 | 668 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
669 | struct async_cow *async_cow) |
670 | { | |
671 | struct async_extent *async_extent; | |
672 | u64 alloc_hint = 0; | |
771ed689 CM |
673 | struct btrfs_key ins; |
674 | struct extent_map *em; | |
675 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
676 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
677 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 678 | int ret = 0; |
771ed689 | 679 | |
3e04e7f1 | 680 | again: |
d397712b | 681 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
682 | async_extent = list_entry(async_cow->extents.next, |
683 | struct async_extent, list); | |
684 | list_del(&async_extent->list); | |
c8b97818 | 685 | |
771ed689 CM |
686 | io_tree = &BTRFS_I(inode)->io_tree; |
687 | ||
f5a84ee3 | 688 | retry: |
771ed689 CM |
689 | /* did the compression code fall back to uncompressed IO? */ |
690 | if (!async_extent->pages) { | |
691 | int page_started = 0; | |
692 | unsigned long nr_written = 0; | |
693 | ||
694 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 695 | async_extent->start + |
d0082371 | 696 | async_extent->ram_size - 1); |
771ed689 CM |
697 | |
698 | /* allocate blocks */ | |
f5a84ee3 JB |
699 | ret = cow_file_range(inode, async_cow->locked_page, |
700 | async_extent->start, | |
701 | async_extent->start + | |
702 | async_extent->ram_size - 1, | |
703 | &page_started, &nr_written, 0); | |
771ed689 | 704 | |
79787eaa JM |
705 | /* JDM XXX */ |
706 | ||
771ed689 CM |
707 | /* |
708 | * if page_started, cow_file_range inserted an | |
709 | * inline extent and took care of all the unlocking | |
710 | * and IO for us. Otherwise, we need to submit | |
711 | * all those pages down to the drive. | |
712 | */ | |
f5a84ee3 | 713 | if (!page_started && !ret) |
771ed689 CM |
714 | extent_write_locked_range(io_tree, |
715 | inode, async_extent->start, | |
d397712b | 716 | async_extent->start + |
771ed689 CM |
717 | async_extent->ram_size - 1, |
718 | btrfs_get_extent, | |
719 | WB_SYNC_ALL); | |
3e04e7f1 JB |
720 | else if (ret) |
721 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
722 | kfree(async_extent); |
723 | cond_resched(); | |
724 | continue; | |
725 | } | |
726 | ||
727 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 728 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 729 | |
00361589 | 730 | ret = btrfs_reserve_extent(root, |
771ed689 CM |
731 | async_extent->compressed_size, |
732 | async_extent->compressed_size, | |
e570fd27 | 733 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 734 | if (ret) { |
40ae837b | 735 | free_async_extent_pages(async_extent); |
3e04e7f1 | 736 | |
fdf8e2ea JB |
737 | if (ret == -ENOSPC) { |
738 | unlock_extent(io_tree, async_extent->start, | |
739 | async_extent->start + | |
740 | async_extent->ram_size - 1); | |
ce62003f LB |
741 | |
742 | /* | |
743 | * we need to redirty the pages if we decide to | |
744 | * fallback to uncompressed IO, otherwise we | |
745 | * will not submit these pages down to lower | |
746 | * layers. | |
747 | */ | |
748 | extent_range_redirty_for_io(inode, | |
749 | async_extent->start, | |
750 | async_extent->start + | |
751 | async_extent->ram_size - 1); | |
752 | ||
79787eaa | 753 | goto retry; |
fdf8e2ea | 754 | } |
3e04e7f1 | 755 | goto out_free; |
f5a84ee3 | 756 | } |
31193213 DY |
757 | btrfs_qgroup_update_reserved_bytes(root->fs_info, |
758 | root->root_key.objectid, | |
759 | ins.offset, 1); | |
c2167754 YZ |
760 | /* |
761 | * here we're doing allocation and writeback of the | |
762 | * compressed pages | |
763 | */ | |
764 | btrfs_drop_extent_cache(inode, async_extent->start, | |
765 | async_extent->start + | |
766 | async_extent->ram_size - 1, 0); | |
767 | ||
172ddd60 | 768 | em = alloc_extent_map(); |
b9aa55be LB |
769 | if (!em) { |
770 | ret = -ENOMEM; | |
3e04e7f1 | 771 | goto out_free_reserve; |
b9aa55be | 772 | } |
771ed689 CM |
773 | em->start = async_extent->start; |
774 | em->len = async_extent->ram_size; | |
445a6944 | 775 | em->orig_start = em->start; |
2ab28f32 JB |
776 | em->mod_start = em->start; |
777 | em->mod_len = em->len; | |
c8b97818 | 778 | |
771ed689 CM |
779 | em->block_start = ins.objectid; |
780 | em->block_len = ins.offset; | |
b4939680 | 781 | em->orig_block_len = ins.offset; |
cc95bef6 | 782 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 783 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 784 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
785 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
786 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 787 | em->generation = -1; |
771ed689 | 788 | |
d397712b | 789 | while (1) { |
890871be | 790 | write_lock(&em_tree->lock); |
09a2a8f9 | 791 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 792 | write_unlock(&em_tree->lock); |
771ed689 CM |
793 | if (ret != -EEXIST) { |
794 | free_extent_map(em); | |
795 | break; | |
796 | } | |
797 | btrfs_drop_extent_cache(inode, async_extent->start, | |
798 | async_extent->start + | |
799 | async_extent->ram_size - 1, 0); | |
800 | } | |
801 | ||
3e04e7f1 JB |
802 | if (ret) |
803 | goto out_free_reserve; | |
804 | ||
261507a0 LZ |
805 | ret = btrfs_add_ordered_extent_compress(inode, |
806 | async_extent->start, | |
807 | ins.objectid, | |
808 | async_extent->ram_size, | |
809 | ins.offset, | |
810 | BTRFS_ORDERED_COMPRESSED, | |
811 | async_extent->compress_type); | |
d9f85963 FM |
812 | if (ret) { |
813 | btrfs_drop_extent_cache(inode, async_extent->start, | |
814 | async_extent->start + | |
815 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 816 | goto out_free_reserve; |
d9f85963 | 817 | } |
771ed689 | 818 | |
771ed689 CM |
819 | /* |
820 | * clear dirty, set writeback and unlock the pages. | |
821 | */ | |
c2790a2e | 822 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
823 | async_extent->start + |
824 | async_extent->ram_size - 1, | |
151a41bc JB |
825 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
826 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 827 | PAGE_SET_WRITEBACK); |
771ed689 | 828 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
829 | async_extent->start, |
830 | async_extent->ram_size, | |
831 | ins.objectid, | |
832 | ins.offset, async_extent->pages, | |
833 | async_extent->nr_pages); | |
fce2a4e6 FM |
834 | if (ret) { |
835 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
836 | struct page *p = async_extent->pages[0]; | |
837 | const u64 start = async_extent->start; | |
838 | const u64 end = start + async_extent->ram_size - 1; | |
839 | ||
840 | p->mapping = inode->i_mapping; | |
841 | tree->ops->writepage_end_io_hook(p, start, end, | |
842 | NULL, 0); | |
843 | p->mapping = NULL; | |
844 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
845 | PAGE_END_WRITEBACK | | |
846 | PAGE_SET_ERROR); | |
40ae837b | 847 | free_async_extent_pages(async_extent); |
fce2a4e6 | 848 | } |
771ed689 CM |
849 | alloc_hint = ins.objectid + ins.offset; |
850 | kfree(async_extent); | |
851 | cond_resched(); | |
852 | } | |
dec8f175 | 853 | return; |
3e04e7f1 | 854 | out_free_reserve: |
e570fd27 | 855 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 856 | out_free: |
c2790a2e | 857 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
858 | async_extent->start + |
859 | async_extent->ram_size - 1, | |
c2790a2e | 860 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
861 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
862 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
863 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
864 | PAGE_SET_ERROR); | |
40ae837b | 865 | free_async_extent_pages(async_extent); |
79787eaa | 866 | kfree(async_extent); |
3e04e7f1 | 867 | goto again; |
771ed689 CM |
868 | } |
869 | ||
4b46fce2 JB |
870 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
871 | u64 num_bytes) | |
872 | { | |
873 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
874 | struct extent_map *em; | |
875 | u64 alloc_hint = 0; | |
876 | ||
877 | read_lock(&em_tree->lock); | |
878 | em = search_extent_mapping(em_tree, start, num_bytes); | |
879 | if (em) { | |
880 | /* | |
881 | * if block start isn't an actual block number then find the | |
882 | * first block in this inode and use that as a hint. If that | |
883 | * block is also bogus then just don't worry about it. | |
884 | */ | |
885 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
886 | free_extent_map(em); | |
887 | em = search_extent_mapping(em_tree, 0, 0); | |
888 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
889 | alloc_hint = em->block_start; | |
890 | if (em) | |
891 | free_extent_map(em); | |
892 | } else { | |
893 | alloc_hint = em->block_start; | |
894 | free_extent_map(em); | |
895 | } | |
896 | } | |
897 | read_unlock(&em_tree->lock); | |
898 | ||
899 | return alloc_hint; | |
900 | } | |
901 | ||
771ed689 CM |
902 | /* |
903 | * when extent_io.c finds a delayed allocation range in the file, | |
904 | * the call backs end up in this code. The basic idea is to | |
905 | * allocate extents on disk for the range, and create ordered data structs | |
906 | * in ram to track those extents. | |
907 | * | |
908 | * locked_page is the page that writepage had locked already. We use | |
909 | * it to make sure we don't do extra locks or unlocks. | |
910 | * | |
911 | * *page_started is set to one if we unlock locked_page and do everything | |
912 | * required to start IO on it. It may be clean and already done with | |
913 | * IO when we return. | |
914 | */ | |
00361589 JB |
915 | static noinline int cow_file_range(struct inode *inode, |
916 | struct page *locked_page, | |
917 | u64 start, u64 end, int *page_started, | |
918 | unsigned long *nr_written, | |
919 | int unlock) | |
771ed689 | 920 | { |
00361589 | 921 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
922 | u64 alloc_hint = 0; |
923 | u64 num_bytes; | |
924 | unsigned long ram_size; | |
925 | u64 disk_num_bytes; | |
926 | u64 cur_alloc_size; | |
927 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
928 | struct btrfs_key ins; |
929 | struct extent_map *em; | |
930 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
931 | int ret = 0; | |
932 | ||
02ecd2c2 JB |
933 | if (btrfs_is_free_space_inode(inode)) { |
934 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
935 | ret = -EINVAL; |
936 | goto out_unlock; | |
02ecd2c2 | 937 | } |
771ed689 | 938 | |
fda2832f | 939 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
940 | num_bytes = max(blocksize, num_bytes); |
941 | disk_num_bytes = num_bytes; | |
771ed689 | 942 | |
4cb5300b | 943 | /* if this is a small write inside eof, kick off defrag */ |
4cb13e5d LB |
944 | if (num_bytes < 64 * 1024 && |
945 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
00361589 | 946 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 947 | |
771ed689 CM |
948 | if (start == 0) { |
949 | /* lets try to make an inline extent */ | |
00361589 JB |
950 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
951 | NULL); | |
771ed689 | 952 | if (ret == 0) { |
c2790a2e JB |
953 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
954 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc | 955 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
956 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
957 | PAGE_END_WRITEBACK); | |
c2167754 | 958 | |
771ed689 CM |
959 | *nr_written = *nr_written + |
960 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | |
961 | *page_started = 1; | |
771ed689 | 962 | goto out; |
79787eaa | 963 | } else if (ret < 0) { |
79787eaa | 964 | goto out_unlock; |
771ed689 CM |
965 | } |
966 | } | |
967 | ||
968 | BUG_ON(disk_num_bytes > | |
6c41761f | 969 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 970 | |
4b46fce2 | 971 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
972 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
973 | ||
d397712b | 974 | while (disk_num_bytes > 0) { |
a791e35e CM |
975 | unsigned long op; |
976 | ||
287a0ab9 | 977 | cur_alloc_size = disk_num_bytes; |
00361589 | 978 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
771ed689 | 979 | root->sectorsize, 0, alloc_hint, |
e570fd27 | 980 | &ins, 1, 1); |
00361589 | 981 | if (ret < 0) |
79787eaa | 982 | goto out_unlock; |
d397712b | 983 | |
31193213 DY |
984 | btrfs_qgroup_update_reserved_bytes(root->fs_info, |
985 | root->root_key.objectid, | |
986 | ins.offset, 1); | |
987 | ||
172ddd60 | 988 | em = alloc_extent_map(); |
b9aa55be LB |
989 | if (!em) { |
990 | ret = -ENOMEM; | |
ace68bac | 991 | goto out_reserve; |
b9aa55be | 992 | } |
e6dcd2dc | 993 | em->start = start; |
445a6944 | 994 | em->orig_start = em->start; |
771ed689 CM |
995 | ram_size = ins.offset; |
996 | em->len = ins.offset; | |
2ab28f32 JB |
997 | em->mod_start = em->start; |
998 | em->mod_len = em->len; | |
c8b97818 | 999 | |
e6dcd2dc | 1000 | em->block_start = ins.objectid; |
c8b97818 | 1001 | em->block_len = ins.offset; |
b4939680 | 1002 | em->orig_block_len = ins.offset; |
cc95bef6 | 1003 | em->ram_bytes = ram_size; |
e6dcd2dc | 1004 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 1005 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 1006 | em->generation = -1; |
c8b97818 | 1007 | |
d397712b | 1008 | while (1) { |
890871be | 1009 | write_lock(&em_tree->lock); |
09a2a8f9 | 1010 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1011 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1012 | if (ret != -EEXIST) { |
1013 | free_extent_map(em); | |
1014 | break; | |
1015 | } | |
1016 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1017 | start + ram_size - 1, 0); |
e6dcd2dc | 1018 | } |
ace68bac LB |
1019 | if (ret) |
1020 | goto out_reserve; | |
e6dcd2dc | 1021 | |
98d20f67 | 1022 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1023 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1024 | ram_size, cur_alloc_size, 0); |
ace68bac | 1025 | if (ret) |
d9f85963 | 1026 | goto out_drop_extent_cache; |
c8b97818 | 1027 | |
17d217fe YZ |
1028 | if (root->root_key.objectid == |
1029 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1030 | ret = btrfs_reloc_clone_csums(inode, start, | |
1031 | cur_alloc_size); | |
00361589 | 1032 | if (ret) |
d9f85963 | 1033 | goto out_drop_extent_cache; |
17d217fe YZ |
1034 | } |
1035 | ||
d397712b | 1036 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1037 | break; |
d397712b | 1038 | |
c8b97818 CM |
1039 | /* we're not doing compressed IO, don't unlock the first |
1040 | * page (which the caller expects to stay locked), don't | |
1041 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1042 | * |
1043 | * Do set the Private2 bit so we know this page was properly | |
1044 | * setup for writepage | |
c8b97818 | 1045 | */ |
c2790a2e JB |
1046 | op = unlock ? PAGE_UNLOCK : 0; |
1047 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1048 | |
c2790a2e JB |
1049 | extent_clear_unlock_delalloc(inode, start, |
1050 | start + ram_size - 1, locked_page, | |
1051 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
1052 | op); | |
c8b97818 | 1053 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1054 | num_bytes -= cur_alloc_size; |
1055 | alloc_hint = ins.objectid + ins.offset; | |
1056 | start += cur_alloc_size; | |
b888db2b | 1057 | } |
79787eaa | 1058 | out: |
be20aa9d | 1059 | return ret; |
b7d5b0a8 | 1060 | |
d9f85963 FM |
1061 | out_drop_extent_cache: |
1062 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1063 | out_reserve: |
e570fd27 | 1064 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 1065 | out_unlock: |
c2790a2e | 1066 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
151a41bc JB |
1067 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1068 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1069 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1070 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1071 | goto out; |
771ed689 | 1072 | } |
c8b97818 | 1073 | |
771ed689 CM |
1074 | /* |
1075 | * work queue call back to started compression on a file and pages | |
1076 | */ | |
1077 | static noinline void async_cow_start(struct btrfs_work *work) | |
1078 | { | |
1079 | struct async_cow *async_cow; | |
1080 | int num_added = 0; | |
1081 | async_cow = container_of(work, struct async_cow, work); | |
1082 | ||
1083 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1084 | async_cow->start, async_cow->end, async_cow, | |
1085 | &num_added); | |
8180ef88 | 1086 | if (num_added == 0) { |
cb77fcd8 | 1087 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1088 | async_cow->inode = NULL; |
8180ef88 | 1089 | } |
771ed689 CM |
1090 | } |
1091 | ||
1092 | /* | |
1093 | * work queue call back to submit previously compressed pages | |
1094 | */ | |
1095 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1096 | { | |
1097 | struct async_cow *async_cow; | |
1098 | struct btrfs_root *root; | |
1099 | unsigned long nr_pages; | |
1100 | ||
1101 | async_cow = container_of(work, struct async_cow, work); | |
1102 | ||
1103 | root = async_cow->root; | |
1104 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> | |
1105 | PAGE_CACHE_SHIFT; | |
1106 | ||
66657b31 | 1107 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
287082b0 | 1108 | 5 * 1024 * 1024 && |
771ed689 CM |
1109 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1110 | wake_up(&root->fs_info->async_submit_wait); | |
1111 | ||
d397712b | 1112 | if (async_cow->inode) |
771ed689 | 1113 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1114 | } |
c8b97818 | 1115 | |
771ed689 CM |
1116 | static noinline void async_cow_free(struct btrfs_work *work) |
1117 | { | |
1118 | struct async_cow *async_cow; | |
1119 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1120 | if (async_cow->inode) |
cb77fcd8 | 1121 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1122 | kfree(async_cow); |
1123 | } | |
1124 | ||
1125 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1126 | u64 start, u64 end, int *page_started, | |
1127 | unsigned long *nr_written) | |
1128 | { | |
1129 | struct async_cow *async_cow; | |
1130 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1131 | unsigned long nr_pages; | |
1132 | u64 cur_end; | |
287082b0 | 1133 | int limit = 10 * 1024 * 1024; |
771ed689 | 1134 | |
a3429ab7 CM |
1135 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1136 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1137 | while (start < end) { |
771ed689 | 1138 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1139 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1140 | async_cow->inode = igrab(inode); |
771ed689 CM |
1141 | async_cow->root = root; |
1142 | async_cow->locked_page = locked_page; | |
1143 | async_cow->start = start; | |
1144 | ||
f79707b0 WS |
1145 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
1146 | !btrfs_test_opt(root, FORCE_COMPRESS)) | |
771ed689 CM |
1147 | cur_end = end; |
1148 | else | |
1149 | cur_end = min(end, start + 512 * 1024 - 1); | |
1150 | ||
1151 | async_cow->end = cur_end; | |
1152 | INIT_LIST_HEAD(&async_cow->extents); | |
1153 | ||
9e0af237 LB |
1154 | btrfs_init_work(&async_cow->work, |
1155 | btrfs_delalloc_helper, | |
1156 | async_cow_start, async_cow_submit, | |
1157 | async_cow_free); | |
771ed689 | 1158 | |
771ed689 CM |
1159 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
1160 | PAGE_CACHE_SHIFT; | |
1161 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); | |
1162 | ||
afe3d242 QW |
1163 | btrfs_queue_work(root->fs_info->delalloc_workers, |
1164 | &async_cow->work); | |
771ed689 CM |
1165 | |
1166 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1167 | wait_event(root->fs_info->async_submit_wait, | |
1168 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1169 | limit)); | |
1170 | } | |
1171 | ||
d397712b | 1172 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1173 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1174 | wait_event(root->fs_info->async_submit_wait, | |
1175 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1176 | 0)); | |
1177 | } | |
1178 | ||
1179 | *nr_written += nr_pages; | |
1180 | start = cur_end + 1; | |
1181 | } | |
1182 | *page_started = 1; | |
1183 | return 0; | |
be20aa9d CM |
1184 | } |
1185 | ||
d397712b | 1186 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1187 | u64 bytenr, u64 num_bytes) |
1188 | { | |
1189 | int ret; | |
1190 | struct btrfs_ordered_sum *sums; | |
1191 | LIST_HEAD(list); | |
1192 | ||
07d400a6 | 1193 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1194 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1195 | if (ret == 0 && list_empty(&list)) |
1196 | return 0; | |
1197 | ||
1198 | while (!list_empty(&list)) { | |
1199 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1200 | list_del(&sums->list); | |
1201 | kfree(sums); | |
1202 | } | |
1203 | return 1; | |
1204 | } | |
1205 | ||
d352ac68 CM |
1206 | /* |
1207 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1208 | * of the extents that exist in the file, and COWs the file as required. | |
1209 | * | |
1210 | * If no cow copies or snapshots exist, we write directly to the existing | |
1211 | * blocks on disk | |
1212 | */ | |
7f366cfe CM |
1213 | static noinline int run_delalloc_nocow(struct inode *inode, |
1214 | struct page *locked_page, | |
771ed689 CM |
1215 | u64 start, u64 end, int *page_started, int force, |
1216 | unsigned long *nr_written) | |
be20aa9d | 1217 | { |
be20aa9d | 1218 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1219 | struct btrfs_trans_handle *trans; |
be20aa9d | 1220 | struct extent_buffer *leaf; |
be20aa9d | 1221 | struct btrfs_path *path; |
80ff3856 | 1222 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1223 | struct btrfs_key found_key; |
80ff3856 YZ |
1224 | u64 cow_start; |
1225 | u64 cur_offset; | |
1226 | u64 extent_end; | |
5d4f98a2 | 1227 | u64 extent_offset; |
80ff3856 YZ |
1228 | u64 disk_bytenr; |
1229 | u64 num_bytes; | |
b4939680 | 1230 | u64 disk_num_bytes; |
cc95bef6 | 1231 | u64 ram_bytes; |
80ff3856 | 1232 | int extent_type; |
79787eaa | 1233 | int ret, err; |
d899e052 | 1234 | int type; |
80ff3856 YZ |
1235 | int nocow; |
1236 | int check_prev = 1; | |
82d5902d | 1237 | bool nolock; |
33345d01 | 1238 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1239 | |
1240 | path = btrfs_alloc_path(); | |
17ca04af | 1241 | if (!path) { |
c2790a2e JB |
1242 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1243 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1244 | EXTENT_DO_ACCOUNTING | |
1245 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1246 | PAGE_CLEAR_DIRTY | |
1247 | PAGE_SET_WRITEBACK | | |
1248 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1249 | return -ENOMEM; |
17ca04af | 1250 | } |
82d5902d | 1251 | |
83eea1f1 | 1252 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1253 | |
1254 | if (nolock) | |
7a7eaa40 | 1255 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1256 | else |
7a7eaa40 | 1257 | trans = btrfs_join_transaction(root); |
ff5714cc | 1258 | |
79787eaa | 1259 | if (IS_ERR(trans)) { |
c2790a2e JB |
1260 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1261 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1262 | EXTENT_DO_ACCOUNTING | |
1263 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1264 | PAGE_CLEAR_DIRTY | |
1265 | PAGE_SET_WRITEBACK | | |
1266 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1267 | btrfs_free_path(path); |
1268 | return PTR_ERR(trans); | |
1269 | } | |
1270 | ||
74b21075 | 1271 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1272 | |
80ff3856 YZ |
1273 | cow_start = (u64)-1; |
1274 | cur_offset = start; | |
1275 | while (1) { | |
33345d01 | 1276 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1277 | cur_offset, 0); |
d788a349 | 1278 | if (ret < 0) |
79787eaa | 1279 | goto error; |
80ff3856 YZ |
1280 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1281 | leaf = path->nodes[0]; | |
1282 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1283 | path->slots[0] - 1); | |
33345d01 | 1284 | if (found_key.objectid == ino && |
80ff3856 YZ |
1285 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1286 | path->slots[0]--; | |
1287 | } | |
1288 | check_prev = 0; | |
1289 | next_slot: | |
1290 | leaf = path->nodes[0]; | |
1291 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1292 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1293 | if (ret < 0) |
79787eaa | 1294 | goto error; |
80ff3856 YZ |
1295 | if (ret > 0) |
1296 | break; | |
1297 | leaf = path->nodes[0]; | |
1298 | } | |
be20aa9d | 1299 | |
80ff3856 YZ |
1300 | nocow = 0; |
1301 | disk_bytenr = 0; | |
17d217fe | 1302 | num_bytes = 0; |
80ff3856 YZ |
1303 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1304 | ||
33345d01 | 1305 | if (found_key.objectid > ino || |
80ff3856 YZ |
1306 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
1307 | found_key.offset > end) | |
1308 | break; | |
1309 | ||
1310 | if (found_key.offset > cur_offset) { | |
1311 | extent_end = found_key.offset; | |
e9061e21 | 1312 | extent_type = 0; |
80ff3856 YZ |
1313 | goto out_check; |
1314 | } | |
1315 | ||
1316 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1317 | struct btrfs_file_extent_item); | |
1318 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1319 | ||
cc95bef6 | 1320 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1321 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1322 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1323 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1324 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1325 | extent_end = found_key.offset + |
1326 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1327 | disk_num_bytes = |
1328 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1329 | if (extent_end <= start) { |
1330 | path->slots[0]++; | |
1331 | goto next_slot; | |
1332 | } | |
17d217fe YZ |
1333 | if (disk_bytenr == 0) |
1334 | goto out_check; | |
80ff3856 YZ |
1335 | if (btrfs_file_extent_compression(leaf, fi) || |
1336 | btrfs_file_extent_encryption(leaf, fi) || | |
1337 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1338 | goto out_check; | |
d899e052 YZ |
1339 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1340 | goto out_check; | |
d2fb3437 | 1341 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1342 | goto out_check; |
33345d01 | 1343 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1344 | found_key.offset - |
1345 | extent_offset, disk_bytenr)) | |
17d217fe | 1346 | goto out_check; |
5d4f98a2 | 1347 | disk_bytenr += extent_offset; |
17d217fe YZ |
1348 | disk_bytenr += cur_offset - found_key.offset; |
1349 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1350 | /* |
1351 | * if there are pending snapshots for this root, | |
1352 | * we fall into common COW way. | |
1353 | */ | |
1354 | if (!nolock) { | |
9ea24bbe | 1355 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1356 | if (!err) |
1357 | goto out_check; | |
1358 | } | |
17d217fe YZ |
1359 | /* |
1360 | * force cow if csum exists in the range. | |
1361 | * this ensure that csum for a given extent are | |
1362 | * either valid or do not exist. | |
1363 | */ | |
1364 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1365 | goto out_check; | |
80ff3856 YZ |
1366 | nocow = 1; |
1367 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1368 | extent_end = found_key.offset + | |
514ac8ad CM |
1369 | btrfs_file_extent_inline_len(leaf, |
1370 | path->slots[0], fi); | |
80ff3856 YZ |
1371 | extent_end = ALIGN(extent_end, root->sectorsize); |
1372 | } else { | |
1373 | BUG_ON(1); | |
1374 | } | |
1375 | out_check: | |
1376 | if (extent_end <= start) { | |
1377 | path->slots[0]++; | |
e9894fd3 | 1378 | if (!nolock && nocow) |
9ea24bbe | 1379 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1380 | goto next_slot; |
1381 | } | |
1382 | if (!nocow) { | |
1383 | if (cow_start == (u64)-1) | |
1384 | cow_start = cur_offset; | |
1385 | cur_offset = extent_end; | |
1386 | if (cur_offset > end) | |
1387 | break; | |
1388 | path->slots[0]++; | |
1389 | goto next_slot; | |
7ea394f1 YZ |
1390 | } |
1391 | ||
b3b4aa74 | 1392 | btrfs_release_path(path); |
80ff3856 | 1393 | if (cow_start != (u64)-1) { |
00361589 JB |
1394 | ret = cow_file_range(inode, locked_page, |
1395 | cow_start, found_key.offset - 1, | |
1396 | page_started, nr_written, 1); | |
e9894fd3 WS |
1397 | if (ret) { |
1398 | if (!nolock && nocow) | |
9ea24bbe | 1399 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1400 | goto error; |
e9894fd3 | 1401 | } |
80ff3856 | 1402 | cow_start = (u64)-1; |
7ea394f1 | 1403 | } |
80ff3856 | 1404 | |
d899e052 YZ |
1405 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1406 | struct extent_map *em; | |
1407 | struct extent_map_tree *em_tree; | |
1408 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1409 | em = alloc_extent_map(); |
79787eaa | 1410 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1411 | em->start = cur_offset; |
70c8a91c | 1412 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1413 | em->len = num_bytes; |
1414 | em->block_len = num_bytes; | |
1415 | em->block_start = disk_bytenr; | |
b4939680 | 1416 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1417 | em->ram_bytes = ram_bytes; |
d899e052 | 1418 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1419 | em->mod_start = em->start; |
1420 | em->mod_len = em->len; | |
d899e052 | 1421 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1422 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1423 | em->generation = -1; |
d899e052 | 1424 | while (1) { |
890871be | 1425 | write_lock(&em_tree->lock); |
09a2a8f9 | 1426 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1427 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1428 | if (ret != -EEXIST) { |
1429 | free_extent_map(em); | |
1430 | break; | |
1431 | } | |
1432 | btrfs_drop_extent_cache(inode, em->start, | |
1433 | em->start + em->len - 1, 0); | |
1434 | } | |
1435 | type = BTRFS_ORDERED_PREALLOC; | |
1436 | } else { | |
1437 | type = BTRFS_ORDERED_NOCOW; | |
1438 | } | |
80ff3856 YZ |
1439 | |
1440 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1441 | num_bytes, num_bytes, type); |
79787eaa | 1442 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1443 | |
efa56464 YZ |
1444 | if (root->root_key.objectid == |
1445 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1446 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1447 | num_bytes); | |
e9894fd3 WS |
1448 | if (ret) { |
1449 | if (!nolock && nocow) | |
9ea24bbe | 1450 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1451 | goto error; |
e9894fd3 | 1452 | } |
efa56464 YZ |
1453 | } |
1454 | ||
c2790a2e JB |
1455 | extent_clear_unlock_delalloc(inode, cur_offset, |
1456 | cur_offset + num_bytes - 1, | |
1457 | locked_page, EXTENT_LOCKED | | |
1458 | EXTENT_DELALLOC, PAGE_UNLOCK | | |
1459 | PAGE_SET_PRIVATE2); | |
e9894fd3 | 1460 | if (!nolock && nocow) |
9ea24bbe | 1461 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1462 | cur_offset = extent_end; |
1463 | if (cur_offset > end) | |
1464 | break; | |
be20aa9d | 1465 | } |
b3b4aa74 | 1466 | btrfs_release_path(path); |
80ff3856 | 1467 | |
17ca04af | 1468 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1469 | cow_start = cur_offset; |
17ca04af JB |
1470 | cur_offset = end; |
1471 | } | |
1472 | ||
80ff3856 | 1473 | if (cow_start != (u64)-1) { |
00361589 JB |
1474 | ret = cow_file_range(inode, locked_page, cow_start, end, |
1475 | page_started, nr_written, 1); | |
d788a349 | 1476 | if (ret) |
79787eaa | 1477 | goto error; |
80ff3856 YZ |
1478 | } |
1479 | ||
79787eaa | 1480 | error: |
a698d075 | 1481 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1482 | if (!ret) |
1483 | ret = err; | |
1484 | ||
17ca04af | 1485 | if (ret && cur_offset < end) |
c2790a2e JB |
1486 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
1487 | locked_page, EXTENT_LOCKED | | |
151a41bc JB |
1488 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1489 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1490 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1491 | PAGE_SET_WRITEBACK | |
1492 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1493 | btrfs_free_path(path); |
79787eaa | 1494 | return ret; |
be20aa9d CM |
1495 | } |
1496 | ||
47059d93 WS |
1497 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1498 | { | |
1499 | ||
1500 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1501 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1502 | return 0; | |
1503 | ||
1504 | /* | |
1505 | * @defrag_bytes is a hint value, no spinlock held here, | |
1506 | * if is not zero, it means the file is defragging. | |
1507 | * Force cow if given extent needs to be defragged. | |
1508 | */ | |
1509 | if (BTRFS_I(inode)->defrag_bytes && | |
1510 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1511 | EXTENT_DEFRAG, 0, NULL)) | |
1512 | return 1; | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
d352ac68 CM |
1517 | /* |
1518 | * extent_io.c call back to do delayed allocation processing | |
1519 | */ | |
c8b97818 | 1520 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1521 | u64 start, u64 end, int *page_started, |
1522 | unsigned long *nr_written) | |
be20aa9d | 1523 | { |
be20aa9d | 1524 | int ret; |
47059d93 | 1525 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1526 | |
47059d93 | 1527 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1528 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1529 | page_started, 1, nr_written); |
47059d93 | 1530 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1531 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1532 | page_started, 0, nr_written); |
7816030e | 1533 | } else if (!inode_need_compress(inode)) { |
7f366cfe CM |
1534 | ret = cow_file_range(inode, locked_page, start, end, |
1535 | page_started, nr_written, 1); | |
7ddf5a42 JB |
1536 | } else { |
1537 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1538 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1539 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1540 | page_started, nr_written); |
7ddf5a42 | 1541 | } |
b888db2b CM |
1542 | return ret; |
1543 | } | |
1544 | ||
1bf85046 JM |
1545 | static void btrfs_split_extent_hook(struct inode *inode, |
1546 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1547 | { |
dcab6a3b JB |
1548 | u64 size; |
1549 | ||
0ca1f7ce | 1550 | /* not delalloc, ignore it */ |
9ed74f2d | 1551 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1552 | return; |
9ed74f2d | 1553 | |
dcab6a3b JB |
1554 | size = orig->end - orig->start + 1; |
1555 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1556 | u64 num_extents; | |
1557 | u64 new_size; | |
1558 | ||
1559 | /* | |
ba117213 JB |
1560 | * See the explanation in btrfs_merge_extent_hook, the same |
1561 | * applies here, just in reverse. | |
dcab6a3b JB |
1562 | */ |
1563 | new_size = orig->end - split + 1; | |
ba117213 | 1564 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
dcab6a3b | 1565 | BTRFS_MAX_EXTENT_SIZE); |
ba117213 JB |
1566 | new_size = split - orig->start; |
1567 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1568 | BTRFS_MAX_EXTENT_SIZE); | |
1569 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1570 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) | |
dcab6a3b JB |
1571 | return; |
1572 | } | |
1573 | ||
9e0baf60 JB |
1574 | spin_lock(&BTRFS_I(inode)->lock); |
1575 | BTRFS_I(inode)->outstanding_extents++; | |
1576 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1577 | } |
1578 | ||
1579 | /* | |
1580 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1581 | * extents so we can keep track of new extents that are just merged onto old | |
1582 | * extents, such as when we are doing sequential writes, so we can properly | |
1583 | * account for the metadata space we'll need. | |
1584 | */ | |
1bf85046 JM |
1585 | static void btrfs_merge_extent_hook(struct inode *inode, |
1586 | struct extent_state *new, | |
1587 | struct extent_state *other) | |
9ed74f2d | 1588 | { |
dcab6a3b JB |
1589 | u64 new_size, old_size; |
1590 | u64 num_extents; | |
1591 | ||
9ed74f2d JB |
1592 | /* not delalloc, ignore it */ |
1593 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1594 | return; |
9ed74f2d | 1595 | |
8461a3de JB |
1596 | if (new->start > other->start) |
1597 | new_size = new->end - other->start + 1; | |
1598 | else | |
1599 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1600 | |
1601 | /* we're not bigger than the max, unreserve the space and go */ | |
1602 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1603 | spin_lock(&BTRFS_I(inode)->lock); | |
1604 | BTRFS_I(inode)->outstanding_extents--; | |
1605 | spin_unlock(&BTRFS_I(inode)->lock); | |
1606 | return; | |
1607 | } | |
1608 | ||
1609 | /* | |
ba117213 JB |
1610 | * We have to add up either side to figure out how many extents were |
1611 | * accounted for before we merged into one big extent. If the number of | |
1612 | * extents we accounted for is <= the amount we need for the new range | |
1613 | * then we can return, otherwise drop. Think of it like this | |
1614 | * | |
1615 | * [ 4k][MAX_SIZE] | |
1616 | * | |
1617 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1618 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1619 | * we have 1 so they are == and we can return. But in this case | |
1620 | * | |
1621 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1622 | * | |
1623 | * Each range on their own accounts for 2 extents, but merged together | |
1624 | * they are only 3 extents worth of accounting, so we need to drop in | |
1625 | * this case. | |
dcab6a3b | 1626 | */ |
ba117213 | 1627 | old_size = other->end - other->start + 1; |
dcab6a3b JB |
1628 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
1629 | BTRFS_MAX_EXTENT_SIZE); | |
ba117213 JB |
1630 | old_size = new->end - new->start + 1; |
1631 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1632 | BTRFS_MAX_EXTENT_SIZE); | |
1633 | ||
dcab6a3b | 1634 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
ba117213 | 1635 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
dcab6a3b JB |
1636 | return; |
1637 | ||
9e0baf60 JB |
1638 | spin_lock(&BTRFS_I(inode)->lock); |
1639 | BTRFS_I(inode)->outstanding_extents--; | |
1640 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1641 | } |
1642 | ||
eb73c1b7 MX |
1643 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1644 | struct inode *inode) | |
1645 | { | |
1646 | spin_lock(&root->delalloc_lock); | |
1647 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1648 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1649 | &root->delalloc_inodes); | |
1650 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1651 | &BTRFS_I(inode)->runtime_flags); | |
1652 | root->nr_delalloc_inodes++; | |
1653 | if (root->nr_delalloc_inodes == 1) { | |
1654 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1655 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1656 | list_add_tail(&root->delalloc_root, | |
1657 | &root->fs_info->delalloc_roots); | |
1658 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1659 | } | |
1660 | } | |
1661 | spin_unlock(&root->delalloc_lock); | |
1662 | } | |
1663 | ||
1664 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1665 | struct inode *inode) | |
1666 | { | |
1667 | spin_lock(&root->delalloc_lock); | |
1668 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1669 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1670 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1671 | &BTRFS_I(inode)->runtime_flags); | |
1672 | root->nr_delalloc_inodes--; | |
1673 | if (!root->nr_delalloc_inodes) { | |
1674 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1675 | BUG_ON(list_empty(&root->delalloc_root)); | |
1676 | list_del_init(&root->delalloc_root); | |
1677 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1678 | } | |
1679 | } | |
1680 | spin_unlock(&root->delalloc_lock); | |
1681 | } | |
1682 | ||
d352ac68 CM |
1683 | /* |
1684 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1685 | * bytes in this file, and to maintain the list of inodes that | |
1686 | * have pending delalloc work to be done. | |
1687 | */ | |
1bf85046 | 1688 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1689 | struct extent_state *state, unsigned *bits) |
291d673e | 1690 | { |
9ed74f2d | 1691 | |
47059d93 WS |
1692 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1693 | WARN_ON(1); | |
75eff68e CM |
1694 | /* |
1695 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1696 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1697 | * bit, which is only set or cleared with irqs on |
1698 | */ | |
0ca1f7ce | 1699 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1701 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1702 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1703 | |
9e0baf60 | 1704 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1705 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1706 | } else { |
1707 | spin_lock(&BTRFS_I(inode)->lock); | |
1708 | BTRFS_I(inode)->outstanding_extents++; | |
1709 | spin_unlock(&BTRFS_I(inode)->lock); | |
1710 | } | |
287a0ab9 | 1711 | |
6a3891c5 JB |
1712 | /* For sanity tests */ |
1713 | if (btrfs_test_is_dummy_root(root)) | |
1714 | return; | |
1715 | ||
963d678b MX |
1716 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1717 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1718 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1719 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1720 | if (*bits & EXTENT_DEFRAG) |
1721 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1722 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1723 | &BTRFS_I(inode)->runtime_flags)) |
1724 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1725 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1726 | } |
291d673e CM |
1727 | } |
1728 | ||
d352ac68 CM |
1729 | /* |
1730 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1731 | */ | |
1bf85046 | 1732 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1733 | struct extent_state *state, |
9ee49a04 | 1734 | unsigned *bits) |
291d673e | 1735 | { |
47059d93 | 1736 | u64 len = state->end + 1 - state->start; |
dcab6a3b JB |
1737 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
1738 | BTRFS_MAX_EXTENT_SIZE); | |
47059d93 WS |
1739 | |
1740 | spin_lock(&BTRFS_I(inode)->lock); | |
1741 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1742 | BTRFS_I(inode)->defrag_bytes -= len; | |
1743 | spin_unlock(&BTRFS_I(inode)->lock); | |
1744 | ||
75eff68e CM |
1745 | /* |
1746 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1747 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1748 | * bit, which is only set or cleared with irqs on |
1749 | */ | |
0ca1f7ce | 1750 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1752 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1753 | |
9e0baf60 | 1754 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1755 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1756 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1757 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1758 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1759 | spin_unlock(&BTRFS_I(inode)->lock); |
1760 | } | |
0ca1f7ce | 1761 | |
b6d08f06 JB |
1762 | /* |
1763 | * We don't reserve metadata space for space cache inodes so we | |
1764 | * don't need to call dellalloc_release_metadata if there is an | |
1765 | * error. | |
1766 | */ | |
1767 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1768 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1769 | btrfs_delalloc_release_metadata(inode, len); |
1770 | ||
6a3891c5 JB |
1771 | /* For sanity tests. */ |
1772 | if (btrfs_test_is_dummy_root(root)) | |
1773 | return; | |
1774 | ||
0cb59c99 | 1775 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
7ee9e440 | 1776 | && do_list && !(state->state & EXTENT_NORESERVE)) |
0ca1f7ce | 1777 | btrfs_free_reserved_data_space(inode, len); |
9ed74f2d | 1778 | |
963d678b MX |
1779 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1780 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1781 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1782 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1783 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1784 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1785 | &BTRFS_I(inode)->runtime_flags)) |
1786 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1787 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1788 | } |
291d673e CM |
1789 | } |
1790 | ||
d352ac68 CM |
1791 | /* |
1792 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1793 | * we don't create bios that span stripes or chunks | |
1794 | */ | |
64a16701 | 1795 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
c8b97818 CM |
1796 | size_t size, struct bio *bio, |
1797 | unsigned long bio_flags) | |
239b14b3 CM |
1798 | { |
1799 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
4f024f37 | 1800 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1801 | u64 length = 0; |
1802 | u64 map_length; | |
239b14b3 CM |
1803 | int ret; |
1804 | ||
771ed689 CM |
1805 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1806 | return 0; | |
1807 | ||
4f024f37 | 1808 | length = bio->bi_iter.bi_size; |
239b14b3 | 1809 | map_length = length; |
64a16701 | 1810 | ret = btrfs_map_block(root->fs_info, rw, logical, |
f188591e | 1811 | &map_length, NULL, 0); |
3ec706c8 | 1812 | /* Will always return 0 with map_multi == NULL */ |
3444a972 | 1813 | BUG_ON(ret < 0); |
d397712b | 1814 | if (map_length < length + size) |
239b14b3 | 1815 | return 1; |
3444a972 | 1816 | return 0; |
239b14b3 CM |
1817 | } |
1818 | ||
d352ac68 CM |
1819 | /* |
1820 | * in order to insert checksums into the metadata in large chunks, | |
1821 | * we wait until bio submission time. All the pages in the bio are | |
1822 | * checksummed and sums are attached onto the ordered extent record. | |
1823 | * | |
1824 | * At IO completion time the cums attached on the ordered extent record | |
1825 | * are inserted into the btree | |
1826 | */ | |
d397712b CM |
1827 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
1828 | struct bio *bio, int mirror_num, | |
eaf25d93 CM |
1829 | unsigned long bio_flags, |
1830 | u64 bio_offset) | |
065631f6 | 1831 | { |
065631f6 | 1832 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1833 | int ret = 0; |
e015640f | 1834 | |
d20f7043 | 1835 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1836 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1837 | return 0; |
1838 | } | |
e015640f | 1839 | |
4a69a410 CM |
1840 | /* |
1841 | * in order to insert checksums into the metadata in large chunks, | |
1842 | * we wait until bio submission time. All the pages in the bio are | |
1843 | * checksummed and sums are attached onto the ordered extent record. | |
1844 | * | |
1845 | * At IO completion time the cums attached on the ordered extent record | |
1846 | * are inserted into the btree | |
1847 | */ | |
b2950863 | 1848 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1849 | int mirror_num, unsigned long bio_flags, |
1850 | u64 bio_offset) | |
4a69a410 CM |
1851 | { |
1852 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1853 | int ret; |
1854 | ||
1855 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); | |
1856 | if (ret) | |
1857 | bio_endio(bio, ret); | |
1858 | return ret; | |
44b8bd7e CM |
1859 | } |
1860 | ||
d352ac68 | 1861 | /* |
cad321ad CM |
1862 | * extent_io.c submission hook. This does the right thing for csum calculation |
1863 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1864 | */ |
b2950863 | 1865 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1866 | int mirror_num, unsigned long bio_flags, |
1867 | u64 bio_offset) | |
44b8bd7e CM |
1868 | { |
1869 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1870 | int ret = 0; | |
19b9bdb0 | 1871 | int skip_sum; |
0417341e | 1872 | int metadata = 0; |
b812ce28 | 1873 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1874 | |
6cbff00f | 1875 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1876 | |
83eea1f1 | 1877 | if (btrfs_is_free_space_inode(inode)) |
0417341e JM |
1878 | metadata = 2; |
1879 | ||
7b6d91da | 1880 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1881 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1882 | if (ret) | |
61891923 | 1883 | goto out; |
5fd02043 | 1884 | |
d20f7043 | 1885 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1886 | ret = btrfs_submit_compressed_read(inode, bio, |
1887 | mirror_num, | |
1888 | bio_flags); | |
1889 | goto out; | |
c2db1073 TI |
1890 | } else if (!skip_sum) { |
1891 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1892 | if (ret) | |
61891923 | 1893 | goto out; |
c2db1073 | 1894 | } |
4d1b5fb4 | 1895 | goto mapit; |
b812ce28 | 1896 | } else if (async && !skip_sum) { |
17d217fe YZ |
1897 | /* csum items have already been cloned */ |
1898 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1899 | goto mapit; | |
19b9bdb0 | 1900 | /* we're doing a write, do the async checksumming */ |
61891923 | 1901 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1902 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1903 | bio_flags, bio_offset, |
1904 | __btrfs_submit_bio_start, | |
4a69a410 | 1905 | __btrfs_submit_bio_done); |
61891923 | 1906 | goto out; |
b812ce28 JB |
1907 | } else if (!skip_sum) { |
1908 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1909 | if (ret) | |
1910 | goto out; | |
19b9bdb0 CM |
1911 | } |
1912 | ||
0b86a832 | 1913 | mapit: |
61891923 SB |
1914 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1915 | ||
1916 | out: | |
1917 | if (ret < 0) | |
1918 | bio_endio(bio, ret); | |
1919 | return ret; | |
065631f6 | 1920 | } |
6885f308 | 1921 | |
d352ac68 CM |
1922 | /* |
1923 | * given a list of ordered sums record them in the inode. This happens | |
1924 | * at IO completion time based on sums calculated at bio submission time. | |
1925 | */ | |
ba1da2f4 | 1926 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1927 | struct inode *inode, u64 file_offset, |
1928 | struct list_head *list) | |
1929 | { | |
e6dcd2dc CM |
1930 | struct btrfs_ordered_sum *sum; |
1931 | ||
c6e30871 | 1932 | list_for_each_entry(sum, list, list) { |
39847c4d | 1933 | trans->adding_csums = 1; |
d20f7043 CM |
1934 | btrfs_csum_file_blocks(trans, |
1935 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1936 | trans->adding_csums = 0; |
e6dcd2dc CM |
1937 | } |
1938 | return 0; | |
1939 | } | |
1940 | ||
2ac55d41 JB |
1941 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1942 | struct extent_state **cached_state) | |
ea8c2819 | 1943 | { |
6c1500f2 | 1944 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
ea8c2819 | 1945 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
2ac55d41 | 1946 | cached_state, GFP_NOFS); |
ea8c2819 CM |
1947 | } |
1948 | ||
d352ac68 | 1949 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1950 | struct btrfs_writepage_fixup { |
1951 | struct page *page; | |
1952 | struct btrfs_work work; | |
1953 | }; | |
1954 | ||
b2950863 | 1955 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1956 | { |
1957 | struct btrfs_writepage_fixup *fixup; | |
1958 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 1959 | struct extent_state *cached_state = NULL; |
247e743c CM |
1960 | struct page *page; |
1961 | struct inode *inode; | |
1962 | u64 page_start; | |
1963 | u64 page_end; | |
87826df0 | 1964 | int ret; |
247e743c CM |
1965 | |
1966 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
1967 | page = fixup->page; | |
4a096752 | 1968 | again: |
247e743c CM |
1969 | lock_page(page); |
1970 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
1971 | ClearPageChecked(page); | |
1972 | goto out_page; | |
1973 | } | |
1974 | ||
1975 | inode = page->mapping->host; | |
1976 | page_start = page_offset(page); | |
1977 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
1978 | ||
2ac55d41 | 1979 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
d0082371 | 1980 | &cached_state); |
4a096752 CM |
1981 | |
1982 | /* already ordered? We're done */ | |
8b62b72b | 1983 | if (PagePrivate2(page)) |
247e743c | 1984 | goto out; |
4a096752 CM |
1985 | |
1986 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1987 | if (ordered) { | |
2ac55d41 JB |
1988 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
1989 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
1990 | unlock_page(page); |
1991 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 1992 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
1993 | goto again; |
1994 | } | |
247e743c | 1995 | |
87826df0 JM |
1996 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
1997 | if (ret) { | |
1998 | mapping_set_error(page->mapping, ret); | |
1999 | end_extent_writepage(page, ret, page_start, page_end); | |
2000 | ClearPageChecked(page); | |
2001 | goto out; | |
2002 | } | |
2003 | ||
2ac55d41 | 2004 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 2005 | ClearPageChecked(page); |
87826df0 | 2006 | set_page_dirty(page); |
247e743c | 2007 | out: |
2ac55d41 JB |
2008 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2009 | &cached_state, GFP_NOFS); | |
247e743c CM |
2010 | out_page: |
2011 | unlock_page(page); | |
2012 | page_cache_release(page); | |
b897abec | 2013 | kfree(fixup); |
247e743c CM |
2014 | } |
2015 | ||
2016 | /* | |
2017 | * There are a few paths in the higher layers of the kernel that directly | |
2018 | * set the page dirty bit without asking the filesystem if it is a | |
2019 | * good idea. This causes problems because we want to make sure COW | |
2020 | * properly happens and the data=ordered rules are followed. | |
2021 | * | |
c8b97818 | 2022 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2023 | * hasn't been properly setup for IO. We kick off an async process |
2024 | * to fix it up. The async helper will wait for ordered extents, set | |
2025 | * the delalloc bit and make it safe to write the page. | |
2026 | */ | |
b2950863 | 2027 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2028 | { |
2029 | struct inode *inode = page->mapping->host; | |
2030 | struct btrfs_writepage_fixup *fixup; | |
2031 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2032 | |
8b62b72b CM |
2033 | /* this page is properly in the ordered list */ |
2034 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2035 | return 0; |
2036 | ||
2037 | if (PageChecked(page)) | |
2038 | return -EAGAIN; | |
2039 | ||
2040 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2041 | if (!fixup) | |
2042 | return -EAGAIN; | |
f421950f | 2043 | |
247e743c CM |
2044 | SetPageChecked(page); |
2045 | page_cache_get(page); | |
9e0af237 LB |
2046 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2047 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2048 | fixup->page = page; |
dc6e3209 | 2049 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2050 | return -EBUSY; |
247e743c CM |
2051 | } |
2052 | ||
d899e052 YZ |
2053 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2054 | struct inode *inode, u64 file_pos, | |
2055 | u64 disk_bytenr, u64 disk_num_bytes, | |
2056 | u64 num_bytes, u64 ram_bytes, | |
2057 | u8 compression, u8 encryption, | |
2058 | u16 other_encoding, int extent_type) | |
2059 | { | |
2060 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2061 | struct btrfs_file_extent_item *fi; | |
2062 | struct btrfs_path *path; | |
2063 | struct extent_buffer *leaf; | |
2064 | struct btrfs_key ins; | |
1acae57b | 2065 | int extent_inserted = 0; |
d899e052 YZ |
2066 | int ret; |
2067 | ||
2068 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2069 | if (!path) |
2070 | return -ENOMEM; | |
d899e052 | 2071 | |
a1ed835e CM |
2072 | /* |
2073 | * we may be replacing one extent in the tree with another. | |
2074 | * The new extent is pinned in the extent map, and we don't want | |
2075 | * to drop it from the cache until it is completely in the btree. | |
2076 | * | |
2077 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2078 | * the caller is expected to unpin it and allow it to be merged | |
2079 | * with the others. | |
2080 | */ | |
1acae57b FDBM |
2081 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2082 | file_pos + num_bytes, NULL, 0, | |
2083 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2084 | if (ret) |
2085 | goto out; | |
d899e052 | 2086 | |
1acae57b FDBM |
2087 | if (!extent_inserted) { |
2088 | ins.objectid = btrfs_ino(inode); | |
2089 | ins.offset = file_pos; | |
2090 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2091 | ||
2092 | path->leave_spinning = 1; | |
2093 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2094 | sizeof(*fi)); | |
2095 | if (ret) | |
2096 | goto out; | |
2097 | } | |
d899e052 YZ |
2098 | leaf = path->nodes[0]; |
2099 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2100 | struct btrfs_file_extent_item); | |
2101 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2102 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2103 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2104 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2105 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2106 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2107 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2108 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2109 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2110 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2111 | |
d899e052 | 2112 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2113 | btrfs_release_path(path); |
d899e052 YZ |
2114 | |
2115 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2116 | |
2117 | ins.objectid = disk_bytenr; | |
2118 | ins.offset = disk_num_bytes; | |
2119 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2120 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2121 | root->root_key.objectid, | |
33345d01 | 2122 | btrfs_ino(inode), file_pos, &ins); |
79787eaa | 2123 | out: |
d899e052 | 2124 | btrfs_free_path(path); |
b9473439 | 2125 | |
79787eaa | 2126 | return ret; |
d899e052 YZ |
2127 | } |
2128 | ||
38c227d8 LB |
2129 | /* snapshot-aware defrag */ |
2130 | struct sa_defrag_extent_backref { | |
2131 | struct rb_node node; | |
2132 | struct old_sa_defrag_extent *old; | |
2133 | u64 root_id; | |
2134 | u64 inum; | |
2135 | u64 file_pos; | |
2136 | u64 extent_offset; | |
2137 | u64 num_bytes; | |
2138 | u64 generation; | |
2139 | }; | |
2140 | ||
2141 | struct old_sa_defrag_extent { | |
2142 | struct list_head list; | |
2143 | struct new_sa_defrag_extent *new; | |
2144 | ||
2145 | u64 extent_offset; | |
2146 | u64 bytenr; | |
2147 | u64 offset; | |
2148 | u64 len; | |
2149 | int count; | |
2150 | }; | |
2151 | ||
2152 | struct new_sa_defrag_extent { | |
2153 | struct rb_root root; | |
2154 | struct list_head head; | |
2155 | struct btrfs_path *path; | |
2156 | struct inode *inode; | |
2157 | u64 file_pos; | |
2158 | u64 len; | |
2159 | u64 bytenr; | |
2160 | u64 disk_len; | |
2161 | u8 compress_type; | |
2162 | }; | |
2163 | ||
2164 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2165 | struct sa_defrag_extent_backref *b2) | |
2166 | { | |
2167 | if (b1->root_id < b2->root_id) | |
2168 | return -1; | |
2169 | else if (b1->root_id > b2->root_id) | |
2170 | return 1; | |
2171 | ||
2172 | if (b1->inum < b2->inum) | |
2173 | return -1; | |
2174 | else if (b1->inum > b2->inum) | |
2175 | return 1; | |
2176 | ||
2177 | if (b1->file_pos < b2->file_pos) | |
2178 | return -1; | |
2179 | else if (b1->file_pos > b2->file_pos) | |
2180 | return 1; | |
2181 | ||
2182 | /* | |
2183 | * [------------------------------] ===> (a range of space) | |
2184 | * |<--->| |<---->| =============> (fs/file tree A) | |
2185 | * |<---------------------------->| ===> (fs/file tree B) | |
2186 | * | |
2187 | * A range of space can refer to two file extents in one tree while | |
2188 | * refer to only one file extent in another tree. | |
2189 | * | |
2190 | * So we may process a disk offset more than one time(two extents in A) | |
2191 | * and locate at the same extent(one extent in B), then insert two same | |
2192 | * backrefs(both refer to the extent in B). | |
2193 | */ | |
2194 | return 0; | |
2195 | } | |
2196 | ||
2197 | static void backref_insert(struct rb_root *root, | |
2198 | struct sa_defrag_extent_backref *backref) | |
2199 | { | |
2200 | struct rb_node **p = &root->rb_node; | |
2201 | struct rb_node *parent = NULL; | |
2202 | struct sa_defrag_extent_backref *entry; | |
2203 | int ret; | |
2204 | ||
2205 | while (*p) { | |
2206 | parent = *p; | |
2207 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2208 | ||
2209 | ret = backref_comp(backref, entry); | |
2210 | if (ret < 0) | |
2211 | p = &(*p)->rb_left; | |
2212 | else | |
2213 | p = &(*p)->rb_right; | |
2214 | } | |
2215 | ||
2216 | rb_link_node(&backref->node, parent, p); | |
2217 | rb_insert_color(&backref->node, root); | |
2218 | } | |
2219 | ||
2220 | /* | |
2221 | * Note the backref might has changed, and in this case we just return 0. | |
2222 | */ | |
2223 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2224 | void *ctx) | |
2225 | { | |
2226 | struct btrfs_file_extent_item *extent; | |
2227 | struct btrfs_fs_info *fs_info; | |
2228 | struct old_sa_defrag_extent *old = ctx; | |
2229 | struct new_sa_defrag_extent *new = old->new; | |
2230 | struct btrfs_path *path = new->path; | |
2231 | struct btrfs_key key; | |
2232 | struct btrfs_root *root; | |
2233 | struct sa_defrag_extent_backref *backref; | |
2234 | struct extent_buffer *leaf; | |
2235 | struct inode *inode = new->inode; | |
2236 | int slot; | |
2237 | int ret; | |
2238 | u64 extent_offset; | |
2239 | u64 num_bytes; | |
2240 | ||
2241 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2242 | inum == btrfs_ino(inode)) | |
2243 | return 0; | |
2244 | ||
2245 | key.objectid = root_id; | |
2246 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2247 | key.offset = (u64)-1; | |
2248 | ||
2249 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2250 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2251 | if (IS_ERR(root)) { | |
2252 | if (PTR_ERR(root) == -ENOENT) | |
2253 | return 0; | |
2254 | WARN_ON(1); | |
2255 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2256 | inum, offset, root_id); | |
2257 | return PTR_ERR(root); | |
2258 | } | |
2259 | ||
2260 | key.objectid = inum; | |
2261 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2262 | if (offset > (u64)-1 << 32) | |
2263 | key.offset = 0; | |
2264 | else | |
2265 | key.offset = offset; | |
2266 | ||
2267 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2268 | if (WARN_ON(ret < 0)) |
38c227d8 | 2269 | return ret; |
50f1319c | 2270 | ret = 0; |
38c227d8 LB |
2271 | |
2272 | while (1) { | |
2273 | cond_resched(); | |
2274 | ||
2275 | leaf = path->nodes[0]; | |
2276 | slot = path->slots[0]; | |
2277 | ||
2278 | if (slot >= btrfs_header_nritems(leaf)) { | |
2279 | ret = btrfs_next_leaf(root, path); | |
2280 | if (ret < 0) { | |
2281 | goto out; | |
2282 | } else if (ret > 0) { | |
2283 | ret = 0; | |
2284 | goto out; | |
2285 | } | |
2286 | continue; | |
2287 | } | |
2288 | ||
2289 | path->slots[0]++; | |
2290 | ||
2291 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2292 | ||
2293 | if (key.objectid > inum) | |
2294 | goto out; | |
2295 | ||
2296 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2297 | continue; | |
2298 | ||
2299 | extent = btrfs_item_ptr(leaf, slot, | |
2300 | struct btrfs_file_extent_item); | |
2301 | ||
2302 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2303 | continue; | |
2304 | ||
e68afa49 LB |
2305 | /* |
2306 | * 'offset' refers to the exact key.offset, | |
2307 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2308 | * (key.offset - extent_offset). | |
2309 | */ | |
2310 | if (key.offset != offset) | |
38c227d8 LB |
2311 | continue; |
2312 | ||
e68afa49 | 2313 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2314 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2315 | |
38c227d8 LB |
2316 | if (extent_offset >= old->extent_offset + old->offset + |
2317 | old->len || extent_offset + num_bytes <= | |
2318 | old->extent_offset + old->offset) | |
2319 | continue; | |
38c227d8 LB |
2320 | break; |
2321 | } | |
2322 | ||
2323 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2324 | if (!backref) { | |
2325 | ret = -ENOENT; | |
2326 | goto out; | |
2327 | } | |
2328 | ||
2329 | backref->root_id = root_id; | |
2330 | backref->inum = inum; | |
e68afa49 | 2331 | backref->file_pos = offset; |
38c227d8 LB |
2332 | backref->num_bytes = num_bytes; |
2333 | backref->extent_offset = extent_offset; | |
2334 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2335 | backref->old = old; | |
2336 | backref_insert(&new->root, backref); | |
2337 | old->count++; | |
2338 | out: | |
2339 | btrfs_release_path(path); | |
2340 | WARN_ON(ret); | |
2341 | return ret; | |
2342 | } | |
2343 | ||
2344 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2345 | struct new_sa_defrag_extent *new) | |
2346 | { | |
2347 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2348 | struct old_sa_defrag_extent *old, *tmp; | |
2349 | int ret; | |
2350 | ||
2351 | new->path = path; | |
2352 | ||
2353 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2354 | ret = iterate_inodes_from_logical(old->bytenr + |
2355 | old->extent_offset, fs_info, | |
38c227d8 LB |
2356 | path, record_one_backref, |
2357 | old); | |
4724b106 JB |
2358 | if (ret < 0 && ret != -ENOENT) |
2359 | return false; | |
38c227d8 LB |
2360 | |
2361 | /* no backref to be processed for this extent */ | |
2362 | if (!old->count) { | |
2363 | list_del(&old->list); | |
2364 | kfree(old); | |
2365 | } | |
2366 | } | |
2367 | ||
2368 | if (list_empty(&new->head)) | |
2369 | return false; | |
2370 | ||
2371 | return true; | |
2372 | } | |
2373 | ||
2374 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2375 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2376 | struct new_sa_defrag_extent *new) |
38c227d8 | 2377 | { |
116e0024 | 2378 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2379 | return 0; |
2380 | ||
2381 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2382 | return 0; | |
2383 | ||
116e0024 LB |
2384 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2385 | return 0; | |
2386 | ||
2387 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2388 | btrfs_file_extent_other_encoding(leaf, fi)) |
2389 | return 0; | |
2390 | ||
2391 | return 1; | |
2392 | } | |
2393 | ||
2394 | /* | |
2395 | * Note the backref might has changed, and in this case we just return 0. | |
2396 | */ | |
2397 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2398 | struct sa_defrag_extent_backref *prev, | |
2399 | struct sa_defrag_extent_backref *backref) | |
2400 | { | |
2401 | struct btrfs_file_extent_item *extent; | |
2402 | struct btrfs_file_extent_item *item; | |
2403 | struct btrfs_ordered_extent *ordered; | |
2404 | struct btrfs_trans_handle *trans; | |
2405 | struct btrfs_fs_info *fs_info; | |
2406 | struct btrfs_root *root; | |
2407 | struct btrfs_key key; | |
2408 | struct extent_buffer *leaf; | |
2409 | struct old_sa_defrag_extent *old = backref->old; | |
2410 | struct new_sa_defrag_extent *new = old->new; | |
2411 | struct inode *src_inode = new->inode; | |
2412 | struct inode *inode; | |
2413 | struct extent_state *cached = NULL; | |
2414 | int ret = 0; | |
2415 | u64 start; | |
2416 | u64 len; | |
2417 | u64 lock_start; | |
2418 | u64 lock_end; | |
2419 | bool merge = false; | |
2420 | int index; | |
2421 | ||
2422 | if (prev && prev->root_id == backref->root_id && | |
2423 | prev->inum == backref->inum && | |
2424 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2425 | merge = true; | |
2426 | ||
2427 | /* step 1: get root */ | |
2428 | key.objectid = backref->root_id; | |
2429 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2430 | key.offset = (u64)-1; | |
2431 | ||
2432 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2433 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2434 | ||
2435 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2436 | if (IS_ERR(root)) { | |
2437 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2438 | if (PTR_ERR(root) == -ENOENT) | |
2439 | return 0; | |
2440 | return PTR_ERR(root); | |
2441 | } | |
38c227d8 | 2442 | |
bcbba5e6 WS |
2443 | if (btrfs_root_readonly(root)) { |
2444 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2445 | return 0; | |
2446 | } | |
2447 | ||
38c227d8 LB |
2448 | /* step 2: get inode */ |
2449 | key.objectid = backref->inum; | |
2450 | key.type = BTRFS_INODE_ITEM_KEY; | |
2451 | key.offset = 0; | |
2452 | ||
2453 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2454 | if (IS_ERR(inode)) { | |
2455 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2456 | return 0; | |
2457 | } | |
2458 | ||
2459 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2460 | ||
2461 | /* step 3: relink backref */ | |
2462 | lock_start = backref->file_pos; | |
2463 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2464 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2465 | 0, &cached); | |
2466 | ||
2467 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2468 | if (ordered) { | |
2469 | btrfs_put_ordered_extent(ordered); | |
2470 | goto out_unlock; | |
2471 | } | |
2472 | ||
2473 | trans = btrfs_join_transaction(root); | |
2474 | if (IS_ERR(trans)) { | |
2475 | ret = PTR_ERR(trans); | |
2476 | goto out_unlock; | |
2477 | } | |
2478 | ||
2479 | key.objectid = backref->inum; | |
2480 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2481 | key.offset = backref->file_pos; | |
2482 | ||
2483 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2484 | if (ret < 0) { | |
2485 | goto out_free_path; | |
2486 | } else if (ret > 0) { | |
2487 | ret = 0; | |
2488 | goto out_free_path; | |
2489 | } | |
2490 | ||
2491 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2492 | struct btrfs_file_extent_item); | |
2493 | ||
2494 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2495 | backref->generation) | |
2496 | goto out_free_path; | |
2497 | ||
2498 | btrfs_release_path(path); | |
2499 | ||
2500 | start = backref->file_pos; | |
2501 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2502 | start += old->extent_offset + old->offset - | |
2503 | backref->extent_offset; | |
2504 | ||
2505 | len = min(backref->extent_offset + backref->num_bytes, | |
2506 | old->extent_offset + old->offset + old->len); | |
2507 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2508 | ||
2509 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2510 | start + len, 1); | |
2511 | if (ret) | |
2512 | goto out_free_path; | |
2513 | again: | |
2514 | key.objectid = btrfs_ino(inode); | |
2515 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2516 | key.offset = start; | |
2517 | ||
a09a0a70 | 2518 | path->leave_spinning = 1; |
38c227d8 LB |
2519 | if (merge) { |
2520 | struct btrfs_file_extent_item *fi; | |
2521 | u64 extent_len; | |
2522 | struct btrfs_key found_key; | |
2523 | ||
3c9665df | 2524 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2525 | if (ret < 0) |
2526 | goto out_free_path; | |
2527 | ||
2528 | path->slots[0]--; | |
2529 | leaf = path->nodes[0]; | |
2530 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2531 | ||
2532 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2533 | struct btrfs_file_extent_item); | |
2534 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2535 | ||
116e0024 LB |
2536 | if (extent_len + found_key.offset == start && |
2537 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2538 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2539 | extent_len + len); | |
2540 | btrfs_mark_buffer_dirty(leaf); | |
2541 | inode_add_bytes(inode, len); | |
2542 | ||
2543 | ret = 1; | |
2544 | goto out_free_path; | |
2545 | } else { | |
2546 | merge = false; | |
2547 | btrfs_release_path(path); | |
2548 | goto again; | |
2549 | } | |
2550 | } | |
2551 | ||
2552 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2553 | sizeof(*extent)); | |
2554 | if (ret) { | |
2555 | btrfs_abort_transaction(trans, root, ret); | |
2556 | goto out_free_path; | |
2557 | } | |
2558 | ||
2559 | leaf = path->nodes[0]; | |
2560 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2561 | struct btrfs_file_extent_item); | |
2562 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2563 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2564 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2565 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2566 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2567 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2568 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2569 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2570 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2571 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2572 | ||
2573 | btrfs_mark_buffer_dirty(leaf); | |
2574 | inode_add_bytes(inode, len); | |
a09a0a70 | 2575 | btrfs_release_path(path); |
38c227d8 LB |
2576 | |
2577 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2578 | new->disk_len, 0, | |
2579 | backref->root_id, backref->inum, | |
2580 | new->file_pos, 0); /* start - extent_offset */ | |
2581 | if (ret) { | |
2582 | btrfs_abort_transaction(trans, root, ret); | |
2583 | goto out_free_path; | |
2584 | } | |
2585 | ||
2586 | ret = 1; | |
2587 | out_free_path: | |
2588 | btrfs_release_path(path); | |
a09a0a70 | 2589 | path->leave_spinning = 0; |
38c227d8 LB |
2590 | btrfs_end_transaction(trans, root); |
2591 | out_unlock: | |
2592 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2593 | &cached, GFP_NOFS); | |
2594 | iput(inode); | |
2595 | return ret; | |
2596 | } | |
2597 | ||
6f519564 LB |
2598 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2599 | { | |
2600 | struct old_sa_defrag_extent *old, *tmp; | |
2601 | ||
2602 | if (!new) | |
2603 | return; | |
2604 | ||
2605 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2606 | list_del(&old->list); | |
2607 | kfree(old); | |
2608 | } | |
2609 | kfree(new); | |
2610 | } | |
2611 | ||
38c227d8 LB |
2612 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2613 | { | |
2614 | struct btrfs_path *path; | |
38c227d8 LB |
2615 | struct sa_defrag_extent_backref *backref; |
2616 | struct sa_defrag_extent_backref *prev = NULL; | |
2617 | struct inode *inode; | |
2618 | struct btrfs_root *root; | |
2619 | struct rb_node *node; | |
2620 | int ret; | |
2621 | ||
2622 | inode = new->inode; | |
2623 | root = BTRFS_I(inode)->root; | |
2624 | ||
2625 | path = btrfs_alloc_path(); | |
2626 | if (!path) | |
2627 | return; | |
2628 | ||
2629 | if (!record_extent_backrefs(path, new)) { | |
2630 | btrfs_free_path(path); | |
2631 | goto out; | |
2632 | } | |
2633 | btrfs_release_path(path); | |
2634 | ||
2635 | while (1) { | |
2636 | node = rb_first(&new->root); | |
2637 | if (!node) | |
2638 | break; | |
2639 | rb_erase(node, &new->root); | |
2640 | ||
2641 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2642 | ||
2643 | ret = relink_extent_backref(path, prev, backref); | |
2644 | WARN_ON(ret < 0); | |
2645 | ||
2646 | kfree(prev); | |
2647 | ||
2648 | if (ret == 1) | |
2649 | prev = backref; | |
2650 | else | |
2651 | prev = NULL; | |
2652 | cond_resched(); | |
2653 | } | |
2654 | kfree(prev); | |
2655 | ||
2656 | btrfs_free_path(path); | |
38c227d8 | 2657 | out: |
6f519564 LB |
2658 | free_sa_defrag_extent(new); |
2659 | ||
38c227d8 LB |
2660 | atomic_dec(&root->fs_info->defrag_running); |
2661 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2662 | } |
2663 | ||
2664 | static struct new_sa_defrag_extent * | |
2665 | record_old_file_extents(struct inode *inode, | |
2666 | struct btrfs_ordered_extent *ordered) | |
2667 | { | |
2668 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2669 | struct btrfs_path *path; | |
2670 | struct btrfs_key key; | |
6f519564 | 2671 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2672 | struct new_sa_defrag_extent *new; |
2673 | int ret; | |
2674 | ||
2675 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2676 | if (!new) | |
2677 | return NULL; | |
2678 | ||
2679 | new->inode = inode; | |
2680 | new->file_pos = ordered->file_offset; | |
2681 | new->len = ordered->len; | |
2682 | new->bytenr = ordered->start; | |
2683 | new->disk_len = ordered->disk_len; | |
2684 | new->compress_type = ordered->compress_type; | |
2685 | new->root = RB_ROOT; | |
2686 | INIT_LIST_HEAD(&new->head); | |
2687 | ||
2688 | path = btrfs_alloc_path(); | |
2689 | if (!path) | |
2690 | goto out_kfree; | |
2691 | ||
2692 | key.objectid = btrfs_ino(inode); | |
2693 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2694 | key.offset = new->file_pos; | |
2695 | ||
2696 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2697 | if (ret < 0) | |
2698 | goto out_free_path; | |
2699 | if (ret > 0 && path->slots[0] > 0) | |
2700 | path->slots[0]--; | |
2701 | ||
2702 | /* find out all the old extents for the file range */ | |
2703 | while (1) { | |
2704 | struct btrfs_file_extent_item *extent; | |
2705 | struct extent_buffer *l; | |
2706 | int slot; | |
2707 | u64 num_bytes; | |
2708 | u64 offset; | |
2709 | u64 end; | |
2710 | u64 disk_bytenr; | |
2711 | u64 extent_offset; | |
2712 | ||
2713 | l = path->nodes[0]; | |
2714 | slot = path->slots[0]; | |
2715 | ||
2716 | if (slot >= btrfs_header_nritems(l)) { | |
2717 | ret = btrfs_next_leaf(root, path); | |
2718 | if (ret < 0) | |
6f519564 | 2719 | goto out_free_path; |
38c227d8 LB |
2720 | else if (ret > 0) |
2721 | break; | |
2722 | continue; | |
2723 | } | |
2724 | ||
2725 | btrfs_item_key_to_cpu(l, &key, slot); | |
2726 | ||
2727 | if (key.objectid != btrfs_ino(inode)) | |
2728 | break; | |
2729 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2730 | break; | |
2731 | if (key.offset >= new->file_pos + new->len) | |
2732 | break; | |
2733 | ||
2734 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2735 | ||
2736 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2737 | if (key.offset + num_bytes < new->file_pos) | |
2738 | goto next; | |
2739 | ||
2740 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2741 | if (!disk_bytenr) | |
2742 | goto next; | |
2743 | ||
2744 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2745 | ||
2746 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2747 | if (!old) | |
6f519564 | 2748 | goto out_free_path; |
38c227d8 LB |
2749 | |
2750 | offset = max(new->file_pos, key.offset); | |
2751 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2752 | ||
2753 | old->bytenr = disk_bytenr; | |
2754 | old->extent_offset = extent_offset; | |
2755 | old->offset = offset - key.offset; | |
2756 | old->len = end - offset; | |
2757 | old->new = new; | |
2758 | old->count = 0; | |
2759 | list_add_tail(&old->list, &new->head); | |
2760 | next: | |
2761 | path->slots[0]++; | |
2762 | cond_resched(); | |
2763 | } | |
2764 | ||
2765 | btrfs_free_path(path); | |
2766 | atomic_inc(&root->fs_info->defrag_running); | |
2767 | ||
2768 | return new; | |
2769 | ||
38c227d8 LB |
2770 | out_free_path: |
2771 | btrfs_free_path(path); | |
2772 | out_kfree: | |
6f519564 | 2773 | free_sa_defrag_extent(new); |
38c227d8 LB |
2774 | return NULL; |
2775 | } | |
2776 | ||
e570fd27 MX |
2777 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2778 | u64 start, u64 len) | |
2779 | { | |
2780 | struct btrfs_block_group_cache *cache; | |
2781 | ||
2782 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2783 | ASSERT(cache); | |
2784 | ||
2785 | spin_lock(&cache->lock); | |
2786 | cache->delalloc_bytes -= len; | |
2787 | spin_unlock(&cache->lock); | |
2788 | ||
2789 | btrfs_put_block_group(cache); | |
2790 | } | |
2791 | ||
d352ac68 CM |
2792 | /* as ordered data IO finishes, this gets called so we can finish |
2793 | * an ordered extent if the range of bytes in the file it covers are | |
2794 | * fully written. | |
2795 | */ | |
5fd02043 | 2796 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2797 | { |
5fd02043 | 2798 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2799 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2800 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2801 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2802 | struct extent_state *cached_state = NULL; |
38c227d8 | 2803 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2804 | int compress_type = 0; |
77cef2ec JB |
2805 | int ret = 0; |
2806 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2807 | bool nolock; |
77cef2ec | 2808 | bool truncated = false; |
e6dcd2dc | 2809 | |
83eea1f1 | 2810 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2811 | |
5fd02043 JB |
2812 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2813 | ret = -EIO; | |
2814 | goto out; | |
2815 | } | |
2816 | ||
f612496b MX |
2817 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2818 | ordered_extent->file_offset + | |
2819 | ordered_extent->len - 1); | |
2820 | ||
77cef2ec JB |
2821 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2822 | truncated = true; | |
2823 | logical_len = ordered_extent->truncated_len; | |
2824 | /* Truncated the entire extent, don't bother adding */ | |
2825 | if (!logical_len) | |
2826 | goto out; | |
2827 | } | |
2828 | ||
c2167754 | 2829 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2830 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
6c760c07 JB |
2831 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2832 | if (nolock) | |
2833 | trans = btrfs_join_transaction_nolock(root); | |
2834 | else | |
2835 | trans = btrfs_join_transaction(root); | |
2836 | if (IS_ERR(trans)) { | |
2837 | ret = PTR_ERR(trans); | |
2838 | trans = NULL; | |
2839 | goto out; | |
c2167754 | 2840 | } |
6c760c07 JB |
2841 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2842 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2843 | if (ret) /* -ENOMEM or corruption */ | |
2844 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2845 | goto out; |
2846 | } | |
e6dcd2dc | 2847 | |
2ac55d41 JB |
2848 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2849 | ordered_extent->file_offset + ordered_extent->len - 1, | |
d0082371 | 2850 | 0, &cached_state); |
e6dcd2dc | 2851 | |
38c227d8 LB |
2852 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2853 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2854 | EXTENT_DEFRAG, 1, cached_state); | |
2855 | if (ret) { | |
2856 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2857 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2858 | /* the inode is shared */ |
2859 | new = record_old_file_extents(inode, ordered_extent); | |
2860 | ||
2861 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2862 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2863 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2864 | } | |
2865 | ||
0cb59c99 | 2866 | if (nolock) |
7a7eaa40 | 2867 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2868 | else |
7a7eaa40 | 2869 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2870 | if (IS_ERR(trans)) { |
2871 | ret = PTR_ERR(trans); | |
2872 | trans = NULL; | |
2873 | goto out_unlock; | |
2874 | } | |
a79b7d4b | 2875 | |
0ca1f7ce | 2876 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2877 | |
c8b97818 | 2878 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2879 | compress_type = ordered_extent->compress_type; |
d899e052 | 2880 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2881 | BUG_ON(compress_type); |
920bbbfb | 2882 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2883 | ordered_extent->file_offset, |
2884 | ordered_extent->file_offset + | |
77cef2ec | 2885 | logical_len); |
d899e052 | 2886 | } else { |
0af3d00b | 2887 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2888 | ret = insert_reserved_file_extent(trans, inode, |
2889 | ordered_extent->file_offset, | |
2890 | ordered_extent->start, | |
2891 | ordered_extent->disk_len, | |
77cef2ec | 2892 | logical_len, logical_len, |
261507a0 | 2893 | compress_type, 0, 0, |
d899e052 | 2894 | BTRFS_FILE_EXTENT_REG); |
e570fd27 MX |
2895 | if (!ret) |
2896 | btrfs_release_delalloc_bytes(root, | |
2897 | ordered_extent->start, | |
2898 | ordered_extent->disk_len); | |
d899e052 | 2899 | } |
5dc562c5 JB |
2900 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2901 | ordered_extent->file_offset, ordered_extent->len, | |
2902 | trans->transid); | |
79787eaa JM |
2903 | if (ret < 0) { |
2904 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2905 | goto out_unlock; |
79787eaa | 2906 | } |
2ac55d41 | 2907 | |
e6dcd2dc CM |
2908 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2909 | &ordered_extent->list); | |
2910 | ||
6c760c07 JB |
2911 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2912 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2913 | if (ret) { /* -ENOMEM or corruption */ | |
2914 | btrfs_abort_transaction(trans, root, ret); | |
2915 | goto out_unlock; | |
1ef30be1 JB |
2916 | } |
2917 | ret = 0; | |
5fd02043 JB |
2918 | out_unlock: |
2919 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2920 | ordered_extent->file_offset + | |
2921 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2922 | out: |
5b0e95bf | 2923 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2924 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2925 | if (trans) |
2926 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2927 | |
77cef2ec JB |
2928 | if (ret || truncated) { |
2929 | u64 start, end; | |
2930 | ||
2931 | if (truncated) | |
2932 | start = ordered_extent->file_offset + logical_len; | |
2933 | else | |
2934 | start = ordered_extent->file_offset; | |
2935 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2936 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2937 | ||
2938 | /* Drop the cache for the part of the extent we didn't write. */ | |
2939 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2940 | |
0bec9ef5 JB |
2941 | /* |
2942 | * If the ordered extent had an IOERR or something else went | |
2943 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2944 | * back to the allocator. We only free the extent in the |
2945 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 2946 | */ |
77cef2ec JB |
2947 | if ((ret || !logical_len) && |
2948 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
2949 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2950 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 2951 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
2952 | } |
2953 | ||
2954 | ||
5fd02043 | 2955 | /* |
8bad3c02 LB |
2956 | * This needs to be done to make sure anybody waiting knows we are done |
2957 | * updating everything for this ordered extent. | |
5fd02043 JB |
2958 | */ |
2959 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2960 | ||
38c227d8 | 2961 | /* for snapshot-aware defrag */ |
6f519564 LB |
2962 | if (new) { |
2963 | if (ret) { | |
2964 | free_sa_defrag_extent(new); | |
2965 | atomic_dec(&root->fs_info->defrag_running); | |
2966 | } else { | |
2967 | relink_file_extents(new); | |
2968 | } | |
2969 | } | |
38c227d8 | 2970 | |
e6dcd2dc CM |
2971 | /* once for us */ |
2972 | btrfs_put_ordered_extent(ordered_extent); | |
2973 | /* once for the tree */ | |
2974 | btrfs_put_ordered_extent(ordered_extent); | |
2975 | ||
5fd02043 JB |
2976 | return ret; |
2977 | } | |
2978 | ||
2979 | static void finish_ordered_fn(struct btrfs_work *work) | |
2980 | { | |
2981 | struct btrfs_ordered_extent *ordered_extent; | |
2982 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2983 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2984 | } |
2985 | ||
b2950863 | 2986 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
2987 | struct extent_state *state, int uptodate) |
2988 | { | |
5fd02043 JB |
2989 | struct inode *inode = page->mapping->host; |
2990 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2991 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
2992 | struct btrfs_workqueue *wq; |
2993 | btrfs_work_func_t func; | |
5fd02043 | 2994 | |
1abe9b8a | 2995 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2996 | ||
8b62b72b | 2997 | ClearPagePrivate2(page); |
5fd02043 JB |
2998 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2999 | end - start + 1, uptodate)) | |
3000 | return 0; | |
3001 | ||
9e0af237 LB |
3002 | if (btrfs_is_free_space_inode(inode)) { |
3003 | wq = root->fs_info->endio_freespace_worker; | |
3004 | func = btrfs_freespace_write_helper; | |
3005 | } else { | |
3006 | wq = root->fs_info->endio_write_workers; | |
3007 | func = btrfs_endio_write_helper; | |
3008 | } | |
5fd02043 | 3009 | |
9e0af237 LB |
3010 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3011 | NULL); | |
3012 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3013 | |
3014 | return 0; | |
211f90e6 CM |
3015 | } |
3016 | ||
dc380aea MX |
3017 | static int __readpage_endio_check(struct inode *inode, |
3018 | struct btrfs_io_bio *io_bio, | |
3019 | int icsum, struct page *page, | |
3020 | int pgoff, u64 start, size_t len) | |
3021 | { | |
3022 | char *kaddr; | |
3023 | u32 csum_expected; | |
3024 | u32 csum = ~(u32)0; | |
3025 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, | |
3026 | DEFAULT_RATELIMIT_BURST); | |
3027 | ||
3028 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3029 | ||
3030 | kaddr = kmap_atomic(page); | |
3031 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3032 | btrfs_csum_final(csum, (char *)&csum); | |
3033 | if (csum != csum_expected) | |
3034 | goto zeroit; | |
3035 | ||
3036 | kunmap_atomic(kaddr); | |
3037 | return 0; | |
3038 | zeroit: | |
3039 | if (__ratelimit(&_rs)) | |
f0954c66 | 3040 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
dc380aea MX |
3041 | "csum failed ino %llu off %llu csum %u expected csum %u", |
3042 | btrfs_ino(inode), start, csum, csum_expected); | |
3043 | memset(kaddr + pgoff, 1, len); | |
3044 | flush_dcache_page(page); | |
3045 | kunmap_atomic(kaddr); | |
3046 | if (csum_expected == 0) | |
3047 | return 0; | |
3048 | return -EIO; | |
3049 | } | |
3050 | ||
d352ac68 CM |
3051 | /* |
3052 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3053 | * if there's a match, we allow the bio to finish. If not, the code in |
3054 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3055 | */ |
facc8a22 MX |
3056 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3057 | u64 phy_offset, struct page *page, | |
3058 | u64 start, u64 end, int mirror) | |
07157aac | 3059 | { |
4eee4fa4 | 3060 | size_t offset = start - page_offset(page); |
07157aac | 3061 | struct inode *inode = page->mapping->host; |
d1310b2e | 3062 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3063 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3064 | |
d20f7043 CM |
3065 | if (PageChecked(page)) { |
3066 | ClearPageChecked(page); | |
dc380aea | 3067 | return 0; |
d20f7043 | 3068 | } |
6cbff00f CH |
3069 | |
3070 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3071 | return 0; |
17d217fe YZ |
3072 | |
3073 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3074 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
17d217fe YZ |
3075 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
3076 | GFP_NOFS); | |
b6cda9bc | 3077 | return 0; |
17d217fe | 3078 | } |
d20f7043 | 3079 | |
facc8a22 | 3080 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3081 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3082 | start, (size_t)(end - start + 1)); | |
07157aac | 3083 | } |
b888db2b | 3084 | |
24bbcf04 YZ |
3085 | struct delayed_iput { |
3086 | struct list_head list; | |
3087 | struct inode *inode; | |
3088 | }; | |
3089 | ||
79787eaa JM |
3090 | /* JDM: If this is fs-wide, why can't we add a pointer to |
3091 | * btrfs_inode instead and avoid the allocation? */ | |
24bbcf04 YZ |
3092 | void btrfs_add_delayed_iput(struct inode *inode) |
3093 | { | |
3094 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
3095 | struct delayed_iput *delayed; | |
3096 | ||
3097 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3098 | return; | |
3099 | ||
3100 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); | |
3101 | delayed->inode = inode; | |
3102 | ||
3103 | spin_lock(&fs_info->delayed_iput_lock); | |
3104 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); | |
3105 | spin_unlock(&fs_info->delayed_iput_lock); | |
3106 | } | |
3107 | ||
3108 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3109 | { | |
3110 | LIST_HEAD(list); | |
3111 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3112 | struct delayed_iput *delayed; | |
3113 | int empty; | |
3114 | ||
3115 | spin_lock(&fs_info->delayed_iput_lock); | |
3116 | empty = list_empty(&fs_info->delayed_iputs); | |
3117 | spin_unlock(&fs_info->delayed_iput_lock); | |
3118 | if (empty) | |
3119 | return; | |
3120 | ||
d7c15171 ZL |
3121 | down_read(&fs_info->delayed_iput_sem); |
3122 | ||
24bbcf04 YZ |
3123 | spin_lock(&fs_info->delayed_iput_lock); |
3124 | list_splice_init(&fs_info->delayed_iputs, &list); | |
3125 | spin_unlock(&fs_info->delayed_iput_lock); | |
3126 | ||
3127 | while (!list_empty(&list)) { | |
3128 | delayed = list_entry(list.next, struct delayed_iput, list); | |
3129 | list_del(&delayed->list); | |
3130 | iput(delayed->inode); | |
3131 | kfree(delayed); | |
3132 | } | |
d7c15171 ZL |
3133 | |
3134 | up_read(&root->fs_info->delayed_iput_sem); | |
24bbcf04 YZ |
3135 | } |
3136 | ||
d68fc57b | 3137 | /* |
42b2aa86 | 3138 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3139 | * files in the subvolume, it removes orphan item and frees block_rsv |
3140 | * structure. | |
3141 | */ | |
3142 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3143 | struct btrfs_root *root) | |
3144 | { | |
90290e19 | 3145 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3146 | int ret; |
3147 | ||
8a35d95f | 3148 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3149 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3150 | return; | |
3151 | ||
90290e19 | 3152 | spin_lock(&root->orphan_lock); |
8a35d95f | 3153 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3154 | spin_unlock(&root->orphan_lock); |
3155 | return; | |
3156 | } | |
3157 | ||
3158 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3159 | spin_unlock(&root->orphan_lock); | |
3160 | return; | |
3161 | } | |
3162 | ||
3163 | block_rsv = root->orphan_block_rsv; | |
3164 | root->orphan_block_rsv = NULL; | |
3165 | spin_unlock(&root->orphan_lock); | |
3166 | ||
27cdeb70 | 3167 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3168 | btrfs_root_refs(&root->root_item) > 0) { |
3169 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3170 | root->root_key.objectid); | |
4ef31a45 JB |
3171 | if (ret) |
3172 | btrfs_abort_transaction(trans, root, ret); | |
3173 | else | |
27cdeb70 MX |
3174 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3175 | &root->state); | |
d68fc57b YZ |
3176 | } |
3177 | ||
90290e19 JB |
3178 | if (block_rsv) { |
3179 | WARN_ON(block_rsv->size > 0); | |
3180 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3181 | } |
3182 | } | |
3183 | ||
7b128766 JB |
3184 | /* |
3185 | * This creates an orphan entry for the given inode in case something goes | |
3186 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3187 | * |
3188 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3189 | * this function. | |
7b128766 JB |
3190 | */ |
3191 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3192 | { | |
3193 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3194 | struct btrfs_block_rsv *block_rsv = NULL; |
3195 | int reserve = 0; | |
3196 | int insert = 0; | |
3197 | int ret; | |
7b128766 | 3198 | |
d68fc57b | 3199 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3200 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3201 | if (!block_rsv) |
3202 | return -ENOMEM; | |
d68fc57b | 3203 | } |
7b128766 | 3204 | |
d68fc57b YZ |
3205 | spin_lock(&root->orphan_lock); |
3206 | if (!root->orphan_block_rsv) { | |
3207 | root->orphan_block_rsv = block_rsv; | |
3208 | } else if (block_rsv) { | |
3209 | btrfs_free_block_rsv(root, block_rsv); | |
3210 | block_rsv = NULL; | |
7b128766 | 3211 | } |
7b128766 | 3212 | |
8a35d95f JB |
3213 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3214 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3215 | #if 0 |
3216 | /* | |
3217 | * For proper ENOSPC handling, we should do orphan | |
3218 | * cleanup when mounting. But this introduces backward | |
3219 | * compatibility issue. | |
3220 | */ | |
3221 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3222 | insert = 2; | |
3223 | else | |
3224 | insert = 1; | |
3225 | #endif | |
3226 | insert = 1; | |
321f0e70 | 3227 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3228 | } |
3229 | ||
72ac3c0d JB |
3230 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3231 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3232 | reserve = 1; |
d68fc57b | 3233 | spin_unlock(&root->orphan_lock); |
7b128766 | 3234 | |
d68fc57b YZ |
3235 | /* grab metadata reservation from transaction handle */ |
3236 | if (reserve) { | |
3237 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
79787eaa | 3238 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
d68fc57b | 3239 | } |
7b128766 | 3240 | |
d68fc57b YZ |
3241 | /* insert an orphan item to track this unlinked/truncated file */ |
3242 | if (insert >= 1) { | |
33345d01 | 3243 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3244 | if (ret) { |
703c88e0 | 3245 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3246 | if (reserve) { |
3247 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3248 | &BTRFS_I(inode)->runtime_flags); | |
3249 | btrfs_orphan_release_metadata(inode); | |
3250 | } | |
3251 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3252 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3253 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3254 | btrfs_abort_transaction(trans, root, ret); |
3255 | return ret; | |
3256 | } | |
79787eaa JM |
3257 | } |
3258 | ret = 0; | |
d68fc57b YZ |
3259 | } |
3260 | ||
3261 | /* insert an orphan item to track subvolume contains orphan files */ | |
3262 | if (insert >= 2) { | |
3263 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3264 | root->root_key.objectid); | |
79787eaa JM |
3265 | if (ret && ret != -EEXIST) { |
3266 | btrfs_abort_transaction(trans, root, ret); | |
3267 | return ret; | |
3268 | } | |
d68fc57b YZ |
3269 | } |
3270 | return 0; | |
7b128766 JB |
3271 | } |
3272 | ||
3273 | /* | |
3274 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3275 | * item for this particular inode. | |
3276 | */ | |
48a3b636 ES |
3277 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3278 | struct inode *inode) | |
7b128766 JB |
3279 | { |
3280 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3281 | int delete_item = 0; |
3282 | int release_rsv = 0; | |
7b128766 JB |
3283 | int ret = 0; |
3284 | ||
d68fc57b | 3285 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3286 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3287 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3288 | delete_item = 1; |
7b128766 | 3289 | |
72ac3c0d JB |
3290 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3291 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3292 | release_rsv = 1; |
d68fc57b | 3293 | spin_unlock(&root->orphan_lock); |
7b128766 | 3294 | |
703c88e0 | 3295 | if (delete_item) { |
8a35d95f | 3296 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3297 | if (trans) |
3298 | ret = btrfs_del_orphan_item(trans, root, | |
3299 | btrfs_ino(inode)); | |
8a35d95f | 3300 | } |
7b128766 | 3301 | |
703c88e0 FDBM |
3302 | if (release_rsv) |
3303 | btrfs_orphan_release_metadata(inode); | |
3304 | ||
4ef31a45 | 3305 | return ret; |
7b128766 JB |
3306 | } |
3307 | ||
3308 | /* | |
3309 | * this cleans up any orphans that may be left on the list from the last use | |
3310 | * of this root. | |
3311 | */ | |
66b4ffd1 | 3312 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3313 | { |
3314 | struct btrfs_path *path; | |
3315 | struct extent_buffer *leaf; | |
7b128766 JB |
3316 | struct btrfs_key key, found_key; |
3317 | struct btrfs_trans_handle *trans; | |
3318 | struct inode *inode; | |
8f6d7f4f | 3319 | u64 last_objectid = 0; |
7b128766 JB |
3320 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3321 | ||
d68fc57b | 3322 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3323 | return 0; |
c71bf099 YZ |
3324 | |
3325 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3326 | if (!path) { |
3327 | ret = -ENOMEM; | |
3328 | goto out; | |
3329 | } | |
7b128766 JB |
3330 | path->reada = -1; |
3331 | ||
3332 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3333 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3334 | key.offset = (u64)-1; |
3335 | ||
7b128766 JB |
3336 | while (1) { |
3337 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3338 | if (ret < 0) |
3339 | goto out; | |
7b128766 JB |
3340 | |
3341 | /* | |
3342 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3343 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3344 | * find the key and see if we have stuff that matches |
3345 | */ | |
3346 | if (ret > 0) { | |
66b4ffd1 | 3347 | ret = 0; |
7b128766 JB |
3348 | if (path->slots[0] == 0) |
3349 | break; | |
3350 | path->slots[0]--; | |
3351 | } | |
3352 | ||
3353 | /* pull out the item */ | |
3354 | leaf = path->nodes[0]; | |
7b128766 JB |
3355 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3356 | ||
3357 | /* make sure the item matches what we want */ | |
3358 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3359 | break; | |
962a298f | 3360 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3361 | break; |
3362 | ||
3363 | /* release the path since we're done with it */ | |
b3b4aa74 | 3364 | btrfs_release_path(path); |
7b128766 JB |
3365 | |
3366 | /* | |
3367 | * this is where we are basically btrfs_lookup, without the | |
3368 | * crossing root thing. we store the inode number in the | |
3369 | * offset of the orphan item. | |
3370 | */ | |
8f6d7f4f JB |
3371 | |
3372 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3373 | btrfs_err(root->fs_info, |
3374 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3375 | ret = -EINVAL; |
3376 | goto out; | |
3377 | } | |
3378 | ||
3379 | last_objectid = found_key.offset; | |
3380 | ||
5d4f98a2 YZ |
3381 | found_key.objectid = found_key.offset; |
3382 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3383 | found_key.offset = 0; | |
73f73415 | 3384 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3385 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3386 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3387 | goto out; |
7b128766 | 3388 | |
f8e9e0b0 AJ |
3389 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3390 | struct btrfs_root *dead_root; | |
3391 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3392 | int is_dead_root = 0; | |
3393 | ||
3394 | /* | |
3395 | * this is an orphan in the tree root. Currently these | |
3396 | * could come from 2 sources: | |
3397 | * a) a snapshot deletion in progress | |
3398 | * b) a free space cache inode | |
3399 | * We need to distinguish those two, as the snapshot | |
3400 | * orphan must not get deleted. | |
3401 | * find_dead_roots already ran before us, so if this | |
3402 | * is a snapshot deletion, we should find the root | |
3403 | * in the dead_roots list | |
3404 | */ | |
3405 | spin_lock(&fs_info->trans_lock); | |
3406 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3407 | root_list) { | |
3408 | if (dead_root->root_key.objectid == | |
3409 | found_key.objectid) { | |
3410 | is_dead_root = 1; | |
3411 | break; | |
3412 | } | |
3413 | } | |
3414 | spin_unlock(&fs_info->trans_lock); | |
3415 | if (is_dead_root) { | |
3416 | /* prevent this orphan from being found again */ | |
3417 | key.offset = found_key.objectid - 1; | |
3418 | continue; | |
3419 | } | |
3420 | } | |
7b128766 | 3421 | /* |
a8c9e576 JB |
3422 | * Inode is already gone but the orphan item is still there, |
3423 | * kill the orphan item. | |
7b128766 | 3424 | */ |
a8c9e576 JB |
3425 | if (ret == -ESTALE) { |
3426 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3427 | if (IS_ERR(trans)) { |
3428 | ret = PTR_ERR(trans); | |
3429 | goto out; | |
3430 | } | |
c2cf52eb SK |
3431 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3432 | found_key.objectid); | |
a8c9e576 JB |
3433 | ret = btrfs_del_orphan_item(trans, root, |
3434 | found_key.objectid); | |
5b21f2ed | 3435 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3436 | if (ret) |
3437 | goto out; | |
7b128766 JB |
3438 | continue; |
3439 | } | |
3440 | ||
a8c9e576 JB |
3441 | /* |
3442 | * add this inode to the orphan list so btrfs_orphan_del does | |
3443 | * the proper thing when we hit it | |
3444 | */ | |
8a35d95f JB |
3445 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3446 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3447 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3448 | |
7b128766 JB |
3449 | /* if we have links, this was a truncate, lets do that */ |
3450 | if (inode->i_nlink) { | |
fae7f21c | 3451 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3452 | iput(inode); |
3453 | continue; | |
3454 | } | |
7b128766 | 3455 | nr_truncate++; |
f3fe820c JB |
3456 | |
3457 | /* 1 for the orphan item deletion. */ | |
3458 | trans = btrfs_start_transaction(root, 1); | |
3459 | if (IS_ERR(trans)) { | |
c69b26b0 | 3460 | iput(inode); |
f3fe820c JB |
3461 | ret = PTR_ERR(trans); |
3462 | goto out; | |
3463 | } | |
3464 | ret = btrfs_orphan_add(trans, inode); | |
3465 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3466 | if (ret) { |
3467 | iput(inode); | |
f3fe820c | 3468 | goto out; |
c69b26b0 | 3469 | } |
f3fe820c | 3470 | |
66b4ffd1 | 3471 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3472 | if (ret) |
3473 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3474 | } else { |
3475 | nr_unlink++; | |
3476 | } | |
3477 | ||
3478 | /* this will do delete_inode and everything for us */ | |
3479 | iput(inode); | |
66b4ffd1 JB |
3480 | if (ret) |
3481 | goto out; | |
7b128766 | 3482 | } |
3254c876 MX |
3483 | /* release the path since we're done with it */ |
3484 | btrfs_release_path(path); | |
3485 | ||
d68fc57b YZ |
3486 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3487 | ||
3488 | if (root->orphan_block_rsv) | |
3489 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3490 | (u64)-1); | |
3491 | ||
27cdeb70 MX |
3492 | if (root->orphan_block_rsv || |
3493 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3494 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3495 | if (!IS_ERR(trans)) |
3496 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3497 | } |
7b128766 JB |
3498 | |
3499 | if (nr_unlink) | |
4884b476 | 3500 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3501 | if (nr_truncate) |
4884b476 | 3502 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3503 | |
3504 | out: | |
3505 | if (ret) | |
68b663d1 | 3506 | btrfs_err(root->fs_info, |
c2cf52eb | 3507 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3508 | btrfs_free_path(path); |
3509 | return ret; | |
7b128766 JB |
3510 | } |
3511 | ||
46a53cca CM |
3512 | /* |
3513 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3514 | * don't find any xattrs, we know there can't be any acls. | |
3515 | * | |
3516 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3517 | */ | |
3518 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3519 | int slot, u64 objectid, |
3520 | int *first_xattr_slot) | |
46a53cca CM |
3521 | { |
3522 | u32 nritems = btrfs_header_nritems(leaf); | |
3523 | struct btrfs_key found_key; | |
f23b5a59 JB |
3524 | static u64 xattr_access = 0; |
3525 | static u64 xattr_default = 0; | |
46a53cca CM |
3526 | int scanned = 0; |
3527 | ||
f23b5a59 JB |
3528 | if (!xattr_access) { |
3529 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, | |
3530 | strlen(POSIX_ACL_XATTR_ACCESS)); | |
3531 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, | |
3532 | strlen(POSIX_ACL_XATTR_DEFAULT)); | |
3533 | } | |
3534 | ||
46a53cca | 3535 | slot++; |
63541927 | 3536 | *first_xattr_slot = -1; |
46a53cca CM |
3537 | while (slot < nritems) { |
3538 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3539 | ||
3540 | /* we found a different objectid, there must not be acls */ | |
3541 | if (found_key.objectid != objectid) | |
3542 | return 0; | |
3543 | ||
3544 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3545 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3546 | if (*first_xattr_slot == -1) |
3547 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3548 | if (found_key.offset == xattr_access || |
3549 | found_key.offset == xattr_default) | |
3550 | return 1; | |
3551 | } | |
46a53cca CM |
3552 | |
3553 | /* | |
3554 | * we found a key greater than an xattr key, there can't | |
3555 | * be any acls later on | |
3556 | */ | |
3557 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3558 | return 0; | |
3559 | ||
3560 | slot++; | |
3561 | scanned++; | |
3562 | ||
3563 | /* | |
3564 | * it goes inode, inode backrefs, xattrs, extents, | |
3565 | * so if there are a ton of hard links to an inode there can | |
3566 | * be a lot of backrefs. Don't waste time searching too hard, | |
3567 | * this is just an optimization | |
3568 | */ | |
3569 | if (scanned >= 8) | |
3570 | break; | |
3571 | } | |
3572 | /* we hit the end of the leaf before we found an xattr or | |
3573 | * something larger than an xattr. We have to assume the inode | |
3574 | * has acls | |
3575 | */ | |
63541927 FDBM |
3576 | if (*first_xattr_slot == -1) |
3577 | *first_xattr_slot = slot; | |
46a53cca CM |
3578 | return 1; |
3579 | } | |
3580 | ||
d352ac68 CM |
3581 | /* |
3582 | * read an inode from the btree into the in-memory inode | |
3583 | */ | |
5d4f98a2 | 3584 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3585 | { |
3586 | struct btrfs_path *path; | |
5f39d397 | 3587 | struct extent_buffer *leaf; |
39279cc3 CM |
3588 | struct btrfs_inode_item *inode_item; |
3589 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3590 | struct btrfs_key location; | |
67de1176 | 3591 | unsigned long ptr; |
46a53cca | 3592 | int maybe_acls; |
618e21d5 | 3593 | u32 rdev; |
39279cc3 | 3594 | int ret; |
2f7e33d4 | 3595 | bool filled = false; |
63541927 | 3596 | int first_xattr_slot; |
2f7e33d4 MX |
3597 | |
3598 | ret = btrfs_fill_inode(inode, &rdev); | |
3599 | if (!ret) | |
3600 | filled = true; | |
39279cc3 CM |
3601 | |
3602 | path = btrfs_alloc_path(); | |
1748f843 MF |
3603 | if (!path) |
3604 | goto make_bad; | |
3605 | ||
39279cc3 | 3606 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3607 | |
39279cc3 | 3608 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3609 | if (ret) |
39279cc3 | 3610 | goto make_bad; |
39279cc3 | 3611 | |
5f39d397 | 3612 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3613 | |
3614 | if (filled) | |
67de1176 | 3615 | goto cache_index; |
2f7e33d4 | 3616 | |
5f39d397 CM |
3617 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3618 | struct btrfs_inode_item); | |
5f39d397 | 3619 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3620 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3621 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3622 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3623 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3624 | |
a937b979 DS |
3625 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3626 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3627 | |
a937b979 DS |
3628 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3629 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3630 | |
a937b979 DS |
3631 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3632 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3633 | |
9cc97d64 | 3634 | BTRFS_I(inode)->i_otime.tv_sec = |
3635 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3636 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3637 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3638 | |
a76a3cd4 | 3639 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3640 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3641 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3642 | ||
3643 | /* | |
3644 | * If we were modified in the current generation and evicted from memory | |
3645 | * and then re-read we need to do a full sync since we don't have any | |
3646 | * idea about which extents were modified before we were evicted from | |
3647 | * cache. | |
3648 | */ | |
3649 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3650 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3651 | &BTRFS_I(inode)->runtime_flags); | |
3652 | ||
0c4d2d95 | 3653 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
e02119d5 | 3654 | inode->i_generation = BTRFS_I(inode)->generation; |
618e21d5 | 3655 | inode->i_rdev = 0; |
5f39d397 CM |
3656 | rdev = btrfs_inode_rdev(leaf, inode_item); |
3657 | ||
aec7477b | 3658 | BTRFS_I(inode)->index_cnt = (u64)-1; |
d2fb3437 | 3659 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
67de1176 MX |
3660 | |
3661 | cache_index: | |
3662 | path->slots[0]++; | |
3663 | if (inode->i_nlink != 1 || | |
3664 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3665 | goto cache_acl; | |
3666 | ||
3667 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3668 | if (location.objectid != btrfs_ino(inode)) | |
3669 | goto cache_acl; | |
3670 | ||
3671 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3672 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3673 | struct btrfs_inode_ref *ref; | |
3674 | ||
3675 | ref = (struct btrfs_inode_ref *)ptr; | |
3676 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3677 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3678 | struct btrfs_inode_extref *extref; | |
3679 | ||
3680 | extref = (struct btrfs_inode_extref *)ptr; | |
3681 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3682 | extref); | |
3683 | } | |
2f7e33d4 | 3684 | cache_acl: |
46a53cca CM |
3685 | /* |
3686 | * try to precache a NULL acl entry for files that don't have | |
3687 | * any xattrs or acls | |
3688 | */ | |
33345d01 | 3689 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3690 | btrfs_ino(inode), &first_xattr_slot); |
3691 | if (first_xattr_slot != -1) { | |
3692 | path->slots[0] = first_xattr_slot; | |
3693 | ret = btrfs_load_inode_props(inode, path); | |
3694 | if (ret) | |
3695 | btrfs_err(root->fs_info, | |
351fd353 | 3696 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3697 | btrfs_ino(inode), |
3698 | root->root_key.objectid, ret); | |
3699 | } | |
3700 | btrfs_free_path(path); | |
3701 | ||
72c04902 AV |
3702 | if (!maybe_acls) |
3703 | cache_no_acl(inode); | |
46a53cca | 3704 | |
39279cc3 | 3705 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3706 | case S_IFREG: |
3707 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3708 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3709 | inode->i_fop = &btrfs_file_operations; |
3710 | inode->i_op = &btrfs_file_inode_operations; | |
3711 | break; | |
3712 | case S_IFDIR: | |
3713 | inode->i_fop = &btrfs_dir_file_operations; | |
3714 | if (root == root->fs_info->tree_root) | |
3715 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3716 | else | |
3717 | inode->i_op = &btrfs_dir_inode_operations; | |
3718 | break; | |
3719 | case S_IFLNK: | |
3720 | inode->i_op = &btrfs_symlink_inode_operations; | |
3721 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
3722 | break; | |
618e21d5 | 3723 | default: |
0279b4cd | 3724 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3725 | init_special_inode(inode, inode->i_mode, rdev); |
3726 | break; | |
39279cc3 | 3727 | } |
6cbff00f CH |
3728 | |
3729 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3730 | return; |
3731 | ||
3732 | make_bad: | |
39279cc3 | 3733 | btrfs_free_path(path); |
39279cc3 CM |
3734 | make_bad_inode(inode); |
3735 | } | |
3736 | ||
d352ac68 CM |
3737 | /* |
3738 | * given a leaf and an inode, copy the inode fields into the leaf | |
3739 | */ | |
e02119d5 CM |
3740 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3741 | struct extent_buffer *leaf, | |
5f39d397 | 3742 | struct btrfs_inode_item *item, |
39279cc3 CM |
3743 | struct inode *inode) |
3744 | { | |
51fab693 LB |
3745 | struct btrfs_map_token token; |
3746 | ||
3747 | btrfs_init_map_token(&token); | |
5f39d397 | 3748 | |
51fab693 LB |
3749 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3750 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3751 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3752 | &token); | |
3753 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3754 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3755 | |
a937b979 | 3756 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3757 | inode->i_atime.tv_sec, &token); |
a937b979 | 3758 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3759 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3760 | |
a937b979 | 3761 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3762 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3763 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3764 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3765 | |
a937b979 | 3766 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3767 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3768 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3769 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3770 | |
9cc97d64 | 3771 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3772 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3773 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3774 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3775 | ||
51fab693 LB |
3776 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3777 | &token); | |
3778 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3779 | &token); | |
3780 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3781 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3782 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3783 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3784 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3785 | } |
3786 | ||
d352ac68 CM |
3787 | /* |
3788 | * copy everything in the in-memory inode into the btree. | |
3789 | */ | |
2115133f | 3790 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3791 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3792 | { |
3793 | struct btrfs_inode_item *inode_item; | |
3794 | struct btrfs_path *path; | |
5f39d397 | 3795 | struct extent_buffer *leaf; |
39279cc3 CM |
3796 | int ret; |
3797 | ||
3798 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3799 | if (!path) |
3800 | return -ENOMEM; | |
3801 | ||
b9473439 | 3802 | path->leave_spinning = 1; |
16cdcec7 MX |
3803 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3804 | 1); | |
39279cc3 CM |
3805 | if (ret) { |
3806 | if (ret > 0) | |
3807 | ret = -ENOENT; | |
3808 | goto failed; | |
3809 | } | |
3810 | ||
5f39d397 CM |
3811 | leaf = path->nodes[0]; |
3812 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3813 | struct btrfs_inode_item); |
39279cc3 | 3814 | |
e02119d5 | 3815 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3816 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3817 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3818 | ret = 0; |
3819 | failed: | |
39279cc3 CM |
3820 | btrfs_free_path(path); |
3821 | return ret; | |
3822 | } | |
3823 | ||
2115133f CM |
3824 | /* |
3825 | * copy everything in the in-memory inode into the btree. | |
3826 | */ | |
3827 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3828 | struct btrfs_root *root, struct inode *inode) | |
3829 | { | |
3830 | int ret; | |
3831 | ||
3832 | /* | |
3833 | * If the inode is a free space inode, we can deadlock during commit | |
3834 | * if we put it into the delayed code. | |
3835 | * | |
3836 | * The data relocation inode should also be directly updated | |
3837 | * without delay | |
3838 | */ | |
83eea1f1 | 3839 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a JB |
3840 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
3841 | && !root->fs_info->log_root_recovering) { | |
8ea05e3a AB |
3842 | btrfs_update_root_times(trans, root); |
3843 | ||
2115133f CM |
3844 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3845 | if (!ret) | |
3846 | btrfs_set_inode_last_trans(trans, inode); | |
3847 | return ret; | |
3848 | } | |
3849 | ||
3850 | return btrfs_update_inode_item(trans, root, inode); | |
3851 | } | |
3852 | ||
be6aef60 JB |
3853 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3854 | struct btrfs_root *root, | |
3855 | struct inode *inode) | |
2115133f CM |
3856 | { |
3857 | int ret; | |
3858 | ||
3859 | ret = btrfs_update_inode(trans, root, inode); | |
3860 | if (ret == -ENOSPC) | |
3861 | return btrfs_update_inode_item(trans, root, inode); | |
3862 | return ret; | |
3863 | } | |
3864 | ||
d352ac68 CM |
3865 | /* |
3866 | * unlink helper that gets used here in inode.c and in the tree logging | |
3867 | * recovery code. It remove a link in a directory with a given name, and | |
3868 | * also drops the back refs in the inode to the directory | |
3869 | */ | |
92986796 AV |
3870 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3871 | struct btrfs_root *root, | |
3872 | struct inode *dir, struct inode *inode, | |
3873 | const char *name, int name_len) | |
39279cc3 CM |
3874 | { |
3875 | struct btrfs_path *path; | |
39279cc3 | 3876 | int ret = 0; |
5f39d397 | 3877 | struct extent_buffer *leaf; |
39279cc3 | 3878 | struct btrfs_dir_item *di; |
5f39d397 | 3879 | struct btrfs_key key; |
aec7477b | 3880 | u64 index; |
33345d01 LZ |
3881 | u64 ino = btrfs_ino(inode); |
3882 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3883 | |
3884 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3885 | if (!path) { |
3886 | ret = -ENOMEM; | |
554233a6 | 3887 | goto out; |
54aa1f4d CM |
3888 | } |
3889 | ||
b9473439 | 3890 | path->leave_spinning = 1; |
33345d01 | 3891 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3892 | name, name_len, -1); |
3893 | if (IS_ERR(di)) { | |
3894 | ret = PTR_ERR(di); | |
3895 | goto err; | |
3896 | } | |
3897 | if (!di) { | |
3898 | ret = -ENOENT; | |
3899 | goto err; | |
3900 | } | |
5f39d397 CM |
3901 | leaf = path->nodes[0]; |
3902 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3903 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3904 | if (ret) |
3905 | goto err; | |
b3b4aa74 | 3906 | btrfs_release_path(path); |
39279cc3 | 3907 | |
67de1176 MX |
3908 | /* |
3909 | * If we don't have dir index, we have to get it by looking up | |
3910 | * the inode ref, since we get the inode ref, remove it directly, | |
3911 | * it is unnecessary to do delayed deletion. | |
3912 | * | |
3913 | * But if we have dir index, needn't search inode ref to get it. | |
3914 | * Since the inode ref is close to the inode item, it is better | |
3915 | * that we delay to delete it, and just do this deletion when | |
3916 | * we update the inode item. | |
3917 | */ | |
3918 | if (BTRFS_I(inode)->dir_index) { | |
3919 | ret = btrfs_delayed_delete_inode_ref(inode); | |
3920 | if (!ret) { | |
3921 | index = BTRFS_I(inode)->dir_index; | |
3922 | goto skip_backref; | |
3923 | } | |
3924 | } | |
3925 | ||
33345d01 LZ |
3926 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3927 | dir_ino, &index); | |
aec7477b | 3928 | if (ret) { |
c2cf52eb SK |
3929 | btrfs_info(root->fs_info, |
3930 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 3931 | name_len, name, ino, dir_ino); |
79787eaa | 3932 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
3933 | goto err; |
3934 | } | |
67de1176 | 3935 | skip_backref: |
16cdcec7 | 3936 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3937 | if (ret) { |
3938 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 3939 | goto err; |
79787eaa | 3940 | } |
39279cc3 | 3941 | |
e02119d5 | 3942 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 3943 | inode, dir_ino); |
79787eaa JM |
3944 | if (ret != 0 && ret != -ENOENT) { |
3945 | btrfs_abort_transaction(trans, root, ret); | |
3946 | goto err; | |
3947 | } | |
e02119d5 CM |
3948 | |
3949 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
3950 | dir, index); | |
6418c961 CM |
3951 | if (ret == -ENOENT) |
3952 | ret = 0; | |
d4e3991b ZB |
3953 | else if (ret) |
3954 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
3955 | err: |
3956 | btrfs_free_path(path); | |
e02119d5 CM |
3957 | if (ret) |
3958 | goto out; | |
3959 | ||
3960 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
3961 | inode_inc_iversion(inode); |
3962 | inode_inc_iversion(dir); | |
e02119d5 | 3963 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
b9959295 | 3964 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 3965 | out: |
39279cc3 CM |
3966 | return ret; |
3967 | } | |
3968 | ||
92986796 AV |
3969 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3970 | struct btrfs_root *root, | |
3971 | struct inode *dir, struct inode *inode, | |
3972 | const char *name, int name_len) | |
3973 | { | |
3974 | int ret; | |
3975 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
3976 | if (!ret) { | |
8b558c5f | 3977 | drop_nlink(inode); |
92986796 AV |
3978 | ret = btrfs_update_inode(trans, root, inode); |
3979 | } | |
3980 | return ret; | |
3981 | } | |
39279cc3 | 3982 | |
a22285a6 YZ |
3983 | /* |
3984 | * helper to start transaction for unlink and rmdir. | |
3985 | * | |
d52be818 JB |
3986 | * unlink and rmdir are special in btrfs, they do not always free space, so |
3987 | * if we cannot make our reservations the normal way try and see if there is | |
3988 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
3989 | * allow the unlink to occur. | |
a22285a6 | 3990 | */ |
d52be818 | 3991 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 3992 | { |
39279cc3 | 3993 | struct btrfs_trans_handle *trans; |
a22285a6 | 3994 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
3995 | int ret; |
3996 | ||
e70bea5f JB |
3997 | /* |
3998 | * 1 for the possible orphan item | |
3999 | * 1 for the dir item | |
4000 | * 1 for the dir index | |
4001 | * 1 for the inode ref | |
e70bea5f JB |
4002 | * 1 for the inode |
4003 | */ | |
6e137ed3 | 4004 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
4005 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
4006 | return trans; | |
4df27c4d | 4007 | |
d52be818 JB |
4008 | if (PTR_ERR(trans) == -ENOSPC) { |
4009 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); | |
4df27c4d | 4010 | |
d52be818 JB |
4011 | trans = btrfs_start_transaction(root, 0); |
4012 | if (IS_ERR(trans)) | |
4013 | return trans; | |
4014 | ret = btrfs_cond_migrate_bytes(root->fs_info, | |
4015 | &root->fs_info->trans_block_rsv, | |
4016 | num_bytes, 5); | |
4017 | if (ret) { | |
4018 | btrfs_end_transaction(trans, root); | |
4019 | return ERR_PTR(ret); | |
a22285a6 | 4020 | } |
5a77d76c | 4021 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
d52be818 | 4022 | trans->bytes_reserved = num_bytes; |
a22285a6 | 4023 | } |
d52be818 | 4024 | return trans; |
a22285a6 YZ |
4025 | } |
4026 | ||
4027 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4028 | { | |
4029 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4030 | struct btrfs_trans_handle *trans; | |
4031 | struct inode *inode = dentry->d_inode; | |
4032 | int ret; | |
a22285a6 | 4033 | |
d52be818 | 4034 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4035 | if (IS_ERR(trans)) |
4036 | return PTR_ERR(trans); | |
5f39d397 | 4037 | |
12fcfd22 CM |
4038 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
4039 | ||
e02119d5 CM |
4040 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
4041 | dentry->d_name.name, dentry->d_name.len); | |
b532402e TI |
4042 | if (ret) |
4043 | goto out; | |
7b128766 | 4044 | |
a22285a6 | 4045 | if (inode->i_nlink == 0) { |
7b128766 | 4046 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4047 | if (ret) |
4048 | goto out; | |
a22285a6 | 4049 | } |
7b128766 | 4050 | |
b532402e | 4051 | out: |
d52be818 | 4052 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4053 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4054 | return ret; |
4055 | } | |
4056 | ||
4df27c4d YZ |
4057 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4058 | struct btrfs_root *root, | |
4059 | struct inode *dir, u64 objectid, | |
4060 | const char *name, int name_len) | |
4061 | { | |
4062 | struct btrfs_path *path; | |
4063 | struct extent_buffer *leaf; | |
4064 | struct btrfs_dir_item *di; | |
4065 | struct btrfs_key key; | |
4066 | u64 index; | |
4067 | int ret; | |
33345d01 | 4068 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4069 | |
4070 | path = btrfs_alloc_path(); | |
4071 | if (!path) | |
4072 | return -ENOMEM; | |
4073 | ||
33345d01 | 4074 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4075 | name, name_len, -1); |
79787eaa JM |
4076 | if (IS_ERR_OR_NULL(di)) { |
4077 | if (!di) | |
4078 | ret = -ENOENT; | |
4079 | else | |
4080 | ret = PTR_ERR(di); | |
4081 | goto out; | |
4082 | } | |
4df27c4d YZ |
4083 | |
4084 | leaf = path->nodes[0]; | |
4085 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4086 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4087 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
4088 | if (ret) { |
4089 | btrfs_abort_transaction(trans, root, ret); | |
4090 | goto out; | |
4091 | } | |
b3b4aa74 | 4092 | btrfs_release_path(path); |
4df27c4d YZ |
4093 | |
4094 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4095 | objectid, root->root_key.objectid, | |
33345d01 | 4096 | dir_ino, &index, name, name_len); |
4df27c4d | 4097 | if (ret < 0) { |
79787eaa JM |
4098 | if (ret != -ENOENT) { |
4099 | btrfs_abort_transaction(trans, root, ret); | |
4100 | goto out; | |
4101 | } | |
33345d01 | 4102 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4103 | name, name_len); |
79787eaa JM |
4104 | if (IS_ERR_OR_NULL(di)) { |
4105 | if (!di) | |
4106 | ret = -ENOENT; | |
4107 | else | |
4108 | ret = PTR_ERR(di); | |
4109 | btrfs_abort_transaction(trans, root, ret); | |
4110 | goto out; | |
4111 | } | |
4df27c4d YZ |
4112 | |
4113 | leaf = path->nodes[0]; | |
4114 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4115 | btrfs_release_path(path); |
4df27c4d YZ |
4116 | index = key.offset; |
4117 | } | |
945d8962 | 4118 | btrfs_release_path(path); |
4df27c4d | 4119 | |
16cdcec7 | 4120 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4121 | if (ret) { |
4122 | btrfs_abort_transaction(trans, root, ret); | |
4123 | goto out; | |
4124 | } | |
4df27c4d YZ |
4125 | |
4126 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4127 | inode_inc_iversion(dir); |
4df27c4d | 4128 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
5a24e84c | 4129 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
4130 | if (ret) |
4131 | btrfs_abort_transaction(trans, root, ret); | |
4132 | out: | |
71d7aed0 | 4133 | btrfs_free_path(path); |
79787eaa | 4134 | return ret; |
4df27c4d YZ |
4135 | } |
4136 | ||
39279cc3 CM |
4137 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4138 | { | |
4139 | struct inode *inode = dentry->d_inode; | |
1832a6d5 | 4140 | int err = 0; |
39279cc3 | 4141 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4142 | struct btrfs_trans_handle *trans; |
39279cc3 | 4143 | |
b3ae244e | 4144 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4145 | return -ENOTEMPTY; |
b3ae244e DS |
4146 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4147 | return -EPERM; | |
134d4512 | 4148 | |
d52be818 | 4149 | trans = __unlink_start_trans(dir); |
a22285a6 | 4150 | if (IS_ERR(trans)) |
5df6a9f6 | 4151 | return PTR_ERR(trans); |
5df6a9f6 | 4152 | |
33345d01 | 4153 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4154 | err = btrfs_unlink_subvol(trans, root, dir, |
4155 | BTRFS_I(inode)->location.objectid, | |
4156 | dentry->d_name.name, | |
4157 | dentry->d_name.len); | |
4158 | goto out; | |
4159 | } | |
4160 | ||
7b128766 JB |
4161 | err = btrfs_orphan_add(trans, inode); |
4162 | if (err) | |
4df27c4d | 4163 | goto out; |
7b128766 | 4164 | |
39279cc3 | 4165 | /* now the directory is empty */ |
e02119d5 CM |
4166 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
4167 | dentry->d_name.name, dentry->d_name.len); | |
d397712b | 4168 | if (!err) |
dbe674a9 | 4169 | btrfs_i_size_write(inode, 0); |
4df27c4d | 4170 | out: |
d52be818 | 4171 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4172 | btrfs_btree_balance_dirty(root); |
3954401f | 4173 | |
39279cc3 CM |
4174 | return err; |
4175 | } | |
4176 | ||
28f75a0e CM |
4177 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4178 | struct btrfs_root *root, | |
4179 | u64 bytes_deleted) | |
4180 | { | |
4181 | int ret; | |
4182 | ||
4183 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); | |
4184 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, | |
4185 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
4186 | if (!ret) | |
4187 | trans->bytes_reserved += bytes_deleted; | |
4188 | return ret; | |
4189 | ||
4190 | } | |
4191 | ||
39279cc3 CM |
4192 | /* |
4193 | * this can truncate away extent items, csum items and directory items. | |
4194 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4195 | * any higher than new_size |
39279cc3 CM |
4196 | * |
4197 | * csum items that cross the new i_size are truncated to the new size | |
4198 | * as well. | |
7b128766 JB |
4199 | * |
4200 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4201 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4202 | */ |
8082510e YZ |
4203 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4204 | struct btrfs_root *root, | |
4205 | struct inode *inode, | |
4206 | u64 new_size, u32 min_type) | |
39279cc3 | 4207 | { |
39279cc3 | 4208 | struct btrfs_path *path; |
5f39d397 | 4209 | struct extent_buffer *leaf; |
39279cc3 | 4210 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4211 | struct btrfs_key key; |
4212 | struct btrfs_key found_key; | |
39279cc3 | 4213 | u64 extent_start = 0; |
db94535d | 4214 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4215 | u64 extent_offset = 0; |
39279cc3 | 4216 | u64 item_end = 0; |
7f4f6e0a | 4217 | u64 last_size = (u64)-1; |
8082510e | 4218 | u32 found_type = (u8)-1; |
39279cc3 CM |
4219 | int found_extent; |
4220 | int del_item; | |
85e21bac CM |
4221 | int pending_del_nr = 0; |
4222 | int pending_del_slot = 0; | |
179e29e4 | 4223 | int extent_type = -1; |
8082510e YZ |
4224 | int ret; |
4225 | int err = 0; | |
33345d01 | 4226 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4227 | u64 bytes_deleted = 0; |
1262133b JB |
4228 | bool be_nice = 0; |
4229 | bool should_throttle = 0; | |
28f75a0e | 4230 | bool should_end = 0; |
8082510e YZ |
4231 | |
4232 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4233 | |
28ed1345 CM |
4234 | /* |
4235 | * for non-free space inodes and ref cows, we want to back off from | |
4236 | * time to time | |
4237 | */ | |
4238 | if (!btrfs_is_free_space_inode(inode) && | |
4239 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4240 | be_nice = 1; | |
4241 | ||
0eb0e19c MF |
4242 | path = btrfs_alloc_path(); |
4243 | if (!path) | |
4244 | return -ENOMEM; | |
4245 | path->reada = -1; | |
4246 | ||
5dc562c5 JB |
4247 | /* |
4248 | * We want to drop from the next block forward in case this new size is | |
4249 | * not block aligned since we will be keeping the last block of the | |
4250 | * extent just the way it is. | |
4251 | */ | |
27cdeb70 MX |
4252 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4253 | root == root->fs_info->tree_root) | |
fda2832f QW |
4254 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4255 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4256 | |
16cdcec7 MX |
4257 | /* |
4258 | * This function is also used to drop the items in the log tree before | |
4259 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4260 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4261 | * items. | |
4262 | */ | |
4263 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4264 | btrfs_kill_delayed_inode_items(inode); | |
4265 | ||
33345d01 | 4266 | key.objectid = ino; |
39279cc3 | 4267 | key.offset = (u64)-1; |
5f39d397 CM |
4268 | key.type = (u8)-1; |
4269 | ||
85e21bac | 4270 | search_again: |
28ed1345 CM |
4271 | /* |
4272 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4273 | * up a huge file in a single leaf. Most of the time that | |
4274 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4275 | */ | |
4276 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | |
4277 | if (btrfs_should_end_transaction(trans, root)) { | |
4278 | err = -EAGAIN; | |
4279 | goto error; | |
4280 | } | |
4281 | } | |
4282 | ||
4283 | ||
b9473439 | 4284 | path->leave_spinning = 1; |
85e21bac | 4285 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4286 | if (ret < 0) { |
4287 | err = ret; | |
4288 | goto out; | |
4289 | } | |
d397712b | 4290 | |
85e21bac | 4291 | if (ret > 0) { |
e02119d5 CM |
4292 | /* there are no items in the tree for us to truncate, we're |
4293 | * done | |
4294 | */ | |
8082510e YZ |
4295 | if (path->slots[0] == 0) |
4296 | goto out; | |
85e21bac CM |
4297 | path->slots[0]--; |
4298 | } | |
4299 | ||
d397712b | 4300 | while (1) { |
39279cc3 | 4301 | fi = NULL; |
5f39d397 CM |
4302 | leaf = path->nodes[0]; |
4303 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4304 | found_type = found_key.type; |
39279cc3 | 4305 | |
33345d01 | 4306 | if (found_key.objectid != ino) |
39279cc3 | 4307 | break; |
5f39d397 | 4308 | |
85e21bac | 4309 | if (found_type < min_type) |
39279cc3 CM |
4310 | break; |
4311 | ||
5f39d397 | 4312 | item_end = found_key.offset; |
39279cc3 | 4313 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4314 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4315 | struct btrfs_file_extent_item); |
179e29e4 CM |
4316 | extent_type = btrfs_file_extent_type(leaf, fi); |
4317 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4318 | item_end += |
db94535d | 4319 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4320 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4321 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4322 | path->slots[0], fi); |
39279cc3 | 4323 | } |
008630c1 | 4324 | item_end--; |
39279cc3 | 4325 | } |
8082510e YZ |
4326 | if (found_type > min_type) { |
4327 | del_item = 1; | |
4328 | } else { | |
4329 | if (item_end < new_size) | |
b888db2b | 4330 | break; |
8082510e YZ |
4331 | if (found_key.offset >= new_size) |
4332 | del_item = 1; | |
4333 | else | |
4334 | del_item = 0; | |
39279cc3 | 4335 | } |
39279cc3 | 4336 | found_extent = 0; |
39279cc3 | 4337 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4338 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4339 | goto delete; | |
4340 | ||
7f4f6e0a JB |
4341 | if (del_item) |
4342 | last_size = found_key.offset; | |
4343 | else | |
4344 | last_size = new_size; | |
4345 | ||
179e29e4 | 4346 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4347 | u64 num_dec; |
db94535d | 4348 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4349 | if (!del_item) { |
db94535d CM |
4350 | u64 orig_num_bytes = |
4351 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4352 | extent_num_bytes = ALIGN(new_size - |
4353 | found_key.offset, | |
4354 | root->sectorsize); | |
db94535d CM |
4355 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4356 | extent_num_bytes); | |
4357 | num_dec = (orig_num_bytes - | |
9069218d | 4358 | extent_num_bytes); |
27cdeb70 MX |
4359 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4360 | &root->state) && | |
4361 | extent_start != 0) | |
a76a3cd4 | 4362 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4363 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4364 | } else { |
db94535d CM |
4365 | extent_num_bytes = |
4366 | btrfs_file_extent_disk_num_bytes(leaf, | |
4367 | fi); | |
5d4f98a2 YZ |
4368 | extent_offset = found_key.offset - |
4369 | btrfs_file_extent_offset(leaf, fi); | |
4370 | ||
39279cc3 | 4371 | /* FIXME blocksize != 4096 */ |
9069218d | 4372 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4373 | if (extent_start != 0) { |
4374 | found_extent = 1; | |
27cdeb70 MX |
4375 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4376 | &root->state)) | |
a76a3cd4 | 4377 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4378 | } |
39279cc3 | 4379 | } |
9069218d | 4380 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4381 | /* |
4382 | * we can't truncate inline items that have had | |
4383 | * special encodings | |
4384 | */ | |
4385 | if (!del_item && | |
4386 | btrfs_file_extent_compression(leaf, fi) == 0 && | |
4387 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
4388 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
e02119d5 CM |
4389 | u32 size = new_size - found_key.offset; |
4390 | ||
27cdeb70 | 4391 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
a76a3cd4 YZ |
4392 | inode_sub_bytes(inode, item_end + 1 - |
4393 | new_size); | |
514ac8ad CM |
4394 | |
4395 | /* | |
4396 | * update the ram bytes to properly reflect | |
4397 | * the new size of our item | |
4398 | */ | |
4399 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
e02119d5 CM |
4400 | size = |
4401 | btrfs_file_extent_calc_inline_size(size); | |
afe5fea7 | 4402 | btrfs_truncate_item(root, path, size, 1); |
27cdeb70 MX |
4403 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4404 | &root->state)) { | |
a76a3cd4 YZ |
4405 | inode_sub_bytes(inode, item_end + 1 - |
4406 | found_key.offset); | |
9069218d | 4407 | } |
39279cc3 | 4408 | } |
179e29e4 | 4409 | delete: |
39279cc3 | 4410 | if (del_item) { |
85e21bac CM |
4411 | if (!pending_del_nr) { |
4412 | /* no pending yet, add ourselves */ | |
4413 | pending_del_slot = path->slots[0]; | |
4414 | pending_del_nr = 1; | |
4415 | } else if (pending_del_nr && | |
4416 | path->slots[0] + 1 == pending_del_slot) { | |
4417 | /* hop on the pending chunk */ | |
4418 | pending_del_nr++; | |
4419 | pending_del_slot = path->slots[0]; | |
4420 | } else { | |
d397712b | 4421 | BUG(); |
85e21bac | 4422 | } |
39279cc3 CM |
4423 | } else { |
4424 | break; | |
4425 | } | |
28f75a0e CM |
4426 | should_throttle = 0; |
4427 | ||
27cdeb70 MX |
4428 | if (found_extent && |
4429 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4430 | root == root->fs_info->tree_root)) { | |
b9473439 | 4431 | btrfs_set_path_blocking(path); |
28ed1345 | 4432 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4433 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4434 | extent_num_bytes, 0, |
4435 | btrfs_header_owner(leaf), | |
66d7e7f0 | 4436 | ino, extent_offset, 0); |
39279cc3 | 4437 | BUG_ON(ret); |
1262133b | 4438 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 CM |
4439 | btrfs_async_run_delayed_refs(root, |
4440 | trans->delayed_ref_updates * 2, 0); | |
28f75a0e CM |
4441 | if (be_nice) { |
4442 | if (truncate_space_check(trans, root, | |
4443 | extent_num_bytes)) { | |
4444 | should_end = 1; | |
4445 | } | |
4446 | if (btrfs_should_throttle_delayed_refs(trans, | |
4447 | root)) { | |
4448 | should_throttle = 1; | |
4449 | } | |
4450 | } | |
39279cc3 | 4451 | } |
85e21bac | 4452 | |
8082510e YZ |
4453 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4454 | break; | |
4455 | ||
4456 | if (path->slots[0] == 0 || | |
1262133b | 4457 | path->slots[0] != pending_del_slot || |
28f75a0e | 4458 | should_throttle || should_end) { |
8082510e YZ |
4459 | if (pending_del_nr) { |
4460 | ret = btrfs_del_items(trans, root, path, | |
4461 | pending_del_slot, | |
4462 | pending_del_nr); | |
79787eaa JM |
4463 | if (ret) { |
4464 | btrfs_abort_transaction(trans, | |
4465 | root, ret); | |
4466 | goto error; | |
4467 | } | |
8082510e YZ |
4468 | pending_del_nr = 0; |
4469 | } | |
b3b4aa74 | 4470 | btrfs_release_path(path); |
28f75a0e | 4471 | if (should_throttle) { |
1262133b JB |
4472 | unsigned long updates = trans->delayed_ref_updates; |
4473 | if (updates) { | |
4474 | trans->delayed_ref_updates = 0; | |
4475 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4476 | if (ret && !err) | |
4477 | err = ret; | |
4478 | } | |
4479 | } | |
28f75a0e CM |
4480 | /* |
4481 | * if we failed to refill our space rsv, bail out | |
4482 | * and let the transaction restart | |
4483 | */ | |
4484 | if (should_end) { | |
4485 | err = -EAGAIN; | |
4486 | goto error; | |
4487 | } | |
85e21bac | 4488 | goto search_again; |
8082510e YZ |
4489 | } else { |
4490 | path->slots[0]--; | |
85e21bac | 4491 | } |
39279cc3 | 4492 | } |
8082510e | 4493 | out: |
85e21bac CM |
4494 | if (pending_del_nr) { |
4495 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4496 | pending_del_nr); | |
79787eaa JM |
4497 | if (ret) |
4498 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4499 | } |
79787eaa | 4500 | error: |
dac5705c FM |
4501 | if (last_size != (u64)-1 && |
4502 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
7f4f6e0a | 4503 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4504 | |
39279cc3 | 4505 | btrfs_free_path(path); |
28ed1345 | 4506 | |
28f75a0e | 4507 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
28ed1345 CM |
4508 | unsigned long updates = trans->delayed_ref_updates; |
4509 | if (updates) { | |
4510 | trans->delayed_ref_updates = 0; | |
4511 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4512 | if (ret && !err) | |
4513 | err = ret; | |
4514 | } | |
4515 | } | |
8082510e | 4516 | return err; |
39279cc3 CM |
4517 | } |
4518 | ||
4519 | /* | |
2aaa6655 JB |
4520 | * btrfs_truncate_page - read, zero a chunk and write a page |
4521 | * @inode - inode that we're zeroing | |
4522 | * @from - the offset to start zeroing | |
4523 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4524 | * offset | |
4525 | * @front - zero up to the offset instead of from the offset on | |
4526 | * | |
4527 | * This will find the page for the "from" offset and cow the page and zero the | |
4528 | * part we want to zero. This is used with truncate and hole punching. | |
39279cc3 | 4529 | */ |
2aaa6655 JB |
4530 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
4531 | int front) | |
39279cc3 | 4532 | { |
2aaa6655 | 4533 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4534 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4535 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4536 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4537 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4538 | char *kaddr; |
db94535d | 4539 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
4540 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
4541 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4542 | struct page *page; | |
3b16a4e3 | 4543 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4544 | int ret = 0; |
a52d9a80 | 4545 | u64 page_start; |
e6dcd2dc | 4546 | u64 page_end; |
39279cc3 | 4547 | |
2aaa6655 JB |
4548 | if ((offset & (blocksize - 1)) == 0 && |
4549 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4550 | goto out; |
0ca1f7ce | 4551 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
5d5e103a JB |
4552 | if (ret) |
4553 | goto out; | |
39279cc3 | 4554 | |
211c17f5 | 4555 | again: |
3b16a4e3 | 4556 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4557 | if (!page) { |
0ca1f7ce | 4558 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
ac6a2b36 | 4559 | ret = -ENOMEM; |
39279cc3 | 4560 | goto out; |
5d5e103a | 4561 | } |
e6dcd2dc CM |
4562 | |
4563 | page_start = page_offset(page); | |
4564 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
4565 | ||
39279cc3 | 4566 | if (!PageUptodate(page)) { |
9ebefb18 | 4567 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4568 | lock_page(page); |
211c17f5 CM |
4569 | if (page->mapping != mapping) { |
4570 | unlock_page(page); | |
4571 | page_cache_release(page); | |
4572 | goto again; | |
4573 | } | |
39279cc3 CM |
4574 | if (!PageUptodate(page)) { |
4575 | ret = -EIO; | |
89642229 | 4576 | goto out_unlock; |
39279cc3 CM |
4577 | } |
4578 | } | |
211c17f5 | 4579 | wait_on_page_writeback(page); |
e6dcd2dc | 4580 | |
d0082371 | 4581 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
4582 | set_page_extent_mapped(page); |
4583 | ||
4584 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
4585 | if (ordered) { | |
2ac55d41 JB |
4586 | unlock_extent_cached(io_tree, page_start, page_end, |
4587 | &cached_state, GFP_NOFS); | |
e6dcd2dc CM |
4588 | unlock_page(page); |
4589 | page_cache_release(page); | |
eb84ae03 | 4590 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4591 | btrfs_put_ordered_extent(ordered); |
4592 | goto again; | |
4593 | } | |
4594 | ||
2ac55d41 | 4595 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
4596 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4597 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4598 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4599 | |
2ac55d41 JB |
4600 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
4601 | &cached_state); | |
9ed74f2d | 4602 | if (ret) { |
2ac55d41 JB |
4603 | unlock_extent_cached(io_tree, page_start, page_end, |
4604 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
4605 | goto out_unlock; |
4606 | } | |
4607 | ||
e6dcd2dc | 4608 | if (offset != PAGE_CACHE_SIZE) { |
2aaa6655 JB |
4609 | if (!len) |
4610 | len = PAGE_CACHE_SIZE - offset; | |
e6dcd2dc | 4611 | kaddr = kmap(page); |
2aaa6655 JB |
4612 | if (front) |
4613 | memset(kaddr, 0, offset); | |
4614 | else | |
4615 | memset(kaddr + offset, 0, len); | |
e6dcd2dc CM |
4616 | flush_dcache_page(page); |
4617 | kunmap(page); | |
4618 | } | |
247e743c | 4619 | ClearPageChecked(page); |
e6dcd2dc | 4620 | set_page_dirty(page); |
2ac55d41 JB |
4621 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
4622 | GFP_NOFS); | |
39279cc3 | 4623 | |
89642229 | 4624 | out_unlock: |
5d5e103a | 4625 | if (ret) |
0ca1f7ce | 4626 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
39279cc3 CM |
4627 | unlock_page(page); |
4628 | page_cache_release(page); | |
4629 | out: | |
4630 | return ret; | |
4631 | } | |
4632 | ||
16e7549f JB |
4633 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4634 | u64 offset, u64 len) | |
4635 | { | |
4636 | struct btrfs_trans_handle *trans; | |
4637 | int ret; | |
4638 | ||
4639 | /* | |
4640 | * Still need to make sure the inode looks like it's been updated so | |
4641 | * that any holes get logged if we fsync. | |
4642 | */ | |
4643 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4644 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4645 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4646 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4647 | return 0; | |
4648 | } | |
4649 | ||
4650 | /* | |
4651 | * 1 - for the one we're dropping | |
4652 | * 1 - for the one we're adding | |
4653 | * 1 - for updating the inode. | |
4654 | */ | |
4655 | trans = btrfs_start_transaction(root, 3); | |
4656 | if (IS_ERR(trans)) | |
4657 | return PTR_ERR(trans); | |
4658 | ||
4659 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4660 | if (ret) { | |
4661 | btrfs_abort_transaction(trans, root, ret); | |
4662 | btrfs_end_transaction(trans, root); | |
4663 | return ret; | |
4664 | } | |
4665 | ||
4666 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4667 | 0, 0, len, 0, len, 0, 0, 0); | |
4668 | if (ret) | |
4669 | btrfs_abort_transaction(trans, root, ret); | |
4670 | else | |
4671 | btrfs_update_inode(trans, root, inode); | |
4672 | btrfs_end_transaction(trans, root); | |
4673 | return ret; | |
4674 | } | |
4675 | ||
695a0d0d JB |
4676 | /* |
4677 | * This function puts in dummy file extents for the area we're creating a hole | |
4678 | * for. So if we are truncating this file to a larger size we need to insert | |
4679 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4680 | * the range between oldsize and size | |
4681 | */ | |
a41ad394 | 4682 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4683 | { |
9036c102 YZ |
4684 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4685 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4686 | struct extent_map *em = NULL; |
2ac55d41 | 4687 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4688 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4689 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4690 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4691 | u64 last_byte; |
4692 | u64 cur_offset; | |
4693 | u64 hole_size; | |
9ed74f2d | 4694 | int err = 0; |
39279cc3 | 4695 | |
a71754fc JB |
4696 | /* |
4697 | * If our size started in the middle of a page we need to zero out the | |
4698 | * rest of the page before we expand the i_size, otherwise we could | |
4699 | * expose stale data. | |
4700 | */ | |
4701 | err = btrfs_truncate_page(inode, oldsize, 0, 0); | |
4702 | if (err) | |
4703 | return err; | |
4704 | ||
9036c102 YZ |
4705 | if (size <= hole_start) |
4706 | return 0; | |
4707 | ||
9036c102 YZ |
4708 | while (1) { |
4709 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4710 | |
2ac55d41 | 4711 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
d0082371 | 4712 | &cached_state); |
fa7c1494 MX |
4713 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4714 | block_end - hole_start); | |
9036c102 YZ |
4715 | if (!ordered) |
4716 | break; | |
2ac55d41 JB |
4717 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4718 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4719 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4720 | btrfs_put_ordered_extent(ordered); |
4721 | } | |
39279cc3 | 4722 | |
9036c102 YZ |
4723 | cur_offset = hole_start; |
4724 | while (1) { | |
4725 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4726 | block_end - cur_offset, 0); | |
79787eaa JM |
4727 | if (IS_ERR(em)) { |
4728 | err = PTR_ERR(em); | |
f2767956 | 4729 | em = NULL; |
79787eaa JM |
4730 | break; |
4731 | } | |
9036c102 | 4732 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4733 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4734 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4735 | struct extent_map *hole_em; |
9036c102 | 4736 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4737 | |
16e7549f JB |
4738 | err = maybe_insert_hole(root, inode, cur_offset, |
4739 | hole_size); | |
4740 | if (err) | |
3893e33b | 4741 | break; |
5dc562c5 JB |
4742 | btrfs_drop_extent_cache(inode, cur_offset, |
4743 | cur_offset + hole_size - 1, 0); | |
4744 | hole_em = alloc_extent_map(); | |
4745 | if (!hole_em) { | |
4746 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4747 | &BTRFS_I(inode)->runtime_flags); | |
4748 | goto next; | |
4749 | } | |
4750 | hole_em->start = cur_offset; | |
4751 | hole_em->len = hole_size; | |
4752 | hole_em->orig_start = cur_offset; | |
8082510e | 4753 | |
5dc562c5 JB |
4754 | hole_em->block_start = EXTENT_MAP_HOLE; |
4755 | hole_em->block_len = 0; | |
b4939680 | 4756 | hole_em->orig_block_len = 0; |
cc95bef6 | 4757 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4758 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4759 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4760 | hole_em->generation = root->fs_info->generation; |
8082510e | 4761 | |
5dc562c5 JB |
4762 | while (1) { |
4763 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4764 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4765 | write_unlock(&em_tree->lock); |
4766 | if (err != -EEXIST) | |
4767 | break; | |
4768 | btrfs_drop_extent_cache(inode, cur_offset, | |
4769 | cur_offset + | |
4770 | hole_size - 1, 0); | |
4771 | } | |
4772 | free_extent_map(hole_em); | |
9036c102 | 4773 | } |
16e7549f | 4774 | next: |
9036c102 | 4775 | free_extent_map(em); |
a22285a6 | 4776 | em = NULL; |
9036c102 | 4777 | cur_offset = last_byte; |
8082510e | 4778 | if (cur_offset >= block_end) |
9036c102 YZ |
4779 | break; |
4780 | } | |
a22285a6 | 4781 | free_extent_map(em); |
2ac55d41 JB |
4782 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4783 | GFP_NOFS); | |
9036c102 YZ |
4784 | return err; |
4785 | } | |
39279cc3 | 4786 | |
9ea24bbe FM |
4787 | static int wait_snapshoting_atomic_t(atomic_t *a) |
4788 | { | |
4789 | schedule(); | |
4790 | return 0; | |
4791 | } | |
4792 | ||
4793 | static void wait_for_snapshot_creation(struct btrfs_root *root) | |
4794 | { | |
4795 | while (true) { | |
4796 | int ret; | |
4797 | ||
4798 | ret = btrfs_start_write_no_snapshoting(root); | |
4799 | if (ret) | |
4800 | break; | |
4801 | wait_on_atomic_t(&root->will_be_snapshoted, | |
4802 | wait_snapshoting_atomic_t, | |
4803 | TASK_UNINTERRUPTIBLE); | |
4804 | } | |
4805 | } | |
4806 | ||
3972f260 | 4807 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4808 | { |
f4a2f4c5 MX |
4809 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4810 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4811 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4812 | loff_t newsize = attr->ia_size; |
4813 | int mask = attr->ia_valid; | |
8082510e YZ |
4814 | int ret; |
4815 | ||
3972f260 ES |
4816 | /* |
4817 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4818 | * special case where we need to update the times despite not having | |
4819 | * these flags set. For all other operations the VFS set these flags | |
4820 | * explicitly if it wants a timestamp update. | |
4821 | */ | |
dff6efc3 CH |
4822 | if (newsize != oldsize) { |
4823 | inode_inc_iversion(inode); | |
4824 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4825 | inode->i_ctime = inode->i_mtime = | |
4826 | current_fs_time(inode->i_sb); | |
4827 | } | |
3972f260 | 4828 | |
a41ad394 | 4829 | if (newsize > oldsize) { |
7caef267 | 4830 | truncate_pagecache(inode, newsize); |
9ea24bbe FM |
4831 | /* |
4832 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4833 | * This is to ensure the snapshot captures a fully consistent | |
4834 | * state of this file - if the snapshot captures this expanding | |
4835 | * truncation, it must capture all writes that happened before | |
4836 | * this truncation. | |
4837 | */ | |
4838 | wait_for_snapshot_creation(root); | |
a41ad394 | 4839 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4840 | if (ret) { |
4841 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4842 | return ret; |
9ea24bbe | 4843 | } |
8082510e | 4844 | |
f4a2f4c5 | 4845 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4846 | if (IS_ERR(trans)) { |
4847 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 4848 | return PTR_ERR(trans); |
9ea24bbe | 4849 | } |
f4a2f4c5 MX |
4850 | |
4851 | i_size_write(inode, newsize); | |
4852 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
4853 | ret = btrfs_update_inode(trans, root, inode); | |
9ea24bbe | 4854 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 4855 | btrfs_end_transaction(trans, root); |
a41ad394 | 4856 | } else { |
8082510e | 4857 | |
a41ad394 JB |
4858 | /* |
4859 | * We're truncating a file that used to have good data down to | |
4860 | * zero. Make sure it gets into the ordered flush list so that | |
4861 | * any new writes get down to disk quickly. | |
4862 | */ | |
4863 | if (newsize == 0) | |
72ac3c0d JB |
4864 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4865 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4866 | |
f3fe820c JB |
4867 | /* |
4868 | * 1 for the orphan item we're going to add | |
4869 | * 1 for the orphan item deletion. | |
4870 | */ | |
4871 | trans = btrfs_start_transaction(root, 2); | |
4872 | if (IS_ERR(trans)) | |
4873 | return PTR_ERR(trans); | |
4874 | ||
4875 | /* | |
4876 | * We need to do this in case we fail at _any_ point during the | |
4877 | * actual truncate. Once we do the truncate_setsize we could | |
4878 | * invalidate pages which forces any outstanding ordered io to | |
4879 | * be instantly completed which will give us extents that need | |
4880 | * to be truncated. If we fail to get an orphan inode down we | |
4881 | * could have left over extents that were never meant to live, | |
4882 | * so we need to garuntee from this point on that everything | |
4883 | * will be consistent. | |
4884 | */ | |
4885 | ret = btrfs_orphan_add(trans, inode); | |
4886 | btrfs_end_transaction(trans, root); | |
4887 | if (ret) | |
4888 | return ret; | |
4889 | ||
a41ad394 JB |
4890 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
4891 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
4892 | |
4893 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
4894 | btrfs_inode_block_unlocked_dio(inode); | |
4895 | inode_dio_wait(inode); | |
4896 | btrfs_inode_resume_unlocked_dio(inode); | |
4897 | ||
a41ad394 | 4898 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
4899 | if (ret && inode->i_nlink) { |
4900 | int err; | |
4901 | ||
4902 | /* | |
4903 | * failed to truncate, disk_i_size is only adjusted down | |
4904 | * as we remove extents, so it should represent the true | |
4905 | * size of the inode, so reset the in memory size and | |
4906 | * delete our orphan entry. | |
4907 | */ | |
4908 | trans = btrfs_join_transaction(root); | |
4909 | if (IS_ERR(trans)) { | |
4910 | btrfs_orphan_del(NULL, inode); | |
4911 | return ret; | |
4912 | } | |
4913 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
4914 | err = btrfs_orphan_del(trans, inode); | |
4915 | if (err) | |
4916 | btrfs_abort_transaction(trans, root, err); | |
4917 | btrfs_end_transaction(trans, root); | |
4918 | } | |
8082510e YZ |
4919 | } |
4920 | ||
a41ad394 | 4921 | return ret; |
8082510e YZ |
4922 | } |
4923 | ||
9036c102 YZ |
4924 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4925 | { | |
4926 | struct inode *inode = dentry->d_inode; | |
b83cc969 | 4927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4928 | int err; |
39279cc3 | 4929 | |
b83cc969 LZ |
4930 | if (btrfs_root_readonly(root)) |
4931 | return -EROFS; | |
4932 | ||
9036c102 YZ |
4933 | err = inode_change_ok(inode, attr); |
4934 | if (err) | |
4935 | return err; | |
2bf5a725 | 4936 | |
5a3f23d5 | 4937 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4938 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4939 | if (err) |
4940 | return err; | |
39279cc3 | 4941 | } |
9036c102 | 4942 | |
1025774c CH |
4943 | if (attr->ia_valid) { |
4944 | setattr_copy(inode, attr); | |
0c4d2d95 | 4945 | inode_inc_iversion(inode); |
22c44fe6 | 4946 | err = btrfs_dirty_inode(inode); |
1025774c | 4947 | |
22c44fe6 | 4948 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 4949 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 4950 | } |
33268eaf | 4951 | |
39279cc3 CM |
4952 | return err; |
4953 | } | |
61295eb8 | 4954 | |
131e404a FDBM |
4955 | /* |
4956 | * While truncating the inode pages during eviction, we get the VFS calling | |
4957 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4958 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4959 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4960 | * extent_state structures over and over, wasting lots of time. | |
4961 | * | |
4962 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4963 | * those expensive operations on a per page basis and do only the ordered io | |
4964 | * finishing, while we release here the extent_map and extent_state structures, | |
4965 | * without the excessive merging and splitting. | |
4966 | */ | |
4967 | static void evict_inode_truncate_pages(struct inode *inode) | |
4968 | { | |
4969 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4970 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4971 | struct rb_node *node; | |
4972 | ||
4973 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 4974 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
4975 | |
4976 | write_lock(&map_tree->lock); | |
4977 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
4978 | struct extent_map *em; | |
4979 | ||
4980 | node = rb_first(&map_tree->map); | |
4981 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
4982 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
4983 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
4984 | remove_extent_mapping(map_tree, em); |
4985 | free_extent_map(em); | |
7064dd5c FM |
4986 | if (need_resched()) { |
4987 | write_unlock(&map_tree->lock); | |
4988 | cond_resched(); | |
4989 | write_lock(&map_tree->lock); | |
4990 | } | |
131e404a FDBM |
4991 | } |
4992 | write_unlock(&map_tree->lock); | |
4993 | ||
4994 | spin_lock(&io_tree->lock); | |
4995 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
4996 | struct extent_state *state; | |
4997 | struct extent_state *cached_state = NULL; | |
4998 | ||
4999 | node = rb_first(&io_tree->state); | |
5000 | state = rb_entry(node, struct extent_state, rb_node); | |
5001 | atomic_inc(&state->refs); | |
5002 | spin_unlock(&io_tree->lock); | |
5003 | ||
5004 | lock_extent_bits(io_tree, state->start, state->end, | |
5005 | 0, &cached_state); | |
5006 | clear_extent_bit(io_tree, state->start, state->end, | |
5007 | EXTENT_LOCKED | EXTENT_DIRTY | | |
5008 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5009 | EXTENT_DEFRAG, 1, 1, | |
5010 | &cached_state, GFP_NOFS); | |
5011 | free_extent_state(state); | |
5012 | ||
7064dd5c | 5013 | cond_resched(); |
131e404a FDBM |
5014 | spin_lock(&io_tree->lock); |
5015 | } | |
5016 | spin_unlock(&io_tree->lock); | |
5017 | } | |
5018 | ||
bd555975 | 5019 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5020 | { |
5021 | struct btrfs_trans_handle *trans; | |
5022 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5023 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5024 | int steal_from_global = 0; |
07127184 | 5025 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 CM |
5026 | int ret; |
5027 | ||
1abe9b8a | 5028 | trace_btrfs_inode_evict(inode); |
5029 | ||
131e404a FDBM |
5030 | evict_inode_truncate_pages(inode); |
5031 | ||
69e9c6c6 SB |
5032 | if (inode->i_nlink && |
5033 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5034 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5035 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5036 | goto no_delete; |
5037 | ||
39279cc3 | 5038 | if (is_bad_inode(inode)) { |
7b128766 | 5039 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5040 | goto no_delete; |
5041 | } | |
bd555975 | 5042 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
4a096752 | 5043 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
5f39d397 | 5044 | |
f612496b MX |
5045 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5046 | ||
c71bf099 | 5047 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 5048 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5049 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5050 | goto no_delete; |
5051 | } | |
5052 | ||
76dda93c | 5053 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5054 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5055 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5056 | goto no_delete; |
5057 | } | |
5058 | ||
0e8c36a9 MX |
5059 | ret = btrfs_commit_inode_delayed_inode(inode); |
5060 | if (ret) { | |
5061 | btrfs_orphan_del(NULL, inode); | |
5062 | goto no_delete; | |
5063 | } | |
5064 | ||
66d8f3dd | 5065 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5066 | if (!rsv) { |
5067 | btrfs_orphan_del(NULL, inode); | |
5068 | goto no_delete; | |
5069 | } | |
4a338542 | 5070 | rsv->size = min_size; |
ca7e70f5 | 5071 | rsv->failfast = 1; |
726c35fa | 5072 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5073 | |
dbe674a9 | 5074 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5075 | |
4289a667 | 5076 | /* |
8407aa46 MX |
5077 | * This is a bit simpler than btrfs_truncate since we've already |
5078 | * reserved our space for our orphan item in the unlink, so we just | |
5079 | * need to reserve some slack space in case we add bytes and update | |
5080 | * inode item when doing the truncate. | |
4289a667 | 5081 | */ |
8082510e | 5082 | while (1) { |
08e007d2 MX |
5083 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5084 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5085 | |
5086 | /* | |
5087 | * Try and steal from the global reserve since we will | |
5088 | * likely not use this space anyway, we want to try as | |
5089 | * hard as possible to get this to work. | |
5090 | */ | |
5091 | if (ret) | |
3bce876f JB |
5092 | steal_from_global++; |
5093 | else | |
5094 | steal_from_global = 0; | |
5095 | ret = 0; | |
d68fc57b | 5096 | |
3bce876f JB |
5097 | /* |
5098 | * steal_from_global == 0: we reserved stuff, hooray! | |
5099 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5100 | * steal_from_global == 2: we've committed, still not a lot of | |
5101 | * room but maybe we'll have room in the global reserve this | |
5102 | * time. | |
5103 | * steal_from_global == 3: abandon all hope! | |
5104 | */ | |
5105 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5106 | btrfs_warn(root->fs_info, |
5107 | "Could not get space for a delete, will truncate on mount %d", | |
5108 | ret); | |
4289a667 JB |
5109 | btrfs_orphan_del(NULL, inode); |
5110 | btrfs_free_block_rsv(root, rsv); | |
5111 | goto no_delete; | |
d68fc57b | 5112 | } |
7b128766 | 5113 | |
0e8c36a9 | 5114 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5115 | if (IS_ERR(trans)) { |
5116 | btrfs_orphan_del(NULL, inode); | |
5117 | btrfs_free_block_rsv(root, rsv); | |
5118 | goto no_delete; | |
d68fc57b | 5119 | } |
7b128766 | 5120 | |
3bce876f JB |
5121 | /* |
5122 | * We can't just steal from the global reserve, we need tomake | |
5123 | * sure there is room to do it, if not we need to commit and try | |
5124 | * again. | |
5125 | */ | |
5126 | if (steal_from_global) { | |
5127 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5128 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
5129 | min_size); | |
5130 | else | |
5131 | ret = -ENOSPC; | |
5132 | } | |
5133 | ||
5134 | /* | |
5135 | * Couldn't steal from the global reserve, we have too much | |
5136 | * pending stuff built up, commit the transaction and try it | |
5137 | * again. | |
5138 | */ | |
5139 | if (ret) { | |
5140 | ret = btrfs_commit_transaction(trans, root); | |
5141 | if (ret) { | |
5142 | btrfs_orphan_del(NULL, inode); | |
5143 | btrfs_free_block_rsv(root, rsv); | |
5144 | goto no_delete; | |
5145 | } | |
5146 | continue; | |
5147 | } else { | |
5148 | steal_from_global = 0; | |
5149 | } | |
5150 | ||
4289a667 JB |
5151 | trans->block_rsv = rsv; |
5152 | ||
d68fc57b | 5153 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5154 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5155 | break; |
85e21bac | 5156 | |
8407aa46 | 5157 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5158 | btrfs_end_transaction(trans, root); |
5159 | trans = NULL; | |
b53d3f5d | 5160 | btrfs_btree_balance_dirty(root); |
8082510e | 5161 | } |
5f39d397 | 5162 | |
4289a667 JB |
5163 | btrfs_free_block_rsv(root, rsv); |
5164 | ||
4ef31a45 JB |
5165 | /* |
5166 | * Errors here aren't a big deal, it just means we leave orphan items | |
5167 | * in the tree. They will be cleaned up on the next mount. | |
5168 | */ | |
8082510e | 5169 | if (ret == 0) { |
4289a667 | 5170 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5171 | btrfs_orphan_del(trans, inode); |
5172 | } else { | |
5173 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5174 | } |
54aa1f4d | 5175 | |
4289a667 | 5176 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5177 | if (!(root == root->fs_info->tree_root || |
5178 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5179 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5180 | |
54aa1f4d | 5181 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5182 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5183 | no_delete: |
89042e5a | 5184 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5185 | clear_inode(inode); |
8082510e | 5186 | return; |
39279cc3 CM |
5187 | } |
5188 | ||
5189 | /* | |
5190 | * this returns the key found in the dir entry in the location pointer. | |
5191 | * If no dir entries were found, location->objectid is 0. | |
5192 | */ | |
5193 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5194 | struct btrfs_key *location) | |
5195 | { | |
5196 | const char *name = dentry->d_name.name; | |
5197 | int namelen = dentry->d_name.len; | |
5198 | struct btrfs_dir_item *di; | |
5199 | struct btrfs_path *path; | |
5200 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5201 | int ret = 0; |
39279cc3 CM |
5202 | |
5203 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5204 | if (!path) |
5205 | return -ENOMEM; | |
3954401f | 5206 | |
33345d01 | 5207 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5208 | namelen, 0); |
0d9f7f3e Y |
5209 | if (IS_ERR(di)) |
5210 | ret = PTR_ERR(di); | |
d397712b | 5211 | |
c704005d | 5212 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5213 | goto out_err; |
d397712b | 5214 | |
5f39d397 | 5215 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5216 | out: |
39279cc3 CM |
5217 | btrfs_free_path(path); |
5218 | return ret; | |
3954401f CM |
5219 | out_err: |
5220 | location->objectid = 0; | |
5221 | goto out; | |
39279cc3 CM |
5222 | } |
5223 | ||
5224 | /* | |
5225 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5226 | * needs to be changed to reflect the root directory of the tree root. This | |
5227 | * is kind of like crossing a mount point. | |
5228 | */ | |
5229 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5230 | struct inode *dir, |
5231 | struct dentry *dentry, | |
5232 | struct btrfs_key *location, | |
5233 | struct btrfs_root **sub_root) | |
39279cc3 | 5234 | { |
4df27c4d YZ |
5235 | struct btrfs_path *path; |
5236 | struct btrfs_root *new_root; | |
5237 | struct btrfs_root_ref *ref; | |
5238 | struct extent_buffer *leaf; | |
1d4c08e0 | 5239 | struct btrfs_key key; |
4df27c4d YZ |
5240 | int ret; |
5241 | int err = 0; | |
39279cc3 | 5242 | |
4df27c4d YZ |
5243 | path = btrfs_alloc_path(); |
5244 | if (!path) { | |
5245 | err = -ENOMEM; | |
5246 | goto out; | |
5247 | } | |
39279cc3 | 5248 | |
4df27c4d | 5249 | err = -ENOENT; |
1d4c08e0 DS |
5250 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5251 | key.type = BTRFS_ROOT_REF_KEY; | |
5252 | key.offset = location->objectid; | |
5253 | ||
5254 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5255 | 0, 0); | |
4df27c4d YZ |
5256 | if (ret) { |
5257 | if (ret < 0) | |
5258 | err = ret; | |
5259 | goto out; | |
5260 | } | |
39279cc3 | 5261 | |
4df27c4d YZ |
5262 | leaf = path->nodes[0]; |
5263 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5264 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5265 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5266 | goto out; | |
39279cc3 | 5267 | |
4df27c4d YZ |
5268 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5269 | (unsigned long)(ref + 1), | |
5270 | dentry->d_name.len); | |
5271 | if (ret) | |
5272 | goto out; | |
5273 | ||
b3b4aa74 | 5274 | btrfs_release_path(path); |
4df27c4d YZ |
5275 | |
5276 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5277 | if (IS_ERR(new_root)) { | |
5278 | err = PTR_ERR(new_root); | |
5279 | goto out; | |
5280 | } | |
5281 | ||
4df27c4d YZ |
5282 | *sub_root = new_root; |
5283 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5284 | location->type = BTRFS_INODE_ITEM_KEY; | |
5285 | location->offset = 0; | |
5286 | err = 0; | |
5287 | out: | |
5288 | btrfs_free_path(path); | |
5289 | return err; | |
39279cc3 CM |
5290 | } |
5291 | ||
5d4f98a2 YZ |
5292 | static void inode_tree_add(struct inode *inode) |
5293 | { | |
5294 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5295 | struct btrfs_inode *entry; | |
03e860bd FNP |
5296 | struct rb_node **p; |
5297 | struct rb_node *parent; | |
cef21937 | 5298 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5299 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5300 | |
1d3382cb | 5301 | if (inode_unhashed(inode)) |
76dda93c | 5302 | return; |
e1409cef | 5303 | parent = NULL; |
5d4f98a2 | 5304 | spin_lock(&root->inode_lock); |
e1409cef | 5305 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5306 | while (*p) { |
5307 | parent = *p; | |
5308 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5309 | ||
33345d01 | 5310 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5311 | p = &parent->rb_left; |
33345d01 | 5312 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5313 | p = &parent->rb_right; |
5d4f98a2 YZ |
5314 | else { |
5315 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5316 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5317 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5318 | RB_CLEAR_NODE(parent); |
5319 | spin_unlock(&root->inode_lock); | |
cef21937 | 5320 | return; |
5d4f98a2 YZ |
5321 | } |
5322 | } | |
cef21937 FDBM |
5323 | rb_link_node(new, parent, p); |
5324 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5325 | spin_unlock(&root->inode_lock); |
5326 | } | |
5327 | ||
5328 | static void inode_tree_del(struct inode *inode) | |
5329 | { | |
5330 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5331 | int empty = 0; |
5d4f98a2 | 5332 | |
03e860bd | 5333 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5334 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5335 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5336 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5337 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5338 | } |
03e860bd | 5339 | spin_unlock(&root->inode_lock); |
76dda93c | 5340 | |
69e9c6c6 | 5341 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5342 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5343 | spin_lock(&root->inode_lock); | |
5344 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5345 | spin_unlock(&root->inode_lock); | |
5346 | if (empty) | |
5347 | btrfs_add_dead_root(root); | |
5348 | } | |
5349 | } | |
5350 | ||
143bede5 | 5351 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5352 | { |
5353 | struct rb_node *node; | |
5354 | struct rb_node *prev; | |
5355 | struct btrfs_inode *entry; | |
5356 | struct inode *inode; | |
5357 | u64 objectid = 0; | |
5358 | ||
7813b3db LB |
5359 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5360 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5361 | |
5362 | spin_lock(&root->inode_lock); | |
5363 | again: | |
5364 | node = root->inode_tree.rb_node; | |
5365 | prev = NULL; | |
5366 | while (node) { | |
5367 | prev = node; | |
5368 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5369 | ||
33345d01 | 5370 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5371 | node = node->rb_left; |
33345d01 | 5372 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5373 | node = node->rb_right; |
5374 | else | |
5375 | break; | |
5376 | } | |
5377 | if (!node) { | |
5378 | while (prev) { | |
5379 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5380 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5381 | node = prev; |
5382 | break; | |
5383 | } | |
5384 | prev = rb_next(prev); | |
5385 | } | |
5386 | } | |
5387 | while (node) { | |
5388 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5389 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5390 | inode = igrab(&entry->vfs_inode); |
5391 | if (inode) { | |
5392 | spin_unlock(&root->inode_lock); | |
5393 | if (atomic_read(&inode->i_count) > 1) | |
5394 | d_prune_aliases(inode); | |
5395 | /* | |
45321ac5 | 5396 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5397 | * the inode cache when its usage count |
5398 | * hits zero. | |
5399 | */ | |
5400 | iput(inode); | |
5401 | cond_resched(); | |
5402 | spin_lock(&root->inode_lock); | |
5403 | goto again; | |
5404 | } | |
5405 | ||
5406 | if (cond_resched_lock(&root->inode_lock)) | |
5407 | goto again; | |
5408 | ||
5409 | node = rb_next(node); | |
5410 | } | |
5411 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5412 | } |
5413 | ||
e02119d5 CM |
5414 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5415 | { | |
5416 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5417 | inode->i_ino = args->location->objectid; |
5418 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5419 | sizeof(*args->location)); | |
e02119d5 | 5420 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5421 | return 0; |
5422 | } | |
5423 | ||
5424 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5425 | { | |
5426 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5427 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5428 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5429 | } |
5430 | ||
5d4f98a2 | 5431 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5432 | struct btrfs_key *location, |
5d4f98a2 | 5433 | struct btrfs_root *root) |
39279cc3 CM |
5434 | { |
5435 | struct inode *inode; | |
5436 | struct btrfs_iget_args args; | |
90d3e592 | 5437 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5438 | |
90d3e592 | 5439 | args.location = location; |
39279cc3 CM |
5440 | args.root = root; |
5441 | ||
778ba82b | 5442 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5443 | btrfs_init_locked_inode, |
5444 | (void *)&args); | |
5445 | return inode; | |
5446 | } | |
5447 | ||
1a54ef8c BR |
5448 | /* Get an inode object given its location and corresponding root. |
5449 | * Returns in *is_new if the inode was read from disk | |
5450 | */ | |
5451 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5452 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5453 | { |
5454 | struct inode *inode; | |
5455 | ||
90d3e592 | 5456 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5457 | if (!inode) |
5d4f98a2 | 5458 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5459 | |
5460 | if (inode->i_state & I_NEW) { | |
1a54ef8c | 5461 | btrfs_read_locked_inode(inode); |
1748f843 MF |
5462 | if (!is_bad_inode(inode)) { |
5463 | inode_tree_add(inode); | |
5464 | unlock_new_inode(inode); | |
5465 | if (new) | |
5466 | *new = 1; | |
5467 | } else { | |
e0b6d65b ST |
5468 | unlock_new_inode(inode); |
5469 | iput(inode); | |
5470 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5471 | } |
5472 | } | |
5473 | ||
1a54ef8c BR |
5474 | return inode; |
5475 | } | |
5476 | ||
4df27c4d YZ |
5477 | static struct inode *new_simple_dir(struct super_block *s, |
5478 | struct btrfs_key *key, | |
5479 | struct btrfs_root *root) | |
5480 | { | |
5481 | struct inode *inode = new_inode(s); | |
5482 | ||
5483 | if (!inode) | |
5484 | return ERR_PTR(-ENOMEM); | |
5485 | ||
4df27c4d YZ |
5486 | BTRFS_I(inode)->root = root; |
5487 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5488 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5489 | |
5490 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5491 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5492 | inode->i_fop = &simple_dir_operations; |
5493 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
9cc97d64 | 5494 | inode->i_mtime = CURRENT_TIME; |
5495 | inode->i_atime = inode->i_mtime; | |
5496 | inode->i_ctime = inode->i_mtime; | |
5497 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5498 | |
5499 | return inode; | |
5500 | } | |
5501 | ||
3de4586c | 5502 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5503 | { |
d397712b | 5504 | struct inode *inode; |
4df27c4d | 5505 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5506 | struct btrfs_root *sub_root = root; |
5507 | struct btrfs_key location; | |
76dda93c | 5508 | int index; |
b4aff1f8 | 5509 | int ret = 0; |
39279cc3 CM |
5510 | |
5511 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5512 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5513 | |
39e3c955 | 5514 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5515 | if (ret < 0) |
5516 | return ERR_PTR(ret); | |
5f39d397 | 5517 | |
4df27c4d | 5518 | if (location.objectid == 0) |
5662344b | 5519 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5520 | |
5521 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5522 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5523 | return inode; |
5524 | } | |
5525 | ||
5526 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5527 | ||
76dda93c | 5528 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5529 | ret = fixup_tree_root_location(root, dir, dentry, |
5530 | &location, &sub_root); | |
5531 | if (ret < 0) { | |
5532 | if (ret != -ENOENT) | |
5533 | inode = ERR_PTR(ret); | |
5534 | else | |
5535 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5536 | } else { | |
73f73415 | 5537 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5538 | } |
76dda93c YZ |
5539 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5540 | ||
34d19bad | 5541 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5542 | down_read(&root->fs_info->cleanup_work_sem); |
5543 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5544 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5545 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5546 | if (ret) { |
5547 | iput(inode); | |
66b4ffd1 | 5548 | inode = ERR_PTR(ret); |
01cd3367 | 5549 | } |
c71bf099 YZ |
5550 | } |
5551 | ||
3de4586c CM |
5552 | return inode; |
5553 | } | |
5554 | ||
fe15ce44 | 5555 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5556 | { |
5557 | struct btrfs_root *root; | |
848cce0d | 5558 | struct inode *inode = dentry->d_inode; |
76dda93c | 5559 | |
848cce0d LZ |
5560 | if (!inode && !IS_ROOT(dentry)) |
5561 | inode = dentry->d_parent->d_inode; | |
76dda93c | 5562 | |
848cce0d LZ |
5563 | if (inode) { |
5564 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5565 | if (btrfs_root_refs(&root->root_item) == 0) |
5566 | return 1; | |
848cce0d LZ |
5567 | |
5568 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5569 | return 1; | |
efefb143 | 5570 | } |
76dda93c YZ |
5571 | return 0; |
5572 | } | |
5573 | ||
b4aff1f8 JB |
5574 | static void btrfs_dentry_release(struct dentry *dentry) |
5575 | { | |
944a4515 | 5576 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5577 | } |
5578 | ||
3de4586c | 5579 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5580 | unsigned int flags) |
3de4586c | 5581 | { |
5662344b | 5582 | struct inode *inode; |
a66e7cc6 | 5583 | |
5662344b TI |
5584 | inode = btrfs_lookup_dentry(dir, dentry); |
5585 | if (IS_ERR(inode)) { | |
5586 | if (PTR_ERR(inode) == -ENOENT) | |
5587 | inode = NULL; | |
5588 | else | |
5589 | return ERR_CAST(inode); | |
5590 | } | |
5591 | ||
41d28bca | 5592 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5593 | } |
5594 | ||
16cdcec7 | 5595 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5596 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5597 | }; | |
5598 | ||
9cdda8d3 | 5599 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5600 | { |
9cdda8d3 | 5601 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5602 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5603 | struct btrfs_item *item; | |
5604 | struct btrfs_dir_item *di; | |
5605 | struct btrfs_key key; | |
5f39d397 | 5606 | struct btrfs_key found_key; |
39279cc3 | 5607 | struct btrfs_path *path; |
16cdcec7 MX |
5608 | struct list_head ins_list; |
5609 | struct list_head del_list; | |
39279cc3 | 5610 | int ret; |
5f39d397 | 5611 | struct extent_buffer *leaf; |
39279cc3 | 5612 | int slot; |
39279cc3 CM |
5613 | unsigned char d_type; |
5614 | int over = 0; | |
5615 | u32 di_cur; | |
5616 | u32 di_total; | |
5617 | u32 di_len; | |
5618 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5619 | char tmp_name[32]; |
5620 | char *name_ptr; | |
5621 | int name_len; | |
9cdda8d3 | 5622 | int is_curr = 0; /* ctx->pos points to the current index? */ |
39279cc3 CM |
5623 | |
5624 | /* FIXME, use a real flag for deciding about the key type */ | |
5625 | if (root->fs_info->tree_root == root) | |
5626 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5627 | |
9cdda8d3 AV |
5628 | if (!dir_emit_dots(file, ctx)) |
5629 | return 0; | |
5630 | ||
49593bfa | 5631 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5632 | if (!path) |
5633 | return -ENOMEM; | |
ff5714cc | 5634 | |
026fd317 | 5635 | path->reada = 1; |
49593bfa | 5636 | |
16cdcec7 MX |
5637 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5638 | INIT_LIST_HEAD(&ins_list); | |
5639 | INIT_LIST_HEAD(&del_list); | |
5640 | btrfs_get_delayed_items(inode, &ins_list, &del_list); | |
5641 | } | |
5642 | ||
962a298f | 5643 | key.type = key_type; |
9cdda8d3 | 5644 | key.offset = ctx->pos; |
33345d01 | 5645 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5646 | |
39279cc3 CM |
5647 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5648 | if (ret < 0) | |
5649 | goto err; | |
49593bfa DW |
5650 | |
5651 | while (1) { | |
5f39d397 | 5652 | leaf = path->nodes[0]; |
39279cc3 | 5653 | slot = path->slots[0]; |
b9e03af0 LZ |
5654 | if (slot >= btrfs_header_nritems(leaf)) { |
5655 | ret = btrfs_next_leaf(root, path); | |
5656 | if (ret < 0) | |
5657 | goto err; | |
5658 | else if (ret > 0) | |
5659 | break; | |
5660 | continue; | |
39279cc3 | 5661 | } |
3de4586c | 5662 | |
dd3cc16b | 5663 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5664 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5665 | ||
5666 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5667 | break; |
962a298f | 5668 | if (found_key.type != key_type) |
39279cc3 | 5669 | break; |
9cdda8d3 | 5670 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5671 | goto next; |
16cdcec7 MX |
5672 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5673 | btrfs_should_delete_dir_index(&del_list, | |
5674 | found_key.offset)) | |
5675 | goto next; | |
5f39d397 | 5676 | |
9cdda8d3 | 5677 | ctx->pos = found_key.offset; |
16cdcec7 | 5678 | is_curr = 1; |
49593bfa | 5679 | |
39279cc3 CM |
5680 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5681 | di_cur = 0; | |
5f39d397 | 5682 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5683 | |
5684 | while (di_cur < di_total) { | |
5f39d397 CM |
5685 | struct btrfs_key location; |
5686 | ||
22a94d44 JB |
5687 | if (verify_dir_item(root, leaf, di)) |
5688 | break; | |
5689 | ||
5f39d397 | 5690 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5691 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5692 | name_ptr = tmp_name; |
5693 | } else { | |
5694 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
5695 | if (!name_ptr) { |
5696 | ret = -ENOMEM; | |
5697 | goto err; | |
5698 | } | |
5f39d397 CM |
5699 | } |
5700 | read_extent_buffer(leaf, name_ptr, | |
5701 | (unsigned long)(di + 1), name_len); | |
5702 | ||
5703 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5704 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5705 | |
fede766f | 5706 | |
3de4586c | 5707 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5708 | * skip it. |
5709 | * | |
5710 | * In contrast to old kernels, we insert the snapshot's | |
5711 | * dir item and dir index after it has been created, so | |
5712 | * we won't find a reference to our own snapshot. We | |
5713 | * still keep the following code for backward | |
5714 | * compatibility. | |
3de4586c CM |
5715 | */ |
5716 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5717 | location.objectid == root->root_key.objectid) { | |
5718 | over = 0; | |
5719 | goto skip; | |
5720 | } | |
9cdda8d3 AV |
5721 | over = !dir_emit(ctx, name_ptr, name_len, |
5722 | location.objectid, d_type); | |
5f39d397 | 5723 | |
3de4586c | 5724 | skip: |
5f39d397 CM |
5725 | if (name_ptr != tmp_name) |
5726 | kfree(name_ptr); | |
5727 | ||
39279cc3 CM |
5728 | if (over) |
5729 | goto nopos; | |
5103e947 | 5730 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5731 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5732 | di_cur += di_len; |
5733 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5734 | } | |
b9e03af0 LZ |
5735 | next: |
5736 | path->slots[0]++; | |
39279cc3 | 5737 | } |
49593bfa | 5738 | |
16cdcec7 MX |
5739 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5740 | if (is_curr) | |
9cdda8d3 AV |
5741 | ctx->pos++; |
5742 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
16cdcec7 MX |
5743 | if (ret) |
5744 | goto nopos; | |
5745 | } | |
5746 | ||
49593bfa | 5747 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5748 | ctx->pos++; |
5749 | ||
5750 | /* | |
5751 | * Stop new entries from being returned after we return the last | |
5752 | * entry. | |
5753 | * | |
5754 | * New directory entries are assigned a strictly increasing | |
5755 | * offset. This means that new entries created during readdir | |
5756 | * are *guaranteed* to be seen in the future by that readdir. | |
5757 | * This has broken buggy programs which operate on names as | |
5758 | * they're returned by readdir. Until we re-use freed offsets | |
5759 | * we have this hack to stop new entries from being returned | |
5760 | * under the assumption that they'll never reach this huge | |
5761 | * offset. | |
5762 | * | |
5763 | * This is being careful not to overflow 32bit loff_t unless the | |
5764 | * last entry requires it because doing so has broken 32bit apps | |
5765 | * in the past. | |
5766 | */ | |
5767 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5768 | if (ctx->pos >= INT_MAX) | |
5769 | ctx->pos = LLONG_MAX; | |
5770 | else | |
5771 | ctx->pos = INT_MAX; | |
5772 | } | |
39279cc3 CM |
5773 | nopos: |
5774 | ret = 0; | |
5775 | err: | |
16cdcec7 MX |
5776 | if (key_type == BTRFS_DIR_INDEX_KEY) |
5777 | btrfs_put_delayed_items(&ins_list, &del_list); | |
39279cc3 | 5778 | btrfs_free_path(path); |
39279cc3 CM |
5779 | return ret; |
5780 | } | |
5781 | ||
a9185b41 | 5782 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5783 | { |
5784 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5785 | struct btrfs_trans_handle *trans; | |
5786 | int ret = 0; | |
0af3d00b | 5787 | bool nolock = false; |
39279cc3 | 5788 | |
72ac3c0d | 5789 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5790 | return 0; |
5791 | ||
83eea1f1 | 5792 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5793 | nolock = true; |
0af3d00b | 5794 | |
a9185b41 | 5795 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5796 | if (nolock) |
7a7eaa40 | 5797 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5798 | else |
7a7eaa40 | 5799 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5800 | if (IS_ERR(trans)) |
5801 | return PTR_ERR(trans); | |
a698d075 | 5802 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5803 | } |
5804 | return ret; | |
5805 | } | |
5806 | ||
5807 | /* | |
54aa1f4d | 5808 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5809 | * inode changes. But, it is most likely to find the inode in cache. |
5810 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5811 | * to keep or drop this code. | |
5812 | */ | |
48a3b636 | 5813 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5814 | { |
5815 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5816 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5817 | int ret; |
5818 | ||
72ac3c0d | 5819 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5820 | return 0; |
39279cc3 | 5821 | |
7a7eaa40 | 5822 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5823 | if (IS_ERR(trans)) |
5824 | return PTR_ERR(trans); | |
8929ecfa YZ |
5825 | |
5826 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5827 | if (ret && ret == -ENOSPC) { |
5828 | /* whoops, lets try again with the full transaction */ | |
5829 | btrfs_end_transaction(trans, root); | |
5830 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5831 | if (IS_ERR(trans)) |
5832 | return PTR_ERR(trans); | |
8929ecfa | 5833 | |
94b60442 | 5834 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5835 | } |
39279cc3 | 5836 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
5837 | if (BTRFS_I(inode)->delayed_node) |
5838 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
5839 | |
5840 | return ret; | |
5841 | } | |
5842 | ||
5843 | /* | |
5844 | * This is a copy of file_update_time. We need this so we can return error on | |
5845 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5846 | */ | |
e41f941a JB |
5847 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
5848 | int flags) | |
22c44fe6 | 5849 | { |
2bc55652 AB |
5850 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5851 | ||
5852 | if (btrfs_root_readonly(root)) | |
5853 | return -EROFS; | |
5854 | ||
e41f941a | 5855 | if (flags & S_VERSION) |
22c44fe6 | 5856 | inode_inc_iversion(inode); |
e41f941a JB |
5857 | if (flags & S_CTIME) |
5858 | inode->i_ctime = *now; | |
5859 | if (flags & S_MTIME) | |
5860 | inode->i_mtime = *now; | |
5861 | if (flags & S_ATIME) | |
5862 | inode->i_atime = *now; | |
5863 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
5864 | } |
5865 | ||
d352ac68 CM |
5866 | /* |
5867 | * find the highest existing sequence number in a directory | |
5868 | * and then set the in-memory index_cnt variable to reflect | |
5869 | * free sequence numbers | |
5870 | */ | |
aec7477b JB |
5871 | static int btrfs_set_inode_index_count(struct inode *inode) |
5872 | { | |
5873 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5874 | struct btrfs_key key, found_key; | |
5875 | struct btrfs_path *path; | |
5876 | struct extent_buffer *leaf; | |
5877 | int ret; | |
5878 | ||
33345d01 | 5879 | key.objectid = btrfs_ino(inode); |
962a298f | 5880 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5881 | key.offset = (u64)-1; |
5882 | ||
5883 | path = btrfs_alloc_path(); | |
5884 | if (!path) | |
5885 | return -ENOMEM; | |
5886 | ||
5887 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5888 | if (ret < 0) | |
5889 | goto out; | |
5890 | /* FIXME: we should be able to handle this */ | |
5891 | if (ret == 0) | |
5892 | goto out; | |
5893 | ret = 0; | |
5894 | ||
5895 | /* | |
5896 | * MAGIC NUMBER EXPLANATION: | |
5897 | * since we search a directory based on f_pos we have to start at 2 | |
5898 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5899 | * else has to start at 2 | |
5900 | */ | |
5901 | if (path->slots[0] == 0) { | |
5902 | BTRFS_I(inode)->index_cnt = 2; | |
5903 | goto out; | |
5904 | } | |
5905 | ||
5906 | path->slots[0]--; | |
5907 | ||
5908 | leaf = path->nodes[0]; | |
5909 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5910 | ||
33345d01 | 5911 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5912 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
5913 | BTRFS_I(inode)->index_cnt = 2; |
5914 | goto out; | |
5915 | } | |
5916 | ||
5917 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
5918 | out: | |
5919 | btrfs_free_path(path); | |
5920 | return ret; | |
5921 | } | |
5922 | ||
d352ac68 CM |
5923 | /* |
5924 | * helper to find a free sequence number in a given directory. This current | |
5925 | * code is very simple, later versions will do smarter things in the btree | |
5926 | */ | |
3de4586c | 5927 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
5928 | { |
5929 | int ret = 0; | |
5930 | ||
5931 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
5932 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5933 | if (ret) { | |
5934 | ret = btrfs_set_inode_index_count(dir); | |
5935 | if (ret) | |
5936 | return ret; | |
5937 | } | |
aec7477b JB |
5938 | } |
5939 | ||
00e4e6b3 | 5940 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
5941 | BTRFS_I(dir)->index_cnt++; |
5942 | ||
5943 | return ret; | |
5944 | } | |
5945 | ||
b0d5d10f CM |
5946 | static int btrfs_insert_inode_locked(struct inode *inode) |
5947 | { | |
5948 | struct btrfs_iget_args args; | |
5949 | args.location = &BTRFS_I(inode)->location; | |
5950 | args.root = BTRFS_I(inode)->root; | |
5951 | ||
5952 | return insert_inode_locked4(inode, | |
5953 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5954 | btrfs_find_actor, &args); | |
5955 | } | |
5956 | ||
39279cc3 CM |
5957 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
5958 | struct btrfs_root *root, | |
aec7477b | 5959 | struct inode *dir, |
9c58309d | 5960 | const char *name, int name_len, |
175a4eb7 AV |
5961 | u64 ref_objectid, u64 objectid, |
5962 | umode_t mode, u64 *index) | |
39279cc3 CM |
5963 | { |
5964 | struct inode *inode; | |
5f39d397 | 5965 | struct btrfs_inode_item *inode_item; |
39279cc3 | 5966 | struct btrfs_key *location; |
5f39d397 | 5967 | struct btrfs_path *path; |
9c58309d CM |
5968 | struct btrfs_inode_ref *ref; |
5969 | struct btrfs_key key[2]; | |
5970 | u32 sizes[2]; | |
ef3b9af5 | 5971 | int nitems = name ? 2 : 1; |
9c58309d | 5972 | unsigned long ptr; |
39279cc3 | 5973 | int ret; |
39279cc3 | 5974 | |
5f39d397 | 5975 | path = btrfs_alloc_path(); |
d8926bb3 MF |
5976 | if (!path) |
5977 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 5978 | |
39279cc3 | 5979 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
5980 | if (!inode) { |
5981 | btrfs_free_path(path); | |
39279cc3 | 5982 | return ERR_PTR(-ENOMEM); |
8fb27640 | 5983 | } |
39279cc3 | 5984 | |
5762b5c9 FM |
5985 | /* |
5986 | * O_TMPFILE, set link count to 0, so that after this point, | |
5987 | * we fill in an inode item with the correct link count. | |
5988 | */ | |
5989 | if (!name) | |
5990 | set_nlink(inode, 0); | |
5991 | ||
581bb050 LZ |
5992 | /* |
5993 | * we have to initialize this early, so we can reclaim the inode | |
5994 | * number if we fail afterwards in this function. | |
5995 | */ | |
5996 | inode->i_ino = objectid; | |
5997 | ||
ef3b9af5 | 5998 | if (dir && name) { |
1abe9b8a | 5999 | trace_btrfs_inode_request(dir); |
6000 | ||
3de4586c | 6001 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6002 | if (ret) { |
8fb27640 | 6003 | btrfs_free_path(path); |
09771430 | 6004 | iput(inode); |
aec7477b | 6005 | return ERR_PTR(ret); |
09771430 | 6006 | } |
ef3b9af5 FM |
6007 | } else if (dir) { |
6008 | *index = 0; | |
aec7477b JB |
6009 | } |
6010 | /* | |
6011 | * index_cnt is ignored for everything but a dir, | |
6012 | * btrfs_get_inode_index_count has an explanation for the magic | |
6013 | * number | |
6014 | */ | |
6015 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6016 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6017 | BTRFS_I(inode)->root = root; |
e02119d5 | 6018 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6019 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6020 | |
5dc562c5 JB |
6021 | /* |
6022 | * We could have gotten an inode number from somebody who was fsynced | |
6023 | * and then removed in this same transaction, so let's just set full | |
6024 | * sync since it will be a full sync anyway and this will blow away the | |
6025 | * old info in the log. | |
6026 | */ | |
6027 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6028 | ||
9c58309d | 6029 | key[0].objectid = objectid; |
962a298f | 6030 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6031 | key[0].offset = 0; |
6032 | ||
9c58309d | 6033 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6034 | |
6035 | if (name) { | |
6036 | /* | |
6037 | * Start new inodes with an inode_ref. This is slightly more | |
6038 | * efficient for small numbers of hard links since they will | |
6039 | * be packed into one item. Extended refs will kick in if we | |
6040 | * add more hard links than can fit in the ref item. | |
6041 | */ | |
6042 | key[1].objectid = objectid; | |
962a298f | 6043 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6044 | key[1].offset = ref_objectid; |
6045 | ||
6046 | sizes[1] = name_len + sizeof(*ref); | |
6047 | } | |
9c58309d | 6048 | |
b0d5d10f CM |
6049 | location = &BTRFS_I(inode)->location; |
6050 | location->objectid = objectid; | |
6051 | location->offset = 0; | |
962a298f | 6052 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6053 | |
6054 | ret = btrfs_insert_inode_locked(inode); | |
6055 | if (ret < 0) | |
6056 | goto fail; | |
6057 | ||
b9473439 | 6058 | path->leave_spinning = 1; |
ef3b9af5 | 6059 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6060 | if (ret != 0) |
b0d5d10f | 6061 | goto fail_unlock; |
5f39d397 | 6062 | |
ecc11fab | 6063 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6064 | inode_set_bytes(inode, 0); |
9cc97d64 | 6065 | |
6066 | inode->i_mtime = CURRENT_TIME; | |
6067 | inode->i_atime = inode->i_mtime; | |
6068 | inode->i_ctime = inode->i_mtime; | |
6069 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6070 | ||
5f39d397 CM |
6071 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6072 | struct btrfs_inode_item); | |
293f7e07 LZ |
6073 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6074 | sizeof(*inode_item)); | |
e02119d5 | 6075 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6076 | |
ef3b9af5 FM |
6077 | if (name) { |
6078 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6079 | struct btrfs_inode_ref); | |
6080 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6081 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6082 | ptr = (unsigned long)(ref + 1); | |
6083 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6084 | } | |
9c58309d | 6085 | |
5f39d397 CM |
6086 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6087 | btrfs_free_path(path); | |
6088 | ||
6cbff00f CH |
6089 | btrfs_inherit_iflags(inode, dir); |
6090 | ||
569254b0 | 6091 | if (S_ISREG(mode)) { |
94272164 CM |
6092 | if (btrfs_test_opt(root, NODATASUM)) |
6093 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
213490b3 | 6094 | if (btrfs_test_opt(root, NODATACOW)) |
f2bdf9a8 JB |
6095 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6096 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6097 | } |
6098 | ||
5d4f98a2 | 6099 | inode_tree_add(inode); |
1abe9b8a | 6100 | |
6101 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6102 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6103 | |
8ea05e3a AB |
6104 | btrfs_update_root_times(trans, root); |
6105 | ||
63541927 FDBM |
6106 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6107 | if (ret) | |
6108 | btrfs_err(root->fs_info, | |
6109 | "error inheriting props for ino %llu (root %llu): %d", | |
6110 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6111 | ||
39279cc3 | 6112 | return inode; |
b0d5d10f CM |
6113 | |
6114 | fail_unlock: | |
6115 | unlock_new_inode(inode); | |
5f39d397 | 6116 | fail: |
ef3b9af5 | 6117 | if (dir && name) |
aec7477b | 6118 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6119 | btrfs_free_path(path); |
09771430 | 6120 | iput(inode); |
5f39d397 | 6121 | return ERR_PTR(ret); |
39279cc3 CM |
6122 | } |
6123 | ||
6124 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6125 | { | |
6126 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6127 | } | |
6128 | ||
d352ac68 CM |
6129 | /* |
6130 | * utility function to add 'inode' into 'parent_inode' with | |
6131 | * a give name and a given sequence number. | |
6132 | * if 'add_backref' is true, also insert a backref from the | |
6133 | * inode to the parent directory. | |
6134 | */ | |
e02119d5 CM |
6135 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6136 | struct inode *parent_inode, struct inode *inode, | |
6137 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6138 | { |
4df27c4d | 6139 | int ret = 0; |
39279cc3 | 6140 | struct btrfs_key key; |
e02119d5 | 6141 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6142 | u64 ino = btrfs_ino(inode); |
6143 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6144 | |
33345d01 | 6145 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6146 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6147 | } else { | |
33345d01 | 6148 | key.objectid = ino; |
962a298f | 6149 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6150 | key.offset = 0; |
6151 | } | |
6152 | ||
33345d01 | 6153 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6154 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6155 | key.objectid, root->root_key.objectid, | |
33345d01 | 6156 | parent_ino, index, name, name_len); |
4df27c4d | 6157 | } else if (add_backref) { |
33345d01 LZ |
6158 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6159 | parent_ino, index); | |
4df27c4d | 6160 | } |
39279cc3 | 6161 | |
79787eaa JM |
6162 | /* Nothing to clean up yet */ |
6163 | if (ret) | |
6164 | return ret; | |
4df27c4d | 6165 | |
79787eaa JM |
6166 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6167 | parent_inode, &key, | |
6168 | btrfs_inode_type(inode), index); | |
9c52057c | 6169 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6170 | goto fail_dir_item; |
6171 | else if (ret) { | |
6172 | btrfs_abort_transaction(trans, root, ret); | |
6173 | return ret; | |
39279cc3 | 6174 | } |
79787eaa JM |
6175 | |
6176 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6177 | name_len * 2); | |
0c4d2d95 | 6178 | inode_inc_iversion(parent_inode); |
79787eaa JM |
6179 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
6180 | ret = btrfs_update_inode(trans, root, parent_inode); | |
6181 | if (ret) | |
6182 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 6183 | return ret; |
fe66a05a CM |
6184 | |
6185 | fail_dir_item: | |
6186 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6187 | u64 local_index; | |
6188 | int err; | |
6189 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6190 | key.objectid, root->root_key.objectid, | |
6191 | parent_ino, &local_index, name, name_len); | |
6192 | ||
6193 | } else if (add_backref) { | |
6194 | u64 local_index; | |
6195 | int err; | |
6196 | ||
6197 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6198 | ino, parent_ino, &local_index); | |
6199 | } | |
6200 | return ret; | |
39279cc3 CM |
6201 | } |
6202 | ||
6203 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6204 | struct inode *dir, struct dentry *dentry, |
6205 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6206 | { |
a1b075d2 JB |
6207 | int err = btrfs_add_link(trans, dir, inode, |
6208 | dentry->d_name.name, dentry->d_name.len, | |
6209 | backref, index); | |
39279cc3 CM |
6210 | if (err > 0) |
6211 | err = -EEXIST; | |
6212 | return err; | |
6213 | } | |
6214 | ||
618e21d5 | 6215 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6216 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6217 | { |
6218 | struct btrfs_trans_handle *trans; | |
6219 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6220 | struct inode *inode = NULL; |
618e21d5 JB |
6221 | int err; |
6222 | int drop_inode = 0; | |
6223 | u64 objectid; | |
00e4e6b3 | 6224 | u64 index = 0; |
618e21d5 JB |
6225 | |
6226 | if (!new_valid_dev(rdev)) | |
6227 | return -EINVAL; | |
6228 | ||
9ed74f2d JB |
6229 | /* |
6230 | * 2 for inode item and ref | |
6231 | * 2 for dir items | |
6232 | * 1 for xattr if selinux is on | |
6233 | */ | |
a22285a6 YZ |
6234 | trans = btrfs_start_transaction(root, 5); |
6235 | if (IS_ERR(trans)) | |
6236 | return PTR_ERR(trans); | |
1832a6d5 | 6237 | |
581bb050 LZ |
6238 | err = btrfs_find_free_ino(root, &objectid); |
6239 | if (err) | |
6240 | goto out_unlock; | |
6241 | ||
aec7477b | 6242 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6243 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6244 | mode, &index); |
7cf96da3 TI |
6245 | if (IS_ERR(inode)) { |
6246 | err = PTR_ERR(inode); | |
618e21d5 | 6247 | goto out_unlock; |
7cf96da3 | 6248 | } |
618e21d5 | 6249 | |
ad19db71 CS |
6250 | /* |
6251 | * If the active LSM wants to access the inode during | |
6252 | * d_instantiate it needs these. Smack checks to see | |
6253 | * if the filesystem supports xattrs by looking at the | |
6254 | * ops vector. | |
6255 | */ | |
ad19db71 | 6256 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6257 | init_special_inode(inode, inode->i_mode, rdev); |
6258 | ||
6259 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6260 | if (err) |
b0d5d10f CM |
6261 | goto out_unlock_inode; |
6262 | ||
6263 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6264 | if (err) { | |
6265 | goto out_unlock_inode; | |
6266 | } else { | |
1b4ab1bb | 6267 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6268 | unlock_new_inode(inode); |
08c422c2 | 6269 | d_instantiate(dentry, inode); |
618e21d5 | 6270 | } |
b0d5d10f | 6271 | |
618e21d5 | 6272 | out_unlock: |
7ad85bb7 | 6273 | btrfs_end_transaction(trans, root); |
c581afc8 | 6274 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6275 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6276 | if (drop_inode) { |
6277 | inode_dec_link_count(inode); | |
6278 | iput(inode); | |
6279 | } | |
618e21d5 | 6280 | return err; |
b0d5d10f CM |
6281 | |
6282 | out_unlock_inode: | |
6283 | drop_inode = 1; | |
6284 | unlock_new_inode(inode); | |
6285 | goto out_unlock; | |
6286 | ||
618e21d5 JB |
6287 | } |
6288 | ||
39279cc3 | 6289 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6290 | umode_t mode, bool excl) |
39279cc3 CM |
6291 | { |
6292 | struct btrfs_trans_handle *trans; | |
6293 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6294 | struct inode *inode = NULL; |
43baa579 | 6295 | int drop_inode_on_err = 0; |
a22285a6 | 6296 | int err; |
39279cc3 | 6297 | u64 objectid; |
00e4e6b3 | 6298 | u64 index = 0; |
39279cc3 | 6299 | |
9ed74f2d JB |
6300 | /* |
6301 | * 2 for inode item and ref | |
6302 | * 2 for dir items | |
6303 | * 1 for xattr if selinux is on | |
6304 | */ | |
a22285a6 YZ |
6305 | trans = btrfs_start_transaction(root, 5); |
6306 | if (IS_ERR(trans)) | |
6307 | return PTR_ERR(trans); | |
9ed74f2d | 6308 | |
581bb050 LZ |
6309 | err = btrfs_find_free_ino(root, &objectid); |
6310 | if (err) | |
6311 | goto out_unlock; | |
6312 | ||
aec7477b | 6313 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6314 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6315 | mode, &index); |
7cf96da3 TI |
6316 | if (IS_ERR(inode)) { |
6317 | err = PTR_ERR(inode); | |
39279cc3 | 6318 | goto out_unlock; |
7cf96da3 | 6319 | } |
43baa579 | 6320 | drop_inode_on_err = 1; |
ad19db71 CS |
6321 | /* |
6322 | * If the active LSM wants to access the inode during | |
6323 | * d_instantiate it needs these. Smack checks to see | |
6324 | * if the filesystem supports xattrs by looking at the | |
6325 | * ops vector. | |
6326 | */ | |
6327 | inode->i_fop = &btrfs_file_operations; | |
6328 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6329 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6330 | |
6331 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6332 | if (err) | |
6333 | goto out_unlock_inode; | |
6334 | ||
6335 | err = btrfs_update_inode(trans, root, inode); | |
6336 | if (err) | |
6337 | goto out_unlock_inode; | |
ad19db71 | 6338 | |
a1b075d2 | 6339 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6340 | if (err) |
b0d5d10f | 6341 | goto out_unlock_inode; |
43baa579 | 6342 | |
43baa579 | 6343 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6344 | unlock_new_inode(inode); |
43baa579 FB |
6345 | d_instantiate(dentry, inode); |
6346 | ||
39279cc3 | 6347 | out_unlock: |
7ad85bb7 | 6348 | btrfs_end_transaction(trans, root); |
43baa579 | 6349 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6350 | inode_dec_link_count(inode); |
6351 | iput(inode); | |
6352 | } | |
c581afc8 | 6353 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6354 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6355 | return err; |
b0d5d10f CM |
6356 | |
6357 | out_unlock_inode: | |
6358 | unlock_new_inode(inode); | |
6359 | goto out_unlock; | |
6360 | ||
39279cc3 CM |
6361 | } |
6362 | ||
6363 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6364 | struct dentry *dentry) | |
6365 | { | |
6366 | struct btrfs_trans_handle *trans; | |
6367 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6368 | struct inode *inode = old_dentry->d_inode; | |
00e4e6b3 | 6369 | u64 index; |
39279cc3 CM |
6370 | int err; |
6371 | int drop_inode = 0; | |
6372 | ||
4a8be425 TH |
6373 | /* do not allow sys_link's with other subvols of the same device */ |
6374 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6375 | return -EXDEV; |
4a8be425 | 6376 | |
f186373f | 6377 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6378 | return -EMLINK; |
4a8be425 | 6379 | |
3de4586c | 6380 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6381 | if (err) |
6382 | goto fail; | |
6383 | ||
a22285a6 | 6384 | /* |
7e6b6465 | 6385 | * 2 items for inode and inode ref |
a22285a6 | 6386 | * 2 items for dir items |
7e6b6465 | 6387 | * 1 item for parent inode |
a22285a6 | 6388 | */ |
7e6b6465 | 6389 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6390 | if (IS_ERR(trans)) { |
6391 | err = PTR_ERR(trans); | |
6392 | goto fail; | |
6393 | } | |
5f39d397 | 6394 | |
67de1176 MX |
6395 | /* There are several dir indexes for this inode, clear the cache. */ |
6396 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6397 | inc_nlink(inode); |
0c4d2d95 | 6398 | inode_inc_iversion(inode); |
3153495d | 6399 | inode->i_ctime = CURRENT_TIME; |
7de9c6ee | 6400 | ihold(inode); |
e9976151 | 6401 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6402 | |
a1b075d2 | 6403 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6404 | |
a5719521 | 6405 | if (err) { |
54aa1f4d | 6406 | drop_inode = 1; |
a5719521 | 6407 | } else { |
10d9f309 | 6408 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6409 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6410 | if (err) |
6411 | goto fail; | |
ef3b9af5 FM |
6412 | if (inode->i_nlink == 1) { |
6413 | /* | |
6414 | * If new hard link count is 1, it's a file created | |
6415 | * with open(2) O_TMPFILE flag. | |
6416 | */ | |
6417 | err = btrfs_orphan_del(trans, inode); | |
6418 | if (err) | |
6419 | goto fail; | |
6420 | } | |
08c422c2 | 6421 | d_instantiate(dentry, inode); |
6a912213 | 6422 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6423 | } |
39279cc3 | 6424 | |
7ad85bb7 | 6425 | btrfs_end_transaction(trans, root); |
c581afc8 | 6426 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6427 | fail: |
39279cc3 CM |
6428 | if (drop_inode) { |
6429 | inode_dec_link_count(inode); | |
6430 | iput(inode); | |
6431 | } | |
b53d3f5d | 6432 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6433 | return err; |
6434 | } | |
6435 | ||
18bb1db3 | 6436 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6437 | { |
b9d86667 | 6438 | struct inode *inode = NULL; |
39279cc3 CM |
6439 | struct btrfs_trans_handle *trans; |
6440 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6441 | int err = 0; | |
6442 | int drop_on_err = 0; | |
b9d86667 | 6443 | u64 objectid = 0; |
00e4e6b3 | 6444 | u64 index = 0; |
39279cc3 | 6445 | |
9ed74f2d JB |
6446 | /* |
6447 | * 2 items for inode and ref | |
6448 | * 2 items for dir items | |
6449 | * 1 for xattr if selinux is on | |
6450 | */ | |
a22285a6 YZ |
6451 | trans = btrfs_start_transaction(root, 5); |
6452 | if (IS_ERR(trans)) | |
6453 | return PTR_ERR(trans); | |
39279cc3 | 6454 | |
581bb050 LZ |
6455 | err = btrfs_find_free_ino(root, &objectid); |
6456 | if (err) | |
6457 | goto out_fail; | |
6458 | ||
aec7477b | 6459 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6460 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6461 | S_IFDIR | mode, &index); |
39279cc3 CM |
6462 | if (IS_ERR(inode)) { |
6463 | err = PTR_ERR(inode); | |
6464 | goto out_fail; | |
6465 | } | |
5f39d397 | 6466 | |
39279cc3 | 6467 | drop_on_err = 1; |
b0d5d10f CM |
6468 | /* these must be set before we unlock the inode */ |
6469 | inode->i_op = &btrfs_dir_inode_operations; | |
6470 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6471 | |
2a7dba39 | 6472 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6473 | if (err) |
b0d5d10f | 6474 | goto out_fail_inode; |
39279cc3 | 6475 | |
dbe674a9 | 6476 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6477 | err = btrfs_update_inode(trans, root, inode); |
6478 | if (err) | |
b0d5d10f | 6479 | goto out_fail_inode; |
5f39d397 | 6480 | |
a1b075d2 JB |
6481 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6482 | dentry->d_name.len, 0, index); | |
39279cc3 | 6483 | if (err) |
b0d5d10f | 6484 | goto out_fail_inode; |
5f39d397 | 6485 | |
39279cc3 | 6486 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6487 | /* |
6488 | * mkdir is special. We're unlocking after we call d_instantiate | |
6489 | * to avoid a race with nfsd calling d_instantiate. | |
6490 | */ | |
6491 | unlock_new_inode(inode); | |
39279cc3 | 6492 | drop_on_err = 0; |
39279cc3 CM |
6493 | |
6494 | out_fail: | |
7ad85bb7 | 6495 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6496 | if (drop_on_err) { |
6497 | inode_dec_link_count(inode); | |
39279cc3 | 6498 | iput(inode); |
c7cfb8a5 | 6499 | } |
c581afc8 | 6500 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6501 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6502 | return err; |
b0d5d10f CM |
6503 | |
6504 | out_fail_inode: | |
6505 | unlock_new_inode(inode); | |
6506 | goto out_fail; | |
39279cc3 CM |
6507 | } |
6508 | ||
e6c4efd8 QW |
6509 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6510 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6511 | { | |
6512 | struct rb_node *next; | |
6513 | ||
6514 | next = rb_next(&em->rb_node); | |
6515 | if (!next) | |
6516 | return NULL; | |
6517 | return container_of(next, struct extent_map, rb_node); | |
6518 | } | |
6519 | ||
6520 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6521 | { | |
6522 | struct rb_node *prev; | |
6523 | ||
6524 | prev = rb_prev(&em->rb_node); | |
6525 | if (!prev) | |
6526 | return NULL; | |
6527 | return container_of(prev, struct extent_map, rb_node); | |
6528 | } | |
6529 | ||
d352ac68 | 6530 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6531 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6532 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6533 | * the best fitted new extent into the tree. |
d352ac68 | 6534 | */ |
3b951516 CM |
6535 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6536 | struct extent_map *existing, | |
e6dcd2dc | 6537 | struct extent_map *em, |
51f395ad | 6538 | u64 map_start) |
3b951516 | 6539 | { |
e6c4efd8 QW |
6540 | struct extent_map *prev; |
6541 | struct extent_map *next; | |
6542 | u64 start; | |
6543 | u64 end; | |
3b951516 | 6544 | u64 start_diff; |
3b951516 | 6545 | |
e6dcd2dc | 6546 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6547 | |
6548 | if (existing->start > map_start) { | |
6549 | next = existing; | |
6550 | prev = prev_extent_map(next); | |
6551 | } else { | |
6552 | prev = existing; | |
6553 | next = next_extent_map(prev); | |
6554 | } | |
6555 | ||
6556 | start = prev ? extent_map_end(prev) : em->start; | |
6557 | start = max_t(u64, start, em->start); | |
6558 | end = next ? next->start : extent_map_end(em); | |
6559 | end = min_t(u64, end, extent_map_end(em)); | |
6560 | start_diff = start - em->start; | |
6561 | em->start = start; | |
6562 | em->len = end - start; | |
c8b97818 CM |
6563 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6564 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6565 | em->block_start += start_diff; |
c8b97818 CM |
6566 | em->block_len -= start_diff; |
6567 | } | |
09a2a8f9 | 6568 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6569 | } |
6570 | ||
c8b97818 CM |
6571 | static noinline int uncompress_inline(struct btrfs_path *path, |
6572 | struct inode *inode, struct page *page, | |
6573 | size_t pg_offset, u64 extent_offset, | |
6574 | struct btrfs_file_extent_item *item) | |
6575 | { | |
6576 | int ret; | |
6577 | struct extent_buffer *leaf = path->nodes[0]; | |
6578 | char *tmp; | |
6579 | size_t max_size; | |
6580 | unsigned long inline_size; | |
6581 | unsigned long ptr; | |
261507a0 | 6582 | int compress_type; |
c8b97818 CM |
6583 | |
6584 | WARN_ON(pg_offset != 0); | |
261507a0 | 6585 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6586 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6587 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6588 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6589 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6590 | if (!tmp) |
6591 | return -ENOMEM; | |
c8b97818 CM |
6592 | ptr = btrfs_file_extent_inline_start(item); |
6593 | ||
6594 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6595 | ||
5b050f04 | 6596 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
261507a0 LZ |
6597 | ret = btrfs_decompress(compress_type, tmp, page, |
6598 | extent_offset, inline_size, max_size); | |
c8b97818 | 6599 | kfree(tmp); |
166ae5a4 | 6600 | return ret; |
c8b97818 CM |
6601 | } |
6602 | ||
d352ac68 CM |
6603 | /* |
6604 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6605 | * the ugly parts come from merging extents from the disk with the in-ram |
6606 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6607 | * where the in-ram extents might be locked pending data=ordered completion. |
6608 | * | |
6609 | * This also copies inline extents directly into the page. | |
6610 | */ | |
d397712b | 6611 | |
a52d9a80 | 6612 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6613 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6614 | int create) |
6615 | { | |
6616 | int ret; | |
6617 | int err = 0; | |
a52d9a80 CM |
6618 | u64 extent_start = 0; |
6619 | u64 extent_end = 0; | |
33345d01 | 6620 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6621 | u32 found_type; |
f421950f | 6622 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6623 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6624 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6625 | struct extent_buffer *leaf; |
6626 | struct btrfs_key found_key; | |
a52d9a80 CM |
6627 | struct extent_map *em = NULL; |
6628 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6629 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6630 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6631 | const bool new_inline = !page || create; |
a52d9a80 | 6632 | |
a52d9a80 | 6633 | again: |
890871be | 6634 | read_lock(&em_tree->lock); |
d1310b2e | 6635 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6636 | if (em) |
6637 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6638 | read_unlock(&em_tree->lock); |
d1310b2e | 6639 | |
a52d9a80 | 6640 | if (em) { |
e1c4b745 CM |
6641 | if (em->start > start || em->start + em->len <= start) |
6642 | free_extent_map(em); | |
6643 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6644 | free_extent_map(em); |
6645 | else | |
6646 | goto out; | |
a52d9a80 | 6647 | } |
172ddd60 | 6648 | em = alloc_extent_map(); |
a52d9a80 | 6649 | if (!em) { |
d1310b2e CM |
6650 | err = -ENOMEM; |
6651 | goto out; | |
a52d9a80 | 6652 | } |
e6dcd2dc | 6653 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6654 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6655 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6656 | em->len = (u64)-1; |
c8b97818 | 6657 | em->block_len = (u64)-1; |
f421950f CM |
6658 | |
6659 | if (!path) { | |
6660 | path = btrfs_alloc_path(); | |
026fd317 JB |
6661 | if (!path) { |
6662 | err = -ENOMEM; | |
6663 | goto out; | |
6664 | } | |
6665 | /* | |
6666 | * Chances are we'll be called again, so go ahead and do | |
6667 | * readahead | |
6668 | */ | |
6669 | path->reada = 1; | |
f421950f CM |
6670 | } |
6671 | ||
179e29e4 CM |
6672 | ret = btrfs_lookup_file_extent(trans, root, path, |
6673 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6674 | if (ret < 0) { |
6675 | err = ret; | |
6676 | goto out; | |
6677 | } | |
6678 | ||
6679 | if (ret != 0) { | |
6680 | if (path->slots[0] == 0) | |
6681 | goto not_found; | |
6682 | path->slots[0]--; | |
6683 | } | |
6684 | ||
5f39d397 CM |
6685 | leaf = path->nodes[0]; |
6686 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6687 | struct btrfs_file_extent_item); |
a52d9a80 | 6688 | /* are we inside the extent that was found? */ |
5f39d397 | 6689 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6690 | found_type = found_key.type; |
5f39d397 | 6691 | if (found_key.objectid != objectid || |
a52d9a80 | 6692 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6693 | /* |
6694 | * If we backup past the first extent we want to move forward | |
6695 | * and see if there is an extent in front of us, otherwise we'll | |
6696 | * say there is a hole for our whole search range which can | |
6697 | * cause problems. | |
6698 | */ | |
6699 | extent_end = start; | |
6700 | goto next; | |
a52d9a80 CM |
6701 | } |
6702 | ||
5f39d397 CM |
6703 | found_type = btrfs_file_extent_type(leaf, item); |
6704 | extent_start = found_key.offset; | |
d899e052 YZ |
6705 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6706 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6707 | extent_end = extent_start + |
db94535d | 6708 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6709 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6710 | size_t size; | |
514ac8ad | 6711 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6712 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6713 | } |
25a50341 | 6714 | next: |
9036c102 YZ |
6715 | if (start >= extent_end) { |
6716 | path->slots[0]++; | |
6717 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6718 | ret = btrfs_next_leaf(root, path); | |
6719 | if (ret < 0) { | |
6720 | err = ret; | |
6721 | goto out; | |
a52d9a80 | 6722 | } |
9036c102 YZ |
6723 | if (ret > 0) |
6724 | goto not_found; | |
6725 | leaf = path->nodes[0]; | |
a52d9a80 | 6726 | } |
9036c102 YZ |
6727 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6728 | if (found_key.objectid != objectid || | |
6729 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6730 | goto not_found; | |
6731 | if (start + len <= found_key.offset) | |
6732 | goto not_found; | |
e2eca69d WS |
6733 | if (start > found_key.offset) |
6734 | goto next; | |
9036c102 | 6735 | em->start = start; |
70c8a91c | 6736 | em->orig_start = start; |
9036c102 YZ |
6737 | em->len = found_key.offset - start; |
6738 | goto not_found_em; | |
6739 | } | |
6740 | ||
7ffbb598 FM |
6741 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6742 | ||
d899e052 YZ |
6743 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6744 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6745 | goto insert; |
6746 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6747 | unsigned long ptr; |
a52d9a80 | 6748 | char *map; |
3326d1b0 CM |
6749 | size_t size; |
6750 | size_t extent_offset; | |
6751 | size_t copy_size; | |
a52d9a80 | 6752 | |
7ffbb598 | 6753 | if (new_inline) |
689f9346 | 6754 | goto out; |
5f39d397 | 6755 | |
514ac8ad | 6756 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6757 | extent_offset = page_offset(page) + pg_offset - extent_start; |
70dec807 | 6758 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 6759 | size - extent_offset); |
3326d1b0 | 6760 | em->start = extent_start + extent_offset; |
fda2832f | 6761 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6762 | em->orig_block_len = em->len; |
70c8a91c | 6763 | em->orig_start = em->start; |
689f9346 | 6764 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6765 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6766 | if (btrfs_file_extent_compression(leaf, item) != |
6767 | BTRFS_COMPRESS_NONE) { | |
c8b97818 CM |
6768 | ret = uncompress_inline(path, inode, page, |
6769 | pg_offset, | |
6770 | extent_offset, item); | |
166ae5a4 ZB |
6771 | if (ret) { |
6772 | err = ret; | |
6773 | goto out; | |
6774 | } | |
c8b97818 CM |
6775 | } else { |
6776 | map = kmap(page); | |
6777 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6778 | copy_size); | |
93c82d57 CM |
6779 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
6780 | memset(map + pg_offset + copy_size, 0, | |
6781 | PAGE_CACHE_SIZE - pg_offset - | |
6782 | copy_size); | |
6783 | } | |
c8b97818 CM |
6784 | kunmap(page); |
6785 | } | |
179e29e4 CM |
6786 | flush_dcache_page(page); |
6787 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6788 | BUG(); |
179e29e4 CM |
6789 | if (!trans) { |
6790 | kunmap(page); | |
6791 | free_extent_map(em); | |
6792 | em = NULL; | |
ff5714cc | 6793 | |
b3b4aa74 | 6794 | btrfs_release_path(path); |
7a7eaa40 | 6795 | trans = btrfs_join_transaction(root); |
ff5714cc | 6796 | |
3612b495 TI |
6797 | if (IS_ERR(trans)) |
6798 | return ERR_CAST(trans); | |
179e29e4 CM |
6799 | goto again; |
6800 | } | |
c8b97818 | 6801 | map = kmap(page); |
70dec807 | 6802 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6803 | copy_size); |
c8b97818 | 6804 | kunmap(page); |
179e29e4 | 6805 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6806 | } |
d1310b2e | 6807 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6808 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6809 | goto insert; |
a52d9a80 CM |
6810 | } |
6811 | not_found: | |
6812 | em->start = start; | |
70c8a91c | 6813 | em->orig_start = start; |
d1310b2e | 6814 | em->len = len; |
a52d9a80 | 6815 | not_found_em: |
5f39d397 | 6816 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6817 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6818 | insert: |
b3b4aa74 | 6819 | btrfs_release_path(path); |
d1310b2e | 6820 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6821 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6822 | em->start, em->len, start, len); |
a52d9a80 CM |
6823 | err = -EIO; |
6824 | goto out; | |
6825 | } | |
d1310b2e CM |
6826 | |
6827 | err = 0; | |
890871be | 6828 | write_lock(&em_tree->lock); |
09a2a8f9 | 6829 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6830 | /* it is possible that someone inserted the extent into the tree |
6831 | * while we had the lock dropped. It is also possible that | |
6832 | * an overlapping map exists in the tree | |
6833 | */ | |
a52d9a80 | 6834 | if (ret == -EEXIST) { |
3b951516 | 6835 | struct extent_map *existing; |
e6dcd2dc CM |
6836 | |
6837 | ret = 0; | |
6838 | ||
e6c4efd8 QW |
6839 | existing = search_extent_mapping(em_tree, start, len); |
6840 | /* | |
6841 | * existing will always be non-NULL, since there must be | |
6842 | * extent causing the -EEXIST. | |
6843 | */ | |
6844 | if (start >= extent_map_end(existing) || | |
32be3a1a | 6845 | start <= existing->start) { |
e6c4efd8 QW |
6846 | /* |
6847 | * The existing extent map is the one nearest to | |
6848 | * the [start, start + len) range which overlaps | |
6849 | */ | |
6850 | err = merge_extent_mapping(em_tree, existing, | |
6851 | em, start); | |
e1c4b745 | 6852 | free_extent_map(existing); |
e6c4efd8 | 6853 | if (err) { |
3b951516 CM |
6854 | free_extent_map(em); |
6855 | em = NULL; | |
6856 | } | |
6857 | } else { | |
6858 | free_extent_map(em); | |
6859 | em = existing; | |
e6dcd2dc | 6860 | err = 0; |
a52d9a80 | 6861 | } |
a52d9a80 | 6862 | } |
890871be | 6863 | write_unlock(&em_tree->lock); |
a52d9a80 | 6864 | out: |
1abe9b8a | 6865 | |
4cd8587c | 6866 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 6867 | |
f421950f CM |
6868 | if (path) |
6869 | btrfs_free_path(path); | |
a52d9a80 CM |
6870 | if (trans) { |
6871 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 6872 | if (!err) |
a52d9a80 CM |
6873 | err = ret; |
6874 | } | |
a52d9a80 CM |
6875 | if (err) { |
6876 | free_extent_map(em); | |
a52d9a80 CM |
6877 | return ERR_PTR(err); |
6878 | } | |
79787eaa | 6879 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6880 | return em; |
6881 | } | |
6882 | ||
ec29ed5b CM |
6883 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
6884 | size_t pg_offset, u64 start, u64 len, | |
6885 | int create) | |
6886 | { | |
6887 | struct extent_map *em; | |
6888 | struct extent_map *hole_em = NULL; | |
6889 | u64 range_start = start; | |
6890 | u64 end; | |
6891 | u64 found; | |
6892 | u64 found_end; | |
6893 | int err = 0; | |
6894 | ||
6895 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
6896 | if (IS_ERR(em)) | |
6897 | return em; | |
6898 | if (em) { | |
6899 | /* | |
f9e4fb53 LB |
6900 | * if our em maps to |
6901 | * - a hole or | |
6902 | * - a pre-alloc extent, | |
6903 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 6904 | */ |
f9e4fb53 LB |
6905 | if (em->block_start != EXTENT_MAP_HOLE && |
6906 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
6907 | return em; |
6908 | else | |
6909 | hole_em = em; | |
6910 | } | |
6911 | ||
6912 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6913 | end = start + len; | |
6914 | if (end < start) | |
6915 | end = (u64)-1; | |
6916 | else | |
6917 | end -= 1; | |
6918 | ||
6919 | em = NULL; | |
6920 | ||
6921 | /* ok, we didn't find anything, lets look for delalloc */ | |
6922 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
6923 | end, len, EXTENT_DELALLOC, 1); | |
6924 | found_end = range_start + found; | |
6925 | if (found_end < range_start) | |
6926 | found_end = (u64)-1; | |
6927 | ||
6928 | /* | |
6929 | * we didn't find anything useful, return | |
6930 | * the original results from get_extent() | |
6931 | */ | |
6932 | if (range_start > end || found_end <= start) { | |
6933 | em = hole_em; | |
6934 | hole_em = NULL; | |
6935 | goto out; | |
6936 | } | |
6937 | ||
6938 | /* adjust the range_start to make sure it doesn't | |
6939 | * go backwards from the start they passed in | |
6940 | */ | |
67871254 | 6941 | range_start = max(start, range_start); |
ec29ed5b CM |
6942 | found = found_end - range_start; |
6943 | ||
6944 | if (found > 0) { | |
6945 | u64 hole_start = start; | |
6946 | u64 hole_len = len; | |
6947 | ||
172ddd60 | 6948 | em = alloc_extent_map(); |
ec29ed5b CM |
6949 | if (!em) { |
6950 | err = -ENOMEM; | |
6951 | goto out; | |
6952 | } | |
6953 | /* | |
6954 | * when btrfs_get_extent can't find anything it | |
6955 | * returns one huge hole | |
6956 | * | |
6957 | * make sure what it found really fits our range, and | |
6958 | * adjust to make sure it is based on the start from | |
6959 | * the caller | |
6960 | */ | |
6961 | if (hole_em) { | |
6962 | u64 calc_end = extent_map_end(hole_em); | |
6963 | ||
6964 | if (calc_end <= start || (hole_em->start > end)) { | |
6965 | free_extent_map(hole_em); | |
6966 | hole_em = NULL; | |
6967 | } else { | |
6968 | hole_start = max(hole_em->start, start); | |
6969 | hole_len = calc_end - hole_start; | |
6970 | } | |
6971 | } | |
6972 | em->bdev = NULL; | |
6973 | if (hole_em && range_start > hole_start) { | |
6974 | /* our hole starts before our delalloc, so we | |
6975 | * have to return just the parts of the hole | |
6976 | * that go until the delalloc starts | |
6977 | */ | |
6978 | em->len = min(hole_len, | |
6979 | range_start - hole_start); | |
6980 | em->start = hole_start; | |
6981 | em->orig_start = hole_start; | |
6982 | /* | |
6983 | * don't adjust block start at all, | |
6984 | * it is fixed at EXTENT_MAP_HOLE | |
6985 | */ | |
6986 | em->block_start = hole_em->block_start; | |
6987 | em->block_len = hole_len; | |
f9e4fb53 LB |
6988 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6989 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
6990 | } else { |
6991 | em->start = range_start; | |
6992 | em->len = found; | |
6993 | em->orig_start = range_start; | |
6994 | em->block_start = EXTENT_MAP_DELALLOC; | |
6995 | em->block_len = found; | |
6996 | } | |
6997 | } else if (hole_em) { | |
6998 | return hole_em; | |
6999 | } | |
7000 | out: | |
7001 | ||
7002 | free_extent_map(hole_em); | |
7003 | if (err) { | |
7004 | free_extent_map(em); | |
7005 | return ERR_PTR(err); | |
7006 | } | |
7007 | return em; | |
7008 | } | |
7009 | ||
4b46fce2 JB |
7010 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7011 | u64 start, u64 len) | |
7012 | { | |
7013 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7014 | struct extent_map *em; |
4b46fce2 JB |
7015 | struct btrfs_key ins; |
7016 | u64 alloc_hint; | |
7017 | int ret; | |
4b46fce2 | 7018 | |
4b46fce2 | 7019 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 7020 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
e570fd27 | 7021 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7022 | if (ret) |
7023 | return ERR_PTR(ret); | |
4b46fce2 | 7024 | |
70c8a91c | 7025 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
cc95bef6 | 7026 | ins.offset, ins.offset, ins.offset, 0); |
00361589 | 7027 | if (IS_ERR(em)) { |
e570fd27 | 7028 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7029 | return em; |
7030 | } | |
4b46fce2 JB |
7031 | |
7032 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, | |
7033 | ins.offset, ins.offset, 0); | |
7034 | if (ret) { | |
e570fd27 | 7035 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7036 | free_extent_map(em); |
7037 | return ERR_PTR(ret); | |
4b46fce2 | 7038 | } |
00361589 | 7039 | |
31193213 DY |
7040 | btrfs_qgroup_update_reserved_bytes(root->fs_info, |
7041 | root->root_key.objectid, | |
7042 | ins.offset, 1); | |
7043 | ||
4b46fce2 JB |
7044 | return em; |
7045 | } | |
7046 | ||
46bfbb5c CM |
7047 | /* |
7048 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7049 | * block must be cow'd | |
7050 | */ | |
00361589 | 7051 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7052 | u64 *orig_start, u64 *orig_block_len, |
7053 | u64 *ram_bytes) | |
46bfbb5c | 7054 | { |
00361589 | 7055 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7056 | struct btrfs_path *path; |
7057 | int ret; | |
7058 | struct extent_buffer *leaf; | |
7059 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7060 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7061 | struct btrfs_file_extent_item *fi; |
7062 | struct btrfs_key key; | |
7063 | u64 disk_bytenr; | |
7064 | u64 backref_offset; | |
7065 | u64 extent_end; | |
7066 | u64 num_bytes; | |
7067 | int slot; | |
7068 | int found_type; | |
7ee9e440 | 7069 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7070 | |
46bfbb5c CM |
7071 | path = btrfs_alloc_path(); |
7072 | if (!path) | |
7073 | return -ENOMEM; | |
7074 | ||
00361589 | 7075 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7076 | offset, 0); |
7077 | if (ret < 0) | |
7078 | goto out; | |
7079 | ||
7080 | slot = path->slots[0]; | |
7081 | if (ret == 1) { | |
7082 | if (slot == 0) { | |
7083 | /* can't find the item, must cow */ | |
7084 | ret = 0; | |
7085 | goto out; | |
7086 | } | |
7087 | slot--; | |
7088 | } | |
7089 | ret = 0; | |
7090 | leaf = path->nodes[0]; | |
7091 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7092 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7093 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7094 | /* not our file or wrong item type, must cow */ | |
7095 | goto out; | |
7096 | } | |
7097 | ||
7098 | if (key.offset > offset) { | |
7099 | /* Wrong offset, must cow */ | |
7100 | goto out; | |
7101 | } | |
7102 | ||
7103 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7104 | found_type = btrfs_file_extent_type(leaf, fi); | |
7105 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7106 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7107 | /* not a regular extent, must cow */ | |
7108 | goto out; | |
7109 | } | |
7ee9e440 JB |
7110 | |
7111 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7112 | goto out; | |
7113 | ||
e77751aa MX |
7114 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7115 | if (extent_end <= offset) | |
7116 | goto out; | |
7117 | ||
46bfbb5c | 7118 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7119 | if (disk_bytenr == 0) |
7120 | goto out; | |
7121 | ||
7122 | if (btrfs_file_extent_compression(leaf, fi) || | |
7123 | btrfs_file_extent_encryption(leaf, fi) || | |
7124 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7125 | goto out; | |
7126 | ||
46bfbb5c CM |
7127 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7128 | ||
7ee9e440 JB |
7129 | if (orig_start) { |
7130 | *orig_start = key.offset - backref_offset; | |
7131 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7132 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7133 | } | |
eb384b55 | 7134 | |
46bfbb5c CM |
7135 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7136 | goto out; | |
7b2b7085 MX |
7137 | |
7138 | num_bytes = min(offset + *len, extent_end) - offset; | |
7139 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7140 | u64 range_end; | |
7141 | ||
7142 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7143 | ret = test_range_bit(io_tree, offset, range_end, | |
7144 | EXTENT_DELALLOC, 0, NULL); | |
7145 | if (ret) { | |
7146 | ret = -EAGAIN; | |
7147 | goto out; | |
7148 | } | |
7149 | } | |
7150 | ||
1bda19eb | 7151 | btrfs_release_path(path); |
46bfbb5c CM |
7152 | |
7153 | /* | |
7154 | * look for other files referencing this extent, if we | |
7155 | * find any we must cow | |
7156 | */ | |
00361589 JB |
7157 | trans = btrfs_join_transaction(root); |
7158 | if (IS_ERR(trans)) { | |
7159 | ret = 0; | |
46bfbb5c | 7160 | goto out; |
00361589 JB |
7161 | } |
7162 | ||
7163 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7164 | key.offset - backref_offset, disk_bytenr); | |
7165 | btrfs_end_transaction(trans, root); | |
7166 | if (ret) { | |
7167 | ret = 0; | |
7168 | goto out; | |
7169 | } | |
46bfbb5c CM |
7170 | |
7171 | /* | |
7172 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7173 | * in this extent we are about to write. If there | |
7174 | * are any csums in that range we have to cow in order | |
7175 | * to keep the csums correct | |
7176 | */ | |
7177 | disk_bytenr += backref_offset; | |
7178 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7179 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7180 | goto out; | |
7181 | /* | |
7182 | * all of the above have passed, it is safe to overwrite this extent | |
7183 | * without cow | |
7184 | */ | |
eb384b55 | 7185 | *len = num_bytes; |
46bfbb5c CM |
7186 | ret = 1; |
7187 | out: | |
7188 | btrfs_free_path(path); | |
7189 | return ret; | |
7190 | } | |
7191 | ||
fc4adbff AG |
7192 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7193 | { | |
7194 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7195 | int found = false; | |
7196 | void **pagep = NULL; | |
7197 | struct page *page = NULL; | |
7198 | int start_idx; | |
7199 | int end_idx; | |
7200 | ||
7201 | start_idx = start >> PAGE_CACHE_SHIFT; | |
7202 | ||
7203 | /* | |
7204 | * end is the last byte in the last page. end == start is legal | |
7205 | */ | |
7206 | end_idx = end >> PAGE_CACHE_SHIFT; | |
7207 | ||
7208 | rcu_read_lock(); | |
7209 | ||
7210 | /* Most of the code in this while loop is lifted from | |
7211 | * find_get_page. It's been modified to begin searching from a | |
7212 | * page and return just the first page found in that range. If the | |
7213 | * found idx is less than or equal to the end idx then we know that | |
7214 | * a page exists. If no pages are found or if those pages are | |
7215 | * outside of the range then we're fine (yay!) */ | |
7216 | while (page == NULL && | |
7217 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7218 | page = radix_tree_deref_slot(pagep); | |
7219 | if (unlikely(!page)) | |
7220 | break; | |
7221 | ||
7222 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7223 | if (radix_tree_deref_retry(page)) { |
7224 | page = NULL; | |
fc4adbff | 7225 | continue; |
809f9016 | 7226 | } |
fc4adbff AG |
7227 | /* |
7228 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7229 | * here as an exceptional entry: so return it without | |
7230 | * attempting to raise page count. | |
7231 | */ | |
6fdef6d4 | 7232 | page = NULL; |
fc4adbff AG |
7233 | break; /* TODO: Is this relevant for this use case? */ |
7234 | } | |
7235 | ||
91405151 FM |
7236 | if (!page_cache_get_speculative(page)) { |
7237 | page = NULL; | |
fc4adbff | 7238 | continue; |
91405151 | 7239 | } |
fc4adbff AG |
7240 | |
7241 | /* | |
7242 | * Has the page moved? | |
7243 | * This is part of the lockless pagecache protocol. See | |
7244 | * include/linux/pagemap.h for details. | |
7245 | */ | |
7246 | if (unlikely(page != *pagep)) { | |
7247 | page_cache_release(page); | |
7248 | page = NULL; | |
7249 | } | |
7250 | } | |
7251 | ||
7252 | if (page) { | |
7253 | if (page->index <= end_idx) | |
7254 | found = true; | |
7255 | page_cache_release(page); | |
7256 | } | |
7257 | ||
7258 | rcu_read_unlock(); | |
7259 | return found; | |
7260 | } | |
7261 | ||
eb838e73 JB |
7262 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7263 | struct extent_state **cached_state, int writing) | |
7264 | { | |
7265 | struct btrfs_ordered_extent *ordered; | |
7266 | int ret = 0; | |
7267 | ||
7268 | while (1) { | |
7269 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7270 | 0, cached_state); | |
7271 | /* | |
7272 | * We're concerned with the entire range that we're going to be | |
7273 | * doing DIO to, so we need to make sure theres no ordered | |
7274 | * extents in this range. | |
7275 | */ | |
7276 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7277 | lockend - lockstart + 1); | |
7278 | ||
7279 | /* | |
7280 | * We need to make sure there are no buffered pages in this | |
7281 | * range either, we could have raced between the invalidate in | |
7282 | * generic_file_direct_write and locking the extent. The | |
7283 | * invalidate needs to happen so that reads after a write do not | |
7284 | * get stale data. | |
7285 | */ | |
fc4adbff AG |
7286 | if (!ordered && |
7287 | (!writing || | |
7288 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7289 | break; |
7290 | ||
7291 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7292 | cached_state, GFP_NOFS); | |
7293 | ||
7294 | if (ordered) { | |
7295 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7296 | btrfs_put_ordered_extent(ordered); | |
7297 | } else { | |
7298 | /* Screw you mmap */ | |
728404da | 7299 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
075bdbdb FM |
7300 | if (ret) |
7301 | break; | |
7302 | ret = filemap_fdatawait_range(inode->i_mapping, | |
7303 | lockstart, | |
7304 | lockend); | |
eb838e73 JB |
7305 | if (ret) |
7306 | break; | |
7307 | ||
7308 | /* | |
7309 | * If we found a page that couldn't be invalidated just | |
7310 | * fall back to buffered. | |
7311 | */ | |
7312 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
7313 | lockstart >> PAGE_CACHE_SHIFT, | |
7314 | lockend >> PAGE_CACHE_SHIFT); | |
7315 | if (ret) | |
7316 | break; | |
7317 | } | |
7318 | ||
7319 | cond_resched(); | |
7320 | } | |
7321 | ||
7322 | return ret; | |
7323 | } | |
7324 | ||
69ffb543 JB |
7325 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7326 | u64 len, u64 orig_start, | |
7327 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7328 | u64 orig_block_len, u64 ram_bytes, |
7329 | int type) | |
69ffb543 JB |
7330 | { |
7331 | struct extent_map_tree *em_tree; | |
7332 | struct extent_map *em; | |
7333 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7334 | int ret; | |
7335 | ||
7336 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7337 | em = alloc_extent_map(); | |
7338 | if (!em) | |
7339 | return ERR_PTR(-ENOMEM); | |
7340 | ||
7341 | em->start = start; | |
7342 | em->orig_start = orig_start; | |
2ab28f32 JB |
7343 | em->mod_start = start; |
7344 | em->mod_len = len; | |
69ffb543 JB |
7345 | em->len = len; |
7346 | em->block_len = block_len; | |
7347 | em->block_start = block_start; | |
7348 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7349 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7350 | em->ram_bytes = ram_bytes; |
70c8a91c | 7351 | em->generation = -1; |
69ffb543 JB |
7352 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7353 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7354 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7355 | |
7356 | do { | |
7357 | btrfs_drop_extent_cache(inode, em->start, | |
7358 | em->start + em->len - 1, 0); | |
7359 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7360 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7361 | write_unlock(&em_tree->lock); |
7362 | } while (ret == -EEXIST); | |
7363 | ||
7364 | if (ret) { | |
7365 | free_extent_map(em); | |
7366 | return ERR_PTR(ret); | |
7367 | } | |
7368 | ||
7369 | return em; | |
7370 | } | |
7371 | ||
7372 | ||
4b46fce2 JB |
7373 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7374 | struct buffer_head *bh_result, int create) | |
7375 | { | |
7376 | struct extent_map *em; | |
7377 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7378 | struct extent_state *cached_state = NULL; |
4b46fce2 | 7379 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7380 | u64 lockstart, lockend; |
4b46fce2 | 7381 | u64 len = bh_result->b_size; |
e1cbbfa5 | 7382 | u64 *outstanding_extents = NULL; |
eb838e73 | 7383 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7384 | int ret = 0; |
eb838e73 | 7385 | |
172a5049 | 7386 | if (create) |
3266789f | 7387 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7388 | else |
c329861d | 7389 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7390 | |
c329861d JB |
7391 | lockstart = start; |
7392 | lockend = start + len - 1; | |
7393 | ||
e1cbbfa5 JB |
7394 | if (current->journal_info) { |
7395 | /* | |
7396 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7397 | * that anything that needs to check if there's a transction doesn't get | |
7398 | * confused. | |
7399 | */ | |
7400 | outstanding_extents = current->journal_info; | |
7401 | current->journal_info = NULL; | |
7402 | } | |
7403 | ||
eb838e73 JB |
7404 | /* |
7405 | * If this errors out it's because we couldn't invalidate pagecache for | |
7406 | * this range and we need to fallback to buffered. | |
7407 | */ | |
7408 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) | |
7409 | return -ENOTBLK; | |
7410 | ||
4b46fce2 | 7411 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7412 | if (IS_ERR(em)) { |
7413 | ret = PTR_ERR(em); | |
7414 | goto unlock_err; | |
7415 | } | |
4b46fce2 JB |
7416 | |
7417 | /* | |
7418 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7419 | * io. INLINE is special, and we could probably kludge it in here, but | |
7420 | * it's still buffered so for safety lets just fall back to the generic | |
7421 | * buffered path. | |
7422 | * | |
7423 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7424 | * decompress it, so there will be buffering required no matter what we | |
7425 | * do, so go ahead and fallback to buffered. | |
7426 | * | |
7427 | * We return -ENOTBLK because thats what makes DIO go ahead and go back | |
7428 | * to buffered IO. Don't blame me, this is the price we pay for using | |
7429 | * the generic code. | |
7430 | */ | |
7431 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7432 | em->block_start == EXTENT_MAP_INLINE) { | |
7433 | free_extent_map(em); | |
eb838e73 JB |
7434 | ret = -ENOTBLK; |
7435 | goto unlock_err; | |
4b46fce2 JB |
7436 | } |
7437 | ||
7438 | /* Just a good old fashioned hole, return */ | |
7439 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7440 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7441 | free_extent_map(em); | |
eb838e73 | 7442 | goto unlock_err; |
4b46fce2 JB |
7443 | } |
7444 | ||
7445 | /* | |
7446 | * We don't allocate a new extent in the following cases | |
7447 | * | |
7448 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7449 | * existing extent. | |
7450 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7451 | * just use the extent. | |
7452 | * | |
7453 | */ | |
46bfbb5c | 7454 | if (!create) { |
eb838e73 JB |
7455 | len = min(len, em->len - (start - em->start)); |
7456 | lockstart = start + len; | |
7457 | goto unlock; | |
46bfbb5c | 7458 | } |
4b46fce2 JB |
7459 | |
7460 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7461 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7462 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7463 | int type; |
eb384b55 | 7464 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7465 | |
7466 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7467 | type = BTRFS_ORDERED_PREALLOC; | |
7468 | else | |
7469 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7470 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7471 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7472 | |
00361589 | 7473 | if (can_nocow_extent(inode, start, &len, &orig_start, |
7ee9e440 | 7474 | &orig_block_len, &ram_bytes) == 1) { |
69ffb543 JB |
7475 | if (type == BTRFS_ORDERED_PREALLOC) { |
7476 | free_extent_map(em); | |
7477 | em = create_pinned_em(inode, start, len, | |
7478 | orig_start, | |
b4939680 | 7479 | block_start, len, |
cc95bef6 JB |
7480 | orig_block_len, |
7481 | ram_bytes, type); | |
555e1286 FM |
7482 | if (IS_ERR(em)) { |
7483 | ret = PTR_ERR(em); | |
69ffb543 | 7484 | goto unlock_err; |
555e1286 | 7485 | } |
69ffb543 JB |
7486 | } |
7487 | ||
46bfbb5c CM |
7488 | ret = btrfs_add_ordered_extent_dio(inode, start, |
7489 | block_start, len, len, type); | |
46bfbb5c CM |
7490 | if (ret) { |
7491 | free_extent_map(em); | |
eb838e73 | 7492 | goto unlock_err; |
46bfbb5c CM |
7493 | } |
7494 | goto unlock; | |
4b46fce2 | 7495 | } |
4b46fce2 | 7496 | } |
00361589 | 7497 | |
46bfbb5c CM |
7498 | /* |
7499 | * this will cow the extent, reset the len in case we changed | |
7500 | * it above | |
7501 | */ | |
7502 | len = bh_result->b_size; | |
70c8a91c JB |
7503 | free_extent_map(em); |
7504 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7505 | if (IS_ERR(em)) { |
7506 | ret = PTR_ERR(em); | |
7507 | goto unlock_err; | |
7508 | } | |
46bfbb5c CM |
7509 | len = min(len, em->len - (start - em->start)); |
7510 | unlock: | |
4b46fce2 JB |
7511 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7512 | inode->i_blkbits; | |
46bfbb5c | 7513 | bh_result->b_size = len; |
4b46fce2 JB |
7514 | bh_result->b_bdev = em->bdev; |
7515 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7516 | if (create) { |
7517 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7518 | set_buffer_new(bh_result); | |
7519 | ||
7520 | /* | |
7521 | * Need to update the i_size under the extent lock so buffered | |
7522 | * readers will get the updated i_size when we unlock. | |
7523 | */ | |
7524 | if (start + len > i_size_read(inode)) | |
7525 | i_size_write(inode, start + len); | |
0934856d | 7526 | |
e1cbbfa5 JB |
7527 | /* |
7528 | * If we have an outstanding_extents count still set then we're | |
7529 | * within our reservation, otherwise we need to adjust our inode | |
7530 | * counter appropriately. | |
7531 | */ | |
7532 | if (*outstanding_extents) { | |
7533 | (*outstanding_extents)--; | |
7534 | } else { | |
3e05bde8 JB |
7535 | spin_lock(&BTRFS_I(inode)->lock); |
7536 | BTRFS_I(inode)->outstanding_extents++; | |
7537 | spin_unlock(&BTRFS_I(inode)->lock); | |
7538 | } | |
e1cbbfa5 JB |
7539 | |
7540 | current->journal_info = outstanding_extents; | |
3266789f | 7541 | btrfs_free_reserved_data_space(inode, len); |
c3473e83 | 7542 | } |
4b46fce2 | 7543 | |
eb838e73 JB |
7544 | /* |
7545 | * In the case of write we need to clear and unlock the entire range, | |
7546 | * in the case of read we need to unlock only the end area that we | |
7547 | * aren't using if there is any left over space. | |
7548 | */ | |
24c03fa5 | 7549 | if (lockstart < lockend) { |
0934856d MX |
7550 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7551 | lockend, unlock_bits, 1, 0, | |
7552 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7553 | } else { |
eb838e73 | 7554 | free_extent_state(cached_state); |
24c03fa5 | 7555 | } |
eb838e73 | 7556 | |
4b46fce2 JB |
7557 | free_extent_map(em); |
7558 | ||
7559 | return 0; | |
eb838e73 JB |
7560 | |
7561 | unlock_err: | |
eb838e73 JB |
7562 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7563 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
e1cbbfa5 JB |
7564 | if (outstanding_extents) |
7565 | current->journal_info = outstanding_extents; | |
eb838e73 | 7566 | return ret; |
4b46fce2 JB |
7567 | } |
7568 | ||
8b110e39 MX |
7569 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7570 | int rw, int mirror_num) | |
7571 | { | |
7572 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7573 | int ret; | |
7574 | ||
7575 | BUG_ON(rw & REQ_WRITE); | |
7576 | ||
7577 | bio_get(bio); | |
7578 | ||
7579 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7580 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7581 | if (ret) | |
7582 | goto err; | |
7583 | ||
7584 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); | |
7585 | err: | |
7586 | bio_put(bio); | |
7587 | return ret; | |
7588 | } | |
7589 | ||
7590 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7591 | struct bio *failed_bio, | |
7592 | struct io_failure_record *failrec, | |
7593 | int failed_mirror) | |
7594 | { | |
7595 | int num_copies; | |
7596 | ||
7597 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7598 | failrec->logical, failrec->len); | |
7599 | if (num_copies == 1) { | |
7600 | /* | |
7601 | * we only have a single copy of the data, so don't bother with | |
7602 | * all the retry and error correction code that follows. no | |
7603 | * matter what the error is, it is very likely to persist. | |
7604 | */ | |
7605 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7606 | num_copies, failrec->this_mirror, failed_mirror); | |
7607 | return 0; | |
7608 | } | |
7609 | ||
7610 | failrec->failed_mirror = failed_mirror; | |
7611 | failrec->this_mirror++; | |
7612 | if (failrec->this_mirror == failed_mirror) | |
7613 | failrec->this_mirror++; | |
7614 | ||
7615 | if (failrec->this_mirror > num_copies) { | |
7616 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7617 | num_copies, failrec->this_mirror, failed_mirror); | |
7618 | return 0; | |
7619 | } | |
7620 | ||
7621 | return 1; | |
7622 | } | |
7623 | ||
7624 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
7625 | struct page *page, u64 start, u64 end, | |
7626 | int failed_mirror, bio_end_io_t *repair_endio, | |
7627 | void *repair_arg) | |
7628 | { | |
7629 | struct io_failure_record *failrec; | |
7630 | struct bio *bio; | |
7631 | int isector; | |
7632 | int read_mode; | |
7633 | int ret; | |
7634 | ||
7635 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
7636 | ||
7637 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7638 | if (ret) | |
7639 | return ret; | |
7640 | ||
7641 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7642 | failed_mirror); | |
7643 | if (!ret) { | |
7644 | free_io_failure(inode, failrec); | |
7645 | return -EIO; | |
7646 | } | |
7647 | ||
7648 | if (failed_bio->bi_vcnt > 1) | |
7649 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; | |
7650 | else | |
7651 | read_mode = READ_SYNC; | |
7652 | ||
7653 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7654 | isector >>= inode->i_sb->s_blocksize_bits; | |
7655 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
7656 | 0, isector, repair_endio, repair_arg); | |
7657 | if (!bio) { | |
7658 | free_io_failure(inode, failrec); | |
7659 | return -EIO; | |
7660 | } | |
7661 | ||
7662 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7663 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7664 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7665 | ||
7666 | ret = submit_dio_repair_bio(inode, bio, read_mode, | |
7667 | failrec->this_mirror); | |
7668 | if (ret) { | |
7669 | free_io_failure(inode, failrec); | |
7670 | bio_put(bio); | |
7671 | } | |
7672 | ||
7673 | return ret; | |
7674 | } | |
7675 | ||
7676 | struct btrfs_retry_complete { | |
7677 | struct completion done; | |
7678 | struct inode *inode; | |
7679 | u64 start; | |
7680 | int uptodate; | |
7681 | }; | |
7682 | ||
7683 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) | |
7684 | { | |
7685 | struct btrfs_retry_complete *done = bio->bi_private; | |
7686 | struct bio_vec *bvec; | |
7687 | int i; | |
7688 | ||
7689 | if (err) | |
7690 | goto end; | |
7691 | ||
7692 | done->uptodate = 1; | |
7693 | bio_for_each_segment_all(bvec, bio, i) | |
7694 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7695 | end: | |
7696 | complete(&done->done); | |
7697 | bio_put(bio); | |
7698 | } | |
7699 | ||
7700 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7701 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7702 | { |
2c30c71b | 7703 | struct bio_vec *bvec; |
8b110e39 | 7704 | struct btrfs_retry_complete done; |
4b46fce2 | 7705 | u64 start; |
2c30c71b | 7706 | int i; |
c1dc0896 | 7707 | int ret; |
4b46fce2 | 7708 | |
8b110e39 MX |
7709 | start = io_bio->logical; |
7710 | done.inode = inode; | |
7711 | ||
7712 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
7713 | try_again: | |
7714 | done.uptodate = 0; | |
7715 | done.start = start; | |
7716 | init_completion(&done.done); | |
7717 | ||
7718 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7719 | start + bvec->bv_len - 1, | |
7720 | io_bio->mirror_num, | |
7721 | btrfs_retry_endio_nocsum, &done); | |
7722 | if (ret) | |
7723 | return ret; | |
7724 | ||
7725 | wait_for_completion(&done.done); | |
7726 | ||
7727 | if (!done.uptodate) { | |
7728 | /* We might have another mirror, so try again */ | |
7729 | goto try_again; | |
7730 | } | |
7731 | ||
7732 | start += bvec->bv_len; | |
7733 | } | |
7734 | ||
7735 | return 0; | |
7736 | } | |
7737 | ||
7738 | static void btrfs_retry_endio(struct bio *bio, int err) | |
7739 | { | |
7740 | struct btrfs_retry_complete *done = bio->bi_private; | |
7741 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7742 | struct bio_vec *bvec; | |
7743 | int uptodate; | |
7744 | int ret; | |
7745 | int i; | |
7746 | ||
7747 | if (err) | |
7748 | goto end; | |
7749 | ||
7750 | uptodate = 1; | |
7751 | bio_for_each_segment_all(bvec, bio, i) { | |
7752 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
7753 | bvec->bv_page, 0, | |
7754 | done->start, bvec->bv_len); | |
7755 | if (!ret) | |
7756 | clean_io_failure(done->inode, done->start, | |
7757 | bvec->bv_page, 0); | |
7758 | else | |
7759 | uptodate = 0; | |
7760 | } | |
7761 | ||
7762 | done->uptodate = uptodate; | |
7763 | end: | |
7764 | complete(&done->done); | |
7765 | bio_put(bio); | |
7766 | } | |
7767 | ||
7768 | static int __btrfs_subio_endio_read(struct inode *inode, | |
7769 | struct btrfs_io_bio *io_bio, int err) | |
7770 | { | |
7771 | struct bio_vec *bvec; | |
7772 | struct btrfs_retry_complete done; | |
7773 | u64 start; | |
7774 | u64 offset = 0; | |
7775 | int i; | |
7776 | int ret; | |
dc380aea | 7777 | |
8b110e39 | 7778 | err = 0; |
c1dc0896 | 7779 | start = io_bio->logical; |
8b110e39 MX |
7780 | done.inode = inode; |
7781 | ||
c1dc0896 | 7782 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
dc380aea MX |
7783 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
7784 | 0, start, bvec->bv_len); | |
8b110e39 MX |
7785 | if (likely(!ret)) |
7786 | goto next; | |
7787 | try_again: | |
7788 | done.uptodate = 0; | |
7789 | done.start = start; | |
7790 | init_completion(&done.done); | |
7791 | ||
7792 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7793 | start + bvec->bv_len - 1, | |
7794 | io_bio->mirror_num, | |
7795 | btrfs_retry_endio, &done); | |
7796 | if (ret) { | |
7797 | err = ret; | |
7798 | goto next; | |
7799 | } | |
7800 | ||
7801 | wait_for_completion(&done.done); | |
7802 | ||
7803 | if (!done.uptodate) { | |
7804 | /* We might have another mirror, so try again */ | |
7805 | goto try_again; | |
7806 | } | |
7807 | next: | |
7808 | offset += bvec->bv_len; | |
4b46fce2 | 7809 | start += bvec->bv_len; |
2c30c71b | 7810 | } |
c1dc0896 MX |
7811 | |
7812 | return err; | |
7813 | } | |
7814 | ||
8b110e39 MX |
7815 | static int btrfs_subio_endio_read(struct inode *inode, |
7816 | struct btrfs_io_bio *io_bio, int err) | |
7817 | { | |
7818 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
7819 | ||
7820 | if (skip_csum) { | |
7821 | if (unlikely(err)) | |
7822 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
7823 | else | |
7824 | return 0; | |
7825 | } else { | |
7826 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
7827 | } | |
7828 | } | |
7829 | ||
c1dc0896 MX |
7830 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
7831 | { | |
7832 | struct btrfs_dio_private *dip = bio->bi_private; | |
7833 | struct inode *inode = dip->inode; | |
7834 | struct bio *dio_bio; | |
7835 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7836 | ||
8b110e39 MX |
7837 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
7838 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 7839 | |
4b46fce2 | 7840 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 7841 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 7842 | dio_bio = dip->dio_bio; |
4b46fce2 | 7843 | |
4b46fce2 | 7844 | kfree(dip); |
c0da7aa1 JB |
7845 | |
7846 | /* If we had a csum failure make sure to clear the uptodate flag */ | |
7847 | if (err) | |
9be3395b CM |
7848 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
7849 | dio_end_io(dio_bio, err); | |
23ea8e5a MX |
7850 | |
7851 | if (io_bio->end_io) | |
7852 | io_bio->end_io(io_bio, err); | |
9be3395b | 7853 | bio_put(bio); |
4b46fce2 JB |
7854 | } |
7855 | ||
7856 | static void btrfs_endio_direct_write(struct bio *bio, int err) | |
7857 | { | |
7858 | struct btrfs_dio_private *dip = bio->bi_private; | |
7859 | struct inode *inode = dip->inode; | |
7860 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b46fce2 | 7861 | struct btrfs_ordered_extent *ordered = NULL; |
163cf09c CM |
7862 | u64 ordered_offset = dip->logical_offset; |
7863 | u64 ordered_bytes = dip->bytes; | |
9be3395b | 7864 | struct bio *dio_bio; |
4b46fce2 JB |
7865 | int ret; |
7866 | ||
7867 | if (err) | |
7868 | goto out_done; | |
163cf09c CM |
7869 | again: |
7870 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
7871 | &ordered_offset, | |
5fd02043 | 7872 | ordered_bytes, !err); |
4b46fce2 | 7873 | if (!ret) |
163cf09c | 7874 | goto out_test; |
4b46fce2 | 7875 | |
9e0af237 LB |
7876 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
7877 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
7878 | btrfs_queue_work(root->fs_info->endio_write_workers, |
7879 | &ordered->work); | |
163cf09c CM |
7880 | out_test: |
7881 | /* | |
7882 | * our bio might span multiple ordered extents. If we haven't | |
7883 | * completed the accounting for the whole dio, go back and try again | |
7884 | */ | |
7885 | if (ordered_offset < dip->logical_offset + dip->bytes) { | |
7886 | ordered_bytes = dip->logical_offset + dip->bytes - | |
7887 | ordered_offset; | |
5fd02043 | 7888 | ordered = NULL; |
163cf09c CM |
7889 | goto again; |
7890 | } | |
4b46fce2 | 7891 | out_done: |
9be3395b | 7892 | dio_bio = dip->dio_bio; |
4b46fce2 | 7893 | |
4b46fce2 | 7894 | kfree(dip); |
c0da7aa1 JB |
7895 | |
7896 | /* If we had an error make sure to clear the uptodate flag */ | |
7897 | if (err) | |
9be3395b CM |
7898 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
7899 | dio_end_io(dio_bio, err); | |
7900 | bio_put(bio); | |
4b46fce2 JB |
7901 | } |
7902 | ||
eaf25d93 CM |
7903 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
7904 | struct bio *bio, int mirror_num, | |
7905 | unsigned long bio_flags, u64 offset) | |
7906 | { | |
7907 | int ret; | |
7908 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7909 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 7910 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
7911 | return 0; |
7912 | } | |
7913 | ||
e65e1535 MX |
7914 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
7915 | { | |
7916 | struct btrfs_dio_private *dip = bio->bi_private; | |
7917 | ||
8b110e39 MX |
7918 | if (err) |
7919 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
7920 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
7921 | btrfs_ino(dip->inode), bio->bi_rw, | |
7922 | (unsigned long long)bio->bi_iter.bi_sector, | |
7923 | bio->bi_iter.bi_size, err); | |
7924 | ||
7925 | if (dip->subio_endio) | |
7926 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
7927 | |
7928 | if (err) { | |
e65e1535 MX |
7929 | dip->errors = 1; |
7930 | ||
7931 | /* | |
7932 | * before atomic variable goto zero, we must make sure | |
7933 | * dip->errors is perceived to be set. | |
7934 | */ | |
4e857c58 | 7935 | smp_mb__before_atomic(); |
e65e1535 MX |
7936 | } |
7937 | ||
7938 | /* if there are more bios still pending for this dio, just exit */ | |
7939 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
7940 | goto out; | |
7941 | ||
9be3395b | 7942 | if (dip->errors) { |
e65e1535 | 7943 | bio_io_error(dip->orig_bio); |
9be3395b CM |
7944 | } else { |
7945 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); | |
e65e1535 MX |
7946 | bio_endio(dip->orig_bio, 0); |
7947 | } | |
7948 | out: | |
7949 | bio_put(bio); | |
7950 | } | |
7951 | ||
7952 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
7953 | u64 first_sector, gfp_t gfp_flags) | |
7954 | { | |
7955 | int nr_vecs = bio_get_nr_vecs(bdev); | |
7956 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); | |
7957 | } | |
7958 | ||
c1dc0896 MX |
7959 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
7960 | struct inode *inode, | |
7961 | struct btrfs_dio_private *dip, | |
7962 | struct bio *bio, | |
7963 | u64 file_offset) | |
7964 | { | |
7965 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7966 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
7967 | int ret; | |
7968 | ||
7969 | /* | |
7970 | * We load all the csum data we need when we submit | |
7971 | * the first bio to reduce the csum tree search and | |
7972 | * contention. | |
7973 | */ | |
7974 | if (dip->logical_offset == file_offset) { | |
7975 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
7976 | file_offset); | |
7977 | if (ret) | |
7978 | return ret; | |
7979 | } | |
7980 | ||
7981 | if (bio == dip->orig_bio) | |
7982 | return 0; | |
7983 | ||
7984 | file_offset -= dip->logical_offset; | |
7985 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
7986 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
7987 | ||
7988 | return 0; | |
7989 | } | |
7990 | ||
e65e1535 MX |
7991 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
7992 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 7993 | int async_submit) |
e65e1535 | 7994 | { |
facc8a22 | 7995 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
7996 | int write = rw & REQ_WRITE; |
7997 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7998 | int ret; | |
7999 | ||
b812ce28 JB |
8000 | if (async_submit) |
8001 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8002 | ||
e65e1535 | 8003 | bio_get(bio); |
5fd02043 JB |
8004 | |
8005 | if (!write) { | |
bfebd8b5 DS |
8006 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8007 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8008 | if (ret) |
8009 | goto err; | |
8010 | } | |
e65e1535 | 8011 | |
1ae39938 JB |
8012 | if (skip_sum) |
8013 | goto map; | |
8014 | ||
8015 | if (write && async_submit) { | |
e65e1535 MX |
8016 | ret = btrfs_wq_submit_bio(root->fs_info, |
8017 | inode, rw, bio, 0, 0, | |
8018 | file_offset, | |
8019 | __btrfs_submit_bio_start_direct_io, | |
8020 | __btrfs_submit_bio_done); | |
8021 | goto err; | |
1ae39938 JB |
8022 | } else if (write) { |
8023 | /* | |
8024 | * If we aren't doing async submit, calculate the csum of the | |
8025 | * bio now. | |
8026 | */ | |
8027 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8028 | if (ret) | |
8029 | goto err; | |
23ea8e5a | 8030 | } else { |
c1dc0896 MX |
8031 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8032 | file_offset); | |
c2db1073 TI |
8033 | if (ret) |
8034 | goto err; | |
8035 | } | |
1ae39938 JB |
8036 | map: |
8037 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
8038 | err: |
8039 | bio_put(bio); | |
8040 | return ret; | |
8041 | } | |
8042 | ||
8043 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
8044 | int skip_sum) | |
8045 | { | |
8046 | struct inode *inode = dip->inode; | |
8047 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8048 | struct bio *bio; |
8049 | struct bio *orig_bio = dip->orig_bio; | |
8050 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8051 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8052 | u64 file_offset = dip->logical_offset; |
8053 | u64 submit_len = 0; | |
8054 | u64 map_length; | |
8055 | int nr_pages = 0; | |
23ea8e5a | 8056 | int ret; |
1ae39938 | 8057 | int async_submit = 0; |
e65e1535 | 8058 | |
4f024f37 | 8059 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8060 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 | 8061 | &map_length, NULL, 0); |
7a5c3c9b | 8062 | if (ret) |
e65e1535 | 8063 | return -EIO; |
facc8a22 | 8064 | |
4f024f37 | 8065 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8066 | bio = orig_bio; |
c1dc0896 | 8067 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8068 | goto submit; |
8069 | } | |
8070 | ||
53b381b3 | 8071 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8072 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8073 | async_submit = 0; |
8074 | else | |
8075 | async_submit = 1; | |
8076 | ||
02f57c7a JB |
8077 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8078 | if (!bio) | |
8079 | return -ENOMEM; | |
7a5c3c9b | 8080 | |
02f57c7a JB |
8081 | bio->bi_private = dip; |
8082 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8083 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8084 | atomic_inc(&dip->pending_bios); |
8085 | ||
e65e1535 | 8086 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
ee39b432 | 8087 | if (map_length < submit_len + bvec->bv_len || |
e65e1535 | 8088 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
ee39b432 | 8089 | bvec->bv_offset) < bvec->bv_len) { |
e65e1535 MX |
8090 | /* |
8091 | * inc the count before we submit the bio so | |
8092 | * we know the end IO handler won't happen before | |
8093 | * we inc the count. Otherwise, the dip might get freed | |
8094 | * before we're done setting it up | |
8095 | */ | |
8096 | atomic_inc(&dip->pending_bios); | |
8097 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
8098 | file_offset, skip_sum, | |
c329861d | 8099 | async_submit); |
e65e1535 MX |
8100 | if (ret) { |
8101 | bio_put(bio); | |
8102 | atomic_dec(&dip->pending_bios); | |
8103 | goto out_err; | |
8104 | } | |
8105 | ||
e65e1535 MX |
8106 | start_sector += submit_len >> 9; |
8107 | file_offset += submit_len; | |
8108 | ||
8109 | submit_len = 0; | |
8110 | nr_pages = 0; | |
8111 | ||
8112 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8113 | start_sector, GFP_NOFS); | |
8114 | if (!bio) | |
8115 | goto out_err; | |
8116 | bio->bi_private = dip; | |
8117 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8118 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8119 | |
4f024f37 | 8120 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8121 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 8122 | start_sector << 9, |
e65e1535 MX |
8123 | &map_length, NULL, 0); |
8124 | if (ret) { | |
8125 | bio_put(bio); | |
8126 | goto out_err; | |
8127 | } | |
8128 | } else { | |
8129 | submit_len += bvec->bv_len; | |
67871254 | 8130 | nr_pages++; |
e65e1535 MX |
8131 | bvec++; |
8132 | } | |
8133 | } | |
8134 | ||
02f57c7a | 8135 | submit: |
e65e1535 | 8136 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 8137 | async_submit); |
e65e1535 MX |
8138 | if (!ret) |
8139 | return 0; | |
8140 | ||
8141 | bio_put(bio); | |
8142 | out_err: | |
8143 | dip->errors = 1; | |
8144 | /* | |
8145 | * before atomic variable goto zero, we must | |
8146 | * make sure dip->errors is perceived to be set. | |
8147 | */ | |
4e857c58 | 8148 | smp_mb__before_atomic(); |
e65e1535 MX |
8149 | if (atomic_dec_and_test(&dip->pending_bios)) |
8150 | bio_io_error(dip->orig_bio); | |
8151 | ||
8152 | /* bio_end_io() will handle error, so we needn't return it */ | |
8153 | return 0; | |
8154 | } | |
8155 | ||
9be3395b CM |
8156 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
8157 | struct inode *inode, loff_t file_offset) | |
4b46fce2 JB |
8158 | { |
8159 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8160 | struct btrfs_dio_private *dip; | |
9be3395b | 8161 | struct bio *io_bio; |
23ea8e5a | 8162 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8163 | int skip_sum; |
7b6d91da | 8164 | int write = rw & REQ_WRITE; |
4b46fce2 JB |
8165 | int ret = 0; |
8166 | ||
8167 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8168 | ||
9be3395b | 8169 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8170 | if (!io_bio) { |
8171 | ret = -ENOMEM; | |
8172 | goto free_ordered; | |
8173 | } | |
8174 | ||
c1dc0896 | 8175 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8176 | if (!dip) { |
8177 | ret = -ENOMEM; | |
9be3395b | 8178 | goto free_io_bio; |
4b46fce2 | 8179 | } |
4b46fce2 | 8180 | |
9be3395b | 8181 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8182 | dip->inode = inode; |
8183 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8184 | dip->bytes = dio_bio->bi_iter.bi_size; |
8185 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8186 | io_bio->bi_private = dip; |
9be3395b CM |
8187 | dip->orig_bio = io_bio; |
8188 | dip->dio_bio = dio_bio; | |
e65e1535 | 8189 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8190 | btrfs_bio = btrfs_io_bio(io_bio); |
8191 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8192 | |
c1dc0896 | 8193 | if (write) { |
9be3395b | 8194 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8195 | } else { |
9be3395b | 8196 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8197 | dip->subio_endio = btrfs_subio_endio_read; |
8198 | } | |
4b46fce2 | 8199 | |
e65e1535 MX |
8200 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
8201 | if (!ret) | |
eaf25d93 | 8202 | return; |
9be3395b | 8203 | |
23ea8e5a MX |
8204 | if (btrfs_bio->end_io) |
8205 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b CM |
8206 | free_io_bio: |
8207 | bio_put(io_bio); | |
8208 | ||
4b46fce2 JB |
8209 | free_ordered: |
8210 | /* | |
8211 | * If this is a write, we need to clean up the reserved space and kill | |
8212 | * the ordered extent. | |
8213 | */ | |
8214 | if (write) { | |
8215 | struct btrfs_ordered_extent *ordered; | |
955256f2 | 8216 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
4b46fce2 JB |
8217 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
8218 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) | |
8219 | btrfs_free_reserved_extent(root, ordered->start, | |
e570fd27 | 8220 | ordered->disk_len, 1); |
4b46fce2 JB |
8221 | btrfs_put_ordered_extent(ordered); |
8222 | btrfs_put_ordered_extent(ordered); | |
8223 | } | |
9be3395b | 8224 | bio_endio(dio_bio, ret); |
4b46fce2 JB |
8225 | } |
8226 | ||
5a5f79b5 | 8227 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
28060d5d | 8228 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8229 | { |
8230 | int seg; | |
a1b75f7d | 8231 | int i; |
5a5f79b5 CM |
8232 | unsigned blocksize_mask = root->sectorsize - 1; |
8233 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8234 | |
8235 | if (offset & blocksize_mask) | |
8236 | goto out; | |
8237 | ||
28060d5d AV |
8238 | if (iov_iter_alignment(iter) & blocksize_mask) |
8239 | goto out; | |
a1b75f7d | 8240 | |
28060d5d AV |
8241 | /* If this is a write we don't need to check anymore */ |
8242 | if (rw & WRITE) | |
8243 | return 0; | |
8244 | /* | |
8245 | * Check to make sure we don't have duplicate iov_base's in this | |
8246 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8247 | * when reading back. | |
8248 | */ | |
8249 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8250 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8251 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8252 | goto out; |
8253 | } | |
5a5f79b5 CM |
8254 | } |
8255 | retval = 0; | |
8256 | out: | |
8257 | return retval; | |
8258 | } | |
eb838e73 | 8259 | |
16432985 | 8260 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
d8d3d94b | 8261 | struct iov_iter *iter, loff_t offset) |
16432985 | 8262 | { |
4b46fce2 JB |
8263 | struct file *file = iocb->ki_filp; |
8264 | struct inode *inode = file->f_mapping->host; | |
e1cbbfa5 | 8265 | u64 outstanding_extents = 0; |
0934856d | 8266 | size_t count = 0; |
2e60a51e | 8267 | int flags = 0; |
38851cc1 MX |
8268 | bool wakeup = true; |
8269 | bool relock = false; | |
0934856d | 8270 | ssize_t ret; |
4b46fce2 | 8271 | |
28060d5d | 8272 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
5a5f79b5 | 8273 | return 0; |
3f7c579c | 8274 | |
38851cc1 | 8275 | atomic_inc(&inode->i_dio_count); |
4e857c58 | 8276 | smp_mb__after_atomic(); |
38851cc1 | 8277 | |
0e267c44 | 8278 | /* |
41bd9ca4 MX |
8279 | * The generic stuff only does filemap_write_and_wait_range, which |
8280 | * isn't enough if we've written compressed pages to this area, so | |
8281 | * we need to flush the dirty pages again to make absolutely sure | |
8282 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8283 | */ |
a6cbcd4a | 8284 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8285 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8286 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8287 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8288 | offset + count - 1); | |
0e267c44 | 8289 | |
0934856d | 8290 | if (rw & WRITE) { |
38851cc1 MX |
8291 | /* |
8292 | * If the write DIO is beyond the EOF, we need update | |
8293 | * the isize, but it is protected by i_mutex. So we can | |
8294 | * not unlock the i_mutex at this case. | |
8295 | */ | |
8296 | if (offset + count <= inode->i_size) { | |
8297 | mutex_unlock(&inode->i_mutex); | |
8298 | relock = true; | |
8299 | } | |
0934856d MX |
8300 | ret = btrfs_delalloc_reserve_space(inode, count); |
8301 | if (ret) | |
38851cc1 | 8302 | goto out; |
e1cbbfa5 JB |
8303 | outstanding_extents = div64_u64(count + |
8304 | BTRFS_MAX_EXTENT_SIZE - 1, | |
8305 | BTRFS_MAX_EXTENT_SIZE); | |
8306 | ||
8307 | /* | |
8308 | * We need to know how many extents we reserved so that we can | |
8309 | * do the accounting properly if we go over the number we | |
8310 | * originally calculated. Abuse current->journal_info for this. | |
8311 | */ | |
8312 | current->journal_info = &outstanding_extents; | |
ee39b432 DS |
8313 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8314 | &BTRFS_I(inode)->runtime_flags)) { | |
38851cc1 MX |
8315 | inode_dio_done(inode); |
8316 | flags = DIO_LOCKING | DIO_SKIP_HOLES; | |
8317 | wakeup = false; | |
0934856d MX |
8318 | } |
8319 | ||
8320 | ret = __blockdev_direct_IO(rw, iocb, inode, | |
8321 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
31b14039 | 8322 | iter, offset, btrfs_get_blocks_direct, NULL, |
2e60a51e | 8323 | btrfs_submit_direct, flags); |
0934856d | 8324 | if (rw & WRITE) { |
e1cbbfa5 | 8325 | current->journal_info = NULL; |
0934856d MX |
8326 | if (ret < 0 && ret != -EIOCBQUEUED) |
8327 | btrfs_delalloc_release_space(inode, count); | |
172a5049 | 8328 | else if (ret >= 0 && (size_t)ret < count) |
0934856d MX |
8329 | btrfs_delalloc_release_space(inode, |
8330 | count - (size_t)ret); | |
0934856d | 8331 | } |
38851cc1 | 8332 | out: |
2e60a51e MX |
8333 | if (wakeup) |
8334 | inode_dio_done(inode); | |
38851cc1 MX |
8335 | if (relock) |
8336 | mutex_lock(&inode->i_mutex); | |
0934856d MX |
8337 | |
8338 | return ret; | |
16432985 CM |
8339 | } |
8340 | ||
05dadc09 TI |
8341 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8342 | ||
1506fcc8 YS |
8343 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8344 | __u64 start, __u64 len) | |
8345 | { | |
05dadc09 TI |
8346 | int ret; |
8347 | ||
8348 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8349 | if (ret) | |
8350 | return ret; | |
8351 | ||
ec29ed5b | 8352 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8353 | } |
8354 | ||
a52d9a80 | 8355 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8356 | { |
d1310b2e CM |
8357 | struct extent_io_tree *tree; |
8358 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8359 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8360 | } |
1832a6d5 | 8361 | |
a52d9a80 | 8362 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8363 | { |
d1310b2e | 8364 | struct extent_io_tree *tree; |
b888db2b CM |
8365 | |
8366 | ||
8367 | if (current->flags & PF_MEMALLOC) { | |
8368 | redirty_page_for_writepage(wbc, page); | |
8369 | unlock_page(page); | |
8370 | return 0; | |
8371 | } | |
d1310b2e | 8372 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 8373 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
8374 | } |
8375 | ||
48a3b636 ES |
8376 | static int btrfs_writepages(struct address_space *mapping, |
8377 | struct writeback_control *wbc) | |
b293f02e | 8378 | { |
d1310b2e | 8379 | struct extent_io_tree *tree; |
771ed689 | 8380 | |
d1310b2e | 8381 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8382 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8383 | } | |
8384 | ||
3ab2fb5a CM |
8385 | static int |
8386 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8387 | struct list_head *pages, unsigned nr_pages) | |
8388 | { | |
d1310b2e CM |
8389 | struct extent_io_tree *tree; |
8390 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8391 | return extent_readpages(tree, mapping, pages, nr_pages, |
8392 | btrfs_get_extent); | |
8393 | } | |
e6dcd2dc | 8394 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8395 | { |
d1310b2e CM |
8396 | struct extent_io_tree *tree; |
8397 | struct extent_map_tree *map; | |
a52d9a80 | 8398 | int ret; |
8c2383c3 | 8399 | |
d1310b2e CM |
8400 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8401 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8402 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8403 | if (ret == 1) { |
8404 | ClearPagePrivate(page); | |
8405 | set_page_private(page, 0); | |
8406 | page_cache_release(page); | |
39279cc3 | 8407 | } |
a52d9a80 | 8408 | return ret; |
39279cc3 CM |
8409 | } |
8410 | ||
e6dcd2dc CM |
8411 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8412 | { | |
98509cfc CM |
8413 | if (PageWriteback(page) || PageDirty(page)) |
8414 | return 0; | |
b335b003 | 8415 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8416 | } |
8417 | ||
d47992f8 LC |
8418 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8419 | unsigned int length) | |
39279cc3 | 8420 | { |
5fd02043 | 8421 | struct inode *inode = page->mapping->host; |
d1310b2e | 8422 | struct extent_io_tree *tree; |
e6dcd2dc | 8423 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8424 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8425 | u64 page_start = page_offset(page); |
8426 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
131e404a | 8427 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8428 | |
8b62b72b CM |
8429 | /* |
8430 | * we have the page locked, so new writeback can't start, | |
8431 | * and the dirty bit won't be cleared while we are here. | |
8432 | * | |
8433 | * Wait for IO on this page so that we can safely clear | |
8434 | * the PagePrivate2 bit and do ordered accounting | |
8435 | */ | |
e6dcd2dc | 8436 | wait_on_page_writeback(page); |
8b62b72b | 8437 | |
5fd02043 | 8438 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8439 | if (offset) { |
8440 | btrfs_releasepage(page, GFP_NOFS); | |
8441 | return; | |
8442 | } | |
131e404a FDBM |
8443 | |
8444 | if (!inode_evicting) | |
8445 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); | |
8446 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
e6dcd2dc | 8447 | if (ordered) { |
eb84ae03 CM |
8448 | /* |
8449 | * IO on this page will never be started, so we need | |
8450 | * to account for any ordered extents now | |
8451 | */ | |
131e404a FDBM |
8452 | if (!inode_evicting) |
8453 | clear_extent_bit(tree, page_start, page_end, | |
8454 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
8455 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8456 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8457 | GFP_NOFS); | |
8b62b72b CM |
8458 | /* |
8459 | * whoever cleared the private bit is responsible | |
8460 | * for the finish_ordered_io | |
8461 | */ | |
77cef2ec JB |
8462 | if (TestClearPagePrivate2(page)) { |
8463 | struct btrfs_ordered_inode_tree *tree; | |
8464 | u64 new_len; | |
8465 | ||
8466 | tree = &BTRFS_I(inode)->ordered_tree; | |
8467 | ||
8468 | spin_lock_irq(&tree->lock); | |
8469 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8470 | new_len = page_start - ordered->file_offset; | |
8471 | if (new_len < ordered->truncated_len) | |
8472 | ordered->truncated_len = new_len; | |
8473 | spin_unlock_irq(&tree->lock); | |
8474 | ||
8475 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
8476 | page_start, | |
8477 | PAGE_CACHE_SIZE, 1)) | |
8478 | btrfs_finish_ordered_io(ordered); | |
8b62b72b | 8479 | } |
e6dcd2dc | 8480 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8481 | if (!inode_evicting) { |
8482 | cached_state = NULL; | |
8483 | lock_extent_bits(tree, page_start, page_end, 0, | |
8484 | &cached_state); | |
8485 | } | |
8486 | } | |
8487 | ||
8488 | if (!inode_evicting) { | |
8489 | clear_extent_bit(tree, page_start, page_end, | |
8490 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8491 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8492 | EXTENT_DEFRAG, 1, 1, | |
8493 | &cached_state, GFP_NOFS); | |
8494 | ||
8495 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8496 | } |
e6dcd2dc | 8497 | |
4a096752 | 8498 | ClearPageChecked(page); |
9ad6b7bc | 8499 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8500 | ClearPagePrivate(page); |
8501 | set_page_private(page, 0); | |
8502 | page_cache_release(page); | |
8503 | } | |
39279cc3 CM |
8504 | } |
8505 | ||
9ebefb18 CM |
8506 | /* |
8507 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8508 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8509 | * be careful to check for EOF conditions here. We set the page up correctly | |
8510 | * for a written page which means we get ENOSPC checking when writing into | |
8511 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8512 | * support these features. | |
8513 | * | |
8514 | * We are not allowed to take the i_mutex here so we have to play games to | |
8515 | * protect against truncate races as the page could now be beyond EOF. Because | |
8516 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8517 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8518 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8519 | * unlock the page. | |
8520 | */ | |
c2ec175c | 8521 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8522 | { |
c2ec175c | 8523 | struct page *page = vmf->page; |
496ad9aa | 8524 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8525 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8526 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8527 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8528 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8529 | char *kaddr; |
8530 | unsigned long zero_start; | |
9ebefb18 | 8531 | loff_t size; |
1832a6d5 | 8532 | int ret; |
9998eb70 | 8533 | int reserved = 0; |
a52d9a80 | 8534 | u64 page_start; |
e6dcd2dc | 8535 | u64 page_end; |
9ebefb18 | 8536 | |
b2b5ef5c | 8537 | sb_start_pagefault(inode->i_sb); |
0ca1f7ce | 8538 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8539 | if (!ret) { |
e41f941a | 8540 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8541 | reserved = 1; |
8542 | } | |
56a76f82 NP |
8543 | if (ret) { |
8544 | if (ret == -ENOMEM) | |
8545 | ret = VM_FAULT_OOM; | |
8546 | else /* -ENOSPC, -EIO, etc */ | |
8547 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8548 | if (reserved) |
8549 | goto out; | |
8550 | goto out_noreserve; | |
56a76f82 | 8551 | } |
1832a6d5 | 8552 | |
56a76f82 | 8553 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8554 | again: |
9ebefb18 | 8555 | lock_page(page); |
9ebefb18 | 8556 | size = i_size_read(inode); |
e6dcd2dc CM |
8557 | page_start = page_offset(page); |
8558 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 8559 | |
9ebefb18 | 8560 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8561 | (page_start >= size)) { |
9ebefb18 CM |
8562 | /* page got truncated out from underneath us */ |
8563 | goto out_unlock; | |
8564 | } | |
e6dcd2dc CM |
8565 | wait_on_page_writeback(page); |
8566 | ||
d0082371 | 8567 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
8568 | set_page_extent_mapped(page); |
8569 | ||
eb84ae03 CM |
8570 | /* |
8571 | * we can't set the delalloc bits if there are pending ordered | |
8572 | * extents. Drop our locks and wait for them to finish | |
8573 | */ | |
e6dcd2dc CM |
8574 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
8575 | if (ordered) { | |
2ac55d41 JB |
8576 | unlock_extent_cached(io_tree, page_start, page_end, |
8577 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 8578 | unlock_page(page); |
eb84ae03 | 8579 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8580 | btrfs_put_ordered_extent(ordered); |
8581 | goto again; | |
8582 | } | |
8583 | ||
fbf19087 JB |
8584 | /* |
8585 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
8586 | * if it was already dirty, so for space accounting reasons we need to | |
8587 | * clear any delalloc bits for the range we are fixing to save. There | |
8588 | * is probably a better way to do this, but for now keep consistent with | |
8589 | * prepare_pages in the normal write path. | |
8590 | */ | |
2ac55d41 | 8591 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
8592 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8593 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 8594 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 8595 | |
2ac55d41 JB |
8596 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
8597 | &cached_state); | |
9ed74f2d | 8598 | if (ret) { |
2ac55d41 JB |
8599 | unlock_extent_cached(io_tree, page_start, page_end, |
8600 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
8601 | ret = VM_FAULT_SIGBUS; |
8602 | goto out_unlock; | |
8603 | } | |
e6dcd2dc | 8604 | ret = 0; |
9ebefb18 CM |
8605 | |
8606 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 8607 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 8608 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 8609 | else |
e6dcd2dc | 8610 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 8611 | |
e6dcd2dc CM |
8612 | if (zero_start != PAGE_CACHE_SIZE) { |
8613 | kaddr = kmap(page); | |
8614 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
8615 | flush_dcache_page(page); | |
8616 | kunmap(page); | |
8617 | } | |
247e743c | 8618 | ClearPageChecked(page); |
e6dcd2dc | 8619 | set_page_dirty(page); |
50a9b214 | 8620 | SetPageUptodate(page); |
5a3f23d5 | 8621 | |
257c62e1 CM |
8622 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
8623 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 8624 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8625 | |
2ac55d41 | 8626 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
8627 | |
8628 | out_unlock: | |
b2b5ef5c JK |
8629 | if (!ret) { |
8630 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 8631 | return VM_FAULT_LOCKED; |
b2b5ef5c | 8632 | } |
9ebefb18 | 8633 | unlock_page(page); |
1832a6d5 | 8634 | out: |
ec39e180 | 8635 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8636 | out_noreserve: |
b2b5ef5c | 8637 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
8638 | return ret; |
8639 | } | |
8640 | ||
a41ad394 | 8641 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
8642 | { |
8643 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 8644 | struct btrfs_block_rsv *rsv; |
a71754fc | 8645 | int ret = 0; |
3893e33b | 8646 | int err = 0; |
39279cc3 | 8647 | struct btrfs_trans_handle *trans; |
dbe674a9 | 8648 | u64 mask = root->sectorsize - 1; |
07127184 | 8649 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 8650 | |
0ef8b726 JB |
8651 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
8652 | (u64)-1); | |
8653 | if (ret) | |
8654 | return ret; | |
39279cc3 | 8655 | |
fcb80c2a JB |
8656 | /* |
8657 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have | |
8658 | * 3 things going on here | |
8659 | * | |
8660 | * 1) We need to reserve space for our orphan item and the space to | |
8661 | * delete our orphan item. Lord knows we don't want to have a dangling | |
8662 | * orphan item because we didn't reserve space to remove it. | |
8663 | * | |
8664 | * 2) We need to reserve space to update our inode. | |
8665 | * | |
8666 | * 3) We need to have something to cache all the space that is going to | |
8667 | * be free'd up by the truncate operation, but also have some slack | |
8668 | * space reserved in case it uses space during the truncate (thank you | |
8669 | * very much snapshotting). | |
8670 | * | |
8671 | * And we need these to all be seperate. The fact is we can use alot of | |
8672 | * space doing the truncate, and we have no earthly idea how much space | |
8673 | * we will use, so we need the truncate reservation to be seperate so it | |
8674 | * doesn't end up using space reserved for updating the inode or | |
8675 | * removing the orphan item. We also need to be able to stop the | |
8676 | * transaction and start a new one, which means we need to be able to | |
8677 | * update the inode several times, and we have no idea of knowing how | |
8678 | * many times that will be, so we can't just reserve 1 item for the | |
8679 | * entirety of the opration, so that has to be done seperately as well. | |
8680 | * Then there is the orphan item, which does indeed need to be held on | |
8681 | * to for the whole operation, and we need nobody to touch this reserved | |
8682 | * space except the orphan code. | |
8683 | * | |
8684 | * So that leaves us with | |
8685 | * | |
8686 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
8687 | * 2) rsv - for the truncate reservation, which we will steal from the | |
8688 | * transaction reservation. | |
8689 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
8690 | * updating the inode. | |
8691 | */ | |
66d8f3dd | 8692 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8693 | if (!rsv) |
8694 | return -ENOMEM; | |
4a338542 | 8695 | rsv->size = min_size; |
ca7e70f5 | 8696 | rsv->failfast = 1; |
f0cd846e | 8697 | |
907cbceb | 8698 | /* |
07127184 | 8699 | * 1 for the truncate slack space |
907cbceb JB |
8700 | * 1 for updating the inode. |
8701 | */ | |
f3fe820c | 8702 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
8703 | if (IS_ERR(trans)) { |
8704 | err = PTR_ERR(trans); | |
8705 | goto out; | |
8706 | } | |
f0cd846e | 8707 | |
907cbceb JB |
8708 | /* Migrate the slack space for the truncate to our reserve */ |
8709 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
8710 | min_size); | |
fcb80c2a | 8711 | BUG_ON(ret); |
f0cd846e | 8712 | |
5dc562c5 JB |
8713 | /* |
8714 | * So if we truncate and then write and fsync we normally would just | |
8715 | * write the extents that changed, which is a problem if we need to | |
8716 | * first truncate that entire inode. So set this flag so we write out | |
8717 | * all of the extents in the inode to the sync log so we're completely | |
8718 | * safe. | |
8719 | */ | |
8720 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8721 | trans->block_rsv = rsv; |
907cbceb | 8722 | |
8082510e YZ |
8723 | while (1) { |
8724 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8725 | inode->i_size, | |
8726 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 8727 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 8728 | err = ret; |
8082510e | 8729 | break; |
3893e33b | 8730 | } |
39279cc3 | 8731 | |
fcb80c2a | 8732 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 8733 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
8734 | if (ret) { |
8735 | err = ret; | |
8736 | break; | |
8737 | } | |
ca7e70f5 | 8738 | |
8082510e | 8739 | btrfs_end_transaction(trans, root); |
b53d3f5d | 8740 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
8741 | |
8742 | trans = btrfs_start_transaction(root, 2); | |
8743 | if (IS_ERR(trans)) { | |
8744 | ret = err = PTR_ERR(trans); | |
8745 | trans = NULL; | |
8746 | break; | |
8747 | } | |
8748 | ||
8749 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
8750 | rsv, min_size); | |
8751 | BUG_ON(ret); /* shouldn't happen */ | |
8752 | trans->block_rsv = rsv; | |
8082510e YZ |
8753 | } |
8754 | ||
8755 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 8756 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 8757 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
8758 | if (ret) |
8759 | err = ret; | |
8082510e YZ |
8760 | } |
8761 | ||
917c16b2 CM |
8762 | if (trans) { |
8763 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
8764 | ret = btrfs_update_inode(trans, root, inode); | |
8765 | if (ret && !err) | |
8766 | err = ret; | |
7b128766 | 8767 | |
7ad85bb7 | 8768 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 8769 | btrfs_btree_balance_dirty(root); |
917c16b2 | 8770 | } |
fcb80c2a JB |
8771 | |
8772 | out: | |
8773 | btrfs_free_block_rsv(root, rsv); | |
8774 | ||
3893e33b JB |
8775 | if (ret && !err) |
8776 | err = ret; | |
a41ad394 | 8777 | |
3893e33b | 8778 | return err; |
39279cc3 CM |
8779 | } |
8780 | ||
d352ac68 CM |
8781 | /* |
8782 | * create a new subvolume directory/inode (helper for the ioctl). | |
8783 | */ | |
d2fb3437 | 8784 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8785 | struct btrfs_root *new_root, |
8786 | struct btrfs_root *parent_root, | |
8787 | u64 new_dirid) | |
39279cc3 | 8788 | { |
39279cc3 | 8789 | struct inode *inode; |
76dda93c | 8790 | int err; |
00e4e6b3 | 8791 | u64 index = 0; |
39279cc3 | 8792 | |
12fc9d09 FA |
8793 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8794 | new_dirid, new_dirid, | |
8795 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8796 | &index); | |
54aa1f4d | 8797 | if (IS_ERR(inode)) |
f46b5a66 | 8798 | return PTR_ERR(inode); |
39279cc3 CM |
8799 | inode->i_op = &btrfs_dir_inode_operations; |
8800 | inode->i_fop = &btrfs_dir_file_operations; | |
8801 | ||
bfe86848 | 8802 | set_nlink(inode, 1); |
dbe674a9 | 8803 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 8804 | unlock_new_inode(inode); |
3b96362c | 8805 | |
63541927 FDBM |
8806 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8807 | if (err) | |
8808 | btrfs_err(new_root->fs_info, | |
351fd353 | 8809 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8810 | new_root->root_key.objectid, err); |
8811 | ||
76dda93c | 8812 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8813 | |
76dda93c | 8814 | iput(inode); |
ce598979 | 8815 | return err; |
39279cc3 CM |
8816 | } |
8817 | ||
39279cc3 CM |
8818 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8819 | { | |
8820 | struct btrfs_inode *ei; | |
2ead6ae7 | 8821 | struct inode *inode; |
39279cc3 CM |
8822 | |
8823 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
8824 | if (!ei) | |
8825 | return NULL; | |
2ead6ae7 YZ |
8826 | |
8827 | ei->root = NULL; | |
2ead6ae7 | 8828 | ei->generation = 0; |
15ee9bc7 | 8829 | ei->last_trans = 0; |
257c62e1 | 8830 | ei->last_sub_trans = 0; |
e02119d5 | 8831 | ei->logged_trans = 0; |
2ead6ae7 | 8832 | ei->delalloc_bytes = 0; |
47059d93 | 8833 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8834 | ei->disk_i_size = 0; |
8835 | ei->flags = 0; | |
7709cde3 | 8836 | ei->csum_bytes = 0; |
2ead6ae7 | 8837 | ei->index_cnt = (u64)-1; |
67de1176 | 8838 | ei->dir_index = 0; |
2ead6ae7 | 8839 | ei->last_unlink_trans = 0; |
46d8bc34 | 8840 | ei->last_log_commit = 0; |
2ead6ae7 | 8841 | |
9e0baf60 JB |
8842 | spin_lock_init(&ei->lock); |
8843 | ei->outstanding_extents = 0; | |
8844 | ei->reserved_extents = 0; | |
2ead6ae7 | 8845 | |
72ac3c0d | 8846 | ei->runtime_flags = 0; |
261507a0 | 8847 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8848 | |
16cdcec7 MX |
8849 | ei->delayed_node = NULL; |
8850 | ||
9cc97d64 | 8851 | ei->i_otime.tv_sec = 0; |
8852 | ei->i_otime.tv_nsec = 0; | |
8853 | ||
2ead6ae7 | 8854 | inode = &ei->vfs_inode; |
a8067e02 | 8855 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
8856 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
8857 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
8858 | ei->io_tree.track_uptodate = 1; |
8859 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 8860 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8861 | mutex_init(&ei->log_mutex); |
f248679e | 8862 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 8863 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8864 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
2ead6ae7 YZ |
8865 | RB_CLEAR_NODE(&ei->rb_node); |
8866 | ||
8867 | return inode; | |
39279cc3 CM |
8868 | } |
8869 | ||
aaedb55b JB |
8870 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8871 | void btrfs_test_destroy_inode(struct inode *inode) | |
8872 | { | |
8873 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
8874 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
8875 | } | |
8876 | #endif | |
8877 | ||
fa0d7e3d NP |
8878 | static void btrfs_i_callback(struct rcu_head *head) |
8879 | { | |
8880 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
8881 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8882 | } | |
8883 | ||
39279cc3 CM |
8884 | void btrfs_destroy_inode(struct inode *inode) |
8885 | { | |
e6dcd2dc | 8886 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
8887 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8888 | ||
b3d9b7a3 | 8889 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 8890 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
8891 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
8892 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
8893 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
8894 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 8895 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 8896 | |
a6dbd429 JB |
8897 | /* |
8898 | * This can happen where we create an inode, but somebody else also | |
8899 | * created the same inode and we need to destroy the one we already | |
8900 | * created. | |
8901 | */ | |
8902 | if (!root) | |
8903 | goto free; | |
8904 | ||
8a35d95f JB |
8905 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8906 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 8907 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 8908 | btrfs_ino(inode)); |
8a35d95f | 8909 | atomic_dec(&root->orphan_inodes); |
7b128766 | 8910 | } |
7b128766 | 8911 | |
d397712b | 8912 | while (1) { |
e6dcd2dc CM |
8913 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
8914 | if (!ordered) | |
8915 | break; | |
8916 | else { | |
c2cf52eb | 8917 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 8918 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
8919 | btrfs_remove_ordered_extent(inode, ordered); |
8920 | btrfs_put_ordered_extent(ordered); | |
8921 | btrfs_put_ordered_extent(ordered); | |
8922 | } | |
8923 | } | |
5d4f98a2 | 8924 | inode_tree_del(inode); |
5b21f2ed | 8925 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 8926 | free: |
fa0d7e3d | 8927 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
8928 | } |
8929 | ||
45321ac5 | 8930 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8931 | { |
8932 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8933 | |
6379ef9f NA |
8934 | if (root == NULL) |
8935 | return 1; | |
8936 | ||
fa6ac876 | 8937 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8938 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8939 | return 1; |
76dda93c | 8940 | else |
45321ac5 | 8941 | return generic_drop_inode(inode); |
76dda93c YZ |
8942 | } |
8943 | ||
0ee0fda0 | 8944 | static void init_once(void *foo) |
39279cc3 CM |
8945 | { |
8946 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8947 | ||
8948 | inode_init_once(&ei->vfs_inode); | |
8949 | } | |
8950 | ||
8951 | void btrfs_destroy_cachep(void) | |
8952 | { | |
8c0a8537 KS |
8953 | /* |
8954 | * Make sure all delayed rcu free inodes are flushed before we | |
8955 | * destroy cache. | |
8956 | */ | |
8957 | rcu_barrier(); | |
39279cc3 CM |
8958 | if (btrfs_inode_cachep) |
8959 | kmem_cache_destroy(btrfs_inode_cachep); | |
8960 | if (btrfs_trans_handle_cachep) | |
8961 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
8962 | if (btrfs_transaction_cachep) | |
8963 | kmem_cache_destroy(btrfs_transaction_cachep); | |
39279cc3 CM |
8964 | if (btrfs_path_cachep) |
8965 | kmem_cache_destroy(btrfs_path_cachep); | |
dc89e982 JB |
8966 | if (btrfs_free_space_cachep) |
8967 | kmem_cache_destroy(btrfs_free_space_cachep); | |
8ccf6f19 MX |
8968 | if (btrfs_delalloc_work_cachep) |
8969 | kmem_cache_destroy(btrfs_delalloc_work_cachep); | |
39279cc3 CM |
8970 | } |
8971 | ||
8972 | int btrfs_init_cachep(void) | |
8973 | { | |
837e1972 | 8974 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 CH |
8975 | sizeof(struct btrfs_inode), 0, |
8976 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); | |
39279cc3 CM |
8977 | if (!btrfs_inode_cachep) |
8978 | goto fail; | |
9601e3f6 | 8979 | |
837e1972 | 8980 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 CH |
8981 | sizeof(struct btrfs_trans_handle), 0, |
8982 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8983 | if (!btrfs_trans_handle_cachep) |
8984 | goto fail; | |
9601e3f6 | 8985 | |
837e1972 | 8986 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 CH |
8987 | sizeof(struct btrfs_transaction), 0, |
8988 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8989 | if (!btrfs_transaction_cachep) |
8990 | goto fail; | |
9601e3f6 | 8991 | |
837e1972 | 8992 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 CH |
8993 | sizeof(struct btrfs_path), 0, |
8994 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8995 | if (!btrfs_path_cachep) |
8996 | goto fail; | |
9601e3f6 | 8997 | |
837e1972 | 8998 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 JB |
8999 | sizeof(struct btrfs_free_space), 0, |
9000 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
9001 | if (!btrfs_free_space_cachep) | |
9002 | goto fail; | |
9003 | ||
8ccf6f19 MX |
9004 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
9005 | sizeof(struct btrfs_delalloc_work), 0, | |
9006 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, | |
9007 | NULL); | |
9008 | if (!btrfs_delalloc_work_cachep) | |
9009 | goto fail; | |
9010 | ||
39279cc3 CM |
9011 | return 0; |
9012 | fail: | |
9013 | btrfs_destroy_cachep(); | |
9014 | return -ENOMEM; | |
9015 | } | |
9016 | ||
9017 | static int btrfs_getattr(struct vfsmount *mnt, | |
9018 | struct dentry *dentry, struct kstat *stat) | |
9019 | { | |
df0af1a5 | 9020 | u64 delalloc_bytes; |
39279cc3 | 9021 | struct inode *inode = dentry->d_inode; |
fadc0d8b DS |
9022 | u32 blocksize = inode->i_sb->s_blocksize; |
9023 | ||
39279cc3 | 9024 | generic_fillattr(inode, stat); |
0ee5dc67 | 9025 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
d6667462 | 9026 | stat->blksize = PAGE_CACHE_SIZE; |
df0af1a5 MX |
9027 | |
9028 | spin_lock(&BTRFS_I(inode)->lock); | |
9029 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9030 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9031 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9032 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9033 | return 0; |
9034 | } | |
9035 | ||
d397712b CM |
9036 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9037 | struct inode *new_dir, struct dentry *new_dentry) | |
39279cc3 CM |
9038 | { |
9039 | struct btrfs_trans_handle *trans; | |
9040 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4df27c4d | 9041 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
39279cc3 CM |
9042 | struct inode *new_inode = new_dentry->d_inode; |
9043 | struct inode *old_inode = old_dentry->d_inode; | |
9044 | struct timespec ctime = CURRENT_TIME; | |
00e4e6b3 | 9045 | u64 index = 0; |
4df27c4d | 9046 | u64 root_objectid; |
39279cc3 | 9047 | int ret; |
33345d01 | 9048 | u64 old_ino = btrfs_ino(old_inode); |
39279cc3 | 9049 | |
33345d01 | 9050 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9051 | return -EPERM; |
9052 | ||
4df27c4d | 9053 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9054 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9055 | return -EXDEV; |
9056 | ||
33345d01 LZ |
9057 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9058 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9059 | return -ENOTEMPTY; |
5f39d397 | 9060 | |
4df27c4d YZ |
9061 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9062 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9063 | return -ENOTEMPTY; | |
9c52057c CM |
9064 | |
9065 | ||
9066 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9067 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9068 | new_dentry->d_name.name, |
9069 | new_dentry->d_name.len); | |
9070 | ||
9071 | if (ret) { | |
9072 | if (ret == -EEXIST) { | |
9073 | /* we shouldn't get | |
9074 | * eexist without a new_inode */ | |
fae7f21c | 9075 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9076 | return ret; |
9077 | } | |
9078 | } else { | |
9079 | /* maybe -EOVERFLOW */ | |
9080 | return ret; | |
9081 | } | |
9082 | } | |
9083 | ret = 0; | |
9084 | ||
5a3f23d5 | 9085 | /* |
8d875f95 CM |
9086 | * we're using rename to replace one file with another. Start IO on it |
9087 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9088 | */ |
8d875f95 | 9089 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9090 | filemap_flush(old_inode->i_mapping); |
9091 | ||
76dda93c | 9092 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9093 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9094 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9095 | /* |
9096 | * We want to reserve the absolute worst case amount of items. So if | |
9097 | * both inodes are subvols and we need to unlink them then that would | |
9098 | * require 4 item modifications, but if they are both normal inodes it | |
9099 | * would require 5 item modifications, so we'll assume their normal | |
9100 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
9101 | * should cover the worst case number of items we'll modify. | |
9102 | */ | |
6e137ed3 | 9103 | trans = btrfs_start_transaction(root, 11); |
b44c59a8 JL |
9104 | if (IS_ERR(trans)) { |
9105 | ret = PTR_ERR(trans); | |
9106 | goto out_notrans; | |
9107 | } | |
76dda93c | 9108 | |
4df27c4d YZ |
9109 | if (dest != root) |
9110 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9111 | |
a5719521 YZ |
9112 | ret = btrfs_set_inode_index(new_dir, &index); |
9113 | if (ret) | |
9114 | goto out_fail; | |
5a3f23d5 | 9115 | |
67de1176 | 9116 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9117 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9118 | /* force full log commit if subvolume involved. */ |
995946dd | 9119 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9120 | } else { |
a5719521 YZ |
9121 | ret = btrfs_insert_inode_ref(trans, dest, |
9122 | new_dentry->d_name.name, | |
9123 | new_dentry->d_name.len, | |
33345d01 LZ |
9124 | old_ino, |
9125 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9126 | if (ret) |
9127 | goto out_fail; | |
4df27c4d YZ |
9128 | /* |
9129 | * this is an ugly little race, but the rename is required | |
9130 | * to make sure that if we crash, the inode is either at the | |
9131 | * old name or the new one. pinning the log transaction lets | |
9132 | * us make sure we don't allow a log commit to come in after | |
9133 | * we unlink the name but before we add the new name back in. | |
9134 | */ | |
9135 | btrfs_pin_log_trans(root); | |
9136 | } | |
5a3f23d5 | 9137 | |
0c4d2d95 JB |
9138 | inode_inc_iversion(old_dir); |
9139 | inode_inc_iversion(new_dir); | |
9140 | inode_inc_iversion(old_inode); | |
39279cc3 CM |
9141 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
9142 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9143 | old_inode->i_ctime = ctime; | |
5f39d397 | 9144 | |
12fcfd22 CM |
9145 | if (old_dentry->d_parent != new_dentry->d_parent) |
9146 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9147 | ||
33345d01 | 9148 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9149 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9150 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9151 | old_dentry->d_name.name, | |
9152 | old_dentry->d_name.len); | |
9153 | } else { | |
92986796 AV |
9154 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
9155 | old_dentry->d_inode, | |
9156 | old_dentry->d_name.name, | |
9157 | old_dentry->d_name.len); | |
9158 | if (!ret) | |
9159 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9160 | } |
79787eaa JM |
9161 | if (ret) { |
9162 | btrfs_abort_transaction(trans, root, ret); | |
9163 | goto out_fail; | |
9164 | } | |
39279cc3 CM |
9165 | |
9166 | if (new_inode) { | |
0c4d2d95 | 9167 | inode_inc_iversion(new_inode); |
39279cc3 | 9168 | new_inode->i_ctime = CURRENT_TIME; |
33345d01 | 9169 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9170 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9171 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9172 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9173 | root_objectid, | |
9174 | new_dentry->d_name.name, | |
9175 | new_dentry->d_name.len); | |
9176 | BUG_ON(new_inode->i_nlink == 0); | |
9177 | } else { | |
9178 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
9179 | new_dentry->d_inode, | |
9180 | new_dentry->d_name.name, | |
9181 | new_dentry->d_name.len); | |
9182 | } | |
4ef31a45 | 9183 | if (!ret && new_inode->i_nlink == 0) |
e02119d5 | 9184 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
79787eaa JM |
9185 | if (ret) { |
9186 | btrfs_abort_transaction(trans, root, ret); | |
9187 | goto out_fail; | |
9188 | } | |
39279cc3 | 9189 | } |
aec7477b | 9190 | |
4df27c4d YZ |
9191 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9192 | new_dentry->d_name.name, | |
a5719521 | 9193 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
9194 | if (ret) { |
9195 | btrfs_abort_transaction(trans, root, ret); | |
9196 | goto out_fail; | |
9197 | } | |
39279cc3 | 9198 | |
67de1176 MX |
9199 | if (old_inode->i_nlink == 1) |
9200 | BTRFS_I(old_inode)->dir_index = index; | |
9201 | ||
33345d01 | 9202 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
10d9f309 | 9203 | struct dentry *parent = new_dentry->d_parent; |
6a912213 | 9204 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d YZ |
9205 | btrfs_end_log_trans(root); |
9206 | } | |
39279cc3 | 9207 | out_fail: |
7ad85bb7 | 9208 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9209 | out_notrans: |
33345d01 | 9210 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9211 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9212 | |
39279cc3 CM |
9213 | return ret; |
9214 | } | |
9215 | ||
80ace85c MS |
9216 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9217 | struct inode *new_dir, struct dentry *new_dentry, | |
9218 | unsigned int flags) | |
9219 | { | |
9220 | if (flags & ~RENAME_NOREPLACE) | |
9221 | return -EINVAL; | |
9222 | ||
9223 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); | |
9224 | } | |
9225 | ||
8ccf6f19 MX |
9226 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9227 | { | |
9228 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9229 | struct inode *inode; |
8ccf6f19 MX |
9230 | |
9231 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9232 | work); | |
9f23e289 JB |
9233 | inode = delalloc_work->inode; |
9234 | if (delalloc_work->wait) { | |
9235 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
9236 | } else { | |
9237 | filemap_flush(inode->i_mapping); | |
9238 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9239 | &BTRFS_I(inode)->runtime_flags)) | |
9240 | filemap_flush(inode->i_mapping); | |
9241 | } | |
8ccf6f19 MX |
9242 | |
9243 | if (delalloc_work->delay_iput) | |
9f23e289 | 9244 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 9245 | else |
9f23e289 | 9246 | iput(inode); |
8ccf6f19 MX |
9247 | complete(&delalloc_work->completion); |
9248 | } | |
9249 | ||
9250 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
9251 | int wait, int delay_iput) | |
9252 | { | |
9253 | struct btrfs_delalloc_work *work; | |
9254 | ||
9255 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); | |
9256 | if (!work) | |
9257 | return NULL; | |
9258 | ||
9259 | init_completion(&work->completion); | |
9260 | INIT_LIST_HEAD(&work->list); | |
9261 | work->inode = inode; | |
9262 | work->wait = wait; | |
9263 | work->delay_iput = delay_iput; | |
9e0af237 LB |
9264 | WARN_ON_ONCE(!inode); |
9265 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
9266 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9267 | |
9268 | return work; | |
9269 | } | |
9270 | ||
9271 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
9272 | { | |
9273 | wait_for_completion(&work->completion); | |
9274 | kmem_cache_free(btrfs_delalloc_work_cachep, work); | |
9275 | } | |
9276 | ||
d352ac68 CM |
9277 | /* |
9278 | * some fairly slow code that needs optimization. This walks the list | |
9279 | * of all the inodes with pending delalloc and forces them to disk. | |
9280 | */ | |
6c255e67 MX |
9281 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
9282 | int nr) | |
ea8c2819 | 9283 | { |
ea8c2819 | 9284 | struct btrfs_inode *binode; |
5b21f2ed | 9285 | struct inode *inode; |
8ccf6f19 MX |
9286 | struct btrfs_delalloc_work *work, *next; |
9287 | struct list_head works; | |
1eafa6c7 | 9288 | struct list_head splice; |
8ccf6f19 | 9289 | int ret = 0; |
ea8c2819 | 9290 | |
8ccf6f19 | 9291 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9292 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9293 | |
573bfb72 | 9294 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9295 | spin_lock(&root->delalloc_lock); |
9296 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9297 | while (!list_empty(&splice)) { |
9298 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9299 | delalloc_inodes); |
1eafa6c7 | 9300 | |
eb73c1b7 MX |
9301 | list_move_tail(&binode->delalloc_inodes, |
9302 | &root->delalloc_inodes); | |
5b21f2ed | 9303 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9304 | if (!inode) { |
eb73c1b7 | 9305 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9306 | continue; |
df0af1a5 | 9307 | } |
eb73c1b7 | 9308 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 MX |
9309 | |
9310 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); | |
5d99a998 | 9311 | if (!work) { |
f4ab9ea7 JB |
9312 | if (delay_iput) |
9313 | btrfs_add_delayed_iput(inode); | |
9314 | else | |
9315 | iput(inode); | |
1eafa6c7 | 9316 | ret = -ENOMEM; |
a1ecaabb | 9317 | goto out; |
5b21f2ed | 9318 | } |
1eafa6c7 | 9319 | list_add_tail(&work->list, &works); |
a44903ab QW |
9320 | btrfs_queue_work(root->fs_info->flush_workers, |
9321 | &work->work); | |
6c255e67 MX |
9322 | ret++; |
9323 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 9324 | goto out; |
5b21f2ed | 9325 | cond_resched(); |
eb73c1b7 | 9326 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9327 | } |
eb73c1b7 | 9328 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9329 | |
a1ecaabb | 9330 | out: |
eb73c1b7 MX |
9331 | list_for_each_entry_safe(work, next, &works, list) { |
9332 | list_del_init(&work->list); | |
9333 | btrfs_wait_and_free_delalloc_work(work); | |
9334 | } | |
9335 | ||
9336 | if (!list_empty_careful(&splice)) { | |
9337 | spin_lock(&root->delalloc_lock); | |
9338 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9339 | spin_unlock(&root->delalloc_lock); | |
9340 | } | |
573bfb72 | 9341 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9342 | return ret; |
9343 | } | |
1eafa6c7 | 9344 | |
eb73c1b7 MX |
9345 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
9346 | { | |
9347 | int ret; | |
1eafa6c7 | 9348 | |
2c21b4d7 | 9349 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
9350 | return -EROFS; |
9351 | ||
6c255e67 MX |
9352 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
9353 | if (ret > 0) | |
9354 | ret = 0; | |
eb73c1b7 MX |
9355 | /* |
9356 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
9357 | * we have to make sure the IO is actually started and that |
9358 | * ordered extents get created before we return | |
9359 | */ | |
9360 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 9361 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 9362 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 9363 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
9364 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
9365 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
9366 | } |
9367 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
9368 | return ret; |
9369 | } | |
9370 | ||
6c255e67 MX |
9371 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
9372 | int nr) | |
eb73c1b7 MX |
9373 | { |
9374 | struct btrfs_root *root; | |
9375 | struct list_head splice; | |
9376 | int ret; | |
9377 | ||
2c21b4d7 | 9378 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9379 | return -EROFS; |
9380 | ||
9381 | INIT_LIST_HEAD(&splice); | |
9382 | ||
573bfb72 | 9383 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9384 | spin_lock(&fs_info->delalloc_root_lock); |
9385 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9386 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9387 | root = list_first_entry(&splice, struct btrfs_root, |
9388 | delalloc_root); | |
9389 | root = btrfs_grab_fs_root(root); | |
9390 | BUG_ON(!root); | |
9391 | list_move_tail(&root->delalloc_root, | |
9392 | &fs_info->delalloc_roots); | |
9393 | spin_unlock(&fs_info->delalloc_root_lock); | |
9394 | ||
6c255e67 | 9395 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 9396 | btrfs_put_fs_root(root); |
6c255e67 | 9397 | if (ret < 0) |
eb73c1b7 MX |
9398 | goto out; |
9399 | ||
6c255e67 MX |
9400 | if (nr != -1) { |
9401 | nr -= ret; | |
9402 | WARN_ON(nr < 0); | |
9403 | } | |
eb73c1b7 | 9404 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9405 | } |
eb73c1b7 | 9406 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9407 | |
6c255e67 | 9408 | ret = 0; |
eb73c1b7 MX |
9409 | atomic_inc(&fs_info->async_submit_draining); |
9410 | while (atomic_read(&fs_info->nr_async_submits) || | |
9411 | atomic_read(&fs_info->async_delalloc_pages)) { | |
9412 | wait_event(fs_info->async_submit_wait, | |
9413 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
9414 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
9415 | } | |
9416 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 9417 | out: |
1eafa6c7 | 9418 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
9419 | spin_lock(&fs_info->delalloc_root_lock); |
9420 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9421 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9422 | } |
573bfb72 | 9423 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9424 | return ret; |
ea8c2819 CM |
9425 | } |
9426 | ||
39279cc3 CM |
9427 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9428 | const char *symname) | |
9429 | { | |
9430 | struct btrfs_trans_handle *trans; | |
9431 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9432 | struct btrfs_path *path; | |
9433 | struct btrfs_key key; | |
1832a6d5 | 9434 | struct inode *inode = NULL; |
39279cc3 CM |
9435 | int err; |
9436 | int drop_inode = 0; | |
9437 | u64 objectid; | |
67871254 | 9438 | u64 index = 0; |
39279cc3 CM |
9439 | int name_len; |
9440 | int datasize; | |
5f39d397 | 9441 | unsigned long ptr; |
39279cc3 | 9442 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9443 | struct extent_buffer *leaf; |
39279cc3 | 9444 | |
f06becc4 | 9445 | name_len = strlen(symname); |
39279cc3 CM |
9446 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
9447 | return -ENAMETOOLONG; | |
1832a6d5 | 9448 | |
9ed74f2d JB |
9449 | /* |
9450 | * 2 items for inode item and ref | |
9451 | * 2 items for dir items | |
9452 | * 1 item for xattr if selinux is on | |
9453 | */ | |
a22285a6 YZ |
9454 | trans = btrfs_start_transaction(root, 5); |
9455 | if (IS_ERR(trans)) | |
9456 | return PTR_ERR(trans); | |
1832a6d5 | 9457 | |
581bb050 LZ |
9458 | err = btrfs_find_free_ino(root, &objectid); |
9459 | if (err) | |
9460 | goto out_unlock; | |
9461 | ||
aec7477b | 9462 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 9463 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 9464 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
9465 | if (IS_ERR(inode)) { |
9466 | err = PTR_ERR(inode); | |
39279cc3 | 9467 | goto out_unlock; |
7cf96da3 | 9468 | } |
39279cc3 | 9469 | |
ad19db71 CS |
9470 | /* |
9471 | * If the active LSM wants to access the inode during | |
9472 | * d_instantiate it needs these. Smack checks to see | |
9473 | * if the filesystem supports xattrs by looking at the | |
9474 | * ops vector. | |
9475 | */ | |
9476 | inode->i_fop = &btrfs_file_operations; | |
9477 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9478 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9479 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9480 | ||
9481 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9482 | if (err) | |
9483 | goto out_unlock_inode; | |
ad19db71 | 9484 | |
a1b075d2 | 9485 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 9486 | if (err) |
b0d5d10f | 9487 | goto out_unlock_inode; |
39279cc3 CM |
9488 | |
9489 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
9490 | if (!path) { |
9491 | err = -ENOMEM; | |
b0d5d10f | 9492 | goto out_unlock_inode; |
d8926bb3 | 9493 | } |
33345d01 | 9494 | key.objectid = btrfs_ino(inode); |
39279cc3 | 9495 | key.offset = 0; |
962a298f | 9496 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9497 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9498 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9499 | datasize); | |
54aa1f4d | 9500 | if (err) { |
b0839166 | 9501 | btrfs_free_path(path); |
b0d5d10f | 9502 | goto out_unlock_inode; |
54aa1f4d | 9503 | } |
5f39d397 CM |
9504 | leaf = path->nodes[0]; |
9505 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9506 | struct btrfs_file_extent_item); | |
9507 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9508 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9509 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9510 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9511 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9512 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9513 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9514 | ||
39279cc3 | 9515 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9516 | write_extent_buffer(leaf, symname, ptr, name_len); |
9517 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9518 | btrfs_free_path(path); |
5f39d397 | 9519 | |
39279cc3 CM |
9520 | inode->i_op = &btrfs_symlink_inode_operations; |
9521 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
d899e052 | 9522 | inode_set_bytes(inode, name_len); |
f06becc4 | 9523 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 9524 | err = btrfs_update_inode(trans, root, inode); |
b0d5d10f | 9525 | if (err) { |
54aa1f4d | 9526 | drop_inode = 1; |
b0d5d10f CM |
9527 | goto out_unlock_inode; |
9528 | } | |
9529 | ||
9530 | unlock_new_inode(inode); | |
9531 | d_instantiate(dentry, inode); | |
39279cc3 CM |
9532 | |
9533 | out_unlock: | |
7ad85bb7 | 9534 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
9535 | if (drop_inode) { |
9536 | inode_dec_link_count(inode); | |
9537 | iput(inode); | |
9538 | } | |
b53d3f5d | 9539 | btrfs_btree_balance_dirty(root); |
39279cc3 | 9540 | return err; |
b0d5d10f CM |
9541 | |
9542 | out_unlock_inode: | |
9543 | drop_inode = 1; | |
9544 | unlock_new_inode(inode); | |
9545 | goto out_unlock; | |
39279cc3 | 9546 | } |
16432985 | 9547 | |
0af3d00b JB |
9548 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9549 | u64 start, u64 num_bytes, u64 min_size, | |
9550 | loff_t actual_len, u64 *alloc_hint, | |
9551 | struct btrfs_trans_handle *trans) | |
d899e052 | 9552 | { |
5dc562c5 JB |
9553 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9554 | struct extent_map *em; | |
d899e052 YZ |
9555 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9556 | struct btrfs_key ins; | |
d899e052 | 9557 | u64 cur_offset = start; |
55a61d1d | 9558 | u64 i_size; |
154ea289 | 9559 | u64 cur_bytes; |
d899e052 | 9560 | int ret = 0; |
0af3d00b | 9561 | bool own_trans = true; |
d899e052 | 9562 | |
0af3d00b JB |
9563 | if (trans) |
9564 | own_trans = false; | |
d899e052 | 9565 | while (num_bytes > 0) { |
0af3d00b JB |
9566 | if (own_trans) { |
9567 | trans = btrfs_start_transaction(root, 3); | |
9568 | if (IS_ERR(trans)) { | |
9569 | ret = PTR_ERR(trans); | |
9570 | break; | |
9571 | } | |
5a303d5d YZ |
9572 | } |
9573 | ||
154ea289 CM |
9574 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
9575 | cur_bytes = max(cur_bytes, min_size); | |
00361589 | 9576 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
e570fd27 | 9577 | *alloc_hint, &ins, 1, 0); |
5a303d5d | 9578 | if (ret) { |
0af3d00b JB |
9579 | if (own_trans) |
9580 | btrfs_end_transaction(trans, root); | |
a22285a6 | 9581 | break; |
d899e052 | 9582 | } |
5a303d5d | 9583 | |
d899e052 YZ |
9584 | ret = insert_reserved_file_extent(trans, inode, |
9585 | cur_offset, ins.objectid, | |
9586 | ins.offset, ins.offset, | |
920bbbfb | 9587 | ins.offset, 0, 0, 0, |
d899e052 | 9588 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 9589 | if (ret) { |
857cc2fc | 9590 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 9591 | ins.offset, 0); |
79787eaa JM |
9592 | btrfs_abort_transaction(trans, root, ret); |
9593 | if (own_trans) | |
9594 | btrfs_end_transaction(trans, root); | |
9595 | break; | |
9596 | } | |
31193213 DY |
9597 | |
9598 | btrfs_qgroup_update_reserved_bytes(root->fs_info, | |
9599 | root->root_key.objectid, | |
9600 | ins.offset, 1); | |
9601 | ||
a1ed835e CM |
9602 | btrfs_drop_extent_cache(inode, cur_offset, |
9603 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 9604 | |
5dc562c5 JB |
9605 | em = alloc_extent_map(); |
9606 | if (!em) { | |
9607 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9608 | &BTRFS_I(inode)->runtime_flags); | |
9609 | goto next; | |
9610 | } | |
9611 | ||
9612 | em->start = cur_offset; | |
9613 | em->orig_start = cur_offset; | |
9614 | em->len = ins.offset; | |
9615 | em->block_start = ins.objectid; | |
9616 | em->block_len = ins.offset; | |
b4939680 | 9617 | em->orig_block_len = ins.offset; |
cc95bef6 | 9618 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9619 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
9620 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
9621 | em->generation = trans->transid; | |
9622 | ||
9623 | while (1) { | |
9624 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9625 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9626 | write_unlock(&em_tree->lock); |
9627 | if (ret != -EEXIST) | |
9628 | break; | |
9629 | btrfs_drop_extent_cache(inode, cur_offset, | |
9630 | cur_offset + ins.offset - 1, | |
9631 | 0); | |
9632 | } | |
9633 | free_extent_map(em); | |
9634 | next: | |
d899e052 YZ |
9635 | num_bytes -= ins.offset; |
9636 | cur_offset += ins.offset; | |
efa56464 | 9637 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9638 | |
0c4d2d95 | 9639 | inode_inc_iversion(inode); |
d899e052 | 9640 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 9641 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9642 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9643 | (actual_len > inode->i_size) && |
9644 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9645 | if (cur_offset > actual_len) |
55a61d1d | 9646 | i_size = actual_len; |
d1ea6a61 | 9647 | else |
55a61d1d JB |
9648 | i_size = cur_offset; |
9649 | i_size_write(inode, i_size); | |
9650 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
9651 | } |
9652 | ||
d899e052 | 9653 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9654 | |
9655 | if (ret) { | |
9656 | btrfs_abort_transaction(trans, root, ret); | |
9657 | if (own_trans) | |
9658 | btrfs_end_transaction(trans, root); | |
9659 | break; | |
9660 | } | |
d899e052 | 9661 | |
0af3d00b JB |
9662 | if (own_trans) |
9663 | btrfs_end_transaction(trans, root); | |
5a303d5d | 9664 | } |
d899e052 YZ |
9665 | return ret; |
9666 | } | |
9667 | ||
0af3d00b JB |
9668 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9669 | u64 start, u64 num_bytes, u64 min_size, | |
9670 | loff_t actual_len, u64 *alloc_hint) | |
9671 | { | |
9672 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9673 | min_size, actual_len, alloc_hint, | |
9674 | NULL); | |
9675 | } | |
9676 | ||
9677 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9678 | struct btrfs_trans_handle *trans, int mode, | |
9679 | u64 start, u64 num_bytes, u64 min_size, | |
9680 | loff_t actual_len, u64 *alloc_hint) | |
9681 | { | |
9682 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9683 | min_size, actual_len, alloc_hint, trans); | |
9684 | } | |
9685 | ||
e6dcd2dc CM |
9686 | static int btrfs_set_page_dirty(struct page *page) |
9687 | { | |
e6dcd2dc CM |
9688 | return __set_page_dirty_nobuffers(page); |
9689 | } | |
9690 | ||
10556cb2 | 9691 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9692 | { |
b83cc969 | 9693 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9694 | umode_t mode = inode->i_mode; |
b83cc969 | 9695 | |
cb6db4e5 JM |
9696 | if (mask & MAY_WRITE && |
9697 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9698 | if (btrfs_root_readonly(root)) | |
9699 | return -EROFS; | |
9700 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9701 | return -EACCES; | |
9702 | } | |
2830ba7f | 9703 | return generic_permission(inode, mask); |
fdebe2bd | 9704 | } |
39279cc3 | 9705 | |
ef3b9af5 FM |
9706 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9707 | { | |
9708 | struct btrfs_trans_handle *trans; | |
9709 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9710 | struct inode *inode = NULL; | |
9711 | u64 objectid; | |
9712 | u64 index; | |
9713 | int ret = 0; | |
9714 | ||
9715 | /* | |
9716 | * 5 units required for adding orphan entry | |
9717 | */ | |
9718 | trans = btrfs_start_transaction(root, 5); | |
9719 | if (IS_ERR(trans)) | |
9720 | return PTR_ERR(trans); | |
9721 | ||
9722 | ret = btrfs_find_free_ino(root, &objectid); | |
9723 | if (ret) | |
9724 | goto out; | |
9725 | ||
9726 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
9727 | btrfs_ino(dir), objectid, mode, &index); | |
9728 | if (IS_ERR(inode)) { | |
9729 | ret = PTR_ERR(inode); | |
9730 | inode = NULL; | |
9731 | goto out; | |
9732 | } | |
9733 | ||
ef3b9af5 FM |
9734 | inode->i_fop = &btrfs_file_operations; |
9735 | inode->i_op = &btrfs_file_inode_operations; | |
9736 | ||
9737 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9738 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9739 | ||
b0d5d10f CM |
9740 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9741 | if (ret) | |
9742 | goto out_inode; | |
9743 | ||
9744 | ret = btrfs_update_inode(trans, root, inode); | |
9745 | if (ret) | |
9746 | goto out_inode; | |
ef3b9af5 FM |
9747 | ret = btrfs_orphan_add(trans, inode); |
9748 | if (ret) | |
b0d5d10f | 9749 | goto out_inode; |
ef3b9af5 | 9750 | |
5762b5c9 FM |
9751 | /* |
9752 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9753 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9754 | * through: | |
9755 | * | |
9756 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9757 | */ | |
9758 | set_nlink(inode, 1); | |
b0d5d10f | 9759 | unlock_new_inode(inode); |
ef3b9af5 FM |
9760 | d_tmpfile(dentry, inode); |
9761 | mark_inode_dirty(inode); | |
9762 | ||
9763 | out: | |
9764 | btrfs_end_transaction(trans, root); | |
9765 | if (ret) | |
9766 | iput(inode); | |
9767 | btrfs_balance_delayed_items(root); | |
9768 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 9769 | return ret; |
b0d5d10f CM |
9770 | |
9771 | out_inode: | |
9772 | unlock_new_inode(inode); | |
9773 | goto out; | |
9774 | ||
ef3b9af5 FM |
9775 | } |
9776 | ||
b38ef71c FM |
9777 | /* Inspired by filemap_check_errors() */ |
9778 | int btrfs_inode_check_errors(struct inode *inode) | |
9779 | { | |
9780 | int ret = 0; | |
9781 | ||
9782 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
9783 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
9784 | ret = -ENOSPC; | |
9785 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
9786 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
9787 | ret = -EIO; | |
9788 | ||
9789 | return ret; | |
9790 | } | |
9791 | ||
6e1d5dcc | 9792 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 9793 | .getattr = btrfs_getattr, |
39279cc3 CM |
9794 | .lookup = btrfs_lookup, |
9795 | .create = btrfs_create, | |
9796 | .unlink = btrfs_unlink, | |
9797 | .link = btrfs_link, | |
9798 | .mkdir = btrfs_mkdir, | |
9799 | .rmdir = btrfs_rmdir, | |
80ace85c | 9800 | .rename2 = btrfs_rename2, |
39279cc3 CM |
9801 | .symlink = btrfs_symlink, |
9802 | .setattr = btrfs_setattr, | |
618e21d5 | 9803 | .mknod = btrfs_mknod, |
95819c05 CH |
9804 | .setxattr = btrfs_setxattr, |
9805 | .getxattr = btrfs_getxattr, | |
5103e947 | 9806 | .listxattr = btrfs_listxattr, |
95819c05 | 9807 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9808 | .permission = btrfs_permission, |
4e34e719 | 9809 | .get_acl = btrfs_get_acl, |
996a710d | 9810 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9811 | .update_time = btrfs_update_time, |
ef3b9af5 | 9812 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 9813 | }; |
6e1d5dcc | 9814 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 9815 | .lookup = btrfs_lookup, |
fdebe2bd | 9816 | .permission = btrfs_permission, |
4e34e719 | 9817 | .get_acl = btrfs_get_acl, |
996a710d | 9818 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9819 | .update_time = btrfs_update_time, |
39279cc3 | 9820 | }; |
76dda93c | 9821 | |
828c0950 | 9822 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
9823 | .llseek = generic_file_llseek, |
9824 | .read = generic_read_dir, | |
9cdda8d3 | 9825 | .iterate = btrfs_real_readdir, |
34287aa3 | 9826 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 9827 | #ifdef CONFIG_COMPAT |
34287aa3 | 9828 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 9829 | #endif |
6bf13c0c | 9830 | .release = btrfs_release_file, |
e02119d5 | 9831 | .fsync = btrfs_sync_file, |
39279cc3 CM |
9832 | }; |
9833 | ||
d1310b2e | 9834 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 9835 | .fill_delalloc = run_delalloc_range, |
065631f6 | 9836 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 9837 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 9838 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 9839 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 9840 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
9841 | .set_bit_hook = btrfs_set_bit_hook, |
9842 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
9843 | .merge_extent_hook = btrfs_merge_extent_hook, |
9844 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
9845 | }; |
9846 | ||
35054394 CM |
9847 | /* |
9848 | * btrfs doesn't support the bmap operation because swapfiles | |
9849 | * use bmap to make a mapping of extents in the file. They assume | |
9850 | * these extents won't change over the life of the file and they | |
9851 | * use the bmap result to do IO directly to the drive. | |
9852 | * | |
9853 | * the btrfs bmap call would return logical addresses that aren't | |
9854 | * suitable for IO and they also will change frequently as COW | |
9855 | * operations happen. So, swapfile + btrfs == corruption. | |
9856 | * | |
9857 | * For now we're avoiding this by dropping bmap. | |
9858 | */ | |
7f09410b | 9859 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
9860 | .readpage = btrfs_readpage, |
9861 | .writepage = btrfs_writepage, | |
b293f02e | 9862 | .writepages = btrfs_writepages, |
3ab2fb5a | 9863 | .readpages = btrfs_readpages, |
16432985 | 9864 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
9865 | .invalidatepage = btrfs_invalidatepage, |
9866 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 9867 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 9868 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
9869 | }; |
9870 | ||
7f09410b | 9871 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
9872 | .readpage = btrfs_readpage, |
9873 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
9874 | .invalidatepage = btrfs_invalidatepage, |
9875 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
9876 | }; |
9877 | ||
6e1d5dcc | 9878 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
9879 | .getattr = btrfs_getattr, |
9880 | .setattr = btrfs_setattr, | |
95819c05 CH |
9881 | .setxattr = btrfs_setxattr, |
9882 | .getxattr = btrfs_getxattr, | |
5103e947 | 9883 | .listxattr = btrfs_listxattr, |
95819c05 | 9884 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9885 | .permission = btrfs_permission, |
1506fcc8 | 9886 | .fiemap = btrfs_fiemap, |
4e34e719 | 9887 | .get_acl = btrfs_get_acl, |
996a710d | 9888 | .set_acl = btrfs_set_acl, |
e41f941a | 9889 | .update_time = btrfs_update_time, |
39279cc3 | 9890 | }; |
6e1d5dcc | 9891 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
9892 | .getattr = btrfs_getattr, |
9893 | .setattr = btrfs_setattr, | |
fdebe2bd | 9894 | .permission = btrfs_permission, |
95819c05 CH |
9895 | .setxattr = btrfs_setxattr, |
9896 | .getxattr = btrfs_getxattr, | |
33268eaf | 9897 | .listxattr = btrfs_listxattr, |
95819c05 | 9898 | .removexattr = btrfs_removexattr, |
4e34e719 | 9899 | .get_acl = btrfs_get_acl, |
996a710d | 9900 | .set_acl = btrfs_set_acl, |
e41f941a | 9901 | .update_time = btrfs_update_time, |
618e21d5 | 9902 | }; |
6e1d5dcc | 9903 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 CM |
9904 | .readlink = generic_readlink, |
9905 | .follow_link = page_follow_link_light, | |
9906 | .put_link = page_put_link, | |
f209561a | 9907 | .getattr = btrfs_getattr, |
22c44fe6 | 9908 | .setattr = btrfs_setattr, |
fdebe2bd | 9909 | .permission = btrfs_permission, |
0279b4cd JO |
9910 | .setxattr = btrfs_setxattr, |
9911 | .getxattr = btrfs_getxattr, | |
9912 | .listxattr = btrfs_listxattr, | |
9913 | .removexattr = btrfs_removexattr, | |
e41f941a | 9914 | .update_time = btrfs_update_time, |
39279cc3 | 9915 | }; |
76dda93c | 9916 | |
82d339d9 | 9917 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 9918 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 9919 | .d_release = btrfs_dentry_release, |
76dda93c | 9920 | }; |